{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "emoji-prosemirror",
  "title": "Emoji ProseMirror",
  "description": "Shortcode suggestions as a ProseMirror plugin, without React. It finds the :query, handles the keys, and replaces the query with the chosen emoji.",
  "dependencies": [
    "@emoji-components/core@1.0.0-alpha.4",
    "prosemirror-state",
    "prosemirror-view"
  ],
  "files": [
    {
      "path": "registry/emoji-prosemirror.ts",
      "content": "import { Plugin, PluginKey, type EditorState } from \"prosemirror-state\";\nimport type { EditorView } from \"prosemirror-view\";\nimport { createEmojiSuggestions, findShortcodeTrigger, type EmojiStore, type ShortcodeCompletion, type EmojiSuggestionsOptions } from \"@emoji-components/core\";\n\ntype Node = EditorState[\"doc\"];\n\nexport type ProseMirrorEmojiSuggestionsOptions = Pick<EmojiSuggestionsOptions, \"id\" | \"limit\" | \"matchBy\" | \"order\" | \"columns\" | \"onSelect\"> & {\n  insertAs?: \"emoji\" | \"shortcode\";\n  content?: (text: string, suggestion: ShortcodeCompletion, schema: EditorState[\"schema\"]) => string | Node | Node[\"content\"] | readonly Node[];\n};\n\ntype Match = { query: string; from: number; to: number };\n\nfunction matchIn({ selection: { $from, empty } }: EditorState): Match | null {\n  const found = empty ? findShortcodeTrigger($from.parent.textBetween(0, $from.parentOffset, undefined, \"￼\")) : null;\n  return found && { query: found.query, from: $from.start() + found.offset, to: $from.pos };\n}\n\nconst keyOf = (match: Match | null) => (match ? `${match.from}:${match.query}` : \"\");\n\nexport function emojiSuggestions(store: EmojiStore, { insertAs = \"emoji\", content = (text) => `${text} `, ...options }: ProseMirrorEmojiSuggestionsOptions = {}) {\n  let mounted: EditorView | null = null;\n  const current = () => {\n    const view = mounted;\n    const match = view && matchIn(view.state);\n    return view && match ? { view, match } : null;\n  };\n  const suggestions = createEmojiSuggestions(store, {\n    ...options,\n    onChoose(suggestion) {\n      const at = current();\n      if (!at) return;\n      const { view, match } = at;\n      const { tr, schema, selection } = view.state;\n      const { parent, parentOffset } = selection.$from;\n      const replacement = content(suggestions.textFor(suggestion, insertAs), suggestion, schema);\n      const text = typeof replacement === \"string\" && /\\s/.test(parent.textBetween(parentOffset, Math.min(parentOffset + 1, parent.content.size))) ? replacement.trimEnd() : replacement;\n      view.dispatch((typeof text === \"string\" ? tr.insertText(text, match.from, match.to) : tr.replaceWith(match.from, match.to, text)).scrollIntoView());\n      view.focus();\n    },\n  });\n  const key = new PluginKey(\"emojiSuggestions\");\n  const plugin = new Plugin({\n    key,\n    view(editor) {\n      mounted = editor;\n      store.load().catch(() => {});\n      const unsync = suggestions.syncCombobox(editor.dom);\n      const sync = (state: EditorState, prev: EditorState | null) => {\n        const match = matchIn(state);\n        if (keyOf(match) !== (prev ? keyOf(matchIn(prev)) : \"\")) suggestions.setQuery(match?.query ?? \"\", keyOf(match));\n      };\n      sync(editor.state, null);\n      return {\n        update: (view, prev) => sync(view.state, prev),\n        destroy() {\n          mounted = null;\n          suggestions.setQuery(\"\");\n          unsync();\n        },\n      };\n    },\n    props: {\n      handleKeyDown: (_, event) => suggestions.keydown(event),\n      handleDOMEvents: { blur: () => void (suggestions.getSnapshot().open && suggestions.dismiss()) },\n    },\n  });\n\n  return {\n    plugin,\n    key,\n    suggestions,\n    anchor(): DOMRect | null {\n      const at = current();\n      if (!at) return null;\n      const { view, match } = at;\n      const start = view.coordsAtPos(match.from);\n      const end = view.coordsAtPos(match.to);\n      return new DOMRect(start.left, start.top, end.right - start.left, start.bottom - start.top);\n    },\n  };\n}\n\nexport type ProseMirrorEmojiSuggestions = ReturnType<typeof emojiSuggestions>;\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:lib"
}