2024-06-01 23:49:47 +08:00
|
|
|
import {EditorFormModal, EditorFormModalDefinition} from "./modals";
|
2024-06-30 17:31:39 +08:00
|
|
|
import {EditorContainerUiElement, EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
|
|
|
|
import {EditorDecorator, EditorDecoratorAdapter} from "./decorator";
|
2024-07-16 23:36:08 +08:00
|
|
|
import {$getSelection, BaseSelection, COMMAND_PRIORITY_LOW, LexicalEditor, SELECTION_CHANGE_COMMAND} from "lexical";
|
2024-06-30 17:31:39 +08:00
|
|
|
import {DecoratorListener} from "lexical/LexicalEditor";
|
|
|
|
import type {NodeKey} from "lexical/LexicalNode";
|
2024-06-30 19:13:13 +08:00
|
|
|
import {EditorContextToolbar, EditorContextToolbarDefinition} from "./toolbars";
|
2024-08-20 20:07:33 +08:00
|
|
|
import {getLastSelection, setLastSelection} from "../../utils/selection";
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-07-16 23:36:08 +08:00
|
|
|
export type SelectionChangeHandler = (selection: BaseSelection|null) => void;
|
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
export class EditorUIManager {
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
protected modalDefinitionsByKey: Record<string, EditorFormModalDefinition> = {};
|
2024-08-14 02:36:18 +08:00
|
|
|
protected activeModalsByKey: Record<string, EditorFormModal> = {};
|
2024-06-05 20:04:49 +08:00
|
|
|
protected decoratorConstructorsByType: Record<string, typeof EditorDecorator> = {};
|
|
|
|
protected decoratorInstancesByNodeKey: Record<string, EditorDecorator> = {};
|
2024-06-01 23:49:47 +08:00
|
|
|
protected context: EditorUiContext|null = null;
|
2024-06-30 17:31:39 +08:00
|
|
|
protected toolbar: EditorContainerUiElement|null = null;
|
2024-06-30 19:13:13 +08:00
|
|
|
protected contextToolbarDefinitionsByKey: Record<string, EditorContextToolbarDefinition> = {};
|
|
|
|
protected activeContextToolbars: EditorContextToolbar[] = [];
|
2024-07-16 23:36:08 +08:00
|
|
|
protected selectionChangeHandlers: Set<SelectionChangeHandler> = new Set();
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
setContext(context: EditorUiContext) {
|
|
|
|
this.context = context;
|
2024-07-20 01:12:51 +08:00
|
|
|
this.setupEventListeners(context);
|
2024-06-30 17:31:39 +08:00
|
|
|
this.setupEditor(context.editor);
|
2024-06-01 23:49:47 +08:00
|
|
|
}
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
getContext(): EditorUiContext {
|
|
|
|
if (this.context === null) {
|
|
|
|
throw new Error(`Context attempted to be used without being set`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.context;
|
|
|
|
}
|
|
|
|
|
2024-06-30 17:31:39 +08:00
|
|
|
triggerStateUpdateForElement(element: EditorUiElement) {
|
2024-06-13 02:51:42 +08:00
|
|
|
element.updateState({
|
|
|
|
selection: null,
|
|
|
|
editor: this.getContext().editor
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
registerModal(key: string, modalDefinition: EditorFormModalDefinition) {
|
|
|
|
this.modalDefinitionsByKey[key] = modalDefinition;
|
|
|
|
}
|
|
|
|
|
|
|
|
createModal(key: string): EditorFormModal {
|
|
|
|
const modalDefinition = this.modalDefinitionsByKey[key];
|
|
|
|
if (!modalDefinition) {
|
2024-06-05 20:04:49 +08:00
|
|
|
throw new Error(`Attempted to show modal of key [${key}] but no modal registered for that key`);
|
2024-06-01 23:49:47 +08:00
|
|
|
}
|
|
|
|
|
2024-08-14 02:36:18 +08:00
|
|
|
const modal = new EditorFormModal(modalDefinition, key);
|
2024-06-01 23:49:47 +08:00
|
|
|
modal.setContext(this.getContext());
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
return modal;
|
|
|
|
}
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-08-14 02:36:18 +08:00
|
|
|
setModalActive(key: string, modal: EditorFormModal): void {
|
|
|
|
this.activeModalsByKey[key] = modal;
|
|
|
|
}
|
|
|
|
|
|
|
|
setModalInactive(key: string): void {
|
|
|
|
delete this.activeModalsByKey[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
getActiveModal(key: string): EditorFormModal|null {
|
|
|
|
return this.activeModalsByKey[key];
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:04:49 +08:00
|
|
|
registerDecoratorType(type: string, decorator: typeof EditorDecorator) {
|
|
|
|
this.decoratorConstructorsByType[type] = decorator;
|
|
|
|
}
|
|
|
|
|
2024-06-30 17:31:39 +08:00
|
|
|
protected getDecorator(decoratorType: string, nodeKey: string): EditorDecorator {
|
2024-06-05 20:04:49 +08:00
|
|
|
if (this.decoratorInstancesByNodeKey[nodeKey]) {
|
|
|
|
return this.decoratorInstancesByNodeKey[nodeKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
const decoratorClass = this.decoratorConstructorsByType[decoratorType];
|
|
|
|
if (!decoratorClass) {
|
|
|
|
throw new Error(`Attempted to use decorator of type [${decoratorType}] but not decorator registered for that type`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const decorator = new decoratorClass(nodeKey);
|
|
|
|
this.decoratorInstancesByNodeKey[nodeKey] = decorator;
|
|
|
|
return decorator;
|
|
|
|
}
|
2024-06-30 17:31:39 +08:00
|
|
|
|
2024-07-16 23:36:08 +08:00
|
|
|
getDecoratorByNodeKey(nodeKey: string): EditorDecorator|null {
|
|
|
|
return this.decoratorInstancesByNodeKey[nodeKey] || null;
|
|
|
|
}
|
|
|
|
|
2024-06-30 17:31:39 +08:00
|
|
|
setToolbar(toolbar: EditorContainerUiElement) {
|
|
|
|
if (this.toolbar) {
|
|
|
|
this.toolbar.getDOMElement().remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.toolbar = toolbar;
|
|
|
|
toolbar.setContext(this.getContext());
|
2024-07-01 17:44:23 +08:00
|
|
|
this.getContext().containerDOM.prepend(toolbar.getDOMElement());
|
2024-06-30 17:31:39 +08:00
|
|
|
}
|
|
|
|
|
2024-06-30 19:13:13 +08:00
|
|
|
registerContextToolbar(key: string, definition: EditorContextToolbarDefinition) {
|
|
|
|
this.contextToolbarDefinitionsByKey[key] = definition;
|
|
|
|
}
|
|
|
|
|
2024-09-08 22:54:59 +08:00
|
|
|
triggerStateUpdate(update: EditorUiStateUpdate): void {
|
2024-08-20 20:07:33 +08:00
|
|
|
setLastSelection(update.editor, update.selection);
|
2024-06-30 19:13:13 +08:00
|
|
|
this.toolbar?.updateState(update);
|
|
|
|
this.updateContextToolbars(update);
|
|
|
|
for (const toolbar of this.activeContextToolbars) {
|
|
|
|
toolbar.updateState(update);
|
|
|
|
}
|
2024-07-16 23:36:08 +08:00
|
|
|
this.triggerSelectionChange(update.selection);
|
2024-06-30 19:13:13 +08:00
|
|
|
}
|
|
|
|
|
2024-07-01 17:44:23 +08:00
|
|
|
triggerStateRefresh(): void {
|
2024-08-20 20:07:33 +08:00
|
|
|
const editor = this.getContext().editor;
|
2024-09-08 22:54:59 +08:00
|
|
|
const update = {
|
2024-08-20 20:07:33 +08:00
|
|
|
editor,
|
|
|
|
selection: getLastSelection(editor),
|
2024-09-08 22:54:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
this.triggerStateUpdate(update);
|
|
|
|
this.updateContextToolbars(update);
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerFutureStateRefresh(): void {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
this.getContext().editor.getEditorState().read(() => {
|
|
|
|
this.triggerStateRefresh();
|
|
|
|
});
|
2024-07-01 17:44:23 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-16 23:36:08 +08:00
|
|
|
protected triggerSelectionChange(selection: BaseSelection|null): void {
|
|
|
|
if (!selection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const handler of this.selectionChangeHandlers) {
|
|
|
|
handler(selection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onSelectionChange(handler: SelectionChangeHandler): void {
|
|
|
|
this.selectionChangeHandlers.add(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
offSelectionChange(handler: SelectionChangeHandler): void {
|
|
|
|
this.selectionChangeHandlers.delete(handler);
|
|
|
|
}
|
|
|
|
|
2024-09-08 01:39:58 +08:00
|
|
|
triggerLayoutUpdate(): void {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
for (const toolbar of this.activeContextToolbars) {
|
|
|
|
toolbar.updatePosition();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-30 19:13:13 +08:00
|
|
|
protected updateContextToolbars(update: EditorUiStateUpdate): void {
|
2024-07-20 01:12:51 +08:00
|
|
|
for (let i = this.activeContextToolbars.length - 1; i >= 0; i--) {
|
|
|
|
const toolbar = this.activeContextToolbars[i];
|
|
|
|
toolbar.destroy();
|
|
|
|
this.activeContextToolbars.splice(i, 1);
|
2024-06-30 19:13:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const node = (update.selection?.getNodes() || [])[0] || null;
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const element = update.editor.getElementByKey(node.getKey());
|
|
|
|
if (!element) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const toolbarKeys = Object.keys(this.contextToolbarDefinitionsByKey);
|
|
|
|
const contentByTarget = new Map<HTMLElement, EditorUiElement[]>();
|
|
|
|
for (const key of toolbarKeys) {
|
|
|
|
const definition = this.contextToolbarDefinitionsByKey[key];
|
|
|
|
const matchingElem = ((element.closest(definition.selector)) || (element.querySelector(definition.selector))) as HTMLElement|null;
|
|
|
|
if (matchingElem) {
|
|
|
|
const targetEl = definition.displayTargetLocator ? definition.displayTargetLocator(matchingElem) : matchingElem;
|
|
|
|
if (!contentByTarget.has(targetEl)) {
|
|
|
|
contentByTarget.set(targetEl, [])
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
contentByTarget.get(targetEl).push(...definition.content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [target, contents] of contentByTarget) {
|
2024-07-20 01:12:51 +08:00
|
|
|
const toolbar = new EditorContextToolbar(target, contents);
|
2024-06-30 19:13:13 +08:00
|
|
|
toolbar.setContext(this.getContext());
|
|
|
|
this.activeContextToolbars.push(toolbar);
|
|
|
|
|
2024-07-01 17:44:23 +08:00
|
|
|
this.getContext().containerDOM.append(toolbar.getDOMElement());
|
2024-07-20 01:12:51 +08:00
|
|
|
toolbar.updatePosition();
|
2024-06-30 19:13:13 +08:00
|
|
|
}
|
2024-06-30 17:31:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected setupEditor(editor: LexicalEditor) {
|
|
|
|
// Register our DOM decorate listener with the editor
|
|
|
|
const domDecorateListener: DecoratorListener<EditorDecoratorAdapter> = (decorators: Record<NodeKey, EditorDecoratorAdapter>) => {
|
2024-07-03 00:34:03 +08:00
|
|
|
editor.getEditorState().read(() => {
|
|
|
|
const keys = Object.keys(decorators);
|
|
|
|
for (const key of keys) {
|
|
|
|
const decoratedEl = editor.getElementByKey(key);
|
|
|
|
if (!decoratedEl) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const adapter = decorators[key];
|
|
|
|
const decorator = this.getDecorator(adapter.type, key);
|
|
|
|
decorator.setNode(adapter.getNode());
|
|
|
|
const decoratorEl = decorator.render(this.getContext(), decoratedEl);
|
|
|
|
if (decoratorEl) {
|
|
|
|
decoratedEl.append(decoratorEl);
|
|
|
|
}
|
2024-07-02 21:46:30 +08:00
|
|
|
}
|
2024-07-03 00:34:03 +08:00
|
|
|
});
|
2024-06-30 17:31:39 +08:00
|
|
|
}
|
|
|
|
editor.registerDecoratorListener(domDecorateListener);
|
|
|
|
}
|
2024-07-20 01:12:51 +08:00
|
|
|
|
|
|
|
protected setupEventListeners(context: EditorUiContext) {
|
2024-09-08 01:39:58 +08:00
|
|
|
const layoutUpdate = this.triggerLayoutUpdate.bind(this);
|
|
|
|
window.addEventListener('scroll', layoutUpdate, {capture: true, passive: true});
|
|
|
|
window.addEventListener('resize', layoutUpdate, {passive: true});
|
2024-07-20 01:12:51 +08:00
|
|
|
}
|
2024-05-30 23:50:55 +08:00
|
|
|
}
|