From 2b3691e7cc1831e5a7cb9c8fc3ccbfee4a6c269d Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Tue, 18 May 2021 01:37:19 -0400 Subject: [PATCH] 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 --- .../js/src/common/utils/BasicEditorDriver.ts | 6 +++--- .../core/js/src/common/utils/insertText.ts | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/framework/core/js/src/common/utils/BasicEditorDriver.ts b/framework/core/js/src/common/utils/BasicEditorDriver.ts index 077853863..a57bf27c9 100644 --- a/framework/core/js/src/common/utils/BasicEditorDriver.ts +++ b/framework/core/js/src/common/utils/BasicEditorDriver.ts @@ -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) { diff --git a/framework/core/js/src/common/utils/insertText.ts b/framework/core/js/src/common/utils/insertText.ts index 5e44ec637..4f8377c70 100644 --- a/framework/core/js/src/common/utils/insertText.ts +++ b/framework/core/js/src/common/utils/insertText.ts @@ -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'; - canInsertText = document.execCommand('insertText', false, text); + 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); + } }