index.ts raw
1 import { Pubkey } from '@/domain'
2 import TTMention from '@tiptap/extension-mention'
3 import { ReactNodeViewRenderer } from '@tiptap/react'
4 import MentionNode from './MentionNode'
5
6 declare module '@tiptap/core' {
7 interface Commands<ReturnType> {
8 mention: {
9 createMention: (id: string) => ReturnType
10 }
11 }
12 }
13
14 // const MENTION_REGEX = /(nostr:)?(npub1[a-z0-9]{58}|nprofile1[a-z0-9]+)/g
15
16 const Mention = TTMention.extend({
17 selectable: true,
18
19 addNodeView() {
20 return ReactNodeViewRenderer(MentionNode)
21 },
22
23 addCommands() {
24 return {
25 ...this.parent?.(),
26
27 createMention:
28 (npub: string) =>
29 ({ chain }) => {
30 chain()
31 .focus()
32 .insertContent([
33 {
34 type: 'mention',
35 attrs: {
36 id: npub,
37 label: Pubkey.tryFromString(npub)?.formatNpub(12) ?? npub.slice(0, 12)
38 }
39 },
40 {
41 type: 'text',
42 text: ' '
43 }
44 ])
45 .run()
46
47 return true
48 }
49 }
50 }
51
52 // addInputRules() {
53 // return [
54 // new InputRule({
55 // find: MENTION_REGEX,
56 // handler: (props) => handler(props)
57 // })
58 // ]
59 // },
60
61 // addPasteRules() {
62 // return [
63 // new PasteRule({
64 // find: MENTION_REGEX,
65 // handler: (props) => handler(props)
66 // })
67 // ]
68 // }
69 })
70 export default Mention
71
72 // function handler({
73 // range,
74 // match,
75 // commands
76 // }: {
77 // commands: SingleCommands
78 // match: ExtendedRegExpMatchArray
79 // range: Range
80 // }) {
81 // const mention = match[0]
82 // if (!mention) return
83 // const npub = mention.replace('nostr:', '')
84
85 // const matchLength = mention.length
86 // const end = range.to
87 // const start = Math.max(0, end - matchLength)
88
89 // commands.deleteRange({ from: start, to: end })
90 // commands.createMention(npub)
91 // }
92