UNPKG

@atlaskit/editor-common

Version:

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

192 lines (188 loc) • 7.38 kB
import _typeof from "@babel/runtime/helpers/typeof"; 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; })(); } /** * @jsxRuntime classic * @jsx jsx */ import { PureComponent } from 'react'; // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766 import { jsx } from '@emotion/react'; import { browser } from '../../utils'; import { panelTextInput, panelTextInputWithCustomWidth } from './styles'; var KeyZCode = 90; var KeyYCode = 89; // Ignored via go/ees005 // eslint-disable-next-line @repo/internal/react/no-class-components var PanelTextInput = /*#__PURE__*/function (_PureComponent) { function PanelTextInput(props) { var _this; _classCallCheck(this, PanelTextInput); _this = _callSuper(this, PanelTextInput, [props]); _defineProperty(_this, "onMouseDown", function () { var onMouseDown = _this.props.onMouseDown; if (onMouseDown) { onMouseDown(); } }); // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any _defineProperty(_this, "onBlur", function (e) { var onBlur = _this.props.onBlur; if (onBlur) { onBlur(e); } }); _defineProperty(_this, "handleChange", function () { var onChange = _this.props.onChange; if (_this.input) { _this.setState({ value: _this.input.value }); } if (onChange && _this.input) { onChange(_this.input.value); } }); // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any _defineProperty(_this, "handleKeydown", function (e) { var _this$props = _this.props, onUndo = _this$props.onUndo, onRedo = _this$props.onRedo, onSubmit = _this$props.onSubmit, onCancel = _this$props.onCancel; if (e.keyCode === 13 && onSubmit) { e.preventDefault(); // Prevent from submitting if an editor is inside a form. // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion onSubmit(_this.input.value); } else if (e.keyCode === 27 && onCancel) { onCancel(e); } else if (typeof onUndo === 'function' && _this.isUndoEvent(e)) { e.preventDefault(); onUndo(); } else if (typeof onRedo === 'function' && _this.isRedoEvent(e)) { e.preventDefault(); onRedo(); } if (_this.props.onKeyDown) { _this.props.onKeyDown(e); } }); _defineProperty(_this, "handleRef", function (input) { if (input instanceof HTMLInputElement) { _this.input = input; if (_this.props.autoFocus) { // Need this to prevent jumping when we render TextInput inside Portal @see ED-2992 _this.focusTimeoutId = window.setTimeout(function () { return _this.focus(); }); } } else { _this.input = undefined; } }); _this.state = { value: props.defaultValue || '' }; return _this; } _inherits(PanelTextInput, _PureComponent); return _createClass(PanelTextInput, [{ key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (prevProps.defaultValue !== this.props.defaultValue) { this.setState({ value: this.props.defaultValue }); } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.clearTimeout(this.focusTimeoutId); } }, { key: "render", value: function render() { var _this$props3, _this$props4; var _this$props2 = this.props, placeholder = _this$props2.placeholder, width = _this$props2.width, maxLength = _this$props2.maxLength, testId = _this$props2.testId, ariaLabel = _this$props2.ariaLabel, describedById = _this$props2.describedById, ariaActiveDescendant = _this$props2.ariaActiveDescendant, ariaControls = _this$props2.ariaControls, ariaExpanded = _this$props2.ariaExpanded, ariaAutoComplete = _this$props2.ariaAutoComplete, role = _this$props2.role, inputId = _this$props2.inputId; var value = this.state.value; return jsx("input", { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766 css: [panelTextInput, width !== undefined && panelTextInputWithCustomWidth(width)], role: role, "aria-autocomplete": ariaAutoComplete ? 'list' : undefined, "aria-expanded": ariaExpanded, "aria-controls": ariaControls, "aria-activedescendant": ariaActiveDescendant, "aria-describedby": describedById, "data-testid": testId || '', type: "text", placeholder: placeholder, value: value, onChange: this.handleChange, onKeyDown: this.handleKeydown, onMouseDown: this.onMouseDown, onBlur: this.onBlur, ref: this.handleRef, maxLength: maxLength, "aria-label": ariaLabel, "aria-required": (_this$props3 = this.props) === null || _this$props3 === void 0 ? void 0 : _this$props3.ariaRequired, "aria-invalid": (_this$props4 = this.props) === null || _this$props4 === void 0 ? void 0 : _this$props4.ariaInvalid, id: inputId }); } }, { key: "focus", value: function focus() { var input = this.input; if (input) { var focusOpts = _typeof(this.props.autoFocus) === 'object' ? this.props.autoFocus : {}; input.focus(focusOpts); } } }, { key: "isUndoEvent", value: // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any function isUndoEvent(event) { return event.keyCode === KeyZCode && ( // cmd + z for mac browser.mac && event.metaKey && !event.shiftKey || // ctrl + z for non-mac !browser.mac && event.ctrlKey); } // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any }, { key: "isRedoEvent", value: function isRedoEvent(event) { return ( // ctrl + y for non-mac !browser.mac && event.ctrlKey && event.keyCode === KeyYCode || browser.mac && event.metaKey && event.shiftKey && event.keyCode === KeyZCode || event.ctrlKey && event.shiftKey && event.keyCode === KeyZCode ); } }]); }(PureComponent); export { PanelTextInput as default };