@portabletext/editor 1.15.0 → 1.15.2

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 (60) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +494 -0
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -0
  3. package/lib/_chunks-cjs/selector.get-text-before.cjs +12 -232
  4. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  5. package/lib/_chunks-cjs/selectors.cjs +234 -0
  6. package/lib/_chunks-cjs/selectors.cjs.map +1 -0
  7. package/lib/_chunks-es/behavior.core.js +496 -0
  8. package/lib/_chunks-es/behavior.core.js.map +1 -0
  9. package/lib/_chunks-es/selector.get-text-before.js +14 -234
  10. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  11. package/lib/_chunks-es/selectors.js +235 -0
  12. package/lib/_chunks-es/selectors.js.map +1 -0
  13. package/lib/behaviors/index.cjs +460 -0
  14. package/lib/behaviors/index.cjs.map +1 -0
  15. package/lib/behaviors/index.d.cts +875 -0
  16. package/lib/behaviors/index.d.ts +875 -0
  17. package/lib/behaviors/index.js +464 -0
  18. package/lib/behaviors/index.js.map +1 -0
  19. package/lib/index.cjs +843 -1489
  20. package/lib/index.cjs.map +1 -1
  21. package/lib/index.d.cts +17 -536
  22. package/lib/index.d.ts +17 -536
  23. package/lib/index.js +837 -1482
  24. package/lib/index.js.map +1 -1
  25. package/lib/selectors/index.cjs +16 -16
  26. package/lib/selectors/index.cjs.map +1 -1
  27. package/lib/selectors/index.js +3 -2
  28. package/lib/selectors/index.js.map +1 -1
  29. package/package.json +13 -7
  30. package/src/{editor/behavior → behavior-actions}/behavior.action-utils.insert-block.ts +2 -2
  31. package/src/{editor/behavior → behavior-actions}/behavior.action.insert-block-object.ts +1 -1
  32. package/src/{editor/behavior → behavior-actions}/behavior.action.insert-break.ts +1 -1
  33. package/src/{editor/behavior → behavior-actions}/behavior.action.insert-inline-object.ts +1 -1
  34. package/src/{editor/behavior → behavior-actions}/behavior.action.list-item.ts +2 -2
  35. package/src/{editor/behavior → behavior-actions}/behavior.action.text-block.set.ts +1 -1
  36. package/src/{editor/behavior → behavior-actions}/behavior.action.text-block.unset.ts +1 -1
  37. package/src/{editor/behavior → behavior-actions}/behavior.actions.ts +14 -14
  38. package/src/{editor/behavior → behavior-actions}/behavior.guards.ts +1 -1
  39. package/src/{editor/behavior → behaviors}/behavior.code-editor.ts +4 -4
  40. package/src/{editor/behavior → behaviors}/behavior.core.block-objects.ts +5 -5
  41. package/src/{editor/behavior → behaviors}/behavior.core.decorators.ts +6 -16
  42. package/src/{editor/behavior → behaviors}/behavior.core.lists.ts +15 -15
  43. package/src/{editor/behavior → behaviors}/behavior.links.ts +4 -4
  44. package/src/{editor/behavior → behaviors}/behavior.markdown.ts +15 -25
  45. package/src/{editor/behavior → behaviors}/behavior.types.ts +32 -39
  46. package/src/behaviors/index.ts +28 -0
  47. package/src/editor/create-editor.ts +26 -46
  48. package/src/editor/editor-machine.ts +14 -12
  49. package/src/editor/plugins/createWithEditableAPI.ts +3 -3
  50. package/src/editor/plugins/createWithPortableTextMarkModel.ts +1 -1
  51. package/src/editor/utils/utils.block-offset.ts +1 -1
  52. package/src/index.ts +11 -26
  53. package/src/selectors/selector.get-active-list-item.ts +1 -1
  54. package/src/selectors/selectors.ts +1 -1
  55. package/src/type-utils.ts +17 -0
  56. package/src/utils/__tests__/operationToPatches.test.ts +1 -1
  57. package/src/utils/__tests__/patchToOperations.test.ts +1 -1
  58. /package/src/{editor/behavior → behavior-actions}/behavior.action.insert-span.ts +0 -0
  59. /package/src/{editor/behavior → behavior-actions}/behavior.action.style.ts +0 -0
  60. /package/src/{editor/behavior → behaviors}/behavior.core.ts +0 -0
