]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/index.ts
Lexical: Range of bug fixes, Updated lexical version
[bookstack] / resources / js / wysiwyg / index.ts
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";
17
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,
23         theme: {
24             text: {
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',
33             }
34         }
35     };
36
37     const editArea = el('div', {
38         contenteditable: 'true',
39         class: 'editor-content-area page-content',
40     });
41     const editWrap = el('div', {
42         class: 'editor-content-wrap',
43     }, [editArea]);
44     container.append(editWrap);
45     container.classList.add('editor-container');
46
47     const editor = createEditor(config);
48     editor.setRootElement(editArea);
49     const context: EditorUiContext = buildEditorUI(container, editArea, editWrap, editor, options);
50
51     mergeRegister(
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),
60     );
61
62     listenToCommonEvents(editor);
63
64     setEditorContentFromHtml(editor, htmlContent);
65
66     const debugView = document.getElementById('lexical-debug');
67     if (debugView) {
68         debugView.hidden = true;
69     }
70
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) {
78             editor.update(() => {
79                 const selection = $getSelection();
80                 context.manager.triggerStateUpdate({
81                     editor, selection,
82                 });
83             });
84         }
85
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;
90             } else {
91                 window.$events.emit('editor-html-change', '');
92             }
93         }
94
95         // Debug logic
96         // console.log('editorState', editorState.toJSON());
97         if (debugView) {
98             debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
99         }
100     });
101
102     // @ts-ignore
103     window.debugEditorState = () => {
104         console.log(editor.getEditorState().toJSON());
105     };
106
107     registerCommonNodeMutationListeners(context);
108
109     return new SimpleWysiwygEditorInterface(editor);
110 }
111
112 export class SimpleWysiwygEditorInterface {
113     protected editor: LexicalEditor;
114
115     constructor(editor: LexicalEditor) {
116         this.editor = editor;
117     }
118
119     async getContentAsHtml(): Promise<string> {
120         return await getEditorContentAsHtml(this.editor);
121     }
122 }