Shun Miyazawa 11 месяцев назад
Родитель
Сommit
ae6bfad793

+ 5 - 8
apps/app/src/features/openai/client/services/editor-assistant.ts

@@ -3,7 +3,9 @@ import {
 } from 'react';
 
 import { GlobalCodeMirrorEditorKey } from '@growi/editor';
-import { acceptChange, rejectChange, useTextSelectionEffect } from '@growi/editor/dist/client/services/unified-merge-view';
+import {
+  acceptAllChunks, useTextSelectionEffect,
+} from '@growi/editor/dist/client/services/unified-merge-view';
 import { useCodeMirrorEditorIsolated } from '@growi/editor/dist/client/stores/codemirror-editor';
 import { useSecondaryYdocs } from '@growi/editor/dist/client/stores/use-secondary-ydocs';
 import { type Text as YText } from 'yjs';
@@ -155,18 +157,13 @@ export const useEditorAssistant: UseEditorAssistant = () => {
       return;
     }
 
-    acceptChange(codeMirrorEditor.view);
+    acceptAllChunks(codeMirrorEditor.view);
     mutateIsEnableUnifiedMergeView(false);
   }, [codeMirrorEditor?.view, mutateIsEnableUnifiedMergeView]);
 
   const reject = useCallback(() => {
-    if (codeMirrorEditor?.view == null) {
-      return;
-    }
-
-    rejectChange(codeMirrorEditor.view);
     mutateIsEnableUnifiedMergeView(false);
-  }, [codeMirrorEditor?.view, mutateIsEnableUnifiedMergeView]);
+  }, [mutateIsEnableUnifiedMergeView]);
 
   const selectTextHandler = useCallback((selectedText: string, selectedTextFirstLineNumber: number) => {
     setSelectedText(selectedText);

+ 21 - 6
packages/editor/src/client/services/unified-merge-view/index.ts

@@ -2,19 +2,34 @@ import { useEffect } from 'react';
 
 import {
   acceptChunk,
-  rejectChunk,
+  getChunks,
 } from '@codemirror/merge';
 import type { ViewUpdate } from '@codemirror/view';
 import { EditorView } from '@codemirror/view';
 
 import type { UseCodeMirrorEditor } from '..';
 
-export const acceptChange = (view: EditorView): boolean => {
-  return acceptChunk(view);
-};
 
-export const rejectChange = (view: EditorView): boolean => {
-  return rejectChunk(view);
+export const acceptAllChunks = (view: EditorView): boolean => {
+  // Get all chunks from the editor state
+  const chunkData = getChunks(view.state);
+  if (!chunkData || chunkData.chunks.length === 0) return false;
+
+  // Get all chunks and sort them in reverse order by position
+  const chunks = [...chunkData.chunks].sort((a, b) => b.fromB - a.fromB);
+
+  // Track if we've accepted at least one chunk
+  let accepted = false;
+
+  // Process each chunk from bottom to top
+  for (const chunk of chunks) {
+    // Use a position inside the chunk (middle point is safe)
+    const pos = chunk.fromB + Math.floor((chunk.endB - chunk.fromB) / 2);
+    const success = acceptChunk(view, pos);
+    if (success) accepted = true;
+  }
+
+  return accepted;
 };