1 import {$getSelection, 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 "./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/node-resizer";
17 import {registerKeyboardHandling} from "./services/keyboard-handling";
18 import {registerAutoLinks} from "./services/auto-links";
20 export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
21 const config: CreateEditorArgs = {
22 namespace: 'BookStackPageEditor',
23 nodes: getNodesForPageEditor(),
24 onError: console.error,
27 bold: 'editor-theme-bold',
28 code: 'editor-theme-code',
29 italic: 'editor-theme-italic',
30 strikethrough: 'editor-theme-strikethrough',
31 subscript: 'editor-theme-subscript',
32 superscript: 'editor-theme-superscript',
33 underline: 'editor-theme-underline',
34 underlineStrikethrough: 'editor-theme-underline-strikethrough',
39 const editArea = el('div', {
40 contenteditable: 'true',
41 class: 'editor-content-area page-content',
43 const editWrap = el('div', {
44 class: 'editor-content-wrap',
47 container.append(editWrap);
48 container.classList.add('editor-container');
49 container.setAttribute('dir', options.textDirection);
50 if (options.darkMode) {
51 container.classList.add('editor-dark');
54 const editor = createEditor(config);
55 editor.setRootElement(editArea);
56 const context: EditorUiContext = buildEditorUI(container, editArea, editWrap, editor, options);
59 registerRichText(editor),
60 registerHistory(editor, createEmptyHistoryState(), 300),
61 registerShortcuts(context),
62 registerKeyboardHandling(context),
63 registerTableResizer(editor, editWrap),
64 registerTableSelectionHandler(editor),
65 registerTaskListHandler(editor, editArea),
66 registerDropPasteHandling(context),
67 registerNodeResizer(context),
68 registerAutoLinks(editor),
71 listenToCommonEvents(editor);
73 setEditorContentFromHtml(editor, htmlContent);
75 const debugView = document.getElementById('lexical-debug');
77 debugView.hidden = true;
78 editor.registerUpdateListener(({dirtyElements, dirtyLeaves, editorState, prevEditorState}) => {
80 // console.log('editorState', editorState.toJSON());
81 debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
86 window.debugEditorState = () => {
87 console.log(editor.getEditorState().toJSON());
90 registerCommonNodeMutationListeners(context);
92 return new SimpleWysiwygEditorInterface(editor);
95 export class SimpleWysiwygEditorInterface {
96 protected editor: LexicalEditor;
98 constructor(editor: LexicalEditor) {
102 async getContentAsHtml(): Promise<string> {
103 return await getEditorContentAsHtml(this.editor);