UNPKG

@atlaskit/editor-plugin-expand

Version:

Expand plugin for @atlaskit/editor-core

75 lines • 3.42 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 const ExpandQuickInsertMenuItem = ({ api, isLegacy, previewImageUrls }) => { const { formatMessage } = useIntl(); const preview = useMemo(() => previewImageUrls ? { image: previewImageUrls, attribution: { name: formatMessage(quickInsertMessages.previewAttributionAtlassian) } } : undefined, [formatMessage, previewImageUrls]); const { editorDisabled, mode } = useSharedPluginStateWithSelector(api, ['editorDisabled', 'editorViewMode'], 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 }; }); const onSelect = useCallback(({ editorView, insert, source }) => { var _api$analytics; const { state } = editorView; const node = isLegacy ? createLegacyExpandNode(state) : createSinglePlayerExpandNode(state, undefined, !!(api !== null && api !== void 0 && api.localId)); if (!node) { return false; } const tr = state.selection.empty ? insert(node) : isLegacy || !fg('platform_editor_adf_with_localid') ? createWrapSelectionTransaction({ state, type: node.type }) : wrapSelectionAndSetExpandedState(state, node); api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? 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) }); };