@portabletext/editor 1.27.0 → 1.28.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.
Files changed (34) hide show
  1. package/README.md +5 -5
  2. package/lib/_chunks-cjs/plugin.event-listener.cjs +302 -43
  3. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
  4. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs +88 -88
  5. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs.map +1 -1
  6. package/lib/_chunks-es/plugin.event-listener.js +302 -43
  7. package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
  8. package/lib/_chunks-es/selector.is-at-the-start-of-block.js +88 -88
  9. package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
  10. package/lib/index.cjs +22 -21
  11. package/lib/index.cjs.map +1 -1
  12. package/lib/index.d.cts +257 -0
  13. package/lib/index.d.ts +257 -0
  14. package/lib/index.js +22 -21
  15. package/lib/index.js.map +1 -1
  16. package/lib/selectors/index.cjs +15 -3
  17. package/lib/selectors/index.cjs.map +1 -1
  18. package/lib/selectors/index.d.cts +18 -0
  19. package/lib/selectors/index.d.ts +18 -0
  20. package/lib/selectors/index.js +17 -4
  21. package/lib/selectors/index.js.map +1 -1
  22. package/package.json +3 -3
  23. package/src/behavior-actions/behavior.action.insert-break.ts +93 -83
  24. package/src/editor/PortableTextEditor.tsx +288 -1
  25. package/src/editor/components/DefaultObject.tsx +21 -0
  26. package/src/editor/components/Element.tsx +5 -5
  27. package/src/editor/components/Leaf.tsx +1 -6
  28. package/src/selectors/index.ts +4 -2
  29. package/src/selectors/selector.get-active-annotations.test.ts +122 -0
  30. package/src/selectors/selector.get-active-annotations.ts +30 -0
  31. package/src/selectors/selector.get-selection.ts +8 -0
  32. package/src/selectors/selector.get-value.ts +11 -0
  33. package/src/editor/nodes/DefaultAnnotation.tsx +0 -20
  34. package/src/editor/nodes/DefaultObject.tsx +0 -18
