@portabletext/editor 1.32.0 → 1.33.0

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 (69) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +4 -4
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs +19 -11
  4. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  5. package/lib/_chunks-cjs/plugin.event-listener.cjs +127 -88
  6. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
  7. package/lib/_chunks-cjs/selector.get-trimmed-selection.cjs +97 -0
  8. package/lib/_chunks-cjs/selector.get-trimmed-selection.cjs.map +1 -0
  9. package/lib/_chunks-cjs/{parse-blocks.cjs → util.block-offsets-to-selection.cjs} +21 -2
  10. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs.map +1 -0
  11. package/lib/_chunks-cjs/util.reverse-selection.cjs +11 -0
  12. package/lib/_chunks-cjs/util.reverse-selection.cjs.map +1 -1
  13. package/lib/_chunks-es/behavior.core.js +1 -1
  14. package/lib/_chunks-es/behavior.core.js.map +1 -1
  15. package/lib/_chunks-es/behavior.markdown.js +18 -11
  16. package/lib/_chunks-es/behavior.markdown.js.map +1 -1
  17. package/lib/_chunks-es/plugin.event-listener.js +127 -87
  18. package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
  19. package/lib/_chunks-es/selector.get-trimmed-selection.js +100 -0
  20. package/lib/_chunks-es/selector.get-trimmed-selection.js.map +1 -0
  21. package/lib/_chunks-es/{parse-blocks.js → util.block-offsets-to-selection.js} +21 -1
  22. package/lib/_chunks-es/util.block-offsets-to-selection.js.map +1 -0
  23. package/lib/_chunks-es/util.reverse-selection.js +11 -0
  24. package/lib/_chunks-es/util.reverse-selection.js.map +1 -1
  25. package/lib/behaviors/index.d.cts +1 -0
  26. package/lib/behaviors/index.d.ts +1 -0
  27. package/lib/index.d.cts +60 -0
  28. package/lib/index.d.ts +60 -0
  29. package/lib/plugins/index.cjs +295 -3
  30. package/lib/plugins/index.cjs.map +1 -1
  31. package/lib/plugins/index.d.cts +74 -1
  32. package/lib/plugins/index.d.ts +74 -1
  33. package/lib/plugins/index.js +300 -4
  34. package/lib/plugins/index.js.map +1 -1
  35. package/lib/selectors/index.cjs +51 -1
  36. package/lib/selectors/index.cjs.map +1 -1
  37. package/lib/selectors/index.d.cts +67 -0
  38. package/lib/selectors/index.d.ts +67 -0
  39. package/lib/selectors/index.js +53 -2
  40. package/lib/selectors/index.js.map +1 -1
  41. package/lib/utils/index.cjs +5 -4
  42. package/lib/utils/index.cjs.map +1 -1
  43. package/lib/utils/index.d.cts +16 -0
  44. package/lib/utils/index.d.ts +16 -0
  45. package/lib/utils/index.js +4 -3
  46. package/package.json +2 -2
  47. package/src/behavior-actions/behavior.action.decorator.add.ts +161 -0
  48. package/src/behavior-actions/behavior.action.delete.text.ts +54 -0
  49. package/src/behavior-actions/behavior.actions.ts +5 -43
  50. package/src/behaviors/behavior.markdown-emphasis.ts +395 -0
  51. package/src/behaviors/behavior.markdown.ts +11 -4
  52. package/src/behaviors/behavior.types.ts +1 -0
  53. package/src/editor/plugins/createWithPortableTextMarkModel.ts +2 -97
  54. package/src/plugins/plugin.markdown.tsx +11 -1
  55. package/src/selectors/index.ts +5 -0
  56. package/src/selectors/selector.get-anchor-block.ts +22 -0
  57. package/src/selectors/selector.get-anchor-child.ts +36 -0
  58. package/src/selectors/selector.get-anchor-span.ts +18 -0
  59. package/src/selectors/selector.get-anchor-text-block.ts +20 -0
  60. package/src/selectors/selector.get-trimmed-selection.test.ts +658 -0
  61. package/src/selectors/selector.get-trimmed-selection.ts +175 -0
  62. package/src/utils/index.ts +1 -0
  63. package/src/utils/util.block-offsets-to-selection.ts +36 -0
  64. package/lib/_chunks-cjs/parse-blocks.cjs.map +0 -1
  65. package/lib/_chunks-cjs/util.is-empty-text-block.cjs +0 -14
  66. package/lib/_chunks-cjs/util.is-empty-text-block.cjs.map +0 -1
  67. package/lib/_chunks-es/parse-blocks.js.map +0 -1
  68. package/lib/_chunks-es/util.is-empty-text-block.js +0 -15
  69. package/lib/_chunks-es/util.is-empty-text-block.js.map +0 -1
