]> BookStack Code Mirror - bookstack/blob - resources/js/components/wysiwyg-editor.js
Merge branch 'development' of github.com:BookStackApp/BookStack into development
[bookstack] / resources / js / components / wysiwyg-editor.js
1 import {Component} from './component';
2
3 export class WysiwygEditor extends Component {
4
5     setup() {
6         this.elem = this.$el;
7         this.editContainer = this.$refs.editContainer;
8         this.input = this.$refs.input;
9
10         /** @var {SimpleWysiwygEditorInterface|null} */
11         this.editor = null;
12
13         const translations = {
14             ...window.editor_translations,
15             imageUploadErrorText: this.$opts.imageUploadErrorText,
16             serverUploadLimitText: this.$opts.serverUploadLimitText,
17         };
18
19         window.importVersioned('wysiwyg').then(wysiwyg => {
20             const editorContent = this.input.value;
21             this.editor = wysiwyg.createPageEditorInstance(this.editContainer, editorContent, {
22                 drawioUrl: this.getDrawIoUrl(),
23                 pageId: Number(this.$opts.pageId),
24                 darkMode: document.documentElement.classList.contains('dark-mode'),
25                 textDirection: this.$opts.textDirection,
26                 translations,
27             });
28             window.wysiwyg = this.editor;
29         });
30
31         let handlingFormSubmit = false;
32         this.input.form.addEventListener('submit', event => {
33             if (!this.editor) {
34                 return;
35             }
36
37             if (!handlingFormSubmit) {
38                 event.preventDefault();
39                 handlingFormSubmit = true;
40                 this.editor.getContentAsHtml().then(html => {
41                     this.input.value = html;
42                     setTimeout(() => {
43                         this.input.form.requestSubmit();
44                     }, 5);
45                 });
46             } else {
47                 handlingFormSubmit = false;
48             }
49         });
50     }
51
52     getDrawIoUrl() {
53         const drawioUrlElem = document.querySelector('[drawio-url]');
54         if (drawioUrlElem) {
55             return drawioUrlElem.getAttribute('drawio-url');
56         }
57         return '';
58     }
59
60     /**
61      * Get the content of this editor.
62      * Used by the parent page editor component.
63      * @return {Promise<{html: String}>}
64      */
65     async getContent() {
66         return {
67             html: await this.editor.getContentAsHtml(),
68         };
69     }
70
71 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.