UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

171 lines • 9.44 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 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) { _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; } 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; })(); } /* eslint-disable jsdoc/require-jsdoc -- Pre-existing lint debt surfaced by this mechanical type-import-only PR. */ import React from 'react'; import { injectIntl } from 'react-intl'; import Transition from 'react-transition-group/Transition'; import { ContextPanelConsumer } from '@atlaskit/editor-common/context-panel'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks'; import { contextPanelMessages } from '@atlaskit/editor-common/messages'; import { akEditorContextPanelWidth } from '@atlaskit/editor-shared-styles'; import { fg } from '@atlaskit/platform-feature-flags'; import { componentWithCondition } from '@atlaskit/platform-feature-flags-react'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { ContextPanelContentCompiled, ContextPanelWrapperCompiled } from './index-compiled'; import { ContextPanelContentEmotion, ContextPanelWrapperEmotion } from './index-emotion'; var ANIM_SPEED_MS = 500; var ContextPanelWrapperMigration = componentWithCondition(function () { return expValEquals('platform_editor_core_non_ecc_static_css', 'isEnabled', true); }, ContextPanelWrapperCompiled, ContextPanelWrapperEmotion); var ContextPanelContentMigration = componentWithCondition(function () { return expValEquals('platform_editor_core_non_ecc_static_css', 'isEnabled', true); }, ContextPanelContentCompiled, 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; _classCallCheck(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)); _defineProperty(_this, "state", { mounted: false, currentPluginContent: undefined }); _defineProperty(_this, "handleTransitionExited", function () { _this.unsetPluginContent(); }); _defineProperty(_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 }); }); _defineProperty(_this, "showPluginContent", function () { var pluginContent = _this.props.pluginContent; var currentPluginContent = _this.state.currentPluginContent; if (!currentPluginContent) { return; } var animSpeedMs = fg('platform_editor_disable_context_panel_animation') ? 0 : ANIM_SPEED_MS; var onExited = expValEquals('platform_editor_perf_lint_cleanup', 'isEnabled', true) ? _this.handleTransitionExited : function () { return _this.unsetPluginContent(); }; return /*#__PURE__*/React.createElement(Transition, { timeout: _this.state.mounted ? animSpeedMs : 0, in: !!pluginContent, mountOnEnter: true, unmountOnExit: true, onExited: onExited }, currentPluginContent); }); _defineProperty(_this, "showProvidedContent", function (isVisible) { var children = _this.props.children; if (!children) { return; } var animSpeedMs = fg('platform_editor_disable_context_panel_animation') ? 0 : ANIM_SPEED_MS; return /*#__PURE__*/React.createElement(Transition, { timeout: _this.state.mounted ? animSpeedMs : 0, in: isVisible, mountOnEnter: true, unmountOnExit: true, onExiting: _this.focusEditor }, children); }); return _this; } _inherits(SwappableContentAreaInner, _React$PureComponent); return _createClass(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 : akEditorContextPanelWidth; var userVisible = !!this.props.visible; var visible = userVisible || !!this.state.currentPluginContent; var hasPadding = this.props.hasPadding === undefined ? true : this.props.hasPadding; var disableAnimation = fg('platform_editor_disable_context_panel_animation'); return /*#__PURE__*/React.createElement(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.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(contextPanelMessages.panelLabel)) || '', "aria-modal": "false", role: "dialog" }, /*#__PURE__*/React.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: fg('platform_editor_nov_a11y_fixes') ? 0 : undefined, role: fg('platform_editor_nov_a11y_fixes') ? 'region' : undefined, "aria-label": fg('platform_editor_nov_a11y_fixes') ? (_this2$props$intl2 = _this2.props.intl) === null || _this2$props$intl2 === void 0 ? void 0 : _this2$props$intl2.formatMessage(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.PureComponent); // eslint-disable-next-line @typescript-eslint/ban-types export var SwappableContentArea = injectIntl(SwappableContentAreaInner); export function ContextPanel(props) { var contextPanelContents = 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.createElement(SwappableContentArea // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading , _extends({}, props, { editorAPI: props.editorAPI, pluginContent: firstContent })); }