@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.
- package/lib/_chunks-dts/index.d.ts +50 -210
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js +103 -20
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
- package/lib/_chunks-es/{util.get-text-block-text.js → util.slice-blocks.js} +29 -5
- package/lib/_chunks-es/util.slice-blocks.js.map +1 -0
- package/lib/_chunks-es/util.slice-text-block.js +13 -2
- package/lib/_chunks-es/util.slice-text-block.js.map +1 -1
- package/lib/behaviors/index.d.ts +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +323 -320
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.ts +2 -133
- package/lib/plugins/index.js +2 -796
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.d.ts +2 -24
- package/lib/selectors/index.js +28 -130
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.d.ts +3 -3
- package/lib/utils/index.js +97 -9
- package/lib/utils/index.js.map +1 -1
- package/package.json +7 -9
- package/src/behaviors/behavior.perform-event.ts +7 -7
- package/src/editor/PortableTextEditor.tsx +0 -19
- package/src/editor/create-editor.ts +0 -3
- package/src/editor/editor-machine.ts +0 -10
- package/src/editor/event-to-change.tsx +5 -1
- package/src/editor/plugins/create-with-event-listeners.ts +0 -4
- package/src/editor/plugins/createWithObjectKeys.ts +2 -1
- package/src/editor/plugins/createWithPatches.ts +3 -3
- package/src/editor/plugins/createWithPlaceholderBlock.ts +2 -1
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +2 -1
- package/src/editor/plugins/with-plugins.ts +10 -14
- package/src/editor/relay-machine.ts +0 -4
- package/src/editor/sync-machine.ts +2 -2
- package/src/editor.ts +0 -4
- package/src/history/behavior.operation.history.redo.ts +67 -0
- package/src/history/behavior.operation.history.undo.ts +71 -0
- package/src/history/event.history.undo.test.tsx +672 -0
- package/src/history/history.preserving-keys.test.tsx +112 -0
- package/src/history/remote-patches.ts +20 -0
- package/src/history/slate-plugin.history.ts +146 -0
- package/src/history/slate-plugin.redoing.ts +21 -0
- package/src/history/slate-plugin.undoing.ts +21 -0
- package/src/history/slate-plugin.without-history.ts +23 -0
- package/src/history/transform-operation.ts +245 -0
- package/src/history/undo-redo-collaboration.test.tsx +541 -0
- package/src/history/undo-redo.feature +125 -0
- package/src/history/undo-redo.test.tsx +195 -0
- package/src/history/undo-step.ts +148 -0
- package/src/index.ts +0 -1
- package/src/internal-utils/applyPatch.ts +46 -1
- package/src/operations/behavior.operations.ts +2 -4
- package/src/plugins/index.ts +0 -3
- package/src/selectors/index.ts +0 -3
- package/src/test/vitest/step-definitions.tsx +88 -8
- package/src/test/vitest/test-editor.tsx +1 -1
- package/lib/_chunks-es/selector.get-selection-text.js +0 -92
- package/lib/_chunks-es/selector.get-selection-text.js.map +0 -1
- package/lib/_chunks-es/selector.get-text-before.js +0 -36
- package/lib/_chunks-es/selector.get-text-before.js.map +0 -1
- package/lib/_chunks-es/util.get-text-block-text.js.map +0 -1
- package/lib/_chunks-es/util.is-empty-text-block.js +0 -40
- package/lib/_chunks-es/util.is-empty-text-block.js.map +0 -1
- package/lib/_chunks-es/util.merge-text-blocks.js +0 -101
- package/lib/_chunks-es/util.merge-text-blocks.js.map +0 -1
- package/src/editor/plugins/createWithMaxBlocks.ts +0 -53
- package/src/editor/plugins/createWithUndoRedo.ts +0 -628
- package/src/editor/with-undo-step.ts +0 -37
- package/src/editor/withUndoRedo.ts +0 -34
- package/src/editor-event-listener.tsx +0 -28
- package/src/plugins/plugin.decorator-shortcut.ts +0 -238
- package/src/plugins/plugin.markdown.test.tsx +0 -42
- package/src/plugins/plugin.markdown.tsx +0 -131
- package/src/plugins/plugin.one-line.tsx +0 -123
- package/src/selectors/selector.get-list-state.test.ts +0 -189
- package/src/selectors/selector.get-list-state.ts +0 -96
- package/src/selectors/selector.get-selected-slice.ts +0 -13
- package/src/selectors/selector.get-trimmed-selection.test.ts +0 -657
- 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
|
-
}
|