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";
11 import {listen as listenToCommonEvents} from "./common-events";
12 import {handleDropEvents} from "./drop-handling";
14 export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
15 const config: CreateEditorArgs = {
16 namespace: 'BookStackPageEditor',
17 nodes: getNodesForPageEditor(),
18 onError: console.error,
21 bold: 'editor-theme-bold',
22 code: 'editor-theme-code',
23 italic: 'editor-theme-italic',
24 strikethrough: 'editor-theme-strikethrough',
25 subscript: 'editor-theme-subscript',
26 superscript: 'editor-theme-superscript',
27 underline: 'editor-theme-underline',
28 underlineStrikethrough: 'editor-theme-underline-strikethrough',
33 const editArea = el('div', {
34 contenteditable: 'true',
35 class: 'editor-content-area page-content',
37 const editWrap = el('div', {
38 class: 'editor-content-wrap',
40 container.append(editWrap);
41 container.classList.add('editor-container');
43 const editor = createEditor(config);
44 editor.setRootElement(editArea);
47 registerRichText(editor),
48 registerHistory(editor, createEmptyHistoryState(), 300),
49 registerTableResizer(editor, editWrap),
52 listenToCommonEvents(editor);
53 handleDropEvents(editor);
55 setEditorContentFromHtml(editor, htmlContent);
57 const debugView = document.getElementById('lexical-debug');
59 debugView.hidden = true;
61 editor.registerUpdateListener(({editorState}) => {
62 console.log('editorState', editorState.toJSON());
64 debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
68 const context: EditorUiContext = buildEditorUI(container, editArea, editWrap, editor, options);
69 registerCommonNodeMutationListeners(context);
71 return new SimpleWysiwygEditorInterface(editor);
74 export class SimpleWysiwygEditorInterface {
75 protected editor: LexicalEditor;
77 constructor(editor: LexicalEditor) {
81 async getContentAsHtml(): Promise<string> {
82 return await getEditorContentAsHtml(this.editor);