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