Fix setting of caret when creating clozes (#1688)

This commit is contained in:
Henrik Giesel 2022-02-25 02:07:44 +01:00 committed by GitHub
parent 0d83581ab0
commit 7c27159149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,13 +8,11 @@ function wrappedExceptForWhitespace(text: string, front: string, back: string):
return match[1] + front + match[2] + back + match[3]; return match[1] + front + match[2] + back + match[3];
} }
function moveCursorPastPostfix( function moveCursorInside(selection: Selection, postfix: string): void {
selection: Selection, const range = getRange(selection)!;
range: Range,
postfix: string, range.setEnd(range.endContainer, range.endOffset - postfix.length);
): void { range.collapse(false);
range.setStart(range.startContainer, range.startOffset - postfix.length);
range.collapse(true);
selection.removeAllRanges(); selection.removeAllRanges();
selection.addRange(range); selection.addRange(range);
@ -33,6 +31,7 @@ export function wrapInternal(
return; return;
} }
const wasCollapsed = range.collapsed;
const content = range.cloneContents(); const content = range.cloneContents();
const span = document.createElement("span"); const span = document.createElement("span");
span.appendChild(content); span.appendChild(content);
@ -46,11 +45,11 @@ export function wrapInternal(
} }
if ( if (
!span.innerHTML && wasCollapsed &&
/* ugly solution: treat <anki-mathjax> differently than other wraps */ !front.includes( /* ugly solution: treat <anki-mathjax> differently than other wraps */ !front.includes(
"<anki-mathjax", "<anki-mathjax",
) )
) { ) {
moveCursorPastPostfix(selection, range, back); moveCursorInside(selection, back);
} }
} }