UNPKG

@atlaskit/editor-common

Version:

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

623 lines (616 loc) • 24.5 kB
import _extends from "@babel/runtime/helpers/extends"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; /* eslint-disable @atlaskit/ui-styling-standard/use-compiled -- Pre-existing lint debt surfaced by this mechanical type-import-only PR. */ /** * @jsxRuntime classic * @jsx jsx */ import React, { Fragment, memo, useCallback, useEffect, useMemo, useState } from 'react'; // 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 import { css, jsx } from '@emotion/react'; import { Grid, List } from 'react-virtualized'; import { AutoSizer } from 'react-virtualized/dist/commonjs/AutoSizer'; import { CellMeasurer, CellMeasurerCache } from 'react-virtualized/dist/commonjs/CellMeasurer'; import withAnalyticsContext from '@atlaskit/analytics-next/withAnalyticsContext'; import { relativeFontSizeToBase16 } from '@atlaskit/editor-shared-styles'; import { shortcutStyle } from '@atlaskit/editor-shared-styles/shortcut'; import { ButtonItem } from '@atlaskit/menu'; import { fg } from '@atlaskit/platform-feature-flags'; import { Flex, Stack, Text } from '@atlaskit/primitives/compiled'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments'; import Tooltip from '@atlaskit/tooltip'; import { ACTION, ACTION_SUBJECT, EVENT_TYPE, fireAnalyticsEvent } from '../../../analytics'; import { IconFallback } from '../../../quick-insert'; import { ELEMENT_ITEM_HEIGHT, ELEMENT_ITEM_PADDING, ELEMENT_LIST_PADDING, SCROLLBAR_WIDTH } from '../../constants'; import useContainerWidth from '../../hooks/use-container-width'; import useFocus from '../../hooks/use-focus'; import { Modes } from '../../types'; import EmptyState from './EmptyState'; import { getColumnCount, getScrollbarWidth } from './utils'; export var ICON_HEIGHT = 40; export var ICON_WIDTH = 40; // eslint-disable-next-line @atlaskit/ui-styling-standard/no-exported-styles -- Ignored via go/DSP-18766 export var itemIcon = css({ width: "".concat(ICON_WIDTH, "px"), height: "".concat(ICON_HEIGHT, "px"), overflow: 'hidden', border: "var(--ds-border-width, 1px)".concat(" solid ", "var(--ds-border, #0B120E24)"), // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766 borderRadius: "var(--ds-radius-small, 3px)", boxSizing: 'border-box', display: 'flex', justifyContent: 'center', alignItems: 'center', // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 div: { width: "".concat(ICON_WIDTH, "px"), height: "".concat(ICON_HEIGHT, "px") } }); function ElementList(_ref) { var items = _ref.items, mode = _ref.mode, selectedItemIndex = _ref.selectedItemIndex, focusedItemIndex = _ref.focusedItemIndex, focusOnEmptyStateButton = _ref.focusOnEmptyStateButton, columnCount = _ref.columnCount, setColumnCount = _ref.setColumnCount, createAnalyticsEvent = _ref.createAnalyticsEvent, emptyStateHandler = _ref.emptyStateHandler, selectedCategory = _ref.selectedCategory, selectedCategoryIndex = _ref.selectedCategoryIndex, searchTerm = _ref.searchTerm, setFocusedCategoryIndex = _ref.setFocusedCategoryIndex, setFocusedItemIndex = _ref.setFocusedItemIndex, cache = _ref.cache, onInsertItem = _ref.onInsertItem, _ref$hasTabListContex = _ref.hasTabListContext, hasTabListContext = _ref$hasTabListContex === void 0 ? false : _ref$hasTabListContex; var _useContainerWidth = useContainerWidth(), containerWidth = _useContainerWidth.containerWidth, ContainerWidthMonitor = _useContainerWidth.ContainerWidthMonitor; var _useState = useState(SCROLLBAR_WIDTH), _useState2 = _slicedToArray(_useState, 2), scrollbarWidth = _useState2[0], setScrollbarWidth = _useState2[1]; var fullMode = mode === Modes.full; useEffect(function () { /** * More of an optimization condition. * Initially the containerWidths are reported 0 twice. **/ if (fullMode && containerWidth > 0) { setColumnCount(getColumnCount(containerWidth)); var updatedScrollbarWidth = getScrollbarWidth(); if (updatedScrollbarWidth > 0) { // eslint-disable-next-line @atlassian/perf-linting/no-chain-state-updates -- Ignored via go/ees017 (to be fixed) setScrollbarWidth(updatedScrollbarWidth); } } }, [fullMode, containerWidth, setColumnCount, scrollbarWidth]); var onExternalLinkClick = useCallback(function () { fireAnalyticsEvent(createAnalyticsEvent)({ payload: { action: ACTION.VISITED, actionSubject: ACTION_SUBJECT.SMART_LINK, eventType: EVENT_TYPE.TRACK } }); }, [createAnalyticsEvent]); var listCache = useMemo(function () { return cache !== null && cache !== void 0 ? cache : new CellMeasurerCache({ fixedWidth: true, defaultHeight: ELEMENT_ITEM_HEIGHT, minHeight: ELEMENT_ITEM_HEIGHT }); }, [cache]); useEffect(function () { // need to recalculate values if we have the same items, but they're reordered listCache.clearAll(); }, [listCache, searchTerm]); var rowHeight = function rowHeight(_ref2) { var index = _ref2.index; return listCache.rowHeight({ index: index }); }; return jsx(Fragment, null, jsx(ContainerWidthMonitor, null), jsx("div", { css: elementItemsWrapper, "data-testid": "element-items", id: selectedCategory ? "browse-category-".concat(selectedCategory, "-tab") : 'browse-category-tab', "aria-labelledby": !hasTabListContext && fg('platform_editor_ally_remove_role_tabpanel') ? undefined : selectedCategory ? "browse-category--".concat(selectedCategory, "-button") : 'browse-category-button', role: !hasTabListContext && fg('platform_editor_ally_remove_role_tabpanel') ? undefined : 'tabpanel', tabIndex: items.length === 0 ? 0 : undefined }, !items.length ? emptyStateHandler ? emptyStateHandler({ mode: mode, selectedCategory: selectedCategory, searchTerm: searchTerm }) : jsx(EmptyState, { onExternalLinkClick: onExternalLinkClick, focusOnEmptyStateButton: focusOnEmptyStateButton }) : jsx(Fragment, null, containerWidth > 0 && jsx(AutoSizer, { disableWidth: true }, function (_ref3) { var height = _ref3.height; return columnCount === 1 ? jsx(ElementListSingleColumn, { cache: listCache, setFocusedCategoryIndex: setFocusedCategoryIndex, selectedCategoryIndex: selectedCategoryIndex, items: items, setFocusedItemIndex: setFocusedItemIndex, fullMode: fullMode, selectedItemIndex: selectedItemIndex, focusedItemIndex: focusedItemIndex, rowHeight: rowHeight, containerWidth: containerWidth, height: height, onInsertItem: onInsertItem }) : jsx(ElementListMultipleColumns, { columnCount: columnCount, cache: listCache, setFocusedCategoryIndex: setFocusedCategoryIndex, selectedCategoryIndex: selectedCategoryIndex, items: items, setFocusedItemIndex: setFocusedItemIndex, fullMode: fullMode, selectedItemIndex: selectedItemIndex, focusedItemIndex: focusedItemIndex, rowHeight: rowHeight, containerWidth: containerWidth, height: height, onInsertItem: onInsertItem }); })))); } var ElementListSingleColumn = function ElementListSingleColumn(props) { var items = props.items, fullMode = props.fullMode, setFocusedItemIndex = props.setFocusedItemIndex, rowHeight = props.rowHeight, containerWidth = props.containerWidth, height = props.height, onInsertItem = props.onInsertItem, cache = props.cache, focusedItemIndex = props.focusedItemIndex, setFocusedCategoryIndex = props.setFocusedCategoryIndex, selectedCategoryIndex = props.selectedCategoryIndex, selectedItemIndex = props.selectedItemIndex; var rowRenderer = useMemo(function () { return function (_ref4) { var index = _ref4.index, key = _ref4.key, style = _ref4.style, parent = _ref4.parent; return jsx(CellMeasurer, { key: key, cache: cache, parent: parent, columnIndex: 0, rowIndex: index }, jsx("div", { // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766 style: style, key: key // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 -- Ignored via go/DSP-18766 , className: "element-item-wrapper", css: elementItemWrapperSingle, role: expValEquals('editor_a11y__enghealth-46814_fy26', 'isEnabled', true) ? 'presentation' : undefined, onKeyDown: function onKeyDown(e) { if (e.key === 'Tab') { if (e.shiftKey && index === 0) { if (setFocusedCategoryIndex) { if (!!selectedCategoryIndex) { setFocusedCategoryIndex(selectedCategoryIndex); } else { setFocusedCategoryIndex(0); } e.preventDefault(); } } // before focus jumps from elements list we need to rerender react-virtualized grid. // Otherwise on the next render 'scrollToCell' will have same cached value // and grid will not be scrolled to top. // So Tab press on category will not work anymore due to invisible 1-t element. else if (index === items.length - 2) { setFocusedItemIndex(items.length - 1); } } } }, jsx(MemoizedElementItem, { inlineMode: !fullMode, index: index, item: items[index], selected: selectedItemIndex === index, focus: focusedItemIndex === index, setFocusedItemIndex: setFocusedItemIndex, onInsertItem: onInsertItem, role: expValEquals('platform_editor_august_a11y', 'isEnabled', true) ? 'option' : undefined }))); }; }, // eslint-disable-next-line react-hooks/exhaustive-deps [cache, items, fullMode, selectedItemIndex, focusedItemIndex, selectedCategoryIndex, setFocusedCategoryIndex, setFocusedItemIndex, props]); return jsx(List, _extends({ rowRenderer: rowRenderer, rowCount: items.length, rowHeight: rowHeight, width: containerWidth - ELEMENT_LIST_PADDING * 2, height: height, overscanRowCount: 3, containerRole: "presentation", role: "listbox" // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading }, selectedItemIndex !== undefined && { scrollToIndex: selectedItemIndex })); }; var ElementListMultipleColumns = function ElementListMultipleColumns(props) { var columnCount = props.columnCount, items = props.items, fullMode = props.fullMode, setFocusedItemIndex = props.setFocusedItemIndex, rowHeight = props.rowHeight, containerWidth = props.containerWidth, height = props.height, onInsertItem = props.onInsertItem, cache = props.cache, focusedItemIndex = props.focusedItemIndex, setFocusedCategoryIndex = props.setFocusedCategoryIndex, selectedCategoryIndex = props.selectedCategoryIndex, selectedItemIndex = props.selectedItemIndex; var columnWidth = (containerWidth - ELEMENT_ITEM_PADDING * 2) / columnCount; var rowCount = Math.ceil(items.length / columnCount); var cellRenderer = useMemo(function () { return function (_ref5) { var columnIndex = _ref5.columnIndex, key = _ref5.key, parent = _ref5.parent, rowIndex = _ref5.rowIndex, style = _ref5.style; var index = rowIndex * columnCount + columnIndex; if (items[index] == null) { return; } return index > items.length - 1 ? null : jsx(CellMeasurer, { cache: cache, key: key, rowIndex: rowIndex, columnIndex: columnIndex, parent: parent }, jsx("div", { // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766 style: style, key: key // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 -- Ignored via go/DSP-18766 , className: "element-item-wrapper", role: "gridcell", tabIndex: 0, css: elementItemWrapper, onKeyDown: function onKeyDown(e) { if (e.key === 'Tab') { if (e.shiftKey && index === 0) { if (setFocusedCategoryIndex) { if (!!selectedCategoryIndex) { setFocusedCategoryIndex(selectedCategoryIndex); } else { setFocusedCategoryIndex(0); } e.preventDefault(); } } // before focus jumps from elements list we need to rerender react-virtualized grid. // Otherwise on the next render 'scrollToCell' will have same cached value // and grid will not be scrolled to top. // So Tab press on category will not work anymore due to invisible 1-t element. else if (index === items.length - 2) { setFocusedItemIndex(items.length - 1); } } } }, jsx(MemoizedElementItem, { inlineMode: !fullMode, index: index, item: items[index], selected: selectedItemIndex === index, focus: focusedItemIndex === index, setFocusedItemIndex: setFocusedItemIndex, onInsertItem: onInsertItem }))); }; }, // eslint-disable-next-line react-hooks/exhaustive-deps [cache, items, fullMode, selectedItemIndex, columnCount, focusedItemIndex, selectedCategoryIndex, setFocusedCategoryIndex, setFocusedItemIndex, props]); return jsx(Grid, _extends({ containerRole: "row", cellRenderer: cellRenderer, height: height, width: containerWidth - ELEMENT_LIST_PADDING * 2 // containerWidth - padding on Left/Right (for focus outline) /** * Refresh Grid on WidthObserver value change. * Length of the items used to force re-render to solve Firefox bug with react-virtualized retaining * scroll position after updating the data. If new data has different number of cells, a re-render * is forced to prevent the scroll position render bug. */, key: containerWidth + items.length, rowHeight: rowHeight, rowCount: rowCount, columnCount: columnCount, columnWidth: columnWidth, deferredMeasurementCache: cache // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading }, selectedItemIndex !== undefined && { scrollToRow: Math.floor(selectedItemIndex / columnCount) })); }; var MemoizedElementItem = /*#__PURE__*/memo(ElementItem); MemoizedElementItem.displayName = 'MemoizedElementItem'; export function ElementItem(_ref6) { var inlineMode = _ref6.inlineMode, selected = _ref6.selected, item = _ref6.item, index = _ref6.index, onInsertItem = _ref6.onInsertItem, focus = _ref6.focus, setFocusedItemIndex = _ref6.setFocusedItemIndex, role = _ref6.role; var ref = useFocus(focus); /** * Note: props.onSelectItem(item) is not called here as the StatelessElementBrowser's * useEffect would trigger it on selectedItemIndex change. (Line 106-110) * This implementation was changed for keyboard/click selection to work with `onInsertItem`. */ var onClick = useCallback(function (e) { e.preventDefault(); e.stopPropagation(); setFocusedItemIndex(index); switch (e.nativeEvent.detail) { case 0: onInsertItem(item); break; case 1: if (inlineMode) { onInsertItem(item); } break; case 2: if (!inlineMode) { onInsertItem(item); } break; default: return; } }, [index, inlineMode, item, onInsertItem, setFocusedItemIndex]); var icon = item.icon, title = item.title, description = item.description, keyshortcut = item.keyshortcut, isDisabled = item.isDisabled, lozenge = item.lozenge; return jsx(Tooltip, { content: description, testId: "element-item-tooltip-".concat(index) }, jsx(ButtonItem, { onClick: onClick, iconBefore: jsx(ElementBefore, { icon: icon, title: title }), isSelected: selected, "aria-describedby": expValEquals('platform_editor_august_a11y', 'isEnabled', true) ? undefined : title, "aria-label": expValEquals('platform_editor_august_a11y', 'isEnabled', true) ? title : undefined, ref: ref, testId: "element-item-".concat(index), id: "searched-item-".concat(index), isDisabled: isDisabled, role: role }, jsx(ItemContent // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766 , { style: inlineMode ? null : itemStyleOverrides, tabIndex: 0, title: title, description: description, keyshortcut: keyshortcut, isDisabled: isDisabled, lozenge: lozenge }))); } /** * Inline mode should use the existing Align-items:center value. */ var itemStyleOverrides = { alignItems: 'flex-start' }; var ElementBefore = /*#__PURE__*/memo(function (_ref7) { var icon = _ref7.icon; return jsx("div", { css: [itemIcon, itemIconStyle] }, icon ? icon() : jsx(IconFallback, null)); }); var ItemContent = /*#__PURE__*/memo(function (_ref8) { var title = _ref8.title, description = _ref8.description, keyshortcut = _ref8.keyshortcut, lozenge = _ref8.lozenge, isDisabled = _ref8.isDisabled; if (fg('platform_editor_typography_ugc')) { return ( // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 jsx("div", { css: itemBody, className: "item-body" }, jsx("div", { css: itemText }, jsx(Stack, { space: "space.025" }, jsx("div", { css: itemTitleWrapper }, editorExperiment('platform_synced_block', true) || lozenge ? jsx(Flex, { alignItems: "center", gap: "space.050" }, jsx(Text, { color: isDisabled ? 'color.text.disabled' : undefined, maxLines: 1 }, title), lozenge) : jsx(Text, { color: isDisabled ? 'color.text.disabled' : undefined, maxLines: 1 }, title), jsx("div", { css: itemAfter }, keyshortcut && jsx("div", { css: shortcutStyle }, keyshortcut))), description && jsx(Text, { color: isDisabled ? 'color.text.disabled' : 'color.text.subtle', size: "small", maxLines: 2 }, description)))) ); } else { return ( // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766 jsx("div", { css: itemBody, className: "item-body" }, jsx("div", { css: itemText }, jsx("div", { css: itemTitleWrapper }, jsx("p", { css: isDisabled ? itemTitleDisabled : itemTitle }, title), jsx("div", { css: itemAfter }, keyshortcut && jsx("div", { css: shortcutStyle }, keyshortcut))), description && // eslint-disable-next-line @atlaskit/design-system/use-primitives-text jsx("p", { css: isDisabled ? itemDescriptionDisabled : itemDescription }, description))) ); } }); var elementItemsWrapper = css({ flex: 1, flexFlow: 'row wrap', alignItems: 'flex-start', justifyContent: 'flex-start', overflow: 'hidden', padding: "var(--ds-space-025, 2px)", // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 '.ReactVirtualized__Grid': { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/design-system/no-unsafe-design-token-usage -- Ignored via go/DSP-18766 borderRadius: "var(--ds-radius-small, 3px)", outline: 'none', '&:focus': { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766 boxShadow: "0 0 0 ".concat(ELEMENT_LIST_PADDING, "px ", "var(--ds-border-focused, #4688EC)") } }, // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 '.ReactVirtualized__Grid__innerScrollContainer': { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors, @atlaskit/ui-styling-standard/no-unsafe-selectors -- Ignored via go/DSP-18766 "div[class='element-item-wrapper']:last-child": { paddingBottom: "var(--ds-space-050, 4px)" } } }); var elementItemWrapperSingle = css({ // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 div: { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 button: { minHeight: '60px', alignItems: 'flex-start', padding: "var(--ds-space-150, 12px)".concat(" ", "var(--ds-space-150, 12px)", " 11px") } } }); var elementItemWrapper = css({ // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 div: { // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 button: { minHeight: '75px', alignItems: 'flex-start', padding: "var(--ds-space-150, 12px)".concat(" ", "var(--ds-space-150, 12px)", " 11px") } } }); var itemBody = css({ display: 'flex', flexDirection: 'row', flexWrap: 'nowrap', justifyContent: 'space-between', // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography lineHeight: 1.4, width: '100%', marginTop: "var(--ds-space-negative-025, -2px)" }); /* * -webkit-line-clamp is also supported by firefox 🎉 * https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Releases/68#CSS */ var multilineStyle = css({ display: '-webkit-box', WebkitLineClamp: 2, WebkitBoxOrient: 'vertical' }); // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values, @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766 var itemDescription = css(multilineStyle, { overflow: 'hidden', // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766 fontSize: relativeFontSizeToBase16(11.67), color: "var(--ds-text-subtle, #505258)", marginTop: "var(--ds-space-025, 2px)" }); // eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values, @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766 var itemDescriptionDisabled = css(multilineStyle, { overflow: 'hidden', // eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766 fontSize: relativeFontSizeToBase16(11.67), color: "var(--ds-text-disabled, #080F214A)", marginTop: "var(--ds-space-025, 2px)" }); var itemText = css({ width: 'inherit', whiteSpace: 'initial' }); var itemTitleWrapper = css({ display: 'flex', justifyContent: 'space-between' }); var itemTitle = css({ width: '100%', overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis' }); var itemTitleDisabled = css({ width: '100%', overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis', color: "var(--ds-text-disabled, #080F214A)" }); var itemAfter = css({ flex: '0 0 auto', paddingTop: "var(--ds-space-025, 2px)", marginBottom: "var(--ds-space-negative-025, -2px)" }); var itemIconStyle = css({ // eslint-disable-next-line @atlaskit/ui-styling-standard/no-nested-selectors -- Ignored via go/DSP-18766 img: { height: '40px', width: '40px', objectFit: 'cover' } }); var MemoizedElementListWithAnalytics = /*#__PURE__*/memo(withAnalyticsContext({ component: 'ElementList' })(ElementList)); export default MemoizedElementListWithAnalytics;