1- import type { UmbBlockTypeWithGroupKey , UmbInputBlockTypeElement } from '../../../block-type/index.js' ;
2- import '../../../block-type/components/input-block-type/index.js' ;
3- import {
4- type UmbPropertyEditorUiElement ,
5- UmbPropertyValueChangeEvent ,
6- type UmbPropertyEditorConfigCollection ,
1+ import type { UmbBlockTypeWithGroupKey , UmbInputBlockTypeElement } from '@umbraco-cms/backoffice/block-type' ;
2+ import type {
3+ UmbPropertyEditorUiElement ,
4+ UmbPropertyEditorConfigCollection ,
75} from '@umbraco-cms/backoffice/property-editor' ;
86import {
97 html ,
@@ -30,6 +28,8 @@ import {
3028} from '@umbraco-cms/backoffice/property' ;
3129import { UmbModalRouteRegistrationController } from '@umbraco-cms/backoffice/router' ;
3230import { UmbSorterController } from '@umbraco-cms/backoffice/sorter' ;
31+ import { UmbChangeEvent } from '@umbraco-cms/backoffice/event' ;
32+ import { umbConfirmModal } from '@umbraco-cms/backoffice/modal' ;
3333
3434interface MappedGroupWithBlockTypes extends UmbBlockGridTypeGroupType {
3535 blocks : Array < UmbBlockTypeWithGroupKey > ;
@@ -43,7 +43,6 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
4343 extends UmbLitElement
4444 implements UmbPropertyEditorUiElement
4545{
46- #moveData?: Array < UmbBlockTypeWithGroupKey > ;
4746 #sorter = new UmbSorterController < MappedGroupWithBlockTypes , HTMLElement > ( this , {
4847 getUniqueOfElement : ( element ) => element . getAttribute ( 'data-umb-group-key' ) ,
4948 getUniqueOfModel : ( modelEntry ) => modelEntry . key ! ,
@@ -104,8 +103,14 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
104103
105104 this . consumeContext ( UMB_PROPERTY_DATASET_CONTEXT , async ( context ) => {
106105 this . #datasetContext = context ;
107- //this.#observeBlocks();
108- this . #observeBlockGroups( ) ;
106+ this . observe (
107+ await this . #datasetContext. propertyValueByAlias ( 'blockGroups' ) ,
108+ ( value ) => {
109+ this . #blockGroups = ( value as Array < UmbBlockGridTypeGroupType > ) ?? [ ] ;
110+ this . #mapValuesToBlockGroups( ) ;
111+ } ,
112+ '_observeBlockGroups' ,
113+ ) ;
109114 } ) ;
110115
111116 this . #blockTypeWorkspaceModalRegistration = new UmbModalRouteRegistrationController (
@@ -119,24 +124,6 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
119124 } ) ;
120125 }
121126
122- async #observeBlockGroups( ) {
123- if ( ! this . #datasetContext) return ;
124- this . observe ( await this . #datasetContext. propertyValueByAlias ( 'blockGroups' ) , ( value ) => {
125- this . #blockGroups = ( value as Array < UmbBlockGridTypeGroupType > ) ?? [ ] ;
126- this . #mapValuesToBlockGroups( ) ;
127- } ) ;
128- }
129- // TODO: No need for this, we just got the value via the value property.. [NL]
130- /*
131- async #observeBlocks() {
132- if (!this.#datasetContext) return;
133- this.observe(await this.#datasetContext.propertyValueByAlias('blocks'), (value) => {
134- this.value = (value as Array<UmbBlockTypeWithGroupKey>) ?? [];
135- this.#mapValuesToBlockGroups();
136- });
137- }
138- */
139-
140127 #mapValuesToBlockGroups( ) {
141128 if ( ! this . #blockGroups) return ;
142129 // Map blocks that are not in any group, or in a group that does not exist
@@ -152,63 +139,60 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
152139 this . #sorter. setModel ( this . _groupsWithBlockTypes ) ;
153140 }
154141
155- #onDelete( e : CustomEvent , groupKey ?: string ) {
156- const updatedValues = ( e . target as UmbInputBlockTypeElement ) . value . map ( ( value ) => ( { ...value , groupKey } ) ) ;
157- const filteredValues = this . #value. filter ( ( value ) => value . groupKey !== groupKey ) ;
158- this . value = [ ...filteredValues , ...updatedValues ] ;
159- this . dispatchEvent ( new UmbPropertyValueChangeEvent ( ) ) ;
160- }
161-
162- async #onChange( e : CustomEvent ) {
142+ async #onChange( e : Event , groupKey ?: string ) {
163143 e . stopPropagation ( ) ;
164144 const element = e . target as UmbInputBlockTypeElement ;
165- const value = element . value ;
166-
167- if ( ! e . detail ?. moveComplete ) {
168- // Container change, store data of the new group...
169- const newGroupKey = element . getAttribute ( 'data-umb-group-key' ) ;
170- const movedItem = e . detail ?. item as UmbBlockTypeWithGroupKey ;
171- // Check if item moved back to original group...
172- if ( movedItem . groupKey === newGroupKey ) {
173- this . #moveData = undefined ;
174- } else {
175- this . #moveData = value . map ( ( block ) => ( { ...block , groupKey : newGroupKey } ) ) ;
176- }
177- } else if ( e . detail ?. moveComplete ) {
178- // Move complete, get the blocks that were in an untouched group
179- const blocks = this . #value
180- . filter ( ( block ) => ! value . find ( ( value ) => value . contentElementTypeKey === block . contentElementTypeKey ) )
181- . filter (
182- ( block ) => ! this . #moveData?. find ( ( value ) => value . contentElementTypeKey === block . contentElementTypeKey ) ,
183- ) ;
184-
185- this . value = this . #moveData ? [ ...blocks , ...value , ...this . #moveData] : [ ...blocks , ...value ] ;
186- this . dispatchEvent ( new UmbPropertyValueChangeEvent ( ) ) ;
187- this . #moveData = undefined ;
145+ const value = element . value . map ( ( x ) => ( { ...x , groupKey } ) ) ;
146+
147+ if ( groupKey ) {
148+ // Update the specific group:
149+ this . _groupsWithBlockTypes = this . _groupsWithBlockTypes . map ( ( group ) => {
150+ if ( group . key === groupKey ) {
151+ return { ...group , blocks : value } ;
152+ }
153+ return group ;
154+ } ) ;
155+ } else {
156+ // Update the not grouped blocks:
157+ this . _notGroupedBlockTypes = value ;
188158 }
159+
160+ this . #updateValue( ) ;
161+ }
162+
163+ #updateValue( ) {
164+ this . value = [ ...this . _notGroupedBlockTypes , ...this . _groupsWithBlockTypes . flatMap ( ( group ) => group . blocks ) ] ;
165+ this . dispatchEvent ( new UmbChangeEvent ( ) ) ;
166+ }
167+
168+ #updateBlockGroupsValue( groups : Array < UmbBlockGridTypeGroupType > ) {
169+ this . #datasetContext?. setPropertyValue ( 'blockGroups' , groups ) ;
189170 }
190171
191172 #onCreate( e : CustomEvent , groupKey ?: string ) {
192173 const selectedElementType = e . detail . contentElementTypeKey ;
193174 if ( selectedElementType ) {
194- this . #blockTypeWorkspaceModalRegistration?. open ( { } , 'create/' + selectedElementType + '/' + ( groupKey ?? null ) ) ;
175+ this . #blockTypeWorkspaceModalRegistration?. open ( { } , 'create/' + selectedElementType + '/' + ( groupKey ?? ' null' ) ) ;
195176 }
196177 }
197178
198179 // TODO: Implement confirm dialog [NL]
199- #deleteGroup( groupKey : string ) {
200- // TODO: make one method for updating the blockGroupsDataSetValue: [NL]
201- // This one that deletes might require the ability to parse what to send as an argument to the method, then a filtering can occur before.
202- this . #datasetContext ?. setPropertyValue (
203- 'blockGroups' ,
204- this . #blockGroups ?. filter ( ( group ) => group . key !== groupKey ) ,
205- ) ;
206-
180+ async #deleteGroup( groupKey : string ) {
181+ const groupName = this . #blockGroups ?. find ( ( group ) => group . key === groupKey ) ?. name ?? '' ;
182+ await umbConfirmModal ( this , {
183+ headline : '#blockEditor_confirmDeleteBlockGroupTitle' ,
184+ content : this . localize . term ( '#blockEditor_confirmDeleteBlockGroupMessage' , [ groupName ] ) ,
185+ color : 'danger' ,
186+ confirmLabel : '#general_delete' ,
187+ } ) ;
207188 // If a group is deleted, Move the blocks to no group:
208189 this . value = this . #value. map ( ( block ) => ( block . groupKey === groupKey ? { ...block , groupKey : undefined } : block ) ) ;
190+ if ( this . #blockGroups) {
191+ this . #updateBlockGroupsValue( this . #blockGroups. filter ( ( group ) => group . key !== groupKey ) ) ;
192+ }
209193 }
210194
211- #changeGroupName ( e : UUIInputEvent , groupKey : string ) {
195+ #onGroupNameChange ( e : UUIInputEvent , groupKey : string ) {
212196 const groupName = e . target . value as string ;
213197 // TODO: make one method for updating the blockGroupsDataSetValue: [NL]
214198 this . #datasetContext?. setPropertyValue (
@@ -224,9 +208,8 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
224208 .propertyAlias = ${ this . _alias }
225209 .value = ${ this . _notGroupedBlockTypes }
226210 .workspacePath = ${ this . _workspacePath }
227- @change = ${ this . #onChange}
228- @create = ${ ( e : CustomEvent ) => this . #onCreate( e , undefined ) }
229- @delete = ${ ( e : CustomEvent ) => this . #onDelete( e , undefined ) } > </ umb- input- block- type> `
211+ @change = ${ ( e : Event ) => this . #onChange( e , undefined ) }
212+ @create = ${ ( e : CustomEvent ) => this . #onCreate( e , undefined ) } > </ umb- input- block- type> `
230213 : '' }
231214 ${ repeat (
232215 this . _groupsWithBlockTypes ,
@@ -239,9 +222,8 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
239222 .propertyAlias = ${ this . _alias + '_' + group . key }
240223 .value = ${ group . blocks }
241224 .workspacePath = ${ this . _workspacePath }
242- @change = ${ this . #onChange}
243- @create = ${ ( e : CustomEvent ) => this . #onCreate( e , group . key ) }
244- @delete = ${ ( e : CustomEvent ) => this . #onDelete( e , group . key ) } > </ umb- input- block- type>
225+ @change = ${ ( e : Event ) => this . #onChange( e , group . key ) }
226+ @create = ${ ( e : CustomEvent ) => this . #onCreate( e , group . key ) } > </ umb- input- block- type>
245227 </ div> ` ,
246228 ) }
247229 </ div> ` ;
@@ -253,7 +235,7 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
253235 auto - width
254236 label= "Group"
255237 .value = ${ groupName ?? '' }
256- @change = ${ ( e : UUIInputEvent ) => this . #changeGroupName ( e , groupKey ) } >
238+ @change = ${ ( e : UUIInputEvent ) => this . #onGroupNameChange ( e , groupKey ) } >
257239 <uui- butto n compact slot= "append" label = "delete" @click = ${ ( ) => this . #deleteGroup( groupKey ) } >
258240 <uui- icon name= "icon-trash" > </ uui- icon>
259241 </ uui- butto n>
0 commit comments