3
3
Component ,
4
4
Input ,
5
5
OnChanges ,
6
- OnInit ,
7
6
output ,
8
7
SimpleChanges ,
9
8
} from '@angular/core' ;
@@ -87,51 +86,41 @@ export class BnaEditorComponent<
87
86
BSchema extends BlockSchema = DefaultBlockSchema ,
88
87
ISchema extends InlineContentSchema = DefaultInlineContentSchema ,
89
88
SSchema extends StyleSchema = DefaultStyleSchema
90
- > implements OnChanges , OnInit
89
+ > implements OnChanges
91
90
{
92
91
@Input ( )
93
92
options ?: BlockNoteEditorOptionsType < BSchema , ISchema , SSchema > ;
94
-
95
- // TODO: move to reusable type
96
93
@Input ( )
97
- initialContent :
94
+ initialContent ! :
98
95
| Block < BSchema , ISchema , SSchema > [ ]
99
96
| PartialBlock < BSchema , ISchema , SSchema > [ ]
100
- | undefined = undefined ;
97
+ | undefined ;
101
98
102
99
contentChanged = output < Block < BSchema , ISchema , SSchema > [ ] > ( ) ;
103
100
selectedBlocks = output < Block < BSchema , ISchema , SSchema > [ ] > ( ) ;
104
101
onEditorReady = output < BlockNoteEditor < BSchema , ISchema , SSchema > > ( ) ;
105
102
106
- editor ! : BlockNoteEditor < BSchema , ISchema , SSchema > ;
103
+ editor = this . createEditor ( undefined ) ;
107
104
slashMenuItems : Omit < DefaultSuggestionItem , 'key' > [ ] = [ ] ;
108
105
109
106
//TODO: remove relying on init flag
110
- isInitialized = false ;
107
+ isInitialized = true ;
111
108
112
109
constructor ( private blockNoteAngularService : BlockNoteAngularService ) { }
113
110
114
- ngOnInit ( ) {
115
- this . createEditor ( this . initialContent ) ;
116
- this . isInitialized = true ;
117
- }
118
-
119
111
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 ) ;
124
118
}
125
119
}
126
120
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 ) {
133
122
const schema = this . options ?. schema ;
134
- this . editor = BlockNoteEditor . create ( {
123
+ const editor = BlockNoteEditor . create ( {
135
124
schema : schema
136
125
? schema
137
126
: ( BlockNoteSchema . create ( {
@@ -146,23 +135,24 @@ export class BnaEditorComponent<
146
135
initialContent : initialContent ,
147
136
uploadFile : this . options ?. uploadFile ,
148
137
} ) ;
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 ) => {
153
142
this . contentChanged . emit ( data . document ) ;
154
143
} ) ;
155
- this . editor . onSelectionChange ( ( change ) => {
156
- const selection = this . editor . getSelection ( ) ;
144
+ editor . onSelectionChange ( ( change ) => {
145
+ const selection = editor . getSelection ( ) ;
157
146
let selectedBlocks = [ ] ;
158
147
// instead.
159
148
if ( selection !== undefined ) {
160
149
selectedBlocks = selection . blocks ;
161
150
} else {
162
- selectedBlocks = [ this . editor . getTextCursorPosition ( ) . block ] ;
151
+ selectedBlocks = [ editor . getTextCursorPosition ( ) . block ] ;
163
152
}
164
153
this . selectedBlocks . emit ( selectedBlocks ) ;
165
154
} ) ;
155
+ return editor ;
166
156
}
167
157
168
158
getSlashMenuItems (
@@ -176,4 +166,17 @@ export class BnaEditorComponent<
176
166
177
167
return [ ...getDefaultSlashMenuItems ( editor ) ] ;
178
168
}
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
+ }
179
182
}
0 commit comments