Skip to content

Commit c606970

Browse files
committed
Lexical: Started comment implementation
Refactors some UI and toolbar code for better abstract use across editor versions.
1 parent dfeca24 commit c606970

File tree

5 files changed

+149
-144
lines changed

5 files changed

+149
-144
lines changed

resources/js/wysiwyg/index.ts

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {$getSelection, createEditor, CreateEditorArgs, LexicalEditor} from 'lexical';
1+
import {createEditor, LexicalEditor} from 'lexical';
22
import {createEmptyHistoryState, registerHistory} from '@lexical/history';
33
import {registerRichText} from '@lexical/rich-text';
44
import {mergeRegister} from '@lexical/utils';
@@ -11,65 +11,66 @@ import {listen as listenToCommonEvents} from "./services/common-events";
1111
import {registerDropPasteHandling} from "./services/drop-paste-handling";
1212
import {registerTaskListHandler} from "./ui/framework/helpers/task-list-handler";
1313
import {registerTableSelectionHandler} from "./ui/framework/helpers/table-selection-handler";
14-
import {el} from "./utils/dom";
1514
import {registerShortcuts} from "./services/shortcuts";
1615
import {registerNodeResizer} from "./ui/framework/helpers/node-resizer";
1716
import {registerKeyboardHandling} from "./services/keyboard-handling";
1817
import {registerAutoLinks} from "./services/auto-links";
18+
import {contextToolbars, getMainEditorFullToolbar} from "./ui/defaults/toolbars";
19+
import {modals} from "./ui/defaults/modals";
20+
import {CodeBlockDecorator} from "./ui/decorators/code-block";
21+
import {DiagramDecorator} from "./ui/decorators/diagram";
22+
23+
const theme = {
24+
text: {
25+
bold: 'editor-theme-bold',
26+
code: 'editor-theme-code',
27+
italic: 'editor-theme-italic',
28+
strikethrough: 'editor-theme-strikethrough',
29+
subscript: 'editor-theme-subscript',
30+
superscript: 'editor-theme-superscript',
31+
underline: 'editor-theme-underline',
32+
underlineStrikethrough: 'editor-theme-underline-strikethrough',
33+
}
34+
};
1935

2036
export function createPageEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
21-
const config: CreateEditorArgs = {
37+
const editor = createEditor({
2238
namespace: 'BookStackPageEditor',
2339
nodes: getNodesForPageEditor(),
2440
onError: console.error,
25-
theme: {
26-
text: {
27-
bold: 'editor-theme-bold',
28-
code: 'editor-theme-code',
29-
italic: 'editor-theme-italic',
30-
strikethrough: 'editor-theme-strikethrough',
31-
subscript: 'editor-theme-subscript',
32-
superscript: 'editor-theme-superscript',
33-
underline: 'editor-theme-underline',
34-
underlineStrikethrough: 'editor-theme-underline-strikethrough',
35-
}
36-
}
37-
};
38-
39-
const editArea = el('div', {
40-
contenteditable: 'true',
41-
class: 'editor-content-area page-content',
41+
theme: theme,
4242
});
43-
const editWrap = el('div', {
44-
class: 'editor-content-wrap',
45-
}, [editArea]);
46-
47-
container.append(editWrap);
48-
container.classList.add('editor-container');
49-
container.setAttribute('dir', options.textDirection);
50-
if (options.darkMode) {
51-
container.classList.add('editor-dark');
52-
}
53-
54-
const editor = createEditor(config);
55-
editor.setRootElement(editArea);
56-
const context: EditorUiContext = buildEditorUI(container, editArea, editWrap, editor, options);
43+
const context: EditorUiContext = buildEditorUI(container, editor, {
44+
...options,
45+
editorClass: 'page-content',
46+
});
47+
editor.setRootElement(context.editorDOM);
5748

5849
mergeRegister(
5950
registerRichText(editor),
6051
registerHistory(editor, createEmptyHistoryState(), 300),
6152
registerShortcuts(context),
6253
registerKeyboardHandling(context),
63-
registerTableResizer(editor, editWrap),
54+
registerTableResizer(editor, context.scrollDOM),
6455
registerTableSelectionHandler(editor),
65-
registerTaskListHandler(editor, editArea),
56+
registerTaskListHandler(editor, context.editorDOM),
6657
registerDropPasteHandling(context),
6758
registerNodeResizer(context),
6859
registerAutoLinks(editor),
6960
);
7061

71-
listenToCommonEvents(editor);
62+
// Register toolbars, modals & decorators
63+
context.manager.setToolbar(getMainEditorFullToolbar(context));
64+
for (const key of Object.keys(contextToolbars)) {
65+
context.manager.registerContextToolbar(key, contextToolbars[key]);
66+
}
67+
for (const key of Object.keys(modals)) {
68+
context.manager.registerModal(key, modals[key]);
69+
}
70+
context.manager.registerDecoratorType('code', CodeBlockDecorator);
71+
context.manager.registerDecoratorType('diagram', DiagramDecorator);
7272

73+
listenToCommonEvents(editor);
7374
setEditorContentFromHtml(editor, htmlContent);
7475

7576
const debugView = document.getElementById('lexical-debug');
@@ -92,6 +93,33 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
9293
return new SimpleWysiwygEditorInterface(editor);
9394
}
9495

96+
export function createCommentEditorInstance(container: HTMLElement, htmlContent: string, options: Record<string, any> = {}): SimpleWysiwygEditorInterface {
97+
const editor = createEditor({
98+
namespace: 'BookStackCommentEditor',
99+
nodes: getNodesForPageEditor(),
100+
onError: console.error,
101+
theme: theme,
102+
});
103+
const context: EditorUiContext = buildEditorUI(container, editor, options);
104+
editor.setRootElement(context.editorDOM);
105+
106+
mergeRegister(
107+
registerRichText(editor),
108+
registerHistory(editor, createEmptyHistoryState(), 300),
109+
registerShortcuts(context),
110+
registerAutoLinks(editor),
111+
);
112+
113+
// Register toolbars, modals & decorators
114+
context.manager.setToolbar(getMainEditorFullToolbar(context)); // TODO - Create comment toolbar
115+
context.manager.registerContextToolbar('link', contextToolbars.link);
116+
context.manager.registerModal('link', modals.link);
117+
118+
setEditorContentFromHtml(editor, htmlContent);
119+
120+
return new SimpleWysiwygEditorInterface(editor);
121+
}
122+
95123
export class SimpleWysiwygEditorInterface {
96124
protected editor: LexicalEditor;
97125

resources/js/wysiwyg/ui/defaults/toolbars.ts

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
import {el} from "../../utils/dom";
8080
import {EditorButtonWithMenu} from "../framework/blocks/button-with-menu";
8181
import {EditorSeparator} from "../framework/blocks/separator";
82+
import {EditorContextToolbarDefinition} from "../framework/toolbars";
8283

8384
export function getMainEditorFullToolbar(context: EditorUiContext): EditorContainerUiElement {
8485

@@ -220,50 +221,64 @@ export function getMainEditorFullToolbar(context: EditorUiContext): EditorContai
220221
]);
221222
}
222223

