Paste an email's text instead of a photo; dates without a year go in the year nearest the scan's month

This commit is contained in:
iamdoubz committed 2026-11-09 20:59:25 -06:00
1 parent 10712db1e9
commit eab7c5a50a
12 files changed
+220 -39

No files matched your search

+1
View File
@@ -35,6 +35,7 @@ export interface MenuRead { items: string[]; no_lunch: boolean }
export interface Scan {
id: number; source_id: number; source_name: string; layout_type: Source['layout_type']; hint_month: string;
date_start: string | null; date_end: string | null; image_keys: string[];
text: string | null; // pasted text (an email) instead of photos
status: 'queued' | 'parsed' | 'failed' | 'abandoned' | 'reviewed' | 'published';
error: string | null; seconds: number | null; model: string | null; cost_tokens: number | null; created_at: string; published_at: string | null;
events: CalEvent[]; menu_items: MenuItem[]; can_undo: boolean;
+34 -5
View File
@@ -3,19 +3,26 @@
import FileText from '@lucide/svelte/icons/file-text';
import ImagePlus from '@lucide/svelte/icons/image-plus';
import LoaderCircle from '@lucide/svelte/icons/loader-circle';
import Camera from '@lucide/svelte/icons/camera';
import Mail from '@lucide/svelte/icons/mail';
import Settings from '@lucide/svelte/icons/settings';
import Sparkles from '@lucide/svelte/icons/sparkles';
import { api, type Scan } from '../lib/api';
import { app, go } from '../lib/state.svelte';
import { thisMonth } from '../lib/dates';
import { LAYOUT_ICONS } from '../lib/icons';
import PageHeader from '../lib/ui/PageHeader.svelte';
import EmptyState from '../lib/ui/EmptyState.svelte';
import Segmented from '../lib/ui/Segmented.svelte';
const sources = $derived(app.household!.sources);
const layoutNames = { month_grid: 'Month calendar', menu: 'Lunch menu', list: 'Flyer or list' } as const;
let sourceId = $state<number | null>(null);
let month = $state(thisMonth());
let files = $state<FileList | null>(null);
let input = $state<'photo' | 'text'>('photo');
let text = $state('');
const ready = $derived(sourceId !== null && (input === 'text' ? text.trim().length > 0 : !!files?.length));
let busy = $state(false);
let error = $state('');
@@ -34,9 +41,20 @@
async function submit(e: SubmitEvent) {
e.preventDefault();
if (!files?.length || sourceId === null) return;
if (!ready) return;
busy = true;
error = '';
if (input === 'text') {
try {
const scan = await api<Scan>('/scans/text', { method: 'POST', json: { source_id: sourceId, month, text } });
go(`#/scan/${scan.id}`);
} catch (err: any) {
error = err.message;
} finally {
busy = false;
}
return;
}
const form = new FormData();
form.set('source_id', String(sourceId));
form.set('month', month);
@@ -54,7 +72,7 @@
}
</script>
<PageHeader title="New scan" subtitle="The photo is read on your own server, then you review what was found." back="#/" />
<PageHeader title="New scan" subtitle="It's read on your own server, then you review what was found." back="#/" />
{#if sources.length === 0}
<EmptyState icon={Settings} title="Add a source first">
@@ -87,7 +105,15 @@
</div>
<div class="section">
<span class="section-title" id="photo-label">3 · Photo, screenshot or PDF</span>
<div class="step-head">
<span class="section-title" id="photo-label">3 · {input === 'text' ? 'The email' : 'Photo, screenshot or PDF'}</span>
<Segmented label="What you have" bind:value={input} options={[{ value: 'photo', label: 'Photo', icon: Camera }, { value: 'text', label: 'Paste text', icon: Mail }]} />
</div>
{#if input === 'text'}
<textarea id="pasted" class="pasted" bind:value={text} maxlength="20000" required aria-labelledby="photo-label"
placeholder={'November Calendar Dates\n5th - Bring a G sound\n9th - Bring your disguised turkey'}></textarea>
<p class="hint">Copy the email's text and paste it here. Keep the month names in; a day like "5th" belongs to the month named before it.</p>
{:else}
<!-- No `capture` attribute: phones then offer both the camera and the photo library. -->
<label class="drop" class:has-files={previews.length}>
<input type="file" accept="image/*,application/pdf" multiple bind:files required aria-labelledby="photo-label" />
@@ -104,14 +130,15 @@
<span class="hint">Two-sided sheet? Add both photos.</span>
{/if}
</label>
{/if}
</div>
{#if error}
<div class="alert error" role="alert"><CircleAlert size={18} aria-hidden="true" /><span>{error}</span></div>
{/if}
<button class="primary lg block" type="submit" disabled={busy || !files?.length || sourceId === null}>
{#if busy}<LoaderCircle size={20} class="spin" aria-hidden="true" /> Uploading…{:else}<Sparkles size={20} aria-hidden="true" /> Read it{/if}
<button class="primary lg block" type="submit" disabled={busy || !ready}>
{#if busy}<LoaderCircle size={20} class="spin" aria-hidden="true" /> {input === 'text' ? 'Sending…' : 'Uploading…'}{:else}<Sparkles size={20} aria-hidden="true" /> Read it{/if}
</button>
</form>
{/if}
@@ -130,6 +157,8 @@
.source.on .src-icon { background: var(--surface); color: var(--primary-text); }
.src-name { display: block; font-weight: 650; }
.month { max-width: 260px; }
.step-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--s2); }
.pasted { min-height: 240px; }
.drop {
position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--s2);
+28 -9
View File
@@ -54,6 +54,13 @@
].map((e) => e.id));
const allSelected = $derived(visibleIds.length > 0 && visibleIds.every((id) => selected.includes(id)));
const activeEvent = $derived(scan.events.find((e) => e.id === active));
// A pasted email: the panel shows its text, with the active event's sentence marked instead of a box on a photo.
const isText = $derived(scan.text != null);
const spot = $derived.by(() => {
const said = activeEvent?.source_text?.trim();
const at = said ? (scan.text ?? '').toLowerCase().indexOf(said.toLowerCase()) : -1;
return at < 0 ? null : [at, at + said!.length];
});
const months = $derived(
monthsBetween(scan.date_start ?? `${scan.hint_month}-01`, scan.date_end ?? `${scan.hint_month}-01`),
);
@@ -78,6 +85,8 @@
if (e.page) page = e.page;
// Scroll the photo panel to the highlighted spot.
requestAnimationFrame(() => {
const mark = photo?.querySelector('mark');
if (photo && mark) return photo.scrollTo({ top: mark.offsetTop - 24, behavior: 'smooth' });
const img = photo?.querySelector('img');
if (!photo || !img || !e.cell_bbox) return;
photo.scrollTo({ top: e.cell_bbox[1] * img.clientHeight - 24, left: e.cell_bbox[0] * img.clientWidth - 24, behavior: 'smooth' });
@@ -140,7 +149,7 @@
<input type="checkbox" bind:group={selected} value={e.id} aria-label={`Select ${e.title}`} />
{/if}
<span class="kind" title={KINDS[e.kind]}><KindIcon size={18} aria-hidden="true" /></span>
<button class="item" onclick={() => focus(e)} aria-pressed={active === e.id} aria-label={`${e.title}, ${whenLabel(e)}. Show on photo`}>
<button class="item" onclick={() => focus(e)} aria-pressed={active === e.id} aria-label={`${e.title}, ${whenLabel(e)}. Show ${isText ? 'in the email' : 'on photo'}`}>
<span class="title">{e.title}</span>
<span class="meta">
<span>{whenLabel(e)}</span>
@@ -195,20 +204,27 @@
{/snippet}
<div class="layout">
<aside class="photo-col" aria-label="Scanned page">
<aside class="photo-col" aria-label={isText ? 'Pasted email' : 'Scanned page'}>
<div class="photo-card">
<div class="photo-bar">
{#if scan.image_keys.length > 1}
{#if isText}
<span class="hint">Tap an event to see where it came from</span>
{:else if scan.image_keys.length > 1}
<Segmented label="Page" bind:value={page} options={scan.image_keys.map((_, i) => ({ value: i + 1, label: `Page ${i + 1}` }))} />
{:else}
<span class="hint">Tap an event to see where it came from</span>
{/if}
<button class="sm ghost" aria-pressed={zoom} onclick={() => (zoom = !zoom)} aria-label={zoom ? 'Fit photo' : 'Zoom photo'}>
{#if zoom}<ZoomOut size={18} aria-hidden="true" />{:else}<ZoomIn size={18} aria-hidden="true" />{/if}
</button>
{#if !isText}
<button class="sm ghost" aria-pressed={zoom} onclick={() => (zoom = !zoom)} aria-label={zoom ? 'Fit photo' : 'Zoom photo'}>
{#if zoom}<ZoomOut size={18} aria-hidden="true" />{:else}<ZoomIn size={18} aria-hidden="true" />{/if}
</button>
{/if}
</div>
<div class="photo" bind:this={photo} class:zoom>
{#if scan.image_keys.length}
<div class="photo" bind:this={photo} class:zoom class:text={isText}>
{#if isText}
<!-- One line: the text keeps its own line breaks (pre-wrap), so template whitespace would show. -->
<p class="email">{#if spot}{scan.text!.slice(0, spot[0])}<mark>{scan.text!.slice(spot[0], spot[1])}</mark>{scan.text!.slice(spot[1])}{:else}{scan.text}{/if}</p>
{:else if scan.image_keys.length}
<div class="frame">
<img src={`/api/scans/${scan.id}/pages/${page}`} alt={`Page ${page} of the scanned ${scan.source_name}`} />
{#if activeEvent?.cell_bbox && activeEvent.page === page}
@@ -276,7 +292,7 @@
{#if !live.length && !scan.menu_items.length}
<div class="alert info">Nothing was found on this page. Add events by hand, or discard the scan.</div>
{/if}
{@render group('Needs a look', TriangleAlert, 'warn', needs, 'Each one says why. Tap it to see where it came from on the photo.')}
{@render group('Needs a look', TriangleAlert, 'warn', needs, `Each one says why. Tap it to see where it came from ${isText ? 'in the email' : 'on the photo'}.`)}
{@render group('Updated', RefreshCw, 'info', updated, 'Already on your calendar; publishing applies these changes.')}
{@render group('New', Sparkles, '', fresh, '')}
{@render collapsed('unchanged', 'Unchanged', CheckCheck, unchanged)}
@@ -313,6 +329,9 @@
.frame { position: relative; }
.frame img { display: block; width: 100%; height: auto; }
.zoom .frame { width: 250%; }
.photo.text { position: relative; background: var(--surface); } /* relative: the mark's offsetTop is within it */
.email { margin: 0; padding: var(--s3) var(--s4); white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.6; }
.email mark { color: var(--text); background: var(--highlight); border-radius: 4px; box-shadow: 0 0 0 2px var(--primary); }
.box { position: absolute; border: 3px solid var(--primary); background: var(--highlight); border-radius: 6px; pointer-events: none; box-shadow: 0 0 0 4px color-mix(in srgb, var(--primary) 20%, transparent); }
.list-col { display: flex; flex-direction: column; gap: var(--s5); min-width: 0; }
+14 -3
View File
@@ -6,6 +6,7 @@
import CircleCheck from '@lucide/svelte/icons/circle-check';
import Download from '@lucide/svelte/icons/download';
import ImageIcon from '@lucide/svelte/icons/image';
import Mail from '@lucide/svelte/icons/mail';
import LoaderCircle from '@lucide/svelte/icons/loader-circle';
import RefreshCw from '@lucide/svelte/icons/refresh-cw';
import Sandwich from '@lucide/svelte/icons/sandwich';
@@ -96,7 +97,7 @@
<div class="wide"><dt>Model</dt><dd>{scan.model}</dd></div>
<div><dt>Tokens used</dt><dd>{scan.cost_tokens?.toLocaleString() ?? '—'}</dd></div>
<!-- Photos are deleted after IMAGE_RETENTION_DAYS, and the page count with them. -->
<div><dt>Pages</dt><dd>{scan.image_keys.length || '—'}</dd></div>
<div><dt>Pages</dt><dd>{scan.text != null ? 'Pasted text' : scan.image_keys.length || '—'}</dd></div>
<div><dt>Events</dt><dd>{scan.events.length}</dd></div>
{#if scan.menu_items.length}<div><dt>Menu days</dt><dd>{scan.menu_items.length}</dd></div>{/if}
<div><dt>Scan time</dt><dd>{scan.seconds!.toFixed(1)} s</dd></div>
@@ -106,7 +107,7 @@
{#if scan.status === 'queued'}
<section class="card state" aria-live="polite">
<span class="state-icon info"><LoaderCircle size={28} class="spin" aria-hidden="true" /></span>
<h2>Reading the page</h2>
<h2>{scan.text != null ? 'Reading the email' : 'Reading the page'}</h2>
<p class="muted">This usually takes under a minute. You can leave this screen; the scan waits on the Scans tab.</p>
</section>
{:else if scan.status === 'failed'}
@@ -144,7 +145,16 @@
<a class="btn ghost" href="#/calendar"><CalendarDays size={18} aria-hidden="true" /> See the calendar</a>
</div>
</section>
{#if scan.image_keys.length}
{#if scan.text != null}
<details class="card photos">
<summary>
<Mail size={18} aria-hidden="true" />
<span class="grow">Pasted email</span>
<ChevronDown size={20} class="chev" aria-hidden="true" />
</summary>
<p class="pasted">{scan.text}</p>
</details>
{:else if scan.image_keys.length}
<!-- Closed by default; the lazy images only load once it's opened. -->
<details class="card photos">
<summary>
@@ -195,6 +205,7 @@
.photo-list { display: flex; flex-direction: column; gap: var(--s3); padding: 0 var(--s4) var(--s4); }
.photo-list a { display: block; border: 1px solid var(--border); border-radius: var(--r-md); overflow: hidden; background: var(--surface-2); }
.photo-list img { display: block; width: 100%; height: auto; }
.pasted { margin: 0; padding: 0 var(--s4) var(--s4); white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.6; }
.photos-gone { margin-top: var(--s4); text-align: center; }
.menu-fix { margin-top: var(--s6); }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: var(--s3) var(--s4); margin: calc(-1 * var(--s2)) 0 var(--s4); }