# bf-filetree: the BenchFlow file browser

A file tree with a file viewer, in plain JavaScript, for any page that shows a folder of files: each WeightBench
task page uses it for the task's folders, and the hub or a run viewer can drop it in the same way.

- `bf-filetree.js`: the component. It defines `window.BfFileTree` (version 4.0.0).
- `bf-filetree.css`: its layout, look and syntax colours, light and dark. The code colours are the generated
  `pierre-theme` block at its end (below).
- `icons-pierre.svg`: the icons for the bar and the states: @pierre/trees' own built-in file icons with the colours
  its tree gives them (Apache-2.0, `../vendor/pierre-trees-LICENSE.md`), copied from the vendored bundle by
  `tools/build_ft_pierre.py`, so the bar shows the icon the tree row shows; plus the browser's own action icons (copy,
  check, raw, download, wrap), drawn for BenchFlow and kept in that script.

| Icons | Code colours | Source | Licence |
|---|---|---|---|
| @pierre/trees' built-in file icons (the `complete` set, coloured) | Pierre Light / Pierre Dark from [@pierre/theme](https://www.npmjs.com/package/@pierre/theme) | the vendored @pierre/trees 1.0.0-beta.5 bundle; @pierre/theme 2.0.0, npm (`sha512-yNDd9GYLQl1mEUJR8AneJ5e4ohLIHQd/wZLWr4fagt78vS2RwwZNW530vVgHqXFAyFVcFlRmGUD5ramXH46OXw==`) | Apache-2.0, `../vendor/pierre-trees-LICENSE.md`, `../vendor/pierre-theme-LICENSE.md` and `../vendor/pierre-theme-NOTICE.md` |

