UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

101 lines 3.54 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import { getNativeEmbedUrl, isNativeEmbedNode } from './nativeEmbedNode'; export var DISPLAY_AS_OPTIONS = ['url', 'inline', 'block', 'embed']; var cardAtPos = function cardAtPos(node, pos) { switch (node === null || node === void 0 ? void 0 : node.type.name) { case 'inlineCard': return { appearance: 'inline', pos: pos }; case 'blockCard': return { appearance: 'block', pos: pos }; case 'embedCard': return { appearance: 'embed', pos: pos }; default: return isNativeEmbedNode(node) ? { appearance: 'embed', isNativeEmbed: true, pos: pos } : undefined; } }; var getNodeUrl = function getNodeUrl(node) { var _attrs$url, _attrs$data; if (isNativeEmbedNode(node)) { return getNativeEmbedUrl(node); } var attrs = node.attrs; var url = (_attrs$url = attrs === null || attrs === void 0 ? void 0 : attrs.url) !== null && _attrs$url !== void 0 ? _attrs$url : attrs === null || attrs === void 0 || (_attrs$data = attrs.data) === null || _attrs$data === void 0 ? void 0 : _attrs$data.url; return typeof url === 'string' ? url : undefined; }; /** * Finds the card that a paste produced. * * The paste range is recorded when the paste happens and then mapped through every later * transaction, so by the time the link has resolved into a card the range can point just * past that card. `pastedUrl` resolves the ambiguity: when it is known, a candidate * holding that URL wins over one that merely sits in the range, which stops a pre-existing * neighbouring card from being reported as the pasted one. */ export var getCardAtPasteRange = function getCardAtPasteRange(state, pasteStartPos, pasteEndPos, pastedUrl) { var docContentSize = state.doc.content.size; var clampedStart = Math.max(0, Math.min(pasteStartPos, docContentSize)); var clampedEnd = Math.max(0, Math.min(pasteEndPos, docContentSize)); var from = Math.min(clampedStart, clampedEnd); var to = Math.max(clampedStart, clampedEnd); try { var _inRange; var candidateAt = function candidateAt(pos) { if (pos < 0 || pos >= docContentSize) { return undefined; } var node = state.doc.nodeAt(pos); var card = cardAtPos(node, pos); return node && card ? { card: card, url: getNodeUrl(node) } : undefined; }; var adjacent = [candidateAt(from - 1), candidateAt(from)].filter(function (candidate) { return Boolean(candidate); }); var inRange = []; state.doc.nodesBetween(from, to, function (node, pos) { var card = cardAtPos(node, pos); if (card) { inRange.push({ card: card, url: getNodeUrl(node) }); return false; } return true; }); if (pastedUrl) { var match = [].concat(_toConsumableArray(adjacent), inRange).find(function (candidate) { return candidate.url === pastedUrl; }); if (match) { return match.card; } } if (from === to) { var _adjacent = _slicedToArray(adjacent, 1), firstAdjacent = _adjacent[0]; if (firstAdjacent) { return firstAdjacent.card; } } return (_inRange = inRange[inRange.length - 1]) === null || _inRange === void 0 ? void 0 : _inRange.card; } catch (_unused) { return undefined; } };