@atlaskit/editor-plugin-synced-block
Version:
SyncedBlock plugin for @atlaskit/editor-core
147 lines • 8.4 kB
JavaScript
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';
var SyncedBlockNewLozenge = function SyncedBlockNewLozenge(_ref) {
var label = _ref.label;
return /*#__PURE__*/React.createElement(Lozenge, {
appearance: fg('confluence_fronend_labels_categorization_migration') ? 'discovery' : 'new'
}, label);
};
var CreateSyncedBlockDropdownItem = function CreateSyncedBlockDropdownItem(_ref2) {
var api = _ref2.api;
var _useIntl = useIntl(),
formatMessage = _useIntl.formatMessage;
var _useSharedPluginState = useSharedPluginStateWithSelector(api, ['selection', 'blockControls', 'connectivity'], function (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
};
}),
selection = _useSharedPluginState.selection,
menuTriggerByNode = _useSharedPluginState.menuTriggerByNode,
mode = _useSharedPluginState.mode;
var isNested = menuTriggerByNode && menuTriggerByNode.rootPos !== menuTriggerByNode.pos;
var canBeConverted = useMemo(function () {
return selection && canBeConvertedToSyncBlock(selection);
}, [selection]);
if (isNested || !canBeConverted) {
return null;
}
var onClick = function 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.
var dispatched = api === null || api === void 0 || (_api$core = api.core) === null || _api$core === void 0 ? void 0 : _api$core.actions.execute(function (_ref3) {
var _api$blockControls, _api$blockControls2;
var tr = _ref3.tr;
// 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.
var result = api === null || api === void 0 ? void 0 : api.syncedBlock.commands.insertSyncedBlock(INPUT_METHOD.BLOCK_MENU)({
tr: tr
});
if (!result) {
return null;
}
api === null || api === void 0 || (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 || (_api$blockControls = _api$blockControls.commands) === null || _api$blockControls === void 0 || _api$blockControls.toggleBlockMenu({
closeMenu: true
})({
tr: tr
});
api === null || api === void 0 || (_api$blockControls2 = api.blockControls) === null || _api$blockControls2 === void 0 || (_api$blockControls2 = _api$blockControls2.commands) === null || _api$blockControls2 === void 0 || _api$blockControls2.stopPreservingSelection()({
tr: tr
});
return tr;
});
if (dispatched) {
var _api$core2;
api === null || api === void 0 || (_api$core2 = api.core) === null || _api$core2 === void 0 || _api$core2.actions.focus();
}
};
var isOffline = isOfflineMode(mode);
var 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));
};
var CopySyncedBlockDropdownItem = function CopySyncedBlockDropdownItem(_ref4) {
var api = _ref4.api;
var _useIntl2 = useIntl(),
formatMessage = _useIntl2.formatMessage;
var _useSharedPluginState2 = useSharedPluginStateWithSelector(api, ['connectivity'], function (states) {
var _states$connectivityS2;
return {
mode: (_states$connectivityS2 = states.connectivityState) === null || _states$connectivityS2 === void 0 ? void 0 : _states$connectivityS2.mode
};
}),
mode = _useSharedPluginState2.mode;
var onClick = function onClick() {
var _api$core3, _api$core4, _api$blockControls3;
api === null || api === void 0 || (_api$core3 = api.core) === null || _api$core3 === 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 || (_api$core4 = api.core) === null || _api$core4 === void 0 || _api$core4.actions.execute(api === null || api === void 0 || (_api$blockControls3 = api.blockControls) === null || _api$blockControls3 === void 0 || (_api$blockControls3 = _api$blockControls3.commands) === null || _api$blockControls3 === void 0 ? void 0 : _api$blockControls3.toggleBlockMenu({
closeMenu: true
}));
};
var lozenge = /*#__PURE__*/React.createElement(SyncedBlockNewLozenge, {
label: formatMessage(blockMenuMessages.newLozenge)
});
var 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 var CreateOrCopySyncedBlockDropdownItem = function CreateOrCopySyncedBlockDropdownItem(_ref5) {
var api = _ref5.api,
enableSourceSyncedBlockCreation = _ref5.enableSourceSyncedBlockCreation;
var _useSharedPluginState3 = useSharedPluginStateWithSelector(api, ['blockControls'], function (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
};
}),
menuTriggerByNode = _useSharedPluginState3.menuTriggerByNode;
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;
}
};