@liveblocks/react-ui 2.1.1-test2 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/dist/components/Comment.js +23 -11
  2. package/dist/components/Comment.js.map +1 -1
  3. package/dist/components/Comment.mjs +23 -11
  4. package/dist/components/Comment.mjs.map +1 -1
  5. package/dist/components/Composer.js.map +1 -1
  6. package/dist/components/Composer.mjs.map +1 -1
  7. package/dist/components/InboxNotification.js.map +1 -1
  8. package/dist/components/InboxNotification.mjs.map +1 -1
  9. package/dist/components/InboxNotificationList.js.map +1 -1
  10. package/dist/components/InboxNotificationList.mjs.map +1 -1
  11. package/dist/components/Thread.js +19 -9
  12. package/dist/components/Thread.js.map +1 -1
  13. package/dist/components/Thread.mjs +20 -10
  14. package/dist/components/Thread.mjs.map +1 -1
  15. package/dist/index.d.mts +5 -12
  16. package/dist/index.d.ts +5 -12
  17. package/dist/primitives/Comment/index.js +5 -3
  18. package/dist/primitives/Comment/index.js.map +1 -1
  19. package/dist/primitives/Comment/index.mjs +5 -3
  20. package/dist/primitives/Comment/index.mjs.map +1 -1
  21. package/dist/primitives/Comment/utils.js +1 -1
  22. package/dist/primitives/Comment/utils.js.map +1 -1
  23. package/dist/primitives/Comment/utils.mjs +1 -1
  24. package/dist/primitives/Comment/utils.mjs.map +1 -1
  25. package/dist/primitives/Composer/index.js +14 -11
  26. package/dist/primitives/Composer/index.js.map +1 -1
  27. package/dist/primitives/Composer/index.mjs +14 -11
  28. package/dist/primitives/Composer/index.mjs.map +1 -1
  29. package/dist/primitives/Composer/utils.js +35 -13
  30. package/dist/primitives/Composer/utils.js.map +1 -1
  31. package/dist/primitives/Composer/utils.mjs +35 -14
  32. package/dist/primitives/Composer/utils.mjs.map +1 -1
  33. package/dist/slate/plugins/auto-links.js +16 -5
  34. package/dist/slate/plugins/auto-links.js.map +1 -1
  35. package/dist/slate/plugins/auto-links.mjs +17 -6
  36. package/dist/slate/plugins/auto-links.mjs.map +1 -1
  37. package/dist/slate/plugins/custom-links.js +71 -0
  38. package/dist/slate/plugins/custom-links.js.map +1 -0
  39. package/dist/slate/plugins/custom-links.mjs +68 -0
  40. package/dist/slate/plugins/custom-links.mjs.map +1 -0
  41. package/dist/slate/plugins/paste-html.js +105 -0
  42. package/dist/slate/plugins/paste-html.js.map +1 -0
  43. package/dist/slate/plugins/paste-html.mjs +103 -0
  44. package/dist/slate/plugins/paste-html.mjs.map +1 -0
  45. package/dist/slate/utils/is-empty.js +3 -2
  46. package/dist/slate/utils/is-empty.js.map +1 -1
  47. package/dist/slate/utils/is-empty.mjs +4 -3
  48. package/dist/slate/utils/is-empty.mjs.map +1 -1
  49. package/dist/slate/utils/is-text.js +12 -0
  50. package/dist/slate/utils/is-text.js.map +1 -0
  51. package/dist/slate/utils/is-text.mjs +9 -0
  52. package/dist/slate/utils/is-text.mjs.map +1 -0
  53. package/dist/slate/utils/selection-contains-inlines.js +36 -0
  54. package/dist/slate/utils/selection-contains-inlines.js.map +1 -0
  55. package/dist/slate/utils/selection-contains-inlines.mjs +34 -0
  56. package/dist/slate/utils/selection-contains-inlines.mjs.map +1 -0
  57. package/dist/utils/use-transition.js +6 -2
  58. package/dist/utils/use-transition.js.map +1 -1
  59. package/dist/utils/use-transition.mjs +7 -3
  60. package/dist/utils/use-transition.mjs.map +1 -1
  61. package/dist/utils/use-window-focus.js +24 -0
  62. package/dist/utils/use-window-focus.js.map +1 -0
  63. package/dist/utils/use-window-focus.mjs +22 -0
  64. package/dist/utils/use-window-focus.mjs.map +1 -0
  65. package/dist/version.js +1 -1
  66. package/dist/version.js.map +1 -1
  67. package/dist/version.mjs +1 -1
  68. package/dist/version.mjs.map +1 -1
  69. package/package.json +6 -4
