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:
@ -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,18 +11,16 @@ 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";
|
||||||
|
|
||||||
export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
|
const theme = {
|
||||||
const config: CreateEditorArgs = {
|
|
||||||
namespace: 'BookStackPageEditor',
|
|
||||||
nodes: getNodesForPageEditor(),
|
|
||||||
onError: console.error,
|
|
||||||
theme: {
|
|
||||||
text: {
|
text: {
|
||||||
bold: 'editor-theme-bold',
|
bold: 'editor-theme-bold',
|
||||||
code: 'editor-theme-code',
|
code: 'editor-theme-code',
|
||||||
@ -33,43 +31,46 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
|
|||||||
underline: 'editor-theme-underline',
|
underline: 'editor-theme-underline',
|
||||||
underlineStrikethrough: 'editor-theme-underline-strikethrough',
|
underlineStrikethrough: 'editor-theme-underline-strikethrough',
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
const editArea = el('div', {
|
export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
|
||||||
contenteditable: 'true',
|
const editor = createEditor({
|
||||||
class: 'editor-content-area page-content',
|
namespace: 'BookStackPageEditor',
|
||||||
|
nodes: getNodesForPageEditor(),
|
||||||
|
onError: console.error,
|
||||||
|
theme: theme,
|
||||||
});
|
});
|
||||||
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;
|
||||||
|
|
||||||
|
@ -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,28 +221,35 @@ 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[] {
|
},
|
||||||
|
link: {
|
||||||
|
selector: 'a',
|
||||||
|
content() {
|
||||||
return [
|
return [
|
||||||
new EditorButton(link),
|
new EditorButton(link),
|
||||||
new EditorButton(unlink),
|
new EditorButton(unlink),
|
||||||
];
|
]
|
||||||
}
|
},
|
||||||
|
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
|
||||||
export function getCodeToolbarContent(): EditorUiElement[] {
|
const image = originalTarget.querySelector('img');
|
||||||
return [
|
return image || originalTarget;
|
||||||
new EditorButton(editCodeBlock),
|
}
|
||||||
];
|
},
|
||||||
}
|
code: {
|
||||||
|
selector: '.editor-code-block-wrap',
|
||||||
export function getTableToolbarContent(): EditorUiElement[] {
|
content: () => [new EditorButton(editCodeBlock)],
|
||||||
|
},
|
||||||
|
table: {
|
||||||
|
selector: 'td,th',
|
||||||
|
content() {
|
||||||
return [
|
return [
|
||||||
new EditorOverflowContainer(2, [
|
new EditorOverflowContainer(2, [
|
||||||
new EditorButton(tableProperties),
|
new EditorButton(tableProperties),
|
||||||
@ -258,12 +266,19 @@ export function getTableToolbarContent(): EditorUiElement[] {
|
|||||||
new EditorButton(deleteColumn),
|
new EditorButton(deleteColumn),
|
||||||
]),
|
]),
|
||||||
];
|
];
|
||||||
}
|
},
|
||||||
|
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
|
||||||
export function getDetailsToolbarContent(): EditorUiElement[] {
|
return originalTarget.closest('table') as HTMLTableElement;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
details: {
|
||||||
|
selector: 'details',
|
||||||
|
content() {
|
||||||
return [
|
return [
|
||||||
new EditorButton(detailsEditLabel),
|
new EditorButton(detailsEditLabel),
|
||||||
new EditorButton(detailsToggle),
|
new EditorButton(detailsToggle),
|
||||||
new EditorButton(detailsUnwrap),
|
new EditorButton(detailsUnwrap),
|
||||||
];
|
]
|
||||||
}
|
},
|
||||||
|
},
|
||||||
|
};
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
Reference in New Issue
Block a user