@portabletext/editor 1.33.2 → 1.33.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +11 -204
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs +7 -7
  4. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  5. package/lib/_chunks-cjs/plugin.event-listener.cjs +208 -33
  6. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
  7. package/lib/_chunks-cjs/selector.get-text-before.cjs +3 -3
  8. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  9. package/lib/_chunks-cjs/selector.is-active-style.cjs +246 -0
  10. package/lib/_chunks-cjs/selector.is-active-style.cjs.map +1 -0
  11. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs +31 -200
  12. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs.map +1 -1
  13. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs +7 -5
  14. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs.map +1 -1
  15. package/lib/_chunks-cjs/util.reverse-selection.cjs +0 -116
  16. package/lib/_chunks-cjs/util.reverse-selection.cjs.map +1 -1
  17. package/lib/_chunks-cjs/util.slice-blocks.cjs +138 -1
  18. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  19. package/lib/_chunks-es/behavior.core.js +9 -202
  20. package/lib/_chunks-es/behavior.core.js.map +1 -1
  21. package/lib/_chunks-es/behavior.markdown.js +1 -1
  22. package/lib/_chunks-es/plugin.event-listener.js +205 -31
  23. package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
  24. package/lib/_chunks-es/selector.get-text-before.js +2 -1
  25. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  26. package/lib/_chunks-es/selector.is-active-style.js +249 -0
  27. package/lib/_chunks-es/selector.is-active-style.js.map +1 -0
  28. package/lib/_chunks-es/selector.is-at-the-start-of-block.js +20 -189
  29. package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
  30. package/lib/_chunks-es/util.block-offsets-to-selection.js +5 -3
  31. package/lib/_chunks-es/util.block-offsets-to-selection.js.map +1 -1
  32. package/lib/_chunks-es/util.reverse-selection.js +1 -117
  33. package/lib/_chunks-es/util.reverse-selection.js.map +1 -1
  34. package/lib/_chunks-es/util.slice-blocks.js +140 -3
  35. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  36. package/lib/index.d.cts +1 -115
  37. package/lib/index.d.ts +1 -115
  38. package/lib/plugins/index.cjs +19 -15
  39. package/lib/plugins/index.cjs.map +1 -1
  40. package/lib/plugins/index.d.cts +1 -115
  41. package/lib/plugins/index.d.ts +1 -115
  42. package/lib/plugins/index.js +9 -5
  43. package/lib/plugins/index.js.map +1 -1
  44. package/lib/selectors/index.cjs +16 -21
  45. package/lib/selectors/index.cjs.map +1 -1
  46. package/lib/selectors/index.d.cts +2 -0
  47. package/lib/selectors/index.d.ts +2 -0
  48. package/lib/selectors/index.js +7 -11
  49. package/lib/selectors/index.js.map +1 -1
  50. package/lib/utils/index.cjs +13 -13
  51. package/lib/utils/index.cjs.map +1 -1
  52. package/lib/utils/index.d.cts +2 -0
  53. package/lib/utils/index.d.ts +2 -0
  54. package/lib/utils/index.js +3 -3
  55. package/package.json +2 -2
  56. package/src/behaviors/behavior.core.annotations.ts +0 -24
  57. package/src/behaviors/behavior.core.decorators.ts +0 -19
  58. package/src/behaviors/behavior.core.insert-break.ts +4 -4
  59. package/src/behaviors/behavior.core.lists.ts +0 -30
  60. package/src/behaviors/behavior.core.ts +2 -17
  61. package/src/behaviors/behavior.default.ts +198 -0
  62. package/src/behaviors/behavior.foundational.ts +12 -12
  63. package/src/behaviors/behavior.markdown-emphasis.ts +4 -0
  64. package/src/converters/converter.text-html.serialize.test.ts +1 -1
  65. package/src/editor/PortableTextEditor.tsx +1 -1
  66. package/src/editor/editor-machine.ts +8 -8
  67. package/src/plugins/plugin.event-listener.tsx +1 -1
  68. package/src/selectors/selector.get-caret-word-selection.ts +9 -0
  69. package/src/selectors/selector.get-selection-text.test.ts +383 -36
  70. package/src/selectors/selector.get-selection-text.ts +13 -73
  71. package/src/utils/util.block-offset.test.ts +312 -0
  72. package/src/utils/util.block-offset.ts +39 -7
  73. package/src/utils/util.block-offsets-to-selection.ts +2 -0
  74. package/src/utils/util.slice-blocks.ts +12 -1
  75. package/lib/_chunks-cjs/selector.get-trimmed-selection.cjs +0 -97
  76. package/lib/_chunks-cjs/selector.get-trimmed-selection.cjs.map +0 -1
  77. package/lib/_chunks-es/selector.get-trimmed-selection.js +0 -100
  78. package/lib/_chunks-es/selector.get-trimmed-selection.js.map +0 -1
  79. package/src/behaviors/behavior.core.deserialize.ts +0 -60
  80. package/src/behaviors/behavior.core.serialize.ts +0 -44
  81. package/src/behaviors/behavior.core.style.ts +0 -19
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
- var util_reverseSelection = require("./util.reverse-selection.cjs"), selector_isAtTheStartOfBlock = require("./selector.is-at-the-start-of-block.cjs");
2
+ var util_sliceBlocks = require("./util.slice-blocks.cjs"), util_reverseSelection = require("./util.reverse-selection.cjs"), selector_isAtTheStartOfBlock = require("./selector.is-at-the-start-of-block.cjs");
3
3
  const getBlockTextBefore = ({
4
4
  context
5
5
  }) => {
6
6
  if (!context.selection)
7
7
  return "";
8
- const point = (context.selection.backward ? util_reverseSelection.reverseSelection(context.selection) : context.selection).anchor, key = util_reverseSelection.isKeyedSegment(point.path[0]) ? point.path[0]._key : void 0, block = key ? context.value.find((block2) => block2._key === key) : void 0;
8
+ const point = (context.selection.backward ? util_reverseSelection.reverseSelection(context.selection) : context.selection).anchor, key = util_sliceBlocks.isKeyedSegment(point.path[0]) ? point.path[0]._key : void 0, block = key ? context.value.find((block2) => block2._key === key) : void 0;
9
9
  if (!block)
10
10
  return "";
11
- const startOfBlock = util_reverseSelection.getBlockStartPoint({
11
+ const startOfBlock = util_sliceBlocks.getBlockStartPoint({
12
12
  node: block,
13
13
  path: [{
14
14
  _key: block._key
@@ -1 +1 @@
1
- {"version":3,"file":"selector.get-text-before.cjs","sources":["../../src/selectors/selector.get-text-before.ts"],"sourcesContent":["import type {EditorSelector} from '../editor/editor-selector'\nimport {getBlockStartPoint} from '../utils/util.get-block-start-point'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {reverseSelection} from '../utils/util.reverse-selection'\nimport {getSelectionText} from './selector.get-selection-text'\n\n/**\n * @public\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 = getBlockStartPoint({\n node: block,\n path: [{_key: block._key}],\n })\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":["getBlockTextBefore","context","selection","point","backward","reverseSelection","anchor","key","isKeyedSegment","path","_key","undefined","block","value","find","startOfBlock","getBlockStartPoint","node","getSelectionText","focus"],"mappings":";;AASO,MAAMA,qBAA6CA,CAAC;AAAA,EAACC;AAAO,MAAM;AACvE,MAAI,CAACA,QAAQC;AACJ,WAAA;AAMT,QAAMC,SAHYF,QAAQC,UAAUE,WAChCC,uCAAiBJ,QAAQC,SAAS,IAClCD,QAAQC,WACYI,QAClBC,MAAMC,sBAAAA,eAAeL,MAAMM,KAAK,CAAC,CAAC,IAAIN,MAAMM,KAAK,CAAC,EAAEC,OAAOC,QAE3DC,QAAQL,MACVN,QAAQY,MAAMC,KAAMF,CAAAA,WAAUA,OAAMF,SAASH,GAAG,IAChDI;AAEJ,MAAI,CAACC;AACI,WAAA;AAGT,QAAMG,eAAeC,sBAAAA,mBAAmB;AAAA,IACtCC,MAAML;AAAAA,IACNH,MAAM,CAAC;AAAA,MAACC,MAAME,MAAMF;AAAAA,IAAK,CAAA;AAAA,EAAA,CAC1B;AAED,SAAOQ,8CAAiB;AAAA,IACtBjB,SAAS;AAAA,MACP,GAAGA;AAAAA,MACHY,OAAOZ,QAAQY;AAAAA,MACfX,WAAW;AAAA,QACTI,QAAQS;AAAAA,QACRI,OAAOhB;AAAAA,MAAAA;AAAAA,IACT;AAAA,EACF,CACD;AACH;;"}
1
+ {"version":3,"file":"selector.get-text-before.cjs","sources":["../../src/selectors/selector.get-text-before.ts"],"sourcesContent":["import type {EditorSelector} from '../editor/editor-selector'\nimport {getBlockStartPoint} from '../utils/util.get-block-start-point'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {reverseSelection} from '../utils/util.reverse-selection'\nimport {getSelectionText} from './selector.get-selection-text'\n\n/**\n * @public\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 = getBlockStartPoint({\n node: block,\n path: [{_key: block._key}],\n })\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":["getBlockTextBefore","context","selection","point","backward","reverseSelection","anchor","key","isKeyedSegment","path","_key","undefined","block","value","find","startOfBlock","getBlockStartPoint","node","getSelectionText","focus"],"mappings":";;AASO,MAAMA,qBAA6CA,CAAC;AAAA,EAACC;AAAO,MAAM;AACvE,MAAI,CAACA,QAAQC;AACJ,WAAA;AAMT,QAAMC,SAHYF,QAAQC,UAAUE,WAChCC,uCAAiBJ,QAAQC,SAAS,IAClCD,QAAQC,WACYI,QAClBC,MAAMC,iBAAAA,eAAeL,MAAMM,KAAK,CAAC,CAAC,IAAIN,MAAMM,KAAK,CAAC,EAAEC,OAAOC,QAE3DC,QAAQL,MACVN,QAAQY,MAAMC,KAAMF,CAAAA,WAAUA,OAAMF,SAASH,GAAG,IAChDI;AAEJ,MAAI,CAACC;AACI,WAAA;AAGT,QAAMG,eAAeC,iBAAAA,mBAAmB;AAAA,IACtCC,MAAML;AAAAA,IACNH,MAAM,CAAC;AAAA,MAACC,MAAME,MAAMF;AAAAA,IAAK,CAAA;AAAA,EAAA,CAC1B;AAED,SAAOQ,8CAAiB;AAAA,IACtBjB,SAAS;AAAA,MACP,GAAGA;AAAAA,MACHY,OAAOZ,QAAQY;AAAAA,MACfX,WAAW;AAAA,QACTI,QAAQS;AAAAA,QACRI,OAAOhB;AAAAA,MAAAA;AAAAA,IACT;AAAA,EACF,CACD;AACH;;"}
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+ var types = require("@sanity/types"), util_sliceBlocks = require("./util.slice-blocks.cjs"), selector_isAtTheStartOfBlock = require("./selector.is-at-the-start-of-block.cjs");
3
+ const getSelectedSpans = ({
4
+ context
5
+ }) => {
6
+ if (!context.selection)
7
+ return [];
8
+ const selectedSpans = [], startPoint = context.selection.backward ? context.selection.focus : context.selection.anchor, endPoint = context.selection.backward ? context.selection.anchor : context.selection.focus, startBlockKey = types.isKeySegment(startPoint.path[0]) ? startPoint.path[0]._key : void 0, endBlockKey = types.isKeySegment(endPoint.path[0]) ? endPoint.path[0]._key : void 0;
9
+ if (!startBlockKey || !endBlockKey)
10
+ return selectedSpans;
11
+ const startSpanKey = types.isKeySegment(startPoint.path[2]) ? startPoint.path[2]._key : void 0, endSpanKey = types.isKeySegment(endPoint.path[2]) ? endPoint.path[2]._key : void 0;
12
+ for (const block of context.value)
13
+ if (types.isPortableTextTextBlock(block)) {
14
+ if (block._key === startBlockKey) {
15
+ for (const child of block.children)
16
+ if (types.isPortableTextSpan(child)) {
17
+ if (startSpanKey && child._key === startSpanKey) {
18
+ if (startPoint.offset < child.text.length && selectedSpans.push({
19
+ node: child,
20
+ path: [{
21
+ _key: block._key
22
+ }, "children", {
23
+ _key: child._key
24
+ }]
25
+ }), startSpanKey === endSpanKey)
26
+ break;
27
+ continue;
28
+ }
29
+ if (endSpanKey && child._key === endSpanKey) {
30
+ endPoint.offset > 0 && selectedSpans.push({
31
+ node: child,
32
+ path: [{
33
+ _key: block._key
34
+ }, "children", {
35
+ _key: child._key
36
+ }]
37
+ });
38
+ break;
39
+ }
40
+ selectedSpans.length > 0 && selectedSpans.push({
41
+ node: child,
42
+ path: [{
43
+ _key: block._key
44
+ }, "children", {
45
+ _key: child._key
46
+ }]
47
+ });
48
+ }
49
+ if (startBlockKey === endBlockKey)
50
+ break;
51
+ continue;
52
+ }
53
+ if (block._key === endBlockKey) {
54
+ for (const child of block.children)
55
+ if (types.isPortableTextSpan(child)) {
56
+ if (endSpanKey && child._key === endSpanKey) {
57
+ endPoint.offset > 0 && selectedSpans.push({
58
+ node: child,
59
+ path: [{
60
+ _key: block._key
61
+ }, "children", {
62
+ _key: child._key
63
+ }]
64
+ });
65
+ break;
66
+ }
67
+ selectedSpans.push({
68
+ node: child,
69
+ path: [{
70
+ _key: block._key
71
+ }, "children", {
72
+ _key: child._key
73
+ }]
74
+ });
75
+ }
76
+ break;
77
+ }
78
+ if (selectedSpans.length > 0)
79
+ for (const child of block.children)
80
+ types.isPortableTextSpan(child) && selectedSpans.push({
81
+ node: child,
82
+ path: [{
83
+ _key: block._key
84
+ }, "children", {
85
+ _key: child._key
86
+ }]
87
+ });
88
+ }
89
+ return selectedSpans;
90
+ }, getActiveListItem = ({
91
+ context
92
+ }) => {
93
+ if (!context.selection)
94
+ return;
95
+ const guards = selector_isAtTheStartOfBlock.createGuards(context), selectedTextBlocks = selector_isAtTheStartOfBlock.getSelectedBlocks({
96
+ context
97
+ }).map((block) => block.node).filter(guards.isTextBlock), firstTextBlock = selectedTextBlocks.at(0);
98
+ if (!firstTextBlock)
99
+ return;
100
+ const firstListItem = firstTextBlock.listItem;
101
+ if (firstListItem && selectedTextBlocks.every((block) => block.listItem === firstListItem))
102
+ return firstListItem;
103
+ }, getActiveStyle = ({
104
+ context
105
+ }) => {
106
+ if (!context.selection)
107
+ return;
108
+ const guards = selector_isAtTheStartOfBlock.createGuards(context), selectedTextBlocks = selector_isAtTheStartOfBlock.getSelectedBlocks({
109
+ context
110
+ }).map((block) => block.node).filter(guards.isTextBlock), firstTextBlock = selectedTextBlocks.at(0);
111
+ if (!firstTextBlock)
112
+ return;
113
+ const firstStyle = firstTextBlock.style;
114
+ if (firstStyle && selectedTextBlocks.every((block) => block.style === firstStyle))
115
+ return firstStyle;
116
+ }, getTrimmedSelection = ({
117
+ context
118
+ }) => {
119
+ if (!context.selection)
120
+ return context.selection;
121
+ const startPoint = selector_isAtTheStartOfBlock.getSelectionStartPoint({
122
+ context
123
+ }), endPoint = selector_isAtTheStartOfBlock.getSelectionEndPoint({
124
+ context
125
+ });
126
+ if (!startPoint || !endPoint)
127
+ return context.selection;
128
+ const startBlockKey = util_sliceBlocks.isKeyedSegment(startPoint.path[0]) ? startPoint.path[0]._key : null, startChildKey = util_sliceBlocks.isKeyedSegment(startPoint.path[2]) ? startPoint.path[2]._key : null, endBlockKey = util_sliceBlocks.isKeyedSegment(endPoint.path[0]) ? endPoint.path[0]._key : null, endChildKey = util_sliceBlocks.isKeyedSegment(endPoint.path[2]) ? endPoint.path[2]._key : null;
129
+ if (!startBlockKey || !endBlockKey)
130
+ return context.selection;
131
+ let startBlockFound = !1, adjustedStartPoint, trimStartPoint = !1, adjustedEndPoint, trimEndPoint = !1, previousPotentialEndpoint;
132
+ for (const block of context.value)
133
+ if (!(block._key === startBlockKey && (startBlockFound = !0, types.isPortableTextTextBlock(block) && util_sliceBlocks.isEmptyTextBlock(block))) && startBlockFound && types.isPortableTextTextBlock(block)) {
134
+ if (block._key === endBlockKey && util_sliceBlocks.isEmptyTextBlock(block))
135
+ break;
136
+ for (const child of block.children) {
137
+ if (child._key === endChildKey && (!types.isPortableTextSpan(child) || endPoint.offset === 0)) {
138
+ adjustedEndPoint = previousPotentialEndpoint ? {
139
+ path: [{
140
+ _key: previousPotentialEndpoint.blockKey
141
+ }, "children", {
142
+ _key: previousPotentialEndpoint.span._key
143
+ }],
144
+ offset: previousPotentialEndpoint.span.text.length
145
+ } : void 0, trimEndPoint = !0;
146
+ break;
147
+ }
148
+ if (trimStartPoint) {
149
+ const lonelySpan = types.isPortableTextSpan(child) && block.children.length === 1;
150
+ (types.isPortableTextSpan(child) && child.text.length > 0 || lonelySpan) && (adjustedStartPoint = {
151
+ path: [{
152
+ _key: block._key
153
+ }, "children", {
154
+ _key: child._key
155
+ }],
156
+ offset: 0
157
+ }, previousPotentialEndpoint = {
158
+ blockKey: block._key,
159
+ span: child
160
+ }, trimStartPoint = !1);
161
+ continue;
162
+ }
163
+ if (child._key === startChildKey) {
164
+ if (!types.isPortableTextSpan(child)) {
165
+ trimStartPoint = !0;
166
+ continue;
167
+ }
168
+ if (startPoint.offset === child.text.length) {
169
+ trimStartPoint = !0, previousPotentialEndpoint = child.text.length > 0 ? {
170
+ blockKey: block._key,
171
+ span: child
172
+ } : previousPotentialEndpoint;
173
+ continue;
174
+ }
175
+ }
176
+ previousPotentialEndpoint = types.isPortableTextSpan(child) && child.text.length > 0 ? {
177
+ blockKey: block._key,
178
+ span: child
179
+ } : previousPotentialEndpoint;
180
+ }
181
+ if (block._key === endBlockKey)
182
+ break;
183
+ }
184
+ const trimmedSelection = context.selection.backward ? {
185
+ anchor: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,
186
+ focus: adjustedStartPoint ?? startPoint,
187
+ backward: !0
188
+ } : {
189
+ anchor: adjustedStartPoint ?? startPoint,
190
+ focus: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint
191
+ };
192
+ if (selector_isAtTheStartOfBlock.isSelectionCollapsed({
193
+ context: {
194
+ ...context,
195
+ selection: trimmedSelection
196
+ }
197
+ })) {
198
+ const focusTextBlock = selector_isAtTheStartOfBlock.getFocusTextBlock({
199
+ context: {
200
+ ...context,
201
+ selection: trimmedSelection
202
+ }
203
+ });
204
+ if (focusTextBlock && !util_sliceBlocks.isEmptyTextBlock(focusTextBlock.node))
205
+ return null;
206
+ }
207
+ return trimmedSelection;
208
+ };
209
+ function isActiveAnnotation(annotation) {
210
+ return (snapshot) => {
211
+ if (!snapshot.context.selection)
212
+ return !1;
213
+ const selectedBlocks = selector_isAtTheStartOfBlock.getSelectedBlocks(snapshot), focusSpan = selector_isAtTheStartOfBlock.getFocusSpan(snapshot), selectedSpans = selector_isAtTheStartOfBlock.isSelectionExpanded(snapshot) ? getSelectedSpans(snapshot) : focusSpan ? [focusSpan] : [];
214
+ if (selectedSpans.length === 0 || selectedSpans.some((span) => !span.node.marks || span.node.marks?.length === 0))
215
+ return !1;
216
+ const selectionMarkDefs = selectedBlocks.flatMap((block) => types.isPortableTextTextBlock(block.node) ? block.node.markDefs ?? [] : []);
217
+ return selectedSpans.every((span) => (span.node.marks?.flatMap((mark) => {
218
+ const markDef = selectionMarkDefs.find((markDef2) => markDef2._key === mark);
219
+ return markDef ? [markDef._type] : [];
220
+ }) ?? []).includes(annotation));
221
+ };
222
+ }
223
+ function isActiveDecorator(decorator) {
224
+ return (snapshot) => {
225
+ if (selector_isAtTheStartOfBlock.isSelectionExpanded(snapshot)) {
226
+ const selectedSpans = getSelectedSpans(snapshot);
227
+ return selectedSpans.length > 0 && selectedSpans.every((span) => span.node.marks?.includes(decorator));
228
+ }
229
+ return snapshot.context.activeDecorators.includes(decorator);
230
+ };
231
+ }
232
+ function isActiveListItem(listItem) {
233
+ return (snapshot) => getActiveListItem(snapshot) === listItem;
234
+ }
235
+ function isActiveStyle(style) {
236
+ return (snapshot) => getActiveStyle(snapshot) === style;
237
+ }
238
+ exports.getActiveListItem = getActiveListItem;
239
+ exports.getActiveStyle = getActiveStyle;
240
+ exports.getSelectedSpans = getSelectedSpans;
241
+ exports.getTrimmedSelection = getTrimmedSelection;
242
+ exports.isActiveAnnotation = isActiveAnnotation;
243
+ exports.isActiveDecorator = isActiveDecorator;
244
+ exports.isActiveListItem = isActiveListItem;
245
+ exports.isActiveStyle = isActiveStyle;
246
+ //# sourceMappingURL=selector.is-active-style.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selector.is-active-style.cjs","sources":["../../src/selectors/selector.get-selected-spans.ts","../../src/selectors/selector.get-active-list-item.ts","../../src/selectors/selector.get-active-style.ts","../../src/selectors/selector.get-trimmed-selection.ts","../../src/selectors/selector.is-active-annotation.ts","../../src/selectors/selector.is-active-decorator.ts","../../src/selectors/selector.is-active-list-item.ts","../../src/selectors/selector.is-active-style.ts"],"sourcesContent":["import {\n isKeySegment,\n isPortableTextSpan,\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextSpan,\n} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @public\n */\nexport const getSelectedSpans: EditorSelector<\n Array<{\n node: PortableTextSpan\n path: [KeyedSegment, 'children', KeyedSegment]\n }>\n> = ({context}) => {\n if (!context.selection) {\n return []\n }\n\n const selectedSpans: Array<{\n node: PortableTextSpan\n path: [KeyedSegment, 'children', KeyedSegment]\n }> = []\n\n const startPoint = context.selection.backward\n ? context.selection.focus\n : context.selection.anchor\n const endPoint = context.selection.backward\n ? context.selection.anchor\n : context.selection.focus\n\n const startBlockKey = isKeySegment(startPoint.path[0])\n ? startPoint.path[0]._key\n : undefined\n const endBlockKey = isKeySegment(endPoint.path[0])\n ? endPoint.path[0]._key\n : undefined\n\n if (!startBlockKey || !endBlockKey) {\n return selectedSpans\n }\n\n const startSpanKey = isKeySegment(startPoint.path[2])\n ? startPoint.path[2]._key\n : undefined\n const endSpanKey = isKeySegment(endPoint.path[2])\n ? endPoint.path[2]._key\n : undefined\n\n for (const block of context.value) {\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n if (block._key === startBlockKey) {\n for (const child of block.children) {\n if (!isPortableTextSpan(child)) {\n continue\n }\n\n if (startSpanKey && child._key === startSpanKey) {\n if (startPoint.offset < child.text.length) {\n selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\n })\n }\n\n if (startSpanKey === endSpanKey) {\n break\n }\n\n continue\n }\n\n if (endSpanKey && child._key === endSpanKey) {\n if (endPoint.offset > 0) {\n selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\n })\n }\n break\n }\n\n if (selectedSpans.length > 0) {\n selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\n })\n }\n }\n\n if (startBlockKey === endBlockKey) {\n break\n }\n\n continue\n }\n\n if (block._key === endBlockKey) {\n for (const child of block.children) {\n if (!isPortableTextSpan(child)) {\n continue\n }\n\n if (endSpanKey && child._key === endSpanKey) {\n if (endPoint.offset > 0) {\n selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\n })\n }\n break\n }\n\n selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\n })\n }\n\n break\n }\n\n if (selectedSpans.length > 0) {\n for (const child of block.children) {\n if (!isPortableTextSpan(child)) {\n continue\n }\n\n selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\n })\n }\n }\n }\n\n return selectedSpans\n}\n","import type {PortableTextListBlock} from '@sanity/types'\nimport {createGuards} from '../behavior-actions/behavior.guards'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getSelectedBlocks} from './selectors'\n\n/**\n * @public\n */\nexport const getActiveListItem: EditorSelector<\n PortableTextListBlock['listItem'] | undefined\n> = ({context}) => {\n if (!context.selection) {\n return undefined\n }\n\n const guards = createGuards(context)\n const selectedBlocks = getSelectedBlocks({context}).map((block) => block.node)\n const selectedTextBlocks = selectedBlocks.filter(guards.isTextBlock)\n\n const firstTextBlock = selectedTextBlocks.at(0)\n\n if (!firstTextBlock) {\n return undefined\n }\n\n const firstListItem = firstTextBlock.listItem\n\n if (!firstListItem) {\n return undefined\n }\n\n if (selectedTextBlocks.every((block) => block.listItem === firstListItem)) {\n return firstListItem\n }\n\n return undefined\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\nimport {createGuards} from '../behavior-actions/behavior.guards'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getSelectedBlocks} from './selectors'\n\n/**\n * @public\n */\nexport const getActiveStyle: EditorSelector<PortableTextTextBlock['style']> = ({\n context,\n}) => {\n if (!context.selection) {\n return undefined\n }\n\n const guards = createGuards(context)\n const selectedBlocks = getSelectedBlocks({context}).map((block) => block.node)\n const selectedTextBlocks = selectedBlocks.filter(guards.isTextBlock)\n\n const firstTextBlock = selectedTextBlocks.at(0)\n\n if (!firstTextBlock) {\n return undefined\n }\n\n const firstStyle = firstTextBlock.style\n\n if (!firstStyle) {\n return undefined\n }\n\n if (selectedTextBlocks.every((block) => block.style === firstStyle)) {\n return firstStyle\n }\n\n return undefined\n}\n","import {\n isPortableTextSpan,\n isPortableTextTextBlock,\n type PortableTextSpan,\n} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport type {EditorSelection, EditorSelectionPoint} from '../types/editor'\nimport {isEmptyTextBlock, isKeyedSegment} from '../utils'\nimport {getSelectionEndPoint} from './selector.get-selection-end-point'\nimport {getSelectionStartPoint} from './selector.get-selection-start-point'\nimport {isSelectionCollapsed} from './selector.is-selection-collapsed'\nimport {getFocusTextBlock} from './selectors'\n\n/**\n * @public\n */\nexport const getTrimmedSelection: EditorSelector<EditorSelection> = ({\n context,\n}) => {\n if (!context.selection) {\n return context.selection\n }\n\n const startPoint = getSelectionStartPoint({context})\n const endPoint = getSelectionEndPoint({context})\n\n if (!startPoint || !endPoint) {\n return context.selection\n }\n\n const startBlockKey = isKeyedSegment(startPoint.path[0])\n ? startPoint.path[0]._key\n : null\n const startChildKey = isKeyedSegment(startPoint.path[2])\n ? startPoint.path[2]._key\n : null\n const endBlockKey = isKeyedSegment(endPoint.path[0])\n ? endPoint.path[0]._key\n : null\n const endChildKey = isKeyedSegment(endPoint.path[2])\n ? endPoint.path[2]._key\n : null\n\n if (!startBlockKey || !endBlockKey) {\n return context.selection\n }\n\n let startBlockFound = false\n let adjustedStartPoint: EditorSelectionPoint | undefined\n let trimStartPoint = false\n let adjustedEndPoint: EditorSelectionPoint | undefined\n let trimEndPoint = false\n let previousPotentialEndpoint:\n | {blockKey: string; span: PortableTextSpan}\n | undefined\n\n for (const block of context.value) {\n if (block._key === startBlockKey) {\n startBlockFound = true\n\n if (isPortableTextTextBlock(block) && isEmptyTextBlock(block)) {\n continue\n }\n }\n\n if (!startBlockFound) {\n continue\n }\n\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n if (block._key === endBlockKey && isEmptyTextBlock(block)) {\n break\n }\n\n for (const child of block.children) {\n if (child._key === endChildKey) {\n if (!isPortableTextSpan(child) || endPoint.offset === 0) {\n adjustedEndPoint = previousPotentialEndpoint\n ? {\n path: [\n {_key: previousPotentialEndpoint.blockKey},\n 'children',\n {_key: previousPotentialEndpoint.span._key},\n ],\n offset: previousPotentialEndpoint.span.text.length,\n }\n : undefined\n\n trimEndPoint = true\n break\n }\n }\n\n if (trimStartPoint) {\n const lonelySpan =\n isPortableTextSpan(child) && block.children.length === 1\n\n if (\n (isPortableTextSpan(child) && child.text.length > 0) ||\n lonelySpan\n ) {\n adjustedStartPoint = {\n path: [{_key: block._key}, 'children', {_key: child._key}],\n offset: 0,\n }\n previousPotentialEndpoint = {blockKey: block._key, span: child}\n trimStartPoint = false\n }\n\n continue\n }\n\n if (child._key === startChildKey) {\n if (!isPortableTextSpan(child)) {\n trimStartPoint = true\n continue\n }\n\n if (startPoint.offset === child.text.length) {\n trimStartPoint = true\n previousPotentialEndpoint =\n child.text.length > 0\n ? {blockKey: block._key, span: child}\n : previousPotentialEndpoint\n continue\n }\n }\n\n previousPotentialEndpoint =\n isPortableTextSpan(child) && child.text.length > 0\n ? {blockKey: block._key, span: child}\n : previousPotentialEndpoint\n }\n\n if (block._key === endBlockKey) {\n break\n }\n }\n\n const trimmedSelection = context.selection.backward\n ? {\n anchor: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,\n focus: adjustedStartPoint ?? startPoint,\n backward: true,\n }\n : {\n anchor: adjustedStartPoint ?? startPoint,\n focus: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,\n }\n\n if (\n isSelectionCollapsed({\n context: {\n ...context,\n selection: trimmedSelection,\n },\n })\n ) {\n const focusTextBlock = getFocusTextBlock({\n context: {\n ...context,\n selection: trimmedSelection,\n },\n })\n\n if (focusTextBlock && !isEmptyTextBlock(focusTextBlock.node)) {\n return null\n }\n }\n\n return trimmedSelection\n}\n","import {isPortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getSelectedSpans} from './selector.get-selected-spans'\nimport {isSelectionExpanded} from './selector.is-selection-expanded'\nimport {getFocusSpan, getSelectedBlocks} from './selectors'\n\n/**\n * @public\n */\nexport function isActiveAnnotation(\n annotation: string,\n): EditorSelector<boolean> {\n return (snapshot) => {\n if (!snapshot.context.selection) {\n return false\n }\n\n const selectedBlocks = getSelectedBlocks(snapshot)\n const focusSpan = getFocusSpan(snapshot)\n\n const selectedSpans = isSelectionExpanded(snapshot)\n ? getSelectedSpans(snapshot)\n : focusSpan\n ? [focusSpan]\n : []\n\n if (selectedSpans.length === 0) {\n return false\n }\n\n if (\n selectedSpans.some(\n (span) => !span.node.marks || span.node.marks?.length === 0,\n )\n ) {\n return false\n }\n\n const selectionMarkDefs = selectedBlocks.flatMap((block) =>\n isPortableTextTextBlock(block.node) ? (block.node.markDefs ?? []) : [],\n )\n\n return selectedSpans.every((span) => {\n const spanMarkDefs =\n span.node.marks?.flatMap((mark) => {\n const markDef = selectionMarkDefs.find(\n (markDef) => markDef._key === mark,\n )\n\n return markDef ? [markDef._type] : []\n }) ?? []\n\n return spanMarkDefs.includes(annotation)\n })\n }\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {getSelectedSpans} from './selector.get-selected-spans'\nimport {isSelectionExpanded} from './selector.is-selection-expanded'\n\n/**\n * @public\n */\nexport function isActiveDecorator(decorator: string): EditorSelector<boolean> {\n return (snapshot) => {\n if (isSelectionExpanded(snapshot)) {\n const selectedSpans = getSelectedSpans(snapshot)\n\n return (\n selectedSpans.length > 0 &&\n selectedSpans.every((span) => span.node.marks?.includes(decorator))\n )\n }\n\n return snapshot.context.activeDecorators.includes(decorator)\n }\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {getActiveListItem} from './selector.get-active-list-item'\n\n/**\n * @public\n */\nexport function isActiveListItem(listItem: string): EditorSelector<boolean> {\n return (snapshot) => {\n const activeListItem = getActiveListItem(snapshot)\n\n return activeListItem === listItem\n }\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {getActiveStyle} from './selector.get-active-style'\n\n/**\n * @public\n */\nexport function isActiveStyle(style: string): EditorSelector<boolean> {\n return (snapshot) => {\n const activeStyle = getActiveStyle(snapshot)\n\n return activeStyle === style\n }\n}\n"],"names":["getSelectedSpans","context","selection","selectedSpans","startPoint","backward","focus","anchor","endPoint","startBlockKey","isKeySegment","path","_key","undefined","endBlockKey","startSpanKey","endSpanKey","block","value","isPortableTextTextBlock","child","children","isPortableTextSpan","offset","text","length","push","node","getActiveListItem","guards","createGuards","selectedTextBlocks","getSelectedBlocks","map","filter","isTextBlock","firstTextBlock","at","firstListItem","listItem","every","getActiveStyle","firstStyle","style","getTrimmedSelection","getSelectionStartPoint","getSelectionEndPoint","isKeyedSegment","startChildKey","endChildKey","startBlockFound","adjustedStartPoint","trimStartPoint","adjustedEndPoint","trimEndPoint","previousPotentialEndpoint","isEmptyTextBlock","blockKey","span","lonelySpan","trimmedSelection","isSelectionCollapsed","focusTextBlock","getFocusTextBlock","isActiveAnnotation","annotation","snapshot","selectedBlocks","focusSpan","getFocusSpan","isSelectionExpanded","some","marks","selectionMarkDefs","flatMap","markDefs","mark","markDef","find","_type","includes","isActiveDecorator","decorator","activeDecorators","isActiveListItem","isActiveStyle"],"mappings":";;AAYO,MAAMA,mBAKTA,CAAC;AAAA,EAACC;AAAO,MAAM;AACjB,MAAI,CAACA,QAAQC;AACX,WAAO,CAAE;AAGLC,QAAAA,gBAGD,IAECC,aAAaH,QAAQC,UAAUG,WACjCJ,QAAQC,UAAUI,QAClBL,QAAQC,UAAUK,QAChBC,WAAWP,QAAQC,UAAUG,WAC/BJ,QAAQC,UAAUK,SAClBN,QAAQC,UAAUI,OAEhBG,gBAAgBC,MAAaN,aAAAA,WAAWO,KAAK,CAAC,CAAC,IACjDP,WAAWO,KAAK,CAAC,EAAEC,OACnBC,QACEC,cAAcJ,mBAAaF,SAASG,KAAK,CAAC,CAAC,IAC7CH,SAASG,KAAK,CAAC,EAAEC,OACjBC;AAEA,MAAA,CAACJ,iBAAiB,CAACK;AACdX,WAAAA;AAGHY,QAAAA,eAAeL,MAAAA,aAAaN,WAAWO,KAAK,CAAC,CAAC,IAChDP,WAAWO,KAAK,CAAC,EAAEC,OACnBC,QACEG,aAAaN,MAAAA,aAAaF,SAASG,KAAK,CAAC,CAAC,IAC5CH,SAASG,KAAK,CAAC,EAAEC,OACjBC;AAEJ,aAAWI,SAAShB,QAAQiB;AACrBC,QAAAA,MAAAA,wBAAwBF,KAAK,GAIlC;AAAIA,UAAAA,MAAML,SAASH,eAAe;AAChC,mBAAWW,SAASH,MAAMI;AACnBC,cAAAA,MAAAA,mBAAmBF,KAAK,GAI7B;AAAIL,gBAAAA,gBAAgBK,MAAMR,SAASG,cAAc;AAQ/C,kBAPIX,WAAWmB,SAASH,MAAMI,KAAKC,UACjCtB,cAAcuB,KAAK;AAAA,gBACjBC,MAAMP;AAAAA,gBACNT,MAAM,CAAC;AAAA,kBAACC,MAAMK,MAAML;AAAAA,mBAAO,YAAY;AAAA,kBAACA,MAAMQ,MAAM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E,IAChC;AAEA,aAAOC,UAAU,CAACA,QAAQE,KAAK,IAAI,CAAE;AAAA,IACtC,CAAA,KAAK,CAEYC,GAAAA,SAASf,UAAU,CACxC;AAAA,EACH;AACF;AChDO,SAASgB,kBAAkBC,WAA4C;AAC5E,SAAQhB,CAAa,aAAA;AACfI,QAAAA,6BAAAA,oBAAoBJ,QAAQ,GAAG;AAC3B/D,YAAAA,gBAAgBH,iBAAiBkE,QAAQ;AAG7C/D,aAAAA,cAAcsB,SAAS,KACvBtB,cAAcqC,MAAOkB,CAASA,SAAAA,KAAK/B,KAAK6C,OAAOQ,SAASE,SAAS,CAAC;AAAA,IAAA;AAItE,WAAOhB,SAASjE,QAAQkF,iBAAiBH,SAASE,SAAS;AAAA,EAC7D;AACF;ACdO,SAASE,iBAAiB7C,UAA2C;AAClE2B,SAAAA,CAAAA,aACiBtC,kBAAkBsC,QAAQ,MAEvB3B;AAE9B;ACNO,SAAS8C,cAAc1C,OAAwC;AAC5DuB,SAAAA,CAAAA,aACczB,eAAeyB,QAAQ,MAEpBvB;AAE3B;;;;;;;;;"}
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- var util_reverseSelection = require("./util.reverse-selection.cjs"), types = require("@sanity/types");
2
+ var util_sliceBlocks = require("./util.slice-blocks.cjs"), types = require("@sanity/types");
3
3
  function createGuards({
4
4
  schema
5
5
  }) {
@@ -14,94 +14,7 @@ function createGuards({
14
14
  isTextBlock
15
15
  };
16
16
  }
17
- const getSelectedSpans = ({
18
- context
19
- }) => {
20
- if (!context.selection)
21
- return [];
22
- const selectedSpans = [], startPoint = context.selection.backward ? context.selection.focus : context.selection.anchor, endPoint = context.selection.backward ? context.selection.anchor : context.selection.focus, startBlockKey = types.isKeySegment(startPoint.path[0]) ? startPoint.path[0]._key : void 0, endBlockKey = types.isKeySegment(endPoint.path[0]) ? endPoint.path[0]._key : void 0;
23
- if (!startBlockKey || !endBlockKey)
24
- return selectedSpans;
25
- const startSpanKey = types.isKeySegment(startPoint.path[2]) ? startPoint.path[2]._key : void 0, endSpanKey = types.isKeySegment(endPoint.path[2]) ? endPoint.path[2]._key : void 0;
26
- for (const block of context.value)
27
- if (types.isPortableTextTextBlock(block)) {
28
- if (block._key === startBlockKey) {
29
- for (const child of block.children)
30
- if (types.isPortableTextSpan(child)) {
31
- if (startSpanKey && child._key === startSpanKey) {
32
- if (startPoint.offset < child.text.length && selectedSpans.push({
33
- node: child,
34
- path: [{
35
- _key: block._key
36
- }, "children", {
37
- _key: child._key
38
- }]
39
- }), startSpanKey === endSpanKey)
40
- break;
41
- continue;
42
- }
43
- if (endSpanKey && child._key === endSpanKey) {
44
- endPoint.offset > 0 && selectedSpans.push({
45
- node: child,
46
- path: [{
47
- _key: block._key
48
- }, "children", {
49
- _key: child._key
50
- }]
51
- });
52
- break;
53
- }
54
- selectedSpans.length > 0 && selectedSpans.push({
55
- node: child,
56
- path: [{
57
- _key: block._key
58
- }, "children", {
59
- _key: child._key
60
- }]
61
- });
62
- }
63
- if (startBlockKey === endBlockKey)
64
- break;
65
- continue;
66
- }
67
- if (block._key === endBlockKey) {
68
- for (const child of block.children)
69
- if (types.isPortableTextSpan(child)) {
70
- if (endSpanKey && child._key === endSpanKey) {
71
- endPoint.offset > 0 && selectedSpans.push({
72
- node: child,
73
- path: [{
74
- _key: block._key
75
- }, "children", {
76
- _key: child._key
77
- }]
78
- });
79
- break;
80
- }
81
- selectedSpans.push({
82
- node: child,
83
- path: [{
84
- _key: block._key
85
- }, "children", {
86
- _key: child._key
87
- }]
88
- });
89
- }
90
- break;
91
- }
92
- if (selectedSpans.length > 0)
93
- for (const child of block.children)
94
- types.isPortableTextSpan(child) && selectedSpans.push({
95
- node: child,
96
- path: [{
97
- _key: block._key
98
- }, "children", {
99
- _key: child._key
100
- }]
101
- });
102
- }
103
- return selectedSpans;
104
- }, getFocusBlock = ({
17
+ const getFocusBlock = ({
105
18
  context
106
19
  }) => {
107
20
  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;
@@ -297,32 +210,6 @@ const getSelectedSpans = ({
297
210
  }
298
211
  if (foundSelectionEndBlock && nextBlock)
299
212
  return nextBlock;
300
- }, getActiveListItem = ({
301
- context
302
- }) => {
303
- if (!context.selection)
304
- return;
305
- const guards = createGuards(context), selectedTextBlocks = getSelectedBlocks({
306
- context
307
- }).map((block) => block.node).filter(guards.isTextBlock), firstTextBlock = selectedTextBlocks.at(0);
308
- if (!firstTextBlock)
309
- return;
310
- const firstListItem = firstTextBlock.listItem;
311
- if (firstListItem && selectedTextBlocks.every((block) => block.listItem === firstListItem))
312
- return firstListItem;
313
- }, getActiveStyle = ({
314
- context
315
- }) => {
316
- if (!context.selection)
317
- return;
318
- const guards = createGuards(context), selectedTextBlocks = getSelectedBlocks({
319
- context
320
- }).map((block) => block.node).filter(guards.isTextBlock), firstTextBlock = selectedTextBlocks.at(0);
321
- if (!firstTextBlock)
322
- return;
323
- const firstStyle = firstTextBlock.style;
324
- if (firstStyle && selectedTextBlocks.every((block) => block.style === firstStyle))
325
- return firstStyle;
326
213
  }, getSelectionEndPoint = ({
327
214
  context
328
215
  }) => {
@@ -349,7 +236,7 @@ const getSelectedSpans = ({
349
236
  endPointChildFound = !0;
350
237
  continue;
351
238
  }
352
- if (!util_reverseSelection.isSpan(context, child) && endPointChildFound) {
239
+ if (!util_sliceBlocks.isSpan(context, child) && endPointChildFound) {
353
240
  inlineObject = {
354
241
  node: child,
355
242
  path: [...focusTextBlock.path, "children", {
@@ -374,7 +261,7 @@ const getSelectedSpans = ({
374
261
  for (const child of focusTextBlock.node.children) {
375
262
  if (child._key === selectionStartPointChildKey)
376
263
  break;
377
- util_reverseSelection.isSpan(context, child) || (inlineObject = {
264
+ util_sliceBlocks.isSpan(context, child) || (inlineObject = {
378
265
  node: child,
379
266
  path: [...focusTextBlock.path, "children", {
380
267
  _key: child._key
@@ -382,42 +269,16 @@ const getSelectedSpans = ({
382
269
  });
383
270
  }
384
271
  return inlineObject;
385
- }, getSelectionText = ({
272
+ }, getSelectedSlice = ({
386
273
  context
387
- }) => {
388
- let text = "";
389
- const {
390
- value,
391
- selection
392
- } = context;
393
- if (!value || !selection)
394
- return text;
395
- const forwardSelection = selection.backward ? util_reverseSelection.reverseSelection(selection) : selection;
396
- if (!forwardSelection)
397
- return text;
398
- for (const block of value)
399
- if (!(util_reverseSelection.isKeyedSegment(forwardSelection.anchor.path[0]) && block._key !== forwardSelection.anchor.path[0]._key) && types.isPortableTextTextBlock(block)) {
400
- for (const child of block.children)
401
- if (types.isPortableTextSpan(child)) {
402
- if (util_reverseSelection.isKeyedSegment(forwardSelection.anchor.path[2]) && child._key === forwardSelection.anchor.path[2]._key && util_reverseSelection.isKeyedSegment(forwardSelection.focus.path[2]) && child._key === forwardSelection.focus.path[2]._key) {
403
- text = text + child.text.slice(forwardSelection.anchor.offset, forwardSelection.focus.offset);
404
- break;
405
- }
406
- if (util_reverseSelection.isKeyedSegment(forwardSelection.anchor.path[2]) && child._key === forwardSelection.anchor.path[2]._key) {
407
- text = text + child.text.slice(forwardSelection.anchor.offset);
408
- continue;
409
- }
410
- if (util_reverseSelection.isKeyedSegment(forwardSelection.focus.path[2]) && child._key === forwardSelection.focus.path[2]._key) {
411
- text = text + child.text.slice(0, forwardSelection.focus.offset);
412
- break;
413
- }
414
- text.length > 0 && (text = text + child.text);
415
- }
416
- if (util_reverseSelection.isKeyedSegment(forwardSelection.focus.path[0]) && block._key === forwardSelection.focus.path[0]._key)
417
- break;
418
- }
419
- return text;
420
- }, isSelectionCollapsed = ({
274
+ }) => util_sliceBlocks.sliceBlocks({
275
+ blocks: context.value,
276
+ selection: context.selection
277
+ }), getSelectionText = ({
278
+ context
279
+ }) => getSelectedSlice({
280
+ context
281
+ }).reduce((text, block) => types.isPortableTextTextBlock(block) ? text + block.children.reduce((text2, child) => types.isPortableTextSpan(child) ? text2 + child.text : text2, "") : text, ""), isSelectionCollapsed = ({
421
282
  context
422
283
  }) => context.selection ? JSON.stringify(context.selection.anchor.path) === JSON.stringify(context.selection.focus.path) && context.selection?.anchor.offset === context.selection?.focus.offset : !1, isSelectionExpanded = ({
423
284
  context
@@ -434,7 +295,7 @@ const getSelectedSpans = ({
434
295
  context
435
296
  }), selectionStartPoint = getSelectionStartPoint({
436
297
  context
437
- }), selectionStartOffset = selectionStartPoint ? util_reverseSelection.spanSelectionPointToBlockOffset({
298
+ }), selectionStartOffset = selectionStartPoint ? util_sliceBlocks.spanSelectionPointToBlockOffset({
438
299
  value: context.value,
439
300
  selectionPoint: selectionStartPoint
440
301
  }) : void 0;
@@ -442,7 +303,7 @@ const getSelectedSpans = ({
442
303
  return null;
443
304
  const previousInlineObject = getPreviousInlineObject({
444
305
  context
445
- }), blockStartPoint = util_reverseSelection.getBlockStartPoint(focusTextBlock), textDirectlyBefore = getSelectionText({
306
+ }), blockStartPoint = util_sliceBlocks.getBlockStartPoint(focusTextBlock), textDirectlyBefore = getSelectionText({
446
307
  context: {
447
308
  ...context,
448
309
  selection: {
@@ -455,7 +316,7 @@ const getSelectedSpans = ({
455
316
  }
456
317
  }).split(/\s+/).at(-1), nextInlineObject = getNextInlineObject({
457
318
  context
458
- }), blockEndPoint = util_reverseSelection.getBlockEndPoint(focusTextBlock), textDirectlyAfter = getSelectionText({
319
+ }), blockEndPoint = util_sliceBlocks.getBlockEndPoint(focusTextBlock), textDirectlyAfter = getSelectionText({
459
320
  context: {
460
321
  ...context,
461
322
  selection: {
@@ -466,18 +327,23 @@ const getSelectedSpans = ({
466
327
  } : blockEndPoint
467
328
  }
468
329
  }
469
- }).split(/\s+/).at(0), caretWordStartOffset = textDirectlyBefore ? {
330
+ }).split(/\s+/).at(0);
331
+ if ((textDirectlyBefore === void 0 || textDirectlyBefore === "") && (textDirectlyAfter === void 0 || textDirectlyAfter === ""))
332
+ return null;
333
+ const caretWordStartOffset = textDirectlyBefore ? {
470
334
  ...selectionStartOffset,
471
335
  offset: selectionStartOffset.offset - textDirectlyBefore.length
472
336
  } : selectionStartOffset, caretWordEndOffset = textDirectlyAfter ? {
473
337
  ...selectionStartOffset,
474
338
  offset: selectionStartOffset.offset + textDirectlyAfter.length
475
- } : selectionStartOffset, caretWordStartSelectionPoint = util_reverseSelection.blockOffsetToSpanSelectionPoint({
339
+ } : selectionStartOffset, caretWordStartSelectionPoint = util_sliceBlocks.blockOffsetToSpanSelectionPoint({
476
340
  value: context.value,
477
- blockOffset: caretWordStartOffset
478
- }), caretWordEndSelectionPoint = util_reverseSelection.blockOffsetToSpanSelectionPoint({
341
+ blockOffset: caretWordStartOffset,
342
+ direction: "backward"
343
+ }), caretWordEndSelectionPoint = util_sliceBlocks.blockOffsetToSpanSelectionPoint({
479
344
  value: context.value,
480
- blockOffset: caretWordEndOffset
345
+ blockOffset: caretWordEndOffset,
346
+ direction: "forward"
481
347
  });
482
348
  if (!caretWordStartSelectionPoint || !caretWordEndSelectionPoint)
483
349
  return null;
@@ -492,35 +358,6 @@ const getSelectedSpans = ({
492
358
  }
493
359
  }) ? caretWordSelection : null;
494
360
  };
495
- function isActiveAnnotation(annotation) {
496
- return (snapshot) => {
497
- if (!snapshot.context.selection)
498
- return !1;
499
- const selectedBlocks = getSelectedBlocks(snapshot), focusSpan = getFocusSpan(snapshot), selectedSpans = isSelectionExpanded(snapshot) ? getSelectedSpans(snapshot) : focusSpan ? [focusSpan] : [];
500
- if (selectedSpans.length === 0 || selectedSpans.some((span) => !span.node.marks || span.node.marks?.length === 0))
501
- return !1;
502
- const selectionMarkDefs = selectedBlocks.flatMap((block) => types.isPortableTextTextBlock(block.node) ? block.node.markDefs ?? [] : []);
503
- return selectedSpans.every((span) => (span.node.marks?.flatMap((mark) => {
504
- const markDef = selectionMarkDefs.find((markDef2) => markDef2._key === mark);
505
- return markDef ? [markDef._type] : [];
506
- }) ?? []).includes(annotation));
507
- };
508
- }
509
- function isActiveDecorator(decorator) {
510
- return (snapshot) => {
511
- if (isSelectionExpanded(snapshot)) {
512
- const selectedSpans = getSelectedSpans(snapshot);
513
- return selectedSpans.length > 0 && selectedSpans.every((span) => span.node.marks?.includes(decorator));
514
- }
515
- return snapshot.context.activeDecorators.includes(decorator);
516
- };
517
- }
518
- function isActiveListItem(listItem) {
519
- return (snapshot) => getActiveListItem(snapshot) === listItem;
520
- }
521
- function isActiveStyle(style) {
522
- return (snapshot) => getActiveStyle(snapshot) === style;
523
- }
524
361
  function isAtTheEndOfBlock(block) {
525
362
  return ({
526
363
  context
@@ -529,8 +366,8 @@ function isAtTheEndOfBlock(block) {
529
366
  context
530
367
  }))
531
368
  return !1;
532
- const blockEndPoint = util_reverseSelection.getBlockEndPoint(block);
533
- return util_reverseSelection.isEqualSelectionPoints(context.selection.focus, blockEndPoint);
369
+ const blockEndPoint = util_sliceBlocks.getBlockEndPoint(block);
370
+ return util_sliceBlocks.isEqualSelectionPoints(context.selection.focus, blockEndPoint);
534
371
  };
535
372
  }
536
373
  function isAtTheStartOfBlock(block) {
@@ -541,13 +378,11 @@ function isAtTheStartOfBlock(block) {
541
378
  context
542
379
  }))
543
380
  return !1;
544
- const blockStartPoint = util_reverseSelection.getBlockStartPoint(block);
545
- return util_reverseSelection.isEqualSelectionPoints(context.selection.focus, blockStartPoint);
381
+ const blockStartPoint = util_sliceBlocks.getBlockStartPoint(block);
382
+ return util_sliceBlocks.isEqualSelectionPoints(context.selection.focus, blockStartPoint);
546
383
  };
547
384
  }
548
385
  exports.createGuards = createGuards;
549
- exports.getActiveListItem = getActiveListItem;
550
- exports.getActiveStyle = getActiveStyle;
551
386
  exports.getCaretWordSelection = getCaretWordSelection;
552
387
  exports.getFirstBlock = getFirstBlock;
553
388
  exports.getFocusBlock = getFocusBlock;
@@ -562,16 +397,12 @@ exports.getNextInlineObject = getNextInlineObject;
562
397
  exports.getPreviousBlock = getPreviousBlock;
563
398
  exports.getPreviousInlineObject = getPreviousInlineObject;
564
399
  exports.getSelectedBlocks = getSelectedBlocks;
565
- exports.getSelectedSpans = getSelectedSpans;
400
+ exports.getSelectedSlice = getSelectedSlice;
566
401
  exports.getSelectionEndBlock = getSelectionEndBlock;
567
402
  exports.getSelectionEndPoint = getSelectionEndPoint;
568
403
  exports.getSelectionStartBlock = getSelectionStartBlock;
569
404
  exports.getSelectionStartPoint = getSelectionStartPoint;
570
405
  exports.getSelectionText = getSelectionText;
571
- exports.isActiveAnnotation = isActiveAnnotation;
572
- exports.isActiveDecorator = isActiveDecorator;
573
- exports.isActiveListItem = isActiveListItem;
574
- exports.isActiveStyle = isActiveStyle;
575
406
  exports.isAtTheEndOfBlock = isAtTheEndOfBlock;
576
407
  exports.isAtTheStartOfBlock = isAtTheStartOfBlock;
577
408
  exports.isSelectionCollapsed = isSelectionCollapsed;