@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
108 lines (107 loc) • 3.94 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getCardAtPasteRange = exports.DISPLAY_AS_OPTIONS = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _nativeEmbedNode = require("./nativeEmbedNode");
var DISPLAY_AS_OPTIONS = exports.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 (0, _nativeEmbedNode.isNativeEmbedNode)(node) ? {
appearance: 'embed',
isNativeEmbed: true,
pos: pos
} : undefined;
}
};
var getNodeUrl = function getNodeUrl(node) {
var _attrs$url, _attrs$data;
if ((0, _nativeEmbedNode.isNativeEmbedNode)(node)) {
return (0, _nativeEmbedNode.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.
*/
var getCardAtPasteRange = exports.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((0, _toConsumableArray2.default)(adjacent), inRange).find(function (candidate) {
return candidate.url === pastedUrl;
});
if (match) {
return match.card;
}
}
if (from === to) {
var _adjacent = (0, _slicedToArray2.default)(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;
}
};