@portabletext/editor 1.40.3 → 1.41.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 (34) hide show
  1. package/lib/_chunks-cjs/editor-provider.cjs +72 -34
  2. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  3. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs +10 -0
  4. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs.map +1 -0
  5. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  6. package/lib/_chunks-es/editor-provider.js +73 -35
  7. package/lib/_chunks-es/editor-provider.js.map +1 -1
  8. package/lib/_chunks-es/util.is-selection-collapsed.js +11 -0
  9. package/lib/_chunks-es/util.is-selection-collapsed.js.map +1 -0
  10. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  11. package/lib/index.cjs +307 -144
  12. package/lib/index.cjs.map +1 -1
  13. package/lib/index.js +309 -145
  14. package/lib/index.js.map +1 -1
  15. package/lib/utils/index.cjs +7 -5
  16. package/lib/utils/index.cjs.map +1 -1
  17. package/lib/utils/index.d.cts +23 -2
  18. package/lib/utils/index.d.ts +23 -2
  19. package/lib/utils/index.js +6 -3
  20. package/lib/utils/index.js.map +1 -1
  21. package/package.json +4 -4
  22. package/src/behavior-actions/behavior.action.insert-blocks.ts +5 -1
  23. package/src/converters/converter.text-plain.ts +24 -11
  24. package/src/editor/Editable.tsx +336 -223
  25. package/src/editor/components/drop-indicator.tsx +4 -1
  26. package/src/internal-utils/drag-selection.test.ts +74 -1
  27. package/src/internal-utils/drag-selection.ts +20 -4
  28. package/src/internal-utils/dragging-on-drag-origin.ts +22 -0
  29. package/src/internal-utils/event-position.ts +69 -10
  30. package/src/internal-utils/slate-utils.ts +74 -6
  31. package/src/utils/index.ts +2 -0
  32. package/src/utils/util.get-selection-end-point.ts +20 -0
  33. package/src/utils/util.get-selection-start-point.ts +20 -0
  34. package/src/utils/util.is-keyed-segment.ts +2 -2
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var util_sliceBlocks = require("../_chunks-cjs/util.slice-blocks.cjs"), util_selectionPointToBlockOffset = require("../_chunks-cjs/util.selection-point-to-block-offset.cjs"), util_mergeTextBlocks = require("../_chunks-cjs/util.merge-text-blocks.cjs");
3
+ var util_sliceBlocks = require("../_chunks-cjs/util.slice-blocks.cjs"), util_selectionPointToBlockOffset = require("../_chunks-cjs/util.selection-point-to-block-offset.cjs"), util_isSelectionCollapsed = require("../_chunks-cjs/util.is-selection-collapsed.cjs"), util_mergeTextBlocks = require("../_chunks-cjs/util.merge-text-blocks.cjs");
4
+ function getSelectionStartPoint(selection) {
5
+ return selection ? selection.backward ? selection.focus : selection.anchor : null;
6
+ }
4
7
  function isEqualSelections(a, b) {
5
8
  return !a && !b ? !0 : !a || !b ? !1 : util_sliceBlocks.isEqualSelectionPoints(a.anchor, b.anchor) && util_sliceBlocks.isEqualSelectionPoints(a.focus, b.focus);
6
9
  }
