@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
151 lines (150 loc) • 7.04 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
/* 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';
const ANIM_SPEED_MS = 500;
const ContextPanelWrapperMigration = componentWithCondition(() => expValEquals('platform_editor_core_non_ecc_static_css', 'isEnabled', true), ContextPanelWrapperCompiled, ContextPanelWrapperEmotion);
const ContextPanelContentMigration = componentWithCondition(() => 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
class SwappableContentAreaInner extends React.PureComponent {
constructor(...args) {
super(...args);
_defineProperty(this, "state", {
mounted: false,
currentPluginContent: undefined
});
_defineProperty(this, "handleTransitionExited", () => {
this.unsetPluginContent();
});
_defineProperty(this, "focusEditor", () => {
var _editorAPI$core;
const {
editorAPI
} = this.props;
editorAPI === null || editorAPI === void 0 ? void 0 : (_editorAPI$core = editorAPI.core) === null || _editorAPI$core === void 0 ? void 0 : _editorAPI$core.actions.focus({
scrollIntoView: false
});
});
_defineProperty(this, "showPluginContent", () => {
const {
pluginContent
} = this.props;
const {
currentPluginContent
} = this.state;
if (!currentPluginContent) {
return;
}
const animSpeedMs = fg('platform_editor_disable_context_panel_animation') ? 0 : ANIM_SPEED_MS;
const onExited = expValEquals('platform_editor_perf_lint_cleanup', 'isEnabled', true) ? this.handleTransitionExited : () => 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", isVisible => {
const {
children
} = this.props;
if (!children) {
return;
}
const 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);
});
}
static getDerivedStateFromProps(props, state) {
if (props.pluginContent !== state.currentPluginContent) {
return {
...state,
currentPluginContent: props.pluginContent
};
}
return null;
}
unsetPluginContent() {
this.setState({
currentPluginContent: undefined
});
}
componentDidMount() {
// use this to trigger an animation
this.setState({
mounted: true
});
}
render() {
var _this$props$customWid;
const width = (_this$props$customWid = this.props.customWidth) !== null && _this$props$customWid !== void 0 ? _this$props$customWid : akEditorContextPanelWidth;
const userVisible = !!this.props.visible;
const visible = userVisible || !!this.state.currentPluginContent;
const hasPadding = this.props.hasPadding === undefined ? true : this.props.hasPadding;
const disableAnimation = fg('platform_editor_disable_context_panel_animation');
return /*#__PURE__*/React.createElement(ContextPanelConsumer, null, ({
broadcastWidth
}) => {
var _this$props$intl, _this$props$intl2;
const contextPanelWidth = visible ? width : 0;
broadcastWidth(contextPanelWidth);
return /*#__PURE__*/React.createElement(ContextPanelWrapperMigration, {
customWidth: this.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": ((_this$props$intl = this.props.intl) === null || _this$props$intl === void 0 ? void 0 : _this$props$intl.formatMessage(contextPanelMessages.panelLabel)) || '',
"aria-modal": "false",
role: "dialog"
}, /*#__PURE__*/React.createElement(ContextPanelContentMigration, {
customWidth: this.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') ? (_this$props$intl2 = this.props.intl) === null || _this$props$intl2 === void 0 ? void 0 : _this$props$intl2.formatMessage(contextPanelMessages.panelContentLabel) : undefined
}, this.showPluginContent() || this.showProvidedContent(userVisible)));
});
}
}
// eslint-disable-next-line @typescript-eslint/ban-types
export const SwappableContentArea = injectIntl(SwappableContentAreaInner);
export function ContextPanel(props) {
const contextPanelContents = useSharedPluginStateWithSelector(props.editorAPI, ['contextPanel'], states => {
var _states$contextPanelS;
return states === null || states === void 0 ? void 0 : (_states$contextPanelS = states.contextPanelState) === null || _states$contextPanelS === void 0 ? void 0 : _states$contextPanelS.contents;
});
const 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
}));
}