UNPKG

@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.

78 lines (74 loc) 2.46 kB
'use strict'; var slate = require('slate'); var isText = require('../../../slate/utils/is-text.cjs'); var marks = require('../../../slate/utils/marks.cjs'); var selectionContainsInlines = require('../../../slate/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 (slate.Element.isElement(node) && node.type === "custom-link") { if (node.children.length === 0 || node.children.length === 1 && node.children[0]?.text === "") { slate.Transforms.removeNodes(editor, { at: path }); } } 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