UNPKG

@atlaskit/editor-plugin-status

Version:

Status plugin for @atlaskit/editor-core

368 lines (366 loc) 16.2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.closingMethods = exports.StatusPickerWithoutAnalytcs = exports.InputMethod = void 0; 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 _react2 = require("@emotion/react"); var _reactIntl = require("react-intl"); var _analyticsNext = require("@atlaskit/analytics-next"); var _browserApis = require("@atlaskit/browser-apis"); var _messages = require("@atlaskit/editor-common/messages"); var _ui = require("@atlaskit/editor-common/ui"); var _uiReact = require("@atlaskit/editor-common/ui-react"); var _userIntent = require("@atlaskit/editor-common/user-intent"); var _editorSharedStyles = require("@atlaskit/editor-shared-styles"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _picker = require("@atlaskit/status/picker"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _visuallyHidden = _interopRequireDefault(require("@atlaskit/visually-hidden")); var _actions = require("../pm-plugins/actions"); var _analytics = require("./analytics"); 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; })(); } /** * @jsxRuntime classic * @jsx jsx */ // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766 var PopupWithListeners = (0, _uiReact.withReactEditorViewOuterListeners)(_ui.Popup); var InputMethod = exports.InputMethod = /*#__PURE__*/function (InputMethod) { InputMethod["blur"] = "blur"; InputMethod["escKey"] = "escKey"; InputMethod["enterKey"] = "enterKey"; return InputMethod; }({}); var closingMethods = exports.closingMethods = /*#__PURE__*/function (closingMethods) { closingMethods["ArrowLeft"] = "arrowLeft"; closingMethods["ArrowRight"] = "arrowRight"; return closingMethods; }({}); var pickerContainerStyles = (0, _react2.css)({ background: "var(--ds-surface-overlay, #FFFFFF)", padding: "var(--ds-space-100, 8px)".concat(" 0"), borderRadius: "var(--ds-radius-small, 3px)", boxShadow: "var(--ds-shadow-overlay, 0px 8px 12px #1E1F2126, 0px 0px 1px #1E1F214f)", // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-selectors -- Ignored via go/DSP-18766 ':focus': { outline: 'none' }, // eslint-disable-next-line @atlaskit/design-system/no-nested-styles, @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 input: { // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography textTransform: 'uppercase' } }); var pickerContainerStylesTeam26 = (0, _react2.css)({ background: "var(--ds-surface-overlay, #FFFFFF)", padding: "var(--ds-space-100, 8px)".concat(" 0"), borderRadius: "var(--ds-radius-small, 3px)", boxShadow: "var(--ds-shadow-overlay, 0 0 1px rgba(9, 30, 66, 0.31), 0 4px 8px -2px rgba(9, 30, 66, 0.25))", // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-selectors ':focus': { outline: 'none' } }); // eslint-disable-next-line @repo/internal/react/no-class-components var StatusPickerWithIntl = /*#__PURE__*/function (_React$Component) { function StatusPickerWithIntl(props) { var _this; (0, _classCallCheck2.default)(this, StatusPickerWithIntl); _this = _callSuper(this, StatusPickerWithIntl, [props]); (0, _defineProperty2.default)(_this, "handleClickOutside", function (event) { event.preventDefault(); _this.inputMethod = InputMethod.blur; var selectedText = window.getSelection(); if (!selectedText) { _this.props.closeStatusPicker(); } }); (0, _defineProperty2.default)(_this, "handleEscapeKeydown", function (event) { event.preventDefault(); _this.inputMethod = InputMethod.escKey; _this.props.onEnter(_this.state); }); (0, _defineProperty2.default)(_this, "handleTabPress", function (event) { var _getDocument, _document; var colorButtons = event.currentTarget.querySelectorAll('button'); var inputField = event.currentTarget.querySelector('input'); var activeElement = (0, _expValEquals.expValEquals)('platform_editor_a11y_eslint_fix', 'isEnabled', true) ? (_getDocument = (0, _browserApis.getDocument)()) === null || _getDocument === void 0 ? void 0 : _getDocument.activeElement : (_document = document) === null || _document === void 0 ? void 0 : _document.activeElement; var isInputFocussed = activeElement === inputField; var isButtonFocussed = Array.from(colorButtons).some(function (buttonElement) { return activeElement === buttonElement; }); if (event !== null && event !== void 0 && event.shiftKey) { /* shift + tab */ if (isInputFocussed) { colorButtons[0].focus(); event.preventDefault(); } /* After the user presses shift + tab the color-palette component updates tab index for the first color to be 0. To correctly set focus to the input field instead of the first color button we need to set focus manually */ if (isButtonFocussed) { inputField === null || inputField === void 0 || inputField.focus(); event.preventDefault(); } } else { /* tab */ if (isButtonFocussed) { inputField === null || inputField === void 0 || inputField.focus(); event.preventDefault(); } } }); (0, _defineProperty2.default)(_this, "handleArrow", function (event, closingMethod) { var _getDocument2, _document2; var activeElement = (0, _expValEquals.expValEquals)('platform_editor_a11y_eslint_fix', 'isEnabled', true) ? (_getDocument2 = (0, _browserApis.getDocument)()) === null || _getDocument2 === void 0 ? void 0 : _getDocument2.activeElement : (_document2 = document) === null || _document2 === void 0 ? void 0 : _document2.activeElement; if (activeElement === _this.popupBodyWrapper.current) { var _this$popupBodyWrappe; event.preventDefault(); (_this$popupBodyWrappe = _this.popupBodyWrapper) === null || _this$popupBodyWrappe === void 0 || (_this$popupBodyWrappe = _this$popupBodyWrappe.current) === null || _this$popupBodyWrappe === void 0 || _this$popupBodyWrappe.blur(); _this.props.closeStatusPicker({ closingMethod: closingMethod }); } }); (0, _defineProperty2.default)(_this, "onKeyDown", function (event) { var isTabPressed = event.key === 'Tab'; if (isTabPressed) { return _this.handleTabPress(event); } if (event.key in closingMethods) { return _this.handleArrow(event, closingMethods[event.key]); } }); (0, _defineProperty2.default)(_this, "onColorHover", function (color) { _this.createStatusAnalyticsAndFireFunc({ action: 'hovered', actionSubject: 'statusColorPicker', attributes: { color: color, localId: _this.state.localId, state: (0, _analytics.analyticsState)(_this.props.isNew) } }); }); (0, _defineProperty2.default)(_this, "onColorClick", function (color) { var _this$state = _this.state, text = _this$state.text, localId = _this$state.localId; if (color === _this.state.color) { _this.createStatusAnalyticsAndFireFunc({ action: 'clicked', actionSubject: 'statusColorPicker', attributes: { color: color, localId: localId, state: (0, _analytics.analyticsState)(_this.props.isNew) } }); // closes status box and commits colour _this.onEnter(); } else { _this.setState({ color: color }); _this.props.onSelect({ text: text, color: color, localId: localId }); } }); (0, _defineProperty2.default)(_this, "onTextChanged", function (text) { var _this$state2 = _this.state, color = _this$state2.color, localId = _this$state2.localId; _this.setState({ text: text }); _this.props.onTextChanged({ text: text, color: color, localId: localId }, !!_this.props.isNew); }); (0, _defineProperty2.default)(_this, "onEnter", function () { _this.inputMethod = InputMethod.enterKey; _this.props.onEnter(_this.state); }); // cancel bubbling to fix clickOutside logic: // popup re-renders its content before the click event bubbles up to the document // therefore click target element would be different from the popup content (0, _defineProperty2.default)(_this, "handlePopupClick", function (event) { return event.nativeEvent.stopImmediatePropagation(); }); _this.state = _this.extractStateFromProps(props); _this.createStatusAnalyticsAndFireFunc = (0, _analytics.createStatusAnalyticsAndFire)(props.createAnalyticsEvent); _this.popupBodyWrapper = /*#__PURE__*/_react.default.createRef(); return _this; } (0, _inherits2.default)(StatusPickerWithIntl, _React$Component); return (0, _createClass2.default)(StatusPickerWithIntl, [{ key: "fireStatusPopupOpenedAnalytics", value: function fireStatusPopupOpenedAnalytics(state) { var color = state.color, text = state.text, localId = state.localId, isNew = state.isNew; this.startTime = Date.now(); this.createStatusAnalyticsAndFireFunc({ action: 'opened', actionSubject: 'statusPopup', attributes: { textLength: text ? text.length : 0, selectedColor: color, localId: localId, state: (0, _analytics.analyticsState)(isNew) } }); } }, { key: "fireStatusPopupClosedAnalytics", value: function fireStatusPopupClosedAnalytics(state) { var color = state.color, text = state.text, localId = state.localId, isNew = state.isNew; this.createStatusAnalyticsAndFireFunc({ action: 'closed', actionSubject: 'statusPopup', attributes: { inputMethod: this.inputMethod, duration: Date.now() - this.startTime, textLength: text ? text.length : 0, selectedColor: color, localId: localId, state: (0, _analytics.analyticsState)(isNew) } }); } }, { key: "reset", value: function reset() { this.startTime = Date.now(); this.inputMethod = InputMethod.blur; } }, { key: "componentDidMount", value: function componentDidMount() { this.reset(); this.fireStatusPopupOpenedAnalytics(this.state); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.focusTimeout && cancelAnimationFrame(this.focusTimeout); this.fireStatusPopupClosedAnalytics(this.state); this.startTime = 0; } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps, prevState) { var element = this.props.target; if (prevProps.target !== element) { var newState = this.extractStateFromProps(this.props); this.setState(newState); this.fireStatusPopupClosedAnalytics(prevState); this.reset(); this.fireStatusPopupOpenedAnalytics(newState); } } }, { key: "extractStateFromProps", value: function extractStateFromProps(props) { var defaultColor = props.defaultColor, defaultText = props.defaultText, defaultLocalId = props.defaultLocalId, isNew = props.isNew; return { color: defaultColor || _actions.DEFAULT_STATUS.color, text: defaultText || _actions.DEFAULT_STATUS.text, localId: defaultLocalId, isNew: isNew }; } }, { key: "setRef", value: function setRef(setOutsideClickTargetRef) { var _this2 = this; return function (ref) { setOutsideClickTargetRef(ref); _this2.popupBodyWrapper.current = ref; }; } }, { key: "renderWithSetOutsideClickTargetRef", value: function renderWithSetOutsideClickTargetRef(setOutsideClickTargetRef) { var _this$props = this.props, isNew = _this$props.isNew, focusStatusInput = _this$props.focusStatusInput, api = _this$props.api; var _this$state3 = this.state, color = _this$state3.color, text = _this$state3.text; return (0, _react2.jsx)(_userIntent.UserIntentPopupWrapper, { api: api, userIntent: "statusPickerOpen" }, (0, _react2.jsx)("div", { css: (0, _platformFeatureFlags.fg)('platform-dst-lozenge-tag-badge-visual-uplifts') ? pickerContainerStylesTeam26 : pickerContainerStyles, role: "none", ref: this.setRef(setOutsideClickTargetRef), onClick: this.handlePopupClick, onKeyDown: this.onKeyDown }, (0, _react2.jsx)(_picker.StatusPicker, { autoFocus: isNew || focusStatusInput, selectedColor: color, text: text, onColorClick: this.onColorClick, onColorHover: this.onColorHover, onTextChanged: this.onTextChanged, onEnter: this.onEnter }))); } }, { key: "render", value: function render() { var _this$props2 = this.props, target = _this$props2.target, mountTo = _this$props2.mountTo, boundariesElement = _this$props2.boundariesElement, scrollableElement = _this$props2.scrollableElement, editorView = _this$props2.editorView, intl = _this$props2.intl; if (!(editorView !== null && editorView !== void 0 && editorView.editable)) { return null; } return target && (0, _react2.jsx)(PopupWithListeners, { ariaLabel: (0, _platformFeatureFlags.fg)('_editor_a11y_aria_label_removal_popup') ? intl.formatMessage(_messages.statusMessages.statusEditorLabel) : undefined, target: target // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , offset: [0, 8], handleClickOutside: this.handleClickOutside, handleEscapeKeydown: this.handleEscapeKeydown, zIndex: _editorSharedStyles.akEditorFloatingDialogZIndex, fitHeight: 40, mountTo: mountTo, boundariesElement: boundariesElement, scrollableElement: scrollableElement, closeOnTab: false }, (0, _react2.jsx)(_visuallyHidden.default, { "aria-atomic": true, role: "alert" }, intl.formatMessage(_messages.statusMessages.statusPickerOpenedAlert)), (0, _react2.jsx)(_uiReact.OutsideClickTargetRefContext.Consumer, null, this.renderWithSetOutsideClickTargetRef.bind(this))); } }]); }(_react.default.Component); // eslint-disable-next-line @typescript-eslint/ban-types var StatusPickerWithoutAnalytcs = exports.StatusPickerWithoutAnalytcs = (0, _reactIntl.injectIntl)(StatusPickerWithIntl); var _default_1 = (0, _analyticsNext.withAnalyticsEvents)()(StatusPickerWithoutAnalytcs); var _default = exports.default = _default_1;