2024-06-30 05:31:39 -04:00
|
|
|
import {LexicalEditor} from "lexical";
|
2024-06-30 14:52:09 -04:00
|
|
|
import {getImageToolbarContent, getLinkToolbarContent, getMainEditorFullToolbar} from "./toolbars";
|
2024-05-30 11:50:55 -04:00
|
|
|
import {EditorUIManager} from "./framework/manager";
|
2024-06-12 09:01:36 -04:00
|
|
|
import {image as imageFormDefinition, link as linkFormDefinition, 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-05-28 13:04:48 -04:00
|
|
|
|
2024-07-01 05:44:23 -04:00
|
|
|
export function buildEditorUI(container: HTMLElement, element: HTMLElement, editor: LexicalEditor) {
|
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-05-30 11:50:55 -04:00
|
|
|
manager,
|
|
|
|
translate: (text: string): string => text,
|
2024-06-05 13:43:42 -04:00
|
|
|
lastSelection: null,
|
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
|
|
|
});
|
|
|
|
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', {
|
|
|
|
selector: 'img',
|
|
|
|
content: getImageToolbarContent(),
|
|
|
|
displayTargetLocator(originalTarget: HTMLElement) {
|
|
|
|
return originalTarget.closest('a') || originalTarget;
|
|
|
|
}
|
|
|
|
});
|
2024-06-30 14:52:09 -04:00
|
|
|
manager.registerContextToolbar('link', {
|
|
|
|
selector: 'a',
|
|
|
|
content: getLinkToolbarContent(),
|
|
|
|
});
|
2024-06-30 07:13:13 -04:00
|
|
|
|
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-05-28 13:04:48 -04:00
|
|
|
}
|