@atlaskit/editor-plugin-toolbar-lists-indentation
Version:
Toolbar lists and indentation plugin for @atlaskit/editor-core
150 lines • 7.5 kB
JavaScript
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(_ref) {
var api = _ref.api,
parents = _ref.parents,
defaultListType = _ref.defaultListType;
var _useIntl = useIntl(),
formatMessage = _useIntl.formatMessage;
var _useSharedPluginState = useSharedPluginStateWithSelector(api, ['list', 'taskDecision'], function (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
};
}),
bulletListActive = _useSharedPluginState.bulletListActive,
bulletListDisabled = _useSharedPluginState.bulletListDisabled,
orderedListActive = _useSharedPluginState.orderedListActive,
taskListActive = _useSharedPluginState.taskListActive;
var getListType = taskListActive ? 'taskList' : orderedListActive ? 'orderedList' : defaultListType;
var taskListKeymap = toggleTaskListKeymap;
var getKeymap = getListType === 'taskList' ? taskListKeymap : getListType === 'orderedList' ? toggleOrderedListKeymap : toggleBulletListKeymap;
var shortcut = formatShortcut(getKeymap);
var keymap = getKeymap;
var message = getListType === 'taskList' ? formatMessage(tasksAndDecisionsMessages.taskList) : getListType === 'orderedList' ? formatMessage(listMessages.orderedList) : formatMessage(listMessages.bulletedList);
var onClick = function onClick() {
var inputMethod = getInputMethodFromParentKeys(parents);
if (getListType === 'taskList') {
var _api$taskDecision;
api === null || api === void 0 || api.core.actions.execute(api === null || api === 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 || api.core.actions.execute(api === null || api === void 0 ? void 0 : api.list.commands.toggleOrderedList(inputMethod));
} else {
api === null || api === void 0 || api.core.actions.execute(api === null || api === void 0 ? void 0 : api.list.commands.toggleBulletList(inputMethod));
}
};
var 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"
});
var isSelected = getListType === 'bulletList' ? bulletListActive : getListType === 'orderedList' ? orderedListActive : taskListActive;
var isDisabled = !orderedListActive && !taskListActive && bulletListDisabled;
return {
shortcut: shortcut,
keymap: keymap,
message: message,
onClick: onClick,
iconBefore: iconBefore,
isSelected: isSelected,
isDisabled: isDisabled
};
}
export var ListsIndentationHeroButtonCollapsed = function ListsIndentationHeroButtonCollapsed(_ref2) {
var api = _ref2.api,
parents = _ref2.parents;
var _useListsIndentationH = useListsIndentationHeroButtonInfo({
api: api,
parents: parents,
defaultListType: 'bulletList'
}),
shortcut = _useListsIndentationH.shortcut,
keymap = _useListsIndentationH.keymap,
message = _useListsIndentationH.message,
onClick = _useListsIndentationH.onClick,
iconBefore = _useListsIndentationH.iconBefore,
isSelected = _useListsIndentationH.isSelected,
isDisabled = _useListsIndentationH.isDisabled;
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 var ListsIndentationHeroButtonNew = function ListsIndentationHeroButtonNew(_ref3) {
var api = _ref3.api,
parents = _ref3.parents;
var _useListsIndentationH2 = useListsIndentationHeroButtonInfo({
api: api,
parents: parents,
defaultListType: 'orderedList'
}),
shortcut = _useListsIndentationH2.shortcut,
keymap = _useListsIndentationH2.keymap,
message = _useListsIndentationH2.message,
onClick = _useListsIndentationH2.onClick,
iconBefore = _useListsIndentationH2.iconBefore,
isSelected = _useListsIndentationH2.isSelected,
isDisabled = _useListsIndentationH2.isDisabled;
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 var ListsIndentationHeroButton = function ListsIndentationHeroButton(_ref4) {
var api = _ref4.api,
parents = _ref4.parents;
var _useListsIndentationH3 = useListsIndentationHeroButtonInfo({
api: api,
parents: parents,
defaultListType: 'bulletList'
}),
shortcut = _useListsIndentationH3.shortcut,
message = _useListsIndentationH3.message,
onClick = _useListsIndentationH3.onClick,
iconBefore = _useListsIndentationH3.iconBefore,
isSelected = _useListsIndentationH3.isSelected,
isDisabled = _useListsIndentationH3.isDisabled;
return /*#__PURE__*/React.createElement(ToolbarTooltip, {
content: message
}, /*#__PURE__*/React.createElement(ToolbarButton, {
iconBefore: iconBefore,
isSelected: isSelected,
isDisabled: isDisabled,
ariaKeyshortcuts: shortcut,
onClick: onClick
}));
};