Lexical: Linked up saving logic of editor via interface

This commit is contained in:
Dan Brown 2024-07-04 13:09:53 +01:00
parent a8f1160743
commit 04c7e680fd
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
5 changed files with 52 additions and 15 deletions

View File

@ -133,9 +133,9 @@ export class MarkdownEditor extends Component {
/** /**
* Get the content of this editor. * Get the content of this editor.
* Used by the parent page editor component. * Used by the parent page editor component.
* @return {{html: String, markdown: String}} * @return {Promise<{html: String, markdown: String}>}
*/ */
getContent() { async getContent() {
return this.editor.actions.getContent(); return this.editor.actions.getContent();
} }

View File

@ -118,7 +118,7 @@ export class PageEditor extends Component {
async saveDraft() { async saveDraft() {
const data = {name: this.titleElem.value.trim()}; const data = {name: this.titleElem.value.trim()};
const editorContent = this.getEditorComponent().getContent(); const editorContent = await this.getEditorComponent().getContent();
Object.assign(data, editorContent); Object.assign(data, editorContent);
let didSave = false; let didSave = false;
@ -235,10 +235,12 @@ export class PageEditor extends Component {
} }
/** /**
* @return MarkdownEditor|WysiwygEditor * @return {MarkdownEditor|WysiwygEditor|WysiwygEditorTinymce}
*/ */
getEditorComponent() { getEditorComponent() {
return window.$components.first('markdown-editor') || window.$components.first('wysiwyg-editor'); return window.$components.first('markdown-editor')
|| window.$components.first('wysiwyg-editor')
|| window.$components.first('wysiwyg-editor-tinymce');
} }
} }

View File

@ -37,9 +37,9 @@ export class WysiwygEditorTinymce extends Component {
/** /**
* Get the content of this editor. * Get the content of this editor.
* Used by the parent page editor component. * Used by the parent page editor component.
* @return {{html: String}} * @return {Promise<{html: String}>}
*/ */
getContent() { async getContent() {
return { return {
html: this.editor.getContent(), html: this.editor.getContent(),
}; };

View File

@ -7,13 +7,35 @@ export class WysiwygEditor extends Component {
this.editContainer = this.$refs.editContainer; this.editContainer = this.$refs.editContainer;
this.input = this.$refs.input; this.input = this.$refs.input;
/** @var {SimpleWysiwygEditorInterface|null} */
this.editor = null;
window.importVersioned('wysiwyg').then(wysiwyg => { window.importVersioned('wysiwyg').then(wysiwyg => {
const editorContent = this.input.value; const editorContent = this.input.value;
wysiwyg.createPageEditorInstance(this.editContainer, editorContent); this.editor = wysiwyg.createPageEditorInstance(this.editContainer, editorContent);
});
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() { getDrawIoUrl() {
// TODO
const drawioUrlElem = document.querySelector('[drawio-url]'); const drawioUrlElem = document.querySelector('[drawio-url]');
if (drawioUrlElem) { if (drawioUrlElem) {
return drawioUrlElem.getAttribute('drawio-url'); return drawioUrlElem.getAttribute('drawio-url');
@ -24,12 +46,11 @@ export class WysiwygEditor extends Component {
/** /**
* Get the content of this editor. * Get the content of this editor.
* Used by the parent page editor component. * Used by the parent page editor component.
* @return {{html: String}} * @return {Promise<{html: String}>}
*/ */
getContent() { async getContent() {
// TODO - Update
return { return {
html: this.editor.getContent(), html: await this.editor.getContentAsHtml(),
}; };
} }

View File

@ -1,14 +1,14 @@
import {createEditor, CreateEditorArgs} from 'lexical'; import {createEditor, CreateEditorArgs, LexicalEditor} from 'lexical';
import {createEmptyHistoryState, registerHistory} from '@lexical/history'; import {createEmptyHistoryState, registerHistory} from '@lexical/history';
import {registerRichText} from '@lexical/rich-text'; import {registerRichText} from '@lexical/rich-text';
import {mergeRegister} from '@lexical/utils'; import {mergeRegister} from '@lexical/utils';
import {getNodesForPageEditor} from './nodes'; import {getNodesForPageEditor} from './nodes';
import {buildEditorUI} from "./ui"; import {buildEditorUI} from "./ui";
import {setEditorContentFromHtml} from "./actions"; import {getEditorContentAsHtml, setEditorContentFromHtml} from "./actions";
import {registerTableResizer} from "./ui/framework/helpers/table-resizer"; import {registerTableResizer} from "./ui/framework/helpers/table-resizer";
import {el} from "./helpers"; import {el} from "./helpers";
export function createPageEditorInstance(container: HTMLElement, htmlContent: string) { export function createPageEditorInstance(container: HTMLElement, htmlContent: string): SimpleWysiwygEditorInterface {
const config: CreateEditorArgs = { const config: CreateEditorArgs = {
namespace: 'BookStackPageEditor', namespace: 'BookStackPageEditor',
nodes: getNodesForPageEditor(), nodes: getNodesForPageEditor(),
@ -57,4 +57,18 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
}); });
buildEditorUI(container, editArea, editor); buildEditorUI(container, editArea, editor);
return new SimpleWysiwygEditorInterface(editor);
} }
export class SimpleWysiwygEditorInterface {
protected editor: LexicalEditor;
constructor(editor: LexicalEditor) {
this.editor = editor;
}
async getContentAsHtml(): Promise<string> {
return await getEditorContentAsHtml(this.editor);
}
}