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

WIP: GC-526 save with update button

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

+ 28 - 1
resource/js/app.js

@@ -216,8 +216,35 @@ const saveWithShortcut = function(markdown) {
 };
 
 const saveWithSubmitButton = function() {
+  const editorMode = crowi.getCrowiForJquery().getCurrentEditorMode();
+  if (editorMode == null) {
+    // do nothing
+    return;
+  }
+
+  // get contents
+  const markdown = (editorMode === 'builtin')
+    ? componentInstances.pageEditor.getMarkdown()
+    : componentInstances.pageEditorByHackmd.getMarkdown();
+  // get options
   const options = componentInstances.savePageControls.getCurrentOptionsToSave();
-  console.log(options);
+
+  let promise = undefined;
+  if (pageId == null) {
+    promise = crowi.createPage(pagePath, markdown, options);
+  }
+  else {
+    promise = crowi.updatePage(pageId, pageRevisionId, markdown, options);
+  }
+
+  // TODO redirect when success
+  promise
+    .then(saveWithShortcutSuccessHandler)
+    .catch(errorHandler);
+
+  if (editorMode === 'builtin') {
+    crowi.clearDraft(pagePath);
+  }
 };
 
 // render SavePageControls

+ 4 - 0
resource/js/components/PageEditor.js

@@ -61,6 +61,10 @@ export default class PageEditor extends React.Component {
     this.renderPreview(this.state.markdown);
   }
 
+  getMarkdown() {
+    return this.state.markdown;
+  }
+
   setMarkdown(markdown) {
     this.setState({ markdown });
     this.refs.editor.setValue(markdown);

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

@@ -29,9 +29,18 @@ export default class PageEditorByHackmd extends React.PureComponent {
   componentWillMount() {
   }
 
+  getMarkdown() {
+    if (!this.state.isInitialized) {
+      throw new Error('HackmdEditor component has not initialized');
+    }
+
+    // TODO impl
+    return this.state.markdown;
+  }
+
   setMarkdown(markdown) {
     this.setState({ markdown });
-    if (this.refs.hackmdEditor != null) {
+    if (this.state.isInitialized) {
       this.refs.hackmdEditor.setValue(markdown);
     }
   }

+ 11 - 11
resource/js/legacy/crowi-form.js

@@ -1,14 +1,14 @@
-const pagePath= $('#content-main').data('path');
+// const pagePath= $('#content-main').data('path');
 
-/**
- * DOM ready
- */
-$(function() {
+// /**
+//  * DOM ready
+//  */
+// $(function() {
 
-  $('#page-form').on('submit', function(e) {
-    // avoid message
-    // isFormChanged = false;
-    window.crowi.clearDraft(pagePath);
-  });
+//   $('#page-form').on('submit', function(e) {
+//     // avoid message
+//     // isFormChanged = false;
+//     window.crowi.clearDraft(pagePath);
+//   });
 
-});
+// });

+ 18 - 0
resource/js/legacy/crowi.js

@@ -255,6 +255,24 @@ Crowi.highlightSelectedSection = function(hash) {
   }
 };
 
+/**
+ * Return editor mode string
+ * @return 'builtin' or 'hackmd' or null (not editing)
+ */
+Crowi.getCurrentEditorMode = function() {
+  const isEditing = $('body').hasClass('on-edit');
+  if (!isEditing) {
+    return null;
+  }
+
+  if ($('body').hasClass('builtin-editor')) {
+    return 'builtin';
+  }
+  else {
+    return 'hackmd';
+  }
+};
+
 $(function() {
   const config = JSON.parse(document.getElementById('crowi-context-hydrate').textContent || '{}');