@portabletext/editor 1.49.2 → 1.49.3
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-cjs/selector.get-text-before.cjs +2 -2
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs +40 -230
- package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-selection-expanded.cjs +201 -0
- package/lib/_chunks-cjs/selector.is-selection-expanded.cjs.map +1 -0
- package/lib/_chunks-cjs/use-editor.cjs +28 -0
- package/lib/_chunks-cjs/use-editor.cjs.map +1 -0
- package/lib/_chunks-cjs/util.child-selection-point-to-block-offset.cjs +79 -0
- package/lib/_chunks-cjs/util.child-selection-point-to-block-offset.cjs.map +1 -0
- package/lib/_chunks-cjs/util.is-equal-selection-points.cjs +34 -0
- package/lib/_chunks-cjs/util.is-equal-selection-points.cjs.map +1 -0
- package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +6 -78
- package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +1 -1
- package/lib/_chunks-cjs/util.slice-blocks.cjs +0 -31
- package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
- package/lib/_chunks-es/selector.get-text-before.js +1 -1
- package/lib/_chunks-es/selector.is-selecting-entire-blocks.js +13 -201
- package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -1
- package/lib/_chunks-es/selector.is-selection-expanded.js +203 -0
- package/lib/_chunks-es/selector.is-selection-expanded.js.map +1 -0
- package/lib/_chunks-es/use-editor.js +25 -0
- package/lib/_chunks-es/use-editor.js.map +1 -0
- package/lib/_chunks-es/util.child-selection-point-to-block-offset.js +80 -0
- package/lib/_chunks-es/util.child-selection-point-to-block-offset.js.map +1 -0
- package/lib/_chunks-es/util.is-equal-selection-points.js +35 -0
- package/lib/_chunks-es/util.is-equal-selection-points.js.map +1 -0
- package/lib/_chunks-es/util.selection-point-to-block-offset.js +6 -77
- package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +1 -1
- package/lib/_chunks-es/util.slice-blocks.js +0 -31
- package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
- package/lib/index.cjs +8998 -290
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +2 -2
- package/lib/index.d.ts +2 -2
- package/lib/index.js +8945 -222
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs +23 -23
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.js +3 -3
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.cjs +22 -22
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.js +4 -3
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.cjs +10 -10
- package/lib/utils/index.cjs.map +1 -1
- package/lib/utils/index.js +6 -4
- package/lib/utils/index.js.map +1 -1
- package/package.json +13 -13
- package/src/editor/create-editor.ts +2 -58
- package/src/editor/editor-context.tsx +1 -1
- package/src/editor/editor-provider.tsx +4 -31
- package/src/editor/editor-selector.ts +2 -1
- package/src/editor/use-editor.ts +27 -0
- package/src/editor-event-listener.tsx +1 -1
- package/src/editor.ts +57 -0
- package/src/index.ts +9 -9
- package/src/internal-utils/text-selection.test.ts +11 -0
- package/src/plugins/plugin.behavior.tsx +1 -1
- package/src/plugins/plugin.decorator-shortcut.ts +2 -2
- package/src/plugins/plugin.editor-ref.tsx +2 -2
- package/src/plugins/plugin.event-listener.tsx +1 -1
- package/src/plugins/plugin.markdown.tsx +1 -1
- package/lib/_chunks-cjs/editor-provider.cjs +0 -8756
- package/lib/_chunks-cjs/editor-provider.cjs.map +0 -1
- package/lib/_chunks-cjs/selector.get-focus-inline-object.cjs +0 -11
- package/lib/_chunks-cjs/selector.get-focus-inline-object.cjs.map +0 -1
- package/lib/_chunks-cjs/util.is-selection-collapsed.cjs +0 -6
- package/lib/_chunks-cjs/util.is-selection-collapsed.cjs.map +0 -1
- package/lib/_chunks-es/editor-provider.js +0 -8782
- package/lib/_chunks-es/editor-provider.js.map +0 -1
- package/lib/_chunks-es/selector.get-focus-inline-object.js +0 -13
- package/lib/_chunks-es/selector.get-focus-inline-object.js.map +0 -1
- package/lib/_chunks-es/util.is-selection-collapsed.js +0 -7
- package/lib/_chunks-es/util.is-selection-collapsed.js.map +0 -1
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { isKeySegment } from "@sanity/types";
|
|
2
|
+
import { isTextBlock, isSpan$1 as isSpan, isListBlock, isKeyedSegment, isSpan as isSpan$1, sliceBlocks } from "./util.slice-blocks.js";
|
|
3
|
+
const isSelectionCollapsed = (snapshot) => snapshot.context.selection ? JSON.stringify(snapshot.context.selection.anchor.path) === JSON.stringify(snapshot.context.selection.focus.path) && snapshot.context.selection?.anchor.offset === snapshot.context.selection?.focus.offset : !1, getFocusBlock = (snapshot) => {
|
|
4
|
+
const key = snapshot.context.selection && isKeyedSegment(snapshot.context.selection.focus.path[0]) ? snapshot.context.selection.focus.path[0]._key : void 0, node = key ? snapshot.context.value.find((block) => block._key === key) : void 0;
|
|
5
|
+
return node && key ? {
|
|
6
|
+
node,
|
|
7
|
+
path: [{
|
|
8
|
+
_key: key
|
|
9
|
+
}]
|
|
10
|
+
} : void 0;
|
|
11
|
+
}, getFocusListBlock = (snapshot) => {
|
|
12
|
+
const focusTextBlock = getFocusTextBlock(snapshot);
|
|
13
|
+
return focusTextBlock && isListBlock(snapshot.context, focusTextBlock.node) ? {
|
|
14
|
+
node: focusTextBlock.node,
|
|
15
|
+
path: focusTextBlock.path
|
|
16
|
+
} : void 0;
|
|
17
|
+
}, getFocusTextBlock = (snapshot) => {
|
|
18
|
+
const focusBlock = getFocusBlock(snapshot);
|
|
19
|
+
return focusBlock && isTextBlock(snapshot.context, focusBlock.node) ? {
|
|
20
|
+
node: focusBlock.node,
|
|
21
|
+
path: focusBlock.path
|
|
22
|
+
} : void 0;
|
|
23
|
+
}, getFocusBlockObject = (snapshot) => {
|
|
24
|
+
const focusBlock = getFocusBlock(snapshot);
|
|
25
|
+
return focusBlock && !isTextBlock(snapshot.context, focusBlock.node) ? {
|
|
26
|
+
node: focusBlock.node,
|
|
27
|
+
path: focusBlock.path
|
|
28
|
+
} : void 0;
|
|
29
|
+
}, getFocusChild = (snapshot) => {
|
|
30
|
+
const focusBlock = getFocusTextBlock(snapshot);
|
|
31
|
+
if (!focusBlock)
|
|
32
|
+
return;
|
|
33
|
+
const key = snapshot.context.selection && isKeyedSegment(snapshot.context.selection.focus.path[2]) ? snapshot.context.selection.focus.path[2]._key : void 0, node = key ? focusBlock.node.children.find((span) => span._key === key) : void 0;
|
|
34
|
+
return node && key ? {
|
|
35
|
+
node,
|
|
36
|
+
path: [...focusBlock.path, "children", {
|
|
37
|
+
_key: key
|
|
38
|
+
}]
|
|
39
|
+
} : void 0;
|
|
40
|
+
}, getFocusSpan = (snapshot) => {
|
|
41
|
+
const focusChild = getFocusChild(snapshot);
|
|
42
|
+
return focusChild && isSpan(snapshot.context, focusChild.node) ? {
|
|
43
|
+
node: focusChild.node,
|
|
44
|
+
path: focusChild.path
|
|
45
|
+
} : void 0;
|
|
46
|
+
}, getFirstBlock = (snapshot) => {
|
|
47
|
+
const node = snapshot.context.value[0];
|
|
48
|
+
return node ? {
|
|
49
|
+
node,
|
|
50
|
+
path: [{
|
|
51
|
+
_key: node._key
|
|
52
|
+
}]
|
|
53
|
+
} : void 0;
|
|
54
|
+
}, getLastBlock = (snapshot) => {
|
|
55
|
+
const node = snapshot.context.value[snapshot.context.value.length - 1] ? snapshot.context.value[snapshot.context.value.length - 1] : void 0;
|
|
56
|
+
return node ? {
|
|
57
|
+
node,
|
|
58
|
+
path: [{
|
|
59
|
+
_key: node._key
|
|
60
|
+
}]
|
|
61
|
+
} : void 0;
|
|
62
|
+
}, getSelectedBlocks = (snapshot) => {
|
|
63
|
+
if (!snapshot.context.selection)
|
|
64
|
+
return [];
|
|
65
|
+
const selectedBlocks = [], startKey = snapshot.context.selection.backward ? isKeyedSegment(snapshot.context.selection.focus.path[0]) ? snapshot.context.selection.focus.path[0]._key : void 0 : isKeyedSegment(snapshot.context.selection.anchor.path[0]) ? snapshot.context.selection.anchor.path[0]._key : void 0, endKey = snapshot.context.selection.backward ? isKeyedSegment(snapshot.context.selection.anchor.path[0]) ? snapshot.context.selection.anchor.path[0]._key : void 0 : isKeyedSegment(snapshot.context.selection.focus.path[0]) ? snapshot.context.selection.focus.path[0]._key : void 0;
|
|
66
|
+
if (!startKey || !endKey)
|
|
67
|
+
return selectedBlocks;
|
|
68
|
+
for (const block of snapshot.context.value) {
|
|
69
|
+
if (block._key === startKey) {
|
|
70
|
+
if (selectedBlocks.push({
|
|
71
|
+
node: block,
|
|
72
|
+
path: [{
|
|
73
|
+
_key: block._key
|
|
74
|
+
}]
|
|
75
|
+
}), startKey === endKey)
|
|
76
|
+
break;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (block._key === endKey) {
|
|
80
|
+
selectedBlocks.push({
|
|
81
|
+
node: block,
|
|
82
|
+
path: [{
|
|
83
|
+
_key: block._key
|
|
84
|
+
}]
|
|
85
|
+
});
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
selectedBlocks.length > 0 && selectedBlocks.push({
|
|
89
|
+
node: block,
|
|
90
|
+
path: [{
|
|
91
|
+
_key: block._key
|
|
92
|
+
}]
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return selectedBlocks;
|
|
96
|
+
}, getSelectionStartBlock = (snapshot) => {
|
|
97
|
+
if (!snapshot.context.selection)
|
|
98
|
+
return;
|
|
99
|
+
const key = snapshot.context.selection.backward ? isKeyedSegment(snapshot.context.selection.focus.path[0]) ? snapshot.context.selection.focus.path[0]._key : void 0 : isKeyedSegment(snapshot.context.selection.anchor.path[0]) ? snapshot.context.selection.anchor.path[0]._key : void 0, node = key ? snapshot.context.value.find((block) => block._key === key) : void 0;
|
|
100
|
+
return node && key ? {
|
|
101
|
+
node,
|
|
102
|
+
path: [{
|
|
103
|
+
_key: key
|
|
104
|
+
}]
|
|
105
|
+
} : void 0;
|
|
106
|
+
}, getSelectionEndBlock = (snapshot) => {
|
|
107
|
+
if (!snapshot.context.selection)
|
|
108
|
+
return;
|
|
109
|
+
const key = snapshot.context.selection.backward ? isKeyedSegment(snapshot.context.selection.anchor.path[0]) ? snapshot.context.selection.anchor.path[0]._key : void 0 : isKeyedSegment(snapshot.context.selection.focus.path[0]) ? snapshot.context.selection.focus.path[0]._key : void 0, node = key ? snapshot.context.value.find((block) => block._key === key) : void 0;
|
|
110
|
+
return node && key ? {
|
|
111
|
+
node,
|
|
112
|
+
path: [{
|
|
113
|
+
_key: key
|
|
114
|
+
}]
|
|
115
|
+
} : void 0;
|
|
116
|
+
}, getPreviousBlock = (snapshot) => {
|
|
117
|
+
let previousBlock;
|
|
118
|
+
const selectionStartBlock = getSelectionStartBlock(snapshot);
|
|
119
|
+
if (!selectionStartBlock)
|
|
120
|
+
return;
|
|
121
|
+
let foundSelectionStartBlock = !1;
|
|
122
|
+
for (const block of snapshot.context.value) {
|
|
123
|
+
if (block._key === selectionStartBlock.node._key) {
|
|
124
|
+
foundSelectionStartBlock = !0;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
previousBlock = {
|
|
128
|
+
node: block,
|
|
129
|
+
path: [{
|
|
130
|
+
_key: block._key
|
|
131
|
+
}]
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
if (foundSelectionStartBlock && previousBlock)
|
|
135
|
+
return previousBlock;
|
|
136
|
+
}, getNextBlock = (snapshot) => {
|
|
137
|
+
let nextBlock;
|
|
138
|
+
const selectionEndBlock = getSelectionEndBlock(snapshot);
|
|
139
|
+
if (!selectionEndBlock)
|
|
140
|
+
return;
|
|
141
|
+
let foundSelectionEndBlock = !1;
|
|
142
|
+
for (const block of snapshot.context.value) {
|
|
143
|
+
if (block._key === selectionEndBlock.node._key) {
|
|
144
|
+
foundSelectionEndBlock = !0;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (foundSelectionEndBlock) {
|
|
148
|
+
nextBlock = {
|
|
149
|
+
node: block,
|
|
150
|
+
path: [{
|
|
151
|
+
_key: block._key
|
|
152
|
+
}]
|
|
153
|
+
};
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (foundSelectionEndBlock && nextBlock)
|
|
158
|
+
return nextBlock;
|
|
159
|
+
}, getSelectionStartPoint = (snapshot) => {
|
|
160
|
+
if (snapshot.context.selection)
|
|
161
|
+
return snapshot.context.selection.backward ? snapshot.context.selection.focus : snapshot.context.selection.anchor;
|
|
162
|
+
}, getPreviousInlineObject = (snapshot) => {
|
|
163
|
+
const focusTextBlock = getFocusTextBlock(snapshot), selectionStartPoint = getSelectionStartPoint(snapshot), selectionStartPointChildKey = selectionStartPoint && isKeySegment(selectionStartPoint.path[2]) ? selectionStartPoint.path[2]._key : void 0;
|
|
164
|
+
if (!focusTextBlock || !selectionStartPointChildKey)
|
|
165
|
+
return;
|
|
166
|
+
let inlineObject;
|
|
167
|
+
for (const child of focusTextBlock.node.children) {
|
|
168
|
+
if (child._key === selectionStartPointChildKey)
|
|
169
|
+
break;
|
|
170
|
+
isSpan$1(snapshot.context, child) || (inlineObject = {
|
|
171
|
+
node: child,
|
|
172
|
+
path: [...focusTextBlock.path, "children", {
|
|
173
|
+
_key: child._key
|
|
174
|
+
}]
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
return inlineObject;
|
|
178
|
+
}, getSelectedSlice = (snapshot) => sliceBlocks({
|
|
179
|
+
context: snapshot.context,
|
|
180
|
+
blocks: snapshot.context.value
|
|
181
|
+
}), getSelectionText = (snapshot) => getSelectedSlice(snapshot).reduce((text, block) => isTextBlock(snapshot.context, block) ? text + block.children.reduce((text2, child) => isSpan(snapshot.context, child) ? text2 + child.text : text2, "") : text, ""), isSelectionExpanded = (snapshot) => !isSelectionCollapsed(snapshot);
|
|
182
|
+
export {
|
|
183
|
+
getFirstBlock,
|
|
184
|
+
getFocusBlock,
|
|
185
|
+
getFocusBlockObject,
|
|
186
|
+
getFocusChild,
|
|
187
|
+
getFocusListBlock,
|
|
188
|
+
getFocusSpan,
|
|
189
|
+
getFocusTextBlock,
|
|
190
|
+
getLastBlock,
|
|
191
|
+
getNextBlock,
|
|
192
|
+
getPreviousBlock,
|
|
193
|
+
getPreviousInlineObject,
|
|
194
|
+
getSelectedBlocks,
|
|
195
|
+
getSelectedSlice,
|
|
196
|
+
getSelectionEndBlock,
|
|
197
|
+
getSelectionStartBlock,
|
|
198
|
+
getSelectionStartPoint,
|
|
199
|
+
getSelectionText,
|
|
200
|
+
isSelectionCollapsed,
|
|
201
|
+
isSelectionExpanded
|
|
202
|
+
};
|
|
203
|
+
//# sourceMappingURL=selector.is-selection-expanded.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selector.is-selection-expanded.js","sources":["../../src/selectors/selector.is-selection-collapsed.ts","../../src/selectors/selectors.ts","../../src/selectors/selector.get-selection-start-point.ts","../../src/selectors/selector.get-previous-inline-object.ts","../../src/selectors/selector.get-selected-slice.ts","../../src/selectors/selector.get-selection-text.ts","../../src/selectors/selector.is-selection-expanded.ts"],"sourcesContent":["import type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @public\n */\nexport const isSelectionCollapsed: EditorSelector<boolean> = (snapshot) => {\n if (!snapshot.context.selection) {\n return false\n }\n\n return (\n JSON.stringify(snapshot.context.selection.anchor.path) ===\n JSON.stringify(snapshot.context.selection.focus.path) &&\n snapshot.context.selection?.anchor.offset ===\n snapshot.context.selection?.focus.offset\n )\n}\n","import type {\n KeyedSegment,\n PortableTextBlock,\n PortableTextListBlock,\n PortableTextObject,\n PortableTextSpan,\n PortableTextTextBlock,\n} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {isListBlock, isSpan, isTextBlock} from '../internal-utils/parse-blocks'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\n\n/**\n * @public\n */\nexport const getFocusBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = (snapshot) => {\n const key = snapshot.context.selection\n ? isKeyedSegment(snapshot.context.selection.focus.path[0])\n ? snapshot.context.selection.focus.path[0]._key\n : undefined\n : undefined\n\n const node = key\n ? snapshot.context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @public\n */\nexport const getFocusListBlock: EditorSelector<\n {node: PortableTextListBlock; path: [KeyedSegment]} | undefined\n> = (snapshot) => {\n const focusTextBlock = getFocusTextBlock(snapshot)\n\n return focusTextBlock && isListBlock(snapshot.context, focusTextBlock.node)\n ? {node: focusTextBlock.node, path: focusTextBlock.path}\n : undefined\n}\n\n/**\n * @public\n */\nexport const getFocusTextBlock: EditorSelector<\n {node: PortableTextTextBlock; path: [KeyedSegment]} | undefined\n> = (snapshot) => {\n const focusBlock = getFocusBlock(snapshot)\n\n return focusBlock && isTextBlock(snapshot.context, focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @public\n */\nexport const getFocusBlockObject: EditorSelector<\n {node: PortableTextObject; path: [KeyedSegment]} | undefined\n> = (snapshot) => {\n const focusBlock = getFocusBlock(snapshot)\n\n return focusBlock && !isTextBlock(snapshot.context, focusBlock.node)\n ? {node: focusBlock.node, path: focusBlock.path}\n : undefined\n}\n\n/**\n * @public\n */\nexport const getFocusChild: EditorSelector<\n | {\n node: PortableTextObject | PortableTextSpan\n path: [KeyedSegment, 'children', KeyedSegment]\n }\n | undefined\n> = (snapshot) => {\n const focusBlock = getFocusTextBlock(snapshot)\n\n if (!focusBlock) {\n return undefined\n }\n\n const key = snapshot.context.selection\n ? isKeyedSegment(snapshot.context.selection.focus.path[2])\n ? snapshot.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 * @public\n */\nexport const getFocusSpan: EditorSelector<\n | {node: PortableTextSpan; path: [KeyedSegment, 'children', KeyedSegment]}\n | undefined\n> = (snapshot) => {\n const focusChild = getFocusChild(snapshot)\n\n return focusChild && isSpan(snapshot.context, focusChild.node)\n ? {node: focusChild.node, path: focusChild.path}\n : undefined\n}\n\n/**\n * @public\n */\nexport const getFirstBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = (snapshot) => {\n const node = snapshot.context.value[0]\n\n return node ? {node, path: [{_key: node._key}]} : undefined\n}\n\n/**\n * @public\n */\nexport const getLastBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = (snapshot) => {\n const node = snapshot.context.value[snapshot.context.value.length - 1]\n ? snapshot.context.value[snapshot.context.value.length - 1]\n : undefined\n\n return node ? {node, path: [{_key: node._key}]} : undefined\n}\n\n/**\n * @public\n */\nexport const getSelectedBlocks: EditorSelector<\n Array<{node: PortableTextBlock; path: [KeyedSegment]}>\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return []\n }\n\n const selectedBlocks: Array<{node: PortableTextBlock; path: [KeyedSegment]}> =\n []\n const startKey = snapshot.context.selection.backward\n ? isKeyedSegment(snapshot.context.selection.focus.path[0])\n ? snapshot.context.selection.focus.path[0]._key\n : undefined\n : isKeyedSegment(snapshot.context.selection.anchor.path[0])\n ? snapshot.context.selection.anchor.path[0]._key\n : undefined\n const endKey = snapshot.context.selection.backward\n ? isKeyedSegment(snapshot.context.selection.anchor.path[0])\n ? snapshot.context.selection.anchor.path[0]._key\n : undefined\n : isKeyedSegment(snapshot.context.selection.focus.path[0])\n ? snapshot.context.selection.focus.path[0]._key\n : undefined\n\n if (!startKey || !endKey) {\n return selectedBlocks\n }\n\n for (const block of snapshot.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 * @public\n */\nexport const getSelectionStartBlock: EditorSelector<\n | {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n const key = snapshot.context.selection.backward\n ? isKeyedSegment(snapshot.context.selection.focus.path[0])\n ? snapshot.context.selection.focus.path[0]._key\n : undefined\n : isKeyedSegment(snapshot.context.selection.anchor.path[0])\n ? snapshot.context.selection.anchor.path[0]._key\n : undefined\n\n const node = key\n ? snapshot.context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @public\n */\nexport const getSelectionEndBlock: EditorSelector<\n | {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n const key = snapshot.context.selection.backward\n ? isKeyedSegment(snapshot.context.selection.anchor.path[0])\n ? snapshot.context.selection.anchor.path[0]._key\n : undefined\n : isKeyedSegment(snapshot.context.selection.focus.path[0])\n ? snapshot.context.selection.focus.path[0]._key\n : undefined\n\n const node = key\n ? snapshot.context.value.find((block) => block._key === key)\n : undefined\n\n return node && key ? {node, path: [{_key: key}]} : undefined\n}\n\n/**\n * @public\n */\nexport const getPreviousBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = (snapshot) => {\n let previousBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n const selectionStartBlock = getSelectionStartBlock(snapshot)\n\n if (!selectionStartBlock) {\n return undefined\n }\n\n let foundSelectionStartBlock = false\n\n for (const block of snapshot.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 * @public\n */\nexport const getNextBlock: EditorSelector<\n {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n> = (snapshot) => {\n let nextBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined\n const selectionEndBlock = getSelectionEndBlock(snapshot)\n\n if (!selectionEndBlock) {\n return undefined\n }\n\n let foundSelectionEndBlock = false\n\n for (const block of snapshot.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 type {EditorSelectionPoint} from '..'\nimport type {EditorSelector} from '../editor/editor-selector'\n\n/**\n * @public\n */\nexport const getSelectionStartPoint: EditorSelector<\n EditorSelectionPoint | undefined\n> = (snapshot) => {\n if (!snapshot.context.selection) {\n return undefined\n }\n\n return snapshot.context.selection.backward\n ? snapshot.context.selection.focus\n : snapshot.context.selection.anchor\n}\n","import {\n isKeySegment,\n type KeyedSegment,\n type PortableTextObject,\n} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {isSpan} from '../utils'\nimport {getSelectionStartPoint} from './selector.get-selection-start-point'\nimport {getFocusTextBlock} from './selectors'\n\n/**\n * @public\n */\nexport const getPreviousInlineObject: EditorSelector<\n | {\n node: PortableTextObject\n path: [KeyedSegment, 'children', KeyedSegment]\n }\n | undefined\n> = (snapshot) => {\n const focusTextBlock = getFocusTextBlock(snapshot)\n const selectionStartPoint = getSelectionStartPoint(snapshot)\n const selectionStartPointChildKey =\n selectionStartPoint && isKeySegment(selectionStartPoint.path[2])\n ? selectionStartPoint.path[2]._key\n : undefined\n\n if (!focusTextBlock || !selectionStartPointChildKey) {\n return undefined\n }\n\n let inlineObject:\n | {\n node: PortableTextObject\n path: [KeyedSegment, 'children', KeyedSegment]\n }\n | undefined\n\n for (const child of focusTextBlock.node.children) {\n if (child._key === selectionStartPointChildKey) {\n break\n }\n\n if (!isSpan(snapshot.context, child)) {\n inlineObject = {\n node: child,\n path: [...focusTextBlock.path, 'children', {_key: child._key}],\n }\n }\n }\n\n return inlineObject\n}\n","import type {PortableTextBlock} from '@sanity/types'\nimport type {EditorSelector} from '../editor/editor-selector'\nimport {sliceBlocks} from '../utils'\n\n/**\n * @public\n */\nexport const getSelectedSlice: EditorSelector<Array<PortableTextBlock>> = (\n snapshot,\n) => {\n return sliceBlocks({\n context: snapshot.context,\n blocks: snapshot.context.value,\n })\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {isSpan, isTextBlock} from '../internal-utils/parse-blocks'\nimport {getSelectedSlice} from './selector.get-selected-slice'\n\n/**\n * @public\n */\nexport const getSelectionText: EditorSelector<string> = (snapshot) => {\n const selectedSlice = getSelectedSlice(snapshot)\n\n return selectedSlice.reduce((text, block) => {\n if (!isTextBlock(snapshot.context, block)) {\n return text\n }\n\n return (\n text +\n block.children.reduce((text, child) => {\n if (isSpan(snapshot.context, child)) {\n return text + child.text\n }\n\n return text\n }, '')\n )\n }, '')\n}\n","import type {EditorSelector} from '../editor/editor-selector'\nimport {isSelectionCollapsed} from './selector.is-selection-collapsed'\n\n/**\n * @public\n */\nexport const isSelectionExpanded: EditorSelector<boolean> = (snapshot) => {\n return !isSelectionCollapsed(snapshot)\n}\n"],"names":["isSelectionCollapsed","snapshot","context","selection","JSON","stringify","anchor","path","focus","offset","getFocusBlock","key","isKeyedSegment","_key","undefined","node","value","find","block","getFocusListBlock","focusTextBlock","getFocusTextBlock","isListBlock","focusBlock","isTextBlock","getFocusBlockObject","getFocusChild","children","span","getFocusSpan","focusChild","isSpan","getFirstBlock","getLastBlock","length","getSelectedBlocks","selectedBlocks","startKey","backward","endKey","push","getSelectionStartBlock","getSelectionEndBlock","getPreviousBlock","previousBlock","selectionStartBlock","foundSelectionStartBlock","getNextBlock","nextBlock","selectionEndBlock","foundSelectionEndBlock","getSelectionStartPoint","getPreviousInlineObject","selectionStartPoint","selectionStartPointChildKey","isKeySegment","inlineObject","child","getSelectedSlice","sliceBlocks","blocks","getSelectionText","reduce","text","isSelectionExpanded"],"mappings":";;AAKO,MAAMA,uBAAiDC,CAAAA,aACvDA,SAASC,QAAQC,YAKpBC,KAAKC,UAAUJ,SAASC,QAAQC,UAAUG,OAAOC,IAAI,MACnDH,KAAKC,UAAUJ,SAASC,QAAQC,UAAUK,MAAMD,IAAI,KACtDN,SAASC,QAAQC,WAAWG,OAAOG,WACjCR,SAASC,QAAQC,WAAWK,MAAMC,SAP7B,ICQEC,gBAERT,CAAa,aAAA;AAChB,QAAMU,MAAMV,SAASC,QAAQC,aACzBS,eAAeX,SAASC,QAAQC,UAAUK,MAAMD,KAAK,CAAC,CAAC,IACrDN,SAASC,QAAQC,UAAUK,MAAMD,KAAK,CAAC,EAAEM,OAE3CC,QAEEC,OAAOJ,MACTV,SAASC,QAAQc,MAAMC,KAAMC,CAAAA,UAAUA,MAAML,SAASF,GAAG,IACzDG;AAEJ,SAAOC,QAAQJ,MAAM;AAAA,IAACI;AAAAA,IAAMR,MAAM,CAAC;AAAA,MAACM,MAAMF;AAAAA,IAAI,CAAA;AAAA,EAAA,IAAKG;AACrD,GAKaK,oBAERlB,CAAa,aAAA;AACVmB,QAAAA,iBAAiBC,kBAAkBpB,QAAQ;AAEjD,SAAOmB,kBAAkBE,YAAYrB,SAASC,SAASkB,eAAeL,IAAI,IACtE;AAAA,IAACA,MAAMK,eAAeL;AAAAA,IAAMR,MAAMa,eAAeb;AAAAA,EAAAA,IACjDO;AACN,GAKaO,oBAERpB,CAAa,aAAA;AACVsB,QAAAA,aAAab,cAAcT,QAAQ;AAEzC,SAAOsB,cAAcC,YAAYvB,SAASC,SAASqB,WAAWR,IAAI,IAC9D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eof globalThis < "u") return globalThis;
|
|
4
|
+
if (typeof window < "u") return window;
|
|
5
|
+
if (typeof self < "u") return self;
|
|
6
|
+
if (typeof global < "u") return global;
|
|
7
|
+
throw new Error("@portabletext/editor: could not locate global scope");
|
|
8
|
+
}
|
|
9
|
+
const globalScope = getGlobalScope();
|
|
10
|
+
function createGloballyScopedContext(key, defaultValue) {
|
|
11
|
+
const symbol = Symbol.for(key);
|
|
12
|
+
return typeof document > "u" ? createContext(defaultValue) : (globalScope[symbol] = globalScope[symbol] ?? createContext(defaultValue), globalScope[symbol]);
|
|
13
|
+
}
|
|
14
|
+
const EditorContext = createGloballyScopedContext("@portabletext/editor/context/editor", null);
|
|
15
|
+
function useEditor() {
|
|
16
|
+
const editor = React.useContext(EditorContext);
|
|
17
|
+
if (!editor)
|
|
18
|
+
throw new Error("No Editor set. Use EditorProvider to set one.");
|
|
19
|
+
return editor;
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
EditorContext,
|
|
23
|
+
useEditor
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=use-editor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-editor.js","sources":["../../src/internal-utils/global-scope.ts","../../src/internal-utils/globally-scoped-context.ts","../../src/editor/editor-context.tsx","../../src/editor/use-editor.ts"],"sourcesContent":["/**\n * Gets the global scope instance in a given environment.\n *\n * The strategy is to return the most modern, and if not, the most common:\n * - The `globalThis` variable is the modern approach to accessing the global scope\n * - The `window` variable is the global scope in a web browser\n * - The `self` variable is the global scope in workers and others\n * - The `global` variable is the global scope in Node.js\n */\nfunction getGlobalScope() {\n if (typeof globalThis !== 'undefined') return globalThis\n if (typeof window !== 'undefined') return window\n if (typeof self !== 'undefined') return self\n if (typeof global !== 'undefined') return global\n\n throw new Error('@portabletext/editor: could not locate global scope')\n}\n\nexport const globalScope = getGlobalScope() as any\n","import {createContext, type Context} from 'react'\nimport {globalScope} from './global-scope'\n\n/**\n * As `@portabletext/editor` is declared as a dependency, and may be\n * duplicated, sometimes across major versions it's critical that vital\n * React Contexts are shared even when there is a duplicate.\n *\n * We have to support a Sanity Plugin being able to call hooks like\n * `useEditor`, and then read the context setup by `sanity`, which calls\n * `EditorProvider`, even if the provider and hook are different instances in\n * memory.\n *\n * For this reason it's vital that all changes to globally scoped providers\n * remain fully backwards compatible.\n */\nexport function createGloballyScopedContext<\n ContextType,\n const T extends ContextType = ContextType,\n>(\n /**\n * Enforce that all Symbol.for keys used for globally scoped contexts have a predictable prefix\n */\n key: `@portabletext/editor/context/${string}`,\n defaultValue: T,\n): Context<ContextType> {\n const symbol = Symbol.for(key)\n\n /**\n * Prevent errors about re-renders on React SSR on Next.js App Router\n */\n if (typeof document === 'undefined') {\n return createContext<ContextType>(defaultValue)\n }\n\n globalScope[symbol] = globalScope[symbol] ?? createContext<T>(defaultValue)\n\n return globalScope[symbol]\n}\n","import type {Editor} from '../editor'\nimport {createGloballyScopedContext} from '../internal-utils/globally-scoped-context'\n\nexport const EditorContext = createGloballyScopedContext<Editor | null>(\n '@portabletext/editor/context/editor',\n null,\n)\n","import React from 'react'\nimport {EditorContext} from './editor-context'\n\n/**\n * @public\n * Get the current editor context from the `EditorProvider`.\n * Must be used inside the `EditorProvider` component.\n * @returns The current editor object.\n * @example\n * ```tsx\n * import { useEditor } from '@portabletext/editor'\n *\n * function MyComponent() {\n * const editor = useEditor()\n * }\n * ```\n * @group Hooks\n */\nexport function useEditor() {\n const editor = React.useContext(EditorContext)\n\n if (!editor) {\n throw new Error('No Editor set. Use EditorProvider to set one.')\n }\n\n return editor\n}\n"],"names":["getGlobalScope","globalThis","window","self","global","Error","globalScope","createGloballyScopedContext","key","defaultValue","symbol","Symbol","for","document","createContext","EditorContext","useEditor","editor","React","useContext"],"mappings":";AASA,SAASA,iBAAiB;AACpB,MAAA,OAAOC,aAAe,IAAoBA,QAAAA;AAC1C,MAAA,OAAOC,SAAW,IAAoBA,QAAAA;AACtC,MAAA,OAAOC,OAAS,IAAoBA,QAAAA;AACpC,MAAA,OAAOC,SAAW,IAAoBA,QAAAA;AAEpC,QAAA,IAAIC,MAAM,qDAAqD;AACvE;AAEO,MAAMC,cAAcN,eAAe;ACF1BO,SAAAA,4BAOdC,KACAC,cACsB;AAChBC,QAAAA,SAASC,OAAOC,IAAIJ,GAAG;AAK7B,SAAI,OAAOK,WAAa,MACfC,cAA2BL,YAAY,KAGhDH,YAAYI,MAAM,IAAIJ,YAAYI,MAAM,KAAKI,cAAiBL,YAAY,GAEnEH,YAAYI,MAAM;AAC3B;ACnCaK,MAAAA,gBAAgBR,4BAC3B,uCACA,IACF;ACYO,SAAAS,YAAA;AACLC,QAAAA,SAAeC,MAAAC,WAAAJ,aAA8B;AAAC,MAAA,CAEzCE;AAAMZ,UAAAA,IAAAA,MACO,+CAA+C;AAG1DY,SAAAA;AAAM;"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { blockOffsetToSpanSelectionPoint, isTextBlock, isSpan$1 as isSpan, isKeyedSegment } from "./util.slice-blocks.js";
|
|
2
|
+
function blockOffsetToBlockSelectionPoint({
|
|
3
|
+
context,
|
|
4
|
+
blockOffset
|
|
5
|
+
}) {
|
|
6
|
+
let selectionPoint;
|
|
7
|
+
for (const block of context.value)
|
|
8
|
+
if (block._key === blockOffset.path[0]._key) {
|
|
9
|
+
selectionPoint = {
|
|
10
|
+
path: [{
|
|
11
|
+
_key: block._key
|
|
12
|
+
}],
|
|
13
|
+
offset: blockOffset.offset
|
|
14
|
+
};
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
return selectionPoint;
|
|
18
|
+
}
|
|
19
|
+
function blockOffsetToSelectionPoint({
|
|
20
|
+
context,
|
|
21
|
+
blockOffset,
|
|
22
|
+
direction
|
|
23
|
+
}) {
|
|
24
|
+
return blockOffsetToSpanSelectionPoint({
|
|
25
|
+
context,
|
|
26
|
+
blockOffset,
|
|
27
|
+
direction
|
|
28
|
+
}) || blockOffsetToBlockSelectionPoint({
|
|
29
|
+
context,
|
|
30
|
+
blockOffset
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function blockOffsetsToSelection({
|
|
34
|
+
context,
|
|
35
|
+
offsets,
|
|
36
|
+
backward
|
|
37
|
+
}) {
|
|
38
|
+
const anchor = blockOffsetToSelectionPoint({
|
|
39
|
+
context,
|
|
40
|
+
blockOffset: offsets.anchor,
|
|
41
|
+
direction: backward ? "backward" : "forward"
|
|
42
|
+
}), focus = blockOffsetToSelectionPoint({
|
|
43
|
+
context,
|
|
44
|
+
blockOffset: offsets.focus,
|
|
45
|
+
direction: backward ? "forward" : "backward"
|
|
46
|
+
});
|
|
47
|
+
return !anchor || !focus ? null : {
|
|
48
|
+
anchor,
|
|
49
|
+
focus,
|
|
50
|
+
backward
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function childSelectionPointToBlockOffset({
|
|
54
|
+
context,
|
|
55
|
+
selectionPoint
|
|
56
|
+
}) {
|
|
57
|
+
let offset = 0;
|
|
58
|
+
const blockKey = isKeyedSegment(selectionPoint.path[0]) ? selectionPoint.path[0]._key : void 0, childKey = isKeyedSegment(selectionPoint.path[2]) ? selectionPoint.path[2]._key : void 0;
|
|
59
|
+
if (!(!blockKey || !childKey)) {
|
|
60
|
+
for (const block of context.value)
|
|
61
|
+
if (block._key === blockKey && isTextBlock(context, block))
|
|
62
|
+
for (const child of block.children) {
|
|
63
|
+
if (child._key === childKey)
|
|
64
|
+
return {
|
|
65
|
+
path: [{
|
|
66
|
+
_key: block._key
|
|
67
|
+
}],
|
|
68
|
+
offset: offset + selectionPoint.offset
|
|
69
|
+
};
|
|
70
|
+
isSpan(context, child) && (offset += child.text.length);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
blockOffsetToBlockSelectionPoint,
|
|
76
|
+
blockOffsetToSelectionPoint,
|
|
77
|
+
blockOffsetsToSelection,
|
|
78
|
+
childSelectionPointToBlockOffset
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=util.child-selection-point-to-block-offset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.child-selection-point-to-block-offset.js","sources":["../../src/utils/util.block-offset-to-block-selection-point.ts","../../src/utils/util.block-offset-to-selection-point.ts","../../src/utils/util.block-offsets-to-selection.ts","../../src/utils/util.child-selection-point-to-block-offset.ts"],"sourcesContent":["import type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function blockOffsetToBlockSelectionPoint({\n context,\n blockOffset,\n}: {\n context: Pick<EditorContext, 'value'>\n blockOffset: BlockOffset\n}): EditorSelectionPoint | undefined {\n let selectionPoint: EditorSelectionPoint | undefined\n\n for (const block of context.value) {\n if (block._key === blockOffset.path[0]._key) {\n selectionPoint = {\n path: [{_key: block._key}],\n offset: blockOffset.offset,\n }\n break\n }\n }\n\n return selectionPoint\n}\n","import type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {blockOffsetToSpanSelectionPoint} from './util.block-offset'\nimport {blockOffsetToBlockSelectionPoint} from './util.block-offset-to-block-selection-point'\n\n/**\n * @public\n */\nexport function blockOffsetToSelectionPoint({\n context,\n blockOffset,\n direction,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n blockOffset: BlockOffset\n direction: 'forward' | 'backward'\n}): EditorSelectionPoint | undefined {\n const spanSelectionPoint = blockOffsetToSpanSelectionPoint({\n context,\n blockOffset,\n direction,\n })\n\n if (!spanSelectionPoint) {\n return blockOffsetToBlockSelectionPoint({\n context,\n blockOffset,\n })\n }\n\n return spanSelectionPoint\n}\n","import type {EditorSelection} from '..'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport {blockOffsetToSelectionPoint} from './util.block-offset-to-selection-point'\n\n/**\n * @public\n */\nexport function blockOffsetsToSelection({\n context,\n offsets,\n backward,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n offsets: {anchor: BlockOffset; focus: BlockOffset}\n backward?: boolean\n}): EditorSelection {\n const anchor = blockOffsetToSelectionPoint({\n context,\n blockOffset: offsets.anchor,\n direction: backward ? 'backward' : 'forward',\n })\n const focus = blockOffsetToSelectionPoint({\n context,\n blockOffset: offsets.focus,\n direction: backward ? 'forward' : 'backward',\n })\n\n if (!anchor || !focus) {\n return null\n }\n\n return {\n anchor,\n focus,\n backward,\n }\n}\n","import type {EditorContext} from '../editor/editor-snapshot'\nimport {isSpan, isTextBlock} from '../internal-utils/parse-blocks'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {isKeyedSegment} from './util.is-keyed-segment'\n\n/**\n * @public\n */\nexport function childSelectionPointToBlockOffset({\n context,\n selectionPoint,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n selectionPoint: EditorSelectionPoint\n}): BlockOffset | undefined {\n let offset = 0\n\n const blockKey = isKeyedSegment(selectionPoint.path[0])\n ? selectionPoint.path[0]._key\n : undefined\n const childKey = isKeyedSegment(selectionPoint.path[2])\n ? selectionPoint.path[2]._key\n : undefined\n\n if (!blockKey || !childKey) {\n return undefined\n }\n\n for (const block of context.value) {\n if (block._key !== blockKey) {\n continue\n }\n\n if (!isTextBlock(context, block)) {\n continue\n }\n\n for (const child of block.children) {\n if (child._key === childKey) {\n return {\n path: [{_key: block._key}],\n offset: offset + selectionPoint.offset,\n }\n }\n\n if (isSpan(context, child)) {\n offset += child.text.length\n }\n }\n }\n}\n"],"names":["blockOffsetToBlockSelectionPoint","context","blockOffset","selectionPoint","block","value","_key","path","offset","blockOffsetToSelectionPoint","direction","blockOffsetToSpanSelectionPoint","blockOffsetsToSelection","offsets","backward","anchor","focus","childSelectionPointToBlockOffset","blockKey","isKeyedSegment","undefined","childKey","isTextBlock","child","children","isS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ACvBc;AAEA,MAAA,EAAA,CAACF,YAAY,CAACG;AAIlB,eAAWjB,SAASH,QAAQI;AAC1B,UAAID,MAAME,SAASY,YAIdI,YAAYrB,SAASG,KAAK;AAIpBmB,mBAAAA,SAASnB,MAAMoB,UAAU;AAClC,cAAID,MAAMjB,SAASe;AACV,mBAAA;AAAA,cACLd,MAAM,CAAC;AAAA,gBAACD,MAAMF,MAAME;AAAAA,cAAAA,CAAK;AAAA,cACzBE,QAAQA,SAASL,eAAeK;AAAAA,YAClC;AAGEiB,iBAAOxB,SAASsB,KAAK,MACvBf,UAAUe,MAAMG,KAAKC;AAAAA,QAAAA;AAAAA;AAI7B;"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isTextBlock, isSpan$1 as isSpan, getTextBlockText } from "./util.slice-blocks.js";
|
|
2
|
+
function getBlockEndPoint({
|
|
3
|
+
context,
|
|
4
|
+
block
|
|
5
|
+
}) {
|
|
6
|
+
if (isTextBlock(context, block.node)) {
|
|
7
|
+
const lastChild = block.node.children[block.node.children.length - 1];
|
|
8
|
+
if (lastChild)
|
|
9
|
+
return {
|
|
10
|
+
path: [...block.path, "children", {
|
|
11
|
+
_key: lastChild._key
|
|
12
|
+
}],
|
|
13
|
+
offset: isSpan(context, lastChild) ? lastChild.text.length : 0
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
path: block.path,
|
|
18
|
+
offset: 0
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function isEmptyTextBlock(context, block) {
|
|
22
|
+
if (!isTextBlock(context, block))
|
|
23
|
+
return !1;
|
|
24
|
+
const onlyText = block.children.every((child) => isSpan(context, child)), blockText = getTextBlockText(block);
|
|
25
|
+
return onlyText && blockText === "";
|
|
26
|
+
}
|
|
27
|
+
function isEqualSelectionPoints(a, b) {
|
|
28
|
+
return a.offset === b.offset && JSON.stringify(a.path) === JSON.stringify(b.path);
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
getBlockEndPoint,
|
|
32
|
+
isEmptyTextBlock,
|
|
33
|
+
isEqualSelectionPoints
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=util.is-equal-selection-points.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"util.is-equal-selection-points.js","sources":["../../src/utils/util.get-block-end-point.ts","../../src/utils/util.is-empty-text-block.ts","../../src/utils/util.is-equal-selection-points.ts"],"sourcesContent":["import type {KeyedSegment, PortableTextBlock} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isSpan, isTextBlock} from '../internal-utils/parse-blocks'\nimport type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function getBlockEndPoint({\n context,\n block,\n}: {\n context: Pick<EditorContext, 'schema'>\n block: {\n node: PortableTextBlock\n path: [KeyedSegment]\n }\n}): EditorSelectionPoint {\n if (isTextBlock(context, block.node)) {\n const lastChild = block.node.children[block.node.children.length - 1]\n\n if (lastChild) {\n return {\n path: [...block.path, 'children', {_key: lastChild._key}],\n offset: isSpan(context, lastChild) ? lastChild.text.length : 0,\n }\n }\n }\n\n return {\n path: block.path,\n offset: 0,\n }\n}\n","import type {PortableTextBlock} from '@sanity/types'\nimport type {EditorContext} from '../editor/editor-snapshot'\nimport {isSpan, isTextBlock} from '../internal-utils/parse-blocks'\nimport {getTextBlockText} from './util.get-text-block-text'\n\n/**\n * @public\n */\nexport function isEmptyTextBlock(\n context: Pick<EditorContext, 'schema'>,\n block: PortableTextBlock,\n) {\n if (!isTextBlock(context, block)) {\n return false\n }\n\n const onlyText = block.children.every((child) => isSpan(context, child))\n const blockText = getTextBlockText(block)\n\n return onlyText && blockText === ''\n}\n","import type {EditorSelectionPoint} from '../types/editor'\n\n/**\n * @public\n */\nexport function isEqualSelectionPoints(\n a: EditorSelectionPoint,\n b: EditorSelectionPoint,\n) {\n return (\n a.offset === b.offset && JSON.stringify(a.path) === JSON.stringify(b.path)\n )\n}\n"],"names":["getBlockEndPoint","context","block","isTextBlock","node","lastChild","children","length","path","_key","offset","isSpan","text","isEmptyTextBlock","onlyText","every","child","blockText","getTextBlockText","isEqualSelectionPoints","a","b","JSON","stringify"],"mappings":";AAQO,SAASA,iBAAiB;AAAA,EAC/BC;AAAAA,EACAC;AAOF,GAAyB;AACvB,MAAIC,YAAYF,SAASC,MAAME,IAAI,GAAG;AAC9BC,UAAAA,YAAYH,MAAME,KAAKE,SAASJ,MAAME,KAAKE,SAASC,SAAS,CAAC;AAEhEF,QAAAA;AACK,aAAA;AAAA,QACLG,MAAM,CAAC,GAAGN,MAAMM,MAAM,YAAY;AAAA,UAACC,MAAMJ,UAAUI;AAAAA,QAAAA,CAAK;AAAA,QACxDC,QAAQC,OAAOV,SAASI,SAAS,IAAIA,UAAUO,KAAKL,SAAS;AAAA,MAC/D;AAAA,EAAA;AAIG,SAAA;AAAA,IACLC,MAAMN,MAAMM;AAAAA,IACZE,QAAQ;AAAA,EACV;AACF;ACzBgBG,SAAAA,iBACdZ,SACAC,OACA;AACI,MAAA,CAACC,YAAYF,SAASC,KAAK;AACtB,WAAA;AAGT,QAAMY,WAAWZ,MAAMI,SAASS,MAAOC,CAAUL,UAAAA,OAAOV,SAASe,KAAK,CAAC,GACjEC,YAAYC,iBAAiBhB,KAAK;AAExC,SAAOY,YAAYG,cAAc;AACnC;ACfgBE,SAAAA,uBACdC,GACAC,GACA;AACA,SACED,EAAEV,WAAWW,EAAEX,UAAUY,KAAKC,UAAUH,EAAEZ,IAAI,MAAMc,KAAKC,UAAUF,EAAEb,IAAI;AAE7E;"}
|
|
@@ -1,82 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
context,
|
|
4
|
-
blockOffset
|
|
5
|
-
}) {
|
|
6
|
-
let selectionPoint;
|
|
7
|
-
for (const block of context.value)
|
|
8
|
-
if (block._key === blockOffset.path[0]._key) {
|
|
9
|
-
selectionPoint = {
|
|
10
|
-
path: [{
|
|
11
|
-
_key: block._key
|
|
12
|
-
}],
|
|
13
|
-
offset: blockOffset.offset
|
|
14
|
-
};
|
|
15
|
-
break;
|
|
16
|
-
}
|
|
17
|
-
return selectionPoint;
|
|
18
|
-
}
|
|
19
|
-
function blockOffsetToSelectionPoint({
|
|
20
|
-
context,
|
|
21
|
-
blockOffset,
|
|
22
|
-
direction
|
|
23
|
-
}) {
|
|
24
|
-
return blockOffsetToSpanSelectionPoint({
|
|
25
|
-
context,
|
|
26
|
-
blockOffset,
|
|
27
|
-
direction
|
|
28
|
-
}) || blockOffsetToBlockSelectionPoint({
|
|
29
|
-
context,
|
|
30
|
-
blockOffset
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
function blockOffsetsToSelection({
|
|
34
|
-
context,
|
|
35
|
-
offsets,
|
|
36
|
-
backward
|
|
37
|
-
}) {
|
|
38
|
-
const anchor = blockOffsetToSelectionPoint({
|
|
39
|
-
context,
|
|
40
|
-
blockOffset: offsets.anchor,
|
|
41
|
-
direction: backward ? "backward" : "forward"
|
|
42
|
-
}), focus = blockOffsetToSelectionPoint({
|
|
43
|
-
context,
|
|
44
|
-
blockOffset: offsets.focus,
|
|
45
|
-
direction: backward ? "forward" : "backward"
|
|
46
|
-
});
|
|
47
|
-
return !anchor || !focus ? null : {
|
|
48
|
-
anchor,
|
|
49
|
-
focus,
|
|
50
|
-
backward
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
function childSelectionPointToBlockOffset({
|
|
54
|
-
context,
|
|
55
|
-
selectionPoint
|
|
56
|
-
}) {
|
|
57
|
-
let offset = 0;
|
|
58
|
-
const blockKey = isKeyedSegment(selectionPoint.path[0]) ? selectionPoint.path[0]._key : void 0, childKey = isKeyedSegment(selectionPoint.path[2]) ? selectionPoint.path[2]._key : void 0;
|
|
59
|
-
if (!(!blockKey || !childKey)) {
|
|
60
|
-
for (const block of context.value)
|
|
61
|
-
if (block._key === blockKey && isTextBlock(context, block))
|
|
62
|
-
for (const child of block.children) {
|
|
63
|
-
if (child._key === childKey)
|
|
64
|
-
return {
|
|
65
|
-
path: [{
|
|
66
|
-
_key: block._key
|
|
67
|
-
}],
|
|
68
|
-
offset: offset + selectionPoint.offset
|
|
69
|
-
};
|
|
70
|
-
isSpan(context, child) && (offset += child.text.length);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
1
|
+
import { childSelectionPointToBlockOffset } from "./util.child-selection-point-to-block-offset.js";
|
|
2
|
+
import { isKeyedSegment } from "./util.slice-blocks.js";
|
|
74
3
|
function getSelectionEndPoint(selection) {
|
|
75
4
|
return selection ? selection.backward ? selection.anchor : selection.focus : null;
|
|
76
5
|
}
|
|
77
6
|
function getSelectionStartPoint(selection) {
|
|
78
7
|
return selection ? selection.backward ? selection.focus : selection.anchor : null;
|
|
79
8
|
}
|
|
9
|
+
function isSelectionCollapsed(selection) {
|
|
10
|
+
return selection ? selection.anchor.path.join() === selection.focus.path.join() && selection.anchor.offset === selection.focus.offset : !1;
|
|
11
|
+
}
|
|
80
12
|
function selectionPointToBlockOffset({
|
|
81
13
|
context,
|
|
82
14
|
selectionPoint
|
|
@@ -92,12 +24,9 @@ function selectionPointToBlockOffset({
|
|
|
92
24
|
});
|
|
93
25
|
}
|
|
94
26
|
export {
|
|
95
|
-
blockOffsetToBlockSelectionPoint,
|
|
96
|
-
blockOffsetToSelectionPoint,
|
|
97
|
-
blockOffsetsToSelection,
|
|
98
|
-
childSelectionPointToBlockOffset,
|
|
99
27
|
getSelectionEndPoint,
|
|
100
28
|
getSelectionStartPoint,
|
|
29
|
+
isSelectionCollapsed,
|
|
101
30
|
selectionPointToBlockOffset
|
|
102
31
|
};
|
|
103
32
|
//# sourceMappingURL=util.selection-point-to-block-offset.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.selection-point-to-block-offset.js","sources":["../../src/utils/util.
|
|
1
|
+
{"version":3,"file":"util.selection-point-to-block-offset.js","sources":["../../src/utils/util.get-selection-end-point.ts","../../src/utils/util.get-selection-start-point.ts","../../src/utils/util.is-selection-collapsed.ts","../../src/utils/util.selection-point-to-block-offset.ts"],"sourcesContent":["import type {EditorSelection, EditorSelectionPoint} from '..'\n\n/**\n * @public\n */\nexport function getSelectionEndPoint<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n TEditorSelectionPoint extends\n EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>\n ? EditorSelectionPoint\n : null,\n>(selection: TEditorSelection): TEditorSelectionPoint {\n if (!selection) {\n return null as TEditorSelectionPoint\n }\n\n return (\n selection.backward ? selection.anchor : selection.focus\n ) as TEditorSelectionPoint\n}\n","import type {EditorSelection, EditorSelectionPoint} from '..'\n\n/**\n * @public\n */\nexport function getSelectionStartPoint<\n TEditorSelection extends NonNullable<EditorSelection> | null,\n TEditorSelectionPoint extends\n EditorSelectionPoint | null = TEditorSelection extends NonNullable<EditorSelection>\n ? EditorSelectionPoint\n : null,\n>(selection: TEditorSelection): TEditorSelectionPoint {\n if (!selection) {\n return null as TEditorSelectionPoint\n }\n\n return (\n selection.backward ? selection.focus : selection.anchor\n ) as TEditorSelectionPoint\n}\n","import type {EditorSelection} from '../types/editor'\n\n/**\n * @public\n */\nexport function isSelectionCollapsed(selection: EditorSelection) {\n if (!selection) {\n return false\n }\n\n return (\n selection.anchor.path.join() === selection.focus.path.join() &&\n selection.anchor.offset === selection.focus.offset\n )\n}\n","import type {EditorContext} from '../editor/editor-snapshot'\nimport type {BlockOffset} from '../types/block-offset'\nimport type {EditorSelectionPoint} from '../types/editor'\nimport {childSelectionPointToBlockOffset} from './util.child-selection-point-to-block-offset'\nimport {isKeyedSegment} from './util.is-keyed-segment'\n\n/**\n * @public\n */\nexport function selectionPointToBlockOffset({\n context,\n selectionPoint,\n}: {\n context: Pick<EditorContext, 'schema' | 'value'>\n selectionPoint: EditorSelectionPoint\n}): BlockOffset | undefined {\n if (\n selectionPoint.path.length === 1 &&\n isKeyedSegment(selectionPoint.path[0])\n ) {\n return {\n path: [{_key: selectionPoint.path[0]._key}],\n offset: selectionPoint.offset,\n }\n }\n\n return childSelectionPointToBlockOffset({\n context,\n selectionPoint,\n })\n}\n"],"names":["getSelectionEndPoint","selection","backward","anchor","focus","getSelectionStartPoint","isSelectionCollapsed","path","join","offset","selectionPointToBlockOffset","context","selectionPoint","length","isKeyedSegment","_key","childSelectionPointToBlockOffset"],"mappings":";;AAKO,SAASA,qBAMdC,WAAoD;AACpD,SAAKA,YAKHA,UAAUC,WAAWD,UAAUE,SAASF,UAAUG,QAJ3C;AAMX;ACdO,SAASC,uBAMdJ,WAAoD;AACpD,SAAKA,YAKHA,UAAUC,WAAWD,UAAUG,QAAQH,UAAUE,SAJ1C;AAMX;ACdO,SAASG,qBAAqBL,WAA4B;AAC/D,SAAKA,YAKHA,UAAUE,OAAOI,KAAKC,KAAAA,MAAWP,UAAUG,MAAMG,KAAKC,KAAAA,KACtDP,UAAUE,OAAOM,WAAWR,UAAUG,MAAMK,SALrC;AAOX;ACLO,SAASC,4BAA4B;AAAA,EAC1CC;AAAAA,EACAC;AAIF,GAA4B;AAExBA,SAAAA,eAAeL,KAAKM,WAAW,KAC/BC,eAAeF,eAAeL,KAAK,CAAC,CAAC,IAE9B;AAAA,IACLA,MAAM,CAAC;AAAA,MAACQ,MAAMH,eAAeL,KAAK,CAAC,EAAEQ;AAAAA,IAAAA,CAAK;AAAA,IAC1CN,QAAQG,eAAeH;AAAAA,MAIpBO,iCAAiC;AAAA,IACtCL;AAAAA,IACAC;AAAAA,EAAAA,CACD;AACH;"}
|
|
@@ -306,25 +306,6 @@ function spanSelectionPointToBlockOffset({
|
|
|
306
306
|
}
|
|
307
307
|
}
|
|
308
308
|
}
|
|
309
|
-
function getBlockEndPoint({
|
|
310
|
-
context,
|
|
311
|
-
block
|
|
312
|
-
}) {
|
|
313
|
-
if (isTextBlock(context, block.node)) {
|
|
314
|
-
const lastChild = block.node.children[block.node.children.length - 1];
|
|
315
|
-
if (lastChild)
|
|
316
|
-
return {
|
|
317
|
-
path: [...block.path, "children", {
|
|
318
|
-
_key: lastChild._key
|
|
319
|
-
}],
|
|
320
|
-
offset: isSpan$1(context, lastChild) ? lastChild.text.length : 0
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
return {
|
|
324
|
-
path: block.path,
|
|
325
|
-
offset: 0
|
|
326
|
-
};
|
|
327
|
-
}
|
|
328
309
|
function getBlockStartPoint({
|
|
329
310
|
context,
|
|
330
311
|
block
|
|
@@ -342,15 +323,6 @@ function getBlockStartPoint({
|
|
|
342
323
|
function getTextBlockText(block) {
|
|
343
324
|
return block.children.map((child) => child.text ?? "").join("");
|
|
344
325
|
}
|
|
345
|
-
function isEmptyTextBlock(context, block) {
|
|
346
|
-
if (!isTextBlock(context, block))
|
|
347
|
-
return !1;
|
|
348
|
-
const onlyText = block.children.every((child) => isSpan$1(context, child)), blockText = getTextBlockText(block);
|
|
349
|
-
return onlyText && blockText === "";
|
|
350
|
-
}
|
|
351
|
-
function isEqualSelectionPoints(a, b) {
|
|
352
|
-
return a.offset === b.offset && JSON.stringify(a.path) === JSON.stringify(b.path);
|
|
353
|
-
}
|
|
354
326
|
function isSpan(context, child) {
|
|
355
327
|
return child._type === context.schema.span.name;
|
|
356
328
|
}
|
|
@@ -455,11 +427,8 @@ function sliceBlocks({
|
|
|
455
427
|
}
|
|
456
428
|
export {
|
|
457
429
|
blockOffsetToSpanSelectionPoint,
|
|
458
|
-
getBlockEndPoint,
|
|
459
430
|
getBlockStartPoint,
|
|
460
431
|
getTextBlockText,
|
|
461
|
-
isEmptyTextBlock,
|
|
462
|
-
isEqualSelectionPoints,
|
|
463
432
|
isKeyedSegment,
|
|
464
433
|
isListBlock,
|
|
465
434
|
isSpan,
|