@portabletext/editor 1.16.4 → 1.17.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.
@@ -117,22 +117,13 @@ function isActiveAnnotation(annotation) {
117
117
  if (!snapshot.context.selection)
118
118
  return !1;
119
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
- }))
120
+ if (selectedSpans.length === 0 || selectedSpans.some((span) => !span.node.marks || span.node.marks?.length === 0))
124
121
  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
- });
122
+ const selectionMarkDefs = selectedBlocks.flatMap((block) => types.isPortableTextTextBlock(block.node) ? block.node.markDefs ?? [] : []);
123
+ return selectedSpans.every((span) => (span.node.marks?.flatMap((mark) => {
124
+ const markDef = selectionMarkDefs.find((markDef2) => markDef2._key === mark);
125
+ return markDef ? [markDef._type] : [];
126
+ }) ?? []).includes(annotation));
136
127
  };
137
128
  }
138
129
  const isSelectionExpanded = ({
@@ -144,10 +135,7 @@ function isActiveDecorator(decorator) {
144
135
  return (snapshot) => {
145
136
  if (isSelectionExpanded(snapshot)) {
146
137
  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
- });
138
+ return selectedSpans.length > 0 && selectedSpans.every((span) => span.node.marks?.includes(decorator));
151
139
  }
152
140
  return snapshot.context.activeDecorators.includes(decorator);
153
141
  };
@@ -1 +1 @@
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;AACX;AAGF,QAAMC,SAASC,8BAAAA,aA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SAAAA,CAAAA,aACc3B,eAAe2B,QAAQ,MAEpBzB;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;"}
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;AACX;AAGF,QAAMC,SAASC,8BAAAA,aAAaH,OAAO,GAE7BI,qBADi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MAAAA,wBAAwB5B,MAAMC,IAAI,IAAKD,MAAMC,KAAK0C,YAAY,CAAA,IAAM,CAAA,CACtE;AAEA,WAAO9B,cAAcL,MAAO+B,CAAAA,UAExBA,KAAKtC,KAAKuC,OAAOE,QAASE,CAAS,SAAA;AACjC,YAAMC,UAAUJ,kBAAkBK,KAC/BD,CAAAA,aAAYA,SAAQvB,SAASsB,IAChC;AAEA,aAAOC,UAAU,CAACA,QAAQE,KAAK,IAAI,CAAE;AAAA,IACtC,CAAA,KAAK,CAEYC,GAAAA,SAASb,UAAU,CACxC;AAAA,EACH;AACF;AC1CO,MAAMc,sBAA+CA,CAAC;AAAA,EAACxD;AAAO,MAC5D,CAACyD,8BAAAA,qBAAqB;AAAA,EAACzD;AAAO,CAAC;ACAjC,SAAS0D,kBAAkBC,WAA4C;AAC5E,SAAQhB,CAAa,aAAA;AACfa,QAAAA,oBAAoBb,QAAQ,GAAG;AAC3BvB,YAAAA,gBAAgBD,iBAAiBwB,QAAQ;AAG7CvB,aAAAA,cAAcoB,SAAS,KACvBpB,cAAcL,MAAO+B,CAASA,SAAAA,KAAKtC,KAAKuC,OAAOQ,SAASI,SAAS,CAAC;AAAA,IAAA;AAItE,WAAOhB,SAAS3C,QAAQ4D,iBAAiBL,SAASI,SAAS;AAAA,EAC7D;AACF;ACdO,SAASE,iBAAiB/C,UAA2C;AAClE6B,SAAAA,CAAAA,aACiB5C,kBAAkB4C,QAAQ,MAEvB7B;AAE9B;ACNO,SAASgD,cAAc5C,OAAwC;AAC5DyB,SAAAA,CAAAA,aACc3B,eAAe2B,QAAQ,MAEpBzB;AAE3B;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -118,22 +118,13 @@ function isActiveAnnotation(annotation) {
118
118
  if (!snapshot.context.selection)
119
119
  return !1;
120
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
- }))
121
+ if (selectedSpans.length === 0 || selectedSpans.some((span) => !span.node.marks || span.node.marks?.length === 0))
125
122
  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
- });
123
+ const selectionMarkDefs = selectedBlocks.flatMap((block) => isPortableTextTextBlock(block.node) ? block.node.markDefs ?? [] : []);
124
+ return selectedSpans.every((span) => (span.node.marks?.flatMap((mark) => {
125
+ const markDef = selectionMarkDefs.find((markDef2) => markDef2._key === mark);
126
+ return markDef ? [markDef._type] : [];
127
+ }) ?? []).includes(annotation));
137
128
  };