@@ -1 +1 @@
1
- {"version":3,"file":"auto-links.js","sources":["../../../src/slate/plugins/auto-links.ts"],"sourcesContent":["import type { NodeEntry } from \"slate\";\nimport { Editor, Element, Node, Path, Range, Text, Transforms } from \"slate\";\n\nimport type { ComposerBodyAutoLink } from \"../../types\";\n\n/**\n * This implementation is inspired by Lexical's AutoLink plugin.\n * Additional modifications and features were added to adapt it to our specific needs.\n *\n * Original Lexical AutoLink plugin can be found at [Lexical's Github Repository](https://github.com/facebook/lexical/blob/main/packages/lexical-react/src/LexicalAutoLinkPlugin.ts)\n */\nexport function withAutoLinks(editor: Editor): Editor {\n const { isInline, normalizeNode, deleteBackward } = editor;\n\n editor.isInline = (element) => {\n return element.type === \"auto-link\" ? true : isInline(element);\n };\n\n editor.normalizeNode = (entry) => {\n const [node, path] = entry;\n\n if (Text.isText(node)) {\n const parentNode = Node.parent(editor, path);\n\n if (isComposerBodyAutoLink(parentNode)) {\n const parentPath = Path.parent(path);\n handleLinkEdit(editor, [parentNode, parentPath]);\n } else {\n handleLinkCreate(editor, [node, path]);\n handleNeighbours(editor, [node, path]);\n }\n }\n\n normalizeNode(entry);\n };\n\n editor.deleteBackward = (unit) => {\n deleteBackward(unit);\n const { selection } = editor;\n if (!selection) return;\n\n if (!Range.isCollapsed(selection)) return;\n\n const [match] = Editor.nodes(editor, {\n at: selection,\n match: isComposerBodyAutoLink,\n mode: \"lowest\",\n });\n\n if (!match) return;\n\n Transforms.unwrapNodes(editor, {\n match: isComposerBodyAutoLink,\n });\n };\n\n return editor;\n}\n\nexport function isComposerBodyAutoLink(\n node: Node\n): node is ComposerBodyAutoLink {\n return Element.isElement(node) && node.type === \"auto-link\";\n}\n\n/**\n * 1. ((https?:\\/\\/(www\\.)?)|(www\\.))\n * - Matches 'http://' or 'https://' optionally followed by 'www.', or just 'www.'\n *\n * 2. [-a-zA-Z0-9@:%._+~#=]{1,256}\n * - Matches any character in the set [-a-zA-Z0-9@:%._+~#=] between 1 and 256 times, often found in the domain and subdomain part of the URL\n *\n * 3. \\.[a-zA-Z0-9()]{1,6}\n * - Matches a period followed by any character in the set [a-zA-Z0-9()] between 1 and 6 times, usually indicating the domain extension like .com, .org, etc.\n *\n * 4. \\b\n * - Represents a word boundary, ensuring that the characters following cannot be part of a different word\n *\n * 5. ([-a-zA-Z0-9().@:%_+~#?&//=]*)\n * - Matches any character in the set [-a-zA-Z0-9().@:%_+~#?&//=] between 0 and unlimited times, often found in the path, query parameters, or anchor part of the URL\n *\n * Matching URLs:\n * - http://www.example.com\n * - https://www.example.com\n * - www.example.com\n * - https://example.com/path?query=param#anchor\n *\n * Non-Matching URLs:\n * - http:/example.com (malformed scheme)\n * - example (missing scheme and domain extension)\n * - ftp://example.com (ftp scheme is not supported)\n * - example.com (missing scheme)\n */\nconst URL_REGEX =\n /((https?:\\/\\/(www\\.)?)|(www\\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9().@:%_+~#?&//=]*)/;\n\nconst PUNCTUATION_OR_SPACE = /[.,;!?\\s()]/;\n\nconst PERIOD_OR_QUESTION_MARK_FOLLOWED_BY_ALPHANUMERIC = /^[.?][a-zA-Z0-9]+/;\n\nconst PARENTHESES = /[()]/;\n\n/**\n * Helper function to check if a character is a separator (punctuation or space)\n * @param char The character to check\n * @returns Whether the character is a separator or not\n */\nfunction isSeparator(char: string): boolean {\n return PUNCTUATION_OR_SPACE.test(char);\n}\n\n/**\n * Helper function to check if a text content ends with a separator (punctuation or space)\n * @param textContent The text content to check\n * @returns Whether the text content ends with a separator or not\n */\nfunction endsWithSeparator(textContent: string): boolean {\n return isSeparator(textContent[textContent.length - 1]);\n}\n\n/**\n * Helper function to check if a text content starts with a separator (punctuation or space)\n * @param textContent The text content to check\n * @returns Whether the text content starts with a separator or not\n */\nfunction startsWithSeparator(textContent: string): boolean {\n return isSeparator(textContent[0]);\n}\n\n/**\n * Helper function to check if a text content ends with a period or question mark\n * @param textContent The text content to check\n * @returns Whether the text content ends with a period or not\n */\nfunction endsWithPeriodOrQuestionMark(textContent: string): boolean {\n return (\n textContent[textContent.length - 1] === \".\" ||\n textContent[textContent.length - 1] === \"?\"\n );\n}\n\n/**\n * Helper function to get the \"logical length\" of a URL, taking into account things like opening/closing parentheses\n * @param url The URL to check\n * @returns The \"logical length\" of the URL\n */\nfunction getUrlLogicalLength(url: string): number {\n if (!PARENTHESES.test(url)) {\n return url.length;\n }\n\n let logicalLength = 0;\n let parenthesesCount = 0;\n\n for (const character of url) {\n if (character === \"(\") {\n parenthesesCount++;\n }\n\n if (character === \")\") {\n parenthesesCount--;\n\n if (parenthesesCount < 0) {\n break;\n }\n }\n\n logicalLength++;\n }\n\n return logicalLength;\n}\n\n/**\n * Helper function to check if the previous node is valid (text node that ends with a separator or is empty)\n */\nfunction isPreviousNodeValid(editor: Editor, path: Path): boolean {\n const entry = Editor.previous(editor, { at: path });\n if (!entry) return true;\n\n return (\n Text.isText(entry[0]) &&\n (endsWithSeparator(entry[0].text) || entry[0].text === \"\")\n );\n}\n\n/**\n * Helper function to check if the next node is valid (text node that starts with a separator or is empty)\n */\nfunction isNextNodeValid(editor: Editor, path: Path): boolean {\n const entry = Editor.next(editor, { at: path });\n if (!entry) return true;\n\n return (\n Text.isText(entry[0]) &&\n (startsWithSeparator(entry[0].text) || entry[0].text === \"\")\n );\n}\n\n/**\n * Helper function to check if the content around a text node is valid.\n * @param editor\n * @param entry\n * @param start\n * @param end\n * @returns\n */\nfunction isContentAroundValid(\n editor: Editor,\n entry: NodeEntry<Text>,\n start: number,\n end: number\n): boolean {\n const [node, path] = entry;\n const text = node.text;\n\n const contentBeforeIsValid =\n start > 0\n ? isSeparator(text[start - 1])\n : isPreviousNodeValid(editor, path);\n\n const contentAfterIsValid =\n end < text.length ? isSeparator(text[end]) : isNextNodeValid(editor, path);\n\n return contentBeforeIsValid && contentAfterIsValid;\n}\n\nconst handleLinkEdit = (\n editor: Editor,\n entry: NodeEntry<ComposerBodyAutoLink>\n) => {\n const [node, path] = entry;\n\n // Step 1: Ensure that the Link node only contains text nodes as children\n const children = Node.children(editor, path);\n for (const [child] of children) {\n if (Text.isText(child)) continue;\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n // Attempt to match the text content (of the Link node) against the URL regex\n const text = Node.string(node);\n const match = URL_REGEX.exec(text);\n\n // Step 2: Ensure that the text content of the Link node matches the URL regex and is identical to the match\n if (!match || match[0] !== text) {\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n\n // Step 3: Ensure that if the text content of the Link node ends with a period, we unwrap the Link node and wrap the text before the period in a new Link node\n if (endsWithPeriodOrQuestionMark(text)) {\n Transforms.unwrapNodes(editor, { at: path });\n\n const textBeforePeriod = text.slice(0, text.length - 1);\n\n // Remove the last character from the link text and wrap the remaining text in a new link node\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: textBeforePeriod,\n children: [],\n },\n {\n at: {\n anchor: { path, offset: 0 },\n focus: { path, offset: textBeforePeriod.length },\n },\n split: true,\n }\n );\n return;\n }\n\n // Step 4: Allow some conditions to shorten the URL (e.g. supporting parentheses but only if they are balanced)\n const logicalLength = getUrlLogicalLength(text);\n\n if (logicalLength < text.length) {\n Transforms.unwrapNodes(editor, { at: path });\n\n const logicalText = text.slice(0, logicalLength);\n\n // Keep the \"logical\" text and wrap it in a new link node\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: logicalText,\n children: [],\n },\n {\n at: {\n anchor: { path, offset: 0 },\n focus: { path, offset: logicalText.length },\n },\n split: true,\n }\n );\n return;\n }\n\n // Step 5: Ensure that the text content of the Link node is surrounded by separators or the start/end of the text content\n if (!isPreviousNodeValid(editor, path) || !isNextNodeValid(editor, path)) {\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n\n // Step 6: Ensure that the url attribute of the Link node is identical to its text content\n if (node.url !== text) {\n Transforms.setNodes(editor, { url: match[0] }, { at: path });\n return;\n }\n};\n\nconst handleLinkCreate = (editor: Editor, entry: NodeEntry<Text>) => {\n const [node, path] = entry;\n\n // Step 1: Ensure that the text content of the node matches the URL regex\n const match = URL_REGEX.exec(node.text);\n if (!match) return;\n\n const start = match.index;\n const end = start + match[0].length;\n\n // Step 2: Ensure that the content around the node is valid\n if (!isContentAroundValid(editor, entry, start, end)) return;\n\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: match[0],\n children: [],\n },\n {\n at: {\n anchor: { path, offset: start },\n focus: { path, offset: end },\n },\n split: true,\n }\n );\n return;\n};\n\nconst handleNeighbours = (editor: Editor, entry: NodeEntry<Text>) => {\n const [node, path] = entry;\n const text = node.text;\n\n const previousSibling = Editor.previous(editor, { at: path });\n\n if (previousSibling && isComposerBodyAutoLink(previousSibling[0])) {\n if (PERIOD_OR_QUESTION_MARK_FOLLOWED_BY_ALPHANUMERIC.test(text)) {\n Transforms.unwrapNodes(editor, { at: previousSibling[1] });\n Transforms.mergeNodes(editor, { at: path });\n return;\n }\n\n if (!startsWithSeparator(text)) {\n Transforms.unwrapNodes(editor, { at: previousSibling[1] });\n return;\n }\n }\n\n const nextSibling = Editor.next(editor, { at: path });\n if (\n nextSibling &&\n isComposerBodyAutoLink(nextSibling[0]) &&\n !endsWithSeparator(text)\n ) {\n Transforms.unwrapNodes(editor, { at: nextSibling[1] });\n return;\n }\n};\n"],"names":["Text","Node","Path","Range","Editor","Transforms","Element"],"mappings":";;;;AAWO,SAAS,cAAc,MAAwB,EAAA;AACpD,EAAA,MAAM,EAAE,QAAA,EAAU,aAAe,EAAA,cAAA,EAAmB,GAAA,MAAA,CAAA;AAEpD,EAAO,MAAA,CAAA,QAAA,GAAW,CAAC,OAAY,KAAA;AAC7B,IAAA,OAAO,OAAQ,CAAA,IAAA,KAAS,WAAc,GAAA,IAAA,GAAO,SAAS,OAAO,CAAA,CAAA;AAAA,GAC/D,CAAA;AAEA,EAAO,MAAA,CAAA,aAAA,GAAgB,CAAC,KAAU,KAAA;AAChC,IAAM,MAAA,CAAC,IAAM,EAAA,IAAI,CAAI,GAAA,KAAA,CAAA;AAErB,IAAI,IAAAA,UAAA,CAAK,MAAO,CAAA,IAAI,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,IAAAC,gBAAA,CAAW,YAAY,MAAQ,EAAA,EAAE,EAAI,EAAA,WAAA,CAAY,IAAI,CAAA,CAAA;AACrD,IAAA,OAAA;AAAA,GACF;AACF,CAAA;;;;;"}
1
+ {"version":3,"file":"auto-links.js","sources":["../../../src/slate/plugins/auto-links.ts"],"sourcesContent":["import type { NodeEntry, Text } from \"slate\";\nimport { Editor, Element, Node, Path, Range, Transforms } from \"slate\";\n\nimport type { ComposerBodyAutoLink } from \"../../types\";\nimport { isPlainText, isText } from \"../utils/is-text\";\nimport { isComposerBodyCustomLink } from \"./custom-links\";\n\n/**\n * This implementation is inspired by Lexical's AutoLink plugin.\n * Additional modifications and features were added to adapt it to our specific needs.\n *\n * Original Lexical AutoLink plugin can be found at [Lexical's Github Repository](https://github.com/facebook/lexical/blob/main/packages/lexical-react/src/LexicalAutoLinkPlugin.ts)\n */\nexport function withAutoLinks(editor: Editor): Editor {\n const { isInline, normalizeNode, deleteBackward } = editor;\n\n editor.isInline = (element) => {\n return element.type === \"auto-link\" ? true : isInline(element);\n };\n\n editor.normalizeNode = (entry) => {\n const [node, path] = entry;\n\n // Prevent auto links from being created inside custom links\n if (isComposerBodyCustomLink(node)) {\n return;\n }\n\n if (isText(node)) {\n const parentNode = Node.parent(editor, path);\n\n // Prevent auto links from being created inside custom links\n if (isComposerBodyCustomLink(parentNode)) {\n return;\n } else if (isComposerBodyAutoLink(parentNode)) {\n const parentPath = Path.parent(path);\n handleLinkEdit(editor, [parentNode, parentPath]);\n\n // Prevent rich text within auto links by removing all marks of inner text nodes\n if (!isPlainText(node)) {\n const marks = Object.keys(node).filter((key) => key !== \"text\");\n\n Transforms.unsetNodes(editor, marks, { at: path });\n }\n } else {\n handleLinkCreate(editor, [node, path]);\n handleNeighbours(editor, [node, path]);\n }\n }\n\n normalizeNode(entry);\n };\n\n editor.deleteBackward = (unit) => {\n deleteBackward(unit);\n const { selection } = editor;\n if (!selection) return;\n\n if (!Range.isCollapsed(selection)) return;\n\n const [match] = Editor.nodes(editor, {\n at: selection,\n match: isComposerBodyAutoLink,\n mode: \"lowest\",\n });\n\n if (!match) return;\n\n Transforms.unwrapNodes(editor, {\n match: isComposerBodyAutoLink,\n });\n };\n\n return editor;\n}\n\nexport function isComposerBodyAutoLink(\n node: Node\n): node is ComposerBodyAutoLink {\n return Element.isElement(node) && node.type === \"auto-link\";\n}\n\n/**\n * 1. ((https?:\\/\\/(www\\.)?)|(www\\.))\n * - Matches 'http://' or 'https://' optionally followed by 'www.', or just 'www.'\n *\n * 2. [-a-zA-Z0-9@:%._+~#=]{1,256}\n * - Matches any character in the set [-a-zA-Z0-9@:%._+~#=] between 1 and 256 times, often found in the domain and subdomain part of the URL\n *\n * 3. \\.[a-zA-Z0-9()]{1,6}\n * - Matches a period followed by any character in the set [a-zA-Z0-9()] between 1 and 6 times, usually indicating the domain extension like .com, .org, etc.\n *\n * 4. \\b\n * - Represents a word boundary, ensuring that the characters following cannot be part of a different word\n *\n * 5. ([-a-zA-Z0-9().@:%_+~#?&//=]*)\n * - Matches any character in the set [-a-zA-Z0-9().@:%_+~#?&//=] between 0 and unlimited times, often found in the path, query parameters, or anchor part of the URL\n *\n * Matching URLs:\n * - http://www.example.com\n * - https://www.example.com\n * - www.example.com\n * - https://example.com/path?query=param#anchor\n *\n * Non-Matching URLs:\n * - http:/example.com (malformed scheme)\n * - example (missing scheme and domain extension)\n * - ftp://example.com (ftp scheme is not supported)\n * - example.com (missing scheme)\n */\nconst URL_REGEX =\n /((https?:\\/\\/(www\\.)?)|(www\\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9().@:%_+~#?&//=]*)/;\n\nconst PUNCTUATION_OR_SPACE = /[.,;!?\\s()]/;\n\nconst PERIOD_OR_QUESTION_MARK_FOLLOWED_BY_ALPHANUMERIC = /^[.?][a-zA-Z0-9]+/;\n\nconst PARENTHESES = /[()]/;\n\n/**\n * Helper function to check if a character is a separator (punctuation or space)\n * @param char The character to check\n * @returns Whether the character is a separator or not\n */\nfunction isSeparator(char: string): boolean {\n return PUNCTUATION_OR_SPACE.test(char);\n}\n\n/**\n * Helper function to check if a text content ends with a separator (punctuation or space)\n * @param textContent The text content to check\n * @returns Whether the text content ends with a separator or not\n */\nfunction endsWithSeparator(textContent: string): boolean {\n return isSeparator(textContent[textContent.length - 1]);\n}\n\n/**\n * Helper function to check if a text content starts with a separator (punctuation or space)\n * @param textContent The text content to check\n * @returns Whether the text content starts with a separator or not\n */\nfunction startsWithSeparator(textContent: string): boolean {\n return isSeparator(textContent[0]);\n}\n\n/**\n * Helper function to check if a text content ends with a period or question mark\n * @param textContent The text content to check\n * @returns Whether the text content ends with a period or not\n */\nfunction endsWithPeriodOrQuestionMark(textContent: string): boolean {\n return (\n textContent[textContent.length - 1] === \".\" ||\n textContent[textContent.length - 1] === \"?\"\n );\n}\n\n/**\n * Helper function to get the \"logical length\" of a URL, taking into account things like opening/closing parentheses\n * @param url The URL to check\n * @returns The \"logical length\" of the URL\n */\nfunction getUrlLogicalLength(url: string): number {\n if (!PARENTHESES.test(url)) {\n return url.length;\n }\n\n let logicalLength = 0;\n let parenthesesCount = 0;\n\n for (const character of url) {\n if (character === \"(\") {\n parenthesesCount++;\n }\n\n if (character === \")\") {\n parenthesesCount--;\n\n if (parenthesesCount < 0) {\n break;\n }\n }\n\n logicalLength++;\n }\n\n return logicalLength;\n}\n\n/**\n * Helper function to check if the previous node is valid (text node that ends with a separator or is empty)\n */\nfunction isPreviousNodeValid(editor: Editor, path: Path): boolean {\n const entry = Editor.previous(editor, { at: path });\n if (!entry) return true;\n\n return (\n isText(entry[0]) &&\n (endsWithSeparator(entry[0].text) || entry[0].text === \"\")\n );\n}\n\n/**\n * Helper function to check if the next node is valid (text node that starts with a separator or is empty)\n */\nfunction isNextNodeValid(editor: Editor, path: Path): boolean {\n const entry = Editor.next(editor, { at: path });\n if (!entry) return true;\n\n return (\n isText(entry[0]) &&\n (startsWithSeparator(entry[0].text) || entry[0].text === \"\")\n );\n}\n\n/**\n * Helper function to check if the content around a text node is valid.\n * @param editor\n * @param entry\n * @param start\n * @param end\n * @returns\n */\nfunction isContentAroundValid(\n editor: Editor,\n entry: NodeEntry<Text>,\n start: number,\n end: number\n): boolean {\n const [node, path] = entry;\n const text = node.text;\n\n const contentBeforeIsValid =\n start > 0\n ? isSeparator(text[start - 1])\n : isPreviousNodeValid(editor, path);\n\n const contentAfterIsValid =\n end < text.length ? isSeparator(text[end]) : isNextNodeValid(editor, path);\n\n return contentBeforeIsValid && contentAfterIsValid;\n}\n\nconst handleLinkEdit = (\n editor: Editor,\n entry: NodeEntry<ComposerBodyAutoLink>\n) => {\n const [node, path] = entry;\n\n // Step 1: Ensure that the Link node only contains text nodes as children\n const children = Node.children(editor, path);\n for (const [child] of children) {\n if (isText(child)) continue;\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n // Attempt to match the text content (of the Link node) against the URL regex\n const text = Node.string(node);\n const match = URL_REGEX.exec(text);\n\n // Step 2: Ensure that the text content of the Link node matches the URL regex and is identical to the match\n if (!match || match[0] !== text) {\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n\n // Step 3: Ensure that if the text content of the Link node ends with a period, we unwrap the Link node and wrap the text before the period in a new Link node\n if (endsWithPeriodOrQuestionMark(text)) {\n Transforms.unwrapNodes(editor, { at: path });\n\n const textBeforePeriod = text.slice(0, text.length - 1);\n\n // Remove the last character from the link text and wrap the remaining text in a new link node\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: textBeforePeriod,\n children: [],\n },\n {\n at: {\n anchor: { path, offset: 0 },\n focus: { path, offset: textBeforePeriod.length },\n },\n split: true,\n }\n );\n return;\n }\n\n // Step 4: Allow some conditions to shorten the URL (e.g. supporting parentheses but only if they are balanced)\n const logicalLength = getUrlLogicalLength(text);\n\n if (logicalLength < text.length) {\n Transforms.unwrapNodes(editor, { at: path });\n\n const logicalText = text.slice(0, logicalLength);\n\n // Keep the \"logical\" text and wrap it in a new link node\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: logicalText,\n children: [],\n },\n {\n at: {\n anchor: { path, offset: 0 },\n focus: { path, offset: logicalText.length },\n },\n split: true,\n }\n );\n return;\n }\n\n // Step 5: Ensure that the text content of the Link node is surrounded by separators or the start/end of the text content\n if (!isPreviousNodeValid(editor, path) || !isNextNodeValid(editor, path)) {\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n\n // Step 6: Ensure that the url attribute of the Link node is identical to its text content\n if (node.url !== text) {\n Transforms.setNodes(editor, { url: match[0] }, { at: path });\n return;\n }\n};\n\nconst handleLinkCreate = (editor: Editor, entry: NodeEntry<Text>) => {\n const [node, path] = entry;\n\n // Step 1: Ensure that the text content of the node matches the URL regex\n const match = URL_REGEX.exec(node.text);\n if (!match) return;\n\n const start = match.index;\n const end = start + match[0].length;\n\n // Step 2: Ensure that the content around the node is valid\n if (!isContentAroundValid(editor, entry, start, end)) return;\n\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: match[0],\n children: [],\n },\n {\n at: {\n anchor: { path, offset: start },\n focus: { path, offset: end },\n },\n split: true,\n }\n );\n return;\n};\n\nconst handleNeighbours = (editor: Editor, entry: NodeEntry<Text>) => {\n const [node, path] = entry;\n const text = node.text;\n\n const previousSibling = Editor.previous(editor, { at: path });\n\n if (previousSibling && isComposerBodyAutoLink(previousSibling[0])) {\n if (PERIOD_OR_QUESTION_MARK_FOLLOWED_BY_ALPHANUMERIC.test(text)) {\n Transforms.unwrapNodes(editor, { at: previousSibling[1] });\n Transforms.mergeNodes(editor, { at: path });\n return;\n }\n\n if (!startsWithSeparator(text)) {\n Transforms.unwrapNodes(editor, { at: previousSibling[1] });\n return;\n }\n }\n\n const nextSibling = Editor.next(editor, { at: path });\n if (\n nextSibling &&\n isComposerBodyAutoLink(nextSibling[0]) &&\n !endsWithSeparator(text)\n ) {\n Transforms.unwrapNodes(editor, { at: nextSibling[1] });\n return;\n }\n};\n"],"names":["isComposerBodyCustomLink","isText","Node","Path","isPlainText","Transforms","Range","Editor","Element"],"mappings":";;;;;;AAaO,SAAS,cAAc,MAAwB,EAAA;AACpD,EAAA,MAAM,EAAE,QAAA,EAAU,aAAe,EAAA,cAAA,EAAmB,GAAA,MAAA,CAAA;AAEpD,EAAO,MAAA,CAAA,QAAA,GAAW,CAAC,OAAY,KAAA;AAC7B,IAAA,OAAO,OAAQ,CAAA,IAAA,KAAS,WAAc,GAAA,IAAA,GAAO,SAAS,OAAO,CAAA,CAAA;AAAA,GAC/D,CAAA;AAEA,EAAO,MAAA,CAAA,aAAA,GAAgB,CAAC,KAAU,KAAA;AAChC,IAAM,MAAA,CAAC,IAAM,EAAA,IAAI,CAAI,GAAA,KAAA,CAAA;AAGrB,IAAI,IAAAA,oCAAA,CAAyB,IAAI,CAAG,EAAA;AAClC,MAAA,OAAA;AAAA,KACF;AAEA,IAAI,IAAAC,aAAA,CAAO,IAAI,CAAG,EAAA;AAChB,MAAA,MAAM,UAAa,GAAAC,UAAA,CAAK,MAAO,CAAA,MAAA,EAAQ,IAAI,CAAA,CAAA;AAG3C,MAAI,IAAAF,oCAAA,CAAyB,UAAU,CAAG,EAAA;AACxC,QAAA,OAAA;AAAA,OACF,MAAA,IAAW,sBAAuB,CAAA,UAAU,CAAG,EAAA;AAC7C,QAAM,MAAA,UAAA,GAAaG,UAAK,CAAA,MAAA,CAAO,IAAI,CAAA,CAAA;AACnC,QAAA,cAAA,CAAe,MAAQ,EAAA,CAAC,UAAY,EAAA,UAAU,CAAC,CAAA,CAAA;AAG/C,QAAI,IAAA,CAACC,kBAAY,CAAA,IAAI,CAAG,EAAA;AACtB,UAAM,MAAA,KAAA,GAAQ,OAAO,IAAK,CAAA,IAAI,EAAE,MAAO,CAAA,CAAC,GAAQ,KAAA,GAAA,KAAQ,MAAM,CAAA,CAAA;AAE9D,UAAAC,gBAAA,CA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A;AACnE,EAAM,MAAA,CAAC,IAAM,EAAA,IAAI,CAAI,GAAA,KAAA,CAAA;AACrB,EAAA,MAAM,OAAO,IAAK,CAAA,IAAA,CAAA;AAElB,EAAA,MAAM,kBAAkBE,YAAO,CAAA,QAAA,CAAS,QAAQ,EAAE,EAAA,EAAI,MAAM,CAAA,CAAA;AAE5D,EAAA,IAAI,eAAmB,IAAA,sBAAA,CAAuB,eAAgB,CAAA,CAAA,CAAE,CAAG,EAAA;AACjE,IAAI,IAAA,gDAAA,CAAiD,IAAK,CAAA,IAAI,CAAG,EAAA;AAC/D,MAAAF,gBAAA,CAAW,YAAY,MAAQ,EAAA,EAAE,EAAI,EAAA,eAAA,CAAgB,IAAI,CAAA,CAAA;AACzD,MAAAA,gBAAA,CAAW,UAAW,CAAA,MAAA,EAAQ,EAAE,EAAA,EAAI,MAAM,CAAA,CAAA;AAC1C,MAAA,OAAA;AAAA,KACF;AAEA,IAAI,IAAA,CAAC,mBAAoB,CAAA,IAAI,CAAG,EAAA;AAC9B,MAAAA,gBAAA,CAAW,YAAY,MAAQ,EAAA,EAAE,EAAI,EAAA,eAAA,CAAgB,IAAI,CAAA,CAAA;AACzD,MAAA,OAAA;AAAA,KACF;AAAA,GACF;AAEA,EAAA,MAAM,cAAcE,YAAO,CAAA,IAAA,CAAK,QAAQ,EAAE,EAAA,EAAI,MAAM,CAAA,CAAA;AACpD,EACE,IAAA,WAAA,IACA,uBAAuB,WAAY,CAAA,CAAA,CAAE,KACrC,CAAC,iBAAA,CAAkB,IAAI,CACvB,EAAA;AACA,IAAAF,gBAAA,CAAW,YAAY,MAAQ,EAAA,EAAE,EAAI,EAAA,WAAA,CAAY,IAAI,CAAA,CAAA;AACrD,IAAA,OAAA;AAAA,GACF;AACF,CAAA;;;;;"}
@@ -1,4 +1,6 @@
1
- import { Element, Text, Node, Path, Range, Editor, Transforms } from 'slate';
1
+ import { Element, Node, Path, Transforms, Range, Editor } from 'slate';
2
+ import { isText, isPlainText } from '../utils/is-text.mjs';
3
+ import { isComposerBodyCustomLink } from './custom-links.mjs';
2
4
 
