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

82 lines
2.8 KiB
TypeScript
Raw Normal View History

import {LexicalEditor} from "lexical";
import {
getCodeToolbarContent,
getImageToolbarContent,
getLinkToolbarContent,
getMainEditorFullToolbar, getTableToolbarContent
} from "./toolbars";
2024-05-30 11:50:55 -04:00
import {EditorUIManager} from "./framework/manager";
2024-07-25 11:25:08 -04:00
import {image as imageFormDefinition, link as linkFormDefinition, media as mediaFormDefinition, 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, scrollContainer: HTMLElement, editor: LexicalEditor, options: Record<string, any>): EditorUiContext {
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,
scrollDOM: scrollContainer,
2024-05-30 11:50:55 -04:00
manager,
translate: (text: string): string => text,
lastSelection: null,
options,
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
});
2024-07-25 11:25:08 -04:00
manager.registerModal('media', {
title: 'Insert/Edit Media',
form: mediaFormDefinition,
});
manager.registerModal('source', {
title: 'Source code',
form: sourceFormDefinition,
});
2024-05-30 11:50:55 -04:00
// Register context toolbars
manager.registerContextToolbar('image', {
selector: 'img:not([drawio-diagram] img)',
content: getImageToolbarContent(),
});
manager.registerContextToolbar('link', {
selector: 'a',
content: getLinkToolbarContent(),
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
const image = originalTarget.querySelector('img');
return image || originalTarget;
}
});
manager.registerContextToolbar('code', {
selector: '.editor-code-block-wrap',
content: getCodeToolbarContent(),
});
manager.registerContextToolbar('table', {
selector: 'td,th',
content: getTableToolbarContent(),
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
return originalTarget.closest('table') as HTMLTableElement;
}
});
// Register image decorator listener
manager.registerDecoratorType('image', ImageDecorator);
manager.registerDecoratorType('code', CodeBlockDecorator);
2024-07-03 05:28:04 -04:00
manager.registerDecoratorType('diagram', DiagramDecorator);
return context;
}