@portabletext/editor 1.44.13 → 1.44.15

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.
@@ -4,7 +4,6 @@ import {
4
4
  type PortableTextBlock,
5
5
  type PortableTextChild,
6
6
  type PortableTextObject,
7
- type PortableTextSpan,
8
7
  type PortableTextTextBlock,
9
8
  } from '@sanity/types'
10
9
  import {
@@ -12,13 +11,12 @@ import {
12
11
  Node,
13
12
  Range,
14
13
  Element as SlateElement,
15
- Path as SlatePath,
16
14
  Text,
17
15
  Transforms,
18
16
  } from 'slate'
19
17
  import type {DOMNode} from 'slate-dom'
20
18
  import {ReactEditor} from 'slate-react'
21
- import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
19
+ import {addAnnotationActionImplementation} from '../../behavior-actions/behavior.action.annotation.add'
22
20
  import {debugWithName} from '../../internal-utils/debug'
23
21
  import {toSlateRange} from '../../internal-utils/ranges'
24
22
  import {
@@ -608,282 +606,3 @@ function isAnnotationActive({
608
606
  return false
609
607
  }
610
608
  }
611
-
612
- /**
613
- * @public
614
- */
615
- export type AddedAnnotationPaths = {
616
- /**
617
- * @deprecated An annotation may be applied to multiple blocks, resulting
618
- * in multiple `markDef`'s being created. Use `markDefPaths` instead.
619
- */
620
- markDefPath: Path
621
- markDefPaths: Array<Path>
622
- /**
623
- * @deprecated Does not return anything meaningful since an annotation
624
- * can span multiple blocks and spans. If references the span closest
625
- * to the focus point of the selection.
626
- */
627
- spanPath: Path
628
- }
629
-
630
- export const addAnnotationActionImplementation: BehaviorActionImplementation<
631
- 'annotation.add',
632
- AddedAnnotationPaths | undefined
633
- > = ({context, action}) => {
634
- const editor = action.editor
635
-
636
- if (!editor.selection || Range.isCollapsed(editor.selection)) {
637
- return
638
- }
639
-
640
- let paths: AddedAnnotationPaths | undefined = undefined
641
- let spanPath: Path | undefined
642
- let markDefPath: Path | undefined
643
- const markDefPaths: Path[] = []
644
-
645
- const selectedBlocks = Editor.nodes(editor, {
646
- at: editor.selection,
647
- match: (node) => editor.isTextBlock(node),
648
- reverse: Range.isBackward(editor.selection),
649
- })
650
-
651
- for (const [block, blockPath] of selectedBlocks) {
652
- if (block.children.length === 0) {
653
- continue
654
- }
655
-
656
- if (block.children.length === 1 && block.children[0].text === '') {
657
- continue
658
- }
659
-
660
- const annotationKey = context.keyGenerator()
661
- const markDefs = block.markDefs ?? []
662
- const existingMarkDef = markDefs.find(
663
- (markDef) =>
664
- markDef._type === action.annotation.name &&
665
- markDef._key === annotationKey,
666
- )
667
-
668
- if (existingMarkDef === undefined) {
669
- Transforms.setNodes(
670
- editor,
671
- {
672
- markDefs: [
673
- ...markDefs,
674
- {
675
- _type: action.annotation.name,
676
- _key: annotationKey,
677
- ...action.annotation.value,
678
- },
679
- ],
680
- },
681
- {at: blockPath},
682
- )
683
-
684
- markDefPath = [{_key: block._key}, 'markDefs', {_key: annotationKey}]
685
-
686
- if (Range.isBackward(editor.selection)) {
687
- markDefPaths.unshift(markDefPath)
688
- } else {
689
- markDefPaths.push(markDefPath)
690
- }
691
- }
692
-
693
- Transforms.setNodes(editor, {}, {match: Text.isText, split: true})
694
-
695
- const children = Node.children(editor, blockPath)
696
-
697
- for (const [span, path] of children) {
698
- if (!editor.isTextSpan(span)) {
699
- continue
700
- }
701
-
702
- if (!Range.includes(editor.selection, path)) {
703
- continue
704
- }
705
-
706
- const marks = span.marks ?? []
707
- const existingSameTypeAnnotations = marks.filter((mark) =>
708
- markDefs.some(
709
- (markDef) =>
710
- markDef._key === mark && markDef._type === action.annotation.name,
711
- ),
712
- )
713
-
714
- Transforms.setNodes(
715
- editor,
716
- {
717
- marks: [
718
- ...marks.filter(
719
- (mark) => !existingSameTypeAnnotations.includes(mark),
720
- ),
721
- annotationKey,
722
- ],
723
- },
724
- {at: path},
725
- )
726
-
727
- spanPath = [{_key: block._key}, 'children', {_key: span._key}]
728
- }
729
- }
730
-
731
- if (markDefPath && spanPath) {
732
- paths = {
733
- markDefPath,
734
- markDefPaths,
735
- spanPath,
736
- }
737
- }
738
-
739
- return paths
740
- }
741
-
742
- export const removeAnnotationActionImplementation: BehaviorActionImplementation<
743
- 'annotation.remove'
744
- > = ({action}) => {
745
- const editor = action.editor
746
-
747
- debug('Removing annotation', action.annotation.name)
748
-
749
- if (!editor.selection) {
750
- return
751
- }
752
-
753
- if (Range.isCollapsed(editor.selection)) {
754
- const [block, blockPath] = Editor.node(editor, editor.selection, {
755
- depth: 1,
756
- })
757
-
758
- if (!editor.isTextBlock(block)) {
759
- return
760
- }
761
-
762
- const markDefs = block.markDefs ?? []
763
- const potentialAnnotations = markDefs.filter(
764
- (markDef) => markDef._type === action.annotation.name,
765
- )
766
-
767
- const [selectedChild, selectedChildPath] = Editor.node(
768
- editor,
769
- editor.selection,
770
- {
771
- depth: 2,
772
- },
773
- )
774
-
775
- if (!editor.isTextSpan(selectedChild)) {
776
- return
777
- }
778
-
779
- const annotationToRemove = selectedChild.marks?.find((mark) =>
780
- potentialAnnotations.some((markDef) => markDef._key === mark),
781
- )
782
-
783
- if (!annotationToRemove) {
784
- return
785
- }
786
-
787
- const previousSpansWithSameAnnotation: Array<
788
- [span: PortableTextSpan, path: SlatePath]
789
- > = []
790
-
791
- for (const [child, childPath] of Node.children(editor, blockPath, {
792
- reverse: true,
793
- })) {
794
- if (!editor.isTextSpan(child)) {
795
- continue
796
- }
797
-
798
- if (!SlatePath.isBefore(childPath, selectedChildPath)) {
799
- continue
800
- }
801
-
802
- if (child.marks?.includes(annotationToRemove)) {
803
- previousSpansWithSameAnnotation.push([child, childPath])
804
- } else {
805
- break
806
- }
807
- }
808
-
809
- const nextSpansWithSameAnnotation: Array<
810
- [span: PortableTextSpan, path: SlatePath]
811
- > = []
812
-
813
- for (const [child, childPath] of Node.children(editor, blockPath)) {
814
- if (!editor.isTextSpan(child)) {
815
- continue
816
- }
817
-
818
- if (!SlatePath.isAfter(childPath, selectedChildPath)) {
819
- continue
820
- }
821
-
822
- if (child.marks?.includes(annotationToRemove)) {
823
- nextSpansWithSameAnnotation.push([child, childPath])
824
- } else {
825
- break
826
- }
827
- }
828
-
829
- for (const [child, childPath] of [
830
- ...previousSpansWithSameAnnotation,
831
- [selectedChild, selectedChildPath] as const,
832
- ...nextSpansWithSameAnnotation,
833
- ]) {
834
- Transforms.setNodes(
835
- editor,
836
- {
837
- marks: child.marks?.filter((mark) => mark !== annotationToRemove),
838
- },
839
- {at: childPath},
840
- )
841
- }
842
- } else {
843
- Transforms.setNodes(
844
- editor,
845
- {},
846
- {
847
- match: (node) => editor.isTextSpan(node),
848
- split: true,
849
- hanging: true,
850
- },
851
- )
852
-
853
- const blocks = Editor.nodes(editor, {
854
- at: editor.selection,
855
- match: (node) => editor.isTextBlock(node),
856
- })
857
-
858
- for (const [block, blockPath] of blocks) {
859
- const children = Node.children(editor, blockPath)
860
-
861
- for (const [child, childPath] of children) {
862
- if (!editor.isTextSpan(child)) {
863
- continue
864
- }
865
-
866
- if (!Range.includes(editor.selection, childPath)) {
867
- continue
868
- }
869
-
870
- const markDefs = block.markDefs ?? []
871
- const marks = child.marks ?? []
872
- const marksWithoutAnnotation = marks.filter((mark) => {
873
- const markDef = markDefs.find((markDef) => markDef._key === mark)
874
- return markDef?._type !== action.annotation.name
875
- })
876
-
877
- if (marksWithoutAnnotation.length !== marks.length) {
878
- Transforms.setNodes(
879
- editor,
880
- {
881
- marks: marksWithoutAnnotation,
882
- },
883
- {at: childPath},
884
- )
885
- }
886
- }
887
- }
888
- }
889
- }
package/src/index.ts CHANGED
@@ -4,6 +4,8 @@ export type {
4
4
  PortableTextChild,
5
5
  PortableTextSpan,
6
6
  } from '@sanity/types'
7
+ export type {AddedAnnotationPaths} from './behavior-actions/behavior.action.annotation.add'
8
+ export {EditorEventListener} from './editor-event-listener'
7
9
  export type {Editor, EditorConfig, EditorEvent} from './editor/create-editor'
8
10
  export {
9
11
  defineSchema,
@@ -28,57 +30,55 @@ export type {EditorContext, EditorSnapshot} from './editor/editor-snapshot'
28
30
  export {usePortableTextEditor} from './editor/hooks/usePortableTextEditor'
29
31
  export {usePortableTextEditorSelection} from './editor/hooks/usePortableTextEditorSelection'
30
32
  export {defaultKeyGenerator as keyGenerator} from './editor/key-generator'
31
- export type {AddedAnnotationPaths} from './editor/plugins/createWithEditableAPI'
32
33
  export {PortableTextEditor} from './editor/PortableTextEditor'
33
34
  export type {PortableTextEditorProps} from './editor/PortableTextEditor'
34
- export {EditorEventListener} from './editor-event-listener'
35
35
  export type {BlockOffset} from './types/block-offset'
36
36
  export type {
37
- EditableAPIDeleteOptions,
38
- EditableAPI,
39
- EditorSelectionPoint,
40
- EditorSelection,
41
- MutationChange,
42
- PatchChange,
43
- ValueChange,
44
- SelectionChange,
45
- FocusChange,
46
- UnsetChange,
37
+ BlockAnnotationRenderProps,
38
+ BlockChildRenderProps,
39
+ BlockDecoratorRenderProps,
40
+ BlockListItemRenderProps,
41
+ BlockRenderProps,
42
+ BlockStyleRenderProps,
47
43
  BlurChange,
48
- LoadingChange,
49
- ReadyChange,
50
- ErrorChange,
51
- InvalidValueResolution,
52
- InvalidValue,
53
- UndoChange,
54
- RedoChange,
55
44
  ConnectionChange,
45
+ EditableAPI,
46
+ EditableAPIDeleteOptions,
56
47
  EditorChange,
57
48
  EditorChanges,
49
+ EditorSelection,
50
+ EditorSelectionPoint,
51
+ ErrorChange,
52
+ FocusChange,
53
+ InvalidValue,
54
+ InvalidValueResolution,
55
+ LoadingChange,
56
+ MutationChange,
57
+ OnBeforeInputFn,
58
+ OnCopyFn,
59
+ OnPasteFn,
58
60
  OnPasteResult,
59
61
  OnPasteResultOrPromise,
60
62
  PasteData,
61
- OnPasteFn,
62
- OnBeforeInputFn,
63
- OnCopyFn,
63
+ PatchChange,
64
64
  PatchObservable,
65
- BlockRenderProps,
66
- BlockChildRenderProps,
67
- BlockAnnotationRenderProps,
68
- BlockDecoratorRenderProps,
69
- BlockListItemRenderProps,
65
+ PortableTextMemberSchemaTypes,
66
+ RangeDecoration,
67
+ RangeDecorationOnMovedDetails,
68
+ ReadyChange,
69
+ RedoChange,
70
+ RenderAnnotationFunction,
70
71
  RenderBlockFunction,
71
72
  RenderChildFunction,
73
+ RenderDecoratorFunction,
72
74
  RenderEditableFunction,
73
- RenderAnnotationFunction,
75
+ RenderListItemFunction,
74
76
  RenderPlaceholderFunction,
75
77
  RenderStyleFunction,
76
- BlockStyleRenderProps,
77
- RenderListItemFunction,
78
- RenderDecoratorFunction,
79
78
  ScrollSelectionIntoViewFunction,
80
- RangeDecorationOnMovedDetails,
81
- RangeDecoration,
82
- PortableTextMemberSchemaTypes,
79
+ SelectionChange,
80
+ UndoChange,
81
+ UnsetChange,
82
+ ValueChange,
83
83
  } from './types/editor'
84
84
  export type {HotkeyOptions} from './types/options'