@prismicio/types-internal 2.2.0-traverse.alpha-2 → 2.2.0-traverse.alpha-4

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 (97) hide show
  1. package/lib/content/Document.d.ts +14 -5
  2. package/lib/content/Document.js +19 -74
  3. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +1 -0
  4. package/lib/content/fields/slices/Slice/CompositeSliceContent.js +30 -1
  5. package/lib/content/fields/slices/Slice/SimpleSliceContent.d.ts +1 -0
  6. package/lib/content/fields/slices/Slice/SimpleSliceContent.js +41 -1
  7. package/lib/customtypes/CustomType.d.ts +5 -2
  8. package/lib/customtypes/CustomType.js +1 -1
  9. package/lib/import/converters/fields/nestable/Image.d.ts +3 -1
  10. package/lib/import/converters/fields/nestable/Image.js +18 -2
  11. package/lib/import/converters/fields/nestable/Nestable.js +2 -0
  12. package/lib/import/converters/fields/nestable/RichText.d.ts +4 -0
  13. package/lib/import/converters/fields/nestable/RichText.js +55 -0
  14. package/lib/import/converters/fields/nestable/index.d.ts +1 -0
  15. package/lib/import/converters/fields/nestable/index.js +1 -0
  16. package/lib/import/validators/fields/ImportField.d.ts +6 -0
  17. package/lib/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.js +1 -1
  18. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.js +1 -1
  19. package/lib/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.js +1 -1
  20. package/lib/import/validators/fields/nestable/Embed.d.ts +3 -0
  21. package/lib/import/validators/fields/nestable/Embed.js +3 -8
  22. package/lib/import/validators/fields/nestable/GeoPoint.js +2 -2
  23. package/lib/import/validators/fields/nestable/Image/default.d.ts +22 -0
  24. package/lib/import/validators/fields/nestable/Image/default.js +19 -0
  25. package/lib/import/validators/fields/nestable/Image/index.d.ts +19 -0
  26. package/lib/import/validators/fields/nestable/Image/index.js +22 -22
  27. package/lib/import/validators/fields/nestable/Image/merge.d.ts +23 -0
  28. package/lib/import/validators/fields/nestable/Image/{Decoder.js → merge.js} +18 -14
  29. package/lib/import/validators/fields/nestable/Image/{Validator.d.ts → validators.d.ts} +4 -2
  30. package/lib/import/validators/fields/nestable/Image/{Validator.js → validators.js} +7 -3
  31. package/lib/import/validators/fields/nestable/ImportRichText/ImportBlock.d.ts +6 -0
  32. package/lib/import/validators/fields/nestable/ImportRichText/ImportBlock.js +25 -0
  33. package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.d.ts +24 -0
  34. package/lib/import/validators/fields/nestable/ImportRichText/ImportRichText.js +26 -0
  35. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.d.ts +6 -0
  36. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.js +18 -0
  37. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.d.ts +10 -0
  38. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.js +11 -0
  39. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.d.ts +8 -0
  40. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.js +11 -0
  41. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.d.ts +70 -0
  42. package/lib/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.js +43 -0
  43. package/lib/import/validators/fields/nestable/ImportRichText/blocks/Span.d.ts +65 -0
  44. package/lib/import/validators/fields/nestable/ImportRichText/blocks/Span.js +20 -0
  45. package/lib/import/validators/fields/nestable/ImportRichText/blocks/index.d.ts +4 -0
  46. package/lib/import/validators/fields/nestable/ImportRichText/blocks/index.js +7 -0
  47. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.d.ts +24 -0
  48. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.js +18 -0
  49. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.d.ts +5 -0
  50. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.js +10 -0
  51. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.d.ts +17 -0
  52. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.js +22 -0
  53. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/index.d.ts +2 -0
  54. package/lib/import/validators/fields/nestable/ImportRichText/blocks/spans/index.js +5 -0
  55. package/lib/import/validators/fields/nestable/ImportRichText/index.d.ts +2 -0
  56. package/lib/import/validators/fields/nestable/ImportRichText/index.js +7 -0
  57. package/lib/import/validators/fields/nestable/Link.d.ts +21 -6
  58. package/lib/import/validators/fields/nestable/Link.js +8 -8
  59. package/lib/import/validators/fields/nestable/Nestable.d.ts +8 -1
  60. package/lib/import/validators/fields/nestable/Nestable.js +5 -1
  61. package/lib/import/validators/fields/nestable/index.d.ts +2 -1
  62. package/lib/import/validators/fields/nestable/index.js +4 -1
  63. package/package.json +1 -1
  64. package/src/content/Document.ts +43 -92
  65. package/src/content/fields/slices/Slice/CompositeSliceContent.ts +37 -1
  66. package/src/content/fields/slices/Slice/SimpleSliceContent.ts +45 -0
  67. package/src/customtypes/CustomType.ts +6 -5
  68. package/src/import/converters/fields/nestable/Image.ts +34 -4
  69. package/src/import/converters/fields/nestable/Nestable.ts +3 -0
  70. package/src/import/converters/fields/nestable/RichText.ts +62 -0
  71. package/src/import/converters/fields/nestable/index.ts +1 -0
  72. package/src/import/validators/fields/ImportSlices/SharedSlice/SharedSlice.ts +1 -1
  73. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceType.ts +1 -1
  74. package/src/import/validators/fields/ImportSlices/SharedSlice/fields/SharedSliceVariation.ts +1 -1
  75. package/src/import/validators/fields/nestable/Embed.ts +4 -17
  76. package/src/import/validators/fields/nestable/GeoPoint.ts +2 -2
  77. package/src/import/validators/fields/nestable/Image/default.ts +25 -0
  78. package/src/import/validators/fields/nestable/Image/index.ts +34 -36
  79. package/src/import/validators/fields/nestable/Image/{Decoder.ts → merge.ts} +23 -20
  80. package/src/import/validators/fields/nestable/Image/{Validator.ts → validators.ts} +9 -2
  81. package/src/import/validators/fields/nestable/ImportRichText/ImportBlock.ts +39 -0
  82. package/src/import/validators/fields/nestable/ImportRichText/ImportRichText.ts +41 -0
  83. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportBlockType.ts +44 -0
  84. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportEmbedBlock.ts +13 -0
  85. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportImageBlock.ts +13 -0
  86. package/src/import/validators/fields/nestable/ImportRichText/blocks/ImportTextBlock.ts +56 -0
  87. package/src/import/validators/fields/nestable/ImportRichText/blocks/Span.ts +44 -0
  88. package/src/import/validators/fields/nestable/ImportRichText/blocks/index.ts +4 -0
  89. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/HyperlinkSpan.ts +24 -0
  90. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/SpanLocation.ts +8 -0
  91. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/TextSpan.ts +26 -0
  92. package/src/import/validators/fields/nestable/ImportRichText/blocks/spans/index.ts +2 -0
  93. package/src/import/validators/fields/nestable/ImportRichText/index.ts +2 -0
  94. package/src/import/validators/fields/nestable/Link.ts +18 -13
  95. package/src/import/validators/fields/nestable/Nestable.ts +6 -1
  96. package/src/import/validators/fields/nestable/index.ts +2 -1
  97. package/lib/import/validators/fields/nestable/Image/Decoder.d.ts +0 -26
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SpanLocation = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const validators_1 = require("../../../../../../../validators");
7
+ exports.SpanLocation = t.type({
8
+ start: validators_1.Number,
9
+ end: validators_1.Number,
10
+ });
@@ -0,0 +1,17 @@
1
+ import * as t from "io-ts";
2
+ export declare const TextSpanType: t.KeyofC<{
3
+ strong: null;
4
+ em: null;
5
+ label: null;
6
+ }>;
7
+ export declare const TextSpan: t.ExactC<t.IntersectionC<[t.TypeC<{
8
+ type: t.KeyofC<{
9
+ strong: null;
10
+ em: null;
11
+ label: null;
12
+ }>;
13
+ }>, t.TypeC<{
14
+ start: t.NumberC;
15
+ end: t.NumberC;
16
+ }>]>>;
17
+ export declare type TextSpan = t.TypeOf<typeof TextSpan>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TextSpan = exports.TextSpanType = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const t = (0, tslib_1.__importStar)(require("io-ts"));
6
+ const SpanLocation_1 = require("./SpanLocation");
7
+ const TextSpanTypes = {
8
+ Strong: "strong",
9
+ Em: "em",
10
+ Label: "label",
11
+ };
12
+ exports.TextSpanType = t.keyof({
13
+ [TextSpanTypes.Strong]: null,
14
+ [TextSpanTypes.Em]: null,
15
+ [TextSpanTypes.Label]: null,
16
+ });
17
+ exports.TextSpan = t.exact(t.intersection([
18
+ t.type({
19
+ type: exports.TextSpanType,
20
+ }),
21
+ SpanLocation_1.SpanLocation,
22
+ ]));
@@ -0,0 +1,2 @@
1
+ export * from "./HyperlinkSpan";
2
+ export * from "./TextSpan";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ (0, tslib_1.__exportStar)(require("./HyperlinkSpan"), exports);
5
+ (0, tslib_1.__exportStar)(require("./TextSpan"), exports);
@@ -0,0 +1,2 @@
1
+ export { ImportBlock } from "./ImportBlock";
2
+ export { ImportRichText } from "./ImportRichText";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImportRichText = exports.ImportBlock = void 0;
4
+ var ImportBlock_1 = require("./ImportBlock");
5
+ Object.defineProperty(exports, "ImportBlock", { enumerable: true, get: function () { return ImportBlock_1.ImportBlock; } });
6
+ var ImportRichText_1 = require("./ImportRichText");
7
+ Object.defineProperty(exports, "ImportRichText", { enumerable: true, get: function () { return ImportRichText_1.ImportRichText; } });
@@ -1,22 +1,37 @@
1
1
  import type { TypeOf } from "io-ts";
