Skip to content

Commit 798c4f5

Browse files
committed
feat: do not create new editor, when initial content changes
1 parent 7165531 commit 798c4f5

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

libs/block-note-angular/src/lib/block-note-editor/bna-editor.component.ts

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
Component,
44
Input,
55
OnChanges,
6-
OnInit,
76
output,
87
SimpleChanges,
98
} from '@angular/core';
@@ -87,51 +86,41 @@ export class BnaEditorComponent<
8786
BSchema extends BlockSchema = DefaultBlockSchema,
8887
ISchema extends InlineContentSchema = DefaultInlineContentSchema,
8988
SSchema extends StyleSchema = DefaultStyleSchema
90-
> implements OnChanges, OnInit
89+
> implements OnChanges
9190
{
9291
@Input()
9392
options?: BlockNoteEditorOptionsType<BSchema, ISchema, SSchema>;
94-
95-
// TODO: move to reusable type
9693
@Input()
97-
initialContent:
94+
initialContent!:
9895
| Block<BSchema, ISchema, SSchema>[]
9996
| PartialBlock<BSchema, ISchema, SSchema>[]
100-
| undefined = undefined;
97+
| undefined;
10198

10299
contentChanged = output<Block<BSchema, ISchema, SSchema>[]>();
103100
selectedBlocks = output<Block<BSchema, ISchema, SSchema>[]>();
104101
onEditorReady = output<BlockNoteEditor<BSchema, ISchema, SSchema>>();
105102

106-
editor!: BlockNoteEditor<BSchema, ISchema, SSchema>;
103+
editor = this.createEditor(undefined);
107104
slashMenuItems: Omit<DefaultSuggestionItem, 'key'>[] = [];
108105

109106
//TODO: remove relying on init flag
110-
isInitialized = false;
107+
isInitialized = true;
111108

112109
constructor(private blockNoteAngularService: BlockNoteAngularService) {}
113110

114-
ngOnInit() {
115-
this.createEditor(this.initialContent);
116-
this.isInitialized = true;
117-
}
118-
119111
ngOnChanges(changes: SimpleChanges) {
120-
if (changes['initialContent']) {
121-
// TODO: try to type the current value (currently any)
122-
this.createEditor(changes['initialContent'].currentValue);
123-
this.isInitialized = true;
112+
if (changes['options']) {
113+
this.editor = this.createEditor(changes['initialContent'].currentValue);
114+
} else if (changes['initialContent']) {
115+
this.updateEditorsInitialChanges(changes['initialContent'].currentValue);
116+
//TODO: remove after improving
117+
this.onEditorReady.emit(this.editor);
124118
}
125119
}
126120

127-
createEditor(
128-
initialContent:
129-
| Block<BSchema, ISchema, SSchema>[]
130-
| PartialBlock<BSchema, ISchema, SSchema>[]
131-
| undefined
132-
) {
121+
createEditor(initialContent: Block<BSchema, ISchema, SSchema>[] | undefined) {
133122
const schema = this.options?.schema;
134-
this.editor = BlockNoteEditor.create({
123+
const editor = BlockNoteEditor.create({
135124
schema: schema
136125
? schema
137126
: (BlockNoteSchema.create({
@@ -146,23 +135,24 @@ export class BnaEditorComponent<
146135
initialContent: initialContent,
147136
uploadFile: this.options?.uploadFile,
148137
});
149-
this.blockNoteAngularService.setEditor(this.editor);
150-
this.onEditorReady.emit(this.editor);
151-
this.slashMenuItems = this.getSlashMenuItems(this.editor);
152-
this.editor.onChange((data) => {
138+
this.blockNoteAngularService.setEditor(editor);
139+
this.onEditorReady.emit(editor);
140+
this.slashMenuItems = this.getSlashMenuItems(editor);
141+
editor.onChange((data) => {
153142
this.contentChanged.emit(data.document);
154143
});
155-
this.editor.onSelectionChange((change) => {
156-
const selection = this.editor.getSelection();
144+
editor.onSelectionChange((change) => {
145+
const selection = editor.getSelection();
157146
let selectedBlocks = [];
158147
// instead.
159148
if (selection !== undefined) {
160149
selectedBlocks = selection.blocks;
161150
} else {
162-
selectedBlocks = [this.editor.getTextCursorPosition().block];
151+
selectedBlocks = [editor.getTextCursorPosition().block];
163152
}
164153
this.selectedBlocks.emit(selectedBlocks);
165154
});
155+
return editor;
166156
}
167157

168158
getSlashMenuItems(
@@ -176,4 +166,17 @@ export class BnaEditorComponent<
176166

177167
return [...getDefaultSlashMenuItems(editor)];
178168
}
169+
170+
private updateEditorsInitialChanges(
171+
initialContent: Block<BSchema, ISchema, SSchema>[]
172+
) {
173+
this.editor.replaceBlocks(
174+
[...this.editor.document],
175+
initialContent ?? [
176+
{
177+
type: 'paragraph',
178+
},
179+
]
180+
);
181+
}
179182
}

0 commit comments

Comments
 (0)