@atlaskit/react-ufo 2.2.0 → 2.2.2

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 (32) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/interaction-metrics/index.js +21 -17
  3. package/dist/cjs/interaction-metrics-init/index.js +3 -0
  4. package/dist/cjs/vc/vc-observer/observers/editor-lnv/index.js +187 -0
  5. package/dist/cjs/vc/vc-observer/observers/editor-lnv/test-utils.js +69 -0
  6. package/dist/cjs/vc/vc-observer/observers/index.js +17 -4
  7. package/dist/cjs/vc/vc-observer/observers/ssr-placeholders/index.js +0 -1
  8. package/dist/es2019/interaction-metrics/index.js +21 -17
  9. package/dist/es2019/interaction-metrics-init/index.js +3 -0
  10. package/dist/es2019/vc/vc-observer/observers/editor-lnv/index.js +140 -0
  11. package/dist/es2019/vc/vc-observer/observers/editor-lnv/test-utils.js +41 -0
  12. package/dist/es2019/vc/vc-observer/observers/index.js +14 -0
  13. package/dist/es2019/vc/vc-observer/observers/ssr-placeholders/index.js +0 -1
  14. package/dist/esm/interaction-metrics/index.js +21 -17
  15. package/dist/esm/interaction-metrics-init/index.js +3 -0
  16. package/dist/esm/vc/vc-observer/observers/editor-lnv/index.js +181 -0
  17. package/dist/esm/vc/vc-observer/observers/editor-lnv/test-utils.js +59 -0
  18. package/dist/esm/vc/vc-observer/observers/index.js +17 -4
  19. package/dist/esm/vc/vc-observer/observers/ssr-placeholders/index.js +0 -1
  20. package/dist/types/common/vc/types.d.ts +1 -1
  21. package/dist/types/config/index.d.ts +2 -1
  22. package/dist/types/vc/vc-observer/observers/editor-lnv/index.d.ts +24 -0
  23. package/dist/types/vc/vc-observer/observers/editor-lnv/test-utils.d.ts +35 -0
  24. package/dist/types/vc/vc-observer/observers/index.d.ts +1 -0
  25. package/dist/types/vc/vc-observer/observers/ssr-placeholders/index.d.ts +9 -7
  26. package/dist/types-ts4.5/common/vc/types.d.ts +1 -1
  27. package/dist/types-ts4.5/config/index.d.ts +2 -1
  28. package/dist/types-ts4.5/vc/vc-observer/observers/editor-lnv/index.d.ts +24 -0
  29. package/dist/types-ts4.5/vc/vc-observer/observers/editor-lnv/test-utils.d.ts +35 -0
  30. package/dist/types-ts4.5/vc/vc-observer/observers/index.d.ts +1 -0
  31. package/dist/types-ts4.5/vc/vc-observer/observers/ssr-placeholders/index.d.ts +9 -7
  32. package/package.json +6 -1
