1 import {$getSelection, createEditor, CreateEditorArgs, isCurrentlyReadOnlyMode, 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 "./utils/actions";
8 import {registerTableResizer} from "./ui/framework/helpers/table-resizer";
9 import {EditorUiContext} from "./ui/framework/core";
10 import {listen as listenToCommonEvents} from "./services/common-events";
11 import {registerDropPasteHandling} from "./services/drop-paste-handling";
12 import {registerTaskListHandler} from "./ui/framework/helpers/task-list-handler";
13 import {registerTableSelectionHandler} from "./ui/framework/helpers/table-selection-handler";
14 import {el} from "./utils/dom";
15 import {registerShortcuts} from "./services/shortcuts";
16 import {registerNodeResizer} from "./ui/framework/helpers/image-resizer";
18 export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
19 const config: CreateEditorArgs = {
20 namespace: 'BookStackPageEditor',
21 nodes: getNodesForPageEditor(),
22 onError: console.error,
25 bold: 'editor-theme-bold',
26 code: 'editor-theme-code',
27 italic: 'editor-theme-italic',
28 strikethrough: 'editor-theme-strikethrough',
29 subscript: 'editor-theme-subscript',
30 superscript: 'editor-theme-superscript',
31 underline: 'editor-theme-underline',
32 underlineStrikethrough: 'editor-theme-underline-strikethrough',
37 const editArea = el('div', {
38 contenteditable: 'true',
39 class: 'editor-content-area page-content',
41 const editWrap = el('div', {
42 class: 'editor-content-wrap',
44 container.append(editWrap);
45 container.classList.add('editor-container');
47 const editor = createEditor(config);
48 editor.setRootElement(editArea);
49 const context: EditorUiContext = buildEditorUI(container, editArea, editWrap, editor, options);
52 registerRichText(editor),
53 registerHistory(editor, createEmptyHistoryState(), 300),
54 registerShortcuts(context),
55 registerTableResizer(editor, editWrap),
56 registerTableSelectionHandler(editor),
57 registerTaskListHandler(editor, editArea),
58 registerDropPasteHandling(context),
59 registerNodeResizer(context),
62 listenToCommonEvents(editor);
64 setEditorContentFromHtml(editor, htmlContent);
66 const debugView = document.getElementById('lexical-debug');
68 debugView.hidden = true;
71 let changeFromLoading = true;
72 editor.registerUpdateListener(({dirtyElements, dirtyLeaves, editorState, prevEditorState}) => {
73 // Watch for selection changes to update the UI on change
74 // Used to be done via SELECTION_CHANGE_COMMAND but this would not always emit
75 // for all selection changes, so this proved more reliable.
76 const selectionChange = !(prevEditorState._selection?.is(editorState._selection) || false);
77 if (selectionChange) {
79 const selection = $getSelection();
80 context.manager.triggerStateUpdate({
86 // Emit change event to component system (for draft detection) on actual user content change
87 if (dirtyElements.size > 0 || dirtyLeaves.size > 0) {
88 if (changeFromLoading) {
89 changeFromLoading = false;
91 window.$events.emit('editor-html-change', '');
96 // console.log('editorState', editorState.toJSON());
98 debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
103 window.debugEditorState = () => {
104 console.log(editor.getEditorState().toJSON());
107 registerCommonNodeMutationListeners(context);
109 return new SimpleWysiwygEditorInterface(editor);
112 export class SimpleWysiwygEditorInterface {
113 protected editor: LexicalEditor;
115 constructor(editor: LexicalEditor) {
116 this.editor = editor;
119 async getContentAsHtml(): Promise<string> {
120 return await getEditorContentAsHtml(this.editor);