Преглед изворни кода

implement of adjustPasteData function

kosei-n пре 2 година
родитељ
комит
b07d03782c

+ 25 - 1
packages/editor/src/components/CodeMirrorEditor/CodeMirrorEditor.tsx

@@ -8,10 +8,14 @@ import type { ReactCodeMirrorProps } from '@uiw/react-codemirror';
 
 import { GlobalCodeMirrorEditorKey, AcceptedUploadFileType } from '../../consts';
 import { useFileDropzone, FileDropzoneOverlay } from '../../services';
+import {
+  getStrFromBolToSelectedUpperPos, adjustPasteData,
+} from '../../services/list-util/markdown-list-util';
 import { useCodeMirrorEditorIsolated } from '../../stores';
 
 import { Toolbar } from './Toolbar';
 
+
 import style from './CodeMirrorEditor.module.scss';
 
 const CodeMirrorEditorContainer = forwardRef<HTMLDivElement>((props, ref) => {
@@ -62,6 +66,19 @@ export const CodeMirrorEditor = (props: Props): JSX.Element => {
     const handlePaste = (event: ClipboardEvent) => {
       event.preventDefault();
 
+      const editor = codeMirrorEditor?.view;
+
+      if (editor == null) {
+        return;
+      }
+
+      const strFromBol = getStrFromBolToSelectedUpperPos(editor);
+
+      const testVar = strFromBol;
+
+      console.log(testVar);
+      console.log('kohsei');
+
       if (event.clipboardData == null) {
         return;
       }
@@ -71,9 +88,16 @@ export const CodeMirrorEditor = (props: Props): JSX.Element => {
       }
 
       if (event.clipboardData.types.includes('text/plain')) {
+
         const textData = event.clipboardData.getData('text/plain');
-        codeMirrorEditor?.replaceText(textData);
+
+        const adjusted = adjustPasteData(strFromBol, textData);
+
+        if (adjusted != null) {
+          codeMirrorEditor?.replaceText(adjusted);
+        }
       }
+      // console.log('kohsei');
     };
 
     const extension = EditorView.domEventHandlers({

+ 48 - 4
packages/editor/src/services/list-util/markdown-list-util.ts

@@ -1,7 +1,6 @@
 import { EditorView } from '@codemirror/view';
 
 const indentAndMarkRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]\s|[*+-]\s|(\d+)([.)]))(\s*)/;
-const indentAndMarkOnlyRE = /^(\s*)(>[> ]*|[*+-] \[[x ]\]|[*+-]|(\d+)[.)])(\s*)$/;
 
 const getBol = (editor: EditorView) => {
   const curPos = editor.state.selection.main.head;
@@ -38,13 +37,58 @@ export const newlineAndIndentContinueMarkdownList = (editor: EditorView): void =
   }
 };
 
+const selectUpperPos = (editor: EditorView, pos1: number, pos2: number) => {
+
+  const pos1Line = editor.state.doc.lineAt(pos1);
+  const pos2Line = editor.state.doc.lineAt(pos2);
+
+  const pos1Ch = pos1 - editor.state.doc.lineAt(pos1).from;
+  const pos2Ch = pos2 - editor.state.doc.lineAt(pos2).from;
+
+  console.log(pos1);
+  console.log(pos2);
+  console.log(pos1Line);
+  console.log(pos2Line);
+  console.log(pos1Ch);
+  console.log(pos2Ch);
+
+
+  if (pos1Line === pos2Line) {
+    return (pos1Ch < pos2Ch) ? pos1 : pos2;
+  }
+  return (pos1Line < pos2Line) ? pos1 : pos2;
+};
+
+export const getStrFromBolToSelectedUpperPos = (editor: EditorView): string => {
+  const pos = selectUpperPos(editor, editor.state.selection.main.from, editor.state.selection.main.to);
+  // ここでエラー起きてそう
+
+  return editor.state.sliceDoc(getBol(editor), pos);
+};
 
 // ここを作っていこう!
-export const adjustPasteData = (indentAndMark: string, text: string): string | null => {
+export const adjustPasteData = (indentAndMark: string, text: string): string => {
+
+  let adjusted = '';
 
   if (text.match(indentAndMarkRE)) {
-    const indent = indentAndMark.match(indentAndMarkRE)[1];
+    const matchResult = indentAndMark.match(indentAndMarkRE);
+    const indent = matchResult ? matchResult[1] : '';
+
+    const lines = text.match(/[^\r\n]+/g);
+
+    const replacedLines = lines?.map((line) => {
+      return indent + line;
+    });
+
+    adjusted = replacedLines ? replacedLines.join('\n') : '';
+  }
+
+  else {
+    const replacedText = text.replace(/(\r\n|\r|\n)/g, `$1${indentAndMark}`);
+
+    adjusted = indentAndMark + replacedText;
   }
 
-  return '';
+  return adjusted;
 };