@prismicio/types-internal 3.11.2 → 3.12.0-alpha.1

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.
Files changed (90) hide show
  1. package/CHANGELOG.md +764 -0
  2. package/lib/content/Document.d.ts +32 -7
  3. package/lib/content/Document.js +22 -8
  4. package/lib/content/LegacyContentCtx.d.ts +25 -3
  5. package/lib/content/LegacyContentCtx.js +67 -5
  6. package/lib/content/fields/GroupContent.d.ts +4 -2
  7. package/lib/content/fields/GroupContent.js +40 -17
  8. package/lib/content/fields/UIDContent.js +1 -0
  9. package/lib/content/fields/WidgetContent.d.ts +6 -0
  10. package/lib/content/fields/nestable/BooleanContent.js +1 -0
  11. package/lib/content/fields/nestable/EmbedContent.js +1 -0
  12. package/lib/content/fields/nestable/FieldContent/ColorContent.js +1 -0
  13. package/lib/content/fields/nestable/FieldContent/DateContent.js +1 -0
  14. package/lib/content/fields/nestable/FieldContent/NumberContent.js +1 -0
  15. package/lib/content/fields/nestable/FieldContent/RangeContent.js +1 -0
  16. package/lib/content/fields/nestable/FieldContent/SelectContent.js +1 -0
  17. package/lib/content/fields/nestable/FieldContent/TextContent.js +1 -0
  18. package/lib/content/fields/nestable/FieldContent/TimestampContent.js +1 -0
  19. package/lib/content/fields/nestable/GeoPointContent.js +1 -0
  20. package/lib/content/fields/nestable/ImageContent.js +1 -0
  21. package/lib/content/fields/nestable/IntegrationFieldContent.js +1 -0
  22. package/lib/content/fields/nestable/LinkContent.js +2 -0
  23. package/lib/content/fields/nestable/RepeatableContent.js +1 -0
  24. package/lib/content/fields/nestable/RichTextContent/index.js +1 -0
  25. package/lib/content/fields/nestable/SeparatorContent.js +1 -0
  26. package/lib/content/fields/nestable/TableContent.js +1 -0
  27. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +3 -0
  28. package/lib/content/fields/slices/Slice/CompositeSliceContent.js +19 -5
  29. package/lib/content/fields/slices/Slice/RepeatableContent.d.ts +5 -4
  30. package/lib/content/fields/slices/Slice/RepeatableContent.js +2 -1
  31. package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +3 -0
  32. package/lib/content/fields/slices/Slice/SharedSliceContent.js +26 -19
  33. package/lib/content/fields/slices/Slice/SimpleSliceContent.js +1 -0
  34. package/lib/content/fields/slices/Slice/index.d.ts +4 -0
  35. package/lib/content/fields/slices/SliceItem.d.ts +12 -0
  36. package/lib/content/fields/slices/SliceItem.js +11 -2
  37. package/lib/content/fields/slices/SlicesContent.d.ts +6 -0
  38. package/lib/content/fields/slices/SlicesContent.js +1 -0
  39. package/lib/customtypes/CustomType.js +6 -9
  40. package/lib/customtypes/Section.js +3 -8
  41. package/lib/customtypes/widgets/Group.js +20 -20
  42. package/lib/customtypes/widgets/nestable/NestableWidgetZ.d.ts +3 -0
  43. package/lib/customtypes/widgets/nestable/NestableWidgetZ.js +29 -0
  44. package/lib/customtypes/widgets/slices/CompositeSlice.js +9 -22
  45. package/lib/customtypes/widgets/slices/SharedSlice.js +18 -37
  46. package/lib/customtypes/widgets/slices/SharedSliceZ.d.ts +16 -0
  47. package/lib/customtypes/widgets/slices/SharedSliceZ.js +39 -0
  48. package/lib/customtypes/widgets/slices/SlicePrimaryWidgetZ.d.ts +3 -0
  49. package/lib/customtypes/widgets/slices/SlicePrimaryWidgetZ.js +6 -0
  50. package/lib/customtypes/widgets/slices/Slices.js +9 -16
  51. package/package.json +1 -1
  52. package/src/content/Document.ts +31 -8
  53. package/src/content/LegacyContentCtx.ts +72 -16
  54. package/src/content/fields/GroupContent.ts +58 -16
  55. package/src/content/fields/UIDContent.ts +1 -0
  56. package/src/content/fields/nestable/BooleanContent.ts +1 -0
  57. package/src/content/fields/nestable/EmbedContent.ts +1 -0
  58. package/src/content/fields/nestable/FieldContent/ColorContent.ts +1 -0
  59. package/src/content/fields/nestable/FieldContent/DateContent.ts +1 -0
  60. package/src/content/fields/nestable/FieldContent/NumberContent.ts +1 -0
  61. package/src/content/fields/nestable/FieldContent/RangeContent.ts +1 -0
  62. package/src/content/fields/nestable/FieldContent/SelectContent.ts +1 -0
  63. package/src/content/fields/nestable/FieldContent/TextContent.ts +1 -0
  64. package/src/content/fields/nestable/FieldContent/TimestampContent.ts +1 -0
  65. package/src/content/fields/nestable/GeoPointContent.ts +1 -0
  66. package/src/content/fields/nestable/ImageContent.ts +1 -0
  67. package/src/content/fields/nestable/IntegrationFieldContent.ts +1 -0
  68. package/src/content/fields/nestable/LinkContent.ts +2 -0
  69. package/src/content/fields/nestable/RepeatableContent.ts +4 -0
  70. package/src/content/fields/nestable/RichTextContent/index.ts +1 -0
  71. package/src/content/fields/nestable/SeparatorContent.ts +1 -0
  72. package/src/content/fields/nestable/TableContent.ts +1 -0
  73. package/src/content/fields/slices/Slice/CompositeSliceContent.ts +19 -5
  74. package/src/content/fields/slices/Slice/RepeatableContent.ts +7 -2
  75. package/src/content/fields/slices/Slice/SharedSliceContent.ts +26 -19
  76. package/src/content/fields/slices/Slice/SimpleSliceContent.ts +1 -0
  77. package/src/content/fields/slices/SliceItem.ts +12 -3
  78. package/src/content/fields/slices/SlicesContent.ts +4 -1
  79. package/src/customtypes/CustomType.ts +6 -9
  80. package/src/customtypes/Section.ts +3 -9
  81. package/src/customtypes/widgets/Group.ts +20 -21
  82. package/src/customtypes/widgets/slices/CompositeSlice.ts +9 -22
  83. package/src/customtypes/widgets/slices/SharedSlice.ts +23 -38
  84. package/src/customtypes/widgets/slices/Slices.ts +9 -15
  85. package/lib/content/fields/RepeatableContent.d.ts +0 -162
  86. package/lib/content/fields/RepeatableContent.js +0 -93
  87. package/lib/content/fields/nestable/RichTextContent/TextBlock.d.ts +0 -727
  88. package/lib/content/fields/nestable/RichTextContent/TextBlock.js +0 -80
  89. package/lib/customtypes/widgets/slices/SliceWidget.d.ts +0 -327
  90. package/lib/customtypes/widgets/slices/SliceWidget.js +0 -8
