BookStack/resources/js/wysiwyg/ui/index.ts

47 lines
1.5 KiB
TypeScript
Raw Normal View History

import {LexicalEditor} from "lexical";
import {getImageToolbarContent, getMainEditorFullToolbar} from "./toolbars";
2024-05-30 23:50:55 +08:00
import {EditorUIManager} from "./framework/manager";
import {image as imageFormDefinition, link as linkFormDefinition, source as sourceFormDefinition} from "./defaults/form-definitions";
import {ImageDecorator} from "./decorators/image";
import {EditorUiContext} from "./framework/core";
export function buildEditorUI(element: HTMLElement, editor: LexicalEditor) {
2024-05-30 23:50:55 +08:00
const manager = new EditorUIManager();
const context: EditorUiContext = {
2024-05-30 23:50:55 +08:00
editor,
editorDOM: element,
2024-05-30 23:50:55 +08:00
manager,
translate: (text: string): string => text,
lastSelection: null,
2024-05-30 23:50:55 +08:00
};
manager.setContext(context);
2024-05-30 23:50:55 +08:00
// Create primary toolbar
manager.setToolbar(getMainEditorFullToolbar());
// Register modals
manager.registerModal('link', {
title: 'Insert/Edit link',
form: linkFormDefinition,
});
manager.registerModal('image', {
title: 'Insert/Edit Image',
form: imageFormDefinition
});
manager.registerModal('source', {
title: 'Source code',
form: sourceFormDefinition,
});
2024-05-30 23:50:55 +08:00
// Register context toolbars
manager.registerContextToolbar('image', {
selector: 'img',
content: getImageToolbarContent(),
displayTargetLocator(originalTarget: HTMLElement) {
return originalTarget.closest('a') || originalTarget;
}
});
// Register image decorator listener
manager.registerDecoratorType('image', ImageDecorator);
}