2024-06-30 17:31:39 +08:00
|
|
|
import {LexicalEditor} from "lexical";
|
2024-07-16 23:36:08 +08:00
|
|
|
import {
|
|
|
|
getCodeToolbarContent,
|
|
|
|
getImageToolbarContent,
|
|
|
|
getLinkToolbarContent,
|
2024-07-21 22:11:24 +08:00
|
|
|
getMainEditorFullToolbar, getTableToolbarContent
|
2024-07-16 23:36:08 +08:00
|
|
|
} from "./toolbars";
|
2024-05-30 23:50:55 +08:00
|
|
|
import {EditorUIManager} from "./framework/manager";
|
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-08-02 22:28:54 +08:00
|
|
|
import {modals} from "./defaults/modals";
|
2024-05-29 01:04:48 +08:00
|
|
|
|
2024-07-20 01:12:51 +08:00
|
|
|
export function buildEditorUI(container: HTMLElement, element: HTMLElement, scrollContainer: 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-07-20 01:12:51 +08:00
|
|
|
scrollDOM: scrollContainer,
|
2024-05-30 23:50:55 +08:00
|
|
|
manager,
|
2024-08-22 17:08:08 +08:00
|
|
|
translate: (text: string): string => text, // TODO - Implement
|
2024-08-22 20:28:30 +08:00
|
|
|
error(error: string|Error): void {
|
|
|
|
const message = error instanceof Error ? error.message : error;
|
|
|
|
window.$events.error(message); // TODO - Translate
|
2024-08-22 17:08:08 +08:00
|
|
|
},
|
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
|
2024-08-02 22:28:54 +08:00
|
|
|
for (const key of Object.keys(modals)) {
|
|
|
|
manager.registerModal(key, modals[key]);
|
|
|
|
}
|
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(),
|
|
|
|
});
|
2024-07-01 02:52:09 +08:00
|
|
|
manager.registerContextToolbar('link', {
|
|
|
|
selector: 'a',
|
|
|
|
content: getLinkToolbarContent(),
|
2024-07-20 01:12:51 +08:00
|
|
|
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
|
|
|
|
const image = originalTarget.querySelector('img');
|
|
|
|
return image || originalTarget;
|
|
|
|
}
|
2024-07-01 02:52:09 +08:00
|
|
|
});
|
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-07-21 22:11:24 +08:00
|
|
|
manager.registerContextToolbar('table', {
|
|
|
|
selector: 'td,th',
|
|
|
|
content: getTableToolbarContent(),
|
|
|
|
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
|
|
|
|
return originalTarget.closest('table') as HTMLTableElement;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-06-30 17:31:39 +08:00
|
|
|
// Register image decorator listener
|
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
|
|
|
}
|