@atlaskit/editor-plugin-layout
Version:
Layout plugin for @atlaskit/editor-core
68 lines • 3.09 kB
JavaScript
import React, { useCallback } from 'react';
import { useIntl } from 'react-intl';
import { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { deleteColumn, getAriaKeyshortcuts, tooltip } from '@atlaskit/editor-common/keymaps';
import { layoutMessages } from '@atlaskit/editor-common/messages';
import { DeleteIcon, ToolbarDropdownItem, ToolbarKeyboardShortcutHint } from '@atlaskit/editor-toolbar';
import { useSelectedLayoutColumns } from './useSelectedLayoutColumns';
const DeleteColumnDropdownItem = ({
api
}) => {
var _tooltip;
const {
formatMessage
} = useIntl();
const selectedLayoutColumns = useSelectedLayoutColumns(api);
const setDangerPreview = useCallback(show => {
var _api$core, _api$layout;
api === null || api === void 0 ? void 0 : (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.execute(api === null || api === void 0 ? void 0 : (_api$layout = api.layout) === null || _api$layout === void 0 ? void 0 : _api$layout.commands.setLayoutColumnDangerPreview(show));
}, [api]);
const showDangerPreview = useCallback(() => {
setDangerPreview(true);
}, [setDangerPreview]);
const hideDangerPreview = useCallback(() => {
setDangerPreview(false);
}, [setDangerPreview]);
const onClick = useCallback(() => {
var _api$layout2, _api$core2;
const deleteCommand = api === null || api === void 0 ? void 0 : (_api$layout2 = api.layout) === null || _api$layout2 === void 0 ? void 0 : _api$layout2.commands.deleteLayoutColumn({
inputMethod: INPUT_METHOD.LAYOUT_COLUMN_MENU
});
api === null || api === void 0 ? void 0 : (_api$core2 = api.core) === null || _api$core2 === void 0 ? void 0 : _api$core2.actions.execute(props => {
var _api$layout3;
const tr = deleteCommand === null || deleteCommand === void 0 ? void 0 : deleteCommand(props);
if (!tr) {
return tr !== null && tr !== void 0 ? tr : null;
}
api === null || api === void 0 ? void 0 : (_api$layout3 = api.layout) === null || _api$layout3 === void 0 ? void 0 : _api$layout3.commands.toggleLayoutColumnMenu({
isOpen: false
})({
tr
});
return tr;
});
}, [api]);
if (selectedLayoutColumns === undefined) {
return null;
}
const selectedColumnCount = selectedLayoutColumns.selectedLayoutColumns.length;
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
ariaKeyshortcuts: getAriaKeyshortcuts(deleteColumn),
onClick: onClick,
onFocus: showDangerPreview,
onMouseEnter: showDangerPreview,
onBlur: hideDangerPreview,
onMouseLeave: hideDangerPreview,
elemBefore: /*#__PURE__*/React.createElement(DeleteIcon, {
color: "currentColor",
label: "",
size: "small"
}),
elemAfter: /*#__PURE__*/React.createElement(ToolbarKeyboardShortcutHint, {
shortcut: (_tooltip = tooltip(deleteColumn)) !== null && _tooltip !== void 0 ? _tooltip : ''
})
}, formatMessage(layoutMessages.deleteColumn, {
count: selectedColumnCount
}));
};
export { DeleteColumnDropdownItem };