@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,218 +1,5 @@
1
1
  "use strict";
2
2
  var types = require("@sanity/types");
3
- function createGuards({
4
- schema
5
- }) {
6
- function isListBlock(block) {
7
- return types.isPortableTextListBlock(block) && block._type === schema.block.name;
8
- }
9
- function isTextBlock(block) {
10
- return types.isPortableTextTextBlock(block) && block._type === schema.block.name;
11
- }
12
- return {
13
- isListBlock,
14
- isTextBlock
15
- };
16
- }
17
- const selectionIsCollapsed = ({
18
- context
19
- }) => JSON.stringify(context.selection?.anchor.path) === JSON.stringify(context.selection?.focus.path) && context.selection?.anchor.offset === context.selection?.focus.offset, getFocusBlock = ({
20
- context
21
- }) => {
22
- const key = context.selection && types.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;
23
- return node && key ? {
24
- node,
25
- path: [{
26
- _key: key
27
- }]
28
- } : void 0;
29
- }, getFocusListBlock = ({
30
- context
31
- }) => {
32
- const guards = createGuards(context), focusBlock = getFocusBlock({
33
- context
34
- });
35
- return focusBlock && guards.isListBlock(focusBlock.node) ? {
36
- node: focusBlock.node,
37
- path: focusBlock.path
38
- } : void 0;
39
- }, getFocusTextBlock = ({
40
- context
41
- }) => {
42
- const focusBlock = getFocusBlock({
43
- context
44
- });
45
- return focusBlock && types.isPortableTextTextBlock(focusBlock.node) ? {
46
- node: focusBlock.node,
47
- path: focusBlock.path
48
- } : void 0;
49
- }, getFocusBlockObject = ({
50
- context
51
- }) => {
52
- const focusBlock = getFocusBlock({
53
- context
54
- });
55
- return focusBlock && !types.isPortableTextTextBlock(focusBlock.node) ? {
56
- node: focusBlock.node,
57
- path: focusBlock.path
58
- } : void 0;
59
- }, getFocusChild = ({
60
- context
61
- }) => {
62
- const focusBlock = getFocusTextBlock({
63
- context
64
- });
65
- if (!focusBlock)
66
- return;
67
- const key = context.selection && types.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;
68
- return node && key ? {
69
- node,
70
- path: [...focusBlock.path, "children", {
71
- _key: key
72
- }]
73
- } : void 0;
74
- }, getFocusSpan = ({
75
- context
76
- }) => {
77
- const focusChild = getFocusChild({
78
- context
79
- });
80
- return focusChild && types.isPortableTextSpan(focusChild.node) ? {
81
- node: focusChild.node,
82
- path: focusChild.path
83
- } : void 0;
84
- }, getFirstBlock = ({
85
- context
86
- }) => {
87
- const node = context.value[0];
88
- return node ? {
89
- node,
90
- path: [{
91
- _key: node._key
92
- }]
93
- } : void 0;
94
- }, getLastBlock = ({
95
- context
96
- }) => {
97
- const node = context.value[context.value.length - 1] ? context.value[context.value.length - 1] : void 0;
98
- return node ? {
99
- node,
100
- path: [{
101
- _key: node._key
102
- }]
103
- } : void 0;
104
- }, getSelectedBlocks = ({
105
- context
106
- }) => {
107
- if (!context.selection)
108
- return [];
109
- const selectedBlocks = [], startKey = context.selection.backward ? types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, endKey = context.selection.backward ? types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0;
110
- if (!startKey || !endKey)
111
- return selectedBlocks;
112
- for (const block of context.value) {
113
- if (block._key === startKey) {
114
- if (selectedBlocks.push({
115
- node: block,
116
- path: [{
117
- _key: block._key
118
- }]
119
- }), startKey === endKey)
120
- break;
121
- continue;
122
- }
123
- if (block._key === endKey) {
124
- selectedBlocks.push({
125
- node: block,
126
- path: [{
127
- _key: block._key
128
- }]
129
- });
130
- break;
131
- }
132
- selectedBlocks.length > 0 && selectedBlocks.push({
133
- node: block,
134
- path: [{
135
- _key: block._key
136
- }]
137
- });
138
- }
139
- return selectedBlocks;
140
- }, getSelectionStartBlock = ({
141
- context
142
- }) => {
143
- if (!context.selection)
144
- return;
145
- const key = context.selection.backward ? types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : types.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;
146
- return node && key ? {
147
- node,
148
- path: [{
149
- _key: key
150
- }]
151
- } : void 0;
152
- }, getSelectionEndBlock = ({
153
- context
154
- }) => {
155
- if (!context.selection)
156
- return;
157
- const key = context.selection.backward ? types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : types.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;
158
- return node && key ? {
159
- node,
160
- path: [{
161
- _key: key
162
- }]
163
- } : void 0;
164
- }, getPreviousBlock = ({
165
- context
166
- }) => {
167
- let previousBlock;
168
- const selectionStartBlock = getSelectionStartBlock({
169
- context
170
- });
171
- if (!selectionStartBlock)
172
- return;
173
- let foundSelectionStartBlock = !1;
174
- for (const block of context.value) {
175
- if (block._key === selectionStartBlock.node._key) {
176
- foundSelectionStartBlock = !0;
177
- break;
178
- }
179
- previousBlock = {
180
- node: block,
181
- path: [{
182
- _key: block._key
183
- }]
184
- };
185
- }
186
- if (foundSelectionStartBlock && previousBlock)
187
- return previousBlock;
188
- }, getNextBlock = ({
189
- context
190
- }) => {
191
- let nextBlock;
192
- const selectionEndBlock = getSelectionEndBlock({
193
- context
194
- });
195
- if (!selectionEndBlock)
196
- return;
197
- let foundSelectionEndBlock = !1;
198
- for (const block of context.value) {
199
- if (block._key === selectionEndBlock.node._key) {
200
- foundSelectionEndBlock = !0;
201
- continue;
202
- }
203
- if (foundSelectionEndBlock) {
204
- nextBlock = {
205
- node: block,
206
- path: [{
207
- _key: block._key
208
- }]
209
- };
210
- break;
211
- }
212
- }
213
- if (foundSelectionEndBlock && nextBlock)
214
- return nextBlock;
215
- };
216
3
  function getStartPoint({
217
4
  node,
218
5
  path
@@ -275,7 +62,16 @@ const getSelectionText = ({
275
62
  break;
276
63
  }
277
64
  return text;
278
- }, getBlockTextBefore = ({
65
+ };
66
+ 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) => {
67
+ for (var prop in b || (b = {}))
68
+ __hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
69
+ if (__getOwnPropSymbols)
70
+ for (var prop of __getOwnPropSymbols(b))
71
+ __propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
72
+ return a;
73
+ }, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
74
+ const getBlockTextBefore = ({
279
75
  context
280
76
  }) => {
281
77
  if (!context.selection)
@@ -290,31 +86,15 @@ const getSelectionText = ({
290
86
  }]
291
87
  });
292
88
  return getSelectionText({
293
- context: {
294
- ...context,
89
+ context: __spreadProps(__spreadValues({}, context), {
295
90
  value: context.value,
296
91
  selection: {
297
92
  anchor: startOfBlock,
298
93
  focus: point
299
94
  }
300
- }
95
+ })
301
96
  });
302
97
  };
