@portabletext/editor 1.15.3 → 1.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +25 -25
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/selector.get-text-before.cjs +14 -14
  4. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  5. package/lib/_chunks-cjs/{selectors.cjs → selector.is-selection-collapsed.cjs} +8 -8
  6. package/lib/_chunks-cjs/selector.is-selection-collapsed.cjs.map +1 -0
  7. package/lib/_chunks-es/behavior.core.js +7 -7
  8. package/lib/_chunks-es/behavior.core.js.map +1 -1
  9. package/lib/_chunks-es/selector.get-text-before.js +14 -14
  10. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  11. package/lib/_chunks-es/{selectors.js → selector.is-selection-collapsed.js} +8 -8
  12. package/lib/_chunks-es/selector.is-selection-collapsed.js.map +1 -0
  13. package/lib/behaviors/index.cjs +23 -23
  14. package/lib/behaviors/index.cjs.map +1 -1
  15. package/lib/behaviors/index.d.cts +1 -0
  16. package/lib/behaviors/index.d.ts +1 -0
  17. package/lib/behaviors/index.js +8 -8
  18. package/lib/behaviors/index.js.map +1 -1
  19. package/lib/index.cjs +855 -515
  20. package/lib/index.cjs.map +1 -1
  21. package/lib/index.d.cts +4126 -4834
  22. package/lib/index.d.ts +4126 -4834
  23. package/lib/index.js +852 -514
  24. package/lib/index.js.map +1 -1
  25. package/lib/selectors/index.cjs +166 -16
  26. package/lib/selectors/index.cjs.map +1 -1
  27. package/lib/selectors/index.d.cts +54 -5
  28. package/lib/selectors/index.d.ts +54 -5
  29. package/lib/selectors/index.js +154 -3
  30. package/lib/selectors/index.js.map +1 -1
  31. package/package.json +11 -11
  32. package/src/behaviors/behavior.code-editor.ts +5 -9
  33. package/src/behaviors/behavior.core.block-objects.ts +13 -19
  34. package/src/behaviors/behavior.core.lists.ts +11 -17
  35. package/src/behaviors/behavior.links.ts +4 -4
  36. package/src/behaviors/behavior.markdown.ts +16 -21
  37. package/src/editor/Editable.tsx +11 -4
  38. package/src/editor/PortableTextEditor.tsx +2 -3
  39. package/src/editor/{hooks/useSyncValue.test.tsx → __tests__/sync-value.test.tsx} +42 -23
  40. package/src/editor/components/Synchronizer.tsx +53 -80
  41. package/src/editor/editor-machine.ts +129 -80
  42. package/src/editor/editor-provider.tsx +0 -3
  43. package/src/editor/editor-selector.ts +5 -0
  44. package/src/editor/editor-snapshot.ts +1 -0
  45. package/src/editor/get-active-decorators.ts +20 -0
  46. package/src/editor/mutation-machine.ts +100 -0
  47. package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +21 -15
  48. package/src/editor/plugins/createWithMaxBlocks.ts +1 -1
  49. package/src/editor/plugins/createWithPatches.ts +0 -4
  50. package/src/editor/plugins/createWithPlaceholderBlock.ts +1 -1
  51. package/src/editor/plugins/createWithPortableTextSelections.ts +4 -1
  52. package/src/editor/plugins/createWithUndoRedo.ts +3 -3
  53. package/src/editor/sync-machine.ts +657 -0
  54. package/src/editor/withSyncRangeDecorations.ts +17 -5
  55. package/src/selectors/_exports/index.ts +1 -0
  56. package/src/selectors/index.ts +9 -1
  57. package/src/selectors/selector.get-active-style.ts +37 -0
  58. package/src/selectors/selector.get-selected-spans.ts +136 -0
  59. package/src/selectors/selector.is-active-annotation.ts +49 -0
  60. package/src/selectors/selector.is-active-decorator.ts +21 -0
  61. package/src/selectors/selector.is-active-list-item.ts +13 -0
  62. package/src/selectors/selector.is-active-style.ts +13 -0
  63. package/src/selectors/selector.is-selection-collapsed.ts +12 -0
  64. package/src/selectors/selector.is-selection-expanded.ts +9 -0
  65. package/src/selectors/selectors.ts +0 -11
  66. package/src/utils/weakMaps.ts +0 -3
  67. package/src/utils/withChanges.ts +1 -8
  68. package/lib/_chunks-cjs/selectors.cjs.map +0 -1
  69. package/lib/_chunks-es/selectors.js.map +0 -1
  70. package/src/editor/hooks/useSyncValue.ts +0 -426
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var selectors = require("../_chunks-cjs/selectors.cjs"), selector_getTextBefore = require("../_chunks-cjs/selector.get-text-before.cjs");
3
+ var selector_isSelectionCollapsed = require("../_chunks-cjs/selector.is-selection-collapsed.cjs"), types = require("@sanity/types"), selector_getTextBefore = require("../_chunks-cjs/selector.get-text-before.cjs");
4
4
  const getActiveListItem = ({
5
5
  context
6
6
  }) => {
7
7
  if (!context.selection)
8
8
  return;
9
- const guards = selectors.createGuards(context), selectedTextBlocks = selectors.getSelectedBlocks({
9
+ const guards = selector_isSelectionCollapsed.createGuards(context), selectedTextBlocks = selector_isSelectionCollapsed.getSelectedBlocks({
10
10
  context
11
11
  }).map((block) => block.node).filter(guards.isTextBlock), firstTextBlock = selectedTextBlocks.at(0);
12
12
  if (!firstTextBlock)
@@ -14,22 +14,172 @@ const getActiveListItem = ({
14
14
  const firstListItem = firstTextBlock.listItem;
15
15
  if (firstListItem && selectedTextBlocks.every((block) => block.listItem === firstListItem))
16
16
  return firstListItem;
17
+ }, getActiveStyle = ({
18
+ context
19
+ }) => {
20
+ if (!context.selection)
21
+ return;
22
+ const guards = selector_isSelectionCollapsed.createGuards(context), selectedTextBlocks = selector_isSelectionCollapsed.getSelectedBlocks({
23
+ context
24
+ }).map((block) => block.node).filter(guards.isTextBlock), firstTextBlock = selectedTextBlocks.at(0);
25
+ if (!firstTextBlock)
26
+ return;
27
+ const firstStyle = firstTextBlock.style;
28
+ if (firstStyle && selectedTextBlocks.every((block) => block.style === firstStyle))
29
+ return firstStyle;
30
+ }, getSelectedSpans = ({
31
+ context
32
+ }) => {
33
+ if (!context.selection)
34
+ return [];
35
+ 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;
36
+ if (!startBlockKey || !endBlockKey)
37
+ return selectedSpans;
38
+ 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;
39
+ for (const block of context.value)
40
+ if (types.isPortableTextTextBlock(block)) {
41
+ if (block._key === startBlockKey) {
42
+ for (const child of block.children)
43
+ if (types.isPortableTextSpan(child)) {
44
+ if (startSpanKey && child._key === startSpanKey && (selectedSpans.push({
45
+ node: child,
46
+ path: [{
47
+ _key: block._key
48
+ }, "children", {
49
+ _key: child._key
50
+ }]
51
+ }), startBlockKey === endBlockKey))
52
+ break;
53
+ if (endSpanKey && child._key === endSpanKey) {
54
+ selectedSpans.push({
55
+ node: child,
56
+ path: [{
57
+ _key: block._key
58
+ }, "children", {
59
+ _key: child._key
60
+ }]
61
+ });
62
+ break;
63
+ }
64
+ selectedSpans.length > 0 && selectedSpans.push({
65
+ node: child,
66
+ path: [{
67
+ _key: block._key
68
+ }, "children", {
69
+ _key: child._key
70
+ }]
71
+ });
72
+ }
73
+ if (startBlockKey === endBlockKey)
74
+ break;
75
+ continue;
76
+ }
77
+ if (block._key === endBlockKey) {
78
+ for (const child of block.children)
79
+ if (types.isPortableTextSpan(child)) {
80
+ if (endSpanKey && child._key === endSpanKey) {
81
+ selectedSpans.push({
82
+ node: child,
83
+ path: [{
84
+ _key: block._key
85
+ }, "children", {
86
+ _key: child._key
87
+ }]
88
+ });
89
+ break;
90
+ }
91
+ selectedSpans.push({
92
+ node: child,
93
+ path: [{
94
+ _key: block._key
95
+ }, "children", {
96
+ _key: child._key
97
+ }]
98
+ });
99
+ }
100
+ break;
101
+ }
102
+ if (selectedSpans.length > 0)
103
+ for (const child of block.children)
104
+ types.isPortableTextSpan(child) && selectedSpans.push({
105
+ node: child,
106
+ path: [{
107
+ _key: block._key
108
+ }, "children", {
109
+ _key: child._key
110
+ }]
111
+ });
112
+ }
113
+ return selectedSpans;
17
114
  };
18
- exports.getFirstBlock = selectors.getFirstBlock;
19
- exports.getFocusBlock = selectors.getFocusBlock;
20
- exports.getFocusBlockObject = selectors.getFocusBlockObject;
21
- exports.getFocusChild = selectors.getFocusChild;
22
- exports.getFocusListBlock = selectors.getFocusListBlock;
23
- exports.getFocusSpan = selectors.getFocusSpan;
24
- exports.getFocusTextBlock = selectors.getFocusTextBlock;
25
- exports.getLastBlock = selectors.getLastBlock;
26
- exports.getNextBlock = selectors.getNextBlock;
27
- exports.getPreviousBlock = selectors.getPreviousBlock;
28
- exports.getSelectedBlocks = selectors.getSelectedBlocks;
29
- exports.getSelectionEndBlock = selectors.getSelectionEndBlock;
30
- exports.getSelectionStartBlock = selectors.getSelectionStartBlock;
31
- exports.selectionIsCollapsed = selectors.selectionIsCollapsed;
115
+ function isActiveAnnotation(annotation) {
116
+ return (snapshot) => {
117
+ if (!snapshot.context.selection)
118
+ return !1;
119
+ const selectedBlocks = selector_isSelectionCollapsed.getSelectedBlocks(snapshot), selectedSpans = getSelectedSpans(snapshot);
120
+ if (selectedSpans.length === 0 || selectedSpans.some((span) => {
121
+ var _a;
122
+ return !span.node.marks || ((_a = span.node.marks) == null ? void 0 : _a.length) === 0;
123
+ }))
124
+ return !1;
125
+ const selectionMarkDefs = selectedBlocks.flatMap((block) => {
126
+ var _a;
127
+ return types.isPortableTextTextBlock(block.node) ? (_a = block.node.markDefs) != null ? _a : [] : [];
128
+ });
129
+ return selectedSpans.every((span) => {
130
+ var _a, _b;
131
+ return ((_b = (_a = span.node.marks) == null ? void 0 : _a.flatMap((mark) => {
132
+ const markDef = selectionMarkDefs.find((markDef2) => markDef2._key === mark);
133
+ return markDef ? [markDef._type] : [];
134
+ })) != null ? _b : []).includes(annotation);
135
+ });
136
+ };
137
+ }
138
+ const isSelectionExpanded = ({
139
+ context
140
+ }) => !selector_isSelectionCollapsed.isSelectionCollapsed({
141
+ context
142
+ });
143
+ function isActiveDecorator(decorator) {
144
+ return (snapshot) => {
145
+ if (isSelectionExpanded(snapshot)) {
146
+ const selectedSpans = getSelectedSpans(snapshot);
147
+ return selectedSpans.length > 0 && selectedSpans.every((span) => {
148
+ var _a;
149
+ return (_a = span.node.marks) == null ? void 0 : _a.includes(decorator);
150
+ });
151
+ }
152
+ return snapshot.context.activeDecorators.includes(decorator);
153
+ };
154
+ }
155
+ function isActiveListItem(listItem) {
156
+ return (snapshot) => getActiveListItem(snapshot) === listItem;
157
+ }
158
+ function isActiveStyle(style) {
159
+ return (snapshot) => getActiveStyle(snapshot) === style;
160
+ }
161
+ exports.getFirstBlock = selector_isSelectionCollapsed.getFirstBlock;
162
+ exports.getFocusBlock = selector_isSelectionCollapsed.getFocusBlock;
163
+ exports.getFocusBlockObject = selector_isSelectionCollapsed.getFocusBlockObject;
164
+ exports.getFocusChild = selector_isSelectionCollapsed.getFocusChild;
165
+ exports.getFocusListBlock = selector_isSelectionCollapsed.getFocusListBlock;
166
+ exports.getFocusSpan = selector_isSelectionCollapsed.getFocusSpan;
167
+ exports.getFocusTextBlock = selector_isSelectionCollapsed.getFocusTextBlock;
168
+ exports.getLastBlock = selector_isSelectionCollapsed.getLastBlock;
169
+ exports.getNextBlock = selector_isSelectionCollapsed.getNextBlock;
170
+ exports.getPreviousBlock = selector_isSelectionCollapsed.getPreviousBlock;
171
+ exports.getSelectedBlocks = selector_isSelectionCollapsed.getSelectedBlocks;
172
+ exports.getSelectionEndBlock = selector_isSelectionCollapsed.getSelectionEndBlock;
173
+ exports.getSelectionStartBlock = selector_isSelectionCollapsed.getSelectionStartBlock;
174
+ exports.isSelectionCollapsed = selector_isSelectionCollapsed.isSelectionCollapsed;
32
175
  exports.getBlockTextBefore = selector_getTextBefore.getBlockTextBefore;
33
176
  exports.getSelectionText = selector_getTextBefore.getSelectionText;
34
177
  exports.getActiveListItem = getActiveListItem;
178
+ exports.getActiveStyle = getActiveStyle;
179
+ exports.getSelectedSpans = getSelectedSpans;
180
+ exports.isActiveAnnotation = isActiveAnnotation;
181
+ exports.isActiveDecorator = isActiveDecorator;
182
+ exports.isActiveListItem = isActiveListItem;
183
+ exports.isActiveStyle = isActiveStyle;
184
+ exports.isSelectionExpanded = isSelectionExpanded;
35
185
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/selectors/selector.get-active-list-item.ts"],"sourcesContent":["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 * @alpha\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"],"names":["getActiveListItem","context","selection","guards","createGuards","selectedTextBlocks","getSelectedBlocks","map","block","node","filter","isTextBlock","firstTextBlock","at","firstListItem","listItem","every"],"mappings":";;;AAQO,MAAMA,oBAETA,CAAC;AAAA,EAACC;AAAO,MAAM;AACjB,MAAI,CAACA,QAAQC;AACX;AAGF,QAAMC,SAASC,UAAAA,aAAaH,OAAO,GAE7BI,qBADiBC,UAAAA,kBAAkB;AAAA,IAACL;AAAAA,EAAQ,CAAA,EAAEM,IAAKC,CAAAA,UAAUA,MAAMC,IAAI,EACnCC,OAAOP,OAAOQ,WAAW,GAE7DC,iBAAiBP,mBAAmBQ,GAAG,CAAC;AAE9C,MAAI,CAACD;AACH;AAGF,QAAME,gBAAgBF,eAAeG;AAErC,MAAKD,iBAIDT,mBAAmBW,MAAOR,CAAUA,UAAAA,MAAMO,aAAaD,aAAa;AAC/DA,WAAAA;AAIX;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/selectors/selector.get-active-list-item.ts","../../src/selectors/selector.get-active-style.ts","../../src/selectors/selector.get-selected-spans.ts","../../src/selectors/selector.is-active-annotation.ts","../../src/selectors/selector.is-selection-expanded.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 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 * @alpha\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 * @alpha\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 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 * @alpha\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 selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\n })\n\n if (startBlockKey === endBlockKey) {\n break\n }\n }\n\n if (endSpanKey && child._key === endSpanKey) {\n selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\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 selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\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 {isPortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getSelectedSpans} from './selector.get-selected-spans'\nimport {getSelectedBlocks} from './selectors'\n\n/**\n * @alpha\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 selectedSpans = getSelectedSpans(snapshot)\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 {isSelectionCollapsed} from './selector.is-selection-collapsed'\n\n/**\n * @alpha\n */\nexport const isSelectionExpanded: EditorSelector<boolean> = ({context}) => {\n return !isSelectionCollapsed({context})\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 * @alpha\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 * @alpha\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 * @alpha\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":["getActiveListItem","context","selection","guards","createGuards","selectedTextBlocks","getSelectedBlocks","map","block","node","filter","isTextBlock","firstTextBlock","at","firstListItem","listItem","every","getActiveStyle","firstStyle","style","getSelectedSpans","selectedSpans","startPoint","backward","focus","anchor","endPoint","startBlockKey","isKeySegment","path","_key","undefined","endBlockKey","startSpanKey","endSpanKey","value","isPortableTextTextBlock","child","children","isPortableTextSpan","push","length","isActiveAnnotation","annotation","snapshot","selectedBlocks","some","span","marks","selectionMarkDefs","flatMap","markDefs","mark","markDef","find","_type","includes","isSelectionExpanded","isSelectionCollapsed","isActiveDecorator","decorator","activeDecorators","isActiveListItem","isActiveStyle"],"mappings":";;;AAQO,MAAMA,oBAETA,CAAC;AAAA,EAACC;AAAO,MAAM;AACjB,MAAI,CAACA,QAAQC;AAC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SAASgD,cAAc5C,OAAwC;AAC5DyB,SAAAA,CAAAA,aACc3B,eAAe2B,QAAQ,MAEpBzB;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -19,6 +19,7 @@ import {
19
19
  * @alpha
20
20
  */
21
21
  export declare type EditorContext = {
22
+ activeDecorators: Array<string>
22
23
  keyGenerator: () => string
23
24
  schema: EditorSchema
24
25
  selection: EditorSelection
@@ -64,6 +65,13 @@ export declare const getActiveListItem: EditorSelector<
64
65
  PortableTextListBlock['listItem'] | undefined
65
66
  >
66
67
 
68
+ /**
69
+ * @alpha
70
+ */
71
+ export declare const getActiveStyle: EditorSelector<
72
+ PortableTextTextBlock['style']
73
+ >
74
+
67
75
  /**
68
76
  * @alpha
69
77
  */
@@ -189,6 +197,16 @@ export declare const getSelectedBlocks: EditorSelector<
189
197
  }>
190
198
  >
191
199
 
200
+ /**
201
+ * @alpha
202
+ */
203
+ export declare const getSelectedSpans: EditorSelector<
204
+ Array<{
205
+ node: PortableTextSpan
206
+ path: [KeyedSegment, 'children', KeyedSegment]
207
+ }>
208
+ >
209
+
192
210
  /**
193
211
  * @alpha
194
212
  */
@@ -216,6 +234,42 @@ export declare const getSelectionStartBlock: EditorSelector<
216
234
  */
217
235
  export declare const getSelectionText: EditorSelector<string>
218
236
 
237
+ /**
238
+ * @alpha
239
+ */
240
+ export declare function isActiveAnnotation(
241
+ annotation: string,
242
+ ): EditorSelector<boolean>
243
+
244
+ /**
245
+ * @alpha
246
+ */
247
+ export declare function isActiveDecorator(
248
+ decorator: string,
249
+ ): EditorSelector<boolean>
250
+
251
+ /**
252
+ * @alpha
253
+ */
254
+ export declare function isActiveListItem(
255
+ listItem: string,
256
+ ): EditorSelector<boolean>
257
+
258
+ /**
259
+ * @alpha
260
+ */
261
+ export declare function isActiveStyle(style: string): EditorSelector<boolean>
262
+
263
+ /**
264
+ * @alpha
265
+ */
266
+ export declare const isSelectionCollapsed: EditorSelector<boolean>
267
+
268
+ /**
269
+ * @alpha
270
+ */
271
+ export declare const isSelectionExpanded: EditorSelector<boolean>
272
+
219
273
  /** @internal */
220
274
  export declare type PortableTextMemberSchemaTypes = {
221
275
  annotations: (ObjectSchemaType & {
@@ -231,9 +285,4 @@ export declare type PortableTextMemberSchemaTypes = {
231
285
  lists: BlockListDefinition[]
232
286
  }
233
287
 
234
- /**
235
- * @alpha
236
- */
237
- export declare const selectionIsCollapsed: EditorSelector<boolean>
238
-
239
288
  export {}
@@ -19,6 +19,7 @@ import {
19
19
  * @alpha
20
20
  */
21
21
  export declare type EditorContext = {
22
+ activeDecorators: Array<string>
22
23
  keyGenerator: () => string
23
24
  schema: EditorSchema
24
25
  selection: EditorSelection
@@ -64,6 +65,13 @@ export declare const getActiveListItem: EditorSelector<
64
65
  PortableTextListBlock['listItem'] | undefined
65
66
  >
66
67
 
68
+ /**
69
+ * @alpha
70
+ */
71
+ export declare const getActiveStyle: EditorSelector<
72
+ PortableTextTextBlock['style']
73
+ >
74
+
67
75
  /**
68
76
  * @alpha
69
77
  */
@@ -189,6 +197,16 @@ export declare const getSelectedBlocks: EditorSelector<
189
197
  }>
190
198
  >
191
199
 
200
+ /**
201
+ * @alpha
202
+ */
203
+ export declare const getSelectedSpans: EditorSelector<
204
+ Array<{
205
+ node: PortableTextSpan
206
+ path: [KeyedSegment, 'children', KeyedSegment]
207
+ }>
208
+ >
209
+
192
210
  /**
193
211
  * @alpha
194
212
  */
@@ -216,6 +234,42 @@ export declare const getSelectionStartBlock: EditorSelector<
216
234
  */
217
235
  export declare const getSelectionText: EditorSelector<string>
218
236
 
237
+ /**
238
+ * @alpha
239
+ */
240
+ export declare function isActiveAnnotation(
241
+ annotation: string,
242
+ ): EditorSelector<boolean>
243
+
244
+ /**
245
+ * @alpha
246
+ */
247
+ export declare function isActiveDecorator(
248
+ decorator: string,
249
+ ): EditorSelector<boolean>
250
+
251
+ /**
252
+ * @alpha
253
+ */
254
+ export declare function isActiveListItem(
255
+ listItem: string,
256
+ ): EditorSelector<boolean>
257
+
258
+ /**
259
+ * @alpha
260
+ */
261
+ export declare function isActiveStyle(style: string): EditorSelector<boolean>
262
+
263
+ /**
264
+ * @alpha
265
+ */
266
+ export declare const isSelectionCollapsed: EditorSelector<boolean>
267
+
268
+ /**
269
+ * @alpha
270
+ */
271
+ export declare const isSelectionExpanded: EditorSelector<boolean>
272
+
219
273
  /** @internal */
220
274
  export declare type PortableTextMemberSchemaTypes = {
221
275
  annotations: (ObjectSchemaType & {
@@ -231,9 +285,4 @@ export declare type PortableTextMemberSchemaTypes = {
231
285
  lists: BlockListDefinition[]
232
286
  }
233
287
 
234
- /**
235
- * @alpha
236
- */
237
- export declare const selectionIsCollapsed: EditorSelector<boolean>
238
-
239
288
  export {}
@@ -1,5 +1,6 @@
1
- import { getSelectedBlocks, createGuards } from "../_chunks-es/selectors.js";
2
- import { getFirstBlock, getFocusBlock, getFocusBlockObject, getFocusChild, getFocusListBlock, getFocusSpan, getFocusTextBlock, getLastBlock, getNextBlock, getPreviousBlock, getSelectionEndBlock, getSelectionStartBlock, selectionIsCollapsed } from "../_chunks-es/selectors.js";
1
+ import { getSelectedBlocks, createGuards, isSelectionCollapsed } from "../_chunks-es/selector.is-selection-collapsed.js";
2
+ import { getFirstBlock, getFocusBlock, getFocusBlockObject, getFocusChild, getFocusListBlock, getFocusSpan, getFocusTextBlock, getLastBlock, getNextBlock, getPreviousBlock, getSelectionEndBlock, getSelectionStartBlock } from "../_chunks-es/selector.is-selection-collapsed.js";
3
+ import { isKeySegment, isPortableTextTextBlock, isPortableTextSpan } from "@sanity/types";
3
4
  import { getBlockTextBefore, getSelectionText } from "../_chunks-es/selector.get-text-before.js";
4
5
  const getActiveListItem = ({
5
6
  context
@@ -14,9 +15,153 @@ const getActiveListItem = ({
14
15
  const firstListItem = firstTextBlock.listItem;
15
16
  if (firstListItem && selectedTextBlocks.every((block) => block.listItem === firstListItem))
16
17
  return firstListItem;
18
+ }, getActiveStyle = ({
19
+ context
20
+ }) => {
21
+ if (!context.selection)
22
+ return;
23
+ const guards = createGuards(context), selectedTextBlocks = getSelectedBlocks({
24
+ context
25
+ }).map((block) => block.node).filter(guards.isTextBlock), firstTextBlock = selectedTextBlocks.at(0);
26
+ if (!firstTextBlock)
27
+ return;
28
+ const firstStyle = firstTextBlock.style;
29
+ if (firstStyle && selectedTextBlocks.every((block) => block.style === firstStyle))
30
+ return firstStyle;
31
+ }, getSelectedSpans = ({
32
+ context
33
+ }) => {
34
+ if (!context.selection)
35
+ return [];
36
+ const selectedSpans = [], startPoint = context.selection.backward ? context.selection.focus : context.selection.anchor, endPoint = context.selection.backward ? context.selection.anchor : context.selection.focus, startBlockKey = isKeySegment(startPoint.path[0]) ? startPoint.path[0]._key : void 0, endBlockKey = isKeySegment(endPoint.path[0]) ? endPoint.path[0]._key : void 0;
37
+ if (!startBlockKey || !endBlockKey)
38
+ return selectedSpans;
39
+ const startSpanKey = isKeySegment(startPoint.path[2]) ? startPoint.path[2]._key : void 0, endSpanKey = isKeySegment(endPoint.path[2]) ? endPoint.path[2]._key : void 0;
40
+ for (const block of context.value)
41
+ if (isPortableTextTextBlock(block)) {
42
+ if (block._key === startBlockKey) {
43
+ for (const child of block.children)
44
+ if (isPortableTextSpan(child)) {
45
+ if (startSpanKey && child._key === startSpanKey && (selectedSpans.push({
46
+ node: child,
47
+ path: [{
48
+ _key: block._key
49
+ }, "children", {
50
+ _key: child._key
51
+ }]
52
+ }), startBlockKey === endBlockKey))
53
+ break;
54
+ if (endSpanKey && child._key === endSpanKey) {
55
+ selectedSpans.push({
56
+ node: child,
57
+ path: [{
58
+ _key: block._key
59
+ }, "children", {
60
+ _key: child._key
61
+ }]
62
+ });
63
+ break;
64
+ }
65
+ selectedSpans.length > 0 && selectedSpans.push({
66
+ node: child,
67
+ path: [{
68
+ _key: block._key
69
+ }, "children", {
70
+ _key: child._key
71
+ }]
72
+ });
73
+ }
74
+ if (startBlockKey === endBlockKey)
75
+ break;
76
+ continue;
77
+ }
78
+ if (block._key === endBlockKey) {
79
+ for (const child of block.children)
80
+ if (isPortableTextSpan(child)) {
81
+ if (endSpanKey && child._key === endSpanKey) {
82
+ selectedSpans.push({
83
+ node: child,
84
+ path: [{
85
+ _key: block._key
86
+ }, "children", {
87
+ _key: child._key
88
+ }]
89
+ });
90
+ break;
91
+ }
92
+ selectedSpans.push({
93
+ node: child,
94
+ path: [{
95
+ _key: block._key
96
+ }, "children", {
97
+ _key: child._key
98
+ }]
99
+ });
100
+ }
101
+ break;
102
+ }
103
+ if (selectedSpans.length > 0)
104
+ for (const child of block.children)
105
+ isPortableTextSpan(child) && selectedSpans.push({
106
+ node: child,
107
+ path: [{
108
+ _key: block._key
109
+ }, "children", {
110
+ _key: child._key
111
+ }]
112
+ });
113
+ }
114
+ return selectedSpans;
17
115
  };
116
+ function isActiveAnnotation(annotation) {
117
+ return (snapshot) => {
118
+ if (!snapshot.context.selection)
119
+ return !1;
120
+ const selectedBlocks = getSelectedBlocks(snapshot), selectedSpans = getSelectedSpans(snapshot);
121
+ if (selectedSpans.length === 0 || selectedSpans.some((span) => {
122
+ var _a;
123
+ return !span.node.marks || ((_a = span.node.marks) == null ? void 0 : _a.length) === 0;
124
+ }))
125
+ return !1;
126
+ const selectionMarkDefs = selectedBlocks.flatMap((block) => {
127
+ var _a;
128
+ return isPortableTextTextBlock(block.node) ? (_a = block.node.markDefs) != null ? _a : [] : [];
129
+ });
130
+ return selectedSpans.every((span) => {
131
+ var _a, _b;
132
+ return ((_b = (_a = span.node.marks) == null ? void 0 : _a.flatMap((mark) => {
133
+ const markDef = selectionMarkDefs.find((markDef2) => markDef2._key === mark);
134
+ return markDef ? [markDef._type] : [];
135
+ })) != null ? _b : []).includes(annotation);
136
+ });
137
+ };
138
+ }
139
+ const isSelectionExpanded = ({
140
+ context
141
+ }) => !isSelectionCollapsed({
142
+ context
143
+ });
144
+ function isActiveDecorator(decorator) {
145
+ return (snapshot) => {
146
+ if (isSelectionExpanded(snapshot)) {
147
+ const selectedSpans = getSelectedSpans(snapshot);
148
+ return selectedSpans.length > 0 && selectedSpans.every((span) => {
149
+ var _a;
150
+ return (_a = span.node.marks) == null ? void 0 : _a.includes(decorator);
151
+ });
152
+ }
153
+ return snapshot.context.activeDecorators.includes(decorator);
154
+ };
155
+ }
156
+ function isActiveListItem(listItem) {
157
+ return (snapshot) => getActiveListItem(snapshot) === listItem;
158
+ }
159
+ function isActiveStyle(style) {
160
+ return (snapshot) => getActiveStyle(snapshot) === style;
161
+ }
18
162
  export {
19
163
  getActiveListItem,
164
+ getActiveStyle,
20
165
  getBlockTextBefore,
21
166
  getFirstBlock,
22
167
  getFocusBlock,
@@ -29,9 +174,15 @@ export {
29
174
  getNextBlock,
30
175
  getPreviousBlock,
31
176
  getSelectedBlocks,
177
+ getSelectedSpans,
32
178
  getSelectionEndBlock,
33
179
  getSelectionStartBlock,
34
180
  getSelectionText,
35
- selectionIsCollapsed
181
+ isActiveAnnotation,
182
+ isActiveDecorator,
183
+ isActiveListItem,
184
+ isActiveStyle,
185
+ isSelectionCollapsed,
186
+ isSelectionExpanded
36
187
  };
37
188
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/selectors/selector.get-active-list-item.ts"],"sourcesContent":["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 * @alpha\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"],"names":["getActiveListItem","context","selection","guards","createGuards","selectedTextBlocks","getSelectedBlocks","map","block","node","filter","isTextBlock","firstTextBlock","at","firstListItem","listItem","every"],"mappings":";;;AAQO,MAAMA,oBAETA,CAAC;AAAA,EAACC;AAAO,MAAM;AACjB,MAAI,CAACA,QAAQC;AACX;AAGF,QAAMC,SAASC,aAAaH,OAAO,GAE7BI,qBADiBC,kBAAkB;AAAA,IAACL;AAAAA,EAAQ,CAAA,EAAEM,IAAKC,CAAAA,UAAUA,MAAMC,IAAI,EACnCC,OAAOP,OAAOQ,WAAW,GAE7DC,iBAAiBP,mBAAmBQ,GAAG,CAAC;AAE9C,MAAI,CAACD;AACH;AAGF,QAAME,gBAAgBF,eAAeG;AAErC,MAAKD,iBAIDT,mBAAmBW,MAAOR,CAAUA,UAAAA,MAAMO,aAAaD,aAAa;AAC/DA,WAAAA;AAIX;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/selectors/selector.get-active-list-item.ts","../../src/selectors/selector.get-active-style.ts","../../src/selectors/selector.get-selected-spans.ts","../../src/selectors/selector.is-active-annotation.ts","../../src/selectors/selector.is-selection-expanded.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 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 * @alpha\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 * @alpha\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 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 * @alpha\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 selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\n })\n\n if (startBlockKey === endBlockKey) {\n break\n }\n }\n\n if (endSpanKey && child._key === endSpanKey) {\n selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\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 selectedSpans.push({\n node: child,\n path: [{_key: block._key}, 'children', {_key: child._key}],\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 {isPortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {getSelectedSpans} from './selector.get-selected-spans'\nimport {getSelectedBlocks} from './selectors'\n\n/**\n * @alpha\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 selectedSpans = getSelectedSpans(snapshot)\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 {isSelectionCollapsed} from './selector.is-selection-collapsed'\n\n/**\n * @alpha\n */\nexport const isSelectionExpanded: EditorSelector<boolean> = ({context}) => {\n return !isSelectionCollapsed({context})\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 * @alpha\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 * @alpha\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 * @alpha\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":["getActiveListItem","context","selection","guards","createGuards","selectedTextBlocks","getSelectedBlocks","map","block","node","filter","isTextBlock","firstTextBlock","at","firstListItem","listItem","every","getActiveStyle","firstStyle","style","getSelectedSpans","selectedSpans","startPoint","backward","focus","anchor","endPoint","startBlockKey","isKeySegment","path","_key","undefined","endBlockKey","startSpanKey","endSpanKey","value","isPortableTextTextBlock","child","children","isPortableTextSpan","push","length","isActiveAnnotation","annotation","snapshot","selectedBlocks","some","span","marks","selectionMarkDefs","flatMap","markDefs","mark","markDef","find","_type","includes","isSelectionExpanded","isSelectionCollapsed","isActiveDecorator","decorator","activeDecorators","isActiveListItem","isActiveStyle"],"mappings":";;;;AAQO,MAAMA,oBAETA,CAAC;AAAA,EAACC;AAAO,MAAM;AACjB,MAAI,CAACA,QAAQC;AACX;AAGF,QAAMC,SAASC,aAAaH,OAAO,GAE7BI,qBADiBC,kBAAkB;AAAA,IAACL;AAAAA,EAAQ,CAAA,EAAEM,IAAKC,CAAAA,UAAUA,MAAMC,IAAI,EACnCC,OAAOP,OAAOQ,WAAW,GAE7DC,iBAAiBP,mBAAmBQ,GAAG,CAAC;AAE9C,MAAI,CAACD;AACH;AAGF,QAAME,gBAAgBF,eAAeG;AAErC,MAAKD,iBAIDT,mBAAmBW,MAAOR,CAAUA,UAAAA,MAAMO,aAAaD,aAAa;AAC/DA,WAAAA;AAIX,GC5BaG,iBAAiEA,CAAC;AAAA,EAC7EhB;AACF,MAAM;AACJ,MAAI,CAACA,QAAQC;AACX;AAGF,QAAMC,SAASC,aAAaH,OAAO,GAE7BI,qBADiBC,kBAAkB;AAAA,IAACL;AAAAA,EAAQ,CAAA,EAAEM,IAAKC,CAAAA,UAAUA,MAAMC,IAAI,EACnCC,OAAOP,OAAOQ,WAAW,GAE7DC,iBAAiBP,mBAAmBQ,GAAG,CAAC;AAE9C,MAAI,CAACD;AACH;AAGF,QAAMM,aAAaN,eAAeO;AAElC,MAAKD,cAIDb,mBAAmBW,MAAOR,CAAUA,UAAAA,MAAMW,UAAUD,UAAU;AACzDA,WAAAA;AAIX,GCxBaE,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