diff --git a/packages/day-card/README.md b/packages/day-card/README.md index 76a9622..ede3d9f 100644 --- a/packages/day-card/README.md +++ b/packages/day-card/README.md @@ -1,8 +1,8 @@ # @magpie-community/page-day-card -A page in [magpie](https://usemagpie.ai)'s window that prints a day of your AI usage as a receipt, a "Daily AI statement" on paper over a backdrop of your choice. It has the day's API-equivalent cost, your streak, requests, errors and the day against your average day before it, then each agent and its models with their logos and costs, and the day's tokens: input, output and the cache hit rate. **Background** picks what the paper lies on: teal, fifteen patterns (gingham, graph paper, kraft, cyanotype, seigaiha waves, xuan paper and more), four landscape photos, or a picture of your own (pick one, drop it on the page or paste it). A slider sets the margin around the paper; a photo also has blur and dim. The choice is kept in the plugin's options. **Copy image** puts the card on the clipboard; **Save PNG** saves it in Downloads. +A page in [magpie](https://usemagpie.ai)'s window that prints a day of your AI usage as a receipt, a "Daily AI statement" on paper over a backdrop of your choice. It has the day's API-equivalent cost, your streak, requests, errors and the day against your average day before it, then each agent and its models with their logos and costs, and the day's tokens: input, output and the cache hit rate. **Background** picks what the paper lies on: nothing (the paper alone, a transparent PNG), teal, fifteen patterns (gingham, graph paper, kraft, cyanotype, seigaiha waves, xuan paper and more), four landscape photos, or a picture of your own (pick one, drop it on the page or paste it). A slider sets the margin around the paper; a photo also has blur and dim. The choice is kept in the plugin's options. **Copy image** puts the card on the clipboard; **Save PNG** saves it in Downloads. -Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @magpie-community/page-day-card`. It then has a tab, **Day card** (titled in the window's language: 每日用量卡, 1日の使用量カード, Tageskarte), under Plugins in the sidebar, in the app and in `magpie web`. Pick an earlier day with the arrows or the date's menu. +Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @magpie-community/page-day-card`. It then has a tab, **Day card** (titled in the window's language: 每日用量卡, 1日の使用量カード, Tageskarte), under Plugins in the sidebar, in the app and in `magpie web`. Pick an earlier day with the arrows or the date's menu. The card is shown as large as fits under the toolbar, so a whole day is on one screen; the picture you copy or save is full size. It needs a magpie with the `usage:day` read and `magpie.image` (see `engines.magpie`). An older magpie refuses the read, and the page says to update. diff --git a/packages/day-card/day-card.test.mjs b/packages/day-card/day-card.test.mjs index ac84cc4..772d4f7 100644 --- a/packages/day-card/day-card.test.mjs +++ b/packages/day-card/day-card.test.mjs @@ -236,6 +236,24 @@ test("every backdrop has a name in every language, and its picture comes with th expect(html.indexOf('src="backgrounds.js"')).toBeLessThan(html.indexOf('src="app.js"')) }) +test("no background is the first choice, and the card fits the room it has", () => { + expect(B.list[0].id).toBe("none") + expect(B.norm({ bg: "none" }).bg).toBe("none") + expect(B.meta("none").photo).toBe(false) + // nothing is drawn under the paper, so what was there is cleared + const calls = [] + const ctx = { save() {}, restore() {}, clearRect: (...a) => calls.push(a), fillRect() { throw new Error("drew") } } + B.paint(ctx, 440, 900, 2, B.norm({ bg: "none" }), null) + expect(calls).toEqual([[0, 0, 440, 900]]) + // fit: wide room, short screen -> the height rules; narrow room -> the width + expect(B.fit(900, 500, 592, 1100, 760, 160)).toBe(269) + expect(B.fit(300, 2000, 592, 1100, 760, 160)).toBe(300) + expect(B.fit(2000, 2000, 592, 1100, 760, 160)).toBe(760) + expect(B.fit(900, 10, 592, 1100, 760, 160)).toBe(160) + expect(B.fit(100, 10, 592, 1100, 760, 160)).toBe(100) + expect(read("app.js")).toContain("fitCard") +}) + test("a saved backdrop is made sound before it is drawn", () => { expect(B.norm(null)).toEqual({ bg: "teal", pad: 76, blur: 0, dim: 0, image: "" }) expect(B.norm({ bg: "seigaiha", pad: 500, blur: -3, dim: "40" })).toEqual({ bg: "seigaiha", pad: 160, blur: 0, dim: 40, image: "" }) diff --git a/packages/day-card/package.json b/packages/day-card/package.json index 3cccf6a..a373716 100644 --- a/packages/day-card/package.json +++ b/packages/day-card/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/page-day-card", - "version": "0.2.1", + "version": "0.2.2", "description": "A page in magpie's window that draws a day of your AI usage as a card: tokens, cost, each agent and its models, your streak and your average day. Copy it as a picture or save it as a PNG", "files": ["page", "README.md"], "keywords": ["magpie", "magpie-page", "usage", "tokens", "cost", "card"], diff --git a/packages/day-card/page/app.js b/packages/day-card/page/app.js index e6c7b21..bb7d577 100644 --- a/packages/day-card/page/app.js +++ b/packages/day-card/page/app.js @@ -333,7 +333,8 @@ // measure, then draw const ph = slip(document.createElement("canvas").getContext("2d"), c, new Map(), 1) + TOOTH; - const side = bg.pad; + const none = bg.bg === "none"; + const side = none ? 0 : bg.pad; const W = PW + side * 2; const H = ph + side * 2; const top = side; @@ -361,7 +362,7 @@ paper.lineTo(i * tw, ph - TOOTH); } paper.closePath(); - ctx.shadowColor = B.meta(bg.bg).shadow; + ctx.shadowColor = none ? "transparent" : B.meta(bg.bg).shadow; ctx.shadowBlur = 36; ctx.shadowOffsetY = 12; ctx.fillStyle = PAPER; @@ -370,8 +371,26 @@ slip(ctx, c, icons, scale); ctx.restore(); $("copy").disabled = $("save").disabled = !magpie.image; + state.size = [W, H]; + fitCard(); } + // The card is drawn as large as the page allows, but no taller than the + // room under the bar, so a whole day is on one screen. The bitmap (and + // the picture copied or saved) is unchanged: only its size on the page. + function fitCard() { + if (!state.size) return; + const [W, H] = state.size; + const m = $("main"); + const cs = getComputedStyle(document.body); + const availW = m.clientWidth; + const availH = window.innerHeight - m.getBoundingClientRect().top - (parseFloat(cs.paddingBottom) || 0); + $("card").style.width = `${B.fit(availW, availH, W, H, 760, 160)}px`; + } + addEventListener("resize", fitCard); + new ResizeObserver(fitCard).observe(document.querySelector("header")); + new ResizeObserver(fitCard).observe($("status")); + // ---- reading a day ---- async function load(date) { const n = ++state.loading; @@ -525,6 +544,12 @@ cv.width = cv.height = size * s; const ctx = cv.getContext("2d"); ctx.setTransform(s, 0, 0, s, 0, 0); + if (id === "none") { + // nothing, shown the way a picture editor shows it: a chequerboard + const q = size / 6; + for (let y = 0; y < 6; y++) for (let x = 0; x < 6; x++) { ctx.fillStyle = (x + y) % 2 ? "#cfcfd4" : "#fafafa"; ctx.fillRect(x * q, y * q, q, q); } + return cv; + } B.paint(ctx, size, size, s, { ...state.bg, bg: id }, null); return cv; } @@ -575,6 +600,7 @@ $("blur").value = bg.blur; $("dim").value = bg.dim; pop.classList.toggle("photo", B.meta(bg.bg).photo === true); + pop.classList.toggle("none", bg.bg === "none"); paintChip(); } async function paintChip() { @@ -586,7 +612,8 @@ const bg = state.bg; const img = await backdrop(bg); if (bg !== state.bg) return; - B.paint(ctx, 14, 14, s, bg, img); + if (bg.bg === "none") ctx.drawImage(patternCanvas("none", 14), 0, 0, 14, 14); + else B.paint(ctx, 14, 14, s, bg, img); } function openPop() { diff --git a/packages/day-card/page/backgrounds.js b/packages/day-card/page/backgrounds.js index e72917a..f4c9725 100644 --- a/packages/day-card/page/backgrounds.js +++ b/packages/day-card/page/backgrounds.js @@ -4,7 +4,9 @@ // test can check it; paint draws on a canvas. (() => { // shadow: the paper's shadow on that ground + // none: the paper alone, on nothing (a transparent PNG), with no margin const LIST = [ + { id: "none", shadow: "transparent" }, { id: "teal", shadow: "rgba(0, 60, 55, .28)" }, { id: "gingham", shadow: "rgba(90, 30, 20, .22)" }, { id: "grid", shadow: "rgba(30, 50, 90, .2)" }, @@ -219,10 +221,19 @@ function paint(ctx, W, H, scale, o, img) { ctx.save(); const m = meta(o.bg); - if (m.photo && img) photo(ctx, W, H, scale, img, o.blur, o.dim); + if (o.bg === "none") ctx.clearRect(0, 0, W, H); + else if (m.photo && img) photo(ctx, W, H, scale, img, o.blur, o.dim); else (PATTERNS[o.bg] || PATTERNS.teal)(ctx, W, H, scale); ctx.restore(); } - globalThis.DayBackgrounds = { list: LIST, meta, norm, paint, PAD, BLUR, DIM }; + // the size the card is drawn at on the page: as wide as the page allows + // (up to max), and no taller than the room it has, so a day's card is on + // one screen; never below min + function fit(availW, availH, W, H, max, min) { + const w = Math.min(max, availW, availH > 0 ? availH * W / H : Infinity); + return Math.max(Math.min(min, availW), Math.floor(Number.isFinite(w) ? w : max)); + } + + globalThis.DayBackgrounds = { list: LIST, meta, norm, paint, fit, PAD, BLUR, DIM }; })(); diff --git a/packages/day-card/page/index.html b/packages/day-card/page/index.html index 9dd697b..041b145 100644 --- a/packages/day-card/page/index.html +++ b/packages/day-card/page/index.html @@ -27,7 +27,7 @@