@@ -4870,51 +4870,54 @@ const blockSetBehaviorActionImplementation = ({
4870
4870
  return;
4871
4871
  const anchorBlockPath = editor.selection.anchor.path.slice(0, 1), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
4872
4872
  if (editor.isTextBlock(focusBlock) && anchorBlockPath[0] === focusBlockPath[0]) {
4873
- Editor.withoutNormalizing(editor, () => {
4874
- if (!editor.selection)
4875
- return;
4876
- Transforms.splitNodes(editor, {
4877
- at: editor.selection
4878
- });
4879
- const [nextNode, nextNodePath] = Editor.node(editor, Path.next(focusBlockPath), {
4880
- depth: 1
4881
- });
4882
- if (Transforms.setSelection(editor, {
4883
- anchor: {
4884
- path: [...nextNodePath, 0],
4885
- offset: 0
4886
- },
4887
- focus: {
4888
- path: [...nextNodePath, 0],
4889
- offset: 0
4890
- }
4891
- }), editor.isTextBlock(nextNode) && nextNode.markDefs && nextNode.markDefs.length > 0) {
4892
- const newMarkDefKeys = /* @__PURE__ */ new Map(), prevNodeSpans = Array.from(Node.children(editor, focusBlockPath)).map((entry) => entry[0]).filter((node) => editor.isTextSpan(node)), children = Node.children(editor, nextNodePath);
4893
- for (const [child, childPath] of children) {
4894
- if (!editor.isTextSpan(child))
4895
- continue;
4896
- const marks = child.marks ?? [];
4897
- for (const mark of marks)
4898
- schema.decorators.some((decorator) => decorator.value === mark) || prevNodeSpans.some((prevNodeSpan) => prevNodeSpan.marks?.includes(mark)) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
4899
- const newMarks = marks.map((mark) => newMarkDefKeys.get(mark) ?? mark);
4900
- isEqual(marks, newMarks) || Transforms.setNodes(editor, {
4901
- marks: newMarks
4902
- }, {
4903
- at: childPath
4904
- });
4905
- }
4906
- const newMarkDefs = nextNode.markDefs.map((markDef) => ({
4907
- ...markDef,
4908
- _key: newMarkDefKeys.get(markDef._key) ?? markDef._key
4909
- }));
4910
- isEqual(nextNode.markDefs, newMarkDefs) || Transforms.setNodes(editor, {
4911
- markDefs: newMarkDefs
4873
+ Transforms.splitNodes(editor, {
4874
+ at: editor.selection
4875
+ });
4876
+ const [nextBlock, nextBlockPath] = Editor.node(editor, Path.next(focusBlockPath), {
4877
+ depth: 1
4878
+ }), nextChild = Node.child(nextBlock, 0);
4879
+ if (!editor.isTextSpan(nextChild) && Transforms.insertNodes(editor, {
4880
+ _key: context.keyGenerator(),
4881
+ _type: "span",
4882
+ text: "",
4883
+ marks: []
4884
+ }, {
4885
+ at: [nextBlockPath[0], 0]
4886
+ }), Transforms.setSelection(editor, {
4887
+ anchor: {
4888
+ path: [...nextBlockPath, 0],
4889
+ offset: 0
4890
+ },
4891
+ focus: {
4892
+ path: [...nextBlockPath, 0],
4893
+ offset: 0
4894
+ }
4895
+ }), editor.isTextBlock(nextBlock) && nextBlock.markDefs && nextBlock.markDefs.length > 0) {
4896
+ const newMarkDefKeys = /* @__PURE__ */ new Map(), prevNodeSpans = Array.from(Node.children(editor, focusBlockPath)).map((entry) => entry[0]).filter((node) => editor.isTextSpan(node)), children = Node.children(editor, nextBlockPath);
4897
+ for (const [child, childPath] of children) {
4898
+ if (!editor.isTextSpan(child))
4899
+ continue;
4900
+ const marks = child.marks ?? [];
4901
+ for (const mark of marks)
4902
+ schema.decorators.some((decorator) => decorator.value === mark) || prevNodeSpans.some((prevNodeSpan) => prevNodeSpan.marks?.includes(mark)) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
4903
+ const newMarks = marks.map((mark) => newMarkDefKeys.get(mark) ?? mark);
4904
+ isEqual(marks, newMarks) || Transforms.setNodes(editor, {
4905
+ marks: newMarks
4912
4906
  }, {
4913
- at: nextNodePath,
4914
- match: (node) => editor.isTextBlock(node)
4907
+ at: childPath
4915
4908
  });
4916
4909
  }
4917
- }), editor.onChange();
4910
+ const newMarkDefs = nextBlock.markDefs.map((markDef) => ({
4911
+ ...markDef,
4912
+ _key: newMarkDefKeys.get(markDef._key) ?? markDef._key
4913
+ }));
4914
+ isEqual(nextBlock.markDefs, newMarkDefs) || Transforms.setNodes(editor, {
4915
+ markDefs: newMarkDefs
4916
+ }, {
4917
+ at: nextBlockPath,
4918
+ match: (node) => editor.isTextBlock(node)
4919
+ });
4920
+ }
4918
4921
  return;
4919
4922
  }
4920
4923
  Transforms.splitNodes(editor, {
@@ -6368,30 +6371,224 @@ class PortableTextEditor extends Component {
6368
6371
  /* @__PURE__ */ jsx(EditorActorContext.Provider, { value: this.editor._internal.editorActor, children: /* @__PURE__ */ jsx(Slate, { editor: this.editor._internal.slateEditor.instance, initialValue: this.editor._internal.slateEditor.initialValue, children: /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsx(PortableTextEditorSelectionProvider, { editorActor: this.editor._internal.editorActor, children: this.props.children }) }) }) })
6369
6372
  ] });
6370
6373
  }
6371
- // Static API methods
6374
+ /**
6375
+ * @deprecated
6376
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6377
+ *
6378
+ * ```
6379
+ * import * as selectors from '@portabletext/editor/selectors'
6380
+ * const editor = useEditor()
6381
+ * const isActive = useEditorSelector(editor, selectors.getActiveAnnotations)
6382
+ * ```
6383
+ */
6372
6384
  static activeAnnotations = (editor) => editor && editor.editable ? editor.editable.activeAnnotations() : [];
6385
+ /**
6386
+ * @deprecated
6387
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6388
+ *
6389
+ * ```
6390
+ * import * as selectors from '@portabletext/editor/selectors'
6391
+ * const editor = useEditor()
6392
+ * const isActive = useEditorSelector(editor, selectors.isActiveAnnotation(...))
6393
+ * ```
6394
+ */
6373
6395
  static isAnnotationActive = (editor, annotationType) => editor && editor.editable ? editor.editable.isAnnotationActive(annotationType) : !1;
6396
+ /**
6397
+ * @deprecated
6398
+ * Use `editor.send(...)` instead
6399
+ *
6400
+ * ```
6401
+ * const editor = useEditor()
6402
+ * editor.send({
6403
+ * type: 'annotation.add',
6404
+ * annotation: {
6405
+ * name: '...',
6406
+ * value: {...},
6407
+ * }
6408
+ * })
6409
+ * ```
6410
+ */
6374
6411
  static addAnnotation = (editor, type, value) => editor.editable?.addAnnotation(type, value);
6412
+ /**
6413
+ * @deprecated
6414
+ * Use `editor.send(...)` instead
6415
+ *
6416
+ * ```
6417
+ * const editor = useEditor()
6418
+ * editor.send({
6419
+ * type: 'blur',
6420
+ * })
6421
+ * ```
6422
+ */
6375
6423
  static blur = (editor) => {
6376
6424
  debug("Host blurred"), editor.editable?.blur();
6377
6425
  };
6378
6426
  static delete = (editor, selection, options) => editor.editable?.delete(selection, options);
6379
6427
  static findDOMNode = (editor, element) => editor.editable?.findDOMNode(element);
6380
6428
  static findByPath = (editor, path) => editor.editable?.findByPath(path) || [];
6429
+ /**
6430
+ * @deprecated
6431
+ * Use `editor.send(...)` instead
6432
+ *
6433
+ * ```
6434
+ * const editor = useEditor()
6435
+ * editor.send({
6436
+ * type: 'focus',
6437
+ * })
6438
+ * ```
6439
+ */
6381
6440
  static focus = (editor) => {
6382
6441
  debug("Host requesting focus"), editor.editable?.focus();
6383
6442
  };
6443
+ /**
6444
+ * @deprecated
6445
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6446
+ *
6447
+ * ```
6448
+ * import * as selectors from '@portabletext/editor/selectors'
6449
+ * const editor = useEditor()
6450
+ * const focusBlock = useEditorSelector(editor, selectors.getFocusBlock)
6451
+ * ```
6452
+ */
6384
6453
  static focusBlock = (editor) => editor.editable?.focusBlock();
6454
+ /**
6455
+ * @deprecated
6456
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6457
+ *
6458
+ * ```
6459
+ * import * as selectors from '@portabletext/editor/selectors'
6460
+ * const editor = useEditor()
6461
+ * const focusChild = useEditorSelector(editor, selectors.getFocusChild)
6462
+ * ```
6463
+ */
6385
6464
  static focusChild = (editor) => editor.editable?.focusChild();
6465
+ /**
6466
+ * @deprecated
6467
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6468
+ *
6469
+ * ```
6470
+ * import * as selectors from '@portabletext/editor/selectors'
6471
+ * const editor = useEditor()
6472
+ * const selection = useEditorSelector(editor, selectors.getSelection)
6473
+ * ```
6474
+ */
6386
6475
  static getSelection = (editor) => editor.editable ? editor.editable.getSelection() : null;
6476
+ /**
6477
+ * @deprecated
6478
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6479
+ *
6480
+ * ```
6481
+ * import * as selectors from '@portabletext/editor/selectors'
6482
+ * const editor = useEditor()
6483
+ * const value = useEditorSelector(editor, selectors.getValue)
6484
+ * ```
6485
+ */
6387
6486
  static getValue = (editor) => editor.editable?.getValue();
6487
+ /**
6488
+ * @deprecated
6489
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6490
+ *
6491
+ * ```
6492
+ * import * as selectors from '@portabletext/editor/selectors'
6493
+ * const editor = useEditor()
6494
+ * const isActive = useEditorSelector(editor, selectors.isActiveStyle(...))
6495
+ * ```
6496
+ */
6388
6497
  static hasBlockStyle = (editor, blockStyle) => editor.editable?.hasBlockStyle(blockStyle);
6498
+ /**
6499
+ * @deprecated
6500
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6501
+ *
6502
+ * ```
6503
+ * import * as selectors from '@portabletext/editor/selectors'
6504
+ * const editor = useEditor()
6505
+ * const isActive = useEditorSelector(editor, selectors.isActiveListItem(...))
6506
+ * ```
6507
+ */
6389
6508
  static hasListStyle = (editor, listStyle) => editor.editable?.hasListStyle(listStyle);
6509
+ /**
6510
+ * @deprecated
6511
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6512
+ *
6513
+ * ```
6514
+ * import * as selectors from '@portabletext/editor/selectors'
6515
+ * const editor = useEditor()
6516
+ * const isSelectionCollapsed = useEditorSelector(editor, selectors.isSelectionCollapsed)
6517
+ * ```
6518
+ */
6390
6519
  static isCollapsedSelection = (editor) => editor.editable?.isCollapsedSelection();
6520
+ /**
6521
+ * @deprecated
6522
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6523
+ *
6524
+ * ```
6525
+ * import * as selectors from '@portabletext/editor/selectors'
6526
+ * const editor = useEditor()
6527
+ * const isSelectionExpanded = useEditorSelector(editor, selectors.isSelectionExpanded)
6528
+ * ```
6529
+ */
6391
6530
  static isExpandedSelection = (editor) => editor.editable?.isExpandedSelection();
6531
+ /**
6532
+ * @deprecated
6533
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6534
+ *
6535
+ * ```
6536
+ * import * as selectors from '@portabletext/editor/selectors'
6537
+ * const editor = useEditor()
6538
+ * const isActive = useEditorSelector(editor, selectors.isActiveDecorator(...))
6539
+ * ```
6540
+ */
6392
6541
  static isMarkActive = (editor, mark) => editor.editable?.isMarkActive(mark);
6542
+ /**
6543
+ * @deprecated
6544
+ * Use `editor.send(...)` instead
6545
+ *
6546
+ * ```
6547
+ * const editor = useEditor()
6548
+ * editor.send({
6549
+ * type: 'insert.span',
6550
+ * text: '...',
6551
+ * annotations: [{name: '...', value: {...}}],
6552
+ * decorators: ['...'],
6553
+ * })
6554
+ * editor.send({
6555
+ * type: 'insert.inline object',
6556
+ * inlineObject: {
6557
+ * name: '...',
6558
+ * value: {...},
6559
+ * },
6560
+ * })
6561
+ * ```
6562
+ */
6393
6563
  static insertChild = (editor, type, value) => (debug("Host inserting child"), editor.editable?.insertChild(type, value));
6564
+ /**
6565
+ * @deprecated
6566
+ * Use `editor.send(...)` instead
6567
+ *
6568
+ * ```
6569
+ * const editor = useEditor()
6570
+ * editor.send({
6571
+ * type: 'insert.block object',
6572
+ * blockObject: {
6573
+ * name: '...',
6574
+ * value: {...},
6575
+ * },
6576
+ * placement: 'auto' | 'after' | 'before',
6577
+ * })
6578
+ * ```
6579
+ */
6394
6580
  static insertBlock = (editor, type, value) => editor.editable?.insertBlock(type, value);
6581
+ /**
6582
+ * @deprecated
6583
+ * Use `editor.send(...)` instead
6584
+ *
6585
+ * ```
6586
+ * const editor = useEditor()
6587
+ * editor.send({
6588
+ * type: 'insert.break',
6589
+ * })
6590
+ * ```
6591
+ */
6395
6592
  static insertBreak = (editor) => editor.editable?.insertBreak();
6396
6593
  static isVoid = (editor, element) => editor.editable?.isVoid(element);
6397
6594
  static isObjectPath = (_editor, path) => {
@@ -6400,12 +6597,74 @@ class PortableTextEditor extends Component {
6400
6597
  return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
6401
6598
  };
6402
6599
  static marks = (editor) => editor.editable?.marks();
6600
+ /**
6601
+ * @deprecated
6602
+ * Use `editor.send(...)` instead
6603
+ *
6604
+ * ```
6605
+ * const editor = useEditor()
6606
+ * editor.send({
6607
+ * type: 'select',
6608
+ * selection: {...},
6609
+ * })
6610
+ * ```
6611
+ */
6403
6612
  static select = (editor, selection) => {
6404
6613
  debug("Host setting selection", selection), editor.editable?.select(selection);
6405
6614
  };
6615
+ /**
6616
+ * @deprecated
6617
+ * Use `editor.send(...)` instead
6618
+ *
6619
+ * ```
6620
+ * const editor = useEditor()
6621
+ * editor.send({
6622
+ * type: 'annotation.remove',
6623
+ * annotation: {
6624
+ * name: '...',
6625
+ * },
6626
+ * })
6627
+ * ```
6628
+ */
6406
6629
  static removeAnnotation = (editor, type) => editor.editable?.removeAnnotation(type);
6630
+ /**
6631
+ * @deprecated
6632
+ * Use `editor.send(...)` instead
6633
+ *
6634
+ * ```
6635
+ * const editor = useEditor()
6636
+ * editor.send({
6637
+ * type: 'style.toggle',
6638
+ * style: '...',
6639
+ * })
6640
+ * ```
6641
+ */
6407
6642
  static toggleBlockStyle = (editor, blockStyle) => (debug("Host is toggling block style"), editor.editable?.toggleBlockStyle(blockStyle));
6643
+ /**
6644
+ * @deprecated
6645
+ * Use `editor.send(...)` instead
6646
+ *
6647
+ * ```
6648
+ * const editor = useEditor()
6649
+ * editor.send({
6650
+ * type: 'list item.toggle',
6651
+ * listItem: '...',
6652
+ * })
6653
+ * ```
6654
+ */
6408
6655
  static toggleList = (editor, listStyle) => editor.editable?.toggleList(listStyle);
6656
+ /**
6657
+ * @deprecated
6658
+ * Use `editor.send(...)` instead
6659
+ *
6660
+ * ```
6661
+ * const editor = useEditor()
6662
+ * editor.send({
6663
+ * type: 'decorator.toggle',
6664
+ * decorator: '...',
6665
+ * })
6666
+ * ```
6667
+ */
6409
6668
  static toggleMark = (editor, mark) => {
6410
6669
  debug("Host toggling mark", mark), editor.editable?.toggleMark(mark);
6411
6670
  };