2024-06-30 05:31:39 -04:00
|
|
|
import {LexicalEditor} from "lexical";
|
2024-07-16 11:36:08 -04:00
|
|
|
import {
|
|
|
|
getCodeToolbarContent,
|
|
|
|
getImageToolbarContent,
|
|
|
|
getLinkToolbarContent,
|
2024-07-21 10:11:24 -04:00
|
|
|
getMainEditorFullToolbar, getTableToolbarContent
|
2024-07-16 11:36:08 -04:00
|
|
|
} from "./toolbars";
|
2024-05-30 11:50:55 -04:00
|
|
|
import {EditorUIManager} from "./framework/manager";
|
2024-07-25 11:25:08 -04:00
|
|
|
import {image as imageFormDefinition, link as linkFormDefinition, media as mediaFormDefinition, source as sourceFormDefinition} from "./defaults/form-definitions";
|
2024-06-05 08:04:49 -04:00
|
|
|
import {ImageDecorator} from "./decorators/image";
|
2024-06-05 13:43:42 -04:00
|
|
|
import {EditorUiContext} from "./framework/core";
|
2024-07-02 09:46:30 -04:00
|
|
|
import {CodeBlockDecorator} from "./decorators/code-block";
|
2024-07-03 05:28:04 -04:00
|
|
|
import {DiagramDecorator} from "./decorators/diagram";
|
2024-05-28 13:04:48 -04:00
|
|
|
|
2024-07-19 13:12:51 -04:00
|
|
|
export function buildEditorUI(container: HTMLElement, element: HTMLElement, scrollContainer: HTMLElement, editor: LexicalEditor, options: Record<string, any>): EditorUiContext {
|
2024-05-30 11:50:55 -04:00
|
|
|
const manager = new EditorUIManager();
|
2024-06-05 13:43:42 -04:00
|
|
|
const context: EditorUiContext = {
|
2024-05-30 11:50:55 -04:00
|
|
|
editor,
|
2024-07-01 05:44:23 -04:00
|
|
|
containerDOM: container,
|
2024-06-30 05:31:39 -04:00
|
|
|
editorDOM: element,
|
2024-07-19 13:12:51 -04:00
|
|
|
scrollDOM: scrollContainer,
|
2024-05-30 11:50:55 -04:00
|
|
|
manager,
|
|
|
|
translate: (text: string): string => text,
|
2024-06-05 13:43:42 -04:00
|
|
|
lastSelection: null,
|
2024-07-19 07:09:41 -04:00
|
|
|
options,
|
2024-05-30 11:50:55 -04:00
|
|
|
};
|
2024-06-01 11:49:47 -04:00
|
|
|
manager.setContext(context);
|
2024-05-30 11:50:55 -04:00
|
|
|
|
|
|
|
// Create primary toolbar
|
2024-06-30 05:31:39 -04:00
|
|
|
manager.setToolbar(getMainEditorFullToolbar());
|
2024-05-28 13:04:48 -04:00
|
|
|
|
2024-06-01 11:49:47 -04:00
|
|
|
// Register modals
|
|
|
|
manager.registerModal('link', {
|
|
|
|
title: 'Insert/Edit link',
|
|
|
|
form: linkFormDefinition,
|
|
|
|
});
|
2024-06-05 13:43:42 -04:00
|
|
|
manager.registerModal('image', {
|
|
|
|
title: 'Insert/Edit Image',
|
|
|
|
form: imageFormDefinition
|
2024-06-12 09:01:36 -04:00
|
|
|
});
|
2024-07-25 11:25:08 -04:00
|
|
|
manager.registerModal('media', {
|
|
|
|
title: 'Insert/Edit Media',
|
|
|
|
form: mediaFormDefinition,
|
|
|
|
});
|
2024-06-12 09:01:36 -04:00
|
|
|
manager.registerModal('source', {
|
|
|
|
title: 'Source code',
|
|
|
|
form: sourceFormDefinition,
|
|
|
|
});
|
2024-05-30 11:50:55 -04:00
|
|
|
|
2024-06-30 07:13:13 -04:00
|
|
|
// Register context toolbars
|
|
|
|
manager.registerContextToolbar('image', {
|
2024-07-19 07:09:41 -04:00
|
|
|
selector: 'img:not([drawio-diagram] img)',
|
2024-06-30 07:13:13 -04:00
|
|
|
content: getImageToolbarContent(),
|
|
|
|
});
|
2024-06-30 14:52:09 -04:00
|
|
|
manager.registerContextToolbar('link', {
|
|
|
|
selector: 'a',
|
|
|
|
content: getLinkToolbarContent(),
|
2024-07-19 13:12:51 -04:00
|
|
|
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
|
|
|
|
const image = originalTarget.querySelector('img');
|
|
|
|
return image || originalTarget;
|
|
|
|
}
|
2024-06-30 14:52:09 -04:00
|
|
|
});
|
2024-07-16 11:36:08 -04:00
|
|
|
manager.registerContextToolbar('code', {
|
|
|
|
selector: '.editor-code-block-wrap',
|
|
|
|
content: getCodeToolbarContent(),
|
|
|
|
});
|
2024-06-30 07:13:13 -04:00
|
|
|
|
2024-07-21 10:11:24 -04:00
|
|
|
manager.registerContextToolbar('table', {
|
|
|
|
selector: 'td,th',
|
|
|
|
content: getTableToolbarContent(),
|
|
|
|
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
|
|
|
|
return originalTarget.closest('table') as HTMLTableElement;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-06-30 05:31:39 -04:00
|
|
|
// Register image decorator listener
|
2024-06-05 08:04:49 -04:00
|
|
|
manager.registerDecoratorType('image', ImageDecorator);
|
2024-07-02 09:46:30 -04:00
|
|
|
manager.registerDecoratorType('code', CodeBlockDecorator);
|
2024-07-03 05:28:04 -04:00
|
|
|
manager.registerDecoratorType('diagram', DiagramDecorator);
|
2024-07-16 11:36:08 -04:00
|
|
|
|
|
|
|
return context;
|
2024-05-28 13:04:48 -04:00
|
|
|
}
|