@@ -2,9 +2,9 @@ import {createEditor, LexicalEditor} from 'lexical';
22import { createEmptyHistoryState , registerHistory } from '@lexical/history' ;
33import { registerRichText } from '@lexical/rich-text' ;
44import { mergeRegister } from '@lexical/utils' ;
5- import { getNodesForPageEditor , registerCommonNodeMutationListeners } from './nodes' ;
5+ import { getNodesForBasicEditor , getNodesForPageEditor , registerCommonNodeMutationListeners } from './nodes' ;
66import { buildEditorUI } from "./ui" ;
7- import { getEditorContentAsHtml , setEditorContentFromHtml } from "./utils/actions" ;
7+ import { focusEditor , getEditorContentAsHtml , setEditorContentFromHtml } from "./utils/actions" ;
88import { registerTableResizer } from "./ui/framework/helpers/table-resizer" ;
99import { EditorUiContext } from "./ui/framework/core" ;
1010import { listen as listenToCommonEvents } from "./services/common-events" ;
@@ -15,7 +15,7 @@ import {registerShortcuts} from "./services/shortcuts";
1515import { registerNodeResizer } from "./ui/framework/helpers/node-resizer" ;
1616import { registerKeyboardHandling } from "./services/keyboard-handling" ;
1717import { registerAutoLinks } from "./services/auto-links" ;
18- import { contextToolbars , getMainEditorFullToolbar } from "./ui/defaults/toolbars" ;
18+ import { contextToolbars , getBasicEditorToolbar , getMainEditorFullToolbar } from "./ui/defaults/toolbars" ;
1919import { modals } from "./ui/defaults/modals" ;
2020import { CodeBlockDecorator } from "./ui/decorators/code-block" ;
2121import { DiagramDecorator } from "./ui/decorators/diagram" ;
@@ -90,44 +90,54 @@ export function createPageEditorInstance(container: HTMLElement, htmlContent: st
9090
9191 registerCommonNodeMutationListeners ( context ) ;
9292
93- return new SimpleWysiwygEditorInterface ( editor ) ;
93+ return new SimpleWysiwygEditorInterface ( context ) ;
9494}
9595
96- export function createCommentEditorInstance ( container : HTMLElement , htmlContent : string , options : Record < string , any > = { } ) : SimpleWysiwygEditorInterface {
96+ export function createBasicEditorInstance ( container : HTMLElement , htmlContent : string , options : Record < string , any > = { } ) : SimpleWysiwygEditorInterface {
9797 const editor = createEditor ( {
98- namespace : 'BookStackCommentEditor ' ,
99- nodes : getNodesForPageEditor ( ) ,
98+ namespace : 'BookStackBasicEditor ' ,
99+ nodes : getNodesForBasicEditor ( ) ,
100100 onError : console . error ,
101101 theme : theme ,
102102 } ) ;
103103 const context : EditorUiContext = buildEditorUI ( container , editor , options ) ;
104104 editor . setRootElement ( context . editorDOM ) ;
105105
106- mergeRegister (
106+ const editorTeardown = mergeRegister (
107107 registerRichText ( editor ) ,
108108 registerHistory ( editor , createEmptyHistoryState ( ) , 300 ) ,
109109 registerShortcuts ( context ) ,
110110 registerAutoLinks ( editor ) ,
111111 ) ;
112112
113113 // Register toolbars, modals & decorators
114- context . manager . setToolbar ( getMainEditorFullToolbar ( context ) ) ; // TODO - Create comment toolbar
114+ context . manager . setToolbar ( getBasicEditorToolbar ( context ) ) ;
115115 context . manager . registerContextToolbar ( 'link' , contextToolbars . link ) ;
116116 context . manager . registerModal ( 'link' , modals . link ) ;
117+ context . manager . onTeardown ( editorTeardown ) ;
117118
118119 setEditorContentFromHtml ( editor , htmlContent ) ;
119120
120- return new SimpleWysiwygEditorInterface ( editor ) ;
121+ return new SimpleWysiwygEditorInterface ( context ) ;
121122}
122123
123124export class SimpleWysiwygEditorInterface {
124- protected editor : LexicalEditor ;
125+ protected context : EditorUiContext ;
125126
126- constructor ( editor : LexicalEditor ) {
127- this . editor = editor ;
127+ constructor ( context : EditorUiContext ) {
128+ this . context = context ;
128129 }
129130
130131 async getContentAsHtml ( ) : Promise < string > {
131- return await getEditorContentAsHtml ( this . editor ) ;
132+ return await getEditorContentAsHtml ( this . context . editor ) ;
133+ }
134+
135+ focus ( ) : void {
136+ focusEditor ( this . context . editor ) ;
137+ }
138+
139+ remove ( ) {
140+ this . context . editorDOM . remove ( ) ;
141+ this . context . manager . teardown ( ) ;
132142 }
133143}
0 commit comments