Browse Source

modify makeTextCodeBlock so that the text is selected

WNomunomu 1 year ago
parent
commit
6e93e5668d
1 changed files with 16 additions and 15 deletions
  1. 16 15
      packages/editor/src/client/stores/use-editor-shortcuts.ts

+ 16 - 15
packages/editor/src/client/stores/use-editor-shortcuts.ts

@@ -43,39 +43,38 @@ const generateAddMarkdownSymbolCommand = (
 
 
 const makeTextCodeBlock: Command = (view: EditorView) => {
 const makeTextCodeBlock: Command = (view: EditorView) => {
   const state = view.state;
   const state = view.state;
-  const dispatch = view.dispatch;
-
-  const selections = state.selection.ranges;
   const doc = state.doc;
   const doc = state.doc;
   const changes: ChangeSpec[] = [];
   const changes: ChangeSpec[] = [];
-  const newSelectionRanges: SelectionRange[] = [];
+  const newSelections: SelectionRange[] = [];
 
 
-  selections.forEach((range) => {
+  state.selection.ranges.forEach((range) => {
     const startLine = doc.lineAt(range.from);
     const startLine = doc.lineAt(range.from);
     const endLine = doc.lineAt(range.to);
     const endLine = doc.lineAt(range.to);
-
     const selectedText = doc.sliceString(range.from, range.to, '');
     const selectedText = doc.sliceString(range.from, range.to, '');
-
     const isAlreadyWrapped = selectedText.startsWith('```') && selectedText.endsWith('```');
     const isAlreadyWrapped = selectedText.startsWith('```') && selectedText.endsWith('```');
 
 
     if (isAlreadyWrapped) {
     if (isAlreadyWrapped) {
-      const textContentLength = selectedText.length - 6;
+      // Remove existing code block markers
+      const startMarkerEnd = startLine.from + 4; // '```\n' length
+      const endMarkerStart = endLine.to - 4; // '\n```' length
 
 
       changes.push({
       changes.push({
         from: startLine.from,
         from: startLine.from,
-        to: startLine.from + 4,
+        to: startMarkerEnd,
         insert: '',
         insert: '',
       });
       });
 
 
       changes.push({
       changes.push({
-        from: endLine.to - 4,
+        from: endMarkerStart,
         to: endLine.to,
         to: endLine.to,
         insert: '',
         insert: '',
       });
       });
 
 
-      newSelectionRanges.push(EditorSelection.cursor(startLine.from + textContentLength));
+      // Position cursor at the start of the unwrapped content
+      newSelections.push(EditorSelection.range(startLine.from, endMarkerStart - 4));
     }
     }
     else {
     else {
+      // Add code block markers
       changes.push({
       changes.push({
         from: startLine.from,
         from: startLine.from,
         insert: '```\n',
         insert: '```\n',
@@ -86,14 +85,16 @@ const makeTextCodeBlock: Command = (view: EditorView) => {
         insert: '\n```',
         insert: '\n```',
       });
       });
 
 
-      newSelectionRanges.push(EditorSelection.cursor(endLine.to + 4));
+      // Position cursor to include the entire wrapped content with markers
+      newSelections.push(EditorSelection.range(startLine.from, endLine.to + 8));
     }
     }
   });
   });
 
 
-  dispatch(state.update({
+  // Send a single transaction with both changes and selection update
+  view.dispatch({
     changes,
     changes,
-    selection: EditorSelection.create(newSelectionRanges),
-  }));
+    selection: EditorSelection.create(newSelections),
+  });
 
 
   return true;
   return true;
 };
 };