@@ -0,0 +1,175 @@
1
+ import {
2
+ isPortableTextSpan,
3
+ isPortableTextTextBlock,
4
+ type PortableTextSpan,
5
+ } from '@sanity/types'
6
+ import type {EditorSelector} from '../editor/editor-selector'
7
+ import type {EditorSelection, EditorSelectionPoint} from '../types/editor'
8
+ import {isEmptyTextBlock, isKeyedSegment} from '../utils'
9
+ import {getSelectionEndPoint} from './selector.get-selection-end-point'
10
+ import {getSelectionStartPoint} from './selector.get-selection-start-point'
11
+ import {isSelectionCollapsed} from './selector.is-selection-collapsed'
12
+ import {getFocusTextBlock} from './selectors'
13
+
14
+ /**
15
+ * @public
16
+ */
17
+ export const getTrimmedSelection: EditorSelector<EditorSelection> = ({
18
+ context,
19
+ }) => {
20
+ if (!context.selection) {
21
+ return context.selection
22
+ }
23
+
24
+ const startPoint = getSelectionStartPoint({context})
25
+ const endPoint = getSelectionEndPoint({context})
26
+
27
+ if (!startPoint || !endPoint) {
28
+ return context.selection
29
+ }
30
+
31
+ const startBlockKey = isKeyedSegment(startPoint.path[0])
32
+ ? startPoint.path[0]._key
33
+ : null
34
+ const startChildKey = isKeyedSegment(startPoint.path[2])
35
+ ? startPoint.path[2]._key
36
+ : null
37
+ const endBlockKey = isKeyedSegment(endPoint.path[0])
38
+ ? endPoint.path[0]._key
39
+ : null
40
+ const endChildKey = isKeyedSegment(endPoint.path[2])
41
+ ? endPoint.path[2]._key
42
+ : null
43
+
44
+ if (!startBlockKey || !endBlockKey) {
45
+ return context.selection
46
+ }
47
+
48
+ let startBlockFound = false
49
+ let adjustedStartPoint: EditorSelectionPoint | undefined
50
+ let trimStartPoint = false
51
+ let adjustedEndPoint: EditorSelectionPoint | undefined
52
+ let trimEndPoint = false
53
+ let previousPotentialEndpoint:
54
+ | {blockKey: string; span: PortableTextSpan}
55
+ | undefined
56
+
57
+ for (const block of context.value) {
58
+ if (block._key === startBlockKey) {
59
+ startBlockFound = true
60
+
61
+ if (isPortableTextTextBlock(block) && isEmptyTextBlock(block)) {
62
+ continue
63
+ }
64
+ }
65
+
66
+ if (!startBlockFound) {
67
+ continue
68
+ }
69
+
70
+ if (!isPortableTextTextBlock(block)) {
71
+ continue
72
+ }
73
+
74
+ if (block._key === endBlockKey && isEmptyTextBlock(block)) {
75
+ break
76
+ }
77
+
78
+ for (const child of block.children) {
79
+ if (child._key === endChildKey) {
80
+ if (!isPortableTextSpan(child) || endPoint.offset === 0) {
81
+ adjustedEndPoint = previousPotentialEndpoint
82
+ ? {
83
+ path: [
84
+ {_key: previousPotentialEndpoint.blockKey},
85
+ 'children',
86
+ {_key: previousPotentialEndpoint.span._key},
87
+ ],
88
+ offset: previousPotentialEndpoint.span.text.length,
89
+ }
90
+ : undefined
91
+
92
+ trimEndPoint = true
93
+ break
94
+ }
95
+ }
96
+
97
+ if (trimStartPoint) {
98
+ const lonelySpan =
99
+ isPortableTextSpan(child) && block.children.length === 1
100
+
101
+ if (
102
+ (isPortableTextSpan(child) && child.text.length > 0) ||
103
+ lonelySpan
104
+ ) {
105
+ adjustedStartPoint = {
106
+ path: [{_key: block._key}, 'children', {_key: child._key}],
107
+ offset: 0,
108
+ }
109
+ previousPotentialEndpoint = {blockKey: block._key, span: child}
110
+ trimStartPoint = false
111
+ }
112
+
113
+ continue
114
+ }
115
+
116
+ if (child._key === startChildKey) {
117
+ if (!isPortableTextSpan(child)) {
118
+ trimStartPoint = true
119
+ continue
120
+ }
121
+
122
+ if (startPoint.offset === child.text.length) {
123
+ trimStartPoint = true
124
+ previousPotentialEndpoint =
125
+ child.text.length > 0
126
+ ? {blockKey: block._key, span: child}
127
+ : previousPotentialEndpoint
128
+ continue
129
+ }
130
+ }
131
+
132
+ previousPotentialEndpoint =
133
+ isPortableTextSpan(child) && child.text.length > 0
134
+ ? {blockKey: block._key, span: child}
135
+ : previousPotentialEndpoint
136
+ }
137
+
138
+ if (block._key === endBlockKey) {
139
+ break
140
+ }
141
+ }
142
+
143
+ const trimmedSelection = context.selection.backward
144
+ ? {
145
+ anchor: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,
146
+ focus: adjustedStartPoint ?? startPoint,
147
+ backward: true,
148
+ }
149
+ : {
150
+ anchor: adjustedStartPoint ?? startPoint,
151
+ focus: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,
152
+ }
153
+
154
+ if (
155
+ isSelectionCollapsed({
156
+ context: {
157
+ ...context,
158
+ selection: trimmedSelection,
159
+ },
160
+ })
161
+ ) {
162
+ const focusTextBlock = getFocusTextBlock({
163
+ context: {
164
+ ...context,
165
+ selection: trimmedSelection,
166
+ },
167
+ })
168
+
169
+ if (focusTextBlock && !isEmptyTextBlock(focusTextBlock.node)) {
170
+ return null
171
+ }
172
+ }
173
+
174
+ return trimmedSelection
175
+ }
@@ -4,6 +4,7 @@ export {
4
4
  blockOffsetToSpanSelectionPoint,
5
5
  spanSelectionPointToBlockOffset,
6
6
  } from './util.block-offset'
