@portabletext/editor 1.34.1 → 1.35.1
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 → editor-provider.cjs} +101 -87
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -0
- 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 → editor-provider.js} +102 -88
- package/lib/_chunks-es/editor-provider.js.map +1 -0
- 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 +19392 -214
- package/lib/behaviors/index.d.ts +19392 -214
- package/lib/behaviors/index.js +18 -48
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +81 -51
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +334 -59
- package/lib/index.d.ts +334 -59
- package/lib/index.js +35 -4
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs +200 -189
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +344 -25
- package/lib/plugins/index.d.ts +344 -25
- package/lib/plugins/index.js +198 -187
- 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 +19485 -174
- package/lib/selectors/index.d.ts +19485 -174
- 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 +19518 -7
- package/lib/utils/index.d.ts +19518 -7
- package/lib/utils/index.js.map +1 -1
- package/package.json +7 -7
- package/src/behavior-actions/behavior.action.decorator.add.ts +1 -0
- package/src/behavior-actions/behavior.action.delete.text.ts +1 -0
- 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 +201 -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 +14 -17
- package/src/behaviors/index.ts +0 -1
- package/src/converters/converter.json.ts +6 -6
- package/src/converters/converter.portable-text.deserialize.test.ts +27 -29
- package/src/converters/converter.portable-text.ts +13 -7
- package/src/converters/converter.text-html.deserialize.test.ts +16 -18
- package/src/converters/converter.text-html.serialize.test.ts +56 -57
- package/src/converters/converter.text-html.ts +14 -10
- package/src/converters/converter.text-plain.test.ts +17 -17
- package/src/converters/converter.text-plain.ts +15 -11
- package/src/converters/converter.types.ts +5 -5
- package/src/editor/Editable.tsx +26 -0
- package/src/editor/editor-machine.ts +170 -142
- package/src/editor/editor-selector.ts +3 -0
- package/src/editor/editor-snapshot.ts +13 -0
- package/src/editor-event-listener.tsx +30 -0
- package/src/index.ts +3 -3
- package/src/internal-utils/create-test-snapshot.ts +23 -0
- 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-annotations.test.ts +4 -13
- 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.test.ts +3 -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.test.ts +5 -9
- 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.test.ts +5 -7
- 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.test.ts +3 -5
- package/src/selectors/selector.get-trimmed-selection.ts +15 -13
- package/src/selectors/selector.get-value.ts +4 -4
- package/src/selectors/selector.is-active-decorator.test.ts +5 -9
- 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/lib/_chunks-cjs/plugin.event-listener.cjs.map +0 -1
- package/lib/_chunks-es/plugin.event-listener.js.map +0 -1
- package/src/behaviors/behavior.markdown-emphasis.ts +0 -437
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type {EditorSelector} from '../editor/editor-selector'
|
|
2
|
+
import type {BlockOffset} from '../types/block-offset'
|
|
2
3
|
import type {EditorSelection} from '../types/editor'
|
|
3
4
|
import {
|
|
4
5
|
blockOffsetToSpanSelectionPoint,
|
|
5
6
|
getBlockEndPoint,
|
|
6
7
|
getBlockStartPoint,
|
|
7
8
|
spanSelectionPointToBlockOffset,
|
|
8
|
-
type BlockOffset,
|
|
9
9
|
} from '../utils'
|
|
10
10
|
import {getNextInlineObject} from './selector.get-next-inline-object'
|
|
11
11
|
import {getPreviousInlineObject} from './selector.get-previous-inline-object'
|
|
@@ -20,22 +20,22 @@ import {getFocusTextBlock} from './selectors'
|
|
|
20
20
|
* Returns the selection of the of the word the caret is placed in.
|
|
21
21
|
* Note: Only returns a word selection if the current selection is collapsed
|
|
22
22
|
*/
|
|
23
|
-
export const getCaretWordSelection: EditorSelector<EditorSelection> = (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
if (!context.selection) {
|
|
23
|
+
export const getCaretWordSelection: EditorSelector<EditorSelection> = (
|
|
24
|
+
snapshot,
|
|
25
|
+
) => {
|
|
26
|
+
if (!snapshot.context.selection) {
|
|
27
27
|
return null
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
if (!isSelectionCollapsed(
|
|
30
|
+
if (!isSelectionCollapsed(snapshot)) {
|
|
31
31
|
return null
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
const focusTextBlock = getFocusTextBlock(
|
|
35
|
-
const selectionStartPoint = getSelectionStartPoint(
|
|
34
|
+
const focusTextBlock = getFocusTextBlock(snapshot)
|
|
35
|
+
const selectionStartPoint = getSelectionStartPoint(snapshot)
|
|
36
36
|
const selectionStartOffset = selectionStartPoint
|
|
37
37
|
? spanSelectionPointToBlockOffset({
|
|
38
|
-
value: context.value,
|
|
38
|
+
value: snapshot.context.value,
|
|
39
39
|
selectionPoint: selectionStartPoint,
|
|
40
40
|
})
|
|
41
41
|
: undefined
|
|
@@ -44,11 +44,12 @@ export const getCaretWordSelection: EditorSelector<EditorSelection> = ({
|
|
|
44
44
|
return null
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
const previousInlineObject = getPreviousInlineObject(
|
|
47
|
+
const previousInlineObject = getPreviousInlineObject(snapshot)
|
|
48
48
|
const blockStartPoint = getBlockStartPoint(focusTextBlock)
|
|
49
49
|
const textBefore = getSelectionText({
|
|
50
|
+
...snapshot,
|
|
50
51
|
context: {
|
|
51
|
-
...context,
|
|
52
|
+
...snapshot.context,
|
|
52
53
|
selection: {
|
|
53
54
|
anchor: previousInlineObject
|
|
54
55
|
? {path: previousInlineObject.path, offset: 0}
|
|
@@ -59,11 +60,12 @@ export const getCaretWordSelection: EditorSelector<EditorSelection> = ({
|
|
|
59
60
|
})
|
|
60
61
|
const textDirectlyBefore = textBefore.split(/\s+/).at(-1)
|
|
61
62
|
|
|
62
|
-
const nextInlineObject = getNextInlineObject(
|
|
63
|
+
const nextInlineObject = getNextInlineObject(snapshot)
|
|
63
64
|
const blockEndPoint = getBlockEndPoint(focusTextBlock)
|
|
64
65
|
const textAfter = getSelectionText({
|
|
66
|
+
...snapshot,
|
|
65
67
|
context: {
|
|
66
|
-
...context,
|
|
68
|
+
...snapshot.context,
|
|
67
69
|
selection: {
|
|
68
70
|
anchor: selectionStartPoint,
|
|
69
71
|
focus: nextInlineObject
|
|
@@ -95,12 +97,12 @@ export const getCaretWordSelection: EditorSelector<EditorSelection> = ({
|
|
|
95
97
|
: selectionStartOffset
|
|
96
98
|
|
|
97
99
|
const caretWordStartSelectionPoint = blockOffsetToSpanSelectionPoint({
|
|
98
|
-
value: context.value,
|
|
100
|
+
value: snapshot.context.value,
|
|
99
101
|
blockOffset: caretWordStartOffset,
|
|
100
102
|
direction: 'backward',
|
|
101
103
|
})
|
|
102
104
|
const caretWordEndSelectionPoint = blockOffsetToSpanSelectionPoint({
|
|
103
|
-
value: context.value,
|
|
105
|
+
value: snapshot.context.value,
|
|
104
106
|
blockOffset: caretWordEndOffset,
|
|
105
107
|
direction: 'forward',
|
|
106
108
|
})
|
|
@@ -115,8 +117,9 @@ export const getCaretWordSelection: EditorSelector<EditorSelection> = ({
|
|
|
115
117
|
}
|
|
116
118
|
|
|
117
119
|
return isSelectionExpanded({
|
|
120
|
+
...snapshot,
|
|
118
121
|
context: {
|
|
119
|
-
...context,
|
|
122
|
+
...snapshot.context,
|
|
120
123
|
selection: caretWordSelection,
|
|
121
124
|
},
|
|
122
125
|
})
|
|
@@ -17,9 +17,9 @@ export const getNextInlineObject: EditorSelector<
|
|
|
17
17
|
path: [KeyedSegment, 'children', KeyedSegment]
|
|
18
18
|
}
|
|
19
19
|
| undefined
|
|
20
|
-
> = (
|
|
21
|
-
const focusTextBlock = getFocusTextBlock(
|
|
22
|
-
const selectionEndPoint = getSelectionEndPoint(
|
|
20
|
+
> = (snapshot) => {
|
|
21
|
+
const focusTextBlock = getFocusTextBlock(snapshot)
|
|
22
|
+
const selectionEndPoint = getSelectionEndPoint(snapshot)
|
|
23
23
|
const selectionEndPointChildKey =
|
|
24
24
|
selectionEndPoint && isKeySegment(selectionEndPoint.path[2])
|
|
25
25
|
? selectionEndPoint.path[2]._key
|
|
@@ -43,7 +43,7 @@ export const getNextInlineObject: EditorSelector<
|
|
|
43
43
|
continue
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
if (!isSpan(context, child) && endPointChildFound) {
|
|
46
|
+
if (!isSpan(snapshot.context, child) && endPointChildFound) {
|
|
47
47
|
inlineObject = {
|
|
48
48
|
node: child,
|
|
49
49
|
path: [...focusTextBlock.path, 'children', {_key: child._key}],
|
|
@@ -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
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import {describe, expect, test} from 'vitest'
|
|
3
|
-
import {getSelectedSpans, type
|
|
4
|
-
import
|
|
3
|
+
import {getSelectedSpans, type EditorSelection} from '.'
|
|
4
|
+
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
5
5
|
|
|
6
6
|
const fooBar = {
|
|
7
7
|
_type: 'block',
|
|
@@ -40,17 +40,13 @@ describe(getSelectedSpans.name, () => {
|
|
|
40
40
|
function snapshot(
|
|
41
41
|
value: Array<PortableTextBlock>,
|
|
42
42
|
selection: EditorSelection,
|
|
43
|
-
)
|
|
44
|
-
return {
|
|
43
|
+
) {
|
|
44
|
+
return createTestSnapshot({
|
|
45
45
|
context: {
|
|
46
|
-
converters: [],
|
|
47
|
-
schema: {} as EditorSchema,
|
|
48
|
-
keyGenerator: () => '',
|
|
49
|
-
activeDecorators: [],
|
|
50
46
|
value,
|
|
51
47
|
selection,
|
|
52
48
|
},
|
|
53
|
-
}
|
|
49
|
+
})
|
|
54
50
|
}
|
|
55
51
|
|
|
56
52
|
test('selecting a single span', () => {
|
|
@@ -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
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import {expect, test} from 'vitest'
|
|
3
|
-
import type {EditorSelection
|
|
3
|
+
import type {EditorSelection} from '.'
|
|
4
4
|
import {compileSchemaDefinition, defineSchema} from '../editor/define-schema'
|
|
5
|
+
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
5
6
|
import {getSelectionText} from './selector.get-selection-text'
|
|
6
7
|
|
|
7
8
|
const brokenBlock = {
|
|
@@ -67,21 +68,18 @@ test(getSelectionText.name, () => {
|
|
|
67
68
|
function snapshot(
|
|
68
69
|
value: Array<PortableTextBlock>,
|
|
69
70
|
selection: EditorSelection,
|
|
70
|
-
)
|
|
71
|
-
return {
|
|
71
|
+
) {
|
|
72
|
+
return createTestSnapshot({
|
|
72
73
|
context: {
|
|
73
|
-
converters: [],
|
|
74
74
|
schema: compileSchemaDefinition(
|
|
75
75
|
defineSchema({
|
|
76
76
|
inlineObjects: [{name: 'stock-ticker'}],
|
|
77
77
|
}),
|
|
78
78
|
),
|
|
79
|
-
keyGenerator: () => '',
|
|
80
|
-
activeDecorators: [],
|
|
81
79
|
value,
|
|
82
80
|
selection,
|
|
83
81
|
},
|
|
84
|
-
}
|
|
82
|
+
})
|
|
85
83
|
}
|
|
86
84
|
|
|
87
85
|
expect(
|
|
@@ -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,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import {describe, expect, test} from 'vitest'
|
|
3
3
|
import {compileSchemaDefinition, defineSchema} from '../editor/define-schema'
|
|
4
|
-
import
|
|
4
|
+
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
5
5
|
import {parseBlock} from '../internal-utils/parse-blocks'
|
|
6
6
|
import {createTestKeyGenerator} from '../internal-utils/test-key-generator'
|
|
7
7
|
import type {EditorSelection} from '../types/editor'
|
|
@@ -20,10 +20,8 @@ function snapshot(
|
|
|
20
20
|
}),
|
|
21
21
|
)
|
|
22
22
|
|
|
23
|
-
return {
|
|
23
|
+
return createTestSnapshot({
|
|
24
24
|
context: {
|
|
25
|
-
activeDecorators: [],
|
|
26
|
-
converters: [],
|
|
27
25
|
keyGenerator,
|
|
28
26
|
schema,
|
|
29
27
|
selection,
|
|
@@ -42,7 +40,7 @@ function snapshot(
|
|
|
42
40
|
return parsedBlock ? [parsedBlock] : []
|
|
43
41
|
}),
|
|
44
42
|
},
|
|
45
|
-
}
|
|
43
|
+
})
|
|
46
44
|
}
|
|
47
45
|
|
|
48
46
|
function createSpan(text: string, marks: Array<string> = []) {
|
|
@@ -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
|
}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import {expect, test} from 'vitest'
|
|
2
|
-
import type {
|
|
3
|
-
import
|
|
2
|
+
import type {EditorSelection} from '.'
|
|
3
|
+
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
4
4
|
import {isActiveDecorator} from './selector.is-active-decorator'
|
|
5
5
|
|
|
6
6
|
test(isActiveDecorator.name, () => {
|
|
7
|
-
function snapshot(selection: EditorSelection)
|
|
8
|
-
return {
|
|
7
|
+
function snapshot(selection: EditorSelection) {
|
|
8
|
+
return createTestSnapshot({
|
|
9
9
|
context: {
|
|
10
|
-
converters: [],
|
|
11
|
-
schema: {} as EditorSchema,
|
|
12
|
-
keyGenerator: () => '',
|
|
13
|
-
activeDecorators: [],
|
|
14
10
|
value: [
|
|
15
11
|
{
|
|
16
12
|
_type: '_block',
|
|
@@ -32,7 +28,7 @@ test(isActiveDecorator.name, () => {
|
|
|
32
28
|
],
|
|
33
29
|
selection,
|
|
34
30
|
},
|
|
35
|
-
}
|
|
31
|
+
})
|
|
36
32
|
}
|
|
37
33
|
|
|
38
34
|
expect(
|
|
@@ -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
|
}
|