UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

82 lines 3.6 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; export var ShadowKeys = /*#__PURE__*/function (ShadowKeys) { ShadowKeys["SHOW_LEFT_SHADOW"] = "showLeftShadow"; ShadowKeys["SHOW_RIGHT_SHADOW"] = "showRightShadow"; return ShadowKeys; }({}); export var shadowObserverClassNames = { SENTINEL_LEFT: 'sentinel-left', SENTINEL_RIGHT: 'sentinel-right', SHADOW_CONTAINER: 'with-shadow-observer' }; var requestIdleCallback = function requestIdleCallback(fn) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any return window.requestIdleCallback ? // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any window.requestIdleCallback(fn) : window.requestAnimationFrame(fn); }; var cancelIdleCallback = function cancelIdleCallback(id) { // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any return window.cancelIdleCallback ? // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any window.cancelIdleCallback(id) : window.cancelAnimationFrame(id); }; export var ShadowObserver = /*#__PURE__*/function () { function ShadowObserver(_ref) { var _this = this; var scrollContainer = _ref.scrollContainer, onUpdateShadows = _ref.onUpdateShadows; _classCallCheck(this, ShadowObserver); _defineProperty(this, "sentinels", {}); _defineProperty(this, "shadowStates", _defineProperty(_defineProperty({}, ShadowKeys.SHOW_LEFT_SHADOW, false), ShadowKeys.SHOW_RIGHT_SHADOW, false)); _defineProperty(this, "init", function () { if (!_this.scrollContainer || _this.intersectionObserver) { return; } _this.sentinels.right = document.createElement('div'); _this.sentinels.right.classList.add(shadowObserverClassNames.SENTINEL_RIGHT); _this.scrollContainer.appendChild(_this.sentinels.right); _this.sentinels.left = document.createElement('div'); _this.sentinels.left.classList.add(shadowObserverClassNames.SENTINEL_LEFT); _this.scrollContainer.prepend(_this.sentinels.left); _this.intersectionObserver = new IntersectionObserver(function (entries, _) { entries.forEach(_this.onIntersect); }, { root: _this.scrollContainer, rootMargin: '1px' }); _this.intersectionObserver.observe(_this.sentinels.left); _this.intersectionObserver.observe(_this.sentinels.right); }); _defineProperty(this, "onIntersect", function (entry) { _this.requestCallbackId = requestIdleCallback(function () { if (entry.target.classList.contains(shadowObserverClassNames.SENTINEL_LEFT)) { _this.shadowStates[ShadowKeys.SHOW_LEFT_SHADOW] = !entry.isIntersecting; } if (entry.target.classList.contains(shadowObserverClassNames.SENTINEL_RIGHT)) { _this.shadowStates[ShadowKeys.SHOW_RIGHT_SHADOW] = !entry.isIntersecting; } _this.onUpdateShadows(_this.shadowStates); }); }); this.scrollContainer = scrollContainer; this.onUpdateShadows = onUpdateShadows; this.init(); } return _createClass(ShadowObserver, [{ key: "dispose", value: function dispose() { if (this.intersectionObserver) { this.intersectionObserver.disconnect(); this.intersectionObserver = undefined; this.requestCallbackId && cancelIdleCallback(this.requestCallbackId); } } }]); }();