The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | const PDF_PAGE_GAP = 16; |
| 2 | export const PDF_MIN_ZOOM = 0.25; |
| 3 | export const PDF_MAX_ZOOM = 3; |
| 4 | const PDF_ZOOM_STEP = 0.25; |
| 5 | |
| 6 | export function pdfPageWidth(viewportWidth: number, pageWidth: number, zoom: number | null) { |
| 7 | const fitWidth = Math.max(1, viewportWidth - 2 * PDF_PAGE_GAP); |
| 8 | return { |
| 9 | scale: zoom ?? fitWidth / pageWidth, |
| 10 | width: zoom === null ? fitWidth : pageWidth * zoom, |
| 11 | }; |
| 12 | } |
| 13 | |
| 14 | export function stepPdfZoom(scale: number, direction: 1 | -1) { |
| 15 | return direction > 0 |
| 16 | ? Math.min(PDF_MAX_ZOOM, scale + PDF_ZOOM_STEP) |
| 17 | : Math.max(PDF_MIN_ZOOM, scale - PDF_ZOOM_STEP); |
| 18 | } |
| 19 | |
| 20 | /** Top offset of each page plus the end of the last one; ratios are height / width. */ |
| 21 | export function pdfPageOffsets(ratios: readonly number[], width: number) { |
| 22 | const result = [PDF_PAGE_GAP]; |
| 23 | for (const ratio of ratios) |
| 24 | result.push(result[result.length - 1]! + width * ratio + PDF_PAGE_GAP); |
| 25 | return result; |
| 26 | } |
| 27 | |
| 28 | export function currentPdfPage(offsets: readonly number[], pageCount: number, scrollTop: number) { |
| 29 | for (let index = 0; index < pageCount; index++) |
| 30 | if (offsets[index + 1]! > scrollTop + PDF_PAGE_GAP + 1) return index; |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | /** Pages within one viewport above and two below the visible area stay mounted. */ |
| 35 | export function visiblePdfPages( |
| 36 | ratios: readonly number[], |
| 37 | offsets: readonly number[], |
| 38 | scrollTop: number, |
| 39 | viewportHeight: number, |
| 40 | ) { |
| 41 | return ratios.flatMap((ratio, index) => |
| 42 | offsets[index + 1]! >= scrollTop - viewportHeight && |
| 43 | offsets[index]! <= scrollTop + viewportHeight * 3 |
| 44 | ? [{ index, ratio }] |
| 45 | : [], |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | export function pdfPageScrollTop(offsets: readonly number[], index: number) { |
| 50 | return Math.max(0, (offsets[index] ?? PDF_PAGE_GAP) - PDF_PAGE_GAP); |
| 51 | } |
| 52 | |
| 53 | /** A saved offset was taken at `savedWidth`; a reloaded document can have fewer or shorter pages. */ |
| 54 | export function pdfRestoredScrollTop( |
| 55 | ratios: readonly number[], |
| 56 | width: number, |
| 57 | view: { page: number; offset: number }, |
| 58 | savedWidth = width, |
| 59 | ) { |
| 60 | const page = Math.min(view.page, ratios.length - 1); |
| 61 | const offsets = pdfPageOffsets(ratios, width); |
| 62 | return ( |
| 63 | offsets[page]! - |
| 64 | PDF_PAGE_GAP + |
| 65 | Math.min((view.offset * width) / savedWidth, Math.max(0, width * ratios[page]! - 1)) |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | export function pdfPageText( |
| 70 | items: readonly ({ str: string; hasEOL: boolean } | { type: string })[], |
| 71 | ) { |
| 72 | return items.map((item) => ("str" in item ? item.str + (item.hasEOL ? "\n" : "") : "")).join(""); |
| 73 | } |
| 74 | |
| 75 | // A typed straight quote finds a typeset curly one, as pdf.js's own find does. |
| 76 | function foldQuotes(text: string) { |
| 77 | return text.replace(/[\u2018-\u201b]/g, "'").replace(/[\u201c-\u201f]/g, '"'); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Lowercased search text with each whitespace run, line ends included, read as one space, and a |
| 82 | * hyphen that ends a line between letters dropped, as pdf.js's own find does. `owner` maps each |
| 83 | * character back to the run it came from. |
| 84 | */ |
| 85 | function pdfFindText(runs: readonly string[]) { |
| 86 | const chars = runs.flatMap((run, index) => |
| 87 | Array.from(foldQuotes(run.toLowerCase()), (char) => ({ char, run: index })), |
| 88 | ); |
| 89 | let text = ""; |
| 90 | let last = ""; |
| 91 | const owner: number[] = []; |
| 92 | for (let index = 0; index < chars.length; index++) { |
| 93 | const { char, run } = chars[index]!; |
| 94 | if (/\s/.test(char)) { |
| 95 | if (!text.endsWith(" ")) { |
| 96 | text += " "; |
| 97 | last = " "; |
| 98 | owner.push(run); |
| 99 | } |
| 100 | } else if ( |
| 101 | char === "-" && |
| 102 | chars[index + 1]?.char === "\n" && |
| 103 | /\p{L}/u.test(last) && |
| 104 | /\p{L}/u.test(chars[index + 2]?.char ?? "") |
| 105 | ) { |
| 106 | index++; |
| 107 | } else { |
| 108 | text += char; |
| 109 | last = char; |
| 110 | for (let unit = 0; unit < char.length; unit++) owner.push(run); |
| 111 | } |
| 112 | } |
| 113 | return { text, owner }; |
| 114 | } |
| 115 | |
| 116 | /** Whether a page's text holds the query, under the same rules as `pdfFindMatchRuns`, without |
| 117 | * mapping characters back to runs: the page scan checks every page on each query. */ |
| 118 | export function pdfFindIncludes(text: string, query: string) { |
| 119 | const needle = foldQuotes(query.toLowerCase()).replace(/\s+/g, " "); |
| 120 | if (!needle.trim()) return false; |
| 121 | return foldQuotes(text.toLowerCase()) |
| 122 | .replace(/(\p{L})-\n(?=\p{L})/gu, "$1") |
| 123 | .replace(/\s+/g, " ") |
| 124 | .includes(needle); |
| 125 | } |
| 126 | |
| 127 | /** Which text runs hold part of a case-insensitive match, when pdf.js splits words across spans. */ |
| 128 | export function pdfFindMatchRuns(runs: readonly string[], query: string) { |
| 129 | const { text, owner } = pdfFindText(runs); |
| 130 | const needle = foldQuotes(query.toLowerCase()).replace(/\s+/g, " "); |
| 131 | const hits = runs.map(() => false); |
| 132 | if (!needle.trim()) return hits; |
| 133 | for ( |
| 134 | let match = text.indexOf(needle); |
| 135 | match !== -1; |
| 136 | match = text.indexOf(needle, match + needle.length) |
| 137 | ) |
| 138 | for (let index = match; index < match + needle.length; index++) hits[owner[index]!] = true; |
| 139 | return hits; |
| 140 | } |