3
5
  function withAutoLinks(editor) {
4
6
  const { isInline, normalizeNode, deleteBackward } = editor;
@@ -7,11 +9,20 @@ function withAutoLinks(editor) {
7
9
  };
8
10
  editor.normalizeNode = (entry) => {
9
11
  const [node, path] = entry;
10
- if (Text.isText(node)) {
12
+ if (isComposerBodyCustomLink(node)) {
13
+ return;
14
+ }
15
+ if (isText(node)) {
11
16
  const parentNode = Node.parent(editor, path);
12
- if (isComposerBodyAutoLink(parentNode)) {
17
+ if (isComposerBodyCustomLink(parentNode)) {
18
+ return;
19
+ } else if (isComposerBodyAutoLink(parentNode)) {
13
20
  const parentPath = Path.parent(path);
14
21
  handleLinkEdit(editor, [parentNode, parentPath]);
22
+ if (!isPlainText(node)) {
23
+ const marks = Object.keys(node).filter((key) => key !== "text");
24
+ Transforms.unsetNodes(editor, marks, { at: path });
25
+ }
15
26
  } else {
16
27
  handleLinkCreate(editor, [node, path]);
17
28
  handleNeighbours(editor, [node, path]);
@@ -82,13 +93,13 @@ function isPreviousNodeValid(editor, path) {
82
93
  const entry = Editor.previous(editor, { at: path });
83
94
  if (!entry)
84
95
  return true;
85
- return Text.isText(entry[0]) && (endsWithSeparator(entry[0].text) || entry[0].text === "");
96
+ return isText(entry[0]) && (endsWithSeparator(entry[0].text) || entry[0].text === "");
86
97
  }
87
98
  function isNextNodeValid(editor, path) {
88
99
  const entry = Editor.next(editor, { at: path });
89
100
  if (!entry)
90
101
  return true;
91
- return Text.isText(entry[0]) && (startsWithSeparator(entry[0].text) || entry[0].text === "");
102
+ return isText(entry[0]) && (startsWithSeparator(entry[0].text) || entry[0].text === "");
92
103
  }
93
104
  function isContentAroundValid(editor, entry, start, end) {
94
105
  const [node, path] = entry;
@@ -101,7 +112,7 @@ const handleLinkEdit = (editor, entry) => {
101
112
  const [node, path] = entry;
102
113
  const children = Node.children(editor, path);
103
114
  for (const [child] of children) {
104
- if (Text.isText(child))
115
+ if (isText(child))
105
116
  continue;
106
117
  Transforms.unwrapNodes(editor, { at: path });
107
118
  return;
@@ -1 +1 @@
1
- {"version":3,"file":"auto-links.mjs","sources":["../../../src/slate/plugins/auto-links.ts"],"sourcesContent":["import type { NodeEntry } from \"slate\";\nimport { Editor, Element, Node, Path, Range, Text, Transforms } from \"slate\";\n\nimport type { ComposerBodyAutoLink } from \"../../types\";\n\n/**\n * This implementation is inspired by Lexical's AutoLink plugin.\n * Additional modifications and features were added to adapt it to our specific needs.\n *\n * Original Lexical AutoLink plugin can be found at [Lexical's Github Repository](https://github.com/facebook/lexical/blob/main/packages/lexical-react/src/LexicalAutoLinkPlugin.ts)\n */\nexport function withAutoLinks(editor: Editor): Editor {\n const { isInline, normalizeNode, deleteBackward } = editor;\n\n editor.isInline = (element) => {\n return element.type === \"auto-link\" ? true : isInline(element);\n };\n\n editor.normalizeNode = (entry) => {\n const [node, path] = entry;\n\n if (Text.isText(node)) {\n const parentNode = Node.parent(editor, path);\n\n if (isComposerBodyAutoLink(parentNode)) {\n const parentPath = Path.parent(path);\n handleLinkEdit(editor, [parentNode, parentPath]);\n } else {\n handleLinkCreate(editor, [node, path]);\n handleNeighbours(editor, [node, path]);\n }\n }\n\n normalizeNode(entry);\n };\n\n editor.deleteBackward = (unit) => {\n deleteBackward(unit);\n const { selection } = editor;\n if (!selection) return;\n\n if (!Range.isCollapsed(selection)) return;\n\n const [match] = Editor.nodes(editor, {\n at: selection,\n match: isComposerBodyAutoLink,\n mode: \"lowest\",\n });\n\n if (!match) return;\n\n Transforms.unwrapNodes(editor, {\n match: isComposerBodyAutoLink,\n });\n };\n\n return editor;\n}\n\nexport function isComposerBodyAutoLink(\n node: Node\n): node is ComposerBodyAutoLink {\n return Element.isElement(node) && node.type === \"auto-link\";\n}\n\n/**\n * 1. ((https?:\\/\\/(www\\.)?)|(www\\.))\n * - Matches 'http://' or 'https://' optionally followed by 'www.', or just 'www.'\n *\n * 2. [-a-zA-Z0-9@:%._+~#=]{1,256}\n * - Matches any character in the set [-a-zA-Z0-9@:%._+~#=] between 1 and 256 times, often found in the domain and subdomain part of the URL\n *\n * 3. \\.[a-zA-Z0-9()]{1,6}\n * - Matches a period followed by any character in the set [a-zA-Z0-9()] between 1 and 6 times, usually indicating the domain extension like .com, .org, etc.\n *\n * 4. \\b\n * - Represents a word boundary, ensuring that the characters following cannot be part of a different word\n *\n * 5. ([-a-zA-Z0-9().@:%_+~#?&//=]*)\n * - Matches any character in the set [-a-zA-Z0-9().@:%_+~#?&//=] between 0 and unlimited times, often found in the path, query parameters, or anchor part of the URL\n *\n * Matching URLs:\n * - http://www.example.com\n * - https://www.example.com\n * - www.example.com\n * - https://example.com/path?query=param#anchor\n *\n * Non-Matching URLs:\n * - http:/example.com (malformed scheme)\n * - example (missing scheme and domain extension)\n * - ftp://example.com (ftp scheme is not supported)\n * - example.com (missing scheme)\n */\nconst URL_REGEX =\n /((https?:\\/\\/(www\\.)?)|(www\\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9().@:%_+~#?&//=]*)/;\n\nconst PUNCTUATION_OR_SPACE = /[.,;!?\\s()]/;\n\nconst PERIOD_OR_QUESTION_MARK_FOLLOWED_BY_ALPHANUMERIC = /^[.?][a-zA-Z0-9]+/;\n\nconst PARENTHESES = /[()]/;\n\n/**\n * Helper function to check if a character is a separator (punctuation or space)\n * @param char The character to check\n * @returns Whether the character is a separator or not\n */\nfunction isSeparator(char: string): boolean {\n return PUNCTUATION_OR_SPACE.test(char);\n}\n\n/**\n * Helper function to check if a text content ends with a separator (punctuation or space)\n * @param textContent The text content to check\n * @returns Whether the text content ends with a separator or not\n */\nfunction endsWithSeparator(textContent: string): boolean {\n return isSeparator(textContent[textContent.length - 1]);\n}\n\n/**\n * Helper function to check if a text content starts with a separator (punctuation or space)\n * @param textContent The text content to check\n * @returns Whether the text content starts with a separator or not\n */\nfunction startsWithSeparator(textContent: string): boolean {\n return isSeparator(textContent[0]);\n}\n\n/**\n * Helper function to check if a text content ends with a period or question mark\n * @param textContent The text content to check\n * @returns Whether the text content ends with a period or not\n */\nfunction endsWithPeriodOrQuestionMark(textContent: string): boolean {\n return (\n textContent[textContent.length - 1] === \".\" ||\n textContent[textContent.length - 1] === \"?\"\n );\n}\n\n/**\n * Helper function to get the \"logical length\" of a URL, taking into account things like opening/closing parentheses\n * @param url The URL to check\n * @returns The \"logical length\" of the URL\n */\nfunction getUrlLogicalLength(url: string): number {\n if (!PARENTHESES.test(url)) {\n return url.length;\n }\n\n let logicalLength = 0;\n let parenthesesCount = 0;\n\n for (const character of url) {\n if (character === \"(\") {\n parenthesesCount++;\n }\n\n if (character === \")\") {\n parenthesesCount--;\n\n if (parenthesesCount < 0) {\n break;\n }\n }\n\n logicalLength++;\n }\n\n return logicalLength;\n}\n\n/**\n * Helper function to check if the previous node is valid (text node that ends with a separator or is empty)\n */\nfunction isPreviousNodeValid(editor: Editor, path: Path): boolean {\n const entry = Editor.previous(editor, { at: path });\n if (!entry) return true;\n\n return (\n Text.isText(entry[0]) &&\n (endsWithSeparator(entry[0].text) || entry[0].text === \"\")\n );\n}\n\n/**\n * Helper function to check if the next node is valid (text node that starts with a separator or is empty)\n */\nfunction isNextNodeValid(editor: Editor, path: Path): boolean {\n const entry = Editor.next(editor, { at: path });\n if (!entry) return true;\n\n return (\n Text.isText(entry[0]) &&\n (startsWithSeparator(entry[0].text) || entry[0].text === \"\")\n );\n}\n\n/**\n * Helper function to check if the content around a text node is valid.\n * @param editor\n * @param entry\n * @param start\n * @param end\n * @returns\n */\nfunction isContentAroundValid(\n editor: Editor,\n entry: NodeEntry<Text>,\n start: number,\n end: number\n): boolean {\n const [node, path] = entry;\n const text = node.text;\n\n const contentBeforeIsValid =\n start > 0\n ? isSeparator(text[start - 1])\n : isPreviousNodeValid(editor, path);\n\n const contentAfterIsValid =\n end < text.length ? isSeparator(text[end]) : isNextNodeValid(editor, path);\n\n return contentBeforeIsValid && contentAfterIsValid;\n}\n\nconst handleLinkEdit = (\n editor: Editor,\n entry: NodeEntry<ComposerBodyAutoLink>\n) => {\n const [node, path] = entry;\n\n // Step 1: Ensure that the Link node only contains text nodes as children\n const children = Node.children(editor, path);\n for (const [child] of children) {\n if (Text.isText(child)) continue;\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n // Attempt to match the text content (of the Link node) against the URL regex\n const text = Node.string(node);\n const match = URL_REGEX.exec(text);\n\n // Step 2: Ensure that the text content of the Link node matches the URL regex and is identical to the match\n if (!match || match[0] !== text) {\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n\n // Step 3: Ensure that if the text content of the Link node ends with a period, we unwrap the Link node and wrap the text before the period in a new Link node\n if (endsWithPeriodOrQuestionMark(text)) {\n Transforms.unwrapNodes(editor, { at: path });\n\n const textBeforePeriod = text.slice(0, text.length - 1);\n\n // Remove the last character from the link text and wrap the remaining text in a new link node\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: textBeforePeriod,\n children: [],\n },\n {\n at: {\n anchor: { path, offset: 0 },\n focus: { path, offset: textBeforePeriod.length },\n },\n split: true,\n }\n );\n return;\n }\n\n // Step 4: Allow some conditions to shorten the URL (e.g. supporting parentheses but only if they are balanced)\n const logicalLength = getUrlLogicalLength(text);\n\n if (logicalLength < text.length) {\n Transforms.unwrapNodes(editor, { at: path });\n\n const logicalText = text.slice(0, logicalLength);\n\n // Keep the \"logical\" text and wrap it in a new link node\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: logicalText,\n children: [],\n },\n {\n at: {\n anchor: { path, offset: 0 },\n focus: { path, offset: logicalText.length },\n },\n split: true,\n }\n );\n return;\n }\n\n // Step 5: Ensure that the text content of the Link node is surrounded by separators or the start/end of the text content\n if (!isPreviousNodeValid(editor, path) || !isNextNodeValid(editor, path)) {\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n\n // Step 6: Ensure that the url attribute of the Link node is identical to its text content\n if (node.url !== text) {\n Transforms.setNodes(editor, { url: match[0] }, { at: path });\n return;\n }\n};\n\nconst handleLinkCreate = (editor: Editor, entry: NodeEntry<Text>) => {\n const [node, path] = entry;\n\n // Step 1: Ensure that the text content of the node matches the URL regex\n const match = URL_REGEX.exec(node.text);\n if (!match) return;\n\n const start = match.index;\n const end = start + match[0].length;\n\n // Step 2: Ensure that the content around the node is valid\n if (!isContentAroundValid(editor, entry, start, end)) return;\n\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: match[0],\n children: [],\n },\n {\n at: {\n anchor: { path, offset: start },\n focus: { path, offset: end },\n },\n split: true,\n }\n );\n return;\n};\n\nconst handleNeighbours = (editor: Editor, entry: NodeEntry<Text>) => {\n const [node, path] = entry;\n const text = node.text;\n\n const previousSibling = Editor.previous(editor, { at: path });\n\n if (previousSibling && isComposerBodyAutoLink(previousSibling[0])) {\n if (PERIOD_OR_QUESTION_MARK_FOLLOWED_BY_ALPHANUMERIC.test(text)) {\n Transforms.unwrapNodes(editor, { at: previousSibling[1] });\n Transforms.mergeNodes(editor, { at: path });\n return;\n }\n\n if (!startsWithSeparator(text)) {\n Transforms.unwrapNodes(editor, { at: previousSibling[1] });\n return;\n }\n }\n\n const nextSibling = Editor.next(editor, { at: path });\n if (\n nextSibling &&\n isComposerBodyAutoLink(nextSibling[0]) &&\n !endsWithSeparator(text)\n ) {\n Transforms.unwrapNodes(editor, { at: nextSibling[1] });\n return;\n }\n};\n"],"names":[],"mappings":";;AAWO,SAAS,cAAc,MAAwB,EAAA;AACpD,EAAA,MAAM,EAAE,QAAA,EAAU,aAAe,EAAA,cAAA,EAAmB,GAAA,MAAA,CAAA;AAEpD,EAAO,MAAA,CAAA,QAAA,GAAW,CAAC,OAAY,KAAA;AAC7B,IAAA,OAAO,OAAQ,CAAA,IAAA,KAAS,WAAc,GAAA,IAAA,GAAO,SAAS,OAAO,CAAA,CAAA;AAAA,GAC/D,CAAA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M,CAAA,CAAA;AACpD,EACE,IAAA,WAAA,IACA,uBAAuB,WAAY,CAAA,CAAA,CAAE,KACrC,CAAC,iBAAA,CAAkB,IAAI,CACvB,EAAA;AACA,IAAA,UAAA,CAAW,YAAY,MAAQ,EAAA,EAAE,EAAI,EAAA,WAAA,CAAY,IAAI,CAAA,CAAA;AACrD,IAAA,OAAA;AAAA,GACF;AACF,CAAA;;;;"}
1
+ {"version":3,"file":"auto-links.mjs","sources":["../../../src/slate/plugins/auto-links.ts"],"sourcesContent":["import type { NodeEntry, Text } from \"slate\";\nimport { Editor, Element, Node, Path, Range, Transforms } from \"slate\";\n\nimport type { ComposerBodyAutoLink } from \"../../types\";\nimport { isPlainText, isText } from \"../utils/is-text\";\nimport { isComposerBodyCustomLink } from \"./custom-links\";\n\n/**\n * This implementation is inspired by Lexical's AutoLink plugin.\n * Additional modifications and features were added to adapt it to our specific needs.\n *\n * Original Lexical AutoLink plugin can be found at [Lexical's Github Repository](https://github.com/facebook/lexical/blob/main/packages/lexical-react/src/LexicalAutoLinkPlugin.ts)\n */\nexport function withAutoLinks(editor: Editor): Editor {\n const { isInline, normalizeNode, deleteBackward } = editor;\n\n editor.isInline = (element) => {\n return element.type === \"auto-link\" ? true : isInline(element);\n };\n\n editor.normalizeNode = (entry) => {\n const [node, path] = entry;\n\n // Prevent auto links from being created inside custom links\n if (isComposerBodyCustomLink(node)) {\n return;\n }\n\n if (isText(node)) {\n const parentNode = Node.parent(editor, path);\n\n // Prevent auto links from being created inside custom links\n if (isComposerBodyCustomLink(parentNode)) {\n return;\n } else if (isComposerBodyAutoLink(parentNode)) {\n const parentPath = Path.parent(path);\n handleLinkEdit(editor, [parentNode, parentPath]);\n\n // Prevent rich text within auto links by removing all marks of inner text nodes\n if (!isPlainText(node)) {\n const marks = Object.keys(node).filter((key) => key !== \"text\");\n\n Transforms.unsetNodes(editor, marks, { at: path });\n }\n } else {\n handleLinkCreate(editor, [node, path]);\n handleNeighbours(editor, [node, path]);\n }\n }\n\n normalizeNode(entry);\n };\n\n editor.deleteBackward = (unit) => {\n deleteBackward(unit);\n const { selection } = editor;\n if (!selection) return;\n\n if (!Range.isCollapsed(selection)) return;\n\n const [match] = Editor.nodes(editor, {\n at: selection,\n match: isComposerBodyAutoLink,\n mode: \"lowest\",\n });\n\n if (!match) return;\n\n Transforms.unwrapNodes(editor, {\n match: isComposerBodyAutoLink,\n });\n };\n\n return editor;\n}\n\nexport function isComposerBodyAutoLink(\n node: Node\n): node is ComposerBodyAutoLink {\n return Element.isElement(node) && node.type === \"auto-link\";\n}\n\n/**\n * 1. ((https?:\\/\\/(www\\.)?)|(www\\.))\n * - Matches 'http://' or 'https://' optionally followed by 'www.', or just 'www.'\n *\n * 2. [-a-zA-Z0-9@:%._+~#=]{1,256}\n * - Matches any character in the set [-a-zA-Z0-9@:%._+~#=] between 1 and 256 times, often found in the domain and subdomain part of the URL\n *\n * 3. \\.[a-zA-Z0-9()]{1,6}\n * - Matches a period followed by any character in the set [a-zA-Z0-9()] between 1 and 6 times, usually indicating the domain extension like .com, .org, etc.\n *\n * 4. \\b\n * - Represents a word boundary, ensuring that the characters following cannot be part of a different word\n *\n * 5. ([-a-zA-Z0-9().@:%_+~#?&//=]*)\n * - Matches any character in the set [-a-zA-Z0-9().@:%_+~#?&//=] between 0 and unlimited times, often found in the path, query parameters, or anchor part of the URL\n *\n * Matching URLs:\n * - http://www.example.com\n * - https://www.example.com\n * - www.example.com\n * - https://example.com/path?query=param#anchor\n *\n * Non-Matching URLs:\n * - http:/example.com (malformed scheme)\n * - example (missing scheme and domain extension)\n * - ftp://example.com (ftp scheme is not supported)\n * - example.com (missing scheme)\n */\nconst URL_REGEX =\n /((https?:\\/\\/(www\\.)?)|(www\\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9().@:%_+~#?&//=]*)/;\n\nconst PUNCTUATION_OR_SPACE = /[.,;!?\\s()]/;\n\nconst PERIOD_OR_QUESTION_MARK_FOLLOWED_BY_ALPHANUMERIC = /^[.?][a-zA-Z0-9]+/;\n\nconst PARENTHESES = /[()]/;\n\n/**\n * Helper function to check if a character is a separator (punctuation or space)\n * @param char The character to check\n * @returns Whether the character is a separator or not\n */\nfunction isSeparator(char: string): boolean {\n return PUNCTUATION_OR_SPACE.test(char);\n}\n\n/**\n * Helper function to check if a text content ends with a separator (punctuation or space)\n * @param textContent The text content to check\n * @returns Whether the text content ends with a separator or not\n */\nfunction endsWithSeparator(textContent: string): boolean {\n return isSeparator(textContent[textContent.length - 1]);\n}\n\n/**\n * Helper function to check if a text content starts with a separator (punctuation or space)\n * @param textContent The text content to check\n * @returns Whether the text content starts with a separator or not\n */\nfunction startsWithSeparator(textContent: string): boolean {\n return isSeparator(textContent[0]);\n}\n\n/**\n * Helper function to check if a text content ends with a period or question mark\n * @param textContent The text content to check\n * @returns Whether the text content ends with a period or not\n */\nfunction endsWithPeriodOrQuestionMark(textContent: string): boolean {\n return (\n textContent[textContent.length - 1] === \".\" ||\n textContent[textContent.length - 1] === \"?\"\n );\n}\n\n/**\n * Helper function to get the \"logical length\" of a URL, taking into account things like opening/closing parentheses\n * @param url The URL to check\n * @returns The \"logical length\" of the URL\n */\nfunction getUrlLogicalLength(url: string): number {\n if (!PARENTHESES.test(url)) {\n return url.length;\n }\n\n let logicalLength = 0;\n let parenthesesCount = 0;\n\n for (const character of url) {\n if (character === \"(\") {\n parenthesesCount++;\n }\n\n if (character === \")\") {\n parenthesesCount--;\n\n if (parenthesesCount < 0) {\n break;\n }\n }\n\n logicalLength++;\n }\n\n return logicalLength;\n}\n\n/**\n * Helper function to check if the previous node is valid (text node that ends with a separator or is empty)\n */\nfunction isPreviousNodeValid(editor: Editor, path: Path): boolean {\n const entry = Editor.previous(editor, { at: path });\n if (!entry) return true;\n\n return (\n isText(entry[0]) &&\n (endsWithSeparator(entry[0].text) || entry[0].text === \"\")\n );\n}\n\n/**\n * Helper function to check if the next node is valid (text node that starts with a separator or is empty)\n */\nfunction isNextNodeValid(editor: Editor, path: Path): boolean {\n const entry = Editor.next(editor, { at: path });\n if (!entry) return true;\n\n return (\n isText(entry[0]) &&\n (startsWithSeparator(entry[0].text) || entry[0].text === \"\")\n );\n}\n\n/**\n * Helper function to check if the content around a text node is valid.\n * @param editor\n * @param entry\n * @param start\n * @param end\n * @returns\n */\nfunction isContentAroundValid(\n editor: Editor,\n entry: NodeEntry<Text>,\n start: number,\n end: number\n): boolean {\n const [node, path] = entry;\n const text = node.text;\n\n const contentBeforeIsValid =\n start > 0\n ? isSeparator(text[start - 1])\n : isPreviousNodeValid(editor, path);\n\n const contentAfterIsValid =\n end < text.length ? isSeparator(text[end]) : isNextNodeValid(editor, path);\n\n return contentBeforeIsValid && contentAfterIsValid;\n}\n\nconst handleLinkEdit = (\n editor: Editor,\n entry: NodeEntry<ComposerBodyAutoLink>\n) => {\n const [node, path] = entry;\n\n // Step 1: Ensure that the Link node only contains text nodes as children\n const children = Node.children(editor, path);\n for (const [child] of children) {\n if (isText(child)) continue;\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n // Attempt to match the text content (of the Link node) against the URL regex\n const text = Node.string(node);\n const match = URL_REGEX.exec(text);\n\n // Step 2: Ensure that the text content of the Link node matches the URL regex and is identical to the match\n if (!match || match[0] !== text) {\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n\n // Step 3: Ensure that if the text content of the Link node ends with a period, we unwrap the Link node and wrap the text before the period in a new Link node\n if (endsWithPeriodOrQuestionMark(text)) {\n Transforms.unwrapNodes(editor, { at: path });\n\n const textBeforePeriod = text.slice(0, text.length - 1);\n\n // Remove the last character from the link text and wrap the remaining text in a new link node\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: textBeforePeriod,\n children: [],\n },\n {\n at: {\n anchor: { path, offset: 0 },\n focus: { path, offset: textBeforePeriod.length },\n },\n split: true,\n }\n );\n return;\n }\n\n // Step 4: Allow some conditions to shorten the URL (e.g. supporting parentheses but only if they are balanced)\n const logicalLength = getUrlLogicalLength(text);\n\n if (logicalLength < text.length) {\n Transforms.unwrapNodes(editor, { at: path });\n\n const logicalText = text.slice(0, logicalLength);\n\n // Keep the \"logical\" text and wrap it in a new link node\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: logicalText,\n children: [],\n },\n {\n at: {\n anchor: { path, offset: 0 },\n focus: { path, offset: logicalText.length },\n },\n split: true,\n }\n );\n return;\n }\n\n // Step 5: Ensure that the text content of the Link node is surrounded by separators or the start/end of the text content\n if (!isPreviousNodeValid(editor, path) || !isNextNodeValid(editor, path)) {\n Transforms.unwrapNodes(editor, { at: path });\n return;\n }\n\n // Step 6: Ensure that the url attribute of the Link node is identical to its text content\n if (node.url !== text) {\n Transforms.setNodes(editor, { url: match[0] }, { at: path });\n return;\n }\n};\n\nconst handleLinkCreate = (editor: Editor, entry: NodeEntry<Text>) => {\n const [node, path] = entry;\n\n // Step 1: Ensure that the text content of the node matches the URL regex\n const match = URL_REGEX.exec(node.text);\n if (!match) return;\n\n const start = match.index;\n const end = start + match[0].length;\n\n // Step 2: Ensure that the content around the node is valid\n if (!isContentAroundValid(editor, entry, start, end)) return;\n\n Transforms.wrapNodes<ComposerBodyAutoLink>(\n editor,\n {\n type: \"auto-link\",\n url: match[0],\n children: [],\n },\n {\n at: {\n anchor: { path, offset: start },\n focus: { path, offset: end },\n },\n split: true,\n }\n );\n return;\n};\n\nconst handleNeighbours = (editor: Editor, entry: NodeEntry<Text>) => {\n const [node, path] = entry;\n const text = node.text;\n\n const previousSibling = Editor.previous(editor, { at: path });\n\n if (previousSibling && isComposerBodyAutoLink(previousSibling[0])) {\n if (PERIOD_OR_QUESTION_MARK_FOLLOWED_BY_ALPHANUMERIC.test(text)) {\n Transforms.unwrapNodes(editor, { at: previousSibling[1] });\n Transforms.mergeNodes(editor, { at: path });\n return;\n }\n\n if (!startsWithSeparator(text)) {\n Transforms.unwrapNodes(editor, { at: previousSibling[1] });\n return;\n }\n }\n\n const nextSibling = Editor.next(editor, { at: path });\n if (\n nextSibling &&\n isComposerBodyAutoLink(nextSibling[0]) &&\n !endsWithSeparator(text)\n ) {\n Transforms.unwrapNodes(editor, { at: nextSibling[1] });\n return;\n }\n};\n"],"names":[],"mappings":";;;;AAaO,SAAS,cAAc,MAAwB,EAAA;AACpD,EAAA,MAAM,EAAE,QAAA,EAAU,aAAe,EAAA,cAAA,EAAmB,GAAA,MAAA,CAAA;AAEpD,EAAO,MAAA,CAAA,QAAA,GAAW,CAAC,OAAY,KAAA;AAC7B,IAAA,OAAO,OAAQ,CAAA,IAAA,KAAS,WAAc,GAAA,IAAA,GAAO,SAAS,OAAO,CAAA,CAAA;AAAA,GAC/D,CAAA;AAEA,EAAO,MAAA,CAAA,aAAA,GAAgB,CAAC,KAAU,KAAA;AAChC,IAAM,MAAA,CAAC,IAAM,EAAA,IAAI,CAAI,GAAA,KAAA,CAAA;AAGrB,IAAI,IAAA,wBAAA,CAAyB,IAAI,CAAG,EAAA;AAClC,MAAA,OAAA;AAAA,KACF;AAEA,IAAI,IAAA,MAAA,CAAO,IAAI,CAAG,EAAA;AAChB,MAAA,MAAM,UAAa,GAAA,IAAA,CAAK,MAAO,CAAA,MAAA,EAAQ,IAAI,CAAA,CAAA;AAG3C,MAAI,IAAA,wBAAA,CAAyB,UAAU,CAAG,EAAA;AACxC,QAAA,OAAA;AAAA,OACF,MAAA,IAAW,sBAAuB,CAAA,UAAU,CAAG,EAAA;AAC7C,QAAM,MAAA,UAAA,GAAa,IAAK,CAAA,MAAA,CAAO,IAAI,CAAA,CAAA;AACnC,QAAA,cAAA,CAAe,MAAQ,EAAA,CAAC,UAAY,EAAA,UAAU,CAAC,CAAA,CAAA;AAG/C,QAAI,IAAA,CAAC,WAAY,CAAA,IAAI,CAAG,EAAA;AACtB,UAAM,MAAA,KAAA,GAAQ,OAAO,IAAK,CAAA,IAAI,EAAE,MAAO,CAAA,CAAC,GAAQ,K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C,MAAA,EAAgB,KAA2B,KAAA;AACnE,EAAM,MAAA,CAAC,IAAM,EAAA,IAAI,CAAI,GAAA,KAAA,CAAA;AACrB,EAAA,MAAM,OAAO,IAAK,CAAA,IAAA,CAAA;AAElB,EAAA,MAAM,kBAAkB,MAAO,CAAA,QAAA,CAAS,QAAQ,EAAE,EAAA,EAAI,MAAM,CAAA,CAAA;AAE5D,EAAA,IAAI,eAAmB,IAAA,sBAAA,CAAuB,eAAgB,CAAA,CAAA,CAAE,CAAG,EAAA;AACjE,IAAI,IAAA,gDAAA,CAAiD,IAAK,CAAA,IAAI,CAAG,EAAA;AAC/D,MAAA,UAAA,CAAW,YAAY,MAAQ,EAAA,EAAE,EAAI,EAAA,eAAA,CAAgB,IAAI,CAAA,CAAA;AACzD,MAAA,UAAA,CAAW,UAAW,CAAA,MAAA,EAAQ,EAAE,EAAA,EAAI,MAAM,CAAA,CAAA;AAC1C,MAAA,OAAA;AAAA,KACF;AAEA,IAAI,IAAA,CAAC,mBAAoB,CAAA,IAAI,CAAG,EAAA;AAC9B,MAAA,UAAA,CAAW,YAAY,MAAQ,EAAA,EAAE,EAAI,EAAA,eAAA,CAAgB,IAAI,CAAA,CAAA;AACzD,MAAA,OAAA;AAAA,KACF;AAAA,GACF;AAEA,EAAA,MAAM,cAAc,MAAO,CAAA,IAAA,CAAK,QAAQ,EAAE,EAAA,EAAI,MAAM,CAAA,CAAA;AACpD,EACE,IAAA,WAAA,IACA,uBAAuB,WAAY,CAAA,CAAA,CAAE,KACrC,CAAC,iBAAA,CAAkB,IAAI,CACvB,EAAA;AACA,IAAA,UAAA,CAAW,YAAY,MAAQ,EAAA,EAAE,EAAI,EAAA,WAAA,CAAY,IAAI,CAAA,CAAA;AACrD,IAAA,OAAA;AAAA,GACF;AACF,CAAA;;;;"}
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ var slate = require('slate');
4
+ var isText = require('../utils/is-text.js');
5
+ var selectionContainsInlines = require('../utils/selection-contains-inlines.js');
6
+
7
+ function isUrl(string) {
8
+ try {
9
+ new URL(string);
10
+ return true;
11
+ } catch (_) {
12
+ return false;
13
+ }
14
+ }
15
+ function withCustomLinks(editor) {
16
+ const { isInline, normalizeNode, insertData } = editor;
17
+ editor.isInline = (element) => {
18
+ return element.type === "custom-link" ? true : isInline(element);
19
+ };
20
+ editor.normalizeNode = (entry) => {
21
+ const [node, path] = entry;
22
+ if (isText.isText(node)) {
23
+ const parentNode = slate.Node.parent(editor, path);
24
+ if (isComposerBodyCustomLink(parentNode)) {
25
+ if (!isText.isPlainText(node)) {
26
+ const marks = Object.keys(node).filter((key) => key !== "text");
27
+ slate.Transforms.unsetNodes(editor, marks, { at: path });
28
+ }
29
+ }
30
+ }
31
+ normalizeNode(entry);
32
+ };
33
+ editor.insertData = (data) => {
34
+ const { selection } = editor;
35
+ const pastedText = data.getData("text/plain");
36
+ let shouldInvokeDefaultBehavior = true;
37
+ if (selection && !slate.Range.isCollapsed(selection)) {
38
+ if (selection.anchor.path[0] === selection.focus.path[0]) {
39
+ if (isUrl(pastedText)) {
40
+ if (!selectionContainsInlines.selectionContainsInlines(editor, (node) => !isText.isText(node))) {
41
+ slate.Transforms.wrapNodes(
42
+ editor,
43
+ {
44
+ type: "custom-link",
45
+ url: pastedText,
46
+ children: []
47
+ },
48
+ {
49
+ at: selection,
50
+ split: true,
51
+ match: isText.isPlainText
52
+ }
53
+ );
54
+ shouldInvokeDefaultBehavior = false;
55
+ }
56
+ }
57
+ }
58
+ }
59
+ if (shouldInvokeDefaultBehavior) {
60
+ insertData(data);
61
+ }
62
+ };
63
+ return editor;
64
+ }
65
+ function isComposerBodyCustomLink(node) {
66
+ return slate.Element.isElement(node) && node.type === "custom-link";
67
+ }
68
+
69
+ exports.isComposerBodyCustomLink = isComposerBodyCustomLink;
70
+ exports.withCustomLinks = withCustomLinks;
71
+ //# sourceMappingURL=custom-links.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"custom-links.js","sources":["../../../src/slate/plugins/custom-links.ts"],"sourcesContent":["import type { Editor } from \"slate\";\nimport { Element, Node, Range, Transforms } from \"slate\";\n\nimport type { ComposerBodyCustomLink } from \"../../types\";\nimport { isPlainText, isText } from \"../utils/is-text\";\nimport { selectionContainsInlines } from \"../utils/selection-contains-inlines\";\n\nfunction isUrl(string: string) {\n try {\n new URL(string);\n return true;\n } catch (_) {\n return false;\n }\n}\n\nexport function withCustomLinks(editor: Editor): Editor {\n const { isInline, normalizeNode, insertData } = editor;\n\n editor.isInline = (element) => {\n return element.type === \"custom-link\" ? true : isInline(element);\n };\n\n editor.normalizeNode = (entry) => {\n const [node, path] = entry;\n\n if (isText(node)) {\n const parentNode = Node.parent(editor, path);\n\n // Prevent rich text within custom links by removing all marks of inner text nodes\n if (isComposerBodyCustomLink(parentNode)) {\n if (!isPlainText(node)) {\n const marks = Object.keys(node).filter((key) => key !== \"text\");\n\n Transforms.unsetNodes(editor, marks, { at: path });\n }\n }\n }\n\n normalizeNode(entry);\n };\n\n // Create custom links when pasting URLs while some text is selected\n editor.insertData = (data) => {\n const { selection } = editor;\n const pastedText = data.getData(\"text/plain\");\n\n // Keep track of whether the default behavior should be invoked\n // This allows us to sequence multiple conditions in a performant way,\n // ordering them by likelihood/cost and stopping early whenever possible\n let shouldInvokeDefaultBehavior = true;\n\n // Check if the selection is a range\n if (selection && !Range.isCollapsed(selection)) {\n // Check if the selection is contained in a single block\n if (selection.anchor.path[0] === selection.focus.path[0]) {\n // Check if the pasted text is a valid URL\n if (isUrl(pastedText)) {\n // Check if the selection only contains (rich and/or plain) text nodes\n if (!selectionContainsInlines(editor, (node) => !isText(node))) {\n // If all conditions are met, wrap the selected nodes in a custom link\n Transforms.wrapNodes<ComposerBodyCustomLink>(\n editor,\n {\n type: \"custom-link\",\n url: pastedText,\n children: [],\n },\n {\n at: selection,\n split: true,\n match: isPlainText,\n }\n );\n shouldInvokeDefaultBehavior = false;\n }\n }\n }\n }\n\n if (shouldInvokeDefaultBehavior) {\n insertData(data);\n }\n };\n\n return editor;\n}\n\nexport function isComposerBodyCustomLink(\n node: Node\n): node is ComposerBodyCustomLink {\n return Element.isElement(node) && node.type === \"custom-link\";\n}\n"],"names":["isText","Node","isPlainText","Transforms","Range","selectionContainsInlines","Element"],"mappings":";;;;;;AAOA,SAAS,MAAM,MAAgB,EAAA;AAC7B,EAAI,IAAA;AACF,IAAA,IAAI,IAAI,MAAM,CAAA,CAAA;AACd,IAAO,OAAA,IAAA,CAAA;AAAA,WACA,CAAP,EAAA;AACA,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF,CAAA;AAEO,SAAS,gBAAgB,MAAwB,EAAA;AACtD,EAAA,MAAM,EAAE,QAAA,EAAU,aAAe,EAAA,UAAA,EAAe,GAAA,MAAA,CAAA;AAEhD,EAAO,MAAA,CAAA,QAAA,GAAW,CAAC,OAAY,KAAA;AAC7B,IAAA,OAAO,OAAQ,CAAA,IAAA,KAAS,aAAgB,GAAA,IAAA,GAAO,SAAS,OAAO,CAAA,CAAA;AAAA,GACjE,CAAA;AAEA,EAAO,MAAA,CAAA,aAAA,GAAgB,CAAC,KAAU,KAAA;AAChC,IAAM,MAAA,CAAC,IAAM,EAAA,IAAI,CAAI,GAAA,KAAA,CAAA;AAErB,IAAI,IAAAA,aAAA,CAAO,IAAI,CAAG,EAAA;AAChB,MAAA,MAAM,UAAa,GAAAC,UAAA,CAAK,MAAO,CAAA,MAAA,EAAQ,IAAI,CAAA,CAAA;AAG3C,MAAI,IAAA,wBAAA,CAAyB,UAAU,CAAG,EAAA;AACxC,QAAI,IAAA,CAACC,kBAAY,CAAA,IAAI,CAAG,EAAA;AACtB,UAAM,MAAA,KAAA,GAAQ,OAAO,IAAK,CAAA,IAAI,EAAE,MAAO,CAAA,CAAC,GAAQ,KAAA,GAAA,KAAQ,MAAM,CAAA,CAAA;AAE9D,UAAAC,gBAAA,CAAW,WAAW,MAAQ,EAAA,KAAA,EAAO,EAAE,EAAA,EAAI,MAAM,CAAA,CAAA;AAAA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,IAAI,CAAA,IAAK,KAAK,IAAS,KAAA,aAAA,CAAA;AAClD;;;;;"}
@@ -0,0 +1,68 @@
1
+ import { Element, Node, Transforms, Range } from 'slate';
2
+ import { isText, isPlainText } from '../utils/is-text.mjs';
3
+ import { selectionContainsInlines } from '../utils/selection-contains-inlines.mjs';
4
+
5
+ function isUrl(string) {
6
+ try {
7
+ new URL(string);
8
+ return true;
9
+ } catch (_) {
10
+ return false;
11
+ }
12
+ }
13
+ function withCustomLinks(editor) {
14
+ const { isInline, normalizeNode, insertData } = editor;
15
+ editor.isInline = (element) => {
16
+ return element.type === "custom-link" ? true : isInline(element);
17
+ };
18
+ editor.normalizeNode = (entry) => {
19
+ const [node, path] = entry;
20
+ if (isText(node)) {
21
+ const parentNode = Node.parent(editor, path);
22
+ if (isComposerBodyCustomLink(parentNode)) {
23
+ if (!isPlainText(node)) {
24
+ const marks = Object.keys(node).filter((key) => key !== "text");
25
+ Transforms.unsetNodes(editor, marks, { at: path });
26
+ }
27
+ }
28
+ }
29
+ normalizeNode(entry);
30
+ };
31
+ editor.insertData = (data) => {
32
+ const { selection } = editor;
33
+ const pastedText = data.getData("text/plain");
34
+ let shouldInvokeDefaultBehavior = true;
35
+ if (selection && !Range.isCollapsed(selection)) {
36
+ if (selection.anchor.path[0] === selection.focus.path[0]) {
37
+ if (isUrl(pastedText)) {
38
+ if (!selectionContainsInlines(editor, (node) => !isText(node))) {
39
+ Transforms.wrapNodes(
40
+ editor,
41
+ {
42
+ type: "custom-link",
43
+ url: pastedText,
44
+ children: []
45
+ },
46
+ {
47
+ at: selection,
48
+ split: true,
49
+ match: isPlainText
50
+ }
51
+ );
52
+ shouldInvokeDefaultBehavior = false;
53
+ }
54
+ }
55
+ }
56
+ }
57
+ if (shouldInvokeDefaultBehavior) {
58
+ insertData(data);
59
+ }
60
+ };
61
+ return editor;
62
+ }
63
+ function isComposerBodyCustomLink(node) {
64
+ return Element.isElement(node) && node.type === "custom-link";
65
+ }
66
+
67
+ export { isComposerBodyCustomLink, withCustomLinks };
68
+ //# sourceMappingURL=custom-links.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"custom-links.mjs","sources":["../../../src/slate/plugins/custom-links.ts"],"sourcesContent":["import type { Editor } from \"slate\";\nimport { Element, Node, Range, Transforms } from \"slate\";\n\nimport type { ComposerBodyCustomLink } from \"../../types\";\nimport { isPlainText, isText } from \"../utils/is-text\";\nimport { selectionContainsInlines } from \"../utils/selection-contains-inlines\";\n\nfunction isUrl(string: string) {\n try {\n new URL(string);\n return true;\n } catch (_) {\n return false;\n }\n}\n\nexport function withCustomLinks(editor: Editor): Editor {\n const { isInline, normalizeNode, insertData } = editor;\n\n editor.isInline = (element) => {\n return element.type === \"custom-link\" ? true : isInline(element);\n };\n\n editor.normalizeNode = (entry) => {\n const [node, path] = entry;\n\n if (isText(node)) {\n const parentNode = Node.parent(editor, path);\n\n // Prevent rich text within custom links by removing all marks of inner text nodes\n if (isComposerBodyCustomLink(parentNode)) {\n if (!isPlainText(node)) {\n const marks = Object.keys(node).filter((key) => key !== \"text\");\n\n Transforms.unsetNodes(editor, marks, { at: path });\n }\n }\n }\n\n normalizeNode(entry);\n };\n\n // Create custom links when pasting URLs while some text is selected\n editor.insertData = (data) => {\n const { selection } = editor;\n const pastedText = data.getData(\"text/plain\");\n\n // Keep track of whether the default behavior should be invoked\n // This allows us to sequence multiple conditions in a performant way,\n // ordering them by likelihood/cost and stopping early whenever possible\n let shouldInvokeDefaultBehavior = true;\n\n // Check if the selection is a range\n if (selection && !Range.isCollapsed(selection)) {\n // Check if the selection is contained in a single block\n if (selection.anchor.path[0] === selection.focus.path[0]) {\n // Check if the pasted text is a valid URL\n if (isUrl(pastedText)) {\n // Check if the selection only contains (rich and/or plain) text nodes\n if (!selectionContainsInlines(editor, (node) => !isText(node))) {\n // If all conditions are met, wrap the selected nodes in a custom link\n Transforms.wrapNodes<ComposerBodyCustomLink>(\n editor,\n {\n type: \"custom-link\",\n url: pastedText,\n children: [],\n },\n {\n at: selection,\n split: true,\n match: isPlainText,\n }\n );\n shouldInvokeDefaultBehavior = false;\n }\n }\n }\n }\n\n if (shouldInvokeDefaultBehavior) {\n insertData(data);\n }\n };\n\n return editor;\n}\n\nexport function isComposerBodyCustomLink(\n node: Node\n): node is ComposerBodyCustomLink {\n return Element.isElement(node) && node.type === \"custom-link\";\n}\n"],"names":[],"mappings":";;;;AAOA,SAAS,MAAM,MAAgB,EAAA;AAC7B,EAAI,IAAA;AACF,IAAA,IAAI,IAAI,MAAM,CAAA,CAAA;AACd,IAAO,OAAA,IAAA,CAAA;AAAA,WACA,CAAP,EAAA;AACA,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF,CAAA;AAEO,SAAS,gBAAgB,MAAwB,EAAA;AACtD,EAAA,MAAM,EAAE,QAAA,EAAU,aAAe,EAAA,UAAA,EAAe,GAAA,MAAA,CAAA;AAEhD,EAAO,MAAA,CAAA,QAAA,GAAW,CAAC,OAAY,KAAA;AAC7B,IAAA,OAAO,OAAQ,CAAA,IAAA,KAAS,aAAgB,GAAA,IAAA,GAAO,SAAS,OAAO,CAAA,CAAA;AAAA,GACjE,CAAA;AAEA,EAAO,MAAA,CAAA,aAAA,GAAgB,CAAC,KAAU,KAAA;AAChC,IAAM,MAAA,CAAC,IAAM,EAAA,IAAI,CAAI,GAAA,KAAA,CAAA;AAErB,IAAI,IAAA,MAAA,CAAO,IAAI,CAAG,EAAA;AAChB,MAAA,MAAM,UAAa,GAAA,IAAA,CAAK,MAAO,CAAA,MAAA,EAAQ,IAAI,CAAA,CAAA;AAG3C,MAAI,IAAA,wBAAA,CAAyB,UAAU,CAAG,EAAA;AACxC,QAAI,IAAA,CAAC,WAAY,CAAA,IAAI,CAAG,EAAA;AACtB,UAAM,MAAA,KAAA,GAAQ,OAAO,IAAK,CAAA,IAAI,EAAE,MAAO,CAAA,CAAC,GAAQ,KAAA,GAAA,KAAQ,MAAM,CAAA,CAAA;AAE9D,UAAA,UAAA,CAAW,WAAW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,yBACd,IACgC,EAAA;AAChC,EAAA,OAAO,OAAQ,CAAA,SAAA,CAAU,IAAI,CAAA,IAAK,KAAK,IAAS,KAAA,aAAA,CAAA;AAClD;;;;"}
@@ -0,0 +1,105 @@
1
+ 'use strict';
2
+
3
+ var slate = require('slate');
4
+ var slateHyperscript = require('slate-hyperscript');
5
+
6
+ function areUrlsEqual(a, b) {
7
+ try {
8
+ const urlA = new URL(a);
9
+ const urlB = new URL(b);
10
+ return urlA.origin === urlB.origin && urlA.pathname === urlB.pathname;
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
15
+ const createParagraphElement = () => ({
16
+ type: "paragraph"
17
+ });
18
+ const ELEMENT_TAGS = {
19
+ A: (element) => {
20
+ const href = element.getAttribute("href");
21
+ const innerText = element.innerText;
22
+ return {
23
+ type: href && areUrlsEqual(href, innerText) ? "auto-link" : "custom-link",
24
+ url: href ?? ""
25
+ };
26
+ },
27
+ P: createParagraphElement,
28
+ BLOCKQUOTE: createParagraphElement,
29
+ H1: createParagraphElement,
30
+ H2: createParagraphElement,
31
+ H3: createParagraphElement,
32
+ H4: createParagraphElement,
33
+ H5: createParagraphElement,
34
+ H6: createParagraphElement,
35
+ LI: createParagraphElement
36
+ };
37
+ const TEXT_TAGS = {
38
+ CODE: () => ({ code: true }),
39
+ DEL: () => ({ strikethrough: true }),
40
+ EM: () => ({ italic: true }),
41
+ I: () => ({ italic: true }),
42
+ S: () => ({ strikethrough: true }),
43
+ STRONG: () => ({ bold: true })
44
+ };
45
+ function flattenListItems(node) {
46
+ const listItems = [];
47
+ if (node.nodeName === "LI") {
48
+ listItems.push(node);
49
+ }
50
+ node.childNodes.forEach((child) => {
51
+ if (child.nodeType === 1) {
52
+ listItems.push(...flattenListItems(child));
53
+ }
54
+ });
55
+ return listItems;
56
+ }
57
+ function deserialize(node) {
58
+ if (node.nodeType === 3) {
59
+ return node.textContent;
60
+ } else if (node.nodeType !== 1) {
61
+ return null;
62
+ } else if (node.nodeName === "BR") {
63
+ return "\n";
64
+ }
65
+ const childNodes = Array.from(node.childNodes);
66
+ let children = childNodes.map(deserialize).flat();
67
+ if (node.nodeName === "UL" || node.nodeName === "OL") {
68
+ const listItems = flattenListItems(node);
69
+ children = listItems.map((li) => deserialize(li)).flat();
70
+ }
71
+ if (children.length === 0) {
72
+ children = [{ text: "" }];
73
+ }
74
+ if (node.nodeName === "BODY") {
75
+ return slateHyperscript.jsx("fragment", {}, children);
76
+ }
77
+ if (ELEMENT_TAGS[node.nodeName]) {
78
+ const attrs = ELEMENT_TAGS[node.nodeName](node);
79
+ return slateHyperscript.jsx("element", attrs, children);
80
+ }
81
+ if (TEXT_TAGS[node.nodeName]) {
82
+ const attrs = TEXT_TAGS[node.nodeName](node);
83
+ return children.map((child) => slateHyperscript.jsx("text", attrs, child));
84
+ }
85
+ return children;
86
+ }
87
+ function withPasteHtml(editor) {
88
+ const { insertData } = editor;
89
+ editor.insertData = (data) => {
90
+ const html = data.getData("text/html");
91
+ if (html) {
92
+ const parsed = new DOMParser().parseFromString(html, "text/html");
93
+ const fragment = deserialize(parsed.body);
94
+ if (fragment !== null && Array.isArray(fragment)) {
95
+ slate.Transforms.insertFragment(editor, fragment);
96
+ return;
97
+ }
98
+ }
99
+ insertData(data);
100
+ };
101
+ return editor;
102
+ }
103
+
104
+ exports.withPasteHtml = withPasteHtml;
105
+ //# sourceMappingURL=paste-html.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paste-html.js","sources":["../../../src/slate/plugins/paste-html.ts"],"sourcesContent":["import type { Descendant, Editor, Element, Node as SlateNode } from \"slate\";\nimport { Transforms } from \"slate\";\nimport { jsx } from \"slate-hyperscript\";\n\nimport type {\n ComposerBodyAutoLink,\n ComposerBodyBlockElement,\n ComposerBodyCustomLink,\n ComposerBodyInlineElement,\n ComposerBodyParagraph,\n ComposerBodyText,\n} from \"../../types\";\n\n// Based on: https://github.com/ianstormtaylor/slate/blob/main/site/examples/paste-html.tsx\n\ntype OmitTextChildren<T> = Omit<T, \"text\" | \"children\">;\n\ntype ComposerBodyElementTag = OmitTextChildren<\n ComposerBodyBlockElement | ComposerBodyInlineElement\n>;\ntype ComposerBodyTextTag = OmitTextChildren<ComposerBodyText>;\n\ntype DeserializedNode =\n | string\n | null\n | Element\n | Descendant[]\n | ComposerBodyText[]\n | DeserializedNode[];\n\nfunction areUrlsEqual(a: string, b: string) {\n try {\n const urlA = new URL(a);\n const urlB = new URL(b);\n\n return urlA.origin === urlB.origin && urlA.pathname === urlB.pathname;\n } catch {\n return false;\n }\n}\n\nconst createParagraphElement = (): OmitTextChildren<ComposerBodyParagraph> => ({\n type: \"paragraph\",\n});\n\nconst ELEMENT_TAGS = {\n A: (\n element\n ): OmitTextChildren<ComposerBodyCustomLink | ComposerBodyAutoLink> => {\n const href = element.getAttribute(\"href\");\n const innerText = element.innerText;\n\n return {\n type: href && areUrlsEqual(href, innerText) ? \"auto-link\" : \"custom-link\",\n url: href ?? \"\",\n };\n },\n P: createParagraphElement,\n // Falling back to paragraphs for unsupported elements\n BLOCKQUOTE: createParagraphElement,\n H1: createParagraphElement,\n H2: createParagraphElement,\n H3: createParagraphElement,\n H4: createParagraphElement,\n H5: createParagraphElement,\n H6: createParagraphElement,\n LI: createParagraphElement,\n} as Record<string, (node: HTMLElement) => ComposerBodyElementTag>;\n\nconst TEXT_TAGS = {\n CODE: (): ComposerBodyTextTag => ({ code: true }),\n DEL: (): ComposerBodyTextTag => ({ strikethrough: true }),\n EM: (): ComposerBodyTextTag => ({ italic: true }),\n I: (): ComposerBodyTextTag => ({ italic: true }),\n S: (): ComposerBodyTextTag => ({ strikethrough: true }),\n STRONG: (): ComposerBodyTextTag => ({ bold: true }),\n // `B` is omitted because Google Docs uses `<b>` in weird ways\n // B: (): ComposerBodyTextTag => ({ bold: true }),\n} as Record<string, (node: HTMLElement) => ComposerBodyTextTag>;\n\nfunction flattenListItems(node: HTMLElement): HTMLElement[] {\n const listItems: HTMLElement[] = [];\n\n if (node.nodeName === \"LI\") {\n listItems.push(node);\n }\n\n node.childNodes.forEach((child) => {\n if (child.nodeType === 1) {\n listItems.push(...flattenListItems(child as HTMLElement));\n }\n });\n\n return listItems;\n}\n\nfunction deserialize(node: Node): DeserializedNode {\n if (node.nodeType === 3) {\n return node.textContent;\n } else if (node.nodeType !== 1) {\n return null;\n } else if (node.nodeName === \"BR\") {\n return \"\\n\";\n }\n\n const childNodes = Array.from(node.childNodes);\n let children = childNodes.map(deserialize).flat();\n\n // Lists aren't supported (yet), so we flatten them into paragraphs\n if (node.nodeName === \"UL\" || node.nodeName === \"OL\") {\n const listItems = flattenListItems(node as HTMLElement);\n\n children = listItems.map((li) => deserialize(li)).flat();\n }\n\n if (children.length === 0) {\n children = [{ text: \"\" }];\n }\n\n if (node.nodeName === \"BODY\") {\n return jsx(\"fragment\", {}, children);\n }\n\n if (ELEMENT_TAGS[node.nodeName]) {\n const attrs = ELEMENT_TAGS[node.nodeName](node as HTMLElement);\n\n return jsx(\"element\", attrs, children);\n }\n\n if (TEXT_TAGS[node.nodeName]) {\n const attrs = TEXT_TAGS[node.nodeName](node as HTMLElement);\n\n return children.map((child) => jsx(\"text\", attrs, child));\n }\n\n return children as DeserializedNode;\n}\n\nexport function withPasteHtml(editor: Editor) {\n const { insertData } = editor;\n\n // Deserialize rich text from HTML when pasting\n editor.insertData = (data) => {\n const html = data.getData(\"text/html\");\n\n if (html) {\n const parsed = new DOMParser().parseFromString(html, \"text/html\");\n const fragment = deserialize(parsed.body);\n\n if (fragment !== null && Array.isArray(fragment)) {\n Transforms.insertFragment(editor, fragment as SlateNode[]);\n\n return;\n }\n }\n\n insertData(data);\n };\n\n return editor;\n}\n"],"names":["jsx","Transforms"],"mappings":";;;;;AA8BA,SAAS,YAAA,CAAa,GAAW,CAAW,EAAA;AAC1C,EAAI,IAAA;AACF,IAAM,MAAA,IAAA,GAAO,IAAI,GAAA,CAAI,CAAC,CAAA,CAAA;AACtB,IAAM,MAAA,IAAA,GAAO,IAAI,GAAA,CAAI,CAAC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,CAAgB,MAAM,WAAW,CAAA,CAAA;AAChE,MAAM,MAAA,QAAA,GAAW,WAAY,CAAA,MAAA,CAAO,IAAI,CAAA,CAAA;AAExC,MAAA,IAAI,QAAa,KAAA,IAAA,IAAQ,KAAM,CAAA,OAAA,CAAQ,QAAQ,CAAG,EAAA;AAChD,QAAWC,gBAAA,CAAA,cAAA,CAAe,QAAQ,QAAuB,CAAA,CAAA;AAEzD,QAAA,OAAA;AAAA,OACF;AAAA,KACF;AAEA,IAAA,UAAA,CAAW,IAAI,CAAA,CAAA;AAAA,GACjB,CAAA;AAEA,EAAO,OAAA,MAAA,CAAA;AACT;;;;"}
@@ -0,0 +1,103 @@
1
+ import { Transforms } from 'slate';
2
+ import { jsx } from 'slate-hyperscript';
3
+
4
+ function areUrlsEqual(a, b) {
5
+ try {
6
+ const urlA = new URL(a);
7
+ const urlB = new URL(b);
8
+ return urlA.origin === urlB.origin && urlA.pathname === urlB.pathname;
9
+ } catch {
10
+ return false;
11
+ }
12
+ }
13
+ const createParagraphElement = () => ({
14
+ type: "paragraph"
15
+ });
16
+ const ELEMENT_TAGS = {
17
+ A: (element) => {
18
+ const href = element.getAttribute("href");
19
+ const innerText = element.innerText;
20
+ return {
21
+ type: href && areUrlsEqual(href, innerText) ? "auto-link" : "custom-link",
22
+ url: href ?? ""
23
+ };
24
+ },
25
+ P: createParagraphElement,
26
+ BLOCKQUOTE: createParagraphElement,
27
+ H1: createParagraphElement,
28
+ H2: createParagraphElement,
29
+ H3: createParagraphElement,
30
+ H4: createParagraphElement,
31
+ H5: createParagraphElement,
32
+ H6: createParagraphElement,
33
+ LI: createParagraphElement
34
+ };
35
+ const TEXT_TAGS = {
36
+ CODE: () => ({ code: true }),
37
+ DEL: () => ({ strikethrough: true }),
38
+ EM: () => ({ italic: true }),
39
+ I: () => ({ italic: true }),
40
+ S: () => ({ strikethrough: true }),
41
+ STRONG: () => ({ bold: true })
42
+ };
43
+ function flattenListItems(node) {
44
+ const listItems = [];
45
+ if (node.nodeName === "LI") {
46
+ listItems.push(node);
47
+ }
48
+ node.childNodes.forEach((child) => {
49
+ if (child.nodeType === 1) {
50
+ listItems.push(...flattenListItems(child));
51
+ }
52
+ });
53
+ return listItems;
54
+ }
55
+ function deserialize(node) {
56
+ if (node.nodeType === 3) {
57
+ return node.textContent;
58
+ } else if (node.nodeType !== 1) {
59
+ return null;
60
+ } else if (node.nodeName === "BR") {
61
+ return "\n";
62
+ }
63
+ const childNodes = Array.from(node.childNodes);
64
+ let children = childNodes.map(deserialize).flat();
65
+ if (node.nodeName === "UL" || node.nodeName === "OL") {
66
+ const listItems = flattenListItems(node);
67
+ children = listItems.map((li) => deserialize(li)).flat();
68
+ }
69
+ if (children.length === 0) {
70
+ children = [{ text: "" }];
71
+ }
72
+ if (node.nodeName === "BODY") {
73
+ return jsx("fragment", {}, children);
74
+ }
75
+ if (ELEMENT_TAGS[node.nodeName]) {
76
+ const attrs = ELEMENT_TAGS[node.nodeName](node);
77
+ return jsx("element", attrs, children);
78
+ }
79
+ if (TEXT_TAGS[node.nodeName]) {
80
+ const attrs = TEXT_TAGS[node.nodeName](node);
81
+ return children.map((child) => jsx("text", attrs, child));
82
+ }
83
+ return children;
84
+ }
85
+ function withPasteHtml(editor) {
86
+ const { insertData } = editor;
87
+ editor.insertData = (data) => {
88
+ const html = data.getData("text/html");
89
+ if (html) {
90
+ const parsed = new DOMParser().parseFromString(html, "text/html");
91
+ const fragment = deserialize(parsed.body);
92
+ if (fragment !== null && Array.isArray(fragment)) {
93
+ Transforms.insertFragment(editor, fragment);
94
+ return;
95
+ }
96
+ }
97
+ insertData(data);
98
+ };
99
+ return editor;
100
+ }
101
+
102
+ export { withPasteHtml };
103
+ //# sourceMappingURL=paste-html.mjs.map