]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/index.ts
Lexical: Added custom alignment handling for blocks
[bookstack] / resources / js / wysiwyg / index.ts
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 "./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 {handleDropEvents} from "./services/drop-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
16 export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
17     const config: CreateEditorArgs = {
18         namespace: 'BookStackPageEditor',
19         nodes: getNodesForPageEditor(),
20         onError: console.error,
21         theme: {
22             text: {
23                 bold: 'editor-theme-bold',
24                 code: 'editor-theme-code',
25                 italic: 'editor-theme-italic',
26                 strikethrough: 'editor-theme-strikethrough',
27                 subscript: 'editor-theme-subscript',
28                 superscript: 'editor-theme-superscript',
29                 underline: 'editor-theme-underline',
30                 underlineStrikethrough: 'editor-theme-underline-strikethrough',
31             }
32         }
33     };
34
35     const editArea = el('div', {
36         contenteditable: 'true',
37         class: 'editor-content-area page-content',
38     });
39     const editWrap = el('div', {
40         class: 'editor-content-wrap',
41     }, [editArea]);
42     container.append(editWrap);
43     container.classList.add('editor-container');
44
45     const editor = createEditor(config);
46     editor.setRootElement(editArea);
47
48     mergeRegister(
49         registerRichText(editor),
50         registerHistory(editor, createEmptyHistoryState(), 300),
51         registerTableResizer(editor, editWrap),
52         registerTableSelectionHandler(editor),
53         registerTaskListHandler(editor, editArea),
54     );
55
56     listenToCommonEvents(editor);
57     handleDropEvents(editor);
58
59     setEditorContentFromHtml(editor, htmlContent);
60
61     const debugView = document.getElementById('lexical-debug');
62     if (debugView) {
63         debugView.hidden = true;
64     }
65
66     let changeFromLoading = true;
67     editor.registerUpdateListener(({editorState, dirtyElements, dirtyLeaves}) => {
68
69         // Emit change event to component system (for draft detection) on actual user content change
70         if (dirtyElements.size > 0 || dirtyLeaves.size > 0) {
71             if (changeFromLoading) {
72                 changeFromLoading = false;
73             } else {
74                 window.$events.emit('editor-html-change', '');
75             }
76         }
77
78         // Debug logic
79         // console.log('editorState', editorState.toJSON());
80         if (debugView) {
81             debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
82         }
83     });
84
85     // @ts-ignore
86     window.debugEditorState = () => {
87         console.log(editor.getEditorState().toJSON());
88     };
89
90     const context: EditorUiContext = buildEditorUI(container, editArea, editWrap, editor, options);
91     registerCommonNodeMutationListeners(context);
92
93     return new SimpleWysiwygEditorInterface(editor);
94 }
95
96 export class SimpleWysiwygEditorInterface {
97     protected editor: LexicalEditor;
98
99     constructor(editor: LexicalEditor) {
100         this.editor = editor;
101     }
102
103     async getContentAsHtml(): Promise<string> {
104         return await getEditorContentAsHtml(this.editor);
105     }
106 }