@portabletext/editor 1.55.4 → 1.55.6

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 (54) hide show
  1. package/lib/_chunks-cjs/util.slice-text-block.cjs +67 -0
  2. package/lib/_chunks-cjs/util.slice-text-block.cjs.map +1 -0
  3. package/lib/_chunks-es/util.slice-text-block.js +69 -0
  4. package/lib/_chunks-es/util.slice-text-block.js.map +1 -0
  5. package/lib/behaviors/index.d.cts +72 -52
  6. package/lib/behaviors/index.d.ts +72 -52
  7. package/lib/index.cjs +15 -15
  8. package/lib/index.cjs.map +1 -1
  9. package/lib/index.d.cts +117 -69
  10. package/lib/index.d.ts +117 -69
  11. package/lib/index.js +8 -8
  12. package/lib/index.js.map +1 -1
  13. package/lib/plugins/index.d.cts +71 -51
  14. package/lib/plugins/index.d.ts +71 -51
  15. package/lib/selectors/index.d.cts +68 -51
  16. package/lib/selectors/index.d.ts +68 -51
  17. package/lib/utils/index.cjs +12 -13
  18. package/lib/utils/index.cjs.map +1 -1
  19. package/lib/utils/index.d.cts +68 -51
  20. package/lib/utils/index.d.ts +68 -51
  21. package/lib/utils/index.js +14 -15
  22. package/lib/utils/index.js.map +1 -1
  23. package/package.json +2 -2
  24. package/src/behaviors/behavior.abstract.split.ts +8 -9
  25. package/src/behaviors/behavior.types.event.ts +3 -0
  26. package/src/behaviors/index.ts +2 -1
  27. package/src/converters/converter.portable-text.deserialize.test.ts +3 -5
  28. package/src/converters/converter.text-html.deserialize.test.ts +2 -2
  29. package/src/converters/converter.text-html.serialize.test.ts +2 -2
  30. package/src/converters/converter.text-plain.test.ts +3 -5
  31. package/src/editor/editor-schema-definition.ts +106 -0
  32. package/src/editor/editor-schema.ts +65 -107
  33. package/src/editor.ts +1 -1
  34. package/src/index.ts +16 -2
  35. package/src/internal-utils/apply-operation-to-portable-text.test.ts +2 -1
  36. package/src/internal-utils/build-index-maps.test.ts +2 -1
  37. package/src/internal-utils/create-test-snapshot.ts +2 -1
  38. package/src/internal-utils/drag-selection.test.ts +2 -1
  39. package/src/internal-utils/parse-blocks.test.ts +2 -1
  40. package/src/internal-utils/selection-text.ts +2 -1
  41. package/src/internal-utils/terse-pt.test.ts +2 -1
  42. package/src/internal-utils/test-editor.tsx +4 -1
  43. package/src/plugins/plugin.markdown.test.tsx +1 -1
  44. package/src/selectors/selector.get-selection-text.test.ts +2 -1
  45. package/src/selectors/selector.get-trimmed-selection.test.ts +2 -1
  46. package/src/utils/util.block-offset.test.ts +2 -1
  47. package/src/utils/util.slice-blocks.test.ts +2 -1
  48. package/src/utils/util.slice-text-block.test.ts +163 -0
  49. package/src/utils/util.slice-text-block.ts +89 -0
  50. package/src/utils/util.split-text-block.ts +7 -16
  51. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +0 -23
  52. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +0 -1
  53. package/lib/_chunks-es/util.selection-point-to-block-offset.js +0 -25
  54. package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +0 -1
