@prismicio/types-internal 3.4.0-alpha.13 → 3.4.0-alpha.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.
Files changed (45) hide show
  1. package/lib/content/Document.d.ts +498 -3366
  2. package/lib/content/fields/GroupContent.d.ts +1 -1
  3. package/lib/content/fields/GroupContent.js +2 -1
  4. package/lib/content/fields/WidgetContent.d.ts +514 -3382
  5. package/lib/content/fields/nestable/NestableContent.d.ts +68 -546
  6. package/lib/content/fields/nestable/NestableContent.js +7 -8
  7. package/lib/content/fields/nestable/RepeatableContent.d.ts +2 -2
  8. package/lib/content/fields/nestable/RepeatableContent.js +5 -2
  9. package/lib/content/fields/nestable/RichTextContent/index.d.ts +9 -641
  10. package/lib/content/fields/nestable/RichTextContent/index.js +4 -8
  11. package/lib/content/fields/nestable/TableContent.d.ts +637 -834
  12. package/lib/content/fields/nestable/TableContent.js +10 -7
  13. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +190 -1146
  14. package/lib/content/fields/slices/Slice/RepeatableContent.d.ts +26 -252
  15. package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +190 -1146
  16. package/lib/content/fields/slices/Slice/SimpleSliceContent.d.ts +68 -546
  17. package/lib/content/fields/slices/Slice/SlicePrimaryContent.d.ts +68 -546
  18. package/lib/content/fields/slices/Slice/index.d.ts +269 -2029
  19. package/lib/content/fields/slices/SliceItem.d.ts +463 -2223
  20. package/lib/content/fields/slices/SlicesContent.d.ts +394 -2784
  21. package/lib/customtypes/widgets/Group.d.ts +36 -36
  22. package/lib/customtypes/widgets/Widget.d.ts +54 -54
  23. package/lib/customtypes/widgets/nestable/NestableWidget.d.ts +6 -6
  24. package/lib/customtypes/widgets/nestable/NestableWidget.js +1 -1
  25. package/lib/customtypes/widgets/nestable/RichText.d.ts +0 -2
  26. package/lib/customtypes/widgets/nestable/RichText.js +0 -2
  27. package/lib/customtypes/widgets/slices/CompositeSlice.d.ts +12 -12
  28. package/lib/customtypes/widgets/slices/LegacySlice.d.ts +12 -12
  29. package/lib/customtypes/widgets/slices/SharedSlice.d.ts +48 -48
  30. package/lib/customtypes/widgets/slices/SlicePrimaryWidget.d.ts +18 -18
  31. package/lib/customtypes/widgets/slices/Slices.d.ts +72 -72
  32. package/package.json +1 -1
  33. package/src/content/fields/GroupContent.ts +2 -1
  34. package/src/content/fields/nestable/NestableContent.ts +13 -9
  35. package/src/content/fields/nestable/RepeatableContent.ts +6 -2
  36. package/src/content/fields/nestable/RichTextContent/Blocks.ts +238 -0
  37. package/src/content/fields/nestable/RichTextContent/index.ts +3 -7
  38. package/src/content/fields/nestable/TableContent.ts +10 -7
  39. package/src/customtypes/widgets/nestable/NestableWidget.ts +1 -1
  40. package/src/customtypes/widgets/nestable/RichText.ts +0 -2
  41. package/src/content/fields/nestable/RichTextContent/Block.ts +0 -35
  42. package/src/content/fields/nestable/RichTextContent/EmbedBlock.ts +0 -81
  43. package/src/content/fields/nestable/RichTextContent/ImageBlock.ts +0 -42
  44. package/src/content/fields/nestable/RichTextContent/TableBlock.ts +0 -36
  45. package/src/content/fields/nestable/RichTextContent/TextBlock.ts +0 -108
@@ -9,6 +9,7 @@ import type {
9
9
  } from "../../../_internal/utils"
10
10
  import type { Link, NestableWidget } from "../../../customtypes"
11
11
  import type { LegacyContentCtx, WithTypes } from "../../LegacyContentCtx"
