From 99ff1120c127254cec838847a8139bfb377b2218 Mon Sep 17 00:00:00 2001 From: NGPixel Date: Sat, 27 May 2017 18:03:24 -0400 Subject: [PATCH] refactor: editor + discard edits + save -> vue components --- client/js/app.js | 4 + .../{editor.js => editor.component.js} | 102 +++++++++--------- client/js/components/modal-discard-page.vue | 43 ++++++++ client/js/components/tree.vue | 2 +- client/js/store/index.js | 4 + client/js/store/modules/editor.js | 9 ++ client/js/store/modules/modal-discard-page.js | 16 +++ server/views/pages/create.pug | 24 ++--- server/views/pages/edit.pug | 24 ++--- server/views/pages/source.pug | 2 +- 10 files changed, 141 insertions(+), 89 deletions(-) rename client/js/components/{editor.js => editor.component.js} (75%) create mode 100644 client/js/components/modal-discard-page.vue create mode 100644 client/js/store/modules/editor.js create mode 100644 client/js/store/modules/modal-discard-page.js diff --git a/client/js/app.js b/client/js/app.js index a2daa776..4f33826a 100644 --- a/client/js/app.js +++ b/client/js/app.js @@ -33,6 +33,7 @@ import colorPickerComponent from './components/color-picker.vue' import loadingSpinnerComponent from './components/loading-spinner.vue' import modalCreatePageComponent from './components/modal-create-page.vue' import modalCreateUserComponent from './components/modal-create-user.vue' +import modalDiscardPageComponent from './components/modal-discard-page.vue' import modalMovePageComponent from './components/modal-move-page.vue' import pageLoaderComponent from './components/page-loader.vue' import searchComponent from './components/search.vue' @@ -41,6 +42,7 @@ import treeComponent from './components/tree.vue' import adminProfileComponent from './pages/admin-profile.component.js' import adminSettingsComponent from './pages/admin-settings.component.js' import contentViewComponent from './pages/content-view.component.js' +import editorComponent from './components/editor.component.js' import sourceViewComponent from './pages/source-view.component.js' // ==================================== @@ -98,9 +100,11 @@ $(() => { anchor: anchorComponent, colorPicker: colorPickerComponent, contentView: contentViewComponent, + editor: editorComponent, loadingSpinner: loadingSpinnerComponent, modalCreatePage: modalCreatePageComponent, modalCreateUser: modalCreateUserComponent, + modalDiscardPage: modalDiscardPageComponent, modalMovePage: modalMovePageComponent, pageLoader: pageLoaderComponent, search: searchComponent, diff --git a/client/js/components/editor.js b/client/js/components/editor.component.js similarity index 75% rename from client/js/components/editor.js rename to client/js/components/editor.component.js index ebc9db06..aacaddc0 100644 --- a/client/js/components/editor.js +++ b/client/js/components/editor.component.js @@ -1,32 +1,54 @@ 'use strict' -import $ from 'jquery' -import Vue from 'vue' -import _ from 'lodash' -import filesize from 'filesize.js' import SimpleMDE from 'simplemde' -import pageLoader from '../components/page-loader' +import filesize from 'filesize.js' +import $ from 'jquery' -// ==================================== -// Markdown Editor -// ==================================== +let mde -module.exports = (alerts, pageEntryPath, socket) => { - if ($('#mk-editor').length === 1) { - Vue.filter('filesize', (v) => { - return _.toUpper(filesize(v)) - }) - - let mdeModalOpenState = false - let vueImage - let vueFile - let vueVideo - let vueCodeBlock - - let mde = new SimpleMDE({ +export default { + name: 'editor', + props: ['currentPath'], + filters: { + filesize(v) { + return this._.toUpper(filesize(v)) + } + }, + data() { + return {} + }, + methods: { + save() { + let self = this + this.$http.put(window.location.href, { + markdown: mde.value() + }).then(resp => { + return resp.json() + }).then(resp => { + if (resp.ok) { + window.location.assign('/' + self.currentPath) + } else { + self.$store.dispatch('alert', { + style: 'red', + icon: 'square-cross', + msg: resp.msg + }) + } + }).catch(err => { + self.$store.dispatch('alert', { + style: 'red', + icon: 'square-cross', + msg: 'Error: ' + err.body.msg + }) + }) + } + }, + mounted() { + let self = this + mde = new SimpleMDE({ autofocus: true, autoDownloadFontAwesome: false, - element: $('#mk-editor').get(0), + element: this.$refs.editorTextArea, placeholder: 'Enter Markdown formatted content here...', spellChecker: false, status: false, @@ -183,46 +205,20 @@ module.exports = (alerts, pageEntryPath, socket) => { } }) - vueImage = require('./editor-image.js')(alerts, mde, mdeModalOpenState, socket) - vueFile = require('./editor-file.js')(alerts, mde, mdeModalOpenState, socket) - vueVideo = require('./editor-video.js')(mde, mdeModalOpenState) - vueCodeBlock = require('./editor-codeblock.js')(mde, mdeModalOpenState) - - pageLoader.complete() - - // -> Save - - let saveCurrentDocument = (ev) => { - $.ajax(window.location.href, { - data: { - markdown: mde.value() - }, - dataType: 'json', - method: 'PUT' - }).then((rData, rStatus, rXHR) => { - if (rData.ok) { - window.location.assign('/' + pageEntryPath) // eslint-disable-line no-undef - } else { - alerts.pushError('Something went wrong', rData.error) - } - }, (rXHR, rStatus, err) => { - alerts.pushError('Something went wrong', 'Save operation failed.') - }) - } - - $('.btn-edit-save, .btn-create-save').on('click', (ev) => { - saveCurrentDocument(ev) - }) + // Save + this.$root.$on('editor-save', this.save) $(window).bind('keydown', (ev) => { if (ev.ctrlKey || ev.metaKey) { switch (String.fromCharCode(ev.which).toLowerCase()) { case 's': ev.preventDefault() - saveCurrentDocument(ev) + self.save() break } } }) + + this.$store.dispatch('pageLoader/complete') } } diff --git a/client/js/components/modal-discard-page.vue b/client/js/components/modal-discard-page.vue new file mode 100644 index 00000000..9968f567 --- /dev/null +++ b/client/js/components/modal-discard-page.vue @@ -0,0 +1,43 @@ + + + diff --git a/client/js/components/tree.vue b/client/js/components/tree.vue index 1ce3483f..21f0b572 100644 --- a/client/js/components/tree.vue +++ b/client/js/components/tree.vue @@ -1,5 +1,5 @@