@portabletext/editor 1.28.0 → 1.30.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 (61) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +40 -37
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/parse-blocks.cjs +79 -0
  4. package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -0
  5. package/lib/_chunks-cjs/plugin.event-listener.cjs +55 -97
  6. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
  7. package/lib/_chunks-cjs/selector.get-selection-start-point.cjs +15 -0
  8. package/lib/_chunks-cjs/selector.get-selection-start-point.cjs.map +1 -0
  9. package/lib/_chunks-es/behavior.core.js +40 -37
  10. package/lib/_chunks-es/behavior.core.js.map +1 -1
  11. package/lib/_chunks-es/parse-blocks.js +80 -0
  12. package/lib/_chunks-es/parse-blocks.js.map +1 -0
  13. package/lib/_chunks-es/plugin.event-listener.js +57 -98
  14. package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
  15. package/lib/_chunks-es/selector.get-selection-start-point.js +16 -0
  16. package/lib/_chunks-es/selector.get-selection-start-point.js.map +1 -0
  17. package/lib/behaviors/index.d.cts +196 -124
  18. package/lib/behaviors/index.d.ts +196 -124
  19. package/lib/index.d.cts +248 -0
  20. package/lib/index.d.ts +248 -0
  21. package/lib/plugins/index.cjs +249 -1
  22. package/lib/plugins/index.cjs.map +1 -1
  23. package/lib/plugins/index.d.cts +246 -1
  24. package/lib/plugins/index.d.ts +246 -1
  25. package/lib/plugins/index.js +257 -3
  26. package/lib/plugins/index.js.map +1 -1
  27. package/lib/selectors/index.cjs +28 -1
  28. package/lib/selectors/index.cjs.map +1 -1
  29. package/lib/selectors/index.d.cts +21 -0
  30. package/lib/selectors/index.d.ts +21 -0
  31. package/lib/selectors/index.js +28 -0
  32. package/lib/selectors/index.js.map +1 -1
  33. package/lib/utils/index.cjs +70 -1
  34. package/lib/utils/index.cjs.map +1 -1
  35. package/lib/utils/index.d.cts +168 -2
  36. package/lib/utils/index.d.ts +168 -2
  37. package/lib/utils/index.js +71 -1
  38. package/lib/utils/index.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/behavior-actions/behavior.action.delete.ts +18 -0
  41. package/src/behavior-actions/behavior.action.insert-break.ts +3 -8
  42. package/src/behavior-actions/behavior.actions.ts +9 -0
  43. package/src/behaviors/_exports/index.ts +1 -0
  44. package/src/behaviors/behavior.core.deserialize.ts +52 -38
  45. package/src/behaviors/behavior.core.ts +4 -11
  46. package/src/behaviors/behavior.types.ts +4 -0
  47. package/src/editor/PortableTextEditor.tsx +20 -0
  48. package/src/internal-utils/__tests__/patchToOperations.test.ts +19 -21
  49. package/src/internal-utils/applyPatch.ts +11 -3
  50. package/src/plugins/index.ts +2 -0
  51. package/src/plugins/plugin.behavior.tsx +22 -0
  52. package/src/plugins/plugin.one-line.tsx +225 -0
  53. package/src/selectors/index.ts +3 -0
  54. package/src/selectors/selector.get-selection-end-point.ts +17 -0
  55. package/src/selectors/selector.get-selection-start-point.ts +17 -0
  56. package/src/selectors/selector.is-overlapping-selection.ts +46 -0
  57. package/src/utils/index.ts +4 -0
  58. package/src/utils/util.is-span.ts +12 -0
  59. package/src/utils/util.is-text-block.ts +12 -0
  60. package/src/utils/util.merge-text-blocks.ts +36 -0
  61. package/src/utils/util.split-text-block.ts +55 -0
@@ -0,0 +1,36 @@
1
+ import type {PortableTextTextBlock} from '@sanity/types'
2
+ import {parseBlock} from '../internal-utils/parse-blocks'
3
+ import type {EditorContext} from '../selectors'
4
+ import {isTextBlock} from './util.is-text-block'
5
+
6
+ /**
7
+ * @beta
8
+ */
9
+ export function mergeTextBlocks({
10
+ context,
11
+ targetBlock,
12
+ incomingBlock,
13
+ }: {
14
+ context: Pick<EditorContext, 'keyGenerator' | 'schema'>
15
+ targetBlock: PortableTextTextBlock
16
+ incomingBlock: PortableTextTextBlock
17
+ }) {
18
+ const parsedIncomingBlock = parseBlock({
19
+ context,
20
+ block: incomingBlock,
21
+ options: {refreshKeys: true},
22
+ })
23
+
24
+ if (!parsedIncomingBlock || !isTextBlock(context, parsedIncomingBlock)) {
25
+ return targetBlock
26
+ }
27
+
28
+ return {
29
+ ...targetBlock,
30
+ children: [...targetBlock.children, ...parsedIncomingBlock.children],
31
+ markDefs: [
32
+ ...(targetBlock.markDefs ?? []),
33
+ ...(parsedIncomingBlock.markDefs ?? []),
34
+ ],
35
+ }
36
+ }
@@ -0,0 +1,55 @@
1
+ import type {PortableTextTextBlock} from '@sanity/types'
2
+ import {isTextBlock, sliceBlocks, type EditorSelectionPoint} from '.'
3
+ import type {EditorContext} from '../selectors'
4
+ import {isSpan} from './util.is-span'
5
+
6
+ /**
7
+ * @beta
8
+ */
9
+ export function splitTextBlock({
10
+ context,
11
+ block,
12
+ point,
13
+ }: {
14
+ context: Pick<EditorContext, 'schema'>
15
+ block: PortableTextTextBlock
16
+ point: EditorSelectionPoint
17
+ }): {before: PortableTextTextBlock; after: PortableTextTextBlock} | undefined {
18
+ const firstChild = block.children.at(0)
19
+ const lastChild = block.children.at(block.children.length - 1)
20
+
21
+ if (!firstChild || !lastChild) {
22
+ return undefined
23
+ }
24
+
25
+ const before = sliceBlocks({
26
+ blocks: [block],
27
+ selection: {
28
+ anchor: {
29
+ path: [{_key: block._key}, 'children', {_key: firstChild._key}],
30
+ offset: 0,
31
+ },
32
+ focus: point,
33
+ },
34
+ }).at(0)
35
+ const after = sliceBlocks({
36
+ blocks: [block],
37
+ selection: {
38
+ anchor: point,
39
+ focus: {
40
+ path: [{_key: block._key}, 'children', {_key: lastChild._key}],
41
+ offset: isSpan(context, lastChild) ? lastChild.text.length : 0,
42
+ },
43
+ },
44
+ }).at(0)
45
+
46
+ if (!before || !after) {
47
+ return undefined
48
+ }
49
+
50
+ if (!isTextBlock(context, before) || !isTextBlock(context, after)) {
51
+ return undefined
52
+ }
53
+
54
+ return {before, after}
55
+ }