7
- function isSelectionCollapsed(selection) {
8
- return selection ? selection.anchor.path.join() === selection.focus.path.join() && selection.anchor.offset === selection.focus.offset : !1;
9
- }
10
10
  function splitTextBlock({
11
11
  context,
12
12
  block,
@@ -64,9 +64,11 @@ exports.blockOffsetToSelectionPoint = util_selectionPointToBlockOffset.blockOffs
64
64
  exports.blockOffsetsToSelection = util_selectionPointToBlockOffset.blockOffsetsToSelection;
65
65
  exports.childSelectionPointToBlockOffset = util_selectionPointToBlockOffset.childSelectionPointToBlockOffset;
66
66
  exports.selectionPointToBlockOffset = util_selectionPointToBlockOffset.selectionPointToBlockOffset;
67
+ exports.getSelectionEndPoint = util_isSelectionCollapsed.getSelectionEndPoint;
68
+ exports.isSelectionCollapsed = util_isSelectionCollapsed.isSelectionCollapsed;
67
69
  exports.isTextBlock = util_mergeTextBlocks.isTextBlock;
68
70
  exports.mergeTextBlocks = util_mergeTextBlocks.mergeTextBlocks;
71
+ exports.getSelectionStartPoint = getSelectionStartPoint;
69
72
  exports.isEqualSelections = isEqualSelections;
70
- exports.isSelectionCollapsed = isSelectionCollapsed;
71
73
  exports.splitTextBlock = splitTextBlock;
72
74
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/utils/util.is-equal-selections.ts","../../src/utils/util.is-selection-collapsed.ts","../../src/utils/util.split-text-block.ts"],"sourcesContent":["import type {EditorSelection} from '../types/editor'\nimport {isEqualSelectionPoints} from './util.is-equal-selection-points'\n\n/**\n * @public\n */\nexport function isEqualSelections(a: EditorSelection, b: EditorSelection) {\n if (!a && !b) {\n return true\n }\n\n if (!a || !b) {\n return false\n }\n\n return (\n isEqualSelectionPoints(a.anchor, b.anchor) &&\n isEqualSelectionPoints(a.focus, b.focus)\n )\n}\n","import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function isSelectionCollapsed(selection: EditorSelection) {\n if (!selection) {\n return false\n }\n\n return (\n selection.anchor.path.join() === selection.focus.path.join() &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelectionPoint} from '..'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isSpan} from './util.is-span'\nimport {isTextBlock} from './util.is-text-block'\nimport {sliceBlocks} from './util.slice-blocks'\n\n/**\n * @beta\n */\nexport function splitTextBlock({\n context,\n block,\n point,\n}: {\n context: Pick<EditorContext, 'schema'>\n block: PortableTextTextBlock\n point: EditorSelectionPoint\n}): {before: PortableTextTextBlock; after: PortableTextTextBlock} | undefined {\n const firstChild = block.children.at(0)\n const lastChild = block.children.at(block.children.length - 1)\n\n if (!firstChild || !lastChild) {\n return undefined\n }\n\n const before = sliceBlocks({\n blocks: [block],\n selection: {\n anchor: {\n path: [{_key: block._key}, 'children', {_key: firstChild._key}],\n offset: 0,\n },\n focus: point,\n },\n }).at(0)\n const after = sliceBlocks({\n blocks: [block],\n selection: {\n anchor: point,\n focus: {\n path: [{_key: block._key}, 'children', {_key: lastChild._key}],\n offset: isSpan(context, lastChild) ? lastChild.text.length : 0,\n },\n },\n }).at(0)\n\n if (!before || !after) {\n return undefined\n }\n\n if (!isTextBlock(context, before) || !isTextBlock(context, after)) {\n return undefined\n }\n\n return {before, after}\n}\n"],"names":["isEqualSelections","a","b","isEqualSelectionPoints","anchor","focus","isSelectionCollapsed","selection","path","join","offset","splitTextBlock","context","block","point","firstChild","children","at","lastChild","length","before","sliceBlocks","blocks","_key","after","isSpan","text","isTextBlock"],"mappings":";;;AAMgBA,SAAAA,kBAAkBC,GAAoBC,GAAoB;AACpE,SAAA,CAACD,KAAK,CAACC,IACF,KAGL,CAACD,KAAK,CAACC,IACF,KAIPC,iBAAAA,uBAAuBF,EAAEG,QAAQF,EAAEE,MAAM,KACzCD,wCAAuBF,EAAEI,OAAOH,EAAEG,KAAK;AAE3C;ACdO,SAASC,qBAAqBC,WAA4B;AAC/D,SAAKA,YAKHA,UAAUH,OAAOI,KAAKC,KAAAA,MAAWF,UAAUF,MAAMG,KAAKC,KAAAA,KACtDF,UAAUH,OAAOM,WAAWH,UAAUF,MAAMK,SALrC;AAOX;ACJO,SAASC,eAAe;AAAA,EAC7BC;AAAAA,EACAC;AAAAA,EACAC;AAKF,GAA8E;AAC5E,QAAMC,aAAaF,MAAMG,SAASC,GAAG,CAAC,GAChCC,YAAYL,MAAMG,SAASC,GAAGJ,MAAMG,SAASG,SAAS,CAAC;AAEzD,MAAA,CAACJ,cAAc,CAACG;AAClB;AAGF,QAAME,SAASC,iBAAAA,YAAY;AAAA,IACzBC,QAAQ,CAACT,KAAK;AAAA,IACdN,WAAW;AAAA,MACTH,QAAQ;AAAA,QACNI,MAAM,CAAC;AAAA,UAACe,MAAMV,MAAMU;AAAAA,WAAO,YAAY;AAAA,UAACA,MAAMR,WAAWQ;AAAAA,QAAAA,CAAK;AAAA,QAC9Db,QAAQ;AAAA,MACV;AAAA,MACAL,OAAOS;AAAAA,IAAAA;AAAAA,EAEV,CAAA,EAAEG,GAAG,CAAC,GACDO,QAAQH,iBAAAA,YAAY;AAAA,IACxBC,QAAQ,CAACT,KAAK;AAAA,IACdN,WAAW;AAAA,MACTH,QAAQU;AAAAA,MACRT,OAAO;AAAA,QACLG,MAAM,CAAC;AAAA,UAACe,MAAMV,MAAMU;AAAAA,WAAO,YAAY;AAAA,UAACA,MAAML,UAAUK;AAAAA,QAAAA,CAAK;AAAA,QAC7Db,QAAQe,iBAAOb,OAAAA,SAASM,SAAS,IAAIA,UAAUQ,KAAKP,SAAS;AAAA,MAAA;AAAA,IAC/D;AAAA,EACF,CACD,EAAEF,GAAG,CAAC;AAEP,MAAI,EAACG,CAAAA,UAAU,CAACI,UAIZ,EAACG,CAAAA,qBAAAA,YAAYf,SAASQ,MAAM,KAAK,CAACO,qBAAYf,YAAAA,SAASY,KAAK;AAIzD,WAAA;AAAA,MAACJ;AAAAA,MAAQI;AAAAA,IAAK;AACvB;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/utils/util.get-selection-start-point.ts","../../src/utils/util.is-equal-selections.ts","../../src/utils/util.split-text-block.ts"],"sourcesContent":["import type {EditorSelection, EditorSelectionPoint} from '..'\n\n/**\n * @public\n */\nexport function getSelectionStartPoint<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n TEditorSelectionPoint extends\n EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>\n ? EditorSelectionPoint\n : null,\n>(selection: TEditorSelection): TEditorSelectionPoint {\n if (!selection) {\n return null as TEditorSelectionPoint\n }\n\n return (\n selection.backward ? selection.focus : selection.anchor\n ) as TEditorSelectionPoint\n}\n","import type {EditorSelection} from '../types/editor'\nimport {isEqualSelectionPoints} from './util.is-equal-selection-points'\n\n/**\n * @public\n */\nexport function isEqualSelections(a: EditorSelection, b: EditorSelection) {\n if (!a && !b) {\n return true\n }\n\n if (!a || !b) {\n return false\n }\n\n return (\n isEqualSelectionPoints(a.anchor, b.anchor) &&\n isEqualSelectionPoints(a.focus, b.focus)\n )\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelectionPoint} from '..'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isSpan} from './util.is-span'\nimport {isTextBlock} from './util.is-text-block'\nimport {sliceBlocks} from './util.slice-blocks'\n\n/**\n * @beta\n */\nexport function splitTextBlock({\n context,\n block,\n point,\n}: {\n context: Pick<EditorContext, 'schema'>\n block: PortableTextTextBlock\n point: EditorSelectionPoint\n}): {before: PortableTextTextBlock; after: PortableTextTextBlock} | undefined {\n const firstChild = block.children.at(0)\n const lastChild = block.children.at(block.children.length - 1)\n\n if (!firstChild || !lastChild) {\n return undefined\n }\n\n const before = sliceBlocks({\n blocks: [block],\n selection: {\n anchor: {\n path: [{_key: block._key}, 'children', {_key: firstChild._key}],\n offset: 0,\n },\n focus: point,\n },\n }).at(0)\n const after = sliceBlocks({\n blocks: [block],\n selection: {\n anchor: point,\n focus: {\n path: [{_key: block._key}, 'children', {_key: lastChild._key}],\n offset: isSpan(context, lastChild) ? lastChild.text.length : 0,\n },\n },\n }).at(0)\n\n if (!before || !after) {\n return undefined\n }\n\n if (!isTextBlock(context, before) || !isTextBlock(context, after)) {\n return undefined\n }\n\n return {before, after}\n}\n"],"names":["getSelectionStartPoint","selection","backward","focus","anchor","isEqualSelections","a","b","isEqualSelectionPoints","splitTextBlock","context","block","point","firstChild","children","at","lastChild","length","before","sliceBlocks","blocks","path","_key","offset","after","isSpan","text","isTextBlock"],"mappings":";;;AAKO,SAASA,uBAMdC,WAAoD;AACpD,SAAKA,YAKHA,UAAUC,WAAWD,UAAUE,QAAQF,UAAUG,SAJ1C;AAMX;ACbgBC,SAAAA,kBAAkBC,GAAoBC,GAAoB;AACpE,SAAA,CAACD,KAAK,CAACC,IACF,KAGL,CAACD,KAAK,CAACC,IACF,KAIPC,iBAAAA,uBAAuBF,EAAEF,QAAQG,EAAEH,MAAM,KACzC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
  ObjectSchemaType,
8
8
  Path,
9
- PathSegment,
10
9
  PortableTextChild,
11
10
  PortableTextListBlock,
12
11
  PortableTextTextBlock,
@@ -22049,6 +22048,28 @@ export declare function getBlockStartPoint({
22049
22048
  path: [KeyedSegment]
22050
22049
  }): EditorSelectionPoint
22051
22050
 
22051
+ /**
22052
+ * @public
22053
+ */
22054
+ export declare function getSelectionEndPoint<
22055
+ TEditorSelection extends NonNullable<EditorSelection> | null,
22056
+ TEditorSelectionPoint extends
22057
+ EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>
22058
+ ? EditorSelectionPoint
22059
+ : null,
22060
+ >(selection: TEditorSelection): TEditorSelectionPoint
22061
+
22062
+ /**
22063
+ * @public
22064
+ */
22065
+ export declare function getSelectionStartPoint<
22066
+ TEditorSelection extends NonNullable<EditorSelection> | null,
22067
+ TEditorSelectionPoint extends
22068
+ EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>
22069
+ ? EditorSelectionPoint
22070
+ : null,
22071
+ >(selection: TEditorSelection): TEditorSelectionPoint
22072
+
22052
22073
  /**
22053
22074
  * @public
22054
22075
  */
@@ -22139,7 +22160,7 @@ export declare function isEqualSelections(
22139
22160
  * @public
22140
22161
  */
22141
22162
  export declare function isKeyedSegment(
22142
- segment: PathSegment,
22163
+ segment: unknown,
22143
22164
  ): segment is KeyedSegment
22144
22165
 
22145
22166
  /**
@@ -6,7 +6,6 @@ import type {
6
6
  BlockStyleDefinition,
7
7
  ObjectSchemaType,
8
8
  Path,
9
- PathSegment,
10
9
  PortableTextChild,
11
10
  PortableTextListBlock,
12
11
  PortableTextTextBlock,
@@ -22049,6 +22048,28 @@ export declare function getBlockStartPoint({
22049
22048
  path: [KeyedSegment]
22050
22049
  }): EditorSelectionPoint
22051
22050
 
22051
+ /**
22052
+ * @public
22053
+ */
22054
+ export declare function getSelectionEndPoint<
22055
+ TEditorSelection extends NonNullable<EditorSelection> | null,
22056
+ TEditorSelectionPoint extends
22057
+ EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>
22058
+ ? EditorSelectionPoint
22059
+ : null,
22060
+ >(selection: TEditorSelection): TEditorSelectionPoint
22061
+
22062
+ /**
22063
+ * @public
22064
+ */
22065
+ export declare function getSelectionStartPoint<
22066
+ TEditorSelection extends NonNullable<EditorSelection> | null,
22067
+ TEditorSelectionPoint extends
22068
+ EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>
22069
+ ? EditorSelectionPoint
22070
+ : null,
22071
+ >(selection: TEditorSelection): TEditorSelectionPoint
22072
+
22052
22073
  /**
22053
22074
  * @public
22054
22075
  */
@@ -22139,7 +22160,7 @@ export declare function isEqualSelections(
22139
22160
  * @public
22140
22161
  */
22141
22162
  export declare function isKeyedSegment(
22142
- segment: PathSegment,
22163
+ segment: unknown,
22143
22164
  ): segment is KeyedSegment
22144
22165
 
22145
22166
  /**
@@ -1,14 +1,15 @@
1
1
  import { isEqualSelectionPoints, sliceBlocks, isSpan } from "../_chunks-es/util.slice-blocks.js";
2
2
  import { blockOffsetToSpanSelectionPoint, getBlockEndPoint, getBlockStartPoint, getTextBlockText, isEmptyTextBlock, isKeyedSegment, reverseSelection, spanSelectionPointToBlockOffset } from "../_chunks-es/util.slice-blocks.js";
3
3
  import { blockOffsetToBlockSelectionPoint, blockOffsetToSelectionPoint, blockOffsetsToSelection, childSelectionPointToBlockOffset, selectionPointToBlockOffset } from "../_chunks-es/util.selection-point-to-block-offset.js";
4
+ import { getSelectionEndPoint, isSelectionCollapsed } from "../_chunks-es/util.is-selection-collapsed.js";
4
5
  import { isTextBlock } from "../_chunks-es/util.merge-text-blocks.js";
5
6
  import { mergeTextBlocks } from "../_chunks-es/util.merge-text-blocks.js";
7
+ function getSelectionStartPoint(selection) {
8
+ return selection ? selection.backward ? selection.focus : selection.anchor : null;
9
+ }
6
10
  function isEqualSelections(a, b) {
7
11
  return !a && !b ? !0 : !a || !b ? !1 : isEqualSelectionPoints(a.anchor, b.anchor) && isEqualSelectionPoints(a.focus, b.focus);
8
12
  }
9
- function isSelectionCollapsed(selection) {
10
- return selection ? selection.anchor.path.join() === selection.focus.path.join() && selection.anchor.offset === selection.focus.offset : !1;
11
- }
12
13
  function splitTextBlock({
13
14
  context,
14
15
  block,
@@ -58,6 +59,8 @@ export {
58
59
  childSelectionPointToBlockOffset,
59
60
  getBlockEndPoint,
60
61
  getBlockStartPoint,
62
+ getSelectionEndPoint,
63
+ getSelectionStartPoint,
61
64
  getTextBlockText,
62
65
  isEmptyTextBlock,
63
66
  isEqualSelectionPoints,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/utils/util.is-equal-selections.ts","../../src/utils/util.is-selection-collapsed.ts","../../src/utils/util.split-text-block.ts"],"sourcesContent":["import type {EditorSelection} from '../types/editor'\nimport {isEqualSelectionPoints} from './util.is-equal-selection-points'\n\n/**\n * @public\n */\nexport function isEqualSelections(a: EditorSelection, b: EditorSelection) {\n if (!a && !b) {\n return true\n }\n\n if (!a || !b) {\n return false\n }\n\n return (\n isEqualSelectionPoints(a.anchor, b.anchor) &&\n isEqualSelectionPoints(a.focus, b.focus)\n )\n}\n","import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function isSelectionCollapsed(selection: EditorSelection) {\n if (!selection) {\n return false\n }\n\n return (\n selection.anchor.path.join() === selection.focus.path.join() &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelectionPoint} from '..'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isSpan} from './util.is-span'\nimport {isTextBlock} from './util.is-text-block'\nimport {sliceBlocks} from './util.slice-blocks'\n\n/**\n * @beta\n */\nexport function splitTextBlock({\n context,\n block,\n point,\n}: {\n context: Pick<EditorContext, 'schema'>\n block: PortableTextTextBlock\n point: EditorSelectionPoint\n}): {before: PortableTextTextBlock; after: PortableTextTextBlock} | undefined {\n const firstChild = block.children.at(0)\n const lastChild = block.children.at(block.children.length - 1)\n\n if (!firstChild || !lastChild) {\n return undefined\n }\n\n const before = sliceBlocks({\n blocks: [block],\n selection: {\n anchor: {\n path: [{_key: block._key}, 'children', {_key: firstChild._key}],\n offset: 0,\n },\n focus: point,\n },\n }).at(0)\n const after = sliceBlocks({\n blocks: [block],\n selection: {\n anchor: point,\n focus: {\n path: [{_key: block._key}, 'children', {_key: lastChild._key}],\n offset: isSpan(context, lastChild) ? lastChild.text.length : 0,\n },\n },\n }).at(0)\n\n if (!before || !after) {\n return undefined\n }\n\n if (!isTextBlock(context, before) || !isTextBlock(context, after)) {\n return undefined\n }\n\n return {before, after}\n}\n"],"names":["isEqualSelections","a","b","isEqualSelectionPoints","anchor","focus","isSelectionCollapsed","selection","path","join","offset","splitTextBlock","context","block","point","firstChild","children","at","lastChild","length","before","sliceBlocks","blocks","_key","after","isSpan","text","isTextBlock"],"mappings":";;;;;AAMgBA,SAAAA,kBAAkBC,GAAoBC,GAAoB;AACpE,SAAA,CAACD,KAAK,CAACC,IACF,KAGL,CAACD,KAAK,CAACC,IACF,KAIPC,uBAAuBF,EAAEG,QAAQF,EAAEE,MAAM,KACzCD,uBAAuBF,EAAEI,OAAOH,EAAEG,KAAK;AAE3C;ACdO,SAASC,qBAAqBC,WAA4B;AAC/D,SAAKA,YAKHA,UAAUH,OAAOI,KAAKC,KAAAA,MAAWF,UAAUF,MAAMG,KAAKC,KAAAA,KACtDF,UAAUH,OAAOM,WAAWH,UAAUF,MAAMK,SALrC;AAOX;ACJO,SAASC,eAAe;AAAA,EAC7BC;AAAAA,EACAC;AAAAA,EACAC;AAKF,GAA8E;AAC5E,QAAMC,aAAaF,MAAMG,SAASC,GAAG,CAAC,GAChCC,YAAYL,MAAMG,SAASC,GAAGJ,MAAMG,SAASG,SAAS,CAAC;AAEzD,MAAA,CAACJ,cAAc,CAACG;AAClB;AAGF,QAAME,SAASC,YAAY;AAAA,IACzBC,QAAQ,CAACT,KAAK;AAAA,IACdN,WAAW;AAAA,MACTH,QAAQ;AAAA,QACNI,MAAM,CAAC;AAAA,UAACe,MAAMV,MAAMU;AAAAA,WAAO,YAAY;AAAA,UAACA,MAAMR,WAAWQ;AAAAA,QAAAA,CAAK;AAAA,QAC9Db,QAAQ;AAAA,MACV;AAAA,MACAL,OAAOS;AAAAA,IAAAA;AAAAA,EAEV,CAAA,EAAEG,GAAG,CAAC,GACDO,QAAQH,YAAY;AAAA,IACxBC,QAAQ,CAACT,KAAK;AAAA,IACdN,WAAW;AAAA,MACTH,QAAQU;AAAAA,MACRT,OAAO;AAAA,QACLG,MAAM,CAAC;AAAA,UAACe,MAAMV,MAAMU;AAAAA,WAAO,YAAY;AAAA,UAACA,MAAML,UAAUK;AAAAA,QAAAA,CAAK;AAAA,QAC7Db,QAAQe,OAAOb,SAASM,SAAS,IAAIA,UAAUQ,KAAKP,SAAS;AAAA,MAAA;AAAA,IAC/D;AAAA,EACF,CACD,EAAEF,GAAG,CAAC;AAEP,MAAI,EAACG,CAAAA,UAAU,CAACI,UAIZ,EAACG,CAAAA,YAAYf,SAASQ,MAAM,KAAK,CAACO,YAAYf,SAASY,KAAK;AAIzD,WAAA;AAAA,MAACJ;AAAAA,MAAQI;AAAAA,IAAK;AACvB;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/utils/util.get-selection-start-point.ts","../../src/utils/util.is-equal-selections.ts","../../src/utils/util.split-text-block.ts"],"sourcesContent":["import type {EditorSelection, EditorSelectionPoint} from '..'\n\n/**\n * @public\n */\nexport function getSelectionStartPoint<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n TEditorSelectionPoint extends\n EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>\n ? EditorSelectionPoint\n : null,\n>(selection: TEditorSelection): TEditorSelectionPoint {\n if (!selection) {\n return null as TEditorSelectionPoint\n }\n\n return (\n selection.backward ? selection.focus : selection.anchor\n ) as TEditorSelectionPoint\n}\n","import type {EditorSelection} from '../types/editor'\nimport {isEqualSelectionPoints} from './util.is-equal-selection-points'\n\n/**\n * @public\n */\nexport function isEqualSelections(a: EditorSelection, b: EditorSelection) {\n if (!a && !b) {\n return true\n }\n\n if (!a || !b) {\n return false\n }\n\n return (\n isEqualSelectionPoints(a.anchor, b.anchor) &&\n isEqualSelectionPoints(a.focus, b.focus)\n )\n}\n","import type {PortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelectionPoint} from '..'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isSpan} from './util.is-span'\nimport {isTextBlock} from './util.is-text-block'\nimport {sliceBlocks} from './util.slice-blocks'\n\n/**\n * @beta\n */\nexport function splitTextBlock({\n context,\n block,\n point,\n}: {\n context: Pick<EditorContext, 'schema'>\n block: PortableTextTextBlock\n point: EditorSelectionPoint\n}): {before: PortableTextTextBlock; after: PortableTextTextBlock} | undefined {\n const firstChild = block.children.at(0)\n const lastChild = block.children.at(block.children.length - 1)\n\n if (!firstChild || !lastChild) {\n return undefined\n }\n\n const before = sliceBlocks({\n blocks: [block],\n selection: {\n anchor: {\n path: [{_key: block._key}, 'children', {_key: firstChild._key}],\n offset: 0,\n },\n focus: point,\n },\n }).at(0)\n const after = sliceBlocks({\n blocks: [block],\n selection: {\n anchor: point,\n focus: {\n path: [{_key: block._key}, 'children', {_key: lastChild._key}],\n offset: isSpan(context, lastChild) ? lastChild.text.length : 0,\n },\n },\n }).at(0)\n\n if (!before || !after) {\n return undefined\n }\n\n if (!isTextBlock(context, before) || !isTextBlock(context, after)) {\n return undefined\n }\n\n return {before, after}\n}\n"],"names":["getSelectionStartPoint","selection","backward","focus","anchor","isEqualSelections","a","b","isEqualSelectionPoints","splitTextBlock","context","block","point","firstChild","children","at","lastChild","length","before","sliceBlocks","blocks","path","_key","offset","after","isSpan","text","isTextBlock"],"mappings":";;;;;;AAKO,SAASA,uBAMdC,WAAoD;AACpD,SAAKA,YAKHA,UAAUC,WAAWD,UAAUE,QAAQF,UAAUG,SAJ1C;AAMX;ACbgBC,SAAAA,kBAAkBC,GAAoBC,GAAoB;AACp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ANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.40.3",
3
+ "version": "1.41.0",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -79,8 +79,8 @@
79
79
  "slate-react": "0.112.1",
80
80
  "use-effect-event": "^1.0.2",
81
81
  "xstate": "^5.19.2",
82
- "@portabletext/block-tools": "1.1.14",
83
- "@portabletext/patches": "1.1.3"
82
+ "@portabletext/patches": "1.1.3",
83
+ "@portabletext/block-tools": "1.1.14"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@portabletext/toolkit": "^2.0.17",
@@ -112,7 +112,7 @@
112
112
  "vite": "^6.2.0",
113
113
  "vitest": "^3.0.8",
114
114
  "vitest-browser-react": "^0.1.1",
115
- "racejar": "1.2.2"
115
+ "racejar": "1.2.3"
116
116
  },
117
117
  "peerDependencies": {
118
118
  "@sanity/schema": "^3.80.1",
@@ -122,14 +122,18 @@ export const insertBlocksActionImplementation: BehaviorActionImplementation<
122
122
  index++
123
123
  }
124
124
  } else {
125
+ let index = 0
126
+
125
127
  for (const block of fragment) {
126
128
  insertBlock({
127
129
  block,
128
- placement: 'auto',
130
+ placement: index === 0 ? 'auto' : 'after',
129
131
  select: 'end',
130
132
  editor: action.editor,
131
133
  schema: context.schema,
132
134
  })
135
+
136
+ index++
133
137
  }
134
138
  }