303
- exports.createGuards = createGuards;
304
98
  exports.getBlockTextBefore = getBlockTextBefore;
305
- exports.getFirstBlock = getFirstBlock;
306
- exports.getFocusBlock = getFocusBlock;
307
- exports.getFocusBlockObject = getFocusBlockObject;
308
- exports.getFocusChild = getFocusChild;
309
- exports.getFocusListBlock = getFocusListBlock;
310
- exports.getFocusSpan = getFocusSpan;
311
- exports.getFocusTextBlock = getFocusTextBlock;
312
- exports.getLastBlock = getLastBlock;
313
- exports.getNextBlock = getNextBlock;
314
- exports.getPreviousBlock = getPreviousBlock;
315
- exports.getSelectedBlocks = getSelectedBlocks;
316
- exports.getSelectionEndBlock = getSelectionEndBlock;
317
- exports.getSelectionStartBlock = getSelectionStartBlock;
318
99
  exports.getSelectionText = getSelectionText;
319
- exports.selectionIsCollapsed = selectionIsCollapsed;
320
100
  //# sourceMappingURL=selector.get-text-before.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"selector.get-text-before.cjs","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,MAAAA,wBAAwBD,KAAK,KAAKA,MAAME,UAAUJ,OAAOE,MAAMG;AAAAA,EAAAA;AAGxE,WAASC,YAAYJ,OAAgD;AACnE,WAAOK,MAAAA,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,MAAAA,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,MA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AGF,iBAAiB9C,MAAMC,MAAM;AAC/D;AAAA,UAAA;AAAA,QAIa4C;AAKnB,UACEJ,eAAeK,iBAAiB9C,MAAMD,KAAK,CAAC,CAAC,KAC7CZ,MAAMkB,SAASyC,iBAAiB9C,MAAMD,KAAK,CAAC,EAAEM;AAE9C;AAAA,IAAA;AAIGwC,SAAAA;AACT,GC7EaI,qBAA6CA,CAAC;AAAA,EAACvD;AAAO,MAAM;AACvE,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.cjs","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,MAAAA,wBAAwBF,IAAI,IACvB;AAAA,IACLC,MAAM,CAAC,GAAGA,MAAM,YAAY;AAAA,MAACE,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U,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,234 @@
