@portabletext/editor 2.21.2 → 3.0.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 (79) hide show
  1. package/lib/_chunks-dts/index.d.ts +50 -210
  2. package/lib/_chunks-es/selector.is-at-the-start-of-block.js +103 -20
  3. package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
  4. package/lib/_chunks-es/{util.get-text-block-text.js → util.slice-blocks.js} +29 -5
  5. package/lib/_chunks-es/util.slice-blocks.js.map +1 -0
  6. package/lib/_chunks-es/util.slice-text-block.js +13 -2
  7. package/lib/_chunks-es/util.slice-text-block.js.map +1 -1
  8. package/lib/behaviors/index.d.ts +1 -1
  9. package/lib/index.d.ts +2 -2
  10. package/lib/index.js +323 -320
  11. package/lib/index.js.map +1 -1
  12. package/lib/plugins/index.d.ts +2 -133
  13. package/lib/plugins/index.js +2 -796
  14. package/lib/plugins/index.js.map +1 -1
  15. package/lib/selectors/index.d.ts +2 -24
  16. package/lib/selectors/index.js +28 -130
  17. package/lib/selectors/index.js.map +1 -1
  18. package/lib/utils/index.d.ts +3 -3
  19. package/lib/utils/index.js +97 -9
  20. package/lib/utils/index.js.map +1 -1
  21. package/package.json +7 -9
  22. package/src/behaviors/behavior.perform-event.ts +7 -7
  23. package/src/editor/PortableTextEditor.tsx +0 -19
  24. package/src/editor/create-editor.ts +0 -3
  25. package/src/editor/editor-machine.ts +0 -10
  26. package/src/editor/event-to-change.tsx +5 -1
  27. package/src/editor/plugins/create-with-event-listeners.ts +0 -4
  28. package/src/editor/plugins/createWithObjectKeys.ts +2 -1
  29. package/src/editor/plugins/createWithPatches.ts +3 -3
  30. package/src/editor/plugins/createWithPlaceholderBlock.ts +2 -1
  31. package/src/editor/plugins/createWithPortableTextMarkModel.ts +2 -1
  32. package/src/editor/plugins/with-plugins.ts +10 -14
  33. package/src/editor/relay-machine.ts +0 -4
  34. package/src/editor/sync-machine.ts +2 -2
  35. package/src/editor.ts +0 -4
  36. package/src/history/behavior.operation.history.redo.ts +67 -0
  37. package/src/history/behavior.operation.history.undo.ts +71 -0
  38. package/src/history/event.history.undo.test.tsx +672 -0
  39. package/src/history/history.preserving-keys.test.tsx +112 -0
  40. package/src/history/remote-patches.ts +20 -0
  41. package/src/history/slate-plugin.history.ts +146 -0
  42. package/src/history/slate-plugin.redoing.ts +21 -0
  43. package/src/history/slate-plugin.undoing.ts +21 -0
  44. package/src/history/slate-plugin.without-history.ts +23 -0
  45. package/src/history/transform-operation.ts +245 -0
  46. package/src/history/undo-redo-collaboration.test.tsx +541 -0
  47. package/src/history/undo-redo.feature +125 -0
  48. package/src/history/undo-redo.test.tsx +195 -0
  49. package/src/history/undo-step.ts +148 -0
  50. package/src/index.ts +0 -1
  51. package/src/internal-utils/applyPatch.ts +46 -1
  52. package/src/operations/behavior.operations.ts +2 -4
  53. package/src/plugins/index.ts +0 -3
  54. package/src/selectors/index.ts +0 -3
  55. package/src/test/vitest/step-definitions.tsx +88 -8
  56. package/src/test/vitest/test-editor.tsx +1 -1
  57. package/lib/_chunks-es/selector.get-selection-text.js +0 -92
  58. package/lib/_chunks-es/selector.get-selection-text.js.map +0 -1
  59. package/lib/_chunks-es/selector.get-text-before.js +0 -36
  60. package/lib/_chunks-es/selector.get-text-before.js.map +0 -1
  61. package/lib/_chunks-es/util.get-text-block-text.js.map +0 -1
  62. package/lib/_chunks-es/util.is-empty-text-block.js +0 -40
  63. package/lib/_chunks-es/util.is-empty-text-block.js.map +0 -1
  64. package/lib/_chunks-es/util.merge-text-blocks.js +0 -101
  65. package/lib/_chunks-es/util.merge-text-blocks.js.map +0 -1
  66. package/src/editor/plugins/createWithMaxBlocks.ts +0 -53
  67. package/src/editor/plugins/createWithUndoRedo.ts +0 -628
  68. package/src/editor/with-undo-step.ts +0 -37
  69. package/src/editor/withUndoRedo.ts +0 -34
  70. package/src/editor-event-listener.tsx +0 -28
  71. package/src/plugins/plugin.decorator-shortcut.ts +0 -238
  72. package/src/plugins/plugin.markdown.test.tsx +0 -42
  73. package/src/plugins/plugin.markdown.tsx +0 -131
  74. package/src/plugins/plugin.one-line.tsx +0 -123
  75. package/src/selectors/selector.get-list-state.test.ts +0 -189
  76. package/src/selectors/selector.get-list-state.ts +0 -96
  77. package/src/selectors/selector.get-selected-slice.ts +0 -13
  78. package/src/selectors/selector.get-trimmed-selection.test.ts +0 -657
  79. package/src/selectors/selector.get-trimmed-selection.ts +0 -189
