Skip to content

Commit 905b9aa

Browse files
committed
animate next line shifting when extra letters are typed
1 parent 2dab48a commit 905b9aa

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

frontend/src/ts/test/test-ui.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ export async function updateWordsInputPosition(initial = false): Promise<void> {
484484
}
485485

486486
function updateTapeModeLine(): void {
487-
if (currentTestLine === 0) return;
488487
const wordElements = document.querySelectorAll<HTMLElement>("#words .word");
489488
const currentTop = wordElements[activeWordElementIndex]?.offsetTop ?? 0;
490489

@@ -968,38 +967,42 @@ export function scrollTape(): void {
968967
let widthToHide = 0;
969968
let wordsToHideCount = 0;
970969
let leadingNewLine = 0;
971-
let lastAfterNewLineElement = undefined;
972970
const linesWidths: number[] = [];
973971
const toHide: HTMLElement[] = [];
974972

975973
// remove leading `.newline` and `.afterNewline` elements
976974
for (const child of wordsChildrenArr) {
977975
if (child.classList.contains("word")) {
978-
if (lastAfterNewLineElement) {
979-
widthToHide += parseInt(lastAfterNewLineElement.style.marginLeft);
980-
}
981976
break;
982977
} else if (child.classList.contains("newline")) {
983978
toHide.push(child);
984979
} else if (child.classList.contains("afterNewline")) {
985980
toHide.push(child);
981+
widthToHide += parseInt(child.style.marginLeft);
986982
leadingNewLine = 1;
987-
lastAfterNewLineElement = child;
988983
}
989984
}
990985
// get last element to loop over
991986
const activeWordIndex = wordsChildrenArr.indexOf(activeWordEl);
992987
const newLinesBeforeActiveWord = wordsChildrenArr
993988
.slice(0, activeWordIndex)
994989
.filter((child) => child.classList.contains("afterNewline")).length;
995-
const lastVisibleAfterNewline = afterNewLineEls[newLinesBeforeActiveWord] as
996-
| HTMLElement
997-
| undefined;
990+
// the second .afterNewline after active word is visible during line jump
991+
let lastVisibleAfterNewline = afterNewLineEls[
992+
newLinesBeforeActiveWord + 1
993+
] as HTMLElement | undefined;
998994
let lastElementIndex;
999995
if (lastVisibleAfterNewline) {
1000996
lastElementIndex = wordsChildrenArr.indexOf(lastVisibleAfterNewline);
1001997
} else {
1002-
lastElementIndex = activeWordIndex - 1;
998+
lastVisibleAfterNewline = afterNewLineEls[newLinesBeforeActiveWord] as
999+
| HTMLElement
1000+
| undefined;
1001+
if (lastVisibleAfterNewline) {
1002+
lastElementIndex = wordsChildrenArr.indexOf(lastVisibleAfterNewline);
1003+
} else {
1004+
lastElementIndex = activeWordIndex - 1;
1005+
}
10031006
}
10041007

10051008
const wordRightMargin = parseInt(
@@ -1037,11 +1040,11 @@ export function scrollTape(): void {
10371040
toHide.forEach((el) => el.remove());
10381041
const currentMargin = parseInt(wordsEl.style.marginLeft);
10391042
wordsEl.style.marginLeft = `${currentMargin + widthToHide}px`;
1040-
}
1041-
for (let i = 0; i < linesWidths.length; i++) {
1042-
(
1043-
afterNewLineEls[i] as HTMLElement
1044-
).style.marginLeft = `${linesWidths[i]}px`;
1043+
for (let i = 0; i < linesWidths.length; i++) {
1044+
const element = afterNewLineEls[i] as HTMLElement;
1045+
const currentMargin = parseInt(element.style.marginLeft);
1046+
element.style.marginLeft = `${currentMargin - widthToHide}px`;
1047+
}
10451048
}
10461049

10471050
let currentWordWidth = 0;
@@ -1074,8 +1077,21 @@ export function scrollTape(): void {
10741077
}
10751078
);
10761079
jqWords.dequeue("leftMargin");
1080+
linesWidths.forEach((width, index) => {
1081+
$(afterNewLineEls[index] as Element)
1082+
.stop(true, false)
1083+
.animate(
1084+
{
1085+
marginLeft: width,
1086+
},
1087+
SlowTimer.get() ? 0 : 125
1088+
);
1089+
});
10771090
} else {
10781091
wordsEl.style.marginLeft = `${newMargin}px`;
1092+
linesWidths.forEach((width, index) => {
1093+
(afterNewLineEls[index] as HTMLElement).style.marginLeft = `${width}px`;
1094+
});
10791095
}
10801096
}
10811097

0 commit comments

Comments
 (0)