UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

178 lines (177 loc) • 10.5 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextPanel = ContextPanel; exports.SwappableContentArea = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _reactIntl = require("react-intl"); var _Transition = _interopRequireDefault(require("react-transition-group/Transition")); var _contextPanel = require("@atlaskit/editor-common/context-panel"); var _hooks = require("@atlaskit/editor-common/hooks"); var _messages = require("@atlaskit/editor-common/messages"); var _editorSharedStyles = require("@atlaskit/editor-shared-styles"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _platformFeatureFlagsReact = require("@atlaskit/platform-feature-flags-react"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _indexCompiled = require("./index-compiled"); var _indexEmotion = require("./index-emotion"); 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; } 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) { (0, _defineProperty2.default)(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; } function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(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; })(); } /* eslint-disable jsdoc/require-jsdoc -- Pre-existing lint debt surfaced by this mechanical type-import-only PR. */ var ANIM_SPEED_MS = 500; var ContextPanelWrapperMigration = (0, _platformFeatureFlagsReact.componentWithCondition)(function () { return (0, _expValEquals.expValEquals)('platform_editor_core_non_ecc_static_css', 'isEnabled', true); }, _indexCompiled.ContextPanelWrapperCompiled, _indexEmotion.ContextPanelWrapperEmotion); var ContextPanelContentMigration = (0, _platformFeatureFlagsReact.componentWithCondition)(function () { return (0, _expValEquals.expValEquals)('platform_editor_core_non_ecc_static_css', 'isEnabled', true); }, _indexCompiled.ContextPanelContentCompiled, _indexEmotion.ContextPanelContentEmotion); // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components var SwappableContentAreaInner = /*#__PURE__*/function (_React$PureComponent) { function SwappableContentAreaInner() { var _this; (0, _classCallCheck2.default)(this, SwappableContentAreaInner); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, SwappableContentAreaInner, [].concat(args)); (0, _defineProperty2.default)(_this, "state", { mounted: false, currentPluginContent: undefined }); (0, _defineProperty2.default)(_this, "handleTransitionExited", function () { _this.unsetPluginContent(); }); (0, _defineProperty2.default)(_this, "focusEditor", function () { var _editorAPI$core; var editorAPI = _this.props.editorAPI; editorAPI === null || editorAPI === void 0 || (_editorAPI$core = editorAPI.core) === null || _editorAPI$core === void 0 || _editorAPI$core.actions.focus({ scrollIntoView: false }); }); (0, _defineProperty2.default)(_this, "showPluginContent", function () { var pluginContent = _this.props.pluginContent; var currentPluginContent = _this.state.currentPluginContent; if (!currentPluginContent) { return; } var animSpeedMs = (0, _platformFeatureFlags.fg)('platform_editor_disable_context_panel_animation') ? 0 : ANIM_SPEED_MS; var onExited = (0, _expValEquals.expValEquals)('platform_editor_perf_lint_cleanup', 'isEnabled', true) ? _this.handleTransitionExited : function () { return _this.unsetPluginContent(); }; return /*#__PURE__*/_react.default.createElement(_Transition.default, { timeout: _this.state.mounted ? animSpeedMs : 0, in: !!pluginContent, mountOnEnter: true, unmountOnExit: true, onExited: onExited }, currentPluginContent); }); (0, _defineProperty2.default)(_this, "showProvidedContent", function (isVisible) { var children = _this.props.children; if (!children) { return; } var animSpeedMs = (0, _platformFeatureFlags.fg)('platform_editor_disable_context_panel_animation') ? 0 : ANIM_SPEED_MS; return /*#__PURE__*/_react.default.createElement(_Transition.default, { timeout: _this.state.mounted ? animSpeedMs : 0, in: isVisible, mountOnEnter: true, unmountOnExit: true, onExiting: _this.focusEditor }, children); }); return _this; } (0, _inherits2.default)(SwappableContentAreaInner, _React$PureComponent); return (0, _createClass2.default)(SwappableContentAreaInner, [{ key: "unsetPluginContent", value: function unsetPluginContent() { this.setState({ currentPluginContent: undefined }); } }, { key: "componentDidMount", value: function componentDidMount() { // use this to trigger an animation this.setState({ mounted: true }); } }, { key: "render", value: function render() { var _this$props$customWid, _this2 = this; var width = (_this$props$customWid = this.props.customWidth) !== null && _this$props$customWid !== void 0 ? _this$props$customWid : _editorSharedStyles.akEditorContextPanelWidth; var userVisible = !!this.props.visible; var visible = userVisible || !!this.state.currentPluginContent; var hasPadding = this.props.hasPadding === undefined ? true : this.props.hasPadding; var disableAnimation = (0, _platformFeatureFlags.fg)('platform_editor_disable_context_panel_animation'); return /*#__PURE__*/_react.default.createElement(_contextPanel.ContextPanelConsumer, null, function (_ref) { var _this2$props$intl, _this2$props$intl2; var broadcastWidth = _ref.broadcastWidth; var contextPanelWidth = visible ? width : 0; broadcastWidth(contextPanelWidth); return /*#__PURE__*/_react.default.createElement(ContextPanelWrapperMigration, { customWidth: _this2.props.customWidth, visible: visible, disableAnimation: disableAnimation, "data-testid": "context-panel-panel" // eslint-disable-next-line @atlassian/a11y/no-empty-aria-label -- Pre-existing; intl should always resolve a label here , "aria-label": ((_this2$props$intl = _this2.props.intl) === null || _this2$props$intl === void 0 ? void 0 : _this2$props$intl.formatMessage(_messages.contextPanelMessages.panelLabel)) || '', "aria-modal": "false", role: "dialog" }, /*#__PURE__*/_react.default.createElement(ContextPanelContentMigration, { customWidth: _this2.props.customWidth, visible: visible, disableAnimation: disableAnimation, hasPadding: hasPadding, "data-testid": "context-panel-content" // Adding tabIndex=0 here to make content focusable as it is a scrollable region , tabIndex: (0, _platformFeatureFlags.fg)('platform_editor_nov_a11y_fixes') ? 0 : undefined, role: (0, _platformFeatureFlags.fg)('platform_editor_nov_a11y_fixes') ? 'region' : undefined, "aria-label": (0, _platformFeatureFlags.fg)('platform_editor_nov_a11y_fixes') ? (_this2$props$intl2 = _this2.props.intl) === null || _this2$props$intl2 === void 0 ? void 0 : _this2$props$intl2.formatMessage(_messages.contextPanelMessages.panelContentLabel) : undefined }, _this2.showPluginContent() || _this2.showProvidedContent(userVisible))); }); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { if (props.pluginContent !== state.currentPluginContent) { return _objectSpread(_objectSpread({}, state), {}, { currentPluginContent: props.pluginContent }); } return null; } }]); }(_react.default.PureComponent); // eslint-disable-next-line @typescript-eslint/ban-types var SwappableContentArea = exports.SwappableContentArea = (0, _reactIntl.injectIntl)(SwappableContentAreaInner); function ContextPanel(props) { var contextPanelContents = (0, _hooks.useSharedPluginStateWithSelector)(props.editorAPI, ['contextPanel'], function (states) { var _states$contextPanelS; return states === null || states === void 0 || (_states$contextPanelS = states.contextPanelState) === null || _states$contextPanelS === void 0 ? void 0 : _states$contextPanelS.contents; }); var firstContent = contextPanelContents && contextPanelContents.find(Boolean); return /*#__PURE__*/_react.default.createElement(SwappableContentArea // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading , (0, _extends2.default)({}, props, { editorAPI: props.editorAPI, pluginContent: firstContent })); }