mirror of
https://github.com/flarum/framework.git
synced 2025-02-22 22:07:57 +08:00
Fix insertText
In 60dea59815e10d453421117fa681a6474eb29ec8, insertText was modified from the original to work with reply mentioning. This was done due to a misunderstanding of the API: the selection range isn't the selection to replace, but rather the final selection state after replacing the *current* selection with the text. This commit restores the original, correct implementation of insertText and instead adjusts the `insertBetween`method of BasicEditorDriver to set selection state before executing `insertText`. Fixes https://github.com/flarum/core/issues/2877
This commit is contained in:
parent
c39724eae2
commit
2b3691e7cc
@ -78,10 +78,10 @@ export default class BasicEditorDriver implements EditorDriverInterface {
|
||||
}
|
||||
|
||||
insertBetween(selectionStart: number, selectionEnd: number, text: string) {
|
||||
insertText(this.el, { text, selectionStart, selectionEnd });
|
||||
this.setSelectionRange(selectionStart, selectionEnd);
|
||||
|
||||
// Move the textarea cursor to the end of the content we just inserted.
|
||||
this.moveCursorTo(selectionStart + text.length);
|
||||
const cursorPos = selectionStart + text.length;
|
||||
insertText(this.el, { text, selectionStart: cursorPos, selectionEnd: cursorPos });
|
||||
}
|
||||
|
||||
replaceBeforeCursor(start: number, text: string) {
|
||||
|
@ -16,16 +16,13 @@ export default function insertText(textarea: HTMLTextAreaElement, { text, select
|
||||
const before = textarea.value.slice(0, originalSelectionStart);
|
||||
const after = textarea.value.slice(textarea.selectionEnd);
|
||||
|
||||
if (selectionStart != null && selectionEnd != null) {
|
||||
textarea.setSelectionRange(selectionStart, selectionEnd + 1);
|
||||
} else {
|
||||
textarea.setSelectionRange(originalSelectionStart, textarea.selectionEnd);
|
||||
}
|
||||
textarea.focus();
|
||||
|
||||
if (canInsertText === null || canInsertText === true) {
|
||||
textarea.contentEditable = 'true';
|
||||
try {
|
||||
canInsertText = document.execCommand('insertText', false, text);
|
||||
} catch (error) {
|
||||
canInsertText = false;
|
||||
}
|
||||
textarea.contentEditable = 'false';
|
||||
}
|
||||
|
||||
@ -37,4 +34,10 @@ export default function insertText(textarea: HTMLTextAreaElement, { text, select
|
||||
textarea.value = before + text + after;
|
||||
textarea.dispatchEvent(new CustomEvent('input', { bubbles: true, cancelable: true }));
|
||||
}
|
||||
|
||||
if (selectionStart != null && selectionEnd != null) {
|
||||
textarea.setSelectionRange(selectionStart, selectionEnd);
|
||||
} else {
|
||||
textarea.setSelectionRange(originalSelectionStart, textarea.selectionEnd);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user