UNPKG

@atlaskit/editor-common

Version:

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

169 lines (168 loc) 7.02 kB
import _extends from "@babel/runtime/helpers/extends"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } import React from 'react'; import { ShadowObserver, shadowObserverClassNames } from './shadowObserver'; export var shadowClassNames = { RIGHT_SHADOW: 'right-shadow', LEFT_SHADOW: 'left-shadow' }; export default function overflowShadow(Component, options) { return /*#__PURE__*/function (_React$Component) { function OverflowShadow() { var _this; _classCallCheck(this, OverflowShadow); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, OverflowShadow, [].concat(args)); _defineProperty(_this, "overflowContainerWidth", 0); _defineProperty(_this, "state", { showLeftShadow: false, showRightShadow: false }); _defineProperty(_this, "handleScroll", function (event) { if (!_this.overflowContainer || event.target !== _this.overflowContainer || options.useShadowObserver) { return; } _this.updateShadows(); }); _defineProperty(_this, "updateShadows", function () { if (options.useShadowObserver) { return; } _this.setState(function (prevState) { if (!_this.overflowContainer) { return; } var diff = _this.calcOverflowDiff(); var showRightShadow = diff > 0 && diff > _this.overflowContainer.scrollLeft; var showLeftShadow = _this.showLeftShadow(_this.overflowContainer); if (showLeftShadow !== prevState.showLeftShadow || showRightShadow !== _this.state.showRightShadow) { return { showLeftShadow: showLeftShadow, showRightShadow: showRightShadow }; } return null; }); }); _defineProperty(_this, "showLeftShadow", function (overflowContainer) { return !!overflowContainer && overflowContainer.scrollLeft > 0; }); _defineProperty(_this, "calcOverflowDiff", function () { if (!_this.overflowContainer) { return 0; } _this.diff = _this.calcScrollableWidth(); return _this.diff - _this.overflowContainer.offsetWidth; }); _defineProperty(_this, "calcScrollableWidth", function () { if (!_this.scrollable && _this.overflowContainer) { return _this.overflowContainer.scrollWidth; } if (!_this.scrollable) { return 0; } var width = 0; for (var i = 0; i < _this.scrollable.length; i++) { // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting var scrollableElement = _this.scrollable[i]; width += scrollableElement.scrollWidth; } return width; }); _defineProperty(_this, "handleContainer", function (container) { if (!container || _this.container) { return; } _this.container = container; // Ignored via go/ees005 // eslint-disable-next-line @atlaskit/editor/no-as-casting _this.overflowContainer = container.querySelector(options.overflowSelector); if (!_this.overflowContainer) { _this.overflowContainer = container; } if (options.useShadowObserver) { _this.initShadowObserver(); return; } _this.updateShadows(); // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners _this.overflowContainer.addEventListener('scroll', _this.handleScroll); }); return _this; } _inherits(OverflowShadow, _React$Component); return _createClass(OverflowShadow, [{ key: "componentWillUnmount", value: function componentWillUnmount() { if (options.useShadowObserver) { return this.shadowObserver && this.shadowObserver.dispose(); } if (this.overflowContainer) { // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners this.overflowContainer.removeEventListener('scroll', this.handleScroll); } this.updateShadows(); } }, { key: "componentDidUpdate", value: function componentDidUpdate() { if (!options.useShadowObserver) { this.updateShadows(); } } }, { key: "initShadowObserver", value: function initShadowObserver() { var _this2 = this; if (this.shadowObserver || !this.overflowContainer) { return; } this.shadowObserver = new ShadowObserver({ scrollContainer: this.overflowContainer, onUpdateShadows: function onUpdateShadows(shadowStates) { _this2.setState(function (prevState) { var showLeftShadow = shadowStates.showLeftShadow, showRightShadow = shadowStates.showRightShadow; if (showLeftShadow !== prevState.showLeftShadow || showRightShadow !== prevState.showRightShadow) { return { showLeftShadow: showLeftShadow, showRightShadow: showRightShadow }; } return null; }); } }); } }, { key: "render", value: function render() { var _this$state = this.state, showLeftShadow = _this$state.showLeftShadow, showRightShadow = _this$state.showRightShadow; var classNames = [showRightShadow && shadowClassNames.RIGHT_SHADOW, showLeftShadow && shadowClassNames.LEFT_SHADOW, options.useShadowObserver && shadowObserverClassNames.SHADOW_CONTAINER].filter(Boolean).join(' '); return ( /*#__PURE__*/ // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading React.createElement(Component, _extends({ handleRef: this.handleContainer, shadowClassNames: classNames }, this.props)) ); } }]); }(React.Component); }