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

37 lines
1.1 KiB
TypeScript
Raw Normal View History

import {
$getSelection,
COMMAND_PRIORITY_LOW,
LexicalEditor,
SELECTION_CHANGE_COMMAND
} from "lexical";
import {getMainEditorFullToolbar} from "./toolbars";
2024-05-30 23:50:55 +08:00
import {EditorUIManager} from "./framework/manager";
import {link as linkFormDefinition} from "./defaults/form-definitions";
export function buildEditorUI(element: HTMLElement, editor: LexicalEditor) {
2024-05-30 23:50:55 +08:00
const manager = new EditorUIManager();
const context = {
editor,
manager,
translate: (text: string): string => text,
};
manager.setContext(context);
2024-05-30 23:50:55 +08:00
// Create primary toolbar
const toolbar = getMainEditorFullToolbar();
2024-05-30 23:50:55 +08:00
toolbar.setContext(context);
element.before(toolbar.getDOMElement());
// Register modals
manager.registerModal('link', {
title: 'Insert/Edit link',
form: linkFormDefinition,
});
2024-05-30 23:50:55 +08:00
// Update button states on editor selection change
editor.registerCommand(SELECTION_CHANGE_COMMAND, () => {
const selection = $getSelection();
toolbar.updateState({editor, selection});
return false;
}, COMMAND_PRIORITY_LOW);
}