Просмотр исходного кода

GC-526: save with submit button

Yuki Takei 7 лет назад
Родитель
Сommit
77d7ad385a

+ 20 - 12
resource/js/app.js

@@ -215,6 +215,11 @@ const saveWithShortcut = function(markdown) {
     .catch(errorHandler);
 };
 
+const saveWithSubmitButtonSuccessHandler = function() {
+  crowi.clearDraft(pagePath);
+  location.href = pagePath;
+};
+
 const saveWithSubmitButton = function() {
   const editorMode = crowi.getCrowiForJquery().getCurrentEditorMode();
   if (editorMode == null) {
@@ -222,29 +227,32 @@ const saveWithSubmitButton = function() {
     return;
   }
 
-  // get contents
-  const markdown = (editorMode === 'builtin')
-    ? componentInstances.pageEditor.getMarkdown()
-    : componentInstances.pageEditorByHackmd.getMarkdown();
   // get options
   const options = componentInstances.savePageControls.getCurrentOptionsToSave();
 
   let promise = undefined;
+  // get markdown
+  if (editorMode === 'builtin') {
+    promise = Promise.resolve(componentInstances.pageEditor.getMarkdown());
+  }
+  else {
+    promise = componentInstances.pageEditorByHackmd.getMarkdown();
+  }
+  // create or update
   if (pageId == null) {
-    promise = crowi.createPage(pagePath, markdown, options);
+    promise = promise.then(markdown => {
+      return crowi.createPage(pagePath, markdown, options);
+    });
   }
   else {
-    promise = crowi.updatePage(pageId, pageRevisionId, markdown, options);
+    promise = promise.then(markdown => {
+      return crowi.updatePage(pageId, pageRevisionId, markdown, options);
+    });
   }
 
-  // TODO redirect when success
   promise
-    .then(saveWithShortcutSuccessHandler)
+    .then(saveWithSubmitButtonSuccessHandler)
     .catch(errorHandler);
-
-  if (editorMode === 'builtin') {
-    crowi.clearDraft(pagePath);
-  }
 };
 
 // render SavePageControls

+ 9 - 2
resource/js/components/PageEditorByHackmd.jsx

@@ -29,13 +29,20 @@ export default class PageEditorByHackmd extends React.PureComponent {
   componentWillMount() {
   }
 
+  /**
+   * return markdown document of HackMD
+   * @return {Promise<string>}
+   */
   getMarkdown() {
     if (!this.state.isInitialized) {
       throw new Error('HackmdEditor component has not initialized');
     }
 
-    // TODO impl
-    return this.state.markdown;
+    return this.refs.hackmdEditor.getValue()
+      .then(document => {
+        this.setState({ markdown: document });
+        return document;
+      });
   }
 
   setMarkdown(markdown) {

+ 8 - 0
resource/js/components/PageEditorByHackmd/HackmdEditor.jsx

@@ -52,6 +52,14 @@ export default class HackmdEditor extends React.PureComponent {
     });
   }
 
+  /**
+   * return markdown document of HackMD
+   * @return {Promise<string>}
+   */
+  getValue() {
+    return this.hackmd.getValue();
+  }
+
   setValue(newValue) {
     this.hackmd.setValue(newValue);
   }