12
+ import { hasContentType } from "../../utils"
12
13
  import {
13
14
  isLinkContent,
14
15
  LinkContent,
@@ -16,15 +17,18 @@ import {
16
17
  LinkContentType,
17
18
  } from "./LinkContent"
18
19
 
20
+ const RepeatableContentType = "RepeatableContent" as const
21
+
19
22
  export const RepeatableContent = t.strict({
20
- __TYPE__: t.literal("RepeatableContent"),
23
+ __TYPE__: t.literal(RepeatableContentType),
21
24
  type: t.literal("Link"),
22
25
  value: t.array(LinkContent),
23
26
  })
24
27
 
25
28
  export type RepeatableContent = t.TypeOf<typeof RepeatableContent>
26
29
 
27
- export const isRepeatableContent = RepeatableContent.is
30
+ export const isRepeatableContent = (u: unknown): u is RepeatableContent =>
31
+ hasContentType(u) && u.__TYPE__ === RepeatableContentType
28
32
 
29
33
  export const RepeatableLegacy = (
30
34
  ctx: LegacyContentCtx,
@@ -0,0 +1,238 @@
1
+ import { either } from "fp-ts"
2
+ import { isLeft } from "fp-ts/lib/Either"
3
+ import { pipe } from "fp-ts/lib/function"
4
+ import * as t from "io-ts"
5
+ import { withFallback } from "io-ts-types"
6
+
7
+ import {
8
+ RichTextNodeType,
9
+ RichTextNodeTypeCodec,
10
+ } from "../../../../customtypes/widgets/nestable"
11
+ import { StringOrNull } from "../../../../validators"
12
+ import { nullable, refineType } from "../../../../validators/function"
13
+ import {
14
+ EmbedContent,
15
+ EmbedContentLegacy,
16
+ EmbedContentType,
17
+ EmbedLegacy,
18
+ } from "../EmbedContent"
19
+ import { ImageContentView } from "../ImageContent"
20
+ import { FilledLink, FilledLinkLegacy } from "../LinkContent"
21
+
22
+ const linkSpan = (linkCodec: typeof FilledLink | typeof FilledLinkLegacy) =>
23
+ t.strict({
24
+ data: linkCodec,
25
+ start: t.number,
26
+ end: t.number,
27
+ type: t.literal(RichTextNodeType.hyperlink),
28
+ })
29
+
30
+ const labelSpan = t.strict({
31
+ data: withFallback(t.string, ""),
32
+ start: t.number,
33
+ end: t.number,
34
+ type: t.literal("label"),
35
+ })
36
+
37
+ const basicSpan = t.strict({
38
+ start: t.number,
39
+ end: t.number,
40
+ type: t.keyof({
41
+ [RichTextNodeType.strong]: null,
42
+ [RichTextNodeType.em]: null,
43
+ "list-item": null, // legacy case that should not happen, we shouldn't support this in new page builder or migration API
44
+ }),
45
+ })
46
+
47
+ export const Span = t.union([linkSpan(FilledLink), labelSpan, basicSpan])
48
+ export type Span = t.TypeOf<typeof Span>
49
+
50
+ export const SpanLegacy = t.union([
51
+ linkSpan(FilledLinkLegacy),
52
+ labelSpan,
53
+ basicSpan,
54
+ ])
55
+ export type SpanLegacy = t.TypeOf<typeof SpanLegacy>
56
+
57
+ export const ValidatedSpans = <C extends typeof Span | typeof SpanLegacy>(
58
+ spanCodec: C,
59
+ ) => {
60
+ type S = t.TypeOf<C>
61
+
62
+ return new t.Type<S[], S[], unknown>(
63
+ "ValidatedSpans",
64
+ (spans): spans is S[] => Array.isArray(spans) && spans.every(spanCodec.is),
65
+ (spans: unknown, c) => {
66
+ if (Array.isArray(spans)) {
67
+ const res = spans
68
+ .reduce<Array<S>>((acc, maybeSpan) => {
69
+ const decodedSpan = spanCodec.decode(maybeSpan)
70
+ if (isLeft(decodedSpan)) return acc
71
+ return [...acc, decodedSpan.right]
72
+ }, [])
73
+ .sort((m1: S, m2: S) => m1.start - m2.start)
74
+ return t.success(res)
75
+ } else return t.failure(spans, c)
76
+ },
77
+ (m) => {
78
+ return m.reduce<Array<S>>((acc, meta) => {
79
+ const encoded = <S>spanCodec.encode(meta)
80
+ return [...acc, encoded]
81
+ }, [])
82
+ },
83
+ )
84
+ }
85
+
86
+ // We allow any codec here and let TypeScript infer the resulting codec.
87
+ // Typing it more precisely creates conflicts.
88
+ //
89
+ // Using `t.Mixed` rather than `t.Type<A, O>` causes `data.linkTo` to be
90
+ // typed as `any`. It seems to be an issue with the `nullable` helper.
91
+ const ImageBlockCodec = <A, O>(linkCodec: t.Type<A, O>) =>
92
+ t.exact(
93
+ t.intersection([
94
+ t.type({
95
+ type: t.literal(RichTextNodeType.image),
96
+ data: t.intersection([
97
+ ImageContentView,
98
+ t.partial({
99
+ linkTo: nullable(linkCodec),
100
+ }),
101
+ ]),
102
+ }),
103
+ t.partial({
104
+ label: StringOrNull,
105
+ direction: StringOrNull,
106
+ }),
107
+ ]),
108
+ )
109
+
110
+ export const ImageBlock = ImageBlockCodec(FilledLink)
111
+ export type ImageBlock = t.TypeOf<typeof ImageBlock>
112
+
113
+ const ImageBlockLegacy = ImageBlockCodec(FilledLinkLegacy)
114
+ type ImageBlockLegacy = t.TypeOf<typeof ImageBlockLegacy>
115
+
116
+ export function checkImageBlock(block: Block): block is ImageBlock {
117
+ return block.type === RichTextNodeType.image
118
+ }
119
+
120
+ const embedBlockLegacyCodec = t.exact(
121
+ t.intersection([
122
+ t.type({
123
+ type: t.literal(RichTextNodeType.embed),
124
+ data: t.unknown,
125
+ }),
126
+ t.partial({
127
+ label: StringOrNull,
128
+ direction: StringOrNull,
129
+ }),
130
+ ]),
131
+ )
132
+
133
+ type EmbedBlockLegacy = t.TypeOf<typeof embedBlockLegacyCodec>
134
+
135
+ const EmbedBlockLegacy = new t.Type<EmbedBlock, EmbedBlockLegacy, unknown>(
136
+ "EmbedBlockLegacy",
137
+ (u): u is EmbedBlock => (u as EmbedBlock).type === "embed",
138
+ (block) =>
139
+ pipe(
140
+ embedBlockLegacyCodec.decode(block),
141
+ either.chain((decodedBlock) => {
142
+ return either.map<EmbedLegacy, [EmbedBlockLegacy, EmbedLegacy]>(
143
+ (decodedData: EmbedLegacy) => {
144
+ return [decodedBlock, decodedData]
145
+ },
146
+ )(EmbedContentLegacy.decode(decodedBlock.data))
147
+ }),
148
+ either.map(([block, parsedData]) => {
149
+ return EmbedBlock.encode({
150
+ ...block,
151
+ data: {
152
+ ...parsedData,
153
+ __TYPE__: EmbedContentType,
154
+ all: block.data,
155
+ },
156
+ })
157
+ }),
158
+ ),
159
+ (embedBlock: EmbedBlock): EmbedBlockLegacy => {
160
+ return {
161
+ ...embedBlockLegacyCodec.encode(embedBlock),
162
+ data: EmbedContentLegacy.encode(embedBlock.data),
163
+ }
164
+ },
165
+ )
166
+ export const EmbedBlock = t.exact(
167
+ t.intersection([
168
+ t.type({
169
+ type: t.literal(RichTextNodeType.embed),
170
+ data: EmbedContent,
171
+ }),
172
+ t.partial({
173
+ label: StringOrNull,
174
+ direction: StringOrNull,
175
+ }),
176
+ ]),
177
+ )
178
+ export type EmbedBlock = t.TypeOf<typeof EmbedBlock>
179
+ export function checkEmbedBlock(block: Block): block is EmbedBlock {
180
+ return block.type === RichTextNodeType.embed
181
+ }
182
+
183
+ const TextBlockCodec = <C extends typeof Span | typeof SpanLegacy>(
184
+ spanCodec: C,
185
+ ) =>
186
+ t.exact(
187
+ t.intersection([
188
+ t.type({
189
+ type: refineType(
190
+ RichTextNodeTypeCodec,
191
+ `string which isn't ${RichTextNodeType.image} ${RichTextNodeType.embed}`,
192
+ (s) => s !== RichTextNodeType.image && s !== RichTextNodeType.embed,
193
+ ),
194
+ content: t.intersection([
195
+ t.type({
196
+ text: t.string,
197
+ }),
198
+ t.partial({
199
+ spans: ValidatedSpans(spanCodec),
200
+ }),
201
+ ]),
202
+ }),
203
+ t.partial({
204
+ label: t.string,
205
+ direction: t.string,
206
+ }),
207
+ ]),
208
+ )
209
+
210
+ /* These Text block will decode codec A and encode from codec B to A */
211
+ export const TextBlock = TextBlockCodec(Span)
212
+ export type TextBlock = t.TypeOf<typeof TextBlock>
213
+
214
+ const TextBlockLegacy = TextBlockCodec(SpanLegacy)
215
+ type TextBlockLegacy = t.TypeOf<typeof TextBlockLegacy>
216
+
217
+ const legacyBlockCodec = t.union([
218
+ EmbedBlockLegacy,
219
+ ImageBlockLegacy,
220
+ TextBlockLegacy,
221
+ ])
222
+ type BlockLegacy = t.TypeOf<typeof legacyBlockCodec>
223
+
224
+ export const BlockLegacy = new t.Type<Block, BlockLegacy, unknown>(
225
+ "BlockLegacy",
226
+ (u): u is Block =>
227
+ EmbedBlockLegacy.is(u) || ImageBlockLegacy.is(u) || TextBlockLegacy.is(u),
228
+ (legacyBlock) => legacyBlockCodec.decode(legacyBlock),
229
+ (block: Block) => {
230
+ return (() => {
231
+ if (ImageBlock.is(block)) return ImageBlockLegacy.encode(block)
232
+ else if (EmbedBlock.is(block)) return EmbedBlockLegacy.encode(block)
233
+ else return TextBlockLegacy.encode(block)
234
+ })() as BlockLegacy
235
+ },
236
+ )
237
+ export const Block = t.union([ImageBlock, EmbedBlock, TextBlock])
238
+ export type Block = t.TypeOf<typeof Block>
@@ -4,7 +4,7 @@ import * as t from "io-ts"
4
4
 
5
5
  import type { LegacyContentCtx, WithTypes } from "../../../LegacyContentCtx"
6
6
  import { hasContentType } from "../../../utils"
7
- import { Block, BlockLegacy } from "./Block"
7
+ import { Block, BlockLegacy } from "./Blocks"
8
8
 
9
9
  export const RichTextContentType = "StructuredTextContent"
10
10
  export const isRichTextContent = (u: unknown): u is RichTextContent =>
@@ -19,6 +19,8 @@ export const RichTextContent = t.strict({
19
19
  })
20
20
  export type RichTextContent = t.TypeOf<typeof RichTextContent>
21
21
 
22
+ export * from "./Blocks"
23
+
22
24
  export const RichTextLegacy = (ctx: LegacyContentCtx) =>
23
25
  new t.Type<RichTextContent, WithTypes<RichTextLegacy>, unknown>(
24
26
  "RichTextLegacy",
@@ -37,9 +39,3 @@ export const RichTextLegacy = (ctx: LegacyContentCtx) =>
37
39
  }
38
40
  },
39
41
  )
