@portabletext/editor 1.43.1 → 1.44.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.
Files changed (75) hide show
  1. package/lib/_chunks-cjs/behavior.markdown.cjs +1 -1
  2. package/lib/_chunks-cjs/editor-provider.cjs +555 -794
  3. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  4. package/lib/_chunks-cjs/parse-blocks.cjs +151 -0
  5. package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -0
  6. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs +2 -1
  7. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs.map +1 -1
  8. package/lib/_chunks-cjs/{selector.is-active-style.cjs → selector.is-selecting-entire-blocks.cjs} +47 -11
  9. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -0
  10. package/lib/_chunks-cjs/util.merge-text-blocks.cjs +3 -3
  11. package/lib/_chunks-cjs/util.merge-text-blocks.cjs.map +1 -1
  12. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +0 -149
  13. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +1 -1
  14. package/lib/_chunks-es/behavior.markdown.js +1 -1
  15. package/lib/_chunks-es/editor-provider.js +539 -777
  16. package/lib/_chunks-es/editor-provider.js.map +1 -1
  17. package/lib/_chunks-es/parse-blocks.js +152 -0
  18. package/lib/_chunks-es/parse-blocks.js.map +1 -0
  19. package/lib/_chunks-es/selector.is-overlapping-selection.js +2 -1
  20. package/lib/_chunks-es/selector.is-overlapping-selection.js.map +1 -1
  21. package/lib/_chunks-es/{selector.is-active-style.js → selector.is-selecting-entire-blocks.js} +49 -12
  22. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -0
  23. package/lib/_chunks-es/util.merge-text-blocks.js +1 -1
  24. package/lib/_chunks-es/util.selection-point-to-block-offset.js +0 -149
  25. package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +1 -1
  26. package/lib/behaviors/index.d.cts +2819 -6421
  27. package/lib/behaviors/index.d.ts +2819 -6421
  28. package/lib/index.cjs +4 -4
  29. package/lib/index.cjs.map +1 -1
  30. package/lib/index.d.cts +2496 -6025
  31. package/lib/index.d.ts +2496 -6025
  32. package/lib/index.js +3 -3
  33. package/lib/plugins/index.cjs +1 -1
  34. package/lib/plugins/index.d.cts +2496 -6025
  35. package/lib/plugins/index.d.ts +2496 -6025
  36. package/lib/plugins/index.js +1 -1
  37. package/lib/selectors/index.cjs +12 -11
  38. package/lib/selectors/index.cjs.map +1 -1
  39. package/lib/selectors/index.d.cts +2464 -6018
  40. package/lib/selectors/index.d.ts +2464 -6018
  41. package/lib/selectors/index.js +2 -1
  42. package/lib/utils/index.d.cts +2458 -6022
  43. package/lib/utils/index.d.ts +2458 -6022
  44. package/package.json +2 -2
  45. package/src/behavior-actions/behavior.actions.ts +2 -152
  46. package/src/behaviors/behavior.default.ts +52 -120
  47. package/src/behaviors/behavior.internal.annotation.ts +26 -0
  48. package/src/behaviors/behavior.internal.decorator.ts +47 -0
  49. package/src/behaviors/behavior.internal.insert.ts +118 -0
  50. package/src/behaviors/behavior.internal.list-item.ts +61 -0
  51. package/src/behaviors/behavior.internal.select.ts +62 -0
  52. package/src/behaviors/behavior.internal.style.ts +54 -0
  53. package/src/behaviors/behavior.perform-event.ts +15 -13
  54. package/src/behaviors/behavior.types.event.ts +155 -108
  55. package/src/editor/create-editor.ts +2 -2
  56. package/src/editor/editor-machine.ts +3 -4
  57. package/src/editor/plugins/createWithEditableAPI.ts +1 -32
  58. package/src/editor/plugins/createWithPortableTextMarkModel.ts +0 -29
  59. package/src/internal-utils/slate-utils.ts +52 -0
  60. package/src/plugins/plugin.internal.editor-actor-ref.tsx +15 -0
  61. package/src/plugins/plugin.internal.slate-editor-ref.tsx +15 -0
  62. package/src/selectors/index.ts +2 -1
  63. package/src/selectors/selector.get-selected-text-blocks.ts +67 -0
  64. package/lib/_chunks-cjs/selector.is-active-style.cjs.map +0 -1
  65. package/lib/_chunks-es/selector.is-active-style.js.map +0 -1
  66. package/src/behavior-actions/behavior.action.data-transfer-set.ts +0 -7
  67. package/src/behavior-actions/behavior.action.deserialization.failure.ts +0 -9
  68. package/src/behavior-actions/behavior.action.deserialization.success.ts +0 -16
  69. package/src/behavior-actions/behavior.action.insert-blocks.ts +0 -140
  70. package/src/behavior-actions/behavior.action.list-item.ts +0 -100
  71. package/src/behavior-actions/behavior.action.select.next-block.ts +0 -44
  72. package/src/behavior-actions/behavior.action.select.previous-block.ts +0 -48
  73. package/src/behavior-actions/behavior.action.serialization.failure.ts +0 -9
  74. package/src/behavior-actions/behavior.action.serialization.success.ts +0 -17
  75. package/src/behavior-actions/behavior.action.style.ts +0 -108
