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 ,
7
5
} from '@umbraco-cms/backoffice/property-editor' ;
8
6
import {
9
7
html ,
@@ -30,6 +28,8 @@ import {
30
28
} from '@umbraco-cms/backoffice/property' ;
31
29
import { UmbModalRouteRegistrationController } from '@umbraco-cms/backoffice/router' ;
32
30
import { UmbSorterController } from '@umbraco-cms/backoffice/sorter' ;
31
+ import { UmbChangeEvent } from '@umbraco-cms/backoffice/event' ;
32
+ import { umbConfirmModal } from '@umbraco-cms/backoffice/modal' ;
33
33
34
34
interface MappedGroupWithBlockTypes extends UmbBlockGridTypeGroupType {
35
35
blocks : Array < UmbBlockTypeWithGroupKey > ;
@@ -43,7 +43,6 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
43
43
extends UmbLitElement
44
44
implements UmbPropertyEditorUiElement
45
45
{
46
- #moveData?: Array < UmbBlockTypeWithGroupKey > ;
47
46
#sorter = new UmbSorterController < MappedGroupWithBlockTypes , HTMLElement > ( this , {
48
47
getUniqueOfElement : ( element ) => element . getAttribute ( 'data-umb-group-key' ) ,
49
48
getUniqueOfModel : ( modelEntry ) => modelEntry . key ! ,
@@ -104,8 +103,14 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
104
103
105
104
this . consumeContext ( UMB_PROPERTY_DATASET_CONTEXT , async ( context ) => {
106
105
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
+ ) ;
109
114
} ) ;
110
115
111
116
this . #blockTypeWorkspaceModalRegistration = new UmbModalRouteRegistrationController (
@@ -119,24 +124,6 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
119
124
} ) ;
120
125
}
121
126
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
-
140
127
#mapValuesToBlockGroups( ) {
141
128
if ( ! this . #blockGroups) return ;
142
129
// Map blocks that are not in any group, or in a group that does not exist
@@ -152,63 +139,60 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
152
139
this . #sorter. setModel ( this . _groupsWithBlockTypes ) ;
153
140
}
154
141
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 ) {
163
143
e . stopPropagation ( ) ;
164
144
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 ;
188
158
}
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 ) ;
189
170
}
190
171
191
172
#onCreate( e : CustomEvent , groupKey ?: string ) {
192
173
const selectedElementType = e . detail . contentElementTypeKey ;
193
174
if ( selectedElementType ) {
194
- this . #blockTypeWorkspaceModalRegistration?. open ( { } , 'create/' + selectedElementType + '/' + ( groupKey ?? null ) ) ;
175
+ this . #blockTypeWorkspaceModalRegistration?. open ( { } , 'create/' + selectedElementType + '/' + ( groupKey ?? ' null' ) ) ;
195
176
}
196
177
}
197
178
198
179
// 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
+ } ) ;
207
188
// If a group is deleted, Move the blocks to no group:
208
189
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
+ }
209
193
}
210
194
211
- #changeGroupName ( e : UUIInputEvent , groupKey : string ) {
195
+ #onGroupNameChange ( e : UUIInputEvent , groupKey : string ) {
212
196
const groupName = e . target . value as string ;
213
197
// TODO: make one method for updating the blockGroupsDataSetValue: [NL]
214
198
this . #datasetContext?. setPropertyValue (
@@ -224,9 +208,8 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
224
208
.propertyAlias = ${ this . _alias }
225
209
.value = ${ this . _notGroupedBlockTypes }
226
210
.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> `
230
213
: '' }
231
214
${ repeat (
232
215
this . _groupsWithBlockTypes ,
@@ -239,9 +222,8 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
239
222
.propertyAlias = ${ this . _alias + '_' + group . key }
240
223
.value = ${ group . blocks }
241
224
.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>
245
227
</ div> ` ,
246
228
) }
247
229
</ div> ` ;
@@ -253,7 +235,7 @@ export class UmbPropertyEditorUIBlockGridTypeConfigurationElement
253
235
auto - width
254
236
label= "Group"
255
237
.value = ${ groupName ?? '' }
256
- @change = ${ ( e : UUIInputEvent ) => this . #changeGroupName ( e , groupKey ) } >
238
+ @change = ${ ( e : UUIInputEvent ) => this . #onGroupNameChange ( e , groupKey ) } >
257
239
<uui- butto n compact slot= "append" label = "delete" @click = ${ ( ) => this . #deleteGroup( groupKey ) } >
258
240
<uui- icon name= "icon-trash" > </ uui- icon>
259
241
</ uui- butto n>
0 commit comments