@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
141 lines (135 loc) • 8.74 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FullPageToolbar = exports.EditorToolbar = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@emotion/react");
var _reactIntl = require("react-intl");
var _contextPanel = require("@atlaskit/editor-common/context-panel");
var _messages = require("@atlaskit/editor-common/messages");
var _uiMenu = require("@atlaskit/editor-common/ui-menu");
var _experiments = require("@atlaskit/tmp-editor-statsig/experiments");
var _ToolbarPortal = require("../../Toolbar/ToolbarPortal");
var _ToolbarWithSizeDetector = require("../../Toolbar/ToolbarWithSizeDetector");
var _BeforeWrapper = require("./BeforeWrapper");
var _CustomToolbarWrapper = require("./CustomToolbarWrapper");
var _MainToolbar = require("./MainToolbar");
var _MainToolbarWrapper = require("./MainToolbarWrapper");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
/**
* @jsxRuntime classic
* @jsx jsx
*/
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled, @typescript-eslint/consistent-type-imports -- Ignored via go/DSP-18766; jsx required at runtime for @jsxRuntime classic
var EditorToolbar = exports.EditorToolbar = /*#__PURE__*/_react.default.memo(function (props) {
var _props$primaryToolbar, _useToolbarPortal, _props$customPrimaryT;
var _useState = (0, _react.useState)(false),
_useState2 = (0, _slicedToArray2.default)(_useState, 2),
shouldSplitToolbar = _useState2[0],
setShouldSplitToolbar = _useState2[1];
var editorAPI = props.editorAPI;
// When primary toolbar components is undefined, do not show two line editor toolbar
var twoLineEditorToolbar = (0, _experiments.editorExperiment)('platform_editor_controls', 'variant1') ? !!props.customPrimaryToolbarComponents && ((_props$primaryToolbar = props.primaryToolbarComponents) === null || _props$primaryToolbar === void 0 ? void 0 : _props$primaryToolbar.length) !== 0 : !!props.customPrimaryToolbarComponents;
// When a toolbar portal context is provided, render the toolbar inside a portal.
// Otherwise fall back to a fragment just to avoid forking rendering logic.
var _ref = (_useToolbarPortal = (0, _ToolbarPortal.useToolbarPortal)()) !== null && _useToolbarPortal !== void 0 ? _useToolbarPortal : {
Portal: _react.default.Fragment
},
ToolbarPortal = _ref.Portal;
var hasToolbarPortal = ToolbarPortal !== _react.default.Fragment;
var popupsMountPoint = hasToolbarPortal ? undefined : props.popupsMountPoint;
var nonCustomToolbar =
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766
(0, _react2.jsx)("div", {
css: _MainToolbar.nonCustomToolbarWrapperStyle
}, props.beforeIcon && (0, _react2.jsx)("div", {
css: _MainToolbar.mainToolbarIconBeforeStyle
}, props.beforeIcon), (0, _react2.jsx)(_ToolbarWithSizeDetector.ToolbarWithSizeDetector, {
editorView: props.editorView,
editorActions: props.editorActions,
eventDispatcher: props.eventDispatcher,
providerFactory: props.providerFactory,
appearance: props.appearance,
items: props.primaryToolbarComponents,
popupsMountPoint: popupsMountPoint,
popupsBoundariesElement: props.popupsBoundariesElement,
popupsScrollableElement: props.popupsScrollableElement,
disabled: props.disabled,
dispatchAnalyticsEvent: props.dispatchAnalyticsEvent,
containerElement: props.containerElement,
hasMinWidth: props.hasMinWidth,
twoLineEditorToolbar: twoLineEditorToolbar
}));
var customToolbar =
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766
(0, _react2.jsx)("div", {
css: _MainToolbar.customToolbarWrapperStyle
}, !!props.customPrimaryToolbarComponents && 'before' in props.customPrimaryToolbarComponents ? (0, _react2.jsx)(_BeforeWrapper.BeforePrimaryToolbarWrapper, {
beforePrimaryToolbarComponents: (_props$customPrimaryT = props.customPrimaryToolbarComponents) === null || _props$customPrimaryT === void 0 ? void 0 : _props$customPrimaryT.before
}) : null, !(0, _experiments.editorExperiment)('platform_editor_controls', 'variant1') && editorAPI !== null && editorAPI !== void 0 && editorAPI.findReplace && twoLineEditorToolbar ? editorAPI === null || editorAPI === void 0 ? void 0 : editorAPI.findReplace.actions.registerToolbarButton({
popupsBoundariesElement: props.popupsBoundariesElement,
popupsMountPoint: popupsMountPoint,
popupsScrollableElement: props.popupsScrollableElement,
editorView: props.editorView,
containerElement: props.containerElement,
dispatchAnalyticsEvent: props.dispatchAnalyticsEvent
}) : null, !!props.customPrimaryToolbarComponents && 'after' in props.customPrimaryToolbarComponents ? props.customPrimaryToolbarComponents.after : props.customPrimaryToolbarComponents);
(0, _react.useEffect)(function () {
if (twoLineEditorToolbar) {
var updateOnResize = function updateOnResize() {
setShouldSplitToolbar(window.innerWidth <= _MainToolbar.MAXIMUM_TWO_LINE_TOOLBAR_BREAKPOINT);
};
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
window.addEventListener('resize', updateOnResize);
updateOnResize();
// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/dom-events/no-unsafe-event-listeners
return function () {
return window.removeEventListener('resize', updateOnResize);
};
}
});
var isShortcutToFocusToolbar = (0, _react.useCallback)(function (event) {
//Alt + F9 to reach first element in this main toolbar
return event.altKey && (event.key === 'F9' || event.keyCode === 120);
}, []);
var handleEscape = (0, _react.useCallback)(function (event) {
var _props$editorView;
if (!((_props$editorView = props.editorView) !== null && _props$editorView !== void 0 && _props$editorView.hasFocus())) {
var _props$editorView2;
(_props$editorView2 = props.editorView) === null || _props$editorView2 === void 0 || _props$editorView2.focus();
}
event.preventDefault();
event.stopPropagation();
}, [props.editorView]);
return (0, _react2.jsx)(_contextPanel.ContextPanelConsumer, null, function (_ref2) {
var contextPanelWidth = _ref2.width;
return (0, _react2.jsx)(_uiMenu.ToolbarArrowKeyNavigationProvider, {
editorView: props.editorView,
childComponentSelector: "[data-testid='ak-editor-main-toolbar']",
isShortcutToFocusToolbar: isShortcutToFocusToolbar,
handleEscape: handleEscape,
intl: props.intl
}, (0, _react2.jsx)(ToolbarPortal, null, (0, _react2.jsx)(_MainToolbarWrapper.MainToolbarWrapper, {
showKeyline: props.showKeyline || contextPanelWidth > 0,
twoLineEditorToolbar: twoLineEditorToolbar,
"data-testid": "ak-editor-main-toolbar"
}, (0, _react2.jsx)(_CustomToolbarWrapper.MainToolbarForFirstChildWrapper, {
twoLineEditorToolbar: twoLineEditorToolbar,
role: "toolbar",
"aria-label": props.intl.formatMessage(_messages.fullPageMessages.toolbarLabel)
}, shouldSplitToolbar ? customToolbar : nonCustomToolbar), (0, _react2.jsx)(_CustomToolbarWrapper.MainToolbarForSecondChildWrapper, {
twoLineEditorToolbar: twoLineEditorToolbar,
"data-testid": "avatar-group-outside-plugin",
role: "region",
"aria-label": props.intl.formatMessage(_messages.fullPageMessages.pageActionsLabel)
}, shouldSplitToolbar ? nonCustomToolbar : customToolbar), (0, _react2.jsx)(_ToolbarPortal.ToolbarPortalMountPoint, null))));
});
});
// eslint-disable-next-line @typescript-eslint/ban-types
var FullPageToolbar = exports.FullPageToolbar = (0, _reactIntl.injectIntl)(EditorToolbar);