mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-01 01:36:00 -04:00
ea4c50c2c2
Also added extra lifecycle handling for decorators to things can be properly cleaned up after node destruction.
79 lines
2.7 KiB
TypeScript
79 lines
2.7 KiB
TypeScript
import {createEditor, CreateEditorArgs, LexicalEditor} from 'lexical';
|
|
import {createEmptyHistoryState, registerHistory} from '@lexical/history';
|
|
import {registerRichText} from '@lexical/rich-text';
|
|
import {mergeRegister} from '@lexical/utils';
|
|
import {getNodesForPageEditor, registerCommonNodeMutationListeners} from './nodes';
|
|
import {buildEditorUI} from "./ui";
|
|
import {getEditorContentAsHtml, setEditorContentFromHtml} from "./actions";
|
|
import {registerTableResizer} from "./ui/framework/helpers/table-resizer";
|
|
import {el} from "./helpers";
|
|
import {EditorUiContext} from "./ui/framework/core";
|
|
|
|
export function createPageEditorInstance(container: HTMLElement, htmlContent: string): SimpleWysiwygEditorInterface {
|
|
const config: CreateEditorArgs = {
|
|
namespace: 'BookStackPageEditor',
|
|
nodes: getNodesForPageEditor(),
|
|
onError: console.error,
|
|
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',
|
|
}
|
|
}
|
|
};
|
|
|
|
const editArea = el('div', {
|
|
contenteditable: 'true',
|
|
class: 'editor-content-area page-content',
|
|
});
|
|
const editWrap = el('div', {
|
|
class: 'editor-content-wrap',
|
|
}, [editArea]);
|
|
container.append(editWrap);
|
|
container.classList.add('editor-container');
|
|
|
|
const editor = createEditor(config);
|
|
editor.setRootElement(editArea);
|
|
|
|
mergeRegister(
|
|
registerRichText(editor),
|
|
registerHistory(editor, createEmptyHistoryState(), 300),
|
|
registerTableResizer(editor, editArea),
|
|
);
|
|
|
|
setEditorContentFromHtml(editor, htmlContent);
|
|
|
|
const debugView = document.getElementById('lexical-debug');
|
|
if (debugView) {
|
|
debugView.hidden = true;
|
|
}
|
|
editor.registerUpdateListener(({editorState}) => {
|
|
console.log('editorState', editorState.toJSON());
|
|
if (debugView) {
|
|
debugView.textContent = JSON.stringify(editorState.toJSON(), null, 2);
|
|
}
|
|
});
|
|
|
|
const context: EditorUiContext = buildEditorUI(container, editArea, editor);
|
|
registerCommonNodeMutationListeners(context);
|
|
|
|
return new SimpleWysiwygEditorInterface(editor);
|
|
}
|
|
|
|
export class SimpleWysiwygEditorInterface {
|
|
protected editor: LexicalEditor;
|
|
|
|
constructor(editor: LexicalEditor) {
|
|
this.editor = editor;
|
|
}
|
|
|
|
async getContentAsHtml(): Promise<string> {
|
|
return await getEditorContentAsHtml(this.editor);
|
|
}
|
|
} |