UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

59 lines • 2.69 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 const embedCardNodeViewWithPreservation = properties => (node, view, getPos) => { var _properties$pluginInj, _properties$pluginInj2, _properties$pluginInj3; const width = (_properties$pluginInj = properties.pluginInjectionApi) === null || _properties$pluginInj === void 0 ? void 0 : (_properties$pluginInj2 = _properties$pluginInj.width) === null || _properties$pluginInj2 === void 0 ? void 0 : (_properties$pluginInj3 = _properties$pluginInj2.sharedState.currentState()) === null || _properties$pluginInj3 === void 0 ? void 0 : _properties$pluginInj3.width; const useResponsiveRenderer = isPreviewPanelResponsivenessEnabled() && !!width && width <= akEditorFullPageNarrowBreakout; if (useResponsiveRenderer) { return embedCardNodeView(properties)(node, view, getPos); } const { portalProviderAPI, eventDispatcher, dispatchAnalyticsEvent } = properties.pmPluginFactoryParams; const renderProps = { eventDispatcher, allowResizing: properties.allowResizing, fullWidthMode: properties.fullWidthMode, 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 }; const createCard = cardNode => new EmbedCard(cardNode, view, getPos, portalProviderAPI, eventDispatcher, renderProps); return preserveNodeView({ node, view, getPos, renderProps, rendererKind: portalProviderAPI, createNodeView: createCard, isSameNode: (previous, next) => previous === next, canUpdateNode: (previous, next) => previous.type === next.type, canReuse: inner => { if (!(inner instanceof EmbedCard) || !inner.canReuseWith(properties.provider, properties.pmPluginFactoryParams.eventDispatcher)) { return false; } let nodeIdentityCount = 0; view.state.doc.descendants(candidate => { if (candidate === inner.node) { nodeIdentityCount++; } }); return nodeIdentityCount === 1; }, onAttach: (inner, slot, freshGetPos) => { if (inner instanceof EmbedCard) { inner.attachPreservationSlot(slot, freshGetPos); } }, requireUniqueMatch: true }); };