@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
176 lines (171 loc) • 7.87 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
/**
* Bounds the per-editor work, since every tracked link is mapped through every transaction. The
* first pasted links win. Sized well above a typical paste; overflow is counted, not silent.
*/
export var MAX_PASTED_INLINE_LINKS = 100;
var isSameLink = function isSameLink(a, b) {
return a.pos === b.pos && a.url === b.url;
};
/**
* Map candidates through a transaction. Anything that was deleted, replaced by another node or
* given a different URL stops being a candidate. Returns the same array when nothing changed.
*/
export var mapPastedInlineLinks = function mapPastedInlineLinks(links, tr) {
if (tr.getMeta('replaceDocument')) {
return links.length ? [] : links;
}
if (!tr.docChanged || !links.length) {
return links;
}
var changed = false;
var mapped = [];
var _iterator = _createForOfIteratorHelper(links),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var link = _step.value;
var result = tr.mapping.mapResult(link.pos, 1);
var node = result.deleted ? null : tr.doc.nodeAt(result.pos);
if ((node === null || node === void 0 ? void 0 : node.type.name) !== 'inlineCard' || node.attrs.url !== link.url) {
changed = true;
continue;
}
if (result.pos !== link.pos) {
changed = true;
mapped.push({
pos: result.pos,
url: link.url
});
} else {
mapped.push(link);
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return changed ? mapped : links;
};
/** Append new candidates, skipping duplicates and counting any dropped by the cap. */
export var addPastedInlineLinksWithOverflow = function addPastedInlineLinksWithOverflow(links, additions) {
var next = links;
var dropped = 0;
var _iterator2 = _createForOfIteratorHelper(additions),
_step2;
try {
var _loop = function _loop() {
var addition = _step2.value;
if (next.some(function (link) {
return isSameLink(link, addition);
})) {
return 1; // continue
}
if (next.length >= MAX_PASTED_INLINE_LINKS) {
dropped += 1;
} else {
next = [].concat(_toConsumableArray(next), [addition]);
}
};
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
if (_loop()) continue;
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return {
dropped: dropped,
links: next
};
};
/** Append new candidates, skipping duplicates and anything beyond the cap. */
export var addPastedInlineLinks = function addPastedInlineLinks(links, additions) {
return addPastedInlineLinksWithOverflow(links, additions).links;
};
export var removePastedInlineLink = function removePastedInlineLink(links, target) {
var next = links.filter(function (link) {
return !isSameLink(link, target);
});
return next.length === links.length ? links : next;
};
/** Inline cards inserted by a paste transaction (copy and paste of existing cards). */
export var getPastedInlineCards = function getPastedInlineCards(tr) {
if (!tr.docChanged || tr.getMeta('uiEvent') !== 'paste' || tr.getMeta('isRemote')) {
return [];
}
var found = [];
tr.mapping.maps.forEach(function (stepMap, index) {
var later = tr.mapping.slice(index + 1);
stepMap.forEach(function (_oldStart, _oldEnd, newStart, newEnd) {
tr.doc.nodesBetween(later.map(newStart, -1), later.map(newEnd, 1), function (node, pos) {
if (node.type.name === 'inlineCard' && typeof node.attrs.url === 'string') {
found.push({
pos: pos,
url: node.attrs.url
});
}
});
});
});
return found;
};
/** Inline cards created from clipboard URLs that just resolved in this transaction. */
export var getResolvedPastedInlineCards = function getResolvedPastedInlineCards(tr, requests, resolvedUrl, clipboardSource) {
if (tr.getMeta('isRemote')) {
return [];
}
var found = [];
var _iterator3 = _createForOfIteratorHelper(requests),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var request = _step3.value;
if (request.url !== resolvedUrl || request.appearance !== 'inline' || request.source !== clipboardSource) {
continue;
}
var pos = tr.mapping.map(request.pos, -1);
if (pos < 0 || pos >= tr.doc.content.size) {
continue;
}
var node = tr.doc.nodeAt(pos);
if ((node === null || node === void 0 ? void 0 : node.type.name) === 'inlineCard' && node.attrs.url === request.url) {
found.push({
pos: pos,
url: request.url
});
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
return found;
};
/** Add candidates unless tracking is off or a link has already been displayed. */
export var withPastedInlineLinks = function withPastedInlineLinks(state, additions) {
var _state$pastedInlineLi;
if (!state.pastedInlineLinks || state.pastedInlineLinkShown || !additions.length) {
return state;
}
var _addPastedInlineLinks = addPastedInlineLinksWithOverflow(state.pastedInlineLinks, additions),
dropped = _addPastedInlineLinks.dropped,
next = _addPastedInlineLinks.links;
if (next === state.pastedInlineLinks && !dropped) {
return state;
}
return _objectSpread(_objectSpread({}, state), {}, {
pastedInlineLinks: next
}, dropped ? {
pastedInlineLinksDropped: ((_state$pastedInlineLi = state.pastedInlineLinksDropped) !== null && _state$pastedInlineLi !== void 0 ? _state$pastedInlineLi : 0) + dropped
} : {});
};