7
+ export {blockOffsetsToSelection} from './util.block-offsets-to-selection'
7
8
  export {getBlockEndPoint} from './util.get-block-end-point'
8
9
  export {getBlockStartPoint} from './util.get-block-start-point'
9
10
  export {getTextBlockText} from './util.get-text-block-text'
@@ -0,0 +1,36 @@
1
+ import type {PortableTextBlock} from '@sanity/types'
2
+ import type {BlockOffset} from '../behaviors'
3
+ import type {EditorSelection} from '../selectors'
4
+ import {blockOffsetToSpanSelectionPoint} from './util.block-offset'
5
+
6
+ /**
7
+ * @public
8
+ */
9
+ export function blockOffsetsToSelection({
10
+ value,
11
+ offsets,
12
+ backward,
13
+ }: {
14
+ value: Array<PortableTextBlock>
15
+ offsets: {anchor: BlockOffset; focus: BlockOffset}
16
+ backward?: boolean
17
+ }): EditorSelection {
18
+ const anchor = blockOffsetToSpanSelectionPoint({
19
+ value,
20
+ blockOffset: offsets.anchor,
21
+ })
22
+ const focus = blockOffsetToSpanSelectionPoint({
23
+ value,
24
+ blockOffset: offsets.focus,
25
+ })
26
+
27
+ if (!anchor || !focus) {
28
+ return null
29
+ }
30
+
31
+ return {
32
+ anchor,
33
+ focus,
34
+ backward,
35
+ }
36
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"parse-blocks.cjs","sources":["../../src/internal-utils/asserters.ts","../../src/internal-utils/parse-blocks.ts"],"sourcesContent":["import type {TypedObject} from '@sanity/types'\n\nexport function isTypedObject(object: unknown): object is TypedObject {\n return isRecord(object) && typeof object._type === 'string'\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && (typeof value === 'object' || typeof value === 'function')\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isTypedObject} from './asserters'\n\nexport function parseBlock({\n context,\n block,\n options,\n}: {\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n block: unknown\n options: {\n refreshKeys: boolean\n }\n}): PortableTextBlock | undefined {\n if (!isTypedObject(block)) {\n return undefined\n }\n\n if (\n block._type !== context.schema.block.name &&\n !context.schema.blockObjects.some(\n (blockObject) => blockObject.name === block._type,\n )\n ) {\n return undefined\n }\n\n if (block._type !== context.schema.block.name) {\n const _key = options.refreshKeys\n ? context.keyGenerator()\n : typeof block._key === 'string'\n ? block._key\n : context.keyGenerator()\n return {\n ...block,\n _key,\n }\n }\n\n if (!isPortableTextTextBlock(block)) {\n return {\n _type: context.schema.block.name,\n _key: options.refreshKeys\n ? context.keyGenerator()\n : typeof block._key === 'string'\n ? block._key\n : context.keyGenerator(),\n children: [\n {\n _key: context.keyGenerator(),\n _type: context.schema.span.name,\n text: '',\n marks: [],\n },\n ],\n markDefs: [],\n style: context.schema.styles[0].value,\n }\n }\n\n const markDefKeyMap = new Map<string, string>()\n const markDefs = (block.markDefs ?? []).flatMap((markDef) => {\n if (\n context.schema.annotations.some(\n (annotation) => annotation.name === markDef._type,\n )\n ) {\n const _key = options.refreshKeys ? context.keyGenerator() : markDef._key\n markDefKeyMap.set(markDef._key, _key)\n\n return [\n {\n ...markDef,\n _key,\n },\n ]\n }\n\n return []\n })\n\n const children = block.children.flatMap((child) => {\n if (!isTypedObject(child)) {\n return []\n }\n\n if (\n child._type !== context.schema.span.name &&\n !context.schema.inlineObjects.some(\n (inlineObject) => inlineObject.name === child._type,\n )\n ) {\n return []\n }\n\n if (!isPortableTextSpan(child)) {\n return [\n {\n ...child,\n _key: options.refreshKeys ? context.keyGenerator() : child._key,\n },\n ]\n }\n\n const marks = (child.marks ?? []).flatMap((mark) => {\n if (markDefKeyMap.has(mark)) {\n return [markDefKeyMap.get(mark)]\n }\n\n if (\n context.schema.decorators.some((decorator) => decorator.value === mark)\n ) {\n return [mark]\n }\n\n return []\n })\n\n return [\n {\n ...child,\n _key: options.refreshKeys ? context.keyGenerator() : child._key,\n marks,\n },\n ]\n })\n\n const parsedBlock = {\n ...block,\n _key: options.refreshKeys ? context.keyGenerator() : block._key,\n children:\n children.length > 0\n ? children\n : [\n {\n _key: context.keyGenerator(),\n _type: context.schema.span.name,\n text: '',\n marks: [],\n },\n ],\n markDefs,\n }\n\n if (!context.schema.styles.find((style) => style.value === block.style)) {\n const defaultStyle = context.schema.styles[0].value\n\n if (defaultStyle !== undefined) {\n parsedBlock.style = defaultStyle\n } else {\n delete parsedBlock.style\n }\n }\n\n if (!context.schema.lists.find((list) => list.value === block.listItem)) {\n delete parsedBlock.listItem\n delete parsedBlock.level\n }\n\n return parsedBlock\n}\n"],"names":["isTypedObject","object","isRecord","_type","value","parseBlock","context","block","options","schema","name","blockObjects","some","blockObject","_key","refreshKeys","keyGenerator","isPortableTextTextBlock","children","span","text","marks","markDefs","style","styles","markDefKeyMap","Map","flatMap","markDef","annotations","annotation","set","child","inlineObjects","inlineObject","isPortableTextSpan","mark","has","get","decorators","decorator","parsedBlock","length","find","defa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,MAAMO;AAAAA,IAC3DI,UACEA,SAASwB,SAAS,IACdxB,WACA,CACE;AAAA,MACEJ,MAAMR,QAAQU,aAAa;AAAA,MAC3Bb,OAAOG,QAAQG,OAAOU,KAAKT;AAAAA,MAC3BU,MAAM;AAAA,MACNC,OAAO,CAAA;AAAA,IAAA,CACR;AAAA,IAETC;AAAAA,EACF;AAEI,MAAA,CAAChB,QAAQG,OAAOe,OAAOmB,KAAMpB,WAAUA,MAAMnB,UAAUG,MAAMgB,KAAK,GAAG;AACvE,UAAMqB,eAAetC,QAAQG,OAAOe,OAAO,CAAC,EAAEpB;AAE1CwC,qBAAiBC,SACnBJ,YAAYlB,QAAQqB,eAEpB,OAAOH,YAAYlB;AAAAA,EAAAA;AAIvB,SAAKjB,QAAQG,OAAOqC,MAAMH,KAAMI,UAASA,KAAK3C,UAAUG,MAAMyC,QAAQ,MACpE,OAAOP,YAAYO,UACnB,OAAOP,YAAYQ,QAGdR;AACT;;"}
@@ -1,14 +0,0 @@
1
- "use strict";
2
- var types = require("@sanity/types");
3
- function getTextBlockText(block) {
4
- return block.children.map((child) => child.text ?? "").join("");
5
- }
6
- function isEmptyTextBlock(block) {
7
- if (!types.isPortableTextTextBlock(block))
8
- return !1;
9
- const onlyText = block.children.every(types.isPortableTextSpan), blockText = getTextBlockText(block);
10
- return onlyText && blockText === "";
11
- }
12
- exports.getTextBlockText = getTextBlockText;
13
- exports.isEmptyTextBlock = isEmptyTextBlock;
14
- //# sourceMappingURL=util.is-empty-text-block.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.is-empty-text-block.cjs","sources":["../../src/utils/util.get-text-block-text.ts","../../src/utils/util.is-empty-text-block.ts"],"sourcesContent":["import type {PortableTextTextBlock} from '@sanity/types'\n\n/**\n * @public\n */\nexport function getTextBlockText(block: PortableTextTextBlock) {\n return block.children.map((child) => child.text ?? '').join('')\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextBlock,\n} from '@sanity/types'\nimport {getTextBlockText} from './util.get-text-block-text'\n\n/**\n * @public\n */\nexport function isEmptyTextBlock(block: PortableTextBlock) {\n if (!isPortableTextTextBlock(block)) {\n return false\n }\n\n const onlyText = block.children.every(isPortableTextSpan)\n const blockText = getTextBlockText(block)\n\n return onlyText && blockText === ''\n}\n"],"names":["getTextBlockText","block","children","map","child","text","join","isEmptyTextBlock","isPortableTextTextBlock","onlyText","every","isPortableTextSpan","blockText"],"mappings":";;AAKO,SAASA,iBAAiBC,OAA8B;AACtDA,SAAAA,MAAMC,SAASC,IAAKC,CAAAA,UAAUA,MAAMC,QAAQ,EAAE,EAAEC,KAAK,EAAE;AAChE;ACGO,SAASC,iBAAiBN,OAA0B;AACrD,MAAA,CAACO,8BAAwBP,KAAK;AACzB,WAAA;AAGHQ,QAAAA,WAAWR,MAAMC,SAASQ,MAAMC,MAAAA,kBAAkB,GAClDC,YAAYZ,iBAAiBC,KAAK;AAExC,SAAOQ,YAAYG,cAAc;AACnC;;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"parse-blocks.js","sources":["../../src/internal-utils/asserters.ts","../../src/internal-utils/parse-blocks.ts"],"sourcesContent":["import type {TypedObject} from '@sanity/types'\n\nexport function isTypedObject(object: unknown): object is TypedObject {\n return isRecord(object) && typeof object._type === 'string'\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && (typeof value === 'object' || typeof value === 'function')\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isTypedObject} from './asserters'\n\nexport function parseBlock({\n context,\n block,\n options,\n}: {\n context: Pick<EditorContext, 'keyGenerator' | 'schema'>\n block: unknown\n options: {\n refreshKeys: boolean\n }\n}): PortableTextBlock | undefined {\n if (!isTypedObject(block)) {\n return undefined\n }\n\n if (\n block._type !== context.schema.block.name &&\n !context.schema.blockObjects.some(\n (blockObject) => blockObject.name === block._type,\n )\n ) {\n return undefined\n }\n\n if (block._type !== context.schema.block.name) {\n const _key = options.refreshKeys\n ? context.keyGenerator()\n : typeof block._key === 'string'\n ? block._key\n : context.keyGenerator()\n return {\n ...block,\n _key,\n }\n }\n\n if (!isPortableTextTextBlock(block)) {\n return {\n _type: context.schema.block.name,\n _key: options.refreshKeys\n ? context.keyGenerator()\n : typeof block._key === 'string'\n ? block._key\n : context.keyGenerator(),\n children: [\n {\n _key: context.keyGenerator(),\n _type: context.schema.span.name,\n text: '',\n marks: [],\n },\n ],\n markDefs: [],\n style: context.schema.styles[0].value,\n }\n }\n\n const markDefKeyMap = new Map<string, string>()\n const markDefs = (block.markDefs ?? []).flatMap((markDef) => {\n if (\n context.schema.annotations.some(\n (annotation) => annotation.name === markDef._type,\n )\n ) {\n const _key = options.refreshKeys ? context.keyGenerator() : markDef._key\n markDefKeyMap.set(markDef._key, _key)\n\n return [\n {\n ...markDef,\n _key,\n },\n ]\n }\n\n return []\n })\n\n const children = block.children.flatMap((child) => {\n if (!isTypedObject(child)) {\n return []\n }\n\n if (\n child._type !== context.schema.span.name &&\n !context.schema.inlineObjects.some(\n (inlineObject) => inlineObject.name === child._type,\n )\n ) {\n return []\n }\n\n if (!isPortableTextSpan(child)) {\n return [\n {\n ...child,\n _key: options.refreshKeys ? context.keyGenerator() : child._key,\n },\n ]\n }\n\n const marks = (child.marks ?? []).flatMap((mark) => {\n if (markDefKeyMap.has(mark)) {\n return [markDefKeyMap.get(mark)]\n }\n\n if (\n context.schema.decorators.some((decorator) => decorator.value === mark)\n ) {\n return [mark]\n }\n\n return []\n })\n\n return [\n {\n ...child,\n _key: options.refreshKeys ? context.keyGenerator() : child._key,\n marks,\n },\n ]\n })\n\n const parsedBlock = {\n ...block,\n _key: options.refreshKeys ? context.keyGenerator() : block._key,\n children:\n children.length > 0\n ? children\n : [\n {\n _key: context.keyGenerator(),\n _type: context.schema.span.name,\n text: '',\n marks: [],\n },\n ],\n markDefs,\n }\n\n if (!context.schema.styles.find((style) => style.value === block.style)) {\n const defaultStyle = context.schema.styles[0].value\n\n if (defaultStyle !== undefined) {\n parsedBlock.style = defaultStyle\n } else {\n delete parsedBlock.style\n }\n }\n\n if (!context.schema.lists.find((list) => list.value === block.listItem)) {\n delete parsedBlock.listItem\n delete parsedBlock.level\n }\n\n return parsedBlock\n}\n"],"names":["isTypedObject","object","isRecord","_type","value","parseBlock","context","block","options","schema","name","blockObjects","some","blockObject","_key","refreshKeys","keyGenerator","isPortableTextTextBlock","children","span","text","marks","markDefs","style","styles","markDefKeyMap","Map","flatMap","markDef","annotations","annotation","set","child","inlineObjects","inlineObject","isPortableTextSpan","mark","has","get","decorators","decorator","parsedBlock","length","find","defaultStyle","undefined","lists","list","listItem","level"],"mappings":";AAEO,SAASA,cAAcC,QAAwC;AACpE,SAAOC,SAASD,MAAM,KAAK,OAAOA,OAAOE,SAAU;AACrD;AAEA,SAASD,SAASE,OAAkD;AAClE,SAAO,CAAC,CAACA,UAAU,OAAOA,SAAU,YAAY,OAAOA,SAAU;AACnE;ACAO,SAASC,WAAW;AAAA,EACzBC;AAAAA,EACAC;AAAAA,EACAC;AAOF,GAAkC;AAKhC,MAJI,CAACR,cAAcO,KAAK,KAKtBA,MAAMJ,UAAUG,QAAQG,OAAOF,MAAMG,QACrC,CAACJ,QAAQG,OAAOE,aAAaC,KAC1BC,iBAAgBA,YAAYH,SAASH,MAAMJ,KAC9C;AAEA;AAGF,MAAII,MAAMJ,UAAUG,QAAQG,OAAOF,MAAMG,MAAM;AAC7C,UAAMI,OAAON,QAAQO,cACjBT,QAAQU,aAAa,IACrB,OAAOT,MAAMO,QAAS,WACpBP,MAAMO,OACNR,QAAQU,aAAa;AACpB,WAAA;AAAA,MACL,GAAGT;AAAAA,MACHO;AAAAA,IACF;AAAA,EAAA;AAGE,MAAA,CAACG,wBAAwBV,KAAK;AACzB,WAAA;AAAA,MACLJ,OAAOG,QAAQG,OAAOF,MAAMG;AAAAA,MAC5BI,MAAMN,QAAQO,cACVT,QAAQU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,cAAcY,IAAID,IAAI,IACjB,CAACX,cAAca,IAAIF,IAAI,CAAC,IAI/B9B,QAAQG,OAAO8B,WAAW3B,KAAM4B,CAAcA,cAAAA,UAAUpC,UAAUgC,IAAI,IAE/D,CAACA,IAAI,IAGP,CAAA,CACR;AAED,WAAO,CACL;AAAA,MACE,GAAGJ;AAAAA,MACHlB,MAAMN,QAAQO,cAAcT,QAAQU,aAAAA,IAAiBgB,MAAMlB;AAAAA,MAC3DO;AAAAA,IAAAA,CACD;AAAA,EAEJ,CAAA,GAEKoB,cAAc;AAAA,IAClB,GAAGlC;AAAAA,IACHO,MAAMN,QAAQO,cAAcT,QAAQU,aAAAA,IAAiBT,MAAMO;AAAAA,IAC3DI,UACEA,SAASwB,SAAS,IACdxB,WACA,CACE;AAAA,MACEJ,MAAMR,QAAQU,aAAa;AAAA,MAC3Bb,OAAOG,QAAQG,OAAOU,KAAKT;AAAAA,MAC3BU,MAAM;AAAA,MACNC,OAAO,CAAA;AAAA,IAAA,CACR;AAAA,IAETC;AAAAA,EACF;AAEI,MAAA,CAAChB,QAAQG,OAAOe,OAAOmB,KAAMpB,WAAUA,MAAMnB,UAAUG,MAAMgB,KAAK,GAAG;AACvE,UAAMqB,eAAetC,QAAQG,OAAOe,OAAO,CAAC,EAAEpB;AAE1CwC,qBAAiBC,SACnBJ,YAAYlB,QAAQqB,eAEpB,OAAOH,YAAYlB;AAAAA,EAAAA;AAIvB,SAAKjB,QAAQG,OAAOqC,MAAMH,KAAMI,UAASA,KAAK3C,UAAUG,MAAMyC,QAAQ,MACpE,OAAOP,YAAYO,UACnB,OAAOP,YAAYQ,QAGdR;AACT;"}
@@ -1,15 +0,0 @@
1
- import { isPortableTextTextBlock, isPortableTextSpan } from "@sanity/types";
2
- function getTextBlockText(block) {
3
- return block.children.map((child) => child.text ?? "").join("");
4
- }
5
- function isEmptyTextBlock(block) {
6
- if (!isPortableTextTextBlock(block))
7
- return !1;
8
- const onlyText = block.children.every(isPortableTextSpan), blockText = getTextBlockText(block);
9
- return onlyText && blockText === "";
10
- }
11
- export {
12
- getTextBlockText,
13
- isEmptyTextBlock
14
- };
15
- //# sourceMappingURL=util.is-empty-text-block.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.is-empty-text-block.js","sources":["../../src/utils/util.get-text-block-text.ts","../../src/utils/util.is-empty-text-block.ts"],"sourcesContent":["import type {PortableTextTextBlock} from '@sanity/types'\n\n/**\n * @public\n */\nexport function getTextBlockText(block: PortableTextTextBlock) {\n return block.children.map((child) => child.text ?? '').join('')\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextBlock,\n} from '@sanity/types'\nimport {getTextBlockText} from './util.get-text-block-text'\n\n/**\n * @public\n */\nexport function isEmptyTextBlock(block: PortableTextBlock) {\n if (!isPortableTextTextBlock(block)) {\n return false\n }\n\n const onlyText = block.children.every(isPortableTextSpan)\n const blockText = getTextBlockText(block)\n\n return onlyText && blockText === ''\n}\n"],"names":["getTextBlockText","block","children","map","child","text","join","isEmptyTextBlock","isPortableTextTextBlock","onlyText","every","isPortableTextSpan","blockText"],"mappings":";AAKO,SAASA,iBAAiBC,OAA8B;AACtDA,SAAAA,MAAMC,SAASC,IAAKC,CAAAA,UAAUA,MAAMC,QAAQ,EAAE,EAAEC,KAAK,EAAE;AAChE;ACGO,SAASC,iBAAiBN,OAA0B;AACrD,MAAA,CAACO,wBAAwBP,KAAK;AACzB,WAAA;AAGHQ,QAAAA,WAAWR,MAAMC,SAASQ,MAAMC,kBAAkB,GAClDC,YAAYZ,iBAAiBC,KAAK;AAExC,SAAOQ,YAAYG,cAAc;AACnC;"}