@portabletext/editor 1.50.4 → 1.50.6

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 (43) hide show
  1. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs +4 -14
  2. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -1
  3. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js +4 -14
  4. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -1
  5. package/lib/behaviors/index.d.cts +9 -1
  6. package/lib/behaviors/index.d.ts +9 -1
  7. package/lib/index.cjs +271 -205
  8. package/lib/index.cjs.map +1 -1
  9. package/lib/index.d.cts +9 -1
  10. package/lib/index.d.ts +9 -1
  11. package/lib/index.js +280 -214
  12. package/lib/index.js.map +1 -1
  13. package/lib/plugins/index.d.cts +9 -1
  14. package/lib/plugins/index.d.ts +9 -1
  15. package/lib/selectors/index.d.cts +9 -1
  16. package/lib/selectors/index.d.ts +9 -1
  17. package/lib/utils/index.d.cts +9 -1
  18. package/lib/utils/index.d.ts +9 -1
  19. package/package.json +3 -3
  20. package/src/behaviors/behavior.abstract.delete.ts +6 -2
  21. package/src/behaviors/behavior.abstract.ts +1 -1
  22. package/src/editor/create-slate-editor.tsx +2 -0
  23. package/src/editor/editor-selector.ts +10 -4
  24. package/src/editor/editor-snapshot.ts +12 -5
  25. package/src/editor/get-active-annotations.ts +15 -0
  26. package/src/editor/get-active-decorators.ts +23 -9
  27. package/src/editor/plugins/create-with-event-listeners.ts +9 -3
  28. package/src/editor/plugins/createWithEditableAPI.ts +16 -14
  29. package/src/editor/plugins/createWithPortableTextMarkModel.ts +26 -190
  30. package/src/editor/plugins/slate-plugin.update-mark-state.ts +21 -0
  31. package/src/editor/plugins/slate-plugin.update-value.ts +1 -4
  32. package/src/editor/plugins/with-plugins.ts +8 -1
  33. package/src/internal-utils/create-test-snapshot.ts +2 -1
  34. package/src/internal-utils/mark-state.ts +172 -0
  35. package/src/internal-utils/slate-utils.ts +52 -0
  36. package/src/operations/behavior.operation.decorator.add.ts +7 -11
  37. package/src/operations/behavior.operation.insert.text.ts +48 -8
  38. package/src/selectors/selector.get-active-annotations.ts +2 -1
  39. package/src/selectors/selector.is-active-annotation.ts +7 -39
  40. package/src/selectors/selector.is-active-decorator.ts +1 -1
  41. package/src/types/editor.ts +3 -0
  42. package/src/editor/get-value.ts +0 -18
  43. package/src/selectors/selector.get-active-annotations.test.ts +0 -141
@@ -34,6 +34,7 @@ export const getActiveAnnotations: EditorSelector<Array<PortableTextObject>> = (
34
34
  }
35
35
  }
36
36
 
37
+ const activeAnnotations = snapshot.beta.activeAnnotations
37
38
  const selectionMarkDefs = selectedBlocks.flatMap((block) =>
38
39
  isTextBlock(snapshot.context, block.node)
39
40
  ? (block.node.markDefs ?? [])
@@ -41,6 +42,6 @@ export const getActiveAnnotations: EditorSelector<Array<PortableTextObject>> = (
41
42
  )
42
43
 
43
44
  return selectionMarkDefs.filter((markDef) =>
44
- selectedSpans.some((span) => span.node.marks?.includes(markDef._key)),
45
+ activeAnnotations.includes(markDef._key),
45
46
  )
46
47
  }
@@ -1,8 +1,6 @@
1
1
  import type {EditorSelector} from '../editor/editor-selector'
2
2
  import {isTextBlock} from '../internal-utils/parse-blocks'
3
- import {getSelectedSpans} from './selector.get-selected-spans'
4
- import {isSelectionExpanded} from './selector.is-selection-expanded'
5
- import {getFocusSpan, getSelectedBlocks} from './selectors'
3
+ import {getSelectedBlocks} from './selectors'
6
4
 
