Skip to content

Commit f198b98

Browse files
committed
combine words and splice in place
1 parent 44dd85a commit f198b98

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

css/style.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ svg text {
2828
margin-top:30px;
2929
margin-bottom: 210px;
3030
}
31-
#drawing.bracket-left{
31+
#drawing.bracket-left, #drawing.bracket-left rect {
3232
cursor:e-resize !important;
3333
}
34-
#drawing.bracket-right {
34+
#drawing.bracket-right, #drawing.bracket-right rect {
3535
cursor:w-resize !important;
3636
}
3737

js/wordcollapser.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)