@portabletext/editor 1.20.0 → 1.21.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 (128) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +8 -68
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/selector.get-text-before.cjs +11 -39
  4. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  5. package/lib/_chunks-cjs/util.get-block-start-point.cjs +30 -0
  6. package/lib/_chunks-cjs/util.get-block-start-point.cjs.map +1 -0
  7. package/lib/_chunks-cjs/util.is-empty-text-block.cjs +71 -0
  8. package/lib/_chunks-cjs/util.is-empty-text-block.cjs.map +1 -0
  9. package/lib/_chunks-cjs/util.is-keyed-segment.cjs +6 -0
  10. package/lib/_chunks-cjs/util.is-keyed-segment.cjs.map +1 -0
  11. package/lib/_chunks-es/behavior.core.js +7 -66
  12. package/lib/_chunks-es/behavior.core.js.map +1 -1
  13. package/lib/_chunks-es/selector.get-text-before.js +5 -31
  14. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  15. package/lib/_chunks-es/util.get-block-start-point.js +31 -0
  16. package/lib/_chunks-es/util.get-block-start-point.js.map +1 -0
  17. package/lib/_chunks-es/util.is-empty-text-block.js +73 -0
  18. package/lib/_chunks-es/util.is-empty-text-block.js.map +1 -0
  19. package/lib/_chunks-es/util.is-keyed-segment.js +7 -0
  20. package/lib/_chunks-es/util.is-keyed-segment.js.map +1 -0
  21. package/lib/behaviors/index.cjs +7 -7
  22. package/lib/behaviors/index.cjs.map +1 -1
  23. package/lib/behaviors/index.d.cts +179 -119
  24. package/lib/behaviors/index.d.ts +179 -119
  25. package/lib/behaviors/index.js +3 -2
  26. package/lib/behaviors/index.js.map +1 -1
  27. package/lib/index.cjs +29 -6
  28. package/lib/index.cjs.map +1 -1
  29. package/lib/index.d.cts +844 -141
  30. package/lib/index.d.ts +844 -141
  31. package/lib/index.js +28 -4
  32. package/lib/index.js.map +1 -1
  33. package/lib/selectors/index.cjs +73 -1
  34. package/lib/selectors/index.cjs.map +1 -1
  35. package/lib/selectors/index.d.cts +14 -0
  36. package/lib/selectors/index.d.ts +14 -0
  37. package/lib/selectors/index.js +73 -0
  38. package/lib/selectors/index.js.map +1 -1
  39. package/lib/utils/index.cjs +11 -0
  40. package/lib/utils/index.cjs.map +1 -0
  41. package/lib/utils/index.d.cts +87 -0
  42. package/lib/utils/index.d.ts +87 -0
  43. package/lib/utils/index.js +13 -0
  44. package/lib/utils/index.js.map +1 -0
  45. package/package.json +15 -9
  46. package/src/behavior-actions/behavior.action-utils.insert-block.ts +1 -1
  47. package/src/behavior-actions/behavior.action.insert-block-object.ts +1 -1
  48. package/src/behavior-actions/behavior.action.insert-inline-object.ts +1 -1
  49. package/src/behavior-actions/behavior.action.text-block.set.ts +1 -1
  50. package/src/behavior-actions/behavior.action.text-block.unset.ts +1 -1
  51. package/src/behavior-actions/behavior.actions.ts +5 -5
  52. package/src/behaviors/behavior.code-editor.ts +1 -1
  53. package/src/behaviors/behavior.core.block-objects.ts +2 -2
  54. package/src/behaviors/behavior.core.decorators.ts +1 -1
  55. package/src/behaviors/behavior.core.lists.ts +2 -2
  56. package/src/behaviors/behavior.emoji-picker.ts +1 -1
  57. package/src/behaviors/behavior.links.ts +1 -1
  58. package/src/behaviors/behavior.markdown.ts +2 -2
  59. package/src/behaviors/behavior.types.ts +52 -19
  60. package/src/behaviors/index.ts +1 -0
  61. package/src/editor/Editable.tsx +12 -12
  62. package/src/editor/PortableTextEditor.tsx +2 -2
  63. package/src/editor/components/DraggableBlock.tsx +2 -2
  64. package/src/editor/components/Element.tsx +3 -3
  65. package/src/editor/components/Leaf.tsx +1 -1
  66. package/src/editor/components/Synchronizer.tsx +1 -1
  67. package/src/editor/create-editor.ts +22 -20
  68. package/src/editor/create-slate-editor.tsx +5 -2
  69. package/src/editor/editor-machine.ts +42 -14
  70. package/src/editor/get-value.ts +2 -2
  71. package/src/editor/hooks/usePortableTextEditorSelection.tsx +1 -1
  72. package/src/editor/mutation-machine.ts +2 -2
  73. package/src/editor/plugins/create-with-event-listeners.ts +11 -3
  74. package/src/editor/plugins/createWithEditableAPI.ts +7 -7
  75. package/src/editor/plugins/createWithHotKeys.ts +2 -2
  76. package/src/editor/plugins/createWithInsertData.ts +7 -7
  77. package/src/editor/plugins/createWithMaxBlocks.ts +2 -2
  78. package/src/editor/plugins/createWithObjectKeys.ts +2 -2
  79. package/src/editor/plugins/createWithPatches.ts +10 -10
  80. package/src/editor/plugins/createWithPlaceholderBlock.ts +3 -3
  81. package/src/editor/plugins/createWithPortableTextBlockStyle.ts +1 -1
  82. package/src/editor/plugins/createWithPortableTextMarkModel.ts +4 -4
  83. package/src/editor/plugins/createWithPortableTextSelections.ts +6 -6
  84. package/src/editor/plugins/createWithSchemaTypes.ts +1 -1
  85. package/src/editor/plugins/createWithUndoRedo.ts +5 -5
  86. package/src/editor/plugins/createWithUtils.ts +2 -2
  87. package/src/editor/plugins/with-plugins.ts +1 -1
  88. package/src/editor/sync-machine.ts +8 -5
  89. package/src/index.ts +5 -1
  90. package/src/selectors/index.ts +2 -0
  91. package/src/selectors/selector.get-selection-text.test.ts +75 -0
  92. package/src/selectors/selector.get-selection-text.ts +3 -3
  93. package/src/selectors/selector.get-text-before.ts +7 -4
  94. package/src/selectors/selector.is-point-after-selection.ts +82 -0
  95. package/src/selectors/selector.is-point-before-selection.ts +82 -0
  96. package/src/utils/_exports/index.ts +1 -0
  97. package/src/utils/index.ts +11 -0
  98. package/src/{editor/utils/utils.block-offset.test.ts → utils/util.block-offset.test.ts} +1 -1
  99. package/src/{editor/utils/utils.block-offset.ts → utils/util.block-offset.ts} +23 -7
  100. package/src/{editor/utils/utils.get-start-point.ts → utils/util.get-block-start-point.ts} +5 -2
  101. package/src/utils/util.get-text-block-text.ts +8 -0
  102. package/src/{editor/utils/utils.ts → utils/util.is-empty-text-block.ts} +4 -5
  103. package/src/{editor/utils/utils.is-keyed-segment.ts → utils/util.is-keyed-segment.ts} +3 -0
  104. package/src/{editor/utils/utils.reverse-selection.ts → utils/util.reverse-selection.ts} +4 -1
  105. /package/src/{utils → internal-utils}/__tests__/dmpToOperations.test.ts +0 -0
  106. /package/src/{utils → internal-utils}/__tests__/operationToPatches.test.ts +0 -0
  107. /package/src/{utils → internal-utils}/__tests__/patchToOperations.test.ts +0 -0
  108. /package/src/{utils → internal-utils}/__tests__/ranges.test.ts +0 -0
  109. /package/src/{utils → internal-utils}/__tests__/valueNormalization.test.tsx +0 -0
  110. /package/src/{utils → internal-utils}/__tests__/values.test.ts +0 -0
  111. /package/src/{utils → internal-utils}/applyPatch.ts +0 -0
  112. /package/src/{utils → internal-utils}/debug.ts +0 -0
  113. /package/src/{utils → internal-utils}/is-hotkey.test.ts +0 -0
  114. /package/src/{utils → internal-utils}/is-hotkey.ts +0 -0
  115. /package/src/{utils → internal-utils}/looks-like-url.test.ts +0 -0
  116. /package/src/{utils → internal-utils}/looks-like-url.ts +0 -0
  117. /package/src/{utils → internal-utils}/operationToPatches.ts +0 -0
  118. /package/src/{utils → internal-utils}/paths.ts +0 -0
  119. /package/src/{utils → internal-utils}/ranges.ts +0 -0
  120. /package/src/{utils → internal-utils}/schema.ts +0 -0
  121. /package/src/{utils → internal-utils}/selection.ts +0 -0
  122. /package/src/{utils → internal-utils}/sibling-utils.ts +0 -0
  123. /package/src/{utils → internal-utils}/validateValue.ts +0 -0
  124. /package/src/{utils → internal-utils}/values.ts +0 -0
  125. /package/src/{utils → internal-utils}/weakMaps.ts +0 -0
  126. /package/src/{utils → internal-utils}/withChanges.ts +0 -0
  127. /package/src/{utils → internal-utils}/withUndoRedo.ts +0 -0
  128. /package/src/{utils → internal-utils}/withoutPatching.ts +0 -0
