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-08-04 01:14:01 +08:00
|
|
|
import {getEditorContentAsHtml, setEditorContentFromHtml} from "./utils/actions";
|
2024-06-21 20:47:47 +08:00
|
|
|
import {registerTableResizer} from "./ui/framework/helpers/table-resizer";
|
2024-07-16 23:36:08 +08:00
|
|
|
import {EditorUiContext} from "./ui/framework/core";
|
2024-08-04 01:14:01 +08:00
|
|
|
import {listen as listenToCommonEvents} from "./services/common-events";
|
|
|
|
import {handleDropEvents} from "./services/drop-handling";
|
2024-07-30 21:42:19 +08:00
|
|
|
import {registerTaskListHandler} from "./ui/framework/helpers/task-list-handler";
|
2024-08-02 22:28:54 +08:00
|
|
|
import {registerTableSelectionHandler} from "./ui/framework/helpers/table-selection-handler";
|
2024-08-04 01:14:01 +08:00
|
|
|
import {el} from "./utils/dom";
|
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-07-23 19:45:58 +08:00
|
|
|
registerTableResizer(editor, editWrap),
|
2024-08-02 22:28:54 +08:00
|
|
|
registerTableSelectionHandler(editor),
|
2024-07-30 21:42:19 +08:00
|
|
|
registerTaskListHandler(editor, editArea),
|
2024-05-27 22:39:41 +08:00
|
|
|
);
|
|
|
|
|
2024-07-23 22:35:18 +08:00
|
|
|
listenToCommonEvents(editor);
|
2024-07-29 22:27:41 +08:00
|
|
|
handleDropEvents(editor);
|
2024-07-23 22:35:18 +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-07-30 04:43:20 +08:00
|
|
|
|
|
|
|
let changeFromLoading = true;
|
|
|
|
editor.registerUpdateListener(({editorState, dirtyElements, dirtyLeaves}) => {
|
|
|
|
|
|
|
|
// Emit change event to component system (for draft detection) on actual user content change
|
|
|
|
if (dirtyElements.size > 0 || dirtyLeaves.size > 0) {
|
|
|
|
if (changeFromLoading) {
|
|
|
|
changeFromLoading = false;
|
|
|
|
} else {
|
|
|
|
window.$events.emit('editor-html-change', '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Debug logic
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|