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

56 lines
2.0 KiB
TypeScript
Raw Normal View History

import {LexicalEditor} from "lexical";
import {getImageToolbarContent, getLinkToolbarContent, getMainEditorFullToolbar} from "./toolbars";
2024-05-30 11:50:55 -04: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";
import {CodeBlockDecorator} from "./decorators/code-block";
2024-07-03 05:28:04 -04:00
import {DiagramDecorator} from "./decorators/diagram";
export function buildEditorUI(container: HTMLElement, element: HTMLElement, editor: LexicalEditor) {
2024-05-30 11:50:55 -04:00
const manager = new EditorUIManager();
const context: EditorUiContext = {
2024-05-30 11:50:55 -04:00
editor,
containerDOM: container,
editorDOM: element,
2024-05-30 11:50:55 -04:00
manager,
translate: (text: string): string => text,
lastSelection: null,
2024-05-30 11:50:55 -04:00
};
manager.setContext(context);
2024-05-30 11:50:55 -04: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 11:50:55 -04:00
// Register context toolbars
manager.registerContextToolbar('image', {
selector: 'img',
content: getImageToolbarContent(),
displayTargetLocator(originalTarget: HTMLElement) {
return originalTarget.closest('a') || originalTarget;
}
});
manager.registerContextToolbar('link', {
selector: 'a',
content: getLinkToolbarContent(),
});
// Register image decorator listener
manager.registerDecoratorType('image', ImageDecorator);
manager.registerDecoratorType('code', CodeBlockDecorator);
2024-07-03 05:28:04 -04:00
manager.registerDecoratorType('diagram', DiagramDecorator);
}