@@ -0,0 +1,140 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ const placeholderDataKey = 'editorLnvPlaceholder'; // data-editor-lnv-placeholder
3
+ const replaceDataKey = 'editorLnvPlaceholderReplace'; // data-editor-lnv-placeholder-replace
4
+
5
+ export class EditorLnvHandler {
6
+ constructor() {
7
+ _defineProperty(this, "placeholders", new Map());
8
+ _defineProperty(this, "getSizeCallbacks", new Map());
9
+ _defineProperty(this, "isAddedPlaceholderMatchingSizeCallbacks", new Map());
10
+ _defineProperty(this, "isAddedReplaceMatchingSizeCallbacks", new Map());
11
+ _defineProperty(this, "intersectionObserverCallback", ({
12
+ target,
13
+ boundingClientRect
14
+ }) => {
15
+ var _target$dataset, _target$dataset2;
16
+ this.intersectionObserver.unobserve(target);
17
+ if (!(target instanceof HTMLElement)) {
18
+ return;
19
+ }
20
+ const placeholderId = (_target$dataset = target.dataset) === null || _target$dataset === void 0 ? void 0 : _target$dataset[placeholderDataKey];
21
+ if (placeholderId && this.getSizeCallbacks.has(placeholderId)) {
22
+ const resolve = this.getSizeCallbacks.get(placeholderId);
23
+ this.getSizeCallbacks.delete(placeholderId);
24
+ if (!resolve) {
25
+ return;
26
+ }
27
+ resolve({
28
+ x: boundingClientRect.x,
29
+ y: boundingClientRect.y,
30
+ width: boundingClientRect.width,
31
+ height: boundingClientRect.height
32
+ });
33
+ return;
34
+ }
35
+ if (placeholderId && this.isAddedPlaceholderMatchingSizeCallbacks.has(placeholderId)) {
36
+ const resolve = this.isAddedPlaceholderMatchingSizeCallbacks.get(placeholderId);
37
+ this.isAddedPlaceholderMatchingSizeCallbacks.delete(placeholderId);
38
+ if (!resolve) {
39
+ return;
40
+ }
41
+ const placeholder = this.placeholders.get(placeholderId);
42
+ if (!placeholder) {
43
+ resolve(false);
44
+ return;
45
+ }
46
+ resolve(this.areRectsSameSize(placeholder, boundingClientRect));
47
+ return;
48
+ }
49
+ const replaceId = (_target$dataset2 = target.dataset) === null || _target$dataset2 === void 0 ? void 0 : _target$dataset2[replaceDataKey];
50
+ if (replaceId && this.isAddedReplaceMatchingSizeCallbacks.has(replaceId)) {
51
+ const resolve = this.isAddedReplaceMatchingSizeCallbacks.get(replaceId);
52
+ this.isAddedReplaceMatchingSizeCallbacks.delete(replaceId);
53
+ if (!resolve) {
54
+ return;
55
+ }
56
+ const placeholder = this.placeholders.get(replaceId);
57
+ if (!placeholder) {
58
+ resolve(false);
59
+ return;
60
+ }
61
+ this.placeholders.delete(replaceId);
62
+ resolve(this.areRectsSameSize(placeholder, boundingClientRect));
63
+ return;
64
+ }
65
+ });
66
+ this.intersectionObserver = new IntersectionObserver(entries => entries.filter(entry => entry.intersectionRatio > 0).forEach(this.intersectionObserverCallback));
67
+ }
68
+ shouldHandleAddedNode(el) {
69
+ var _el$dataset, _el$dataset2;
70
+ return ((_el$dataset = el.dataset) === null || _el$dataset === void 0 ? void 0 : _el$dataset[placeholderDataKey]) || ((_el$dataset2 = el.dataset) === null || _el$dataset2 === void 0 ? void 0 : _el$dataset2[replaceDataKey]);
71
+ }
72
+ handleAddedNode(el) {
73
+ var _el$dataset3, _el$dataset4;
74
+ // If it placeholder does not already exist, add it to the map
75
+ const placeholderId = (_el$dataset3 = el.dataset) === null || _el$dataset3 === void 0 ? void 0 : _el$dataset3[placeholderDataKey];
76
+ if (placeholderId) {
77
+ return this.handleAddedPlaceholderNode(el, placeholderId);
78
+ }
79
+ const replaceId = (_el$dataset4 = el.dataset) === null || _el$dataset4 === void 0 ? void 0 : _el$dataset4[replaceDataKey];
80
+ if (replaceId) {
81
+ return this.handleAddedReplaceNode(el, replaceId);
82
+ }
83
+ return Promise.resolve({
84
+ shouldIgnore: false
85
+ });
86
+ }
87
+ clear() {
88
+ this.placeholders.clear();
89
+ this.intersectionObserver.disconnect();
90
+ }
91
+ handleAddedPlaceholderNode(el, placeholderId) {
92
+ if (this.isExistingPlaceholder(placeholderId)) {
93
+ return this.isAddedPlaceholderMatchingSize(el, placeholderId).then(isMatching => ({
94
+ shouldIgnore: isMatching
95
+ }));
96
+ }
97
+ return this.registerPlaceholder(el, placeholderId).then(() => ({
98
+ shouldIgnore: false
99
+ }));
100
+ }
101
+ handleAddedReplaceNode(el, placeholderId) {
102
+ if (this.isExistingPlaceholder(placeholderId)) {
103
+ return this.isAddedReplaceMatchingSize(el, placeholderId).then(isMatching => ({
104
+ shouldIgnore: isMatching
105
+ }));
106
+ }
107
+ return Promise.resolve({
108
+ shouldIgnore: false
109
+ });
110
+ }
111
+ isExistingPlaceholder(placeholderId) {
112
+ return this.placeholders.has(placeholderId);
113
+ }
114
+ registerPlaceholder(el, placeholderId) {
115
+ return this.getSize(el, placeholderId).then(size => {
116
+ this.placeholders.set(placeholderId, size);
117
+ });
118
+ }
119
+ getSize(el, placeholderId) {
120
+ return new Promise(resolve => {
121
+ this.getSizeCallbacks.set(placeholderId, resolve);
122
+ this.intersectionObserver.observe(el);
123
+ });
124
+ }
125
+ isAddedPlaceholderMatchingSize(el, placeholderId) {
126
+ return new Promise(resolve => {
127
+ this.isAddedPlaceholderMatchingSizeCallbacks.set(placeholderId, resolve);
128
+ this.intersectionObserver.observe(el);
129
+ });
130
+ }
131
+ isAddedReplaceMatchingSize(el, placeholderId) {
132
+ return new Promise(resolve => {
133
+ this.isAddedReplaceMatchingSizeCallbacks.set(placeholderId, resolve);
134
+ this.intersectionObserver.observe(el);
135
+ });
136
+ }
137
+ areRectsSameSize(a, b) {
138
+ return Math.abs(a.width - b.width) < 1 && Math.abs(a.height - b.height) < 1;
139
+ }
140
+ }
@@ -0,0 +1,41 @@
1
+ export function createPlaceholderElement(id) {
2
+ const el = document.createElement('div');
3
+ el.dataset.editorLnvPlaceholder = id;
4
+ return el;
5
+ }
6
+ export function createReplaceElement(id) {
7
+ const el = document.createElement('div');
8
+ el.dataset.editorLnvPlaceholderReplace = id;
9
+ return el;
10
+ }
11
+ export function handleElements(elements, handler, observerCallback, callback) {
12
+ const [first, ...rest] = elements;
13
+ handler.handleAddedNode(first.element).then(result => {
14
+ if (!rest.length) {
15
+ // Recursion terminating condition
16
+ callback(result);
17
+ } else {
18
+ // Recursively handle the rest of the elements
19
+ handleElements(rest, handler, observerCallback, callback);
20
+ }
21
+ });
22
+ observerCallback([{
23
+ target: first.element,
24
+ boundingClientRect: first.rect
25
+ }]);
26
+ }
27
+ export function createMockIntersectionObserver(cbRef) {
28
+ return class {
29
+ constructor(fn) {
30
+ cbRef.current = entries => fn(entries.map(entry => {
31
+ return {
32
+ ...entry,
33
+ intersectionRatio: 1
34
+ };
35
+ }));
36
+ }
37
+ observe() {}
38
+ unobserve() {}
39
+ disconnect() {}
40
+ };
41
+ }
@@ -1,5 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import { fg } from '@atlaskit/platform-feature-flags';
2
3
  import { isContainedWithinMediaWrapper } from '../media-wrapper/vc-utils';
