@contentful/field-editor-rich-text 3.4.7 → 3.4.9

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,94 @@ 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 (_ref) {
2776
+ var oldSlideLevel = _ref.oldSlideLevel,
2777
+ newSlideLevel = _ref.newSlideLevel;
2778
+
2779
+ if (initialSlideLevel === undefined) {
2780
+ initialSlideLevel = oldSlideLevel;
2781
+ }
2782
+
2783
+ lastSlideLevel = newSlideLevel;
2784
+
2785
+ if (newSlideLevel < initialSlideLevel) {
2786
+ wasSlideClosed = true;
2787
+ off(); // No more point in watching, slide got closed.
2788
+
2789
+ onActiveCallbacks.clear();
2790
+ }
2791
+
2792
+ if (status().isActive && newSlideLevel !== oldSlideLevel) {
2793
+ onActiveCallbacks.forEach(function (cb) {
2794
+ return cb();
2795
+ });
2796
+ }
2797
+ });
2798
+ /**
2799
+ * Call to unsubscribe from navigator events when the watcher is no longer
2800
+ * needed.
2801
+ */
2802
+
2803
+ function unwatch() {
2804
+ off();
2805
+ onActiveCallbacks.clear();
2806
+ }
2807
+ /**
2808
+ * Fires immediately when the slide is currently active, or at the point when
2809
+ * it becomes active again, if there are slides on top that get closed. Does not
2810
+ * fire when the observed slide gets closed, and then re-opened through browser
2811
+ * back, as this technically opens a new slide and editor instance.
2812
+ */
2813
+
2814
+
2815
+ function onActive(cb) {
2816
+ if (wasSlideClosed) return noop; // Can't re-activate already closed slide.
2817
+
2818
+ if (status().isActive) {
2819
+ cb();
2820
+ }
2821
+
2822
+ onActiveCallbacks.add(cb);
2823
+ return function () {
2824
+ return onActiveCallbacks["delete"](cb);
2825
+ };
2826
+ }
2827
+
2828
+ return {
2829
+ status: status,
2830
+ onActive: onActive,
2831
+ unwatch: unwatch
2832
+ };
2833
+ }
2834
+
2754
2835
  function selectEntityAndInsert(_x, _x2, _x3, _x4) {
2755
2836
  return _selectEntityAndInsert.apply(this, arguments);
2756
2837
  } // TODO: incorporate this logic inside the trailingParagraph plugin instead
2757
2838
 
