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