7
5
  /**
8
6
  * @public
@@ -11,48 +9,18 @@ export function isActiveAnnotation(
11
9
  annotation: string,
12
10
  ): EditorSelector<boolean> {
13
11
  return (snapshot) => {
14
- if (!snapshot.context.selection) {
15
- return false
16
- }
17
-
18
12
  const selectedBlocks = getSelectedBlocks(snapshot)
19
- const focusSpan = getFocusSpan(snapshot)
20
-
21
- const selectedSpans = isSelectionExpanded(snapshot)
22
- ? getSelectedSpans(snapshot)
23
- : focusSpan
24
- ? [focusSpan]
25
- : []
26
-
27
- if (selectedSpans.length === 0) {
28
- return false
29
- }
30
-
31
- if (
32
- selectedSpans.some(
33
- (span) => !span.node.marks || span.node.marks?.length === 0,
34
- )
35
- ) {
36
- return false
37
- }
38
-
39
13
  const selectionMarkDefs = selectedBlocks.flatMap((block) =>
40
14
  isTextBlock(snapshot.context, block.node)
41
15
  ? (block.node.markDefs ?? [])
42
16
  : [],
43
17
  )
18
+ const activeMarkDefs = selectionMarkDefs.filter(
19
+ (markDef) =>
20
+ markDef._type === annotation &&
21
+ snapshot.beta.activeAnnotations.includes(markDef._key),
22
+ )
44
23
 
45
- return selectedSpans.every((span) => {
46
- const spanMarkDefs =
47
- span.node.marks?.flatMap((mark) => {
48
- const markDef = selectionMarkDefs.find(
49
- (markDef) => markDef._key === mark,
50
- )
51
-
52
- return markDef ? [markDef._type] : []
53
- }) ?? []
54
-
55
- return spanMarkDefs.includes(annotation)
56
- })
24
+ return activeMarkDefs.length > 0
57
25
  }
58
26
  }
@@ -16,6 +16,6 @@ export function isActiveDecorator(decorator: string): EditorSelector<boolean> {
16
16
  )
17
17
  }
18
18
 
19
- return snapshot.context.activeDecorators.includes(decorator)
19
+ return snapshot.beta.activeDecorators.includes(decorator)
20
20
  }
21
21
  }
@@ -29,6 +29,7 @@ import type {DOMNode} from 'slate-dom'
29
29
  import type {ReactEditor} from 'slate-react'
30
30
  import type {PortableTextEditableProps} from '../editor/Editable'
31
31
  import type {PortableTextEditor} from '../editor/PortableTextEditor'
32
+ import type {MarkState} from '../internal-utils/mark-state'
32
33
  import type {BlockPath} from './paths'
33
34
 
34
35
  /** @beta */
