{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "emoji-lexical",
  "title": "Emoji Lexical",
  "description": "Shortcode suggestions for a Lexical editor: the useLexicalEmojiSuggestions hook, the EmojiSuggestionsPlugin component, and an EmojiNode for custom emoji.",
  "dependencies": [
    "emoji-components@1.0.0-alpha.4",
    "lexical",
    "@lexical/react"
  ],
  "registryDependencies": [
    "https://emoji.brianhung.me/r/emoji-suggestions.json"
  ],
  "files": [
    {
      "path": "registry/use-emoji-lexical.ts",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { $getNodeByKey, $getSelection, $isRangeSelection, $isTextNode, COMMAND_PRIORITY_HIGH, KEY_DOWN_COMMAND, type LexicalNode, type NodeKey } from \"lexical\";\nimport { useLexicalComposerContext } from \"@lexical/react/LexicalComposerContext\";\nimport { findShortcodeTrigger, useEmojiSuggestions, type UseEmojiSuggestionsReturnValue, type UseEmojiSuggestionsOptions, type ShortcodeCompletion } from \"emoji-components\";\n\nexport type LexicalEmojiSuggestionsOptions = Omit<UseEmojiSuggestionsOptions, \"trigger\" | \"element\"> & {\n  node?: (text: string, suggestion: ShortcodeCompletion) => LexicalNode;\n};\n\ntype Match = { query: string; key: NodeKey; from: number; to: number };\n\nfunction $matchAtSelection(): Match | null {\n  const selection = $getSelection();\n  if (!$isRangeSelection(selection) || !selection.isCollapsed()) return null;\n  const { offset } = selection.anchor;\n  const text = selection.anchor.getNode();\n  if (!$isTextNode(text) || !text.isSimpleText()) return null;\n  const found = findShortcodeTrigger(text.getTextContent().slice(0, offset));\n  return found && { query: found.query, key: text.getKey(), from: found.offset, to: offset };\n}\n\nexport function useLexicalEmojiSuggestions({ node, ...options }: LexicalEmojiSuggestionsOptions = {}): UseEmojiSuggestionsReturnValue {\n  const [editor] = useLexicalComposerContext();\n  const [match, setMatch] = React.useState<Match | null>(null);\n  const suggestions = useEmojiSuggestions({\n    ...options,\n    element: () => editor.getRootElement(),\n    trigger: match && {\n      query: match.query,\n      anchor() {\n        const text = editor.getElementByKey(match.key)?.firstChild;\n        if (!text) return null;\n        const length = text.textContent?.length ?? 0;\n        const range = document.createRange();\n        range.setStart(text, Math.min(match.from, length));\n        range.setEnd(text, Math.min(match.to, length));\n        return range.getBoundingClientRect();\n      },\n      replace: (text, s) =>\n        editor.update(() => {\n          const target = $getNodeByKey(match.key);\n          if (!$isTextNode(target)) return;\n          const selection = target.select(match.from, match.to);\n          if (node) selection.insertNodes([node(text, s)]);\n          else selection.insertText(/\\s/.test(target.getTextContent().charAt(match.to)) ? text : `${text} `);\n        }),\n    },\n  });\n  const { onKeyDown } = suggestions;\n  React.useEffect(\n    () =>\n      editor.registerUpdateListener(({ editorState }) => editorState.read(() => setMatch($matchAtSelection()))),\n    [editor],\n  );\n  React.useEffect(() => editor.registerCommand(KEY_DOWN_COMMAND, onKeyDown, COMMAND_PRIORITY_HIGH), [editor, onKeyDown]);\n  return suggestions;\n}\n",
      "type": "registry:hook"
    },
    {
      "path": "src/components/lexical/emoji-suggestions-plugin.tsx",
      "content": "import { $createTextNode } from \"lexical\";\nimport { useLexicalEmojiSuggestions } from \"@/hooks/use-emoji-lexical\";\nimport { EmojiSuggestions } from \"@/components/ui/emoji-suggestions\";\nimport { $createEmojiNode } from \"./emoji-node\";\n\nexport function EmojiSuggestionsPlugin() {\n  const emoji = useLexicalEmojiSuggestions({\n    node: (text, { record, shortcode }) => (record.imageUrl ? $createEmojiNode({ unified: record.unified, shortcode, text: null }) : $createTextNode(`${text} `)),\n  });\n  return <EmojiSuggestions {...emoji} />;\n}\n",
      "type": "registry:component",
      "target": "components/lexical/emoji-suggestions-plugin.tsx"
    },
    {
      "path": "src/components/lexical/emoji-node.tsx",
      "content": "import * as React from \"react\";\nimport { $applyNodeReplacement, DecoratorNode, TextNode, type EditorConfig, type NodeKey, type SerializedLexicalNode, type Spread } from \"lexical\";\nimport { useLexicalComposerContext } from \"@lexical/react/LexicalComposerContext\";\nimport { Emoji, useEmojiStore, type EmojiStore } from \"emoji-components\";\n\nexport type EmojiAttrs = {\n  unified: string;\n  shortcode: string;\n  text: string | null;\n};\n\ntype SerializedEmojiNode = Spread<EmojiAttrs, SerializedLexicalNode>;\n\nexport function emojiAttrs(store: EmojiStore, idOrShortcode: string): EmojiAttrs | null {\n  const record = store.get(idOrShortcode) ?? store.fromShortcode(idOrShortcode);\n  if (!record) return null;\n  const unified = record.imageUrl ? record.unified : store.withSkinTone(record.unified);\n  const text = record.imageUrl ? null : String.fromCodePoint(...unified.split(\"-\").map((hex) => parseInt(hex, 16)));\n  return { unified, shortcode: record.shortcodes[0] ?? record.unified, text };\n}\n\nexport class EmojiNode extends DecoratorNode<React.JSX.Element> {\n  __attrs: EmojiAttrs;\n\n  static getType() {\n    return \"emoji\";\n  }\n\n  static clone(node: EmojiNode) {\n    return new EmojiNode(node.__attrs, node.__key);\n  }\n\n  static importJSON({ unified, shortcode, text }: SerializedEmojiNode) {\n    return $createEmojiNode({ unified, shortcode, text });\n  }\n\n  constructor(attrs: EmojiAttrs, key?: NodeKey) {\n    super(key);\n    this.__attrs = attrs;\n  }\n\n  exportJSON(): SerializedEmojiNode {\n    return { ...super.exportJSON(), ...this.__attrs, type: \"emoji\", version: 1 };\n  }\n\n  createDOM(_config: EditorConfig) {\n    const span = document.createElement(\"span\");\n    span.className = \"inline-block align-[-0.2em]\";\n    span.dataset.emojiNode = \"\";\n    return span;\n  }\n\n  updateDOM() {\n    return false;\n  }\n\n  isInline() {\n    return true;\n  }\n\n  getTextContent() {\n    return this.__attrs.text ?? `:${this.__attrs.shortcode}:`;\n  }\n\n  decorate() {\n    return <Emoji emoji={this.__attrs.unified} size={20} className=\"inline-block size-[1.25em] leading-none\" />;\n  }\n}\n\nexport function $createEmojiNode(attrs: EmojiAttrs) {\n  return $applyNodeReplacement(new EmojiNode(attrs));\n}\n\nconst SHORTCODE = /:([a-z0-9_+'-]+):/i;\n\nexport function EmojiShortcodePlugin() {\n  const [editor] = useLexicalComposerContext();\n  const store = useEmojiStore();\n  React.useEffect(\n    () =>\n      editor.registerNodeTransform(TextNode, (node) => {\n        if (!node.isSimpleText()) return;\n        const match = SHORTCODE.exec(node.getTextContent());\n        const attrs = match ? emojiAttrs(store, match[1]!) : null;\n        if (!match || !attrs) return;\n        const [, target] = match.index === 0 ? [null, node.splitText(match[0].length)[0]] : node.splitText(match.index, match.index + match[0].length);\n        target?.replace($createEmojiNode(attrs)).selectNext();\n      }),\n    [editor, store],\n  );\n  return null;\n}\n",
      "type": "registry:component",
      "target": "components/lexical/emoji-node.tsx"
    }
  ],
  "type": "registry:block"
}