@@ -0,0 +1,89 @@
1
+ import type {PortableTextChild, PortableTextTextBlock} from '@sanity/types'
2
+ import type {EditorContext} from '..'
3
+ import {isSpan} from '../internal-utils/parse-blocks'
4
+ import {
5
+ getBlockKeyFromSelectionPoint,
6
+ getChildKeyFromSelectionPoint,
7
+ } from '../selection/selection-point'
8
+ import {getSelectionEndPoint} from './util.get-selection-end-point'
9
+ import {getSelectionStartPoint} from './util.get-selection-start-point'
10
+
11
+ export function sliceTextBlock({
12
+ context,
13
+ block,
14
+ }: {
15
+ context: Pick<EditorContext, 'schema' | 'selection'>
16
+ block: PortableTextTextBlock
17
+ }): PortableTextTextBlock {
18
+ const startPoint = getSelectionStartPoint(context.selection)
19
+ const endPoint = getSelectionEndPoint(context.selection)
20
+
21
+ if (!startPoint || !endPoint) {
22
+ return block
23
+ }
24
+
25
+ const startBlockKey = getBlockKeyFromSelectionPoint(startPoint)
26
+ const endBlockKey = getBlockKeyFromSelectionPoint(endPoint)
27
+
28
+ if (startBlockKey !== endBlockKey || startBlockKey !== block._key) {
29
+ return block
30
+ }
31
+
32
+ const startChildKey = getChildKeyFromSelectionPoint(startPoint)
33
+ const endChildKey = getChildKeyFromSelectionPoint(endPoint)
34
+
35
+ if (!startChildKey || !endChildKey) {
36
+ return block
37
+ }
38
+
39
+ let startChildFound = false
40
+ const children: Array<PortableTextChild> = []
41
+
42
+ for (const child of block.children) {
43
+ if (child._key === startChildKey) {
44
+ startChildFound = true
45
+
46
+ if (isSpan(context, child)) {
47
+ const text =
48
+ child._key === endChildKey
49
+ ? child.text.slice(startPoint.offset, endPoint.offset)
50
+ : child.text.slice(startPoint.offset)
51
+
52
+ children.push({
53
+ ...child,
54
+ text,
55
+ })
56
+ } else {
57
+ children.push(child)
58
+ }
59
+
60
+ if (startChildKey === endChildKey) {
61
+ break
62
+ }
63
+
64
+ continue
65
+ }
66
+
67
+ if (child._key === endChildKey) {
68
+ if (isSpan(context, child)) {
69
+ children.push({
70
+ ...child,
71
+ text: child.text.slice(0, endPoint.offset),
72
+ })
73
+ } else {
74
+ children.push(child)
75
+ }
76
+
77
+ break
78
+ }
79
+
80
+ if (startChildFound) {
81
+ children.push(child)
82
+ }
83
+ }
84
+
85
+ return {
86
+ ...block,
87
+ children,
88
+ }
89
+ }
@@ -2,8 +2,7 @@ import type {PortableTextTextBlock} from '@sanity/types'
2
2
  import type {EditorSelectionPoint} from '..'
3
3
  import type {EditorContext} from '../editor/editor-snapshot'
4
4
  import {isSpan} from './util.is-span'
5
- import {isTextBlock} from './util.is-text-block'
6
- import {sliceBlocks} from './util.slice-blocks'
5
+ import {sliceTextBlock} from './util.slice-text-block'
7
6
 
