Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

save: a pristine New document adopts its launch handle (saves in place) (#749)

nyblnetb406030e9b9c
diff --git a/kernel/src/save.ts b/kernel/src/save.ts
index 84458292..f035f6b3 100644
--- a/kernel/src/save.ts
+++ b/kernel/src/save.ts
@@ -57,7 +57,12 @@ export function readEmbeddedDoc(): string | null {
* lookalike — which is linear, where a regex over an unterminated `<script` is
* quadratic. CRLF is normalised so a file saved with CRLF line endings still
* compares equal. The body is <-escaped at write time so it can never itself
- * contain the close tag. null if there is no block. */
+ * contain the close tag. Returns '' for a present-but-EMPTY block (a pristine
+ * New shell), and null only when there is NO readable block at all — the two are
+ * different documents, and the launch adopter (consumeLaunchQueue) must tell a
+ * pristine shell from a non-Bento file. (readEmbeddedDoc deliberately still
+ * collapses both to null — dash's boot dispatcher depends on that; this is the
+ * string-level extractor, where '' vs null is meaningful.) */
const DOC_BLOCK_ID_ATTR = ` id="${DATA_BLOCK_ID}"`
export function embeddedDocBlock(html: string): string | null {
const at = html.indexOf(DOC_BLOCK_ID_ATTR)
@@ -66,7 +71,7 @@ export function embeddedDocBlock(html: string): string | null {
if (start < 0) return null
const end = html.indexOf(SCRIPT_CLOSE, start + 1)
if (end < 0) return null
- return html.slice(start + 1, end).replace(/\r\n/g, '\n').trim() || null
+ return html.slice(start + 1, end).replace(/\r\n/g, '\n').trim()
}
/**
@@ -895,15 +900,22 @@ export function consumeLaunchQueue(): void {
}
const lq = w.launchQueue
if (!lq || typeof lq.setConsumer !== 'function') return
- // The open document's #bento-doc, captured now (boot, before any edit), CRLF
- // normalised to match embeddedDocBlock. The handle we adopt must still hold THIS
- // document: a host bug that vends the wrong file would otherwise have the first
- // autosave silently overwrite it.
- const booted = readEmbeddedDoc()?.replace(/\r\n/g, '\n') ?? null
+ // The open document's #bento-doc, captured now (boot, before any edit), read
+ // from the element directly and normalised EXACTLY as embeddedDocBlock does, so
+ // the two compare equal. This distinguishes a present-but-EMPTY block (a pristine
+ // New document → '') from a MISSING one (not a Bento document → null), which
+ // readEmbeddedDoc collapses: a New document must still adopt the host's handle
+ // (B6), or its first ⌘S becomes a Save-As picker that writes a second file. The
+ // handle we adopt must still hold THIS document: a host bug that vends the wrong
+ // file would otherwise have the first autosave silently overwrite it.
+ const blockEl = document.getElementById(DATA_BLOCK_ID)
+ const booted = blockEl ? (blockEl.textContent ?? '').replace(/\r\n/g, '\n').trim() : null
try {
lq.setConsumer(async (params) => {
const handle = params?.files?.[0]
- if (!handle || !booted || typeof handle.getFile !== 'function') return
+ // booted === null means no block at all (not a Bento document) — nothing to
+ // verify against; '' (a pristine shell) is a real, adoptable document.
+ if (!handle || booted === null || typeof handle.getFile !== 'function') return
try {
const text = await (await handle.getFile()).text()
// Adopt only if the file is still this document AND nothing claimed a handle
diff --git a/scripts/test-file-handle.ts b/scripts/test-file-handle.ts
index 4567e949..0035a4f7 100644
--- a/scripts/test-file-handle.ts
+++ b/scripts/test-file-handle.ts
@@ -270,6 +270,8 @@ console.log('\nembeddedDocBlock reads the #bento-doc body from file bytes')
const lookalike = `<script data-id="bento-doc">\nDECOY\n${close}` + file(body)
ok(embeddedDocBlock(lookalike) === body, 'a data-id="bento-doc" lookalike is skipped; the real id block is read')
ok(embeddedDocBlock('<html>no block here</html>') === null, 'no block → null')
+ ok(embeddedDocBlock(file('')) === '', 'a present-but-EMPTY block → "" (a pristine New shell, distinct from a non-Bento file that has no block)')
+ ok(embeddedDocBlock(file(' \n ')) === '', 'a whitespace-only block is empty too → ""')
}
// --- launchQueue: a launched document holds a writable handle at boot ---------
@@ -391,6 +393,50 @@ console.log('\nlaunchQueue adopts the open document — and only it')
delete (globalThis as Record<string, unknown>).document
}
+// --- B6: a pristine New document adopts its handle ----------------------------
+// A document made with New in bento/home is a pristine shell: #bento-doc is
+// PRESENT but empty. readEmbeddedDoc collapses that to null (like a missing
+// block), so consumeLaunchQueue used to discard the host's hand-over and the
+// first Cmd-S opened a Save-As picker that wrote a SECOND file, leaving the
+// created one empty — the 1.2.9 B6 regression, on every New document in bento/home
+// (Android and iOS via the shared bridge). The adopter now reads the element
+// directly: '' (pristine) adopts; null (no block at all) does not.
+console.log('\nlaunchQueue: a pristine New document adopts its handle (B6)')
+{
+ const pristineFile =
+ `<!doctype html><html><head><script type="application/json" id="bento-doc">\n\n` +
+ `</scr` + `ipt></head><body></body></html>`
+ const handle = (fileText: string | null) => ({
+ kind: 'file', name: 'New-1.bento.html',
+ getFile: async () => { if (fileText === null) throw new Error('unreadable'); return { text: async () => fileText } },
+ async createWritable() { return { async write() {}, async close() {} } },
+ })
+ let pending: unknown
+ const vend = async (files: unknown[], liveBlock: { textContent: string } | null) => {
+ adoptFileHandle(null as never) // reset so this adoption is observable
+ ;(globalThis as Record<string, unknown>).document = {
+ getElementById: (id: string) => (id === 'bento-doc' ? liveBlock : null),
+ }
+ ;(globalThis as Record<string, unknown>).launchQueue =
+ { setConsumer(fn: (p: unknown) => void) { pending = fn({ files }) } }
+ pending = undefined
+ consumeLaunchQueue()
+ await pending
+ }
+
+ // pristine live page (empty block) + a pristine host file (empty block) → adopt
+ await vend([handle(pristineFile)], { textContent: '\n\n' })
+ ok(hasFileHandle(), 'a pristine New shell adopts the host handle — first Cmd-S saves in place, no picker')
+ ok(currentFileName() === 'New-1.bento.html', `and it is the launched New document (${currentFileName()})`)
+
+ // a live page with NO #bento-doc at all (not a Bento document) → booted null → never adopt
+ await vend([handle(pristineFile)], null)
+ ok(!hasFileHandle(), 'a page with no document block adopts nothing (booted is null, not "")')
+
+ adoptFileHandle(null as never)
+ delete (globalThis as Record<string, unknown>).document
+}
+
// Source pin for the trigger the behavioural row stands in for: the editor
// autosave calls writeUpdatedFile only under hasFileHandle() — which the adopted
// launch handle satisfies, so the first autosave takes the file path, not the