Przeglądaj źródła

impl GC-525: copy document when start to edit on HackMD

Yuki Takei 7 lat temu
rodzic
commit
b0014b9708

+ 11 - 2
resource/js/agent-for-hackmd.js

@@ -11,6 +11,8 @@
  */
  */
 import { debounce } from 'throttle-debounce';
 import { debounce } from 'throttle-debounce';
 
 
+const JSON = window.JSON;
+
 /* eslint-disable no-console  */
 /* eslint-disable no-console  */
 
 
 const allowedOrigin = '{{origin}}';         // will be replaced by swig
 const allowedOrigin = '{{origin}}';         // will be replaced by swig
@@ -42,7 +44,7 @@ function postMessageOnSave(body) {
     operation: 'notifyBodyChanges',
     operation: 'notifyBodyChanges',
     body
     body
   };
   };
-  window.parent.postMessage(window.JSON.stringify(data), allowedOrigin);
+  window.parent.postMessage(JSON.stringify(data), allowedOrigin);
 }
 }
 
 
 function addEventListenersToCodemirror() {
 function addEventListenersToCodemirror() {
@@ -57,6 +59,13 @@ function addEventListenersToCodemirror() {
   });
   });
 }
 }
 
 
+function setValue(document) {
+  // get CodeMirror instance
+  const editor = window.editor;
+  editor.doc.setValue(document);
+}
+
+
 /**
 /**
  * main
  * main
  */
  */
@@ -81,7 +90,7 @@ function addEventListenersToCodemirror() {
         console.log('getValue called');
         console.log('getValue called');
         break;
         break;
       case 'setValue':
       case 'setValue':
-        console.log('setValue called');
+        setValue(data.document);
         break;
         break;
     }
     }
   });
   });

+ 6 - 6
resource/js/components/PageEditorByHackmd.jsx

@@ -118,12 +118,16 @@ export default class PageEditorByHackmd extends React.PureComponent {
   render() {
   render() {
     const hackmdUri = this.getHackmdUri();
     const hackmdUri = this.getHackmdUri();
 
 
+    const isPageExistsOnHackmd = (this.state.pageIdOnHackmd != null);
+    const isRevisionMatch = (this.props.revisionId === this.props.revisionIdHackmdSynced);
+    const isResume = isPageExistsOnHackmd && isRevisionMatch && this.props.hasDraftOnHackmd;
+
     if (this.state.isInitialized) {
     if (this.state.isInitialized) {
       return (
       return (
         <HackmdEditor
         <HackmdEditor
-          markdown={this.props.markdown}
           hackmdUri={hackmdUri}
           hackmdUri={hackmdUri}
           pageIdOnHackmd={this.state.pageIdOnHackmd}
           pageIdOnHackmd={this.state.pageIdOnHackmd}
+          initializationMarkdown={isResume ? null : this.props.markdown}
           onChange={this.hackmdEditorChangeHandler}
           onChange={this.hackmdEditorChangeHandler}
         >
         >
         </HackmdEditor>
         </HackmdEditor>
@@ -131,9 +135,6 @@ export default class PageEditorByHackmd extends React.PureComponent {
     }
     }
 
 
     let content = undefined;
     let content = undefined;
-    const isPageExistsOnHackmd = (this.state.pageIdOnHackmd != null);
-    const isRevisionMatch = (this.props.revisionId === this.props.revisionIdHackmdSynced);
-
     // HackMD is not setup
     // HackMD is not setup
     if (hackmdUri == null) {
     if (hackmdUri == null) {
       content = (
       content = (
@@ -142,8 +143,7 @@ export default class PageEditorByHackmd extends React.PureComponent {
         </div>
         </div>
       );
       );
     }
     }
-    // page is exists, revisions are match, hasDraftOnHackmd is true
-    else if (isPageExistsOnHackmd && isRevisionMatch && this.props.hasDraftOnHackmd) {
+    else if (isResume) {
       content = (
       content = (
         <div>
         <div>
           <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is READY!</p>
           <p className="text-center hackmd-status-label"><i className="fa fa-file-text"></i> HackMD is READY!</p>

+ 14 - 2
resource/js/components/PageEditorByHackmd/HackmdEditor.jsx

@@ -40,6 +40,14 @@ export default class HackmdEditor extends React.PureComponent {
     });
     });
   }
   }
 
 
+  /**
+   *
+   * @param {object} data
+   */
+  postMessageToHackmd(data) {
+    this.refs.iframe.contentWindow.postMessage(JSON.stringify(data), this.props.hackmdUri);
+  }
+
   notifyBodyChangesHandler(body) {
   notifyBodyChangesHandler(body) {
     // dispatch onChange()
     // dispatch onChange()
     if (this.props.onChange != null) {
     if (this.props.onChange != null) {
@@ -48,7 +56,11 @@ export default class HackmdEditor extends React.PureComponent {
   }
   }
 
 
   loadHandler() {
   loadHandler() {
-    // this.refs.iframe.postMessage('initialize', )
+    const data = { operation: 'setValue' };
+    if (this.props.initializationMarkdown != null) {
+      data.document = this.props.initializationMarkdown;
+    }
+    this.postMessageToHackmd(data);
   }
   }
 
 
   render() {
   render() {
@@ -65,8 +77,8 @@ export default class HackmdEditor extends React.PureComponent {
 }
 }
 
 
 HackmdEditor.propTypes = {
 HackmdEditor.propTypes = {
-  markdown: PropTypes.string.isRequired,
   hackmdUri: PropTypes.string.isRequired,
   hackmdUri: PropTypes.string.isRequired,
   pageIdOnHackmd: PropTypes.string.isRequired,
   pageIdOnHackmd: PropTypes.string.isRequired,
+  initializationMarkdown: PropTypes.string,
   onChange: PropTypes.func,
   onChange: PropTypes.func,
 };
 };