Skip to content

Commit 196a579

Browse files
authored
fix: shifting in multi-line tape (@nadalaba) (#7832)
**commit 1:** horizontal shift <img width="1280" height="450" alt="horizontalShift" src="https://github.com/user-attachments/assets/806c12da-7f3f-40b5-8185-346e7d785392" /> **commit 3:** vertical shift <img width="840" height="400" alt="verticalShift" src="https://github.com/user-attachments/assets/d64c5bed-c9a8-4b03-9d49-d1f1b69d3468" />
1 parent 9504dcd commit 196a579

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,25 @@ export function updateActiveElement(
169169
activeWordTop = newActiveWord.getOffsetTop();
170170
activeWordHeight = newActiveWord.getOffsetHeight();
171171

172-
updateWordsInputPosition();
173-
174-
if (previousActiveWordTop === null) return;
175-
176-
const isTimedTest =
177-
Config.mode === "time" ||
178-
(Config.mode === "custom" && CustomText.getLimitMode() === "time") ||
179-
(Config.mode === "custom" && CustomText.getLimitValue() === 0);
172+
if (previousActiveWordTop !== null) {
173+
const isTimedTest =
174+
Config.mode === "time" ||
175+
(Config.mode === "custom" && CustomText.getLimitMode() === "time") ||
176+
(Config.mode === "custom" && CustomText.getLimitValue() === 0);
180177

181-
if (isTimedTest || !Config.showAllLines) {
182-
const newActiveWordTop = newActiveWord.getOffsetTop();
183-
if (newActiveWordTop > previousActiveWordTop) {
184-
await lineJump(previousActiveWordTop);
178+
if (isTimedTest || !Config.showAllLines) {
179+
const newActiveWordTop = newActiveWord.getOffsetTop();
180+
if (newActiveWordTop > previousActiveWordTop) {
181+
await lineJump(previousActiveWordTop);
182+
}
185183
}
186184
}
187185

188186
if (!initial && Config.tapeMode !== "off") {
189187
await scrollTape();
190188
}
189+
190+
updateWordsInputPosition();
191191
});
192192
}
193193

@@ -953,6 +953,7 @@ export async function scrollTape(noAnimation = false): Promise<void> {
953953
const widthRemovedFromLine: number[] = [];
954954
const afterNewlinesNewMargins: number[] = [];
955955
const toRemove: ElementWithUtils[] = [];
956+
let removedAfterNewlines = 0;
956957

957958
/* remove leading `.afterNewline` elements */
958959
for (const child of wordsChildrenArr) {
@@ -968,6 +969,7 @@ export async function scrollTape(noAnimation = false): Promise<void> {
968969
toRemove.push(child);
969970
leadingNewLine = true;
970971
lastAfterNewLineElement = child;
972+
removedAfterNewlines++;
971973
}
972974
}
973975

@@ -976,7 +978,7 @@ export async function scrollTape(noAnimation = false): Promise<void> {
976978
// index of the active word in all #words.children
977979
// (which contains .word/.newline/.beforeNewline/.afterNewline elements)
978980
const activeWordIndex = wordsChildrenArr.indexOf(activeWordEl);
979-
// this will be 0 or 1
981+
// this will between 0 and 2
980982
const newLinesBeforeActiveWord = wordsChildrenArr
981983
.slice(0, activeWordIndex)
982984
.filter((child) => child.hasClass("afterNewline")).length;
@@ -1002,16 +1004,12 @@ export async function scrollTape(noAnimation = false): Promise<void> {
10021004
const child = wordsChildrenArr[i] as ElementWithUtils;
10031005
if (child.hasClass("word")) {
10041006
leadingNewLine = false;
1005-
const childComputedStyle = window.getComputedStyle(child.native);
1006-
const wordOuterWidth =
1007-
child.getOffsetWidth() +
1008-
parseFloat(childComputedStyle.marginRight) +
1009-
parseFloat(childComputedStyle.marginLeft);
1010-
const forWordLeft = Math.floor(child.getOffsetLeft());
1011-
const forWordWidth = Math.floor(child.getOffsetWidth());
1007+
const wordOuterWidth = child.getOuterWidth();
1008+
const wordLeft = Math.floor(child.getOffsetLeft());
1009+
const wordWidth = Math.floor(child.getOffsetWidth());
10121010
if (
1013-
(!isTestRightToLeft && forWordLeft < 0 - forWordWidth) ||
1014-
(isTestRightToLeft && forWordLeft > wordsWrapperWidth)
1011+
(!isTestRightToLeft && wordLeft < 0 - wordWidth) ||
1012+
(isTestRightToLeft && wordLeft > wordsWrapperWidth)
10151013
) {
10161014
toRemove.push(child);
10171015
widthRemoved += wordOuterWidth;
@@ -1049,6 +1047,7 @@ export async function scrollTape(noAnimation = false): Promise<void> {
10491047
/* remove overflown elements */
10501048
if (toRemove.length > 0) {
10511049
for (const el of toRemove) el.remove();
1050+
afterNewLineEls.splice(0, removedAfterNewlines);
10521051
for (let i = 0; i < widthRemovedFromLine.length; i++) {
10531052
const afterNewlineEl = afterNewLineEls[i] as ElementWithUtils;
10541053
const currentLineIndent =

0 commit comments

Comments
 (0)