@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
38 lines • 1.54 kB
JavaScript
export const DISPLAY_AS_OPTIONS = ['url', 'inline', 'block', 'embed'];
export const getCardAtPasteRange = (state, pasteStartPos, pasteEndPos) => {
let result;
const docContentSize = state.doc.content.size;
const clampedStart = Math.max(0, Math.min(pasteStartPos, docContentSize));
const clampedEnd = Math.max(0, Math.min(pasteEndPos, docContentSize));
const from = Math.min(clampedStart, clampedEnd);
const to = Math.max(clampedStart, clampedEnd);
try {
if (from === to) {
for (const pos of [from - 1, from]) {
if (pos < 0 || pos >= docContentSize) {
continue;
}
const node = state.doc.nodeAt(pos);
if ((node === null || node === void 0 ? void 0 : node.type.name) === 'inlineCard' || (node === null || node === void 0 ? void 0 : node.type.name) === 'blockCard' || (node === null || node === void 0 ? void 0 : node.type.name) === 'embedCard') {
return {
appearance: node.type.name === 'inlineCard' ? 'inline' : node.type.name === 'blockCard' ? 'block' : 'embed',
pos
};
}
}
}
state.doc.nodesBetween(from, to, (node, pos) => {
if (node.type.name === 'inlineCard' || node.type.name === 'blockCard' || node.type.name === 'embedCard') {
result = {
appearance: node.type.name === 'inlineCard' ? 'inline' : node.type.name === 'blockCard' ? 'block' : 'embed',
pos
};
return false;
}
return true;
});
} catch {
return;
}
return result;
};