UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

116 lines (114 loc) 6.83 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.InlineCardNodeView = InlineCardNodeView; exports.inlineCardNodeView = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _react = _interopRequireWildcard(require("react")); var _hooks = require("@atlaskit/editor-common/hooks"); var _ui = require("@atlaskit/editor-common/ui"); var _editorSmartLinkDraggable = require("@atlaskit/editor-smart-link-draggable"); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _expVal = require("@atlaskit/tmp-editor-statsig/expVal"); var _utils = require("../pm-plugins/utils"); var _SmartCardSSRReactContextsProvider = require("../ui/SmartCardSSRReactContextsProvider"); var _genericCard = require("./genericCard"); var _inlineCardWithAwareness = require("./inlineCardWithAwareness"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } var WrappedInlineCardWithAwareness = (0, _genericCard.Card)(_inlineCardWithAwareness.InlineCardWithAwareness, _ui.UnsupportedInline); var selectorWithCard = function selectorWithCard(states) { var _states$editorViewMod, _states$cardState; return { mode: (_states$editorViewMod = states.editorViewModeState) === null || _states$editorViewMod === void 0 ? void 0 : _states$editorViewMod.mode, resolvedInlineSmartLinks: (_states$cardState = states.cardState) === null || _states$cardState === void 0 ? void 0 : _states$cardState.resolvedInlineSmartLinks }; }; var selectorWithoutCard = function selectorWithoutCard(states) { var _states$editorViewMod2; return { mode: (_states$editorViewMod2 = states.editorViewModeState) === null || _states$editorViewMod2 === void 0 ? void 0 : _states$editorViewMod2.mode, resolvedInlineSmartLinks: undefined }; }; /** * Inline card node view component that renders a Smart Link inline card within the editor. * * @param props * @example */ function InlineCardNodeView(props) { var _resolvedInlineSmartL; var useAlternativePreloader = props.useAlternativePreloader, node = props.node, view = props.view, getPos = props.getPos, actionOptions = props.actionOptions, allowEmbeds = props.allowEmbeds, allowBlockCards = props.allowBlockCards, enableInlineUpgradeFeatures = props.enableInlineUpgradeFeatures, pluginInjectionApi = props.pluginInjectionApi, onClickCallback = props.onClickCallback, isPageSSRed = props.isPageSSRed, provider = props.provider, CompetitorPrompt = props.CompetitorPrompt, intl = props.intl, smartCardContext = props.smartCardContext; var _useSharedPluginState = (0, _hooks.useSharedPluginStateWithSelector)(pluginInjectionApi, (0, _expVal.expVal)('cc_dnd_smart_link_changeboard_platform_css', 'isEnabled', false) && (0, _platformFeatureFlags.fg)('cc_drag_and_drop_smart_link_from_content_to_tree') ? ['editorViewMode', 'card'] : ['editorViewMode'], (0, _expVal.expVal)('cc_dnd_smart_link_changeboard_platform_css', 'isEnabled', false) && (0, _platformFeatureFlags.fg)('cc_drag_and_drop_smart_link_from_content_to_tree') ? selectorWithCard : selectorWithoutCard), mode = _useSharedPluginState.mode, resolvedInlineSmartLinks = _useSharedPluginState.resolvedInlineSmartLinks; var url = node.attrs.url; var CompetitorPromptComponent = CompetitorPrompt && url ? /*#__PURE__*/_react.default.createElement(CompetitorPrompt, { sourceUrl: url, linkType: "inline" }) : null; (0, _react.useEffect)(function () { if ((0, _expValEquals.expValEquals)('platform_editor_smartlink_local_cache', 'isEnabled', true)) { // Refresh cache in the background provider === null || provider === void 0 || provider.then(function (providerInstance) { var _refreshCache, _ref; (_refreshCache = (_ref = providerInstance).refreshCache) === null || _refreshCache === void 0 || _refreshCache.call(_ref, props.node); }); } }, [provider, props.node]); var linkPosition = (0, _react.useMemo)(function () { if (!getPos || typeof getPos === 'boolean') { return undefined; } var pos = getPos(); return typeof pos === 'number' ? pos : undefined; }, [getPos]); var isChangeboardTarget = linkPosition !== undefined && (resolvedInlineSmartLinks === null || resolvedInlineSmartLinks === void 0 || (_resolvedInlineSmartL = resolvedInlineSmartLinks[0]) === null || _resolvedInlineSmartL === void 0 ? void 0 : _resolvedInlineSmartL.pos) === linkPosition; var inlineCardContent = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(WrappedInlineCardWithAwareness, (0, _extends2.default)({ node: node, view: view, getPos: getPos, actionOptions: actionOptions, useAlternativePreloader: useAlternativePreloader, pluginInjectionApi: pluginInjectionApi, onClickCallback: onClickCallback, isPageSSRed: isPageSSRed, provider: provider, appearance: "inline" // Ignored via go/ees005 // eslint-disable-next-line react/jsx-props-no-spreading }, enableInlineUpgradeFeatures && (0, _utils.getAwarenessProps)(view.state, getPos, allowEmbeds, allowBlockCards, mode === 'view'))), CompetitorPromptComponent); return /*#__PURE__*/_react.default.createElement(_SmartCardSSRReactContextsProvider.SmartCardSSRReactContextsProvider, { intl: intl, smartCardContext: smartCardContext }, /*#__PURE__*/_react.default.createElement(_editorSmartLinkDraggable.SmartLinkDraggable, { url: url, appearance: _editorSmartLinkDraggable.SMART_LINK_APPEARANCE.INLINE, source: _editorSmartLinkDraggable.SMART_LINK_DRAG_TYPES.EDITOR, isChangeboardTarget: isChangeboardTarget }, inlineCardContent)); } var inlineCardNodeView = exports.inlineCardNodeView = function inlineCardNodeView(_ref2) { var inlineCardViewProducer = _ref2.inlineCardViewProducer; return function (node, view, getPos, decorations) { return inlineCardViewProducer(node, view, getPos, decorations); }; };