code-view.tsx
This file
Path
apps/web/src/features/code/code-view.tsx
136 lines · 4.2 KiB · TypeScript
Last changed by
Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
History of this fileTake it
Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 1–7why, one step back1/**2 * One file, drawn by `@pierre/diffs`: highlighted (Shiki's JavaScript3 * engine, never its WASM one), its lines selectable, the selection kept in4 * the address as `#L10` or `#L10-L20` (`line-hash.ts`) and scrolled to on5 * load. Its lines live in a shadow root, where a native `#L` fragment6 * cannot reach, so the drawing scrolls to the anchored line itself.7 *Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 8–11why, one step back8 * It is Pierre's `File`, not its virtualized `CodeView`: a file is capped9 * at 1 MiB, it flows in the page's own scroll under the file header (so it10 * draws no header of its own), and past the shared limits (`pierre.ts`) it is11 * drawn without highlighting. Highlighting runs in the worker pool.Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 12–22why, one step back12 *13 * The library and Shiki's grammars are megabytes, so only the browser14 * loads this module (`client-code.tsx`); the Worker never bundles it.15 */16import { File } from "@pierre/diffs/react";17import type { FileOptions, SelectedLineRange } from "@pierre/diffs/react";18import { Option } from "effect";19import { useEffect, useMemo, useRef, useState } from "react";2021import { lineHash, parseLineHash } from "./line-hash.ts";22import type { LineRange } from "./line-hash.ts";Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 23why, one step back23import { diffsOptions, registerCodeTheme } from "./pierre.ts";Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 24why, one step back24import { CodePool } from "./pool.tsx";Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
line 25why, one step back25Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
lines 26–27why, one step back26registerCodeTheme();27Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 28–56why, one step back28export interface FileCodeProps {29 readonly path: string;30 /** The blob's id: the drawing's cache key. */31 readonly sha: string;32 readonly text: string;33}3435/** The lines the address names now, if any. */36const fromAddress = (): LineRange | null =>37 Option.getOrNull(parseLineHash(globalThis.location.hash));3839/** The drawing once it has rendered, and the line to bring into view in it. */40interface Pending {41 line?: number | undefined;42 node?: HTMLElement;43}4445/**46 * Scrolls the drawing to the pending line, once: the line clears when it47 * is found, so later renders leave the reader where they are.48 */49const reveal = (pending: Pending) => {50 const { line, node } = pending;5152 const row =53 line === undefined54 ? null55 : node?.shadowRoot?.querySelector(`[data-line="${line}"]`);56Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 57–61why, one step back57 if (row instanceof HTMLElement) {58 // Whichever scrolls the code (main from the shell's width, the page59 // below it) brings the line to its middle, and only up or down: the60 // code's own sideways scroll stays where it is.61 row.scrollIntoView({ block: "center", inline: "nearest" });Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 62–93why, one step back62 pending.line = undefined;63 }64};6566export const FileCode = ({ path, sha, text }: FileCodeProps) => {67 const [selected, setSelected] = useState<LineRange | null>(null);68 const pending = useRef<Pending>({});6970 useEffect(() => {71 const read = () => {72 const range = fromAddress();7374 setSelected(range);75 pending.current.line = range?.start;76 reveal(pending.current);77 };7879 read();80 globalThis.addEventListener("hashchange", read);8182 return () => {83 globalThis.removeEventListener("hashchange", read);84 };85 }, []);8687 const file = useMemo(88 () => ({ cacheKey: sha, contents: text, name: path }),89 [path, sha, text]90 );9192 const options = useMemo(93 (): FileOptions<undefined, undefined> => ({Draw text, controls, prose and code from one set of tokensoscabrielbefore adoption60f862e
line 94why, one step back94 ...diffsOptions,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 95–115why, one step back95 enableLineSelection: true,96 lineHoverHighlight: "number",97 onLineSelected: (range: SelectedLineRange | null) => {98 const lines =99 range === null100 ? null101 : {102 end: Math.max(range.start, range.end),103 start: Math.min(range.start, range.end),104 };105106 setSelected(lines);107 globalThis.history.replaceState(108 globalThis.history.state,109 "",110 lines === null111 ? `${globalThis.location.pathname}${globalThis.location.search}`112 : lineHash(lines)113 );114 },115 onPostRender: (node: HTMLElement) => {Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
line 116why, one step back116 diffsOptions.onPostRender(node);Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 117–119why, one step back117 pending.current.node = node;118 reveal(pending.current);119 },Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
line 120why, one step back120 disableFileHeader: true,Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 121–126why, one step back121 overflow: "scroll",122 }),123 []124 );125126 return (Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 127–131why, one step back127 <CodePool>128 <div className="min-w-0" data-code="pierre">129 <File file={file} options={options} selectedLines={selected} />130 </div>131 </CodePool>Merge branch 'build/904-web-code-browser'oscabrielbefore adoption06e2229
lines 132–136why, one step back132 );133};134135// `React.lazy` takes a module's default export.136export default FileCode;