8
7
  /**
9
8
  * @beta
@@ -24,7 +23,7 @@ export function splitTextBlock({
24
23
  return undefined
25
24
  }
26
25
 
27
- const before = sliceBlocks({
26
+ const before = sliceTextBlock({
28
27
  context: {
29
28
  schema: context.schema,
30
29
  selection: {
@@ -35,9 +34,9 @@ export function splitTextBlock({
35
34
  focus: point,
36
35
  },
37
36
  },
38
- blocks: [block],
39
- }).at(0)
40
- const after = sliceBlocks({
37
+ block,
38
+ })
39
+ const after = sliceTextBlock({
41
40
  context: {
42
41
  schema: context.schema,
43
42
  selection: {
@@ -48,16 +47,8 @@ export function splitTextBlock({
48
47
  },
49
48
  },
50
49
  },
51
- blocks: [block],
52
- }).at(0)
53
-
54
- if (!before || !after) {
55
- return undefined
56
- }
57
-
58
- if (!isTextBlock(context, before) || !isTextBlock(context, after)) {
59
- return undefined
60
- }
50
+ block,
51
+ })
61
52
 
62
53
  return {before, after}
63
54
  }
@@ -1,23 +0,0 @@
1
- "use strict";
2
- var selectionPoint = require("./selection-point.cjs"), util_childSelectionPointToBlockOffset = require("./util.child-selection-point-to-block-offset.cjs");
3
- function isSelectionCollapsed(selection) {
4
- return selection ? JSON.stringify(selection.anchor.path) === JSON.stringify(selection.focus.path) && selection.anchor.offset === selection.focus.offset : !1;
5
- }
6
- function selectionPointToBlockOffset({
7
- context,
8
- selectionPoint: selectionPoint$1
9
- }) {
10
- const blockKey = selectionPoint.getBlockKeyFromSelectionPoint(selectionPoint$1);
11
- return selectionPoint$1.path.length === 1 && blockKey !== void 0 ? {
12
- path: [{
13
- _key: blockKey
14
- }],
15
- offset: selectionPoint$1.offset
16
- } : util_childSelectionPointToBlockOffset.childSelectionPointToBlockOffset({
17
- context,
18
- selectionPoint: selectionPoint$1
19
- });
20
- }
21
- exports.isSelectionCollapsed = isSelectionCollapsed;
22
- exports.selectionPointToBlockOffset = selectionPointToBlockOffset;
23
- //# sourceMappingURL=util.selection-point-to-block-offset.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.selection-point-to-block-offset.cjs","sources":["../../src/utils/util.is-selection-collapsed.ts","../../src/utils/util.selection-point-to-block-offset.ts"],"sourcesContent":["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 JSON.stringify(selection.anchor.path) ===\n JSON.stringify(selection.focus.path) &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n","import type {EditorContext} from '../editor/editor-snapshot'\nimport {getBlockKeyFromSelectionPoint} from '../selection/selection-point'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {childSelectionPointToBlockOffset} from './util.child-selection-point-to-block-offset'\n\n/**\n * @public\n */\nexport function selectionPointToBlockOffset({\n context,\n selectionPoint,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n selectionPoint: EditorSelectionPoint\n}): BlockOffset | undefined {\n const blockKey = getBlockKeyFromSelectionPoint(selectionPoint)\n\n if (selectionPoint.path.length === 1 && blockKey !== undefined) {\n return {\n path: [{_key: blockKey}],\n offset: selectionPoint.offset,\n }\n }\n\n return childSelectionPointToBlockOffset({\n context,\n selectionPoint,\n })\n}\n"],"names":["isSelectionCollapsed","selection","JSON","stringify","anchor","path","focus","offset","selectionPointToBlockOffset","context","selectionPoint","blockKey","getBlockKeyFromSelectionPoint","length","undefined","_key","childSelectionPointToBlockOffset"],"mappings":";;AAKO,SAASA,qBAAqBC,WAA4B;AAC/D,SAAKA,YAKHC,KAAKC,UAAUF,UAAUG,OAAOC,IAAI,MAClCH,KAAKC,UAAUF,UAAUK,MAAMD,IAAI,KACrCJ,UAAUG,OAAOG,WAAWN,UAAUK,MAAMC,SANrC;AAQX;ACNO,SAASC,4BAA4B;AAAA,EAC1CC;AAAAA,EAAAA,gBACAC;AAIF,GAA4B;AAC1B,QAAMC,WAAWC,eAAAA,8BAA8BF,gBAAc;AAE7D,SAAIA,iBAAeL,KAAKQ,WAAW,KAAKF,aAAaG,SAC5C;AAAA,IACLT,MAAM,CAAC;AAAA,MAACU,MAAMJ;AAAAA,IAAAA,CAAS;AAAA,IACvBJ,QAAQG,iBAAeH;AAAAA,EAAAA,IAIpBS,uEAAiC;AAAA,IACtCP;AAAAA,IAAAA,gBACAC;AAAAA,EAAAA,CACD;AACH;;;"}
@@ -1,25 +0,0 @@
1
- import { getBlockKeyFromSelectionPoint } from "./selection-point.js";
2
- import { childSelectionPointToBlockOffset } from "./util.child-selection-point-to-block-offset.js";
3
- function isSelectionCollapsed(selection) {
4
- return selection ? JSON.stringify(selection.anchor.path) === JSON.stringify(selection.focus.path) && selection.anchor.offset === selection.focus.offset : !1;
5
- }
6
- function selectionPointToBlockOffset({
7
- context,
8
- selectionPoint
9
- }) {
10
- const blockKey = getBlockKeyFromSelectionPoint(selectionPoint);
11
- return selectionPoint.path.length === 1 && blockKey !== void 0 ? {
12
- path: [{
13
- _key: blockKey
14
- }],
15
- offset: selectionPoint.offset
16
- } : childSelectionPointToBlockOffset({
17
- context,
18
- selectionPoint
19
- });
20
- }
21
- export {
22
- isSelectionCollapsed,
23
- selectionPointToBlockOffset
24
- };
25
- //# sourceMappingURL=util.selection-point-to-block-offset.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"util.selection-point-to-block-offset.js","sources":["../../src/utils/util.is-selection-collapsed.ts","../../src/utils/util.selection-point-to-block-offset.ts"],"sourcesContent":["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 JSON.stringify(selection.anchor.path) ===\n JSON.stringify(selection.focus.path) &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n","import type {EditorContext} from '../editor/editor-snapshot'\nimport {getBlockKeyFromSelectionPoint} from '../selection/selection-point'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {childSelectionPointToBlockOffset} from './util.child-selection-point-to-block-offset'\n\n/**\n * @public\n */\nexport function selectionPointToBlockOffset({\n context,\n selectionPoint,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n selectionPoint: EditorSelectionPoint\n}): BlockOffset | undefined {\n const blockKey = getBlockKeyFromSelectionPoint(selectionPoint)\n\n if (selectionPoint.path.length === 1 && blockKey !== undefined) {\n return {\n path: [{_key: blockKey}],\n offset: selectionPoint.offset,\n }\n }\n\n return childSelectionPointToBlockOffset({\n context,\n selectionPoint,\n })\n}\n"],"names":["isSelectionCollapsed","selection","JSON","stringify","anchor","path","focus","offset","selectionPointToBlockOffset","context","selectionPoint","blockKey","getBlockKeyFromSelectionPoint","length","undefined","_key","childSelectionPointToBlockOffset"],"mappings":";;AAKO,SAASA,qBAAqBC,WAA4B;AAC/D,SAAKA,YAKHC,KAAKC,UAAUF,UAAUG,OAAOC,IAAI,MAClCH,KAAKC,UAAUF,UAAUK,MAAMD,IAAI,KACrCJ,UAAUG,OAAOG,WAAWN,UAAUK,MAAMC,SANrC;AAQX;ACNO,SAASC,4BAA4B;AAAA,EAC1CC;AAAAA,EACAC;AAIF,GAA4B;AAC1B,QAAMC,WAAWC,8BAA8BF,cAAc;AAE7D,SAAIA,eAAeL,KAAKQ,WAAW,KAAKF,aAAaG,SAC5C;AAAA,IACLT,MAAM,CAAC;AAAA,MAACU,MAAMJ;AAAAA,IAAAA,CAAS;AAAA,IACvBJ,QAAQG,eAAeH;AAAAA,EAAAA,IAIpBS,iCAAiC;AAAA,IACtCP;AAAAA,IACAC;AAAAA,EAAAA,CACD;AACH;"}