2
2
  import * as t from "io-ts";
3
- declare const WebLink: t.IntersectionC<[t.TypeC<{
3
+ declare const WebLink: t.ExactC<t.IntersectionC<[t.TypeC<{
4
4
  link_type: t.LiteralC<"Web">;
5
5
  url: t.StringC;
6
6
  }>, t.PartialC<{
7
7
  target: t.StringC;
8
- }>]>;
8
+ }>]>>;
9
9
  export declare type WebLink = t.TypeOf<typeof WebLink>;
10
- declare const DocumentLink: t.TypeC<{
10
+ declare const DocumentLink: t.ExactC<t.TypeC<{
11
11
  link_type: t.LiteralC<"Document">;
12
12
  id: t.Type<string, string, unknown>;
13
- }>;
13
+ }>>;
14
14
  export declare type DocumentLink = t.TypeOf<typeof DocumentLink>;
15
- declare const MediaLink: t.TypeC<{
15
+ declare const MediaLink: t.ExactC<t.TypeC<{
16
16
  link_type: t.LiteralC<"Media">;
17
17
  id: t.StringC;
18
- }>;
18
+ }>>;
19
19
  export declare type MediaLink = t.TypeOf<typeof MediaLink>;
20
+ export declare const Link: t.Type<({
21
+ link_type: "Web";
22
+ url: string;
23
+ } & {
24
+ target?: string;
25
+ }) | {
26
+ link_type: "Document";
27
+ id: string;
28
+ } | {
29
+ link_type: "Media";
30
+ id: string;
31
+ }, {
32
+ link_type: "Document" | "Web" | "Media";
33
+ }, unknown>;
34
+ export declare type Link = TypeOf<typeof Link>;
20
35
  export declare const ImportLink: t.Type<{
21
36
  type: "Link";
22
37
  value: ({
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ImportLink = void 0;
3
+ exports.ImportLink = exports.Link = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const t = (0, tslib_1.__importStar)(require("io-ts"));
6
6
  const DocumentId_1 = require("../../../../utils/DocumentId");
@@ -10,7 +10,7 @@ const ImportContent_1 = require("../ImportContent");
10
10
  const LinkTypeValidator = t.type({
11
11
  link_type: (0, function_1.withCustomError)(t.union([t.literal("Web"), t.literal("Document"), t.literal("Media")]), () => "The value must be `Web`, `Document` or `Media`"),
12
12
  });
13
- const WebLink = t.intersection([
13
+ const WebLink = t.exact(t.intersection([
14
14
  t.type({
15
15
  link_type: t.literal("Web"),
16
16
  url: validators_1.String,
@@ -18,18 +18,18 @@ const WebLink = t.intersection([
18
18
  t.partial({
19
19
  target: validators_1.String,
20
20
  }),
21
- ]);
22
- const DocumentLink = t.type({
21
+ ]));
22
+ const DocumentLink = t.strict({
23
23
  link_type: t.literal("Document"),
24
24
  id: DocumentId_1.DocumentId,
25
25
  });
26
- const MediaLink = t.type({
26
+ const MediaLink = t.strict({
27
27
  link_type: t.literal("Media"),
28
28
  id: validators_1.String,
29
29
  });
30
- const Link = LinkTypeValidator.pipe(t.union([WebLink, DocumentLink, MediaLink]));
30
+ exports.Link = LinkTypeValidator.pipe(t.union([WebLink, DocumentLink, MediaLink]));
31
31
  // This is the default value for the link
32
- const AnyLink = t.type({
32
+ const AnyLink = t.strict({
33
33
  link_type: t.literal("Any"),
34
34
  });
35
- exports.ImportLink = (0, ImportContent_1.ImportContent)("Link", (0, validators_1.DefaultOrElse)(AnyLink)(Link));
35
+ exports.ImportLink = (0, ImportContent_1.ImportContent)("Link", (0, validators_1.DefaultOrElse)(AnyLink)(exports.Link));
@@ -5,12 +5,13 @@ import { ImportDate } from "./Date";
5
5
  import { ImportEmbed } from "./Embed";
6
6
  import { ImportGeoPoint } from "./GeoPoint";
7
7
  import { ImportImage } from "./Image";
8
+ import { ImportRichText } from "./ImportRichText";
8
9
  import { ImportLink } from "./Link";
9
10
  import { ImportNumber } from "./Number";
10
11
  import { ImportSelect } from "./Select";
11
12
  import { ImportText } from "./Text";
12
13
  import { ImportTimestamp } from "./Timestamp";
13
- export declare type ImportNestable = ImportBoolean | ImportColor | ImportNumber | ImportSelect | ImportText | ImportDate | ImportTimestamp | ImportEmbed | ImportLink | ImportGeoPoint | ImportImage;
14
+ export declare type ImportNestable = ImportBoolean | ImportColor | ImportNumber | ImportSelect | ImportText | ImportDate | ImportTimestamp | ImportEmbed | ImportLink | ImportGeoPoint | ImportImage | ImportRichText;
14
15
  export declare const ImportNestable: {
15
16
  is(u: unknown): u is ImportNestable;
16
17
  decode: (field: NestableWidget) => (content: unknown) => {
@@ -59,6 +60,9 @@ export declare const ImportNestable: {
59
60
  }, {
60
61
  link_type: "Document" | "Web" | "Media";
61
62
  } | undefined, unknown> | import("io-ts").Type<{
63
+ type: "StructuredText";
64
+ value: import("./ImportRichText").ImportBlock[] | null;
65
+ }, unknown[] | undefined, unknown> | import("io-ts").Type<{
62
66
  type: "Number";
63
67
  value: number | null;
64
68
  }, number | undefined, unknown> | import("io-ts").Type<{
@@ -83,6 +87,9 @@ export declare const ImportNestable: {
83
87
  }> | import("fp-ts/lib/Either").Right<{
84
88
  type: "Number";
85
89
  value: number | null;
90
+ }> | import("fp-ts/lib/Either").Right<{
91
+ type: "StructuredText";
92
+ value: import("./ImportRichText").ImportBlock[] | null;
86
93
  }> | import("fp-ts/lib/Either").Right<{
87
94
  type: "Link";
88
95
  value: ({
@@ -7,6 +7,7 @@ const Date_1 = require("./Date");
7
7
  const Embed_1 = require("./Embed");
8
8
  const GeoPoint_1 = require("./GeoPoint");
9
9
  const Image_1 = require("./Image");
10
+ const ImportRichText_1 = require("./ImportRichText");
10
11
  const Link_1 = require("./Link");
11
12
  const Number_1 = require("./Number");
12
13
  const Select_1 = require("./Select");
@@ -24,7 +25,8 @@ exports.ImportNestable = {
24
25
  Embed_1.ImportEmbed.is(u) ||
25
26
  Link_1.ImportLink.is(u) ||
26
27
  GeoPoint_1.ImportGeoPoint.is(u) ||
27
- (0, Image_1.ImportImage)().is(u));
28
+ (0, Image_1.ImportImage)().is(u) ||
29
+ (0, ImportRichText_1.ImportRichText)().is(u));
28
30
  },
29
31
  decode: (field) => {
30
32
  return (content) => {
@@ -52,6 +54,8 @@ exports.ImportNestable = {
52
54
  return (0, Image_1.ImportImage)(field);
53
55
  case "GeoPoint":
54
56
  return GeoPoint_1.ImportGeoPoint;
57
+ case "StructuredText":
58
+ return (0, ImportRichText_1.ImportRichText)(field);
55
59
  default:
56
60
  throw new Error(`Unsupported type of nestable field ${field.type}`);
57
61
  }
@@ -1,10 +1,11 @@
1
1
  export * from "./Boolean";
2
2
  export * from "./Color";
3
3
  export * from "./Date";
4
- export * from "./Embed";
4
+ export { ImportEmbed } from "./Embed";
5
5
  export * from "./GeoPoint";
6
6
  export * from "./Image";
7
7
  export * from "./Image/index";
8
+ export * from "./ImportRichText";
8
9
  export * from "./Link";
9
10
  export * from "./Nestable";
10
11
  export * from "./Number";
@@ -1,13 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImportEmbed = void 0;
3
4
  const tslib_1 = require("tslib");
4
5
  (0, tslib_1.__exportStar)(require("./Boolean"), exports);
5
6
  (0, tslib_1.__exportStar)(require("./Color"), exports);
6
7
  (0, tslib_1.__exportStar)(require("./Date"), exports);
7
- (0, tslib_1.__exportStar)(require("./Embed"), exports);
8
+ var Embed_1 = require("./Embed");
9
+ Object.defineProperty(exports, "ImportEmbed", { enumerable: true, get: function () { return Embed_1.ImportEmbed; } });
8
10
  (0, tslib_1.__exportStar)(require("./GeoPoint"), exports);
9
11
  (0, tslib_1.__exportStar)(require("./Image"), exports);
10
12
  (0, tslib_1.__exportStar)(require("./Image/index"), exports);
13
+ (0, tslib_1.__exportStar)(require("./ImportRichText"), exports);
11
14
  (0, tslib_1.__exportStar)(require("./Link"), exports);
12
15
  (0, tslib_1.__exportStar)(require("./Nestable"), exports);
13
16
  (0, tslib_1.__exportStar)(require("./Number"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-traverse.alpha-2",
3
+ "version": "2.2.0-traverse.alpha-4",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -14,15 +14,12 @@ import {
14
14
  collectSharedSlices,
15
15
  flattenStaticWidgets,
16
16
  StaticCustomType,
17
- VariationFields,
18
17
  } from "../customtypes"
19
18
  import {
20
- CompositeSliceItemContent,
21
- GroupItemContent,
22
19
  isCompositeSliceItemContent,
23
20
  isSimpleSliceItemContent,
24
- SharedSliceItemContent,
25
- SimpleSliceItemContent,
21
+ migrateCompositeSlice,
22
+ migrateSimpleSlice,
26
23
  traverseGroupContent,
27
24
  traverseSlices,
28
25
  WidgetContent,
@@ -132,7 +129,10 @@ function extractMetadata(data: { [p: string]: unknown }): {
132
129
 
133
130
  function parseLegacyDocument(
134
131
  legacyDoc: unknown,
135
- customType: StaticCustomType,
132
+ customType: {
133
+ customTypeId: string
134
+ fields: Record<string, StaticWidget>
135
+ },
136
136
  ): Document | undefined {
137
137
  const result = pipe(
138
138
  // ensure it's the right document format first
@@ -169,11 +169,21 @@ export const DocumentLegacy = {
169
169
  */
170
170
  export function traverseDocument({
171
171
  document,
172
- model,
172
+ customType,
173
173
  }: {
174
174
  document: Document
175
- model?: StaticCustomType
175
+ customType?:
176
+ | StaticCustomType
177
+ | {
178
+ customTypeId: string
179
+ fields: Record<string, StaticWidget>
180
+ }
181
+ | undefined
176
182
  }) {
183
+ const model =
184
+ customType && StaticCustomType.is(customType)
185
+ ? simplifyCustomType(customType)
186
+ : customType
177
187
  return ({
178
188
  transformWidget = ({ content }) => content,
179
189
  transformSlice = ({ content }) => content,
@@ -183,7 +193,7 @@ export function traverseDocument({
183
193
  }): Document => {
184
194
  const fieldModels =
185
195
  model &&
186
- flattenStaticWidgets(model).reduce<Record<string, StaticWidget>>(
196
+ Object.entries(model.fields).reduce<Record<string, StaticWidget>>(
187
197
  (acc, [key, def]) => ({ ...acc, [key]: def }),
188
198
  {},
189
199
  )
@@ -192,7 +202,7 @@ export function traverseDocument({
192
202
  const fieldModel = fieldModels && fieldModels[key]
193
203
 
194
204
  const path = ContentPath.make([
195
- { key: model?.id, type: "CustomType" },
205
+ { key: model?.customTypeId, type: "CustomType" },
196
206
  { key, type: "Widget" },
197
207
  ])
198
208
 
@@ -240,6 +250,16 @@ export function traverseDocument({
240
250
  }
241
251
  }
242
252
 
253
+ function simplifyCustomType(customType: StaticCustomType): {
254
+ customTypeId: string
255
+ fields: Record<string, StaticWidget>
256
+ } {
257
+ return {
258
+ customTypeId: customType?.id,
259
+ fields: Object.fromEntries(flattenStaticWidgets(customType)),
260
+ }
261
+ }
262
+
243
263
  // /**
244
264
  // * The goal is to be able to collect all widgets or slices of a given type at any level of nesting inside a prismic content
245
265
  // *
@@ -263,87 +283,18 @@ export function collectWidgets<W extends WidgetContent>(
263
283
  return collected
264
284
  }
265
285
 
266
- function migrateCompositeSlice(
267
- model: VariationFields,
268
- content: CompositeSliceItemContent,
269
- ): SharedSliceItemContent {
270
- return {
271
- key: content.key,
272
- name: model.sliceName,
273
- maybeLabel: content.maybeLabel,
274
- widget: {
275
- __TYPE__: "SharedSliceContent",
276
- variation: model.variationId,
277
- primary: Object.entries(content.widget.nonRepeat).reduce(
278
- (acc, [fieldKey, fieldContent]) => {
279
- return model.fields.primary?.[fieldKey]
280
- ? { ...acc, [fieldKey]: fieldContent }
281
- : acc
282
- },
283
- {},
284
- ),
285
- items: content.widget.repeat.map((groupItem) => {
286
- return {
287
- __TYPE__: "GroupItemContent",
288
- value: groupItem.value.reduce<GroupItemContent["value"]>(
289
- (acc, [fieldKey, fieldContent]) => {
290
- return model.fields.items?.[fieldKey]
291
- ? acc.concat([[fieldKey, fieldContent]])
292
- : acc
293
- },
294
- [],
295
- ),
296
- }
297
- }, []),
298
- },
299
- }
300
- }
301
-
302
- function migrateSimpleSlice(
303
- model: VariationFields,
304
- content: SimpleSliceItemContent,
305
- ): SharedSliceItemContent {
306
- if (content.widget.__TYPE__ === "GroupContentType") {
307
- return {
308
- key: content.key,
309
- name: model.sliceName,
310
- maybeLabel: content.maybeLabel,
311
- widget: {
312
- __TYPE__: "SharedSliceContent",
313
- variation: model.variationId,
314
- primary: {},
315
- items: content.widget.value.map((groupItem) => {
316
- return {
317
- __TYPE__: "GroupItemContent",
318
- value: groupItem.value.reduce<GroupItemContent["value"]>(
319
- (acc, [fieldKey, fieldContent]) => {
320
- return model.fields.items?.[fieldKey]
321
- ? acc.concat([[fieldKey, fieldContent]])
322
- : acc
323
- },
324
- [],
325
- ),
326
- }
327
- }, []),
328
- },
329
- }
330
- }
331
- return {
332
- key: content.key,
333
- name: model.sliceName,
334
- maybeLabel: content.maybeLabel,
335
- widget: {
336
- __TYPE__: "SharedSliceContent",
337
- variation: model.variationId,
338
- primary: model.fields.primary?.[content.name]
339
- ? { [content.key]: content.widget }
340
- : {},
341
- items: [],
342
- },
343
- }
344
- }
345
-
346
- export function migrateDocument(document: Document, model: StaticCustomType) {
286
+ export function migrateDocument(
287
+ document: Document,
288
+ customType:
289
+ | StaticCustomType
290
+ | {
291
+ customTypeId: string
292
+ fields: Record<string, StaticWidget>
293
+ },
294
+ ) {
295
+ const model = StaticCustomType.is(customType)
296
+ ? simplifyCustomType(customType)
297
+ : customType
347
298
  const needsMigration = Object.values(collectSharedSlices(model)).some(
348
299
  (slice) => Boolean(slice.legacyPaths),
349
300
  )
@@ -352,7 +303,7 @@ export function migrateDocument(document: Document, model: StaticCustomType) {
352
303
 
353
304
  return traverseDocument({
354
305
  document,
355
- model,
306
+ customType,
356
307
  })({
357
308
  transformSlice: ({ content, model }) => {
358
309
  if (isCompositeSliceItemContent(content) && model?.type === "SharedSlice")
@@ -18,7 +18,7 @@ import {
18
18
  WithTypes,
19
19
  } from "../../../LegacyContentCtx"
20
20
  import { hasContentType } from "../../../utils"
21
- import { traverseGroupItemsContent } from "../../GroupContent"
21
+ import { GroupItemContent, traverseGroupItemsContent } from "../../GroupContent"
22
22
  import {
23
23
  isNestableContent,
24
24
  NestableContent,
@@ -205,3 +205,39 @@ export function traverseCompositeSliceContent({
205
205
  })
206
206
  }
207
207
  }
208
+
209
+ export function migrateCompositeSlice(
210
+ model: VariationFields,
211
+ content: CompositeSliceItemContent,
212
+ ): SharedSliceItemContent {
213
+ return {
214
+ key: content.key,
215
+ name: model.sliceName,
216
+ maybeLabel: content.maybeLabel,
217
+ widget: {
218
+ __TYPE__: "SharedSliceContent",
219
+ variation: model.variationId,
220
+ primary: Object.entries(content.widget.nonRepeat).reduce(
221
+ (acc, [fieldKey, fieldContent]) => {
222
+ return model.fields.primary?.[fieldKey]
223
+ ? { ...acc, [fieldKey]: fieldContent }
224
+ : acc
225
+ },
226
+ {},
227
+ ),
228
+ items: content.widget.repeat.map((groupItem) => {
229
+ return {
230
+ __TYPE__: "GroupItemContent",
231
+ value: groupItem.value.reduce<GroupItemContent["value"]>(
232
+ (acc, [fieldKey, fieldContent]) => {
233
+ return model.fields.items?.[fieldKey]
234
+ ? acc.concat([[fieldKey, fieldContent]])
235
+ : acc
236
+ },
237
+ [],
238
+ ),
239
+ }
240
+ }, []),
241
+ },
242
+ }
243
+ }
@@ -13,6 +13,7 @@ import type {
13
13
  import type { LegacyContentCtx } from "../../../LegacyContentCtx"
14
14
  import {
15
15
  GroupContent,
16
+ GroupItemContent,
16
17
  GroupLegacy,
17
18
  isGroupContent,
18
19
  traverseGroupContent,
@@ -129,3 +130,47 @@ export function traverseSimpleSliceContent({
129
130
  )
130
131
  }
131
132
  }
133
+
134
+ export function migrateSimpleSlice(
135
+ model: VariationFields,
136
+ content: SimpleSliceItemContent,
137
+ ): SharedSliceItemContent {
138
+ if (content.widget.__TYPE__ === "GroupContentType") {
139
+ return {
140
+ key: content.key,
141
+ name: model.sliceName,
142
+ maybeLabel: content.maybeLabel,
143
+ widget: {
144
+ __TYPE__: "SharedSliceContent",
145
+ variation: model.variationId,
146
+ primary: {},
147
+ items: content.widget.value.map((groupItem) => {
148
+ return {
149
+ __TYPE__: "GroupItemContent",
150
+ value: groupItem.value.reduce<GroupItemContent["value"]>(
151
+ (acc, [fieldKey, fieldContent]) => {
152
+ return model.fields.items?.[fieldKey]
153
+ ? acc.concat([[fieldKey, fieldContent]])
154
+ : acc
155
+ },
156
+ [],
157
+ ),
158
+ }
159
+ }, []),
160
+ },
161
+ }
162
+ }
163
+ return {
164
+ key: content.key,
165
+ name: model.sliceName,
166
+ maybeLabel: content.maybeLabel,
167
+ widget: {
168
+ __TYPE__: "SharedSliceContent",
169
+ variation: model.variationId,
170
+ primary: model.fields.primary?.[content.name]
171
+ ? { [content.key]: content.widget }
172
+ : {},
173
+ items: [],
174
+ },
175
+ }
176
+ }
@@ -215,7 +215,7 @@ export function filterMissingSharedSlices(
215
215
 
216
216
  export function flattenCustomTypeFields(
217
217
  customType: StaticCustomType,
218
- ): Partial<Record<string, StaticWidget>> {
218
+ ): Record<string, StaticWidget> {
219
219
  return Object.values(customType.json).reduce(
220
220
  (acc, tab) => ({
221
221
  ...acc,
@@ -225,10 +225,11 @@ export function flattenCustomTypeFields(
225
225
  )
226
226
  }
227
227
 
228
- export function collectSharedSlices(
229
- customType: StaticCustomType,
230
- ): Record<string, SharedSlice> {
231
- return flattenStaticWidgets(customType).reduce((acc, [, w]) => {
228
+ export function collectSharedSlices(customType: {
229
+ customTypeId: string
230
+ fields: Record<string, StaticWidget>
231
+ }): Record<string, SharedSlice> {
232
+ return Object.entries(customType.fields).reduce((acc, [, w]) => {
232
233
  if (w.type === "Slices" || w.type === "Choice") {
233
234
  return {
234
235
  ...acc,
@@ -1,11 +1,28 @@
1
1
  import type { Asset } from "../../../../common"
2
2
  import { getAssetOrThrow } from "../../../../common"
3
- import type { ImageContent } from "../../../../content"
3
+ import type { ImageContent, ImageContentView } from "../../../../content"
4
4
  import { withOptionals } from "../../../../utils/Objects"
5
5
  import type { ImageField, ImportImage } from "../../../validators"
6
+ import type { ImportImageBlock } from "../../../validators/fields/nestable/ImportRichText/blocks"
6
7
 
7
- function convertImage(imageField: ImageField, image: Asset) {
8
- return withOptionals<Omit<ImageContent, "__TYPE__" | "thumbnails">>(
8
+ function convertImage<Input extends ImageField | ImportImageBlock>(
9
+ inputImage: Input,
10
+ image: Asset,
11
+ ) {
12
+ // We can do that because anyway all properties being read
13
+ const imageField: ImageField = {
14
+ ...inputImage,
15
+ edit:
16
+ "edit" in inputImage
17
+ ? inputImage.edit
18
+ : {
19
+ x: 0,
20
+ y: 0,
21
+ zoom: 1,
22
+ },
23
+ dimensions: "dimensions" in inputImage ? inputImage.dimensions : {},
24
+ }
25
+ return withOptionals<ImageContentView>(
9
26
  {
10
27
  origin: {
11
28
  id: image.id,
@@ -57,6 +74,16 @@ function convertThumbnails(
57
74
  )
58
75
  }
59
76
 
77
+ export const imageBlockConverter = (
78
+ imageBlock: ImportImageBlock,
79
+ assets: Record<Asset["id"], Asset | undefined>,
80
+ ): ImageContentView => {
81
+ return convertImage<ImportImageBlock>(
82
+ imageBlock,
83
+ getAssetOrThrow(assets)(imageBlock.id),
84
+ )
85
+ }
86
+
60
87
  // All assets from `imageField` must be present in `assets`
61
88
  // If not then function will throw an error
62
89
  export const imageConverter = (
@@ -66,7 +93,10 @@ export const imageConverter = (
66
93
  if (!imageField) return
67
94
 
68
95
  return {
69
- ...convertImage(imageField, getAssetOrThrow(assets)(imageField.id)),
96
+ ...convertImage<ImageField>(
97
+ imageField,
98
+ getAssetOrThrow(assets)(imageField.id),
99
+ ),
70
100
  thumbnails: convertThumbnails(imageField, assets),
71
101
  __TYPE__: "ImageContent" as const,
72
102
  }
@@ -11,6 +11,7 @@ import {
11
11
  imageConverter,
12
12
  linkConverter,
13
13
  numberConverter,
14
+ richTextConverter,
14
15
  selectConverter,
15
16
  textConverter,
16
17
  timestampConverter,
@@ -44,6 +45,8 @@ export function convertNestableWidget(
44
45
  return linkConverter(field.value, assets)
45
46
  case "Image":
46
47
  return imageConverter(field.value, assets)
48
+ case "StructuredText":
49
+ return richTextConverter(field.value, assets, embeds)
47
50
  default:
48
51
  throw new Error(
49
52
  `Unsupported type of nestable converter ${JSON.stringify(field)}`,