2024-07-04 20:09:53 +08:00
|
|
|
import {createEditor, CreateEditorArgs, LexicalEditor} from 'lexical';
|
2024-05-27 22:39:41 +08:00
|
|
|
import {createEmptyHistoryState, registerHistory} from '@lexical/history';
|
2024-05-28 03:23:45 +08:00
|
|
|
import {registerRichText} from '@lexical/rich-text';
|
2024-05-29 01:04:48 +08:00
|
|
|
import {mergeRegister} from '@lexical/utils';
|
2024-07-16 23:36:08 +08:00
|
|
|
import {getNodesForPageEditor, registerCommonNodeMutationListeners} from './nodes';
|
2024-05-29 01:04:48 +08:00
|
|
|
import {buildEditorUI} from "./ui";
|
2024-07-04 20:09:53 +08:00
|
|
|
import {getEditorContentAsHtml, setEditorContentFromHtml} from "./actions";
|
2024-06-21 20:47:47 +08:00
|
|
|
import {registerTableResizer} from "./ui/framework/helpers/table-resizer";
|
2024-07-01 17:44:23 +08:00
|
|
|
import {el} from "./helpers";
|
2024-07-16 23:36:08 +08:00
|
|
|
import {EditorUiContext} from "./ui/framework/core";
|
2024-05-27 22:39:41 +08:00
|
|
|
|
2024-07-19 19:09:41 +08:00
|
|
|
export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
|
2024-05-28 06:50:28 +08:00
|
|
|
const config: CreateEditorArgs = {
|
2024-05-27 22:39:41 +08:00
|
|
|
namespace: 'BookStackPageEditor',
|
2024-05-28 03:23:45 +08:00
|
|
|
nodes: getNodesForPageEditor(),
|
2024-05-27 22:39:41 +08:00
|
|
|
onError: console.error,
|
2024-06-23 18:36:48 +08: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 22:39:41 +08:00
|
|
|
};
|
|
|
|
|
2024-07-01 17:44:23 +08:00
|
|
|
const editArea = el('div', {
|
|
|
|
contenteditable: 'true',
|
2024-07-01 22:10:22 +08:00
|
|
|
class: 'editor-content-area page-content',
|
2024-07-01 17:44:23 +08:00
|
|
|
});
|
2024-07-01 22:10:22 +08:00
|
|
|
const editWrap = el('div', {
|
|
|
|
class: 'editor-content-wrap',
|
|
|
|
}, [editArea]);
|
|
|
|
container.append(editWrap);
|
2024-07-01 17:44:23 +08:00
|
|
|
container.classList.add('editor-container');
|
2024-05-27 22:39:41 +08:00
|
|
|
|
|
|
|
const editor = createEditor(config);
|
|
|
|
editor.setRootElement(editArea);
|
|
|
|
|
|
|
|
mergeRegister(
|
|
|
|
registerRichText(editor),
|
|
|
|
registerHistory(editor, createEmptyHistoryState(), 300),
|
2024-06-21 20:47:47 +08:00
|
|
|
registerTableResizer(editor, editArea),
|
2024-05-27 22:39:41 +08:00
|
|
|
);
|
|
|
|
|
2024-07-01 17:44:23 +08:00
|
|
|
setEditorContentFromHtml(editor, htmlContent);
|
2024-05-27 22:39:41 +08:00
|
|
|
|
|
|
|
const debugView = document.getElementById('lexical-debug');
|
2024-07-10 03:49:47 +08:00
|
|
|
if (debugView) {
|
|
|
|
debugView.hidden = true;
|
|
|
|
}
|
2024-05-27 22:39:41 +08:00
|
|
|
editor.registerUpdateListener(({editorState}) => {
|
|
|
|
console.log('editorState', editorState.toJSON());
|
2024-05-29 01:04:48 +08:00
|
|
|
if (debugView) {
|
|
|
|
debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
|
|
|
|
}
|
2024-05-27 22:39:41 +08:00
|
|
|
});
|
2024-05-28 03:23:45 +08:00
|
|
|
|
2024-07-20 01:12:51 +08:00
|
|
|
const context: EditorUiContext = buildEditorUI(container, editArea, editWrap, editor, options);
|
2024-07-16 23:36:08 +08:00
|
|
|
registerCommonNodeMutationListeners(context);
|
2024-07-04 20:09:53 +08:00
|
|
|
|
|
|
|
return new SimpleWysiwygEditorInterface(editor);
|
2024-05-28 06:50:28 +08:00
|
|
|
}
|
2024-07-04 20:09:53 +08:00
|
|
|
|
|
|
|
export class SimpleWysiwygEditorInterface {
|
|
|
|
protected editor: LexicalEditor;
|
|
|
|
|
|
|
|
constructor(editor: LexicalEditor) {
|
|
|
|
this.editor = editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getContentAsHtml(): Promise<string> {
|
|
|
|
return await getEditorContentAsHtml(this.editor);
|
|
|
|
}
|
|
|
|
}
|