UNPKG

@atlaskit/editor-plugin-synced-block

Version:

SyncedBlock plugin for @atlaskit/editor-core

153 lines 7.95 kB
import React, { useMemo } from 'react'; import { useIntl } from 'react-intl'; import { INPUT_METHOD } from '@atlaskit/editor-common/analytics'; import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks'; import { blockMenuMessages, syncBlockMessages } from '@atlaskit/editor-common/messages'; import { isOfflineMode } from '@atlaskit/editor-plugin-connectivity'; import { SyncBlocksIcon, ToolbarDropdownItem } from '@atlaskit/editor-toolbar'; import Lozenge from '@atlaskit/lozenge'; import { fg } from '@atlaskit/platform-feature-flags'; import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals'; import { canBeConvertedToSyncBlock } from '../pm-plugins/utils/utils'; import { SYNCED_BLOCK_BUTTON_TEST_ID } from '../types'; const SyncedBlockNewLozenge = ({ label }) => /*#__PURE__*/React.createElement(Lozenge, { appearance: fg('confluence_fronend_labels_categorization_migration') ? 'discovery' : 'new' }, label); const CreateSyncedBlockDropdownItem = ({ api }) => { const { formatMessage } = useIntl(); const { selection, menuTriggerByNode, mode } = useSharedPluginStateWithSelector(api, ['selection', 'blockControls', 'connectivity'], states => { var _states$selectionStat, _states$blockControls, _states$blockControls2, _states$connectivityS; return { selection: (_states$selectionStat = states.selectionState) === null || _states$selectionStat === void 0 ? void 0 : _states$selectionStat.selection, menuTriggerByNode: (_states$blockControls = (_states$blockControls2 = states.blockControlsState) === null || _states$blockControls2 === void 0 ? void 0 : _states$blockControls2.menuTriggerByNode) !== null && _states$blockControls !== void 0 ? _states$blockControls : undefined, mode: (_states$connectivityS = states.connectivityState) === null || _states$connectivityS === void 0 ? void 0 : _states$connectivityS.mode }; }); const isNested = menuTriggerByNode && menuTriggerByNode.rootPos !== menuTriggerByNode.pos; const canBeConverted = useMemo(() => selection && canBeConvertedToSyncBlock(selection), [selection]); if (isNested || !canBeConverted) { return null; } const onClick = () => { var _api$core; // Insert the synced block, close the block menu, and stop preserving the // selection — all in a single transaction, mirroring the block-menu delete // item. Stopping preservation is required so the caret that // insertSyncedBlock places inside the new block survives; otherwise // block-controls restores a whole-node NodeSelection over it, hiding the // caret (EDITOR-7949). Then re-focus the editor so the caret is active, // but only when the transaction was actually dispatched — otherwise a // failed insertion would still steal DOM focus into the editor. const dispatched = api === null || api === void 0 ? void 0 : (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.execute(({ tr }) => { var _api$blockControls, _api$blockControls$co, _api$blockControls2, _api$blockControls2$c; // If the insertion fails, bail out without closing the menu or // stopping selection preservation — otherwise we would dispatch an // effectively-empty transaction and close the menu despite nothing // having been inserted. const result = api === null || api === void 0 ? void 0 : api.syncedBlock.commands.insertSyncedBlock(INPUT_METHOD.BLOCK_MENU)({ tr }); if (!result) { return null; } api === null || api === void 0 ? void 0 : (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : (_api$blockControls$co = _api$blockControls.commands) === null || _api$blockControls$co === void 0 ? void 0 : _api$blockControls$co.toggleBlockMenu({ closeMenu: true })({ tr }); api === null || api === void 0 ? void 0 : (_api$blockControls2 = api.blockControls) === null || _api$blockControls2 === void 0 ? void 0 : (_api$blockControls2$c = _api$blockControls2.commands) === null || _api$blockControls2$c === void 0 ? void 0 : _api$blockControls2$c.stopPreservingSelection()({ tr }); return tr; }); if (dispatched) { var _api$core2; api === null || api === void 0 ? void 0 : (_api$core2 = api.core) === null || _api$core2 === void 0 ? void 0 : _api$core2.actions.focus(); } }; const isOffline = isOfflineMode(mode); const lozenge = /*#__PURE__*/React.createElement(SyncedBlockNewLozenge, { label: formatMessage(blockMenuMessages.newLozenge) }); return /*#__PURE__*/React.createElement(ToolbarDropdownItem, { elemBefore: /*#__PURE__*/React.createElement(SyncBlocksIcon, { label: "", size: "small" }), onClick: onClick, isDisabled: isOffline, testId: SYNCED_BLOCK_BUTTON_TEST_ID.blockMenuCreate, elemAfterText: lozenge }, formatMessage(blockMenuMessages.syncBlock)); }; const CopySyncedBlockDropdownItem = ({ api }) => { const { formatMessage } = useIntl(); const { mode } = useSharedPluginStateWithSelector(api, ['connectivity'], states => { var _states$connectivityS2; return { mode: (_states$connectivityS2 = states.connectivityState) === null || _states$connectivityS2 === void 0 ? void 0 : _states$connectivityS2.mode }; }); const onClick = () => { var _api$core3, _api$core4, _api$blockControls3, _api$blockControls3$c; api === null || api === void 0 ? void 0 : (_api$core3 = api.core) === null || _api$core3 === void 0 ? void 0 : _api$core3.actions.execute(api === null || api === void 0 ? void 0 : api.syncedBlock.commands.copySyncedBlockReferenceToClipboard(INPUT_METHOD.BLOCK_MENU)); api === null || api === void 0 ? void 0 : (_api$core4 = api.core) === null || _api$core4 === void 0 ? void 0 : _api$core4.actions.execute(api === null || api === void 0 ? void 0 : (_api$blockControls3 = api.blockControls) === null || _api$blockControls3 === void 0 ? void 0 : (_api$blockControls3$c = _api$blockControls3.commands) === null || _api$blockControls3$c === void 0 ? void 0 : _api$blockControls3$c.toggleBlockMenu({ closeMenu: true })); }; const lozenge = /*#__PURE__*/React.createElement(SyncedBlockNewLozenge, { label: formatMessage(blockMenuMessages.newLozenge) }); const copyLabel = expValEquals('platform_editor_sync_block_activation', 'isEnabled', true) ? syncBlockMessages.copyToSyncLabel : blockMenuMessages.copySyncedBlock; return /*#__PURE__*/React.createElement(ToolbarDropdownItem, { elemBefore: /*#__PURE__*/React.createElement(SyncBlocksIcon, { label: "", size: "small" }), onClick: onClick, isDisabled: isOfflineMode(mode), elemAfterText: lozenge }, formatMessage(copyLabel)); }; export const CreateOrCopySyncedBlockDropdownItem = ({ api, enableSourceSyncedBlockCreation }) => { const { menuTriggerByNode } = useSharedPluginStateWithSelector(api, ['blockControls'], states => { var _states$blockControls3, _states$blockControls4; return { menuTriggerByNode: (_states$blockControls3 = (_states$blockControls4 = states.blockControlsState) === null || _states$blockControls4 === void 0 ? void 0 : _states$blockControls4.menuTriggerByNode) !== null && _states$blockControls3 !== void 0 ? _states$blockControls3 : undefined }; }); if ((menuTriggerByNode === null || menuTriggerByNode === void 0 ? void 0 : menuTriggerByNode.nodeType) === 'syncBlock' || (menuTriggerByNode === null || menuTriggerByNode === void 0 ? void 0 : menuTriggerByNode.nodeType) === 'bodiedSyncBlock') { return /*#__PURE__*/React.createElement(CopySyncedBlockDropdownItem, { api: api }); } else if (enableSourceSyncedBlockCreation) { return /*#__PURE__*/React.createElement(CreateSyncedBlockDropdownItem, { api: api }); } else { return null; } };