@portabletext/editor 2.8.1 → 2.8.3

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.
@@ -44,23 +44,6 @@ type BehaviorConfig = {
44
44
  behavior: Behavior;
45
45
  priority: EditorPriority;
46
46
  };
47
- /**
48
- * @public
49
- */
50
- type AddedAnnotationPaths = {
51
- /**
52
- * @deprecated An annotation may be applied to multiple blocks, resulting
53
- * in multiple `markDef`'s being created. Use `markDefPaths` instead.
54
- */
55
- markDefPath: Path;
56
- markDefPaths: Array<Path>;
57
- /**
58
- * @deprecated Does not return anything meaningful since an annotation
59
- * can span multiple blocks and spans. If references the span closest
60
- * to the focus point of the selection.
61
- */
62
- spanPath: Path;
63
- };
64
47
  /**
65
48
  * @public
66
49
  */
@@ -640,6 +623,23 @@ type ChildPath = [{
640
623
  interface EditableAPIDeleteOptions {
641
624
  mode?: 'blocks' | 'children' | 'selected';
642
625
  }
626
+ /**
627
+ * @public
628
+ */
629
+ type AddedAnnotationPaths = {
630
+ /**
631
+ * @deprecated An annotation may be applied to multiple blocks, resulting
632
+ * in multiple `markDef`'s being created. Use `markDefPaths` instead.
633
+ */
634
+ markDefPath: Path;
635
+ markDefPaths: Array<Path>;
636
+ /**
637
+ * @deprecated Does not return anything meaningful since an annotation
638
+ * can span multiple blocks and spans. If references the span closest
639
+ * to the focus point of the selection.
640
+ */
641
+ spanPath: Path;
642
+ };
643
643
  /** @beta */
644
644
  interface EditableAPI {
645
645
  activeAnnotations: () => PortableTextObject[];
@@ -648,11 +648,7 @@ interface EditableAPI {
648
648
  name: string;
649
649
  }>(type: TSchemaType, value?: {
650
650
  [prop: string]: unknown;
651
- }) => {
652
- markDefPath: Path;
653
- markDefPaths: Array<Path>;
654
- spanPath: Path;
655
- } | undefined;
651
+ }) => AddedAnnotationPaths | undefined;
656
652
  blur: () => void;
657
653
  delete: (selection: EditorSelection, options?: EditableAPIDeleteOptions) => void;
658
654
  findByPath: (path: Path) => [PortableTextBlock | PortableTextChild | undefined, Path | undefined];
@@ -1506,7 +1502,7 @@ declare const editorMachine: xstate241.StateMachine<{
1506
1502
  }, xstate241.AnyEventObject>;
1507
1503
  }) => {
1508
1504
  behaviors: Set<{
1509
- behavior: Behavior<"*" | "split" | `custom.${string}` | "annotation.add" | "annotation.remove" | "block.set" | "block.unset" | "child.set" | "child.unset" | "decorator.add" | "decorator.remove" | "delete" | "history.redo" | "history.undo" | "insert.block" | "insert.text" | "move.backward" | "move.block" | "move.forward" | "select" | "annotation.set" | "annotation.toggle" | "decorator.toggle" | "delete.backward" | "delete.block" | "delete.child" | "delete.forward" | "delete.text" | "deserialize" | "deserialize.data" | "deserialization.success" | "deserialization.failure" | "insert.blocks" | "insert.break" | "insert.inline object" | "insert.soft break" | "insert.span" | "list item.add" | "list item.remove" | "list item.toggle" | "move.block down" | "move.block up" | "select.previous block" | "select.next block" | "serialize" | "serialize.data" | "serialization.success" | "serialization.failure" | "style.add" | "style.remove" | "style.toggle" | "clipboard.copy" | "clipboard.cut" | "clipboard.paste" | "drag.dragstart" | "drag.drag" | "drag.dragend" | "drag.dragenter" | "drag.dragover" | "drag.dragleave" | "drag.drop" | "input.*" | "keyboard.keydown" | "keyboard.keyup" | "mouse.click" | "history.*" | "split.*" | "delete.*" | "select.*" | "deserialize.*" | "serialize.*" | "annotation.*" | "block.*" | "child.*" | "decorator.*" | "insert.*" | "move.*" | "deserialization.*" | "list item.*" | "serialization.*" | "style.*" | "clipboard.*" | "drag.*" | "keyboard.*" | "mouse.*", true, {
1505
+ behavior: Behavior<"*" | "split" | `custom.${string}` | "annotation.add" | "annotation.remove" | "block.set" | "block.unset" | "child.set" | "child.unset" | "decorator.add" | "decorator.remove" | "delete" | "history.redo" | "history.undo" | "insert.block" | "insert.text" | "move.backward" | "move.block" | "move.forward" | "select" | "annotation.set" | "annotation.toggle" | "decorator.toggle" | "delete.backward" | "delete.block" | "delete.child" | "delete.forward" | "delete.text" | "deserialize" | "deserialize.data" | "deserialization.success" | "deserialization.failure" | "insert.blocks" | "insert.break" | "insert.inline object" | "insert.soft break" | "insert.span" | "list item.add" | "list item.remove" | "list item.toggle" | "move.block down" | "move.block up" | "select.previous block" | "select.next block" | "serialize" | "serialize.data" | "serialization.success" | "serialization.failure" | "style.add" | "style.remove" | "style.toggle" | "clipboard.copy" | "clipboard.cut" | "clipboard.paste" | "drag.dragstart" | "drag.drag" | "drag.dragend" | "drag.dragenter" | "drag.dragover" | "drag.dragleave" | "drag.drop" | "input.*" | "keyboard.keydown" | "keyboard.keyup" | "mouse.click" | "history.*" | "split.*" | "style.*" | "delete.*" | "select.*" | "deserialize.*" | "serialize.*" | "annotation.*" | "block.*" | "child.*" | "decorator.*" | "insert.*" | "move.*" | "deserialization.*" | "list item.*" | "serialization.*" | "clipboard.*" | "drag.*" | "keyboard.*" | "mouse.*", true, {
1510
1506
  type: StrictExtract<"split" | "annotation.add" | "annotation.remove" | "block.set" | "block.unset" | "child.set" | "child.unset" | "decorator.add" | "decorator.remove" | "delete" | "history.redo" | "history.undo" | "insert.block" | "insert.text" | "move.backward" | "move.block" | "move.forward" | "select" | "annotation.set" | "annotation.toggle" | "decorator.toggle" | "delete.backward" | "delete.block" | "delete.child" | "delete.forward" | "delete.text" | "deserialize" | "deserialize.data" | "deserialization.success" | "deserialization.failure" | "insert.blocks" | "insert.break" | "insert.inline object" | "insert.soft break" | "insert.span" | "list item.add" | "list item.remove" | "list item.toggle" | "move.block down" | "move.block up" | "select.previous block" | "select.next block" | "serialize" | "serialize.data" | "serialization.success" | "serialization.failure" | "style.add" | "style.remove" | "style.toggle", "annotation.add">;
1511
1507
  annotation: {
1512
1508
  name: string;
@@ -6,7 +6,7 @@ import { AnnotationDefinition, AnnotationSchemaType, BaseDefinition, BlockObject
6
6
  import * as xstate227 from "xstate";
7
7
  import { ActorRef, ActorRefFrom, EventObject, Snapshot } from "xstate";
8
8
  import { BaseRange, Descendant, Operation } from "slate";
9
- import * as react22 from "react";
9
+ import * as react20 from "react";
10
10
  import React$1, { BaseSyntheticEvent, ClipboardEvent, Component, FocusEvent, JSX, KeyboardEvent as KeyboardEvent$1, MutableRefObject, PropsWithChildren, ReactElement, RefObject, TextareaHTMLAttributes } from "react";
11
11
  import * as xstate_guards12 from "xstate/guards";
12
12
  import { Observable, Subject } from "rxjs";
@@ -44,23 +44,6 @@ type BehaviorConfig = {
44
44
  behavior: Behavior;
45
45
  priority: EditorPriority;
46
46
  };
47
- /**
48
- * @public
49
- */
50
- type AddedAnnotationPaths = {
51
- /**
52
- * @deprecated An annotation may be applied to multiple blocks, resulting
53
- * in multiple `markDef`'s being created. Use `markDefPaths` instead.
54
- */
55
- markDefPath: Path;
56
- markDefPaths: Array<Path>;
57
- /**
58
- * @deprecated Does not return anything meaningful since an annotation
59
- * can span multiple blocks and spans. If references the span closest
60
- * to the focus point of the selection.
61
- */
62
- spanPath: Path;
63
- };
64
47
  /**
65
48
  * @public
66
49
  */
@@ -215,7 +198,7 @@ declare class PortableTextEditor extends Component<PortableTextEditorProps<Inter
215
198
  componentDidUpdate(prevProps: PortableTextEditorProps): void;
216
199
  componentWillUnmount(): void;
217
200
  setEditable: (editable: EditableAPI) => void;
218
- render(): react22.JSX.Element;
201
+ render(): react20.JSX.Element;
219
202
  /**
220
203
  * @deprecated
221
204
  * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
@@ -610,7 +593,7 @@ type PortableTextEditableProps = Omit<TextareaHTMLAttributes<HTMLDivElement>, 'o
610
593
  * ```
611
594
  * @group Components
612
595
  */
613
- declare const PortableTextEditable: react22.ForwardRefExoticComponent<Omit<PortableTextEditableProps, "ref"> & react22.RefAttributes<Omit<HTMLDivElement, "as" | "onPaste" | "onBeforeInput">>>;
596
+ declare const PortableTextEditable: react20.ForwardRefExoticComponent<Omit<PortableTextEditableProps, "ref"> & react20.RefAttributes<Omit<HTMLDivElement, "as" | "onPaste" | "onBeforeInput">>>;
614
597
  type DecoratedRange = BaseRange & {
615
598
  rangeDecoration: RangeDecoration;
616
599
  };
@@ -640,6 +623,23 @@ type ChildPath = [{
640
623
  interface EditableAPIDeleteOptions {
641
624
  mode?: 'blocks' | 'children' | 'selected';
642
625
  }
626
+ /**
627
+ * @public
628
+ */
629
+ type AddedAnnotationPaths = {
630
+ /**
631
+ * @deprecated An annotation may be applied to multiple blocks, resulting
632
+ * in multiple `markDef`'s being created. Use `markDefPaths` instead.
633
+ */
634
+ markDefPath: Path;
635
+ markDefPaths: Array<Path>;
636
+ /**
637
+ * @deprecated Does not return anything meaningful since an annotation
638
+ * can span multiple blocks and spans. If references the span closest
639
+ * to the focus point of the selection.
640
+ */
641
+ spanPath: Path;
642
+ };
643
643
  /** @beta */
644
644
  interface EditableAPI {
645
645
  activeAnnotations: () => PortableTextObject[];
@@ -648,11 +648,7 @@ interface EditableAPI {
648
648
  name: string;
649
649
  }>(type: TSchemaType, value?: {
650
650
  [prop: string]: unknown;
651
- }) => {
652
- markDefPath: Path;
653
- markDefPaths: Array<Path>;
654
- spanPath: Path;
655
- } | undefined;
651
+ }) => AddedAnnotationPaths | undefined;
656
652
  blur: () => void;
657
653
  delete: (selection: EditorSelection, options?: EditableAPIDeleteOptions) => void;
658
654
  findByPath: (path: Path) => [PortableTextBlock | PortableTextChild | undefined, Path | undefined];
@@ -1339,7 +1335,7 @@ declare const editorMachine: xstate227.StateMachine<{
1339
1335
  initialValue?: Array<PortableTextBlock>;
1340
1336
  }, xstate227.NonReducibleUnknown, InternalPatchEvent | MutationEvent | PatchesEvent | {
1341
1337
  type: "blurred";
1342
- event: react22.FocusEvent<HTMLDivElement, Element>;
1338
+ event: react20.FocusEvent<HTMLDivElement, Element>;
1343
1339
  } | {
1344
1340
  type: "done loading";
1345
1341
  } | {
@@ -1351,7 +1347,7 @@ declare const editorMachine: xstate227.StateMachine<{
1351
1347
  data: unknown;
1352
1348
  } | {
1353
1349
  type: "focused";
1354
- event: react22.FocusEvent<HTMLDivElement, Element>;
1350
+ event: react20.FocusEvent<HTMLDivElement, Element>;
1355
1351
  } | {
1356
1352
  type: "invalid value";
1357
1353
  resolution: InvalidValueResolution | null;
@@ -2007,7 +2003,7 @@ declare const editorMachine: xstate227.StateMachine<{
2007
2003
  type: "drop";
2008
2004
  }, undefined, never, never, never, never, InternalPatchEvent | MutationEvent | PatchesEvent | {
2009
2005
  type: "blurred";
2010
- event: react22.FocusEvent<HTMLDivElement, Element>;
2006
+ event: react20.FocusEvent<HTMLDivElement, Element>;
2011
2007
  } | {
2012
2008
  type: "done loading";
2013
2009
  } | {
@@ -2019,7 +2015,7 @@ declare const editorMachine: xstate227.StateMachine<{
2019
2015
  data: unknown;
2020
2016
  } | {
2021
2017
  type: "focused";
2022
- event: react22.FocusEvent<HTMLDivElement, Element>;
2018
+ event: react20.FocusEvent<HTMLDivElement, Element>;
2023
2019
  } | {
2024
2020
  type: "invalid value";
2025
2021
  resolution: InvalidValueResolution | null;
@@ -2890,7 +2886,7 @@ declare const editorMachine: xstate227.StateMachine<{
2890
2886
  type: "drop";
2891
2887
  }, undefined, never, never, never, never, InternalPatchEvent | MutationEvent | PatchesEvent | {
2892
2888
  type: "blurred";
2893
- event: react22.FocusEvent<HTMLDivElement, Element>;
2889
+ event: react20.FocusEvent<HTMLDivElement, Element>;
2894
2890
  } | {
2895
2891
  type: "done loading";
2896
2892
  } | {
@@ -2902,7 +2898,7 @@ declare const editorMachine: xstate227.StateMachine<{
2902
2898
  data: unknown;
2903
2899
  } | {
2904
2900
  type: "focused";
2905
- event: react22.FocusEvent<HTMLDivElement, Element>;
2901
+ event: react20.FocusEvent<HTMLDivElement, Element>;
2906
2902
  } | {
2907
2903
  type: "invalid value";
2908
2904
  resolution: InvalidValueResolution | null;
package/lib/index.cjs CHANGED
@@ -3962,8 +3962,7 @@ const addAnnotationOperationImplementation = ({
3962
3962
  const editor = operation.editor;
3963
3963
  if (!editor.selection || slate.Range.isCollapsed(editor.selection))
3964
3964
  return;
3965
- let paths, spanPath, markDefPath;
3966
- const markDefPaths = [], selectedBlocks = slate.Editor.nodes(editor, {
3965
+ const selectedBlocks = slate.Editor.nodes(editor, {
3967
3966
  at: editor.selection,
3968
3967
  match: (node) => editor.isTextBlock(node),
3969
3968
  reverse: slate.Range.isBackward(editor.selection)
@@ -3973,18 +3972,14 @@ const addAnnotationOperationImplementation = ({
3973
3972
  if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
3974
3973
  continue;
3975
3974
  const annotationKey = blockIndex === 0 ? parsedAnnotation._key : context.keyGenerator(), markDefs = block.markDefs ?? [];
3976
- markDefs.find((markDef) => markDef._type === parsedAnnotation._type && markDef._key === annotationKey) === void 0 && (slate.Transforms.setNodes(editor, {
3975
+ markDefs.find((markDef) => markDef._type === parsedAnnotation._type && markDef._key === annotationKey) === void 0 && slate.Transforms.setNodes(editor, {
3977
3976
  markDefs: [...markDefs, {
3978
3977
  ...parsedAnnotation,
3979
3978
  _key: annotationKey
3980
3979
  }]
3981
3980
  }, {
3982
3981
  at: blockPath
3983
- }), markDefPath = [{
3984
- _key: block._key
3985
- }, "markDefs", {
3986
- _key: annotationKey
3987
- }], slate.Range.isBackward(editor.selection) ? markDefPaths.unshift(markDefPath) : markDefPaths.push(markDefPath)), slate.Transforms.setNodes(editor, {}, {
3982
+ }), slate.Transforms.setNodes(editor, {}, {
3988
3983
  match: slate.Text.isText,
3989
3984
  split: !0
3990
3985
  });
@@ -3997,19 +3992,10 @@ const addAnnotationOperationImplementation = ({
3997
3992
  marks: [...marks, annotationKey]
3998
3993
  }, {
3999
3994
  at: path
4000
- }), spanPath = [{
4001
- _key: block._key
4002
- }, "children", {
4003
- _key: span._key
4004
- }];
3995
+ });
4005
3996
  }
4006
3997
  blockIndex++;
4007
3998
  }
4008
- return markDefPath && spanPath && (paths = {
4009
- markDefPath,
4010
- markDefPaths,
4011
- spanPath
4012
- }), paths;
4013
3999
  }, removeAnnotationOperationImplementation = ({
4014
4000
  operation
4015
4001
  }) => {
@@ -10288,37 +10274,38 @@ function createEditableAPI(editor, editorActor) {
10288
10274
  return selector_isSelectingEntireBlocks.isActiveAnnotation(annotationType)(snapshot);
10289
10275
  },
10290
10276
  addAnnotation: (type, value) => {
10291
- let paths;
10292
- const snapshot = getEditorSnapshot({
10277
+ const snapshotBefore = getEditorSnapshot({
10293
10278
  editorActorSnapshot: editorActor.getSnapshot(),
10294
10279
  slateEditorInstance: editor
10295
- });
10296
- return selector_isSelectingEntireBlocks.isActiveAnnotation(type.name, {
10297
- mode: "partial"
10298
- })(snapshot) && editorActor.send({
10280
+ }), selectedValueBefore = selector_isSelectionExpanded.getSelectedValue(snapshotBefore), focusSpanBefore = selector_isSelectionExpanded.getFocusSpan(snapshotBefore), markDefsBefore = selectedValueBefore.flatMap((block) => schema.isTextBlock(snapshotBefore.context, block) ? block.markDefs ?? [] : []);
10281
+ editorActor.send({
10299
10282
  type: "behavior event",
10300
10283
  behaviorEvent: {
10301
- type: "annotation.remove",
10284
+ type: "annotation.add",
10302
10285
  annotation: {
10303
- name: type.name
10286
+ name: type.name,
10287
+ value: value ?? {}
10304
10288
  }
10305
10289
  },
10306
10290
  editor
10307
- }), slate.Editor.withoutNormalizing(editor, () => {
10308
- paths = addAnnotationOperationImplementation({
10309
- context: {
10310
- keyGenerator: editorActor.getSnapshot().context.keyGenerator,
10311
- schema: types
10312
- },
10313
- operation: {
10314
- annotation: {
10315
- name: type.name,
10316
- value: value ?? {}
10317
- },
10318
- editor
10319
- }
10320
- });
10321
- }), editor.onChange(), paths;
10291
+ });
10292
+ const snapshotAfter = getEditorSnapshot({
10293
+ editorActorSnapshot: editorActor.getSnapshot(),
10294
+ slateEditorInstance: editor
10295
+ }), selectedValueAfter = selector_isSelectionExpanded.getSelectedValue(snapshotAfter), focusBlockAfter = selector_isSelectionExpanded.getFocusBlock(snapshotAfter), focusSpanAfter = selector_isSelectionExpanded.getFocusSpan(snapshotAfter), newMarkDefKeysOnFocusSpan = focusSpanAfter?.node.marks?.filter((mark) => !focusSpanBefore?.node.marks?.includes(mark) && !snapshotAfter.context.schema.decorators.map((decorator) => decorator.name).includes(mark)), markDefs = selectedValueAfter.flatMap((block) => schema.isTextBlock(snapshotAfter.context, block) ? block.markDefs?.map((markDef2) => ({
10296
+ markDef: markDef2,
10297
+ path: [{
10298
+ _key: block._key
10299
+ }, "markDefs", {
10300
+ _key: markDef2._key
10301
+ }]
10302
+ })) ?? [] : []).filter((markDef2) => !markDefsBefore.some((markDefBefore) => markDefBefore._key === markDef2.markDef._key)), spanPath = focusSpanAfter?.path, markDef = markDefs.find((markDef2) => newMarkDefKeysOnFocusSpan?.some((mark) => mark === markDef2.markDef._key));
10303
+ if (focusBlockAfter && spanPath && markDef)
10304
+ return {
10305
+ markDefPath: markDef.path,
10306
+ markDefPaths: markDefs.map((markDef2) => markDef2.path),
10307
+ spanPath
10308
+ };
10322
10309
  },
10323
10310
  delete: (selection, options) => {
10324
10311
  if (selection) {