@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
46 lines • 2.27 kB
JavaScript
var getUrlFromTextLinkNode = function getUrlFromTextLinkNode(node) {
var _node$marks$0$attrs;
if (!node.isText || node.marks.length !== 1 || node.marks[0].type.name !== 'link') {
return undefined;
}
var 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;
};
var getUrlFromCardNode = function getUrlFromCardNode(node) {
var _node$attrs$url, _node$attrs, _node$attrs2;
if (node.type.name !== 'inlineCard' && node.type.name !== 'blockCard' && node.type.name !== 'embedCard') {
return undefined;
}
var url = (_node$attrs$url = (_node$attrs = node.attrs) === null || _node$attrs === void 0 ? void 0 : _node$attrs.url) !== null && _node$attrs$url !== void 0 ? _node$attrs$url : (_node$attrs2 = node.attrs) === null || _node$attrs2 === void 0 || (_node$attrs2 = _node$attrs2.data) === null || _node$attrs2 === void 0 ? void 0 : _node$attrs2.url;
return typeof url === 'string' ? url : undefined;
};
var significantChildren = function significantChildren(fragment) {
var children = [];
fragment.forEach(function (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 var getSingleSmartLinkUrlFromSlice = function getSingleSmartLinkUrlFromSlice(slice) {
var _getUrlFromTextLinkNo, _getUrlFromTextLinkNo2;
if (!slice || slice.content.childCount !== 1) {
return undefined;
}
var topNode = slice.content.child(0);
var topNodeUrl = (_getUrlFromTextLinkNo = getUrlFromTextLinkNode(topNode)) !== null && _getUrlFromTextLinkNo !== void 0 ? _getUrlFromTextLinkNo : getUrlFromCardNode(topNode);
if (topNodeUrl) {
return topNodeUrl;
}
if (topNode.type.name !== 'paragraph') {
return undefined;
}
var children = significantChildren(topNode.content);
if (children.length !== 1) {
return undefined;
}
return (_getUrlFromTextLinkNo2 = getUrlFromTextLinkNode(children[0])) !== null && _getUrlFromTextLinkNo2 !== void 0 ? _getUrlFromTextLinkNo2 : getUrlFromCardNode(children[0]);
};