X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/9135a85de4eef32a91c7a3ee0aa405ed454e5a4c..refs/pull/5429/head:/resources/js/components/wysiwyg-editor.js diff --git a/resources/js/components/wysiwyg-editor.js b/resources/js/components/wysiwyg-editor.js index 976dba68f..56dbe8d7c 100644 --- a/resources/js/components/wysiwyg-editor.js +++ b/resources/js/components/wysiwyg-editor.js @@ -1,31 +1,49 @@ -import {build as buildEditorConfig} from "../wysiwyg/config"; -import {Component} from "./component"; +import {Component} from './component'; export class WysiwygEditor extends Component { setup() { this.elem = this.$el; + this.editContainer = this.$refs.editContainer; + this.input = this.$refs.input; - this.pageId = this.$opts.pageId; - this.textDirection = this.$opts.textDirection; - this.isDarkMode = document.documentElement.classList.contains('dark-mode'); - - this.tinyMceConfig = buildEditorConfig({ - language: this.$opts.language, - containerElement: this.elem, - darkMode: this.isDarkMode, - textDirection: this.textDirection, - drawioUrl: this.getDrawIoUrl(), - pageId: Number(this.pageId), - translations: { - imageUploadErrorText: this.$opts.imageUploadErrorText, - serverUploadLimitText: this.$opts.serverUploadLimitText, - }, - translationMap: window.editor_translations, + /** @var {SimpleWysiwygEditorInterface|null} */ + this.editor = null; + + const translations = { + ...window.editor_translations, + imageUploadErrorText: this.$opts.imageUploadErrorText, + serverUploadLimitText: this.$opts.serverUploadLimitText, + }; + + window.importVersioned('wysiwyg').then(wysiwyg => { + const editorContent = this.input.value; + this.editor = wysiwyg.createPageEditorInstance(this.editContainer, editorContent, { + drawioUrl: this.getDrawIoUrl(), + pageId: Number(this.$opts.pageId), + darkMode: document.documentElement.classList.contains('dark-mode'), + textDirection: this.$opts.textDirection, + translations, + }); }); - window.$events.emitPublic(this.elem, 'editor-tinymce::pre-init', {config: this.tinyMceConfig}); - window.tinymce.init(this.tinyMceConfig); + let handlingFormSubmit = false; + this.input.form.addEventListener('submit', event => { + if (!this.editor) { + return; + } + + if (!handlingFormSubmit) { + event.preventDefault(); + handlingFormSubmit = true; + this.editor.getContentAsHtml().then(html => { + this.input.value = html; + this.input.form.submit(); + }); + } else { + handlingFormSubmit = false; + } + }); } getDrawIoUrl() { @@ -36,4 +54,15 @@ export class WysiwygEditor extends Component { return ''; } -} \ No newline at end of file + /** + * Get the content of this editor. + * Used by the parent page editor component. + * @return {Promise<{html: String}>} + */ + async getContent() { + return { + html: await this.editor.getContentAsHtml(), + }; + } + +}