@@ -4,8 +4,13 @@ import {
4
4
  type KeyedSegment,
5
5
  type PortableTextBlock,
6
6
  } from '@sanity/types'
7
- import type {BlockOffset} from '../../behaviors/behavior.types'
7
+ import type {BlockOffset} from '../behaviors/behavior.types'
8
+ import type {EditorSelectionPoint} from '../types/editor'
9
+ import {isKeyedSegment} from './util.is-keyed-segment'
8
10
 
11
+ /**
12
+ * @public
13
+ */
9
14
  export function blockOffsetToSpanSelectionPoint({
10
15
  value,
11
16
  blockOffset,
@@ -55,20 +60,31 @@ export function blockOffsetToSpanSelectionPoint({
55
60
  return selectionPoint
56
61
  }
57
62
 
63
+ /**
64
+ * @public
65
+ */
58
66
  export function spanSelectionPointToBlockOffset({
59
67
  value,
60
68
  selectionPoint,
61
69
  }: {
62
70
  value: Array<PortableTextBlock>
63
- selectionPoint: {
64
- path: [KeyedSegment, 'children', KeyedSegment]
65
- offset: number
66
- }
71
+ selectionPoint: EditorSelectionPoint
67
72
  }): BlockOffset | undefined {
68
73
  let offset = 0
69
74
 
75
+ const blockKey = isKeyedSegment(selectionPoint.path[0])
76
+ ? selectionPoint.path[0]._key
77
+ : undefined
78
+ const spanKey = isKeyedSegment(selectionPoint.path[2])
79
+ ? selectionPoint.path[2]._key
80
+ : undefined
81
+
82
+ if (!blockKey || !spanKey) {
83
+ return undefined
84
+ }
85
+
70
86
  for (const block of value) {
71
- if (block._key !== selectionPoint.path[0]._key) {
87
+ if (block._key !== blockKey) {
72
88
  continue
73
89
  }
74
90
 
@@ -81,7 +97,7 @@ export function spanSelectionPointToBlockOffset({
81
97
  continue
82
98
  }
83
99
 
84
- if (child._key === selectionPoint.path[2]._key) {
100
+ if (child._key === spanKey) {
85
101
  return {
86
102
  path: [{_key: block._key}],
87
103
  offset: offset + selectionPoint.offset,
@@ -3,9 +3,12 @@ import {
3
3
  type KeyedSegment,
4
4
  type PortableTextBlock,
5
5
  } from '@sanity/types'
6
- import type {EditorSelectionPoint} from '../../types/editor'
6
+ import type {EditorSelectionPoint} from '../types/editor'
7
7
 
8
- export function getStartPoint({
8
+ /**
9
+ * @public
10
+ */
11
+ export function getBlockStartPoint({
9
12
  node,
10
13
  path,
11
14
  }: {
@@ -0,0 +1,8 @@
1
+ import type {PortableTextTextBlock} from '@sanity/types'
2
+
3
+ /**
4
+ * @public
5
+ */
6
+ export function getTextBlockText(block: PortableTextTextBlock) {
7
+ return block.children.map((child) => child.text ?? '').join('')
8
+ }
@@ -2,9 +2,12 @@ import {
2
2
  isPortableTextSpan,
3
3
  isPortableTextTextBlock,
4
4
  type PortableTextBlock,
5
- type PortableTextTextBlock,
6
5
  } from '@sanity/types'
6
+ import {getTextBlockText} from './util.get-text-block-text'
7
7
 
8
+ /**
9
+ * @public
10
+ */
8
11
  export function isEmptyTextBlock(block: PortableTextBlock) {
9
12
  if (!isPortableTextTextBlock(block)) {
10
13
  return false
@@ -15,7 +18,3 @@ export function isEmptyTextBlock(block: PortableTextBlock) {
15
18
 
16
19
  return onlyText && blockText === ''
17
20
  }
18
-
19
- export function getTextBlockText(block: PortableTextTextBlock) {
20
- return block.children.map((child) => child.text ?? '').join('')
21
- }
@@ -1,5 +1,8 @@
1
1
  import type {KeyedSegment, PathSegment} from '@sanity/types'
2
2
 
3
+ /**
4
+ * @public
5
+ */
3
6
  export function isKeyedSegment(segment: PathSegment): segment is KeyedSegment {
4
7
  return typeof segment === 'object' && segment !== null && '_key' in segment
5
8
  }
@@ -1,5 +1,8 @@
1
- import type {EditorSelection} from '../../types/editor'
1
+ import type {EditorSelection} from '../types/editor'
2
2
 
3
+ /**
4
+ * @public
5
+ */
3
6
  export function reverseSelection(
4
7
  selection: NonNullable<EditorSelection>,
5
8
  ): NonNullable<EditorSelection> {
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes