UNPKG

@atlaskit/editor-plugin-text-formatting

Version:

Text-formatting plugin for @atlaskit/editor-core

256 lines (251 loc) 13.4 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useComponentInfo = exports.formatOptions = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _reactIntl = require("react-intl"); var _analytics = require("@atlaskit/editor-common/analytics"); var _hooks = require("@atlaskit/editor-common/hooks"); var _keymaps = require("@atlaskit/editor-common/keymaps"); var _messages = require("@atlaskit/editor-common/messages"); var _toolbar = require("@atlaskit/editor-common/toolbar"); var _editorToolbar = require("@atlaskit/editor-toolbar"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _expValEqualsNoExposure = require("@atlaskit/tmp-editor-statsig/exp-val-equals-no-exposure"); var _toggleMark = require("../../../editor-commands/toggle-mark"); 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) { (0, _defineProperty2.default)(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; } /** * Local copy of `@atlassian/editor-plugin-markdown-mode/source-view-utils`'s * `SourceViewInlineFormat` union. Kept here so this `@atlaskit/`-published * package doesn't depend on the `@atlassian/`-namespaced source-view-utils * subpath for type resolution. Must stay in sync with the source-of-truth * declaration in markdown-mode. */ /** * Maps the text-formatting plugin's `FormatOptions` keys to their * `SourceViewInlineFormat` equivalents for the CM6 source view. * Only formats that have a markdown syntax equivalent are included — * underline, subscript, and superscript have no GFM equivalent and * are left as undefined (they remain PM-only). */ var FORMAT_OPTION_TO_SOURCE_VIEW = { strong: 'bold', em: 'italic', code: 'inlineCode', strike: 'strikethrough' }; var SOURCE_VIEW_ANALYTICS_ACTION_SUBJECT_ID = { bold: _analytics.ACTION_SUBJECT_ID.FORMAT_STRONG, italic: _analytics.ACTION_SUBJECT_ID.FORMAT_ITALIC, inlineCode: _analytics.ACTION_SUBJECT_ID.FORMAT_CODE, strikethrough: _analytics.ACTION_SUBJECT_ID.FORMAT_STRIKE }; var TEXT_FORMATTING_PLUGIN_STATE_KEYS = ['textFormatting']; var TEXT_FORMATTING_MARKDOWN_PLUGIN_STATE_KEYS = ['textFormatting', 'markdownMode']; var FormatMarkSchema = { strong: 'strong', em: 'em', underline: 'underline', strike: 'strike', code: 'code', subscript: 'subsup', superscript: 'subsup' }; var useComponentInfo = exports.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 isMarkdownBridgeEnabled = (0, _expValEqualsNoExposure.expValEqualsNoExposure)('cc-markdown-mode', 'isEnabled', true) && (0, _platformFeatureFlags.fg)('platform_editor_markdown_compatible_toolbar'); var pluginStateKeys = isMarkdownBridgeEnabled ? TEXT_FORMATTING_MARKDOWN_PLUGIN_STATE_KEYS : TEXT_FORMATTING_PLUGIN_STATE_KEYS; var _useSharedPluginState = (0, _hooks.useSharedPluginStateWithSelector)(api, pluginStateKeys, function (states) { var _states$textFormattin, _states$textFormattin2, _states$textFormattin3, _states$textFormattin4, _states$markdownModeS, _states$markdownModeS2; 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, markdownModeView: isMarkdownBridgeEnabled ? (_states$markdownModeS = states.markdownModeState) === null || _states$markdownModeS === void 0 ? void 0 : _states$markdownModeS.view : undefined, sourceFormatState: isMarkdownBridgeEnabled ? (_states$markdownModeS2 = states.markdownModeState) === null || _states$markdownModeS2 === void 0 ? void 0 : _states$markdownModeS2.sourceInlineFormatState : null }; }), isActive = _useSharedPluginState.isActive, isDisabled = _useSharedPluginState.isDisabled, isHidden = _useSharedPluginState.isHidden, isPluginInitialised = _useSharedPluginState.isPluginInitialised, markdownModeView = _useSharedPluginState.markdownModeView, sourceFormatState = _useSharedPluginState.sourceFormatState; 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 = (0, _reactIntl.useIntl)(), formatMessage = _useIntl.formatMessage; var formatTitle = formatMessage(title); var shortcutContent = (0, _keymaps.tooltip)(shortcut); var ariaLabel = (0, _keymaps.tooltip)(shortcut, formatTitle); // The SourceViewInlineFormat key for this format option, if it has one. // undefined for underline, subscript, superscript (no GFM equivalent). var sourceViewFormat = FORMAT_OPTION_TO_SOURCE_VIEW[optionType]; // The CM6 source view is authoritative when the user has switched to it. // We rely on `markdownModeState.view === 'syntax'` (synchronous, set on // `setView`) rather than the presence of `sourceFormatState` — the latter // is `null` until the CM6 update listener fires for the first time, so // using it as the sentinel would let the first toolbar click after entering // source view fall through to the PM path and mutate the hidden PM doc. var isInSourceView = markdownModeView === 'syntax'; if (isInSourceView && !sourceViewFormat) { // Underline, subscript, superscript have no GFM equivalent. Disable // them immediately on entering source view — independent of whether // the CM6 update listener has fired yet — so the buttons don't briefly // appear active/enabled during that race window. formatOptionState = { isActive: false, isDisabled: true, isHidden: false }; } else if (isInSourceView && sourceFormatState && sourceViewFormat) { // Map each SourceViewInlineFormat to its isActive field on the snapshot. var activeByFormat = { bold: sourceFormatState.boldActive, italic: sourceFormatState.italicActive, inlineCode: sourceFormatState.codeActive, strikethrough: sourceFormatState.strikeActive }; formatOptionState = { isActive: activeByFormat[sourceViewFormat], isDisabled: !sourceFormatState.inlineFormattingAvailable, isHidden: false }; } else if (isInSourceView) { // Race window: the user has switched to source view but the CM6 update // listener hasn't pushed its first format state snapshot yet // (`sourceFormatState` is still `null`). Without this branch the // button would render stale PM-cursor state (e.g. "Bold active" based // on where the PM selection was) until CM6 catches up. Render a // neutral, non-disabled state instead. formatOptionState = { isActive: false, isDisabled: false, isHidden: false }; } var onClick = function onClick() { var _api$markdownMode, _api$markdownMode2, _api$markdownMode3, _api$markdownMode4, _api$analytics2; // Route to CM6 when in source view and the format has a CM6 equivalent. if (isInSourceView) { if (sourceViewFormat) { var _api$analytics; api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || _api$analytics.actions.fireAnalyticsEvent({ action: _analytics.ACTION.FORMATTED, actionSubject: _analytics.ACTION_SUBJECT.TEXT, eventType: _analytics.EVENT_TYPE.TRACK, actionSubjectId: SOURCE_VIEW_ANALYTICS_ACTION_SUBJECT_ID[sourceViewFormat], attributes: { inputMethod: (0, _toolbar.getInputMethodFromParentKeys)(parents) } }); switch (sourceViewFormat) { case 'bold': api === null || api === void 0 || (_api$markdownMode = api.markdownMode) === null || _api$markdownMode === void 0 || _api$markdownMode.actions.toggleSourceBold(); break; case 'italic': api === null || api === void 0 || (_api$markdownMode2 = api.markdownMode) === null || _api$markdownMode2 === void 0 || _api$markdownMode2.actions.toggleSourceItalic(); break; case 'inlineCode': api === null || api === void 0 || (_api$markdownMode3 = api.markdownMode) === null || _api$markdownMode3 === void 0 || _api$markdownMode3.actions.toggleSourceInlineCode(); break; case 'strikethrough': api === null || api === void 0 || (_api$markdownMode4 = api.markdownMode) === null || _api$markdownMode4 === void 0 || _api$markdownMode4.actions.toggleSourceStrike(); break; } } return; } // Default PM path. api === null || api === void 0 || api.core.actions.execute(toggleMarkWithAnalyticsCallback(api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions)((0, _toolbar.getInputMethodFromParentKeys)(parents))); }; return _objectSpread(_objectSpread({}, formatOptionState), {}, { formatTitle: formatTitle, shortcutContent: shortcutContent, ariaLabel: ariaLabel || formatTitle, onClick: onClick }); }; var formatOptions = exports.formatOptions = function formatOptions() { return { strong: { rank: _toolbar.TEXT_FORMATTING_MENU_SECTION_RANK[_toolbar.BOLD_MENU_ITEM.key], key: _toolbar.BOLD_MENU_ITEM.key, icon: _editorToolbar.BoldIcon, title: _messages.toolbarMessages.bold, command: _toggleMark.toggleStrongWithAnalytics, shortcut: _keymaps.toggleBold }, em: { rank: _toolbar.TEXT_FORMATTING_MENU_SECTION_RANK[_toolbar.ITALIC_MENU_ITEM.key], key: _toolbar.ITALIC_MENU_ITEM.key, icon: _editorToolbar.ItalicIcon, title: _messages.toolbarMessages.italic, command: _toggleMark.toggleEmWithAnalytics, shortcut: _keymaps.toggleItalic }, underline: { rank: _toolbar.TEXT_FORMATTING_MENU_SECTION_RANK[_toolbar.UNDERLINE_MENU_ITEM.key], key: _toolbar.UNDERLINE_MENU_ITEM.key, icon: _editorToolbar.UnderlineIcon, title: _messages.toolbarMessages.underline, command: _toggleMark.toggleUnderlineWithAnalytics, shortcut: _keymaps.toggleUnderline }, strike: { rank: _toolbar.TEXT_FORMATTING_MENU_SECTION_RANK[_toolbar.STRIKE_MENU_ITEM.key], key: _toolbar.STRIKE_MENU_ITEM.key, icon: _editorToolbar.StrikeThroughIcon, title: _messages.toolbarMessages.strike, command: _toggleMark.toggleStrikeWithAnalytics, shortcut: _keymaps.toggleStrikethrough }, code: { rank: _toolbar.TEXT_FORMATTING_MENU_SECTION_RANK[_toolbar.CODE_MENU_ITEM.key], key: _toolbar.CODE_MENU_ITEM.key, icon: _editorToolbar.CodeIcon, title: _messages.toolbarMessages.code, command: _toggleMark.toggleCodeWithAnalytics, shortcut: _keymaps.toggleCode }, subscript: { rank: _toolbar.TEXT_FORMATTING_MENU_SECTION_RANK[_toolbar.SUBSCRIPT_MENU_ITEM.key], key: _toolbar.SUBSCRIPT_MENU_ITEM.key, icon: _editorToolbar.SubscriptIcon, title: _messages.toolbarMessages.subscript, command: _toggleMark.toggleSubscriptWithAnalytics, shortcut: _keymaps.toggleSubscript }, superscript: { rank: _toolbar.TEXT_FORMATTING_MENU_SECTION_RANK[_toolbar.SUPERSCRIPT_MENU_ITEM.key], key: _toolbar.SUPERSCRIPT_MENU_ITEM.key, icon: _editorToolbar.SuperscriptIcon, title: _messages.toolbarMessages.superscript, command: _toggleMark.toggleSuperscriptWithAnalytics, shortcut: _keymaps.toggleSuperscript } }; };