Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

refactor(ui):Refactor wrapText method

robine28a54a42846
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 = (