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