40
-
41
- export * from "./Block"
42
- export * from "./EmbedBlock"
43
- export * from "./ImageBlock"
44
- export * from "./TableBlock"
45
- export * from "./TextBlock"
@@ -4,14 +4,17 @@ import * as t from "io-ts"
4
4
 
5
5
  import type { LegacyContentCtx, WithTypes } from "../../LegacyContentCtx"
6
6
  import { hasContentType } from "../../utils"
7
- import { TextBlock } from "./RichTextContent/TextBlock"
7
+ import { RichTextContentType, TextBlock } from "./RichTextContent"
8
8
 
9
9
  export const TableContentType = "TableContent"
10
10
 
11
11
  export const isTableContent = (u: unknown): u is TableContent =>
12
12
  hasContentType(u) && u.__TYPE__ === TableContentType
13
13
 
14
- const TableCellContent = t.array(TextBlock)
14
+ const TableCellContent = t.strict({
15
+ __TYPE__: t.literal(RichTextContentType),
16
+ value: t.array(TextBlock),
17
+ })
15
18
 
16
19
  export const TableDataCell = t.strict({
17
20
  type: t.literal("tableCell"),
@@ -31,11 +34,11 @@ export const TableRow = t.strict({
31
34
  })
32
35
  export type TableRow = t.TypeOf<typeof TableRow>
33
36
 
34
- export const tableContentLegacyCodec = t.strict({
37
+ const legacyReader = t.strict({
35
38
  content: t.array(TableRow),
36
39
  })
37
40
 
38
- export type TableLegacy = t.TypeOf<typeof tableContentLegacyCodec>
41
+ type TableLegacy = t.TypeOf<typeof legacyReader>
39
42
 
40
43
  export const TableLegacy = (ctx: LegacyContentCtx) =>
41
44
  new t.Type<TableContent, WithTypes<TableLegacy>, unknown>(
@@ -43,7 +46,7 @@ export const TableLegacy = (ctx: LegacyContentCtx) =>
43
46
  isTableContent,
44
47
  (u) => {
45
48
  return pipe(
46
- tableContentLegacyCodec.decode(u),
49
+ legacyReader.decode(u),
47
50
  either.map((t) =>
48
51
  TableContent.encode({ ...t, __TYPE__: TableContentType }),
49
52
  ),
@@ -52,14 +55,14 @@ export const TableLegacy = (ctx: LegacyContentCtx) =>
52
55
 
53
56
  (t: TableContent) => {
54
57
  return {
55
- content: tableContentLegacyCodec.encode(t),
58
+ content: legacyReader.encode(t),
56
59
  types: { [ctx.keyOfType]: "Table" },
57
60
  }
58
61
  },
59
62
  )
60
63
 
61
64
  export const TableContent = t.intersection([
62
- tableContentLegacyCodec,
65
+ legacyReader,
63
66
  t.strict({
64
67
  __TYPE__: t.literal(TableContentType),
65
68
  }),
@@ -28,12 +28,12 @@ export const NestableWidget = t.union([
28
28
  RichText,
29
29
  Select,
30
30
  Separator,
31
+ Table,
31
32
  Text,
32
33
  Timestamp,
33
34
  Link,
34
35
  Image,
35
36
  IntegrationField,
36
- Table,
37
37
  ])
38
38
 
39
39
  export type NestableWidget = t.TypeOf<typeof NestableWidget>
@@ -29,7 +29,6 @@ export const RichTextNodeType = {
29
29
  list: "list-item",
30
30
  orderedList: "o-list-item",
31
31
  rtl: "rtl",
32
- table: "table",
33
32
  } as const
34
33
 
35
34
  export const RichTextNodeTypeCodec = t.keyof({
@@ -49,7 +48,6 @@ export const RichTextNodeTypeCodec = t.keyof({
49
48
  [RichTextNodeType.list]: null,
50
49
  [RichTextNodeType.orderedList]: null,
51
50
  [RichTextNodeType.rtl]: null,
52
- [RichTextNodeType.table]: null,
53
51
  })
54
52
 
55
53
  const RichTextOptions = new t.Type<string, string, unknown>(
@@ -1,35 +0,0 @@
1
- import * as t from "io-ts"
2
-
3
- import { EmbedBlock, EmbedBlockLegacy } from "./EmbedBlock"
4
- import { ImageBlock, ImageBlockLegacy } from "./ImageBlock"
5
- import { TableBlock, TableBlockLegacy } from "./TableBlock"
6
- import { TextBlock, TextBlockLegacy } from "./TextBlock"
7
-
8
- const legacyBlockCodec = t.union([
9
- EmbedBlockLegacy,
10
- ImageBlockLegacy,
11
- TextBlockLegacy,
12
- TableBlockLegacy,
13
- ])
14
- type BlockLegacy = t.TypeOf<typeof legacyBlockCodec>
15
-
16
- export const BlockLegacy = new t.Type<Block, BlockLegacy, unknown>(
17
- "BlockLegacy",
18
- (u): u is Block =>
19
- EmbedBlockLegacy.is(u) ||
20
- ImageBlockLegacy.is(u) ||
21
- TextBlockLegacy.is(u) ||
22
- TableBlockLegacy.is(u),
23
- (legacyBlock) => legacyBlockCodec.decode(legacyBlock),
24
- (block: Block) => {
25
- return (() => {
26
- if (ImageBlock.is(block)) return ImageBlockLegacy.encode(block)
27
- else if (EmbedBlock.is(block)) return EmbedBlockLegacy.encode(block)
28
- else if (TableBlock.is(block)) return TableBlockLegacy.encode(block)
29
- else return TextBlockLegacy.encode(block)
30
- })() as BlockLegacy
31
- },
32
- )
33
-
34
- export const Block = t.union([ImageBlock, EmbedBlock, TextBlock, TableBlock])
35
- export type Block = t.TypeOf<typeof Block>
@@ -1,81 +0,0 @@
1
- import { either } from "fp-ts"
2
- import { pipe } from "fp-ts/lib/function"
3
- import * as t from "io-ts"
4
-
5
- import { RichTextNodeType } from "../../../../customtypes/widgets/nestable"
6
- import { StringOrNull } from "../../../../validators"
7
- import {
8
- EmbedContent,
9
- EmbedContentLegacy,
10
- EmbedContentType,
11
- EmbedLegacy,
12
- } from "../EmbedContent"
13
- import type { Block } from "./Block"
14
-
15
- const embedBlockLegacyCodec = t.exact(
16
- t.intersection([
17
- t.type({
18
- type: t.literal(RichTextNodeType.embed),
19
- data: t.unknown,
20
- }),
21
- t.partial({
22
- label: StringOrNull,
23
- direction: StringOrNull,
24
- }),
25
- ]),
26
- )
27
-
28
- type EmbedBlockLegacy = t.TypeOf<typeof embedBlockLegacyCodec>
29
-
30
- export const EmbedBlockLegacy = new t.Type<
31
- EmbedBlock,
32
- EmbedBlockLegacy,
33
- unknown
34
- >(
35
- "EmbedBlockLegacy",
36
- (u): u is EmbedBlock => (u as EmbedBlock).type === "embed",
37
- (block) =>
38
- pipe(
39
- embedBlockLegacyCodec.decode(block),
40
- either.chain((decodedBlock) => {
41
- return either.map<EmbedLegacy, [EmbedBlockLegacy, EmbedLegacy]>(
42
- (decodedData: EmbedLegacy) => {
43
- return [decodedBlock, decodedData]
44
- },
45
- )(EmbedContentLegacy.decode(decodedBlock.data))
46
- }),
47
- either.map(([block, parsedData]) => {
48
- return EmbedBlock.encode({
49
- ...block,
50
- data: {
51
- ...parsedData,
52
- __TYPE__: EmbedContentType,
53
- all: block.data,
54
- },
55
- })
56
- }),
57
- ),
58
- (embedBlock: EmbedBlock): EmbedBlockLegacy => {
59
- return {
60
- ...embedBlockLegacyCodec.encode(embedBlock),
61
- data: EmbedContentLegacy.encode(embedBlock.data),
62
- }
63
- },
64
- )
65
- export const EmbedBlock = t.exact(
66
- t.intersection([
67
- t.type({
68
- type: t.literal(RichTextNodeType.embed),
69
- data: EmbedContent,
70
- }),
71
- t.partial({
72
- label: StringOrNull,
73
- direction: StringOrNull,
74
- }),
75
- ]),
76
- )
77
- export type EmbedBlock = t.TypeOf<typeof EmbedBlock>
78
-
79
- export function checkEmbedBlock(block: Block): block is EmbedBlock {
80
- return block.type === RichTextNodeType.embed
81
- }
@@ -1,42 +0,0 @@
1
- import * as t from "io-ts"
2
-
3
- import { RichTextNodeType } from "../../../../customtypes/widgets/nestable"
4
- import { StringOrNull } from "../../../../validators"
5
- import { nullable } from "../../../../validators/function"
6
- import { ImageContentView } from "../ImageContent"
7
- import { FilledLink, FilledLinkLegacy } from "../LinkContent"
8
- import type { Block } from "./Block"
9
-
10
- // We allow any codec here and let TypeScript infer the resulting codec.
11
- // Typing it more precisely creates conflicts.
12
- //
13
- // Using `t.Mixed` rather than `t.Type<A, O>` causes `data.linkTo` to be
14
- // typed as `any`. It seems to be an issue with the `nullable` helper.
15
- const ImageBlockCodec = <A, O>(linkCodec: t.Type<A, O>) =>
16
- t.exact(
17
- t.intersection([
18
- t.type({
19
- type: t.literal(RichTextNodeType.image),
20
- data: t.intersection([
21
- ImageContentView,
22
- t.partial({
23
- linkTo: nullable(linkCodec),
24
- }),
25
- ]),
26
- }),
27
- t.partial({
28
- label: StringOrNull,
29
- direction: StringOrNull,
30
- }),
31
- ]),
32
- )
33
-
34
- export const ImageBlock = ImageBlockCodec(FilledLink)
35
- export type ImageBlock = t.TypeOf<typeof ImageBlock>
36
-
37
- export const ImageBlockLegacy = ImageBlockCodec(FilledLinkLegacy)
38
- export type ImageBlockLegacy = t.TypeOf<typeof ImageBlockLegacy>
39
-
40
- export function checkImageBlock(block: Block): block is ImageBlock {
41
- return block.type === RichTextNodeType.image
42
- }
@@ -1,36 +0,0 @@
1
- import { either } from "fp-ts"
2
- import { pipe } from "fp-ts/lib/function"
3
- import * as t from "io-ts"
4
-
5
- import { RichTextNodeType } from "../../../../customtypes/widgets/nestable"
6
- import { tableContentLegacyCodec, TableLegacy, TableRow } from "../TableContent"
7
-
8
- export const TableBlockLegacy = new t.Type<TableBlock, TableLegacy, unknown>(
9
- "TableBlockLegacy",
10
- (u): u is TableBlock => (u as TableBlock).type === "table",
11
- (block: unknown) => {
12
- return pipe(
13
- tableContentLegacyCodec.decode(block),
14
- either.map(
15
- (parseBlock: TableLegacy): TableBlock => ({
16
- type: RichTextNodeType.table,
17
- content: parseBlock.content.map(TableRow.encode),
18
- }),
19
- ),
20
- )
21
- },
22
- (tableBlock: TableBlock): TableLegacy => {
23
- return tableContentLegacyCodec.encode(tableBlock)
24
- },
25
- )
26
-
27
- export type TableBlockLegacy = t.TypeOf<typeof TableBlockLegacy>
28
-
29
- export const TableBlock = t.exact(
30
- t.type({
31
- type: t.literal(RichTextNodeType.table),
32
- content: t.array(TableRow),
33
- }),
34
- )
35
-
36
- export type TableBlock = t.TypeOf<typeof TableBlock>
@@ -1,108 +0,0 @@
1
- import { isLeft } from "fp-ts/lib/Either"
2
- import * as t from "io-ts"
3
- import { withFallback } from "io-ts-types"
4
-
5
- import {
6
- RichTextNodeType,
7
- RichTextNodeTypeCodec,
8
- } from "../../../../customtypes/widgets/nestable"
9
- import { refineType } from "../../../../validators/function"
10
- import { FilledLink, FilledLinkLegacy } from "../LinkContent"
11
-
12
- const linkSpan = (linkCodec: typeof FilledLink | typeof FilledLinkLegacy) =>
13
- t.strict({
14
- data: linkCodec,
15
- start: t.number,
16
- end: t.number,
17
- type: t.literal(RichTextNodeType.hyperlink),
18
- })
19
-
20
- const labelSpan = t.strict({
21
- data: withFallback(t.string, ""),
22
- start: t.number,
23
- end: t.number,
24
- type: t.literal("label"),
25
- })
26
-
27
- const basicSpan = t.strict({
28
- start: t.number,
29
- end: t.number,
30
- type: t.keyof({
31
- [RichTextNodeType.strong]: null,
32
- [RichTextNodeType.em]: null,
33
- "list-item": null, // legacy case that should not happen, we shouldn't support this in new page builder or migration API
34
- }),
35
- })
36
-
37
- export const Span = t.union([linkSpan(FilledLink), labelSpan, basicSpan])
38
- export type Span = t.TypeOf<typeof Span>
39
-
40
- export const SpanLegacy = t.union([
41
- linkSpan(FilledLinkLegacy),
42
- labelSpan,
43
- basicSpan,
44
- ])
45
- export type SpanLegacy = t.TypeOf<typeof SpanLegacy>
46
-
47
- export const ValidatedSpans = <C extends typeof Span | typeof SpanLegacy>(
48
- spanCodec: C,
49
- ) => {
50
- type S = t.TypeOf<C>
51
-
52
- return new t.Type<S[], S[], unknown>(
53
- "ValidatedSpans",
54
- (spans): spans is S[] => Array.isArray(spans) && spans.every(spanCodec.is),
55
- (spans: unknown, c) => {
56
- if (Array.isArray(spans)) {
57
- const res = spans
58
- .reduce<Array<S>>((acc, maybeSpan) => {
59
- const decodedSpan = spanCodec.decode(maybeSpan)
60
- if (isLeft(decodedSpan)) return acc
61
- return [...acc, decodedSpan.right]
62
- }, [])
63
- .sort((m1: S, m2: S) => m1.start - m2.start)
64
- return t.success(res)
65
- } else return t.failure(spans, c)
66
- },
67
- (m) => {
68
- return m.reduce<Array<S>>((acc, meta) => {
69
- const encoded = spanCodec.encode(meta) as S
70
- return [...acc, encoded]
71
- }, [])
72
- },
73
- )
74
- }
75
-
76
- const TextBlockCodec = <C extends typeof Span | typeof SpanLegacy>(
77
- spanCodec: C,
78
- ) =>
79
- t.exact(
80
- t.intersection([
81
- t.type({
82
- type: refineType(
83
- RichTextNodeTypeCodec,
84
- `string which isn't ${RichTextNodeType.image} ${RichTextNodeType.embed}`,
85
- (s) => s !== RichTextNodeType.image && s !== RichTextNodeType.embed,
86
- ),
87
- content: t.intersection([
88
- t.type({
89
- text: t.string,
90
- }),
91
- t.partial({
92
- spans: ValidatedSpans(spanCodec),
93
- }),
94
- ]),
95
- }),
96
- t.partial({
97
- label: t.string,
98
- direction: t.string,
99
- }),
100
- ]),
101
- )
102
-
103
- /* These Text block will decode codec A and encode from codec B to A */
104
- export const TextBlock = TextBlockCodec(Span)
105
- export type TextBlock = t.TypeOf<typeof TextBlock>
106
-
107
- export const TextBlockLegacy = TextBlockCodec(SpanLegacy)
108
- export type TextBlockLegacy = t.TypeOf<typeof TextBlockLegacy>