UNPKG

@atlaskit/editor-plugin-synced-block

Version:

SyncedBlock plugin for @atlaskit/editor-core

61 lines • 2.69 kB
import React, { useCallback, useMemo } from 'react'; import { useIntl } from 'react-intl'; import { INPUT_METHOD } from '@atlaskit/editor-common/analytics/types/enums'; import { messages as blockTypeMessages } from '@atlaskit/editor-common/messages/block-type'; import { QuickInsertMenuItem } from '@atlaskit/editor-common/quick-insert/menu-item'; import { messages as quickInsertMessages } from '@atlaskit/editor-common/quick-insert/messages'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/useSharedPluginStateWithSelector'; import BlockSyncedIcon from '@atlaskit/icon-lab/core/block-synced'; import Lozenge from '@atlaskit/lozenge/new/lozenge'; import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled'; import { createSyncedBlock } from '../../editor-commands'; export const SyncedBlockQuickInsertMenuItem = ({ api, previewImageUrls, syncBlockStore }) => { const { formatMessage } = useIntl(); const preview = useMemo(() => previewImageUrls ? { image: previewImageUrls, attribution: { name: formatMessage(quickInsertMessages.previewAttributionAtlassian) } } : undefined, [formatMessage, previewImageUrls]); const { editorDisabled } = useSharedPluginStateWithSelector(api, ['editorDisabled'], states => { var _states$editorDisable; return { editorDisabled: (_states$editorDisable = states.editorDisabledState) === null || _states$editorDisable === void 0 ? void 0 : _states$editorDisable.editorDisabled }; }); const onSelect = useCallback(({ editorView, insert, source }) => { var _api$analytics; return createSyncedBlock({ tr: editorView.state.tr, syncBlockStore, typeAheadInsert: insert, fireAnalyticsEvent: api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions.fireAnalyticsEvent, inputMethod: source !== null && source !== void 0 ? source : INPUT_METHOD.QUICK_INSERT }); }, [api, syncBlockStore]); return /*#__PURE__*/React.createElement(QuickInsertMenuItem, { description: formatMessage(blockTypeMessages.syncedBlockDescription), iconBefore: /*#__PURE__*/React.createElement(BlockSyncedIcon, { label: "" }), isDisabled: editorDisabled, lozenge: !isExperimentEnabled('platform_editor_synced_block_remove_new_tag') ? /*#__PURE__*/React.createElement(Lozenge, { appearance: "new" }, formatMessage(blockTypeMessages.newLozenge)) : undefined, onSelect: onSelect, preview: preview, title: formatMessage(blockTypeMessages.syncedBlockQuickInsertTitle) }); };