This is an automated email from the ASF dual-hosted git repository.

robin0716 pushed a commit to branch refactor/editor
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git

commit e28a54a42846c90a729a11d41872b3ee8a8514f5
Author: robin <[email protected]>
AuthorDate: Tue Apr 16 11:57:13 2024 +0800

    refactor(ui):Refactor wrapText method
---
 ui/src/components/Editor/utils/extension.ts | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/ui/src/components/Editor/utils/extension.ts 
b/ui/src/components/Editor/utils/extension.ts
index 84687e84..4118fed4 100644
--- a/ui/src/components/Editor/utils/extension.ts
+++ b/ui/src/components/Editor/utils/extension.ts
@@ -159,11 +159,21 @@ const createEditorUtils = (editor: Editor) => {
   editor.wrapText = (before: string, after = before, defaultText) => {
     const range = editor.state.selection.ranges[0];
     const selection = editor.state.sliceDoc(range.from, range.to);
-    if (selection) {
-      editor.replaceSelection(`${before}${selection}${after}`);
-    } else {
-      editor.replaceSelection(`${before}${defaultText}${after}`);
-    }
+    const text = `${before}${selection || defaultText}${after}`;
+
+    editor.dispatch({
+      changes: [
+        {
+          from: range.from,
+          to: range.to,
+          insert: text,
+        },
+      ],
+      selection: EditorSelection.range(
+        range.from + before.length,
+        range.to + before.length,
+      ),
+    });
   };
 
   editor.replaceLines = (

Reply via email to