@@ -1,189 +0,0 @@
1
- import {isSpan, isTextBlock} from '@portabletext/schema'
2
- import type {PortableTextSpan} from '@sanity/types'
3
- import type {EditorSelector} from '../editor/editor-selector'
4
- import type {EditorSelection, EditorSelectionPoint} from '../types/editor'
5
- import {getSelectionEndPoint} from '../utils/util.get-selection-end-point'
6
- import {getSelectionStartPoint} from '../utils/util.get-selection-start-point'
7
- import {isEmptyTextBlock} from '../utils/util.is-empty-text-block'
8
- import {
9
- getBlockKeyFromSelectionPoint,
10
- getChildKeyFromSelectionPoint,
11
- } from '../utils/util.selection-point'
12
- import {getFocusTextBlock} from './selector.get-focus-text-block'
13
- import {isSelectionCollapsed} from './selector.is-selection-collapsed'
14
-
15
- /**
16
- * @public
17
- *
18
- * @deprecated - Will be removed in the next major version.
19
- */
20
- export const getTrimmedSelection: EditorSelector<EditorSelection> = (
21
- snapshot,
22
- ) => {
23
- if (!snapshot.context.selection) {
24
- return snapshot.context.selection
25
- }
26
-
27
- const startPoint = getSelectionStartPoint(snapshot.context.selection)
28
- const endPoint = getSelectionEndPoint(snapshot.context.selection)
29
-
30
- const startBlockKey = getBlockKeyFromSelectionPoint(startPoint)
31
- const startChildKey = getChildKeyFromSelectionPoint(startPoint)
32
- const endBlockKey = getBlockKeyFromSelectionPoint(endPoint)
33
- const endChildKey = getChildKeyFromSelectionPoint(endPoint)
34
-
35
- if (!startBlockKey || !endBlockKey) {
36
- return snapshot.context.selection
37
- }
38
-
39
- const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey)
40
- const endBlockIndex = snapshot.blockIndexMap.get(endBlockKey)
41
-
42
- if (startBlockIndex === undefined || endBlockIndex === undefined) {
43
- return snapshot.context.selection
44
- }
45
-
46
- const slicedValue = snapshot.context.value.slice(
47
- startBlockIndex,
48
- endBlockIndex + 1,
49
- )
50
-
51
- let startBlockFound = false
52
- let adjustedStartPoint: EditorSelectionPoint | undefined
53
- let trimStartPoint = false
54
- let adjustedEndPoint: EditorSelectionPoint | undefined
55
- let trimEndPoint = false
56
- let previousPotentialEndpoint:
57
- | {blockKey: string; span: PortableTextSpan}
58
- | undefined
59
-
60
- for (const block of slicedValue) {
61
- if (block._key === startBlockKey) {
62
- startBlockFound = true
63
-
64
- if (
65
- isTextBlock(snapshot.context, block) &&
66
- isEmptyTextBlock(snapshot.context, block)
67
- ) {
68
- continue
69
- }
70
- }
71
-
72
- if (!startBlockFound) {
73
- continue
74
- }
75
-
76
- if (!isTextBlock(snapshot.context, block)) {
77
- continue
78
- }
79
-
80
- if (
81
- block._key === endBlockKey &&
82
- isEmptyTextBlock(snapshot.context, block)
83
- ) {
84
- break
85
- }
86
-
87
- for (const child of block.children) {
88
- if (child._key === endChildKey) {
89
- if (!isSpan(snapshot.context, child) || endPoint.offset === 0) {
90
- adjustedEndPoint = previousPotentialEndpoint
91
- ? {
92
- path: [
93
- {_key: previousPotentialEndpoint.blockKey},
94
- 'children',
95
- {_key: previousPotentialEndpoint.span._key},
96
- ],
97
- offset: previousPotentialEndpoint.span.text.length,
98
- }
99
- : undefined
100
-
101
- trimEndPoint = true
102
- break
103
- }
104
- }
105
-
106
- if (trimStartPoint) {
107
- const lonelySpan =
108
- isSpan(snapshot.context, child) && block.children.length === 1
109
-
110
- if (
111
- (isSpan(snapshot.context, child) && child.text.length > 0) ||
112
- lonelySpan
113
- ) {
114
- adjustedStartPoint = {
115
- path: [{_key: block._key}, 'children', {_key: child._key}],
116
- offset: 0,
117
- }
118
- previousPotentialEndpoint = {blockKey: block._key, span: child}
119
- trimStartPoint = false
120
- }
121
-
122
- continue
123
- }
124
-
125
- if (child._key === startChildKey) {
126
- if (!isSpan(snapshot.context, child)) {
127
- trimStartPoint = true
128
- continue
129
- }
130
-
131
- if (startPoint.offset === child.text.length) {
132
- trimStartPoint = true
133
- previousPotentialEndpoint =
134
- child.text.length > 0
135
- ? {blockKey: block._key, span: child}
136
- : previousPotentialEndpoint
137
- continue
138
- }
139
- }
140
-
141
- previousPotentialEndpoint =
142
- isSpan(snapshot.context, child) && child.text.length > 0
143
- ? {blockKey: block._key, span: child}
144
- : previousPotentialEndpoint
145
- }
146
-
147
- if (block._key === endBlockKey) {
148
- break
149
- }
150
- }
151
-
152
- const trimmedSelection = snapshot.context.selection.backward
153
- ? {
154
- anchor: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,
155
- focus: adjustedStartPoint ?? startPoint,
156
- backward: true,
157
- }
158
- : {
159
- anchor: adjustedStartPoint ?? startPoint,
160
- focus: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,
161
- }
162
-
163
- if (
164
- isSelectionCollapsed({
165
- ...snapshot,
166
- context: {
167
- ...snapshot.context,
168
- selection: trimmedSelection,
169
- },
170
- })
171
- ) {
172
- const focusTextBlock = getFocusTextBlock({
173
- ...snapshot,
174
- context: {
175
- ...snapshot.context,
176
- selection: trimmedSelection,
177
- },
178
- })
179
-
180
- if (
181
- focusTextBlock &&
182
- !isEmptyTextBlock(snapshot.context, focusTextBlock.node)
183
- ) {
184
- return null
185
- }
186
- }
187
-
188
- return trimmedSelection
189
- }