[@pierre/trees](https://www.npmjs.com/package/@pierre/trees) 1.0.0-beta.5 (Apache-2.0) does the tree's
virtualization, search, keyboard navigation and selection. [Prism](https://prismjs.com/) 1.30.0 (MIT) highlights
code. Both are vendored in `../vendor/` with their licences, and Prism loads only when a file's source is first
shown. The styles inside and around the tree and the viewer are this component's own.

## The look

The browser has one look, the look of T3 Code's file panel (it was `v5` among the looks the WeightBench site
compared; the root element carries `data-ft="v5"`, which the styles key on). The earlier looks (v3 with its own icons,
v4a-v4d on four published icon themes) were removed with their sprites, licences and build script.

| Icons | Tree | Viewer |
|---|---|---|
| @pierre/trees' own (the tree draws them; the bar and states use `icons-pierre.svg`) | the system sans at 12 px on compact 24 px rows, rounded hover and selection tints, a "Files" header with the count, a rounded search field | the path in monospace (folder muted), size and lines; code in the system monospace at 13 px with line numbers and Pierre Light / Pierre Dark colours; Markdown in the sans at reading width with the frontmatter folded as YAML; footers that line up across a resizable divider |

It also counts the files that match a search (the tree's own rule: the path contains the query) and says when none
does, marks the matched text in each name (a CSS highlight; nothing inside the tree changes), shows "Loading…" only
after 200 ms, and gives Python calls and ALL_CAPS names the colours the Pierre themes give them under a TextMate
grammar (a small Prism addition). The divider between the panes takes a drag, the arrow keys, Home and End, and a
double click to reset; it is hidden on phones, where the tree sits above the file.

## Use

```html
<link rel="stylesheet" href="assets/bf-filetree/bf-filetree.css">
<script src="assets/bf-filetree/bf-filetree.js"></script>
<div id="files"></div>
<script>
  const browser = BfFileTree.mount(document.getElementById('files'), {
    files: [
      {path: 'task.md', size: 2048, html: '<h3>Task</h3><p>…</p>', meta: ['sha256 9f2c…']},
      {path: 'environment/Dockerfile', size: 311},
      {path: 'data/weights.safetensors', size: 5e6, note: 'Files over 1 MB are in the download only.'},
    ],
    preview: (file) => file.size < 1e6 ? `files/${file.path}` : null,
    archive: {href: 'task.tar.gz', label: 'Download task.tar.gz (2.1 MB)'},
    note: '4 grader data files withheld',
    initial: 'task.md',
    onSelect: (path, file) => console.log(path, file),
  });
  // browser.select('environment/Dockerfile'); browser.destroy();
</script>
```

`mount(element, options)` replaces the element's content with the browser and returns `{select(path), destroy()}`.
`select` shows a file and scrolls the tree to it (it returns false for an unknown path); `destroy` removes the browser
and frees the tree.

| Option | Meaning |
|---|---|
| `files` | Required. The files to list: `{path, size?, html?, note?, meta?}`. `html` is the file already rendered (for example Markdown); it is inserted as HTML, so pass only HTML you trust. `note` is shown when the file has no preview. `meta` is a list of short lines shown under the file. |
| `preview(file)` | Returns the URL to fetch the file's text (or image) from, or `null` for no preview. The URL is also the "Raw" link. A file with `html` and no URL shows its `html` only. |
| `download(file)` | The URL for the file's Download button. Default: the preview URL. |
| `archive` | `{href, label}`: a download for the whole folder, offered where a file cannot be shown. |
| `note` | One quiet line under the tree, for example what the folder withholds. Files that are not published are not listed. |
| `initial` | The path selected first. Default: the first file in order. |
| `onSelect(path, file)` | Called whenever a file is shown. |
| `kinds` | Extra extensions by file type, merged over the defaults: `{python: ['pyx'], yaml: ['cff'], ...}`. Types: python, json, markdown, shell, docker, toml, yaml, ini, text, table, template, code, image, archive, binary. |
| `trees`, `icons`, `highlighter` | URLs of the @pierre/trees bundle, the icon sprite and the Prism bundle. Default: next to this file (`../vendor/pierre-trees-1.0.0-beta.5.js`, `icons-pierre.svg`, `../vendor/prism-1.30.0-bfft.min.js`). |

The viewer shows:
- Markdown with `html` as rendered, with a Preview / Source switch;
- source and text highlighted (Python, JSON, shell, Dockerfile, TOML, YAML, Markdown) with line numbers. Long lines
  scroll sideways and the Wrap button wraps them at the code's left edge. Text over 400 KB shows without highlighting;
- images as images;
- a state for a folder (its file count and size), an empty file, a file with no preview (its type, size, note and
  the folder download) and a file that failed to load.

The bar above the file shows its type icon, folder and name, its type, size and line count, and the actions Wrap,
Copy, Raw and Download. Keyboard: the tree takes arrow keys, Enter and type-to-search; `/` moves to the search
field from anywhere in the browser; the file pane scrolls with the keyboard once focused.

## Colours and type

The colours, type and sizes are CSS custom properties on `.bf-ft`: `--bf-ft-bg`, `--bf-ft-page`, `--bf-ft-fg`,
`--bf-ft-mfg`, `--bf-ft-muted`, `--bf-ft-border`, `--bf-ft-focus`, `--bf-ft-warn`, `--bf-ft-radius`, `--bf-ft-font`,
`--bf-ft-mono`, `--bf-ft-height` (640px) and `--bf-ft-side` (the tree's width, 280px), and the fallback syntax colours
`--bf-tok-*`. The base rules follow the BenchFlow house tokens (`--sb-*`) when the page defines them, and otherwise
their own light and dark values, following `prefers-color-scheme` or `data-theme="light|dark"` on the root element.
The look sets neutral greys with `light-dark()` under `.bf-ft[data-ft="v5"]` and takes its background from
`--sb-card` when the page has it; its tokens add `--bf-ft-rule` (the quieter inner rules), `--bf-ft-r` and
`--bf-ft-r-sm` (corner radii) and `--bf-pt-*` (the Pierre themes' text, line-number, accent and selection colours). Its
syntax colours are generated: `tools/build_ft_pierre.py --theme DIR` gives each Prism token class the TextMate scope
its grammar would give the same text, looks the colour up in pierre-light.json and pierre-dark.json as an editor does,
and writes the block between the `pierre-theme:` markers; `--check` rebuilds and compares. Below 640px the tree sits
above the viewer.

## Drop it into another site

Copy `assets/bf-filetree/` and `assets/vendor/` (the @pierre/trees and Prism bundles, and the licence files of those
and of @pierre/theme) as they are, keeping the two folders side by side, or pass `trees`, `icons` and `highlighter` with your own URLs. Load the
CSS and the script, give the browser an element, and pass your files. The tree bundle is an ES module loaded on the
first `mount`; the page needs no build step and no framework. For a hub or run viewer, map each artifact to
`{path, size}`, return its URL from `preview`, and say in `note` what you cannot publish.
