1 import {createEditor, CreateEditorArgs} 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} from './nodes';
6 import {buildEditorUI} from "./ui";
7 import {setEditorContentFromHtml} from "./actions";
9 export function createPageEditorInstance(editArea: HTMLElement) {
10 const config: CreateEditorArgs = {
11 namespace: 'BookStackPageEditor',
12 nodes: getNodesForPageEditor(),
13 onError: console.error,
16 const startingHtml = editArea.innerHTML;
18 const editor = createEditor(config);
19 editor.setRootElement(editArea);
22 registerRichText(editor),
23 registerHistory(editor, createEmptyHistoryState(), 300),
26 setEditorContentFromHtml(editor, startingHtml);
28 const debugView = document.getElementById('lexical-debug');
29 editor.registerUpdateListener(({editorState}) => {
30 console.log('editorState', editorState.toJSON());
32 debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
36 buildEditorUI(editArea, editor);
38 // Example of creating, registering and using a custom command
40 // const SET_BLOCK_CALLOUT_COMMAND = createCommand();
41 // editor.registerCommand(SET_BLOCK_CALLOUT_COMMAND, (category: CalloutCategory = 'info') => {
42 // const selection = $getSelection();
43 // const blockElement = $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]);
44 // if ($isCalloutNode(blockElement)) {
45 // $setBlocksType(selection, $createParagraphNode);
47 // $setBlocksType(selection, () => $createCalloutNode(category));
50 // }, COMMAND_PRIORITY_LOW);
52 // const button = document.getElementById('lexical-button');
53 // button.addEventListener('click', event => {
54 // editor.dispatchCommand(SET_BLOCK_CALLOUT_COMMAND, 'info');