2024-05-29 01:04:48 +08:00
|
|
|
import {
|
|
|
|
$getSelection,
|
|
|
|
COMMAND_PRIORITY_LOW,
|
|
|
|
LexicalEditor,
|
|
|
|
SELECTION_CHANGE_COMMAND
|
|
|
|
} from "lexical";
|
2024-05-30 03:38:31 +08:00
|
|
|
import {getMainEditorFullToolbar} from "./toolbars";
|
2024-05-30 23:50:55 +08:00
|
|
|
import {EditorUIManager} from "./framework/manager";
|
2024-06-01 23:49:47 +08:00
|
|
|
import {link as linkFormDefinition} from "./defaults/form-definitions";
|
2024-06-03 23:56:31 +08:00
|
|
|
import {DecoratorListener} from "lexical/LexicalEditor";
|
|
|
|
import type {NodeKey} from "lexical/LexicalNode";
|
|
|
|
import {el} from "../helpers";
|
2024-05-29 01:04:48 +08:00
|
|
|
|
|
|
|
export function buildEditorUI(element: HTMLElement, editor: LexicalEditor) {
|
2024-05-30 23:50:55 +08:00
|
|
|
const manager = new EditorUIManager();
|
|
|
|
const context = {
|
|
|
|
editor,
|
|
|
|
manager,
|
|
|
|
translate: (text: string): string => text,
|
|
|
|
};
|
2024-06-01 23:49:47 +08:00
|
|
|
manager.setContext(context);
|
2024-05-30 23:50:55 +08:00
|
|
|
|
|
|
|
// Create primary toolbar
|
2024-05-30 03:38:31 +08:00
|
|
|
const toolbar = getMainEditorFullToolbar();
|
2024-05-30 23:50:55 +08:00
|
|
|
toolbar.setContext(context);
|
2024-05-30 03:38:31 +08:00
|
|
|
element.before(toolbar.getDOMElement());
|
2024-05-29 01:04:48 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
// Register modals
|
|
|
|
manager.registerModal('link', {
|
|
|
|
title: 'Insert/Edit link',
|
|
|
|
form: linkFormDefinition,
|
|
|
|
});
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-03 23:56:31 +08:00
|
|
|
// Register decorator listener
|
|
|
|
// Maybe move to manager?
|
|
|
|
const domDecorateListener: DecoratorListener<HTMLElement> = (decorator: Record<NodeKey, HTMLElement>) => {
|
|
|
|
const keys = Object.keys(decorator);
|
|
|
|
for (const key of keys) {
|
|
|
|
const decoratedEl = editor.getElementByKey(key);
|
|
|
|
const decoratorEl = decorator[key];
|
|
|
|
if (decoratedEl) {
|
|
|
|
decoratedEl.append(decoratorEl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
editor.registerDecoratorListener(domDecorateListener);
|
|
|
|
|
2024-05-29 01:04:48 +08:00
|
|
|
// Update button states on editor selection change
|
|
|
|
editor.registerCommand(SELECTION_CHANGE_COMMAND, () => {
|
|
|
|
const selection = $getSelection();
|
2024-05-30 03:38:31 +08:00
|
|
|
toolbar.updateState({editor, selection});
|
2024-05-29 01:04:48 +08:00
|
|
|
return false;
|
|
|
|
}, COMMAND_PRIORITY_LOW);
|
|
|
|
}
|