2025-06-24 17:47:53 +01:00
|
|
|
import {createEditor, LexicalEditor} from 'lexical';
|
2024-05-27 15:39:41 +01:00
|
|
|
import {createEmptyHistoryState, registerHistory} from '@lexical/history';
|
2024-05-27 20:23:45 +01:00
|
|
|
import {registerRichText} from '@lexical/rich-text';
|
2024-05-28 18:04:48 +01:00
|
|
|
import {mergeRegister} from '@lexical/utils';
|
2025-06-25 14:16:01 +01:00
|
|
|
import {getNodesForBasicEditor, getNodesForPageEditor, registerCommonNodeMutationListeners} from './nodes';
|
2024-05-28 18:04:48 +01:00
|
|
|
import {buildEditorUI} from "./ui";
|
2025-06-25 14:16:01 +01:00
|
|
|
import {focusEditor, getEditorContentAsHtml, setEditorContentFromHtml} from "./utils/actions";
|
2024-06-21 13:47:47 +01:00
|
|
|
import {registerTableResizer} from "./ui/framework/helpers/table-resizer";
|
2024-07-16 16:36:08 +01:00
|
|
|
import {EditorUiContext} from "./ui/framework/core";
|
2024-08-03 18:14:01 +01:00
|
|
|
import {listen as listenToCommonEvents} from "./services/common-events";
|
2024-08-21 12:59:45 +01:00
|
|
|
import {registerDropPasteHandling} from "./services/drop-paste-handling";
|
2024-07-30 14:42:19 +01:00
|
|
|
import {registerTaskListHandler} from "./ui/framework/helpers/task-list-handler";
|
2024-08-02 15:28:54 +01:00
|
|
|
import {registerTableSelectionHandler} from "./ui/framework/helpers/table-selection-handler";
|
2024-08-20 13:07:33 +01:00
|
|
|
import {registerShortcuts} from "./services/shortcuts";
|
2024-09-09 12:28:01 +01:00
|
|
|
import {registerNodeResizer} from "./ui/framework/helpers/node-resizer";
|
2024-09-09 18:33:54 +01:00
|
|
|
import {registerKeyboardHandling} from "./services/keyboard-handling";
|
2024-12-14 12:35:13 +00:00
|
|
|
import {registerAutoLinks} from "./services/auto-links";
|
2025-06-25 14:16:01 +01:00
|
|
|
import {contextToolbars, getBasicEditorToolbar, getMainEditorFullToolbar} from "./ui/defaults/toolbars";
|
2025-06-24 17:47:53 +01:00
|
|
|
import {modals} from "./ui/defaults/modals";
|
|
|
|
|
import {CodeBlockDecorator} from "./ui/decorators/code-block";
|
|
|
|
|
import {DiagramDecorator} from "./ui/decorators/diagram";
|
|
|
|
|
|
|
|
|
|
const 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 15:39:41 +01:00
|
|
|
|
2024-07-19 12:09:41 +01:00
|
|
|
export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
|
2025-06-24 17:47:53 +01:00
|
|
|
const editor = createEditor({
|
2024-05-27 15:39:41 +01:00
|
|
|
namespace: 'BookStackPageEditor',
|
2024-05-27 20:23:45 +01:00
|
|
|
nodes: getNodesForPageEditor(),
|
2024-05-27 15:39:41 +01:00
|
|
|
onError: console.error,
|
2025-06-24 17:47:53 +01:00
|
|
|
theme: theme,
|
2024-07-01 10:44:23 +01:00
|
|
|
});
|
2025-06-24 17:47:53 +01:00
|
|
|
const context: EditorUiContext = buildEditorUI(container, editor, {
|
|
|
|
|
...options,
|
|
|
|
|
editorClass: 'page-content',
|
|
|
|
|
});
|
|
|
|
|
editor.setRootElement(context.editorDOM);
|
2024-05-27 15:39:41 +01:00
|
|
|
|
|
|
|
|
mergeRegister(
|
|
|
|
|
registerRichText(editor),
|
|
|
|
|
registerHistory(editor, createEmptyHistoryState(), 300),
|
2024-08-20 14:54:53 +01:00
|
|
|
registerShortcuts(context),
|
2024-09-09 18:33:54 +01:00
|
|
|
registerKeyboardHandling(context),
|
2025-06-24 17:47:53 +01:00
|
|
|
registerTableResizer(editor, context.scrollDOM),
|
2024-08-02 15:28:54 +01:00
|
|
|
registerTableSelectionHandler(editor),
|
2025-06-24 17:47:53 +01:00
|
|
|
registerTaskListHandler(editor, context.editorDOM),
|
2024-08-21 12:59:45 +01:00
|
|
|
registerDropPasteHandling(context),
|
2024-09-08 13:37:13 +01:00
|
|
|
registerNodeResizer(context),
|
2024-12-14 12:35:13 +00:00
|
|
|
registerAutoLinks(editor),
|
2024-05-27 15:39:41 +01:00
|
|
|
);
|
|
|
|
|
|
2025-06-24 17:47:53 +01:00
|
|
|
// Register toolbars, modals & decorators
|
|
|
|
|
context.manager.setToolbar(getMainEditorFullToolbar(context));
|
|
|
|
|
for (const key of Object.keys(contextToolbars)) {
|
|
|
|
|
context.manager.registerContextToolbar(key, contextToolbars[key]);
|
|
|
|
|
}
|
|
|
|
|
for (const key of Object.keys(modals)) {
|
|
|
|
|
context.manager.registerModal(key, modals[key]);
|
|
|
|
|
}
|
|
|
|
|
context.manager.registerDecoratorType('code', CodeBlockDecorator);
|
|
|
|
|
context.manager.registerDecoratorType('diagram', DiagramDecorator);
|
2024-07-23 15:35:18 +01:00
|
|
|
|
2025-06-24 17:47:53 +01:00
|
|
|
listenToCommonEvents(editor);
|
2024-07-01 10:44:23 +01:00
|
|
|
setEditorContentFromHtml(editor, htmlContent);
|
2024-05-27 15:39:41 +01:00
|
|
|
|
|
|
|
|
const debugView = document.getElementById('lexical-debug');
|
2024-07-09 20:49:47 +01:00
|
|
|
if (debugView) {
|
|
|
|
|
debugView.hidden = true;
|
2024-12-16 16:24:47 +00:00
|
|
|
editor.registerUpdateListener(({dirtyElements, dirtyLeaves, editorState, prevEditorState}) => {
|
|
|
|
|
// Debug logic
|
|
|
|
|
// console.log('editorState', editorState.toJSON());
|
2024-05-28 18:04:48 +01:00
|
|
|
debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
|
2024-12-16 16:24:47 +00:00
|
|
|
});
|
|
|
|
|
}
|
2024-05-27 20:23:45 +01:00
|
|
|
|
2024-08-11 16:08:51 +01:00
|
|
|
// @ts-ignore
|
|
|
|
|
window.debugEditorState = () => {
|
2025-06-13 19:40:13 +01:00
|
|
|
return editor.getEditorState().toJSON();
|
2024-08-11 16:08:51 +01:00
|
|
|
};
|
|
|
|
|
|
2024-07-16 16:36:08 +01:00
|
|
|
registerCommonNodeMutationListeners(context);
|
2024-07-04 13:09:53 +01:00
|
|
|
|
2025-06-25 14:16:01 +01:00
|
|
|
return new SimpleWysiwygEditorInterface(context);
|
2024-05-27 23:50:28 +01:00
|
|
|
}
|
2024-07-04 13:09:53 +01:00
|
|
|
|
2025-06-25 14:16:01 +01:00
|
|
|
export function createBasicEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
|
2025-06-24 17:47:53 +01:00
|
|
|
const editor = createEditor({
|
2025-06-25 14:16:01 +01:00
|
|
|
namespace: 'BookStackBasicEditor',
|
|
|
|
|
nodes: getNodesForBasicEditor(),
|
2025-06-24 17:47:53 +01:00
|
|
|
onError: console.error,
|
|
|
|
|
theme: theme,
|
|
|
|
|
});
|
|
|
|
|
const context: EditorUiContext = buildEditorUI(container, editor, options);
|
|
|
|
|
editor.setRootElement(context.editorDOM);
|
|
|
|
|
|
2025-06-25 14:16:01 +01:00
|
|
|
const editorTeardown = mergeRegister(
|
2025-06-24 17:47:53 +01:00
|
|
|
registerRichText(editor),
|
|
|
|
|
registerHistory(editor, createEmptyHistoryState(), 300),
|
|
|
|
|
registerShortcuts(context),
|
|
|
|
|
registerAutoLinks(editor),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Register toolbars, modals & decorators
|
2025-06-25 14:16:01 +01:00
|
|
|
context.manager.setToolbar(getBasicEditorToolbar(context));
|
2025-06-24 17:47:53 +01:00
|
|
|
context.manager.registerContextToolbar('link', contextToolbars.link);
|
|
|
|
|
context.manager.registerModal('link', modals.link);
|
2025-06-25 14:16:01 +01:00
|
|
|
context.manager.onTeardown(editorTeardown);
|
2025-06-24 17:47:53 +01:00
|
|
|
|
|
|
|
|
setEditorContentFromHtml(editor, htmlContent);
|
|
|
|
|
|
2025-06-25 14:16:01 +01:00
|
|
|
return new SimpleWysiwygEditorInterface(context);
|
2025-06-24 17:47:53 +01:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 13:09:53 +01:00
|
|
|
export class SimpleWysiwygEditorInterface {
|
2025-06-25 14:16:01 +01:00
|
|
|
protected context: EditorUiContext;
|
2024-07-04 13:09:53 +01:00
|
|
|
|
2025-06-25 14:16:01 +01:00
|
|
|
constructor(context: EditorUiContext) {
|
|
|
|
|
this.context = context;
|
2024-07-04 13:09:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getContentAsHtml(): Promise<string> {
|
2025-06-25 14:16:01 +01:00
|
|
|
return await getEditorContentAsHtml(this.context.editor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focus(): void {
|
|
|
|
|
focusEditor(this.context.editor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remove() {
|
|
|
|
|
this.context.editorDOM.remove();
|
|
|
|
|
this.context.manager.teardown();
|
2024-07-04 13:09:53 +01:00
|
|
|
}
|
|
|
|
|
}
|