@@ -41,6 +41,7 @@ const WordCollapser = (function() {
4141 // selecting left word
4242 if ( leftWord === null ) {
4343 leftWord = word ;
44+ listenForRightWord ( ) ;
4445 console . log ( 'left' , word ) ;
4546 }
4647 // selected words in the wrong order
@@ -50,27 +51,34 @@ const WordCollapser = (function() {
5051 }
5152 // select second word
5253 else {
53- console . log ( 'right' , leftWord , word ) ;
54-
55- let text = "" ;
56- for ( let i = leftWord . idx ; i <= word . idx ; ++ i ) {
57- if ( i > leftWord . idx ) {
58- text += ' ' ;
59- }
60- text += wordObjs [ i ] . val ;
54+ console . log ( 'right' , word ) ;
55+
56+ let lIndex = wordObjs . indexOf ( leftWord ) ;
57+ let rIndex = wordObjs . indexOf ( word ) ;
58+ let text = leftWord . val ;
59+ for ( let i = lIndex + 1 ; i <= rIndex ; ++ i ) {
60+ text += ' ' + wordObjs [ i ] . val ;
61+ }
62+
63+ // condense text if too long
64+ if ( text . length > 25 ) {
65+ text = text . slice ( 0 , 12 ) + "…" + text . slice ( - 12 ) ;
6166 }
6267
6368 let phrase = new Word ( text , leftWord . idx ) ;
6469
6570 let row = leftWord . row ;
6671
67- wordObjs . splice ( wordObjs . indexOf ( leftWord ) , 1 , phrase ) ;
68- row . words . splice ( row . words . indexOf ( leftWord ) , 1 , phrase ) ;
72+ let numberToSplice = 1 + rIndex - lIndex ;
73+
74+ // todo: correct number over multi rows
75+ let removedWords = wordObjs . splice ( lIndex , numberToSplice , phrase ) ;
76+ row . words . splice ( row . words . indexOf ( leftWord ) , numberToSplice , phrase ) ;
6977
7078 phrase . leftX = leftWord . leftX ;
7179 phrase . row = row ;
7280 phrase . draw ( ) ;
73- leftWord . svg . hide ( ) ;
81+ removedWords . forEach ( word => word . svg . hide ( ) ) ;
7482 cancel ( ) ;
7583 }
7684 }
0 commit comments