@@ -1,217 +1,4 @@
1
- import { isPortableTextListBlock, isPortableTextTextBlock, isKeySegment, isPortableTextSpan } from "@sanity/types";
2
- function createGuards({
3
- schema
4
- }) {
5
- function isListBlock(block) {
6
- return isPortableTextListBlock(block) && block._type === schema.block.name;
7
- }
8
- function isTextBlock(block) {
9
- return isPortableTextTextBlock(block) && block._type === schema.block.name;
10
- }
11
- return {
12
- isListBlock,
13
- isTextBlock
14
- };
15
- }
16
- const selectionIsCollapsed = ({
17
- context
18
- }) => JSON.stringify(context.selection?.anchor.path) === JSON.stringify(context.selection?.focus.path) && context.selection?.anchor.offset === context.selection?.focus.offset, getFocusBlock = ({
19
- context
20
- }) => {
21
- const key = context.selection && isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
22
- return node && key ? {
23
- node,
24
- path: [{
25
- _key: key
26
- }]
27
- } : void 0;
28
- }, getFocusListBlock = ({
29
- context
30
- }) => {
31
- const guards = createGuards(context), focusBlock = getFocusBlock({
32
- context
33
- });
34
- return focusBlock && guards.isListBlock(focusBlock.node) ? {
35
- node: focusBlock.node,
36
- path: focusBlock.path
37
- } : void 0;
38
- }, getFocusTextBlock = ({
39
- context
40
- }) => {
41
- const focusBlock = getFocusBlock({
42
- context
43
- });
44
- return focusBlock && isPortableTextTextBlock(focusBlock.node) ? {
45
- node: focusBlock.node,
46
- path: focusBlock.path
47
- } : void 0;
48
- }, getFocusBlockObject = ({
49
- context
50
- }) => {
51
- const focusBlock = getFocusBlock({
52
- context
53
- });
54
- return focusBlock && !isPortableTextTextBlock(focusBlock.node) ? {
55
- node: focusBlock.node,
56
- path: focusBlock.path
57
- } : void 0;
58
- }, getFocusChild = ({
59
- context
60
- }) => {
61
- const focusBlock = getFocusTextBlock({
62
- context
63
- });
64
- if (!focusBlock)
65
- return;
66
- const key = context.selection && isKeySegment(context.selection.focus.path[2]) ? context.selection.focus.path[2]._key : void 0, node = key ? focusBlock.node.children.find((span) => span._key === key) : void 0;
67
- return node && key ? {
68
- node,
69
- path: [...focusBlock.path, "children", {
70
- _key: key
71
- }]
72
- } : void 0;
73
- }, getFocusSpan = ({
74
- context
75
- }) => {
76
- const focusChild = getFocusChild({
77
- context
78
- });
79
- return focusChild && isPortableTextSpan(focusChild.node) ? {
80
- node: focusChild.node,
81
- path: focusChild.path
82
- } : void 0;
83
- }, getFirstBlock = ({
84
- context
85
- }) => {
86
- const node = context.value[0];
87
- return node ? {
88
- node,
89
- path: [{
90
- _key: node._key
91
- }]
92
- } : void 0;
93
- }, getLastBlock = ({
94
- context
95
- }) => {
96
- const node = context.value[context.value.length - 1] ? context.value[context.value.length - 1] : void 0;
97
- return node ? {
98
- node,
99
- path: [{
100
- _key: node._key
101
- }]
102
- } : void 0;
103
- }, getSelectedBlocks = ({
104
- context
105
- }) => {
106
- if (!context.selection)
107
- return [];
108
- const selectedBlocks = [], startKey = context.selection.backward ? isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, endKey = context.selection.backward ? isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0;
109
- if (!startKey || !endKey)
110
- return selectedBlocks;
111
- for (const block of context.value) {
112
- if (block._key === startKey) {
113
- if (selectedBlocks.push({
114
- node: block,
115
- path: [{
116
- _key: block._key
117
- }]
118
- }), startKey === endKey)
119
- break;
120
- continue;
121
- }
122
- if (block._key === endKey) {
123
- selectedBlocks.push({
124
- node: block,
125
- path: [{
126
- _key: block._key
127
- }]
128
- });
129
- break;
130
- }
131
- selectedBlocks.length > 0 && selectedBlocks.push({
132
- node: block,
133
- path: [{
134
- _key: block._key
135
- }]
136
- });
137
- }
138
- return selectedBlocks;
139
- }, getSelectionStartBlock = ({
140
- context
141
- }) => {
142
- if (!context.selection)
143
- return;
144
- const key = context.selection.backward ? isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
145
- return node && key ? {
146
- node,
147
- path: [{
148
- _key: key
149
- }]
150
- } : void 0;
151
- }, getSelectionEndBlock = ({
152
- context
153
- }) => {
154
- if (!context.selection)
155
- return;
156
- const key = context.selection.backward ? isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
157
- return node && key ? {
158
- node,
159
- path: [{
160
- _key: key
161
- }]
162
- } : void 0;
163
- }, getPreviousBlock = ({
164
- context
165
- }) => {
166
- let previousBlock;
167
- const selectionStartBlock = getSelectionStartBlock({
168
- context
169
- });
170
- if (!selectionStartBlock)
171
- return;
172
- let foundSelectionStartBlock = !1;
173
- for (const block of context.value) {
174
- if (block._key === selectionStartBlock.node._key) {
175
- foundSelectionStartBlock = !0;
176
- break;
177
- }
178
- previousBlock = {
179
- node: block,
180
- path: [{
181
- _key: block._key
182
- }]
183
- };
184
- }
185
- if (foundSelectionStartBlock && previousBlock)
186
- return previousBlock;
187
- }, getNextBlock = ({
188
- context
189
- }) => {
190
- let nextBlock;
191
- const selectionEndBlock = getSelectionEndBlock({
192
- context
193
- });
194
- if (!selectionEndBlock)
195
- return;
196
- let foundSelectionEndBlock = !1;
197
- for (const block of context.value) {
198
- if (block._key === selectionEndBlock.node._key) {
199
- foundSelectionEndBlock = !0;
200
- continue;
201
- }
202
- if (foundSelectionEndBlock) {
203
- nextBlock = {
204
- node: block,
205
- path: [{
206
- _key: block._key
207
- }]
208
- };
209
- break;
210
- }
211
- }
212
- if (foundSelectionEndBlock && nextBlock)
213
- return nextBlock;
214
- };
1
+ import { isPortableTextTextBlock, isPortableTextSpan } from "@sanity/types";
215
2
  function getStartPoint({
216
3
  node,
217
4
  path
@@ -274,7 +61,16 @@ const getSelectionText = ({
274
61
  break;
275
62
  }
276
63
  return text;
277
- }, getBlockTextBefore = ({
64
+ };
65
+ var __defProp = Object.defineProperty, __defProps = Object.defineProperties, __getOwnPropDescs = Object.getOwnPropertyDescriptors, __getOwnPropSymbols = Object.getOwnPropertySymbols, __hasOwnProp = Object.prototype.hasOwnProperty, __propIsEnum = Object.prototype.propertyIsEnumerable, __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues = (a, b) => {
66
+ for (var prop in b || (b = {}))
67
+ __hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
68
+ if (__getOwnPropSymbols)
69
+ for (var prop of __getOwnPropSymbols(b))
70
+ __propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
71
+ return a;
72
+ }, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
73
+ const getBlockTextBefore = ({
278
74
  context
279
75
  }) => {
280
76
  if (!context.selection)
@@ -289,33 +85,17 @@ const getSelectionText = ({
289
85
  }]
290
86
  });
291
87
  return getSelectionText({
292
- context: {
293
- ...context,
88
+ context: __spreadProps(__spreadValues({}, context), {
294
89
  value: context.value,
295
90
  selection: {
296
91
  anchor: startOfBlock,
297
92
  focus: point
298
93
  }
299
- }
94
+ })
300
95
  });
301
96
  };
302
97
  export {
303
- createGuards,
304
98
  getBlockTextBefore,
305
- getFirstBlock,
306
- getFocusBlock,
307
- getFocusBlockObject,
308
- getFocusChild,
309
- getFocusListBlock,
310
- getFocusSpan,
311
- getFocusTextBlock,
312
- getLastBlock,
313
- getNextBlock,
314
- getPreviousBlock,
315
- getSelectedBlocks,
316
- getSelectionEndBlock,
317
- getSelectionStartBlock,
318
- getSelectionText,
319
- selectionIsCollapsed
99
+ getSelectionText
320
100
  };
321
101
  //# sourceMappingURL=selector.get-text-before.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"selector.get-text-before.js","sources":["../../src/editor/behavior/behavior.guards.ts","../../src/selectors/selectors.ts","../../src/editor/utils/utils.get-start-point.ts","../../src/editor/utils/utils.is-keyed-segment.ts","../../src/editor/utils/utils.reverse-selection.ts","../../src/selectors/selector.get-selection-text.ts","../../src/selectors/selector.get-text-before.ts"],"sourcesContent":["import {\n isPortableTextListBlock,\n isPortableTextTextBlock,\n type PortableTextListBlock,\n type PortableTextTextBlock,\n} from '@sanity/types'\nimport type {PortableTextMemberSchemaTypes} from '../../types/editor'\n\n/**\n * @alpha\n */\nexport type BehaviorGuards = ReturnType<typeof createGuards>\n\nexport function createGuards({\n schema,\n}: {\n schema: PortableTextMemberSchemaTypes\n}) {\n function isListBlock(block: unknown): block is PortableTextListBlock {\n return isPortableTextListBlock(block) && block._type === schema.block.name\n }\n\n function isTextBlock(block: unknown): block is PortableTextTextBlock {\n return isPortableTextTextBlock(block) && block._type === schema.block.name\n }\n\n return {isListBlock, isTextBlock}\n}\n","import {\n isKeySegment,\n isPortableTextSpan,\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n type PortableTextListBlock,\n type PortableTextObject,\n type PortableTextSpan,\n type PortableTextTextBlock,\n} from '@sanity/types'\nimport {createGuards} from '../editor/behavior/behavior.guards'\nimport type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @alpha\n */\nexport const selectionIsCollapsed: EditorSelector<boolean> = ({context}) => {\n return (\n JSON.stringify(context.selection?.anchor.path) ===\n JSON.stringify(context.selection?.focus.path) &&\n context.selection?.anchor.offset === context.selection?.focus.offset\n )\n}\n\n/**\n * @alpha\n */\nexport const getFocusBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const key = context.selection\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusListBlock: EditorSelector<\n {node: PortableTextListBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const guards = createGuards(context)\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && guards.isListBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusTextBlock: EditorSelector<\n {node: PortableTextTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && isPortableTextTextBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusBlockObject: EditorSelector<\n {node: PortableTextObject; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && !isPortableTextTextBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusChild: EditorSelector<\n | {\n node: PortableTextObject | PortableTextSpan\n path: [KeyedSegment, 'children', KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n const focusBlock = getFocusTextBlock({context})\n\n if (!focusBlock) {\n return undefined\n }\n\n const key = context.selection\n ? isKeySegment(context.selection.focus.path[2])\n ? context.selection.focus.path[2]._key\n : undefined\n : undefined\n\n const node = key\n ? focusBlock.node.children.find((span) => span._key === key)\n : undefined\n\n return node && key\n ? {node, path: [...focusBlock.path, 'children', {_key: key}]}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusSpan: EditorSelector<\n | {node: PortableTextSpan; path: [KeyedSegment, 'children', KeyedSegment]}\n | undefined\n> = ({context}) => {\n const focusChild = getFocusChild({context})\n\n return focusChild && isPortableTextSpan(focusChild.node)\n ? {node: focusChild.node, path: focusChild.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFirstBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const node = context.value[0]\n\n return node ? {node, path: [{_key: node._key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getLastBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const node = context.value[context.value.length - 1]\n ? context.value[context.value.length - 1]\n : undefined\n\n return node ? {node, path: [{_key: node._key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getSelectedBlocks: EditorSelector<\n Array<{node: PortableTextBlock; path: [KeyedSegment]}>\n> = ({context}) => {\n if (!context.selection) {\n return []\n }\n\n const selectedBlocks: Array<{node: PortableTextBlock; path: [KeyedSegment]}> =\n []\n const startKey = context.selection.backward\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n const endKey = context.selection.backward\n ? isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n : isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n\n if (!startKey || !endKey) {\n return selectedBlocks\n }\n\n for (const block of context.value) {\n if (block._key === startKey) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n\n if (startKey === endKey) {\n break\n }\n continue\n }\n\n if (block._key === endKey) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n break\n }\n\n if (selectedBlocks.length > 0) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n }\n }\n\n return selectedBlocks\n}\n\n/**\n * @alpha\n */\nexport const getSelectionStartBlock: EditorSelector<\n | {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n if (!context.selection) {\n return undefined\n }\n\n const key = context.selection.backward\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getSelectionEndBlock: EditorSelector<\n | {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n if (!context.selection) {\n return undefined\n }\n\n const key = context.selection.backward\n ? isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n : isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getPreviousBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n let previousBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n const selectionStartBlock = getSelectionStartBlock({context})\n\n if (!selectionStartBlock) {\n return undefined\n }\n\n let foundSelectionStartBlock = false\n\n for (const block of context.value) {\n if (block._key === selectionStartBlock.node._key) {\n foundSelectionStartBlock = true\n break\n }\n\n previousBlock = {node: block, path: [{_key: block._key}]}\n }\n\n if (foundSelectionStartBlock && previousBlock) {\n return previousBlock\n }\n\n return undefined\n}\n\n/**\n * @alpha\n */\nexport const getNextBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n let nextBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n const selectionEndBlock = getSelectionEndBlock({context})\n\n if (!selectionEndBlock) {\n return undefined\n }\n\n let foundSelectionEndBlock = false\n\n for (const block of context.value) {\n if (block._key === selectionEndBlock.node._key) {\n foundSelectionEndBlock = true\n continue\n }\n\n if (foundSelectionEndBlock) {\n nextBlock = {node: block, path: [{_key: block._key}]}\n break\n }\n }\n\n if (foundSelectionEndBlock && nextBlock) {\n return nextBlock\n }\n\n return undefined\n}\n","import {\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelectionPoint} from '../../types/editor'\n\nexport function getStartPoint({\n node,\n path,\n}: {\n node: PortableTextBlock\n path: [KeyedSegment]\n}): EditorSelectionPoint {\n if (isPortableTextTextBlock(node)) {\n return {\n path: [...path, 'children', {_key: node.children[0]._key}],\n offset: 0,\n }\n }\n\n return {\n path,\n offset: 0,\n }\n}\n","import type {KeyedSegment, PathSegment} from '@sanity/types'\n\nexport function isKeyedSegment(segment: PathSegment): segment is KeyedSegment {\n return typeof segment === 'object' && segment !== null && '_key' in segment\n}\n","import type {EditorSelection} from '../../types/editor'\n\nexport function reverseSelection(\n selection: NonNullable<EditorSelection>,\n): NonNullable<EditorSelection> {\n if (selection.backward) {\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: false,\n }\n }\n\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: true,\n }\n}\n","import {isPortableTextSpan, isPortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {isKeyedSegment} from '../editor/utils/utils.is-keyed-segment'\nimport {reverseSelection} from '../editor/utils/utils.reverse-selection'\n\n/**\n * @alpha\n */\nexport const getSelectionText: EditorSelector<string> = ({context}) => {\n let text = ''\n\n const {value, selection} = context\n\n if (!value || !selection) {\n return text\n }\n\n const forwardSelection = selection.backward\n ? reverseSelection(selection)\n : selection\n\n if (!forwardSelection) {\n return text\n }\n\n for (const block of value) {\n if (\n isKeyedSegment(forwardSelection.anchor.path[0]) &&\n block._key !== forwardSelection.anchor.path[0]._key\n ) {\n continue\n }\n\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n for (const child of block.children) {\n if (isPortableTextSpan(child)) {\n if (\n isKeyedSegment(forwardSelection.anchor.path[2]) &&\n child._key === forwardSelection.anchor.path[2]._key &&\n isKeyedSegment(forwardSelection.focus.path[2]) &&\n child._key === forwardSelection.focus.path[2]._key\n ) {\n text =\n text +\n child.text.slice(\n forwardSelection.anchor.offset,\n forwardSelection.focus.offset,\n )\n\n break\n }\n\n if (\n isKeyedSegment(forwardSelection.anchor.path[2]) &&\n child._key === forwardSelection.anchor.path[2]._key\n ) {\n text = text + child.text.slice(forwardSelection.anchor.offset)\n continue\n }\n\n if (\n isKeyedSegment(forwardSelection.focus.path[2]) &&\n child._key === forwardSelection.focus.path[2]._key\n ) {\n text = text + child.text.slice(0, forwardSelection.focus.offset)\n break\n }\n\n if (text.length > 0) {\n text + child.text\n }\n }\n }\n\n if (\n isKeyedSegment(forwardSelection.focus.path[0]) &&\n block._key === forwardSelection.focus.path[0]._key\n ) {\n break\n }\n }\n\n return text\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {getStartPoint} from '../editor/utils/utils.get-start-point'\nimport {isKeyedSegment} from '../editor/utils/utils.is-keyed-segment'\nimport {reverseSelection} from '../editor/utils/utils.reverse-selection'\nimport {getSelectionText} from './selector.get-selection-text'\n\n/**\n * @alpha\n */\nexport const getBlockTextBefore: EditorSelector<string> = ({context}) => {\n if (!context.selection) {\n return ''\n }\n\n const selection = context.selection.backward\n ? reverseSelection(context.selection)\n : context.selection\n const point = selection.anchor\n const key = isKeyedSegment(point.path[0]) ? point.path[0]._key : undefined\n\n const block = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n if (!block) {\n return ''\n }\n\n const startOfBlock = getStartPoint({node: block, path: [{_key: block._key}]})\n\n return getSelectionText({\n context: {\n ...context,\n value: context.value,\n selection: {\n anchor: startOfBlock,\n focus: point,\n },\n },\n })\n}\n"],"names":["createGuards","schema","isListBlock","block","isPortableTextListBlock","_type","name","isTextBlock","isPortableTextTextBlock","selectionIsCollapsed","context","JSON","stringify","selection","anchor","path","focus","offset","getFocusBlock","key","isKeySegment","_key","undefined","node","value","find","getFocusListBlock","guards","focusBlock","getFocusTextBlock","getFocusBlockObject","getFocusChild","children","span","getFocusSpan","focusChild","isPortableTextSpan","getFirstBlock","getLastBlock","length","getSelectedBlocks","selectedBlocks","startKey","backward","endKey","push","getSelectionStartBlock","getSelectionEndBlock","getPreviousBlock","previousBlock","selectionStartBlock","foundSelectionStartBlock","getNextBlock","nextBlock","selectionEndBlock","foundSelectionEndBlock","getStartPoint","isKeyedSegment","segment","reverseSelection","getSelectionText","text","forwardSelection","child","slice","getBlockTextBefore","point","startOfBlock"],"mappings":";AAaO,SAASA,aAAa;AAAA,EAC3BC;AAGF,GAAG;AACD,WAASC,YAAYC,OAAgD;AACnE,WAAOC,wBAAwBD,KAAK,KAAKA,MAAME,UAAUJ,OAAOE,MAAMG;AAAAA,EAAAA;AAGxE,WAASC,YAAYJ,OAAgD;AACnE,WAAOK,wBAAwBL,KAAK,KAAKA,MAAME,UAAUJ,OAAOE,MAAMG;AAAAA,EAAAA;AAGjE,SAAA;AAAA,IAACJ;AAAAA,IAAaK;AAAAA,EAAW;AAClC;ACVO,MAAME,uBAAgDA,CAAC;AAAA,EAACC;AAAO,MAElEC,KAAKC,UAAUF,QAAQG,WAAWC,OAAOC,IAAI,MAC3CJ,KAAKC,UAAUF,QAAQG,WAAWG,MAAMD,IAAI,KAC9CL,QAAQG,WAAWC,OAAOG,WAAWP,QAAQG,WAAWG,MAAMC,QAOrDC,gBAETA,CAAC;AAAA,EAACR;AAAO,MAAM;AACjB,QAAMS,MAAMT,QAAQG,aAChBO,aAAaV,QAAQG,UAAUG,MAAMD,KAAK,CAAC,CAAC,IAC1CL,QAAQG,UAAUG,MAAMD,KAAK,CAAC,EAAEM,OAElCC,QAEEC,OAAOJ,MACTT,QAAQc,MAAMC,KAAMtB,CAAUA,UAAAA,MAAMkB,SAASF,GAAG,IAChDG;AAEJ,SAAOC,QAAQJ,MAAM;AAAA,IAACI;AAAAA,IAAMR,MAAM,CAAC;AAAA,MAACM,MAAMF;AAAAA,IAAI,CAAA;AAAA,EAAKG,IAAAA;AACrD,GAKaI,oBAETA,CAAC;AAAA,EAAChB;AAAO,MAAM;AACjB,QAAMiB,SAAS3B,aAAaU,OAAO,GAC7BkB,aAAaV,cAAc;AAAA,IAACR;AAAAA,EAAAA,CAAQ;AAE1C,SAAOkB,cAAcD,OAAOzB,YAAY0B,WAAWL,IAAI,IACnD;AAAA,IAACA,MAAMK,WAAWL;AAAAA,IAAMR,MA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,MAAI,CAACA,QAAQG;AACJ,WAAA;AAMT,QAAMqD,SAHYxD,QAAQG,UAAU8B,WAChCgB,iBAAiBjD,QAAQG,SAAS,IAClCH,QAAQG,WACYC,QAClBK,MAAMsC,eAAeS,MAAMnD,KAAK,CAAC,CAAC,IAAImD,MAAMnD,KAAK,CAAC,EAAEM,OAAOC,QAE3DnB,QAAQgB,MACVT,QAAQc,MAAMC,KAAMtB,CAAAA,WAAUA,OAAMkB,SAASF,GAAG,IAChDG;AAEJ,MAAI,CAACnB;AACI,WAAA;AAGT,QAAMgE,eAAeX,cAAc;AAAA,IAACjC,MAAMpB;AAAAA,IAAOY,MAAM,CAAC;AAAA,MAACM,MAAMlB,MAAMkB;AAAAA,IAAK,CAAA;AAAA,EAAA,CAAE;AAE5E,SAAOuC,iBAAiB;AAAA,IACtBlD,SAAS;AAAA,MACP,GAAGA;AAAAA,MACHc,OAAOd,QAAQc;AAAAA,MACfX,WAAW;AAAA,QACTC,QAAQqD;AAAAA,QACRnD,OAAOkD;AAAAA,MAAAA;AAAAA,IACT;AAAA,EACF,CACD;AACH;"}
1
+ {"version":3,"file":"selector.get-text-before.js","sources":["../../src/editor/utils/utils.get-start-point.ts","../../src/editor/utils/utils.is-keyed-segment.ts","../../src/editor/utils/utils.reverse-selection.ts","../../src/selectors/selector.get-selection-text.ts","../../src/selectors/selector.get-text-before.ts"],"sourcesContent":["import {\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelectionPoint} from '../../types/editor'\n\nexport function getStartPoint({\n node,\n path,\n}: {\n node: PortableTextBlock\n path: [KeyedSegment]\n}): EditorSelectionPoint {\n if (isPortableTextTextBlock(node)) {\n return {\n path: [...path, 'children', {_key: node.children[0]._key}],\n offset: 0,\n }\n }\n\n return {\n path,\n offset: 0,\n }\n}\n","import type {KeyedSegment, PathSegment} from '@sanity/types'\n\nexport function isKeyedSegment(segment: PathSegment): segment is KeyedSegment {\n return typeof segment === 'object' && segment !== null && '_key' in segment\n}\n","import type {EditorSelection} from '../../types/editor'\n\nexport function reverseSelection(\n selection: NonNullable<EditorSelection>,\n): NonNullable<EditorSelection> {\n if (selection.backward) {\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: false,\n }\n }\n\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: true,\n }\n}\n","import {isPortableTextSpan, isPortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {isKeyedSegment} from '../editor/utils/utils.is-keyed-segment'\nimport {reverseSelection} from '../editor/utils/utils.reverse-selection'\n\n/**\n * @alpha\n */\nexport const getSelectionText: EditorSelector<string> = ({context}) => {\n let text = ''\n\n const {value, selection} = context\n\n if (!value || !selection) {\n return text\n }\n\n const forwardSelection = selection.backward\n ? reverseSelection(selection)\n : selection\n\n if (!forwardSelection) {\n return text\n }\n\n for (const block of value) {\n if (\n isKeyedSegment(forwardSelection.anchor.path[0]) &&\n block._key !== forwardSelection.anchor.path[0]._key\n ) {\n continue\n }\n\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n for (const child of block.children) {\n if (isPortableTextSpan(child)) {\n if (\n isKeyedSegment(forwardSelection.anchor.path[2]) &&\n child._key === forwardSelection.anchor.path[2]._key &&\n isKeyedSegment(forwardSelection.focus.path[2]) &&\n child._key === forwardSelection.focus.path[2]._key\n ) {\n text =\n text +\n child.text.slice(\n forwardSelection.anchor.offset,\n forwardSelection.focus.offset,\n )\n\n break\n }\n\n if (\n isKeyedSegment(forwardSelection.anchor.path[2]) &&\n child._key === forwardSelection.anchor.path[2]._key\n ) {\n text = text + child.text.slice(forwardSelection.anchor.offset)\n continue\n }\n\n if (\n isKeyedSegment(forwardSelection.focus.path[2]) &&\n child._key === forwardSelection.focus.path[2]._key\n ) {\n text = text + child.text.slice(0, forwardSelection.focus.offset)\n break\n }\n\n if (text.length > 0) {\n text + child.text\n }\n }\n }\n\n if (\n isKeyedSegment(forwardSelection.focus.path[0]) &&\n block._key === forwardSelection.focus.path[0]._key\n ) {\n break\n }\n }\n\n return text\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {getStartPoint} from '../editor/utils/utils.get-start-point'\nimport {isKeyedSegment} from '../editor/utils/utils.is-keyed-segment'\nimport {reverseSelection} from '../editor/utils/utils.reverse-selection'\nimport {getSelectionText} from './selector.get-selection-text'\n\n/**\n * @alpha\n */\nexport const getBlockTextBefore: EditorSelector<string> = ({context}) => {\n if (!context.selection) {\n return ''\n }\n\n const selection = context.selection.backward\n ? reverseSelection(context.selection)\n : context.selection\n const point = selection.anchor\n const key = isKeyedSegment(point.path[0]) ? point.path[0]._key : undefined\n\n const block = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n if (!block) {\n return ''\n }\n\n const startOfBlock = getStartPoint({node: block, path: [{_key: block._key}]})\n\n return getSelectionText({\n context: {\n ...context,\n value: context.value,\n selection: {\n anchor: startOfBlock,\n focus: point,\n },\n },\n })\n}\n"],"names":["getStartPoint","node","path","isPortableTextTextBlock","_key","children","offset","isKeyedSegment","segment","reverseSelection","selection","backward","anchor","focus","getSelectionText","context","text","value","forwardSelection","block","child","isPortableTextSpan","slice","getBlockTextBefore","point","key","undefined","find","startOfBlock"],"mappings":";AAOO,SAASA,cAAc;AAAA,EAC5BC;AAAAA,EACAC;AAIF,GAAyB;AACnBC,SAAAA,wBAAwBF,IAAI,IACvB;AAAA,IACLC,MAAM,CAAC,GAAGA,MAAM,YAAY;AAAA,MAACE,MAAMH,KAAKI,SAAS,CAAC,EAAED;AAAAA,IAAAA,CAAK;AAAA,IACzDE,QAAQ;AAAA,EAAA,IAIL;AAAA,IACLJ;AAAAA,IACAI,QAAQ;AAAA,EACV;AACF;ACvBO,SAASC,eAAeC,SAA+C;AAC5E,SAAO,OAAOA,WAAY,YAAYA,YAAY,QAAQ,UAAUA;AACtE;ACFO,SAASC,iBACdC,WAC8B;AAC9B,SAAIA,UAAUC,WACL;AAAA,IACLC,QAAQF,UAAUG;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iBAAiBL,MAAMX,KAAK,CAAC,CAAC,KAC7CkB,MAAMhB,SAASc,iBAAiBL,MAAMX,KAAK,CAAC,EAAEE,MAC9C;AACAY,mBAAOA,OAAOI,MAAMJ,KAAKM,MAAM,GAAGJ,iBAAiBL,MAAMP,MAAM;AAC/D;AAAA,UAAA;AAAA,QAIaU;AAKnB,UACET,eAAeW,iBAAiBL,MAAMX,KAAK,CAAC,CAAC,KAC7CiB,MAAMf,SAASc,iBAAiBL,MAAMX,KAAK,CAAC,EAAEE;AAE9C;AAAA,IAAA;AAIGY,SAAAA;AACT;;;;;;;;;AC7EO,MAAMO,qBAA6CA,CAAC;AAAA,EAACR;AAAO,MAAM;AACvE,MAAI,CAACA,QAAQL;AACJ,WAAA;AAMT,QAAMc,SAHYT,QAAQL,UAAUC,WAChCF,iBAAiBM,QAAQL,SAAS,IAClCK,QAAQL,WACYE,QAClBa,MAAMlB,eAAeiB,MAAMtB,KAAK,CAAC,CAAC,IAAIsB,MAAMtB,KAAK,CAAC,EAAEE,OAAOsB,QAE3DP,QAAQM,MACVV,QAAQE,MAAMU,KAAMR,CAAAA,WAAUA,OAAMf,SAASqB,GAAG,IAChDC;AAEJ,MAAI,CAACP;AACI,WAAA;AAGT,QAAMS,eAAe5B,cAAc;AAAA,IAACC,MAAMkB;AAAAA,IAAOjB,MAAM,CAAC;AAAA,MAACE,MAAMe,MAAMf;AAAAA,IAAK,CAAA;AAAA,EAAA,CAAE;AAE5E,SAAOU,iBAAiB;AAAA,IACtBC,SAAS,iCACJA,OADI,GAAA;AAAA,MAEPE,OAAOF,QAAQE;AAAAA,MACfP,WAAW;AAAA,QACTE,QAAQgB;AAAAA,QACRf,OAAOW;AAAAA,MAAAA;AAAAA,IAEX,CAAA;AAAA,EAAA,CACD;AACH;"}
@@ -0,0 +1,235 @@
1
+ import { isPortableTextListBlock, isPortableTextTextBlock, isKeySegment, isPortableTextSpan } from "@sanity/types";
2
+ function createGuards({
3
+ schema
4
+ }) {
5
+ function isListBlock(block) {
6
+ return isPortableTextListBlock(block) && block._type === schema.block.name;
7
+ }
8
+ function isTextBlock(block) {
9
+ return isPortableTextTextBlock(block) && block._type === schema.block.name;
10
+ }
11
+ return {
12
+ isListBlock,
13
+ isTextBlock
14
+ };
15
+ }
16
+ const selectionIsCollapsed = ({
17
+ context
18
+ }) => {
19
+ var _a, _b, _c, _d;
20
+ return JSON.stringify((_a = context.selection) == null ? void 0 : _a.anchor.path) === JSON.stringify((_b = context.selection) == null ? void 0 : _b.focus.path) && ((_c = context.selection) == null ? void 0 : _c.anchor.offset) === ((_d = context.selection) == null ? void 0 : _d.focus.offset);
21
+ }, getFocusBlock = ({
22
+ context
23
+ }) => {
24
+ const key = context.selection && isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
25
+ return node && key ? {
26
+ node,
27
+ path: [{
28
+ _key: key
29
+ }]
30
+ } : void 0;
31
+ }, getFocusListBlock = ({
32
+ context
33
+ }) => {
34
+ const guards = createGuards(context), focusBlock = getFocusBlock({
35
+ context
36
+ });
37
+ return focusBlock && guards.isListBlock(focusBlock.node) ? {
38
+ node: focusBlock.node,
39
+ path: focusBlock.path
40
+ } : void 0;
41
+ }, getFocusTextBlock = ({
42
+ context
43
+ }) => {
44
+ const focusBlock = getFocusBlock({
45
+ context
46
+ });
47
+ return focusBlock && isPortableTextTextBlock(focusBlock.node) ? {
48
+ node: focusBlock.node,
49
+ path: focusBlock.path
50
+ } : void 0;
51
+ }, getFocusBlockObject = ({
52
+ context
53
+ }) => {
54
+ const focusBlock = getFocusBlock({
55
+ context
56
+ });
57
+ return focusBlock && !isPortableTextTextBlock(focusBlock.node) ? {
58
+ node: focusBlock.node,
59
+ path: focusBlock.path
60
+ } : void 0;
61
+ }, getFocusChild = ({
62
+ context
63
+ }) => {
64
+ const focusBlock = getFocusTextBlock({
65
+ context
66
+ });
67
+ if (!focusBlock)
68
+ return;
69
+ const key = context.selection && isKeySegment(context.selection.focus.path[2]) ? context.selection.focus.path[2]._key : void 0, node = key ? focusBlock.node.children.find((span) => span._key === key) : void 0;
70
+ return node && key ? {
71
+ node,
72
+ path: [...focusBlock.path, "children", {
73
+ _key: key
74
+ }]
75
+ } : void 0;
76
+ }, getFocusSpan = ({
77
+ context
78
+ }) => {
79
+ const focusChild = getFocusChild({
80
+ context
81
+ });
82
+ return focusChild && isPortableTextSpan(focusChild.node) ? {
83
+ node: focusChild.node,
84
+ path: focusChild.path
85
+ } : void 0;
86
+ }, getFirstBlock = ({
87
+ context
88
+ }) => {
89
+ const node = context.value[0];
90
+ return node ? {
91
+ node,
92
+ path: [{
93
+ _key: node._key
94
+ }]
95
+ } : void 0;
96
+ }, getLastBlock = ({
97
+ context
98
+ }) => {
99
+ const node = context.value[context.value.length - 1] ? context.value[context.value.length - 1] : void 0;
100
+ return node ? {
101
+ node,
102
+ path: [{
103
+ _key: node._key
104
+ }]
105
+ } : void 0;
106
+ }, getSelectedBlocks = ({
107
+ context
108
+ }) => {
109
+ if (!context.selection)
110
+ return [];
111
+ const selectedBlocks = [], startKey = context.selection.backward ? isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, endKey = context.selection.backward ? isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0;
112
+ if (!startKey || !endKey)
113
+ return selectedBlocks;
114
+ for (const block of context.value) {
115
+ if (block._key === startKey) {
116
+ if (selectedBlocks.push({
117
+ node: block,
118
+ path: [{
119
+ _key: block._key
120
+ }]
121
+ }), startKey === endKey)
122
+ break;
123
+ continue;
124
+ }
125
+ if (block._key === endKey) {
126
+ selectedBlocks.push({
127
+ node: block,
128
+ path: [{
129
+ _key: block._key
130
+ }]
131
+ });
132
+ break;
133
+ }
134
+ selectedBlocks.length > 0 && selectedBlocks.push({
135
+ node: block,
136
+ path: [{
137
+ _key: block._key
138
+ }]
139
+ });
140
+ }
141
+ return selectedBlocks;
142
+ }, getSelectionStartBlock = ({
143
+ context
144
+ }) => {
145
+ if (!context.selection)
146
+ return;
147
+ const key = context.selection.backward ? isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
148
+ return node && key ? {
149
+ node,
150
+ path: [{
151
+ _key: key
152
+ }]
153
+ } : void 0;
154
+ }, getSelectionEndBlock = ({
155
+ context
156
+ }) => {
157
+ if (!context.selection)
158
+ return;
159
+ const key = context.selection.backward ? isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
160
+ return node && key ? {
161
+ node,
162
+ path: [{
163
+ _key: key
164
+ }]
165
+ } : void 0;
166
+ }, getPreviousBlock = ({
167
+ context
168
+ }) => {
169
+ let previousBlock;
170
+ const selectionStartBlock = getSelectionStartBlock({
171
+ context
172
+ });
173
+ if (!selectionStartBlock)
174
+ return;
175
+ let foundSelectionStartBlock = !1;
176
+ for (const block of context.value) {
177
+ if (block._key === selectionStartBlock.node._key) {
178
+ foundSelectionStartBlock = !0;
179
+ break;
180
+ }
181
+ previousBlock = {
182
+ node: block,
183
+ path: [{
184
+ _key: block._key
185
+ }]
186
+ };
187
+ }
188
+ if (foundSelectionStartBlock && previousBlock)
189
+ return previousBlock;
190
+ }, getNextBlock = ({
191
+ context
192
+ }) => {
193
+ let nextBlock;
194
+ const selectionEndBlock = getSelectionEndBlock({
195
+ context
196
+ });
197
+ if (!selectionEndBlock)
198
+ return;
199
+ let foundSelectionEndBlock = !1;
200
+ for (const block of context.value) {
201
+ if (block._key === selectionEndBlock.node._key) {
202
+ foundSelectionEndBlock = !0;
203
+ continue;
204
+ }
205
+ if (foundSelectionEndBlock) {
206
+ nextBlock = {
207
+ node: block,
208
+ path: [{
209
+ _key: block._key
210
+ }]
211
+ };
212
+ break;
213
+ }
214
+ }
215
+ if (foundSelectionEndBlock && nextBlock)
216
+ return nextBlock;
217
+ };
218
+ export {
219
+ createGuards,
220
+ getFirstBlock,
221
+ getFocusBlock,
222
+ getFocusBlockObject,
223
+ getFocusChild,
224
+ getFocusListBlock,
225
+ getFocusSpan,
226
+ getFocusTextBlock,
227
+ getLastBlock,
228
+ getNextBlock,
229
+ getPreviousBlock,
230
+ getSelectedBlocks,
231
+ getSelectionEndBlock,
232
+ getSelectionStartBlock,
233
+ selectionIsCollapsed
234
+ };
235
+ //# sourceMappingURL=selectors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selectors.js","sources":["../../src/behavior-actions/behavior.guards.ts","../../src/selectors/selectors.ts"],"sourcesContent":["import {\n isPortableTextListBlock,\n isPortableTextTextBlock,\n type PortableTextListBlock,\n type PortableTextTextBlock,\n} from '@sanity/types'\nimport type {PortableTextMemberSchemaTypes} from '../types/editor'\n\n/**\n * @alpha\n */\nexport type BehaviorGuards = ReturnType<typeof createGuards>\n\nexport function createGuards({\n schema,\n}: {\n schema: PortableTextMemberSchemaTypes\n}) {\n function isListBlock(block: unknown): block is PortableTextListBlock {\n return isPortableTextListBlock(block) && block._type === schema.block.name\n }\n\n function isTextBlock(block: unknown): block is PortableTextTextBlock {\n return isPortableTextTextBlock(block) && block._type === schema.block.name\n }\n\n return {isListBlock, isTextBlock}\n}\n","import {\n isKeySegment,\n isPortableTextSpan,\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n type PortableTextListBlock,\n type PortableTextObject,\n type PortableTextSpan,\n type PortableTextTextBlock,\n} from '@sanity/types'\nimport {createGuards} from '../behavior-actions/behavior.guards'\nimport type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @alpha\n */\nexport const selectionIsCollapsed: EditorSelector<boolean> = ({context}) => {\n return (\n JSON.stringify(context.selection?.anchor.path) ===\n JSON.stringify(context.selection?.focus.path) &&\n context.selection?.anchor.offset === context.selection?.focus.offset\n )\n}\n\n/**\n * @alpha\n */\nexport const getFocusBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const key = context.selection\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusListBlock: EditorSelector<\n {node: PortableTextListBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const guards = createGuards(context)\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && guards.isListBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusTextBlock: EditorSelector<\n {node: PortableTextTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && isPortableTextTextBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusBlockObject: EditorSelector<\n {node: PortableTextObject; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && !isPortableTextTextBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusChild: EditorSelector<\n | {\n node: PortableTextObject | PortableTextSpan\n path: [KeyedSegment, 'children', KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n const focusBlock = getFocusTextBlock({context})\n\n if (!focusBlock) {\n return undefined\n }\n\n const key = context.selection\n ? isKeySegment(context.selection.focus.path[2])\n ? context.selection.focus.path[2]._key\n : undefined\n : undefined\n\n const node = key\n ? focusBlock.node.children.find((span) => span._key === key)\n : undefined\n\n return node && key\n ? {node, path: [...focusBlock.path, 'children', {_key: key}]}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusSpan: EditorSelector<\n | {node: PortableTextSpan; path: [KeyedSegment, 'children', KeyedSegment]}\n | undefined\n> = ({context}) => {\n const focusChild = getFocusChild({context})\n\n return focusChild && isPortableTextSpan(focusChild.node)\n ? {node: focusChild.node, path: focusChild.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFirstBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const node = context.value[0]\n\n return node ? {node, path: [{_key: node._key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getLastBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const node = context.value[context.value.length - 1]\n ? context.value[context.value.length - 1]\n : undefined\n\n return node ? {node, path: [{_key: node._key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getSelectedBlocks: EditorSelector<\n Array<{node: PortableTextBlock; path: [KeyedSegment]}>\n> = ({context}) => {\n if (!context.selection) {\n return []\n }\n\n const selectedBlocks: Array<{node: PortableTextBlock; path: [KeyedSegment]}> =\n []\n const startKey = context.selection.backward\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n const endKey = context.selection.backward\n ? isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n : isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n\n if (!startKey || !endKey) {\n return selectedBlocks\n }\n\n for (const block of context.value) {\n if (block._key === startKey) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n\n if (startKey === endKey) {\n break\n }\n continue\n }\n\n if (block._key === endKey) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n break\n }\n\n if (selectedBlocks.length > 0) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n }\n }\n\n return selectedBlocks\n}\n\n/**\n * @alpha\n */\nexport const getSelectionStartBlock: EditorSelector<\n | {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n if (!context.selection) {\n return undefined\n }\n\n const key = context.selection.backward\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getSelectionEndBlock: EditorSelector<\n | {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n if (!context.selection) {\n return undefined\n }\n\n const key = context.selection.backward\n ? isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n : isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getPreviousBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n let previousBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n const selectionStartBlock = getSelectionStartBlock({context})\n\n if (!selectionStartBlock) {\n return undefined\n }\n\n let foundSelectionStartBlock = false\n\n for (const block of context.value) {\n if (block._key === selectionStartBlock.node._key) {\n foundSelectionStartBlock = true\n break\n }\n\n previousBlock = {node: block, path: [{_key: block._key}]}\n }\n\n if (foundSelectionStartBlock && previousBlock) {\n return previousBlock\n }\n\n return undefined\n}\n\n/**\n * @alpha\n */\nexport const getNextBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n let nextBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n const selectionEndBlock = getSelectionEndBlock({context})\n\n if (!selectionEndBlock) {\n return undefined\n }\n\n let foundSelectionEndBlock = false\n\n for (const block of context.value) {\n if (block._key === selectionEndBlock.node._key) {\n foundSelectionEndBlock = true\n continue\n }\n\n if (foundSelectionEndBlock) {\n nextBlock = {node: block, path: [{_key: block._key}]}\n break\n }\n }\n\n if (foundSelectionEndBlock && nextBlock) {\n return nextBlock\n }\n\n return undefined\n}\n"],"names":["createGuards","schema","isListBlock","block","isPortableTextListBlock","_type","name","isTextBlock","isPortableTextTextBlock","selectionIsCollapsed","context","JSON","stringify","selection","anchor","path","focus","offset","getFocusBlock","key","isKeySegment","_key","undefined","node","value","find","getFocusListBlock","guards","focusBlock","getFocusTextBlock","getFocusBlockObject","getFocusChild","children","span","getFocusSpan","focusChild","isPortableTextSpan","getFirstBlock","getLastBlock","length","getSelectedBlocks","selectedBlocks","startKey","backward","endKey","push","getSelectionStartBlock","getSelectionEndBlock","getPreviousBlock","previousBlock","selectionStartBlock","foundSelectionStartBlock","getNextBlock","nextBlock","selectionEndBlock","foundSelectionEndBlock"],"mappings":";AAaO,SAASA,aAAa;AAAA,EAC3BC;AAGF,GAAG;AACD,WAASC,YAAYC,OAAgD;AACnE,WAAOC,wBAAwBD,KAAK,KAAKA,MAAME,UAAUJ,OAAOE,MAAMG;AAAAA,EAAAA;AAGxE,WAASC,YAAYJ,OAAgD;AACnE,WAAOK,wBAAwBL,KAAK,KAAKA,MAAME,UAAUJ,OAAOE,MAAMG;AAAAA,EAAAA;AAGjE,SAAA;AAAA,IAACJ;AAAAA,IAAaK;AAAAA,EAAW;AAClC;ACVO,MAAME,uBAAgDA,CAAC;AAAA,EAACC;AAAO,MAAM;AAjB5E,MAAA,IAAA,IAAA,IAAA;AAmBIC,SAAAA,KAAKC,WAAUF,KAAAA,QAAQG,cAARH,OAAAA,SAAAA,GAAmBI,OAAOC,IAAI,MA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