@portabletext/editor 2.3.2 → 2.3.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 (38) hide show
  1. package/lib/_chunks-dts/behavior.types.action.d.cts +5 -142
  2. package/lib/_chunks-dts/behavior.types.action.d.ts +55 -192
  3. package/lib/index.cjs +43 -279
  4. package/lib/index.cjs.map +1 -1
  5. package/lib/index.js +5 -245
  6. package/lib/index.js.map +1 -1
  7. package/lib/utils/index.d.cts +2 -2
  8. package/lib/utils/index.d.ts +2 -2
  9. package/package.json +7 -5
  10. package/src/converters/converter.portable-text.deserialize.test.ts +6 -4
  11. package/src/converters/converter.text-html.deserialize.test.ts +15 -15
  12. package/src/converters/converter.text-html.serialize.test.ts +8 -8
  13. package/src/converters/converter.text-plain.test.ts +8 -8
  14. package/src/editor/create-editor.ts +12 -10
  15. package/src/editor/editor-schema.ts +2 -312
  16. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +2 -2
  17. package/src/editor.ts +1 -1
  18. package/src/index.ts +3 -3
  19. package/src/internal-utils/__tests__/values.test.ts +2 -3
  20. package/src/internal-utils/apply-operation-to-portable-text.test.ts +2 -3
  21. package/src/internal-utils/build-index-maps.test.ts +2 -3
  22. package/src/internal-utils/create-test-snapshot.ts +2 -4
  23. package/src/internal-utils/drag-selection.test.ts +2 -5
  24. package/src/internal-utils/operation-to-patches.test.ts +5 -7
  25. package/src/internal-utils/parse-blocks.test.ts +21 -30
  26. package/src/internal-utils/selection-text.ts +2 -3
  27. package/src/internal-utils/terse-pt.test.ts +2 -3
  28. package/src/internal-utils/test-editor.tsx +1 -4
  29. package/src/internal-utils/to-slate-range.test.ts +2 -3
  30. package/src/plugins/plugin.internal.auto-close-brackets.test.tsx +1 -1
  31. package/src/plugins/plugin.markdown.test.tsx +1 -1
  32. package/src/selectors/selector.get-selection-text.test.ts +2 -3
  33. package/src/selectors/selector.get-trimmed-selection.test.ts +2 -3
  34. package/src/utils/util.block-offset.test.ts +2 -3
  35. package/src/utils/util.slice-blocks.test.ts +2 -3
  36. package/src/utils/util.slice-text-block.test.ts +2 -3
  37. package/src/editor/editor-schema-definition.ts +0 -105
  38. package/src/editor/legacy-schema.ts +0 -115
@@ -1,115 +0,0 @@
1
- import type {
2
- ArraySchemaType,
3
- BlockSchemaType,
4
- ObjectSchemaType,
5
- PortableTextBlock,
6
- SchemaType,
7
- SpanSchemaType,
8
- } from '@sanity/types'
9
- import type {PortableTextMemberSchemaTypes} from '../types/editor'
10
-
11
- export function createLegacySchema(
12
- portableTextType: ArraySchemaType<PortableTextBlock>,
13
- ): PortableTextMemberSchemaTypes {
14
- if (!portableTextType) {
15
- throw new Error("Parameter 'portabletextType' missing (required)")
16
- }
17
- const blockType = portableTextType.of?.find(findBlockType) as
18
- | BlockSchemaType
19
- | undefined
20
- if (!blockType) {
21
- throw new Error('Block type is not defined in this schema (required)')
22
- }
23
- const childrenField = blockType.fields?.find(
24
- (field) => field.name === 'children',
25
- ) as {type: ArraySchemaType} | undefined
26
- if (!childrenField) {
27
- throw new Error('Children field for block type found in schema (required)')
28
- }
29
- const ofType = childrenField.type.of
30
- if (!ofType) {
31
- throw new Error(
32
- 'Valid types for block children not found in schema (required)',
33
- )
34
- }
35
- const spanType = ofType.find((memberType) => memberType.name === 'span') as
36
- | ObjectSchemaType
37
- | undefined
38
- if (!spanType) {
39
- throw new Error('Span type not found in schema (required)')
40
- }
41
- const inlineObjectTypes = (ofType.filter(
42
- (memberType) => memberType.name !== 'span',
43
- ) || []) as ObjectSchemaType[]
44
- const blockObjectTypes = (portableTextType.of?.filter(
45
- (field) => field.name !== blockType.name,
46
- ) || []) as ObjectSchemaType[]
47
- return {
48
- styles: resolveEnabledStyles(blockType),
49
- decorators: resolveEnabledDecorators(spanType),
50
- lists: resolveEnabledListItems(blockType),
51
- block: blockType,
52
- span: spanType,
53
- portableText: portableTextType,
54
- inlineObjects: inlineObjectTypes,
55
- blockObjects: blockObjectTypes,
56
- annotations: (spanType as SpanSchemaType).annotations,
57
- }
58
- }
59
-
60
- function resolveEnabledStyles(blockType: ObjectSchemaType) {
61
- const styleField = blockType.fields?.find(
62
- (btField) => btField.name === 'style',
63
- )
64
- if (!styleField) {
65
- throw new Error(
66
- "A field with name 'style' is not defined in the block type (required).",
67
- )
68
- }
69
- const textStyles =
70
- styleField.type.options?.list &&
71
- styleField.type.options.list?.filter(
72
- (style: {value: string}) => style.value,
73
- )
74
- if (!textStyles || textStyles.length === 0) {
75
- throw new Error(
76
- 'The style fields need at least one style ' +
77
- "defined. I.e: {title: 'Normal', value: 'normal'}.",
78
- )
79
- }
80
- return textStyles
81
- }
82
-
83
- function resolveEnabledDecorators(spanType: ObjectSchemaType) {
84
- return (spanType as any).decorators
85
- }
86
-
87
- function resolveEnabledListItems(blockType: ObjectSchemaType) {
88
- const listField = blockType.fields?.find(
89
- (btField) => btField.name === 'listItem',
90
- )
91
- if (!listField) {
92
- throw new Error(
93
- "A field with name 'listItem' is not defined in the block type (required).",
94
- )
95
- }
96
- const listItems =
97
- listField.type.options?.list &&
98
- listField.type.options.list.filter((list: {value: string}) => list.value)
99
- if (!listItems) {
100
- throw new Error('The list field need at least to be an empty array')
101
- }
102
- return listItems
103
- }
104
-
105
- function findBlockType(type: SchemaType): BlockSchemaType | null {
106
- if (type.type) {
107
- return findBlockType(type.type)
108
- }
109
-
110
- if (type.name === 'block') {
111
- return type as BlockSchemaType
112
- }
113
-
114
- return null
115
- }