@contentful/field-editor-rich-text 3.4.6 → 3.4.8

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.
@@ -2751,13 +2751,91 @@ function getEntityTypeFromRichTextNode(nodeType) {
2751
2751
  throw new Error("RichText node type `" + nodeType + "` has no associated `entityType`");
2752
2752
  }
2753
2753
 
2754
+ /**
2755
+ * Allows to observe when the current slide-in navigation slide gets e.g.
2756
+ * re-activated after opening another slide on top. This is useful as the sdk
2757
+ * does not give full insights into e.g. whether sdk.dialogs.selectSingleEntry()
2758
+ * with `withCreate: true` option opens the slide-in navigation to edit the
2759
+ * created entry after returning it.
2760
+ */
2761
+
2762
+ function watchCurrentSlide(navigator) {
2763
+ var onActiveCallbacks = new Set();
2764
+ var wasSlideClosed = false;
2765
+ var initialSlideLevel;
2766
+ var lastSlideLevel;
2767
+
2768
+ var status = function status() {
2769
+ return {
2770
+ wasClosed: wasSlideClosed,
2771
+ isActive: !wasSlideClosed && lastSlideLevel === initialSlideLevel
2772
+ };
2773
+ };
2774
+
2775
+ var off = navigator.onSlideInNavigation(function (info) {
2776
+ if (initialSlideLevel === undefined) {
2777
+ initialSlideLevel = info.oldSlideLevel;
2778
+ }
2779
+
2780
+ lastSlideLevel = info.newSlideLevel;
2781
+
2782
+ if (info.newSlideLevel < initialSlideLevel) {
2783
+ wasSlideClosed = true;
2784
+ off(); // No more point in watching, slide got closed.
2785
+
2786
+ onActiveCallbacks.clear();
2787
+ }
2788
+
2789
+ if (status().isActive) {
2790
+ onActiveCallbacks.forEach(function (cb) {
2791
+ return cb();
2792
+ });
2793
+ }
2794
+ });
2795
+ /**
2796
+ * Call to unsubscribe from navigator events when the watcher is no longer
2797
+ * needed.
2798
+ */
2799
+
2800
+ function unwatch() {
2801
+ off();
2802
+ onActiveCallbacks.clear();
2803
+ }
2804
+ /**
2805
+ * Fires immediately when the slide is currently active, or at the point when
2806
+ * it becomes active again, if there are slides on top that get closed. Does not
2807
+ * fire when the observed slide gets closed, and then re-opened through browser
2808
+ * back, as this technically opens a new slide and editor instance.
2809
+ */
2810
+
2811
+
2812
+ function onActive(cb) {
2813
+ if (wasSlideClosed) return noop; // Can't re-activate already closed slide.
2814
+
2815
+ if (status().isActive) {
2816
+ cb();
2817
+ }
2818
+
2819
+ onActiveCallbacks.add(cb);
2820
+ return function () {
2821
+ return onActiveCallbacks["delete"](cb);
2822
+ };
2823
+ }
2824
+
2825
+ return {
2826
+ status: status,
2827
+ onActive: onActive,
2828
+ unwatch: unwatch
2829
+ };
2830
+ }
2831
+
2754
2832
  function selectEntityAndInsert(_x, _x2, _x3, _x4) {
2755
2833
  return _selectEntityAndInsert.apply(this, arguments);
2756
2834
  } // TODO: incorporate this logic inside the trailingParagraph plugin instead
2757
2835
 
2758
2836
  function _selectEntityAndInsert() {
2759
2837
  _selectEntityAndInsert = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(nodeType, sdk, editor, logAction) {
2760
- var field, dialogs, baseConfig, selectEntity, config, selection, entity;
2838
+ var field, dialogs, baseConfig, selectEntity, config, rteSlide, entity;
2761
2839
  return runtime_1.wrap(function _callee$(_context) {
2762
2840
  while (1) {
2763
2841
  switch (_context.prev = _context.next) {
@@ -2771,32 +2849,33 @@ function _selectEntityAndInsert() {
2771
2849
  config = _extends({}, baseConfig, {
2772
2850
  withCreate: true
2773
2851
  });
2774
- selection = editor.selection;
2852
+ rteSlide = watchCurrentSlide(sdk.navigator);
2775
2853
  _context.next = 8;
2776
2854
  return selectEntity(config);
2777
2855
 
2778
2856
  case 8:
2779
2857
  entity = _context.sent;
2780
2858
 
2781
- if (entity) {
2782
- _context.next = 12;
2783
- break;
2784
- }
2859
+ if (!entity) {
2860
+ logAction('cancelCreateEmbedDialog', {
2861
+ nodeType: nodeType
2862
+ });
2863
+ } else {
2864
+ insertBlock$1(editor, nodeType, entity);
2865
+ ensureFollowingParagraph(editor);
2866
+ logAction('insert', {
2867
+ nodeType: nodeType
2868
+ });
2869
+ } // If user chose to create a new entity, this might open slide-in to edit the
2870
+ // entity. In this case, no point in focusing RTE which is now in the slide below.
2785
2871
 
2786
- logAction('cancelCreateEmbedDialog', {
2787
- nodeType: nodeType
2788
- });
2789
- return _context.abrupt("return");
2790
2872
 
2791
- case 12:
2792
- select(editor, selection);
2793
- insertBlock$1(editor, nodeType, entity);
2794
- ensureFollowingParagraph(editor);
2795
- logAction('insert', {
2796
- nodeType: nodeType
2873
+ rteSlide.onActive(function () {
2874
+ rteSlide.unwatch();
2875
+ focus(editor);
2797
2876
  });
2798
2877
 
2799
- case 16:
2878
+ case 11:
2800
2879
  case "end":
2801
2880
  return _context.stop();
2802
2881
  }
@@ -2861,8 +2940,6 @@ function insertBlock$1(editor, nodeType, entity) {
2861
2940
  } else {
2862
2941
  setNodes(editor, linkedEntityBlock);
2863
2942
  }
2864
-
2865
- focus(editor);
2866
2943
  }
2867
2944
 
2868
2945
  var styles$2 = {