2024-06-12 21:01:36 +08:00
|
|
|
import {createEditor, CreateEditorArgs} 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-05-28 06:50:28 +08:00
|
|
|
import {getNodesForPageEditor} from './nodes';
|
2024-05-29 01:04:48 +08:00
|
|
|
import {buildEditorUI} from "./ui";
|
2024-06-12 21:01:36 +08:00
|
|
|
import {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-05-27 22:39:41 +08:00
|
|
|
|
2024-07-01 17:44:23 +08:00
|
|
|
export function createPageEditorInstance(container: HTMLElement, htmlContent: string) {
|
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');
|
|
|
|
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-01 17:44:23 +08:00
|
|
|
buildEditorUI(container, editArea, editor);
|
2024-05-28 06:50:28 +08:00
|
|
|
}
|