WNomunomu 1 год назад
Родитель
Сommit
0dadddcb17

+ 9 - 1
packages/editor/src/client/services/use-codemirror-editor/utils/insert-prefix.ts

@@ -5,6 +5,13 @@ import type { EditorView } from '@codemirror/view';
 
 
 export type InsertPrefix = (prefix: string, noSpaceIfPrefixExists?: boolean) => void;
 export type InsertPrefix = (prefix: string, noSpaceIfPrefixExists?: boolean) => void;
 
 
+const removePrefix = (text: string, prefix: string): string => {
+  if (text.startsWith(prefix)) {
+    return text.slice(prefix.length).trimStart();
+  }
+  return text;
+};
+
 export const useInsertPrefix = (view?: EditorView): InsertPrefix => {
 export const useInsertPrefix = (view?: EditorView): InsertPrefix => {
   return useCallback((prefix: string, noSpaceIfPrefixExists = false) => {
   return useCallback((prefix: string, noSpaceIfPrefixExists = false) => {
     if (view == null) {
     if (view == null) {
@@ -83,11 +90,12 @@ export const useInsertPrefix = (view?: EditorView): InsertPrefix => {
             const prefixWithSpaces = contentStartMatch[0];
             const prefixWithSpaces = contentStartMatch[0];
             const indentLevel = Math.floor(leadingSpaces.length / 2) * 2;
             const indentLevel = Math.floor(leadingSpaces.length / 2) * 2;
             const newIndent = ' '.repeat(indentLevel);
             const newIndent = ' '.repeat(indentLevel);
+            const prefixRemovedText = removePrefix(contentTrimmed, prefix);
 
 
             changes.push({
             changes.push({
               from: line.from,
               from: line.from,
               to: line.from + prefixWithSpaces.length,
               to: line.from + prefixWithSpaces.length,
-              insert: newIndent,
+              insert: `${newIndent}${prefixRemovedText}`,
             });
             });
 
 
             totalLengthChange -= (prefixWithSpaces.length - newIndent.length);
             totalLengthChange -= (prefixWithSpaces.length - newIndent.length);