2024-06-12 09:01:36 -04:00
|
|
|
import {createEditor, CreateEditorArgs} from 'lexical';
|
2024-05-27 10:39:41 -04:00
|
|
|
import {createEmptyHistoryState, registerHistory} from '@lexical/history';
|
2024-05-27 15:23:45 -04:00
|
|
|
import {registerRichText} from '@lexical/rich-text';
|
2024-05-28 13:04:48 -04:00
|
|
|
import {mergeRegister} from '@lexical/utils';
|
2024-05-27 18:50:28 -04:00
|
|
|
import {getNodesForPageEditor} from './nodes';
|
2024-05-28 13:04:48 -04:00
|
|
|
import {buildEditorUI} from "./ui";
|
2024-06-12 09:01:36 -04:00
|
|
|
import {setEditorContentFromHtml} from "./actions";
|
2024-06-21 08:47:47 -04:00
|
|
|
import {registerTableResizer} from "./ui/framework/helpers/table-resizer";
|
2024-05-27 10:39:41 -04:00
|
|
|
|
2024-05-27 18:50:28 -04:00
|
|
|
export function createPageEditorInstance(editArea: HTMLElement) {
|
|
|
|
const config: CreateEditorArgs = {
|
2024-05-27 10:39:41 -04:00
|
|
|
namespace: 'BookStackPageEditor',
|
2024-05-27 15:23:45 -04:00
|
|
|
nodes: getNodesForPageEditor(),
|
2024-05-27 10:39:41 -04:00
|
|
|
onError: console.error,
|
2024-06-23 06:36:48 -04:00
|
|
|
theme: {
|
|
|
|
text: {
|
|
|
|
bold: 'editor-theme-bold',
|
|
|
|
code: 'editor-theme-code',
|
|
|
|
italic: 'editor-theme-italic',
|
|
|
|
strikethrough: 'editor-theme-strikethrough',
|
|
|
|
subscript: 'editor-theme-subscript',
|
|
|
|
superscript: 'editor-theme-superscript',
|
|
|
|
underline: 'editor-theme-underline',
|
|
|
|
underlineStrikethrough: 'editor-theme-underline-strikethrough',
|
|
|
|
}
|
|
|
|
}
|
2024-05-27 10:39:41 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const startingHtml = editArea.innerHTML;
|
|
|
|
|
|
|
|
const editor = createEditor(config);
|
|
|
|
editor.setRootElement(editArea);
|
|
|
|
|
|
|
|
mergeRegister(
|
|
|
|
registerRichText(editor),
|
|
|
|
registerHistory(editor, createEmptyHistoryState(), 300),
|
2024-06-21 08:47:47 -04:00
|
|
|
registerTableResizer(editor, editArea),
|
2024-05-27 10:39:41 -04:00
|
|
|
);
|
|
|
|
|
2024-06-12 09:01:36 -04:00
|
|
|
setEditorContentFromHtml(editor, startingHtml);
|
2024-05-27 10:39:41 -04:00
|
|
|
|
|
|
|
const debugView = document.getElementById('lexical-debug');
|
|
|
|
editor.registerUpdateListener(({editorState}) => {
|
|
|
|
console.log('editorState', editorState.toJSON());
|
2024-05-28 13:04:48 -04:00
|
|
|
if (debugView) {
|
|
|
|
debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
|
|
|
|
}
|
2024-05-27 10:39:41 -04:00
|
|
|
});
|
2024-05-27 15:23:45 -04:00
|
|
|
|
2024-05-28 13:04:48 -04:00
|
|
|
buildEditorUI(editArea, editor);
|
2024-05-27 15:23:45 -04:00
|
|
|
|
2024-05-28 13:04:48 -04:00
|
|
|
// Example of creating, registering and using a custom command
|
2024-05-27 15:23:45 -04:00
|
|
|
|
2024-05-28 13:04:48 -04:00
|
|
|
// const SET_BLOCK_CALLOUT_COMMAND = createCommand();
|
|
|
|
// editor.registerCommand(SET_BLOCK_CALLOUT_COMMAND, (category: CalloutCategory = 'info') => {
|
|
|
|
// const selection = $getSelection();
|
|
|
|
// const blockElement = $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]);
|
|
|
|
// if ($isCalloutNode(blockElement)) {
|
|
|
|
// $setBlocksType(selection, $createParagraphNode);
|
|
|
|
// } else {
|
|
|
|
// $setBlocksType(selection, () => $createCalloutNode(category));
|
|
|
|
// }
|
|
|
|
// return true;
|
|
|
|
// }, COMMAND_PRIORITY_LOW);
|
|
|
|
//
|
|
|
|
// const button = document.getElementById('lexical-button');
|
|
|
|
// button.addEventListener('click', event => {
|
|
|
|
// editor.dispatchCommand(SET_BLOCK_CALLOUT_COMMAND, 'info');
|
|
|
|
// });
|
2024-05-27 18:50:28 -04:00
|
|
|
}
|