@atlaskit/editor-plugin-synced-block
Version:
SyncedBlock plugin for @atlaskit/editor-core
208 lines (205 loc) • 12.5 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import { syncBlock, bodiedSyncBlock } from '@atlaskit/adf-schema';
import { useSharedPluginStateWithSelector } from '@atlaskit/editor-common/hooks';
import { SyncBlockStoreManager } from '@atlaskit/editor-synced-block-provider';
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { flushBodiedSyncBlocks as _flushBodiedSyncBlocks, flushSyncBlocks, discardUnpublishedSyncBlocks as _discardUnpublishedSyncBlocks } from './editor-actions';
import { copySyncedBlockReferenceToClipboardEditorCommand, createSyncedBlock } from './editor-commands';
import { createPlugin, syncedBlockPluginKey } from './pm-plugins/main';
import { getMenuAndToolbarExperiencesPlugin } from './pm-plugins/menu-and-toolbar-experiences';
import { getBlockMenuComponents } from './ui/block-menu-components';
import { DeleteConfirmationModal } from './ui/DeleteConfirmationModal';
import { Flag } from './ui/Flag';
import { getToolbarConfig } from './ui/floating-toolbar';
import { getQuickInsertConfig } from './ui/quick-insert';
import { getSyncedBlockQuickInsertComponents } from './ui/quick-insert/getSyncedBlockQuickInsertComponents';
import { SourceSyncBlockPlaceholder } from './ui/SourceSyncBlockPlaceholder';
import { SyncBlockRefresher } from './ui/SyncBlockRefresher';
import { getToolbarComponents } from './ui/toolbar-components';
/**
* EDITOR-6929 / PR-G: Guard contentComponent rendering.
* When `hasSyncedBlocks` is false return null
* to avoid mounting SyncBlockRefresher, DeleteConfirmationModal, and Flag —
* their hooks (useSharedPluginStateWithSelector) would execute selectors on
* every transaction for no benefit on the ~99.98% of pages with zero synced
* blocks.
*/
var LazySyncedBlockUI = function LazySyncedBlockUI(_ref) {
var syncBlockStoreManager = _ref.syncBlockStore,
api = _ref.api,
editorView = _ref.editorView,
onFeedbackPromptShown = _ref.onFeedbackPromptShown,
onGiveFeedback = _ref.onGiveFeedback;
var hasSyncBlocks = useSharedPluginStateWithSelector(api, ['syncedBlock'], function (states) {
var _states$syncedBlockSt;
return (_states$syncedBlockSt = states.syncedBlockState) === null || _states$syncedBlockSt === void 0 ? void 0 : _states$syncedBlockSt.hasSyncedBlocks;
});
if (!hasSyncBlocks) {
return null;
}
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SyncBlockRefresher, {
syncBlockStoreManager: syncBlockStoreManager,
api: api
}), /*#__PURE__*/React.createElement(DeleteConfirmationModal, {
syncBlockStoreManager: syncBlockStoreManager,
api: api,
editorView: editorView
}), /*#__PURE__*/React.createElement(Flag, {
api: api,
onFeedbackPromptShown: onFeedbackPromptShown,
onGiveFeedback: onGiveFeedback
}));
};
export var syncedBlockPlugin = function syncedBlockPlugin(_ref2) {
var _api$editorViewMode, _api$analytics, _api$blockMenu, _config$enableSourceC, _api$toolbar, _config$enableSourceC2;
var config = _ref2.config,
api = _ref2.api;
var refs = {};
var viewMode = api === null || api === void 0 || (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 || (_api$editorViewMode = _api$editorViewMode.sharedState.currentState()) === null || _api$editorViewMode === void 0 ? void 0 : _api$editorViewMode.mode;
var syncBlockStore = new SyncBlockStoreManager(config === null || config === void 0 ? void 0 : config.syncBlockDataProvider, viewMode, config === null || config === void 0 ? void 0 : config.__livePage);
syncBlockStore.setFireAnalyticsEvent(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || (_api$analytics = _api$analytics.actions) === null || _api$analytics === void 0 ? void 0 : _api$analytics.fireAnalyticsEvent);
// --- Memoized getSharedState (EDITOR-6929 / PR-F) ---
// Cache the last returned shared state object. On each call, perform a
// shallow comparison of all fields against the cached value. If nothing
// changed, return the cached reference so SharedStateAPI subscribers
// (React components) skip re-rendering.
var cachedSharedState;
api === null || api === void 0 || (_api$blockMenu = api.blockMenu) === null || _api$blockMenu === void 0 || _api$blockMenu.actions.registerBlockMenuComponents(getBlockMenuComponents(api, (_config$enableSourceC = config === null || config === void 0 ? void 0 : config.enableSourceCreation) !== null && _config$enableSourceC !== void 0 ? _config$enableSourceC : false));
api === null || api === void 0 || (_api$toolbar = api.toolbar) === null || _api$toolbar === void 0 || _api$toolbar.actions.registerComponents(getToolbarComponents(api, (_config$enableSourceC2 = config === null || config === void 0 ? void 0 : config.enableSourceCreation) !== null && _config$enableSourceC2 !== void 0 ? _config$enableSourceC2 : false));
var isRegisteredSlashCommandEnabled = isExperimentEnabled('platform_editor_slash_command');
if (isRegisteredSlashCommandEnabled && config !== null && config !== void 0 && config.enableSourceCreation) {
var _api$uiControlRegistr;
api === null || api === void 0 || (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 || _api$uiControlRegistr.actions.register(getSyncedBlockQuickInsertComponents({
api: api,
syncBlockStore: syncBlockStore
}));
}
return {
name: 'syncedBlock',
nodes: function nodes() {
return [{
name: 'syncBlock',
node: syncBlock
}, {
name: 'bodiedSyncBlock',
node: bodiedSyncBlock
}];
},
pmPlugins: function pmPlugins() {
return [{
name: 'syncedBlockPlugin',
plugin: function plugin(params) {
return createPlugin(config, params, syncBlockStore, api);
}
}, {
name: 'menuAndToolbarExperiencesPlugin',
plugin: function plugin() {
return getMenuAndToolbarExperiencesPlugin({
refs: refs,
dispatchAnalyticsEvent: function dispatchAnalyticsEvent(payload) {
var _api$analytics2;
return api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 || (_api$analytics2 = _api$analytics2.actions) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.fireAnalyticsEvent(payload);
}
});
}
}];
},
commands: {
copySyncedBlockReferenceToClipboard: function copySyncedBlockReferenceToClipboard(inputMethod) {
return copySyncedBlockReferenceToClipboardEditorCommand(syncBlockStore, inputMethod, api, config === null || config === void 0 ? void 0 : config.__livePage);
},
insertSyncedBlock: function insertSyncedBlock(inputMethod) {
return function (_ref3) {
var _api$analytics3;
var tr = _ref3.tr;
if (!(config !== null && config !== void 0 && config.enableSourceCreation)) {
return null;
}
return createSyncedBlock({
tr: tr,
syncBlockStore: syncBlockStore,
fireAnalyticsEvent: api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 ? void 0 : _api$analytics3.actions.fireAnalyticsEvent,
inputMethod: inputMethod
}) || null;
};
}
},
actions: {
flushBodiedSyncBlocks: function flushBodiedSyncBlocks() {
return _flushBodiedSyncBlocks(syncBlockStore);
},
flushSyncedBlocks: function flushSyncedBlocks() {
return flushSyncBlocks(syncBlockStore);
},
discardUnpublishedSyncBlocks: function discardUnpublishedSyncBlocks() {
return _discardUnpublishedSyncBlocks(syncBlockStore);
}
},
pluginsOptions: _objectSpread(_objectSpread({}, !isRegisteredSlashCommandEnabled && {
quickInsert: getQuickInsertConfig(config, api, syncBlockStore)
}), {}, {
floatingToolbar: function floatingToolbar(state, intl) {
var _syncedBlockPluginKey;
// When registered slash-command support is on and the document has no synced blocks,
// skip the toolbar config entirely to avoid the per-selection-change
// cost of findSyncBlockOrBodiedSyncBlock (EDITOR-6931).
if (!((_syncedBlockPluginKey = syncedBlockPluginKey.getState(state)) !== null && _syncedBlockPluginKey !== void 0 && _syncedBlockPluginKey.hasSyncedBlocks)) {
return undefined;
}
return getToolbarConfig(state, intl, api, syncBlockStore, config === null || config === void 0 ? void 0 : config.__livePage, config === null || config === void 0 ? void 0 : config.onGiveFeedback);
}
}),
contentComponent: function contentComponent(_ref4) {
var containerElement = _ref4.containerElement,
wrapperElement = _ref4.wrapperElement,
popupsMountPoint = _ref4.popupsMountPoint,
editorView = _ref4.editorView;
refs.containerElement = containerElement || undefined;
refs.popupsMountPoint = popupsMountPoint || undefined;
refs.wrapperElement = wrapperElement || undefined;
return /*#__PURE__*/React.createElement(React.Fragment, null, expValEquals('platform_editor_sync_block_activation', 'isEnabled', true) && /*#__PURE__*/React.createElement(SourceSyncBlockPlaceholder, null), /*#__PURE__*/React.createElement(LazySyncedBlockUI, {
syncBlockStore: syncBlockStore,
api: api,
editorView: editorView,
onFeedbackPromptShown: config === null || config === void 0 ? void 0 : config.onFeedbackPromptShown,
onGiveFeedback: config === null || config === void 0 ? void 0 : config.onGiveFeedback
}));
},
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return;
}
var pluginState = syncedBlockPluginKey.getState(editorState);
var activeFlag = pluginState.activeFlag,
currentSyncBlockStore = pluginState.syncBlockStore,
bodiedSyncBlockDeletionStatus = pluginState.bodiedSyncBlockDeletionStatus,
retryCreationPosMap = pluginState.retryCreationPosMap,
hasSyncedBlocks = pluginState.hasSyncedBlocks,
hasUnsavedBodiedSyncBlockChanges = pluginState.hasUnsavedBodiedSyncBlockChanges;
// --- EDITOR-6929 / PR-F: return a stable reference when all
// fields are unchanged to prevent unnecessary React re-renders. ---
if (cachedSharedState !== undefined && cachedSharedState.activeFlag === activeFlag && cachedSharedState.syncBlockStore === currentSyncBlockStore && cachedSharedState.bodiedSyncBlockDeletionStatus === bodiedSyncBlockDeletionStatus && cachedSharedState.retryCreationPosMap === retryCreationPosMap && cachedSharedState.hasSyncedBlocks === hasSyncedBlocks && cachedSharedState.hasUnsavedBodiedSyncBlockChanges === hasUnsavedBodiedSyncBlockChanges) {
return cachedSharedState;
}
var nextSharedState = {
activeFlag: activeFlag,
syncBlockStore: currentSyncBlockStore,
bodiedSyncBlockDeletionStatus: bodiedSyncBlockDeletionStatus,
retryCreationPosMap: retryCreationPosMap,
hasSyncedBlocks: hasSyncedBlocks,
hasUnsavedBodiedSyncBlockChanges: hasUnsavedBodiedSyncBlockChanges
};
cachedSharedState = nextSharedState;
return nextSharedState;
},
// Destroy the SyncBlockStoreManager on editor unmount to cancel
// pending timers, subscriptions, and in-flight fetches.
destroy: function destroy() {
syncBlockStore.destroy();
}
};
};