Official
files
Built-in files UI (JavaScript UI plugin).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/files@0.2.4
Permissions in 0.2.4
Files
views.tsx4.8 KB
// Files rail and panel trees. Rendering only reads state; handlers run effects.
// HTM whitespace is significant; keep these templates out of Prettier's HTML formatter.
import type { EffectViewContext } from "divergence/effect";
import type { Files } from "./controller.ts";
import { isOpen, isPending, selectedPath, treeItems } from "./panel.ts";
// Layout for the templates below: a flex column, a row that centres its
// children across (the old `ui.row`), and a child that takes the free
// space and may shrink below its content.
const column = { display: "flex", flexDirection: "column" } as const;
const row = { display: "flex", flexDirection: "row", alignItems: "center" } as const;
const fill = { flexGrow: 1, flexShrink: 1, flexBasis: "0%", minWidth: 0, minHeight: 0 } as const;
const full = { width: "100%", height: "100%" } as const;
export function renderRail(ctx: EffectViewContext<object>, actions: Files["Service"]) {
// Drawn a little larger than the button's usual icon (about 13.3px): the glyph is narrower than the
// reload button's round one above it, and at 15.1px it is as wide.
return <button id="rail-files" icon="files" iconSize={15.1} variant="ghost" active={ctx.app.shared?.rightPanel === "files"} tooltip="Files" onClick={() => ctx.run(actions.toggle(ctx.app))} />;
}
export function renderPanel(ctx: EffectViewContext<object>, actions: Files["Service"]) {
const { state } = actions;
if (!isOpen(ctx.app)) return null;
const workspaceId = ctx.app.workspaceId;
if (!workspaceId) {
return <div {...column} gap="small" padding="medium"><text style="muted">Select a workspace to browse files.</text></div>;
}
// Matches show in their own tree with every folder open; the browsing
// tree stays mounted behind it, so clearing the query gives it back with
// the folders that were open.
const searching = Boolean(state.query.trim());
const open = state.open[workspaceId] ?? new Set<string>();
const source = state.all ? { files: state.all.map((path) => ({ path })) } : { items: treeItems(state.listing, open) };
// A kernel before `expanded` reports a click on a folder, which turns it.
const onSelect = (e: { id: string; folder: boolean; expanded?: boolean }) => {
if (e.folder) ctx.run(actions.setOpen(workspaceId, e.id, e.expanded ?? !open.has(e.id)));
else if (!isPending(e.id)) ctx.run(actions.openFile(workspaceId, e.id));
};
// One 0.5rem inset and one gap: the title, the field's mark and the
// tree's icons start on the same line, and the close button ends where
// the field does. The header is a rail button's height at the panel's
// top, so its buttons sit on the line of the rail's first icon.
return (
<div {...column} {...fill} {...full} padding="medium" paddingTop={0} gap="small">
<div {...row} gap="small" height="2rem">
<div {...row} {...fill} paddingX={9}><text style="body" size={13.125} weight={600} truncate>Files</text></div>
<button id="files-refresh" icon="rotate-cw" variant="ghost" small tooltip="Refresh" onClick={() => ctx.run(actions.refresh(workspaceId))} />
<button id="files-close" icon="x" variant="ghost" small tooltip="Close" onClick={() => ctx.run(actions.close)} />
</div>
<input id="files-search" value={state.query} placeholder="Search files..." icon="search" onChange={(e: { value: string }) => {
state.query = e.value;
ctx.update();
ctx.run(actions.search(workspaceId, e.value));
}} />
{searching
? (
<div {...column} {...fill}>
<tree id={`files-search-${workspaceId}`} rowDrag={fileDrag(workspaceId)} openFolders files={state.results.map((path) => ({ path }))} selected={selectedPath(state.activePath)} onSelect={(e: { id: string; folder: boolean }) => {
if (e.folder) return;
// The search has found what it was for: the tree comes
// back, with the opened file selected in it.
state.query = "";
state.results = [];
ctx.update();
ctx.run(actions.openFile(workspaceId, e.id));
}} />
</div>
)
: null}
<div {...column} {...fill} display={searching ? "none" : "flex"}>
<tree id={`files-${workspaceId}`} rowDrag={fileDrag(workspaceId)} {...source} selected={selectedPath(state.activePath)} onSelect={onSelect} />
</div>
</div>
);
}
/** A file row drags as the tabs plugin's file tab, which a pane opens where it lands. */
const fileDrag = (workspaceId: string) => ({ type: "divergence/item", itemKind: "tabs.file", keyPrefix: `file:${workspaceId}:` });
No comments yet.