@@ -35,6 +35,7 @@ export const ColorLegacy = (ctx: LegacyContentCtx) =>
35
35
  return {
36
36
  content: b.value,
37
37
  types: { [ctx.keyOfType]: ColorFieldType },
38
+ keys: {},
38
39
  }
39
40
  },
40
41
  )
@@ -35,6 +35,7 @@ export const DateLegacy = (ctx: LegacyContentCtx) =>
35
35
  return {
36
36
  content: b.value,
37
37
  types: { [ctx.keyOfType]: DateFieldType },
38
+ keys: {},
38
39
  }
39
40
  },
40
41
  )
@@ -35,6 +35,7 @@ export const NumberLegacy = (ctx: LegacyContentCtx) =>
35
35
  return {
36
36
  content: b.value,
37
37
  types: { [ctx.keyOfType]: NumberFieldType },
38
+ keys: {},
38
39
  }
39
40
  },
40
41
  )
@@ -35,6 +35,7 @@ export const RangeLegacy = (ctx: LegacyContentCtx) =>
35
35
  return {
36
36
  content: b.value,
37
37
  types: { [ctx.keyOfType]: RangeFieldType },
38
+ keys: {},
38
39
  }
39
40
  },
40
41
  )
@@ -35,6 +35,7 @@ export const SelectLegacy = (ctx: LegacyContentCtx) =>
35
35
  return {
36
36
  content: b.value,
37
37
  types: { [ctx.keyOfType]: SelectFieldType },
38
+ keys: {},
38
39
  }
