@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
222 lines (220 loc) • 10.7 kB
JavaScript
import _objectDestructuringEmpty from "@babel/runtime/helpers/objectDestructuringEmpty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["url"];
/* eslint-disable @atlaskit/design-system/prefer-primitives */
/**
* @jsxRuntime classic
* @jsx jsx
*/
import React, { Fragment, useState } from 'react';
// eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
import { css, jsx } from '@emotion/react';
import classnames from 'classnames';
import EditorFileIcon from '@atlaskit/icon/glyph/editor/file';
import { useSharedPluginState } from '../../hooks';
import { removeMarginsAndBorder, sharedMultiBodiedExtensionStyles } from '../../ui/MultiBodiedExtension';
import { calculateBreakoutStyles, getExtensionLozengeData } from '../../utils';
import ExtensionLozenge from '../Extension/Lozenge';
import { useMultiBodiedExtensionActions } from './action-api';
import { mbeExtensionWrapperCSSStyles, overlayStyles } from './styles';
var getContainerCssExtendedStyles = function getContainerCssExtendedStyles(activeChildIndex, showMacroInteractionDesignUpdates) {
return (
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
css(sharedMultiBodiedExtensionStyles.mbeExtensionContainer, _defineProperty({}, ".multiBodiedExtension-content-dom-wrapper > [data-extension-frame='true']:nth-of-type(".concat(activeChildIndex + 1
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
, ")"), css(
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values, @atlaskit/ui-styling-standard/no-unsafe-values -- Ignored via go/DSP-18766
sharedMultiBodiedExtensionStyles.extensionFrameContent,
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-unsafe-values, @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766
showMacroInteractionDesignUpdates && removeMarginsAndBorder)))
);
};
var imageStyles = css({
maxHeight: '24px',
maxWidth: '24px'
});
// Similar to the one in platform/packages/editor/editor-common/src/extensibility/Extension/Lozenge.tsx
var getWrapperTitleContent = function getWrapperTitleContent(imageData, title, showMacroInteractionDesignUpdates) {
if (showMacroInteractionDesignUpdates) {
return null;
}
if (imageData) {
var url = imageData.url,
rest = _objectWithoutProperties(imageData, _excluded);
return (
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
jsx("div", {
className: "extension-title"
}, jsx("img", _extends({
css: imageStyles,
src: url
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
}, rest, {
alt: title
})), title)
);
}
return jsx("div", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: "extension-title",
"data-testid": 'multiBodiedExtension-default-lozenge'
}, jsx(EditorFileIcon, {
label: title
}), title);
};
var MultiBodiedExtensionWithWidth = function MultiBodiedExtensionWithWidth(_ref) {
var node = _ref.node,
handleContentDOMRef = _ref.handleContentDOMRef,
getPos = _ref.getPos,
tryExtensionHandler = _ref.tryExtensionHandler,
editorView = _ref.editorView,
eventDispatcher = _ref.eventDispatcher,
widthState = _ref.widthState,
editorAppearance = _ref.editorAppearance,
macroInteractionDesignFeatureFlags = _ref.macroInteractionDesignFeatureFlags,
isNodeSelected = _ref.isNodeSelected,
isNodeHovered = _ref.isNodeHovered,
isNodeNested = _ref.isNodeNested,
setIsNodeHovered = _ref.setIsNodeHovered;
var _ref2 = macroInteractionDesignFeatureFlags || {},
showMacroInteractionDesignUpdates = _ref2.showMacroInteractionDesignUpdates;
var _node$attrs = node.attrs,
parameters = _node$attrs.parameters,
extensionKey = _node$attrs.extensionKey;
var title = parameters && parameters.extensionTitle || parameters && parameters.macroMetadata && parameters.macroMetadata.title || extensionKey || node.type.name;
var imageData = getExtensionLozengeData({
node: node,
type: 'image'
});
var _useState = useState(0),
_useState2 = _slicedToArray(_useState, 2),
activeChildIndex = _useState2[0],
setActiveChildIndex = _useState2[1];
// Adding to avoid aliasing `this` for the callbacks
var updateActiveChild = React.useCallback(function (index) {
if (typeof index !== 'number') {
setActiveChildIndex(0);
throw new Error('Index is not valid');
}
setActiveChildIndex(index);
return true;
}, [setActiveChildIndex]);
var actions = useMultiBodiedExtensionActions({
updateActiveChild: updateActiveChild,
editorView: editorView,
getPos: getPos,
eventDispatcher: eventDispatcher,
node: node
});
var extensionHandlerResult = React.useMemo(function () {
return tryExtensionHandler(actions);
}, [tryExtensionHandler, actions]);
var articleRef = React.useCallback(function (node) {
return handleContentDOMRef(node);
}, [handleContentDOMRef]);
var shouldBreakout =
// Extension should breakout when the layout is set to 'full-width' or 'wide'.
['full-width', 'wide'].includes(node.attrs.layout) &&
// Extension breakout state should not be respected when the editor appearance is full-width mode
editorAppearance !== 'full-width';
var mbeWrapperStyles = {};
if (shouldBreakout) {
var _calculateBreakoutSty = calculateBreakoutStyles({
mode: node.attrs.layout,
widthStateLineLength: widthState === null || widthState === void 0 ? void 0 : widthState.lineLength,
widthStateWidth: widthState === null || widthState === void 0 ? void 0 : widthState.width
}),
breakoutStyles = _extends({}, (_objectDestructuringEmpty(_calculateBreakoutSty), _calculateBreakoutSty));
mbeWrapperStyles = breakoutStyles;
}
var wrapperClassNames = classnames('multiBodiedExtension--wrapper', 'extension-container', 'block', {
'with-border': showMacroInteractionDesignUpdates,
'with-danger-overlay': showMacroInteractionDesignUpdates,
'with-padding-background-styles': showMacroInteractionDesignUpdates,
'with-margin-styles': showMacroInteractionDesignUpdates && !isNodeNested,
'with-hover-border': showMacroInteractionDesignUpdates && isNodeHovered
});
var containerClassNames = classnames('multiBodiedExtension--container', {
'remove-padding': showMacroInteractionDesignUpdates
});
var navigationClassNames = classnames('multiBodiedExtension--navigation', {
'remove-margins': showMacroInteractionDesignUpdates,
'remove-border': showMacroInteractionDesignUpdates
});
var overlayClassNames = classnames('multiBodiedExtension--overlay', {
'with-margin': showMacroInteractionDesignUpdates
});
var handleMouseEvent = function handleMouseEvent(didHover) {
if (setIsNodeHovered) {
setIsNodeHovered(didHover);
}
};
return jsx(Fragment, null, showMacroInteractionDesignUpdates && jsx(ExtensionLozenge, {
isNodeSelected: isNodeSelected,
node: node,
showMacroInteractionDesignUpdates: true,
customContainerStyles: mbeWrapperStyles,
isNodeHovered: isNodeHovered,
isNodeNested: isNodeNested,
setIsNodeHovered: setIsNodeHovered,
isBodiedMacro: true
}), jsx("div", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: wrapperClassNames
// 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: mbeExtensionWrapperCSSStyles,
"data-testid": "multiBodiedExtension--wrapper"
// eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
,
style: mbeWrapperStyles,
onMouseEnter: function onMouseEnter() {
return handleMouseEvent(true);
},
onMouseLeave: function onMouseLeave() {
return handleMouseEvent(false);
}
}, jsx("div", {
// 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: overlayStyles
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
,
className: overlayClassNames,
"data-testid": "multiBodiedExtension--overlay"
}), getWrapperTitleContent(imageData, title, showMacroInteractionDesignUpdates), jsx("div", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: containerClassNames
// eslint-disable-next-line @atlaskit/design-system/consistent-css-prop-usage -- Ignored via go/DSP-18766
,
css: getContainerCssExtendedStyles(activeChildIndex, showMacroInteractionDesignUpdates),
"data-testid": "multiBodiedExtension--container",
"data-active-child-index": activeChildIndex
}, jsx("nav", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: navigationClassNames
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-imported-style-values -- Ignored via go/DSP-18766
,
css: sharedMultiBodiedExtensionStyles.mbeNavigation,
"data-testid": "multiBodiedExtension-navigation"
}, extensionHandlerResult), jsx("article", {
// eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
className: "multiBodiedExtension--frames",
"data-testid": "multiBodiedExtension--frames",
ref: articleRef
}))));
};
var MultiBodiedExtension = function MultiBodiedExtension(props) {
var pluginInjectionApi = props.pluginInjectionApi;
var _useSharedPluginState = useSharedPluginState(pluginInjectionApi, ['width']),
widthState = _useSharedPluginState.widthState;
// Ignored via go/ees005
// eslint-disable-next-line react/jsx-props-no-spreading
return jsx(MultiBodiedExtensionWithWidth, _extends({
widthState: widthState
}, props));
};
export default MultiBodiedExtension;