Refactor wrap code
This commit is contained in:
parent
d10383ea9d
commit
a90931b91e
@ -5,11 +5,19 @@ 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(selection: Selection, postfix: string): void {
|
||||||
|
const range = selection.getRangeAt(0);
|
||||||
|
range.setStart(range.startContainer, range.startOffset - postfix.length);
|
||||||
|
range.collapse(true);
|
||||||
|
selection.removeAllRanges();
|
||||||
|
selection.addRange(range);
|
||||||
|
}
|
||||||
|
|
||||||
function wrapInternal(front: string, back: string, plainText: boolean): void {
|
function wrapInternal(front: string, back: string, plainText: boolean): void {
|
||||||
const currentField = getCurrentField()!;
|
const currentField = getCurrentField()!;
|
||||||
const s = currentField.getSelection();
|
const selection = currentField.getSelection();
|
||||||
let r = s.getRangeAt(0);
|
const range = selection.getRangeAt(0);
|
||||||
const content = r.cloneContents();
|
const content = range.cloneContents();
|
||||||
const span = document.createElement("span");
|
const span = document.createElement("span");
|
||||||
span.appendChild(content);
|
span.appendChild(content);
|
||||||
|
|
||||||
@ -22,12 +30,7 @@ function wrapInternal(front: string, back: string, plainText: boolean): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!span.innerHTML) {
|
if (!span.innerHTML) {
|
||||||
// run with an empty selection; move cursor back past postfix
|
moveCursorPastPostfix(selection, back);
|
||||||
r = s.getRangeAt(0);
|
|
||||||
r.setStart(r.startContainer, r.startOffset - back.length);
|
|
||||||
r.collapse(true);
|
|
||||||
s.removeAllRanges();
|
|
||||||
s.addRange(r);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user