UNPKG

@atlaskit/editor-plugin-toolbar-lists-indentation

Version:

Toolbar lists and indentation plugin for @atlaskit/editor-core

159 lines • 6.17 kB
import React from 'react'; import { useIntl } from 'react-intl'; import { toggleBulletList as toggleBulletListKeymap, toggleOrderedList as toggleOrderedListKeymap, toggleTaskList as toggleTaskListKeymap, formatShortcut, ToolTipContent } from '@atlaskit/editor-common/keymaps'; import { messages as listMessages } from '@atlaskit/editor-common/messages/list'; import { tasksAndDecisionsMessages } from '@atlaskit/editor-common/messages/tasks-and-decisions'; import { getInputMethodFromParentKeys } from '@atlaskit/editor-common/toolbar'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/useSharedPluginStateWithSelector'; import { ListBulletedIcon } from '@atlaskit/editor-toolbar/list-bulleted-icon'; import { ListNumberedIcon } from '@atlaskit/editor-toolbar/list-numbered-icon'; import { ToolbarButton } from '@atlaskit/editor-toolbar/toolbar-button'; import { ToolbarTooltip } from '@atlaskit/editor-toolbar/toolbar-tooltip'; import TaskIcon from '@atlaskit/icon/core/task'; function useListsIndentationHeroButtonInfo({ api, parents, defaultListType }) { const { formatMessage } = useIntl(); const { bulletListActive, bulletListDisabled, orderedListActive, taskListActive } = useSharedPluginStateWithSelector(api, ['list', 'taskDecision'], states => { var _states$listState, _states$listState2, _states$listState3, _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, orderedListActive: (_states$listState3 = states.listState) === null || _states$listState3 === void 0 ? void 0 : _states$listState3.orderedListActive, taskListActive: (_states$taskDecisionS = states.taskDecisionState) === null || _states$taskDecisionS === void 0 ? void 0 : _states$taskDecisionS.isInsideTask }; }); const getListType = taskListActive ? 'taskList' : orderedListActive ? 'orderedList' : defaultListType; const taskListKeymap = toggleTaskListKeymap; const getKeymap = getListType === 'taskList' ? taskListKeymap : getListType === 'orderedList' ? toggleOrderedListKeymap : toggleBulletListKeymap; const shortcut = formatShortcut(getKeymap); const keymap = getKeymap; const message = getListType === 'taskList' ? formatMessage(tasksAndDecisionsMessages.taskList) : getListType === 'orderedList' ? formatMessage(listMessages.orderedList) : formatMessage(listMessages.bulletedList); const onClick = () => { const inputMethod = getInputMethodFromParentKeys(parents); if (getListType === 'taskList') { var _api$taskDecision; api === null || api === void 0 ? void 0 : api.core.actions.execute(api === null || api === void 0 ? void 0 : (_api$taskDecision = api.taskDecision) === null || _api$taskDecision === void 0 ? void 0 : _api$taskDecision.commands.toggleTaskList()); } else if (getListType === 'orderedList') { api === null || api === void 0 ? void 0 : api.core.actions.execute(api === null || api === void 0 ? void 0 : api.list.commands.toggleOrderedList(inputMethod)); } else { api === null || api === void 0 ? void 0 : api.core.actions.execute(api === null || api === void 0 ? void 0 : api.list.commands.toggleBulletList(inputMethod)); } }; const iconBefore = getListType === 'taskList' ? /*#__PURE__*/React.createElement(TaskIcon, { label: formatMessage(tasksAndDecisionsMessages.taskList), size: "small" }) : getListType === 'orderedList' ? /*#__PURE__*/React.createElement(ListNumberedIcon, { label: formatMessage(listMessages.orderedList), size: "small" }) : /*#__PURE__*/React.createElement(ListBulletedIcon, { label: formatMessage(listMessages.bulletedList), size: "small" }); const isSelected = getListType === 'bulletList' ? bulletListActive : getListType === 'orderedList' ? orderedListActive : taskListActive; const isDisabled = !orderedListActive && !taskListActive && bulletListDisabled; return { shortcut, keymap, message, onClick, iconBefore, isSelected, isDisabled }; } export const ListsIndentationHeroButtonCollapsed = ({ api, parents }) => { const { shortcut, keymap, message, onClick, iconBefore, isSelected, isDisabled } = useListsIndentationHeroButtonInfo({ api, parents, defaultListType: 'bulletList' }); return /*#__PURE__*/React.createElement(ToolbarTooltip, { content: /*#__PURE__*/React.createElement(ToolTipContent, { description: message, keymap: keymap }) }, /*#__PURE__*/React.createElement(ToolbarButton, { iconBefore: iconBefore, isSelected: isSelected, isDisabled: isDisabled, ariaKeyshortcuts: shortcut, onClick: onClick })); }; export const ListsIndentationHeroButtonNew = ({ api, parents }) => { const { shortcut, keymap, message, onClick, iconBefore, isSelected, isDisabled } = useListsIndentationHeroButtonInfo({ api, parents, defaultListType: 'orderedList' }); return /*#__PURE__*/React.createElement(ToolbarTooltip, { content: /*#__PURE__*/React.createElement(ToolTipContent, { description: message, keymap: keymap }) }, /*#__PURE__*/React.createElement(ToolbarButton, { iconBefore: iconBefore, isSelected: isSelected, isDisabled: isDisabled, ariaKeyshortcuts: shortcut, onClick: onClick })); }; export const ListsIndentationHeroButton = ({ api, parents }) => { const { shortcut, message, onClick, iconBefore, isSelected, isDisabled } = useListsIndentationHeroButtonInfo({ api, parents, defaultListType: 'bulletList' }); return /*#__PURE__*/React.createElement(ToolbarTooltip, { content: message }, /*#__PURE__*/React.createElement(ToolbarButton, { iconBefore: iconBefore, isSelected: isSelected, isDisabled: isDisabled, ariaKeyshortcuts: shortcut, onClick: onClick })); };