1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-21 09:22:09 +03:00

Lexical: Started comment implementation

Refactors some UI and toolbar code for better abstract use across editor
versions.
This commit is contained in:
Dan Brown
2025-06-24 17:47:53 +01:00
parent dfeca246a0
commit c606970e38
5 changed files with 149 additions and 144 deletions

View File

@ -1,4 +1,4 @@
import {$getSelection, createEditor, CreateEditorArgs, LexicalEditor} from 'lexical'; import {createEditor, LexicalEditor} from 'lexical';
import {createEmptyHistoryState, registerHistory} from '@lexical/history'; import {createEmptyHistoryState, registerHistory} from '@lexical/history';
import {registerRichText} from '@lexical/rich-text'; import {registerRichText} from '@lexical/rich-text';
import {mergeRegister} from '@lexical/utils'; import {mergeRegister} from '@lexical/utils';
@ -11,65 +11,66 @@ import {listen as listenToCommonEvents} from "./services/common-events";
import {registerDropPasteHandling} from "./services/drop-paste-handling"; import {registerDropPasteHandling} from "./services/drop-paste-handling";
import {registerTaskListHandler} from "./ui/framework/helpers/task-list-handler"; import {registerTaskListHandler} from "./ui/framework/helpers/task-list-handler";
import {registerTableSelectionHandler} from "./ui/framework/helpers/table-selection-handler"; import {registerTableSelectionHandler} from "./ui/framework/helpers/table-selection-handler";
import {el} from "./utils/dom";
import {registerShortcuts} from "./services/shortcuts"; import {registerShortcuts} from "./services/shortcuts";
import {registerNodeResizer} from "./ui/framework/helpers/node-resizer"; import {registerNodeResizer} from "./ui/framework/helpers/node-resizer";
import {registerKeyboardHandling} from "./services/keyboard-handling"; import {registerKeyboardHandling} from "./services/keyboard-handling";
import {registerAutoLinks} from "./services/auto-links"; import {registerAutoLinks} from "./services/auto-links";
import {contextToolbars, getMainEditorFullToolbar} from "./ui/defaults/toolbars";
import {modals} from "./ui/defaults/modals";
import {CodeBlockDecorator} from "./ui/decorators/code-block";
import {DiagramDecorator} from "./ui/decorators/diagram";
const theme = {
text: {
bold: 'editor-theme-bold',
code: 'editor-theme-code',
italic: 'editor-theme-italic',
strikethrough: 'editor-theme-strikethrough',
subscript: 'editor-theme-subscript',
superscript: 'editor-theme-superscript',
underline: 'editor-theme-underline',
underlineStrikethrough: 'editor-theme-underline-strikethrough',
}
};
export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface { export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
const config: CreateEditorArgs = { const editor = createEditor({
namespace: 'BookStackPageEditor', namespace: 'BookStackPageEditor',
nodes: getNodesForPageEditor(), nodes: getNodesForPageEditor(),
onError: console.error, onError: console.error,
theme: { theme: theme,
text: {
bold: 'editor-theme-bold',
code: 'editor-theme-code',
italic: 'editor-theme-italic',
strikethrough: 'editor-theme-strikethrough',
subscript: 'editor-theme-subscript',
superscript: 'editor-theme-superscript',
underline: 'editor-theme-underline',
underlineStrikethrough: 'editor-theme-underline-strikethrough',
}
}
};
const editArea = el('div', {
contenteditable: 'true',
class: 'editor-content-area page-content',
}); });
const editWrap = el('div', { const context: EditorUiContext = buildEditorUI(container, editor, {
class: 'editor-content-wrap', ...options,
}, [editArea]); editorClass: 'page-content',
});
container.append(editWrap); editor.setRootElement(context.editorDOM);
container.classList.add('editor-container');
container.setAttribute('dir', options.textDirection);
if (options.darkMode) {
container.classList.add('editor-dark');
}
const editor = createEditor(config);
editor.setRootElement(editArea);
const context: EditorUiContext = buildEditorUI(container, editArea, editWrap, editor, options);
mergeRegister( mergeRegister(
registerRichText(editor), registerRichText(editor),
registerHistory(editor, createEmptyHistoryState(), 300), registerHistory(editor, createEmptyHistoryState(), 300),
registerShortcuts(context), registerShortcuts(context),
registerKeyboardHandling(context), registerKeyboardHandling(context),
registerTableResizer(editor, editWrap), registerTableResizer(editor, context.scrollDOM),
registerTableSelectionHandler(editor), registerTableSelectionHandler(editor),
registerTaskListHandler(editor, editArea), registerTaskListHandler(editor, context.editorDOM),
registerDropPasteHandling(context), registerDropPasteHandling(context),
registerNodeResizer(context), registerNodeResizer(context),
registerAutoLinks(editor), registerAutoLinks(editor),
); );
listenToCommonEvents(editor); // Register toolbars, modals & decorators
context.manager.setToolbar(getMainEditorFullToolbar(context));
for (const key of Object.keys(contextToolbars)) {
context.manager.registerContextToolbar(key, contextToolbars[key]);
}
for (const key of Object.keys(modals)) {
context.manager.registerModal(key, modals[key]);
}
context.manager.registerDecoratorType('code', CodeBlockDecorator);
context.manager.registerDecoratorType('diagram', DiagramDecorator);
listenToCommonEvents(editor);
setEditorContentFromHtml(editor, htmlContent); setEditorContentFromHtml(editor, htmlContent);
const debugView = document.getElementById('lexical-debug'); const debugView = document.getElementById('lexical-debug');
@ -92,6 +93,33 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
return new SimpleWysiwygEditorInterface(editor); return new SimpleWysiwygEditorInterface(editor);
} }
export function createCommentEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
const editor = createEditor({
namespace: 'BookStackCommentEditor',
nodes: getNodesForPageEditor(),
onError: console.error,
theme: theme,
});
const context: EditorUiContext = buildEditorUI(container, editor, options);
editor.setRootElement(context.editorDOM);
mergeRegister(
registerRichText(editor),
registerHistory(editor, createEmptyHistoryState(), 300),
registerShortcuts(context),
registerAutoLinks(editor),
);
// Register toolbars, modals & decorators
context.manager.setToolbar(getMainEditorFullToolbar(context)); // TODO - Create comment toolbar
context.manager.registerContextToolbar('link', contextToolbars.link);
context.manager.registerModal('link', modals.link);
setEditorContentFromHtml(editor, htmlContent);
return new SimpleWysiwygEditorInterface(editor);
}
export class SimpleWysiwygEditorInterface { export class SimpleWysiwygEditorInterface {
protected editor: LexicalEditor; protected editor: LexicalEditor;

View File

@ -79,6 +79,7 @@ import {
import {el} from "../../utils/dom"; import {el} from "../../utils/dom";
import {EditorButtonWithMenu} from "../framework/blocks/button-with-menu"; import {EditorButtonWithMenu} from "../framework/blocks/button-with-menu";
import {EditorSeparator} from "../framework/blocks/separator"; import {EditorSeparator} from "../framework/blocks/separator";
import {EditorContextToolbarDefinition} from "../framework/toolbars";
export function getMainEditorFullToolbar(context: EditorUiContext): EditorContainerUiElement { export function getMainEditorFullToolbar(context: EditorUiContext): EditorContainerUiElement {
@ -220,50 +221,64 @@ export function getMainEditorFullToolbar(context: EditorUiContext): EditorContai
]); ]);
} }
export function getImageToolbarContent(): EditorUiElement[] { export const contextToolbars: Record<string, EditorContextToolbarDefinition> = {
return [new EditorButton(image)]; image: {
} selector: 'img:not([drawio-diagram] img)',
content: () => [new EditorButton(image)],
export function getMediaToolbarContent(): EditorUiElement[] { },
return [new EditorButton(media)]; media: {
} selector: '.editor-media-wrap',
content: () => [new EditorButton(media)],
export function getLinkToolbarContent(): EditorUiElement[] { },
return [ link: {
new EditorButton(link), selector: 'a',
new EditorButton(unlink), content() {
]; return [
} new EditorButton(link),
new EditorButton(unlink),
export function getCodeToolbarContent(): EditorUiElement[] { ]
return [ },
new EditorButton(editCodeBlock), displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
]; const image = originalTarget.querySelector('img');
} return image || originalTarget;
}
export function getTableToolbarContent(): EditorUiElement[] { },
return [ code: {
new EditorOverflowContainer(2, [ selector: '.editor-code-block-wrap',
new EditorButton(tableProperties), content: () => [new EditorButton(editCodeBlock)],
new EditorButton(deleteTable), },
]), table: {
new EditorOverflowContainer(3, [ selector: 'td,th',
new EditorButton(insertRowAbove), content() {
new EditorButton(insertRowBelow), return [
new EditorButton(deleteRow), new EditorOverflowContainer(2, [
]), new EditorButton(tableProperties),
new EditorOverflowContainer(3, [ new EditorButton(deleteTable),
new EditorButton(insertColumnBefore), ]),
new EditorButton(insertColumnAfter), new EditorOverflowContainer(3, [
new EditorButton(deleteColumn), new EditorButton(insertRowAbove),
]), new EditorButton(insertRowBelow),
]; new EditorButton(deleteRow),
} ]),
new EditorOverflowContainer(3, [
export function getDetailsToolbarContent(): EditorUiElement[] { new EditorButton(insertColumnBefore),
return [ new EditorButton(insertColumnAfter),
new EditorButton(detailsEditLabel), new EditorButton(deleteColumn),
new EditorButton(detailsToggle), ]),
new EditorButton(detailsUnwrap), ];
]; },
} displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
return originalTarget.closest('table') as HTMLTableElement;
}
},
details: {
selector: 'details',
content() {
return [
new EditorButton(detailsEditLabel),
new EditorButton(detailsToggle),
new EditorButton(detailsUnwrap),
]
},
},
};

