@portabletext/editor 1.11.3 → 1.12.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.
@@ -0,0 +1,92 @@
1
+ import {
2
+ isPortableTextSpan,
3
+ isPortableTextTextBlock,
4
+ type PortableTextBlock,
5
+ } from '@sanity/types'
6
+ import type {EditorSelection} from '../../types/editor'
7
+ import {isKeyedSegment} from './behavior.utils.is-keyed-segment'
8
+ import {reverseSelection} from './behavior.utils.reverse-selection'
9
+
10
+ export function getSelectionText({
11
+ value,
12
+ selection,
13
+ }: {
14
+ value: Array<PortableTextBlock>
15
+ selection: NonNullable<EditorSelection>
16
+ }): string {
17
+ let text = ''
18
+
19
+ if (!value || !selection) {
20
+ return text
21
+ }
22
+
23
+ const forwardSelection = selection.backward
24
+ ? reverseSelection(selection)
25
+ : selection
26
+
27
+ if (!forwardSelection) {
28
+ return text
29
+ }
30
+
31
+ for (const block of value) {
32
+ if (
33
+ isKeyedSegment(forwardSelection.anchor.path[0]) &&
34
+ block._key !== forwardSelection.anchor.path[0]._key
35
+ ) {
36
+ continue
37
+ }
38
+
39
+ if (!isPortableTextTextBlock(block)) {
40
+ continue
41
+ }
42
+
43
+ for (const child of block.children) {
44
+ if (isPortableTextSpan(child)) {
45
+ if (
46
+ isKeyedSegment(forwardSelection.anchor.path[2]) &&
47
+ child._key === forwardSelection.anchor.path[2]._key &&
48
+ isKeyedSegment(forwardSelection.focus.path[2]) &&
49
+ child._key === forwardSelection.focus.path[2]._key
50
+ ) {
51
+ text =
52
+ text +
53
+ child.text.slice(
54
+ forwardSelection.anchor.offset,
55
+ forwardSelection.focus.offset,
56
+ )
57
+
58
+ break
59
+ }
60
+
61
+ if (
62
+ isKeyedSegment(forwardSelection.anchor.path[2]) &&
63
+ child._key === forwardSelection.anchor.path[2]._key
64
+ ) {
65
+ text = text + child.text.slice(forwardSelection.anchor.offset)
66
+ continue
67
+ }
68
+
69
+ if (
70
+ isKeyedSegment(forwardSelection.focus.path[2]) &&
71
+ child._key === forwardSelection.focus.path[2]._key
72
+ ) {
73
+ text = text + child.text.slice(0, forwardSelection.focus.offset)
74
+ break
75
+ }
76
+
77
+ if (text.length > 0) {
78
+ text + child.text
79
+ }
80
+ }
81
+ }
82
+
83
+ if (
84
+ isKeyedSegment(forwardSelection.focus.path[0]) &&
85
+ block._key === forwardSelection.focus.path[0]._key
86
+ ) {
87
+ break
88
+ }
89
+ }
90
+
91
+ return text
92
+ }
@@ -0,0 +1,26 @@
1
+ import {
2
+ isPortableTextTextBlock,
3
+ type KeyedSegment,
4
+ type PortableTextBlock,
5
+ } from '@sanity/types'
6
+ import type {EditorSelectionPoint} from '../../types/editor'
7
+
8
+ export function getStartPoint({
9
+ node,
10
+ path,
11
+ }: {
12
+ node: PortableTextBlock
13
+ path: [KeyedSegment]
14
+ }): EditorSelectionPoint {
15
+ if (isPortableTextTextBlock(node)) {
16
+ return {
17
+ path: [...path, 'children', {_key: node.children[0]._key}],
18
+ offset: 0,
19
+ }
20
+ }
21
+
22
+ return {
23
+ path,
24
+ offset: 0,
25
+ }
26
+ }
@@ -0,0 +1,5 @@
1
+ import type {KeyedSegment, PathSegment} from '@sanity/types'
2
+
3
+ export function isKeyedSegment(segment: PathSegment): segment is KeyedSegment {
4
+ return typeof segment === 'object' && segment !== null && '_key' in segment
5
+ }
@@ -0,0 +1,21 @@
1
+ import type {EditorSelection} from '../../types/editor'
2
+
3
+ export function reverseSelection(selection: EditorSelection): EditorSelection {
4
+ if (!selection) {
5
+ return selection
6
+ }
7
+
8
+ if (selection.backward) {
9
+ return {
10
+ anchor: selection.focus,
11
+ focus: selection.anchor,
12
+ backward: false,
13
+ }
14
+ }
15
+
16
+ return {
17
+ anchor: selection.focus,
18
+ focus: selection.anchor,
19
+ backward: true,
20
+ }
21
+ }
@@ -202,6 +202,17 @@ export function getNextBlock(
202
202
  return undefined
203
203
  }
