reiji-h 2 лет назад
Родитель
Сommit
8cf1b048a1

+ 3 - 2
apps/app/src/components/PageEditor/PageEditor.tsx

@@ -331,7 +331,7 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
         const attachment = res.attachment;
         const attachment = res.attachment;
         const fileName = attachment.originalName;
         const fileName = attachment.originalName;
 
 
-        let insertText = `[${fileName}](${attachment.filePathProxied})`;
+        let insertText = `[${fileName}](${attachment.filePathProxied})\n`;
         // when image
         // when image
         if (attachment.fileFormat.startsWith('image/')) {
         if (attachment.fileFormat.startsWith('image/')) {
         // modify to "![fileName](url)" syntax
         // modify to "![fileName](url)" syntax
@@ -340,6 +340,8 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
         // TODO: implement
         // TODO: implement
         // refs: https://redmine.weseek.co.jp/issues/126528
         // refs: https://redmine.weseek.co.jp/issues/126528
         // editorRef.current.insertText(insertText);
         // editorRef.current.insertText(insertText);
+        // codeMirrorEditor?.view?.state.replaceSelection(insertText);
+        codeMirrorEditor?.insertText(insertText);
 
 
         // when if created newly
         // when if created newly
         // Not using 'mutateGrant' to inherit the grant of the parent page
         // Not using 'mutateGrant' to inherit the grant of the parent page
@@ -357,7 +359,6 @@ export const PageEditor = React.memo((props: Props): JSX.Element => {
         toastError(e);
         toastError(e);
       }
       }
       finally {
       finally {
-        codeMirrorEditor?.insertText(insertText);
       }
       }
 
 
     });
     });

+ 4 - 0
packages/editor/src/services/codemirror-editor/use-codemirror-editor/use-codemirror-editor.ts

@@ -12,6 +12,7 @@ import { useAppendExtensions, type AppendExtensions } from './utils/append-exten
 import { useFocus, type Focus } from './utils/focus';
 import { useFocus, type Focus } from './utils/focus';
 import { useGetDoc, type GetDoc } from './utils/get-doc';
 import { useGetDoc, type GetDoc } from './utils/get-doc';
 import { useInitDoc, type InitDoc } from './utils/init-doc';
 import { useInitDoc, type InitDoc } from './utils/init-doc';
+import { useInsertText, type InsertText } from './utils/insert-text';
 import { useSetCaretLine, type SetCaretLine } from './utils/set-caret-line';
 import { useSetCaretLine, type SetCaretLine } from './utils/set-caret-line';
 
 
 type UseCodeMirrorEditorUtils = {
 type UseCodeMirrorEditorUtils = {
@@ -20,6 +21,7 @@ type UseCodeMirrorEditorUtils = {
   getDoc: GetDoc,
   getDoc: GetDoc,
   focus: Focus,
   focus: Focus,
   setCaretLine: SetCaretLine,
   setCaretLine: SetCaretLine,
+  insertText: InsertText,
 }
 }
 export type UseCodeMirrorEditor = {
 export type UseCodeMirrorEditor = {
   state: EditorState | undefined;
   state: EditorState | undefined;
@@ -56,6 +58,7 @@ export const useCodeMirrorEditor = (props?: UseCodeMirror): UseCodeMirrorEditor
   const getDoc = useGetDoc(view);
   const getDoc = useGetDoc(view);
   const focus = useFocus(view);
   const focus = useFocus(view);
   const setCaretLine = useSetCaretLine(view);
   const setCaretLine = useSetCaretLine(view);
+  const insertText = useInsertText(view);
 
 
   return {
   return {
     state,
     state,
@@ -65,5 +68,6 @@ export const useCodeMirrorEditor = (props?: UseCodeMirror): UseCodeMirrorEditor
     getDoc,
     getDoc,
     focus,
     focus,
     setCaretLine,
     setCaretLine,
+    insertText,
   };
   };
 };
 };

+ 17 - 0
packages/editor/src/services/codemirror-editor/use-codemirror-editor/utils/insert-text.ts

@@ -0,0 +1,17 @@
+import { useCallback } from 'react';
+
+import { EditorView } from '@codemirror/view';
+
+export type InsertText = (text: string) => void;
+
+export const useInsertText = (view?: EditorView): InsertText => {
+
+  return useCallback((text) => {
+    view?.dispatch(
+      view?.state.update(
+        view?.state.replaceSelection(text),
+      ),
+    );
+  }, [view]);
+
+};