@atlaskit/editor-plugin-paste-options-toolbar
Version:
Paste options toolbar for @atlaskit/editor-core
50 lines • 1.95 kB
JavaScript
import { isNotSingleLink } from './paste-menu-rules/isNotSingleLink';
const getUrlFromTextLinkNode = node => {
var _node$marks$0$attrs;
if (!node.isText || node.marks.length !== 1 || node.marks[0].type.name !== 'link') {
return undefined;
}
const href = (_node$marks$0$attrs = node.marks[0].attrs) === null || _node$marks$0$attrs === void 0 ? void 0 : _node$marks$0$attrs.href;
return typeof href === 'string' && node.text === href ? href : undefined;
};
const getUrlFromInlineCardNode = node => {
var _node$attrs;
if (node.type.name !== 'inlineCard') {
return undefined;
}
const url = (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.url;
return typeof url === 'string' ? url : undefined;
};
const significantChildren = node => {
const children = [];
node.content.forEach(child => {
var _child$text;
if (child.isText && ((_child$text = child.text) === null || _child$text === void 0 ? void 0 : _child$text.trim()) === '') {
return;
}
children.push(child);
});
return children;
};
export const getSingleSmartLinkUrlFromSlice = slice => {
var _getUrlFromTextLinkNo, _getUrlFromTextLinkNo2;
if (!slice || isNotSingleLink(slice)) {
return undefined;
}
if (slice.content.childCount !== 1) {
return undefined;
}
const topNode = slice.content.child(0);
const topNodeUrl = (_getUrlFromTextLinkNo = getUrlFromTextLinkNode(topNode)) !== null && _getUrlFromTextLinkNo !== void 0 ? _getUrlFromTextLinkNo : getUrlFromInlineCardNode(topNode);
if (topNodeUrl) {
return topNodeUrl;
}
if (topNode.type.name !== 'paragraph') {
return undefined;
}
const children = significantChildren(topNode);
if (children.length !== 1) {
return undefined;
}
return (_getUrlFromTextLinkNo2 = getUrlFromTextLinkNode(children[0])) !== null && _getUrlFromTextLinkNo2 !== void 0 ? _getUrlFromTextLinkNo2 : getUrlFromInlineCardNode(children[0]);
};