Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Fix: removing a file's LAST language pack left it in the file

nyblnet18868541a285
diff --git a/docs/i18n-packs.md b/docs/i18n-packs.md
index 2b80ab1d..0a8aeb3e 100644
--- a/docs/i18n-packs.md
+++ b/docs/i18n-packs.md
@@ -162,8 +162,8 @@ id="bento-lang-<lang>">` in `<head>`, holding the pack JSON with `<` escaped as
`\u003c` — exactly the treatment `#bento-doc` gets. The kernel side is
deliberately ignorant: `registerShellBlocks` / `readShellBlocks`
(`kernel/src/save.ts`) carry *typed blocks*, and know nothing about languages.
-The app registers `shellBlocksForPacks()` once, at boot, and every serialize
-re-declares the whole set.
+The app registers `shellBlocksForPacks()` once, at boot — together with the
+block types it owns — and every serialize re-declares the whole set.
## Where a pack lives
@@ -202,16 +202,11 @@ File System Access API, "write" means **silently downloading a second copy of
the user's deck**. Handing someone an unexpected `deck (1).bento.html` because
they asked for Korean is a worse surprise than asking them to save.
-Removal is meant to be symmetric and to need no deletion path: `serializeBody`
-drops every block of a managed type and rewrites the current set, so "remove
-from this file" is just *stop listing it*.
-
-> **Known gap (kernel, unfixed at time of writing).** `serializeBody` derives
-> the managed types from the blocks it is *about* to write, so when the set is
-> EMPTY it removes nothing. Removing the file's **last** pack therefore leaves
-> the old block in the saved file — every other removal works. The fix is for
-> the provider to declare its managed types rather than have them inferred.
-> Kernel zone, so it wants its own small PR (`docs/PARALLEL-WORK.md` §1).
+Removal is symmetric and needs no deletion path: `serializeBody` drops every
+block of a managed type and rewrites the current set, so "remove from this
+file" is just *stop listing it*. The managed types are the ones the provider
+DECLARED at registration, not the ones it is about to write — so an empty set
+still clears, which is exactly what removing the file's last pack looks like.
### Staying current
diff --git a/kernel/src/save.ts b/kernel/src/save.ts
index 4bc8a063..e1f53fe7 100644
--- a/kernel/src/save.ts
+++ b/kernel/src/save.ts
@@ -46,10 +46,21 @@ export interface ShellBlock {
attrs?: Record<string, string>
}
let shellBlocks: () => ShellBlock[] = () => []
+let managedTypes: string[] = []
-/** Register the provider consulted on every serialize. Call once, at boot. */
-export function registerShellBlocks(fn: () => ShellBlock[]): void {
+/**
+ * Register the provider consulted on every serialize, and the block types it
+ * OWNS. Call once, at boot.
+ *
+ * The types are declared rather than inferred from what the provider returns,
+ * because the empty list is meaningful: "this file should carry no language
+ * pack" has to clear the blocks the file arrived with, and a set derived from
+ * the blocks about to be written would be empty exactly then — leaving the
+ * last removed pack in the file (it would come back on the next open).
+ */
+export function registerShellBlocks(fn: () => ShellBlock[], types: string[]): void {
shellBlocks = fn
+ managedTypes = types
}
/** Every extra block currently in THIS document (as loaded from disk). */
@@ -67,9 +78,12 @@ function serializeBody(shell: Document, body: string, title: string): string {
// Re-declare the app's extra blocks: drop every one of a managed type, then
// write the current set back. Removing a language from the file is therefore
- // just "stop listing it" — no deletion path to get wrong.
+ // just "stop listing it" — no deletion path to get wrong. The clear-set is
+ // the DECLARED types, never the types about to be written: an empty write
+ // set still has to clear (that is what removing the file's last pack looks
+ // like).
const wanted = shellBlocks()
- for (const type of new Set(wanted.map((b) => b.type))) {
+ for (const type of new Set([...managedTypes, ...wanted.map((b) => b.type)])) {
for (const stale of Array.from(clone.querySelectorAll(`script[type="${type}"]`))) stale.remove()
}
for (const b of wanted) {
diff --git a/slides/src/i18n.ts b/slides/src/i18n.ts
index ccfe3c74..42ab6088 100644
--- a/slides/src/i18n.ts
+++ b/slides/src/i18n.ts
@@ -18,7 +18,7 @@
// node scripts/build-i18n.mjs
//
import { PACKED, PACKED_LOCALES } from './i18n/packed'
-import { readPacksFromShell, refreshPacksForVersion, shellBlocksForPacks } from './packs'
+import { PACK_BLOCK_TYPE, readPacksFromShell, refreshPacksForVersion, shellBlocksForPacks } from './packs'
import { registerShellBlocks } from '../../kernel/src/save.ts'
import { registerUpdatePrepare } from '../../kernel/src/update.ts'
import { registerI18n } from '../../kernel/src/i18n.ts'
@@ -64,8 +64,10 @@ registerI18n({
readPacksFromShell()
// Every save re-declares the file's packs, so staging one is enough to make
-// it travel and dropping it is enough to remove it.
-registerShellBlocks(shellBlocksForPacks)
+// it travel and dropping it is enough to remove it. The type is declared here
+// so the kernel clears pack blocks even when the list is empty — removing the
+// file's last pack must actually remove it.
+registerShellBlocks(shellBlocksForPacks, [PACK_BLOCK_TYPE])
// A self-update fetches a NEW shell and re-splices this document into it. The
// block provider above already carries the file's packs across; this also
diff --git a/slides/src/packs.ts b/slides/src/packs.ts
index 519ab6fc..4804c6dc 100644
--- a/slides/src/packs.ts
+++ b/slides/src/packs.ts
@@ -43,7 +43,7 @@ export interface PackListing {
export type PackError = 'offline' | 'bad-pack' | 'wrong-app'
/** The block type carrying a pack inside a saved shell. */
-const BLOCK_TYPE = 'application/bento+lang'
+export const PACK_BLOCK_TYPE = 'application/bento+lang'
const blockId = (lang: string) => `bento-lang-${lang}`
/** Packs destined for the file: those already in it, plus any staged since. */
@@ -58,7 +58,7 @@ const pending = new Set<string>()
*/
export function readPacksFromShell(): number {
let n = 0
- for (const { body } of readShellBlocks(BLOCK_TYPE)) {
+ for (const { body } of readShellBlocks(PACK_BLOCK_TYPE)) {
try {
const pack = JSON.parse(body) as LanguagePack
if (pack?.lang && pack.strings && addPack(pack, 'slides')) {
@@ -144,7 +144,7 @@ export function markFileSaved(): void {
export function shellBlocksForPacks(): ShellBlock[] {
return [...inFile.values()].map((p) => ({
id: blockId(p.lang),
- type: BLOCK_TYPE,
+ type: PACK_BLOCK_TYPE,
body: JSON.stringify(p),
attrs: { 'data-lang': p.lang },
}))