|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { usePreventScroll } from "@react-aria/overlays"; |
| 4 | +import { Bold } from "@tiptap/extension-bold"; |
| 5 | +import { Document } from "@tiptap/extension-document"; |
| 6 | +import { Link } from "@tiptap/extension-link"; |
| 7 | +import { Paragraph } from "@tiptap/extension-paragraph"; |
| 8 | +import { Text } from "@tiptap/extension-text"; |
| 9 | +import { Placeholder } from "@tiptap/extensions"; |
| 10 | +import { type Editor, EditorContent, useEditor, useEditorState } from "@tiptap/react"; |
| 11 | +import DOMPurify from "dompurify"; |
| 12 | +import { BoldIcon, LinkIcon, UnlinkIcon } from "lucide-react"; |
| 13 | +import { Fragment, type ReactNode, useState } from "react"; |
| 14 | +import { DialogTrigger, Group } from "react-aria-components"; |
| 15 | +import { z } from "zod"; |
| 16 | + |
| 17 | +import { TextInputField } from "@/components/ui/blocks/text-input-field"; |
| 18 | +import { Button } from "@/components/ui/button"; |
| 19 | +import { |
| 20 | + Dialog, |
| 21 | + DialogCancelButton, |
| 22 | + DialogFooter, |
| 23 | + DialogHeader, |
| 24 | + DialogTitle, |
| 25 | +} from "@/components/ui/dialog"; |
| 26 | +import { IconButton } from "@/components/ui/icon-button"; |
| 27 | +import { IconToggleButton } from "@/components/ui/icon-toggle-button"; |
| 28 | +import { Label } from "@/components/ui/label"; |
| 29 | +import { Modal, ModalOverlay } from "@/components/ui/modal"; |
| 30 | +import { variants } from "@/lib/styles"; |
| 31 | + |
| 32 | +const editorStyles = variants({ |
| 33 | + base: [ |
| 34 | + "block prose w-full min-w-0 min-h-20 appearance-none transition", |
| 35 | + "resize-none data-resizable:resize-y", |
| 36 | + "rounded-md px-3 py-1.5", |
| 37 | + "text-sm leading-normal text-neutral-950 placeholder:text-neutral-500 dark:text-neutral-0", |
| 38 | + "border border-neutral-950/10 hover:border-neutral-950/20 dark:border-neutral-0/10 dark:hover:border-neutral-0/20", |
| 39 | + "bg-neutral-0 dark:bg-neutral-0/5", |
| 40 | + "shadow-xs dark:shadow-none", |
| 41 | + "invalid:border-negative-500 invalid:shadow-negative-500/10 invalid:hover:border-negative-500 dark:invalid:border-negative-500 dark:invalid:hover:border-negative-500", |
| 42 | + "disabled:border-neutral-950/20 disabled:bg-neutral-950/5 disabled:opacity-50 disabled:shadow-none dark:disabled:border-neutral-0/15 dark:disabled:hover:border-neutral-0/15", |
| 43 | + "outline-solid outline-0 outline-neutral-950 invalid:outline-negative-500 focus:outline-1 focus-visible:outline-2 dark:outline-neutral-0 forced-colors:outline-[Highlight]", |
| 44 | + ], |
| 45 | +}); |
| 46 | + |
| 47 | +interface TipTapLink { |
| 48 | + href?: string; |
| 49 | + target?: string; |
| 50 | + rel?: string; |
| 51 | + class?: string; |
| 52 | + [key: string]: unknown; |
| 53 | +} |
| 54 | + |
| 55 | +interface TipTapEditorProps { |
| 56 | + defaultContent: string | null | undefined; |
| 57 | + label: string; |
| 58 | + name: string; |
| 59 | +} |
| 60 | + |
| 61 | +const allowedProtocols = ["http:", "https:", "mailto:"]; |
| 62 | + |
| 63 | +export function TiptapEditor(props: TipTapEditorProps): ReactNode { |
| 64 | + const { defaultContent, label, name } = props; |
| 65 | + |
| 66 | + const [isOpen, setIsOpen] = useState(false); |
| 67 | + |
| 68 | + const [link, setLink] = useState<TipTapLink>({}); |
| 69 | + |
| 70 | + const handleChange = (isOpen: boolean) => { |
| 71 | + setIsOpen(isOpen); |
| 72 | + if (isOpen) { |
| 73 | + const url = editorState?.link as TipTapLink; |
| 74 | + setLink(url); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + usePreventScroll({ isDisabled: isOpen }); |
| 79 | + |
| 80 | + const [content, setContent] = useState(defaultContent); |
| 81 | + const editor = useEditor({ |
| 82 | + editorProps: { |
| 83 | + attributes: { |
| 84 | + "aria-label": "Rich Text Editor", |
| 85 | + class: editorStyles(), |
| 86 | + }, |
| 87 | + }, |
| 88 | + extensions: [ |
| 89 | + Document, |
| 90 | + Paragraph, |
| 91 | + Text, |
| 92 | + Link.configure({ |
| 93 | + openOnClick: false, |
| 94 | + autolink: true, |
| 95 | + defaultProtocol: "https", |
| 96 | + protocols: allowedProtocols.map((protocol) => { |
| 97 | + return protocol.replace(":", ""); |
| 98 | + }), |
| 99 | + }), |
| 100 | + Bold, |
| 101 | + Placeholder.configure({ |
| 102 | + emptyEditorClass: "before:content-[attr(data-placeholder)]", |
| 103 | + placeholder: `Enter ${label}...`, |
| 104 | + }), |
| 105 | + ], |
| 106 | + content: defaultContent, |
| 107 | + onUpdate({ editor }) { |
| 108 | + setContent(DOMPurify.sanitize(editor.getHTML())); |
| 109 | + }, |
| 110 | + |
| 111 | + immediatelyRender: false, |
| 112 | + }); |
| 113 | + |
| 114 | + const editorState = useEditorState({ |
| 115 | + editor, |
| 116 | + selector: (ctx) => { |
| 117 | + return { |
| 118 | + isBold: ctx.editor?.isActive("bold"), |
| 119 | + isLink: ctx.editor?.isActive("link"), |
| 120 | + link: ctx.editor?.getAttributes("link"), |
| 121 | + }; |
| 122 | + }, |
| 123 | + }); |
| 124 | + |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <Label>{label}</Label> |
| 128 | + <Group aria-label="rich text editor"> |
| 129 | + <Group className="flex gap-x-1.5"> |
| 130 | + <IconToggleButton |
| 131 | + aria-label={"toggle-bold"} |
| 132 | + isSelected={editorState?.isBold} |
| 133 | + onClick={() => { |
| 134 | + return editor?.chain().focus().toggleBold().run(); |
| 135 | + }} |
| 136 | + variant="outline" |
| 137 | + > |
| 138 | + <BoldIcon aria-hidden={true} className="size-5 shrink-0" /> |
| 139 | + </IconToggleButton> |
| 140 | + <DialogTrigger isOpen={isOpen} onOpenChange={handleChange}> |
| 141 | + <IconButton variant="outline"> |
| 142 | + <LinkIcon aria-hidden={true} className="size-5 shrink-0" /> |
| 143 | + <span className="sr-only">edit link</span> |
| 144 | + </IconButton> |
| 145 | + <EditLinkDialog editor={editor} link={link} /> |
| 146 | + </DialogTrigger> |
| 147 | + <IconButton |
| 148 | + isDisabled={!editorState?.isLink} |
| 149 | + onClick={() => { |
| 150 | + return editor?.chain().focus().extendMarkRange("link").unsetLink().run(); |
| 151 | + }} |
| 152 | + variant="outline" |
| 153 | + > |
| 154 | + <UnlinkIcon aria-hidden={true} className="size-5 shrink-0" /> |
| 155 | + <span className="sr-only">remove link</span> |
| 156 | + </IconButton> |
| 157 | + </Group> |
| 158 | + <EditorContent className="mt-2" editor={editor} /> |
| 159 | + <input name={name} type="hidden" value={String(content)} /> |
| 160 | + </Group> |
| 161 | + </> |
| 162 | + ); |
| 163 | +} |
| 164 | + |
| 165 | +interface EditLinkDialogProps { |
| 166 | + editor: Editor | null; |
| 167 | + link: TipTapLink; |
| 168 | +} |
| 169 | + |
| 170 | +function EditLinkDialog(props: EditLinkDialogProps): ReactNode { |
| 171 | + const { editor, link } = props; |
| 172 | + |
| 173 | + const urlSchema = z |
| 174 | + .string() |
| 175 | + .url() |
| 176 | + .refine((url) => { |
| 177 | + try { |
| 178 | + const parsedURL = new URL(url); |
| 179 | + return allowedProtocols.includes(parsedURL.protocol); |
| 180 | + } catch { |
| 181 | + return false; |
| 182 | + } |
| 183 | + }); |
| 184 | + |
| 185 | + const isValidUrl = (url: string) => { |
| 186 | + return urlSchema.safeParse(url).success; |
| 187 | + }; |
| 188 | + |
| 189 | + const [url, setUrl] = useState<string | undefined>(link.href); |
| 190 | + |
| 191 | + return ( |
| 192 | + <ModalOverlay> |
| 193 | + <Modal isDismissable={true}> |
| 194 | + <Dialog> |
| 195 | + {({ close }) => { |
| 196 | + const handleUpdate = () => { |
| 197 | + if (url) { |
| 198 | + editor?.chain().focus().extendMarkRange("link").setLink({ href: url }).run(); |
| 199 | + } |
| 200 | + close(); |
| 201 | + }; |
| 202 | + |
| 203 | + return ( |
| 204 | + <Fragment> |
| 205 | + <DialogHeader> |
| 206 | + <DialogTitle>Edit Link</DialogTitle> |
| 207 | + </DialogHeader> |
| 208 | + |
| 209 | + <div> |
| 210 | + <TextInputField |
| 211 | + autoFocus={true} |
| 212 | + defaultValue={link.href} |
| 213 | + description={`allowed protocols: ${allowedProtocols.join(", ")}`} |
| 214 | + isRequired={true} |
| 215 | + label="Link" |
| 216 | + name="link" |
| 217 | + onChange={(v) => { |
| 218 | + setUrl(v); |
| 219 | + }} |
| 220 | + /> |
| 221 | + </div> |
| 222 | + <DialogFooter> |
| 223 | + <DialogCancelButton>Cancel</DialogCancelButton> |
| 224 | + <Button isDisabled={!url || !isValidUrl(url)} onPress={handleUpdate}> |
| 225 | + Update |
| 226 | + </Button> |
| 227 | + </DialogFooter> |
| 228 | + </Fragment> |
| 229 | + ); |
| 230 | + }} |
| 231 | + </Dialog> |
| 232 | + </Modal> |
| 233 | + </ModalOverlay> |
| 234 | + ); |
| 235 | +} |
0 commit comments