@portabletext/toolbar 2.1.1 → 2.2.0

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,48 @@
1
+ import {useEditor} from '@portabletext/editor'
2
+ import {defineBehavior, execute, raise} from '@portabletext/editor/behaviors'
3
+ import {isActiveAnnotation} from '@portabletext/editor/selectors'
4
+ import {useEffect} from 'react'
5
+ import type {ToolbarAnnotationSchemaType} from './use-toolbar-schema'
6
+
7
+ export function useMutuallyExclusiveAnnotation(props: {
8
+ schemaType: ToolbarAnnotationSchemaType
9
+ }) {
10
+ const editor = useEditor()
11
+
12
+ useEffect(() => {
13
+ const mutuallyExclusive = props.schemaType.mutuallyExclusive
14
+
15
+ if (!mutuallyExclusive) {
16
+ return
17
+ }
18
+
19
+ return editor.registerBehavior({
20
+ behavior: defineBehavior({
21
+ on: 'annotation.add',
22
+ guard: ({snapshot, event}) => {
23
+ if (event.annotation.name !== props.schemaType.name) {
24
+ return false
25
+ }
26
+
27
+ const activeMutuallyExclusive = mutuallyExclusive.filter(
28
+ (annotation) =>
29
+ isActiveAnnotation(annotation, {mode: 'partial'})(snapshot),
30
+ )
31
+
32
+ return {activeMutuallyExclusive}
33
+ },
34
+ actions: [
35
+ ({event}, {activeMutuallyExclusive}) => [
36
+ ...activeMutuallyExclusive.map((annotation) =>
37
+ raise({
38
+ type: 'annotation.remove',
39
+ annotation: {name: annotation},
40
+ }),
41
+ ),
42
+ execute(event),
43
+ ],
44
+ ],
45
+ }),
46
+ })
47
+ }, [editor, props.schemaType.name, props.schemaType.mutuallyExclusive])
48
+ }
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  useEditor,
3
3
  useEditorSelector,
4
+ type AnnotationDefinition,
4
5
  type AnnotationSchemaType,
5
6
  type BlockObjectSchemaType,
6
7
  type DecoratorDefinition,
@@ -128,6 +129,7 @@ export type ToolbarAnnotationSchemaType = AnnotationSchemaType & {
128
129
  icon?: React.ComponentType
129
130
  defaultValues?: Record<string, unknown>
130
131
  shortcut?: KeyboardShortcut
132
+ mutuallyExclusive?: ReadonlyArray<AnnotationDefinition['name']>
131
133
  }
132
134
 
133
135
  /**