UNPKG

@atlaskit/editor-plugin-synced-block

Version:

SyncedBlock plugin for @atlaskit/editor-core

59 lines • 3 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 var SyncedBlockQuickInsertMenuItem = function SyncedBlockQuickInsertMenuItem(_ref) { var api = _ref.api, previewImageUrls = _ref.previewImageUrls, syncBlockStore = _ref.syncBlockStore; 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'], function (states) { var _states$editorDisable; return { editorDisabled: (_states$editorDisable = states.editorDisabledState) === null || _states$editorDisable === void 0 ? void 0 : _states$editorDisable.editorDisabled }; }), editorDisabled = _useSharedPluginState.editorDisabled; var onSelect = useCallback(function (_ref2) { var _api$analytics; var editorView = _ref2.editorView, insert = _ref2.insert, source = _ref2.source; return createSyncedBlock({ tr: editorView.state.tr, syncBlockStore: syncBlockStore, typeAheadInsert: insert, fireAnalyticsEvent: api === null || api === 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) }); };