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

create adjustPasteData function

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

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

@@ -57,6 +57,7 @@ export const CodeMirrorEditor = (props: Props): JSX.Element => {
 
 
   }, [codeMirrorEditor, indentSize]);
   }, [codeMirrorEditor, indentSize]);
 
 
+  // ここのPaste処理を改善!
   useEffect(() => {
   useEffect(() => {
     const handlePaste = (event: ClipboardEvent) => {
     const handlePaste = (event: ClipboardEvent) => {
       event.preventDefault();
       event.preventDefault();

+ 50 - 0
packages/editor/src/services/list-util/markdown-list-util.ts

@@ -0,0 +1,50 @@
+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;
+  const aboveLine = editor.state.doc.lineAt(curPos).number - 1;
+  return editor.state.doc.line(aboveLine).from;
+};
+
+const getStrFromBol = (editor: EditorView) => {
+  const curPos = editor.state.selection.main.head;
+  return editor.state.sliceDoc(getBol(editor), curPos);
+};
+
+const insertText = (editor: EditorView, text: string) => {
+  const curPos = editor.state.selection.main.head;
+  const line = editor.state.doc.lineAt(curPos).from;
+  editor.dispatch({
+    changes: {
+      from: line,
+      to: curPos,
+      insert: text,
+    },
+  });
+};
+
+export const newlineAndIndentContinueMarkdownList = (editor: EditorView): void => {
+  const strFromBol = getStrFromBol(editor);
+
+  const matchResult = strFromBol.match(indentAndMarkRE);
+
+  if (matchResult != null) {
+    // continue list
+    const indentAndMark = matchResult[0];
+    insertText(editor, indentAndMark);
+  }
+};
+
+
+// ここを作っていこう!
+export const adjustPasteData = (indentAndMark: string, text: string): string | null => {
+
+  if (text.match(indentAndMarkRE)) {
+    const indent = indentAndMark.match(indentAndMarkRE)[1];
+  }
+
+  return '';
+};