39
40
  },
40
41
  )
@@ -35,6 +35,7 @@ export const TextLegacy = (ctx: LegacyContentCtx) =>
35
35
  return {
36
36
  content: b.value,
37
37
  types: { [ctx.keyOfType]: TextFieldType },
38
+ keys: {},
38
39
  }
39
40
  },
40
41
  )
@@ -35,6 +35,7 @@ export const TimestampLegacy = (ctx: LegacyContentCtx) =>
35
35
  return {
36
36
  content: b.value,
37
37
  types: { [ctx.keyOfType]: TimestampFieldType },
38
+ keys: {},
38
39
  }
39
40
  },
40
41
  )
@@ -34,6 +34,7 @@ export const GeoPointLegacy = (ctx: LegacyContentCtx) =>
34
34
  (geo: GeoPointContent) => ({
35
35
  content: { position: geo.position },
36
36
  types: { [ctx.keyOfType]: "GeoPoint" },
37
+ keys: {},
37
38
  }),
38
39
  )
39
40
 
@@ -86,6 +86,7 @@ export const ImageLegacy = (ctx: LegacyContentCtx) =>
86
86
  ],
87
87
  ),
88
88
  types: { [ctx.keyOfType]: "Image" },
89
+ keys: {},
89
90
  }
90
91
  },
91
92
  )
@@ -38,6 +38,7 @@ export const IntegrationFieldLegacy = (ctx: LegacyContentCtx) =>
38
38
  return {
39
39
  content: i.value,
40
40
  types: { [ctx.keyOfType]: "IntegrationFields" },
41
+ keys: {},
41
42
  }
42
43
  },
43
44
  )
@@ -562,6 +562,7 @@ export const LinkContentLegacy = (ctx: LegacyContentCtx) =>
562
562
  key,
563
563
  }),
564
564
  types: { [ctx.keyOfType]: "Link" },
565
+ keys: {},
565
566
  }
566
567
  },
567
568
  )
@@ -627,6 +628,7 @@ export const FilledLinkContentLegacy = (ctx: LegacyContentCtx) => {
627
628
  key,
628
629
  }),
629
630
  types: { [ctx.keyOfType]: "Link" },
631
+ keys: {},
630
632
  }
631
633
  },
632
634
  )
@@ -88,6 +88,10 @@ export const RepeatableLegacy = (
88
88
  return {
89
89
  content: encodedItems.map((encodedItem) => encodedItem.content),
90
90
  types: { [ctx.keyOfType]: `Repeatable.${fieldType}` },
91
+ keys: encodedItems.reduce<Record<string, string>>(
92
+ (acc, item) => ({ ...acc, ...item.keys }),
93
+ {},
94
+ ),
91
95
  }
92
96
  },
93
97
  )
@@ -36,6 +36,7 @@ export const RichTextLegacy = (ctx: LegacyContentCtx) =>
36
36
  return {
37
37
  content: RichTextLegacyContent.encode(r.value),
38
38
  types: { [ctx.keyOfType]: "StructuredText" },
39
+ keys: {},
39
40
  }
40
41
  },
41
42
  )
@@ -22,6 +22,7 @@ export const SeparatorLegacy = (ctx: LegacyContentCtx) =>
22
22
  return {
23
23
  content: {},
24
24
  types: { [ctx.keyOfType]: "Separator" },
25
+ keys: {},
25
26
  }
26
27
  },
27
28
  )
@@ -108,6 +108,7 @@ export const TableLegacy = (ctx: LegacyContentCtx) =>
108
108
  })),
109
109
  }),
110
110
  types: { [ctx.keyOfType]: "Table" },
111
+ keys: {},
111
112
  }
112
113
  },
113
114
  )
