fix: thumbnails via image() relative to appHome, paste dir from appHome

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 12:09:32 +01:00
1 parent dd95bf2c68
commit 65b66f86cd
3 files changed
+60 -6

No files matched your search

+23
View File
@@ -192,3 +192,26 @@ export function formatBytes(bytes) {
if (bytes < 1024 * 1024 * 1024) return unit(bytes / 1024 / 1024, "MB");
return unit(bytes / 1024 / 1024 / 1024, "GB");
}
/**
* The path of a file relative to a folder - or nothing when the file is not
* inside it.
*
* `image()` draws only files of the application's own folder, and wants their
* path relative to it (forward slashes). Windows paths compare without regard
* to case and to the kind of slash.
*
* @param {string} base - the folder (`appHome()`)
* @param {string} path - the file
* @returns {string|null}
*/
export function relativeTo(base, path) {
if (typeof base !== "string" || typeof path !== "string" || base === "" || path === "") return null;
const clean = (text) => text.replace(/\\/g, "/").replace(/\/+$/, "");
const root = clean(base);
const file = clean(path);
if (file.length <= root.length + 1) return null;
if (file.slice(0, root.length).toLowerCase() !== root.toLowerCase()) return null;
if (file[root.length] !== "/") return null;
return file.slice(root.length + 1);
}
+18 -6
View File
@@ -27,7 +27,7 @@ import {
theme,
exec,
hc,
home,
appHome,
run,
settings,
shared,
@@ -42,7 +42,7 @@ import { keyIntent } from "./keys.js";
import { decide, shouldDrain, shouldStartCountdown, parseSendArgs, sendNow as sendToTerminal } from "./delivery.js";
import { detect as detectTrigger, rank, merge, startsLine } from "./suggest.js";
import * as catalog from "./catalog.js";
import { LIMITS, validate, mention, removeMention, kindOf, formatBytes } from "./attachments.js";
import { LIMITS, validate, mention, removeMention, kindOf, formatBytes, relativeTo } from "./attachments.js";
import {
clipboardScript,
parseClipboardResult,
@@ -876,7 +876,9 @@ export default class Composer extends View {
// --- the preview of an image ----------------------------------------------
openPreview(file, cx) {
this.preview = { path: file.path, caption: `${file.name} · ${formatBytes(file.bytes) || "?"} · Esc zamyka` };
const path = this.imageSource(file);
if (!path) return;
this.preview = { path, caption: `${file.name} · ${formatBytes(file.bytes) || "?"} · Esc zamyka` };
// The overlay is drawn on the next frame; it takes the keyboard then.
cx.timer.after(30, () => {
try {
@@ -888,6 +890,15 @@ export default class Composer extends View {
cx.notify();
}
/**
* What `image()` is given for a file: its path relative to the app's folder
* (the pasted images are there) - or nothing for a file outside it, which
* `image()` cannot draw.
*/
imageSource(file) {
return file.kind === "image" ? relativeTo(appHome(), file.path) : null;
}
closePreview(cx) {
this.preview = null;
cx.notify();
@@ -1879,7 +1890,7 @@ export default class Composer extends View {
/** Where pasted images are saved before they go to the agent. */
pasteDir() {
return [home(), ".config", "herdr-app", "data", "ovis-composer", "paste"].join("\\");
return [appHome(), "data", "ovis-composer", "paste"].join("\\");
}
/**
@@ -2412,8 +2423,9 @@ export default class Composer extends View {
const adding = file.state === "adding";
const extension = (file.name.split(".").pop() || "").slice(0, 4);
const source = this.imageSource(file);
const thumb =
file.kind === "image"
source
? div()
.id(`file-thumb-${file.id}`)
.size(36)
@@ -2423,7 +2435,7 @@ export default class Composer extends View {
.cursor_pointer()
.when(adding, (el) => el.opacity(0.35))
.on_click((event, cx) => this.openPreview(file, cx))
.child(image(file.path).size(36))
.child(image(source).size(36))
: div()
.size(36)
.flex_none()