UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

66 lines • 3.17 kB
import { preserveNodeView } from '@atlaskit/editor-common/preserve-node-view'; import { akEditorFullPageNarrowBreakout } from '@atlaskit/editor-shared-styles/constants'; import { embedCardNodeView, EmbedCard, isPreviewPanelResponsivenessEnabled } from './embedCard'; export var embedCardNodeViewWithPreservation = function embedCardNodeViewWithPreservation(properties) { return function (node, view, getPos) { var _properties$pluginInj; var width = (_properties$pluginInj = properties.pluginInjectionApi) === null || _properties$pluginInj === void 0 || (_properties$pluginInj = _properties$pluginInj.width) === null || _properties$pluginInj === void 0 || (_properties$pluginInj = _properties$pluginInj.sharedState.currentState()) === null || _properties$pluginInj === void 0 ? void 0 : _properties$pluginInj.width; var useResponsiveRenderer = isPreviewPanelResponsivenessEnabled() && !!width && width <= akEditorFullPageNarrowBreakout; if (useResponsiveRenderer) { return embedCardNodeView(properties)(node, view, getPos); } var _properties$pmPluginF = properties.pmPluginFactoryParams, portalProviderAPI = _properties$pmPluginF.portalProviderAPI, eventDispatcher = _properties$pmPluginF.eventDispatcher, dispatchAnalyticsEvent = _properties$pmPluginF.dispatchAnalyticsEvent; var renderProps = { eventDispatcher: eventDispatcher, allowResizing: properties.allowResizing, fullWidthMode: properties.fullWidthMode, dispatchAnalyticsEvent: dispatchAnalyticsEvent, pluginInjectionApi: properties.pluginInjectionApi, actionOptions: properties.actionOptions, onClickCallback: properties.onClickCallback, CompetitorPrompt: properties.CompetitorPrompt, isPageSSRed: properties.isPageSSRed, provider: properties.provider, intl: properties.intl, smartCardContext: properties.smartCardContext }; var createCard = function createCard(cardNode) { return new EmbedCard(cardNode, view, getPos, portalProviderAPI, eventDispatcher, renderProps); }; return preserveNodeView({ node: node, view: view, getPos: getPos, renderProps: renderProps, rendererKind: portalProviderAPI, createNodeView: createCard, isSameNode: function isSameNode(previous, next) { return previous === next; }, canUpdateNode: function canUpdateNode(previous, next) { return previous.type === next.type; }, canReuse: function canReuse(inner) { if (!(inner instanceof EmbedCard) || !inner.canReuseWith(properties.provider, properties.pmPluginFactoryParams.eventDispatcher)) { return false; } var nodeIdentityCount = 0; view.state.doc.descendants(function (candidate) { if (candidate === inner.node) { nodeIdentityCount++; } }); return nodeIdentityCount === 1; }, onAttach: function onAttach(inner, slot, freshGetPos) { if (inner instanceof EmbedCard) { inner.attachPreservationSlot(slot, freshGetPos); } }, requireUniqueMatch: true }); }; };