@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
156 lines (154 loc) • 9.05 kB
JavaScript
/* FullPageToolbarNext.tsx generated by @compiled/babel-plugin v0.39.1 */
import "./FullPageToolbarNext.compiled.css";
import { ax, ix } from "@compiled/react/runtime";
import React, { useCallback, useMemo } from 'react';
import { useIntl } from 'react-intl';
import { ContextPanelConsumer } from '@atlaskit/editor-common/context-panel';
import { isSSR } from '@atlaskit/editor-common/core-utils';
import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
import { shouldShowPrimaryToolbar, TOOLBARS, VIEW_MODE_TOGGLE_SECTION } from '@atlaskit/editor-common/toolbar';
import { ToolbarArrowKeyNavigationProvider } from '@atlaskit/editor-common/ui-menu';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { isToolbar } from '../../../utils/toolbar';
import ExcludeFromHydration from '../../ExcludeFromHydration';
import { ToolbarNext } from '../../Toolbar/Toolbar';
import { ToolbarPortalMountPoint, useToolbarPortal } from '../../Toolbar/ToolbarPortal';
const styles = {
mainToolbarIconBefore: "_19pkpxbi _2hwxpxbi _otyrpxbi",
mainToolbarIconBeforeNew: "_4cvr1h6o _1e0c1txw",
mainToolbarWrapper: "_1rjc12x7 _18zrutpp _n7zlia51",
mainToolbarNew: "_1e0c1txw _4t3izwfg",
mainToolbarZIndex: "_1pby16oo _kqswh2mm",
mainToolbarWithKeyline: "_16qs1nax",
customToolbarWrapperStyle: "_4cvr1h6o _1e0c1txw",
firstChildWrapperOneLine: "_1e0c1txw _16jlkb7n",
firstChildWrapperContainerContext: "_7cca15wb _1f38ptqj",
secondChildWrapperOneLine: "_1e0c1txw _1ul91ns9 _4cvr1h6o",
beforePrimaryToolbarComponents: "_1e0c1txw _16jlkb7n _1bahesu3 _4cvr1h6o",
backgroundColor: "_bfhkvuon"
};
const MainToolbarWrapper = ({
children,
testId,
showKeyline
}) => {
return /*#__PURE__*/React.createElement("div", {
"data-testid": testId,
className: ax([styles.mainToolbarWrapper, showKeyline && styles.mainToolbarWithKeyline, styles.mainToolbarZIndex, styles.mainToolbarNew, expValEquals('platform_editor_table_sticky_header_improvements', 'cohort', 'test_with_overflow') && expValEquals('platform_editor_table_sticky_header_patch_9', 'isEnabled', true) && styles.backgroundColor])
}, children);
};
const FirstChildWrapper = ({
children
}) => {
return /*#__PURE__*/React.createElement("div", {
"data-testid": "main-toolbar-first-child-wrapper",
className: ax([styles.firstChildWrapperOneLine, styles.firstChildWrapperContainerContext])
}, children);
};
const SecondChildWrapper = ({
children
}) => {
return /*#__PURE__*/React.createElement("div", {
"data-testid": "main-toolbar-second-child-wrapper",
className: ax([styles.secondChildWrapperOneLine])
}, children);
};
const shouldShowToolbarContainer = (toolbar, customPrimaryToolbarComponents) => {
return !!toolbar || !!customPrimaryToolbarComponents;
};
const getToolbarComponentsForMarkdownView = (components, markdownModeView) => {
if ((markdownModeView === 'syntax' || markdownModeView === 'preview') && fg('platform_editor_markdown_mode_hide_source_toolbar')) {
return components.filter(component => component.key === TOOLBARS.PRIMARY_TOOLBAR || component.key === VIEW_MODE_TOGGLE_SECTION.key);
}
return components;
};
export const FullPageToolbarNext = ({
editorAPI,
beforeIcon,
toolbarDockingPosition,
editorView,
popupsMountPoint,
showKeyline,
customPrimaryToolbarComponents,
disabled
}) => {
var _editorAPI$toolbar, _editorAPI$toolbar2, _useToolbarPortal;
const components = editorAPI === null || editorAPI === void 0 ? void 0 : (_editorAPI$toolbar = editorAPI.toolbar) === null || _editorAPI$toolbar === void 0 ? void 0 : _editorAPI$toolbar.actions.getComponents();
const runtimeOverride = useSharedPluginStateWithSelector(editorAPI, ['toolbar'], states => {
var _states$toolbarState;
return (_states$toolbarState = states.toolbarState) === null || _states$toolbarState === void 0 ? void 0 : _states$toolbarState.contextualFormattingModeOverride;
});
const effectiveRuntimeOverride = runtimeOverride !== undefined && fg('platform_editor_toolbar_mode_override') ? runtimeOverride : undefined;
const contextualFormattingEnabled = effectiveRuntimeOverride !== null && effectiveRuntimeOverride !== void 0 ? effectiveRuntimeOverride : editorAPI === null || editorAPI === void 0 ? void 0 : (_editorAPI$toolbar2 = editorAPI.toolbar) === null || _editorAPI$toolbar2 === void 0 ? void 0 : _editorAPI$toolbar2.actions.contextualFormattingMode();
const markdownModeView = useSharedPluginStateWithSelector(editorAPI, ['markdownMode'], states => {
var _states$markdownModeS;
return (_states$markdownModeS = states.markdownModeState) === null || _states$markdownModeS === void 0 ? void 0 : _states$markdownModeS.view;
});
const intl = useIntl();
const toolbar = components === null || components === void 0 ? void 0 : components.find(component => component.key === TOOLBARS.PRIMARY_TOOLBAR);
const visibleToolbarComponents = useMemo(() => components ? getToolbarComponentsForMarkdownView(components, markdownModeView) : undefined, [components, markdownModeView]);
const primaryToolbarDockingConfigEnabled = shouldShowPrimaryToolbar(contextualFormattingEnabled, toolbarDockingPosition);
// 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.
const {
Portal: ToolbarPortal
} = (_useToolbarPortal = useToolbarPortal()) !== null && _useToolbarPortal !== void 0 ? _useToolbarPortal : {
Portal: React.Fragment
};
const hasToolbarPortal = ToolbarPortal !== React.Fragment;
const mountPoint = hasToolbarPortal ? undefined : popupsMountPoint;
const isShortcutToFocusToolbar = useCallback(event => {
//Alt + F9 to reach first element in this main toolbar
return event.altKey && event.key === 'F9';
}, []);
const handleEscape = useCallback(event => {
if (!(editorView !== null && editorView !== void 0 && editorView.hasFocus())) {
editorView === null || editorView === void 0 ? void 0 : editorView.focus();
}
event.preventDefault();
event.stopPropagation();
}, [editorView]);
// Remove entire primary toolbar region if:
// - primary toolbar isn't registered
// - no custom primary toolbar components to render
// note: primary toolbar must render if toolbar docking preference is set to "controlled" to avoid SSR conflicts
if (!shouldShowToolbarContainer(toolbar, customPrimaryToolbarComponents)) {
return /*#__PURE__*/React.createElement(ToolbarPortal, null, null);
}
return /*#__PURE__*/React.createElement(ContextPanelConsumer, null, ({
width: ContextPanelWidth
}) => /*#__PURE__*/React.createElement(ToolbarArrowKeyNavigationProvider, {
editorView: editorView,
childComponentSelector: "[data-testid='ak-editor-main-toolbar']",
isShortcutToFocusToolbar: isShortcutToFocusToolbar,
handleEscape: handleEscape,
intl: intl
}, /*#__PURE__*/React.createElement(ToolbarPortal, null, /*#__PURE__*/React.createElement(MainToolbarWrapper, {
testId: "ak-editor-main-toolbar",
showKeyline: showKeyline || ContextPanelWidth > 0
}, beforeIcon && /*#__PURE__*/React.createElement("div", {
className: ax([styles.mainToolbarIconBefore, styles.mainToolbarIconBeforeNew])
}, beforeIcon), /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(FirstChildWrapper, null, expValEquals('platform_editor_default_toolbar_state', 'isEnabled', true) ? primaryToolbarDockingConfigEnabled && components && visibleToolbarComponents && isToolbar(toolbar) && /*#__PURE__*/React.createElement(ToolbarNext, {
toolbar: toolbar,
components: visibleToolbarComponents,
editorView: editorView,
editorAPI: editorAPI,
popupsMountPoint: mountPoint,
editorAppearance: "full-page",
isDisabled: disabled
}) : /*#__PURE__*/React.createElement(ExcludeFromHydration, null, primaryToolbarDockingConfigEnabled && components && visibleToolbarComponents && isToolbar(toolbar) && editorView && !isSSR() && /*#__PURE__*/React.createElement(ToolbarNext, {
toolbar: toolbar,
components: visibleToolbarComponents,
editorView: editorView,
editorAPI: editorAPI,
popupsMountPoint: mountPoint,
editorAppearance: "full-page",
isDisabled: disabled
}))), /*#__PURE__*/React.createElement(SecondChildWrapper, null, /*#__PURE__*/React.createElement("div", {
className: ax([styles.customToolbarWrapperStyle])
}, !!customPrimaryToolbarComponents && 'before' in customPrimaryToolbarComponents && /*#__PURE__*/React.createElement("div", {
"data-testid": 'before-primary-toolbar-components-plugin',
className: ax([styles.beforePrimaryToolbarComponents])
}, customPrimaryToolbarComponents.before), !!customPrimaryToolbarComponents && 'after' in customPrimaryToolbarComponents ? customPrimaryToolbarComponents.after : customPrimaryToolbarComponents)), /*#__PURE__*/React.createElement(ToolbarPortalMountPoint, null))))));
};