4
+ import { EditorLnvHandler } from './editor-lnv';
3
5
  import { SSRPlaceholderHandlers } from './ssr-placeholders';
4
6
  const state = {
5
7
  normal: 1,
@@ -45,6 +47,7 @@ export class Observers {
45
47
  this.intersectionObserver = this.getIntersectionObserver();
46
48
  this.mutationObserver = this.getMutationObserver();
47
49
  this.ssrPlaceholderHandler = new SSRPlaceholderHandlers();
50
+ this.editorLnvHandler = new EditorLnvHandler();
48
51
  }
49
52
  isBrowserSupported() {
50
53
  return typeof window.IntersectionObserver === 'function' && typeof window.MutationObserver === 'function';
@@ -75,6 +78,7 @@ export class Observers {
75
78
  this.callbacks = new Set();
76
79
  this.ssr.reactRootElement = null;
77
80
  this.ssrPlaceholderHandler.clear();
81
+ this.editorLnvHandler.clear();
78
82
  }
79
83
  getTotalTime() {
80
84
  return this.totalTime;
@@ -135,6 +139,16 @@ export class Observers {
135
139
  });
136
140
  return;
137
141
  }
142
+ if (fg('platform_editor_ed-25557_lnv_add_ssr_placeholder')) {
143
+ if (this.editorLnvHandler.shouldHandleAddedNode(node)) {
144
+ this.editorLnvHandler.handleAddedNode(node).then(({
145
+ shouldIgnore
146
+ }) => {
147
+ this.observeElement(node, mutation, 'html', shouldIgnore ? 'editor-lazy-node-view' : ignoreReason);
148
+ });
149
+ return;
150
+ }
151
+ }
138
152
  this.observeElement(node, mutation, 'html', ignoreReason);
139
153
  }