204
204
 
205
- export function isEmptyTextBlock(block: PortableTextTextBlock) {
206
- return block.children.length === 1 && block.children[0].text === ''
205
+ export function isEmptyTextBlock(block: PortableTextBlock) {
206
+ if (!isPortableTextTextBlock(block)) {
207
+ return false
208
+ }
209
+
210
+ const onlyText = block.children.every(isPortableTextSpan)
211
+ const blockText = getTextBlockText(block)
212
+
213
+ return onlyText && blockText === ''
214
+ }
215
+
216
+ export function getTextBlockText(block: PortableTextTextBlock) {
217
+ return block.children.map((child) => child.text ?? '').join('')
207
218
  }
@@ -0,0 +1,31 @@
1
+ import type {PortableTextBlock} from '@sanity/types'
2
+ import type {EditorSelectionPoint} from '../../types/editor'
3
+ import {getSelectionText} from './behavior.utils.get-selection-text'
4
+ import {getStartPoint} from './behavior.utils.get-start-point'
5
+ import {isKeyedSegment} from './behavior.utils.is-keyed-segment'
6
+
7
+ export function getBlockTextBefore({
8
+ value,
9
+ point,
10
+ }: {
11
+ value: Array<PortableTextBlock>
12
+ point: EditorSelectionPoint
13
+ }) {
14
+ const key = isKeyedSegment(point.path[0]) ? point.path[0]._key : undefined
15
+
16
+ const block = key ? value.find((block) => block._key === key) : undefined
17
+
18
+ if (!block) {
19
+ return ''
20
+ }
21
+
22
+ const startPoint = getStartPoint({node: block, path: [{_key: block._key}]})
23
+
24
+ return getSelectionText({
25
+ value,
26
+ selection: {
27
+ anchor: startPoint,
28
+ focus: point,
29
+ },
30
+ })
31
+ }
@@ -26,15 +26,12 @@ import type {
26
26
  } from '../../types/editor'
27
27
  import {debugWithName} from '../../utils/debug'
28
28
  import {toPortableTextRange, toSlateRange} from '../../utils/ranges'
29
- import {
30
- fromSlateValue,
31
- isEqualToEmptyEditor,
32
- toSlateValue,
33
- } from '../../utils/values'
29
+ import {fromSlateValue, toSlateValue} from '../../utils/values'
34
30
  import {
35
31
  KEY_TO_VALUE_ELEMENT,
36
32
  SLATE_TO_PORTABLE_TEXT_RANGE,
37
33
  } from '../../utils/weakMaps'
34
+ import {insertBlockObjectActionImplementation} from '../behavior/behavior.action.insert-block-object'
38
35
  import type {BehaviorActionImplementation} from '../behavior/behavior.actions'
39
36
  import type {EditorActor} from '../editor-machine'
40
37
  import {isDecoratorActive} from './createWithPortableTextMarkModel'
@@ -206,18 +203,35 @@ export function createEditableAPI(
206
203
  type: TSchemaType,
207
204
  value?: {[prop: string]: any},
208
205
  ): Path => {
209
- return insertBlockObjectActionImplementation({
206
+ insertBlockObjectActionImplementation({
210
207
  context: {
211
208
  keyGenerator: editorActor.getSnapshot().context.keyGenerator,
212
209
  schema: types,
213
210
  },
214
211
  action: {
215
212
  type: 'insert block object',
216
- name: type.name,
217
- value,
213
+ blockObject: {
214
+ name: type.name,
215
+ value,
216
+ },
217
+ placement: 'auto',
218
218
  editor,
219
219
  },
220
220
  })
221
+
222
+ editor.onChange()
223
+
224
+ return (
225
+ toPortableTextRange(
226
+ fromSlateValue(
227
+ editor.children,
228
+ types.block.name,
229
+ KEY_TO_VALUE_ELEMENT.get(editor),
230
+ ),
231
+ editor.selection,
232
+ types,
233
+ )?.focus.path ?? []
234
+ )
221
235
  },
222
236
  hasBlockStyle: (style: string): boolean => {
223
237
  try {
@@ -487,85 +501,6 @@ export function createEditableAPI(
487
501
  return editableApi
488
502
  }
489
503
 
490
- export const insertBlockObjectActionImplementation: BehaviorActionImplementation<
491
- 'insert block object',
492
- Path
493
- > = ({context, action}) => {
494
- const editor = action.editor
495
- const types = context.schema
496
- const block = toSlateValue(
497
- [
498
- {
499
- _key: context.keyGenerator(),
500
- _type: action.name,
501
- ...(action.value ? action.value : {}),
502
- },
503
- ],
504
- {schemaTypes: context.schema},
505
- )[0] as unknown as Node
506
-
507
- if (!editor.selection) {
508
- const lastBlock = Array.from(
509
- Editor.nodes(editor, {
510
- match: (n) => !Editor.isEditor(n),
511
- at: [],
512
- reverse: true,
513
- }),
514
- )[0]
515
-
516
- // If there is no selection, let's just insert the new block at the
517
- // end of the document
518
- Editor.insertNode(editor, block)
519
-
520
- if (lastBlock && isEqualToEmptyEditor([lastBlock[0]], types)) {
521
- // And if the last block was an empty text block, let's remove
522
- // that too
523
- Transforms.removeNodes(editor, {at: lastBlock[1]})
524
- }
525
-
526
- editor.onChange()
527
-
528
- return (
529
- toPortableTextRange(
530
- fromSlateValue(
531
- editor.children,
532
- types.block.name,
533
- KEY_TO_VALUE_ELEMENT.get(editor),
534
- ),
535
- editor.selection,
536
- types,
537
- )?.focus.path ?? []
538
- )
539
- }
540
-
541
- const focusBlock = Array.from(
542
- Editor.nodes(editor, {
543
- at: editor.selection.focus.path.slice(0, 1),
544
- match: (n) => n._type === types.block.name,
545
- }),
546
- )[0]
547
-
548
- Editor.insertNode(editor, block)
549
-
550
- if (focusBlock && isEqualToEmptyEditor([focusBlock[0]], types)) {
551
- Transforms.removeNodes(editor, {at: focusBlock[1]})
552
- }
553
-
554
- editor.onChange()
555
-
556
- return (
557
- toPortableTextRange(
558
- fromSlateValue(
559
- editor.children,
560
- types.block.name,
561
- KEY_TO_VALUE_ELEMENT.get(editor),
562
- ),
563
- editor.selection,
564
- types,
565
- )?.focus.path || []
566
- )
567
- }
568
-
569
504
  function isAnnotationActive({
570
505
  editor,
571
506
  annotation,
package/src/index.ts CHANGED
@@ -20,6 +20,7 @@ export {
20
20
  type OmitFromUnion,
21
21
  type PickFromUnion,
22
22
  } from './editor/behavior/behavior.types'
23
+ export type {BlockOffset} from './editor/behavior/behavior.utils.block-offset'
23
24
  export type {SlateEditor} from './editor/create-slate-editor'
24
25
  export {
25
26
  defineSchema,