@@ -130,6 +130,70 @@ export class UmbBlockWorkspaceContext<LayoutDataType extends UmbBlockLayoutBaseM
130130 'observeLayoutInitially' ,
131131 ) ;
132132
133+ this . #observeBlockData( unique ) ;
134+
135+ if ( this . #liveEditingMode) {
136+ this . #establishLiveSync( ) ;
137+ }
138+ }
139+
140+ async create ( contentElementTypeId : string ) {
141+ await this . #retrieveBlockEntries;
142+ await this . #retrieveModalContext;
143+ if ( ! this . #blockEntries) {
144+ throw new Error ( 'Block Entries not found' ) ;
145+ return ;
146+ }
147+ if ( ! this . #modalContext) {
148+ throw new Error ( 'Modal Context not found' ) ;
149+ return ;
150+ }
151+
152+ // TODO: Missing some way to append more layout data... this could be part of modal data, (or context api?)
153+
154+ this . setIsNew ( true ) ;
155+
156+ const blockCreated = await this . #blockEntries. create (
157+ contentElementTypeId ,
158+ { } ,
159+ this . #modalContext. data as UmbBlockWorkspaceData ,
160+ ) ;
161+ if ( ! blockCreated ) {
162+ throw new Error ( 'Block Entries could not create block' ) ;
163+ }
164+
165+ // TODO: We should investigate if it makes sense to gather
166+
167+ if ( ! this . #liveEditingMode) {
168+ this . #layout. setValue ( blockCreated . layout as LayoutDataType ) ;
169+ this . content . setData ( blockCreated . content ) ;
170+ if ( blockCreated . settings ) {
171+ this . settings . setData ( blockCreated . settings ) ;
172+ }
173+ } else {
174+ // Insert already, cause we are in live editing mode:
175+ const blockInserted = await this . #blockEntries. insert (
176+ blockCreated . layout ,
177+ blockCreated . content ,
178+ blockCreated . settings ,
179+ this . #modalContext. data as UmbBlockWorkspaceData ,
180+ ) ;
181+ if ( ! blockInserted ) {
182+ throw new Error ( 'Block Entries could not insert block' ) ;
183+ }
184+
185+ const unique = blockCreated . layout . contentUdi ;
186+
187+ this . #observeBlockData( unique ) ;
188+ this . #establishLiveSync( ) ;
189+ }
190+ }
191+
192+ #observeBlockData( unique : string ) {
193+ if ( ! this . #blockEntries) {
194+ throw new Error ( 'Block Entries not found' ) ;
195+ return ;
196+ }
133197 this . observe (
134198 this . #blockEntries. layoutOf ( unique ) ,
135199 ( layoutData ) => {
@@ -183,56 +247,6 @@ export class UmbBlockWorkspaceContext<LayoutDataType extends UmbBlockLayoutBaseM
183247 } ,
184248 'observeLayout' ,
185249 ) ;
186-
187- if ( this . #liveEditingMode) {
188- this . #establishLiveSync( ) ;
189- }
190- }
191-
192- async create ( contentElementTypeId : string ) {
193- await this . #retrieveBlockEntries;
194- await this . #retrieveModalContext;
195- if ( ! this . #blockEntries) {
196- throw new Error ( 'Block Entries not found' ) ;
197- return ;
198- }
199- if ( ! this . #modalContext) {
200- throw new Error ( 'Modal Context not found' ) ;
201- return ;
202- }
203-
204- // TODO: Missing some way to append more layout data... this could be part of modal data, (or context api?)
205-
206- this . setIsNew ( true ) ;
207-
208- const blockCreated = await this . #blockEntries. create (
209- contentElementTypeId ,
210- { } ,
211- this . #modalContext. data as UmbBlockWorkspaceData ,
212- ) ;
213- if ( ! blockCreated ) {
214- throw new Error ( 'Block Entries could not create block' ) ;
215- }
216-
217- this . #layout. setValue ( blockCreated . layout as LayoutDataType ) ;
218- this . content . setData ( blockCreated . content ) ;
219- if ( blockCreated . settings ) {
220- this . settings . setData ( blockCreated . settings ) ;
221- }
222-
223- if ( this . #liveEditingMode) {
224- // Insert already, cause we are in live editing mode:
225- const blockInserted = await this . #blockEntries. insert (
226- blockCreated . layout ,
227- blockCreated . content ,
228- blockCreated . settings ,
229- this . #modalContext. data as UmbBlockWorkspaceData ,
230- ) ;
231- if ( ! blockInserted ) {
232- throw new Error ( 'Block Entries could not insert block' ) ;
233- }
234- this . #establishLiveSync( ) ;
235- }
236250 }
237251
238252 #establishLiveSync( ) {
0 commit comments