]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/ui/index.ts
Tests: Updated comment test to account for new editor usage
[bookstack] / resources / js / wysiwyg / ui / index.ts
index b2fd7e05a1391d435c4d85c35b5e11b2128167fc..c48386bb4920e196eba48be07eca100bc713ae0f 100644 (file)
@@ -1,70 +1,42 @@
-import {
-    $getSelection,
-    COMMAND_PRIORITY_LOW,
-    LexicalEditor,
-    SELECTION_CHANGE_COMMAND
-} from "lexical";
-import {getMainEditorFullToolbar} from "./toolbars";
+import {LexicalEditor} from "lexical";
 import {EditorUIManager} from "./framework/manager";
-import {image as imageFormDefinition, link as linkFormDefinition, source as sourceFormDefinition} from "./defaults/form-definitions";
-import {DecoratorListener} from "lexical/LexicalEditor";
-import type {NodeKey} from "lexical/LexicalNode";
-import {EditorDecoratorAdapter} from "./framework/decorator";
-import {ImageDecorator} from "./decorators/image";
 import {EditorUiContext} from "./framework/core";
+import {el} from "../utils/dom";
+
+export function buildEditorUI(containerDOM: HTMLElement, editor: LexicalEditor, options: Record<string, any>): EditorUiContext {
+    const editorDOM = el('div', {
+        contenteditable: 'true',
+        class: `editor-content-area ${options.editorClass || ''}`,
+    });
+    const scrollDOM = el('div', {
+        class: 'editor-content-wrap',
+    }, [editorDOM]);
+
+    containerDOM.append(scrollDOM);
+    containerDOM.classList.add('editor-container');
+    containerDOM.setAttribute('dir', options.textDirection);
+    if (options.darkMode) {
+        containerDOM.classList.add('editor-dark');
+    }
 
-export function buildEditorUI(element: HTMLElement, editor: LexicalEditor) {
     const manager = new EditorUIManager();
     const context: EditorUiContext = {
         editor,
+        containerDOM: containerDOM,
+        editorDOM: editorDOM,
+        scrollDOM: scrollDOM,
         manager,
-        translate: (text: string): string => text,
-        lastSelection: null,
+        translate(text: string): string {
+            const translations = options.translations;
+            return translations[text] || text;
+        },
+        error(error: string|Error): void {
+            const message = error instanceof Error ? error.message : error;
+            window.$events.error(message); // TODO - Translate
+        },
+        options,
     };
     manager.setContext(context);
 
-    // Create primary toolbar
-    const toolbar = getMainEditorFullToolbar();
-    toolbar.setContext(context);
-    element.before(toolbar.getDOMElement());
-
-    // 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,
-    });
-
-    // Register decorator listener
-    // Maybe move to manager?
-    manager.registerDecoratorType('image', ImageDecorator);
-    const domDecorateListener: DecoratorListener<EditorDecoratorAdapter> = (decorators: Record<NodeKey, EditorDecoratorAdapter>) => {
-        const keys = Object.keys(decorators);
-        for (const key of keys) {
-            const decoratedEl = editor.getElementByKey(key);
-            const adapter = decorators[key];
-            const decorator = manager.getDecorator(adapter.type, key);
-            decorator.setNode(adapter.getNode());
-            const decoratorEl = decorator.render(context);
-            if (decoratedEl) {
-                decoratedEl.append(decoratorEl);
-            }
-        }
-    }
-    editor.registerDecoratorListener(domDecorateListener);
-
-    // Update button states on editor selection change
-    editor.registerCommand(SELECTION_CHANGE_COMMAND, () => {
-        const selection = $getSelection();
-        toolbar.updateState({editor, selection});
-        context.lastSelection = selection;
-        return false;
-    }, COMMAND_PRIORITY_LOW);
+    return context;
 }
\ No newline at end of file