-
+
diff --git a/packages/day-card/page/style.css b/packages/day-card/page/style.css index 89a0529..8c6dfe6 100644 --- a/packages/day-card/page/style.css +++ b/packages/day-card/page/style.css @@ -29,7 +29,7 @@ svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: .status { max-width: 760px; margin: 8px auto; min-height: 1.45em; color: var(--muted); overflow-wrap: anywhere; } .status.bad { color: var(--red); } main { max-width: 760px; margin: 0 auto; } -canvas { display: block; width: 100%; height: auto; border-radius: 10px; } +canvas { display: block; width: 100%; height: auto; margin: 0 auto; border-radius: 10px; } @media (prefers-reduced-motion: reduce) { #menu.closing, #menu.opening { transform: none; } button:active:not(:disabled) { transform: none; } } /* the background menu */ @@ -49,6 +49,6 @@ canvas { display: block; width: 100%; height: auto; border-radius: 10px; } @media (hover: hover) and (pointer: fine) { .sw:hover:not([aria-checked="true"]) { box-shadow: inset 0 0 0 1px var(--muted); } } .slider { display: grid; grid-template-columns: 5.5em 1fr; align-items: center; gap: 10px; padding: 0 2px; color: var(--muted); } .slider input { width: 100%; margin: 0; accent-color: var(--fg); } -#bg-pop:not(.photo) .photo-only { display: none; } +#bg-pop:not(.photo) .photo-only, #bg-pop.none .pad-only { display: none; } main.drop canvas { outline: 2px dashed var(--muted); outline-offset: 4px; } @media (prefers-reduced-motion: reduce) { #bg-pop.closing, #bg-pop.opening { transform: none; } } diff --git a/packages/day-card/page/view.js b/packages/day-card/page/view.js index 3163117..e5e8904 100644 --- a/packages/day-card/page/view.js +++ b/packages/day-card/page/view.js @@ -19,7 +19,7 @@ today: "Today", prev: "Previous day", next: "Next day", copy: "Copy image", copied: "Copied", save: "Save PNG", saved: "Saved to", failed: "Couldn't read usage", shareFailed: "Couldn't share the picture", update: "This card needs a newer magpie. Update magpie, then open it again.", background: "Background", padding: "Padding", blur: "Blur", dim: "Dim", upload: "Add your own picture", imageFailed: "Couldn't use that picture", keepFailed: "Couldn't keep the background", - "bg-teal": "Teal", "bg-gingham": "Gingham", "bg-grid": "Graph paper", "bg-kraft": "Kraft paper", "bg-cyan": "Cyanotype", "bg-dots": "Night dots", "bg-deck": "Deckchair stripes", "bg-linen": "Olive linen", "bg-seigaiha": "Seigaiha waves", "bg-xuan": "Xuan paper", "bg-zigzag": "Zigzag", "bg-terracotta": "Terracotta", "bg-lilac": "Lilac lines", "bg-velvet": "Green velvet", "bg-polka": "Apricot polka", "bg-alpine": "Alpine dawn", "bg-sea": "Misty shore", "bg-dunes": "Dunes", "bg-lake": "Forest lake", "bg-custom": "Your picture", + "bg-none": "No background", "bg-teal": "Teal", "bg-gingham": "Gingham", "bg-grid": "Graph paper", "bg-kraft": "Kraft paper", "bg-cyan": "Cyanotype", "bg-dots": "Night dots", "bg-deck": "Deckchair stripes", "bg-linen": "Olive linen", "bg-seigaiha": "Seigaiha waves", "bg-xuan": "Xuan paper", "bg-zigzag": "Zigzag", "bg-terracotta": "Terracotta", "bg-lilac": "Lilac lines", "bg-velvet": "Green velvet", "bg-polka": "Apricot polka", "bg-alpine": "Alpine dawn", "bg-sea": "Misty shore", "bg-dunes": "Dunes", "bg-lake": "Forest lake", "bg-custom": "Your picture", }, zh: { title: "每日 AI 账单", tagline: "经 magpie 路由", @@ -34,7 +34,7 @@ today: "今天", prev: "前一天", next: "后一天", copy: "复制图片", copied: "已复制", save: "保存 PNG", saved: "已保存到", failed: "读取用量失败", shareFailed: "分享图片失败", update: "这个卡片需要更新版本的 magpie。请更新 magpie 后再打开。", background: "背景", padding: "边距", blur: "模糊", dim: "压暗", upload: "用自己的图片", imageFailed: "这张图片用不了", keepFailed: "背景没能保存", - "bg-teal": "青绿", "bg-gingham": "野餐格", "bg-grid": "方格纸", "bg-kraft": "牛皮纸", "bg-cyan": "蓝晒", "bg-dots": "夜色网点", "bg-deck": "躺椅条纹", "bg-linen": "橄榄亚麻", "bg-seigaiha": "青海波", "bg-xuan": "宣纸", "bg-zigzag": "人字纹", "bg-terracotta": "赤陶", "bg-lilac": "雾紫细线", "bg-velvet": "墨绿丝绒", "bg-polka": "杏色波点", "bg-alpine": "雪山晨光", "bg-sea": "雾中海岸", "bg-dunes": "沙丘", "bg-lake": "林间湖", "bg-custom": "自己的图片", + "bg-none": "无背景", "bg-teal": "青绿", "bg-gingham": "野餐格", "bg-grid": "方格纸", "bg-kraft": "牛皮纸", "bg-cyan": "蓝晒", "bg-dots": "夜色网点", "bg-deck": "躺椅条纹", "bg-linen": "橄榄亚麻", "bg-seigaiha": "青海波", "bg-xuan": "宣纸", "bg-zigzag": "人字纹", "bg-terracotta": "赤陶", "bg-lilac": "雾紫细线", "bg-velvet": "墨绿丝绒", "bg-polka": "杏色波点", "bg-alpine": "雪山晨光", "bg-sea": "雾中海岸", "bg-dunes": "沙丘", "bg-lake": "林间湖", "bg-custom": "自己的图片", }, "zh-TW": { title: "每日 AI 帳單", tagline: "經 magpie 路由", @@ -49,7 +49,7 @@ today: "今天", prev: "前一天", next: "後一天", copy: "複製圖片", copied: "已複製", save: "儲存 PNG", saved: "已儲存到", failed: "讀取用量失敗", shareFailed: "分享圖片失敗", update: "這張卡片需要更新版本的 magpie。請更新 magpie 後再開啟。", background: "背景", padding: "邊距", blur: "模糊", dim: "壓暗", upload: "用自己的圖片", imageFailed: "這張圖片用不了", keepFailed: "背景沒能儲存", - "bg-teal": "青綠", "bg-gingham": "野餐格", "bg-grid": "方格紙", "bg-kraft": "牛皮紙", "bg-cyan": "藍曬", "bg-dots": "夜色網點", "bg-deck": "躺椅條紋", "bg-linen": "橄欖亞麻", "bg-seigaiha": "青海波", "bg-xuan": "宣紙", "bg-zigzag": "人字紋", "bg-terracotta": "赤陶", "bg-lilac": "霧紫細線", "bg-velvet": "墨綠絲絨", "bg-polka": "杏色波點", "bg-alpine": "雪山晨光", "bg-sea": "霧中海岸", "bg-dunes": "沙丘", "bg-lake": "林間湖", "bg-custom": "自己的圖片", + "bg-none": "無背景", "bg-teal": "青綠", "bg-gingham": "野餐格", "bg-grid": "方格紙", "bg-kraft": "牛皮紙", "bg-cyan": "藍曬", "bg-dots": "夜色網點", "bg-deck": "躺椅條紋", "bg-linen": "橄欖亞麻", "bg-seigaiha": "青海波", "bg-xuan": "宣紙", "bg-zigzag": "人字紋", "bg-terracotta": "赤陶", "bg-lilac": "霧紫細線", "bg-velvet": "墨綠絲絨", "bg-polka": "杏色波點", "bg-alpine": "雪山晨光", "bg-sea": "霧中海岸", "bg-dunes": "沙丘", "bg-lake": "林間湖", "bg-custom": "自己的圖片", }, ja: { title: "1日の AI 明細", tagline: "magpie 経由", @@ -64,7 +64,7 @@ today: "今日", prev: "前の日", next: "次の日", copy: "画像をコピー", copied: "コピーしました", save: "PNG を保存", saved: "保存先", failed: "使用量を読み込めませんでした", shareFailed: "画像を共有できませんでした", update: "このカードには新しい magpie が必要です。magpie を更新してから開き直してください。", background: "背景", padding: "余白", blur: "ぼかし", dim: "暗さ", upload: "自分の画像を使う", imageFailed: "この画像は使えません", keepFailed: "背景を保存できませんでした", - "bg-teal": "ティール", "bg-gingham": "ギンガム", "bg-grid": "方眼紙", "bg-kraft": "クラフト紙", "bg-cyan": "青写真", "bg-dots": "夜のドット", "bg-deck": "デッキチェア", "bg-linen": "オリーブリネン", "bg-seigaiha": "青海波", "bg-xuan": "宣紙", "bg-zigzag": "ジグザグ", "bg-terracotta": "テラコッタ", "bg-lilac": "ライラック", "bg-velvet": "深緑ベルベット", "bg-polka": "アプリコット水玉", "bg-alpine": "雪山の朝", "bg-sea": "霧の海辺", "bg-dunes": "砂丘", "bg-lake": "森の湖", "bg-custom": "自分の画像", + "bg-none": "背景なし", "bg-teal": "ティール", "bg-gingham": "ギンガム", "bg-grid": "方眼紙", "bg-kraft": "クラフト紙", "bg-cyan": "青写真", "bg-dots": "夜のドット", "bg-deck": "デッキチェア", "bg-linen": "オリーブリネン", "bg-seigaiha": "青海波", "bg-xuan": "宣紙", "bg-zigzag": "ジグザグ", "bg-terracotta": "テラコッタ", "bg-lilac": "ライラック", "bg-velvet": "深緑ベルベット", "bg-polka": "アプリコット水玉", "bg-alpine": "雪山の朝", "bg-sea": "霧の海辺", "bg-dunes": "砂丘", "bg-lake": "森の湖", "bg-custom": "自分の画像", }, de: { title: "TÄGLICHE KI-ABRECHNUNG", tagline: "Geroutet über magpie.", @@ -79,7 +79,7 @@ today: "Heute", prev: "Vorheriger Tag", next: "Nächster Tag", copy: "Bild kopieren", copied: "Kopiert", save: "PNG sichern", saved: "Gesichert in", failed: "Nutzung nicht lesbar", shareFailed: "Bild nicht teilbar", update: "Diese Karte braucht ein neueres magpie. Aktualisiere magpie und öffne sie dann erneut.", background: "Hintergrund", padding: "Rand", blur: "Weichzeichnen", dim: "Abdunkeln", upload: "Eigenes Bild wählen", imageFailed: "Dieses Bild geht nicht", keepFailed: "Hintergrund nicht gesichert", - "bg-teal": "Türkis", "bg-gingham": "Vichy-Karo", "bg-grid": "Millimeterpapier", "bg-kraft": "Packpapier", "bg-cyan": "Cyanotypie", "bg-dots": "Nachtpunkte", "bg-deck": "Liegestuhlstreifen", "bg-linen": "Olivleinen", "bg-seigaiha": "Seigaiha", "bg-xuan": "Xuan-Papier", "bg-zigzag": "Zickzack", "bg-terracotta": "Terrakotta", "bg-lilac": "Fliederlinien", "bg-velvet": "Grüner Samt", "bg-polka": "Aprikosenpunkte", "bg-alpine": "Alpenmorgen", "bg-sea": "Nebelküste", "bg-dunes": "Dünen", "bg-lake": "Waldsee", "bg-custom": "Eigenes Bild", + "bg-none": "Kein Hintergrund", "bg-teal": "Türkis", "bg-gingham": "Vichy-Karo", "bg-grid": "Millimeterpapier", "bg-kraft": "Packpapier", "bg-cyan": "Cyanotypie", "bg-dots": "Nachtpunkte", "bg-deck": "Liegestuhlstreifen", "bg-linen": "Olivleinen", "bg-seigaiha": "Seigaiha", "bg-xuan": "Xuan-Papier", "bg-zigzag": "Zickzack", "bg-terracotta": "Terrakotta", "bg-lilac": "Fliederlinien", "bg-velvet": "Grüner Samt", "bg-polka": "Aprikosenpunkte", "bg-alpine": "Alpenmorgen", "bg-sea": "Nebelküste", "bg-dunes": "Dünen", "bg-lake": "Waldsee", "bg-custom": "Eigenes Bild", }, };