View File

@ -198,7 +198,7 @@ export class EditorUIManager {
contentByTarget.set(targetEl, []) contentByTarget.set(targetEl, [])
} }
// @ts-ignore // @ts-ignore
contentByTarget.get(targetEl).push(...definition.content); contentByTarget.get(targetEl).push(...definition.content());
} }
} }

View File

@ -4,7 +4,7 @@ import {el} from "../../utils/dom";
export type EditorContextToolbarDefinition = { export type EditorContextToolbarDefinition = {
selector: string; selector: string;
content: EditorUiElement[], content: () => EditorUiElement[],
displayTargetLocator?: (originalTarget: HTMLElement) => HTMLElement; displayTargetLocator?: (originalTarget: HTMLElement) => HTMLElement;
}; };

View File

@ -1,23 +1,30 @@
import {LexicalEditor} from "lexical"; import {LexicalEditor} from "lexical";
import {
getCodeToolbarContent, getDetailsToolbarContent,
getImageToolbarContent,
getLinkToolbarContent,
getMainEditorFullToolbar, getMediaToolbarContent, getTableToolbarContent
} from "./defaults/toolbars";
import {EditorUIManager} from "./framework/manager"; import {EditorUIManager} from "./framework/manager";
import {EditorUiContext} from "./framework/core"; import {EditorUiContext} from "./framework/core";
import {CodeBlockDecorator} from "./decorators/code-block"; import {el} from "../utils/dom";
import {DiagramDecorator} from "./decorators/diagram";
import {modals} from "./defaults/modals"; 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(container: HTMLElement, element: HTMLElement, scrollContainer: HTMLElement, editor: LexicalEditor, options: Record<string, any>): EditorUiContext {
const manager = new EditorUIManager(); const manager = new EditorUIManager();
const context: EditorUiContext = { const context: EditorUiContext = {
editor, editor,
containerDOM: container, containerDOM: containerDOM,
editorDOM: element, editorDOM: editorDOM,
scrollDOM: scrollContainer, scrollDOM: scrollDOM,
manager, manager,
translate(text: string): string { translate(text: string): string {
const translations = options.translations; const translations = options.translations;
@ -31,50 +38,5 @@ export function buildEditorUI(container: HTMLElement, element: HTMLElement, scro
}; };
manager.setContext(context); manager.setContext(context);
// Create primary toolbar
manager.setToolbar(getMainEditorFullToolbar(context));
// Register modals
for (const key of Object.keys(modals)) {
manager.registerModal(key, modals[key]);
}
// Register context toolbars
manager.registerContextToolbar('image', {
selector: 'img:not([drawio-diagram] img)',
content: getImageToolbarContent(),
});
manager.registerContextToolbar('media', {
selector: '.editor-media-wrap',
content: getMediaToolbarContent(),
});
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;
}
});
manager.registerContextToolbar('details', {
selector: 'details',
content: getDetailsToolbarContent(),
});
// Register image decorator listener
manager.registerDecoratorType('code', CodeBlockDecorator);
manager.registerDecoratorType('diagram', DiagramDecorator);
return context; return context;
} }