I’m making a plugin for punctuation completion, which executes well in English, but there are always some unpredictable problems with Chinese punctuation execution, I guess it’s related to rendering speed? When I make a break point in range.setEndBefore(newQuote);
When this line of code, punctuation completion in the Chinese state will be performed very well, I am very confused, can you point it out
const quotes: { [key: string]: string } = {
"(": ")",
"【": "】",
"《": "》",
"『": "』",
"“": "”",
"‘": "’",
"(": ")",
"[": "]",
"「": "」",
"{": "}",
'"': '"',
"'": "'",
"”": "“",
"’": "‘"
};
const quotes_reverse = ["”", "’"]; // 左移补全
export const autoPairPunctuationPlugin = new Plugin({
props: {
handleDOMEvents: {
input(view: any, event: any) {
const quote = quotes[event.data];
const selection = document.getSelection();
if (quote && (event.inputType === "insertText" || "insertCompositionText")) {
const newQuote = document.createTextNode(quote);
const range = selection.getRangeAt(0);
const reverse = quotes_reverse.includes(event.data);
if (reverse) {
const { startContainer, startOffset, endContainer, endOffset } = range;
range.setStart(startContainer, startOffset - 1);
range.setEnd(endContainer, endOffset - 1);
}
range.insertNode(newQuote);
if (reverse) {
range.setStartAfter(newQuote);
} else {
range.setEndBefore(newQuote);
}
range.commonAncestorContainer.normalize();
}
return true;
}
}
}
});