140
154
  if (node instanceof Text && node.parentElement != null) {
@@ -30,7 +30,6 @@ export class SSRPlaceholderHandlers {
30
30
  this.callbacks.delete(staticKey);
31
31
  }
32
32
  } else {
33
- // react component check
34
33
  const key = target.dataset.ssrPlaceholderReplace || '';
35
34
  const resolve = this.reactValidateCallbacks.get(key);
36
35
  if (!resolve) {
@@ -32,6 +32,10 @@ export function getActiveInteraction() {
32
32
  }
33
33
  return interactions.get(interactionId.current);
34
34
  }
35
+ function isPerformanceTracingEnabled() {
36
+ var _getConfig;
37
+ return ((_getConfig = getConfig()) === null || _getConfig === void 0 ? void 0 : _getConfig.enableAdditionalPerformanceMarks) || window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production';
38
+ }
35
39
  function labelStackToString(labelStack, name) {
36
40
  var _stack$map;
37
41
  var stack = _toConsumableArray(labelStack !== null && labelStack !== void 0 ? labelStack : []);
@@ -111,7 +115,7 @@ export function addCustomTiming(interactionId, labelStack, data) {
111
115
  labelStack: labelStack,
112
116
  data: data
113
117
  });
114
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
118
+ if (isPerformanceTracingEnabled()) {
115
119
  for (var _i = 0, _Object$entries = Object.entries(data); _i < _Object$entries.length; _i++) {
116
120
  var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
117
121
  key = _Object$entries$_i[0],
@@ -142,7 +146,7 @@ export function addMark(interactionId, type, name, labelStack) {
142
146
  time: time
143
147
  });
144
148
  }
145
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
149
+ if (isPerformanceTracingEnabled()) {
146
150
  performance.mark("\uD83D\uDEF8 ".concat(labelStackToString(labelStack, name), " [").concat(type, "]"), {
147
151
  startTime: time
148
152
  });
@@ -158,7 +162,7 @@ export function addMarkToAll(type, name, labelStack) {
158
162
  time: time
159
163
  });
160
164
  });
161
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
165
+ if (isPerformanceTracingEnabled()) {
162
166
  performance.mark("\uD83D\uDEF8 ".concat(labelStackToString(labelStack, name), " [").concat(type, "]"), {
163
167
  startTime: time
164
168
  });
@@ -177,7 +181,7 @@ export function addSpan(interactionId, type, name, labelStack, start) {
177
181
  end: end,
178
182
  size: size
179
183
  });
