2023-04-19 05:20:02 +08:00
|
|
|
import {Component} from './component';
|
2019-08-12 03:04:43 +08:00
|
|
|
|
2022-11-16 21:04:22 +08:00
|
|
|
export class WysiwygEditor extends Component {
|
2017-09-23 19:24:06 +08:00
|
|
|
|
2020-07-06 04:18:17 +08:00
|
|
|
setup() {
|
|
|
|
this.elem = this.$el;
|
2024-07-01 17:44:23 +08:00
|
|
|
this.editContainer = this.$refs.editContainer;
|
2024-07-01 22:10:22 +08:00
|
|
|
this.input = this.$refs.input;
|
2020-07-06 04:18:17 +08:00
|
|
|
|
2024-07-04 20:09:53 +08:00
|
|
|
/** @var {SimpleWysiwygEditorInterface|null} */
|
|
|
|
this.editor = null;
|
|
|
|
|
2024-05-27 22:39:41 +08:00
|
|
|
window.importVersioned('wysiwyg').then(wysiwyg => {
|
2024-07-01 22:10:22 +08:00
|
|
|
const editorContent = this.input.value;
|
2024-07-19 19:09:41 +08:00
|
|
|
this.editor = wysiwyg.createPageEditorInstance(this.editContainer, editorContent, {
|
|
|
|
drawioUrl: this.getDrawIoUrl(),
|
|
|
|
pageId: Number(this.$opts.pageId),
|
2024-09-15 23:10:46 +08:00
|
|
|
darkMode: document.documentElement.classList.contains('dark-mode'),
|
|
|
|
textDirection: this.$opts.textDirection,
|
2024-07-19 19:09:41 +08:00
|
|
|
translations: {
|
|
|
|
imageUploadErrorText: this.$opts.imageUploadErrorText,
|
|
|
|
serverUploadLimitText: this.$opts.serverUploadLimitText,
|
|
|
|
},
|
|
|
|
});
|
2024-07-04 20:09:53 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-02-23 20:30:27 +08:00
|
|
|
});
|
2018-04-01 20:21:11 +08:00
|
|
|
}
|
|
|
|
|
2022-02-06 07:15:58 +08:00
|
|
|
getDrawIoUrl() {
|
2020-04-06 00:27:16 +08:00
|
|
|
const drawioUrlElem = document.querySelector('[drawio-url]');
|
|
|
|
if (drawioUrlElem) {
|
2022-02-06 07:15:58 +08:00
|
|
|
return drawioUrlElem.getAttribute('drawio-url');
|
2018-04-01 20:21:11 +08:00
|
|
|
}
|
2022-02-06 07:15:58 +08:00
|
|
|
return '';
|
2017-09-23 19:24:06 +08:00
|
|
|
}
|
|
|
|
|
2023-02-23 20:30:27 +08:00
|
|
|
/**
|
|
|
|
* Get the content of this editor.
|
|
|
|
* Used by the parent page editor component.
|
2024-07-04 20:09:53 +08:00
|
|
|
* @return {Promise<{html: String}>}
|
2023-02-23 20:30:27 +08:00
|
|
|
*/
|
2024-07-04 20:09:53 +08:00
|
|
|
async getContent() {
|
2023-02-23 20:30:27 +08:00
|
|
|
return {
|
2024-07-04 20:09:53 +08:00
|
|
|
html: await this.editor.getContentAsHtml(),
|
2023-02-23 20:30:27 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|