|
| 1 | +import { autoUpdate, computePosition, flip, limitShift, offset, shift, type VirtualElement, } from "@floating-ui/dom"; |
| 2 | +import Mention from "@tiptap/extension-mention"; |
| 3 | +import { PluginKey, } from "@tiptap/pm/state"; |
| 4 | +import { ReactRenderer, } from "@tiptap/react"; |
| 5 | +import type { SuggestionOptions, } from "@tiptap/suggestion"; |
| 6 | +import { CalendarDays, Repeat2, } from "lucide-react"; |
| 7 | +import { forwardRef, useEffect, useImperativeHandle, useState, } from "react"; |
| 8 | +import { getMentionSuggestions, type MentionSuggestion, } from "../../../../services/mentions"; |
| 9 | + |
| 10 | +const MentionMenu = forwardRef< |
| 11 | + { onKeyDown: (props: { event: KeyboardEvent; },) => boolean; }, |
| 12 | + { items: MentionSuggestion[]; command: (item: MentionSuggestion,) => void; } |
| 13 | +>(function MentionMenu({ items, command, }, ref,) { |
| 14 | + const [selectedIndex, setSelectedIndex,] = useState(0,); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + setSelectedIndex(0,); |
| 18 | + }, [items,],); |
| 19 | + |
| 20 | + useImperativeHandle(ref, () => ({ |
| 21 | + onKeyDown: ({ event, },) => { |
| 22 | + if (items.length === 0) return false; |
| 23 | + |
| 24 | + if (event.key === "ArrowUp" || event.key === "ArrowDown" || event.key === "Enter") { |
| 25 | + event.preventDefault(); |
| 26 | + } |
| 27 | + |
| 28 | + if (event.key === "ArrowUp") { |
| 29 | + setSelectedIndex((current,) => (current + items.length - 1) % items.length); |
| 30 | + return true; |
| 31 | + } |
| 32 | + |
| 33 | + if (event.key === "ArrowDown") { |
| 34 | + setSelectedIndex((current,) => (current + 1) % items.length); |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + if (event.key === "Enter") { |
| 39 | + const item = items[selectedIndex]; |
| 40 | + if (item) command(item,); |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + return false; |
| 45 | + }, |
| 46 | + }), [items, selectedIndex, command,],); |
| 47 | + |
| 48 | + if (items.length === 0) return null; |
| 49 | + |
| 50 | + let sawRecurring = false; |
| 51 | + |
| 52 | + return ( |
| 53 | + <div className="mention-menu"> |
| 54 | + {items.map((item, index,) => { |
| 55 | + const showRecurringDivider = item.group === "recurring" && !sawRecurring; |
| 56 | + if (item.group === "recurring") sawRecurring = true; |
| 57 | + const Icon = item.kind === "recurring" ? Repeat2 : CalendarDays; |
| 58 | + |
| 59 | + return ( |
| 60 | + <div key={item.id}> |
| 61 | + {showRecurringDivider && <div className="mention-menu-divider" />} |
| 62 | + <button |
| 63 | + className={`mention-menu-item ${index === selectedIndex ? "is-selected" : ""}`} |
| 64 | + onClick={() => command(item,)} |
| 65 | + type="button" |
| 66 | + > |
| 67 | + <Icon className="mention-menu-icon" size={14} /> |
| 68 | + <span className="mention-menu-label">{item.label}</span> |
| 69 | + </button> |
| 70 | + </div> |
| 71 | + ); |
| 72 | + },)} |
| 73 | + </div> |
| 74 | + ); |
| 75 | +},); |
| 76 | + |
| 77 | +export function buildMentionChipSuggestion(referenceDate?: string,): Omit<SuggestionOptions, "editor"> { |
| 78 | + return { |
| 79 | + char: "@", |
| 80 | + allowSpaces: true, |
| 81 | + pluginKey: new PluginKey("mention-chip-suggestion",), |
| 82 | + items: ({ query, },) => getMentionSuggestions(query, referenceDate,), |
| 83 | + command: ({ editor, range, props, },) => { |
| 84 | + const item = props as MentionSuggestion; |
| 85 | + editor |
| 86 | + .chain() |
| 87 | + .focus() |
| 88 | + .insertContentAt(range, [ |
| 89 | + { |
| 90 | + type: "mentionChip", |
| 91 | + attrs: { |
| 92 | + id: item.id, |
| 93 | + label: item.label, |
| 94 | + kind: item.kind, |
| 95 | + }, |
| 96 | + }, |
| 97 | + { type: "text", text: " ", }, |
| 98 | + ],) |
| 99 | + .run(); |
| 100 | + }, |
| 101 | + render: () => { |
| 102 | + let renderer: ReactRenderer; |
| 103 | + let cleanup: (() => void) | undefined; |
| 104 | + let floatingEl: HTMLElement; |
| 105 | + let referenceEl: VirtualElement | null = null; |
| 106 | + |
| 107 | + const update = () => { |
| 108 | + if (!referenceEl) return; |
| 109 | + void computePosition(referenceEl, floatingEl, { |
| 110 | + placement: "bottom-start", |
| 111 | + middleware: [offset(6,), flip(), shift({ limiter: limitShift(), },),], |
| 112 | + },).then(({ x, y, },) => { |
| 113 | + Object.assign(floatingEl.style, { |
| 114 | + left: `${x}px`, |
| 115 | + top: `${y}px`, |
| 116 | + },); |
| 117 | + },); |
| 118 | + }; |
| 119 | + |
| 120 | + return { |
| 121 | + onStart: (props,) => { |
| 122 | + renderer = new ReactRenderer(MentionMenu, { |
| 123 | + props: { |
| 124 | + items: props.items as MentionSuggestion[], |
| 125 | + command: (item: MentionSuggestion,) => props.command(item,), |
| 126 | + }, |
| 127 | + editor: props.editor, |
| 128 | + },); |
| 129 | + |
| 130 | + floatingEl = renderer.element as HTMLElement; |
| 131 | + Object.assign(floatingEl.style, { |
| 132 | + position: "absolute", |
| 133 | + top: "0", |
| 134 | + left: "0", |
| 135 | + zIndex: "40", |
| 136 | + },); |
| 137 | + document.body.appendChild(floatingEl,); |
| 138 | + |
| 139 | + if (!props.clientRect) return; |
| 140 | + |
| 141 | + referenceEl = { |
| 142 | + getBoundingClientRect: () => props.clientRect?.() ?? new DOMRect(), |
| 143 | + }; |
| 144 | + |
| 145 | + cleanup = autoUpdate(referenceEl, floatingEl, update,); |
| 146 | + update(); |
| 147 | + }, |
| 148 | + onUpdate: (props,) => { |
| 149 | + renderer.updateProps({ |
| 150 | + items: props.items as MentionSuggestion[], |
| 151 | + command: (item: MentionSuggestion,) => props.command(item,), |
| 152 | + },); |
| 153 | + |
| 154 | + if (props.clientRect && referenceEl) { |
| 155 | + referenceEl.getBoundingClientRect = () => props.clientRect?.() ?? new DOMRect(); |
| 156 | + } |
| 157 | + update(); |
| 158 | + }, |
| 159 | + onKeyDown: (props,) => { |
| 160 | + if (props.event.key === "Escape") { |
| 161 | + cleanup?.(); |
| 162 | + floatingEl.remove(); |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + return ((renderer.ref as { onKeyDown?: (input: { event: KeyboardEvent; },) => boolean; } | null) |
| 167 | + ?.onKeyDown?.({ event: props.event, },) ?? false); |
| 168 | + }, |
| 169 | + onExit: () => { |
| 170 | + cleanup?.(); |
| 171 | + floatingEl.remove(); |
| 172 | + renderer.destroy(); |
| 173 | + }, |
| 174 | + }; |
| 175 | + }, |
| 176 | + }; |
| 177 | +} |
| 178 | + |
| 179 | +export const MentionChipExtension = Mention.extend({ |
| 180 | + name: "mentionChip", |
| 181 | + |
| 182 | + addOptions() { |
| 183 | + return { |
| 184 | + HTMLAttributes: {}, |
| 185 | + renderText: ({ node, },) => String(node.attrs.label ?? "",), |
| 186 | + deleteTriggerWithBackspace: true, |
| 187 | + renderHTML: ({ node, },) => [ |
| 188 | + "span", |
| 189 | + { |
| 190 | + "data-mention-chip": "", |
| 191 | + class: `mention-chip mention-chip-${String(node.attrs.kind ?? "tag",)}`, |
| 192 | + }, |
| 193 | + String(node.attrs.label ?? "",), |
| 194 | + ], |
| 195 | + suggestions: [], |
| 196 | + suggestion: buildMentionChipSuggestion(), |
| 197 | + }; |
| 198 | + }, |
| 199 | + |
| 200 | + addAttributes() { |
| 201 | + return { |
| 202 | + id: { |
| 203 | + default: "", |
| 204 | + parseHTML: (element: Element,) => element.getAttribute("data-id",) ?? "", |
| 205 | + renderHTML: (attributes: { id: string; },) => ({ "data-id": attributes.id, }), |
| 206 | + }, |
| 207 | + label: { |
| 208 | + default: "", |
| 209 | + parseHTML: (element: Element,) => element.getAttribute("data-label",) ?? "", |
| 210 | + renderHTML: (attributes: { label: string; },) => ({ "data-label": attributes.label, }), |
| 211 | + }, |
| 212 | + kind: { |
| 213 | + default: "tag", |
| 214 | + parseHTML: (element: Element,) => element.getAttribute("data-kind",) ?? "tag", |
| 215 | + renderHTML: (attributes: { kind: string; },) => ({ "data-kind": attributes.kind, }), |
| 216 | + }, |
| 217 | + }; |
| 218 | + }, |
| 219 | + |
| 220 | + parseHTML() { |
| 221 | + return [{ tag: "span[data-mention-chip]", },]; |
| 222 | + }, |
| 223 | + |
| 224 | + renderMarkdown(node,) { |
| 225 | + const id = String(node.attrs?.id ?? "",); |
| 226 | + const label = String(node.attrs?.label ?? "",); |
| 227 | + if (!id) return ""; |
| 228 | + if (!label || label === id) return `[[${id}]]`; |
| 229 | + return `[[${id}|${label}]]`; |
| 230 | + }, |
| 231 | +},); |
0 commit comments