2758
2839
  function _selectEntityAndInsert() {
2759
2840
  _selectEntityAndInsert = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee(nodeType, sdk, editor, logAction) {
2760
- var field, dialogs, baseConfig, selectEntity, config, selection, entity;
2841
+ var field, dialogs, baseConfig, selectEntity, config, rteSlide, entity;
2761
2842
  return runtime_1.wrap(function _callee$(_context) {
2762
2843
  while (1) {
2763
2844
  switch (_context.prev = _context.next) {
@@ -2771,32 +2852,33 @@ function _selectEntityAndInsert() {
2771
2852
  config = _extends({}, baseConfig, {
2772
2853
  withCreate: true
2773
2854
  });
2774
- selection = editor.selection;
2855
+ rteSlide = watchCurrentSlide(sdk.navigator);
2775
2856
  _context.next = 8;
2776
2857
  return selectEntity(config);
2777
2858
 
2778
2859
  case 8:
2779
2860
  entity = _context.sent;
2780
2861
 
2781
- if (entity) {
2782
- _context.next = 12;
2783
- break;
2784
- }
2862
+ if (!entity) {
2863
+ logAction('cancelCreateEmbedDialog', {
2864
+ nodeType: nodeType
2865
+ });
2866
+ } else {
2867
+ insertBlock$1(editor, nodeType, entity);
2868
+ ensureFollowingParagraph(editor);
2869
+ logAction('insert', {
2870
+ nodeType: nodeType
2871
+ });
2872
+ } // If user chose to create a new entity, this might open slide-in to edit the
2873
+ // entity. In this case, no point in focusing RTE which is now in the slide below.
2785
2874
 
2786
- logAction('cancelCreateEmbedDialog', {
2787
- nodeType: nodeType
2788
- });
2789
- return _context.abrupt("return");
2790
2875
 
2791
- case 12:
2792
- select(editor, selection);
2793
- insertBlock$1(editor, nodeType, entity);
2794
- ensureFollowingParagraph(editor);
2795
- logAction('insert', {
2796
- nodeType: nodeType
2876
+ rteSlide.onActive(function () {
2877
+ rteSlide.unwatch();
2878
+ focus(editor);
2797
2879
  });
2798
2880
 
2799
- case 16:
2881
+ case 11:
2800
2882
  case "end":
2801
2883
  return _context.stop();
2802
2884
  }
@@ -2861,8 +2943,6 @@ function insertBlock$1(editor, nodeType, entity) {
2861
2943
  } else {
2862
2944
  setNodes(editor, linkedEntityBlock);
2863
2945
  }
2864
-
2865
- focus(editor);
2866
2946
  }
2867
2947
 
2868
2948
  var styles$2 = {
@@ -3160,7 +3240,11 @@ function EmbeddedEntityInline(props) {
3160
3240
 
3161
3241
  function handleEditClick() {
3162
3242
  return sdk.navigator.openEntry(entryId, {
3163
- slideIn: true
3243
+ slideIn: {
3244
+ waitForClose: true
3245
+ }
3246
+ }).then(function () {
3247
+ editor && focus(editor);
3164
3248
  });
3165
3249
  }
3166
3250
 
@@ -3199,7 +3283,7 @@ function selectEntityAndInsert$1(_x, _x2, _x3) {
3199
3283
 
3200
3284
  function _selectEntityAndInsert$1() {
3201
3285
  _selectEntityAndInsert$1 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2(editor, sdk, logAction) {
3202
- var config, selection, entry, inlineEntryNode;
3286
+ var config, rteSlide, entry;
3203
3287
  return runtime_1.wrap(function _callee2$(_context2) {
3204
3288
  while (1) {
3205
3289
  switch (_context2.prev = _context2.next) {
@@ -3210,39 +3294,30 @@ function _selectEntityAndInsert$1() {
3210
3294
  config = _extends({}, newEntitySelectorConfigFromRichTextField(sdk.field, INLINES.EMBEDDED_ENTRY), {
3211
3295
  withCreate: true
3212
3296
  });
3213
- selection = editor.selection;
3297
+ rteSlide = watchCurrentSlide(sdk.navigator);
3214
3298
  _context2.next = 5;
3215
3299
  return sdk.dialogs.selectSingleEntry(config);
3216
3300
 
3217
3301
  case 5:
3218
3302
  entry = _context2.sent;
3219
- focus(editor); // Dialog steals focus from editor, return it.
3220
3303
 
3221
- if (entry) {
3222
- _context2.next = 10;
3223
- break;
3304
+ if (!entry) {
3305
+ logAction('cancelCreateEmbedDialog', {
3306
+ nodeType: INLINES.EMBEDDED_ENTRY
3307
+ });
3308
+ } else {
3309
+ insertNodes(editor, createInlineEntryNode$1(entry.sys.id));
3310
+ logAction('insert', {
3311
+ nodeType: INLINES.EMBEDDED_ENTRY
3312
+ });
3224
3313
  }
3225
3314
 
3226
- logAction('cancelCreateEmbedDialog', {
3227
- nodeType: INLINES.EMBEDDED_ENTRY
3315
+ rteSlide.onActive(function () {
3316
+ rteSlide.unwatch();
3317
+ focus(editor);
3228
3318
  });
3229
- return _context2.abrupt("return");
3230
3319
 
3231
- case 10:
3232
- inlineEntryNode = createInlineEntryNode$1(entry.sys.id);
3233
- logAction('insert', {
3234
- nodeType: INLINES.EMBEDDED_ENTRY
3235
- }); // Got to wait until focus is really back on the editor or setSelection() won't work.
3236
-
3237
- return _context2.abrupt("return", new Promise(function (resolve) {
3238
- setTimeout(function () {
3239
- setSelection(editor, selection);
3240
- insertNodes(editor, inlineEntryNode);
3241
- resolve();
3242
- }, 0);
3243
- }));
3244
-
3245
- case 13:
3320
+ case 8:
3246
3321
  case "end":
3247
3322
  return _context2.stop();
3248
3323
  }