@portabletext/editor 1.28.0 → 1.30.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 +40 -37
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/parse-blocks.cjs +79 -0
- package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -0
- package/lib/_chunks-cjs/plugin.event-listener.cjs +55 -97
- package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.get-selection-start-point.cjs +15 -0
- package/lib/_chunks-cjs/selector.get-selection-start-point.cjs.map +1 -0
- package/lib/_chunks-es/behavior.core.js +40 -37
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/parse-blocks.js +80 -0
- package/lib/_chunks-es/parse-blocks.js.map +1 -0
- package/lib/_chunks-es/plugin.event-listener.js +57 -98
- package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
- package/lib/_chunks-es/selector.get-selection-start-point.js +16 -0
- package/lib/_chunks-es/selector.get-selection-start-point.js.map +1 -0
- package/lib/behaviors/index.d.cts +196 -124
- package/lib/behaviors/index.d.ts +196 -124
- package/lib/index.d.cts +248 -0
- package/lib/index.d.ts +248 -0
- package/lib/plugins/index.cjs +249 -1
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +246 -1
- package/lib/plugins/index.d.ts +246 -1
- package/lib/plugins/index.js +257 -3
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.cjs +28 -1
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +21 -0
- package/lib/selectors/index.d.ts +21 -0
- package/lib/selectors/index.js +28 -0
- package/lib/selectors/index.js.map +1 -1
- package/lib/utils/index.cjs +70 -1
- package/lib/utils/index.cjs.map +1 -1
- package/lib/utils/index.d.cts +168 -2
- package/lib/utils/index.d.ts +168 -2
- package/lib/utils/index.js +71 -1
- package/lib/utils/index.js.map +1 -1
- package/package.json +5 -5
- package/src/behavior-actions/behavior.action.delete.ts +18 -0
- package/src/behavior-actions/behavior.action.insert-break.ts +3 -8
- package/src/behavior-actions/behavior.actions.ts +9 -0
- package/src/behaviors/_exports/index.ts +1 -0
- package/src/behaviors/behavior.core.deserialize.ts +52 -38
- package/src/behaviors/behavior.core.ts +4 -11
- package/src/behaviors/behavior.types.ts +4 -0
- package/src/editor/PortableTextEditor.tsx +20 -0
- package/src/internal-utils/__tests__/patchToOperations.test.ts +19 -21
- package/src/internal-utils/applyPatch.ts +11 -3
- package/src/plugins/index.ts +2 -0
- package/src/plugins/plugin.behavior.tsx +22 -0
- package/src/plugins/plugin.one-line.tsx +225 -0
- package/src/selectors/index.ts +3 -0
- package/src/selectors/selector.get-selection-end-point.ts +17 -0
- package/src/selectors/selector.get-selection-start-point.ts +17 -0
- package/src/selectors/selector.is-overlapping-selection.ts +46 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/util.is-span.ts +12 -0
- package/src/utils/util.is-text-block.ts +12 -0
- package/src/utils/util.merge-text-blocks.ts +36 -0
- package/src/utils/util.split-text-block.ts +55 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type {PortableTextBlock, PortableTextTextBlock} from '@sanity/types'
|
|
2
|
+
import type {EditorContext} from '../selectors'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export function isTextBlock(
|
|
8
|
+
context: Pick<EditorContext, 'schema'>,
|
|
9
|
+
block: PortableTextBlock,
|
|
10
|
+
): block is PortableTextTextBlock {
|
|
11
|
+
return block._type === context.schema.block.name
|
|
12
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type {PortableTextTextBlock} from '@sanity/types'
|
|
2
|
+
import {parseBlock} from '../internal-utils/parse-blocks'
|
|
3
|
+
import type {EditorContext} from '../selectors'
|
|
4
|
+
import {isTextBlock} from './util.is-text-block'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @beta
|
|
8
|
+
*/
|
|
9
|
+
export function mergeTextBlocks({
|
|
10
|
+
context,
|
|
11
|
+
targetBlock,
|
|
12
|
+
incomingBlock,
|
|
13
|
+
}: {
|
|
14
|
+
context: Pick<EditorContext, 'keyGenerator' | 'schema'>
|
|
15
|
+
targetBlock: PortableTextTextBlock
|
|
16
|
+
incomingBlock: PortableTextTextBlock
|
|
17
|
+
}) {
|
|
18
|
+
const parsedIncomingBlock = parseBlock({
|
|
19
|
+
context,
|
|
20
|
+
block: incomingBlock,
|
|
21
|
+
options: {refreshKeys: true},
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
if (!parsedIncomingBlock || !isTextBlock(context, parsedIncomingBlock)) {
|
|
25
|
+
return targetBlock
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
...targetBlock,
|
|
30
|
+
children: [...targetBlock.children, ...parsedIncomingBlock.children],
|
|
31
|
+
markDefs: [
|
|
32
|
+
...(targetBlock.markDefs ?? []),
|
|
33
|
+
...(parsedIncomingBlock.markDefs ?? []),
|
|
34
|
+
],
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type {PortableTextTextBlock} from '@sanity/types'
|
|
2
|
+
import {isTextBlock, sliceBlocks, type EditorSelectionPoint} from '.'
|
|
3
|
+
import type {EditorContext} from '../selectors'
|
|
4
|
+
import {isSpan} from './util.is-span'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @beta
|
|
8
|
+
*/
|
|
9
|
+
export function splitTextBlock({
|
|
10
|
+
context,
|
|
11
|
+
block,
|
|
12
|
+
point,
|
|
13
|
+
}: {
|
|
14
|
+
context: Pick<EditorContext, 'schema'>
|
|
15
|
+
block: PortableTextTextBlock
|
|
16
|
+
point: EditorSelectionPoint
|
|
17
|
+
}): {before: PortableTextTextBlock; after: PortableTextTextBlock} | undefined {
|
|
18
|
+
const firstChild = block.children.at(0)
|
|
19
|
+
const lastChild = block.children.at(block.children.length - 1)
|
|
20
|
+
|
|
21
|
+
if (!firstChild || !lastChild) {
|
|
22
|
+
return undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const before = sliceBlocks({
|
|
26
|
+
blocks: [block],
|
|
27
|
+
selection: {
|
|
28
|
+
anchor: {
|
|
29
|
+
path: [{_key: block._key}, 'children', {_key: firstChild._key}],
|
|
30
|
+
offset: 0,
|
|
31
|
+
},
|
|
32
|
+
focus: point,
|
|
33
|
+
},
|
|
34
|
+
}).at(0)
|
|
35
|
+
const after = sliceBlocks({
|
|
36
|
+
blocks: [block],
|
|
37
|
+
selection: {
|
|
38
|
+
anchor: point,
|
|
39
|
+
focus: {
|
|
40
|
+
path: [{_key: block._key}, 'children', {_key: lastChild._key}],
|
|
41
|
+
offset: isSpan(context, lastChild) ? lastChild.text.length : 0,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
}).at(0)
|
|
45
|
+
|
|
46
|
+
if (!before || !after) {
|
|
47
|
+
return undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!isTextBlock(context, before) || !isTextBlock(context, after)) {
|
|
51
|
+
return undefined
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return {before, after}
|
|
55
|
+
}
|