1- import { $getSelection , createEditor , CreateEditorArgs , LexicalEditor } from 'lexical' ;
1+ import { createEditor , LexicalEditor } from 'lexical' ;
22import { createEmptyHistoryState , registerHistory } from '@lexical/history' ;
33import { registerRichText } from '@lexical/rich-text' ;
44import { mergeRegister } from '@lexical/utils' ;
@@ -11,65 +11,66 @@ import {listen as listenToCommonEvents} from "./services/common-events";
1111import { registerDropPasteHandling } from "./services/drop-paste-handling" ;
1212import { registerTaskListHandler } from "./ui/framework/helpers/task-list-handler" ;
1313import { registerTableSelectionHandler } from "./ui/framework/helpers/table-selection-handler" ;
14- import { el } from "./utils/dom" ;
1514import { registerShortcuts } from "./services/shortcuts" ;
1615import { registerNodeResizer } from "./ui/framework/helpers/node-resizer" ;
1716import { registerKeyboardHandling } from "./services/keyboard-handling" ;
1817import { 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
2036export 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+
95123export class SimpleWysiwygEditorInterface {
96124 protected editor : LexicalEditor ;
97125
0 commit comments