223-
export function getImageToolbarContent(): EditorUiElement[] {
224-
return [new EditorButton(image)];
225-
}
226-
227-
export function getMediaToolbarContent(): EditorUiElement[] {
228-
return [new EditorButton(media)];
229-
}
230-
231-
export function getLinkToolbarContent(): EditorUiElement[] {
232-
return [
233-
new EditorButton(link),
234-
new EditorButton(unlink),
235-
];
236-
}
237-
238-
export function getCodeToolbarContent(): EditorUiElement[] {
239-
return [
240-
new EditorButton(editCodeBlock),
241-
];
242-
}
243-
244-
export function getTableToolbarContent(): EditorUiElement[] {
245-
return [
246-
new EditorOverflowContainer(2, [
247-
new EditorButton(tableProperties),
248-
new EditorButton(deleteTable),
249-
]),
250-
new EditorOverflowContainer(3, [
251-
new EditorButton(insertRowAbove),
252-
new EditorButton(insertRowBelow),
253-
new EditorButton(deleteRow),
254-
]),
255-
new EditorOverflowContainer(3, [
256-
new EditorButton(insertColumnBefore),
257-
new EditorButton(insertColumnAfter),
258-
new EditorButton(deleteColumn),
259-
]),
260-
];
261-
}
262-
263-
export function getDetailsToolbarContent(): EditorUiElement[] {
264-
return [
265-
new EditorButton(detailsEditLabel),
266-
new EditorButton(detailsToggle),
267-
new EditorButton(detailsUnwrap),
268-
];
269-
}
224+
export const contextToolbars: Record<string, EditorContextToolbarDefinition> = {
225+
image: {
226+
selector: 'img:not([drawio-diagram] img)',
227+
content: () => [new EditorButton(image)],
228+
},
229+
media: {
230+
selector: '.editor-media-wrap',
231+
content: () => [new EditorButton(media)],
232+
},
233+
link: {
234+
selector: 'a',
235+
content() {
236+
return [
237+
new EditorButton(link),
238+
new EditorButton(unlink),
239+
]
240+
},
241+
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
242+
const image = originalTarget.querySelector('img');
243+
return image || originalTarget;
244+
}
245+
},
246+
code: {
247+
selector: '.editor-code-block-wrap',
248+
content: () => [new EditorButton(editCodeBlock)],
249+
},
250+
table: {
251+
selector: 'td,th',
252+
content() {
253+
return [
254+
new EditorOverflowContainer(2, [
255+
new EditorButton(tableProperties),
256+
new EditorButton(deleteTable),
257+
]),
258+
new EditorOverflowContainer(3, [
259+
new EditorButton(insertRowAbove),
260+
new EditorButton(insertRowBelow),
261+
new EditorButton(deleteRow),
262+
]),
263+
new EditorOverflowContainer(3, [
264+
new EditorButton(insertColumnBefore),
265+
new EditorButton(insertColumnAfter),
266+
new EditorButton(deleteColumn),
267+
]),
268+
];
269+
},
270+
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
271+
return originalTarget.closest('table') as HTMLTableElement;
272+
}
273+
},
274+
details: {
275+
selector: 'details',
276+
content() {
277+
return [
278+
new EditorButton(detailsEditLabel),
279+
new EditorButton(detailsToggle),
280+
new EditorButton(detailsUnwrap),
281+
]
282+
},
283+
},
284+
};

