@portabletext/editor 1.34.0 → 1.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/_chunks-cjs/behavior.core.cjs +57 -118
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/behavior.markdown.cjs +27 -67
- package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
- package/lib/_chunks-cjs/plugin.event-listener.cjs +53 -71
- package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.get-text-before.cjs +5 -7
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-active-style.cjs +22 -36
- package/lib/_chunks-cjs/selector.is-active-style.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs +68 -153
- package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs.map +1 -1
- package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs.map +1 -1
- package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js +57 -118
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/behavior.markdown.js +27 -67
- package/lib/_chunks-es/behavior.markdown.js.map +1 -1
- package/lib/_chunks-es/plugin.event-listener.js +53 -71
- package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
- package/lib/_chunks-es/selector.get-text-before.js +5 -7
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
- package/lib/_chunks-es/selector.is-active-style.js +22 -36
- package/lib/_chunks-es/selector.is-active-style.js.map +1 -1
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js +68 -153
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
- package/lib/_chunks-es/util.block-offsets-to-selection.js.map +1 -1
- package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
- package/lib/behaviors/index.cjs +18 -48
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +28 -16
- package/lib/behaviors/index.d.ts +28 -16
- package/lib/behaviors/index.js +18 -48
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.d.cts +132 -71
- package/lib/index.d.ts +132 -71
- package/lib/plugins/index.cjs +182 -186
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +147 -82
- package/lib/plugins/index.d.ts +147 -82
- package/lib/plugins/index.js +182 -186
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.cjs +22 -50
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +9 -200
- package/lib/selectors/index.d.ts +9 -200
- package/lib/selectors/index.js +22 -50
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.cjs.map +1 -1
- package/lib/utils/index.d.cts +15 -7
- package/lib/utils/index.d.ts +15 -7
- package/lib/utils/index.js.map +1 -1
- package/package.json +6 -6
- package/src/behaviors/behavior.code-editor.ts +6 -6
- package/src/behaviors/behavior.core.annotations.ts +5 -4
- package/src/behaviors/behavior.core.block-objects.ts +17 -17
- package/src/behaviors/behavior.core.decorators.ts +12 -8
- package/src/behaviors/behavior.core.insert-break.ts +27 -29
- package/src/behaviors/behavior.core.lists.ts +19 -19
- package/src/behaviors/behavior.decorator-pair.ts +200 -0
- package/src/behaviors/behavior.default.ts +35 -30
- package/src/behaviors/behavior.emoji-picker.ts +12 -12
- package/src/behaviors/behavior.links.ts +7 -7
- package/src/behaviors/behavior.markdown.ts +41 -42
- package/src/behaviors/behavior.types.ts +15 -18
- package/src/behaviors/index.ts +0 -1
- package/src/converters/converter.json.ts +6 -6
- package/src/converters/converter.portable-text.deserialize.test.ts +28 -26
- package/src/converters/converter.portable-text.ts +6 -6
- package/src/converters/converter.text-html.deserialize.test.ts +17 -15
- package/src/converters/converter.text-html.serialize.test.ts +57 -53
- package/src/converters/converter.text-html.ts +14 -10
- package/src/converters/converter.text-plain.test.ts +17 -15
- package/src/converters/converter.text-plain.ts +15 -11
- package/src/converters/converter.types.ts +8 -7
- package/src/editor/editor-machine.ts +6 -1
- package/src/editor/plugins/create-with-event-listeners.ts +0 -5
- package/src/index.ts +3 -3
- package/src/internal-utils/get-text-to-emphasize.ts +29 -7
- package/src/plugins/plugin.decorator-shortcut.ts +235 -0
- package/src/plugins/plugin.markdown.tsx +56 -8
- package/src/plugins/plugin.one-line.tsx +17 -17
- package/src/selectors/selector.get-active-list-item.ts +4 -4
- package/src/selectors/selector.get-active-style.ts +6 -6
- package/src/selectors/selector.get-anchor-block.ts +5 -5
- package/src/selectors/selector.get-anchor-child.ts +5 -5
- package/src/selectors/selector.get-anchor-span.ts +2 -2
- package/src/selectors/selector.get-anchor-text-block.ts +2 -2
- package/src/selectors/selector.get-block-offsets.ts +8 -7
- package/src/selectors/selector.get-caret-word-selection.ts +19 -16
- package/src/selectors/selector.get-next-inline-object.ts +4 -4
- package/src/selectors/selector.get-previous-inline-object.ts +4 -4
- package/src/selectors/selector.get-selected-slice.ts +7 -4
- package/src/selectors/selector.get-selected-spans.ts +9 -9
- package/src/selectors/selector.get-selection-end-point.ts +5 -5
- package/src/selectors/selector.get-selection-start-point.ts +5 -5
- package/src/selectors/selector.get-selection-text.ts +2 -2
- package/src/selectors/selector.get-selection.ts +2 -2
- package/src/selectors/selector.get-text-before.ts +8 -8
- package/src/selectors/selector.get-trimmed-selection.ts +15 -13
- package/src/selectors/selector.get-value.ts +4 -4
- package/src/selectors/selector.is-at-the-end-of-block.ts +6 -3
- package/src/selectors/selector.is-at-the-start-of-block.ts +3 -3
- package/src/selectors/selector.is-overlapping-selection.ts +8 -6
- package/src/selectors/selector.is-selection-collapsed.ts +6 -5
- package/src/selectors/selector.is-selection-expanded.ts +2 -2
- package/src/selectors/selectors.ts +59 -59
- package/src/types/block-offset.ts +9 -0
- package/src/utils/index.ts +0 -1
- package/src/utils/util.block-offset.ts +1 -1
- package/src/utils/util.block-offsets-to-selection.ts +1 -1
- package/src/utils/util.child-selection-point-to-block-offset.ts +1 -1
- package/src/behaviors/behavior.markdown-emphasis.ts +0 -437
|
@@ -17,9 +17,9 @@ export const getPreviousInlineObject: EditorSelector<
|
|
|
17
17
|
path: [KeyedSegment, 'children', KeyedSegment]
|
|
18
18
|
}
|
|
19
19
|
| undefined
|
|
20
|
-
> = (
|
|
21
|
-
const focusTextBlock = getFocusTextBlock(
|
|
22
|
-
const selectionStartPoint = getSelectionStartPoint(
|
|
20
|
+
> = (snapshot) => {
|
|
21
|
+
const focusTextBlock = getFocusTextBlock(snapshot)
|
|
22
|
+
const selectionStartPoint = getSelectionStartPoint(snapshot)
|
|
23
23
|
const selectionStartPointChildKey =
|
|
24
24
|
selectionStartPoint && isKeySegment(selectionStartPoint.path[2])
|
|
25
25
|
? selectionStartPoint.path[2]._key
|
|
@@ -41,7 +41,7 @@ export const getPreviousInlineObject: EditorSelector<
|
|
|
41
41
|
break
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
if (!isSpan(context, child)) {
|
|
44
|
+
if (!isSpan(snapshot.context, child)) {
|
|
45
45
|
inlineObject = {
|
|
46
46
|
node: child,
|
|
47
47
|
path: [...focusTextBlock.path, 'children', {_key: child._key}],
|
|
@@ -5,8 +5,11 @@ import {sliceBlocks} from '../utils'
|
|
|
5
5
|
/**
|
|
6
6
|
* @public
|
|
7
7
|
*/
|
|
8
|
-
export const getSelectedSlice: EditorSelector<Array<PortableTextBlock>> = (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return sliceBlocks({
|
|
8
|
+
export const getSelectedSlice: EditorSelector<Array<PortableTextBlock>> = (
|
|
9
|
+
snapshot,
|
|
10
|
+
) => {
|
|
11
|
+
return sliceBlocks({
|
|
12
|
+
blocks: snapshot.context.value,
|
|
13
|
+
selection: snapshot.context.selection,
|
|
14
|
+
})
|
|
12
15
|
}
|
|
@@ -15,8 +15,8 @@ export const getSelectedSpans: EditorSelector<
|
|
|
15
15
|
node: PortableTextSpan
|
|
16
16
|
path: [KeyedSegment, 'children', KeyedSegment]
|
|
17
17
|
}>
|
|
18
|
-
> = (
|
|
19
|
-
if (!context.selection) {
|
|
18
|
+
> = (snapshot) => {
|
|
19
|
+
if (!snapshot.context.selection) {
|
|
20
20
|
return []
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -25,12 +25,12 @@ export const getSelectedSpans: EditorSelector<
|
|
|
25
25
|
path: [KeyedSegment, 'children', KeyedSegment]
|
|
26
26
|
}> = []
|
|
27
27
|
|
|
28
|
-
const startPoint = context.selection.backward
|
|
29
|
-
? context.selection.focus
|
|
30
|
-
: context.selection.anchor
|
|
31
|
-
const endPoint = context.selection.backward
|
|
32
|
-
? context.selection.anchor
|
|
33
|
-
: context.selection.focus
|
|
28
|
+
const startPoint = snapshot.context.selection.backward
|
|
29
|
+
? snapshot.context.selection.focus
|
|
30
|
+
: snapshot.context.selection.anchor
|
|
31
|
+
const endPoint = snapshot.context.selection.backward
|
|
32
|
+
? snapshot.context.selection.anchor
|
|
33
|
+
: snapshot.context.selection.focus
|
|
34
34
|
|
|
35
35
|
const startBlockKey = isKeySegment(startPoint.path[0])
|
|
36
36
|
? startPoint.path[0]._key
|
|
@@ -50,7 +50,7 @@ export const getSelectedSpans: EditorSelector<
|
|
|
50
50
|
? endPoint.path[2]._key
|
|
51
51
|
: undefined
|
|
52
52
|
|
|
53
|
-
for (const block of context.value) {
|
|
53
|
+
for (const block of snapshot.context.value) {
|
|
54
54
|
if (!isPortableTextTextBlock(block)) {
|
|
55
55
|
continue
|
|
56
56
|
}
|
|
@@ -6,12 +6,12 @@ import type {EditorSelectionPoint} from '../utils'
|
|
|
6
6
|
*/
|
|
7
7
|
export const getSelectionEndPoint: EditorSelector<
|
|
8
8
|
EditorSelectionPoint | undefined
|
|
9
|
-
> = (
|
|
10
|
-
if (!context.selection) {
|
|
9
|
+
> = (snapshot) => {
|
|
10
|
+
if (!snapshot.context.selection) {
|
|
11
11
|
return undefined
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
return context.selection.backward
|
|
15
|
-
? context.selection.anchor
|
|
16
|
-
: context.selection.focus
|
|
14
|
+
return snapshot.context.selection.backward
|
|
15
|
+
? snapshot.context.selection.anchor
|
|
16
|
+
: snapshot.context.selection.focus
|
|
17
17
|
}
|
|
@@ -6,12 +6,12 @@ import type {EditorSelectionPoint} from '../utils'
|
|
|
6
6
|
*/
|
|
7
7
|
export const getSelectionStartPoint: EditorSelector<
|
|
8
8
|
EditorSelectionPoint | undefined
|
|
9
|
-
> = (
|
|
10
|
-
if (!context.selection) {
|
|
9
|
+
> = (snapshot) => {
|
|
10
|
+
if (!snapshot.context.selection) {
|
|
11
11
|
return undefined
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
return context.selection.backward
|
|
15
|
-
? context.selection.focus
|
|
16
|
-
: context.selection.anchor
|
|
14
|
+
return snapshot.context.selection.backward
|
|
15
|
+
? snapshot.context.selection.focus
|
|
16
|
+
: snapshot.context.selection.anchor
|
|
17
17
|
}
|
|
@@ -5,8 +5,8 @@ import {getSelectedSlice} from './selector.get-selected-slice'
|
|
|
5
5
|
/**
|
|
6
6
|
* @public
|
|
7
7
|
*/
|
|
8
|
-
export const getSelectionText: EditorSelector<string> = (
|
|
9
|
-
const selectedSlice = getSelectedSlice(
|
|
8
|
+
export const getSelectionText: EditorSelector<string> = (snapshot) => {
|
|
9
|
+
const selectedSlice = getSelectedSlice(snapshot)
|
|
10
10
|
|
|
11
11
|
return selectedSlice.reduce((text, block) => {
|
|
12
12
|
if (!isPortableTextTextBlock(block)) {
|
|
@@ -3,6 +3,6 @@ import type {EditorSelection, EditorSelector} from './_exports'
|
|
|
3
3
|
/**
|
|
4
4
|
* @public
|
|
5
5
|
*/
|
|
6
|
-
export const getSelection: EditorSelector<EditorSelection> = (
|
|
7
|
-
return context.selection
|
|
6
|
+
export const getSelection: EditorSelector<EditorSelection> = (snapshot) => {
|
|
7
|
+
return snapshot.context.selection
|
|
8
8
|
}
|
|
@@ -7,19 +7,19 @@ import {getSelectionText} from './selector.get-selection-text'
|
|
|
7
7
|
/**
|
|
8
8
|
* @public
|
|
9
9
|
*/
|
|
10
|
-
export const getBlockTextBefore: EditorSelector<string> = (
|
|
11
|
-
if (!context.selection) {
|
|
10
|
+
export const getBlockTextBefore: EditorSelector<string> = (snapshot) => {
|
|
11
|
+
if (!snapshot.context.selection) {
|
|
12
12
|
return ''
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
const selection = context.selection.backward
|
|
16
|
-
? reverseSelection(context.selection)
|
|
17
|
-
: context.selection
|
|
15
|
+
const selection = snapshot.context.selection.backward
|
|
16
|
+
? reverseSelection(snapshot.context.selection)
|
|
17
|
+
: snapshot.context.selection
|
|
18
18
|
const point = selection.anchor
|
|
19
19
|
const key = isKeyedSegment(point.path[0]) ? point.path[0]._key : undefined
|
|
20
20
|
|
|
21
21
|
const block = key
|
|
22
|
-
? context.value.find((block) => block._key === key)
|
|
22
|
+
? snapshot.context.value.find((block) => block._key === key)
|
|
23
23
|
: undefined
|
|
24
24
|
|
|
25
25
|
if (!block) {
|
|
@@ -32,9 +32,9 @@ export const getBlockTextBefore: EditorSelector<string> = ({context}) => {
|
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
return getSelectionText({
|
|
35
|
+
...snapshot,
|
|
35
36
|
context: {
|
|
36
|
-
...context,
|
|
37
|
-
value: context.value,
|
|
37
|
+
...snapshot.context,
|
|
38
38
|
selection: {
|
|
39
39
|
anchor: startOfBlock,
|
|
40
40
|
focus: point,
|
|
@@ -14,18 +14,18 @@ import {getFocusTextBlock} from './selectors'
|
|
|
14
14
|
/**
|
|
15
15
|
* @public
|
|
16
16
|
*/
|
|
17
|
-
export const getTrimmedSelection: EditorSelector<EditorSelection> = (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (!context.selection) {
|
|
21
|
-
return context.selection
|
|
17
|
+
export const getTrimmedSelection: EditorSelector<EditorSelection> = (
|
|
18
|
+
snapshot,
|
|
19
|
+
) => {
|
|
20
|
+
if (!snapshot.context.selection) {
|
|
21
|
+
return snapshot.context.selection
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
const startPoint = getSelectionStartPoint(
|
|
25
|
-
const endPoint = getSelectionEndPoint(
|
|
24
|
+
const startPoint = getSelectionStartPoint(snapshot)
|
|
25
|
+
const endPoint = getSelectionEndPoint(snapshot)
|
|
26
26
|
|
|
27
27
|
if (!startPoint || !endPoint) {
|
|
28
|
-
return context.selection
|
|
28
|
+
return snapshot.context.selection
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
const startBlockKey = isKeyedSegment(startPoint.path[0])
|
|
@@ -42,7 +42,7 @@ export const getTrimmedSelection: EditorSelector<EditorSelection> = ({
|
|
|
42
42
|
: null
|
|
43
43
|
|
|
44
44
|
if (!startBlockKey || !endBlockKey) {
|
|
45
|
-
return context.selection
|
|
45
|
+
return snapshot.context.selection
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
let startBlockFound = false
|
|
@@ -54,7 +54,7 @@ export const getTrimmedSelection: EditorSelector<EditorSelection> = ({
|
|
|
54
54
|
| {blockKey: string; span: PortableTextSpan}
|
|
55
55
|
| undefined
|
|
56
56
|
|
|
57
|
-
for (const block of context.value) {
|
|
57
|
+
for (const block of snapshot.context.value) {
|
|
58
58
|
if (block._key === startBlockKey) {
|
|
59
59
|
startBlockFound = true
|
|
60
60
|
|
|
@@ -140,7 +140,7 @@ export const getTrimmedSelection: EditorSelector<EditorSelection> = ({
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
const trimmedSelection = context.selection.backward
|
|
143
|
+
const trimmedSelection = snapshot.context.selection.backward
|
|
144
144
|
? {
|
|
145
145
|
anchor: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,
|
|
146
146
|
focus: adjustedStartPoint ?? startPoint,
|
|
@@ -153,15 +153,17 @@ export const getTrimmedSelection: EditorSelector<EditorSelection> = ({
|
|
|
153
153
|
|
|
154
154
|
if (
|
|
155
155
|
isSelectionCollapsed({
|
|
156
|
+
...snapshot,
|
|
156
157
|
context: {
|
|
157
|
-
...context,
|
|
158
|
+
...snapshot.context,
|
|
158
159
|
selection: trimmedSelection,
|
|
159
160
|
},
|
|
160
161
|
})
|
|
161
162
|
) {
|
|
162
163
|
const focusTextBlock = getFocusTextBlock({
|
|
164
|
+
...snapshot,
|
|
163
165
|
context: {
|
|
164
|
-
...context,
|
|
166
|
+
...snapshot.context,
|
|
165
167
|
selection: trimmedSelection,
|
|
166
168
|
},
|
|
167
169
|
})
|
|
@@ -4,8 +4,8 @@ import type {EditorSelector} from './_exports'
|
|
|
4
4
|
/**
|
|
5
5
|
* @public
|
|
6
6
|
*/
|
|
7
|
-
export const getValue: EditorSelector<Array<PortableTextBlock>> = (
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return context.value
|
|
7
|
+
export const getValue: EditorSelector<Array<PortableTextBlock>> = (
|
|
8
|
+
snapshot,
|
|
9
|
+
) => {
|
|
10
|
+
return snapshot.context.value
|
|
11
11
|
}
|
|
@@ -10,13 +10,16 @@ export function isAtTheEndOfBlock(block: {
|
|
|
10
10
|
node: PortableTextBlock
|
|
11
11
|
path: [KeyedSegment]
|
|
12
12
|
}): EditorSelector<boolean> {
|
|
13
|
-
return (
|
|
14
|
-
if (!context.selection || !isSelectionCollapsed(
|
|
13
|
+
return (snapshot) => {
|
|
14
|
+
if (!snapshot.context.selection || !isSelectionCollapsed(snapshot)) {
|
|
15
15
|
return false
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const blockEndPoint = utils.getBlockEndPoint(block)
|
|
19
19
|
|
|
20
|
-
return utils.isEqualSelectionPoints(
|
|
20
|
+
return utils.isEqualSelectionPoints(
|
|
21
|
+
snapshot.context.selection.focus,
|
|
22
|
+
blockEndPoint,
|
|
23
|
+
)
|
|
21
24
|
}
|
|
22
25
|
}
|
|
@@ -10,15 +10,15 @@ export function isAtTheStartOfBlock(block: {
|
|
|
10
10
|
node: PortableTextBlock
|
|
11
11
|
path: [KeyedSegment]
|
|
12
12
|
}): EditorSelector<boolean> {
|
|
13
|
-
return (
|
|
14
|
-
if (!context.selection || !isSelectionCollapsed(
|
|
13
|
+
return (snapshot) => {
|
|
14
|
+
if (!snapshot.context.selection || !isSelectionCollapsed(snapshot)) {
|
|
15
15
|
return false
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const blockStartPoint = utils.getBlockStartPoint(block)
|
|
19
19
|
|
|
20
20
|
return utils.isEqualSelectionPoints(
|
|
21
|
-
context.selection.focus,
|
|
21
|
+
snapshot.context.selection.focus,
|
|
22
22
|
blockStartPoint,
|
|
23
23
|
)
|
|
24
24
|
}
|
|
@@ -11,20 +11,22 @@ import {isPointBeforeSelection} from './selector.is-point-before-selection'
|
|
|
11
11
|
export function isOverlappingSelection(
|
|
12
12
|
selection: EditorSelection,
|
|
13
13
|
): EditorSelector<boolean> {
|
|
14
|
-
return (
|
|
15
|
-
if (!selection || !context.selection) {
|
|
14
|
+
return (snapshot) => {
|
|
15
|
+
if (!selection || !snapshot.context.selection) {
|
|
16
16
|
return false
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
const selectionStartPoint = getSelectionStartPoint({
|
|
20
|
+
...snapshot,
|
|
20
21
|
context: {
|
|
21
|
-
...context,
|
|
22
|
+
...snapshot.context,
|
|
22
23
|
selection,
|
|
23
24
|
},
|
|
24
25
|
})
|
|
25
26
|
const selectionEndPoint = getSelectionEndPoint({
|
|
27
|
+
...snapshot,
|
|
26
28
|
context: {
|
|
27
|
-
...context,
|
|
29
|
+
...snapshot.context,
|
|
28
30
|
selection,
|
|
29
31
|
},
|
|
30
32
|
})
|
|
@@ -33,11 +35,11 @@ export function isOverlappingSelection(
|
|
|
33
35
|
return false
|
|
34
36
|
}
|
|
35
37
|
|
|
36
|
-
if (!isPointAfterSelection(selectionStartPoint)(
|
|
38
|
+
if (!isPointAfterSelection(selectionStartPoint)(snapshot)) {
|
|
37
39
|
return false
|
|
38
40
|
}
|
|
39
41
|
|
|
40
|
-
if (!isPointBeforeSelection(selectionEndPoint)(
|
|
42
|
+
if (!isPointBeforeSelection(selectionEndPoint)(snapshot)) {
|
|
41
43
|
return false
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -3,14 +3,15 @@ import type {EditorSelector} from '../editor/editor-selector'
|
|
|
3
3
|
/**
|
|
4
4
|
* @public
|
|
5
5
|
*/
|
|
6
|
-
export const isSelectionCollapsed: EditorSelector<boolean> = (
|
|
7
|
-
if (!context.selection) {
|
|
6
|
+
export const isSelectionCollapsed: EditorSelector<boolean> = (snapshot) => {
|
|
7
|
+
if (!snapshot.context.selection) {
|
|
8
8
|
return false
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
return (
|
|
12
|
-
JSON.stringify(context.selection.anchor.path) ===
|
|
13
|
-
JSON.stringify(context.selection.focus.path) &&
|
|
14
|
-
context.selection?.anchor.offset ===
|
|
12
|
+
JSON.stringify(snapshot.context.selection.anchor.path) ===
|
|
13
|
+
JSON.stringify(snapshot.context.selection.focus.path) &&
|
|
14
|
+
snapshot.context.selection?.anchor.offset ===
|
|
15
|
+
snapshot.context.selection?.focus.offset
|
|
15
16
|
)
|
|
16
17
|
}
|
|
@@ -4,6 +4,6 @@ import {isSelectionCollapsed} from './selector.is-selection-collapsed'
|
|
|
4
4
|
/**
|
|
5
5
|
* @public
|
|
6
6
|
*/
|
|
7
|
-
export const isSelectionExpanded: EditorSelector<boolean> = (
|
|
8
|
-
return !isSelectionCollapsed(
|
|
7
|
+
export const isSelectionExpanded: EditorSelector<boolean> = (snapshot) => {
|
|
8
|
+
return !isSelectionCollapsed(snapshot)
|
|
9
9
|
}
|
|
@@ -17,15 +17,15 @@ import type {EditorSelector} from '../editor/editor-selector'
|
|
|
17
17
|
*/
|
|
18
18
|
export const getFocusBlock: EditorSelector<
|
|
19
19
|
{node: PortableTextBlock; path: [KeyedSegment]} | undefined
|
|
20
|
-
> = (
|
|
21
|
-
const key = context.selection
|
|
22
|
-
? isKeySegment(context.selection.focus.path[0])
|
|
23
|
-
? context.selection.focus.path[0]._key
|
|
20
|
+
> = (snapshot) => {
|
|
21
|
+
const key = snapshot.context.selection
|
|
22
|
+
? isKeySegment(snapshot.context.selection.focus.path[0])
|
|
23
|
+
? snapshot.context.selection.focus.path[0]._key
|
|
24
24
|
: undefined
|
|
25
25
|
: undefined
|
|
26
26
|
|
|
27
27
|
const node = key
|
|
28
|
-
? context.value.find((block) => block._key === key)
|
|
28
|
+
? snapshot.context.value.find((block) => block._key === key)
|
|
29
29
|
: undefined
|
|
30
30
|
|
|
31
31
|
return node && key ? {node, path: [{_key: key}]} : undefined
|
|
@@ -36,9 +36,9 @@ export const getFocusBlock: EditorSelector<
|
|
|
36
36
|
*/
|
|
37
37
|
export const getFocusListBlock: EditorSelector<
|
|
38
38
|
{node: PortableTextListBlock; path: [KeyedSegment]} | undefined
|
|
39
|
-
> = (
|
|
40
|
-
const guards = createGuards(context)
|
|
41
|
-
const focusBlock = getFocusBlock(
|
|
39
|
+
> = (snapshot) => {
|
|
40
|
+
const guards = createGuards(snapshot.context)
|
|
41
|
+
const focusBlock = getFocusBlock(snapshot)
|
|
42
42
|
|
|
43
43
|
return focusBlock && guards.isListBlock(focusBlock.node)
|
|
44
44
|
? {node: focusBlock.node, path: focusBlock.path}
|
|
@@ -50,8 +50,8 @@ export const getFocusListBlock: EditorSelector<
|
|
|
50
50
|
*/
|
|
51
51
|
export const getFocusTextBlock: EditorSelector<
|
|
52
52
|
{node: PortableTextTextBlock; path: [KeyedSegment]} | undefined
|
|
53
|
-
> = (
|
|
54
|
-
const focusBlock = getFocusBlock(
|
|
53
|
+
> = (snapshot) => {
|
|
54
|
+
const focusBlock = getFocusBlock(snapshot)
|
|
55
55
|
|
|
56
56
|
return focusBlock && isPortableTextTextBlock(focusBlock.node)
|
|
57
57
|
? {node: focusBlock.node, path: focusBlock.path}
|
|
@@ -63,8 +63,8 @@ export const getFocusTextBlock: EditorSelector<
|
|
|
63
63
|
*/
|
|
64
64
|
export const getFocusBlockObject: EditorSelector<
|
|
65
65
|
{node: PortableTextObject; path: [KeyedSegment]} | undefined
|
|
66
|
-
> = (
|
|
67
|
-
const focusBlock = getFocusBlock(
|
|
66
|
+
> = (snapshot) => {
|
|
67
|
+
const focusBlock = getFocusBlock(snapshot)
|
|
68
68
|
|
|
69
69
|
return focusBlock && !isPortableTextTextBlock(focusBlock.node)
|
|
70
70
|
? {node: focusBlock.node, path: focusBlock.path}
|
|
@@ -80,16 +80,16 @@ export const getFocusChild: EditorSelector<
|
|
|
80
80
|
path: [KeyedSegment, 'children', KeyedSegment]
|
|
81
81
|
}
|
|
82
82
|
| undefined
|
|
83
|
-
> = (
|
|
84
|
-
const focusBlock = getFocusTextBlock(
|
|
83
|
+
> = (snapshot) => {
|
|
84
|
+
const focusBlock = getFocusTextBlock(snapshot)
|
|
85
85
|
|
|
86
86
|
if (!focusBlock) {
|
|
87
87
|
return undefined
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
const key = context.selection
|
|
91
|
-
? isKeySegment(context.selection.focus.path[2])
|
|
92
|
-
? context.selection.focus.path[2]._key
|
|
90
|
+
const key = snapshot.context.selection
|
|
91
|
+
? isKeySegment(snapshot.context.selection.focus.path[2])
|
|
92
|
+
? snapshot.context.selection.focus.path[2]._key
|
|
93
93
|
: undefined
|
|
94
94
|
: undefined
|
|
95
95
|
|
|
@@ -108,8 +108,8 @@ export const getFocusChild: EditorSelector<
|
|
|
108
108
|
export const getFocusSpan: EditorSelector<
|
|
109
109
|
| {node: PortableTextSpan; path: [KeyedSegment, 'children', KeyedSegment]}
|
|
110
110
|
| undefined
|
|
111
|
-
> = (
|
|
112
|
-
const focusChild = getFocusChild(
|
|
111
|
+
> = (snapshot) => {
|
|
112
|
+
const focusChild = getFocusChild(snapshot)
|
|
113
113
|
|
|
114
114
|
return focusChild && isPortableTextSpan(focusChild.node)
|
|
115
115
|
? {node: focusChild.node, path: focusChild.path}
|
|
@@ -121,8 +121,8 @@ export const getFocusSpan: EditorSelector<
|
|
|
121
121
|
*/
|
|
122
122
|
export const getFirstBlock: EditorSelector<
|
|
123
123
|
{node: PortableTextBlock; path: [KeyedSegment]} | undefined
|
|
124
|
-
> = (
|
|
125
|
-
const node = context.value[0]
|
|
124
|
+
> = (snapshot) => {
|
|
125
|
+
const node = snapshot.context.value[0]
|
|
126
126
|
|
|
127
127
|
return node ? {node, path: [{_key: node._key}]} : undefined
|
|
128
128
|
}
|
|
@@ -132,9 +132,9 @@ export const getFirstBlock: EditorSelector<
|
|
|
132
132
|
*/
|
|
133
133
|
export const getLastBlock: EditorSelector<
|
|
134
134
|
{node: PortableTextBlock; path: [KeyedSegment]} | undefined
|
|
135
|
-
> = (
|
|
136
|
-
const node = context.value[context.value.length - 1]
|
|
137
|
-
? context.value[context.value.length - 1]
|
|
135
|
+
> = (snapshot) => {
|
|
136
|
+
const node = snapshot.context.value[snapshot.context.value.length - 1]
|
|
137
|
+
? snapshot.context.value[snapshot.context.value.length - 1]
|
|
138
138
|
: undefined
|
|
139
139
|
|
|
140
140
|
return node ? {node, path: [{_key: node._key}]} : undefined
|
|
@@ -145,33 +145,33 @@ export const getLastBlock: EditorSelector<
|
|
|
145
145
|
*/
|
|
146
146
|
export const getSelectedBlocks: EditorSelector<
|
|
147
147
|
Array<{node: PortableTextBlock; path: [KeyedSegment]}>
|
|
148
|
-
> = (
|
|
149
|
-
if (!context.selection) {
|
|
148
|
+
> = (snapshot) => {
|
|
149
|
+
if (!snapshot.context.selection) {
|
|
150
150
|
return []
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
const selectedBlocks: Array<{node: PortableTextBlock; path: [KeyedSegment]}> =
|
|
154
154
|
[]
|
|
155
|
-
const startKey = context.selection.backward
|
|
156
|
-
? isKeySegment(context.selection.focus.path[0])
|
|
157
|
-
? context.selection.focus.path[0]._key
|
|
155
|
+
const startKey = snapshot.context.selection.backward
|
|
156
|
+
? isKeySegment(snapshot.context.selection.focus.path[0])
|
|
157
|
+
? snapshot.context.selection.focus.path[0]._key
|
|
158
158
|
: undefined
|
|
159
|
-
: isKeySegment(context.selection.anchor.path[0])
|
|
160
|
-
? context.selection.anchor.path[0]._key
|
|
159
|
+
: isKeySegment(snapshot.context.selection.anchor.path[0])
|
|
160
|
+
? snapshot.context.selection.anchor.path[0]._key
|
|
161
161
|
: undefined
|
|
162
|
-
const endKey = context.selection.backward
|
|
163
|
-
? isKeySegment(context.selection.anchor.path[0])
|
|
164
|
-
? context.selection.anchor.path[0]._key
|
|
162
|
+
const endKey = snapshot.context.selection.backward
|
|
163
|
+
? isKeySegment(snapshot.context.selection.anchor.path[0])
|
|
164
|
+
? snapshot.context.selection.anchor.path[0]._key
|
|
165
165
|
: undefined
|
|
166
|
-
: isKeySegment(context.selection.focus.path[0])
|
|
167
|
-
? context.selection.focus.path[0]._key
|
|
166
|
+
: isKeySegment(snapshot.context.selection.focus.path[0])
|
|
167
|
+
? snapshot.context.selection.focus.path[0]._key
|
|
168
168
|
: undefined
|
|
169
169
|
|
|
170
170
|
if (!startKey || !endKey) {
|
|
171
171
|
return selectedBlocks
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
for (const block of context.value) {
|
|
174
|
+
for (const block of snapshot.context.value) {
|
|
175
175
|
if (block._key === startKey) {
|
|
176
176
|
selectedBlocks.push({node: block, path: [{_key: block._key}]})
|
|
177
177
|
|
|
@@ -203,21 +203,21 @@ export const getSelectionStartBlock: EditorSelector<
|
|
|
203
203
|
path: [KeyedSegment]
|
|
204
204
|
}
|
|
205
205
|
| undefined
|
|
206
|
-
> = (
|
|
207
|
-
if (!context.selection) {
|
|
206
|
+
> = (snapshot) => {
|
|
207
|
+
if (!snapshot.context.selection) {
|
|
208
208
|
return undefined
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
const key = context.selection.backward
|
|
212
|
-
? isKeySegment(context.selection.focus.path[0])
|
|
213
|
-
? context.selection.focus.path[0]._key
|
|
211
|
+
const key = snapshot.context.selection.backward
|
|
212
|
+
? isKeySegment(snapshot.context.selection.focus.path[0])
|
|
213
|
+
? snapshot.context.selection.focus.path[0]._key
|
|
214
214
|
: undefined
|
|
215
|
-
: isKeySegment(context.selection.anchor.path[0])
|
|
216
|
-
? context.selection.anchor.path[0]._key
|
|
215
|
+
: isKeySegment(snapshot.context.selection.anchor.path[0])
|
|
216
|
+
? snapshot.context.selection.anchor.path[0]._key
|
|
217
217
|
: undefined
|
|
218
218
|
|
|
219
219
|
const node = key
|
|
220
|
-
? context.value.find((block) => block._key === key)
|
|
220
|
+
? snapshot.context.value.find((block) => block._key === key)
|
|
221
221
|
: undefined
|
|
222
222
|
|
|
223
223
|
return node && key ? {node, path: [{_key: key}]} : undefined
|
|
@@ -232,21 +232,21 @@ export const getSelectionEndBlock: EditorSelector<
|
|
|
232
232
|
path: [KeyedSegment]
|
|
233
233
|
}
|
|
234
234
|
| undefined
|
|
235
|
-
> = (
|
|
236
|
-
if (!context.selection) {
|
|
235
|
+
> = (snapshot) => {
|
|
236
|
+
if (!snapshot.context.selection) {
|
|
237
237
|
return undefined
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
-
const key = context.selection.backward
|
|
241
|
-
? isKeySegment(context.selection.anchor.path[0])
|
|
242
|
-
? context.selection.anchor.path[0]._key
|
|
240
|
+
const key = snapshot.context.selection.backward
|
|
241
|
+
? isKeySegment(snapshot.context.selection.anchor.path[0])
|
|
242
|
+
? snapshot.context.selection.anchor.path[0]._key
|
|
243
243
|
: undefined
|
|
244
|
-
: isKeySegment(context.selection.focus.path[0])
|
|
245
|
-
? context.selection.focus.path[0]._key
|
|
244
|
+
: isKeySegment(snapshot.context.selection.focus.path[0])
|
|
245
|
+
? snapshot.context.selection.focus.path[0]._key
|
|
246
246
|
: undefined
|
|
247
247
|
|
|
248
248
|
const node = key
|
|
249
|
-
? context.value.find((block) => block._key === key)
|
|
249
|
+
? snapshot.context.value.find((block) => block._key === key)
|
|
250
250
|
: undefined
|
|
251
251
|
|
|
252
252
|
return node && key ? {node, path: [{_key: key}]} : undefined
|
|
@@ -257,9 +257,9 @@ export const getSelectionEndBlock: EditorSelector<
|
|
|
257
257
|
*/
|
|
258
258
|
export const getPreviousBlock: EditorSelector<
|
|
259
259
|
{node: PortableTextBlock; path: [KeyedSegment]} | undefined
|
|
260
|
-
> = (
|
|
260
|
+
> = (snapshot) => {
|
|
261
261
|
let previousBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined
|
|
262
|
-
const selectionStartBlock = getSelectionStartBlock(
|
|
262
|
+
const selectionStartBlock = getSelectionStartBlock(snapshot)
|
|
263
263
|
|
|
264
264
|
if (!selectionStartBlock) {
|
|
265
265
|
return undefined
|
|
@@ -267,7 +267,7 @@ export const getPreviousBlock: EditorSelector<
|
|
|
267
267
|
|
|
268
268
|
let foundSelectionStartBlock = false
|
|
269
269
|
|
|
270
|
-
for (const block of context.value) {
|
|
270
|
+
for (const block of snapshot.context.value) {
|
|
271
271
|
if (block._key === selectionStartBlock.node._key) {
|
|
272
272
|
foundSelectionStartBlock = true
|
|
273
273
|
break
|
|
@@ -288,9 +288,9 @@ export const getPreviousBlock: EditorSelector<
|
|
|
288
288
|
*/
|
|
289
289
|
export const getNextBlock: EditorSelector<
|
|
290
290
|
{node: PortableTextBlock; path: [KeyedSegment]} | undefined
|
|
291
|
-
> = (
|
|
291
|
+
> = (snapshot) => {
|
|
292
292
|
let nextBlock: {node: PortableTextBlock; path: [KeyedSegment]} | undefined
|
|
293
|
-
const selectionEndBlock = getSelectionEndBlock(
|
|
293
|
+
const selectionEndBlock = getSelectionEndBlock(snapshot)
|
|
294
294
|
|
|
295
295
|
if (!selectionEndBlock) {
|
|
296
296
|
return undefined
|
|
@@ -298,7 +298,7 @@ export const getNextBlock: EditorSelector<
|
|
|
298
298
|
|
|
299
299
|
let foundSelectionEndBlock = false
|
|
300
300
|
|
|
301
|
-
for (const block of context.value) {
|
|
301
|
+
for (const block of snapshot.context.value) {
|
|
302
302
|
if (block._key === selectionEndBlock.node._key) {
|
|
303
303
|
foundSelectionEndBlock = true
|
|
304
304
|
continue
|
package/src/utils/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
type KeyedSegment,
|
|
5
5
|
type PortableTextBlock,
|
|
6
6
|
} from '@sanity/types'
|
|
7
|
-
import type {BlockOffset} from '../
|
|
7
|
+
import type {BlockOffset} from '../types/block-offset'
|
|
8
8
|
import type {EditorSelectionPoint} from '../types/editor'
|
|
9
9
|
import {isKeyedSegment} from './util.is-keyed-segment'
|
|
10
10
|
|