@atlaskit/editor-plugin-text-formatting
Version:
Text-formatting plugin for @atlaskit/editor-core
131 lines • 6.75 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { useIntl } from 'react-intl';
import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
import { toggleBold, toggleCode, toggleItalic, toggleStrikethrough, toggleSubscript, toggleSuperscript, toggleUnderline, tooltip } from '@atlaskit/editor-common/keymaps';
import { toolbarMessages } from '@atlaskit/editor-common/messages';
import { BOLD_MENU_ITEM, CODE_MENU_ITEM, ITALIC_MENU_ITEM, STRIKE_MENU_ITEM, SUBSCRIPT_MENU_ITEM, SUPERSCRIPT_MENU_ITEM, TEXT_FORMATTING_MENU_SECTION_RANK, UNDERLINE_MENU_ITEM, getInputMethodFromParentKeys } from '@atlaskit/editor-common/toolbar';
import { BoldIcon, ItalicIcon, UnderlineIcon, CodeIcon, StrikeThroughIcon, SubscriptIcon, SuperscriptIcon } from '@atlaskit/editor-toolbar';
import { toggleStrongWithAnalytics, toggleEmWithAnalytics, toggleUnderlineWithAnalytics, toggleCodeWithAnalytics, toggleStrikeWithAnalytics, toggleSubscriptWithAnalytics, toggleSuperscriptWithAnalytics } from '../../../editor-commands/toggle-mark';
var FormatMarkSchema = {
strong: 'strong',
em: 'em',
underline: 'underline',
strike: 'strike',
code: 'code',
subscript: 'subsup',
superscript: 'subsup'
};
export var useComponentInfo = function useComponentInfo(_ref) {
var _api$core;
var api = _ref.api,
optionType = _ref.optionType,
title = _ref.title,
shortcut = _ref.shortcut,
toggleMarkWithAnalyticsCallback = _ref.toggleMarkWithAnalyticsCallback,
parents = _ref.parents;
var _useSharedPluginState = useSharedPluginStateWithSelector(api, ['textFormatting'], function (states) {
var _states$textFormattin, _states$textFormattin2, _states$textFormattin3, _states$textFormattin4;
return {
isActive: (_states$textFormattin = states.textFormattingState) === null || _states$textFormattin === void 0 ? void 0 : _states$textFormattin["".concat(optionType, "Active")],
isDisabled: (_states$textFormattin2 = states.textFormattingState) === null || _states$textFormattin2 === void 0 ? void 0 : _states$textFormattin2["".concat(optionType, "Disabled")],
isHidden: (_states$textFormattin3 = states.textFormattingState) === null || _states$textFormattin3 === void 0 ? void 0 : _states$textFormattin3["".concat(optionType, "Hidden")],
isPluginInitialised: (_states$textFormattin4 = states.textFormattingState) === null || _states$textFormattin4 === void 0 ? void 0 : _states$textFormattin4.isInitialised
};
}),
isActive = _useSharedPluginState.isActive,
isDisabled = _useSharedPluginState.isDisabled,
isHidden = _useSharedPluginState.isHidden,
isPluginInitialised = _useSharedPluginState.isPluginInitialised;
var hasMarkSchema = api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 || (_api$core = _api$core.sharedState.currentState()) === null || _api$core === void 0 || (_api$core = _api$core.schema) === null || _api$core === void 0 ? void 0 : _api$core.marks[FormatMarkSchema[optionType]];
var formatOptionState;
if (!isPluginInitialised) {
formatOptionState = {
isActive: false,
isDisabled: true,
isHidden: false
};
} else {
formatOptionState = {
isActive: Boolean(isActive),
isDisabled: Boolean(isDisabled),
isHidden: Boolean(!hasMarkSchema || isHidden)
};
}
var _useIntl = useIntl(),
formatMessage = _useIntl.formatMessage;
var formatTitle = formatMessage(title);
var shortcutContent = tooltip(shortcut);
var ariaLabel = tooltip(shortcut, formatTitle);
var onClick = function onClick() {
var _api$analytics;
api === null || api === void 0 || api.core.actions.execute(toggleMarkWithAnalyticsCallback(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)(getInputMethodFromParentKeys(parents)));
};
return _objectSpread(_objectSpread({}, formatOptionState), {}, {
formatTitle: formatTitle,
shortcutContent: shortcutContent,
ariaLabel: ariaLabel || formatTitle,
onClick: onClick
});
};
export var formatOptions = function formatOptions() {
return {
strong: {
rank: TEXT_FORMATTING_MENU_SECTION_RANK[BOLD_MENU_ITEM.key],
key: BOLD_MENU_ITEM.key,
icon: BoldIcon,
title: toolbarMessages.bold,
command: toggleStrongWithAnalytics,
shortcut: toggleBold
},
em: {
rank: TEXT_FORMATTING_MENU_SECTION_RANK[ITALIC_MENU_ITEM.key],
key: ITALIC_MENU_ITEM.key,
icon: ItalicIcon,
title: toolbarMessages.italic,
command: toggleEmWithAnalytics,
shortcut: toggleItalic
},
underline: {
rank: TEXT_FORMATTING_MENU_SECTION_RANK[UNDERLINE_MENU_ITEM.key],
key: UNDERLINE_MENU_ITEM.key,
icon: UnderlineIcon,
title: toolbarMessages.underline,
command: toggleUnderlineWithAnalytics,
shortcut: toggleUnderline
},
strike: {
rank: TEXT_FORMATTING_MENU_SECTION_RANK[STRIKE_MENU_ITEM.key],
key: STRIKE_MENU_ITEM.key,
icon: StrikeThroughIcon,
title: toolbarMessages.strike,
command: toggleStrikeWithAnalytics,
shortcut: toggleStrikethrough
},
code: {
rank: TEXT_FORMATTING_MENU_SECTION_RANK[CODE_MENU_ITEM.key],
key: CODE_MENU_ITEM.key,
icon: CodeIcon,
title: toolbarMessages.code,
command: toggleCodeWithAnalytics,
shortcut: toggleCode
},
subscript: {
rank: TEXT_FORMATTING_MENU_SECTION_RANK[SUBSCRIPT_MENU_ITEM.key],
key: SUBSCRIPT_MENU_ITEM.key,
icon: SubscriptIcon,
title: toolbarMessages.subscript,
command: toggleSubscriptWithAnalytics,
shortcut: toggleSubscript
},
superscript: {
rank: TEXT_FORMATTING_MENU_SECTION_RANK[SUPERSCRIPT_MENU_ITEM.key],
key: SUPERSCRIPT_MENU_ITEM.key,
icon: SuperscriptIcon,
title: toolbarMessages.superscript,
command: toggleSuperscriptWithAnalytics,
shortcut: toggleSuperscript
}
};
};