@liveblocks/react-ui
Version:
A set of React pre-built components for the Liveblocks products. Liveblocks is the all-in-one toolkit to build collaborative products like Figma, Notion, and more.
73 lines (69 loc) • 2.18 kB
JavaScript
var slate = require('slate');
var isText = require('../utils/is-text.cjs');
var marks = require('../utils/marks.cjs');
var selectionContainsInlines = require('../utils/selection-contains-inlines.cjs');
function isUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
function withCustomLinks(editor) {
const { isInline, normalizeNode, insertData } = editor;
editor.isInline = (element) => {
return element.type === "custom-link" ? true : isInline(element);
};
editor.normalizeNode = (entry) => {
const [node, path] = entry;
if (isText.isText(node)) {
const parentNode = slate.Node.parent(editor, path);
if (isComposerBodyCustomLink(parentNode)) {
if (!isText.isPlainText(node)) {
const marks$1 = marks.filterActiveMarks(node);
slate.Transforms.unsetNodes(editor, marks$1, { at: path });
}
}
}
normalizeNode(entry);
};
editor.insertData = (data) => {
const { selection } = editor;
const pastedText = data.getData("text/plain");
let shouldInvokeDefaultBehavior = true;
if (selection && !slate.Range.isCollapsed(selection)) {
if (selection.anchor.path[0] === selection.focus.path[0]) {
if (isUrl(pastedText)) {
if (!selectionContainsInlines.selectionContainsInlines(editor, (node) => !isText.isText(node))) {
slate.Transforms.wrapNodes(
editor,
{
type: "custom-link",
url: pastedText,
children: []
},
{
at: selection,
split: true,
match: isText.isPlainText
}
);
shouldInvokeDefaultBehavior = false;
}
}
}
}
if (shouldInvokeDefaultBehavior) {
insertData(data);
}
};
return editor;
}
function isComposerBodyCustomLink(node) {
return slate.Element.isElement(node) && node.type === "custom-link";
}
exports.isComposerBodyCustomLink = isComposerBodyCustomLink;
exports.withCustomLinks = withCustomLinks;
//# sourceMappingURL=custom-links.cjs.map
;