mirror of
https://github.com/flarum/core.git
synced 2025-07-17 14:51:19 +02:00
Fix insertText
In 60dea59815
, 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:
@@ -78,10 +78,10 @@ export default class BasicEditorDriver implements EditorDriverInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
insertBetween(selectionStart: number, selectionEnd: number, text: string) {
|
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.
|
const cursorPos = selectionStart + text.length;
|
||||||
this.moveCursorTo(selectionStart + text.length);
|
insertText(this.el, { text, selectionStart: cursorPos, selectionEnd: cursorPos });
|
||||||
}
|
}
|
||||||
|
|
||||||
replaceBeforeCursor(start: number, text: string) {
|
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 before = textarea.value.slice(0, originalSelectionStart);
|
||||||
const after = textarea.value.slice(textarea.selectionEnd);
|
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) {
|
if (canInsertText === null || canInsertText === true) {
|
||||||
textarea.contentEditable = 'true';
|
textarea.contentEditable = 'true';
|
||||||
canInsertText = document.execCommand('insertText', false, text);
|
try {
|
||||||
|
canInsertText = document.execCommand('insertText', false, text);
|
||||||
|
} catch (error) {
|
||||||
|
canInsertText = false;
|
||||||
|
}
|
||||||
textarea.contentEditable = 'false';
|
textarea.contentEditable = 'false';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,4 +34,10 @@ export default function insertText(textarea: HTMLTextAreaElement, { text, select
|
|||||||
textarea.value = before + text + after;
|
textarea.value = before + text + after;
|
||||||
textarea.dispatchEvent(new CustomEvent('input', { bubbles: true, cancelable: true }));
|
textarea.dispatchEvent(new CustomEvent('input', { bubbles: true, cancelable: true }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (selectionStart != null && selectionEnd != null) {
|
||||||
|
textarea.setSelectionRange(selectionStart, selectionEnd);
|
||||||
|
} else {
|
||||||
|
textarea.setSelectionRange(originalSelectionStart, textarea.selectionEnd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user