1 import {createEditor, CreateEditorArgs, LexicalEditor} from 'lexical';
2 import {createEmptyHistoryState, registerHistory} from '@lexical/history';
3 import {registerRichText} from '@lexical/rich-text';
4 import {mergeRegister} from '@lexical/utils';
5 import {getNodesForPageEditor, registerCommonNodeMutationListeners} from './nodes';
6 import {buildEditorUI} from "./ui";
7 import {getEditorContentAsHtml, setEditorContentFromHtml} from "./actions";
8 import {registerTableResizer} from "./ui/framework/helpers/table-resizer";
9 import {el} from "./helpers";
10 import {EditorUiContext} from "./ui/framework/core";
12 export function createPageEditorInstance(container: HTMLElement, htmlContent: string): SimpleWysiwygEditorInterface {
13 const config: CreateEditorArgs = {
14 namespace: 'BookStackPageEditor',
15 nodes: getNodesForPageEditor(),
16 onError: console.error,
19 bold: 'editor-theme-bold',
20 code: 'editor-theme-code',
21 italic: 'editor-theme-italic',
22 strikethrough: 'editor-theme-strikethrough',
23 subscript: 'editor-theme-subscript',
24 superscript: 'editor-theme-superscript',
25 underline: 'editor-theme-underline',
26 underlineStrikethrough: 'editor-theme-underline-strikethrough',
31 const editArea = el('div', {
32 contenteditable: 'true',
33 class: 'editor-content-area page-content',
35 const editWrap = el('div', {
36 class: 'editor-content-wrap',
38 container.append(editWrap);
39 container.classList.add('editor-container');
41 const editor = createEditor(config);
42 editor.setRootElement(editArea);
45 registerRichText(editor),
46 registerHistory(editor, createEmptyHistoryState(), 300),
47 registerTableResizer(editor, editArea),
50 setEditorContentFromHtml(editor, htmlContent);
52 const debugView = document.getElementById('lexical-debug');
54 debugView.hidden = true;
56 editor.registerUpdateListener(({editorState}) => {
57 console.log('editorState', editorState.toJSON());
59 debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
63 const context: EditorUiContext = buildEditorUI(container, editArea, editor);
64 registerCommonNodeMutationListeners(context);
66 return new SimpleWysiwygEditorInterface(editor);
69 export class SimpleWysiwygEditorInterface {
70 protected editor: LexicalEditor;
72 constructor(editor: LexicalEditor) {
76 async getContentAsHtml(): Promise<string> {
77 return await getEditorContentAsHtml(this.editor);