138
129
  }
139
130
  const isSelectionExpanded = ({
@@ -145,10 +136,7 @@ function isActiveDecorator(decorator) {
145
136
  return (snapshot) => {
146
137
  if (isSelectionExpanded(snapshot)) {
147
138
  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
- });
139
+ return selectedSpans.length > 0 && selectedSpans.every((span) => span.node.marks?.includes(decorator));
152
140
  }
153
141
  return snapshot.context.activeDecorators.includes(decorator);
154
142
  };
@@ -1 +1 @@
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,iBA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AASsB,IAChC;AAEA,eAAOC,UAAU,CAACA,QAAQE,KAAK,IAAI,CAAE;AAAA,MALvCR,CAAAA,MAAAA,OAAAA,KAMM,CAAA,GAEYS,SAASb,UAAU;AAAA,IAAA,CACxC;AAAA,EACH;AACF;AC1CO,MAAMc,sBAA+CA,CAAC;AAAA,EAACxD;AAAO,MAC5D,CAACyD,qBAAqB;AAAA,EAACzD;AAAO,CAAC;ACAjC,SAAS0D,kBAAkBC,WAA4C;AAC5E,SAAQhB,CAAa,aAAA;AACfa,QAAAA,oBAAoBb,QAAQ,GAAG;AAC3BvB,YAAAA,gBAAgBD,iBAAiBwB,QAAQ;AAE/C,aACEvB,cAAcoB,SAAS,KACvBpB,cAAcL,MAAO+B;AAb7B,YAAA;AAa2CtC,gBAAAA,KAAAA,KAAAA,KAAKuC,UAAVD,OAAAA,SAAAA,GAAiBS,SAASI,SAAAA;AAAAA,MAAAA,CAAU;AAAA,IAAA;AAItE,WAAOhB,SAAS3C,QAAQ4D,iBAAiBL,SAASI,SAAS;AAAA,EAC7D;AACF;ACdO,SAASE,iBAAiB/C,UAA2C;AAClE6B,SAAAA,CAAAA,aACiB5C,kBAAkB4C,QAAQ,MAEvB7B;AAE9B;ACNO,SAASgD,cAAc5C,OAAwC;AAC5DyB,SAAAA,CAAAA,aACc3B,eAAe2B,QAAQ,MAEpBzB;AAE3B;"}
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,EAAE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gBAAgBD,iBAAiBwB,QAAQ;AAG7CvB,aAAAA,cAAcoB,SAAS,KACvBpB,cAAcL,MAAO+B,CAASA,SAAAA,KAAKtC,KAAKuC,OAAOQ,SAASI,SAAS,CAAC;AAAA,IAAA;AAItE,WAAOhB,SAAS3C,QAAQ4D,iBAAiBL,SAASI,SAAS;AAAA,EAC7D;AACF;ACdO,SAASE,iBAAiB/C,UAA2C;AAClE6B,SAAAA,CAAAA,aACiB5C,kBAAkB4C,QAAQ,MAEvB7B;AAE9B;ACNO,SAASgD,cAAc5C,OAAwC;AAC5DyB,SAAAA,CAAAA,aACc3B,eAAe2B,QAAQ,MAEpBzB;AAE3B;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.16.4",
3
+ "version": "1.17.0",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -82,8 +82,8 @@
82
82
  "@types/lodash.startcase": "^4.4.9",
83
83
  "@types/react": "^19.0.1",
84
84
  "@types/react-dom": "^19.0.2",
85
- "@typescript-eslint/eslint-plugin": "^8.17.0",
86
- "@typescript-eslint/parser": "^8.17.0",
85
+ "@typescript-eslint/eslint-plugin": "^8.18.1",
86
+ "@typescript-eslint/parser": "^8.18.1",
87
87
  "@vitejs/plugin-react": "^4.3.4",
88
88
  "@vitest/browser": "^2.1.8",
89
89
  "@vitest/coverage-istanbul": "^2.1.8",
@@ -0,0 +1,410 @@
1
+ import {assertEvent, assign, createActor, setup} from 'xstate'
2
+ import * as selectors from '../selectors'
3
+ import {isHotkey} from '../utils/is-hotkey'
4
+ import {defineBehavior} from './behavior.types'
5
+
6
+ const emojiCharRegEx = /^[a-zA-Z-_0-9]{1}$/
7
+ const incompleteEmojiRegEx = /:([a-zA-Z-_0-9]+)$/
8
+ const emojiRegEx = /:([a-zA-Z-_0-9]+):$/
9
+
10
+ /**
11
+ * @alpha
12
+ */
13
+ export type EmojiPickerBehaviorsConfig<TEmojiMatch> = {
14
+ /**
15
+ * Match emojis by keyword.
16
+ */
17
+ matchEmojis: ({keyword}: {keyword: string}) => Array<TEmojiMatch>
18
+ onMatchesChanged: ({matches}: {matches: Array<TEmojiMatch>}) => void
19
+ onSelectedIndexChanged: ({selectedIndex}: {selectedIndex: number}) => void
20
+ /**
21
+ * Parse an emoji match to a string that will be inserted into the editor.
22
+ */
23
+ parseMatch: ({match}: {match: TEmojiMatch}) => string | undefined
24
+ }
25
+
26
+ /**
27
+ * @alpha
28
+ */
29
+ export function createEmojiPickerBehaviors<TEmojiMatch>(
30
+ config: EmojiPickerBehaviorsConfig<TEmojiMatch>,
31
+ ) {
32
+ const emojiPickerActor = createActor(createEmojiPickerMachine<TEmojiMatch>())
33
+ emojiPickerActor.start()
34
+ emojiPickerActor.subscribe((state) => {
35
+ config.onMatchesChanged({matches: state.context.matches})
36
+ config.onSelectedIndexChanged({selectedIndex: state.context.selectedIndex})
37
+ })
38
+
39
+ return [
40
+ defineBehavior({
41
+ on: 'insert.text',
42
+ guard: ({context, event}) => {
43
+ const isColon = event.text === ':'
44
+
45
+ if (!isColon) {
46
+ return false
47
+ }
48
+
49
+ const matches = emojiPickerActor.getSnapshot().context.matches
50
+ const selectedIndex =
51
+ emojiPickerActor.getSnapshot().context.selectedIndex
52
+ const emoji = matches[selectedIndex]
53
+ ? config.parseMatch({match: matches[selectedIndex]})
54
+ : undefined
55
+
56
+ const focusBlock = selectors.getFocusTextBlock({context})
57
+ const textBefore = selectors.getBlockTextBefore({context})
58
+ const emojiKeyword = `${textBefore}:`.match(emojiRegEx)?.[1]
59
+
60
+ if (!focusBlock || emojiKeyword === undefined) {
61
+ return false
62
+ }
63
+
64
+ const emojiStringLength = emojiKeyword.length + 2
65
+
66
+ if (emoji) {
67
+ return {
68
+ focusBlock,
69
+ emoji,
70
+ emojiStringLength,
71
+ textBeforeLength: textBefore.length + 1,
72
+ }
73
+ }
74
+
75
+ return false
76
+ },
77
+ actions: [
78
+ () => [
79
+ {
80
+ type: 'insert.text',
81
+ text: ':',
82
+ },
83
+ ],
84
+ (_, params) => [
85
+ {
86
+ type: 'effect',
87
+ effect: () => {
88
+ emojiPickerActor.send({type: 'select'})
89
+ },
90
+ },
91
+ {
92
+ type: 'delete.text',
93
+ anchor: {
94
+ path: params.focusBlock.path,
95
+ offset: params.textBeforeLength - params.emojiStringLength,
96
+ },
97
+ focus: {
98
+ path: params.focusBlock.path,
99
+ offset: params.textBeforeLength,
100
+ },
101
+ },
102
+ {
103
+ type: 'insert.text',
104
+ text: params.emoji,
105
+ },
106
+ ],
107
+ ],
108
+ }),
109
+ defineBehavior({
110
+ on: 'insert.text',
111
+ guard: ({context, event}) => {
112
+ const isEmojiChar = emojiCharRegEx.test(event.text)
113
+
114
+ if (!isEmojiChar) {
115
+ return {emojis: []}
116
+ }
117
+
118
+ const focusBlock = selectors.getFocusTextBlock({context})
119
+ const textBefore = selectors.getBlockTextBefore({context})
120
+ const emojiKeyword = `${textBefore}${event.text}`.match(
121
+ incompleteEmojiRegEx,
122
+ )?.[1]
123
+
124
+ if (!focusBlock || emojiKeyword === undefined) {
125
+ return {emojis: []}
126
+ }
127
+
128
+ const emojis = config.matchEmojis({keyword: emojiKeyword})
129
+
130
+ return {emojis}
131
+ },
132
+ actions: [
133
+ (_, params) => [
134
+ {
135
+ type: 'effect',
136
+ effect: () => {
137
+ emojiPickerActor.send({
138
+ type: 'emojis found',
139
+ matches: params.emojis,
140
+ })
141
+ },
142
+ },
143
+ ],
144
+ ],
145
+ }),
146
+ defineBehavior({
147
+ on: 'key.down',
148
+ guard: ({context, event}) => {
149
+ const isShift = isHotkey('Shift', event.keyboardEvent)
150
+ const isColon = event.keyboardEvent.key === ':'
151
+ const isEmojiChar = emojiCharRegEx.test(event.keyboardEvent.key)
152
+
153
+ if (isShift || isColon || isEmojiChar) {
154
+ return false
155
+ }
156
+
157
+ const isArrowDown = isHotkey('ArrowDown', event.keyboardEvent)
158
+ const isArrowUp = isHotkey('ArrowUp', event.keyboardEvent)
159
+ const isEnter = isHotkey('Enter', event.keyboardEvent)
160
+ const isTab = isHotkey('Tab', event.keyboardEvent)
161
+
162
+ if (isEnter || isTab) {
163
+ const matches = emojiPickerActor.getSnapshot().context.matches
164
+ const selectedIndex =
165
+ emojiPickerActor.getSnapshot().context.selectedIndex
166
+
167
+ const emoji = matches[selectedIndex]
168
+ ? config.parseMatch({match: matches[selectedIndex]})
169
+ : undefined
170
+
171
+ if (!emoji) {
172
+ return false
173
+ }
174
+
175
+ const focusBlock = selectors.getFocusTextBlock({context})
176
+ const textBefore = selectors.getBlockTextBefore({context})
177
+ const emojiKeyword = textBefore.match(incompleteEmojiRegEx)?.[1]
178
+
179
+ if (!focusBlock || emojiKeyword === undefined) {
180
+ return false
181
+ }
182
+
183
+ const emojiStringLength = emojiKeyword.length + 1
184
+
185
+ if (emoji) {
186
+ return {
187
+ action: 'select' as const,
188
+ focusBlock,
189
+ emoji,
190
+ emojiStringLength,
191
+ textBeforeLength: textBefore.length,
192
+ }
193
+ }
194
+
195
+ return false
196
+ }
197
+
198
+ if (isArrowDown) {
199
+ return {action: 'navigate down' as const}
200
+ }
201
+
202
+ if (isArrowUp) {
203
+ return {action: 'navigate up' as const}
204
+ }
205
+
206
+ return {action: 'reset' as const}
207
+ },
208
+ actions: [
209
+ (_, params) => {
210
+ if (params.action === 'select') {
211
+ return [
212
+ {
213
+ type: 'effect',
214
+ effect: () => {
215
+ emojiPickerActor.send({type: 'select'})
216
+ },
217
+ },
218
+ {
219
+ type: 'delete.text',
220
+ anchor: {
221
+ path: params.focusBlock.path,
222
+ offset: params.textBeforeLength - params.emojiStringLength,
223
+ },
224
+ focus: {
225
+ path: params.focusBlock.path,
226
+ offset: params.textBeforeLength,
227
+ },
228
+ },
229
+ {
230
+ type: 'insert.text',
231
+ text: params.emoji,
232
+ },
233
+ ]
234
+ }
235
+
236
+ if (params.action === 'navigate up') {
237
+ return [
238
+ // If we are navigating then we want to hijack the key event and
239
+ // turn it into a noop.
240
+ {
241
+ type: 'noop',
242
+ },
243
+ {
244
+ type: 'effect',
245
+ effect: () => {
246
+ emojiPickerActor.send({type: 'navigate up'})
247
+ },
248
+ },
249
+ ]
250
+ }
251
+
252
+ if (params.action === 'navigate down') {
253
+ return [
254
+ // If we are navigating then we want to hijack the key event and
255
+ // turn it into a noop.
256
+ {
257
+ type: 'noop',
258
+ },
259
+ {
260
+ type: 'effect',
261
+ effect: () => {
262
+ emojiPickerActor.send({type: 'navigate down'})
263
+ },
264
+ },
265
+ ]
266
+ }
267
+
268
+ return [
269
+ {
270
+ type: 'effect',
271
+ effect: () => {
272
+ emojiPickerActor.send({type: 'reset'})
273
+ },
274
+ },
275
+ ]
276
+ },
277
+ ],
278
+ }),
279
+ defineBehavior({
280
+ on: 'delete.backward',
281
+ guard: ({context, event}) => {
282
+ if (event.unit !== 'character') {
283
+ return false
284
+ }
285
+
286
+ const focusBlock = selectors.getFocusTextBlock({context})
287
+ const textBefore = selectors.getBlockTextBefore({context})
288
+ const emojiKeyword = textBefore
289
+ .slice(0, textBefore.length - 1)
290
+ .match(incompleteEmojiRegEx)?.[1]
291
+
292
+ if (!focusBlock || emojiKeyword === undefined) {
293
+ return {emojis: [], event}
294
+ }
295
+
296
+ const emojis = config.matchEmojis({keyword: emojiKeyword})
297
+
298
+ return {emojis}
299
+ },
300
+ actions: [
301
+ (_, params) => [
302
+ {
303
+ type: 'effect',
304
+ effect: () => {
305
+ emojiPickerActor.send({
306
+ type: 'emojis found',
307
+ matches: params.emojis,
308
+ })
309
+ },
310
+ },
311
+ ],
312
+ ],
313
+ }),
314
+ ]
315
+ }
316
+
317
+ function createEmojiPickerMachine<TEmojiSearchResult>() {
318
+ return setup({
319
+ types: {
320
+ context: {} as {
321
+ matches: Array<TEmojiSearchResult>
322
+ selectedIndex: number
323
+ },
324
+ events: {} as
325
+ | {
326
+ type: 'emojis found'
327
+ matches: Array<TEmojiSearchResult>
328
+ }
329
+ | {
330
+ type: 'navigate down' | 'navigate up' | 'select' | 'reset'
331
+ },
332
+ },
333
+ actions: {
334
+ 'assign matches': assign({
335
+ matches: ({event}) => {
336
+ assertEvent(event, 'emojis found')
337
+ return event.matches
338
+ },
339
+ }),
340
+ 'reset matches': assign({
341
+ matches: [],
342
+ }),
343
+ 'reset selected index': assign({
344
+ selectedIndex: 0,
345
+ }),
346
+ 'increment selected index': assign({
347
+ selectedIndex: ({context}) => {
348
+ if (context.selectedIndex === context.matches.length - 1) {
349
+ return 0
350
+ }
351
+ return context.selectedIndex + 1
352
+ },
353
+ }),
354
+ 'decrement selected index': assign({
355
+ selectedIndex: ({context}) => {
356
+ if (context.selectedIndex === 0) {
357
+ return context.matches.length - 1
358
+ }
359
+ return context.selectedIndex - 1
360
+ },
361
+ }),
362
+ },
363
+ guards: {
364
+ 'no matches': ({context}) => context.matches.length === 0,
365
+ },
366
+ }).createMachine({
367
+ id: 'emoji picker',
368
+ context: {
369
+ matches: [],
370
+ selectedIndex: 0,
371
+ },
372
+ initial: 'idle',
373
+ states: {
374
+ 'idle': {
375
+ on: {
376
+ 'emojis found': {
377
+ actions: 'assign matches',
378
+ target: 'showing matches',
379
+ },
380
+ },
381
+ },
382
+ 'showing matches': {
383
+ always: {
384
+ guard: 'no matches',
385
+ target: 'idle',
386
+ },
387
+ exit: ['reset selected index'],
388
+ on: {
389
+ 'emojis found': {
390
+ actions: 'assign matches',
391
+ },
392
+ 'navigate down': {
393
+ actions: 'increment selected index',
394
+ },
395
+ 'navigate up': {
396
+ actions: 'decrement selected index',
397
+ },
398
+ 'reset': {
399
+ target: 'idle',
400
+ actions: ['reset selected index', 'reset matches'],
401
+ },
402
+ 'select': {
403
+ target: 'idle',
404
+ actions: ['reset selected index', 'reset matches'],
405
+ },
406
+ },
407
+ },
408
+ },
409
+ })
410
+ }
@@ -11,6 +11,10 @@ export {
11
11
  type CodeEditorBehaviorsConfig,
12
12
  } from './behavior.code-editor'
13
13
  export {coreBehavior, coreBehaviors} from './behavior.core'
14
+ export {
15
+ type EmojiPickerBehaviorsConfig,
16
+ createEmojiPickerBehaviors,
17
+ } from './behavior.emoji-picker'
14
18
  export {createLinkBehaviors, type LinkBehaviorsConfig} from './behavior.links'
15
19
  export {
16
20
  createMarkdownBehaviors,