@atlaskit/editor-plugin-toolbar-lists-indentation
Version:
Toolbar lists and indentation plugin for @atlaskit/editor-core
69 lines • 2.75 kB
JavaScript
import React from 'react';
import { useIntl } from 'react-intl';
import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
import { toggleBulletList as toggleBulletListKeymap, formatShortcut } from '@atlaskit/editor-common/keymaps';
import { listMessages } from '@atlaskit/editor-common/messages';
import { getInputMethodFromParentKeys } from '@atlaskit/editor-common/toolbar';
import { ListBulletedIcon, ToolbarDropdownItem, ToolbarKeyboardShortcutHint } from '@atlaskit/editor-toolbar';
export const useBulletedListInfo = ({
api,
parents
}) => {
const {
formatMessage
} = useIntl();
const bulletMessage = formatMessage(listMessages.bulletedList);
const {
bulletListActive,
bulletListDisabled,
taskListActive
} = useSharedPluginStateWithSelector(api, ['list', 'taskDecision'], states => {
var _states$listState, _states$listState2, _states$taskDecisionS;
return {
bulletListActive: (_states$listState = states.listState) === null || _states$listState === void 0 ? void 0 : _states$listState.bulletListActive,
bulletListDisabled: (_states$listState2 = states.listState) === null || _states$listState2 === void 0 ? void 0 : _states$listState2.bulletListDisabled,
taskListActive: (_states$taskDecisionS = states.taskDecisionState) === null || _states$taskDecisionS === void 0 ? void 0 : _states$taskDecisionS.isInsideTask
};
});
const onClick = () => {
var _api$taskDecision;
api === null || api === void 0 ? void 0 : api.core.actions.execute(taskListActive ? api === null || api === void 0 ? void 0 : (_api$taskDecision = api.taskDecision) === null || _api$taskDecision === void 0 ? void 0 : _api$taskDecision.commands.toggleTaskList('bulletList') : api === null || api === void 0 ? void 0 : api.list.commands.toggleBulletList(getInputMethodFromParentKeys(parents)));
};
const isDisabled = bulletListDisabled && !taskListActive;
const shortcut = formatShortcut(toggleBulletListKeymap);
return {
bulletMessage,
onClick,
isDisabled,
isSelected: bulletListActive,
shortcut
};
};
export const BulletedListMenuItem = ({
api,
parents
}) => {
const {
bulletMessage,
onClick,
isDisabled,
isSelected,
shortcut
} = useBulletedListInfo({
api,
parents
});
return /*#__PURE__*/React.createElement(ToolbarDropdownItem, {
elemBefore: /*#__PURE__*/React.createElement(ListBulletedIcon, {
size: "small",
label: ""
}),
elemAfter: shortcut ? /*#__PURE__*/React.createElement(ToolbarKeyboardShortcutHint, {
shortcut: shortcut
}) : undefined,
isSelected: isSelected,
isDisabled: isDisabled,
onClick: onClick,
ariaKeyshortcuts: shortcut
}, bulletMessage);
};