UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

83 lines (80 loc) 3.9 kB
import { canRenderDatasource, hasDocAsParent } from '@atlaskit/editor-common/utils'; import { setProvider } from '../actions'; import { handleFallbackWithAnalytics, replaceQueuedUrlWithCard } from '../doc'; var isFreshlyPastedOnNewLine = function isFreshlyPastedOnNewLine(view) { var selection = view.state.selection; var _ref = selection, $cursor = _ref.$cursor, $anchor = _ref.$anchor; if (!$cursor) { return false; } if (!hasDocAsParent($anchor)) { return false; } var node = $cursor.node(); if (!node) { return false; } if (node.type.name !== 'paragraph') { return false; } return node.childCount === 1; // The pasted blue link itself }; // ============================================================================ // // ============================== PROVIDER UTILS ============================== // // ============================================================================ // // Used for all interactions with the EditorCardProvider. // ============================================================================ // export var resolveWithProvider = function resolveWithProvider(view, provider, request, options, editorAnalyticsApi, createAnalyticsEvent, embedCardNodeTransformer) { var isEmbedFriendlyLocation = isFreshlyPastedOnNewLine(view); // When user manually changes appearance from blue link to smart link, we should respect that, var shouldForceAppearance = // This flag is set to true only in one place atm: // packages/editor/editor-core/src/plugins/card/pm-plugins/doc.ts @ convertHyperlinkToSmartCard // Which is used when user switching from URL to smart link appearance. !!request.shouldReplaceLink; var handleResolve = provider.resolve(request.url, request.appearance, shouldForceAppearance, isEmbedFriendlyLocation).then(handleResolved(view, request, editorAnalyticsApi, createAnalyticsEvent, options, embedCardNodeTransformer), handleRejected(view, request, editorAnalyticsApi)); return handleResolve; }; var updateCardType = function updateCardType(resolvedCard, options) { if (resolvedCard.type === 'blockCard' && 'datasource' in resolvedCard.attrs) { var datasourceId = resolvedCard.attrs.datasource.id; if (!options.allowDatasource || !canRenderDatasource(datasourceId)) { delete resolvedCard.attrs.datasource; resolvedCard.type = 'inlineCard'; return; } } if ((resolvedCard === null || resolvedCard === void 0 ? void 0 : resolvedCard.type) === 'blockCard' && !options.allowBlockCards || (resolvedCard === null || resolvedCard === void 0 ? void 0 : resolvedCard.type) === 'embedCard' && !options.allowEmbeds) { // clean out the 'layout' attr from an embedCard type that should be transformed into the inlineCard type. if (resolvedCard.type === 'embedCard') { // eslint-disable-next-line @typescript-eslint/no-explicit-any delete resolvedCard.attrs.layout; } resolvedCard.type = 'inlineCard'; } }; var handleResolved = function handleResolved(view, request, editorAnalyticsApi, createAnalyticsEvent, options, embedCardNodeTransformer) { return function (resolvedCard) { updateCardType(resolvedCard, options); replaceQueuedUrlWithCard(request.url, resolvedCard, request.analyticsAction, editorAnalyticsApi, createAnalyticsEvent, embedCardNodeTransformer)(view.state, view.dispatch); return resolvedCard; }; }; var handleRejected = function handleRejected(view, request, editorAnalyticsApi) { return function () { handleFallbackWithAnalytics(request, editorAnalyticsApi)(view.state, view.dispatch); }; }; // listen for card provider changes export var handleProvider = function handleProvider(_, provider, view) { if (!provider) { return; } provider.then(function (cardProvider) { var state = view.state, dispatch = view.dispatch; dispatch(setProvider(cardProvider)(state.tr)); }); };