resources/js/wysiwyg/ui/framework/manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class EditorUIManager {
198198
contentByTarget.set(targetEl, [])
199199
}
200200
// @ts-ignore
201-
contentByTarget.get(targetEl).push(...definition.content);
201+
contentByTarget.get(targetEl).push(...definition.content());
202202
}
203203
}
204204

resources/js/wysiwyg/ui/framework/toolbars.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {el} from "../../utils/dom";
44

55
export type EditorContextToolbarDefinition = {
66
selector: string;
7-
content: EditorUiElement[],
7+
content: () => EditorUiElement[],
88
displayTargetLocator?: (originalTarget: HTMLElement) => HTMLElement;
99
};
1010

resources/js/wysiwyg/ui/index.ts

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
import {LexicalEditor} from "lexical";
2-
import {
3-
getCodeToolbarContent, getDetailsToolbarContent,
4-
getImageToolbarContent,
5-
getLinkToolbarContent,
6-
getMainEditorFullToolbar, getMediaToolbarContent, getTableToolbarContent
7-
} from "./defaults/toolbars";
82
import {EditorUIManager} from "./framework/manager";
93
import {EditorUiContext} from "./framework/core";
10-
import {CodeBlockDecorator} from "./decorators/code-block";
11-
import {DiagramDecorator} from "./decorators/diagram";
12-
import {modals} from "./defaults/modals";
4+
import {el} from "../utils/dom";
5+
6+
export function buildEditorUI(containerDOM: HTMLElement, editor: LexicalEditor, options: Record<string, any>): EditorUiContext {
7+
const editorDOM = el('div', {
8+
contenteditable: 'true',
9+
class: `editor-content-area ${options.editorClass || ''}`,
10+
});
11+
const scrollDOM = el('div', {
12+
class: 'editor-content-wrap',
13+
}, [editorDOM]);
14+
15+
containerDOM.append(scrollDOM);
16+
containerDOM.classList.add('editor-container');
17+
containerDOM.setAttribute('dir', options.textDirection);
18+
if (options.darkMode) {
19+
containerDOM.classList.add('editor-dark');
20+
}
1321

14-
export function buildEditorUI(container: HTMLElement, element: HTMLElement, scrollContainer: HTMLElement, editor: LexicalEditor, options: Record<string, any>): EditorUiContext {
1522
const manager = new EditorUIManager();
1623
const context: EditorUiContext = {
1724
editor,
18-
containerDOM: container,
19-
editorDOM: element,
20-
scrollDOM: scrollContainer,
25+
containerDOM: containerDOM,
26+
editorDOM: editorDOM,
27+
scrollDOM: scrollDOM,
2128
manager,
2229
translate(text: string): string {
2330
const translations = options.translations;
@@ -31,50 +38,5 @@ export function buildEditorUI(container: HTMLElement, element: HTMLElement, scro
3138
};
3239
manager.setContext(context);
3340

34-
// Create primary toolbar
35-
manager.setToolbar(getMainEditorFullToolbar(context));
36-
37-
// Register modals
38-
for (const key of Object.keys(modals)) {
39-
manager.registerModal(key, modals[key]);
40-
}
41-
42-
// Register context toolbars
43-
manager.registerContextToolbar('image', {
44-
selector: 'img:not([drawio-diagram] img)',
45-
content: getImageToolbarContent(),
46-
});
47-
manager.registerContextToolbar('media', {
48-
selector: '.editor-media-wrap',
49-
content: getMediaToolbarContent(),
50-
});
51-
manager.registerContextToolbar('link', {
52-
selector: 'a',
53-
content: getLinkToolbarContent(),
54-
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
55-
const image = originalTarget.querySelector('img');
56-
return image || originalTarget;
57-
}
58-
});
59-
manager.registerContextToolbar('code', {
60-
selector: '.editor-code-block-wrap',
61-
content: getCodeToolbarContent(),
62-
});
63-
manager.registerContextToolbar('table', {
64-
selector: 'td,th',
65-
content: getTableToolbarContent(),
66-
displayTargetLocator(originalTarget: HTMLElement): HTMLElement {
67-
return originalTarget.closest('table') as HTMLTableElement;
68-
}
69-
});
70-
manager.registerContextToolbar('details', {
71-
selector: 'details',
72-
content: getDetailsToolbarContent(),
73-
});
74-
75-
// Register image decorator listener
76-
manager.registerDecoratorType('code', CodeBlockDecorator);
77-
manager.registerDecoratorType('diagram', DiagramDecorator);
78-
7941
return context;
8042
}

0 commit comments

Comments
 (0)