|
| 1 | +import type { CodeViewHandle } from "@pierre/diffs/react"; |
| 2 | +import { type Ref, useEffect, useState } from "react"; |
| 3 | + |
| 4 | +import { isEditableFocused } from "~/lib/editableFocus"; |
| 5 | +import { buildDiffFileText } from "~/reviewCommentContext"; |
| 6 | + |
| 7 | +function isSelectAll(event: KeyboardEvent): boolean { |
| 8 | + return ( |
| 9 | + event.key.toLowerCase() === "a" && |
| 10 | + (event.metaKey || event.ctrlKey) && |
| 11 | + !event.altKey && |
| 12 | + !event.shiftKey |
| 13 | + ); |
| 14 | +} |
| 15 | + |
| 16 | +function isCopy(event: KeyboardEvent): boolean { |
| 17 | + return event.key.toLowerCase() === "c" && (event.metaKey || event.ctrlKey); |
| 18 | +} |
| 19 | + |
| 20 | +const MODIFIER_KEYS = new Set(["Meta", "Control", "Shift", "Alt"]); |
| 21 | + |
| 22 | +class DiffSelectAll<LAnnotation> { |
| 23 | + handle: CodeViewHandle<LAnnotation, undefined> | null = null; |
| 24 | + // The file the last pointer interaction landed in. |
| 25 | + engagedItemId: string | null = null; |
| 26 | + // The file select all chose. The virtualizer drops its offscreen rows, so |
| 27 | + // copy writes the file's text instead of the drawn selection. |
| 28 | + selectedItemId: string | null = null; |
| 29 | + |
| 30 | + setHandle = (handle: CodeViewHandle<LAnnotation, undefined> | null) => { |
| 31 | + this.handle = handle; |
| 32 | + }; |
| 33 | + |
| 34 | + itemText(itemId: string): string | null { |
| 35 | + const item = this.handle?.getItem(itemId); |
| 36 | + if (!item) return null; |
| 37 | + return item.type === "diff" ? buildDiffFileText(item.fileDiff) : item.file.contents; |
| 38 | + } |
| 39 | + |
| 40 | + drawSelection(itemId: string) { |
| 41 | + const element = this.handle |
| 42 | + ?.getInstance() |
| 43 | + ?.getRenderedItems() |
| 44 | + .find((item) => item.id === itemId)?.element; |
| 45 | + const rows = element?.shadowRoot?.querySelectorAll("[data-line]"); |
| 46 | + const first = rows?.[0]; |
| 47 | + const last = rows?.[rows.length - 1]; |
| 48 | + if (!first || !last) return; |
| 49 | + document.getSelection()?.setBaseAndExtent(first, 0, last, last.childNodes.length); |
| 50 | + } |
| 51 | + |
| 52 | + listen(): () => void { |
| 53 | + const onPointerDown = (event: PointerEvent) => { |
| 54 | + this.selectedItemId = null; |
| 55 | + const path = event.composedPath(); |
| 56 | + this.engagedItemId = |
| 57 | + this.handle |
| 58 | + ?.getInstance() |
| 59 | + ?.getRenderedItems() |
| 60 | + .find((item) => path.includes(item.element))?.id ?? null; |
| 61 | + }; |
| 62 | + const onKeyDown = (event: KeyboardEvent) => { |
| 63 | + if (MODIFIER_KEYS.has(event.key) || isCopy(event)) return; |
| 64 | + this.selectedItemId = null; |
| 65 | + const itemId = this.engagedItemId; |
| 66 | + if (!isSelectAll(event) || itemId === null || isEditableFocused(event.target)) return; |
| 67 | + if (this.itemText(itemId) === null) return; |
| 68 | + event.preventDefault(); |
| 69 | + this.selectedItemId = itemId; |
| 70 | + this.drawSelection(itemId); |
| 71 | + }; |
| 72 | + const onCopy = (event: ClipboardEvent) => { |
| 73 | + const itemId = this.selectedItemId; |
| 74 | + if (itemId === null || !event.clipboardData || isEditableFocused()) return; |
| 75 | + const text = this.itemText(itemId); |
| 76 | + if (text === null) return; |
| 77 | + event.clipboardData.setData("text/plain", text); |
| 78 | + event.preventDefault(); |
| 79 | + }; |
| 80 | + document.addEventListener("pointerdown", onPointerDown, true); |
| 81 | + document.addEventListener("keydown", onKeyDown, true); |
| 82 | + document.addEventListener("copy", onCopy, true); |
| 83 | + return () => { |
| 84 | + document.removeEventListener("pointerdown", onPointerDown, true); |
| 85 | + document.removeEventListener("keydown", onKeyDown, true); |
| 86 | + document.removeEventListener("copy", onCopy, true); |
| 87 | + }; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Scopes select all to the diff file the user is working in and copies all of |
| 93 | + * its lines, since the virtualizer only keeps the rows near the viewport. |
| 94 | + */ |
| 95 | +export function useDiffSelectAllCopy<LAnnotation>() { |
| 96 | + const [selectAll] = useState(() => new DiffSelectAll<LAnnotation>()); |
| 97 | + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- document listeners live while the viewer is mounted |
| 98 | + useEffect(() => selectAll.listen(), [selectAll]); |
| 99 | + return selectAll.setHandle; |
| 100 | +} |
| 101 | + |
| 102 | +export function setRef<T>(ref: Ref<T> | undefined, value: T | null) { |
| 103 | + if (typeof ref === "function") ref(value); |
| 104 | + else if (ref) ref.current = value; |
| 105 | +} |
0 commit comments