@portabletext/editor 1.47.9 → 1.47.11

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.
Files changed (37) hide show
  1. package/lib/_chunks-cjs/editor-provider.cjs +105 -73
  2. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  3. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs +0 -4
  4. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs.map +1 -1
  5. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +8 -0
  6. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +1 -1
  7. package/lib/_chunks-es/editor-provider.js +108 -76
  8. package/lib/_chunks-es/editor-provider.js.map +1 -1
  9. package/lib/_chunks-es/util.is-selection-collapsed.js +0 -4
  10. package/lib/_chunks-es/util.is-selection-collapsed.js.map +1 -1
  11. package/lib/_chunks-es/util.selection-point-to-block-offset.js +8 -0
  12. package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +1 -1
  13. package/lib/behaviors/index.d.cts +359 -339
  14. package/lib/behaviors/index.d.ts +359 -339
  15. package/lib/behaviors/index.js +1 -1
  16. package/lib/index.cjs +3 -3
  17. package/lib/index.cjs.map +1 -1
  18. package/lib/index.d.cts +4 -4
  19. package/lib/index.d.ts +4 -4
  20. package/lib/index.js +2 -1
  21. package/lib/index.js.map +1 -1
  22. package/lib/plugins/index.d.cts +4 -4
  23. package/lib/plugins/index.d.ts +4 -4
  24. package/lib/selectors/index.d.cts +4 -4
  25. package/lib/selectors/index.d.ts +4 -4
  26. package/lib/utils/index.cjs +2 -5
  27. package/lib/utils/index.cjs.map +1 -1
  28. package/lib/utils/index.d.cts +4 -4
  29. package/lib/utils/index.d.ts +4 -4
  30. package/lib/utils/index.js +2 -5
  31. package/lib/utils/index.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/behavior-actions/behavior.actions.ts +0 -9
  34. package/src/behaviors/behavior.abstract.split.ts +162 -0
  35. package/src/behaviors/behavior.default.ts +2 -0
  36. package/src/behaviors/behavior.types.event.ts +4 -4
  37. package/src/behavior-actions/behavior.action.split.block.ts +0 -146
@@ -1,146 +0,0 @@
1
- import {isEqual} from 'lodash'
2
- import {Editor, Node, Path, Transforms} from 'slate'
3
- import type {SlateTextBlock, VoidElement} from '../types/slate'
4
- import type {BehaviorActionImplementation} from './behavior.actions'
5
-
6
- export const splitBlockActionImplementation: BehaviorActionImplementation<
7
- 'split.block'
8
- > = ({context, action}) => {
9
- const keyGenerator = context.keyGenerator
10
- const schema = context.schema
11
- const editor = action.editor
12
-
13
- if (!editor.selection) {
14
- return
15
- }
16
-
17
- const anchorBlockPath = editor.selection.anchor.path.slice(0, 1)
18
- const focusBlockPath = editor.selection.focus.path.slice(0, 1)
19
- const focusBlock = Node.descendant(editor, focusBlockPath) as
20
- | SlateTextBlock
21
- | VoidElement
22
-
23
- if (editor.isTextBlock(focusBlock)) {
24
- const selectionAcrossBlocks = anchorBlockPath[0] !== focusBlockPath[0]
25
-
26
- if (!selectionAcrossBlocks) {
27
- Transforms.splitNodes(editor, {
28
- at: editor.selection,
29
- always: true,
30
- })
31
-
32
- const [nextBlock, nextBlockPath] = Editor.node(
33
- editor,
34
- Path.next(focusBlockPath),
35
- {depth: 1},
36
- )
37
-
38
- const nextChild = Node.child(nextBlock, 0)
39
- const firstChildIsInlineObject = !editor.isTextSpan(nextChild)
40
-
41
- if (firstChildIsInlineObject) {
42
- // If the first child in the next block is an inline object then we
43
- // add an empty span right before it to a place to put the cursor.
44
- // This is a Slate constraint that we have to adhere to.
45
- Transforms.insertNodes(
46
- editor,
47
- {
48
- _key: context.keyGenerator(),
49
- _type: 'span',
50
- text: '',
51
- marks: [],
52
- },
53
- {
54
- at: [nextBlockPath[0], 0],
55
- },
56
- )
57
- }
58
-
59
- Transforms.setSelection(editor, {
60
- anchor: {path: [...nextBlockPath, 0], offset: 0},
61
- focus: {path: [...nextBlockPath, 0], offset: 0},
62
- })
63
-
64
- /**
65
- * Assign new keys to markDefs that are now split across two blocks
66
- */
67
- if (
68
- editor.isTextBlock(nextBlock) &&
69
- nextBlock.markDefs &&
70
- nextBlock.markDefs.length > 0
71
- ) {
72
- const newMarkDefKeys = new Map<string, string>()
73
-
74
- const prevNodeSpans = Array.from(Node.children(editor, focusBlockPath))
75
- .map((entry) => entry[0])
76
- .filter((node) => editor.isTextSpan(node))
77
- const children = Node.children(editor, nextBlockPath)
78
-
79
- for (const [child, childPath] of children) {
80
- if (!editor.isTextSpan(child)) {
81
- continue
82
- }
83
-
84
- const marks = child.marks ?? []
85
-
86
- // Go through the marks of the span and figure out if any of
87
- // them refer to annotations that are also present in the
88
- // previous block
89
- for (const mark of marks) {
90
- if (
91
- schema.decorators.some((decorator) => decorator.name === mark)
92
- ) {
93
- continue
94
- }
95
-
96
- if (
97
- prevNodeSpans.some((prevNodeSpan) =>
98
- prevNodeSpan.marks?.includes(mark),
99
- ) &&
100
- !newMarkDefKeys.has(mark)
101
- ) {
102
- // This annotation is both present in the previous block
103
- // and this block, so let's assign a new key to it
104
- newMarkDefKeys.set(mark, keyGenerator())
105
- }
106
- }
107
-
108
- const newMarks = marks.map((mark) => newMarkDefKeys.get(mark) ?? mark)
109
-
110
- // No need to update the marks if they are the same
111
- if (!isEqual(marks, newMarks)) {
112
- Transforms.setNodes(
113
- editor,
114
- {marks: newMarks},
115
- {
116
- at: childPath,
117
- },
118
- )
119
- }
120
- }
121
-
122
- // Time to update all the markDefs that need a new key because
123
- // they've been split across blocks
124
- const newMarkDefs = nextBlock.markDefs.map((markDef) => ({
125
- ...markDef,
126
- _key: newMarkDefKeys.get(markDef._key) ?? markDef._key,
127
- }))
128
-
129
- // No need to update the markDefs if they are the same
130
- if (!isEqual(nextBlock.markDefs, newMarkDefs)) {
131
- Transforms.setNodes(
132
- editor,
133
- {markDefs: newMarkDefs},
134
- {
135
- at: nextBlockPath,
136
- match: (node) => editor.isTextBlock(node),
137
- },
138
- )
139
- }
140
- }
141
- return
142
- }
143
- }
144
-
145
- Transforms.splitNodes(editor, {always: true})
146
- }