|
| 1 | +import { useEffect, useRef, useState } from "react" |
| 2 | +import type { Annotation } from "../annotations" |
| 3 | + |
| 4 | +interface AnnotationEditorProps { |
| 5 | + readonly cols: number |
| 6 | + readonly rows: number |
| 7 | + readonly annotations: ReadonlyArray<Annotation> |
| 8 | + readonly onAdd: (row: number, column: number, note: string) => void |
| 9 | + readonly onChange: (id: string, note: string) => void |
| 10 | + readonly onDelete: (id: string) => void |
| 11 | + readonly issueLink: string |
| 12 | + readonly onDone: () => void |
| 13 | +} |
| 14 | + |
| 15 | +interface Draft { |
| 16 | + readonly id?: string |
| 17 | + readonly row: number |
| 18 | + readonly column: number |
| 19 | + readonly note: string |
| 20 | +} |
| 21 | + |
| 22 | +export function AnnotationEditor(props: AnnotationEditorProps) { |
| 23 | + const [draft, setDraft] = useState<Draft>() |
| 24 | + const textareaRef = useRef<HTMLTextAreaElement>(null) |
| 25 | + const complete = props.annotations.filter((annotation) => annotation.note.trim() !== "") |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + if (!draft) return |
| 29 | + const frame = requestAnimationFrame(() => { |
| 30 | + textareaRef.current?.focus() |
| 31 | + textareaRef.current?.setSelectionRange(draft.note.length, draft.note.length) |
| 32 | + }) |
| 33 | + return () => cancelAnimationFrame(frame) |
| 34 | + }, [draft?.id, draft?.row, draft?.column]) |
| 35 | + |
| 36 | + const save = () => { |
| 37 | + if (!draft?.note.trim()) return |
| 38 | + if (draft.id) props.onChange(draft.id, draft.note.trim()) |
| 39 | + else props.onAdd(draft.row, draft.column, draft.note.trim()) |
| 40 | + setDraft(undefined) |
| 41 | + } |
| 42 | + |
| 43 | + const edit = (annotation: Annotation) => |
| 44 | + setDraft({ id: annotation.id, row: annotation.row, column: annotation.column, note: annotation.note }) |
| 45 | + |
| 46 | + return ( |
| 47 | + <> |
| 48 | + <div |
| 49 | + className="annotation-layer" |
| 50 | + aria-label="Click the terminal to add an annotation" |
| 51 | + onPointerDown={(event) => { |
| 52 | + if (event.target !== event.currentTarget) return |
| 53 | + const bounds = event.currentTarget.getBoundingClientRect() |
| 54 | + const column = Math.min(props.cols - 1, Math.max(0, Math.floor(((event.clientX - bounds.left) / bounds.width) * props.cols))) |
| 55 | + const row = Math.min(props.rows - 1, Math.max(0, Math.floor(((event.clientY - bounds.top) / bounds.height) * props.rows))) |
| 56 | + setDraft({ row, column, note: "" }) |
| 57 | + }} |
| 58 | + > |
| 59 | + {props.annotations.map((annotation, index) => ( |
| 60 | + <button |
| 61 | + key={annotation.id} |
| 62 | + type="button" |
| 63 | + className={`annotation-pin${annotation.id === draft?.id ? " selected" : ""}`} |
| 64 | + style={{ |
| 65 | + left: `${((annotation.column + 0.5) / props.cols) * 100}%`, |
| 66 | + top: `${((annotation.row + 0.5) / props.rows) * 100}%`, |
| 67 | + }} |
| 68 | + aria-label={`Edit annotation ${index + 1}, row ${annotation.row + 1}, column ${annotation.column + 1}`} |
| 69 | + onPointerDown={(event) => event.stopPropagation()} |
| 70 | + onClick={() => edit(annotation)} |
| 71 | + > |
| 72 | + {index + 1} |
| 73 | + </button> |
| 74 | + ))} |
| 75 | + {draft ? ( |
| 76 | + <div |
| 77 | + className={`annotation-composer${draft.row > props.rows / 2 ? " above" : ""}`} |
| 78 | + style={{ |
| 79 | + left: `clamp(9rem, ${((draft.column + 0.5) / props.cols) * 100}%, calc(100% - 9rem))`, |
| 80 | + top: `${((draft.row + 0.5) / props.rows) * 100}%`, |
| 81 | + }} |
| 82 | + onPointerDown={(event) => event.stopPropagation()} |
| 83 | + > |
| 84 | + <header> |
| 85 | + <span>{draft.id ? "Edit annotation" : "New annotation"}</span> |
| 86 | + <small>R{draft.row + 1} · C{draft.column + 1}</small> |
| 87 | + </header> |
| 88 | + <textarea |
| 89 | + ref={textareaRef} |
| 90 | + name="annotation-note" |
| 91 | + aria-label="Annotation note" |
| 92 | + value={draft.note} |
| 93 | + maxLength={2_000} |
| 94 | + rows={2} |
| 95 | + placeholder="What should change?" |
| 96 | + onChange={(event) => setDraft({ ...draft, note: event.target.value })} |
| 97 | + onKeyDown={(event) => { |
| 98 | + if (event.nativeEvent.isComposing) return |
| 99 | + if (event.key === "Enter" && !event.shiftKey) { |
| 100 | + event.preventDefault() |
| 101 | + save() |
| 102 | + } |
| 103 | + if (event.key === "Escape") { |
| 104 | + setDraft(undefined) |
| 105 | + } |
| 106 | + }} |
| 107 | + /> |
| 108 | + <footer> |
| 109 | + {draft.id ? ( |
| 110 | + <button |
| 111 | + type="button" |
| 112 | + className="annotation-composer-delete" |
| 113 | + onClick={() => { |
| 114 | + if (draft.id) props.onDelete(draft.id) |
| 115 | + setDraft(undefined) |
| 116 | + }} |
| 117 | + > |
| 118 | + Delete |
| 119 | + </button> |
| 120 | + ) : <span />} |
| 121 | + <button type="button" onClick={() => setDraft(undefined)}>Cancel</button> |
| 122 | + <button type="button" className="annotation-composer-save" disabled={!draft.note.trim()} onClick={save}> |
| 123 | + {draft.id ? "Save" : "Add"} |
| 124 | + </button> |
| 125 | + </footer> |
| 126 | + </div> |
| 127 | + ) : undefined} |
| 128 | + </div> |
| 129 | + <aside className="annotation-panel" aria-label="Capture annotations"> |
| 130 | + <header> |
| 131 | + <div> |
| 132 | + <strong>Annotations</strong> |
| 133 | + <span>{props.annotations.length === 0 ? "Click anywhere on the terminal" : `${props.annotations.length} placed`}</span> |
| 134 | + </div> |
| 135 | + <button type="button" onClick={props.onDone}>Done</button> |
| 136 | + </header> |
| 137 | + <div className="annotation-list"> |
| 138 | + {props.annotations.map((annotation, index) => ( |
| 139 | + <button key={annotation.id} type="button" className="annotation-list-row" onClick={() => edit(annotation)}> |
| 140 | + <span className="annotation-list-pin">{index + 1}</span> |
| 141 | + <span> |
| 142 | + <small>Row {annotation.row + 1} · Column {annotation.column + 1}</small> |
| 143 | + <strong>{annotation.note}</strong> |
| 144 | + </span> |
| 145 | + </button> |
| 146 | + ))} |
| 147 | + </div> |
| 148 | + <footer> |
| 149 | + <a |
| 150 | + className="annotation-issue" |
| 151 | + href={complete.length === 0 ? undefined : props.issueLink} |
| 152 | + target="_blank" |
| 153 | + rel="noreferrer" |
| 154 | + aria-disabled={complete.length === 0} |
| 155 | + > |
| 156 | + Open GitHub issue |
| 157 | + </a> |
| 158 | + <span>{complete.length === 0 ? "Add a note to continue" : `${complete.length} note${complete.length === 1 ? "" : "s"} will be included`}</span> |
| 159 | + </footer> |
| 160 | + </aside> |
| 161 | + </> |
| 162 | + ) |
| 163 | +} |
0 commit comments