Przeglądaj źródła

Merge branch 'imprv/press-done-button-to-paste-code' into imprv/re-edit-in-row-with-editor

# Conflicts:
#	src/client/js/components/PageEditor/GridEditorUtil.js
ryohek 5 lat temu
rodzic
commit
5b315e41de

+ 1 - 1
src/client/js/components/PageEditor/CodeMirrorEditor.jsx

@@ -868,7 +868,7 @@ export default class CodeMirrorEditor extends AbstractEditor {
 
         <GridEditModal
           ref={this.gridEditModal}
-          onSave={() => { return geu.replaceGridWithHtmlWithEditor(this.getCodeMirror()) }}
+          onSave={(grid) => { return geu.replaceGridWithHtmlWithEditor(this.getCodeMirror(), grid) }}
         />
         <HandsontableModal
           ref={this.handsontableModal}

+ 3 - 1
src/client/js/components/PageEditor/GridEditModal.jsx

@@ -33,8 +33,10 @@ export default class GridEditModal extends React.PureComponent {
   }
 
   pasteCodedGrid() {
+    // dummy data
+    const pastedGridData = '<div class="container"><div class="row"><div class="col-sm-6 col-md-5 col-lg-12">dummy</div></div></div>';
     if (this.props.onSave != null) {
-      this.props.onSave();
+      this.props.onSave(pastedGridData);
     }
     this.cancel();
   }

+ 68 - 10
src/client/js/components/PageEditor/GridEditorUtil.js

@@ -3,9 +3,10 @@
  */
 class GridEditorUtil {
   constructor() {
-    // regex101 の url
-    this.linePartOfTableRE = /(<div>.*)(</div>)$/;
-    this.isInRow = this.isInRow.bind(this);
+    // TODO url
+    this.lineBeginPartOfGridRE = /(<[^/].*>)/;
+    this.lineEndPartOfGridRE = /(<\/.*>)/;
+    this.linePartOfGridRE = /(<[^/].*>)[\s\S]*<\/.*>$/;
     this.replaceGridWithHtmlWithEditor = this.replaceGridWithHtmlWithEditor.bind(this);
   }
   /**
@@ -17,17 +18,74 @@ class GridEditorUtil {
     return this.linePartOfRow.test(editor.getDoc().getLine(curPos.line));
   }
 
-  replaceGridWithHtmlWithEditor(editor) {
+  /**
+   * return the postion of the BOD(beginning of grid)
+   */
+  getBog(editor) {
+    const curPos = editor.getCursor();
+    const firstLine = editor.getDoc().firstLine();
+
+    let line = curPos.line - 1;
+    let isFound = false;
+    for (; line >= firstLine; line--) {
+      const strLine = editor.getDoc().getLine(line);
+      if (this.lineBeginPartOfGridRE.test(strLine)) {
+        isFound = true;
+        break;
+      }
+
+      if (this.lineBeginPartOfGridRE.test(strLine)) {
+        isFound = false;
+        break;
+      }
+    }
+
+    if (!isFound) {
+      return { line: curPos.line, ch: curPos.ch };
+    }
+
+    const bodLine = Math.max(firstLine, line);
+    return { line: bodLine, ch: 0 };
+  }
+
+  /**
+   * return the postion of the EOD(end of grid)
+   */
+  getEog(editor) {
+    const curPos = editor.getCursor();
+    const lastLine = editor.getDoc().lastLine();
+
+    let line = curPos.line;
+    let isFound = false;
+    for (; line <= lastLine; line++) {
+      const strLine = editor.getDoc().getLine(line);
+      if (this.lineEndPartOfGridRE.test(strLine)) {
+        isFound = true;
+        break;
+      }
+
+      if (this.lineEndPartOfGridRE.test(strLine)) {
+        isFound = false;
+        break;
+      }
+    }
+
+    if (!isFound) {
+      return { line: curPos.line, ch: curPos.ch };
+    }
+
+    const eodLine = Math.min(line, lastLine);
+    const lineLength = editor.getDoc().getLine(eodLine).length;
+    return { line: eodLine, ch: lineLength };
+    // return { line: lastLine, ch: curPos.ch };
+  }
+
+  replaceGridWithHtmlWithEditor(editor, grid) {
     const curPos = editor.getCursor();
-    editor.getDoc().replaceRange(
-      // dummy data
-      '<div class="container"><div class="row"><div class="col-sm-6 col-md-5 col-lg-12">dummy</div></div></div>',
-      { line: editor.getDoc().getCursor().line, ch: editor.getDoc().getCursor().ch },
-    );
     editor.getDoc().setCursor(curPos.line + 1, 2);
   }
 }
 // singleton pattern
 const instance = new GridEditorUtil();
 Object.freeze(instance);
-export default instance;
+export default instance;