Skip to content

Commit

Permalink
impr(caret): better placement, cleaned up some animations, fix right …
Browse files Browse the repository at this point in the history
…to left issues
  • Loading branch information
Miodec committed Feb 22, 2024
1 parent b03ea66 commit 8b0c614
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
72 changes: 53 additions & 19 deletions frontend/src/ts/test/caret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,39 @@ export function hide(): void {
caret.classList.add("hidden");
}

export async function updatePosition(): Promise<void> {
function getTargetPositionLeft(
fullWidthCaret: boolean,
isLanguageRightToLeft: boolean,
currentLetter: HTMLElement | undefined,
previousLetter: HTMLElement | undefined
): number {
let result = 0;

if (isLanguageRightToLeft) {
const fullWidthOffset = fullWidthCaret
? 0
: currentLetter?.offsetWidth ?? previousLetter?.offsetWidth ?? 0;
if (currentLetter !== undefined) {
result = currentLetter.offsetLeft + fullWidthOffset;
} else if (previousLetter !== undefined) {
result =
previousLetter.offsetLeft -
previousLetter.offsetWidth +
fullWidthOffset;
}
} else {
if (currentLetter !== undefined) {
result = currentLetter.offsetLeft;
} else if (previousLetter !== undefined) {
result = previousLetter.offsetLeft + previousLetter.offsetWidth;
}
}

return result;
}

export async function updatePosition(noAnim = false): Promise<void> {
console.trace();
const caretWidth = Math.round(
document.querySelector("#caret")?.getBoundingClientRect().width ?? 0
);
Expand Down Expand Up @@ -63,22 +95,26 @@ export async function updatePosition(): Promise<void> {

const currentLanguage = await Misc.getCurrentLanguage(Config.language);
const isLanguageRightToLeft = currentLanguage.rightToLeft;
const letterPosLeft =
(currentLetter !== undefined
? currentLetter.offsetLeft
: previousLetter.offsetLeft + previousLetter.offsetWidth) +
(!isLanguageRightToLeft
? 0
: currentLetter
? currentLetter.offsetWidth
: -previousLetter.offsetWidth);
const letterPosLeft = getTargetPositionLeft(
fullWidthCaret,
isLanguageRightToLeft,
currentLetter,
previousLetter
);

const letterPosTop = currentLetter
? currentLetter.offsetTop
: previousLetter.offsetTop;

const newTop =
letterPosTop - Config.fontSize * Misc.convertRemToPixels(1) * 0.1;
const letterHeight =
currentLetter?.offsetHeight ??
previousLetter?.offsetHeight ??
Config.fontSize * Misc.convertRemToPixels(1);

const diff = letterHeight - caret.offsetHeight;

const newTop = letterPosTop + diff / 2;

let newLeft = letterPosLeft - (fullWidthCaret ? 0 : caretWidth / 2);

const wordsWrapperWidth =
Expand Down Expand Up @@ -139,7 +175,7 @@ export async function updatePosition(): Promise<void> {

jqcaret
.stop(true, false)
.animate(animation, !SlowTimer.get() ? smoothCaretSpeed : 0);
.animate(animation, SlowTimer.get() || noAnim ? 0 : smoothCaretSpeed);

if (Config.showAllLines) {
const browserHeight = window.innerHeight;
Expand All @@ -164,10 +200,8 @@ export async function updatePosition(): Promise<void> {
}
}

export function show(): void {
if ($("#result").hasClass("hidden")) {
caret.classList.remove("hidden");
void updatePosition();
startAnimation();
}
export function show(noAnim = false): void {
caret.classList.remove("hidden");
void updatePosition(noAnim);
startAnimation();
}
2 changes: 1 addition & 1 deletion frontend/src/ts/test/test-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ $("#wordsInput").on("focus", () => {
if (!resultVisible && Config.showOutOfFocusWarning) {
OutOfFocus.hide();
}
Caret.show();
Caret.show(true);
});

$("#wordsInput").on("focusout", () => {
Expand Down

0 comments on commit 8b0c614

Please sign in to comment.