UNPKG

@atlaskit/editor-plugin-synced-block

Version:

SyncedBlock plugin for @atlaskit/editor-core

124 lines 6.44 kB
import React from 'react'; import { syncBlock, bodiedSyncBlock } from '@atlaskit/adf-schema'; import { SyncBlockStoreManager } from '@atlaskit/editor-synced-block-provider'; import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments'; import { flushBodiedSyncBlocks, flushSyncBlocks } from './editor-actions'; import { copySyncedBlockReferenceToClipboardEditorCommand, createSyncedBlock } from './editor-commands'; import { bodiedSyncBlockNodeWithToDOMFixed } from './nodeviews/bodiedSyncBlockNodeWithToDOMFixed'; 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 { SyncBlockRefresher } from './ui/SyncBlockRefresher'; import { getToolbarComponents } from './ui/toolbar-components'; export const syncedBlockPlugin = ({ config, api }) => { var _api$editorViewMode, _api$editorViewMode$s, _api$analytics, _api$analytics$action, _api$blockMenu, _config$enableSourceC, _api$toolbar, _config$enableSourceC2; const refs = {}; const viewMode = api === null || api === void 0 ? void 0 : (_api$editorViewMode = api.editorViewMode) === null || _api$editorViewMode === void 0 ? void 0 : (_api$editorViewMode$s = _api$editorViewMode.sharedState.currentState()) === null || _api$editorViewMode$s === void 0 ? void 0 : _api$editorViewMode$s.mode; const 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 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : (_api$analytics$action = _api$analytics.actions) === null || _api$analytics$action === void 0 ? void 0 : _api$analytics$action.fireAnalyticsEvent); api === null || api === void 0 ? void 0 : (_api$blockMenu = api.blockMenu) === null || _api$blockMenu === void 0 ? 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 ? void 0 : (_api$toolbar = api.toolbar) === null || _api$toolbar === void 0 ? 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)); return { name: 'syncedBlock', nodes() { return [{ name: 'syncBlock', node: syncBlock }, { name: 'bodiedSyncBlock', node: !editorExperiment('platform_synced_block_patch_6', true, { exposure: true }) ? // delete bodiedSyncBlockNodeWithToDOMFixed when cleaning up platform_synced_block_patch_6 bodiedSyncBlockNodeWithToDOMFixed() : bodiedSyncBlock }]; }, pmPlugins() { return [{ name: 'syncedBlockPlugin', plugin: params => createPlugin(config, params, syncBlockStore, api) }, { name: 'menuAndToolbarExperiencesPlugin', plugin: () => getMenuAndToolbarExperiencesPlugin({ refs, dispatchAnalyticsEvent: payload => { var _api$analytics2, _api$analytics2$actio; return api === null || api === void 0 ? void 0 : (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : (_api$analytics2$actio = _api$analytics2.actions) === null || _api$analytics2$actio === void 0 ? void 0 : _api$analytics2$actio.fireAnalyticsEvent(payload); } }) }]; }, commands: { copySyncedBlockReferenceToClipboard: inputMethod => copySyncedBlockReferenceToClipboardEditorCommand(syncBlockStore, inputMethod, api), insertSyncedBlock: () => ({ tr }) => { var _api$analytics3; if (!(config !== null && config !== void 0 && config.enableSourceCreation)) { return null; } return createSyncedBlock({ tr, syncBlockStore, fireAnalyticsEvent: api === null || api === void 0 ? void 0 : (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 ? void 0 : _api$analytics3.actions.fireAnalyticsEvent }) || null; } }, actions: { flushBodiedSyncBlocks: () => { return flushBodiedSyncBlocks(syncBlockStore); }, flushSyncedBlocks: () => { return flushSyncBlocks(syncBlockStore); } }, pluginsOptions: { quickInsert: getQuickInsertConfig(config, api, syncBlockStore), floatingToolbar: (state, intl) => getToolbarConfig(state, intl, api, syncBlockStore) }, contentComponent: ({ containerElement, wrapperElement, popupsMountPoint }) => { refs.containerElement = containerElement || undefined; refs.popupsMountPoint = popupsMountPoint || undefined; refs.wrapperElement = wrapperElement || undefined; return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SyncBlockRefresher, { syncBlockStoreManager: syncBlockStore, api: api }), /*#__PURE__*/React.createElement(DeleteConfirmationModal, { syncBlockStoreManager: syncBlockStore, api: api }), /*#__PURE__*/React.createElement(Flag, { api: api })); }, getSharedState: editorState => { if (!editorState) { return; } const { activeFlag, syncBlockStore: currentSyncBlockStore, bodiedSyncBlockDeletionStatus, retryCreationPosMap, hasUnsavedBodiedSyncBlockChanges } = syncedBlockPluginKey.getState(editorState); return { activeFlag, syncBlockStore: currentSyncBlockStore, bodiedSyncBlockDeletionStatus, retryCreationPosMap, hasUnsavedBodiedSyncBlockChanges }; } }; };