Просмотр исходного кода

134164 insert prefix to above line

soumaeda 2 лет назад
Родитель
Сommit
4c04f84452

+ 6 - 6
packages/editor/src/components/CodeMirrorEditor/Toolbar/TextFormatTools.tsx

@@ -49,8 +49,8 @@ export const TextFormatTools = (props: TextFormatToolsType): JSX.Element => {
     codeMirrorEditor?.insertMarkdownElements(prefix, suffix);
   };
 
-  const onClickInsertPrefix = (prefix: string, isContinuous: boolean) => {
-    codeMirrorEditor?.insertPrefix(prefix, isContinuous);
+  const onClickInsertPrefix = (prefix: string, noSpaceIfPrefixExists?: boolean) => {
+    codeMirrorEditor?.insertPrefix(prefix, noSpaceIfPrefixExists);
   };
 
   return (
@@ -74,16 +74,16 @@ export const TextFormatTools = (props: TextFormatToolsType): JSX.Element => {
           <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertMarkdownElements('`', '`')}>
             <span className="material-symbols-outlined fs-5">code</span>
           </button>
-          <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertPrefix('-', false)}>
+          <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertPrefix('-', undefined)}>
             <span className="material-symbols-outlined fs-5">format_list_bulleted</span>
           </button>
-          <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertPrefix('1.', false)}>
+          <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertPrefix('1.', undefined)}>
             <span className="material-symbols-outlined fs-5">format_list_numbered</span>
           </button>
-          <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertPrefix('>', false)}>
+          <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertPrefix('>', undefined)}>
             <span className="material-symbols-outlined fs-5">block</span>
           </button>
-          <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertPrefix('- [ ]', false)}>
+          <button type="button" className="btn btn-toolbar-button" onClick={() => onClickInsertPrefix('- [ ]', undefined)}>
             <span className="material-symbols-outlined fs-5">checklist</span>
           </button>
         </div>

+ 5 - 6
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-prefix.ts

@@ -2,27 +2,26 @@ import { useCallback } from 'react';
 
 import { EditorView } from '@codemirror/view';
 
-export type InsertPrefix = (prefix: string, flag: boolean) => void;
+export type InsertPrefix = (prefix: string, noSpaceIfPrefixExists?: boolean) => void;
 
 export const useInsertPrefix = (view?: EditorView): InsertPrefix => {
 
-  return useCallback((prefix: string, isContinuous: boolean) => {
+  return useCallback((prefix: string, noSpaceIfPrefixExists = false) => {
     if (view == null) {
       return;
     }
 
-    const cursorPos = view.state.selection.main.head;
+    const cursorPos = view.state.selection.main.from;
     const space = ' ';
     const line = view.state.doc.lineAt(cursorPos);
-    const insertText = isContinuous && line.text.startsWith(prefix) ? prefix : prefix + space;
+    const insertText = noSpaceIfPrefixExists && line.text.startsWith(prefix) ? prefix : prefix + space;
 
     view.dispatch({
       changes: {
         from: line.from,
-        to: line.from,
         insert: insertText,
       },
-      selection: { anchor: line.from + line.text.length + insertText.length },
+      selection: { anchor: line.to + insertText.length },
     });
     view.focus();
   }, [view]);