slice-machine-ui 1.26.1-beta.12 → 1.26.1-beta.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/404.html +1 -1
- package/out/_next/static/chunks/127-ebfffb85bdb0bf53.js +1 -0
- package/out/_next/static/chunks/453-4e2d00a3515202f4.js +1 -0
- package/out/_next/static/chunks/46-bd45510ac2a839b5.js +10 -0
- package/out/_next/static/chunks/{91-1c3665ce38e58792.js → 91-e96855cdfbfbbade.js} +1 -1
- package/out/_next/static/chunks/96-24fed6adc069e313.js +1 -0
- package/out/_next/static/chunks/pages/{_app-01bcd29a595cec3a.js → _app-451e6ea8230ca7e5.js} +3 -3
- package/out/_next/static/chunks/pages/{changelog-ac1004bdd31d17f8.js → changelog-db23fdc674e68e53.js} +1 -1
- package/out/_next/static/chunks/pages/{changes-5c4bd80747884a8c.js → changes-92929e59b38adef8.js} +1 -1
- package/out/_next/static/chunks/pages/custom-types/{[customTypeId]-8bcc4d88f470d3f7.js → [customTypeId]-5b08515fec5e6668.js} +1 -1
- package/out/_next/static/chunks/pages/{labs-d6f2feb6720ee64c.js → labs-f9e359543631af2a.js} +1 -1
- package/out/_next/static/chunks/pages/page-types/{[pageTypeId]-8461ab4fcfaf0755.js → [pageTypeId]-41fc6d4f726fdfb9.js} +1 -1
- package/out/_next/static/chunks/pages/{settings-7ad1540dc0af794d.js → settings-a61e874536e40574.js} +1 -1
- package/out/_next/static/chunks/pages/slices/[lib]/[sliceName]/[variation]-a822e349e3e0b18f.js +1 -0
- package/out/_next/static/chunks/pages/{slices-801310de228fd05e.js → slices-98f88ac46b6ad58b.js} +1 -1
- package/out/_next/static/css/{df904982b9637c1a.css → 09a4e5669351d4e2.css} +1 -1
- package/out/_next/static/{svF8JPCb25QlsmymuzIiN → meJPGne56O3xxrr3mYozg}/_buildManifest.js +1 -1
- package/out/changelog.html +1 -1
- package/out/changes.html +1 -1
- package/out/custom-types/[customTypeId].html +1 -1
- package/out/custom-types.html +1 -1
- package/out/index.html +1 -1
- package/out/labs.html +1 -1
- package/out/page-types/[pageTypeId].html +1 -1
- package/out/settings.html +1 -1
- package/out/slices/[lib]/[sliceName]/[variation]/simulator.html +1 -1
- package/out/slices/[lib]/[sliceName]/[variation].html +1 -1
- package/out/slices.html +1 -1
- package/package.json +6 -6
- package/src/components/Divider/Divider.module.css +23 -0
- package/src/components/Divider/Divider.module.css.d.ts +7 -0
- package/src/components/Divider/Divider.stories.tsx +3 -4
- package/src/components/Divider/Divider.tsx +5 -8
- package/src/domain/__tests__/customType.test.ts +0 -184
- package/src/domain/__tests__/group.test.ts +161 -0
- package/src/domain/customType.ts +0 -191
- package/src/domain/group.ts +89 -0
- package/src/domain/slice.ts +24 -11
- package/src/legacy/lib/builders/CustomTypeBuilder/TabZone/index.tsx +4 -7
- package/src/legacy/lib/builders/SliceBuilder/FieldZones/index.tsx +27 -35
- package/src/legacy/lib/builders/common/Zone/Card/index.jsx +3 -0
- package/src/legacy/lib/builders/common/Zone/index.jsx +4 -7
- package/src/legacy/lib/models/common/Slice.ts +17 -3
- package/src/legacy/lib/models/common/widgets/Group/ListItem/index.jsx +51 -60
- package/src/legacy/lib/models/common/widgets/groupBuilderArray.ts +18 -0
- package/src/legacy/lib/models/common/widgets/sliceBuilderArray.ts +16 -28
- package/src/legacy/lib/models/common/widgets/withGroup.ts +1 -18
- package/src/utils/getContentTypeForTracking.ts +21 -0
- package/out/_next/static/chunks/207-f71df53ed0b0ac4d.js +0 -1
- package/out/_next/static/chunks/453-1a14e86de385fe7c.js +0 -1
- package/out/_next/static/chunks/46-d22d7a121254e31d.js +0 -8
- package/out/_next/static/chunks/96-33e38032f1e55366.js +0 -1
- package/out/_next/static/chunks/pages/slices/[lib]/[sliceName]/[variation]-217d0b0469d4320e.js +0 -1
- package/src/components/Divider/Divider.css.ts +0 -31
- /package/out/_next/static/{svF8JPCb25QlsmymuzIiN → meJPGne56O3xxrr3mYozg}/_ssgManifest.js +0 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
addFieldToGroup,
|
|
5
|
+
deleteFieldFromGroup,
|
|
6
|
+
reorderFieldInGroup,
|
|
7
|
+
updateFieldInGroup,
|
|
8
|
+
} from "../group";
|
|
9
|
+
|
|
10
|
+
describe("addFieldToGroup", () => {
|
|
11
|
+
it("adds a field to a group field", (ctx) => {
|
|
12
|
+
const field = ctx.createMock.model.boolean();
|
|
13
|
+
const group = ctx.createMock.model.group();
|
|
14
|
+
const res = addFieldToGroup({ group, field, fieldId: "field" });
|
|
15
|
+
|
|
16
|
+
expect(res.config?.fields?.field).toStrictEqual(field);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("does not mutate the provided group field", (ctx) => {
|
|
20
|
+
const field = ctx.createMock.model.boolean();
|
|
21
|
+
const group = ctx.createMock.model.group();
|
|
22
|
+
const res = addFieldToGroup({ group, field, fieldId: "field" });
|
|
23
|
+
|
|
24
|
+
expect(res).not.toBe(group);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("updateFieldInGroup", () => {
|
|
29
|
+
it("updates an existing field in a group", (ctx) => {
|
|
30
|
+
const field = ctx.createMock.model.boolean();
|
|
31
|
+
const group = ctx.createMock.model.group({ fields: { field } });
|
|
32
|
+
|
|
33
|
+
expect(group.config?.fields?.field).toStrictEqual(field);
|
|
34
|
+
|
|
35
|
+
const newField = structuredClone(field);
|
|
36
|
+
newField.config ??= {};
|
|
37
|
+
newField.config.placeholder_true = "Placeholder";
|
|
38
|
+
|
|
39
|
+
const res = updateFieldInGroup({
|
|
40
|
+
group,
|
|
41
|
+
field: newField,
|
|
42
|
+
previousFieldId: "field",
|
|
43
|
+
newFieldId: "field",
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(res.config?.fields?.field).toStrictEqual(newField);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("can rename an existing field in a group", (ctx) => {
|
|
50
|
+
const field = ctx.createMock.model.boolean();
|
|
51
|
+
const group = ctx.createMock.model.group({ fields: { field } });
|
|
52
|
+
|
|
53
|
+
expect(group.config?.fields).toStrictEqual({ field });
|
|
54
|
+
|
|
55
|
+
const res = updateFieldInGroup({
|
|
56
|
+
group,
|
|
57
|
+
field,
|
|
58
|
+
previousFieldId: "field",
|
|
59
|
+
newFieldId: "newField",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(res.config?.fields).toStrictEqual({ newField: field });
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("does not make any changes if the field does not exist", (ctx) => {
|
|
66
|
+
const field = ctx.createMock.model.boolean();
|
|
67
|
+
const group = ctx.createMock.model.group({ fields: { field } });
|
|
68
|
+
|
|
69
|
+
expect(group.config?.fields).toStrictEqual({ field });
|
|
70
|
+
|
|
71
|
+
const res = updateFieldInGroup({
|
|
72
|
+
group,
|
|
73
|
+
field: ctx.createMock.model.boolean(),
|
|
74
|
+
previousFieldId: "non-existent",
|
|
75
|
+
newFieldId: "non-existent",
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(res).toStrictEqual(group);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("does not mutate the provided group field", (ctx) => {
|
|
82
|
+
const field = ctx.createMock.model.boolean();
|
|
83
|
+
const group = ctx.createMock.model.group({ fields: { field } });
|
|
84
|
+
const res = updateFieldInGroup({
|
|
85
|
+
group,
|
|
86
|
+
field,
|
|
87
|
+
previousFieldId: "field",
|
|
88
|
+
newFieldId: "field",
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
expect(res).not.toBe(group);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe("reorderFieldInGroup", () => {
|
|
96
|
+
it("reorders a field in a group", (ctx) => {
|
|
97
|
+
const field1 = ctx.createMock.model.boolean();
|
|
98
|
+
const field2 = ctx.createMock.model.boolean();
|
|
99
|
+
const group = ctx.createMock.model.group({ fields: { field1, field2 } });
|
|
100
|
+
|
|
101
|
+
expect(Object.keys(group.config?.fields ?? {})).toStrictEqual([
|
|
102
|
+
"field1",
|
|
103
|
+
"field2",
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
const res = reorderFieldInGroup({
|
|
107
|
+
group,
|
|
108
|
+
sourceIndex: 1,
|
|
109
|
+
destinationIndex: 0,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(Object.keys(res.config?.fields ?? {})).toStrictEqual([
|
|
113
|
+
"field2",
|
|
114
|
+
"field1",
|
|
115
|
+
]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it("does not mutate the provided group field", (ctx) => {
|
|
119
|
+
const field1 = ctx.createMock.model.boolean();
|
|
120
|
+
const field2 = ctx.createMock.model.boolean();
|
|
121
|
+
const group = ctx.createMock.model.group({ fields: { field1, field2 } });
|
|
122
|
+
const res = reorderFieldInGroup({
|
|
123
|
+
group,
|
|
124
|
+
sourceIndex: 1,
|
|
125
|
+
destinationIndex: 0,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
expect(res).not.toBe(group);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe("deleteFieldFromGroup", () => {
|
|
133
|
+
it("deletes a field from a group", (ctx) => {
|
|
134
|
+
const field1 = ctx.createMock.model.boolean();
|
|
135
|
+
const field2 = ctx.createMock.model.boolean();
|
|
136
|
+
const group = ctx.createMock.model.group({ fields: { field1, field2 } });
|
|
137
|
+
|
|
138
|
+
const res = deleteFieldFromGroup({ group, fieldId: "field2" });
|
|
139
|
+
|
|
140
|
+
expect(res.config?.fields).toStrictEqual({ field1 });
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("does not make any changes if the field does not exist", (ctx) => {
|
|
144
|
+
const field = ctx.createMock.model.boolean();
|
|
145
|
+
const group = ctx.createMock.model.group({ fields: { field } });
|
|
146
|
+
|
|
147
|
+
const res = deleteFieldFromGroup({ group, fieldId: "non-existent" });
|
|
148
|
+
|
|
149
|
+
expect(res).toStrictEqual(group);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("does not mutate the provided group field", (ctx) => {
|
|
153
|
+
const field1 = ctx.createMock.model.boolean();
|
|
154
|
+
const field2 = ctx.createMock.model.boolean();
|
|
155
|
+
const group = ctx.createMock.model.group({ fields: { field1, field2 } });
|
|
156
|
+
|
|
157
|
+
const res = deleteFieldFromGroup({ group, fieldId: "field2" });
|
|
158
|
+
|
|
159
|
+
expect(res).not.toBe(group);
|
|
160
|
+
});
|
|
161
|
+
});
|
package/src/domain/customType.ts
CHANGED
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
DynamicSlices,
|
|
6
6
|
DynamicSlicesConfig,
|
|
7
7
|
Group,
|
|
8
|
-
GroupFieldType,
|
|
9
8
|
NestableWidget,
|
|
10
9
|
SlicesFieldType,
|
|
11
10
|
UID,
|
|
@@ -46,38 +45,6 @@ type ReorderFieldArgs = {
|
|
|
46
45
|
destinationIndex: number;
|
|
47
46
|
};
|
|
48
47
|
|
|
49
|
-
type AddGroupField = {
|
|
50
|
-
customType: CustomType;
|
|
51
|
-
sectionId: string;
|
|
52
|
-
groupFieldId: string;
|
|
53
|
-
newField: NestableWidget;
|
|
54
|
-
newFieldId: string;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
type DeleteGroupField = {
|
|
58
|
-
customType: CustomType;
|
|
59
|
-
sectionId: string;
|
|
60
|
-
groupFieldId: string;
|
|
61
|
-
fieldId: string;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
type UpdateGroupFieldArgs = {
|
|
65
|
-
customType: CustomType;
|
|
66
|
-
sectionId: string;
|
|
67
|
-
groupFieldId: string;
|
|
68
|
-
previousFieldId: string;
|
|
69
|
-
newFieldId: string;
|
|
70
|
-
newField: NestableWidget;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
type ReorderGroupFieldArgs = {
|
|
74
|
-
customType: CustomType;
|
|
75
|
-
sectionId: string;
|
|
76
|
-
groupFieldId: string;
|
|
77
|
-
sourceIndex: number;
|
|
78
|
-
destinationIndex: number;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
48
|
type UpdateSectionArgs = {
|
|
82
49
|
customType: CustomType;
|
|
83
50
|
sectionId: string;
|
|
@@ -97,12 +64,6 @@ type ReorderFieldsArgs<T> = {
|
|
|
97
64
|
destinationIndex: number;
|
|
98
65
|
};
|
|
99
66
|
|
|
100
|
-
type GetGroupFieldArgs = {
|
|
101
|
-
customType: CustomType;
|
|
102
|
-
sectionId: string;
|
|
103
|
-
groupFieldId: string;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
67
|
type CreateArgs = {
|
|
107
68
|
format: CustomTypeFormat;
|
|
108
69
|
id: string;
|
|
@@ -464,134 +425,6 @@ export function reorderField(args: ReorderFieldArgs): CustomType {
|
|
|
464
425
|
return newCustomType;
|
|
465
426
|
}
|
|
466
427
|
|
|
467
|
-
export function addGroupField(args: AddGroupField): CustomType {
|
|
468
|
-
const { customType, sectionId, groupFieldId, newField, newFieldId } = args;
|
|
469
|
-
const groupField = getGroupField({
|
|
470
|
-
customType,
|
|
471
|
-
sectionId,
|
|
472
|
-
groupFieldId,
|
|
473
|
-
});
|
|
474
|
-
const groupFields = groupField?.config?.fields;
|
|
475
|
-
|
|
476
|
-
if (!groupField) {
|
|
477
|
-
return customType;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
const newGroupField = updateGroupFields(groupField, {
|
|
481
|
-
...groupFields,
|
|
482
|
-
// Add the new field to the group fields
|
|
483
|
-
[newFieldId]: newField,
|
|
484
|
-
});
|
|
485
|
-
const newCustomType = updateSection({
|
|
486
|
-
customType,
|
|
487
|
-
sectionId,
|
|
488
|
-
updatedSection: {
|
|
489
|
-
...customType.json[sectionId],
|
|
490
|
-
[groupFieldId]: newGroupField,
|
|
491
|
-
},
|
|
492
|
-
});
|
|
493
|
-
|
|
494
|
-
return newCustomType;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
export function deleteGroupField(args: DeleteGroupField): CustomType {
|
|
498
|
-
const { customType, sectionId, groupFieldId, fieldId } = args;
|
|
499
|
-
const groupField = getGroupField({
|
|
500
|
-
customType,
|
|
501
|
-
sectionId,
|
|
502
|
-
groupFieldId,
|
|
503
|
-
});
|
|
504
|
-
const groupFields = groupField?.config?.fields;
|
|
505
|
-
|
|
506
|
-
if (!groupField || !groupFields) {
|
|
507
|
-
return customType;
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
const updatedFields = removeKey(groupFields, fieldId);
|
|
511
|
-
const newGroupField = updateGroupFields(groupField, updatedFields);
|
|
512
|
-
const newCustomType = updateSection({
|
|
513
|
-
customType,
|
|
514
|
-
sectionId,
|
|
515
|
-
updatedSection: {
|
|
516
|
-
...customType.json[sectionId],
|
|
517
|
-
[groupFieldId]: newGroupField,
|
|
518
|
-
},
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
return newCustomType;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
export function updateGroupField(args: UpdateGroupFieldArgs): CustomType {
|
|
525
|
-
const {
|
|
526
|
-
customType,
|
|
527
|
-
sectionId,
|
|
528
|
-
groupFieldId,
|
|
529
|
-
previousFieldId,
|
|
530
|
-
newFieldId,
|
|
531
|
-
newField,
|
|
532
|
-
} = args;
|
|
533
|
-
const groupField = getGroupField({
|
|
534
|
-
customType,
|
|
535
|
-
sectionId,
|
|
536
|
-
groupFieldId,
|
|
537
|
-
});
|
|
538
|
-
const groupFields = groupField?.config?.fields;
|
|
539
|
-
|
|
540
|
-
if (!groupField || !groupFields) {
|
|
541
|
-
return customType;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
const updatedGroupFields = updateFields({
|
|
545
|
-
fields: groupFields,
|
|
546
|
-
previousFieldId,
|
|
547
|
-
newFieldId,
|
|
548
|
-
newField,
|
|
549
|
-
});
|
|
550
|
-
const newGroupField = updateGroupFields(groupField, updatedGroupFields);
|
|
551
|
-
const newCustomType = updateSection({
|
|
552
|
-
customType,
|
|
553
|
-
sectionId,
|
|
554
|
-
updatedSection: {
|
|
555
|
-
...customType.json[sectionId],
|
|
556
|
-
[groupFieldId]: newGroupField,
|
|
557
|
-
},
|
|
558
|
-
});
|
|
559
|
-
|
|
560
|
-
return newCustomType;
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
export function reorderGroupField(args: ReorderGroupFieldArgs): CustomType {
|
|
564
|
-
const { customType, sectionId, groupFieldId, sourceIndex, destinationIndex } =
|
|
565
|
-
args;
|
|
566
|
-
const groupField = getGroupField({
|
|
567
|
-
customType,
|
|
568
|
-
sectionId,
|
|
569
|
-
groupFieldId,
|
|
570
|
-
});
|
|
571
|
-
const groupFields = groupField?.config?.fields;
|
|
572
|
-
|
|
573
|
-
if (!groupField || !groupFields) {
|
|
574
|
-
return customType;
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
const updatedGroupFields = reorderFields({
|
|
578
|
-
fields: groupFields,
|
|
579
|
-
sourceIndex,
|
|
580
|
-
destinationIndex,
|
|
581
|
-
});
|
|
582
|
-
const newGroupField = updateGroupFields(groupField, updatedGroupFields);
|
|
583
|
-
const newCustomType = updateSection({
|
|
584
|
-
customType,
|
|
585
|
-
sectionId,
|
|
586
|
-
updatedSection: {
|
|
587
|
-
...customType.json[sectionId],
|
|
588
|
-
[groupFieldId]: newGroupField,
|
|
589
|
-
},
|
|
590
|
-
});
|
|
591
|
-
|
|
592
|
-
return newCustomType;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
428
|
export function updateSection(args: UpdateSectionArgs): CustomType {
|
|
596
429
|
const { customType, sectionId, updatedSection } = args;
|
|
597
430
|
|
|
@@ -633,30 +466,6 @@ export function reorderFields<T>(args: ReorderFieldsArgs<T>) {
|
|
|
633
466
|
return reorderedFields;
|
|
634
467
|
}
|
|
635
468
|
|
|
636
|
-
export function getGroupField(args: GetGroupFieldArgs): Group | undefined {
|
|
637
|
-
const { customType, sectionId, groupFieldId } = args;
|
|
638
|
-
const field = customType.json[sectionId][groupFieldId];
|
|
639
|
-
|
|
640
|
-
if (field.type === GroupFieldType) {
|
|
641
|
-
return field;
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
return undefined;
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
export function updateGroupFields(
|
|
648
|
-
field: Group,
|
|
649
|
-
updatedGroupFields: Record<string, NestableWidget>,
|
|
650
|
-
): Group {
|
|
651
|
-
return {
|
|
652
|
-
...field,
|
|
653
|
-
config: {
|
|
654
|
-
...field.config,
|
|
655
|
-
fields: updatedGroupFields,
|
|
656
|
-
},
|
|
657
|
-
};
|
|
658
|
-
}
|
|
659
|
-
|
|
660
469
|
export function create(args: CreateArgs) {
|
|
661
470
|
const { id, label, repeatable, format } = args;
|
|
662
471
|
const mainTab = makeMainTab(repeatable, format);
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Group,
|
|
3
|
+
NestableWidget,
|
|
4
|
+
} from "@prismicio/types-internal/lib/customtypes";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Adds a field to a group field.
|
|
8
|
+
*
|
|
9
|
+
* @returns A copy of `args.group` with the appended field.
|
|
10
|
+
*/
|
|
11
|
+
export function addFieldToGroup(args: {
|
|
12
|
+
group: Group;
|
|
13
|
+
fieldId: string;
|
|
14
|
+
field: NestableWidget;
|
|
15
|
+
}): Group {
|
|
16
|
+
const newGroup = structuredClone(args.group);
|
|
17
|
+
newGroup.config ??= {};
|
|
18
|
+
newGroup.config.fields ??= {};
|
|
19
|
+
newGroup.config.fields[args.fieldId] = args.field;
|
|
20
|
+
|
|
21
|
+
return newGroup;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Updates a field in a group field.
|
|
26
|
+
*
|
|
27
|
+
* @returns A copy of `args.group` with the updated field.
|
|
28
|
+
*/
|
|
29
|
+
export function updateFieldInGroup(args: {
|
|
30
|
+
group: Group;
|
|
31
|
+
previousFieldId: string;
|
|
32
|
+
newFieldId: string;
|
|
33
|
+
field: NestableWidget;
|
|
34
|
+
}): Group {
|
|
35
|
+
const newGroup = structuredClone(args.group);
|
|
36
|
+
if (!newGroup.config?.fields || !args.group.config?.fields) {
|
|
37
|
+
return newGroup;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
newGroup.config.fields = {};
|
|
41
|
+
for (const fieldId in args.group.config.fields) {
|
|
42
|
+
if (fieldId === args.previousFieldId) {
|
|
43
|
+
newGroup.config.fields[args.newFieldId] = args.field;
|
|
44
|
+
} else {
|
|
45
|
+
newGroup.config.fields[fieldId] = args.group.config.fields[fieldId];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return newGroup;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Moves a field in a group field to a new position.
|
|
54
|
+
*
|
|
55
|
+
* @returns A copy of `args.group` with reordered fields.
|
|
56
|
+
*/
|
|
57
|
+
export function reorderFieldInGroup(args: {
|
|
58
|
+
group: Group;
|
|
59
|
+
sourceIndex: number;
|
|
60
|
+
destinationIndex: number;
|
|
61
|
+
}): Group {
|
|
62
|
+
const newGroup = structuredClone(args.group);
|
|
63
|
+
if (!newGroup.config?.fields) {
|
|
64
|
+
return newGroup;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const entries = Object.entries(newGroup.config.fields);
|
|
68
|
+
const [removedEntry] = entries.splice(args.sourceIndex, 1);
|
|
69
|
+
entries.splice(args.destinationIndex, 0, removedEntry);
|
|
70
|
+
|
|
71
|
+
newGroup.config.fields = Object.fromEntries(entries);
|
|
72
|
+
|
|
73
|
+
return newGroup;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Deletes a field from a group field.
|
|
78
|
+
*
|
|
79
|
+
* @returns A copy of `args.group` without the field.
|
|
80
|
+
*/
|
|
81
|
+
export function deleteFieldFromGroup(args: {
|
|
82
|
+
group: Group;
|
|
83
|
+
fieldId: string;
|
|
84
|
+
}): Group {
|
|
85
|
+
const newGroup = structuredClone(args.group);
|
|
86
|
+
delete newGroup.config?.fields?.[args.fieldId];
|
|
87
|
+
|
|
88
|
+
return newGroup;
|
|
89
|
+
}
|
package/src/domain/slice.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GroupFieldType } from "@prismicio/types-internal/lib/customtypes";
|
|
2
2
|
import type {
|
|
3
3
|
CompositeSlice,
|
|
4
4
|
LegacySlice,
|
|
5
5
|
SharedSlice,
|
|
6
|
+
SlicePrimaryWidget,
|
|
6
7
|
} from "@prismicio/types-internal/lib/customtypes/widgets/slices";
|
|
7
8
|
|
|
8
9
|
import type { ComponentUI } from "@/legacy/lib/models/common/ComponentUI";
|
|
9
|
-
import
|
|
10
|
-
|
|
10
|
+
import { Groups } from "@/legacy/lib/models/common/Group";
|
|
11
|
+
import {
|
|
12
|
+
type VariationSM,
|
|
11
13
|
WidgetsArea,
|
|
12
14
|
} from "@/legacy/lib/models/common/Slice";
|
|
13
15
|
import { pascalize, snakelize } from "@/legacy/lib/utils/str";
|
|
@@ -32,7 +34,7 @@ type UpdateFieldArgs = {
|
|
|
32
34
|
widgetArea: WidgetsArea;
|
|
33
35
|
previousFieldId: string;
|
|
34
36
|
newFieldId: string;
|
|
35
|
-
newField:
|
|
37
|
+
newField: SlicePrimaryWidget;
|
|
36
38
|
};
|
|
37
39
|
|
|
38
40
|
type AddFieldArgs = {
|
|
@@ -40,7 +42,7 @@ type AddFieldArgs = {
|
|
|
40
42
|
variationId: string;
|
|
41
43
|
widgetArea: WidgetsArea;
|
|
42
44
|
newFieldId: string;
|
|
43
|
-
newField:
|
|
45
|
+
newField: SlicePrimaryWidget;
|
|
44
46
|
};
|
|
45
47
|
|
|
46
48
|
type ReorderFieldArgs = {
|
|
@@ -210,18 +212,29 @@ export function addField(args: AddFieldArgs): ComponentUI {
|
|
|
210
212
|
model: {
|
|
211
213
|
...slice.model,
|
|
212
214
|
variations: slice.model.variations.map((v) => {
|
|
213
|
-
if (v.id
|
|
215
|
+
if (v.id !== variationId) {
|
|
216
|
+
return v;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (newField.type === GroupFieldType) {
|
|
220
|
+
if (widgetArea === WidgetsArea.Items) {
|
|
221
|
+
return v;
|
|
222
|
+
} else {
|
|
223
|
+
return {
|
|
224
|
+
...v,
|
|
225
|
+
[widgetArea]: v[widgetArea]?.concat([
|
|
226
|
+
{ key: newFieldId, value: Groups.toSM(newField) },
|
|
227
|
+
]),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
} else {
|
|
214
231
|
return {
|
|
215
232
|
...v,
|
|
216
233
|
[widgetArea]: v[widgetArea]?.concat([
|
|
217
|
-
{
|
|
218
|
-
key: newFieldId,
|
|
219
|
-
value: newField,
|
|
220
|
-
},
|
|
234
|
+
{ key: newFieldId, value: newField },
|
|
221
235
|
]),
|
|
222
236
|
};
|
|
223
237
|
}
|
|
224
|
-
return v;
|
|
225
238
|
}),
|
|
226
239
|
},
|
|
227
240
|
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Box, ProgressCircle } from "@prismicio/editor-ui";
|
|
2
2
|
import {
|
|
3
|
-
FieldType,
|
|
4
3
|
Group,
|
|
5
4
|
NestableWidget,
|
|
6
5
|
UID,
|
|
@@ -38,6 +37,7 @@ import {
|
|
|
38
37
|
ensureWidgetTypeExistence,
|
|
39
38
|
} from "@/legacy/lib/utils";
|
|
40
39
|
import { transformKeyAccessor } from "@/legacy/lib/utils/str";
|
|
40
|
+
import { getContentTypeForTracking } from "@/utils/getContentTypeForTracking";
|
|
41
41
|
|
|
42
42
|
import * as Widgets from "../../../../lib/models/common/widgets/withGroup";
|
|
43
43
|
import EditModal from "../../common/EditModal";
|
|
@@ -83,9 +83,8 @@ const TabZone: FC<TabZoneProps> = ({ tabId }) => {
|
|
|
83
83
|
}: {
|
|
84
84
|
id: string;
|
|
85
85
|
label: string;
|
|
86
|
-
widgetTypeName:
|
|
86
|
+
widgetTypeName: keyof typeof Widgets;
|
|
87
87
|
}) => {
|
|
88
|
-
// @ts-expect-error We have to create a widget map or a service instead of using export name
|
|
89
88
|
if (ensureWidgetTypeExistence(Widgets, widgetTypeName)) {
|
|
90
89
|
return;
|
|
91
90
|
}
|
|
@@ -126,9 +125,9 @@ const TabZone: FC<TabZoneProps> = ({ tabId }) => {
|
|
|
126
125
|
event: "field:added",
|
|
127
126
|
id,
|
|
128
127
|
name: label,
|
|
129
|
-
type:
|
|
128
|
+
type: newField.type,
|
|
130
129
|
isInAGroup: false,
|
|
131
|
-
contentType:
|
|
130
|
+
contentType: getContentTypeForTracking(window.location.pathname),
|
|
132
131
|
});
|
|
133
132
|
};
|
|
134
133
|
|
|
@@ -164,7 +163,6 @@ const TabZone: FC<TabZoneProps> = ({ tabId }) => {
|
|
|
164
163
|
newKey: string;
|
|
165
164
|
value: TabField;
|
|
166
165
|
}) => {
|
|
167
|
-
// @ts-expect-error We have to create a widget map or a service instead of using export name
|
|
168
166
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
|
169
167
|
if (ensureWidgetTypeExistence(Widgets, value.type)) {
|
|
170
168
|
return;
|
|
@@ -217,7 +215,6 @@ const TabZone: FC<TabZoneProps> = ({ tabId }) => {
|
|
|
217
215
|
{query.newPageType === undefined ? (
|
|
218
216
|
<Zone
|
|
219
217
|
zoneType="customType"
|
|
220
|
-
zoneTypeFormat={customType.format}
|
|
221
218
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
222
219
|
tabId={tabId}
|
|
223
220
|
title="Static Zone"
|