@slicemachine/manager 0.24.14-alpha.jp-update-cr-links-unit.9 → 0.24.14-alpha.jp-update-cr-links-remove-recursion.5

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.
@@ -188,6 +188,143 @@ export class CustomTypesManager extends BaseManager {
188
188
  };
189
189
  }
190
190
 
191
+ private updateCRCustomType(
192
+ args: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,
193
+ ): CrCustomType {
194
+ const { previousPath, newPath } = args;
195
+
196
+ const customType = shallowClone(args.customType);
197
+
198
+ const previousId = previousPath[0];
199
+ const newId = newPath[0];
200
+
201
+ if (
202
+ !previousId ||
203
+ !newId ||
204
+ // we don't support custom type id renaming
205
+ typeof customType === "string"
206
+ ) {
207
+ return customType;
208
+ }
209
+
210
+ if (customType.fields) {
211
+ const newFields = customType.fields.map((fieldArg) => {
212
+ const field = shallowClone(fieldArg);
213
+
214
+ const previousId = previousPath[1];
215
+ const newId = newPath[1];
216
+
217
+ if (!previousId || !newId) {
218
+ return field;
219
+ }
220
+
221
+ if (typeof field === "string") {
222
+ if (field === previousId && field !== newId) {
223
+ // We have reached a field id that matches the id that was renamed,
224
+ // so we update it new one. The field is a string, so return the new
225
+ // id.
226
+ return newId;
227
+ }
228
+
229
+ return field;
230
+ }
231
+
232
+ if (field.id === previousId && field.id !== newId) {
233
+ // We have reached a field id that matches the id that was renamed,
234
+ // so we update it new one.
235
+ // Since field is not a string, we don't exit, as we might have
236
+ // something to update further down in customtypes.
237
+ field.id = newId;
238
+ }
239
+
240
+ return {
241
+ ...field,
242
+ customtypes: field.customtypes.map((customTypeArg) => {
243
+ const customType = shallowClone(customTypeArg);
244
+ const previousId = previousPath[0];
245
+ const newId = newPath[0];
246
+
247
+ if (
248
+ !previousId ||
249
+ !newId ||
250
+ // we don't support custom type id renaming
251
+ typeof customType === "string"
252
+ ) {
253
+ return customType;
254
+ }
255
+
256
+ if (customType.fields) {
257
+ return {
258
+ ...customType,
259
+ fields: customType.fields.map((fieldArg) => {
260
+ const field = shallowClone(fieldArg);
261
+ const previousId = previousPath[1];
262
+ const newId = newPath[1];
263
+
264
+ if (field === previousId && field !== newId) {
265
+ // Matches the previous id, so we update it and return because
266
+ // it's the last level.
267
+ return newId;
268
+ }
269
+
270
+ return field;
271
+ }),
272
+ };
273
+ }
274
+
275
+ return customType;
276
+ }),
277
+ };
278
+ });
279
+
280
+ return { ...customType, fields: newFields };
281
+ }
282
+
283
+ return customType;
284
+ }
285
+
286
+ /**
287
+ * Map over the custom types of a Content Relationship Link and update the API
288
+ * IDs that were changed during the custom type update.
289
+ */
290
+ private updateCRCustomTypes(
291
+ args: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,
292
+ ): CrCustomTypes {
293
+ const { customTypes, ...updateMeta } = args;
294
+
295
+ return customTypes.map((customType) => {
296
+ return this.updateCRCustomType({ customType, ...updateMeta });
297
+ });
298
+ }
299
+
300
+ /**
301
+ * Update the Content Relationship API IDs of a single field. The change is
302
+ * determined by the `previousPath` and `newPath` properties.
303
+ */
304
+ private updateFieldContentRelationships<
305
+ T extends UID | NestableWidget | Group | NestedGroup,
306
+ >(args: { field: T } & CustomTypeFieldIdChangedMeta): T {
307
+ const { field, ...updateMeta } = args;
308
+ if (
309
+ field.type !== "Link" ||
310
+ field.config?.select !== "document" ||
311
+ !field.config?.customtypes
312
+ ) {
313
+ // not a content relationship field
314
+ return field;
315
+ }
316
+
317
+ const newCustomTypes = this.updateCRCustomTypes({
318
+ ...updateMeta,
319
+ customTypes: field.config.customtypes,
320
+ });
321
+
322
+ return {
323
+ ...field,
324
+ config: { ...field.config, customtypes: newCustomTypes },
325
+ };
326
+ }
327
+
191
328
  /**
192
329
  * Update the Content Relationship API IDs for all existing custom types and
193
330
  * slices. The change is determined by properties inside the `updateMeta`
@@ -213,19 +350,24 @@ export class CustomTypesManager extends BaseManager {
213
350
  // any custom type and update them to use the new one.
214
351
  const customTypes = await this.readAllCustomTypes();
215
352
 
216
- updateCustomTypeContentRelationships({
217
- models: customTypes.models,
218
- onUpdate: (model) => {
219
- pushIfDefined(
220
- crUpdates,
221
- this.sliceMachinePluginRunner?.callHook("custom-type:update", {
222
- model,
223
- }),
224
- );
225
- },
226
- previousPath,
227
- newPath,
228
- });
353
+ for (const customType of customTypes.models) {
354
+ const updatedCustomTypeModel = traverseCustomType({
355
+ customType: customType.model,
356
+ onField: ({ field }) => {
357
+ return this.updateFieldContentRelationships({
358
+ field,
359
+ previousPath,
360
+ newPath,
361
+ });
362
+ },
363
+ });
364
+
365
+ crUpdates.push(
366
+ this.sliceMachinePluginRunner.callHook("custom-type:update", {
367
+ model: updatedCustomTypeModel,
368
+ }),
369
+ );
370
+ }
229
371
 
230
372
  // Find existing slice with content relationships that link to the renamed
231
373
  // field id in all libraries and update them to use the new one.
@@ -236,20 +378,26 @@ export class CustomTypesManager extends BaseManager {
236
378
  libraryID: library.libraryID,
237
379
  });
238
380
 
239
- updateSharedSliceContentRelationships({
240
- models: slices.models,
241
- onUpdate: (model) => {
242
- pushIfDefined(
243
- crUpdates,
244
- this.sliceMachinePluginRunner?.callHook("slice:update", {
245
- libraryID: library.libraryID,
246
- model,
247
- }),
248
- );
249
- },
250
- previousPath,
251
- newPath,
252
- });
381
+ for (const slice of slices.models) {
382
+ const updatedSliceModel = traverseSharedSlice({
383
+ path: ["."],
384
+ slice: slice.model,
385
+ onField: ({ field }) => {
386
+ return this.updateFieldContentRelationships({
387
+ field,
388
+ previousPath,
389
+ newPath,
390
+ });
391
+ },
392
+ });
393
+
394
+ crUpdates.push(
395
+ this.sliceMachinePluginRunner.callHook("slice:update", {
396
+ libraryID: library.libraryID,
397
+ model: updatedSliceModel,
398
+ }),
399
+ );
400
+ }
253
401
  }
254
402
 
255
403
  // Process all the Content Relationship updates at once.
@@ -495,192 +643,10 @@ const InferSliceResponse = z.object({
495
643
  langSmithUrl: z.string().url().optional(),
496
644
  });
497
645
 
498
- function updateCRCustomType(
499
- args: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,
500
- ): CrCustomType {
501
- const { previousPath, newPath } = args;
502
-
503
- const customType = shallowCloneIfObject(args.customType);
504
-
505
- const previousId = previousPath[0];
506
- const newId = newPath[0];
507
-
508
- if (!previousId || !newId || typeof customType === "string") {
509
- return customType; // we don't support custom type id renaming
510
- }
511
-
512
- if (customType.fields) {
513
- const newFields = customType.fields.map((fieldArg) => {
514
- const field = shallowCloneIfObject(fieldArg);
515
-
516
- const previousId = previousPath[1];
517
- const newId = newPath[1];
518
-
519
- if (!previousId || !newId) {
520
- return field;
521
- }
522
-
523
- if (typeof field === "string") {
524
- if (field === previousId && field !== newId) {
525
- // We have reached a field id that matches the id that was renamed,
526
- // so we update it new one. The field is a string, so return the new
527
- // id.
528
- return newId;
529
- }
530
-
531
- return field;
532
- }
533
-
534
- if (field.id === previousId && field.id !== newId) {
535
- // We have reached a field id that matches the id that was renamed,
536
- // so we update it new one.
537
- // Since field is not a string, we don't exit, as we might have
538
- // something to update further down in customtypes.
539
- field.id = newId;
540
- }
541
-
542
- return {
543
- ...field,
544
- customtypes: field.customtypes.map((customTypeArg) => {
545
- const customType = shallowCloneIfObject(customTypeArg);
546
- const previousId = previousPath[0];
547
- const newId = newPath[0];
548
-
549
- if (!previousId || !newId || typeof customType === "string") {
550
- return customType; // we don't support custom type id renaming
551
- }
552
-
553
- if (customType.fields) {
554
- return {
555
- ...customType,
556
- fields: customType.fields.map((fieldArg) => {
557
- const field = shallowCloneIfObject(fieldArg);
558
- const previousId = previousPath[1];
559
- const newId = newPath[1];
560
-
561
- if (field === previousId && field !== newId) {
562
- // Matches the previous id, so we update it and return because
563
- // it's the last level.
564
- return newId;
565
- }
566
-
567
- return field;
568
- }),
569
- };
570
- }
571
-
572
- return customType;
573
- }),
574
- };
575
- });
576
-
577
- return { ...customType, fields: newFields };
578
- }
579
-
580
- return customType;
581
- }
582
-
583
- /**
584
- * Map over the custom types of a Content Relationship Link and update the API
585
- * IDs that were changed during the custom type update.
586
- */
587
- function updateCRCustomTypes(
588
- args: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,
589
- ): CrCustomTypes {
590
- const { customTypes, ...updateMeta } = args;
591
-
592
- return customTypes.map((customType) => {
593
- return updateCRCustomType({ customType, ...updateMeta });
594
- });
595
- }
596
-
597
- /**
598
- * Update the Content Relationship API IDs of a single field. The change is
599
- * determined by the `previousPath` and `newPath` properties.
600
- */
601
- function updateFieldContentRelationships<
602
- T extends UID | NestableWidget | Group | NestedGroup,
603
- >(args: { field: T } & CustomTypeFieldIdChangedMeta): T {
604
- const { field, ...updateMeta } = args;
605
- if (
606
- field.type !== "Link" ||
607
- field.config?.select !== "document" ||
608
- !field.config?.customtypes
609
- ) {
610
- // not a content relationship field
611
- return field;
612
- }
613
-
614
- const newCustomTypes = updateCRCustomTypes({
615
- ...updateMeta,
616
- customTypes: field.config.customtypes,
617
- });
618
-
619
- return {
620
- ...field,
621
- config: { ...field.config, customtypes: newCustomTypes },
622
- };
623
- }
624
-
625
- export function updateCustomTypeContentRelationships(
626
- args: {
627
- models: { model: CustomType }[];
628
- onUpdate: (model: CustomType) => void;
629
- } & CustomTypeFieldIdChangedMeta,
630
- ): void {
631
- const { models, previousPath, newPath, onUpdate } = args;
632
-
633
- for (const customType of models) {
634
- const updatedCustomTypeModel = traverseCustomType({
635
- customType: customType.model,
636
- onField: ({ field }) => {
637
- return updateFieldContentRelationships({
638
- field,
639
- previousPath,
640
- newPath,
641
- });
642
- },
643
- });
644
-
645
- onUpdate(updatedCustomTypeModel);
646
- }
647
- }
648
-
649
- export function updateSharedSliceContentRelationships(
650
- args: {
651
- models: { model: SharedSlice }[];
652
- onUpdate: (model: SharedSlice) => void;
653
- } & CustomTypeFieldIdChangedMeta,
654
- ): void {
655
- const { models, previousPath, newPath, onUpdate } = args;
656
-
657
- for (const slice of models) {
658
- const updatedSliceModel = traverseSharedSlice({
659
- path: ["."],
660
- slice: slice.model,
661
- onField: ({ field }) => {
662
- return updateFieldContentRelationships({
663
- field,
664
- previousPath,
665
- newPath,
666
- });
667
- },
668
- });
669
-
670
- onUpdate(updatedSliceModel);
671
- }
672
- }
673
-
674
- function shallowCloneIfObject<T>(value: T): T {
646
+ function shallowClone<T>(value: T): T {
675
647
  if (typeof value === "object") {
676
648
  return { ...value };
677
649
  }
678
650
 
679
651
  return value;
680
652
  }
681
-
682
- function pushIfDefined<T>(array: T[], value: T | undefined) {
683
- if (value) {
684
- array.push(value);
685
- }
686
- }