@@ -125,6 +126,8 @@ export interface PortableTextSlateEditor extends ReactEditor {
125
126
  isTextSpan: (value: unknown) => value is PortableTextSpan
126
127
  isListBlock: (value: unknown) => value is PortableTextListBlock
127
128
  value: Array<PortableTextBlock>
129
+ decoratorState: Record<string, boolean | undefined>
130
+ markState: MarkState | undefined
128
131
 
129
132
  /**
130
133
  * Use hotkeys
@@ -1,18 +0,0 @@
1
- import {fromSlateValue} from '../internal-utils/values'
2
- import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
3
- import type {PortableTextSlateEditor} from '../types/editor'
4
- import type {EditorActor} from './editor-machine'
5
-
6
- export function getValue({
7
- editorActorSnapshot,
8
- slateEditorInstance,
9
- }: {
10
- editorActorSnapshot: ReturnType<EditorActor['getSnapshot']>
11
- slateEditorInstance: PortableTextSlateEditor
12
- }) {
13
- return fromSlateValue(
14
- slateEditorInstance.children,
15
- editorActorSnapshot.context.schema.block.name,
16
- KEY_TO_VALUE_ELEMENT.get(slateEditorInstance),
17
- )
18
- }
@@ -1,141 +0,0 @@
1
- import type {PortableTextBlock} from '@sanity/types'
2
- import {expect, test} from 'vitest'
3
- import type {EditorSelection} from '..'
4
- import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
5
- import {getActiveAnnotations} from './selector.get-active-annotations'
6
-
7
- function snapshot(value: Array<PortableTextBlock>, selection: EditorSelection) {
8
- return createTestSnapshot({
9
- context: {
10
- value,
11
- selection,
12
- },
13
- })
14
- }
15
-
16
- const link = {
17
- _key: 'k4',
18
- _type: 'link',
19
- href: 'https://example.com',
20
- }
21
-
22
- const comment = {
23
- _key: 'k5',
24
- _type: 'comment',
25
- comment: 'Consider rewriting this',
26
- }
27
-
28
- const block = {
29
- _type: 'block',
30
- _key: 'k0',
31
- children: [
32
- {
33
- _key: 'k1',
34
- _type: 'span',
35
- text: 'foo',
36
- marks: ['strong'],
37
- },
38
- {
39
- _type: 'span',
40
- _key: 'k2',
41
- text: 'bar',
42
- marks: [link._key, comment._key, 'strong'],
43
- },
44
- {
45
- _key: 'k3',
46
- _type: 'span',
47
- text: 'baz',
48
- marks: [comment._key, 'strong'],
49
- },
50
- ],
51
- markDefs: [link, comment],
52
- }
53
-
54
- test(getActiveAnnotations.name, () => {
55
- expect(getActiveAnnotations(snapshot([], null))).toEqual([])
56
- expect(getActiveAnnotations(snapshot([block], null))).toEqual([])
57
- expect(
58
- getActiveAnnotations(
59
- snapshot([block], {
60
- anchor: {
61
- path: [{_key: 'k0'}, 'children', {_key: 'k1'}],
62
- offset: 0,
63
- },
64
- focus: {
65
- path: [{_key: 'k0'}, 'children', {_key: 'k1'}],
66
- offset: 3,
67
- },
68
- }),
69
- ),
70
- ).toEqual([])
71
- expect(
72
- getActiveAnnotations(
73
- snapshot([block], {
74
- anchor: {
75
- path: [{_key: 'k0'}, 'children', {_key: 'k1'}],
76
- offset: 0,
77
- },
78
- focus: {
79
- path: [{_key: 'k0'}, 'children', {_key: 'k2'}],
80
- offset: 3,
81
- },
82
- }),
83
- ),
84
- ).toEqual([link, comment])
85
- expect(
86
- getActiveAnnotations(
87
- snapshot([block], {
88
- anchor: {
89
- path: [{_key: 'k0'}, 'children', {_key: 'k1'}],
90
- offset: 0,
91
- },
92
- focus: {
93
- path: [{_key: 'k0'}, 'children', {_key: 'k3'}],
94
- offset: 3,
95
- },
96
- }),
97
- ),
98
- ).toEqual([link, comment])
99
- expect(
100
- getActiveAnnotations(
101
- snapshot([block], {
102
- anchor: {
103
- path: [{_key: 'k0'}, 'children', {_key: 'k3'}],
104
- offset: 0,
105
- },
106
- focus: {
107
- path: [{_key: 'k0'}, 'children', {_key: 'k3'}],
108
- offset: 0,
109
- },
110
- }),
111
- ),
112
- ).toEqual([])
113
- expect(
114
- getActiveAnnotations(
115
- snapshot([block], {
116
- anchor: {
117
- path: [{_key: 'k0'}, 'children', {_key: 'k3'}],
118
- offset: 0,
119
- },
120
- focus: {
121
- path: [{_key: 'k0'}, 'children', {_key: 'k3'}],
122
- offset: 3,
123
- },
124
- }),
125
- ),
126
- ).toEqual([comment])
127
- expect(
128
- getActiveAnnotations(
129
- snapshot([block], {
130
- anchor: {
131
- path: [{_key: 'k0'}, 'children', {_key: 'k3'}],
132
- offset: 3,
133
- },
134
- focus: {
135
- path: [{_key: 'k0'}, 'children', {_key: 'k3'}],
136
- offset: 3,
137
- },
138
- }),
139
- ),
140
- ).toEqual([])
141
- })