Official
tabs
The tabbed detail pane beside the chat: files to edit and diffs (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/tabs@0.2.5
Permissions in 0.2.5
Files
views.tsx8 KB
// What a file, diff, terminal or diagram tab shows, wherever its tab is: the
// side split or a task's pane (specs/tasks-and-splits/05). The chat plugin
// draws the tab and its pane; handlers only run this plugin's actions.
import * as kit from "divergence";
import type { CodeQuoteEvent, DropEvent, QuoteEvent } from "divergence";
import type { EffectViewContext } from "divergence/effect";
import type { Tabs } from "./controller.ts";
import { activeTab, editorId, editorText, extension, isDirty, LANGUAGES, previewKind, tabName } from "./tabs.ts";
import type { FileState, Tab } from "./tabs.ts";
// The shared surface looks from the runtime; an older runtime without them
// gets the same `elevation`, which its kernel already draws.
const RAISED = kit.RAISED ?? { elevation: "raised" };
const EDGED = kit.EDGED ?? { elevation: "edge" };
const FLAT = kit.FLAT ?? { elevation: "none" };
// An older runtime drops a web document's `file`, and its kernel cannot
// show a workspace page: HTML files offer no preview there.
// What a file tab can show besides its editor.
function previewable(tab: Tab) {
const kind = tab.kind === "file" ? previewKind(tab.path) : null;
return kind;
}
// Every tab strip in the app caps a tab at this width (DESIGN.md, "Tabs").
const TAB_MAX_WIDTH = 200;
// Layout for the templates below: a flex column, a row that centres its
// children across, a child that takes the free space (and may shrink below
// its content), the whole of the parent, the hover wash, and a child of a
// sideways strip that keeps its width.
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;
const HOVER = { background: "hover" } as const;
const keep = { flexGrow: 0, flexShrink: 0, flexBasis: "auto" } as const;
// The close button and the unsaved mark lie over each other; the pointer
// on the tab swaps them.
const layer = {
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
...column,
justifyContent: "center",
alignItems: "center",
};
const hideOnHover = { groupHover: { group: "hover", visibility: "hidden" } } as const;
const revealOnHover = { visibility: "hidden", groupHover: { group: "hover", visibility: "visible" } } as const;
// The open tab is raised like the panes (the kernel's `elevation`). Every
// tab keeps the edge's width, so opening one never moves its title.
// A tab's icon sits as far from its left edge as from its top: the close
// button (1.5rem) sets the tab's height, the icon is 0.875rem, the tab's
// padding 0.25rem.
const TAB_LEAD = 15 * (0.25 + (1.5 - 0.875) / 2);
// What a tab carries while it is dragged to another place in the strip.
const TAB_DRAG = "divergence/tabs.tab";
/** The tab kind's icon, for a tab of this plugin. */
export function tabIcon(kind: Tab["kind"]): string {
return kind === "diff" ? "git-compare" : kind === "terminal" ? "square-terminal" : kind === "diagram" ? "workflow" : "file";
}
const errorText = (error: string) => <div {...column} gap="small" padding="medium"><text style="body" size="xs" tone="danger">{error}</text></div>;
export function renderItem(ctx: EffectViewContext<object>, actions: Tabs["Service"], tab: Tab) {
const { state } = actions;
function renderContent(tab: Tab) {
if (tab.kind === "terminal") {
return <div {...column} {...fill} {...full} gap="small" padding="small" paddingX="small"><terminal terminal={tab.path} /></div>;
}
const content = state.contents[tab.id];
if (tab.kind === "diagram") {
return content?.error ? errorText(content.error) : <diagram id={`diagram-${tab.id}`} source={tab.source} language={tab.language} />;
}
// A file waits a moment for its text; only a slow read shows it loading.
if (!content) return state.waiting.has(tab.id) ? null : <empty>Loading…</empty>;
// What was typed and not saved is kept in the tab's own state, which
// outlives a reload of this plugin; it goes back over the file read again.
const kept = ctx.state as { draft?: string };
if (kept.draft !== undefined && content.draft === undefined && content.text !== undefined) content.draft = kept.draft;
// A native file editor reports its own errors (below).
if (content.error && (tab.kind !== "file" || !actions.nativeFiles)) return errorText(content.error);
// A selection in a file, a diff or a Markdown preview can be quoted into
// the open chat.
const onQuote = (event: QuoteEvent | CodeQuoteEvent) => ctx.run(actions.reply(tab, event));
if (tab.kind === "diff") {
return (
<div {...column} {...fill} gap="small" padding="medium" overflowY="scroll">
{content.diff
? <diff id={`diff-${tab.id}`} diff={content.diff} path={tab.path} onQuote={onQuote} />
: <text style="muted">No changes.</text>}
</div>
);
}
const ext = extension(tab.path);
const preview = tab.preview ? previewable(tab) : null;
if (preview === "markdown" && !actions.nativeFiles) {
return (
<div {...column} {...fill} gap="small" paddingX="small" overflowY="scroll">
<markdown id={`md-${tab.id}`} path={tab.path} onQuote={onQuote}>{editorText(content)}</markdown>
</div>
);
}
if (actions.nativeFiles && tab.kind === "file") {
const error = content.fileState?.state === "error" ? content.fileState.error : undefined;
return (
<div {...column} {...fill} minWidth={0}>
{error ? <text style="body" size="xs" tone="danger">{error}</text> : null}
<editor
id={editorId(tab)}
file={{ workspaceId: tab.workspaceId, path: tab.path }}
revision={content.revision}
presentation={preview === "markdown" ? "markdown" : preview === "html" ? "html" : "editor"}
language={LANGUAGES[ext] ?? "text"}
readOnly={false}
onFileState={(event: FileState) => ctx.run(actions.fileState(tab.id, event))}
onQuote={onQuote}
/>
</div>
);
}
// The file as saved, with what it names, from the workspace.
if (preview === "html") {
return <web_document id={`web-${tab.id}`} height={0} file={{ workspaceId: tab.workspaceId, path: tab.path }} revision={content.revision} />;
}
return <editor id={editorId(tab)} text={editorText(content)} language={LANGUAGES[ext] ?? "text"} readOnly={!!content.truncated} onChange={({ value }: { value: string }) => {
kept.draft = value;
ctx.run(actions.edit(tab.id, value));
}} onSave={({ value }: { value: string }) => {
kept.draft = undefined;
ctx.run(actions.save(tab, value));
}} onQuote={onQuote} />;
}
return (
<div {...column} {...fill} {...full} padding="small">
{tab.kind === "terminal" || tab.kind === "diagram"
? null
: (
<div {...row} gap="small" padding="small" paddingX="medium" paddingY="small">
<text style="mono" tone="muted" truncate>{tab.path}</text>
<spacer />
{previewable(tab)
? (
<div {...row} gap="small" padding="small">
<button id={`preview-${tab.id}`} icon="eye" label="Preview" variant="ghost" small active={tab.preview} onClick={() => ctx.run(actions.showPreview(tab, true))} />
<button id={`editor-${tab.id}`} icon="code" label="Editor" variant="ghost" small active={!tab.preview} onClick={() => ctx.run(actions.showPreview(tab, false))} />
</div>
)
: null}
</div>
)}
<div {...column} {...fill} gap="small" padding="small">{renderContent(tab)}</div>
</div>
);
}
No comments yet.