@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.
package/lib/index.js CHANGED
@@ -3991,8 +3991,7 @@ const addAnnotationOperationImplementation = ({
3991
3991
  const editor = operation.editor;
3992
3992
  if (!editor.selection || Range.isCollapsed(editor.selection))
3993
3993
  return;
3994
- let paths, spanPath, markDefPath;
3995
- const markDefPaths = [], selectedBlocks = Editor.nodes(editor, {
3994
+ const selectedBlocks = Editor.nodes(editor, {
3996
3995
  at: editor.selection,
3997
3996
  match: (node) => editor.isTextBlock(node),
3998
3997
  reverse: Range.isBackward(editor.selection)
@@ -4002,18 +4001,14 @@ const addAnnotationOperationImplementation = ({
4002
4001
  if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
4003
4002
  continue;
4004
4003
  const annotationKey = blockIndex === 0 ? parsedAnnotation._key : context.keyGenerator(), markDefs = block.markDefs ?? [];
4005
- markDefs.find((markDef) => markDef._type === parsedAnnotation._type && markDef._key === annotationKey) === void 0 && (Transforms.setNodes(editor, {
4004
+ markDefs.find((markDef) => markDef._type === parsedAnnotation._type && markDef._key === annotationKey) === void 0 && Transforms.setNodes(editor, {
4006
4005
  markDefs: [...markDefs, {
4007
4006
  ...parsedAnnotation,
4008
4007
  _key: annotationKey
4009
4008
  }]
4010
4009
  }, {
4011
4010
  at: blockPath
4012
- }), markDefPath = [{
4013
- _key: block._key
4014
- }, "markDefs", {
4015
- _key: annotationKey
4016
- }], Range.isBackward(editor.selection) ? markDefPaths.unshift(markDefPath) : markDefPaths.push(markDefPath)), Transforms.setNodes(editor, {}, {
4011
+ }), Transforms.setNodes(editor, {}, {
4017
4012
  match: Text.isText,
4018
4013
  split: !0
4019
4014
  });
@@ -4026,19 +4021,10 @@ const addAnnotationOperationImplementation = ({
4026
4021
  marks: [...marks, annotationKey]
4027
4022
  }, {
4028
4023
  at: path
4029
- }), spanPath = [{
4030
- _key: block._key
4031
- }, "children", {
4032
- _key: span._key
4033
- }];
4024
+ });
4034
4025
  }
4035
4026
  blockIndex++;
4036
4027
  }
4037
- return markDefPath && spanPath && (paths = {
4038
- markDefPath,
4039
- markDefPaths,
4040
- spanPath
4041
- }), paths;
4042
4028
  }, removeAnnotationOperationImplementation = ({
4043
4029
  operation
4044
4030
  }) => {
@@ -10317,37 +10303,38 @@ function createEditableAPI(editor, editorActor) {
10317
10303
  return isActiveAnnotation(annotationType)(snapshot);
10318
10304
  },
10319
10305
  addAnnotation: (type, value) => {
10320
- let paths;
10321
- const snapshot = getEditorSnapshot({
10306
+ const snapshotBefore = getEditorSnapshot({
10322
10307
  editorActorSnapshot: editorActor.getSnapshot(),
10323
10308
  slateEditorInstance: editor
10324
- });
10325
- return isActiveAnnotation(type.name, {
10326
- mode: "partial"
10327
- })(snapshot) && editorActor.send({
10309
+ }), selectedValueBefore = getSelectedValue(snapshotBefore), focusSpanBefore = getFocusSpan$1(snapshotBefore), markDefsBefore = selectedValueBefore.flatMap((block) => isTextBlock(snapshotBefore.context, block) ? block.markDefs ?? [] : []);
10310
+ editorActor.send({
10328
10311
  type: "behavior event",
10329
10312
  behaviorEvent: {
10330
- type: "annotation.remove",
10313
+ type: "annotation.add",
10331
10314
  annotation: {
10332
- name: type.name
10315
+ name: type.name,
10316
+ value: value ?? {}
10333
10317
  }
10334
10318
  },
10335
10319
  editor
10336
- }), Editor.withoutNormalizing(editor, () => {
10337
- paths = addAnnotationOperationImplementation({
10338
- context: {
10339
- keyGenerator: editorActor.getSnapshot().context.keyGenerator,
10340
- schema: types
10341
- },
10342
- operation: {
10343
- annotation: {
10344
- name: type.name,
10345
- value: value ?? {}
10346
- },
10347
- editor
10348
- }
10349
- });
10350
- }), editor.onChange(), paths;
10320
+ });
10321
+ const snapshotAfter = getEditorSnapshot({
10322
+ editorActorSnapshot: editorActor.getSnapshot(),
10323
+ slateEditorInstance: editor
10324
+ }), selectedValueAfter = getSelectedValue(snapshotAfter), focusBlockAfter = getFocusBlock$1(snapshotAfter), focusSpanAfter = getFocusSpan$1(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) => isTextBlock(snapshotAfter.context, block) ? block.markDefs?.map((markDef2) => ({
10325
+ markDef: markDef2,
10326
+ path: [{
10327
+ _key: block._key
10328
+ }, "markDefs", {
10329
+ _key: markDef2._key
10330
+ }]
10331
+ })) ?? [] : []).filter((markDef2) => !markDefsBefore.some((markDefBefore) => markDefBefore._key === markDef2.markDef._key)), spanPath = focusSpanAfter?.path, markDef = markDefs.find((markDef2) => newMarkDefKeysOnFocusSpan?.some((mark) => mark === markDef2.markDef._key));
10332
+ if (focusBlockAfter && spanPath && markDef)
10333
+ return {
10334
+ markDefPath: markDef.path,
10335
+ markDefPaths: markDefs.map((markDef2) => markDef2.path),
10336
+ spanPath
10337
+ };
10351
10338
  },
10352
10339
  delete: (selection, options) => {
10353
10340
  if (selection) {