// Mirroring the cloud into a folder on disk: pick where, what to convert, // and watch it go. import { LitElement, html, nothing } from 'lit'; import { api } from '../api.ts'; import { pickDirectory, showInFolder } from '../desktop.ts'; import { msg, type Settings, type SyncEvent, type SyncSummary } from '../model.ts'; const FORMATS = [ { id: 'markdown', label: 'Markdown', hint: 'typed text, one .md per document' }, { id: 'svg', label: 'SVG', hint: 'one drawing per page' }, { id: 'pdf', label: 'PDF', hint: 'handwriting as a PDF' }, ]; export class NbSync extends LitElement { static properties = { settings: { attribute: false }, dir: { state: true }, formats: { state: true }, raw: { state: true }, trash: { state: true }, prune: { state: true }, dryRun: { state: true }, running: { state: true }, log: { state: true }, summary: { state: true }, error: { state: true } }; declare settings: Settings | null; declare dir: string; declare formats: string[]; declare raw: boolean; declare trash: boolean; declare prune: boolean; declare dryRun: boolean; declare running: boolean; declare log: string[]; declare summary: SyncSummary | null; declare error: string; constructor() { super(); this.settings = null; this.dir = ''; this.formats = ['markdown']; this.raw = true; this.trash = false; this.prune = true; this.dryRun = false; this.running = false; this.log = []; this.summary = null; this.error = ''; } protected override createRenderRoot() { return this; } override willUpdate(changed: Map) { if (changed.has('settings') && this.settings) { const s = this.settings; if (s.syncDir) this.dir = s.syncDir; if (s.syncFormats) this.formats = s.syncFormats; if (s.syncRaw !== undefined) this.raw = s.syncRaw; if (s.syncTrash !== undefined) this.trash = s.syncTrash; if (s.syncPrune !== undefined) this.prune = s.syncPrune; } } open() { this.summary = null; this.error = ''; this.log = []; (this.querySelector('dialog') as HTMLDialogElement | null)?.showModal(); } close() { if (this.running) return; (this.querySelector('dialog') as HTMLDialogElement | null)?.close(); } private async choose() { const picked = await pickDirectory('Mirror the cloud into…', this.dir || undefined); if (picked) this.dir = picked; } private toggleFormat(id: string, on: boolean) { this.formats = on ? [...new Set([...this.formats, id])] : this.formats.filter((f) => f !== id); } private describe(e: SyncEvent): string { switch (e.type) { case 'download': return `↓ ${e.doc?.name ?? e.path}`; case 'export': return `⇢ ${e.doc?.name ?? e.path} → ${e.format}`; case 'move': return `→ ${e.from} → ${e.to}`; case 'prune': return `✕ ${e.name ?? e.path}`; case 'skip': return `· ${e.doc?.name ?? e.path}: ${e.reason ?? 'skipped'}`; default: return JSON.stringify(e); } } private async run() { if (!this.dir) { this.error = 'Choose a folder first.'; return; } this.running = true; this.error = ''; this.summary = null; this.log = []; try { this.summary = await api.sync({ dir: this.dir, formats: this.formats, raw: this.raw, trash: this.trash, prune: this.prune, dryRun: this.dryRun }, (e) => { this.log = [...this.log.slice(-400), this.describe(e)]; }); this.dispatchEvent(new CustomEvent('synced', { detail: this.summary, bubbles: true, composed: true })); } catch (err) { this.error = msg(err); } finally { this.running = false; } } override render() { const s = this.summary; return html` { if (this.running) e.preventDefault(); }}>
e.preventDefault()}>

Mirror to a folder

A one-way copy of the cloud: every document as its raw files, plus the conversions you pick. Only what changed is fetched again; renames become moves; what left the cloud is removed here too.

Also convert to ${FORMATS.map((f) => html``)}
Options
${this.log.length ? html`
${this.log.join('\n')}
` : nothing} ${s ? html`

Done: ${s.downloaded} downloaded, ${s.exported} converted, ${s.moved} moved, ${s.pruned} removed, ${s.unchanged} unchanged${s.skipped ? `, ${s.skipped} with nothing to convert` : ''}. ${this.dir ? html`` : nothing}

` : nothing} ${this.error ? html`` : nothing}
`; } } customElements.define('nb-sync', NbSync);