UNPKG

@atlaskit/editor-plugin-expand

Version:

Expand plugin for @atlaskit/editor-core

71 lines • 3.71 kB
import React, { useCallback, useMemo } from 'react'; import { useIntl } from 'react-intl'; import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks'; import { toolbarInsertBlockMessages as messages } from '@atlaskit/editor-common/messages'; import { QuickInsertMenuItem } from '@atlaskit/editor-common/quick-insert/menu-item'; import { messages as quickInsertMessages } from '@atlaskit/editor-common/quick-insert/messages'; import { createWrapSelectionTransaction } from '@atlaskit/editor-common/utils'; import ExpandElementIcon from '@atlaskit/icon-lab/core/expand-element'; import { fg } from '@atlaskit/platform-feature-flags/fg'; import { createExpandNode as createLegacyExpandNode } from '../../legacyExpand/commands'; import { createExpandNode as createSinglePlayerExpandNode, wrapSelectionAndSetExpandedState } from '../../singlePlayerExpand/commands'; export var ExpandQuickInsertMenuItem = function ExpandQuickInsertMenuItem(_ref) { var api = _ref.api, isLegacy = _ref.isLegacy, previewImageUrls = _ref.previewImageUrls; var _useIntl = useIntl(), formatMessage = _useIntl.formatMessage; var preview = useMemo(function () { return previewImageUrls ? { image: previewImageUrls, attribution: { name: formatMessage(quickInsertMessages.previewAttributionAtlassian) } } : undefined; }, [formatMessage, previewImageUrls]); var _useSharedPluginState = useSharedPluginStateWithSelector(api, ['editorDisabled', 'editorViewMode'], function (states) { var _states$editorDisable, _states$editorViewMod; return { editorDisabled: (_states$editorDisable = states.editorDisabledState) === null || _states$editorDisable === void 0 ? void 0 : _states$editorDisable.editorDisabled, mode: (_states$editorViewMod = states.editorViewModeState) === null || _states$editorViewMod === void 0 ? void 0 : _states$editorViewMod.mode }; }), editorDisabled = _useSharedPluginState.editorDisabled, mode = _useSharedPluginState.mode; var onSelect = useCallback(function (_ref2) { var _api$analytics; var editorView = _ref2.editorView, insert = _ref2.insert, source = _ref2.source; var state = editorView.state; var node = isLegacy ? createLegacyExpandNode(state) : createSinglePlayerExpandNode(state, undefined, !!(api !== null && api !== void 0 && api.localId)); if (!node) { return false; } var tr = state.selection.empty ? insert(node) : isLegacy || !fg('platform_editor_adf_with_localid') ? createWrapSelectionTransaction({ state: state, type: node.type }) : wrapSelectionAndSetExpandedState(state, node); api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || _api$analytics.actions.attachAnalyticsEvent({ action: ACTION.INSERTED, actionSubject: ACTION_SUBJECT.DOCUMENT, actionSubjectId: node.type === state.schema.nodes.nestedExpand ? ACTION_SUBJECT_ID.NESTED_EXPAND : ACTION_SUBJECT_ID.EXPAND, attributes: { inputMethod: isLegacy ? INPUT_METHOD.QUICK_INSERT : source }, eventType: EVENT_TYPE.TRACK })(tr); return tr; }, [api, isLegacy]); return /*#__PURE__*/React.createElement(QuickInsertMenuItem, { description: formatMessage(messages.expandDescription), iconBefore: /*#__PURE__*/React.createElement(ExpandElementIcon, { label: "" }), isDisabled: editorDisabled || mode === 'view', onSelect: onSelect, preview: preview, title: formatMessage(messages.expand) }); };