@portabletext/editor 1.20.0 → 1.21.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 +8 -68
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.get-text-before.cjs +11 -39
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/util.get-block-start-point.cjs +30 -0
- package/lib/_chunks-cjs/util.get-block-start-point.cjs.map +1 -0
- package/lib/_chunks-cjs/util.is-empty-text-block.cjs +71 -0
- package/lib/_chunks-cjs/util.is-empty-text-block.cjs.map +1 -0
- package/lib/_chunks-cjs/util.is-keyed-segment.cjs +6 -0
- package/lib/_chunks-cjs/util.is-keyed-segment.cjs.map +1 -0
- package/lib/_chunks-es/behavior.core.js +7 -66
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/selector.get-text-before.js +5 -31
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
- package/lib/_chunks-es/util.get-block-start-point.js +31 -0
- package/lib/_chunks-es/util.get-block-start-point.js.map +1 -0
- package/lib/_chunks-es/util.is-empty-text-block.js +73 -0
- package/lib/_chunks-es/util.is-empty-text-block.js.map +1 -0
- package/lib/_chunks-es/util.is-keyed-segment.js +7 -0
- package/lib/_chunks-es/util.is-keyed-segment.js.map +1 -0
- package/lib/behaviors/index.cjs +7 -7
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +179 -119
- package/lib/behaviors/index.d.ts +179 -119
- package/lib/behaviors/index.js +3 -2
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +112 -21
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +870 -141
- package/lib/index.d.ts +870 -141
- package/lib/index.js +111 -19
- package/lib/index.js.map +1 -1
- package/lib/selectors/index.cjs +73 -1
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +14 -0
- package/lib/selectors/index.d.ts +14 -0
- package/lib/selectors/index.js +73 -0
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.cjs +11 -0
- package/lib/utils/index.cjs.map +1 -0
- package/lib/utils/index.d.cts +87 -0
- package/lib/utils/index.d.ts +87 -0
- package/lib/utils/index.js +13 -0
- package/lib/utils/index.js.map +1 -0
- package/package.json +15 -9
- package/src/behavior-actions/behavior.action-utils.insert-block.ts +1 -1
- package/src/behavior-actions/behavior.action.insert-block-object.ts +1 -1
- package/src/behavior-actions/behavior.action.insert-inline-object.ts +1 -1
- package/src/behavior-actions/behavior.action.text-block.set.ts +1 -1
- package/src/behavior-actions/behavior.action.text-block.unset.ts +1 -1
- package/src/behavior-actions/behavior.actions.ts +5 -5
- package/src/behaviors/behavior.code-editor.ts +1 -1
- package/src/behaviors/behavior.core.block-objects.ts +2 -2
- package/src/behaviors/behavior.core.decorators.ts +1 -1
- package/src/behaviors/behavior.core.lists.ts +2 -2
- package/src/behaviors/behavior.emoji-picker.ts +1 -1
- package/src/behaviors/behavior.links.ts +1 -1
- package/src/behaviors/behavior.markdown.ts +2 -2
- package/src/behaviors/behavior.types.ts +52 -19
- package/src/behaviors/index.ts +1 -0
- package/src/editor/Editable.tsx +12 -12
- package/src/editor/PortableTextEditor.tsx +2 -2
- package/src/editor/components/DraggableBlock.tsx +2 -2
- package/src/editor/components/Element.tsx +3 -3
- package/src/editor/components/Leaf.tsx +1 -1
- package/src/editor/components/Synchronizer.tsx +1 -1
- package/src/editor/create-editor.ts +22 -20
- package/src/editor/create-slate-editor.tsx +5 -2
- package/src/editor/editor-machine.ts +115 -23
- package/src/editor/get-value.ts +2 -2
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +1 -1
- package/src/editor/mutation-machine.ts +2 -2
- package/src/editor/plugins/create-with-event-listeners.ts +50 -4
- package/src/editor/plugins/createWithEditableAPI.ts +7 -7
- package/src/editor/plugins/createWithHotKeys.ts +2 -2
- package/src/editor/plugins/createWithInsertData.ts +7 -7
- package/src/editor/plugins/createWithMaxBlocks.ts +2 -2
- package/src/editor/plugins/createWithObjectKeys.ts +2 -2
- package/src/editor/plugins/createWithPatches.ts +10 -10
- package/src/editor/plugins/createWithPlaceholderBlock.ts +3 -3
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +1 -1
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +4 -4
- package/src/editor/plugins/createWithPortableTextSelections.ts +6 -6
- package/src/editor/plugins/createWithSchemaTypes.ts +1 -1
- package/src/editor/plugins/createWithUndoRedo.ts +5 -5
- package/src/editor/plugins/createWithUtils.ts +2 -2
- package/src/editor/plugins/with-plugins.ts +1 -1
- package/src/editor/sync-machine.ts +8 -5
- package/src/index.ts +5 -1
- package/src/selectors/index.ts +2 -0
- package/src/selectors/selector.get-selection-text.test.ts +75 -0
- package/src/selectors/selector.get-selection-text.ts +3 -3
- package/src/selectors/selector.get-text-before.ts +7 -4
- package/src/selectors/selector.is-point-after-selection.ts +82 -0
- package/src/selectors/selector.is-point-before-selection.ts +82 -0
- package/src/utils/_exports/index.ts +1 -0
- package/src/utils/index.ts +11 -0
- package/src/{editor/utils/utils.block-offset.test.ts → utils/util.block-offset.test.ts} +1 -1
- package/src/{editor/utils/utils.block-offset.ts → utils/util.block-offset.ts} +23 -7
- package/src/{editor/utils/utils.get-start-point.ts → utils/util.get-block-start-point.ts} +5 -2
- package/src/utils/util.get-text-block-text.ts +8 -0
- package/src/{editor/utils/utils.ts → utils/util.is-empty-text-block.ts} +4 -5
- package/src/{editor/utils/utils.is-keyed-segment.ts → utils/util.is-keyed-segment.ts} +3 -0
- package/src/{editor/utils/utils.reverse-selection.ts → utils/util.reverse-selection.ts} +4 -1
- /package/src/{utils → internal-utils}/__tests__/dmpToOperations.test.ts +0 -0
- /package/src/{utils → internal-utils}/__tests__/operationToPatches.test.ts +0 -0
- /package/src/{utils → internal-utils}/__tests__/patchToOperations.test.ts +0 -0
- /package/src/{utils → internal-utils}/__tests__/ranges.test.ts +0 -0
- /package/src/{utils → internal-utils}/__tests__/valueNormalization.test.tsx +0 -0
- /package/src/{utils → internal-utils}/__tests__/values.test.ts +0 -0
- /package/src/{utils → internal-utils}/applyPatch.ts +0 -0
- /package/src/{utils → internal-utils}/debug.ts +0 -0
- /package/src/{utils → internal-utils}/is-hotkey.test.ts +0 -0
- /package/src/{utils → internal-utils}/is-hotkey.ts +0 -0
- /package/src/{utils → internal-utils}/looks-like-url.test.ts +0 -0
- /package/src/{utils → internal-utils}/looks-like-url.ts +0 -0
- /package/src/{utils → internal-utils}/operationToPatches.ts +0 -0
- /package/src/{utils → internal-utils}/paths.ts +0 -0
- /package/src/{utils → internal-utils}/ranges.ts +0 -0
- /package/src/{utils → internal-utils}/schema.ts +0 -0
- /package/src/{utils → internal-utils}/selection.ts +0 -0
- /package/src/{utils → internal-utils}/sibling-utils.ts +0 -0
- /package/src/{utils → internal-utils}/validateValue.ts +0 -0
- /package/src/{utils → internal-utils}/values.ts +0 -0
- /package/src/{utils → internal-utils}/weakMaps.ts +0 -0
- /package/src/{utils → internal-utils}/withChanges.ts +0 -0
- /package/src/{utils → internal-utils}/withUndoRedo.ts +0 -0
- /package/src/{utils → internal-utils}/withoutPatching.ts +0 -0
package/src/index.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export type {Patch} from '@portabletext/patches'
|
|
2
|
-
export type {
|
|
2
|
+
export type {
|
|
3
|
+
PortableTextBlock,
|
|
4
|
+
PortableTextChild,
|
|
5
|
+
PortableTextSpan,
|
|
6
|
+
} from '@sanity/types'
|
|
3
7
|
export type {
|
|
4
8
|
Behavior,
|
|
5
9
|
BehaviorActionIntend,
|
package/src/selectors/index.ts
CHANGED
|
@@ -16,6 +16,8 @@ export {isActiveAnnotation} from './selector.is-active-annotation'
|
|
|
16
16
|
export {isActiveDecorator} from './selector.is-active-decorator'
|
|
17
17
|
export {isActiveListItem} from './selector.is-active-list-item'
|
|
18
18
|
export {isActiveStyle} from './selector.is-active-style'
|
|
19
|
+
export {isPointAfterSelection} from './selector.is-point-after-selection'
|
|
20
|
+
export {isPointBeforeSelection} from './selector.is-point-before-selection'
|
|
19
21
|
export {isSelectionCollapsed} from './selector.is-selection-collapsed'
|
|
20
22
|
export {isSelectionExpanded} from './selector.is-selection-expanded'
|
|
21
23
|
export * from './selectors'
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {expect, test} from 'vitest'
|
|
2
|
+
import type {EditorSchema, EditorSelection, EditorSnapshot} from '.'
|
|
3
|
+
import {getSelectionText} from './selector.get-selection-text'
|
|
4
|
+
|
|
5
|
+
test(getSelectionText.name, () => {
|
|
6
|
+
function snapshot(selection: EditorSelection): EditorSnapshot {
|
|
7
|
+
return {
|
|
8
|
+
context: {
|
|
9
|
+
schema: {} as EditorSchema,
|
|
10
|
+
keyGenerator: () => '',
|
|
11
|
+
activeDecorators: [],
|
|
12
|
+
value: [
|
|
13
|
+
{
|
|
14
|
+
_type: 'block',
|
|
15
|
+
_key: 'e0-k8',
|
|
16
|
+
style: 'normal',
|
|
17
|
+
markDefs: [],
|
|
18
|
+
children: [
|
|
19
|
+
{
|
|
20
|
+
_type: 'span',
|
|
21
|
+
_key: 'e0-k7',
|
|
22
|
+
text: ':b',
|
|
23
|
+
marks: [],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
text: 'a',
|
|
27
|
+
_type: 'span',
|
|
28
|
+
_key: 'e0-k9',
|
|
29
|
+
marks: ['strong'],
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
text: 'r',
|
|
33
|
+
marks: [],
|
|
34
|
+
_type: 'span',
|
|
35
|
+
_key: 'e0-k10',
|
|
36
|
+
},
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
selection,
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
expect(
|
|
46
|
+
getSelectionText(
|
|
47
|
+
snapshot({
|
|
48
|
+
anchor: {
|
|
49
|
+
path: [
|
|
50
|
+
{
|
|
51
|
+
_key: 'e0-k8',
|
|
52
|
+
},
|
|
53
|
+
'children',
|
|
54
|
+
{
|
|
55
|
+
_key: 'e0-k7',
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
offset: 0,
|
|
59
|
+
},
|
|
60
|
+
focus: {
|
|
61
|
+
path: [
|
|
62
|
+
{
|
|
63
|
+
_key: 'e0-k8',
|
|
64
|
+
},
|
|
65
|
+
'children',
|
|
66
|
+
{
|
|
67
|
+
_key: 'e0-k10',
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
offset: 1,
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
),
|
|
74
|
+
).toBe(':bar')
|
|
75
|
+
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {isPortableTextSpan, isPortableTextTextBlock} from '@sanity/types'
|
|
2
2
|
import type {EditorSelector} from '../editor/editor-selector'
|
|
3
|
-
import {isKeyedSegment} from '../
|
|
4
|
-
import {reverseSelection} from '../
|
|
3
|
+
import {isKeyedSegment} from '../utils/util.is-keyed-segment'
|
|
4
|
+
import {reverseSelection} from '../utils/util.reverse-selection'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* @public
|
|
@@ -70,7 +70,7 @@ export const getSelectionText: EditorSelector<string> = ({context}) => {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
if (text.length > 0) {
|
|
73
|
-
text + child.text
|
|
73
|
+
text = text + child.text
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {EditorSelector} from '../editor/editor-selector'
|
|
2
|
-
import {
|
|
3
|
-
import {isKeyedSegment} from '../
|
|
4
|
-
import {reverseSelection} from '../
|
|
2
|
+
import {getBlockStartPoint} from '../utils/util.get-block-start-point'
|
|
3
|
+
import {isKeyedSegment} from '../utils/util.is-keyed-segment'
|
|
4
|
+
import {reverseSelection} from '../utils/util.reverse-selection'
|
|
5
5
|
import {getSelectionText} from './selector.get-selection-text'
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -26,7 +26,10 @@ export const getBlockTextBefore: EditorSelector<string> = ({context}) => {
|
|
|
26
26
|
return ''
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
const startOfBlock =
|
|
29
|
+
const startOfBlock = getBlockStartPoint({
|
|
30
|
+
node: block,
|
|
31
|
+
path: [{_key: block._key}],
|
|
32
|
+
})
|
|
30
33
|
|
|
31
34
|
return getSelectionText({
|
|
32
35
|
context: {
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {isKeySegment, isPortableTextTextBlock} from '@sanity/types'
|
|
2
|
+
import type {EditorSelector} from '../editor/editor-selector'
|
|
3
|
+
import type {EditorSelectionPoint} from '../types/editor'
|
|
4
|
+
import {reverseSelection} from '../utils/util.reverse-selection'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export function isPointAfterSelection(
|
|
10
|
+
point: EditorSelectionPoint,
|
|
11
|
+
): EditorSelector<boolean> {
|
|
12
|
+
return (snapshot) => {
|
|
13
|
+
if (!snapshot.context.selection) {
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const selection = reverseSelection(snapshot.context.selection)
|
|
18
|
+
|
|
19
|
+
const pointBlockKey = isKeySegment(point.path[0])
|
|
20
|
+
? point.path[0]._key
|
|
21
|
+
: undefined
|
|
22
|
+
const pointChildKey = isKeySegment(point.path[2])
|
|
23
|
+
? point.path[2]._key
|
|
24
|
+
: undefined
|
|
25
|
+
|
|
26
|
+
const endBlockKey = isKeySegment(selection.focus.path[0])
|
|
27
|
+
? selection.focus.path[0]._key
|
|
28
|
+
: undefined
|
|
29
|
+
const endChildKey = isKeySegment(selection.focus.path[2])
|
|
30
|
+
? selection.focus.path[2]._key
|
|
31
|
+
: undefined
|
|
32
|
+
|
|
33
|
+
if (!pointBlockKey || !endBlockKey) {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let after = false
|
|
38
|
+
|
|
39
|
+
for (const block of snapshot.context.value) {
|
|
40
|
+
if (block._key === endBlockKey) {
|
|
41
|
+
if (block._key !== pointBlockKey) {
|
|
42
|
+
after = true
|
|
43
|
+
break
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Both the point and the selection end in this block
|
|
47
|
+
|
|
48
|
+
if (!isPortableTextTextBlock(block)) {
|
|
49
|
+
break
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!pointChildKey || !endChildKey) {
|
|
53
|
+
break
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for (const child of block.children) {
|
|
57
|
+
if (child._key === endChildKey) {
|
|
58
|
+
if (child._key !== pointChildKey) {
|
|
59
|
+
after = true
|
|
60
|
+
break
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Both the point and the selection end in this child
|
|
64
|
+
|
|
65
|
+
after = point.offset > selection.focus.offset
|
|
66
|
+
break
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (child._key === pointChildKey) {
|
|
70
|
+
break
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (block._key === pointBlockKey) {
|
|
76
|
+
break
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return after
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {isKeySegment, isPortableTextTextBlock} from '@sanity/types'
|
|
2
|
+
import type {EditorSelector} from '../editor/editor-selector'
|
|
3
|
+
import type {EditorSelectionPoint} from '../types/editor'
|
|
4
|
+
import {reverseSelection} from '../utils/util.reverse-selection'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
export function isPointBeforeSelection(
|
|
10
|
+
point: EditorSelectionPoint,
|
|
11
|
+
): EditorSelector<boolean> {
|
|
12
|
+
return (snapshot) => {
|
|
13
|
+
if (!snapshot.context.selection) {
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const selection = reverseSelection(snapshot.context.selection)
|
|
18
|
+
|
|
19
|
+
const pointBlockKey = isKeySegment(point.path[0])
|
|
20
|
+
? point.path[0]._key
|
|
21
|
+
: undefined
|
|
22
|
+
const pointChildKey = isKeySegment(point.path[2])
|
|
23
|
+
? point.path[2]._key
|
|
24
|
+
: undefined
|
|
25
|
+
|
|
26
|
+
const startBlockKey = isKeySegment(selection.anchor.path[0])
|
|
27
|
+
? selection.anchor.path[0]._key
|
|
28
|
+
: undefined
|
|
29
|
+
const startChildKey = isKeySegment(selection.anchor.path[2])
|
|
30
|
+
? selection.anchor.path[2]._key
|
|
31
|
+
: undefined
|
|
32
|
+
|
|
33
|
+
if (!pointBlockKey || !startBlockKey) {
|
|
34
|
+
return false
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let before = false
|
|
38
|
+
|
|
39
|
+
for (const block of snapshot.context.value) {
|
|
40
|
+
if (block._key === pointBlockKey) {
|
|
41
|
+
if (block._key !== startBlockKey) {
|
|
42
|
+
before = true
|
|
43
|
+
break
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Both the point and the selection start in this block
|
|
47
|
+
|
|
48
|
+
if (!isPortableTextTextBlock(block)) {
|
|
49
|
+
break
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!pointChildKey || !startChildKey) {
|
|
53
|
+
break
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for (const child of block.children) {
|
|
57
|
+
if (child._key === pointChildKey) {
|
|
58
|
+
if (child._key !== startChildKey) {
|
|
59
|
+
before = true
|
|
60
|
+
break
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Both the point and the selection start in this child
|
|
64
|
+
|
|
65
|
+
before = point.offset < selection.anchor.offset
|
|
66
|
+
break
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (child._key === startChildKey) {
|
|
70
|
+
break
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (block._key === startBlockKey) {
|
|
76
|
+
break
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return before
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '../index'
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type {BlockOffset} from '../behaviors/behavior.types'
|
|
2
|
+
export type {EditorSelection, EditorSelectionPoint} from '../types/editor'
|
|
3
|
+
export {
|
|
4
|
+
blockOffsetToSpanSelectionPoint,
|
|
5
|
+
spanSelectionPointToBlockOffset,
|
|
6
|
+
} from './util.block-offset'
|
|
7
|
+
export {getBlockStartPoint} from './util.get-block-start-point'
|
|
8
|
+
export {getTextBlockText} from './util.get-text-block-text'
|
|
9
|
+
export {isEmptyTextBlock} from './util.is-empty-text-block'
|
|
10
|
+
export {isKeyedSegment} from './util.is-keyed-segment'
|
|
11
|
+
export {reverseSelection} from './util.reverse-selection'
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import {expect, test} from 'vitest'
|
|
3
|
-
import {blockOffsetToSpanSelectionPoint} from './
|
|
3
|
+
import {blockOffsetToSpanSelectionPoint} from './util.block-offset'
|
|
4
4
|
|
|
5
5
|
test(blockOffsetToSpanSelectionPoint.name, () => {
|
|
6
6
|
const value: Array<PortableTextBlock> = [
|
|
@@ -4,8 +4,13 @@ import {
|
|
|
4
4
|
type KeyedSegment,
|
|
5
5
|
type PortableTextBlock,
|
|
6
6
|
} from '@sanity/types'
|
|
7
|
-
import type {BlockOffset} from '
|
|
7
|
+
import type {BlockOffset} from '../behaviors/behavior.types'
|
|
8
|
+
import type {EditorSelectionPoint} from '../types/editor'
|
|
9
|
+
import {isKeyedSegment} from './util.is-keyed-segment'
|
|
8
10
|
|
|
11
|
+
/**
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
9
14
|
export function blockOffsetToSpanSelectionPoint({
|
|
10
15
|
value,
|
|
11
16
|
blockOffset,
|
|
@@ -55,20 +60,31 @@ export function blockOffsetToSpanSelectionPoint({
|
|
|
55
60
|
return selectionPoint
|
|
56
61
|
}
|
|
57
62
|
|
|
63
|
+
/**
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
58
66
|
export function spanSelectionPointToBlockOffset({
|
|
59
67
|
value,
|
|
60
68
|
selectionPoint,
|
|
61
69
|
}: {
|
|
62
70
|
value: Array<PortableTextBlock>
|
|
63
|
-
selectionPoint:
|
|
64
|
-
path: [KeyedSegment, 'children', KeyedSegment]
|
|
65
|
-
offset: number
|
|
66
|
-
}
|
|
71
|
+
selectionPoint: EditorSelectionPoint
|
|
67
72
|
}): BlockOffset | undefined {
|
|
68
73
|
let offset = 0
|
|
69
74
|
|
|
75
|
+
const blockKey = isKeyedSegment(selectionPoint.path[0])
|
|
76
|
+
? selectionPoint.path[0]._key
|
|
77
|
+
: undefined
|
|
78
|
+
const spanKey = isKeyedSegment(selectionPoint.path[2])
|
|
79
|
+
? selectionPoint.path[2]._key
|
|
80
|
+
: undefined
|
|
81
|
+
|
|
82
|
+
if (!blockKey || !spanKey) {
|
|
83
|
+
return undefined
|
|
84
|
+
}
|
|
85
|
+
|
|
70
86
|
for (const block of value) {
|
|
71
|
-
if (block._key !==
|
|
87
|
+
if (block._key !== blockKey) {
|
|
72
88
|
continue
|
|
73
89
|
}
|
|
74
90
|
|
|
@@ -81,7 +97,7 @@ export function spanSelectionPointToBlockOffset({
|
|
|
81
97
|
continue
|
|
82
98
|
}
|
|
83
99
|
|
|
84
|
-
if (child._key ===
|
|
100
|
+
if (child._key === spanKey) {
|
|
85
101
|
return {
|
|
86
102
|
path: [{_key: block._key}],
|
|
87
103
|
offset: offset + selectionPoint.offset,
|
|
@@ -3,9 +3,12 @@ import {
|
|
|
3
3
|
type KeyedSegment,
|
|
4
4
|
type PortableTextBlock,
|
|
5
5
|
} from '@sanity/types'
|
|
6
|
-
import type {EditorSelectionPoint} from '
|
|
6
|
+
import type {EditorSelectionPoint} from '../types/editor'
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export function getBlockStartPoint({
|
|
9
12
|
node,
|
|
10
13
|
path,
|
|
11
14
|
}: {
|
|
@@ -2,9 +2,12 @@ import {
|
|
|
2
2
|
isPortableTextSpan,
|
|
3
3
|
isPortableTextTextBlock,
|
|
4
4
|
type PortableTextBlock,
|
|
5
|
-
type PortableTextTextBlock,
|
|
6
5
|
} from '@sanity/types'
|
|
6
|
+
import {getTextBlockText} from './util.get-text-block-text'
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
8
11
|
export function isEmptyTextBlock(block: PortableTextBlock) {
|
|
9
12
|
if (!isPortableTextTextBlock(block)) {
|
|
10
13
|
return false
|
|
@@ -15,7 +18,3 @@ export function isEmptyTextBlock(block: PortableTextBlock) {
|
|
|
15
18
|
|
|
16
19
|
return onlyText && blockText === ''
|
|
17
20
|
}
|
|
18
|
-
|
|
19
|
-
export function getTextBlockText(block: PortableTextTextBlock) {
|
|
20
|
-
return block.children.map((child) => child.text ?? '').join('')
|
|
21
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|