1
+ "use strict";
2
+ var types = require("@sanity/types");
3
+ function createGuards({
4
+ schema
5
+ }) {
6
+ function isListBlock(block) {
7
+ return types.isPortableTextListBlock(block) && block._type === schema.block.name;
8
+ }
9
+ function isTextBlock(block) {
10
+ return types.isPortableTextTextBlock(block) && block._type === schema.block.name;
11
+ }
12
+ return {
13
+ isListBlock,
14
+ isTextBlock
15
+ };
16
+ }
17
+ const selectionIsCollapsed = ({
18
+ context
19
+ }) => {
20
+ var _a, _b, _c, _d;
21
+ 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);
22
+ }, getFocusBlock = ({
23
+ context
24
+ }) => {
25
+ const key = context.selection && types.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;
26
+ return node && key ? {
27
+ node,
28
+ path: [{
29
+ _key: key
30
+ }]
31
+ } : void 0;
32
+ }, getFocusListBlock = ({
33
+ context
34
+ }) => {
35
+ const guards = createGuards(context), focusBlock = getFocusBlock({
36
+ context
37
+ });
38
+ return focusBlock && guards.isListBlock(focusBlock.node) ? {
39
+ node: focusBlock.node,
40
+ path: focusBlock.path
41
+ } : void 0;
42
+ }, getFocusTextBlock = ({
43
+ context
44
+ }) => {
45
+ const focusBlock = getFocusBlock({
46
+ context
47
+ });
48
+ return focusBlock && types.isPortableTextTextBlock(focusBlock.node) ? {
49
+ node: focusBlock.node,
50
+ path: focusBlock.path
51
+ } : void 0;
52
+ }, getFocusBlockObject = ({
53
+ context
54
+ }) => {
55
+ const focusBlock = getFocusBlock({
56
+ context
57
+ });
58
+ return focusBlock && !types.isPortableTextTextBlock(focusBlock.node) ? {
59
+ node: focusBlock.node,
60
+ path: focusBlock.path
61
+ } : void 0;
62
+ }, getFocusChild = ({
63
+ context
64
+ }) => {
65
+ const focusBlock = getFocusTextBlock({
66
+ context
67
+ });
68
+ if (!focusBlock)
69
+ return;
70
+ const key = context.selection && types.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;
71
+ return node && key ? {
72
+ node,
73
+ path: [...focusBlock.path, "children", {
74
+ _key: key
75
+ }]
76
+ } : void 0;
77
+ }, getFocusSpan = ({
78
+ context
79
+ }) => {
80
+ const focusChild = getFocusChild({
81
+ context
82
+ });
83
+ return focusChild && types.isPortableTextSpan(focusChild.node) ? {
84
+ node: focusChild.node,
85
+ path: focusChild.path
86
+ } : void 0;
87
+ }, getFirstBlock = ({
88
+ context
89
+ }) => {
90
+ const node = context.value[0];
91
+ return node ? {
92
+ node,
93
+ path: [{
94
+ _key: node._key
95
+ }]
96
+ } : void 0;
97
+ }, getLastBlock = ({
98
+ context
99
+ }) => {
100
+ const node = context.value[context.value.length - 1] ? context.value[context.value.length - 1] : void 0;
101
+ return node ? {
102
+ node,
103
+ path: [{
104
+ _key: node._key
105
+ }]
106
+ } : void 0;
107
+ }, getSelectedBlocks = ({
108
+ context
109
+ }) => {
110
+ if (!context.selection)
111
+ return [];
112
+ const selectedBlocks = [], startKey = context.selection.backward ? types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, endKey = context.selection.backward ? types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0;
113
+ if (!startKey || !endKey)
114
+ return selectedBlocks;
115
+ for (const block of context.value) {
116
+ if (block._key === startKey) {
117
+ if (selectedBlocks.push({
118
+ node: block,
119
+ path: [{
120
+ _key: block._key
121
+ }]
122
+ }), startKey === endKey)
123
+ break;
124
+ continue;
125
+ }
126
+ if (block._key === endKey) {
127
+ selectedBlocks.push({
128
+ node: block,
129
+ path: [{
130
+ _key: block._key
131
+ }]
132
+ });
133
+ break;
134
+ }
135
+ selectedBlocks.length > 0 && selectedBlocks.push({
136
+ node: block,
137
+ path: [{
138
+ _key: block._key
139
+ }]
140
+ });
141
+ }
142
+ return selectedBlocks;
143
+ }, getSelectionStartBlock = ({
144
+ context
145
+ }) => {
146
+ if (!context.selection)
147
+ return;
148
+ const key = context.selection.backward ? types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : types.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;
149
+ return node && key ? {
150
+ node,
151
+ path: [{
152
+ _key: key
153
+ }]
154
+ } : void 0;
155
+ }, getSelectionEndBlock = ({
156
+ context
157
+ }) => {
158
+ if (!context.selection)
159
+ return;
160
+ const key = context.selection.backward ? types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : types.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;
161
+ return node && key ? {
162
+ node,
163
+ path: [{
164
+ _key: key
165
+ }]
166
+ } : void 0;
167
+ }, getPreviousBlock = ({
168
+ context
169
+ }) => {
170
+ let previousBlock;
171
+ const selectionStartBlock = getSelectionStartBlock({
172
+ context
173
+ });
174
+ if (!selectionStartBlock)
175
+ return;
176
+ let foundSelectionStartBlock = !1;
177
+ for (const block of context.value) {
178
+ if (block._key === selectionStartBlock.node._key) {
179
+ foundSelectionStartBlock = !0;
180
+ break;
181
+ }
182
+ previousBlock = {
183
+ node: block,
184
+ path: [{
185
+ _key: block._key
186
+ }]
187
+ };
188
+ }
189
+ if (foundSelectionStartBlock && previousBlock)
190
+ return previousBlock;
191
+ }, getNextBlock = ({
192
+ context
193
+ }) => {
194
+ let nextBlock;
195
+ const selectionEndBlock = getSelectionEndBlock({
196
+ context
197
+ });
198
+ if (!selectionEndBlock)
199
+ return;
200
+ let foundSelectionEndBlock = !1;
201
+ for (const block of context.value) {
202
+ if (block._key === selectionEndBlock.node._key) {
203
+ foundSelectionEndBlock = !0;
204
+ continue;
205
+ }
206
+ if (foundSelectionEndBlock) {
207
+ nextBlock = {
208
+ node: block,
209
+ path: [{
210
+ _key: block._key
211
+ }]
212
+ };
213
+ break;
214
+ }
215
+ }
216
+ if (foundSelectionEndBlock && nextBlock)
217
+ return nextBlock;
218
+ };
219
+ exports.createGuards = createGuards;
220
+ exports.getFirstBlock = getFirstBlock;
221
+ exports.getFocusBlock = getFocusBlock;
222
+ exports.getFocusBlockObject = getFocusBlockObject;
223
+ exports.getFocusChild = getFocusChild;
224
+ exports.getFocusListBlock = getFocusListBlock;
225
+ exports.getFocusSpan = getFocusSpan;
226
+ exports.getFocusTextBlock = getFocusTextBlock;
227
+ exports.getLastBlock = getLastBlock;
228
+ exports.getNextBlock = getNextBlock;
229
+ exports.getPreviousBlock = getPreviousBlock;
230
+ exports.getSelectedBlocks = getSelectedBlocks;
231
+ exports.getSelectionEndBlock = getSelectionEndBlock;
232
+ exports.getSelectionStartBlock = getSelectionStartBlock;
233
+ exports.selectionIsCollapsed = selectionIsCollapsed;
234
+ //# sourceMappingURL=selectors.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selectors.cjs","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","nextB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