@@ -1,100 +0,0 @@
1
- import {Editor, Transforms} from 'slate'
2
- import type {BehaviorActionImplementation} from './behavior.actions'
3
- import {createGuards} from './behavior.guards'
4
-
5
- export const toggleListItemActionImplementation: BehaviorActionImplementation<
6
- 'list item.toggle'
7
- > = ({context, action}) => {
8
- const isActive = isListItemActive({
9
- editor: action.editor,
10
- listItem: action.listItem,
11
- })
12
-
13
- if (isActive) {
14
- removeListItemActionImplementation({
15
- context,
16
- action: {...action, type: 'list item.remove'},
17
- })
18
- } else {
19
- addListItemActionImplementation({
20
- context,
21
- action: {...action, type: 'list item.add'},
22
- })
23
- }
24
- }
25
-
26
- export const removeListItemActionImplementation: BehaviorActionImplementation<
27
- 'list item.remove'
28
- > = ({context, action}) => {
29
- if (!action.editor.selection) {
30
- return
31
- }
32
-
33
- const guards = createGuards(context)
34
-
35
- const selectedBlocks = [
36
- ...Editor.nodes(action.editor, {
37
- at: action.editor.selection,
38
- match: (node) => guards.isListBlock(node),
39
- }),
40
- ]
41
-
42
- for (const [, at] of selectedBlocks) {
43
- Transforms.unsetNodes(action.editor, ['listItem', 'level'], {at})
44
- }
45
- }
46
-
47
- export const addListItemActionImplementation: BehaviorActionImplementation<
48
- 'list item.add'
49
- > = ({context, action}) => {
50
- if (!action.editor.selection) {
51
- return
52
- }
53
-
54
- const guards = createGuards(context)
55
-
56
- const selectedBlocks = [
57
- ...Editor.nodes(action.editor, {
58
- at: action.editor.selection,
59
- match: (node) => guards.isTextBlock(node),
60
- }),
61
- ]
62
-
63
- for (const [, at] of selectedBlocks) {
64
- Transforms.setNodes(
65
- action.editor,
66
- {
67
- level: 1,
68
- listItem: action.listItem,
69
- },
70
- {at},
71
- )
72
- }
73
- }
74
-
75
- export function isListItemActive({
76
- editor,
77
- listItem,
78
- }: {
79
- editor: Editor
80
- listItem: string
81
- }): boolean {
82
- if (!editor.selection) {
83
- return false
84
- }
85
-
86
- const selectedBlocks = [
87
- ...Editor.nodes(editor, {
88
- at: editor.selection,
89
- match: (node) => editor.isTextBlock(node),
90
- }),
91
- ]
92
-
93
- if (selectedBlocks.length > 0) {
94
- return selectedBlocks.every(
95
- ([node]) => editor.isListBlock(node) && node.listItem === listItem,
96
- )
97
- }
98
-
99
- return false
100
- }
@@ -1,44 +0,0 @@
1
- import {Editor} from 'slate'
2
- import {toPortableTextRange} from '../internal-utils/ranges'
3
- import {selectActionImplementation} from './behavior.action.select'
4
- import type {BehaviorActionImplementation} from './behavior.actions'
5
-
6
- export const selectNextBlockActionImplementation: BehaviorActionImplementation<
7
- 'select.next block'
8
- > = ({context, action}) => {
9
- if (!action.editor.selection) {
10
- console.error('Unable to select previous block without a selection')
11
- return
12
- }
13
-
14
- const blockPath = action.editor.selection.focus.path.slice(0, 1)
15
- const nextBlockPath = [blockPath[0] + 1]
16
-
17
- const position =
18
- action.select === 'end'
19
- ? Editor.end(action.editor, nextBlockPath)
20
- : Editor.start(action.editor, nextBlockPath)
21
-
22
- const newSelection = toPortableTextRange(
23
- action.editor.children,
24
- {
25
- anchor: position,
26
- focus: position,
27
- },
28
- context.schema,
29
- )
30
-
31
- if (!newSelection) {
32
- console.error('Could not find selection for next block')
33
- return
34
- }
35
-
36
- selectActionImplementation({
37
- context,
38
- action: {
39
- type: 'select',
40
- selection: newSelection,
41
- editor: action.editor,
42
- },
43
- })
44
- }
@@ -1,48 +0,0 @@
1
- import {Editor, Path} from 'slate'
2
- import {toPortableTextRange} from '../internal-utils/ranges'
3
- import {selectActionImplementation} from './behavior.action.select'
4
- import type {BehaviorActionImplementation} from './behavior.actions'
5
-
6
- export const selectPreviousBlockActionImplementation: BehaviorActionImplementation<
7
- 'select.previous block'
8
- > = ({context, action}) => {
9
- if (!action.editor.selection) {
10
- console.error('Unable to select previous block without a selection')
11
- return
12
- }
13
-
14
- const blockPath = action.editor.selection.focus.path.slice(0, 1)
15
-
16
- if (!Path.hasPrevious(blockPath)) {
17
- console.error("There's no previous block to select")
18
- return
19
- }
20
-
21
- const position =
22
- action.select === 'end'
23
- ? Editor.end(action.editor, Path.previous(blockPath))
24
- : Editor.start(action.editor, Path.previous(blockPath))
25
-
26
- const newSelection = toPortableTextRange(
27
- action.editor.children,
28
- {
29
- anchor: position,
30
- focus: position,
31
- },
32
- context.schema,
33
- )
34
-
35
- if (!newSelection) {
36
- console.error('Could not find selection for previous block')
37
- return
38
- }
39
-
40
- selectActionImplementation({
41
- context,
42
- action: {
43
- type: 'select',
44
- selection: newSelection,
45
- editor: action.editor,
46
- },
47
- })
48
- }
@@ -1,9 +0,0 @@
1
- import type {BehaviorActionImplementation} from './behavior.actions'
2
-
3
- export const serializationFailureActionImplementation: BehaviorActionImplementation<
4
- 'serialization.failure'
5
- > = ({action}) => {
6
- console.warn(
7
- `Serialization of ${action.mimeType} failed with reason "${action.reason}"`,
8
- )
9
- }
@@ -1,17 +0,0 @@
1
- import {dataTransferSetActionImplementation} from './behavior.action.data-transfer-set'
2
- import type {BehaviorActionImplementation} from './behavior.actions'
3
-
4
- export const serializationSuccessActionImplementation: BehaviorActionImplementation<
5
- 'serialization.success'
6
- > = ({context, action}) => {
7
- dataTransferSetActionImplementation({
8
- context,
9
- action: {
10
- type: 'data transfer.set',
11
- data: action.data,
12
- dataTransfer: action.originEvent.originEvent.dataTransfer,
13
- mimeType: action.mimeType,
14
- editor: action.editor,
15
- },
16
- })
17
- }
@@ -1,108 +0,0 @@
1
- import {Editor, Transforms} from 'slate'
2
- import type {BehaviorActionImplementation} from './behavior.actions'
3
- import {createGuards} from './behavior.guards'
4
-
5
- export const toggleStyleActionImplementation: BehaviorActionImplementation<
6
- 'style.toggle'
7
- > = ({context, action}) => {
8
- const isActive = isStyleActive({
9
- editor: action.editor,
10
- style: action.style,
11
- })
12
-
13
- if (isActive) {
14
- removeStyleActionImplementation({
15
- context,
16
- action: {...action, type: 'style.remove'},
17
- })
18
- } else {
19
- addStyleActionImplementation({
20
- context,
21
- action: {...action, type: 'style.add'},
22
- })
23
- }
24
- }
25
-
26
- export const removeStyleActionImplementation: BehaviorActionImplementation<
27
- 'style.remove'
28
- > = ({context, action}) => {
29
- if (!action.editor.selection) {
30
- return
31
- }
32
-
33
- const defaultStyle = context.schema.styles[0].value
34
- const guards = createGuards(context)
35
-
36
- const selectedBlocks = [
37
- ...Editor.nodes(action.editor, {
38
- at: action.editor.selection,
39
- match: (node) => guards.isTextBlock(node),
40
- }),
41
- ]
42
-
43
- for (const [, at] of selectedBlocks) {
44
- Transforms.setNodes(
45
- action.editor,
46
- {
47
- style: defaultStyle,
48
- },
49
- {
50
- at,
51
- },
52
- )
53
- }
54
- }
55
-
56
- export const addStyleActionImplementation: BehaviorActionImplementation<
57
- 'style.add'
58
- > = ({context, action}) => {
59
- if (!action.editor.selection) {
60
- return
61
- }
62
-
63
- const guards = createGuards(context)
64
-
65
- const selectedBlocks = [
66
- ...Editor.nodes(action.editor, {
67
- at: action.editor.selection,
68
- match: (node) => guards.isTextBlock(node),
69
- }),
70
- ]
71
-
72
- for (const [, at] of selectedBlocks) {
73
- Transforms.setNodes(
74
- action.editor,
75
- {
76
- style: action.style,
77
- },
78
- {
79
- at,
80
- },
81
- )
82
- }
83
- }
84
-
85
- export function isStyleActive({
86
- editor,
87
- style,
88
- }: {
89
- editor: Editor
90
- style: string
91
- }): boolean {
92
- if (!editor.selection) {
93
- return false
94
- }
95
-
96
- const selectedBlocks = [
97
- ...Editor.nodes(editor, {
98
- at: editor.selection,
99
- match: (node) => editor.isTextBlock(node),
100
- }),
101
- ]
102
-
103
- if (selectedBlocks.length > 0) {
104
- return selectedBlocks.every(([node]) => node.style === style)
105
- }
106
-
107
- return false
108
- }