Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

v0.1.3: in-place self-update — rewrite the open file, reload into new version

nyblnet6aa31bfe1b9a
diff --git a/slides/package.json b/slides/package.json
index f7c3fc0f..5b0db1cb 100644
--- a/slides/package.json
+++ b/slides/package.json
@@ -1,7 +1,7 @@
{
"name": "bento-slides",
"private": true,
- "version": "0.1.2",
+ "version": "0.1.3",
"type": "module",
"scripts": {
"dev": "vite",
diff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.ts
index 58828969..f17b72dd 100644
--- a/slides/src/editor/editor.ts
+++ b/slides/src/editor/editor.ts
@@ -8,7 +8,7 @@ import {
instantiateLayout, layoutElementIds, uid,
type ShapeKind, type Slide, type SlideElement,
} from '../model'
-import { APP_VERSION, applyUpdate, checkForUpdates } from '../update'
+import { APP_VERSION, applyUpdate, applyUpdateInPlace, canUpdateInPlace, checkForUpdates } from '../update'
import { CHART_PRESETS } from '../charts'
import { renderSlide, renderThumbnail } from '../render'
import { SlideCanvas } from './canvas'
@@ -747,21 +747,58 @@ export class Editor {
notes.textContent = release.notes
status.appendChild(notes)
}
+ const fail = (err: any) => { status.textContent = `Update failed: ${err?.message ?? err}` }
+ const done = () => {
+ status.textContent = ''
+ const ok = div('ed-about-new')
+ ok.textContent = `Updated to v${release.version} on disk.`
+ status.appendChild(ok)
+ const note = div('ed-about-notes')
+ note.textContent = canUpdateInPlace()
+ ? `This window is still running v${APP_VERSION} — reload to finish. A v${APP_VERSION} backup was downloaded.`
+ : `This window is still running v${APP_VERSION}. If you overwrote the file that's open here, reload; otherwise open the file you saved.`
+ status.appendChild(note)
+ const reloadB = document.createElement('button')
+ reloadB.className = 'ed-btn ed-btn-primary'
+ reloadB.textContent = 'Reload into new version'
+ reloadB.addEventListener('click', () => {
+ this.store.setDirty(false) // disk already holds this exact document
+ location.reload()
+ })
+ status.appendChild(reloadB)
+ }
+
+ const inPlaceB = document.createElement('button')
+ inPlaceB.className = 'ed-btn ed-btn-primary'
+ inPlaceB.textContent = canUpdateInPlace() ? 'Update this file' : 'Update this file…'
+ inPlaceB.title = canUpdateInPlace()
+ ? 'Downloads a backup of the current version, then rewrites this file on disk as the new version — document untouched.'
+ : 'Verifies and builds the new version with this document inside, then asks where to save it — pick the file you have open to update it.'
+ inPlaceB.addEventListener('click', async () => {
+ inPlaceB.disabled = true
+ inPlaceB.textContent = 'Verifying…'
+ try {
+ const written = await applyUpdateInPlace(release, this.store.doc)
+ if (written) done()
+ else { inPlaceB.disabled = false; inPlaceB.textContent = 'Update this file…' }
+ } catch (err: any) { fail(err) }
+ })
+ status.appendChild(inPlaceB)
+
const getB = document.createElement('button')
- getB.className = 'ed-btn ed-btn-primary'
+ getB.className = 'ed-btn'
getB.textContent = 'Download updated copy'
- getB.title = 'Verifies the new app’s signature, puts this document inside it, and downloads the result. The file you have now is not touched.'
+ getB.title = 'Downloads the new version with this document inside. The file you have now is not touched.'
getB.addEventListener('click', async () => {
getB.disabled = true
getB.textContent = 'Verifying…'
try {
await applyUpdate(release, this.store.doc)
getB.textContent = 'Downloaded ✓'
- this.toast('Updated copy downloaded — replace your old file with it')
- } catch (err: any) {
- getB.remove()
- status.textContent = `Update failed: ${err?.message ?? err}`
- }
+ const note = div('ed-about-notes')
+ note.textContent = `This window keeps running v${APP_VERSION} until you open the downloaded file.`
+ status.appendChild(note)
+ } catch (err: any) { fail(err) }
})
status.appendChild(getB)
}
diff --git a/slides/src/save.ts b/slides/src/save.ts
index b27aa7d4..2a1cd3d3 100644
--- a/slides/src/save.ts
+++ b/slides/src/save.ts
@@ -128,3 +128,31 @@ export async function saveFile(doc: BentoDoc, forcePicker = false): Promise<Save
}
export const currentFileName = () => fileHandle?.name ?? null
+
+// --- self-update writing ----------------------------------------------------
+
+/** Whether we hold a writable handle to the file (in-place update possible). */
+export const hasFileHandle = () => fileHandle !== null
+
+/** Overwrite the held file with arbitrary html (the freshly updated shell). */
+export async function writeUpdatedFile(html: string): Promise<void> {
+ if (!fileHandle) throw new Error('no file handle')
+ await writeHandle(fileHandle, html)
+}
+
+/**
+ * Save updated html via a picker (user points it at the file they have open,
+ * or anywhere else). Returns false if cancelled. Keeps the picked handle so
+ * subsequent ⌘S saves go to the same place.
+ */
+export async function writeUpdatedFileAs(html: string, doc: BentoDoc): Promise<boolean> {
+ if (!hasFsAccess()) {
+ downloadFile(html, suggestedFileName(doc))
+ return true
+ }
+ const handle = await pickHandle(doc)
+ if (!handle) return false
+ fileHandle = handle
+ await writeHandle(handle, html)
+ return true
+}
diff --git a/slides/src/update.ts b/slides/src/update.ts
index a5943880..6ece2452 100644
--- a/slides/src/update.ts
+++ b/slides/src/update.ts
@@ -17,7 +17,10 @@
// the file on disk, so the original is its own rollback.
import type { BentoDoc } from './model'
-import { serializeWith, suggestedFileName, downloadFile } from './save'
+import {
+ serializeWith, serializeFile, suggestedFileName, downloadFile,
+ hasFileHandle, writeUpdatedFile, writeUpdatedFileAs,
+} from './save'
declare const __APP_VERSION__: string
@@ -141,3 +144,23 @@ export async function buildUpdatedFile(release: ReleaseInfo, doc: BentoDoc): Pro
export async function applyUpdate(release: ReleaseInfo, doc: BentoDoc): Promise<void> {
downloadFile(await buildUpdatedFile(release, doc), suggestedFileName(doc))
}
+
+/** Can we rewrite the open file directly (a FS Access handle is held)? */
+export const canUpdateInPlace = hasFileHandle
+
+/**
+ * Update the file on disk. With a held handle: download a backup of the
+ * current version first, then overwrite in place — a reload then boots the
+ * new app with this document. Without one: a save picker lets the user point
+ * at the file they have open (or anywhere). Returns false if cancelled.
+ */
+export async function applyUpdateInPlace(release: ReleaseInfo, doc: BentoDoc): Promise<boolean> {
+ const html = await buildUpdatedFile(release, doc)
+ if (hasFileHandle()) {
+ const base = suggestedFileName(doc).replace(/\.bento\.html$/, '')
+ downloadFile(serializeFile(doc), `${base}.v${APP_VERSION}-backup.bento.html`)
+ await writeUpdatedFile(html)
+ return true
+ }
+ return writeUpdatedFileAs(html, doc)
+}