Ver Fonte

GC-529 invoke saveOnHackmd API

* ensure to check the body sent by HackMD and determine whether invoke /_api/hackmd.saveOnHackmd
Yuki Takei há 7 anos atrás
pai
commit
9e3c93afc6

+ 7 - 4
resource/js/agent-for-hackmd.js

@@ -37,9 +37,12 @@ function insertStyle() {
   document.getElementsByTagName('head')[0].appendChild(element);
 }
 
-function postMessageOnSave() {
-  const data = '{ "operation": "notifyBodyChanges" }';
-  window.parent.postMessage(data, allowedOrigin);
+function postMessageOnSave(body) {
+  const data = {
+    operation: 'notifyBodyChanges',
+    body
+  };
+  window.parent.postMessage(window.JSON.stringify(data), allowedOrigin);
 }
 
 function addEventListenersToCodemirror() {
@@ -50,7 +53,7 @@ function addEventListenersToCodemirror() {
   const debouncedFunc = debounce(1500, postMessageOnSave);
 
   editor.on('change', (cm, change) => {
-    debouncedFunc();
+    debouncedFunc(cm.doc.getValue());
   });
 }
 

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

@@ -79,7 +79,7 @@ export default class PageEditorByHackmd extends React.PureComponent {
   /**
    * onChange event of HackmdEditor handler
    */
-  hackmdEditorChangeHandler() {
+  hackmdEditorChangeHandler(body) {
     const hackmdUri = this.getHackmdUri();
 
     if (hackmdUri == null) {
@@ -87,6 +87,11 @@ export default class PageEditorByHackmd extends React.PureComponent {
       return;
     }
 
+    // do nothing if contents are same
+    if (this.props.markdown === body) {
+      return;
+    }
+
     const params = {
       pageId: this.props.pageId,
     };

+ 4 - 3
resource/js/components/PageEditorByHackmd/HackmdEditor.jsx

@@ -32,17 +32,18 @@ export default class HackmdEditor extends React.PureComponent {
 
       const data = JSON.parse(e.data);
       const operation = data.operation;
+      const body = data.body;
 
       if (operation === 'notifyBodyChanges') {
-        this.notifyBodyChangesHandler();
+        this.notifyBodyChangesHandler(body);
       }
     });
   }
 
-  notifyBodyChangesHandler() {
+  notifyBodyChangesHandler(body) {
     // dispatch onChange()
     if (this.props.onChange != null) {
-      this.props.onChange();
+      this.props.onChange(body);
     }
   }