@portabletext/editor 1.13.0 → 1.14.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/README.md +1 -1
- package/lib/_chunks-cjs/selector.get-text-before.cjs +320 -0
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -0
- package/lib/_chunks-es/selector.get-text-before.js +321 -0
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -0
- package/lib/{index.esm.js → index.cjs} +1703 -1431
- package/lib/index.cjs.map +1 -0
- package/lib/{index.d.mts → index.d.cts} +4038 -313
- package/lib/index.d.ts +4038 -313
- package/lib/index.js +1724 -1407
- package/lib/index.js.map +1 -1
- package/lib/selectors/index.cjs +35 -0
- package/lib/selectors/index.cjs.map +1 -0
- package/lib/selectors/index.d.cts +243 -0
- package/lib/selectors/index.d.ts +243 -0
- package/lib/selectors/index.js +36 -0
- package/lib/selectors/index.js.map +1 -0
- package/package.json +21 -13
- package/src/editor/Editable.tsx +1 -1
- package/src/editor/PortableTextEditor.tsx +19 -4
- package/src/editor/__tests__/handleClick.test.tsx +4 -4
- package/src/editor/behavior/behavior.action.insert-block-object.ts +1 -1
- package/src/editor/behavior/behavior.action.insert-break.ts +3 -3
- package/src/editor/behavior/behavior.action.insert-inline-object.ts +58 -0
- package/src/editor/behavior/behavior.action.insert-span.ts +1 -1
- package/src/editor/behavior/behavior.action.list-item.ts +100 -0
- package/src/editor/behavior/behavior.action.style.ts +108 -0
- package/src/editor/behavior/behavior.action.text-block.set.ts +25 -0
- package/src/editor/behavior/behavior.action.text-block.unset.ts +17 -0
- package/src/editor/behavior/behavior.actions.ts +178 -109
- package/src/editor/behavior/behavior.code-editor.ts +30 -40
- package/src/editor/behavior/behavior.core.block-objects.ts +26 -26
- package/src/editor/behavior/behavior.core.decorators.ts +9 -6
- package/src/editor/behavior/behavior.core.lists.ts +139 -17
- package/src/editor/behavior/behavior.core.ts +5 -2
- package/src/editor/behavior/behavior.guards.ts +28 -0
- package/src/editor/behavior/behavior.links.ts +7 -7
- package/src/editor/behavior/behavior.markdown.ts +68 -79
- package/src/editor/behavior/behavior.types.ts +86 -60
- package/src/editor/{use-editor.ts → create-editor.ts} +13 -8
- package/src/editor/editor-event-listener.tsx +2 -2
- package/src/editor/editor-machine.ts +52 -13
- package/src/editor/editor-provider.tsx +5 -5
- package/src/editor/editor-selector.ts +49 -0
- package/src/editor/editor-snapshot.ts +22 -0
- package/src/editor/get-value.ts +11 -0
- package/src/editor/plugins/create-with-event-listeners.ts +93 -5
- package/src/editor/plugins/createWithEditableAPI.ts +69 -20
- package/src/editor/plugins/createWithHotKeys.ts +0 -54
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +1 -55
- package/src/editor/plugins/with-plugins.ts +4 -8
- package/src/editor/{behavior/behavior.utils.block-offset.test.ts → utils/utils.block-offset.test.ts} +1 -1
- package/src/editor/{behavior/behavior.utils.block-offset.ts → utils/utils.block-offset.ts} +1 -8
- package/src/editor/{behavior/behavior.utils.reverse-selection.ts → utils/utils.reverse-selection.ts} +3 -5
- package/src/editor/utils/utils.ts +21 -0
- package/src/index.ts +13 -13
- package/src/selectors/index.ts +15 -0
- package/src/selectors/selector.get-active-list-item.ts +37 -0
- package/src/{editor/behavior/behavior.utils.get-selection-text.ts → selectors/selector.get-selection-text.ts} +10 -15
- package/src/selectors/selector.get-text-before.ts +41 -0
- package/src/selectors/selectors.ts +329 -0
- package/src/types/editor.ts +0 -60
- package/src/utils/is-hotkey.test.ts +2 -0
- package/src/utils/operationToPatches.ts +5 -0
- package/src/utils/paths.ts +4 -11
- package/src/utils/ranges.ts +3 -3
- package/lib/index.esm.js.map +0 -1
- package/lib/index.mjs +0 -7541
- package/lib/index.mjs.map +0 -1
- package/src/editor/behavior/behavior.utils.ts +0 -218
- package/src/editor/behavior/behavior.utilts.get-text-before.ts +0 -31
- package/src/editor/plugins/createWithPortableTextLists.ts +0 -172
- /package/src/editor/{behavior/behavior.utils.get-start-point.ts → utils/utils.get-start-point.ts} +0 -0
- /package/src/editor/{behavior/behavior.utils.is-keyed-segment.ts → utils/utils.is-keyed-segment.ts} +0 -0
package/README.md
CHANGED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var types = require("@sanity/types");
|
|
3
|
+
function createGuards({
|
|
4
|
+
schema
|
|
5
|
+
}) {
|
|
6
|
+
function isListBlock(block) {
|
|
7
|
+
return types.isPortableTextListBlock(block) && block._type === schema.block.name;
|
|
8
|
+
}
|
|
9
|
+
function isTextBlock(block) {
|
|
10
|
+
return types.isPortableTextTextBlock(block) && block._type === schema.block.name;
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
isListBlock,
|
|
14
|
+
isTextBlock
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const selectionIsCollapsed = ({
|
|
18
|
+
context
|
|
19
|
+
}) => JSON.stringify(context.selection?.anchor.path) === JSON.stringify(context.selection?.focus.path) && context.selection?.anchor.offset === context.selection?.focus.offset, getFocusBlock = ({
|
|
20
|
+
context
|
|
21
|
+
}) => {
|
|
22
|
+
const key = context.selection && types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
|
|
23
|
+
return node && key ? {
|
|
24
|
+
node,
|
|
25
|
+
path: [{
|
|
26
|
+
_key: key
|
|
27
|
+
}]
|
|
28
|
+
} : void 0;
|
|
29
|
+
}, getFocusListBlock = ({
|
|
30
|
+
context
|
|
31
|
+
}) => {
|
|
32
|
+
const guards = createGuards(context), focusBlock = getFocusBlock({
|
|
33
|
+
context
|
|
34
|
+
});
|
|
35
|
+
return focusBlock && guards.isListBlock(focusBlock.node) ? {
|
|
36
|
+
node: focusBlock.node,
|
|
37
|
+
path: focusBlock.path
|
|
38
|
+
} : void 0;
|
|
39
|
+
}, getFocusTextBlock = ({
|
|
40
|
+
context
|
|
41
|
+
}) => {
|
|
42
|
+
const focusBlock = getFocusBlock({
|
|
43
|
+
context
|
|
44
|
+
});
|
|
45
|
+
return focusBlock && types.isPortableTextTextBlock(focusBlock.node) ? {
|
|
46
|
+
node: focusBlock.node,
|
|
47
|
+
path: focusBlock.path
|
|
48
|
+
} : void 0;
|
|
49
|
+
}, getFocusBlockObject = ({
|
|
50
|
+
context
|
|
51
|
+
}) => {
|
|
52
|
+
const focusBlock = getFocusBlock({
|
|
53
|
+
context
|
|
54
|
+
});
|
|
55
|
+
return focusBlock && !types.isPortableTextTextBlock(focusBlock.node) ? {
|
|
56
|
+
node: focusBlock.node,
|
|
57
|
+
path: focusBlock.path
|
|
58
|
+
} : void 0;
|
|
59
|
+
}, getFocusChild = ({
|
|
60
|
+
context
|
|
61
|
+
}) => {
|
|
62
|
+
const focusBlock = getFocusTextBlock({
|
|
63
|
+
context
|
|
64
|
+
});
|
|
65
|
+
if (!focusBlock)
|
|
66
|
+
return;
|
|
67
|
+
const key = context.selection && types.isKeySegment(context.selection.focus.path[2]) ? context.selection.focus.path[2]._key : void 0, node = key ? focusBlock.node.children.find((span) => span._key === key) : void 0;
|
|
68
|
+
return node && key ? {
|
|
69
|
+
node,
|
|
70
|
+
path: [...focusBlock.path, "children", {
|
|
71
|
+
_key: key
|
|
72
|
+
}]
|
|
73
|
+
} : void 0;
|
|
74
|
+
}, getFocusSpan = ({
|
|
75
|
+
context
|
|
76
|
+
}) => {
|
|
77
|
+
const focusChild = getFocusChild({
|
|
78
|
+
context
|
|
79
|
+
});
|
|
80
|
+
return focusChild && types.isPortableTextSpan(focusChild.node) ? {
|
|
81
|
+
node: focusChild.node,
|
|
82
|
+
path: focusChild.path
|
|
83
|
+
} : void 0;
|
|
84
|
+
}, getFirstBlock = ({
|
|
85
|
+
context
|
|
86
|
+
}) => {
|
|
87
|
+
const node = context.value[0];
|
|
88
|
+
return node ? {
|
|
89
|
+
node,
|
|
90
|
+
path: [{
|
|
91
|
+
_key: node._key
|
|
92
|
+
}]
|
|
93
|
+
} : void 0;
|
|
94
|
+
}, getLastBlock = ({
|
|
95
|
+
context
|
|
96
|
+
}) => {
|
|
97
|
+
const node = context.value[context.value.length - 1] ? context.value[context.value.length - 1] : void 0;
|
|
98
|
+
return node ? {
|
|
99
|
+
node,
|
|
100
|
+
path: [{
|
|
101
|
+
_key: node._key
|
|
102
|
+
}]
|
|
103
|
+
} : void 0;
|
|
104
|
+
}, getSelectedBlocks = ({
|
|
105
|
+
context
|
|
106
|
+
}) => {
|
|
107
|
+
if (!context.selection)
|
|
108
|
+
return [];
|
|
109
|
+
const selectedBlocks = [], startKey = context.selection.backward ? types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, endKey = context.selection.backward ? types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0;
|
|
110
|
+
if (!startKey || !endKey)
|
|
111
|
+
return selectedBlocks;
|
|
112
|
+
for (const block of context.value) {
|
|
113
|
+
if (block._key === startKey) {
|
|
114
|
+
if (selectedBlocks.push({
|
|
115
|
+
node: block,
|
|
116
|
+
path: [{
|
|
117
|
+
_key: block._key
|
|
118
|
+
}]
|
|
119
|
+
}), startKey === endKey)
|
|
120
|
+
break;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (block._key === endKey) {
|
|
124
|
+
selectedBlocks.push({
|
|
125
|
+
node: block,
|
|
126
|
+
path: [{
|
|
127
|
+
_key: block._key
|
|
128
|
+
}]
|
|
129
|
+
});
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
selectedBlocks.length > 0 && selectedBlocks.push({
|
|
133
|
+
node: block,
|
|
134
|
+
path: [{
|
|
135
|
+
_key: block._key
|
|
136
|
+
}]
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return selectedBlocks;
|
|
140
|
+
}, getSelectionStartBlock = ({
|
|
141
|
+
context
|
|
142
|
+
}) => {
|
|
143
|
+
if (!context.selection)
|
|
144
|
+
return;
|
|
145
|
+
const key = context.selection.backward ? types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
|
|
146
|
+
return node && key ? {
|
|
147
|
+
node,
|
|
148
|
+
path: [{
|
|
149
|
+
_key: key
|
|
150
|
+
}]
|
|
151
|
+
} : void 0;
|
|
152
|
+
}, getSelectionEndBlock = ({
|
|
153
|
+
context
|
|
154
|
+
}) => {
|
|
155
|
+
if (!context.selection)
|
|
156
|
+
return;
|
|
157
|
+
const key = context.selection.backward ? types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
|
|
158
|
+
return node && key ? {
|
|
159
|
+
node,
|
|
160
|
+
path: [{
|
|
161
|
+
_key: key
|
|
162
|
+
}]
|
|
163
|
+
} : void 0;
|
|
164
|
+
}, getPreviousBlock = ({
|
|
165
|
+
context
|
|
166
|
+
}) => {
|
|
167
|
+
let previousBlock;
|
|
168
|
+
const selectionStartBlock = getSelectionStartBlock({
|
|
169
|
+
context
|
|
170
|
+
});
|
|
171
|
+
if (!selectionStartBlock)
|
|
172
|
+
return;
|
|
173
|
+
let foundSelectionStartBlock = !1;
|
|
174
|
+
for (const block of context.value) {
|
|
175
|
+
if (block._key === selectionStartBlock.node._key) {
|
|
176
|
+
foundSelectionStartBlock = !0;
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
previousBlock = {
|
|
180
|
+
node: block,
|
|
181
|
+
path: [{
|
|
182
|
+
_key: block._key
|
|
183
|
+
}]
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (foundSelectionStartBlock && previousBlock)
|
|
187
|
+
return previousBlock;
|
|
188
|
+
}, getNextBlock = ({
|
|
189
|
+
context
|
|
190
|
+
}) => {
|
|
191
|
+
let nextBlock;
|
|
192
|
+
const selectionEndBlock = getSelectionEndBlock({
|
|
193
|
+
context
|
|
194
|
+
});
|
|
195
|
+
if (!selectionEndBlock)
|
|
196
|
+
return;
|
|
197
|
+
let foundSelectionEndBlock = !1;
|
|
198
|
+
for (const block of context.value) {
|
|
199
|
+
if (block._key === selectionEndBlock.node._key) {
|
|
200
|
+
foundSelectionEndBlock = !0;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (foundSelectionEndBlock) {
|
|
204
|
+
nextBlock = {
|
|
205
|
+
node: block,
|
|
206
|
+
path: [{
|
|
207
|
+
_key: block._key
|
|
208
|
+
}]
|
|
209
|
+
};
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (foundSelectionEndBlock && nextBlock)
|
|
214
|
+
return nextBlock;
|
|
215
|
+
};
|
|
216
|
+
function getStartPoint({
|
|
217
|
+
node,
|
|
218
|
+
path
|
|
219
|
+
}) {
|
|
220
|
+
return types.isPortableTextTextBlock(node) ? {
|
|
221
|
+
path: [...path, "children", {
|
|
222
|
+
_key: node.children[0]._key
|
|
223
|
+
}],
|
|
224
|
+
offset: 0
|
|
225
|
+
} : {
|
|
226
|
+
path,
|
|
227
|
+
offset: 0
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function isKeyedSegment(segment) {
|
|
231
|
+
return typeof segment == "object" && segment !== null && "_key" in segment;
|
|
232
|
+
}
|
|
233
|
+
function reverseSelection(selection) {
|
|
234
|
+
return selection.backward ? {
|
|
235
|
+
anchor: selection.focus,
|
|
236
|
+
focus: selection.anchor,
|
|
237
|
+
backward: !1
|
|
238
|
+
} : {
|
|
239
|
+
anchor: selection.focus,
|
|
240
|
+
focus: selection.anchor,
|
|
241
|
+
backward: !0
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
const getSelectionText = ({
|
|
245
|
+
context
|
|
246
|
+
}) => {
|
|
247
|
+
let text = "";
|
|
248
|
+
const {
|
|
249
|
+
value,
|
|
250
|
+
selection
|
|
251
|
+
} = context;
|
|
252
|
+
if (!value || !selection)
|
|
253
|
+
return text;
|
|
254
|
+
const forwardSelection = selection.backward ? reverseSelection(selection) : selection;
|
|
255
|
+
if (!forwardSelection)
|
|
256
|
+
return text;
|
|
257
|
+
for (const block of value)
|
|
258
|
+
if (!(isKeyedSegment(forwardSelection.anchor.path[0]) && block._key !== forwardSelection.anchor.path[0]._key) && types.isPortableTextTextBlock(block)) {
|
|
259
|
+
for (const child of block.children)
|
|
260
|
+
if (types.isPortableTextSpan(child)) {
|
|
261
|
+
if (isKeyedSegment(forwardSelection.anchor.path[2]) && child._key === forwardSelection.anchor.path[2]._key && isKeyedSegment(forwardSelection.focus.path[2]) && child._key === forwardSelection.focus.path[2]._key) {
|
|
262
|
+
text = text + child.text.slice(forwardSelection.anchor.offset, forwardSelection.focus.offset);
|
|
263
|
+
break;
|
|
264
|
+
}
|
|
265
|
+
if (isKeyedSegment(forwardSelection.anchor.path[2]) && child._key === forwardSelection.anchor.path[2]._key) {
|
|
266
|
+
text = text + child.text.slice(forwardSelection.anchor.offset);
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (isKeyedSegment(forwardSelection.focus.path[2]) && child._key === forwardSelection.focus.path[2]._key) {
|
|
270
|
+
text = text + child.text.slice(0, forwardSelection.focus.offset);
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (isKeyedSegment(forwardSelection.focus.path[0]) && block._key === forwardSelection.focus.path[0]._key)
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
return text;
|
|
278
|
+
}, getBlockTextBefore = ({
|
|
279
|
+
context
|
|
280
|
+
}) => {
|
|
281
|
+
if (!context.selection)
|
|
282
|
+
return "";
|
|
283
|
+
const point = (context.selection.backward ? reverseSelection(context.selection) : context.selection).anchor, key = isKeyedSegment(point.path[0]) ? point.path[0]._key : void 0, block = key ? context.value.find((block2) => block2._key === key) : void 0;
|
|
284
|
+
if (!block)
|
|
285
|
+
return "";
|
|
286
|
+
const startOfBlock = getStartPoint({
|
|
287
|
+
node: block,
|
|
288
|
+
path: [{
|
|
289
|
+
_key: block._key
|
|
290
|
+
}]
|
|
291
|
+
});
|
|
292
|
+
return getSelectionText({
|
|
293
|
+
context: {
|
|
294
|
+
...context,
|
|
295
|
+
value: context.value,
|
|
296
|
+
selection: {
|
|
297
|
+
anchor: startOfBlock,
|
|
298
|
+
focus: point
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
};
|
|
303
|
+
exports.createGuards = createGuards;
|
|
304
|
+
exports.getBlockTextBefore = getBlockTextBefore;
|
|
305
|
+
exports.getFirstBlock = getFirstBlock;
|
|
306
|
+
exports.getFocusBlock = getFocusBlock;
|
|
307
|
+
exports.getFocusBlockObject = getFocusBlockObject;
|
|
308
|
+
exports.getFocusChild = getFocusChild;
|
|
309
|
+
exports.getFocusListBlock = getFocusListBlock;
|
|
310
|
+
exports.getFocusSpan = getFocusSpan;
|
|
311
|
+
exports.getFocusTextBlock = getFocusTextBlock;
|
|
312
|
+
exports.getLastBlock = getLastBlock;
|
|
313
|
+
exports.getNextBlock = getNextBlock;
|
|
314
|
+
exports.getPreviousBlock = getPreviousBlock;
|
|
315
|
+
exports.getSelectedBlocks = getSelectedBlocks;
|
|
316
|
+
exports.getSelectionEndBlock = getSelectionEndBlock;
|
|
317
|
+
exports.getSelectionStartBlock = getSelectionStartBlock;
|
|
318
|
+
exports.getSelectionText = getSelectionText;
|
|
319
|
+
exports.selectionIsCollapsed = selectionIsCollapsed;
|
|
320
|
+
//# sourceMappingURL=selector.get-text-before.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selector.get-text-before.cjs","sources":["../../src/editor/behavior/behavior.guards.ts","../../src/selectors/selectors.ts","../../src/editor/utils/utils.get-start-point.ts","../../src/editor/utils/utils.is-keyed-segment.ts","../../src/editor/utils/utils.reverse-selection.ts","../../src/selectors/selector.get-selection-text.ts","../../src/selectors/selector.get-text-before.ts"],"sourcesContent":["import {\n isPortableTextListBlock,\n isPortableTextTextBlock,\n type PortableTextListBlock,\n type PortableTextTextBlock,\n} from '@sanity/types'\nimport type {PortableTextMemberSchemaTypes} from '../../types/editor'\n\n/**\n * @alpha\n */\nexport type BehaviorGuards = ReturnType<typeof createGuards>\n\nexport function createGuards({\n schema,\n}: {\n schema: PortableTextMemberSchemaTypes\n}) {\n function isListBlock(block: unknown): block is PortableTextListBlock {\n return isPortableTextListBlock(block) && block._type === schema.block.name\n }\n\n function isTextBlock(block: unknown): block is PortableTextTextBlock {\n return isPortableTextTextBlock(block) && block._type === schema.block.name\n }\n\n return {isListBlock, isTextBlock}\n}\n","import {\n isKeySegment,\n isPortableTextSpan,\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n type PortableTextListBlock,\n type PortableTextObject,\n type PortableTextSpan,\n type PortableTextTextBlock,\n} from '@sanity/types'\nimport {createGuards} from '../editor/behavior/behavior.guards'\nimport type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @alpha\n */\nexport const selectionIsCollapsed: EditorSelector<boolean> = ({context}) => {\n return (\n JSON.stringify(context.selection?.anchor.path) ===\n JSON.stringify(context.selection?.focus.path) &&\n context.selection?.anchor.offset === context.selection?.focus.offset\n )\n}\n\n/**\n * @alpha\n */\nexport const getFocusBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const key = context.selection\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusListBlock: EditorSelector<\n {node: PortableTextListBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const guards = createGuards(context)\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && guards.isListBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusTextBlock: EditorSelector<\n {node: PortableTextTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && isPortableTextTextBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusBlockObject: EditorSelector<\n {node: PortableTextObject; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const focusBlock = getFocusBlock({context})\n\n return focusBlock && !isPortableTextTextBlock(focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusChild: EditorSelector<\n | {\n node: PortableTextObject | PortableTextSpan\n path: [KeyedSegment, 'children', KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n const focusBlock = getFocusTextBlock({context})\n\n if (!focusBlock) {\n return undefined\n }\n\n const key = context.selection\n ? isKeySegment(context.selection.focus.path[2])\n ? context.selection.focus.path[2]._key\n : undefined\n : undefined\n\n const node = key\n ? focusBlock.node.children.find((span) => span._key === key)\n : undefined\n\n return node && key\n ? {node, path: [...focusBlock.path, 'children', {_key: key}]}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFocusSpan: EditorSelector<\n | {node: PortableTextSpan; path: [KeyedSegment, 'children', KeyedSegment]}\n | undefined\n> = ({context}) => {\n const focusChild = getFocusChild({context})\n\n return focusChild && isPortableTextSpan(focusChild.node)\n ? {node: focusChild.node, path: focusChild.path}\n : undefined\n}\n\n/**\n * @alpha\n */\nexport const getFirstBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const node = context.value[0]\n\n return node ? {node, path: [{_key: node._key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getLastBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n const node = context.value[context.value.length - 1]\n ? context.value[context.value.length - 1]\n : undefined\n\n return node ? {node, path: [{_key: node._key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getSelectedBlocks: EditorSelector<\n Array<{node: PortableTextBlock; path: [KeyedSegment]}>\n> = ({context}) => {\n if (!context.selection) {\n return []\n }\n\n const selectedBlocks: Array<{node: PortableTextBlock; path: [KeyedSegment]}> =\n []\n const startKey = context.selection.backward\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n const endKey = context.selection.backward\n ? isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n : isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n\n if (!startKey || !endKey) {\n return selectedBlocks\n }\n\n for (const block of context.value) {\n if (block._key === startKey) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n\n if (startKey === endKey) {\n break\n }\n continue\n }\n\n if (block._key === endKey) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n break\n }\n\n if (selectedBlocks.length > 0) {\n selectedBlocks.push({node: block, path: [{_key: block._key}]})\n }\n }\n\n return selectedBlocks\n}\n\n/**\n * @alpha\n */\nexport const getSelectionStartBlock: EditorSelector<\n | {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n if (!context.selection) {\n return undefined\n }\n\n const key = context.selection.backward\n ? isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n : isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getSelectionEndBlock: EditorSelector<\n | {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n | undefined\n> = ({context}) => {\n if (!context.selection) {\n return undefined\n }\n\n const key = context.selection.backward\n ? isKeySegment(context.selection.anchor.path[0])\n ? context.selection.anchor.path[0]._key\n : undefined\n : isKeySegment(context.selection.focus.path[0])\n ? context.selection.focus.path[0]._key\n : undefined\n\n const node = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @alpha\n */\nexport const getPreviousBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n let previousBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n const selectionStartBlock = getSelectionStartBlock({context})\n\n if (!selectionStartBlock) {\n return undefined\n }\n\n let foundSelectionStartBlock = false\n\n for (const block of context.value) {\n if (block._key === selectionStartBlock.node._key) {\n foundSelectionStartBlock = true\n break\n }\n\n previousBlock = {node: block, path: [{_key: block._key}]}\n }\n\n if (foundSelectionStartBlock && previousBlock) {\n return previousBlock\n }\n\n return undefined\n}\n\n/**\n * @alpha\n */\nexport const getNextBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = ({context}) => {\n let nextBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n const selectionEndBlock = getSelectionEndBlock({context})\n\n if (!selectionEndBlock) {\n return undefined\n }\n\n let foundSelectionEndBlock = false\n\n for (const block of context.value) {\n if (block._key === selectionEndBlock.node._key) {\n foundSelectionEndBlock = true\n continue\n }\n\n if (foundSelectionEndBlock) {\n nextBlock = {node: block, path: [{_key: block._key}]}\n break\n }\n }\n\n if (foundSelectionEndBlock && nextBlock) {\n return nextBlock\n }\n\n return undefined\n}\n","import {\n isPortableTextTextBlock,\n type KeyedSegment,\n type PortableTextBlock,\n} from '@sanity/types'\nimport type {EditorSelectionPoint} from '../../types/editor'\n\nexport function getStartPoint({\n node,\n path,\n}: {\n node: PortableTextBlock\n path: [KeyedSegment]\n}): EditorSelectionPoint {\n if (isPortableTextTextBlock(node)) {\n return {\n path: [...path, 'children', {_key: node.children[0]._key}],\n offset: 0,\n }\n }\n\n return {\n path,\n offset: 0,\n }\n}\n","import type {KeyedSegment, PathSegment} from '@sanity/types'\n\nexport function isKeyedSegment(segment: PathSegment): segment is KeyedSegment {\n return typeof segment === 'object' && segment !== null && '_key' in segment\n}\n","import type {EditorSelection} from '../../types/editor'\n\nexport function reverseSelection(\n selection: NonNullable<EditorSelection>,\n): NonNullable<EditorSelection> {\n if (selection.backward) {\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: false,\n }\n }\n\n return {\n anchor: selection.focus,\n focus: selection.anchor,\n backward: true,\n }\n}\n","import {isPortableTextSpan, isPortableTextTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {isKeyedSegment} from '../editor/utils/utils.is-keyed-segment'\nimport {reverseSelection} from '../editor/utils/utils.reverse-selection'\n\n/**\n * @alpha\n */\nexport const getSelectionText: EditorSelector<string> = ({context}) => {\n let text = ''\n\n const {value, selection} = context\n\n if (!value || !selection) {\n return text\n }\n\n const forwardSelection = selection.backward\n ? reverseSelection(selection)\n : selection\n\n if (!forwardSelection) {\n return text\n }\n\n for (const block of value) {\n if (\n isKeyedSegment(forwardSelection.anchor.path[0]) &&\n block._key !== forwardSelection.anchor.path[0]._key\n ) {\n continue\n }\n\n if (!isPortableTextTextBlock(block)) {\n continue\n }\n\n for (const child of block.children) {\n if (isPortableTextSpan(child)) {\n if (\n isKeyedSegment(forwardSelection.anchor.path[2]) &&\n child._key === forwardSelection.anchor.path[2]._key &&\n isKeyedSegment(forwardSelection.focus.path[2]) &&\n child._key === forwardSelection.focus.path[2]._key\n ) {\n text =\n text +\n child.text.slice(\n forwardSelection.anchor.offset,\n forwardSelection.focus.offset,\n )\n\n break\n }\n\n if (\n isKeyedSegment(forwardSelection.anchor.path[2]) &&\n child._key === forwardSelection.anchor.path[2]._key\n ) {\n text = text + child.text.slice(forwardSelection.anchor.offset)\n continue\n }\n\n if (\n isKeyedSegment(forwardSelection.focus.path[2]) &&\n child._key === forwardSelection.focus.path[2]._key\n ) {\n text = text + child.text.slice(0, forwardSelection.focus.offset)\n break\n }\n\n if (text.length > 0) {\n text + child.text\n }\n }\n }\n\n if (\n isKeyedSegment(forwardSelection.focus.path[0]) &&\n block._key === forwardSelection.focus.path[0]._key\n ) {\n break\n }\n }\n\n return text\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {getStartPoint} from '../editor/utils/utils.get-start-point'\nimport {isKeyedSegment} from '../editor/utils/utils.is-keyed-segment'\nimport {reverseSelection} from '../editor/utils/utils.reverse-selection'\nimport {getSelectionText} from './selector.get-selection-text'\n\n/**\n * @alpha\n */\nexport const getBlockTextBefore: EditorSelector<string> = ({context}) => {\n if (!context.selection) {\n return ''\n }\n\n const selection = context.selection.backward\n ? reverseSelection(context.selection)\n : context.selection\n const point = selection.anchor\n const key = isKeyedSegment(point.path[0]) ? point.path[0]._key : undefined\n\n const block = key\n ? context.value.find((block) => block._key === key)\n : undefined\n\n if (!block) {\n return ''\n }\n\n const startOfBlock = getStartPoint({node: block, path: [{_key: block._key}]})\n\n return getSelectionText({\n context: {\n ...context,\n value: context.value,\n selection: {\n anchor: startOfBlock,\n focus: point,\n },\n },\n })\n}\n"],"names":["createGuards","schema","isListBlock","block","isPortableTextListBlock","_type","name","isTextBlock","isPortableTextTextBlock","selectionIsCollapsed","context","JSON","stringify","selection","anchor","path","focus","offset","getFocusBlock","key","isKeySegment","_key","undefined","node","value","find","getFocusListBlock","guards","focusBlock","getFocusTextBlock","getFocusBlockObject","getFocusChild","children","span","getFocusSpan","focusChild","isPortableTextSpan","getFirstBlock","getLastBlock","length","getSelectedBlocks","selectedBlocks","startKey","backward","endKey","push","getSelectionStartBlock","getSelectionEndBlock","getPreviousBlock","previousBlock","selectionStartBlock","foundSelectionStartBlock","getNextBlock","nextBlock","selectionEndBlock","foundSelectionEndBlock","getStartPoint","isKeyedSegment","segment","reverseSelection","getSelectionText","text","forwardSelection","child","slice","getBlockTextBefore","point","startOfBlock"],"mappings":";;AAaO,SAASA,aAAa;AAAA,EAC3BC;AAGF,GAAG;AACD,WAASC,YAAYC,OAAgD;AACnE,WAAOC,MAAAA,wBAAwBD,KAAK,KAAKA,MAAME,UAAUJ,OAAOE,MAAMG;AAAAA,EAAAA;AAGxE,WAASC,YAAYJ,OAAgD;AACnE,WAAOK,MAAAA,wBAAwBL,KAAK,KAAKA,MAAME,UAAUJ,OAAOE,MAAMG;AAAAA,EAAAA;AAGjE,SAAA;AAAA,IAACJ;AAAAA,IAAaK;AAAAA,EAAW;AAClC;ACVO,MAAME,uBAAgDA,CAAC;AAAA,EAACC;AAAO,MAElEC,KAAKC,UAAUF,QAAQG,WAAWC,OAAOC,IAAI,MAC3CJ,KAAKC,UAAUF,QAAQG,WAAWG,MAAMD,IAAI,KAC9CL,QAAQG,WAAWC,OAAOG,WAAWP,QAAQG,WAAWG,MAAMC,QAOrDC,gBAETA,CAAC;AAAA,EAACR;AAAO,MAAM;AACjB,QAAMS,MAAMT,QAAQG,aAChBO,MAAAA,aAAaV,QAAQG,UAAUG,MAAMD,KAAK,CAAC,CAAC,IAC1CL,QAAQG,UAAUG,MAAMD,KAAK,CAAC,EAAEM,OAElCC,QAEEC,OAAOJ,MACTT,QAAQc,MAAMC,KAAMtB,CAAUA,UAAAA,MAAMkB,SAASF,GAAG,IAChDG;AAEJ,SAAOC,QAAQJ,MAAM;AAAA,IAACI;AAAAA,IAAMR,MAAM,CAAC;AAAA,MAACM,MAAMF;AAAAA,IAAI,CAAA;AAAA,EAAKG,IAAAA;AACrD,GAKaI,oBAETA,CAAC;AAAA,EAAChB;AAAO,MAAM;AACjB,QAAMiB,SAAS3B,aAAaU,OAAO,GAC7BkB,aAAaV,cAAc;AAAA,IAACR;AAAAA,EAAAA,CA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9C,MAAMD,KAAK,CAAC,CAAC,KAC7CZ,MAAMkB,SAASyC,iBAAiB9C,MAAMD,KAAK,CAAC,EAAEM;AAE9C;AAAA,IAAA;AAIGwC,SAAAA;AACT,GC7EaI,qBAA6CA,CAAC;AAAA,EAACvD;AAAO,MAAM;AACvE,MAAI,CAACA,QAAQG;AACJ,WAAA;AAMT,QAAMqD,SAHYxD,QAAQG,UAAU8B,WAChCgB,iBAAiBjD,QAAQG,SAAS,IAClCH,QAAQG,WACYC,QAClBK,MAAMsC,eAAeS,MAAMnD,KAAK,CAAC,CAAC,IAAImD,MAAMnD,KAAK,CAAC,EAAEM,OAAOC,QAE3DnB,QAAQgB,MACVT,QAAQc,MAAMC,KAAMtB,CAAAA,WAAUA,OAAMkB,SAASF,GAAG,IAChDG;AAEJ,MAAI,CAACnB;AACI,WAAA;AAGT,QAAMgE,eAAeX,cAAc;AAAA,IAACjC,MAAMpB;AAAAA,IAAOY,MAAM,CAAC;AAAA,MAACM,MAAMlB,MAAMkB;AAAAA,IAAK,CAAA;AAAA,EAAA,CAAE;AAE5E,SAAOuC,iBAAiB;AAAA,IACtBlD,SAAS;AAAA,MACP,GAAGA;AAAAA,MACHc,OAAOd,QAAQc;AAAAA,MACfX,WAAW;AAAA,QACTC,QAAQqD;AAAAA,QACRnD,OAAOkD;AAAAA,MAAAA;AAAAA,IACT;AAAA,EACF,CACD;AACH;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { isPortableTextListBlock, isPortableTextTextBlock, isKeySegment, isPortableTextSpan } from "@sanity/types";
|
|
2
|
+
function createGuards({
|
|
3
|
+
schema
|
|
4
|
+
}) {
|
|
5
|
+
function isListBlock(block) {
|
|
6
|
+
return isPortableTextListBlock(block) && block._type === schema.block.name;
|
|
7
|
+
}
|
|
8
|
+
function isTextBlock(block) {
|
|
9
|
+
return isPortableTextTextBlock(block) && block._type === schema.block.name;
|
|
10
|
+
}
|
|
11
|
+
return {
|
|
12
|
+
isListBlock,
|
|
13
|
+
isTextBlock
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
const selectionIsCollapsed = ({
|
|
17
|
+
context
|
|
18
|
+
}) => JSON.stringify(context.selection?.anchor.path) === JSON.stringify(context.selection?.focus.path) && context.selection?.anchor.offset === context.selection?.focus.offset, getFocusBlock = ({
|
|
19
|
+
context
|
|
20
|
+
}) => {
|
|
21
|
+
const key = context.selection && isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
|
|
22
|
+
return node && key ? {
|
|
23
|
+
node,
|
|
24
|
+
path: [{
|
|
25
|
+
_key: key
|
|
26
|
+
}]
|
|
27
|
+
} : void 0;
|
|
28
|
+
}, getFocusListBlock = ({
|
|
29
|
+
context
|
|
30
|
+
}) => {
|
|
31
|
+
const guards = createGuards(context), focusBlock = getFocusBlock({
|
|
32
|
+
context
|
|
33
|
+
});
|
|
34
|
+
return focusBlock && guards.isListBlock(focusBlock.node) ? {
|
|
35
|
+
node: focusBlock.node,
|
|
36
|
+
path: focusBlock.path
|
|
37
|
+
} : void 0;
|
|
38
|
+
}, getFocusTextBlock = ({
|
|
39
|
+
context
|
|
40
|
+
}) => {
|
|
41
|
+
const focusBlock = getFocusBlock({
|
|
42
|
+
context
|
|
43
|
+
});
|
|
44
|
+
return focusBlock && isPortableTextTextBlock(focusBlock.node) ? {
|
|
45
|
+
node: focusBlock.node,
|
|
46
|
+
path: focusBlock.path
|
|
47
|
+
} : void 0;
|
|
48
|
+
}, getFocusBlockObject = ({
|
|
49
|
+
context
|
|
50
|
+
}) => {
|
|
51
|
+
const focusBlock = getFocusBlock({
|
|
52
|
+
context
|
|
53
|
+
});
|
|
54
|
+
return focusBlock && !isPortableTextTextBlock(focusBlock.node) ? {
|
|
55
|
+
node: focusBlock.node,
|
|
56
|
+
path: focusBlock.path
|
|
57
|
+
} : void 0;
|
|
58
|
+
}, getFocusChild = ({
|
|
59
|
+
context
|
|
60
|
+
}) => {
|
|
61
|
+
const focusBlock = getFocusTextBlock({
|
|
62
|
+
context
|
|
63
|
+
});
|
|
64
|
+
if (!focusBlock)
|
|
65
|
+
return;
|
|
66
|
+
const key = context.selection && isKeySegment(context.selection.focus.path[2]) ? context.selection.focus.path[2]._key : void 0, node = key ? focusBlock.node.children.find((span) => span._key === key) : void 0;
|
|
67
|
+
return node && key ? {
|
|
68
|
+
node,
|
|
69
|
+
path: [...focusBlock.path, "children", {
|
|
70
|
+
_key: key
|
|
71
|
+
}]
|
|
72
|
+
} : void 0;
|
|
73
|
+
}, getFocusSpan = ({
|
|
74
|
+
context
|
|
75
|
+
}) => {
|
|
76
|
+
const focusChild = getFocusChild({
|
|
77
|
+
context
|
|
78
|
+
});
|
|
79
|
+
return focusChild && isPortableTextSpan(focusChild.node) ? {
|
|
80
|
+
node: focusChild.node,
|
|
81
|
+
path: focusChild.path
|
|
82
|
+
} : void 0;
|
|
83
|
+
}, getFirstBlock = ({
|
|
84
|
+
context
|
|
85
|
+
}) => {
|
|
86
|
+
const node = context.value[0];
|
|
87
|
+
return node ? {
|
|
88
|
+
node,
|
|
89
|
+
path: [{
|
|
90
|
+
_key: node._key
|
|
91
|
+
}]
|
|
92
|
+
} : void 0;
|
|
93
|
+
}, getLastBlock = ({
|
|
94
|
+
context
|
|
95
|
+
}) => {
|
|
96
|
+
const node = context.value[context.value.length - 1] ? context.value[context.value.length - 1] : void 0;
|
|
97
|
+
return node ? {
|
|
98
|
+
node,
|
|
99
|
+
path: [{
|
|
100
|
+
_key: node._key
|
|
101
|
+
}]
|
|
102
|
+
} : void 0;
|
|
103
|
+
}, getSelectedBlocks = ({
|
|
104
|
+
context
|
|
105
|
+
}) => {
|
|
106
|
+
if (!context.selection)
|
|
107
|
+
return [];
|
|
108
|
+
const selectedBlocks = [], startKey = context.selection.backward ? isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, endKey = context.selection.backward ? isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0;
|
|
109
|
+
if (!startKey || !endKey)
|
|
110
|
+
return selectedBlocks;
|
|
111
|
+
for (const block of context.value) {
|
|
112
|
+
if (block._key === startKey) {
|
|
113
|
+
if (selectedBlocks.push({
|
|
114
|
+
node: block,
|
|
115
|
+
path: [{
|
|
116
|
+
_key: block._key
|
|
117
|
+
}]
|
|
118
|
+
}), startKey === endKey)
|
|
119
|
+
break;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (block._key === endKey) {
|
|
123
|
+
selectedBlocks.push({
|
|
124
|
+
node: block,
|
|
125
|
+
path: [{
|
|
126
|
+
_key: block._key
|
|
127
|
+
}]
|
|
128
|
+
});
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
selectedBlocks.length > 0 && selectedBlocks.push({
|
|
132
|
+
node: block,
|
|
133
|
+
path: [{
|
|
134
|
+
_key: block._key
|
|
135
|
+
}]
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return selectedBlocks;
|
|
139
|
+
}, getSelectionStartBlock = ({
|
|
140
|
+
context
|
|
141
|
+
}) => {
|
|
142
|
+
if (!context.selection)
|
|
143
|
+
return;
|
|
144
|
+
const key = context.selection.backward ? isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
|
|
145
|
+
return node && key ? {
|
|
146
|
+
node,
|
|
147
|
+
path: [{
|
|
148
|
+
_key: key
|
|
149
|
+
}]
|
|
150
|
+
} : void 0;
|
|
151
|
+
}, getSelectionEndBlock = ({
|
|
152
|
+
context
|
|
153
|
+
}) => {
|
|
154
|
+
if (!context.selection)
|
|
155
|
+
return;
|
|
156
|
+
const key = context.selection.backward ? isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0 : isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
|
|
157
|
+
return node && key ? {
|
|
158
|
+
node,
|
|
159
|
+
path: [{
|
|
160
|
+
_key: key
|
|
161
|
+
}]
|
|
162
|
+
} : void 0;
|
|
163
|
+
}, getPreviousBlock = ({
|
|
164
|
+
context
|
|
165
|
+
}) => {
|
|
166
|
+
let previousBlock;
|
|
167
|
+
const selectionStartBlock = getSelectionStartBlock({
|
|
168
|
+
context
|
|
169
|
+
});
|
|
170
|
+
if (!selectionStartBlock)
|
|
171
|
+
return;
|
|
172
|
+
let foundSelectionStartBlock = !1;
|
|
173
|
+
for (const block of context.value) {
|
|
174
|
+
if (block._key === selectionStartBlock.node._key) {
|
|
175
|
+
foundSelectionStartBlock = !0;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
previousBlock = {
|
|
179
|
+
node: block,
|
|
180
|
+
path: [{
|
|
181
|
+
_key: block._key
|
|
182
|
+
}]
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (foundSelectionStartBlock && previousBlock)
|
|
186
|
+
return previousBlock;
|
|
187
|
+
}, getNextBlock = ({
|
|
188
|
+
context
|
|
189
|
+
}) => {
|
|
190
|
+
let nextBlock;
|
|
191
|
+
const selectionEndBlock = getSelectionEndBlock({
|
|
192
|
+
context
|
|
193
|
+
});
|
|
194
|
+
if (!selectionEndBlock)
|
|
195
|
+
return;
|
|
196
|
+
let foundSelectionEndBlock = !1;
|
|
197
|
+
for (const block of context.value) {
|
|
198
|
+
if (block._key === selectionEndBlock.node._key) {
|
|
199
|
+
foundSelectionEndBlock = !0;
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (foundSelectionEndBlock) {
|
|
203
|
+
nextBlock = {
|
|
204
|
+
node: block,
|
|
205
|
+
path: [{
|
|
206
|
+
_key: block._key
|
|
207
|
+
}]
|
|
208
|
+
};
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (foundSelectionEndBlock && nextBlock)
|
|
213
|
+
return nextBlock;
|
|
214
|
+
};
|
|
215
|
+
function getStartPoint({
|
|
216
|
+
node,
|
|
217
|
+
path
|
|
218
|
+
}) {
|
|
219
|
+
return isPortableTextTextBlock(node) ? {
|
|
220
|
+
path: [...path, "children", {
|
|
221
|
+
_key: node.children[0]._key
|
|
222
|
+
}],
|
|
223
|
+
offset: 0
|
|
224
|
+
} : {
|
|
225
|
+
path,
|
|
226
|
+
offset: 0
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function isKeyedSegment(segment) {
|
|
230
|
+
return typeof segment == "object" && segment !== null && "_key" in segment;
|
|
231
|
+
}
|
|
232
|
+
function reverseSelection(selection) {
|
|
233
|
+
return selection.backward ? {
|
|
234
|
+
anchor: selection.focus,
|
|
235
|
+
focus: selection.anchor,
|
|
236
|
+
backward: !1
|
|
237
|
+
} : {
|
|
238
|
+
anchor: selection.focus,
|
|
239
|
+
focus: selection.anchor,
|
|
240
|
+
backward: !0
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
const getSelectionText = ({
|
|
244
|
+
context
|
|
245
|
+
}) => {
|
|
246
|
+
let text = "";
|
|
247
|
+
const {
|
|
248
|
+
value,
|
|
249
|
+
selection
|
|
250
|
+
} = context;
|
|
251
|
+
if (!value || !selection)
|
|
252
|
+
return text;
|
|
253
|
+
const forwardSelection = selection.backward ? reverseSelection(selection) : selection;
|
|
254
|
+
if (!forwardSelection)
|
|
255
|
+
return text;
|
|
256
|
+
for (const block of value)
|
|
257
|
+
if (!(isKeyedSegment(forwardSelection.anchor.path[0]) && block._key !== forwardSelection.anchor.path[0]._key) && isPortableTextTextBlock(block)) {
|
|
258
|
+
for (const child of block.children)
|
|
259
|
+
if (isPortableTextSpan(child)) {
|
|
260
|
+
if (isKeyedSegment(forwardSelection.anchor.path[2]) && child._key === forwardSelection.anchor.path[2]._key && isKeyedSegment(forwardSelection.focus.path[2]) && child._key === forwardSelection.focus.path[2]._key) {
|
|
261
|
+
text = text + child.text.slice(forwardSelection.anchor.offset, forwardSelection.focus.offset);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
if (isKeyedSegment(forwardSelection.anchor.path[2]) && child._key === forwardSelection.anchor.path[2]._key) {
|
|
265
|
+
text = text + child.text.slice(forwardSelection.anchor.offset);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (isKeyedSegment(forwardSelection.focus.path[2]) && child._key === forwardSelection.focus.path[2]._key) {
|
|
269
|
+
text = text + child.text.slice(0, forwardSelection.focus.offset);
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (isKeyedSegment(forwardSelection.focus.path[0]) && block._key === forwardSelection.focus.path[0]._key)
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
return text;
|
|
277
|
+
}, getBlockTextBefore = ({
|
|
278
|
+
context
|
|
279
|
+
}) => {
|
|
280
|
+
if (!context.selection)
|
|
281
|
+
return "";
|
|
282
|
+
const point = (context.selection.backward ? reverseSelection(context.selection) : context.selection).anchor, key = isKeyedSegment(point.path[0]) ? point.path[0]._key : void 0, block = key ? context.value.find((block2) => block2._key === key) : void 0;
|
|
283
|
+
if (!block)
|
|
284
|
+
return "";
|
|
285
|
+
const startOfBlock = getStartPoint({
|
|
286
|
+
node: block,
|
|
287
|
+
path: [{
|
|
288
|
+
_key: block._key
|
|
289
|
+
}]
|
|
290
|
+
});
|
|
291
|
+
return getSelectionText({
|
|
292
|
+
context: {
|
|
293
|
+
...context,
|
|
294
|
+
value: context.value,
|
|
295
|
+
selection: {
|
|
296
|
+
anchor: startOfBlock,
|
|
297
|
+
focus: point
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
};
|
|
302
|
+
export {
|
|
303
|
+
createGuards,
|
|
304
|
+
getBlockTextBefore,
|
|
305
|
+
getFirstBlock,
|
|
306
|
+
getFocusBlock,
|
|
307
|
+
getFocusBlockObject,
|
|
308
|
+
getFocusChild,
|
|
309
|
+
getFocusListBlock,
|
|
310
|
+
getFocusSpan,
|
|
311
|
+
getFocusTextBlock,
|
|
312
|
+
getLastBlock,
|
|
313
|
+
getNextBlock,
|
|
314
|
+
getPreviousBlock,
|
|
315
|
+
getSelectedBlocks,
|
|
316
|
+
getSelectionEndBlock,
|
|
317
|
+
getSelectionStartBlock,
|
|
318
|
+
getSelectionText,
|
|
319
|
+
selectionIsCollapsed
|
|
320
|
+
};
|
|
321
|
+
//# sourceMappingURL=selector.get-text-before.js.map
|