@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
80 lines (78 loc) • 3.5 kB
JavaScript
import React, { useEffect } from 'react';
import { injectIntl, useIntl } from 'react-intl';
import { getDocument } from '@atlaskit/browser-apis';
import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
import { BLOCK_MENU_ACTION_TEST_ID } from '@atlaskit/editor-common/block-menu';
import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
import { blockMenuMessages as messages } from '@atlaskit/editor-common/messages';
import { DIRECTION } from '@atlaskit/editor-common/types';
import { ToolbarDropdownItem } from '@atlaskit/editor-toolbar';
import ArrowDownIcon from '@atlaskit/icon/core/arrow-down';
import { useBlockMenu } from './block-menu-provider';
import { BLOCK_MENU_ITEM_NAME } from './consts';
import { fixBlockMenuPositionAndScroll } from './utils/fixBlockMenuPositionAndScroll';
const MoveDownDropdownItemContent = ({
api
}) => {
const {
formatMessage
} = useIntl();
const {
moveUpRef,
moveDownRef,
getFirstSelectedDomNode
} = useBlockMenu();
const {
canMoveDown
} = useSharedPluginStateWithSelector(api, ['blockControls'], ({
blockControlsState
}) => {
var _blockControlsState$b;
return {
canMoveDown: blockControlsState === null || blockControlsState === void 0 ? void 0 : (_blockControlsState$b = blockControlsState.blockMenuOptions) === null || _blockControlsState$b === void 0 ? void 0 : _blockControlsState$b.canMoveDown
};
});
// Maybe don't need this
useEffect(() => {
const doc = getDocument();
if (!canMoveDown && moveDownRef.current && doc && moveDownRef.current === doc.activeElement && moveUpRef.current) {
moveUpRef.current.focus();
}
}, [canMoveDown, moveUpRef, moveDownRef]);
const handleClick = () => {
api === null || api === void 0 ? void 0 : api.core.actions.execute(({
tr
}) => {
var _api$analytics, _api$analytics$action, _api$blockControls, _api$blockControls$co;
const payload = {
action: ACTION.CLICKED,
actionSubject: ACTION_SUBJECT.BLOCK_MENU_ITEM,
attributes: {
menuItemName: BLOCK_MENU_ITEM_NAME.MOVE_DOWN
},
eventType: EVENT_TYPE.UI
};
api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : (_api$analytics$action = _api$analytics.actions) === null || _api$analytics$action === void 0 ? void 0 : _api$analytics$action.attachAnalyticsEvent(payload)(tr);
api === null || api === void 0 ? void 0 : (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : (_api$blockControls$co = _api$blockControls.commands) === null || _api$blockControls$co === void 0 ? void 0 : _api$blockControls$co.moveNodeWithBlockMenu(DIRECTION.DOWN)({
tr
});
return tr;
});
requestAnimationFrame(() => {
const newFirstNode = getFirstSelectedDomNode();
fixBlockMenuPositionAndScroll(newFirstNode);
});
};
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
triggerRef: moveDownRef,
onClick: handleClick,
elemBefore: /*#__PURE__*/React.createElement(ArrowDownIcon, {
label: "",
size: "small"
}),
isDisabled: !canMoveDown,
testId: BLOCK_MENU_ACTION_TEST_ID.MOVE_DOWN
}, formatMessage(messages.moveDownBlock));
};
// eslint-disable-next-line @typescript-eslint/ban-types
export const MoveDownDropdownItem = injectIntl(MoveDownDropdownItemContent);