@@ -60,7 +60,7 @@ export const CompositeSliceLegacy = (ctx: LegacyContentCtx) => {
60
60
  either.map((parsedSlice) => {
61
61
  const repeat =
62
62
  (() => {
63
- const itemsCtx = getFieldCtx("repeat", ctx)
63
+ const itemsCtx = getFieldCtx({ fieldKey: "repeat", ctx })
64
64
  const result = RepeatableWidgetsLegacy(itemsCtx).decode(
65
65
  parsedSlice.repeat,
66
66
  )
@@ -71,7 +71,11 @@ export const CompositeSliceLegacy = (ctx: LegacyContentCtx) => {
71
71
  const nonRepeat = Object.entries(
72
72
  parsedSlice["non-repeat"] || {},
73
73
  ).reduce<Record<string, NestableContent>>((acc, [key, value]) => {
74
- const fieldCtx = getFieldCtx(key, ctx, ["non-repeat"])
74
+ const fieldCtx = getFieldCtx({
75
+ fieldKey: key,
76
+ ctx,
77
+ prefixes: ["non-repeat"],
78
+ })
75
79
  if (!fieldCtx) return acc
76
80
 
77
81
  const result = NestableLegacy(fieldCtx).decode(value)
@@ -94,7 +98,11 @@ export const CompositeSliceLegacy = (ctx: LegacyContentCtx) => {
94
98
  WithTypes<{ [key: string]: unknown }>
95
99
  >(
96
100
  (acc, [key, value]) => {
97
- const FieldCtx = getFieldCtx(key, ctx, ["non-repeat"])
101
+ const FieldCtx = getFieldCtx({
102
+ fieldKey: key,
103
+ ctx,
104
+ prefixes: ["non-repeat"],
105
+ })
98
106
 
99
107
  const result = NestableLegacy(FieldCtx).encode(value)
100
108
  if (!result) return acc
@@ -102,14 +110,15 @@ export const CompositeSliceLegacy = (ctx: LegacyContentCtx) => {
102
110
  return {
103
111
  content: { ...acc.content, [key]: result.content },
104
112
  types: { ...acc.types, ...result.types },
113
+ keys: { ...acc.keys, ...result.keys },
105
114
  }
106
115
  },
107
- { content: {}, types: {} },
116
+ { content: {}, types: {}, keys: {} },
108
117
  )
109
118
 
110
119
  const repeat =
111
120
  (() => {
112
- const itemsCtx = getFieldCtx("repeat", ctx)
121
+ const itemsCtx = getFieldCtx({ fieldKey: "repeat", ctx })
113
122
  const result = RepeatableWidgetsLegacy(itemsCtx).encode(s.repeat)
114
123
  return result
115
124
  })() || []
@@ -124,6 +133,10 @@ export const CompositeSliceLegacy = (ctx: LegacyContentCtx) => {
124
133
  ...nonRepeat.types,
125
134
  ...repeat.reduce((acc, i) => ({ ...acc, ...i.types }), {}),
126
135
  },
136
+ keys: {
137
+ ...nonRepeat.keys,
138
+ ...repeat.reduce((acc, i) => ({ ...acc, ...i.keys }), {}),
139
+ },
127
140
  }
128
141
  },
129
142
  )
@@ -260,6 +273,7 @@ export function migrateCompositeSlice(
260
273
  items: content.widget.repeat.map((groupItem) => {
261
274
  return {
262
275
  __TYPE__: "GroupItemContent",
276
+ key: groupItem.key,
263
277
  value: groupItem.value.reduce<GroupItemContent["value"]>(
264
278
  (acc, [fieldKey, fieldContent]) => {
265
279
  if (!model.fields.items?.[fieldKey]) {
@@ -1,15 +1,19 @@
1
1
  import * as t from "io-ts"
2
2
 
3
- import type { LegacyContentCtx } from "../../../LegacyContentCtx"
3
+ import type { LegacyContentCtx, WithTypes } from "../../../LegacyContentCtx"
4
4
  import {
5
+ arrayWithIndexCodec,
5
6
  GroupContent,
7
+ GroupItemContent,
6
8
  GroupItemContentType,
7
9
  GroupItemLegacy,
8
10
  } from "../../GroupContent"
9
11
  import { NestableContent } from "../../nestable"
10
12
 
11
13
  export const RepeatableWidgetsLegacy = (ctx: LegacyContentCtx) => {
12
- return t.array(GroupItemLegacy(ctx))
14
+ return arrayWithIndexCodec<GroupItemContent, WithTypes<GroupItemLegacy>>(
15
+ (index) => GroupItemLegacy(ctx, index),
16
+ )
13
17
  }
14
18
 
15
19
  const RepeatableWidget = t.tuple([
@@ -18,6 +22,7 @@ const RepeatableWidget = t.tuple([
18
22
  ])
19
23
  const RepeatableWidgetsBlock = t.strict({
20
24
  __TYPE__: t.literal(GroupItemContentType),
25
+ key: t.string,
21
26
  value: t.array(RepeatableWidget),
22
27
  })
23
28
 
@@ -68,10 +68,11 @@ export const SharedSliceLegacy = (ctx: LegacyContentCtx) => {
68
68
  either.map((parsedSlice) => {
69
69
  const items =
70
70
  (() => {
71
- const itemsCtx = getFieldCtx("items", ctx, [
72
- "variations",
73
- parsedSlice.variation,
74
- ])
71
+ const itemsCtx = getFieldCtx({
72
+ fieldKey: "items",
73
+ ctx,
74
+ prefixes: ["variations", parsedSlice.variation],
75
+ })
75
76
  const result = RepeatableWidgetsLegacy(itemsCtx).decode(
76
77
  parsedSlice.items,
77
78
  )
@@ -82,11 +83,11 @@ export const SharedSliceLegacy = (ctx: LegacyContentCtx) => {
82
83
  const primary = Object.entries(parsedSlice.primary).reduce<
83
84
  Record<string, SlicePrimaryContent>
84
85
  >((acc, [key, value]) => {
85
- const FieldCtx = getFieldCtx(key, ctx, [
86
- "variations",
87
- parsedSlice.variation,
88
- "primary",
89
- ])
86
+ const FieldCtx = getFieldCtx({
87
+ fieldKey: key,
88
+ ctx,
89
+ prefixes: ["variations", parsedSlice.variation, "primary"],
90
+ })
90
91
 
91
92
  const result = SlicePrimaryLegacy(FieldCtx).decode(value)
92
93
  if (!result) return acc
@@ -109,11 +110,11 @@ export const SharedSliceLegacy = (ctx: LegacyContentCtx) => {
109
110
  WithTypes<Record<string, unknown>>
110
111
  >(
111
112
  (acc, [key, value]) => {
112
- const FieldCtx = getFieldCtx(key, ctx, [
113
- "variations",
114
- s.variation,
115
- "primary",
116
- ])
113
+ const FieldCtx = getFieldCtx({
114
+ fieldKey: key,
115
+ ctx,
116
+ prefixes: ["variations", s.variation, "primary"],
117
+ })
117
118
 
118
119
  const result = SlicePrimaryLegacy(FieldCtx).encode(value)
119
120
  if (!result) return acc
@@ -121,17 +122,19 @@ export const SharedSliceLegacy = (ctx: LegacyContentCtx) => {
121
122
  return {
122
123
  content: { ...acc.content, [key]: result.content },
123
124
  types: { ...acc.types, ...result.types },
125
+ keys: { ...acc.keys, ...result.keys },
124
126
  }
125
127
  },
126
- { content: {}, types: {} },
128
+ { content: {}, types: {}, keys: {} },
127
129
  )
128
130
 
129
131
  const items =
130
132
  (() => {
131
- const itemsCtx = getFieldCtx("items", ctx, [
132
- "variations",
133
- s.variation,
134
- ])
133
+ const itemsCtx = getFieldCtx({
134
+ fieldKey: "items",
135
+ ctx,
136
+ prefixes: ["variations", s.variation],
137
+ })
135
138
  const result = RepeatableWidgetsLegacy(itemsCtx).encode(s.items)
136
139
  return result
137
140
  })() || []
@@ -147,6 +150,10 @@ export const SharedSliceLegacy = (ctx: LegacyContentCtx) => {
147
150
  ...primary.types,
148
151
  ...items.reduce((acc, i) => ({ ...acc, ...i.types }), {}),
149
152
  },
153
+ keys: {
154
+ ...primary.keys,
155
+ ...items.reduce((acc, i) => ({ ...acc, ...i.keys }), {}),
156
+ },
150
157
  }
151
158
  },
152
159
  )
@@ -150,6 +150,7 @@ export function migrateSimpleSlice(
150
150
  items: content.widget.value.map((groupItem) => {
151
151
  return {
152
152
  __TYPE__: "GroupItemContent",
153
+ key: groupItem.key,
153
154
  value: groupItem.value.reduce<GroupItemContent["value"]>(
154
155
  (acc, [fieldKey, fieldContent]) => {
155
156
  return model.fields.items?.[fieldKey]
@@ -115,7 +115,7 @@ const itemLegacyReader = t.exact(
115
115
  ]),
116
116
  )
117
117
 
118
- type SliceItemLegacy = t.TypeOf<typeof itemLegacyReader>
118
+ export type SliceItemLegacy = t.TypeOf<typeof itemLegacyReader>
119
119
 
120
120
  export const SlicesItemLegacy = (ctx: LegacyContentCtx) => {
121
121
  return new t.Type<SliceItemContent, WithTypes<SliceItemLegacy>, unknown>(
@@ -134,7 +134,11 @@ export const SlicesItemLegacy = (ctx: LegacyContentCtx) => {
134
134
  0,
135
135
  stopIdx > 0 ? stopIdx : undefined,
136
136
  )
137
- const itemCtx = getFieldCtx(sliceName, ctx)
137
+ const itemCtx = getFieldCtx({
138
+ fieldKey: sliceName,
139
+ contentKey: parsedSlice.key,
140
+ ctx,
141
+ })
138
142
  const item = SliceLegacy(itemCtx).decode(parsedSlice.value)
139
143
  if (!item || isLeft(item)) return t.failure(sliceItem, context)
140
144
 
@@ -148,7 +152,11 @@ export const SlicesItemLegacy = (ctx: LegacyContentCtx) => {
148
152
  )
149
153
  },
150
154
  (sItem: SliceItemContent) => {
151
- const itemCtx = getFieldCtx(sItem.name, ctx)
155
+ const itemCtx = getFieldCtx({
156
+ fieldKey: sItem.name,
157
+ contentKey: sItem.key,
158
+ ctx,
159
+ })
152
160
  const result = SliceLegacy(itemCtx).encode(sItem.widget)
153
161
 
154
162
  return {
@@ -158,6 +166,7 @@ export const SlicesItemLegacy = (ctx: LegacyContentCtx) => {
158
166
  value: result?.content || {},
159
167
  },
160
168
  types: result?.types || {},
169
+ keys: result?.keys || {},
161
170
  }
162
171
  },
163
172
  )
@@ -46,7 +46,6 @@ type SlicesLegacy = Array<unknown>
46
46
 
47
47
  export const SlicesLegacy = (ctx: LegacyContentCtx) => {
48
48
  const codec = t.array(SlicesItemLegacy(ctx))
49
-
50
49
  return new t.Type<SlicesContent, WithTypes<SlicesLegacy>, unknown>(
51
50
  "SlicesLegacy",
52
51
  isSlicesContent,
@@ -70,6 +69,10 @@ export const SlicesLegacy = (ctx: LegacyContentCtx) => {
70
69
  (acc, s) => ({ ...acc, ...s.types }),
71
70
  { [ctx.keyOfType]: "Slices" },
72
71
  ),
72
+ keys: result.reduce<Record<string, string>>(
73
+ (acc, s) => ({ ...acc, ...s.keys }),
74
+ {},
75
+ ),
73
76
  }
74
77
  },
75
78
  )
@@ -261,20 +261,17 @@ export function traverseCustomType<
261
261
  }): T {
262
262
  const { customType, onField } = args
263
263
 
264
- let json: Record<string, typeof customType.json[string]> | undefined
264
+ const json: Record<string, typeof customType.json[string]> = {}
265
265
  for (const [key, section] of Object.entries(customType.json)) {
266
- const newSection = traverseSection({
266
+ json[key] = traverseSection({
267
267
  path: [key],
268
268
  section,
269
269
  onField,
270
270
  })
271
-
272
- if (newSection !== section) {
273
- if (!json) json = { ...customType.json }
274
- json[key] = newSection
275
- }
276
271
  }
277
272
 
278
- // returns the traversed model instead of a new one if it didn't change
279
- return json ? { ...customType, json } : customType
273
+ return {
274
+ ...customType,
275
+ json,
276
+ }
280
277
  }
@@ -52,7 +52,7 @@ export function traverseSection<
52
52
  }): T {
53
53
  const { path: prevPath, section: prevSection, onField } = args
54
54
 
55
- let section: T | undefined
55
+ const section = { ...prevSection }
56
56
  for (const [key, prevModel] of Object.entries(prevSection)) {
57
57
  const path = [...prevPath, key]
58
58
  let model
@@ -84,13 +84,7 @@ export function traverseSection<
84
84
  })
85
85
  break
86
86
  }
87
-
88
- if (model !== prevModel) {
89
- if (!section) section = { ...prevSection }
90
- section[key] = model
91
- }
87
+ section[key] = model
92
88
  }
93
-
94
- // returns the traversed model instead of a new one if it didn't change
95
- return section ?? prevSection
89
+ return section
96
90
  }
@@ -58,23 +58,23 @@ export function traverseNestedGroup(args: {
58
58
 
59
59
  if (!group.config?.fields) return group
60
60
 
61
- let fields: Record<string, NestableWidget> | undefined
62
- for (const [key, field] of Object.entries(group.config.fields)) {
61
+ const fields: Record<string, NestableWidget> = {}
62
+ for (const [key, prevField] of Object.entries(group.config.fields)) {
63
63
  const path = [...prevPath, key]
64
- const newField = onField({
64
+ fields[key] = onField({
65
65
  path,
66
66
  key,
67
- field,
67
+ field: prevField,
68
68
  })
69
-
70
- if (field !== newField) {
71
- if (!fields) fields = { ...group.config.fields }
72
- fields[key] = newField
73
- }
74
69
  }
75
70
 
76
- // returns the traversed model instead of a new one if it didn't change
77
- return fields ? { ...group, config: { ...group.config, fields } } : group
71
+ return {
72
+ ...group,
73
+ config: {
74
+ ...group.config,
75
+ fields,
76
+ },
77
+ }
78
78
  }
79
79
 
80
80
  export function traverseGroup(args: {
@@ -86,7 +86,7 @@ export function traverseGroup(args: {
86
86
 
87
87
  if (!group.config?.fields) return group
88
88
 
89
- let fields: Record<string, NestableWidget | NestedGroup> | undefined
89
+ const fields: Record<string, NestableWidget | NestedGroup> = {}
90
90
  for (const [key, prevField] of Object.entries(group.config.fields)) {
91
91
  const path = [...prevPath, key]
92
92
  let field
@@ -102,19 +102,18 @@ export function traverseGroup(args: {
102
102
  field = prevField
103
103
  break
104
104
  }
105
-
106
- const newField = onField({
105
+ fields[key] = onField({
107
106
  path,
108
107
  key,
109
108
  field,
110
109
  })
111
-
112
- if (field !== newField) {
113
- if (!fields) fields = { ...group.config.fields }
114
- fields[key] = newField
115
- }
116
110
  }
117
111
 
118
- // returns the traversed model instead of a new one if it didn't change
119
- return fields ? { ...group, config: { ...group.config, fields } } : group
112
+ return {
113
+ ...group,
114
+ config: {
115
+ ...group.config,
116
+ fields,
117
+ },
118
+ }
120
119
  }
@@ -46,34 +46,21 @@ export function traverseCompositeSlice(args: {
46
46
  }): CompositeSlice {
47
47
  const { path: prevPath, slice, onField } = args
48
48
 
49
- let nonRepeat: Record<string, NestableWidget> | undefined
49
+ const nonRepeat: Record<string, NestableWidget> = {}
50
50
  for (const [key, field] of Object.entries(slice["non-repeat"] ?? {})) {
51
51
  const path = [...prevPath, "non-repeat", key]
52
- const newField = onField({ path, key, field })
53
-
54
- if (field !== newField) {
55
- if (!nonRepeat) nonRepeat = { ...slice["non-repeat"] }
56
- nonRepeat[key] = newField
57
- }
52
+ nonRepeat[key] = onField({ path, key, field })
58
53
  }
59
54
 
60
- let repeat: Record<string, NestableWidget> | undefined
55
+ const repeat: Record<string, NestableWidget> = {}
61
56
  for (const [key, field] of Object.entries(slice.repeat ?? {})) {
62
57
  const path = [...prevPath, "repeat", key]
63
- const newField = onField({ path, key, field })
64
-
65
- if (field !== newField) {
66
- if (!repeat) repeat = { ...slice.repeat }
67
- repeat[key] = newField
68
- }
58
+ repeat[key] = onField({ path, key, field })
69
59
  }
70
60
 
71
- // returns the traversed model instead of a new one if it didn't change
72
- return repeat || nonRepeat
73
- ? {
74
- ...slice,
75
- ...(nonRepeat && { "non-repeat": nonRepeat }),
76
- ...(repeat && { repeat }),
77
- }
78
- : slice
61
+ return {
62
+ ...slice,
63
+ ...(slice["non-repeat"] && { "non-repeat": nonRepeat }),
64
+ ...(slice.repeat && { repeat }),
65
+ }
79
66
  }
@@ -79,7 +79,7 @@ export function traverseVariation(args: {
79
79
  }): Variation {
80
80
  const { path: prevPath, variation, onField } = args
81
81
 
82
- let primary: Record<string, NestableWidget | Group> | undefined
82
+ const primary: Record<string, NestableWidget | Group> = {}
83
83
  for (const [key, prevField] of Object.entries(variation.primary ?? {})) {
84
84
  const path = [...prevPath, "primary", key]
85
85
  let field
@@ -95,32 +95,20 @@ export function traverseVariation(args: {
95
95
  field = prevField
96
96
  break
97
97
  }
98
-
99
- const newField = onField({ path, key, field })
100
- if (field !== newField) {
101
- if (!primary) primary = { ...variation.primary }
102
- primary[key] = newField
103
- }
98
+ primary[key] = onField({ path, key, field })
104
99
  }
105
100
 
106
- let items: Record<string, NestableWidget> | undefined
107
- for (const [key, prevField] of Object.entries(variation.items ?? {})) {
101
+ const items: Record<string, NestableWidget> = {}
102
+ for (const [key, field] of Object.entries(variation.items ?? {})) {
108
103
  const path = [...prevPath, "items", key]
109
- const newField = (onField as OnFieldFn<NestableWidget>)({
110
- path,
111
- key,
112
- field: prevField,
113
- })
114
- if (prevField !== newField) {
115
- if (!items) items = { ...variation.items }
116
- items[key] = newField
117
- }
104
+ items[key] = (onField as OnFieldFn<NestableWidget>)({ path, key, field })
118
105
  }
119
106
 
120
- // returns the traversed model instead of a new one if it didn't change
121
- return primary || items
122
- ? { ...variation, ...(primary && { primary }), ...(items && { items }) }
123
- : variation
107
+ return {
108
+ ...variation,
109
+ ...(variation.primary && { primary }),
110
+ ...(variation.items && { items }),
111
+ }
124
112
  }
125
113
 
126
114
  export function traverseSharedSlice(args: {
@@ -130,23 +118,20 @@ export function traverseSharedSlice(args: {
130
118
  }): SharedSlice {
131
119
  const { path: prevPath, slice, onField } = args
132
120
 
133
- let variations: Variation[] | undefined
134
- for (let i = 0; i < slice.variations.length; i++) {
135
- const variation = slice.variations[i]
136
- if (!variation) continue
121
+ const variations: Variation[] = []
122
+ for (const variation of slice.variations) {
137
123
  const path = [...prevPath, variation.id]
138
- const newVariation = traverseVariation({
139
- path,
140
- variation,
141
- onField,
142
- })
143
-
144
- if (newVariation !== variation) {
145
- if (!variations) variations = [...slice.variations]
146
- variations[i] = newVariation
147
- }
124
+ variations.push(
125
+ traverseVariation({
126
+ path,
127
+ variation,
128
+ onField,
129
+ }),
130
+ )
148
131
  }
149
132
 
150
- // returns the traversed model instead of a new one if it didn't change
151
- return variations ? { ...slice, variations } : slice
133
+ return {
134
+ ...slice,
135
+ variations,
136
+ }
152
137
  }