+
+ setToolbar(toolbar: EditorContainerUiElement) {
+ if (this.toolbar) {
+ this.toolbar.getDOMElement().remove();
+ }
+
+ this.toolbar = toolbar;
+ toolbar.setContext(this.getContext());
+ this.getContext().editorDOM.before(toolbar.getDOMElement());
+ }
+
+ protected triggerStateUpdate(state: EditorUiStateUpdate): void {
+ const context = this.getContext();
+ context.lastSelection = state.selection;
+ this.toolbar?.updateState(state);
+ }
+
+ protected setupEditor(editor: LexicalEditor) {
+ // Update button states on editor selection change
+ editor.registerCommand(SELECTION_CHANGE_COMMAND, () => {
+ this.triggerStateUpdate({
+ editor: editor,
+ selection: $getSelection(),
+ });
+ return false;
+ }, COMMAND_PRIORITY_LOW);
+
+ // Register our DOM decorate listener with the editor
+ const domDecorateListener: DecoratorListener<EditorDecoratorAdapter> = (decorators: Record<NodeKey, EditorDecoratorAdapter>) => {
+ const keys = Object.keys(decorators);
+ for (const key of keys) {
+ const decoratedEl = editor.getElementByKey(key);
+ const adapter = decorators[key];
+ const decorator = this.getDecorator(adapter.type, key);
+ decorator.setNode(adapter.getNode());
+ const decoratorEl = decorator.render(this.getContext());
+ if (decoratedEl) {
+ decoratedEl.append(decoratorEl);
+ }
+ }
+ }
+ editor.registerDecoratorListener(domDecorateListener);
+ }