2024-06-30 17:31:39 +08:00
|
|
|
import {LexicalEditor} from "lexical";
|
2024-07-01 02:52:09 +08:00
|
|
|
import {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-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();
|
2024-06-06 01:43:42 +08:00
|
|
|
const context: EditorUiContext = {
|
2024-05-30 23:50:55 +08:00
|
|
|
editor,
|
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-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', {
|
|
|
|
selector: 'img',
|
|
|
|
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-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-05-29 01:04:48 +08:00
|
|
|
}
|