135
139
  }
@@ -25,19 +25,32 @@ export const converterTextPlain = defineConverter({
25
25
 
26
26
  const data = blocks
27
27
  .map((block) => {
28
- if (!isPortableTextTextBlock(block)) {
29
- return ''
30
- }
28
+ if (isPortableTextTextBlock(block)) {
29
+ return block.children
30
+ .map((child) => {
31
+ if (child._type === snapshot.context.schema.span.name) {
32
+ return child.text
33
+ }
31
34
 
32
- return block.children
33
- .map((child) => {
34
- if (child._type === snapshot.context.schema.span.name) {
35
- return child.text
36
- }
35
+ return snapshot.beta.hasTag('dragging internally')
36
+ ? `[${
37
+ snapshot.context.schema.inlineObjects.find(
38
+ (inlineObjectType) =>
39
+ inlineObjectType.name === child._type,
40
+ )?.title ?? 'Object'
41
+ }]`
42
+ : ''
43
+ })
44
+ .join('')
45
+ }
37
46
 
38
- return ''
39
- })
40
- .join('')
47
+ return snapshot.beta.hasTag('dragging internally')
48
+ ? `[${
49
+ snapshot.context.schema.blockObjects.find(
50
+ (blockObjectType) => blockObjectType.name === block._type,
51
+ )?.title ?? 'Object'
52
+ }]`
53
+ : ''
41
54
  })
42
55
  .filter((block) => block !== '')
43
56
  .join('\n\n')