180
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
184
+ if (isPerformanceTracingEnabled()) {
181
185
  try {
182
186
  // for Firefox 102 and older
183
187
  performance.measure("\uD83D\uDEF8 ".concat(labelStackToString(labelStack, name), " [").concat(type, "]"), {
@@ -203,7 +207,7 @@ export function addSpanToAll(type, name, labelStack, start) {
203
207
  size: size
204
208
  });
205
209
  });
206
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
210
+ if (isPerformanceTracingEnabled()) {
207
211
  try {
208
212
  // for Firefox 102 and older
209
213
  performance.measure("\uD83D\uDEF8 ".concat(labelStackToString(labelStack, name), " [").concat(type, "]"), {
@@ -256,7 +260,7 @@ export function addHold(interactionId, labelStack, name) {
256
260
  addHoldCriterion(id, labelStack, name, start);
257
261
  return function () {
258
262
  var end = performance.now();
259
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
263
+ if (isPerformanceTracingEnabled()) {
260
264
  try {
261
265
  // for Firefox 102 and older
262
266
  performance.measure("\uD83D\uDEF8 ".concat(labelStackToString(labelStack, name), " [hold]"), {
@@ -376,8 +380,8 @@ export function addErrorToAll(name, labelStack, errorType, errorMessage, errorSt
376
380
  });
377
381
  }
378
382
  export var addProfilerTimings = function addProfilerTimings(interactionId, labelStack, type, actualDuration, baseDuration, startTime, commitTime) {
379
- var _getConfig;
380
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
383
+ var _getConfig2;
384
+ if (isPerformanceTracingEnabled()) {
381
385
  try {
382
386
  // for Firefox 102 and older
383
387
  performance.measure("\uD83D\uDEF8 ".concat(labelStackToString(labelStack), " [react-profiler] ").concat(type), {
@@ -398,7 +402,7 @@ export var addProfilerTimings = function addProfilerTimings(interactionId, label
398
402
  startTime: startTime,
399
403
  commitTime: commitTime
400
404
  });
401
- } else if ((_getConfig = getConfig()) !== null && _getConfig !== void 0 && (_getConfig = _getConfig.postInteractionLog) !== null && _getConfig !== void 0 && _getConfig.enabled) {
405
+ } else if ((_getConfig2 = getConfig()) !== null && _getConfig2 !== void 0 && (_getConfig2 = _getConfig2.postInteractionLog) !== null && _getConfig2 !== void 0 && _getConfig2.enabled) {
402
406
  postInteractionLog.addProfilerTimings(labelStack, type, actualDuration, baseDuration, startTime, commitTime);
403
407
  }
404
408
  };
@@ -415,7 +419,7 @@ function callCleanUpCallbacks(interaction) {
415
419
  });
416
420
  }
417
421
  var finishInteraction = function finishInteraction(id, data) {
418
- var _getConfig2;
422
+ var _getConfig3;
419
423
  var endTime = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : performance.now();
420
424
  // eslint-disable-next-line no-param-reassign
421
425
  data.end = endTime;
@@ -434,7 +438,7 @@ var finishInteraction = function finishInteraction(id, data) {
434
438
  }
435
439
  clearActiveTrace();
436
440
  callCleanUpCallbacks(data);
437
- if ((_getConfig2 = getConfig()) !== null && _getConfig2 !== void 0 && (_getConfig2 = _getConfig2.vc) !== null && _getConfig2 !== void 0 && _getConfig2.stopVCAtInteractionFinish) {
441
+ if ((_getConfig3 = getConfig()) !== null && _getConfig3 !== void 0 && (_getConfig3 = _getConfig3.vc) !== null && _getConfig3 !== void 0 && _getConfig3.stopVCAtInteractionFinish) {
438
442
  data.vc = getVCObserver().getVCRawData();
439
443
  }
440
444
  remove(id);
@@ -443,7 +447,7 @@ var finishInteraction = function finishInteraction(id, data) {
443
447
  if (data.ufoName) {
444
448
  handleInteraction(id, data);
445
449
  }
446
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
450
+ if (isPerformanceTracingEnabled()) {
447
451
  var profilerTimingMap = new Map();
448
452
  data.reactProfilerTimings.forEach(function (profilerTiming) {
449
453
  var labelStackId = labelStackToIdString(profilerTiming.labelStack);
@@ -508,9 +512,9 @@ export function tryComplete(interactionId, endTime) {
508
512
  if (interaction != null) {
509
513
  var noMoreHolds = interaction.holdActive.size === 0;
510
514
  if (noMoreHolds) {
511
- var _getConfig3;
515
+ var _getConfig4;
512
516
  finishInteraction(interactionId, interaction, endTime);
513
- if ((_getConfig3 = getConfig()) !== null && _getConfig3 !== void 0 && (_getConfig3 = _getConfig3.postInteractionLog) !== null && _getConfig3 !== void 0 && _getConfig3.enabled) {
517
+ if ((_getConfig4 = getConfig()) !== null && _getConfig4 !== void 0 && (_getConfig4 = _getConfig4.postInteractionLog) !== null && _getConfig4 !== void 0 && _getConfig4.enabled) {
514
518
  postInteractionLog.onInteractionComplete(interaction);
515
519
  }
516
520
  }
@@ -558,9 +562,9 @@ export function addOnCancelCallback(id, cancelCallback) {
558
562
  interaction === null || interaction === void 0 || interaction.cancelCallbacks.push(cancelCallback);
559
563
  }
560
564
  export function addNewInteraction(interactionId, ufoName, type, startTime, rate, labelStack, routeName) {
561
- var _getConfig4;
565
+ var _getConfig5;
562
566
  var trace = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : null;
563
- if ((_getConfig4 = getConfig()) !== null && _getConfig4 !== void 0 && (_getConfig4 = _getConfig4.postInteractionLog) !== null && _getConfig4 !== void 0 && _getConfig4.enabled) {
567
+ if ((_getConfig5 = getConfig()) !== null && _getConfig5 !== void 0 && (_getConfig5 = _getConfig5.postInteractionLog) !== null && _getConfig5 !== void 0 && _getConfig5.enabled) {
564
568
  postInteractionLog.reset();
565
569
  }
566
570
  var previousTime = startTime;
@@ -756,7 +760,7 @@ export function addRedirect(interactionId, fromUfoName, nextUfoName, nextRouteNa
756
760
  fromInteractionName: fromUfoName,
757
761
  time: time
758
762
  });
759
- if (window.__REACT_UFO_ENABLE_PERF_TRACING || process.env.NODE_ENV !== 'production') {
763
+ if (isPerformanceTracingEnabled()) {
760
764
  var prevRedirect = interaction.redirects.at(-2);
761
765
  try {
762
766
  var _prevRedirect$time;
@@ -31,6 +31,9 @@ export var init = function init(analyticsWebClientAsync, config) {
31
31
  if (initialized) {
32
32
  return;
33
33
  }
34
+ if (window !== undefined) {
35
+ window.__REACT_UFO_ENABLE_PERF_TRACING = Boolean(sessionStorage.getItem('additionalPerfMarks') === 'true');
36
+ }
34
37
  setUFOConfig(config);
35
38
  if ((_config$vc = config.vc) !== null && _config$vc !== void 0 && _config$vc.enabled) {
36
39
  var vcOptions = {
@@ -0,0 +1,181 @@
1
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
2
+ import _createClass from "@babel/runtime/helpers/createClass";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+ var placeholderDataKey = 'editorLnvPlaceholder'; // data-editor-lnv-placeholder
5
+ var replaceDataKey = 'editorLnvPlaceholderReplace'; // data-editor-lnv-placeholder-replace
6
+
7
+ export var EditorLnvHandler = /*#__PURE__*/function () {
8
+ function EditorLnvHandler() {
9
+ var _this = this;
10
+ _classCallCheck(this, EditorLnvHandler);
11
+ _defineProperty(this, "placeholders", new Map());
12
+ _defineProperty(this, "getSizeCallbacks", new Map());
13
+ _defineProperty(this, "isAddedPlaceholderMatchingSizeCallbacks", new Map());
14
+ _defineProperty(this, "isAddedReplaceMatchingSizeCallbacks", new Map());
15
+ _defineProperty(this, "intersectionObserverCallback", function (_ref) {
16
+ var _target$dataset, _target$dataset2;
17
+ var target = _ref.target,
18
+ boundingClientRect = _ref.boundingClientRect;
19
+ _this.intersectionObserver.unobserve(target);
20
+ if (!(target instanceof HTMLElement)) {
21
+ return;
22
+ }
23
+ var placeholderId = (_target$dataset = target.dataset) === null || _target$dataset === void 0 ? void 0 : _target$dataset[placeholderDataKey];
24
+ if (placeholderId && _this.getSizeCallbacks.has(placeholderId)) {
25
+ var _resolve = _this.getSizeCallbacks.get(placeholderId);
26
+ _this.getSizeCallbacks.delete(placeholderId);
27
+ if (!_resolve) {
28
+ return;
29
+ }
30
+ _resolve({
31
+ x: boundingClientRect.x,
32
+ y: boundingClientRect.y,
33
+ width: boundingClientRect.width,
34
+ height: boundingClientRect.height
35
+ });
36
+ return;
37
+ }
38
+ if (placeholderId && _this.isAddedPlaceholderMatchingSizeCallbacks.has(placeholderId)) {
39
+ var _resolve2 = _this.isAddedPlaceholderMatchingSizeCallbacks.get(placeholderId);
40
+ _this.isAddedPlaceholderMatchingSizeCallbacks.delete(placeholderId);
41
+ if (!_resolve2) {
42
+ return;
43
+ }
44
+ var placeholder = _this.placeholders.get(placeholderId);
45
+ if (!placeholder) {
46
+ _resolve2(false);
47
+ return;
48
+ }
49
+ _resolve2(_this.areRectsSameSize(placeholder, boundingClientRect));
50
+ return;
51
+ }
52
+ var replaceId = (_target$dataset2 = target.dataset) === null || _target$dataset2 === void 0 ? void 0 : _target$dataset2[replaceDataKey];
53
+ if (replaceId && _this.isAddedReplaceMatchingSizeCallbacks.has(replaceId)) {
54
+ var _resolve3 = _this.isAddedReplaceMatchingSizeCallbacks.get(replaceId);
55
+ _this.isAddedReplaceMatchingSizeCallbacks.delete(replaceId);
56
+ if (!_resolve3) {
57
+ return;
58
+ }
59
+ var _placeholder = _this.placeholders.get(replaceId);
60
+ if (!_placeholder) {
61
+ _resolve3(false);
62
+ return;
63
+ }
64
+ _this.placeholders.delete(replaceId);
65
+ _resolve3(_this.areRectsSameSize(_placeholder, boundingClientRect));
66
+ return;
67
+ }
68
+ });
69
+ this.intersectionObserver = new IntersectionObserver(function (entries) {
70
+ return entries.filter(function (entry) {
71
+ return entry.intersectionRatio > 0;
72
+ }).forEach(_this.intersectionObserverCallback);
73
+ });
74
+ }
75
+ _createClass(EditorLnvHandler, [{
76
+ key: "shouldHandleAddedNode",
77
+ value: function shouldHandleAddedNode(el) {
78
+ var _el$dataset, _el$dataset2;
79
+ return ((_el$dataset = el.dataset) === null || _el$dataset === void 0 ? void 0 : _el$dataset[placeholderDataKey]) || ((_el$dataset2 = el.dataset) === null || _el$dataset2 === void 0 ? void 0 : _el$dataset2[replaceDataKey]);
80
+ }
81
+ }, {
82
+ key: "handleAddedNode",
83
+ value: function handleAddedNode(el) {
84
+ var _el$dataset3, _el$dataset4;
85
+ // If it placeholder does not already exist, add it to the map
86
+ var placeholderId = (_el$dataset3 = el.dataset) === null || _el$dataset3 === void 0 ? void 0 : _el$dataset3[placeholderDataKey];
87
+ if (placeholderId) {
88
+ return this.handleAddedPlaceholderNode(el, placeholderId);
89
+ }
90
+ var replaceId = (_el$dataset4 = el.dataset) === null || _el$dataset4 === void 0 ? void 0 : _el$dataset4[replaceDataKey];
91
+ if (replaceId) {
92
+ return this.handleAddedReplaceNode(el, replaceId);
93
+ }
94
+ return Promise.resolve({
95
+ shouldIgnore: false
96
+ });
97
+ }
98
+ }, {
99
+ key: "clear",
100
+ value: function clear() {
101
+ this.placeholders.clear();
102
+ this.intersectionObserver.disconnect();
103
+ }
104
+ }, {
105
+ key: "handleAddedPlaceholderNode",
106
+ value: function handleAddedPlaceholderNode(el, placeholderId) {
107
+ if (this.isExistingPlaceholder(placeholderId)) {
108
+ return this.isAddedPlaceholderMatchingSize(el, placeholderId).then(function (isMatching) {
109
+ return {
110
+ shouldIgnore: isMatching
111
+ };
112
+ });
113
+ }
114
+ return this.registerPlaceholder(el, placeholderId).then(function () {
115
+ return {
116
+ shouldIgnore: false
117
+ };
118
+ });
119
+ }
120
+ }, {
121
+ key: "handleAddedReplaceNode",
122
+ value: function handleAddedReplaceNode(el, placeholderId) {
123
+ if (this.isExistingPlaceholder(placeholderId)) {
124
+ return this.isAddedReplaceMatchingSize(el, placeholderId).then(function (isMatching) {
125
+ return {
126
+ shouldIgnore: isMatching
127
+ };
128
+ });
129
+ }
130
+ return Promise.resolve({
131
+ shouldIgnore: false
132
+ });
133
+ }
134
+ }, {
135
+ key: "isExistingPlaceholder",
136
+ value: function isExistingPlaceholder(placeholderId) {
137
+ return this.placeholders.has(placeholderId);
138
+ }
139
+ }, {
140
+ key: "registerPlaceholder",
141
+ value: function registerPlaceholder(el, placeholderId) {
142
+ var _this2 = this;
143
+ return this.getSize(el, placeholderId).then(function (size) {
144
+ _this2.placeholders.set(placeholderId, size);
145
+ });
146
+ }
147
+ }, {
148
+ key: "getSize",
149
+ value: function getSize(el, placeholderId) {
150
+ var _this3 = this;
151
+ return new Promise(function (resolve) {
152
+ _this3.getSizeCallbacks.set(placeholderId, resolve);
153
+ _this3.intersectionObserver.observe(el);
154
+ });
155
+ }
156
+ }, {
157
+ key: "isAddedPlaceholderMatchingSize",
158
+ value: function isAddedPlaceholderMatchingSize(el, placeholderId) {
159
+ var _this4 = this;
160
+ return new Promise(function (resolve) {
161
+ _this4.isAddedPlaceholderMatchingSizeCallbacks.set(placeholderId, resolve);
162
+ _this4.intersectionObserver.observe(el);
163
+ });
164
+ }
165
+ }, {
166
+ key: "isAddedReplaceMatchingSize",
167
+ value: function isAddedReplaceMatchingSize(el, placeholderId) {
168
+ var _this5 = this;
169
+ return new Promise(function (resolve) {
170
+ _this5.isAddedReplaceMatchingSizeCallbacks.set(placeholderId, resolve);
171
+ _this5.intersectionObserver.observe(el);
172
+ });
173
+ }
174
+ }, {
175
+ key: "areRectsSameSize",
176
+ value: function areRectsSameSize(a, b) {
177
+ return Math.abs(a.width - b.width) < 1 && Math.abs(a.height - b.height) < 1;
178
+ }
179
+ }]);
180
+ return EditorLnvHandler;
181
+ }();
@@ -0,0 +1,59 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
3
+ import _createClass from "@babel/runtime/helpers/createClass";
4
+ import _toArray from "@babel/runtime/helpers/toArray";
5
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
6
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7
+ export function createPlaceholderElement(id) {
8
+ var el = document.createElement('div');
9
+ el.dataset.editorLnvPlaceholder = id;
10
+ return el;
11
+ }
12
+ export function createReplaceElement(id) {
13
+ var el = document.createElement('div');
14
+ el.dataset.editorLnvPlaceholderReplace = id;
15
+ return el;
16
+ }
17
+ export function handleElements(elements, handler, observerCallback, callback) {
18
+ var _elements = _toArray(elements),
19
+ first = _elements[0],
20
+ rest = _elements.slice(1);
21
+ handler.handleAddedNode(first.element).then(function (result) {
22
+ if (!rest.length) {
23
+ // Recursion terminating condition
24
+ callback(result);
25
+ } else {
26
+ // Recursively handle the rest of the elements
27
+ handleElements(rest, handler, observerCallback, callback);
28
+ }
29
+ });
30
+ observerCallback([{
31
+ target: first.element,
32
+ boundingClientRect: first.rect
33
+ }]);
34
+ }
35
+ export function createMockIntersectionObserver(cbRef) {
36
+ return /*#__PURE__*/function () {
37
+ function _class(fn) {
38
+ _classCallCheck(this, _class);
39
+ cbRef.current = function (entries) {
40
+ return fn(entries.map(function (entry) {
41
+ return _objectSpread(_objectSpread({}, entry), {}, {
42
+ intersectionRatio: 1
43
+ });
44
+ }));
45
+ };
46
+ }
47
+ _createClass(_class, [{
48
+ key: "observe",
49
+ value: function observe() {}
50
+ }, {
51
+ key: "unobserve",
52
+ value: function unobserve() {}
53
+ }, {
54
+ key: "disconnect",
55
+ value: function disconnect() {}
56
+ }]);
57
+ return _class;
58
+ }();
59
+ }