From 1b30572943668c6904110f44b21c9ca8fc4692b8 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 01:02:06 +0000 Subject: [PATCH 1/9] Keep a pasted link over a waiting save, a size the drawer could not take, and the set-aside record in step A link pasted over a page as an edit's save came due was written over: a page still drawing came to the waiting save before the paste's hashchange, the save wrote the page's own design into the address, and the hashchange then reloaded that. The save now leaves an address the page did not write alone (ownHash), so the link loads, and the layout it replaces, with the edit, is set aside. With the drawer's saves refused, a hand-over carried a size the drawer did not have, and the page at the other end caught up with the drawer's older one. A drawer with both marks as the note has them has not moved on since the page that handed over, so it is not caught up with. And with the storage full, a layout set aside whose link record would not fit kept the record of the layout set aside before. That record now goes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 39 ++++++++++++++++++---- index.html | 39 ++++++++++++++++++---- src/bins/ui.js | 30 ++++++++++++++--- src/shared-ui/drawers.js | 9 +++++- src/ui.js | 30 ++++++++++++++--- sw.js | 2 +- test/ui/drawers.spec.js | 61 +++++++++++++++++++++++++++++++++++ test/ui/hostile-links.spec.js | 25 ++++++++++++++ 8 files changed, 211 insertions(+), 24 deletions(-) diff --git a/bins/index.html b/bins/index.html index ecd73de..d6fbe16 100644 --- a/bins/index.html +++ b/bins/index.html @@ -6704,7 +6704,14 @@

Your drawers

link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - if (!(noted && next.caughtUp) && catchUp(arrivedWith, d)) return; + /* Not a hand-over from a page whose saves into the drawer were refused, the + storage full: the drawer has both halves as that page last saved them (the + note's marks), so a size that differs is one changed there since, which the + drawer could not take. Caught up, the drawer's older size came back over it, + as though another tab had moved the drawer on. */ + const asLeft = noted && typeof came[otherTool] === 'string' && + d.marks[otherTool] === came[otherTool] && d.marks[o.tool] === came[o.tool]; + if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the drawer, and the page came back unsaved. The half goes in with the mark, as @@ -16469,11 +16476,24 @@

Your drawers

drawer the address is at: marked before it, a reload took the save before for its own, and after another tab put the drawer back to that one, the reload wrote this page's later change back over it. */ - try { history.replaceState(drawers.stamp(h, linkedNow && bootDesc !== null), '', '#' + h); } - catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not - worth an exception that stops the rest of the page working */ } + if (!pastedOver()) { + try { history.replaceState(drawers.stamp(h, linkedNow && bootDesc !== null), '', '#' + h); } + catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not + worth an exception that stops the rest of the page working */ } + ownHash = location.hash; + } } } +/* The address as this page last left it: as it arrived, then as each save wrote it. One + that differs, with a layout in it, is a link gone to over this page, pasted or picked + from the bookmarks, whose hashchange (above) has not run yet: a page busy drawing comes + to a save that was waiting first. That save is still made, so the layout the link + replaces holds the change and is set aside with it, but the address is the link's. + Written back over, the hashchange found this page's design there and reloaded that, + and the link was gone. */ +let ownHash = null; +const pastedOver = () => ownHash !== null && location.hash !== ownHash && + isLayoutHash((location.hash || '').replace(/^#/, '')); /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. @@ -16878,14 +16898,20 @@

Your drawers

layout was set aside and offered a Put back that brought nothing back. */ let keptAside = false; if (aside) { + const asideLinked = linkKeys(saved, linked).length ? linked : ''; writeKey(PREV_KEY, saved); keptAside = readKey(PREV_KEY) === saved; - if (keptAside) writeKey(PREV_LINKED_KEY, linkKeys(saved, linked).length ? linked : ''); + if (keptAside) writeKey(PREV_LINKED_KEY, asideLinked); + /* With the record of whether it is a link's refused, the one left from the layout set + aside before would answer for it. That goes, and put back, this one is yours: still + better than not setting it aside (unkept), which keeps it only until the first + change made on the link. */ + if (keptAside && readKey(PREV_LINKED_KEY) !== asideLinked) writeKey(PREV_LINKED_KEY, ''); /* Not kept, it is not written over either (unkept), unless a hand-over of your own brings on anything the other page sets: then the save holds the drawer, bed or the other page's settings from before, and a bare visit took those back to the other page. One still carrying someone's link is that link, and your layout is kept. */ - else unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); + if (!keptAside) unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); } const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || (back && keptAside); @@ -16929,6 +16955,7 @@

Your drawers

} readControls(); hashReady = true; // loadFromHash has had its say; ours may start +ownHash = location.hash; initThree(); initMap(); drawLayerTabs(); diff --git a/index.html b/index.html index e1f5c16..f935279 100644 --- a/index.html +++ b/index.html @@ -6505,7 +6505,14 @@

Your drawers

link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - if (!(noted && next.caughtUp) && catchUp(arrivedWith, d)) return; + /* Not a hand-over from a page whose saves into the drawer were refused, the + storage full: the drawer has both halves as that page last saved them (the + note's marks), so a size that differs is one changed there since, which the + drawer could not take. Caught up, the drawer's older size came back over it, + as though another tab had moved the drawer on. */ + const asLeft = noted && typeof came[otherTool] === 'string' && + d.marks[otherTool] === came[otherTool] && d.marks[o.tool] === came[o.tool]; + if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the drawer, and the page came back unsaved. The half goes in with the mark, as @@ -8888,11 +8895,24 @@

Your drawers

drawer the address is at: marked before it, a reload took the save before for its own, and after another tab put the drawer back to that one, the reload wrote this page's later change back over it. */ - try { history.replaceState(drawers.stamp(h, linkedNow && bootDesc !== null), '', '#' + h); } - catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not - worth an exception that stops the rest of the page working */ } + if (!pastedOver()) { + try { history.replaceState(drawers.stamp(h, linkedNow && bootDesc !== null), '', '#' + h); } + catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not + worth an exception that stops the rest of the page working */ } + ownHash = location.hash; + } } } +/* The address as this page last left it: as it arrived, then as each save wrote it. One + that differs, with a layout in it, is a link gone to over this page, pasted or picked + from the bookmarks, whose hashchange (above) has not run yet: a page busy drawing comes + to a save that was waiting first. That save is still made, so the layout the link + replaces holds the change and is set aside with it, but the address is the link's. + Written back over, the hashchange found this page's design there and reloaded that, + and the link was gone. */ +let ownHash = null; +const pastedOver = () => ownHash !== null && location.hash !== ownHash && + isLayoutHash((location.hash || '').replace(/^#/, '')); /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. @@ -9327,14 +9347,20 @@

Your drawers

layout was set aside and offered a Put back that brought nothing back. */ let keptAside = false; if (aside) { + const asideLinked = linkKeys(saved, linked).length ? linked : ''; writeKey(PREV_KEY, saved); keptAside = readKey(PREV_KEY) === saved; - if (keptAside) writeKey(PREV_LINKED_KEY, linkKeys(saved, linked).length ? linked : ''); + if (keptAside) writeKey(PREV_LINKED_KEY, asideLinked); + /* With the record of whether it is a link's refused, the one left from the layout set + aside before would answer for it. That goes, and put back, this one is yours: still + better than not setting it aside (unkept), which keeps it only until the first + change made on the link. */ + if (keptAside && readKey(PREV_LINKED_KEY) !== asideLinked) writeKey(PREV_LINKED_KEY, ''); /* Not kept, it is not written over either (unkept), unless a hand-over of your own brings on anything the other page sets: then the save holds the drawer, bed or the other page's settings from before, and a bare visit took those back to the other page. One still carrying someone's link is that link, and your layout is kept. */ - else unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); + if (!keptAside) unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); } const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || (back && keptAside); @@ -9365,6 +9391,7 @@

Your drawers

} } hashReady = true; // loadFromHash has had its say; ours may start +ownHash = location.hash; recomputeLayout(); autoFrame(); bootDesc = encodeDesc(descriptor()); diff --git a/src/bins/ui.js b/src/bins/ui.js index dccaf3a..0b13be2 100644 --- a/src/bins/ui.js +++ b/src/bins/ui.js @@ -5214,11 +5214,24 @@ function saveNow() { drawer the address is at: marked before it, a reload took the save before for its own, and after another tab put the drawer back to that one, the reload wrote this page's later change back over it. */ - try { history.replaceState(drawers.stamp(h, linkedNow && bootDesc !== null), '', '#' + h); } - catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not - worth an exception that stops the rest of the page working */ } + if (!pastedOver()) { + try { history.replaceState(drawers.stamp(h, linkedNow && bootDesc !== null), '', '#' + h); } + catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not + worth an exception that stops the rest of the page working */ } + ownHash = location.hash; + } } } +/* The address as this page last left it: as it arrived, then as each save wrote it. One + that differs, with a layout in it, is a link gone to over this page, pasted or picked + from the bookmarks, whose hashchange (above) has not run yet: a page busy drawing comes + to a save that was waiting first. That save is still made, so the layout the link + replaces holds the change and is set aside with it, but the address is the link's. + Written back over, the hashchange found this page's design there and reloaded that, + and the link was gone. */ +let ownHash = null; +const pastedOver = () => ownHash !== null && location.hash !== ownHash && + isLayoutHash((location.hash || '').replace(/^#/, '')); /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. @@ -5623,14 +5636,20 @@ let arrivedWith = ''; // the design string this page was opened with layout was set aside and offered a Put back that brought nothing back. */ let keptAside = false; if (aside) { + const asideLinked = linkKeys(saved, linked).length ? linked : ''; writeKey(PREV_KEY, saved); keptAside = readKey(PREV_KEY) === saved; - if (keptAside) writeKey(PREV_LINKED_KEY, linkKeys(saved, linked).length ? linked : ''); + if (keptAside) writeKey(PREV_LINKED_KEY, asideLinked); + /* With the record of whether it is a link's refused, the one left from the layout set + aside before would answer for it. That goes, and put back, this one is yours: still + better than not setting it aside (unkept), which keeps it only until the first + change made on the link. */ + if (keptAside && readKey(PREV_LINKED_KEY) !== asideLinked) writeKey(PREV_LINKED_KEY, ''); /* Not kept, it is not written over either (unkept), unless a hand-over of your own brings on anything the other page sets: then the save holds the drawer, bed or the other page's settings from before, and a bare visit took those back to the other page. One still carrying someone's link is that link, and your layout is kept. */ - else unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); + if (!keptAside) unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); } const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || (back && keptAside); @@ -5674,6 +5693,7 @@ if (pendingNotes) { } readControls(); hashReady = true; // loadFromHash has had its say; ours may start +ownHash = location.hash; initThree(); initMap(); drawLayerTabs(); diff --git a/src/shared-ui/drawers.js b/src/shared-ui/drawers.js index 4beb04e..f2261a8 100644 --- a/src/shared-ui/drawers.js +++ b/src/shared-ui/drawers.js @@ -983,7 +983,14 @@ const DRAWERS = (function () { link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - if (!(noted && next.caughtUp) && catchUp(arrivedWith, d)) return; + /* Not a hand-over from a page whose saves into the drawer were refused, the + storage full: the drawer has both halves as that page last saved them (the + note's marks), so a size that differs is one changed there since, which the + drawer could not take. Caught up, the drawer's older size came back over it, + as though another tab had moved the drawer on. */ + const asLeft = noted && typeof came[otherTool] === 'string' && + d.marks[otherTool] === came[otherTool] && d.marks[o.tool] === came[o.tool]; + if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the drawer, and the page came back unsaved. The half goes in with the mark, as diff --git a/src/ui.js b/src/ui.js index 6d7d7cd..f8eb186 100644 --- a/src/ui.js +++ b/src/ui.js @@ -2293,11 +2293,24 @@ function saveNow() { drawer the address is at: marked before it, a reload took the save before for its own, and after another tab put the drawer back to that one, the reload wrote this page's later change back over it. */ - try { history.replaceState(drawers.stamp(h, linkedNow && bootDesc !== null), '', '#' + h); } - catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not - worth an exception that stops the rest of the page working */ } + if (!pastedOver()) { + try { history.replaceState(drawers.stamp(h, linkedNow && bootDesc !== null), '', '#' + h); } + catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not + worth an exception that stops the rest of the page working */ } + ownHash = location.hash; + } } } +/* The address as this page last left it: as it arrived, then as each save wrote it. One + that differs, with a layout in it, is a link gone to over this page, pasted or picked + from the bookmarks, whose hashchange (above) has not run yet: a page busy drawing comes + to a save that was waiting first. That save is still made, so the layout the link + replaces holds the change and is set aside with it, but the address is the link's. + Written back over, the hashchange found this page's design there and reloaded that, + and the link was gone. */ +let ownHash = null; +const pastedOver = () => ownHash !== null && location.hash !== ownHash && + isLayoutHash((location.hash || '').replace(/^#/, '')); /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. @@ -2732,14 +2745,20 @@ let arrivedWith = ''; // the design string this page was opened with, if it s layout was set aside and offered a Put back that brought nothing back. */ let keptAside = false; if (aside) { + const asideLinked = linkKeys(saved, linked).length ? linked : ''; writeKey(PREV_KEY, saved); keptAside = readKey(PREV_KEY) === saved; - if (keptAside) writeKey(PREV_LINKED_KEY, linkKeys(saved, linked).length ? linked : ''); + if (keptAside) writeKey(PREV_LINKED_KEY, asideLinked); + /* With the record of whether it is a link's refused, the one left from the layout set + aside before would answer for it. That goes, and put back, this one is yours: still + better than not setting it aside (unkept), which keeps it only until the first + change made on the link. */ + if (keptAside && readKey(PREV_LINKED_KEY) !== asideLinked) writeKey(PREV_LINKED_KEY, ''); /* Not kept, it is not written over either (unkept), unless a hand-over of your own brings on anything the other page sets: then the save holds the drawer, bed or the other page's settings from before, and a bare visit took those back to the other page. One still carrying someone's link is that link, and your layout is kept. */ - else unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); + if (!keptAside) unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); } const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || (back && keptAside); @@ -2770,6 +2789,7 @@ let arrivedWith = ''; // the design string this page was opened with, if it s } } hashReady = true; // loadFromHash has had its say; ours may start +ownHash = location.hash; recomputeLayout(); autoFrame(); bootDesc = encodeDesc(descriptor()); diff --git a/sw.js b/sw.js index 882d655..f0d6b56 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "a5c1d5b58da0"; +const VERSION = "1c07c503d14f"; const FILES = [ "./", "bins/", diff --git a/test/ui/drawers.spec.js b/test/ui/drawers.spec.js index d54ccd0..38f9178 100644 --- a/test/ui/drawers.spec.js +++ b/test/ui/drawers.spec.js @@ -429,6 +429,67 @@ test('with storage full, a size changed on the other page outlives the save Bins expect(errors).toEqual([]); }); +/* With the drawer's saves refused, a size changed on one page goes to the other in the + address alone, and the drawer still has the size from before. The page at the other + end caught up with the drawer on the way in, as it does when another tab has moved the + drawer on, and the size went back. Here nothing has moved the drawer on: it has both + halves as the page that handed over last saved them. */ +for (const tool of ['bins', 'plates']) { + test(`with storage full, a size changed on ${tool} is the size on the other page`, async ({ page }) => { + await page.addInitScript(fullStorage); + const errors = await openPlates(page); + await H.setField(page, 'drawerW', '400'); + await saveAs(page, 'Kitchen'); + // there and back first, so each page's own save already has the keys a trip brings + await toBins(page); + await toPlates(page); + if (tool === 'bins') await toBins(page); + await settle(page); + await page.evaluate(() => sessionStorage.setItem('full', '1')); + await H.setField(page, 'drawerW', '410'); // the same length, so the page's own save fits + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + if (tool === 'bins') await toPlates(page); else await toBins(page); + expect(await page.inputValue('#drawerW'), 'on the other page').toBe('410'); + if (tool === 'bins') await toBins(page); else await toPlates(page); + expect(await page.inputValue('#drawerW'), 'and back').toBe('410'); + await expect(page.locator('#setAside')).toBeHidden(); + expect(errors).toEqual([]); + }); +} + +/* A layout set aside with the storage full, where the layout fits and the record of whether + it is a link's does not: the record of the layout set aside before was left beside it, + to say so for the wrong layout. Here that one is planted: a long layout, so the next + fits in its place, with a short record, so the next does not. */ +test('with storage full, a layout set aside is not given the record of the one before', + async ({ page }) => { + await page.addInitScript(fullStorage); + const errors = []; + page.on('pageerror', (e) => errors.push(String(e))); + const KEY = 'drawerforge:bins:v1'; + await page.goto(base + 'bins/#w=444&d=444&bl=0-0-1-1-3'); // someone's link + await binsReady(page); + await H.setField(page, 'gap', '6'); // changed, still their drawer + await settle(page); + const edited = await page.evaluate((k) => localStorage.getItem(k), KEY); + expect(edited).toContain('bgap=6'); + await page.evaluate((k) => { + localStorage.setItem(k + ':prev', 'v=2&' + 'x'.repeat(4000)); + localStorage.setItem(k + ':prev:linked', 'v=2&w=300'); + sessionStorage.setItem('full', '1'); + }, KEY); + + await page.goto('about:blank'); + await page.goto(base + 'bins/#w=555&bl=2-2-1-1-3'); // a second link + await binsReady(page); + await expect(page.locator('#putBack')).toBeVisible(); + const kept = await page.evaluate((k) => [localStorage.getItem(k + ':prev'), + localStorage.getItem(k + ':prev:linked')], KEY); + expect(kept[0], 'the changed link set aside').toBe(edited); + expect(kept[1], 'and not with the record before it').not.toBe('v=2&w=300'); + expect(errors).toEqual([]); + }); + /* The same reload with no save landing. The page is your own drawer handed over, and the reload is that page again. Onto a layout another drawer holds, it used to say someone's link had replaced your layout, set that aside, and count the drawer's settings as the diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index 4be1890..176c920 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -902,3 +902,28 @@ test('a layout that will not write is not saved as something else', async ({ pag await settle(page); expect(await stored(page, BINS)).toBe(save); }); + +/* A link pasted over the page as an edit's save comes due. The paste puts the link in the + address at once and its hashchange, which reloads onto it, comes after; a page still + drawing the edit comes to the waiting save first. That save wrote the page's own design + back over the link, the hashchange then found that and reloaded it, and the link was + gone, with nothing said. Here the save runs in the same task as the paste, as it did. */ +for (const [tool, key] of [['plates', PLATES], ['bins', BINS]]) { + test(`a link pasted over ${tool} as an edit's save comes due is the link`, async ({ page }) => { + const errors = watch(page); + await arrive(page, tool === 'bins' ? binsUrl() : platesUrl()); + await H.setField(page, 'drawerW', '412'); // its save waiting + await Promise.all([page.waitForEvent('load'), page.evaluate(() => { + location.hash = '#w=333&d=444&v=2'; + saveNow(); + })]); + await ready(page); + expect(await page.inputValue('#drawerW')).toBe('333'); + await expect(page.locator('#setAsideMsg')).toHaveText('This link replaced the layout you had here.'); + // the edit was saved, and is what the link set aside + expect(await stored(page, key + ':prev')).toMatch(/(^|&)w=412(&|$)/); + await clickAndLoad(page, '#putBack'); + expect(await page.inputValue('#drawerW')).toBe('412'); + expect(errors).toEqual([]); + }); +} From 14833dc813d50c6e4572d733714e060670d20021 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 01:52:04 +0000 Subject: [PATCH 2/9] Wait for the save before each hop away in the links tests, and follow the guide's own pages From #63's review: the links tests waited a fixed 900 ms before going to another address, which drops a save still waiting; they now wait for the save itself (saved), and for a Bins field edit still to be read in. The guide anchor test goes on by the guide's own link to "Drawer size to grid", the polls name what they wait for, the declined-link test checks the link is still recorded before the edit, and markHere's comment no longer blames file://. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 3 +- index.html | 3 +- src/shared-ui/drawers.js | 3 +- sw.js | 2 +- test/ui/hostile-links.spec.js | 52 ++++++++++++++++++++++------------- test/ui/persist.spec.js | 4 +-- 6 files changed, 42 insertions(+), 25 deletions(-) diff --git a/bins/index.html b/bins/index.html index d6fbe16..4b7a0a5 100644 --- a/bins/index.html +++ b/bins/index.html @@ -6162,7 +6162,8 @@

Your drawers

function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; try { history.replaceState(stamp(h, link, note), ''); } - catch (err) { /* refused on file://: a reload goes by the tab's record instead */ } + catch (err) { /* refused, as a browser may when it is called too often: a reload + goes by the tab's record instead */ } } // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); diff --git a/index.html b/index.html index f935279..27eb1ad 100644 --- a/index.html +++ b/index.html @@ -5963,7 +5963,8 @@

Your drawers

function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; try { history.replaceState(stamp(h, link, note), ''); } - catch (err) { /* refused on file://: a reload goes by the tab's record instead */ } + catch (err) { /* refused, as a browser may when it is called too often: a reload + goes by the tab's record instead */ } } // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); diff --git a/src/shared-ui/drawers.js b/src/shared-ui/drawers.js index f2261a8..cb9cb46 100644 --- a/src/shared-ui/drawers.js +++ b/src/shared-ui/drawers.js @@ -441,7 +441,8 @@ const DRAWERS = (function () { function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; try { history.replaceState(stamp(h, link, note), ''); } - catch (err) { /* refused on file://: a reload goes by the tab's record instead */ } + catch (err) { /* refused, as a browser may when it is called too often: a reload + goes by the tab's record instead */ } } // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); diff --git a/sw.js b/sw.js index f0d6b56..1af56ae 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "1c07c503d14f"; +const VERSION = "7346160ff7ff"; const FILES = [ "./", "bins/", diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index 176c920..57a5666 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -29,6 +29,14 @@ const platesUrl = () => site.base; const binsUrl = () => site.base + 'bins/'; const settle = (page) => page.waitForTimeout(900); // past the 400 ms save debounce +/* Until the page's save has landed: no edit still waiting to be read in (the bins page's + fields are read in a moment after they change, `timer`), and this browser's save is the + design on the page. Going on to another address drops a save still waiting (see + dropSave), and a fixed wait for it was now and then too short for a page still drawing. */ +const saved = (page) => expect.poll(() => page.evaluate(() => + (typeof timer === 'undefined' || timer === null) && + localStorage.getItem(SAVE_KEY) === encodeDesc(descriptor())), { message: 'the page has saved' }) + .toBe(true); const PLATES = 'drawerforge:plates:v1'; const BINS = 'drawerforge:bins:v1'; @@ -127,7 +135,7 @@ test('following a link puts the layout it replaced aside, with a way back', async ({ page }) => { await H.openPlates(page, platesUrl()); await H.setField(page, 'drawerW', '512'); - await settle(page); + await saved(page); await arrive(page, platesUrl() + '#w=333'); expect(await page.inputValue('#drawerW')).toBe('333'); // the link still wins @@ -141,7 +149,7 @@ test('following a link puts the layout it replaced aside, with a way back', test('a save nobody has touched is not offered back', async ({ page }) => { await H.openPlates(page, platesUrl()); - await settle(page); // the defaults are saved + await saved(page); // the defaults are saved await arrive(page, platesUrl() + '#w=333'); await expect(page.locator('#setAside')).toBeHidden(); expect(await stored(page, PLATES + ':prev')).toBeNull(); @@ -205,7 +213,7 @@ test('a saved layout that did not finish loading is still declined after a reloa test('a second link does not push your own layout out of the backup', async ({ page }) => { await H.openPlates(page, platesUrl()); await H.setField(page, 'drawerW', '512'); - await settle(page); + await saved(page); await arrive(page, platesUrl() + '#w=333'); await arrive(page, platesUrl() + '#w=444'); await expect(page.locator('#putBack')).toBeVisible(); @@ -220,13 +228,13 @@ test('a second link does not push your own layout out of the backup', async ({ p test('changing a linked layout takes the offer to put yours back away', async ({ page }) => { await H.openPlates(page, platesUrl()); await H.setField(page, 'drawerW', '512'); - await settle(page); + await saved(page); await arrive(page, platesUrl() + '#w=333'); await expect(page.locator('#setAside')).toBeVisible(); await page.waitForTimeout(600); // the boot's own save is no edit await expect(page.locator('#setAside')).toBeVisible(); await H.setField(page, 'drawerW', '340'); - await settle(page); + await saved(page); await expect(page.locator('#setAside')).toBeHidden(); // and that edited layout is yours now: the next link sets it aside await arrive(page, platesUrl() + '#w=444'); @@ -480,7 +488,7 @@ test('bed, infill and gap survive a trip through baseplates and back', async ({ await H.openBins(page, binsUrl()); const want = [['bedW', '200'], ['bedD', '210'], ['bedH', '220'], ['infill', '35'], ['gap', '6']]; for (const [id, v] of want) await H.setField(page, id, v); - await settle(page); + await saved(page); // each page's own link, as the page means it await arrive(page, await page.evaluate(() => new URL(platesHref(), location.href).href)); @@ -520,7 +528,7 @@ test('following a link puts the bins it replaced aside, with a way back', async await H.openBins(page, binsUrl()); await H.dragCells(page, [0, 0], [1, 1]); await H.dragCells(page, [3, 3], [3, 3]); - await settle(page); + await saved(page); await arrive(page, binsUrl() + '#bl=0-0-1-1-3'); expect(await binsIn(page)).toBe(1); @@ -635,7 +643,7 @@ test("someone's bins link, edited, carried to baseplates still keeps your drawer '&bl=0-0-1-1-3'; await arrive(page, binsUrl() + '#' + theirs); await H.setField(page, 'gap', '6'); // changed, but still their drawer - await settle(page); + await saved(page); await arrive(page, binsUrl()); // and a reload does not make it yours await viaButton(page, '#navPlates', platesUrl()); @@ -674,7 +682,7 @@ for (const [id, value] of [['infill', '20'], ['bedH', '200'], ['drawerD', '300'] test("an edited link put back is still the link's", async ({ page }) => { await theirBinsLink(page); await H.setField(page, 'gap', '6'); - await settle(page); + await saved(page); await arrive(page, binsUrl() + '#bl=2-2-1-1-3'); // sets the edited one aside await clickAndLoad(page, '#putBack'); expect(await page.inputValue('#gap')).toBe('6'); @@ -693,7 +701,7 @@ test("your own layout put back is not the link's, even in the same drawer", await viaButton(page, '#navBins', binsUrl()); await H.dragCells(page, [0, 0], [0, 0]); await H.setField(page, 'drawerW', '400'); - await settle(page); + await saved(page); const mine = await stored(page, BINS); await arrive(page, binsUrl() + '#' + mine.replace(/(^|&)bl=[^&]*/, '$1bl=2-2-1-1-3')); await clickAndLoad(page, '#putBack'); @@ -712,7 +720,7 @@ test("your own layout put back is not the link's, even in the same drawer", test("someone's drawer changed on baseplates comes back to bins as yours", async ({ page }) => { await H.openBins(page, binsUrl()); await H.dragCells(page, [0, 0], [1, 1]); - await settle(page); + await saved(page); const mine = await stored(page, BINS); await arrive(page, binsUrl() + '#w=333&bl=0-0-1-1-3'); await H.setField(page, 'gap', '6'); @@ -740,7 +748,7 @@ test("your own drawer on someone's link is not recorded as theirs", async ({ pag expect(theirs).toContain('bw=220'); await arrive(page, binsUrl()); await H.dragCells(page, [0, 0], [1, 1]); // your own bins - await settle(page); + await saved(page); const mine = await stored(page, BINS); await arrive(page, binsUrl() + '#' + theirs); @@ -752,7 +760,7 @@ test("your own drawer on someone's link is not recorded as theirs", async ({ pag await arrive(page, binsUrl()); await H.setField(page, 'drawerW', '600'); - await settle(page); + await saved(page); await arrive(page, platesUrl()); await viaButton(page, '#navBins', binsUrl()); expect(await page.inputValue('#drawerW')).toBe('500'); @@ -771,7 +779,7 @@ for (const reload of [false, true]) { await H.setField(page, 'drawerD', '400'); await H.setField(page, 'bedW', '220'); await page.selectOption('#connector', 'snap'); - await settle(page); + await saved(page); const mine = await stored(page, PLATES); await arrive(page, binsUrl() + '#w=333&d=333&bw=250&bd=250&bl=0-0-1-1-3'); @@ -798,9 +806,11 @@ test('a page that declined a link goes on from its defaults, not the link', asyn await page.evaluate(([k, v]) => localStorage.setItem(k, v), [BINS + ':loading', save]); await arrive(page, binsUrl()); await expect(page.locator('#setAside')).toContainText(/did not finish loading/); + expect(await stored(page, BINS + ':linked'), 'declined, the link is still recorded').not.toBeNull(); await H.dragCells(page, [0, 0], [0, 0]); // the edit's save, however long a busy page takes to it - await expect.poll(() => stored(page, BINS + ':linked')).toBeNull(); + await expect.poll(() => stored(page, BINS + ':linked'), { message: 'the edit ends the record' }) + .toBeNull(); }); test('the guide passes a layout on without its own anchors in the way', async ({ page }) => { @@ -815,8 +825,12 @@ test('the guide passes a layout on without its own anchors in the way', async ({ const layout = await page.evaluate(() => location.hash); expect(layout).toMatch(/(^#|&)ph=/); - // a guide link with an anchor of its own carries the layout, not "#heights#w=…" - await page.goto(site.base + 'guide/drawer-sizes/' + layout); + // on from the guide to one of its own pages, which carries it too + await Promise.all([page.waitForEvent('load'), page.click('a[href="drawer-sizes/"]')]); + expect(page.url().split('#')[0]).toBe(site.base + 'guide/drawer-sizes/'); + expect(await page.evaluate(() => location.hash)).toBe(layout); + + // and a link there with an anchor of its own carries the layout, not "#heights#w=…" await Promise.all([page.waitForEvent('load'), page.click('a[href="../#heights"]')]); expect(page.url().split('#')[0]).toBe(site.base + 'guide/'); expect(await page.evaluate(() => location.hash)).toBe(layout); @@ -830,7 +844,7 @@ test('looking at a linked layout differently is not changing it', async ({ page await H.openBins(page, binsUrl()); await H.dragCells(page, [0, 0], [1, 1]); await H.dragCells(page, [3, 3], [3, 3]); - await settle(page); + await saved(page); await arrive(page, binsUrl() + '#bl=0-0-1-1-3'); await expect(page.locator('#putBack')).toBeVisible(); @@ -850,7 +864,7 @@ test('a link with your bins in a drawer of another size still offers yours back' async ({ page }) => { await H.openBins(page, binsUrl()); await H.dragCells(page, [0, 0], [1, 1]); - await settle(page); + await saved(page); const save = await stored(page, BINS); await arrive(page, binsUrl() + '#' + save.replace(/(^|&)w=\d+/, '$1w=500')); expect(await page.evaluate(() => state.drawerW)).toBe(500); diff --git a/test/ui/persist.spec.js b/test/ui/persist.spec.js index 071e87b..edd646c 100644 --- a/test/ui/persist.spec.js +++ b/test/ui/persist.spec.js @@ -42,8 +42,8 @@ async function openBins(page) { const settle = (page) => page.waitForTimeout(900); // past the 400 ms debounce /* Until this browser holds the save `re` matches. A page still busy drawing the edit comes to its save late, and a fixed wait was now and then too short for it. */ -const savedAs = (page, key, re) => - expect.poll(() => page.evaluate((k) => localStorage.getItem(k), key)).toMatch(re); +const savedAs = (page, key, re) => expect.poll(() => + page.evaluate((k) => localStorage.getItem(k) || '', key), { message: `${key} is saved` }).toMatch(re); test('the baseplates page comes back the way you left it', async ({ page }) => { await openPlates(page); From b889fc7328ac66c67b147afd84b04fe5ebaa9d34 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 04:50:52 +0000 Subject: [PATCH 3/9] Keep a size the drawer could not take across a reload and Back, and offer a pasted-over edit on Back From #65's review. The page that took a hand-over with the storage full was caught up with the drawer's older size again when reloaded, gone Back to or shown from the back-forward cache: the guard covered hand-overs only. Now any arrival whose marks show the drawer as the page last knew it (unmoved), and the pageshow return, are not caught up. Back from a link pasted as an edit's save came due goes to an address that never had the edit, which is aside: it is now offered there. The O1 test checks the old record is gone, and the pasted-link test makes the edit and the paste in one task. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 28 +++++++++++++++++++--------- index.html | 28 +++++++++++++++++++--------- src/bins/ui.js | 7 ++++++- src/shared-ui/drawers.js | 21 +++++++++++++-------- src/ui.js | 7 ++++++- sw.js | 2 +- test/ui/drawers.spec.js | 15 ++++++++++++++- test/ui/hostile-links.spec.js | 16 +++++++++++++++- 8 files changed, 93 insertions(+), 31 deletions(-) diff --git a/bins/index.html b/bins/index.html index bb25007..72189e6 100644 --- a/bins/index.html +++ b/bins/index.html @@ -6184,6 +6184,12 @@

Your drawers

} // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); + /* Whether nothing has saved into drawer `d` since a page that had this tool's mark + `mine` and the other tool's `their`: the drawer has both halves as that page last + knew them. A size the page has that the drawer does not is then the page's own, + one the drawer could not take with the storage full, not the drawer moved on. */ + const unmoved = (d, mine, their) => typeof their === 'string' && + d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ const movedOn = (d, h, was) => d.marks[o.tool] !== fingerprint(h) && d.marks[o.tool] !== was; @@ -6588,7 +6594,7 @@

Your drawers

if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else catchUp(h, d); + else if (!unmoved(d, ours, theirs)) catchUp(h, d); // nor as this page left it (attach) }); return { @@ -6722,13 +6728,12 @@

Your drawers

link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not a hand-over from a page whose saves into the drawer were refused, the - storage full: the drawer has both halves as that page last saved them (the - note's marks), so a size that differs is one changed there since, which the - drawer could not take. Caught up, the drawer's older size came back over it, - as though another tab had moved the drawer on. */ - const asLeft = noted && typeof came[otherTool] === 'string' && - d.marks[otherTool] === came[otherTool] && d.marks[o.tool] === came[o.tool]; + /* Not when the drawer is as the page this design comes from left it (unmoved, by + the marks it came with): a hand-over from a page whose saves into the drawer + were refused, or this page reloaded or gone Back to after its own were. Caught + up, the drawer's older size came back over the one changed since, as though + another tab had moved the drawer on. */ + const asLeft = unmoved(d, came[o.tool], came[otherTool]); if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the @@ -17698,8 +17703,13 @@

Your drawers

page. One still carrying someone's link is that link, and your layout is kept. */ if (!keptAside) unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); } + /* Back to your own earlier page over a link's layout, untouched, sets nothing aside: what + the link replaced is aside already. That can be later than the page Back brings: a change + whose save came due as the link was pasted is aside, and in no earlier address (see + pastedOver). So a layout aside that is not this one is offered. */ + const later = back && savedLinked ? readKey(PREV_KEY) : ''; const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || - (back && keptAside); + (back && (keptAside || (!!later && !sameDesign(later, src)))); if (stalled) { showSetAside('This layout did not finish loading last time, so the page has started ' + 'from its defaults rather than try it again.', canPutBack, true); diff --git a/index.html b/index.html index b8ee9f3..70edf30 100644 --- a/index.html +++ b/index.html @@ -5971,6 +5971,12 @@

Your drawers

} // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); + /* Whether nothing has saved into drawer `d` since a page that had this tool's mark + `mine` and the other tool's `their`: the drawer has both halves as that page last + knew them. A size the page has that the drawer does not is then the page's own, + one the drawer could not take with the storage full, not the drawer moved on. */ + const unmoved = (d, mine, their) => typeof their === 'string' && + d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ const movedOn = (d, h, was) => d.marks[o.tool] !== fingerprint(h) && d.marks[o.tool] !== was; @@ -6375,7 +6381,7 @@

Your drawers

if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else catchUp(h, d); + else if (!unmoved(d, ours, theirs)) catchUp(h, d); // nor as this page left it (attach) }); return { @@ -6509,13 +6515,12 @@

Your drawers

link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not a hand-over from a page whose saves into the drawer were refused, the - storage full: the drawer has both halves as that page last saved them (the - note's marks), so a size that differs is one changed there since, which the - drawer could not take. Caught up, the drawer's older size came back over it, - as though another tab had moved the drawer on. */ - const asLeft = noted && typeof came[otherTool] === 'string' && - d.marks[otherTool] === came[otherTool] && d.marks[o.tool] === came[o.tool]; + /* Not when the drawer is as the page this design comes from left it (unmoved, by + the marks it came with): a hand-over from a page whose saves into the drawer + were refused, or this page reloaded or gone Back to after its own were. Caught + up, the drawer's older size came back over the one changed since, as though + another tab had moved the drawer on. */ + const asLeft = unmoved(d, came[o.tool], came[otherTool]); if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the @@ -9366,8 +9371,13 @@

Your drawers

page. One still carrying someone's link is that link, and your layout is kept. */ if (!keptAside) unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); } + /* Back to your own earlier page over a link's layout, untouched, sets nothing aside: what + the link replaced is aside already. That can be later than the page Back brings: a change + whose save came due as the link was pasted is aside, and in no earlier address (see + pastedOver). So a layout aside that is not this one is offered. */ + const later = back && savedLinked ? readKey(PREV_KEY) : ''; const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || - (back && keptAside); + (back && (keptAside || (!!later && !sameDesign(later, src)))); if (stalled) { showSetAside('This layout did not finish loading last time, so the page has started ' + 'from its defaults rather than try it again.', canPutBack, true); diff --git a/src/bins/ui.js b/src/bins/ui.js index ad451a3..83f5ecf 100644 --- a/src/bins/ui.js +++ b/src/bins/ui.js @@ -5927,8 +5927,13 @@ let arrivedWith = ''; // the design string this page was opened with page. One still carrying someone's link is that link, and your layout is kept. */ if (!keptAside) unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); } + /* Back to your own earlier page over a link's layout, untouched, sets nothing aside: what + the link replaced is aside already. That can be later than the page Back brings: a change + whose save came due as the link was pasted is aside, and in no earlier address (see + pastedOver). So a layout aside that is not this one is offered. */ + const later = back && savedLinked ? readKey(PREV_KEY) : ''; const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || - (back && keptAside); + (back && (keptAside || (!!later && !sameDesign(later, src)))); if (stalled) { showSetAside('This layout did not finish loading last time, so the page has started ' + 'from its defaults rather than try it again.', canPutBack, true); diff --git a/src/shared-ui/drawers.js b/src/shared-ui/drawers.js index cb9cb46..1adfcf0 100644 --- a/src/shared-ui/drawers.js +++ b/src/shared-ui/drawers.js @@ -446,6 +446,12 @@ const DRAWERS = (function () { } // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); + /* Whether nothing has saved into drawer `d` since a page that had this tool's mark + `mine` and the other tool's `their`: the drawer has both halves as that page last + knew them. A size the page has that the drawer does not is then the page's own, + one the drawer could not take with the storage full, not the drawer moved on. */ + const unmoved = (d, mine, their) => typeof their === 'string' && + d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ const movedOn = (d, h, was) => d.marks[o.tool] !== fingerprint(h) && d.marks[o.tool] !== was; @@ -850,7 +856,7 @@ const DRAWERS = (function () { if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else catchUp(h, d); + else if (!unmoved(d, ours, theirs)) catchUp(h, d); // nor as this page left it (attach) }); return { @@ -984,13 +990,12 @@ const DRAWERS = (function () { link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not a hand-over from a page whose saves into the drawer were refused, the - storage full: the drawer has both halves as that page last saved them (the - note's marks), so a size that differs is one changed there since, which the - drawer could not take. Caught up, the drawer's older size came back over it, - as though another tab had moved the drawer on. */ - const asLeft = noted && typeof came[otherTool] === 'string' && - d.marks[otherTool] === came[otherTool] && d.marks[o.tool] === came[o.tool]; + /* Not when the drawer is as the page this design comes from left it (unmoved, by + the marks it came with): a hand-over from a page whose saves into the drawer + were refused, or this page reloaded or gone Back to after its own were. Caught + up, the drawer's older size came back over the one changed since, as though + another tab had moved the drawer on. */ + const asLeft = unmoved(d, came[o.tool], came[otherTool]); if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the diff --git a/src/ui.js b/src/ui.js index f8eb186..a5b4bc2 100644 --- a/src/ui.js +++ b/src/ui.js @@ -2760,8 +2760,13 @@ let arrivedWith = ''; // the design string this page was opened with, if it s page. One still carrying someone's link is that link, and your layout is kept. */ if (!keptAside) unkept = !handOver || handOver.link.length > 0 || drawers.onlyMine(saved, src); } + /* Back to your own earlier page over a link's layout, untouched, sets nothing aside: what + the link replaced is aside already. That can be later than the page Back brings: a change + whose save came due as the link was pasted is aside, and in no earlier address (see + pastedOver). So a layout aside that is not this one is offered. */ + const later = back && savedLinked ? readKey(PREV_KEY) : ''; const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || - (back && keptAside); + (back && (keptAside || (!!later && !sameDesign(later, src)))); if (stalled) { showSetAside('This layout did not finish loading last time, so the page has started ' + 'from its defaults rather than try it again.', canPutBack, true); diff --git a/sw.js b/sw.js index 623d3ea..aaf995f 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "2f4c710c7069"; +const VERSION = "4590d3129720"; const FILES = [ "./", "bins/", diff --git a/test/ui/drawers.spec.js b/test/ui/drawers.spec.js index 38f9178..2d77eab 100644 --- a/test/ui/drawers.spec.js +++ b/test/ui/drawers.spec.js @@ -450,9 +450,22 @@ for (const tool of ['bins', 'plates']) { await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); if (tool === 'bins') await toPlates(page); else await toBins(page); expect(await page.inputValue('#drawerW'), 'on the other page').toBe('410'); + // reloaded there, and shown again from the back-forward cache: the drawer has not moved on + await page.reload(); + await (tool === 'bins' ? platesReady : binsReady)(page); + expect(await page.inputValue('#drawerW'), 'reloaded').toBe('410'); + const stayed = await page.evaluate(() => { + dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true })); + return !!(history.state && history.state.drawerforge); // catching up clears it to reload + }); + expect(stayed, 'shown again from the cache, it stays').toBe(true); if (tool === 'bins') await toBins(page); else await toPlates(page); expect(await page.inputValue('#drawerW'), 'and back').toBe('410'); await expect(page.locator('#setAside')).toBeHidden(); + // and Back to the other page, a page of its own come back + await page.goBack(); + await (tool === 'bins' ? platesReady : binsReady)(page); + expect(await page.inputValue('#drawerW'), 'Back on the other page').toBe('410'); expect(errors).toEqual([]); }); } @@ -486,7 +499,7 @@ test('with storage full, a layout set aside is not given the record of the one b const kept = await page.evaluate((k) => [localStorage.getItem(k + ':prev'), localStorage.getItem(k + ':prev:linked')], KEY); expect(kept[0], 'the changed link set aside').toBe(edited); - expect(kept[1], 'and not with the record before it').not.toBe('v=2&w=300'); + expect(kept[1], 'and not with the record before it').toBeNull(); expect(errors).toEqual([]); }); diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index 57a5666..81e3cf5 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -926,8 +926,14 @@ for (const [tool, key] of [['plates', PLATES], ['bins', BINS]]) { test(`a link pasted over ${tool} as an edit's save comes due is the link`, async ({ page }) => { const errors = watch(page); await arrive(page, tool === 'bins' ? binsUrl() : platesUrl()); - await H.setField(page, 'drawerW', '412'); // its save waiting + const before = await page.inputValue('#drawerW'); + // the edit, its save still waiting when the paste lands, all in one task await Promise.all([page.waitForEvent('load'), page.evaluate(() => { + const e = document.getElementById('drawerW'); + e.value = '412'; + e.dispatchEvent(new Event('input', { bubbles: true })); + e.dispatchEvent(new Event('change', { bubbles: true })); + if (typeof landEdit === 'function') landEdit(); // the edit read in, as a save does location.hash = '#w=333&d=444&v=2'; saveNow(); })]); @@ -936,6 +942,14 @@ for (const [tool, key] of [['plates', PLATES], ['bins', BINS]]) { await expect(page.locator('#setAsideMsg')).toHaveText('This link replaced the layout you had here.'); // the edit was saved, and is what the link set aside expect(await stored(page, key + ':prev')).toMatch(/(^|&)w=412(&|$)/); + + /* Back goes to the address before the paste, which never had the edit: it is aside, and + in no earlier address, so it is offered there too. */ + await page.goBack(); + await ready(page); + expect(await page.inputValue('#drawerW')).toBe(before); + await expect(page.locator('#setAsideMsg')) + .toHaveText('This page went back to an earlier layout of yours. The later one is set aside.'); await clickAndLoad(page, '#putBack'); expect(await page.inputValue('#drawerW')).toBe('412'); expect(errors).toEqual([]); From cbf497047314d817bce9e4f17650343a88358802 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 08:51:26 +0000 Subject: [PATCH 4/9] Keep a size over the drawer only when the drawer refused it, and offer only the pasted-over save on Back Round 2 of the review of the pasted-link and storage-full fixes: - A page keeps a size the drawer does not have only when its design holds a change the drawer refused. Equal marks alone did not prove that: a page that saves keeps another tab's size where it has not changed its own, and sets its mark all the same, so a tab that saved after another tab resized the drawer kept the old size through a reload or Back. The refusal now travels in the address's mark, the hand-over note and the tab's record of its last save. - A drawer only one tool has saved into counts too. - The kept change is counted from the drawer's settings, so the next save that goes through writes it into the drawer. - Back to the address a pasted link came over offers only the save the paste came over, recorded for the tab; any other layout aside could be older than the page Back brings. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 96 +++++++++++++++++-------- index.html | 96 +++++++++++++++++-------- src/bins/ui.js | 21 +++++- src/shared-ui/drawers.js | 75 ++++++++++++-------- src/ui.js | 21 +++++- sw.js | 2 +- test/ui/drawers.spec.js | 128 ++++++++++++++++++++++++++++++++++ test/ui/hostile-links.spec.js | 38 ++++++++++ 8 files changed, 381 insertions(+), 96 deletions(-) diff --git a/bins/index.html b/bins/index.html index f5bb7cc..38b4a08 100644 --- a/bins/index.html +++ b/bins/index.html @@ -6633,6 +6633,10 @@

Your drawers

let attached = null; // the drawer this page saves into, or null for an unsaved design let base = null; // its shared keys as this page arrived with or last saved them let failing = false; // the browser refused the last save into it + /* The page arrived as a page whose change the drawer refused left it (see attach), and + nothing it has saved since has gone through: the change is still the address's alone. */ + let refusedIn = false; + const refused = () => failing || refusedIn; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -6664,13 +6668,14 @@

Your drawers

ui.js works that out); `open` says a drawer was opened from the list, or caught up, rather than handed over. `marks` gives, for each tool, its mark in the drawer when its settings in `h` were current, where that is known (see restore): by tool, since - by way of the guide the next page can be either. Read once, by the page's boot (see - arrival). False if the browser would not keep it. */ + by way of the guide the next page can be either. `refused` says `h` holds a change + the drawer refused (see attach). Read once, by the page's boot (see arrival). False + if the browser would not keep it. */ function handOver(id, h, caughtUp, more) { const n = readNote('sessionStorage', TAB); n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), - marks: (more && more.marks) || {} }; + marks: (more && more.marks) || {}, refused: !!(more && more.refused) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -6698,11 +6703,12 @@

Your drawers

pasted over the page reloads it too, and the page says so first (forget). The record also carries what the address's own mark would have said: the page's marks from before the save (ours, theirs), by which the drawer has moved on from the address or - not, and which settings in the design saved are still someone's link's (link). */ - function leaving(id, now, had, link) { + not, which settings in the design saved are still someone's link's (link), and + whether the drawer refused the save (refused). */ + function leaving(id, now, had, link, refusedSave) { const n = readNote('sessionStorage', TAB); n.left = { tool: o.tool, id, was: fingerprint((location.hash || '').replace(/^#/, '')), now, - ours: had, theirs, link: strings(link) }; + ours: had, theirs, link: strings(link), refused: !!refusedSave }; writeNote('sessionStorage', TAB, n); } function reloaded() { @@ -6743,7 +6749,7 @@

Your drawers

link, untouched since it arrived; `note` the note the page arrived at it by, until the page saves from it. */ const stamp = (h, link, note) => ({ drawerforge: { tool: o.tool, fp: fingerprint(h), - id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs } }); + id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, refused: refused() } }); // marks the address the page is at, if it is `h`, leaving it as it is function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; @@ -6755,9 +6761,12 @@

Your drawers

const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); /* Whether nothing has saved into drawer `d` since a page that had this tool's mark `mine` and the other tool's `their`: the drawer has both halves as that page last - knew them. A size the page has that the drawer does not is then the page's own, - one the drawer could not take with the storage full, not the drawer moved on. */ - const unmoved = (d, mine, their) => typeof their === 'string' && + knew them. Either can be one the drawer has none of, as long as it has the other: a + drawer that one tool alone has saved into. For a page whose change the drawer + refused, a size the page has that the drawer does not is then that change, not the + drawer moved on. For no other page: one that saves keeps another tab's size where it + has not changed its own (mergeDesign), and sets its mark all the same. */ + const unmoved = (d, mine, their) => (typeof their === 'string' || typeof mine === 'string') && d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ @@ -6848,7 +6857,8 @@

Your drawers

} function paintBar() { const d = attached && find(loadAll(), attached); - if (attached && !d) { attached = null; failing = false; } // deleted on another tab + // deleted on another tab + if (attached && !d) { attached = null; failing = false; refusedIn = false; } /* A refused save leads, ahead of the name: on a phone the bar shows about a dozen characters, and the warning is the part that has to survive the cut. */ const shown = d ? (failing ? `not saving · ${d.name}` : d.name) : 'not saved'; @@ -6985,7 +6995,7 @@

Your drawers

return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; + base = sharedOf(h); failing = false; refusedIn = false; ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -7044,7 +7054,7 @@

Your drawers

s.drawers = s.drawers.filter((x) => x.id !== id); if (!saveAll(s)) { say('This browser would not store the change.', 'bad'); return; } const wasOpen = attached === id; - if (wasOpen) { attached = null; failing = false; } + if (wasOpen) { attached = null; failing = false; refusedIn = false; } armed = ''; render(); say(`Deleted “${d.name}”.` + (wasOpen @@ -7163,7 +7173,7 @@

Your drawers

if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else if (!unmoved(d, ours, theirs)) catchUp(h, d); // nor as this page left it (attach) + else if (!(refused() && unmoved(d, ours, theirs))) catchUp(h, d); // nor as this page left it (attach) }); return { @@ -7252,6 +7262,7 @@

Your drawers

attached = null; ours = undefined; theirs = undefined; + refusedIn = false; const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -7279,10 +7290,12 @@

Your drawers

if (d) { attached = d.id; base = sharedOf(arrivedWith); - // the marks the design's halves came with, from the note or this tab's own mark - const came = noted ? (isPlain(next.marks) ? next.marks : {}) - : mine && mine.id === d.id ? { [o.tool]: mine.ours, [otherTool]: mine.theirs } - : gone && gone.id === d.id ? { [o.tool]: gone.ours, [otherTool]: gone.theirs } : {}; + // the record the design came with: the note, this tab's own mark, or a raced reload's + const from = noted ? next : mine && mine.id === d.id ? mine + : gone && gone.id === d.id ? gone : null; + // the marks the design's halves came with, by it + const came = !from ? {} : noted ? (isPlain(next.marks) ? next.marks : {}) + : { [o.tool]: from.ours, [otherTool]: from.theirs }; const str = (v) => (typeof v === 'string' ? v : undefined); theirs = str(came[otherTool]); /* A raced reload's link settings, those the drawer still has as the page had @@ -7297,12 +7310,15 @@

Your drawers

link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not when the drawer is as the page this design comes from left it (unmoved, by - the marks it came with): a hand-over from a page whose saves into the drawer - were refused, or this page reloaded or gone Back to after its own were. Caught - up, the drawer's older size came back over the one changed since, as though - another tab had moved the drawer on. */ - const asLeft = unmoved(d, came[o.tool], came[otherTool]); + /* Not when the design holds a change the drawer refused, and the drawer is as the + page that made it left it (unmoved, by the marks it came with): a hand-over from + a page whose saves into the drawer were refused, or this page reloaded or gone + Back to after its own were. Caught up, the drawer's older size came back over + the one changed since, as though another tab had moved the drawer on. The + change is still to be saved, so it is counted from the drawer's settings: from + the design's, a save that went through left the drawer's in place. */ + const asLeft = !!from && from.refused === true && unmoved(d, came[o.tool], came[otherTool]); + if (asLeft) { refusedIn = true; base = sharedOf(d.hash); } if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the @@ -7312,7 +7328,7 @@

Your drawers

if (noted) { d.hash = mergeDesign(d.hash, arrivedWith, o.owns, base); d.marks[o.tool] = fp; - if (saveAll(s)) ours = fp; + if (saveAll(s)) { ours = fp; base = sharedOf(arrivedWith); refusedIn = false; } savedInto(d.id); } } @@ -7335,7 +7351,8 @@

Your drawers

const s = loadAll(); const d = find(s, attached); if (!d) { - attached = null; failing = false; savedInto(''); leaving('', '', ours, link); paintBar(); return; + attached = null; failing = false; refusedIn = false; + savedInto(''); leaving('', '', ours, link); paintBar(); return; } // nothing of this page's own to save, and a reload reopens onto the drawer if (behind(d, h)) { leaving(d.id, d.marks[o.tool], ours, link); return; } @@ -7344,12 +7361,12 @@

Your drawers

d.marks[o.tool] = fingerprint(h); d.saved = Date.now(); const ok = saveAll(s); - if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; } + if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's change, past the save the page last made into it (had): a reload that raced this save keeps it, rather than reopening onto the older half. */ - leaving(d.id, ok ? d.marks[o.tool] : was, had, link); + leaving(d.id, ok ? d.marks[o.tool] : was, had, link, !ok); /* The page's own save shares this storage and fails without a word, but this one is promised in so many words — the dialog says changes save into the drawer as you work — so a refused save is said, on the bar and in the dialog, until one goes @@ -7380,7 +7397,7 @@

Your drawers

} /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ - handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {} }); + handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, refused: !!d && refused() }); }, }; } @@ -17836,6 +17853,10 @@

Your drawers

catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not worth an exception that stops the rest of the page working */ } ownHash = location.hash; + } else { + try { sessionStorage.setItem(PASTED_KEY, JSON.stringify({ save: DRAWERS.fingerprint(h), + at: DRAWERS.fingerprint(ownHash) })); } + catch (err) { /* not kept, Back offers nothing: no worse than before */ } } } } @@ -17849,6 +17870,17 @@

Your drawers

let ownHash = null; const pastedOver = () => ownHash !== null && location.hash !== ownHash && isLayoutHash((location.hash || '').replace(/^#/, '')); +/* That save, and the address it left as it was, by fingerprint, for this tab: the layout + the link sets aside is the save, and it is later than the address, which never had the + change. Back to the address offers it, and only it (see `later` below). */ +const PASTED_KEY = SAVE_KEY + ':pasted'; +const pastedFrom = (prev) => { + try { + const r = JSON.parse(sessionStorage.getItem(PASTED_KEY) || 'null'); + return !!r && !!prev && r.save === DRAWERS.fingerprint(prev) && + r.at === DRAWERS.fingerprint(location.hash); + } catch (err) { return false; } +}; /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. @@ -18275,8 +18307,10 @@

Your drawers

/* Back to your own earlier page over a link's layout, untouched, sets nothing aside: what the link replaced is aside already. That can be later than the page Back brings: a change whose save came due as the link was pasted is aside, and in no earlier address (see - pastedOver). So a layout aside that is not this one is offered. */ - const later = back && savedLinked ? readKey(PREV_KEY) : ''; + pastedOver). So that layout is offered, if it is the one aside and this is the address it + came from, and no other: one aside for any other reason can be older than this page, and + Put back went further back, not on. */ + const later = back && savedLinked && pastedFrom(readKey(PREV_KEY)) ? readKey(PREV_KEY) : ''; const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || (back && (keptAside || (!!later && !sameDesign(later, src)))); if (stalled) { diff --git a/index.html b/index.html index 80c6e00..b8b3fdf 100644 --- a/index.html +++ b/index.html @@ -6420,6 +6420,10 @@

Your drawers

let attached = null; // the drawer this page saves into, or null for an unsaved design let base = null; // its shared keys as this page arrived with or last saved them let failing = false; // the browser refused the last save into it + /* The page arrived as a page whose change the drawer refused left it (see attach), and + nothing it has saved since has gone through: the change is still the address's alone. */ + let refusedIn = false; + const refused = () => failing || refusedIn; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -6451,13 +6455,14 @@

Your drawers

ui.js works that out); `open` says a drawer was opened from the list, or caught up, rather than handed over. `marks` gives, for each tool, its mark in the drawer when its settings in `h` were current, where that is known (see restore): by tool, since - by way of the guide the next page can be either. Read once, by the page's boot (see - arrival). False if the browser would not keep it. */ + by way of the guide the next page can be either. `refused` says `h` holds a change + the drawer refused (see attach). Read once, by the page's boot (see arrival). False + if the browser would not keep it. */ function handOver(id, h, caughtUp, more) { const n = readNote('sessionStorage', TAB); n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), - marks: (more && more.marks) || {} }; + marks: (more && more.marks) || {}, refused: !!(more && more.refused) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -6485,11 +6490,12 @@

Your drawers

pasted over the page reloads it too, and the page says so first (forget). The record also carries what the address's own mark would have said: the page's marks from before the save (ours, theirs), by which the drawer has moved on from the address or - not, and which settings in the design saved are still someone's link's (link). */ - function leaving(id, now, had, link) { + not, which settings in the design saved are still someone's link's (link), and + whether the drawer refused the save (refused). */ + function leaving(id, now, had, link, refusedSave) { const n = readNote('sessionStorage', TAB); n.left = { tool: o.tool, id, was: fingerprint((location.hash || '').replace(/^#/, '')), now, - ours: had, theirs, link: strings(link) }; + ours: had, theirs, link: strings(link), refused: !!refusedSave }; writeNote('sessionStorage', TAB, n); } function reloaded() { @@ -6530,7 +6536,7 @@

Your drawers

link, untouched since it arrived; `note` the note the page arrived at it by, until the page saves from it. */ const stamp = (h, link, note) => ({ drawerforge: { tool: o.tool, fp: fingerprint(h), - id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs } }); + id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, refused: refused() } }); // marks the address the page is at, if it is `h`, leaving it as it is function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; @@ -6542,9 +6548,12 @@

Your drawers

const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); /* Whether nothing has saved into drawer `d` since a page that had this tool's mark `mine` and the other tool's `their`: the drawer has both halves as that page last - knew them. A size the page has that the drawer does not is then the page's own, - one the drawer could not take with the storage full, not the drawer moved on. */ - const unmoved = (d, mine, their) => typeof their === 'string' && + knew them. Either can be one the drawer has none of, as long as it has the other: a + drawer that one tool alone has saved into. For a page whose change the drawer + refused, a size the page has that the drawer does not is then that change, not the + drawer moved on. For no other page: one that saves keeps another tab's size where it + has not changed its own (mergeDesign), and sets its mark all the same. */ + const unmoved = (d, mine, their) => (typeof their === 'string' || typeof mine === 'string') && d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ @@ -6635,7 +6644,8 @@

Your drawers

} function paintBar() { const d = attached && find(loadAll(), attached); - if (attached && !d) { attached = null; failing = false; } // deleted on another tab + // deleted on another tab + if (attached && !d) { attached = null; failing = false; refusedIn = false; } /* A refused save leads, ahead of the name: on a phone the bar shows about a dozen characters, and the warning is the part that has to survive the cut. */ const shown = d ? (failing ? `not saving · ${d.name}` : d.name) : 'not saved'; @@ -6772,7 +6782,7 @@

Your drawers

return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; + base = sharedOf(h); failing = false; refusedIn = false; ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -6831,7 +6841,7 @@

Your drawers

s.drawers = s.drawers.filter((x) => x.id !== id); if (!saveAll(s)) { say('This browser would not store the change.', 'bad'); return; } const wasOpen = attached === id; - if (wasOpen) { attached = null; failing = false; } + if (wasOpen) { attached = null; failing = false; refusedIn = false; } armed = ''; render(); say(`Deleted “${d.name}”.` + (wasOpen @@ -6950,7 +6960,7 @@

Your drawers

if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else if (!unmoved(d, ours, theirs)) catchUp(h, d); // nor as this page left it (attach) + else if (!(refused() && unmoved(d, ours, theirs))) catchUp(h, d); // nor as this page left it (attach) }); return { @@ -7039,6 +7049,7 @@

Your drawers

attached = null; ours = undefined; theirs = undefined; + refusedIn = false; const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -7066,10 +7077,12 @@

Your drawers

if (d) { attached = d.id; base = sharedOf(arrivedWith); - // the marks the design's halves came with, from the note or this tab's own mark - const came = noted ? (isPlain(next.marks) ? next.marks : {}) - : mine && mine.id === d.id ? { [o.tool]: mine.ours, [otherTool]: mine.theirs } - : gone && gone.id === d.id ? { [o.tool]: gone.ours, [otherTool]: gone.theirs } : {}; + // the record the design came with: the note, this tab's own mark, or a raced reload's + const from = noted ? next : mine && mine.id === d.id ? mine + : gone && gone.id === d.id ? gone : null; + // the marks the design's halves came with, by it + const came = !from ? {} : noted ? (isPlain(next.marks) ? next.marks : {}) + : { [o.tool]: from.ours, [otherTool]: from.theirs }; const str = (v) => (typeof v === 'string' ? v : undefined); theirs = str(came[otherTool]); /* A raced reload's link settings, those the drawer still has as the page had @@ -7084,12 +7097,15 @@

Your drawers

link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not when the drawer is as the page this design comes from left it (unmoved, by - the marks it came with): a hand-over from a page whose saves into the drawer - were refused, or this page reloaded or gone Back to after its own were. Caught - up, the drawer's older size came back over the one changed since, as though - another tab had moved the drawer on. */ - const asLeft = unmoved(d, came[o.tool], came[otherTool]); + /* Not when the design holds a change the drawer refused, and the drawer is as the + page that made it left it (unmoved, by the marks it came with): a hand-over from + a page whose saves into the drawer were refused, or this page reloaded or gone + Back to after its own were. Caught up, the drawer's older size came back over + the one changed since, as though another tab had moved the drawer on. The + change is still to be saved, so it is counted from the drawer's settings: from + the design's, a save that went through left the drawer's in place. */ + const asLeft = !!from && from.refused === true && unmoved(d, came[o.tool], came[otherTool]); + if (asLeft) { refusedIn = true; base = sharedOf(d.hash); } if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the @@ -7099,7 +7115,7 @@

Your drawers

if (noted) { d.hash = mergeDesign(d.hash, arrivedWith, o.owns, base); d.marks[o.tool] = fp; - if (saveAll(s)) ours = fp; + if (saveAll(s)) { ours = fp; base = sharedOf(arrivedWith); refusedIn = false; } savedInto(d.id); } } @@ -7122,7 +7138,8 @@

Your drawers

const s = loadAll(); const d = find(s, attached); if (!d) { - attached = null; failing = false; savedInto(''); leaving('', '', ours, link); paintBar(); return; + attached = null; failing = false; refusedIn = false; + savedInto(''); leaving('', '', ours, link); paintBar(); return; } // nothing of this page's own to save, and a reload reopens onto the drawer if (behind(d, h)) { leaving(d.id, d.marks[o.tool], ours, link); return; } @@ -7131,12 +7148,12 @@

Your drawers

d.marks[o.tool] = fingerprint(h); d.saved = Date.now(); const ok = saveAll(s); - if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; } + if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's change, past the save the page last made into it (had): a reload that raced this save keeps it, rather than reopening onto the older half. */ - leaving(d.id, ok ? d.marks[o.tool] : was, had, link); + leaving(d.id, ok ? d.marks[o.tool] : was, had, link, !ok); /* The page's own save shares this storage and fails without a word, but this one is promised in so many words — the dialog says changes save into the drawer as you work — so a refused save is said, on the bar and in the dialog, until one goes @@ -7167,7 +7184,7 @@

Your drawers

} /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ - handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {} }); + handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, refused: !!d && refused() }); }, }; } @@ -9484,6 +9501,10 @@

Your drawers

catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not worth an exception that stops the rest of the page working */ } ownHash = location.hash; + } else { + try { sessionStorage.setItem(PASTED_KEY, JSON.stringify({ save: DRAWERS.fingerprint(h), + at: DRAWERS.fingerprint(ownHash) })); } + catch (err) { /* not kept, Back offers nothing: no worse than before */ } } } } @@ -9497,6 +9518,17 @@

Your drawers

let ownHash = null; const pastedOver = () => ownHash !== null && location.hash !== ownHash && isLayoutHash((location.hash || '').replace(/^#/, '')); +/* That save, and the address it left as it was, by fingerprint, for this tab: the layout + the link sets aside is the save, and it is later than the address, which never had the + change. Back to the address offers it, and only it (see `later` below). */ +const PASTED_KEY = SAVE_KEY + ':pasted'; +const pastedFrom = (prev) => { + try { + const r = JSON.parse(sessionStorage.getItem(PASTED_KEY) || 'null'); + return !!r && !!prev && r.save === DRAWERS.fingerprint(prev) && + r.at === DRAWERS.fingerprint(location.hash); + } catch (err) { return false; } +}; /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. @@ -9949,8 +9981,10 @@

Your drawers

/* Back to your own earlier page over a link's layout, untouched, sets nothing aside: what the link replaced is aside already. That can be later than the page Back brings: a change whose save came due as the link was pasted is aside, and in no earlier address (see - pastedOver). So a layout aside that is not this one is offered. */ - const later = back && savedLinked ? readKey(PREV_KEY) : ''; + pastedOver). So that layout is offered, if it is the one aside and this is the address it + came from, and no other: one aside for any other reason can be older than this page, and + Put back went further back, not on. */ + const later = back && savedLinked && pastedFrom(readKey(PREV_KEY)) ? readKey(PREV_KEY) : ''; const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || (back && (keptAside || (!!later && !sameDesign(later, src)))); if (stalled) { diff --git a/src/bins/ui.js b/src/bins/ui.js index 83f5ecf..93fb622 100644 --- a/src/bins/ui.js +++ b/src/bins/ui.js @@ -5491,6 +5491,10 @@ function saveNow() { catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not worth an exception that stops the rest of the page working */ } ownHash = location.hash; + } else { + try { sessionStorage.setItem(PASTED_KEY, JSON.stringify({ save: DRAWERS.fingerprint(h), + at: DRAWERS.fingerprint(ownHash) })); } + catch (err) { /* not kept, Back offers nothing: no worse than before */ } } } } @@ -5504,6 +5508,17 @@ function saveNow() { let ownHash = null; const pastedOver = () => ownHash !== null && location.hash !== ownHash && isLayoutHash((location.hash || '').replace(/^#/, '')); +/* That save, and the address it left as it was, by fingerprint, for this tab: the layout + the link sets aside is the save, and it is later than the address, which never had the + change. Back to the address offers it, and only it (see `later` below). */ +const PASTED_KEY = SAVE_KEY + ':pasted'; +const pastedFrom = (prev) => { + try { + const r = JSON.parse(sessionStorage.getItem(PASTED_KEY) || 'null'); + return !!r && !!prev && r.save === DRAWERS.fingerprint(prev) && + r.at === DRAWERS.fingerprint(location.hash); + } catch (err) { return false; } +}; /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. @@ -5930,8 +5945,10 @@ let arrivedWith = ''; // the design string this page was opened with /* Back to your own earlier page over a link's layout, untouched, sets nothing aside: what the link replaced is aside already. That can be later than the page Back brings: a change whose save came due as the link was pasted is aside, and in no earlier address (see - pastedOver). So a layout aside that is not this one is offered. */ - const later = back && savedLinked ? readKey(PREV_KEY) : ''; + pastedOver). So that layout is offered, if it is the one aside and this is the address it + came from, and no other: one aside for any other reason can be older than this page, and + Put back went further back, not on. */ + const later = back && savedLinked && pastedFrom(readKey(PREV_KEY)) ? readKey(PREV_KEY) : ''; const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || (back && (keptAside || (!!later && !sameDesign(later, src)))); if (stalled) { diff --git a/src/shared-ui/drawers.js b/src/shared-ui/drawers.js index 1adfcf0..b5e4915 100644 --- a/src/shared-ui/drawers.js +++ b/src/shared-ui/drawers.js @@ -326,6 +326,10 @@ const DRAWERS = (function () { let attached = null; // the drawer this page saves into, or null for an unsaved design let base = null; // its shared keys as this page arrived with or last saved them let failing = false; // the browser refused the last save into it + /* The page arrived as a page whose change the drawer refused left it (see attach), and + nothing it has saved since has gone through: the change is still the address's alone. */ + let refusedIn = false; + const refused = () => failing || refusedIn; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -357,13 +361,14 @@ const DRAWERS = (function () { ui.js works that out); `open` says a drawer was opened from the list, or caught up, rather than handed over. `marks` gives, for each tool, its mark in the drawer when its settings in `h` were current, where that is known (see restore): by tool, since - by way of the guide the next page can be either. Read once, by the page's boot (see - arrival). False if the browser would not keep it. */ + by way of the guide the next page can be either. `refused` says `h` holds a change + the drawer refused (see attach). Read once, by the page's boot (see arrival). False + if the browser would not keep it. */ function handOver(id, h, caughtUp, more) { const n = readNote('sessionStorage', TAB); n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), - marks: (more && more.marks) || {} }; + marks: (more && more.marks) || {}, refused: !!(more && more.refused) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -391,11 +396,12 @@ const DRAWERS = (function () { pasted over the page reloads it too, and the page says so first (forget). The record also carries what the address's own mark would have said: the page's marks from before the save (ours, theirs), by which the drawer has moved on from the address or - not, and which settings in the design saved are still someone's link's (link). */ - function leaving(id, now, had, link) { + not, which settings in the design saved are still someone's link's (link), and + whether the drawer refused the save (refused). */ + function leaving(id, now, had, link, refusedSave) { const n = readNote('sessionStorage', TAB); n.left = { tool: o.tool, id, was: fingerprint((location.hash || '').replace(/^#/, '')), now, - ours: had, theirs, link: strings(link) }; + ours: had, theirs, link: strings(link), refused: !!refusedSave }; writeNote('sessionStorage', TAB, n); } function reloaded() { @@ -436,7 +442,7 @@ const DRAWERS = (function () { link, untouched since it arrived; `note` the note the page arrived at it by, until the page saves from it. */ const stamp = (h, link, note) => ({ drawerforge: { tool: o.tool, fp: fingerprint(h), - id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs } }); + id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, refused: refused() } }); // marks the address the page is at, if it is `h`, leaving it as it is function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; @@ -448,9 +454,12 @@ const DRAWERS = (function () { const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); /* Whether nothing has saved into drawer `d` since a page that had this tool's mark `mine` and the other tool's `their`: the drawer has both halves as that page last - knew them. A size the page has that the drawer does not is then the page's own, - one the drawer could not take with the storage full, not the drawer moved on. */ - const unmoved = (d, mine, their) => typeof their === 'string' && + knew them. Either can be one the drawer has none of, as long as it has the other: a + drawer that one tool alone has saved into. For a page whose change the drawer + refused, a size the page has that the drawer does not is then that change, not the + drawer moved on. For no other page: one that saves keeps another tab's size where it + has not changed its own (mergeDesign), and sets its mark all the same. */ + const unmoved = (d, mine, their) => (typeof their === 'string' || typeof mine === 'string') && d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ @@ -541,7 +550,8 @@ const DRAWERS = (function () { } function paintBar() { const d = attached && find(loadAll(), attached); - if (attached && !d) { attached = null; failing = false; } // deleted on another tab + // deleted on another tab + if (attached && !d) { attached = null; failing = false; refusedIn = false; } /* A refused save leads, ahead of the name: on a phone the bar shows about a dozen characters, and the warning is the part that has to survive the cut. */ const shown = d ? (failing ? `not saving · ${d.name}` : d.name) : 'not saved'; @@ -678,7 +688,7 @@ const DRAWERS = (function () { return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; + base = sharedOf(h); failing = false; refusedIn = false; ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -737,7 +747,7 @@ const DRAWERS = (function () { s.drawers = s.drawers.filter((x) => x.id !== id); if (!saveAll(s)) { say('This browser would not store the change.', 'bad'); return; } const wasOpen = attached === id; - if (wasOpen) { attached = null; failing = false; } + if (wasOpen) { attached = null; failing = false; refusedIn = false; } armed = ''; render(); say(`Deleted “${d.name}”.` + (wasOpen @@ -856,7 +866,7 @@ const DRAWERS = (function () { if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else if (!unmoved(d, ours, theirs)) catchUp(h, d); // nor as this page left it (attach) + else if (!(refused() && unmoved(d, ours, theirs))) catchUp(h, d); // nor as this page left it (attach) }); return { @@ -945,6 +955,7 @@ const DRAWERS = (function () { attached = null; ours = undefined; theirs = undefined; + refusedIn = false; const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -972,10 +983,12 @@ const DRAWERS = (function () { if (d) { attached = d.id; base = sharedOf(arrivedWith); - // the marks the design's halves came with, from the note or this tab's own mark - const came = noted ? (isPlain(next.marks) ? next.marks : {}) - : mine && mine.id === d.id ? { [o.tool]: mine.ours, [otherTool]: mine.theirs } - : gone && gone.id === d.id ? { [o.tool]: gone.ours, [otherTool]: gone.theirs } : {}; + // the record the design came with: the note, this tab's own mark, or a raced reload's + const from = noted ? next : mine && mine.id === d.id ? mine + : gone && gone.id === d.id ? gone : null; + // the marks the design's halves came with, by it + const came = !from ? {} : noted ? (isPlain(next.marks) ? next.marks : {}) + : { [o.tool]: from.ours, [otherTool]: from.theirs }; const str = (v) => (typeof v === 'string' ? v : undefined); theirs = str(came[otherTool]); /* A raced reload's link settings, those the drawer still has as the page had @@ -990,12 +1003,15 @@ const DRAWERS = (function () { link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not when the drawer is as the page this design comes from left it (unmoved, by - the marks it came with): a hand-over from a page whose saves into the drawer - were refused, or this page reloaded or gone Back to after its own were. Caught - up, the drawer's older size came back over the one changed since, as though - another tab had moved the drawer on. */ - const asLeft = unmoved(d, came[o.tool], came[otherTool]); + /* Not when the design holds a change the drawer refused, and the drawer is as the + page that made it left it (unmoved, by the marks it came with): a hand-over from + a page whose saves into the drawer were refused, or this page reloaded or gone + Back to after its own were. Caught up, the drawer's older size came back over + the one changed since, as though another tab had moved the drawer on. The + change is still to be saved, so it is counted from the drawer's settings: from + the design's, a save that went through left the drawer's in place. */ + const asLeft = !!from && from.refused === true && unmoved(d, came[o.tool], came[otherTool]); + if (asLeft) { refusedIn = true; base = sharedOf(d.hash); } if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the @@ -1005,7 +1021,7 @@ const DRAWERS = (function () { if (noted) { d.hash = mergeDesign(d.hash, arrivedWith, o.owns, base); d.marks[o.tool] = fp; - if (saveAll(s)) ours = fp; + if (saveAll(s)) { ours = fp; base = sharedOf(arrivedWith); refusedIn = false; } savedInto(d.id); } } @@ -1028,7 +1044,8 @@ const DRAWERS = (function () { const s = loadAll(); const d = find(s, attached); if (!d) { - attached = null; failing = false; savedInto(''); leaving('', '', ours, link); paintBar(); return; + attached = null; failing = false; refusedIn = false; + savedInto(''); leaving('', '', ours, link); paintBar(); return; } // nothing of this page's own to save, and a reload reopens onto the drawer if (behind(d, h)) { leaving(d.id, d.marks[o.tool], ours, link); return; } @@ -1037,12 +1054,12 @@ const DRAWERS = (function () { d.marks[o.tool] = fingerprint(h); d.saved = Date.now(); const ok = saveAll(s); - if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; } + if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's change, past the save the page last made into it (had): a reload that raced this save keeps it, rather than reopening onto the older half. */ - leaving(d.id, ok ? d.marks[o.tool] : was, had, link); + leaving(d.id, ok ? d.marks[o.tool] : was, had, link, !ok); /* The page's own save shares this storage and fails without a word, but this one is promised in so many words — the dialog says changes save into the drawer as you work — so a refused save is said, on the bar and in the dialog, until one goes @@ -1073,7 +1090,7 @@ const DRAWERS = (function () { } /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ - handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {} }); + handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, refused: !!d && refused() }); }, }; } diff --git a/src/ui.js b/src/ui.js index 0b0087f..c371271 100644 --- a/src/ui.js +++ b/src/ui.js @@ -2304,6 +2304,10 @@ function saveNow() { catch (err) { /* some browsers refuse replaceState on file:// — a lost URL is not worth an exception that stops the rest of the page working */ } ownHash = location.hash; + } else { + try { sessionStorage.setItem(PASTED_KEY, JSON.stringify({ save: DRAWERS.fingerprint(h), + at: DRAWERS.fingerprint(ownHash) })); } + catch (err) { /* not kept, Back offers nothing: no worse than before */ } } } } @@ -2317,6 +2321,17 @@ function saveNow() { let ownHash = null; const pastedOver = () => ownHash !== null && location.hash !== ownHash && isLayoutHash((location.hash || '').replace(/^#/, '')); +/* That save, and the address it left as it was, by fingerprint, for this tab: the layout + the link sets aside is the save, and it is later than the address, which never had the + change. Back to the address offers it, and only it (see `later` below). */ +const PASTED_KEY = SAVE_KEY + ':pasted'; +const pastedFrom = (prev) => { + try { + const r = JSON.parse(sessionStorage.getItem(PASTED_KEY) || 'null'); + return !!r && !!prev && r.save === DRAWERS.fingerprint(prev) && + r.at === DRAWERS.fingerprint(location.hash); + } catch (err) { return false; } +}; /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. @@ -2769,8 +2784,10 @@ let arrivedWith = ''; // the design string this page was opened with, if it s /* Back to your own earlier page over a link's layout, untouched, sets nothing aside: what the link replaced is aside already. That can be later than the page Back brings: a change whose save came due as the link was pasted is aside, and in no earlier address (see - pastedOver). So a layout aside that is not this one is offered. */ - const later = back && savedLinked ? readKey(PREV_KEY) : ''; + pastedOver). So that layout is offered, if it is the one aside and this is the address it + came from, and no other: one aside for any other reason can be older than this page, and + Put back went further back, not on. */ + const later = back && savedLinked && pastedFrom(readKey(PREV_KEY)) ? readKey(PREV_KEY) : ''; const canPutBack = (replaces && !kept && (keptAside || (savedLinked && !!readKey(PREV_KEY)))) || (back && (keptAside || (!!later && !sameDesign(later, src)))); if (stalled) { diff --git a/sw.js b/sw.js index d2370a8..69aa88e 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "2de75635e909"; +const VERSION = "fdf4a9819e32"; const FILES = [ "./", "bins/", diff --git a/test/ui/drawers.spec.js b/test/ui/drawers.spec.js index 2d77eab..d08076f 100644 --- a/test/ui/drawers.spec.js +++ b/test/ui/drawers.spec.js @@ -466,6 +466,45 @@ for (const tool of ['bins', 'plates']) { await page.goBack(); await (tool === 'bins' ? platesReady : binsReady)(page); expect(await page.inputValue('#drawerW'), 'Back on the other page').toBe('410'); + /* The size is still to be saved there. With room again, the next change saves it into + the drawer: counted from the size the page arrived with, the save kept the drawer's. */ + await page.evaluate(() => sessionStorage.setItem('full', '0')); + if (tool === 'bins') await page.selectOption('#connector', 'hclip'); + else await H.setField(page, 'gap', '4'); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'the drawer has the size', timeout: 20000 }) + .toMatchObject(tool === 'bins' ? { w: '410', cn: 'hclip' } : { w: '410', bgap: '4' }); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + expect(errors).toEqual([]); + }); +} + +/* So it is on the page that made the change, reloaded, when that page's tool alone has + saved into the drawer. With no mark of the other page's in it, the drawer was taken to + have moved on, and the size went back. */ +for (const tool of ['bins', 'plates']) { + test(`with storage full, a size changed on ${tool} is still there after a reload, ` + + `in a drawer only ${tool} has saved into`, async ({ page }) => { + const ready = tool === 'bins' ? binsReady : platesReady; + await page.addInitScript(fullStorage); + const errors = []; + page.on('pageerror', (e) => errors.push(String(e))); + await page.goto(base + (tool === 'bins' ? 'bins/' : '')); + await ready(page); + await H.setField(page, 'drawerW', '400'); + await saveAs(page, 'Kitchen'); + await settle(page); + expect(await page.evaluate(() => JSON.parse(localStorage.getItem('drawerforge:drawers:v1')) + .drawers.map((d) => Object.keys(d.marks).join()))).toEqual([tool]); + await page.evaluate(() => sessionStorage.setItem('full', '1')); + await H.setField(page, 'drawerW', '410'); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + await settle(page); + await page.reload(); + await ready(page); + await settle(page); + expect(await page.inputValue('#drawerW'), 'reloaded').toBe('410'); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); expect(errors).toEqual([]); }); } @@ -944,6 +983,95 @@ for (const tool of ['plates', 'bins']) { }); } +// a change to a page's own half alone: the field, the value it is changed to, and its key +const ownChange = (tool) => (tool === 'bins' ? ['gap', '4', 'bgap'] : ['connector', 'hclip', 'cn']); +async function changeOwn(page, tool) { + const [field, v] = ownChange(tool); + if (tool === 'bins') await H.setField(page, field, v); + else await page.selectOption('#' + field, v); +} +// Kitchen at 400, with both pages' marks in it, open in this tab on `tool` +async function kitchenOn(page, tool) { + const errors = await openPlates(page); + await H.setField(page, 'drawerW', '400'); + await saveAs(page, 'Kitchen'); + await toBins(page); + await toPlates(page); + if (tool === 'bins') await toBins(page); + await settle(page); + return errors; +} +// another tab of `tool` on Kitchen moves its size to 420, and changes that page's own half too +async function moveKitchen(context, tool, errors, own) { + const other = await context.newPage(); + other.on('pageerror', (e) => errors.push(String(e))); + await other.goto(base + (tool === 'bins' ? 'bins/' : '')); + await (tool === 'bins' ? binsReady : platesReady)(other); + await expect(other.locator('#drawerName')).toHaveText('Kitchen'); + await H.setField(other, 'drawerW', '420'); + if (own) await changeOwn(other, tool); + await settle(other); + await other.close(); +} + +/* And when the first tab saves a change of its own before it comes back. Its save keeps the + other tab's size, which it never changed (see mergeDesign in drawers.js), and sets its + mark all the same, so the drawer had the marks the first tab last knew. Taken for a size + the drawer had refused, the old size stayed: on the page, in its downloads and in what it + handed over. Reloaded, and shown again from the back-forward cache. */ +for (const tool of ['plates', 'bins']) for (const how of ['reloaded', 'shown again from the cache']) { + test(`a ${tool} tab that saves after another tab moved its drawer on catches up, ${how}`, + async ({ page, context }) => { + const ready = tool === 'plates' ? platesReady : binsReady; + const errors = await kitchenOn(page, tool); + await moveKitchen(context, tool, errors, false); + expect(await page.inputValue('#drawerW')).toBe('400'); + await changeOwn(page, tool); + const [field, v, key] = ownChange(tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'its change is saved, beside the other tab\'s size', timeout: 20000 }) + .toMatchObject({ w: '420', [key]: v }); + + if (how === 'reloaded') await page.reload(); + else await page.evaluate(() => dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true }))); + await expect.poll(() => page.inputValue('#drawerW').catch(() => ''), + { message: 'caught up with the other tab', timeout: 20000 }).toBe('420'); + await ready(page); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + expect(await page.inputValue('#' + field), 'with its own change').toBe(v); + await settle(page); + expect((await stored(page)).Kitchen).toMatchObject({ w: '420', [key]: v }); + expect(errors).toEqual([]); + }); +} + +/* A hand-over onto a drawer whose other half another tab has moved on, with its size: the + page there takes that half from the drawer (restore, in drawers.js), reloading with the + drawer's mark for it, and the drawer then had the marks that reload knew. Taken for a + size the drawer had refused, the old size stayed, handed over and reloaded. */ +for (const tool of ['plates', 'bins']) { + test(`a hand-over from ${tool} that takes the other page's half from the drawer catches up its size`, + async ({ page, context }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + const ready = there === 'plates' ? platesReady : binsReady; + const errors = await kitchenOn(page, tool); + await moveKitchen(context, there, errors, true); + expect(await page.inputValue('#drawerW')).toBe('400'); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + const [field, v] = ownChange(there); + for (const step of ['handed over', 'reloaded']) { + if (step === 'reloaded') await page.reload(); + await expect.poll(() => page.inputValue('#drawerW').catch(() => ''), + { message: step, timeout: 20000 }).toBe('420'); + await ready(page); + expect(await page.inputValue('#' + field), `${step}, with the other tab's change there`).toBe(v); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + await settle(page); + } + expect(errors).toEqual([]); + }); +} + /* Each page saves its whole half of the drawer, so one whose half is older than the drawer's would write it back: a reload of a tab after another tab of the same tool changed the drawer, or Back to a page of yours that a later page has moved on. Both diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index 81e3cf5..3a07440 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -955,3 +955,41 @@ for (const [tool, key] of [['plates', PLATES], ['bins', BINS]]) { expect(errors).toEqual([]); }); } + +/* And only that save. A layout aside for any other reason can be older than the page Back + brings: someone's link kept as a drawer of yours, a second link over it, then Back to the + first. That offered "the later one", and Put back brought the layout from before the + first link, with the second gone from Forward. */ +for (const tool of ['plates', 'bins']) { + test(`Back to a link kept as a drawer on ${tool}, over a second link, offers nothing older`, + async ({ page }) => { + const errors = watch(page); + const url = tool === 'bins' ? binsUrl() : platesUrl(); + await arrive(page, url); + await H.setField(page, 'drawerW', '400'); + await saved(page); + const paste = async (h) => { + await Promise.all([page.waitForEvent('load'), page.evaluate((x) => { location.hash = x; }, h)]); + await ready(page); + }; + await paste('#w=333&d=444&v=2'); + await page.click('#drawersBtn'); + await page.fill('#drawersNewName', 'Kit'); + await page.press('#drawersNewName', 'Enter'); + await expect(page.locator('#drawersMsg')).toContainText('Saved as “Kit”'); + await page.click('#drawersClose'); + await saved(page); + await paste('#w=355&d=466&v=2'); + await expect(page.locator('#setAsideMsg')).toHaveText('This link replaced the layout you had here.'); + + await page.goBack(); + await ready(page); + expect(await page.inputValue('#drawerW')).toBe('333'); + await expect(page.locator('#drawerName')).toHaveText('Kit'); + await expect(page.locator('#setAside'), 'nothing is offered').toBeHidden(); + await page.goForward(); + await ready(page); + expect(await page.inputValue('#drawerW'), 'and the second link is still Forward').toBe('355'); + expect(errors).toEqual([]); + }); +} From 984bd8adbc858b58ab3a57f921705f4f9512ca22 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 12:45:14 +0000 Subject: [PATCH 5/9] Leave on Start fresh as Put back does, so no change saves after the clearing Start fresh cleared this browser's save and set the address, and the page ran on until the bare one arrived. A change that landed in between was saved after the clearing, and the bare page came back with it, saying it had been restored. It now leaves by leaveFor, which stops the page's saves, on both pages. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 8 +++-- index.html | 8 +++-- src/bins/ui.js | 8 +++-- src/ui.js | 8 +++-- sw.js | 2 +- test/ui/hostile-links.spec.js | 65 +++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 9 deletions(-) diff --git a/bins/index.html b/bins/index.html index 2b86725..8276a78 100644 --- a/bins/index.html +++ b/bins/index.html @@ -17738,10 +17738,14 @@

Your drawers

const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } - location.href = location.origin + location.pathname; // drop the hash and reload clean + /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and + a change that landed in between was saved after the clearing, and came back with the + note that it had been restored. */ + leaveFor(location.origin + location.pathname); // drop the hash and reload clean } /* Wired here, before the boot below reads any link: a link that throws there must not - also take away the button that gets you out of it, or stop the next link working. */ + also take away the button that gets you out of it, or stop the next link working. What + leaveFor stops, hashReady and hashSaveT, is declared above, so it works from here too. */ $('startFresh').addEventListener('click', startFresh); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a diff --git a/index.html b/index.html index b8b3fdf..e984776 100644 --- a/index.html +++ b/index.html @@ -9326,10 +9326,14 @@

Your drawers

const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } - location.href = location.origin + location.pathname; // drop the hash and reload clean + /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and + a change that landed in between was saved after the clearing, and came back with the + note that it had been restored. */ + leaveFor(location.origin + location.pathname); // drop the hash and reload clean } /* Wired here, before the boot below reads any link: a link that throws there must not - also take away the button that gets you out of it, or stop the next link working. */ + also take away the button that gets you out of it, or stop the next link working. What + leaveFor stops, hashReady and hashSaveT, is declared above, so it works from here too. */ $('startFresh').addEventListener('click', startFresh); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a diff --git a/src/bins/ui.js b/src/bins/ui.js index 53bb035..5c8e892 100644 --- a/src/bins/ui.js +++ b/src/bins/ui.js @@ -5357,10 +5357,14 @@ const readLocal = () => { try { return localStorage.getItem(SAVE_KEY) || ''; } c const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } - location.href = location.origin + location.pathname; // drop the hash and reload clean + /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and + a change that landed in between was saved after the clearing, and came back with the + note that it had been restored. */ + leaveFor(location.origin + location.pathname); // drop the hash and reload clean } /* Wired here, before the boot below reads any link: a link that throws there must not - also take away the button that gets you out of it, or stop the next link working. */ + also take away the button that gets you out of it, or stop the next link working. What + leaveFor stops, hashReady and hashSaveT, is declared above, so it works from here too. */ $('startFresh').addEventListener('click', startFresh); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a diff --git a/src/ui.js b/src/ui.js index c371271..706d913 100644 --- a/src/ui.js +++ b/src/ui.js @@ -2129,10 +2129,14 @@ const readLocal = () => { try { return localStorage.getItem(SAVE_KEY) || ''; } c const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } - location.href = location.origin + location.pathname; // drop the hash and reload clean + /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and + a change that landed in between was saved after the clearing, and came back with the + note that it had been restored. */ + leaveFor(location.origin + location.pathname); // drop the hash and reload clean } /* Wired here, before the boot below reads any link: a link that throws there must not - also take away the button that gets you out of it, or stop the next link working. */ + also take away the button that gets you out of it, or stop the next link working. What + leaveFor stops, hashReady and hashSaveT, is declared above, so it works from here too. */ $('startFresh').addEventListener('click', startFresh); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a diff --git a/sw.js b/sw.js index c08eced..41ce3a0 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "9558c9892493"; +const VERSION = "c8675f90b493"; const FILES = [ "./", "bins/", diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index 3a07440..f95cd03 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -993,3 +993,68 @@ for (const tool of ['plates', 'bins']) { expect(errors).toEqual([]); }); } + +/* ---------- Start fresh ----------------------------------------------------- */ + +/* Start fresh clears this browser's save and loads the bare page, and the page runs on + until that one arrives. A change that landed in between was saved after the clearing, + and the bare page came back with it, saying it had been restored. Here the bare page is + slow to come, and the change lands 50 ms after the press: on Baseplates a field's + change, on Bins its input, which Bins reads in 180 ms on. Both pages are covered. */ +for (const tool of ['plates', 'bins']) { + test(`Start fresh on ${tool} is not undone by a change that lands as the page goes`, async ({ page }) => { + const errors = watch(page); + const url = tool === 'bins' ? binsUrl() : platesUrl(); + await arrive(page, url + '#w=333&d=444&v=2'); + await page.route((u) => u.href === url, async (route) => { + await new Promise((r) => setTimeout(r, 1500)); + await route.continue(); + }); + await Promise.all([page.waitForEvent('load'), page.evaluate((ev) => { + document.getElementById('startFresh').click(); + setTimeout(() => { + const e = document.getElementById('drawerW'); + e.value = '345'; + e.dispatchEvent(new Event(ev, { bubbles: true })); + }, 50); + }, tool === 'bins' ? 'input' : 'change')]); + await ready(page); + expect(await page.inputValue('#drawerW')).not.toBe('345'); + await expect(page.locator('#restored')).toBeHidden(); + expect(await stored(page, tool === 'bins' ? BINS : PLATES)).not.toMatch(/(^|&)w=345(&|$)/); + expect(errors).toEqual([]); + }); +} + +/* And the button is wired before the boot reads a link, so a link that throws partway + through the boot does not take it away: Start fresh still clears the save and loads the + bare page with the defaults, though the page never got as far as letting saves start. + The links that used to throw no longer do, so the page is made to throw here, after it + has read a link carrying `boom`. The note the button sits in is shown later in the + boot, so it is pressed from script. */ +for (const tool of ['plates', 'bins']) { + test(`Start fresh on ${tool} still clears the save when a link throws in the boot`, async ({ page }) => { + const url = tool === 'bins' ? binsUrl() : platesUrl(); + await arrive(page, url); + await H.setField(page, 'drawerW', '451'); + await saved(page); + await page.route((u) => u.href === url, async (route) => { + const res = await route.fetch(); + await route.fulfill({ response: res, body: (await res.text()).replace('loadFromHash(src);', + "loadFromHash(src); if (/(^|&)boom=/.test(src)) throw new Error('boom');") }); + }); + const thrown = []; + page.on('pageerror', (e) => thrown.push(String(e))); + await arrive(page, url + '#w=345&d=444&boom=1&v=2'); + expect(thrown.join(), 'the boot threw').toContain('boom'); + await Promise.all([page.waitForEvent('load'), + page.evaluate(() => document.getElementById('startFresh').click())]); + await ready(page); + expect(await page.evaluate(() => new URL(performance.getEntriesByType('navigation')[0].name).hash), + 'the bare page').toBe(''); + expect(await page.inputValue('#drawerW')).not.toBe('451'); + expect(await page.inputValue('#drawerW')).not.toBe('345'); + await expect(page.locator('#restored')).toBeHidden(); + expect(thrown.filter((e) => !/boom/.test(e))).toEqual([]); + }); +} From 6d99671981eb9d580ebf86579583dac457a5f080 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 16:08:41 +0000 Subject: [PATCH 6/9] Count a refused change from the page's own base, carried with it A page whose saves into the drawer are refused passes its base (its shared keys as it last saved them or arrived with them) along with the flag: in the address's mark, the hand-over note, the raced reload's record, and the notes for a catch-up and for restore. The page that takes the change on counts it from that base, so the change is still to be saved and whatever another tab moved on since is caught up, as on main. Round 2 counted it from the drawer's settings instead, so a page whose last save had kept another tab's 420 took its own stale 400 for the change and wrote it over that tab's once there was room (M2). The catch-up and restore notes dropped the flag, so a refused size went back after either (N1, R1). The pageshow return is main's again, and the marks no longer decide it. Start fresh and Put back also forget the pasted-link record, which offered an edit older than the fresh page on a later Back (O14b). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 86 ++++++++++++------- index.html | 86 ++++++++++++------- src/bins/ui.js | 9 ++ src/shared-ui/drawers.js | 77 ++++++++++------- src/ui.js | 9 ++ sw.js | 2 +- test/ui/drawers.spec.js | 153 ++++++++++++++++++++++++++++++++-- test/ui/hostile-links.spec.js | 43 ++++++++++ 8 files changed, 359 insertions(+), 106 deletions(-) diff --git a/bins/index.html b/bins/index.html index 8276a78..ac048db 100644 --- a/bins/index.html +++ b/bins/index.html @@ -6662,20 +6662,33 @@

Your drawers

if (n[o.tool] !== id) { n[o.tool] = id; writeNote(area, key, n); } } } + /* For a page whose design holds a change the drawer refused (`on`), what the page that + takes the design on needs to know of it, in a note, a mark or a raced reload's + record: that it does, and the page's base `b`, its shared keys as it last saved them + or arrived with them, which its changes are counted from (see attach). Nothing for + any other page, whose base is the design it hands on. */ + const carry = (on, b) => (on && b ? { refused: true, base: [...b] } : {}); + // the base a note, mark or record carries (see carry), or null for one that carries none + function carried(r) { + if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; + return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && + typeof p[1] === 'string' && SHARED.has(p[0]))); + } /* The note for the next page to load in this tab: the drawer ('' for none), and the fingerprint of the design that page will arrive with. `link` lists the drawer, bed and infill settings in it that are still someone's link's, not yours (the tool's ui.js works that out); `open` says a drawer was opened from the list, or caught up, rather than handed over. `marks` gives, for each tool, its mark in the drawer when its settings in `h` were current, where that is known (see restore): by tool, since - by way of the guide the next page can be either. `refused` says `h` holds a change - the drawer refused (see attach). Read once, by the page's boot (see arrival). False - if the browser would not keep it. */ + by way of the guide the next page can be either. `refused` and `base` say `h` holds + a change the drawer refused, and what it is counted from (see carry). Read once, by + the page's boot (see arrival). False if the browser would not keep it. */ function handOver(id, h, caughtUp, more) { const n = readNote('sessionStorage', TAB); n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), - marks: (more && more.marks) || {}, refused: !!(more && more.refused) }; + marks: (more && more.marks) || {}, + ...(more && more.refused ? { refused: true, base: more.base } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -6704,11 +6717,11 @@

Your drawers

also carries what the address's own mark would have said: the page's marks from before the save (ours, theirs), by which the drawer has moved on from the address or not, which settings in the design saved are still someone's link's (link), and - whether the drawer refused the save (refused). */ + whether the drawer refused the save, with the page's base (see carry). */ function leaving(id, now, had, link, refusedSave) { const n = readNote('sessionStorage', TAB); n.left = { tool: o.tool, id, was: fingerprint((location.hash || '').replace(/^#/, '')), now, - ours: had, theirs, link: strings(link), refused: !!refusedSave }; + ours: had, theirs, link: strings(link), ...carry(refusedSave, base) }; writeNote('sessionStorage', TAB, n); } function reloaded() { @@ -6749,7 +6762,7 @@

Your drawers

link, untouched since it arrived; `note` the note the page arrived at it by, until the page saves from it. */ const stamp = (h, link, note) => ({ drawerforge: { tool: o.tool, fp: fingerprint(h), - id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, refused: refused() } }); + id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, ...carry(refused(), base) } }); // marks the address the page is at, if it is `h`, leaving it as it is function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; @@ -6759,15 +6772,6 @@

Your drawers

} // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); - /* Whether nothing has saved into drawer `d` since a page that had this tool's mark - `mine` and the other tool's `their`: the drawer has both halves as that page last - knew them. Either can be one the drawer has none of, as long as it has the other: a - drawer that one tool alone has saved into. For a page whose change the drawer - refused, a size the page has that the drawer does not is then that change, not the - drawer moved on. For no other page: one that saves keeps another tab's size where it - has not changed its own (mergeDesign), and sets its mark all the same. */ - const unmoved = (d, mine, their) => (typeof their === 'string' || typeof mine === 'string') && - d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ const movedOn = (d, h, was) => d.marks[o.tool] !== fingerprint(h) && d.marks[o.tool] !== was; @@ -6789,7 +6793,9 @@

Your drawers

this page finds, or another tab did. Showing the old values would be wrong twice over, since a download would use them too, so the page reloads onto the stored ones instead. The note marks that reload as a catch-up, and a catch-up is never followed - by another, so this cannot loop. True if the page is reloading. */ + by another, so this cannot loop. A change of the page's that the drawer refused goes + with it, counted from the base as it moves on with the drawer. True if the page is + reloading. */ function catchUp(h, d) { const stored = sharedOf(d.hash); const moved = [...stored.keys()].filter((k) => o.owns(k) && base.get(k) !== stored.get(k)); @@ -6797,8 +6803,10 @@

Your drawers

const pairs = parsePairs(h) || []; const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); + const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put - if (!handOver(d.id, merged, true, { open: true, marks: marksFor(ours) })) return false; + if (!handOver(d.id, merged, true, more)) return false; go(merged); return true; } @@ -6832,7 +6840,8 @@

Your drawers

half the other page's copy was current with (`seen`), and while the drawer still holds that save, the copy is as new as the drawer or newer: with the browser's storage full, a bin placed before going across was saved nowhere else, and taking - the drawer's half lost it. True if the page is reloading. */ + the drawer's half lost it. A change the drawer refused goes with it, with its base + (see carry): the half reloaded is this tool's alone. True if the page is reloading. */ const half = (k) => o.owns(k) && !SHARED.has(k) && !(o.given || []).includes(k); function restore(h, d, link, seen) { if (seen === d.marks[o.tool]) return false; @@ -6841,7 +6850,8 @@

Your drawers

const stored = mine(d.hash); if (encodePairs(stored) === encodePairs(mine(h))) return false; const merged = encodePairs((parsePairs(h) || []).filter(([k]) => !half(k)).concat(stored)); - if (!handOver(d.id, merged, false, { link, marks: marksFor(d.marks[o.tool]) })) return false; + const more = { link, marks: marksFor(d.marks[o.tool]), ...carry(refused(), base) }; + if (!handOver(d.id, merged, false, more)) return false; go(merged); return true; } @@ -7173,7 +7183,7 @@

Your drawers

if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else if (!(refused() && unmoved(d, ours, theirs))) catchUp(h, d); // nor as this page left it (attach) + else catchUp(h, d); }); return { @@ -7305,21 +7315,23 @@

Your drawers

? strings(gone.link).filter((k) => stored.get(k) === base.get(k)) : null; if (!noted && movedOn(d, arrivedWith, str(came[o.tool])) && reopen(d, held)) return; ours = d.marks[o.tool]; + /* A design holding a change the drawer refused: a hand-over from a page whose + saves into the drawer were refused, or this page reloaded or gone Back to after + its own were. The record says so, with that page's base (see carry), and the + change is counted from that base, as it was there. It is still to be saved, + and a size another tab moved on since that page last saved is caught up below. + Counted from the design, the change was taken for the drawer's, and the + catch-up put the older size back over it. Counted from the drawer's settings, + a size that page had kept from before another tab moved it on was taken for + the change, and its next save wrote it over that tab's. */ + const kept = carried(from); + if (kept) { refusedIn = true; base = kept; } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not when the design holds a change the drawer refused, and the drawer is as the - page that made it left it (unmoved, by the marks it came with): a hand-over from - a page whose saves into the drawer were refused, or this page reloaded or gone - Back to after its own were. Caught up, the drawer's older size came back over - the one changed since, as though another tab had moved the drawer on. The - change is still to be saved, so it is counted from the drawer's settings: from - the design's, a save that went through left the drawer's in place. */ - const asLeft = !!from && from.refused === true && unmoved(d, came[o.tool], came[otherTool]); - if (asLeft) { refusedIn = true; base = sharedOf(d.hash); } - if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; + if (!(noted && next.caughtUp) && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the drawer, and the page came back unsaved. The half goes in with the mark, as @@ -7397,7 +7409,8 @@

Your drawers

} /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ - handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, refused: !!d && refused() }); + handOver(d ? d.id : '', h, false, + { link, marks: d ? marksFor(ours) : {}, ...carry(!!d && refused(), base) }); }, }; } @@ -17738,6 +17751,7 @@

Your drawers

const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } + forgetPasted(); /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ @@ -17831,6 +17845,7 @@

Your drawers

} saveLocal(prev); writeKey(LINKED_KEY, prevLinked); + forgetPasted(); leaveFor(location.href.split('#')[0]); // a bare visit restores it, and says so } function tryAnyway() { @@ -17925,6 +17940,13 @@

Your drawers

r.at === DRAWERS.fingerprint(location.hash); } catch (err) { return false; } }; +/* Start fresh and Put back each leave that save behind, and the record goes with them. + Kept past Start fresh, Back to a page at the same address offered the save as the later + layout, though Start fresh came after it. Either button can be pressed when the boot + stopped short of this line, so whatever goes wrong here is let go. */ +function forgetPasted() { + try { sessionStorage.removeItem(PASTED_KEY); } catch (err) { /* nothing kept to forget */ } +} /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. diff --git a/index.html b/index.html index e984776..3b83115 100644 --- a/index.html +++ b/index.html @@ -6449,20 +6449,33 @@

Your drawers

if (n[o.tool] !== id) { n[o.tool] = id; writeNote(area, key, n); } } } + /* For a page whose design holds a change the drawer refused (`on`), what the page that + takes the design on needs to know of it, in a note, a mark or a raced reload's + record: that it does, and the page's base `b`, its shared keys as it last saved them + or arrived with them, which its changes are counted from (see attach). Nothing for + any other page, whose base is the design it hands on. */ + const carry = (on, b) => (on && b ? { refused: true, base: [...b] } : {}); + // the base a note, mark or record carries (see carry), or null for one that carries none + function carried(r) { + if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; + return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && + typeof p[1] === 'string' && SHARED.has(p[0]))); + } /* The note for the next page to load in this tab: the drawer ('' for none), and the fingerprint of the design that page will arrive with. `link` lists the drawer, bed and infill settings in it that are still someone's link's, not yours (the tool's ui.js works that out); `open` says a drawer was opened from the list, or caught up, rather than handed over. `marks` gives, for each tool, its mark in the drawer when its settings in `h` were current, where that is known (see restore): by tool, since - by way of the guide the next page can be either. `refused` says `h` holds a change - the drawer refused (see attach). Read once, by the page's boot (see arrival). False - if the browser would not keep it. */ + by way of the guide the next page can be either. `refused` and `base` say `h` holds + a change the drawer refused, and what it is counted from (see carry). Read once, by + the page's boot (see arrival). False if the browser would not keep it. */ function handOver(id, h, caughtUp, more) { const n = readNote('sessionStorage', TAB); n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), - marks: (more && more.marks) || {}, refused: !!(more && more.refused) }; + marks: (more && more.marks) || {}, + ...(more && more.refused ? { refused: true, base: more.base } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -6491,11 +6504,11 @@

Your drawers

also carries what the address's own mark would have said: the page's marks from before the save (ours, theirs), by which the drawer has moved on from the address or not, which settings in the design saved are still someone's link's (link), and - whether the drawer refused the save (refused). */ + whether the drawer refused the save, with the page's base (see carry). */ function leaving(id, now, had, link, refusedSave) { const n = readNote('sessionStorage', TAB); n.left = { tool: o.tool, id, was: fingerprint((location.hash || '').replace(/^#/, '')), now, - ours: had, theirs, link: strings(link), refused: !!refusedSave }; + ours: had, theirs, link: strings(link), ...carry(refusedSave, base) }; writeNote('sessionStorage', TAB, n); } function reloaded() { @@ -6536,7 +6549,7 @@

Your drawers

link, untouched since it arrived; `note` the note the page arrived at it by, until the page saves from it. */ const stamp = (h, link, note) => ({ drawerforge: { tool: o.tool, fp: fingerprint(h), - id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, refused: refused() } }); + id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, ...carry(refused(), base) } }); // marks the address the page is at, if it is `h`, leaving it as it is function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; @@ -6546,15 +6559,6 @@

Your drawers

} // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); - /* Whether nothing has saved into drawer `d` since a page that had this tool's mark - `mine` and the other tool's `their`: the drawer has both halves as that page last - knew them. Either can be one the drawer has none of, as long as it has the other: a - drawer that one tool alone has saved into. For a page whose change the drawer - refused, a size the page has that the drawer does not is then that change, not the - drawer moved on. For no other page: one that saves keeps another tab's size where it - has not changed its own (mergeDesign), and sets its mark all the same. */ - const unmoved = (d, mine, their) => (typeof their === 'string' || typeof mine === 'string') && - d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ const movedOn = (d, h, was) => d.marks[o.tool] !== fingerprint(h) && d.marks[o.tool] !== was; @@ -6576,7 +6580,9 @@

Your drawers

this page finds, or another tab did. Showing the old values would be wrong twice over, since a download would use them too, so the page reloads onto the stored ones instead. The note marks that reload as a catch-up, and a catch-up is never followed - by another, so this cannot loop. True if the page is reloading. */ + by another, so this cannot loop. A change of the page's that the drawer refused goes + with it, counted from the base as it moves on with the drawer. True if the page is + reloading. */ function catchUp(h, d) { const stored = sharedOf(d.hash); const moved = [...stored.keys()].filter((k) => o.owns(k) && base.get(k) !== stored.get(k)); @@ -6584,8 +6590,10 @@

Your drawers

const pairs = parsePairs(h) || []; const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); + const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put - if (!handOver(d.id, merged, true, { open: true, marks: marksFor(ours) })) return false; + if (!handOver(d.id, merged, true, more)) return false; go(merged); return true; } @@ -6619,7 +6627,8 @@

Your drawers

half the other page's copy was current with (`seen`), and while the drawer still holds that save, the copy is as new as the drawer or newer: with the browser's storage full, a bin placed before going across was saved nowhere else, and taking - the drawer's half lost it. True if the page is reloading. */ + the drawer's half lost it. A change the drawer refused goes with it, with its base + (see carry): the half reloaded is this tool's alone. True if the page is reloading. */ const half = (k) => o.owns(k) && !SHARED.has(k) && !(o.given || []).includes(k); function restore(h, d, link, seen) { if (seen === d.marks[o.tool]) return false; @@ -6628,7 +6637,8 @@

Your drawers

const stored = mine(d.hash); if (encodePairs(stored) === encodePairs(mine(h))) return false; const merged = encodePairs((parsePairs(h) || []).filter(([k]) => !half(k)).concat(stored)); - if (!handOver(d.id, merged, false, { link, marks: marksFor(d.marks[o.tool]) })) return false; + const more = { link, marks: marksFor(d.marks[o.tool]), ...carry(refused(), base) }; + if (!handOver(d.id, merged, false, more)) return false; go(merged); return true; } @@ -6960,7 +6970,7 @@

Your drawers

if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else if (!(refused() && unmoved(d, ours, theirs))) catchUp(h, d); // nor as this page left it (attach) + else catchUp(h, d); }); return { @@ -7092,21 +7102,23 @@

Your drawers

? strings(gone.link).filter((k) => stored.get(k) === base.get(k)) : null; if (!noted && movedOn(d, arrivedWith, str(came[o.tool])) && reopen(d, held)) return; ours = d.marks[o.tool]; + /* A design holding a change the drawer refused: a hand-over from a page whose + saves into the drawer were refused, or this page reloaded or gone Back to after + its own were. The record says so, with that page's base (see carry), and the + change is counted from that base, as it was there. It is still to be saved, + and a size another tab moved on since that page last saved is caught up below. + Counted from the design, the change was taken for the drawer's, and the + catch-up put the older size back over it. Counted from the drawer's settings, + a size that page had kept from before another tab moved it on was taken for + the change, and its next save wrote it over that tab's. */ + const kept = carried(from); + if (kept) { refusedIn = true; base = kept; } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not when the design holds a change the drawer refused, and the drawer is as the - page that made it left it (unmoved, by the marks it came with): a hand-over from - a page whose saves into the drawer were refused, or this page reloaded or gone - Back to after its own were. Caught up, the drawer's older size came back over - the one changed since, as though another tab had moved the drawer on. The - change is still to be saved, so it is counted from the drawer's settings: from - the design's, a save that went through left the drawer's in place. */ - const asLeft = !!from && from.refused === true && unmoved(d, came[o.tool], came[otherTool]); - if (asLeft) { refusedIn = true; base = sharedOf(d.hash); } - if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; + if (!(noted && next.caughtUp) && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the drawer, and the page came back unsaved. The half goes in with the mark, as @@ -7184,7 +7196,8 @@

Your drawers

} /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ - handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, refused: !!d && refused() }); + handOver(d ? d.id : '', h, false, + { link, marks: d ? marksFor(ours) : {}, ...carry(!!d && refused(), base) }); }, }; } @@ -9326,6 +9339,7 @@

Your drawers

const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } + forgetPasted(); /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ @@ -9419,6 +9433,7 @@

Your drawers

} saveLocal(prev); writeKey(LINKED_KEY, prevLinked); + forgetPasted(); leaveFor(location.href.split('#')[0]); // a bare visit restores it, and says so } function tryAnyway() { @@ -9533,6 +9548,13 @@

Your drawers

r.at === DRAWERS.fingerprint(location.hash); } catch (err) { return false; } }; +/* Start fresh and Put back each leave that save behind, and the record goes with them. + Kept past Start fresh, Back to a page at the same address offered the save as the later + layout, though Start fresh came after it. Either button can be pressed when the boot + stopped short of this line, so whatever goes wrong here is let go. */ +function forgetPasted() { + try { sessionStorage.removeItem(PASTED_KEY); } catch (err) { /* nothing kept to forget */ } +} /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. diff --git a/src/bins/ui.js b/src/bins/ui.js index 5c8e892..5cac230 100644 --- a/src/bins/ui.js +++ b/src/bins/ui.js @@ -5357,6 +5357,7 @@ const readLocal = () => { try { return localStorage.getItem(SAVE_KEY) || ''; } c const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } + forgetPasted(); /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ @@ -5450,6 +5451,7 @@ function putBack() { } saveLocal(prev); writeKey(LINKED_KEY, prevLinked); + forgetPasted(); leaveFor(location.href.split('#')[0]); // a bare visit restores it, and says so } function tryAnyway() { @@ -5544,6 +5546,13 @@ const pastedFrom = (prev) => { r.at === DRAWERS.fingerprint(location.hash); } catch (err) { return false; } }; +/* Start fresh and Put back each leave that save behind, and the record goes with them. + Kept past Start fresh, Back to a page at the same address offered the save as the later + layout, though Start fresh came after it. Either button can be pressed when the boot + stopped short of this line, so whatever goes wrong here is let go. */ +function forgetPasted() { + try { sessionStorage.removeItem(PASTED_KEY); } catch (err) { /* nothing kept to forget */ } +} /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. diff --git a/src/shared-ui/drawers.js b/src/shared-ui/drawers.js index b5e4915..294f61e 100644 --- a/src/shared-ui/drawers.js +++ b/src/shared-ui/drawers.js @@ -355,20 +355,33 @@ const DRAWERS = (function () { if (n[o.tool] !== id) { n[o.tool] = id; writeNote(area, key, n); } } } + /* For a page whose design holds a change the drawer refused (`on`), what the page that + takes the design on needs to know of it, in a note, a mark or a raced reload's + record: that it does, and the page's base `b`, its shared keys as it last saved them + or arrived with them, which its changes are counted from (see attach). Nothing for + any other page, whose base is the design it hands on. */ + const carry = (on, b) => (on && b ? { refused: true, base: [...b] } : {}); + // the base a note, mark or record carries (see carry), or null for one that carries none + function carried(r) { + if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; + return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && + typeof p[1] === 'string' && SHARED.has(p[0]))); + } /* The note for the next page to load in this tab: the drawer ('' for none), and the fingerprint of the design that page will arrive with. `link` lists the drawer, bed and infill settings in it that are still someone's link's, not yours (the tool's ui.js works that out); `open` says a drawer was opened from the list, or caught up, rather than handed over. `marks` gives, for each tool, its mark in the drawer when its settings in `h` were current, where that is known (see restore): by tool, since - by way of the guide the next page can be either. `refused` says `h` holds a change - the drawer refused (see attach). Read once, by the page's boot (see arrival). False - if the browser would not keep it. */ + by way of the guide the next page can be either. `refused` and `base` say `h` holds + a change the drawer refused, and what it is counted from (see carry). Read once, by + the page's boot (see arrival). False if the browser would not keep it. */ function handOver(id, h, caughtUp, more) { const n = readNote('sessionStorage', TAB); n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), - marks: (more && more.marks) || {}, refused: !!(more && more.refused) }; + marks: (more && more.marks) || {}, + ...(more && more.refused ? { refused: true, base: more.base } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -397,11 +410,11 @@ const DRAWERS = (function () { also carries what the address's own mark would have said: the page's marks from before the save (ours, theirs), by which the drawer has moved on from the address or not, which settings in the design saved are still someone's link's (link), and - whether the drawer refused the save (refused). */ + whether the drawer refused the save, with the page's base (see carry). */ function leaving(id, now, had, link, refusedSave) { const n = readNote('sessionStorage', TAB); n.left = { tool: o.tool, id, was: fingerprint((location.hash || '').replace(/^#/, '')), now, - ours: had, theirs, link: strings(link), refused: !!refusedSave }; + ours: had, theirs, link: strings(link), ...carry(refusedSave, base) }; writeNote('sessionStorage', TAB, n); } function reloaded() { @@ -442,7 +455,7 @@ const DRAWERS = (function () { link, untouched since it arrived; `note` the note the page arrived at it by, until the page saves from it. */ const stamp = (h, link, note) => ({ drawerforge: { tool: o.tool, fp: fingerprint(h), - id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, refused: refused() } }); + id: attached || '', link: !!link, ...(note ? { note } : {}), ours, theirs, ...carry(refused(), base) } }); // marks the address the page is at, if it is `h`, leaving it as it is function markHere(h, link, note) { if ((location.hash || '').replace(/^#/, '') !== h) return; @@ -452,15 +465,6 @@ const DRAWERS = (function () { } // the marks for a note: this tool's `mine`, and the other's as this page has it const marksFor = (mine) => ({ [o.tool]: mine, [otherTool]: theirs }); - /* Whether nothing has saved into drawer `d` since a page that had this tool's mark - `mine` and the other tool's `their`: the drawer has both halves as that page last - knew them. Either can be one the drawer has none of, as long as it has the other: a - drawer that one tool alone has saved into. For a page whose change the drawer - refused, a size the page has that the drawer does not is then that change, not the - drawer moved on. For no other page: one that saves keeps another tab's size where it - has not changed its own (mergeDesign), and sets its mark all the same. */ - const unmoved = (d, mine, their) => (typeof their === 'string' || typeof mine === 'string') && - d.marks[otherTool] === their && d.marks[o.tool] === mine; /* Whether the drawer has saved this tool's half since the page's copy `h`, which was current with this tool's mark `was`: it holds neither that design nor that save. */ const movedOn = (d, h, was) => d.marks[o.tool] !== fingerprint(h) && d.marks[o.tool] !== was; @@ -482,7 +486,9 @@ const DRAWERS = (function () { this page finds, or another tab did. Showing the old values would be wrong twice over, since a download would use them too, so the page reloads onto the stored ones instead. The note marks that reload as a catch-up, and a catch-up is never followed - by another, so this cannot loop. True if the page is reloading. */ + by another, so this cannot loop. A change of the page's that the drawer refused goes + with it, counted from the base as it moves on with the drawer. True if the page is + reloading. */ function catchUp(h, d) { const stored = sharedOf(d.hash); const moved = [...stored.keys()].filter((k) => o.owns(k) && base.get(k) !== stored.get(k)); @@ -490,8 +496,10 @@ const DRAWERS = (function () { const pairs = parsePairs(h) || []; const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); + const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put - if (!handOver(d.id, merged, true, { open: true, marks: marksFor(ours) })) return false; + if (!handOver(d.id, merged, true, more)) return false; go(merged); return true; } @@ -525,7 +533,8 @@ const DRAWERS = (function () { half the other page's copy was current with (`seen`), and while the drawer still holds that save, the copy is as new as the drawer or newer: with the browser's storage full, a bin placed before going across was saved nowhere else, and taking - the drawer's half lost it. True if the page is reloading. */ + the drawer's half lost it. A change the drawer refused goes with it, with its base + (see carry): the half reloaded is this tool's alone. True if the page is reloading. */ const half = (k) => o.owns(k) && !SHARED.has(k) && !(o.given || []).includes(k); function restore(h, d, link, seen) { if (seen === d.marks[o.tool]) return false; @@ -534,7 +543,8 @@ const DRAWERS = (function () { const stored = mine(d.hash); if (encodePairs(stored) === encodePairs(mine(h))) return false; const merged = encodePairs((parsePairs(h) || []).filter(([k]) => !half(k)).concat(stored)); - if (!handOver(d.id, merged, false, { link, marks: marksFor(d.marks[o.tool]) })) return false; + const more = { link, marks: marksFor(d.marks[o.tool]), ...carry(refused(), base) }; + if (!handOver(d.id, merged, false, more)) return false; go(merged); return true; } @@ -866,7 +876,7 @@ const DRAWERS = (function () { if (!d) return; const h = o.design(); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it - else if (!(refused() && unmoved(d, ours, theirs))) catchUp(h, d); // nor as this page left it (attach) + else catchUp(h, d); }); return { @@ -998,21 +1008,23 @@ const DRAWERS = (function () { ? strings(gone.link).filter((k) => stored.get(k) === base.get(k)) : null; if (!noted && movedOn(d, arrivedWith, str(came[o.tool])) && reopen(d, held)) return; ours = d.marks[o.tool]; + /* A design holding a change the drawer refused: a hand-over from a page whose + saves into the drawer were refused, or this page reloaded or gone Back to after + its own were. The record says so, with that page's base (see carry), and the + change is counted from that base, as it was there. It is still to be saved, + and a size another tab moved on since that page last saved is caught up below. + Counted from the design, the change was taken for the drawer's, and the + catch-up put the older size back over it. Counted from the drawer's settings, + a size that page had kept from before another tab moved it on was taken for + the change, and its next save wrote it over that tab's. */ + const kept = carried(from); + if (kept) { refusedIn = true; base = kept; } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ if (noted && next.open !== true && typeof d.marks[o.tool] === 'string' && restore(arrivedWith, d, next.link, str(came[o.tool]))) return; - /* Not when the design holds a change the drawer refused, and the drawer is as the - page that made it left it (unmoved, by the marks it came with): a hand-over from - a page whose saves into the drawer were refused, or this page reloaded or gone - Back to after its own were. Caught up, the drawer's older size came back over - the one changed since, as though another tab had moved the drawer on. The - change is still to be saved, so it is counted from the drawer's settings: from - the design's, a save that went through left the drawer's in place. */ - const asLeft = !!from && from.refused === true && unmoved(d, came[o.tool], came[otherTool]); - if (asLeft) { refusedIn = true; base = sharedOf(d.hash); } - if (!(noted && next.caughtUp) && !asLeft && catchUp(arrivedWith, d)) return; + if (!(noted && next.caughtUp) && catchUp(arrivedWith, d)) return; /* A hand-over is written down now rather than at the page's first save, which is 400 ms off: a reload before it found no record of this tool in the drawer, and the page came back unsaved. The half goes in with the mark, as @@ -1090,7 +1102,8 @@ const DRAWERS = (function () { } /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ - handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, refused: !!d && refused() }); + handOver(d ? d.id : '', h, false, + { link, marks: d ? marksFor(ours) : {}, ...carry(!!d && refused(), base) }); }, }; } diff --git a/src/ui.js b/src/ui.js index 706d913..62af0ac 100644 --- a/src/ui.js +++ b/src/ui.js @@ -2129,6 +2129,7 @@ const readLocal = () => { try { return localStorage.getItem(SAVE_KEY) || ''; } c const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } + forgetPasted(); /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ @@ -2222,6 +2223,7 @@ function putBack() { } saveLocal(prev); writeKey(LINKED_KEY, prevLinked); + forgetPasted(); leaveFor(location.href.split('#')[0]); // a bare visit restores it, and says so } function tryAnyway() { @@ -2336,6 +2338,13 @@ const pastedFrom = (prev) => { r.at === DRAWERS.fingerprint(location.hash); } catch (err) { return false; } }; +/* Start fresh and Put back each leave that save behind, and the record goes with them. + Kept past Start fresh, Back to a page at the same address offered the save as the later + layout, though Start fresh came after it. Either button can be pressed when the boot + stopped short of this line, so whatever goes wrong here is let go. */ +function forgetPasted() { + try { sessionStorage.removeItem(PASTED_KEY); } catch (err) { /* nothing kept to forget */ } +} /* A reload takes the address as it stands when it starts, and the page runs on until the new one arrives. A save still waiting would land in that gap and record in the saved drawer a design the reloaded page did not arrive with, and the page came back unsaved. diff --git a/sw.js b/sw.js index 41ce3a0..ebd173e 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "c8675f90b493"; +const VERSION = "21a4787a61cc"; const FILES = [ "./", "bins/", diff --git a/test/ui/drawers.spec.js b/test/ui/drawers.spec.js index d08076f..9432d6f 100644 --- a/test/ui/drawers.spec.js +++ b/test/ui/drawers.spec.js @@ -245,7 +245,8 @@ for (const tool of ['bins', 'plates']) { and the drawer still has the save before both. The reload took the drawer for newer than the page and reopened onto that older save, and the page's next save wrote it over the change everywhere. Now it keeps the address, and the later change, which the reload - never saw, is set aside. */ + never saw, is set aside. The change before has a size in it too, which the next save + writes: counted from the design the reload took, it was taken for the drawer's. */ for (const tool of ['bins', 'plates']) { const ready = tool === 'bins' ? binsReady : platesReady; const [field, key, before, refused, raced] = tool === 'bins' ? ['gap', 'bgap', '6', '4', '7'] @@ -265,6 +266,7 @@ for (const tool of ['bins', 'plates']) { return write.apply(this, arguments); }; }); + await H.setField(page, 'drawerW', '410'); if (tool === 'bins') await H.setField(page, field, refused); else await page.selectOption('#' + field, refused); await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); @@ -284,9 +286,10 @@ for (const tool of ['bins', 'plates']) { await ready(page); await expect(page.locator('#drawerName')).toHaveText('Kitchen'); await expect(page.locator('#' + field)).toHaveValue(refused); + await expect(page.locator('#drawerW')).toHaveValue('410'); await expect(page.locator('#setAsideMsg')).toHaveText('The layout you had here is set aside.'); await settle(page); - expect((await stored(page)).Kitchen[key], 'the drawer takes it now').toBe(refused); + expect((await stored(page)).Kitchen, 'the drawer takes it now').toMatchObject({ [key]: refused, w: '410' }); await page.click('#putBack'); await expect.poll(() => page.inputValue('#' + field).catch(() => ''), { message: 'and Put back brings the later change', timeout: 20000 }).toBe(raced); @@ -985,10 +988,12 @@ for (const tool of ['plates', 'bins']) { // a change to a page's own half alone: the field, the value it is changed to, and its key const ownChange = (tool) => (tool === 'bins' ? ['gap', '4', 'bgap'] : ['connector', 'hclip', 'cn']); -async function changeOwn(page, tool) { +// and a second, no longer, so the page's own save of it still fits with the storage full +const ownChange2 = (tool) => (tool === 'bins' ? '5' : 'snap'); +async function changeOwn(page, tool, to) { const [field, v] = ownChange(tool); - if (tool === 'bins') await H.setField(page, field, v); - else await page.selectOption('#' + field, v); + if (tool === 'bins') await H.setField(page, field, to || v); + else await page.selectOption('#' + field, to || v); } // Kitchen at 400, with both pages' marks in it, open in this tab on `tool` async function kitchenOn(page, tool) { @@ -1001,18 +1006,31 @@ async function kitchenOn(page, tool) { await settle(page); return errors; } -// another tab of `tool` on Kitchen moves its size to 420, and changes that page's own half too -async function moveKitchen(context, tool, errors, own) { +// another tab of `tool`, on Kitchen, makes `change` to it +async function inOtherTab(context, tool, errors, change) { const other = await context.newPage(); other.on('pageerror', (e) => errors.push(String(e))); await other.goto(base + (tool === 'bins' ? 'bins/' : '')); await (tool === 'bins' ? binsReady : platesReady)(other); await expect(other.locator('#drawerName')).toHaveText('Kitchen'); - await H.setField(other, 'drawerW', '420'); - if (own) await changeOwn(other, tool); + await change(other); await settle(other); await other.close(); } +// moves its size to 420, and changes that page's own half too +async function moveKitchen(context, tool, errors, own) { + await inOtherTab(context, tool, errors, async (other) => { + await H.setField(other, 'drawerW', '420'); + if (own) await changeOwn(other, tool); + }); +} +// with the storage full from here on, the page's size changed to 410, which the drawer refuses +async function refuseSize(page) { + await page.evaluate(() => sessionStorage.setItem('full', '1')); + await H.setField(page, 'drawerW', '410'); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + await settle(page); +} /* And when the first tab saves a change of its own before it comes back. Its save keeps the other tab's size, which it never changed (see mergeDesign in drawers.js), and sets its @@ -1072,6 +1090,123 @@ for (const tool of ['plates', 'bins']) { }); } +/* As above, with the storage full by the time the first tab comes back: its save that went + through kept the other tab's size, and a change after it was refused, so the address is + the one copy of that change. Counted from the drawer's settings, which the page took for + its own when it came back, the change took in the old size the page still had, and once + there was room it wrote that over the other tab's. It is counted from what the page last + saved, and the size the other tab moved on since is caught up: reloaded, and handed over, + then the change taken back to the first page. */ +for (const tool of ['plates', 'bins']) for (const how of ['reloaded', 'handed over']) { + test(`a ${tool} tab whose save kept another tab's size, then had a change refused, catches up, ${how}`, + async ({ page, context }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + const now = how === 'reloaded' ? tool : there; + const [field, v, key] = ownChange(tool); + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await moveKitchen(context, tool, errors, false); + await changeOwn(page, tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'its change is saved, beside the other tab\'s size', timeout: 20000 }) + .toMatchObject({ w: '420', [key]: v }); + await page.evaluate(() => sessionStorage.setItem('full', '1')); + await changeOwn(page, tool, ownChange2(tool)); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + await settle(page); + + if (how === 'reloaded') await page.reload(); + else await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await expect.poll(() => page.inputValue('#drawerW').catch(() => ''), + { message: 'caught up with the other tab', timeout: 20000 }).toBe('420'); + await (now === 'bins' ? binsReady : platesReady)(page); + if (how === 'reloaded') { + expect(await page.inputValue('#' + field), 'with the change').toBe(ownChange2(tool)); + } + await settle(page); + // with room again, the next change saves into the drawer, and keeps the other tab's size + await page.evaluate(() => sessionStorage.setItem('full', '0')); + const [, first, nowKey] = ownChange(now); + const next = now !== tool ? first : tool === 'bins' ? '6' : 'bowtie'; + await changeOwn(page, now, next); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'the next change is saved', timeout: 20000 }).toMatchObject({ [nowKey]: next }); + expect((await stored(page)).Kitchen.w, 'the other tab\'s size').toBe('420'); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + if (how === 'handed over') { + await (tool === 'bins' ? toBins : toPlates)(page); + expect(await page.inputValue('#' + field), 'the change, back where it was made').toBe(ownChange2(tool)); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'and saved there', timeout: 20000 }).toMatchObject({ w: '420', [key]: ownChange2(tool) }); + } + expect(errors).toEqual([]); + }); +} + +/* With the storage full, a size changed and refused, then another tab on the other page + changes the depth, and the page comes back, reloaded or from the back-forward cache. It + catches up the depth and keeps the size, still to be saved. Reloaded, the catch-up was + counted from the design, so the size was taken for the drawer's and went back; from the + cache, the reload it took lost that the size was refused, and the next save kept the + drawer's old one. */ +for (const tool of ['plates', 'bins']) for (const how of ['reloaded', 'shown again from the cache']) { + test(`with storage full, a size changed on ${tool} outlives catching up another tab's depth, ${how}`, + async ({ page, context }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + await inOtherTab(context, there, errors, (other) => H.setField(other, 'drawerD', '555')); + + if (how === 'reloaded') await page.reload(); + else await page.evaluate(() => dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true }))); + await expect.poll(() => page.inputValue('#drawerD').catch(() => ''), + { message: 'caught up with the other tab', timeout: 20000 }).toBe('555'); + await (tool === 'bins' ? binsReady : platesReady)(page); + expect(await page.inputValue('#drawerW'), 'with the size changed').toBe('410'); + await settle(page); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, tool); + const [, v, key] = ownChange(tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'with room again, the next change saves the size too', timeout: 20000 }) + .toMatchObject({ w: '410', d: '555', [key]: v }); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + expect(errors).toEqual([]); + }); +} + +/* And handed over, onto a page whose own half another tab has moved on, and nothing else: + the page there takes that half from the drawer (restore), and the reload that takes it + lost that the size was refused, so the drawer's older size came back over it. */ +for (const tool of ['plates', 'bins']) { + test(`with storage full, a size changed on ${tool} is the size on the other page as it takes its half ` + + 'from the drawer', async ({ page, context }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + const [field, v, key] = ownChange(there); + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await inOtherTab(context, there, errors, (other) => changeOwn(other, there)); + await refuseSize(page); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + for (const step of ['handed over', 'reloaded']) { + if (step === 'reloaded') await page.reload(); + await expect.poll(() => page.inputValue('#' + field).catch(() => ''), + { message: `${step}, with the other tab's change`, timeout: 20000 }).toBe(v); + await (there === 'bins' ? binsReady : platesReady)(page); + expect(await page.inputValue('#drawerW'), step).toBe('410'); + await settle(page); + } + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, there, ownChange2(there)); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'with room again, the next change saves the size too', timeout: 20000 }) + .toMatchObject({ w: '410', [key]: ownChange2(there) }); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + expect(errors).toEqual([]); + }); +} + /* Each page saves its whole half of the drawer, so one whose half is older than the drawer's would write it back: a reload of a tab after another tab of the same tool changed the drawer, or Back to a page of yours that a later page has moved on. Both diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index f95cd03..3fd75f7 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -1058,3 +1058,46 @@ for (const tool of ['plates', 'bins']) { expect(thrown.filter((e) => !/boom/.test(e))).toEqual([]); }); } + +/* And it ends the offer of an edit a pasted link set aside (see the paste tests above). The + edit was offered on Back, and left; then a bare visit, and Start fresh there. The record + of the edit's save stayed, and the fresh page is at the address it names: another link + pasted over it, then Back to it, offered the edit as the later layout, and Put back + brought it, though Start fresh came after it. Both pages are covered. */ +for (const tool of ['plates', 'bins']) { + test(`Start fresh on ${tool} ends the offer of an edit a pasted link set aside`, async ({ page }) => { + const errors = watch(page); + const url = tool === 'bins' ? binsUrl() : platesUrl(); + await arrive(page, url); + await saved(page); + const paste = async (h, edit) => { + await Promise.all([page.waitForEvent('load'), page.evaluate(([h, edit]) => { + if (edit) { + const e = document.getElementById('drawerW'); + e.value = edit; + e.dispatchEvent(new Event('input', { bubbles: true })); + e.dispatchEvent(new Event('change', { bubbles: true })); + if (typeof landEdit === 'function') landEdit(); + } + location.hash = h; + if (edit) saveNow(); + }, [h, edit])]); + await ready(page); + }; + await paste('#w=333&d=444&v=2', '412'); + await page.goBack(); + await ready(page); + await expect(page.locator('#setAsideMsg')) + .toHaveText('This page went back to an earlier layout of yours. The later one is set aside.'); + + await arrive(page, url); + await clickAndLoad(page, '#startFresh'); + await saved(page); + await paste('#w=355&d=466&v=2'); + await page.goBack(); + await ready(page); + expect(await page.inputValue('#drawerW'), 'the fresh page').not.toBe('355'); + await expect(page.locator('#setAside'), 'nothing is offered').toBeHidden(); + expect(errors).toEqual([]); + }); +} From 95ba00bc5e2fc94787a79d1ab5aec2fcf58f4b8a Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 20:51:41 +0000 Subject: [PATCH 7/9] Count a refusal as done once a save carrying it goes through A page holding a size the drawer refused hands its base on with an id for the refusal. A save that goes through while the page holds it records the id for the tab, and a mark, note or record whose refusal has landed counts for nothing. Back to the page after its size was saved on the other page and set back there now shows the size set back and leaves the drawer at it, rather than saving the refused size again over it. Also: a catch-up that changes nothing in the page takes the drawer's settings as its base without reloading; PASTED_KEY is declared with SAVE_KEY, so Start fresh clears it even when the boot stopped short; and a page left by Start fresh and shown again from the back-forward cache saves again, as it does loaded. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 75 +++++++++++++++++++++++++++----- index.html | 75 +++++++++++++++++++++++++++----- src/bins/ui.js | 16 +++++-- src/shared-ui/drawers.js | 59 +++++++++++++++++++++---- src/ui.js | 16 +++++-- sw.js | 2 +- test/ui/drawers.spec.js | 82 +++++++++++++++++++++++++++++++++++ test/ui/hostile-links.spec.js | 57 ++++++++++++++++++++++++ 8 files changed, 342 insertions(+), 40 deletions(-) diff --git a/bins/index.html b/bins/index.html index fcf4e70..0701389 100644 --- a/bins/index.html +++ b/bins/index.html @@ -7211,6 +7211,8 @@

Your drawers

nothing it has saved since has gone through: the change is still the address's alone. */ let refusedIn = false; const refused = () => failing || refusedIn; + // the refusal the page holds, by the id its records hand on (see carry and land), or '' + let rid = ''; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -7239,12 +7241,34 @@

Your drawers

/* For a page whose design holds a change the drawer refused (`on`), what the page that takes the design on needs to know of it, in a note, a mark or a raced reload's record: that it does, and the page's base `b`, its shared keys as it last saved them - or arrived with them, which its changes are counted from (see attach). Nothing for - any other page, whose base is the design it hands on. */ - const carry = (on, b) => (on && b ? { refused: true, base: [...b] } : {}); - // the base a note, mark or record carries (see carry), or null for one that carries none + or arrived with them, which its changes are counted from (see attach). With an id + for the refusal, the same in every record of it, so that once it lands they can all + be told apart from one still to save (see land). Nothing for any other page, whose + base is the design it hands on. */ + const carry = (on, b) => { + if (!(on && b)) return {}; + rid = rid || Math.random().toString(36).slice(2, 10); + return { refused: true, base: [...b], rid }; + }; + /* A save that went through while the page held a refusal: the change is in the drawer + now, or was changed again before it got there, and either way every record of the + refusal is older than the drawer. Kept for this tab, the last few, so Back to one of + them is a page of yours come back, caught up with the drawer. Counted from its base, + a size set back on the other page since was taken for the change, and saved over the + one set back. */ + function land() { + if (!rid) return; + const n = readNote('sessionStorage', TAB); + const done = strings(n.landed); + if (!done.includes(rid)) { n.landed = done.concat(rid).slice(-32); writeNote('sessionStorage', TAB, n); } + rid = ''; + } + const landed = (id) => strings(readNote('sessionStorage', TAB).landed).includes(id); + /* The base a note, mark or record carries (see carry), or null for one that carries + none, or whose refusal has landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; + if (typeof r.rid === 'string' && landed(r.rid)) return null; return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); } @@ -7262,7 +7286,7 @@

Your drawers

n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rid: more.rid } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -7378,6 +7402,8 @@

Your drawers

const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + // the page has the drawer's settings already: they are its base now, with nothing to reload + if (merged === h) { base = caught; return false; } const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; @@ -7756,6 +7782,11 @@

Your drawers

const d = find(loadAll(), attached); if (!d) return; const h = o.design(); + // a refusal that has landed since, from a later page: this one is as any page come back + if (refused() && rid && landed(rid)) { + failing = false; refusedIn = false; rid = ''; base = sharedOf(h); + paintBar(); + } if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -7847,6 +7878,7 @@

Your drawers

ours = undefined; theirs = undefined; refusedIn = false; + rid = ''; const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -7899,7 +7931,7 @@

Your drawers

a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; } + if (kept) { refusedIn = true; base = kept; rid = typeof from.rid === 'string' ? from.rid : ''; } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -7914,7 +7946,10 @@

Your drawers

if (noted) { d.hash = mergeDesign(d.hash, arrivedWith, o.owns, base); d.marks[o.tool] = fp; - if (saveAll(s)) { ours = fp; base = sharedOf(arrivedWith); refusedIn = false; } + if (saveAll(s)) { + if (refused()) land(); + ours = fp; base = sharedOf(arrivedWith); refusedIn = false; + } savedInto(d.id); } } @@ -7947,7 +7982,10 @@

Your drawers

d.marks[o.tool] = fingerprint(h); d.saved = Date.now(); const ok = saveAll(s); - if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; } + if (ok) { + if (refused()) land(); + base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's change, past the save the page last made into it (had): a reload that raced this @@ -7979,7 +8017,10 @@

Your drawers

if (d && typeof mine === 'string' && !behind(d, mine)) { d.hash = mergeDesign(d.hash, h, o.owns, base); d.saved = Date.now(); - if (saveAll(s)) base = sharedOf(h); + if (saveAll(s)) { + if (refused()) land(); + base = sharedOf(h); + } } /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ @@ -18293,6 +18334,8 @@

Your drawers

* stops that being your problem. */ let hashSaveT = 0, hashReady = false; +// saves were on when Start fresh left the page (see startFresh) +let freshLeft = false; /* Kept on this browser, so the work survives arriving without a link. * * The address bar already carries the design and a refresh already restores it. What it @@ -18313,6 +18356,8 @@

Your drawers

* when it has done one and offers a way back. */ const SAVE_KEY = 'drawerforge:bins:v1'; +// this tab's record of a save a pasted link set aside (see pastedFrom), declared here for Start fresh +const PASTED_KEY = SAVE_KEY + ':pasted'; const saveLocal = (h) => { try { localStorage.setItem(SAVE_KEY, h); } catch (err) { /* private mode, or the quota is full — losing the save is not worth @@ -18326,6 +18371,7 @@

Your drawers

function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); + freshLeft = hashReady; /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ @@ -18335,6 +18381,12 @@

Your drawers

also take away the button that gets you out of it, or stop the next link working. What leaveFor stops, hashReady and hashSaveT, is declared above, so it works from here too. */ $('startFresh').addEventListener('click', startFresh); +/* Left by Start fresh and shown again from the back-forward cache, as Back from the fresh + page can be in some browsers: the page is yours again, and saves as it did before, as it + does loaded. Not after Put back or Try anyway, which leave by leaveFor too: loaded, that + page is a link again and sets your layout aside first, and from the cache its first save + would go over the layout just put back. */ +addEventListener('pageshow', (e) => { if (e.persisted && freshLeft) { hashReady = true; freshLeft = false; } }); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a same-document navigation, so nothing re-reads it and the drawer on screen stays put. @@ -18506,7 +18558,6 @@

Your drawers

/* That save, and the address it left as it was, by fingerprint, for this tab: the layout the link sets aside is the save, and it is later than the address, which never had the change. Back to the address offers it, and only it (see `later` below). */ -const PASTED_KEY = SAVE_KEY + ':pasted'; const pastedFrom = (prev) => { try { const r = JSON.parse(sessionStorage.getItem(PASTED_KEY) || 'null'); @@ -18516,8 +18567,8 @@

Your drawers

}; /* Start fresh and Put back each leave that save behind, and the record goes with them. Kept past Start fresh, Back to a page at the same address offered the save as the later - layout, though Start fresh came after it. Either button can be pressed when the boot - stopped short of this line, so whatever goes wrong here is let go. */ + layout, though Start fresh came after it. Start fresh works from before the boot reads a + link, so PASTED_KEY is declared with SAVE_KEY, above it. */ function forgetPasted() { try { sessionStorage.removeItem(PASTED_KEY); } catch (err) { /* nothing kept to forget */ } } diff --git a/index.html b/index.html index 71dec04..d93b69c 100644 --- a/index.html +++ b/index.html @@ -6998,6 +6998,8 @@

Your drawers

nothing it has saved since has gone through: the change is still the address's alone. */ let refusedIn = false; const refused = () => failing || refusedIn; + // the refusal the page holds, by the id its records hand on (see carry and land), or '' + let rid = ''; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -7026,12 +7028,34 @@

Your drawers

/* For a page whose design holds a change the drawer refused (`on`), what the page that takes the design on needs to know of it, in a note, a mark or a raced reload's record: that it does, and the page's base `b`, its shared keys as it last saved them - or arrived with them, which its changes are counted from (see attach). Nothing for - any other page, whose base is the design it hands on. */ - const carry = (on, b) => (on && b ? { refused: true, base: [...b] } : {}); - // the base a note, mark or record carries (see carry), or null for one that carries none + or arrived with them, which its changes are counted from (see attach). With an id + for the refusal, the same in every record of it, so that once it lands they can all + be told apart from one still to save (see land). Nothing for any other page, whose + base is the design it hands on. */ + const carry = (on, b) => { + if (!(on && b)) return {}; + rid = rid || Math.random().toString(36).slice(2, 10); + return { refused: true, base: [...b], rid }; + }; + /* A save that went through while the page held a refusal: the change is in the drawer + now, or was changed again before it got there, and either way every record of the + refusal is older than the drawer. Kept for this tab, the last few, so Back to one of + them is a page of yours come back, caught up with the drawer. Counted from its base, + a size set back on the other page since was taken for the change, and saved over the + one set back. */ + function land() { + if (!rid) return; + const n = readNote('sessionStorage', TAB); + const done = strings(n.landed); + if (!done.includes(rid)) { n.landed = done.concat(rid).slice(-32); writeNote('sessionStorage', TAB, n); } + rid = ''; + } + const landed = (id) => strings(readNote('sessionStorage', TAB).landed).includes(id); + /* The base a note, mark or record carries (see carry), or null for one that carries + none, or whose refusal has landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; + if (typeof r.rid === 'string' && landed(r.rid)) return null; return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); } @@ -7049,7 +7073,7 @@

Your drawers

n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rid: more.rid } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -7165,6 +7189,8 @@

Your drawers

const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + // the page has the drawer's settings already: they are its base now, with nothing to reload + if (merged === h) { base = caught; return false; } const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; @@ -7543,6 +7569,11 @@

Your drawers

const d = find(loadAll(), attached); if (!d) return; const h = o.design(); + // a refusal that has landed since, from a later page: this one is as any page come back + if (refused() && rid && landed(rid)) { + failing = false; refusedIn = false; rid = ''; base = sharedOf(h); + paintBar(); + } if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -7634,6 +7665,7 @@

Your drawers

ours = undefined; theirs = undefined; refusedIn = false; + rid = ''; const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -7686,7 +7718,7 @@

Your drawers

a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; } + if (kept) { refusedIn = true; base = kept; rid = typeof from.rid === 'string' ? from.rid : ''; } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -7701,7 +7733,10 @@

Your drawers

if (noted) { d.hash = mergeDesign(d.hash, arrivedWith, o.owns, base); d.marks[o.tool] = fp; - if (saveAll(s)) { ours = fp; base = sharedOf(arrivedWith); refusedIn = false; } + if (saveAll(s)) { + if (refused()) land(); + ours = fp; base = sharedOf(arrivedWith); refusedIn = false; + } savedInto(d.id); } } @@ -7734,7 +7769,10 @@

Your drawers

d.marks[o.tool] = fingerprint(h); d.saved = Date.now(); const ok = saveAll(s); - if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; } + if (ok) { + if (refused()) land(); + base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's change, past the save the page last made into it (had): a reload that raced this @@ -7766,7 +7804,10 @@

Your drawers

if (d && typeof mine === 'string' && !behind(d, mine)) { d.hash = mergeDesign(d.hash, h, o.owns, base); d.saved = Date.now(); - if (saveAll(s)) base = sharedOf(h); + if (saveAll(s)) { + if (refused()) land(); + base = sharedOf(h); + } } /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ @@ -9984,6 +10025,8 @@

Your drawers

* stops that being your problem. */ let hashSaveT = 0, hashReady = false; +// saves were on when Start fresh left the page (see startFresh) +let freshLeft = false; /* Kept on this browser, so the work survives arriving without a link. * * The address bar already carries the design and a refresh already restores it. What it @@ -10004,6 +10047,8 @@

Your drawers

* when it has done one and offers a way back. */ const SAVE_KEY = 'drawerforge:plates:v1'; +// this tab's record of a save a pasted link set aside (see pastedFrom), declared here for Start fresh +const PASTED_KEY = SAVE_KEY + ':pasted'; const saveLocal = (h) => { try { localStorage.setItem(SAVE_KEY, h); } catch (err) { /* private mode, or the quota is full — losing the save is not worth @@ -10017,6 +10062,7 @@

Your drawers

function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); + freshLeft = hashReady; /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ @@ -10026,6 +10072,12 @@

Your drawers

also take away the button that gets you out of it, or stop the next link working. What leaveFor stops, hashReady and hashSaveT, is declared above, so it works from here too. */ $('startFresh').addEventListener('click', startFresh); +/* Left by Start fresh and shown again from the back-forward cache, as Back from the fresh + page can be in some browsers: the page is yours again, and saves as it did before, as it + does loaded. Not after Put back or Try anyway, which leave by leaveFor too: loaded, that + page is a link again and sets your layout aside first, and from the cache its first save + would go over the layout just put back. */ +addEventListener('pageshow', (e) => { if (e.persisted && freshLeft) { hashReady = true; freshLeft = false; } }); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a same-document navigation, so nothing re-reads it and the drawer on screen stays put. @@ -10217,7 +10269,6 @@

Your drawers

/* That save, and the address it left as it was, by fingerprint, for this tab: the layout the link sets aside is the save, and it is later than the address, which never had the change. Back to the address offers it, and only it (see `later` below). */ -const PASTED_KEY = SAVE_KEY + ':pasted'; const pastedFrom = (prev) => { try { const r = JSON.parse(sessionStorage.getItem(PASTED_KEY) || 'null'); @@ -10227,8 +10278,8 @@

Your drawers

}; /* Start fresh and Put back each leave that save behind, and the record goes with them. Kept past Start fresh, Back to a page at the same address offered the save as the later - layout, though Start fresh came after it. Either button can be pressed when the boot - stopped short of this line, so whatever goes wrong here is let go. */ + layout, though Start fresh came after it. Start fresh works from before the boot reads a + link, so PASTED_KEY is declared with SAVE_KEY, above it. */ function forgetPasted() { try { sessionStorage.removeItem(PASTED_KEY); } catch (err) { /* nothing kept to forget */ } } diff --git a/src/bins/ui.js b/src/bins/ui.js index 5cac230..cb2d004 100644 --- a/src/bins/ui.js +++ b/src/bins/ui.js @@ -5325,6 +5325,8 @@ function cleanNote(n) { * stops that being your problem. */ let hashSaveT = 0, hashReady = false; +// saves were on when Start fresh left the page (see startFresh) +let freshLeft = false; /* Kept on this browser, so the work survives arriving without a link. * * The address bar already carries the design and a refresh already restores it. What it @@ -5345,6 +5347,8 @@ let hashSaveT = 0, hashReady = false; * when it has done one and offers a way back. */ const SAVE_KEY = 'drawerforge:bins:v1'; +// this tab's record of a save a pasted link set aside (see pastedFrom), declared here for Start fresh +const PASTED_KEY = SAVE_KEY + ':pasted'; const saveLocal = (h) => { try { localStorage.setItem(SAVE_KEY, h); } catch (err) { /* private mode, or the quota is full — losing the save is not worth @@ -5358,6 +5362,7 @@ const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); + freshLeft = hashReady; /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ @@ -5367,6 +5372,12 @@ function startFresh() { also take away the button that gets you out of it, or stop the next link working. What leaveFor stops, hashReady and hashSaveT, is declared above, so it works from here too. */ $('startFresh').addEventListener('click', startFresh); +/* Left by Start fresh and shown again from the back-forward cache, as Back from the fresh + page can be in some browsers: the page is yours again, and saves as it did before, as it + does loaded. Not after Put back or Try anyway, which leave by leaveFor too: loaded, that + page is a link again and sets your layout aside first, and from the cache its first save + would go over the layout just put back. */ +addEventListener('pageshow', (e) => { if (e.persisted && freshLeft) { hashReady = true; freshLeft = false; } }); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a same-document navigation, so nothing re-reads it and the drawer on screen stays put. @@ -5538,7 +5549,6 @@ const pastedOver = () => ownHash !== null && location.hash !== ownHash && /* That save, and the address it left as it was, by fingerprint, for this tab: the layout the link sets aside is the save, and it is later than the address, which never had the change. Back to the address offers it, and only it (see `later` below). */ -const PASTED_KEY = SAVE_KEY + ':pasted'; const pastedFrom = (prev) => { try { const r = JSON.parse(sessionStorage.getItem(PASTED_KEY) || 'null'); @@ -5548,8 +5558,8 @@ const pastedFrom = (prev) => { }; /* Start fresh and Put back each leave that save behind, and the record goes with them. Kept past Start fresh, Back to a page at the same address offered the save as the later - layout, though Start fresh came after it. Either button can be pressed when the boot - stopped short of this line, so whatever goes wrong here is let go. */ + layout, though Start fresh came after it. Start fresh works from before the boot reads a + link, so PASTED_KEY is declared with SAVE_KEY, above it. */ function forgetPasted() { try { sessionStorage.removeItem(PASTED_KEY); } catch (err) { /* nothing kept to forget */ } } diff --git a/src/shared-ui/drawers.js b/src/shared-ui/drawers.js index 294f61e..f513e51 100644 --- a/src/shared-ui/drawers.js +++ b/src/shared-ui/drawers.js @@ -330,6 +330,8 @@ const DRAWERS = (function () { nothing it has saved since has gone through: the change is still the address's alone. */ let refusedIn = false; const refused = () => failing || refusedIn; + // the refusal the page holds, by the id its records hand on (see carry and land), or '' + let rid = ''; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -358,12 +360,34 @@ const DRAWERS = (function () { /* For a page whose design holds a change the drawer refused (`on`), what the page that takes the design on needs to know of it, in a note, a mark or a raced reload's record: that it does, and the page's base `b`, its shared keys as it last saved them - or arrived with them, which its changes are counted from (see attach). Nothing for - any other page, whose base is the design it hands on. */ - const carry = (on, b) => (on && b ? { refused: true, base: [...b] } : {}); - // the base a note, mark or record carries (see carry), or null for one that carries none + or arrived with them, which its changes are counted from (see attach). With an id + for the refusal, the same in every record of it, so that once it lands they can all + be told apart from one still to save (see land). Nothing for any other page, whose + base is the design it hands on. */ + const carry = (on, b) => { + if (!(on && b)) return {}; + rid = rid || Math.random().toString(36).slice(2, 10); + return { refused: true, base: [...b], rid }; + }; + /* A save that went through while the page held a refusal: the change is in the drawer + now, or was changed again before it got there, and either way every record of the + refusal is older than the drawer. Kept for this tab, the last few, so Back to one of + them is a page of yours come back, caught up with the drawer. Counted from its base, + a size set back on the other page since was taken for the change, and saved over the + one set back. */ + function land() { + if (!rid) return; + const n = readNote('sessionStorage', TAB); + const done = strings(n.landed); + if (!done.includes(rid)) { n.landed = done.concat(rid).slice(-32); writeNote('sessionStorage', TAB, n); } + rid = ''; + } + const landed = (id) => strings(readNote('sessionStorage', TAB).landed).includes(id); + /* The base a note, mark or record carries (see carry), or null for one that carries + none, or whose refusal has landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; + if (typeof r.rid === 'string' && landed(r.rid)) return null; return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); } @@ -381,7 +405,7 @@ const DRAWERS = (function () { n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rid: more.rid } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -497,6 +521,8 @@ const DRAWERS = (function () { const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + // the page has the drawer's settings already: they are its base now, with nothing to reload + if (merged === h) { base = caught; return false; } const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; @@ -875,6 +901,11 @@ const DRAWERS = (function () { const d = find(loadAll(), attached); if (!d) return; const h = o.design(); + // a refusal that has landed since, from a later page: this one is as any page come back + if (refused() && rid && landed(rid)) { + failing = false; refusedIn = false; rid = ''; base = sharedOf(h); + paintBar(); + } if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -966,6 +997,7 @@ const DRAWERS = (function () { ours = undefined; theirs = undefined; refusedIn = false; + rid = ''; const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -1018,7 +1050,7 @@ const DRAWERS = (function () { a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; } + if (kept) { refusedIn = true; base = kept; rid = typeof from.rid === 'string' ? from.rid : ''; } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -1033,7 +1065,10 @@ const DRAWERS = (function () { if (noted) { d.hash = mergeDesign(d.hash, arrivedWith, o.owns, base); d.marks[o.tool] = fp; - if (saveAll(s)) { ours = fp; base = sharedOf(arrivedWith); refusedIn = false; } + if (saveAll(s)) { + if (refused()) land(); + ours = fp; base = sharedOf(arrivedWith); refusedIn = false; + } savedInto(d.id); } } @@ -1066,7 +1101,10 @@ const DRAWERS = (function () { d.marks[o.tool] = fingerprint(h); d.saved = Date.now(); const ok = saveAll(s); - if (ok) { base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; } + if (ok) { + if (refused()) land(); + base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's change, past the save the page last made into it (had): a reload that raced this @@ -1098,7 +1136,10 @@ const DRAWERS = (function () { if (d && typeof mine === 'string' && !behind(d, mine)) { d.hash = mergeDesign(d.hash, h, o.owns, base); d.saved = Date.now(); - if (saveAll(s)) base = sharedOf(h); + if (saveAll(s)) { + if (refused()) land(); + base = sharedOf(h); + } } /* Behind or not, the note says which save this page's half is: one the drawer has moved on from comes back from the other page as the drawer has it (restore). */ diff --git a/src/ui.js b/src/ui.js index 369122f..33b8074 100644 --- a/src/ui.js +++ b/src/ui.js @@ -2200,6 +2200,8 @@ function parseHash(h) { * stops that being your problem. */ let hashSaveT = 0, hashReady = false; +// saves were on when Start fresh left the page (see startFresh) +let freshLeft = false; /* Kept on this browser, so the work survives arriving without a link. * * The address bar already carries the design and a refresh already restores it. What it @@ -2220,6 +2222,8 @@ let hashSaveT = 0, hashReady = false; * when it has done one and offers a way back. */ const SAVE_KEY = 'drawerforge:plates:v1'; +// this tab's record of a save a pasted link set aside (see pastedFrom), declared here for Start fresh +const PASTED_KEY = SAVE_KEY + ':pasted'; const saveLocal = (h) => { try { localStorage.setItem(SAVE_KEY, h); } catch (err) { /* private mode, or the quota is full — losing the save is not worth @@ -2233,6 +2237,7 @@ const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); + freshLeft = hashReady; /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ @@ -2242,6 +2247,12 @@ function startFresh() { also take away the button that gets you out of it, or stop the next link working. What leaveFor stops, hashReady and hashSaveT, is declared above, so it works from here too. */ $('startFresh').addEventListener('click', startFresh); +/* Left by Start fresh and shown again from the back-forward cache, as Back from the fresh + page can be in some browsers: the page is yours again, and saves as it did before, as it + does loaded. Not after Put back or Try anyway, which leave by leaveFor too: loaded, that + page is a link again and sets your layout aside first, and from the cache its first save + would go over the layout just put back. */ +addEventListener('pageshow', (e) => { if (e.persisted && freshLeft) { hashReady = true; freshLeft = false; } }); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a same-document navigation, so nothing re-reads it and the drawer on screen stays put. @@ -2433,7 +2444,6 @@ const pastedOver = () => ownHash !== null && location.hash !== ownHash && /* That save, and the address it left as it was, by fingerprint, for this tab: the layout the link sets aside is the save, and it is later than the address, which never had the change. Back to the address offers it, and only it (see `later` below). */ -const PASTED_KEY = SAVE_KEY + ':pasted'; const pastedFrom = (prev) => { try { const r = JSON.parse(sessionStorage.getItem(PASTED_KEY) || 'null'); @@ -2443,8 +2453,8 @@ const pastedFrom = (prev) => { }; /* Start fresh and Put back each leave that save behind, and the record goes with them. Kept past Start fresh, Back to a page at the same address offered the save as the later - layout, though Start fresh came after it. Either button can be pressed when the boot - stopped short of this line, so whatever goes wrong here is let go. */ + layout, though Start fresh came after it. Start fresh works from before the boot reads a + link, so PASTED_KEY is declared with SAVE_KEY, above it. */ function forgetPasted() { try { sessionStorage.removeItem(PASTED_KEY); } catch (err) { /* nothing kept to forget */ } } diff --git a/sw.js b/sw.js index cf79a5b..279c71e 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "01d1297450f6"; +const VERSION = "0765e40a1a0a"; const FILES = [ "./", "bins/", diff --git a/test/ui/drawers.spec.js b/test/ui/drawers.spec.js index 9432d6f..fd03f95 100644 --- a/test/ui/drawers.spec.js +++ b/test/ui/drawers.spec.js @@ -1207,6 +1207,88 @@ for (const tool of ['plates', 'bins']) { }); } +/* A refused size handed over is saved on the other page once there is room, and then set + back there, before Back to the page that made it. That page's address still says the + size was refused, counted from the size the drawer has again, so it took the size for a + change still to save, and its first save wrote it over the one set back, with nothing + done on it. The refusal has landed since, so the page is a page of yours come back, and + catches up with the drawer. The room comes, and the size is saved: + - on the other page, by setting it back; + - there, by a change of that page's own, before it is set back; + - as the other page arrives, by the save it makes then; + - before the hand-over, by the hand-over's own save. */ +for (const tool of ['plates', 'bins']) { + for (const [room, saved] of [['set back', 'as it is set back'], ['a change first', 'by a change there first'], + ['arriving', 'as that page arrives'], ['before the hand-over', 'by the hand-over']]) { + test(`Back to a ${tool} page whose refused size was saved on the other page and set back there ` + + `is the size set back, saved ${saved}`, async ({ page }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + await page.addInitScript(fullStorage); + // room from the next page's first line, before it saves on arriving + await page.addInitScript(() => { + if (sessionStorage.getItem('roomNext') !== '1') return; + sessionStorage.removeItem('roomNext'); + sessionStorage.setItem('full', '0'); + }); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + if (room === 'before the hand-over') await page.evaluate(() => sessionStorage.setItem('full', '0')); + if (room === 'arriving') await page.evaluate(() => sessionStorage.setItem('roomNext', '1')); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await (there === 'bins' ? binsReady : platesReady)(page); + expect(await page.inputValue('#drawerW'), 'handed over').toBe('410'); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + if (room === 'a change first') await changeOwn(page, there); + if (room !== 'set back') { + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'the size saved', timeout: 20000 }).toBe('410'); + } + await H.setField(page, 'drawerW', '400'); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'set back there', timeout: 20000 }).toBe('400'); + await settle(page); + + await page.goBack(); + await expect.poll(() => page.inputValue('#drawerW').catch(() => ''), + { message: 'Back, caught up', timeout: 20000 }).toBe('400'); + await (tool === 'bins' ? binsReady : platesReady)(page); + await settle(page); + expect((await stored(page)).Kitchen.w, 'the size set back stays').toBe('400'); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + expect(errors).toEqual([]); + }); + } +} + +/* A catch-up for a size the page already has: another tab set the size this page holds + refused. Nothing on the page changes, so it is not reloaded, and the size is the + drawer's from then on, not a change still to save. */ +for (const tool of ['plates', 'bins']) { + test(`a ${tool} page holding a refused size another tab has saved since is not reloaded to catch up`, + async ({ page, context }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + await inOtherTab(context, there, errors, (other) => H.setField(other, 'drawerW', '410')); + const stayed = await page.evaluate(() => { + window.notReloaded = true; + dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true })); + return !!(history.state && history.state.drawerforge); // catching up clears it to reload + }); + expect(stayed, 'shown again from the cache, it stays').toBe(true); + await settle(page); + expect(await page.evaluate(() => window.notReloaded === true), 'not reloaded').toBe(true); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, tool); + const [, v, key] = ownChange(tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'with room again, the next change saves', timeout: 20000 }) + .toMatchObject({ w: '410', [key]: v }); + expect(errors).toEqual([]); + }); +} + /* Each page saves its whole half of the drawer, so one whose half is older than the drawer's would write it back: a reload of a tab after another tab of the same tool changed the drawer, or Back to a page of yours that a later page has moved on. Both diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index 3fd75f7..a1fdaba 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -1059,6 +1059,34 @@ for (const tool of ['plates', 'bins']) { }); } +/* However early the boot stops, Start fresh also ends the record of an edit a pasted link + set aside (see the paste tests above). The record's key is declared with the save's: declared + where the record is read, further on, it was not there yet for a boot stopped before + that, and the record stayed. The page is made to throw just after the button is wired. */ +for (const tool of ['plates', 'bins']) { + test(`Start fresh on ${tool} ends the pasted-link record when the boot stops early`, async ({ page }) => { + const url = tool === 'bins' ? binsUrl() : platesUrl(); + const key = (tool === 'bins' ? BINS : PLATES) + ':pasted'; + await arrive(page, url); + await page.evaluate((k) => sessionStorage.setItem(k, JSON.stringify({ save: 'a', at: 'b' })), key); + const wired = "$('startFresh').addEventListener('click', startFresh);"; + await page.route((u) => u.href === url, async (route) => { + const res = await route.fetch(); + await route.fulfill({ response: res, body: (await res.text()).replace(wired, + (m) => m + " if (/(^|&)boom=/.test(location.hash)) throw new Error('boom');") }); + }); + const thrown = []; + page.on('pageerror', (e) => thrown.push(String(e))); + await arrive(page, url + '#w=345&d=444&boom=1&v=2'); + expect(thrown.join(), 'the boot threw').toContain('boom'); + await Promise.all([page.waitForEvent('load'), + page.evaluate(() => document.getElementById('startFresh').click())]); + await ready(page); + expect(await page.evaluate((k) => sessionStorage.getItem(k), key), 'the record').toBeNull(); + expect(thrown.filter((e) => !/boom/.test(e))).toEqual([]); + }); +} + /* And it ends the offer of an edit a pasted link set aside (see the paste tests above). The edit was offered on Back, and left; then a bare visit, and Start fresh there. The record of the edit's save stayed, and the fresh page is at the address it names: another link @@ -1101,3 +1129,32 @@ for (const tool of ['plates', 'bins']) { expect(errors).toEqual([]); }); } + +/* Start fresh stops the page's saves as it goes, and a browser that keeps the page in its + back-forward cache can show it again on Back. Shown again, it is a page of yours, and its + changes save, as they did before Start fresh and as they do when it is loaded again. + Chromium does not keep it, so here the page is stopped before the fresh one arrives and + shown again as the cache would. Both pages are covered. */ +for (const tool of ['plates', 'bins']) { + test(`a ${tool} page left by Start fresh and shown again from the cache saves again`, async ({ page }) => { + const errors = watch(page); + const url = tool === 'bins' ? binsUrl() : platesUrl(); + await arrive(page, url); + await H.setField(page, 'drawerW', '451'); + await saved(page); + await arrive(page, url); // a bare visit, restoring it, which offers Start fresh + await page.route((u) => u.href === url, async (route) => { + await new Promise((r) => setTimeout(r, 5000)); + await route.continue().catch(() => {}); + }); + await page.evaluate(() => { + document.getElementById('startFresh').click(); + window.stop(); // the fresh page never arrives + dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true })); + }); + await H.setField(page, 'drawerW', '377'); + await saved(page); + expect(await stored(page, tool === 'bins' ? BINS : PLATES)).toMatch(/(^|&)w=377(&|$)/); + expect(errors).toEqual([]); + }); +} From 47987bd4f5c1614eb40afcdcde2e6169b5a06006 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 10 Oct 2026 02:53:43 +0000 Subject: [PATCH 8/9] Give each page its own refusal ids, and drop them with the refusal A page now holds the ids of the refusals it arrived with, and adds one of its own for the saves the drawer refuses before it next hands them on. Records carry the list, and count as landed only once every id in it has. With one id for a whole chain, a save on an earlier page counted a later page's refused depth as saved, and Forward lost it. Save as and deleting the drawer drop the ids, so a refusal in the new drawer no longer counts Kitchen's as saved. From the back-forward cache, a page whose refusals have landed keeps "not saving" while its own half differs from the drawer's, and a catch-up with nothing to reload rewrites the address's mark with the new base. leaveFor clears freshLeft, ids are read only as short lowercase words, and the tab keeps the last 128 landed. S3 and O1 are also tested with a real back-forward cache. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 122 +++++++++++------- index.html | 122 +++++++++++------- src/bins/ui.js | 14 ++- src/shared-ui/drawers.js | 108 ++++++++++------ src/ui.js | 14 ++- sw.js | 2 +- test/ui/drawers.spec.js | 224 +++++++++++++++++++++++++++++++++- test/ui/hostile-links.spec.js | 32 +++++ 8 files changed, 504 insertions(+), 134 deletions(-) diff --git a/bins/index.html b/bins/index.html index e9edb15..a97c546 100644 --- a/bins/index.html +++ b/bins/index.html @@ -7274,8 +7274,15 @@

Your drawers

nothing it has saved since has gone through: the change is still the address's alone. */ let refusedIn = false; const refused = () => failing || refusedIn; - // the refusal the page holds, by the id its records hand on (see carry and land), or '' - let rid = ''; + /* The refusals the page holds, by the ids its records hand on (see carry and land): any + it arrived with, and one of its own for its refused saves since it last handed them + on, by a hand-over or as it went into the back-forward cache (`sealed` till then). + With one id for the whole chain, a save that went through on an earlier page counted + a later page's change as landed too, though it had never been saved: Forward to that + page lost a depth refused there. */ + let rids = []; + let sealed = true; + const drop = () => { rids = []; sealed = true; }; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -7304,34 +7311,47 @@

Your drawers

/* For a page whose design holds a change the drawer refused (`on`), what the page that takes the design on needs to know of it, in a note, a mark or a raced reload's record: that it does, and the page's base `b`, its shared keys as it last saved them - or arrived with them, which its changes are counted from (see attach). With an id - for the refusal, the same in every record of it, so that once it lands they can all - be told apart from one still to save (see land). Nothing for any other page, whose - base is the design it hands on. */ + or arrived with them, which its changes are counted from (see attach). With the ids + of the refusals it holds (see rids), so that once they land, its records can be told + apart from one still to save (see land). Nothing for any other page, whose base is + the design it hands on. */ + const newRid = () => Math.random().toString(36).slice(2, 10) || 'r'; const carry = (on, b) => { if (!(on && b)) return {}; - rid = rid || Math.random().toString(36).slice(2, 10); - return { refused: true, base: [...b], rid }; + if (!rids.length) rids = [newRid()]; + return { refused: true, base: [...b], rids: rids.slice() }; }; - /* A save that went through while the page held a refusal: the change is in the drawer - now, or was changed again before it got there, and either way every record of the - refusal is older than the drawer. Kept for this tab, the last few, so Back to one of + // ids as a record or the tab's note has them, what carry makes and no more + const ridsIn = (v) => strings(v).filter((id) => /^[a-z0-9]{1,16}$/.test(id)); + const landedIds = () => ridsIn(readNote('sessionStorage', TAB).landed); + /* A save that went through while the page held refusals: their changes are in the + drawer now, or were changed again before they got there, and either way every record + of them is older than the drawer. Kept for this tab, the last 128, so Back to one of them is a page of yours come back, caught up with the drawer. Counted from its base, a size set back on the other page since was taken for the change, and saved over the one set back. */ function land() { - if (!rid) return; - const n = readNote('sessionStorage', TAB); - const done = strings(n.landed); - if (!done.includes(rid)) { n.landed = done.concat(rid).slice(-32); writeNote('sessionStorage', TAB, n); } - rid = ''; + if (rids.length) { + const done = landedIds(); + const add = rids.filter((id) => !done.includes(id)); + if (add.length) { + const n = readNote('sessionStorage', TAB); + n.landed = done.concat(add).slice(-128); + writeNote('sessionStorage', TAB, n); + } + } + drop(); } - const landed = (id) => strings(readNote('sessionStorage', TAB).landed).includes(id); + // whether every one of `ids` has landed, so a record holding them has nothing left to save + const allLanded = (ids) => { + const done = landedIds(); + return ids.length > 0 && ids.every((id) => done.includes(id)); + }; /* The base a note, mark or record carries (see carry), or null for one that carries - none, or whose refusal has landed since. */ + none, or whose refusals have all landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; - if (typeof r.rid === 'string' && landed(r.rid)) return null; + if (allLanded(ridsIn(r.rids))) return null; return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); } @@ -7349,7 +7369,7 @@

Your drawers

n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base, rid: more.rid } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -7465,8 +7485,16 @@

Your drawers

const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); - // the page has the drawer's settings already: they are its base now, with nothing to reload - if (merged === h) { base = caught; return false; } + /* The page has the drawer's settings already: they are its base now, with nothing to + reload, and its address's mark says so. Left with the base before, a reload after + another tab set the size back took the page's size for its own change still to + save, and wrote it over the one set back. */ + if (merged === h) { + base = caught; + const m = refused() && markOf(h); + if (m) markHere(h, m.link, m.note); + return false; + } const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; @@ -7506,12 +7534,13 @@

Your drawers

the drawer's half lost it. A change the drawer refused goes with it, with its base (see carry): the half reloaded is this tool's alone. True if the page is reloading. */ const half = (k) => o.owns(k) && !SHARED.has(k) && !(o.given || []).includes(k); + // this tool's half of the design `s`, in one order, so two halves compare as strings + const halfPairs = (s) => (parsePairs(s) || []).filter(([k]) => half(k)) + .sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)); function restore(h, d, link, seen) { if (seen === d.marks[o.tool]) return false; - const mine = (s) => (parsePairs(s) || []).filter(([k]) => half(k)) - .sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)); - const stored = mine(d.hash); - if (encodePairs(stored) === encodePairs(mine(h))) return false; + const stored = halfPairs(d.hash); + if (encodePairs(stored) === encodePairs(halfPairs(h))) return false; const merged = encodePairs((parsePairs(h) || []).filter(([k]) => !half(k)).concat(stored)); const more = { link, marks: marksFor(d.marks[o.tool]), ...carry(refused(), base) }; if (!handOver(d.id, merged, false, more)) return false; @@ -7531,7 +7560,7 @@

Your drawers

function paintBar() { const d = attached && find(loadAll(), attached); // deleted on another tab - if (attached && !d) { attached = null; failing = false; refusedIn = false; } + if (attached && !d) { attached = null; failing = false; refusedIn = false; drop(); } /* A refused save leads, ahead of the name: on a phone the bar shows about a dozen characters, and the warning is the part that has to survive the cut. */ const shown = d ? (failing ? `not saving · ${d.name}` : d.name) : 'not saved'; @@ -7668,7 +7697,7 @@

Your drawers

return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; refusedIn = false; + base = sharedOf(h); failing = false; refusedIn = false; drop(); ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -7727,7 +7756,7 @@

Your drawers

s.drawers = s.drawers.filter((x) => x.id !== id); if (!saveAll(s)) { say('This browser would not store the change.', 'bad'); return; } const wasOpen = attached === id; - if (wasOpen) { attached = null; failing = false; refusedIn = false; } + if (wasOpen) { attached = null; failing = false; refusedIn = false; drop(); } armed = ''; render(); say(`Deleted “${d.name}”.` + (wasOpen @@ -7845,11 +7874,16 @@

Your drawers

const d = find(loadAll(), attached); if (!d) return; const h = o.design(); - // a refusal that has landed since, from a later page: this one is as any page come back - if (refused() && rid && landed(rid)) { - failing = false; refusedIn = false; rid = ''; base = sharedOf(h); + /* Refusals that have all landed since, from a later page: this one is as any page come + back. All but a change to its own half, which the later page did not save: refused + with the size, it is still this page's alone, and the bar still says so until a save + goes through. */ + if (refused() && allLanded(rids)) { + refusedIn = false; drop(); base = sharedOf(h); + if (encodePairs(halfPairs(h)) === encodePairs(halfPairs(d.hash))) failing = false; paintBar(); } + sealed = true; // its records so far went into the cache with it if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -7941,7 +7975,7 @@

Your drawers

ours = undefined; theirs = undefined; refusedIn = false; - rid = ''; + drop(); const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -7994,7 +8028,7 @@

Your drawers

a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; rid = typeof from.rid === 'string' ? from.rid : ''; } + if (kept) { refusedIn = true; base = kept; rids = ridsIn(from.rids).slice(-32); } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -8035,7 +8069,7 @@

Your drawers

const s = loadAll(); const d = find(s, attached); if (!d) { - attached = null; failing = false; refusedIn = false; + attached = null; failing = false; refusedIn = false; drop(); savedInto(''); leaving('', '', ours, link); paintBar(); return; } // nothing of this page's own to save, and a reload reopens onto the drawer @@ -8048,6 +8082,9 @@

Your drawers

if (ok) { if (refused()) land(); base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + } else if (sealed) { + // a refusal of this page's own, apart from any it arrived with (see rids) + rids = rids.concat(newRid()); sealed = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's @@ -8089,6 +8126,7 @@

Your drawers

moved on from comes back from the other page as the drawer has it (restore). */ handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, ...carry(!!d && refused(), base) }); + sealed = true; // handed on: a refusal after this, from the cache, is another }, }; } @@ -18397,7 +18435,7 @@

Your drawers

* stops that being your problem. */ let hashSaveT = 0, hashReady = false; -// saves were on when Start fresh left the page (see startFresh) +// saves were on when Start fresh left the page, and nothing has left it since (see startFresh) let freshLeft = false; /* Kept on this browser, so the work survives arriving without a link. * @@ -18434,11 +18472,12 @@

Your drawers

function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); - freshLeft = hashReady; + const saving = hashReady; /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ leaveFor(location.origin + location.pathname); // drop the hash and reload clean + freshLeft = saving; // after leaveFor, which clears it } /* Wired here, before the boot below reads any link: a link that throws there must not also take away the button that gets you out of it, or stop the next link working. What @@ -18446,9 +18485,10 @@

Your drawers

$('startFresh').addEventListener('click', startFresh); /* Left by Start fresh and shown again from the back-forward cache, as Back from the fresh page can be in some browsers: the page is yours again, and saves as it did before, as it - does loaded. Not after Put back or Try anyway, which leave by leaveFor too: loaded, that - page is a link again and sets your layout aside first, and from the cache its first save - would go over the layout just put back. */ + does loaded. Not after Put back or Try anyway, which leave by leaveFor too, even pressed + after Start fresh before the page has gone: loaded, that page is a link again and sets + your layout aside first, and from the cache its first save would go over the layout just + put back. */ addEventListener('pageshow', (e) => { if (e.persisted && freshLeft) { hashReady = true; freshLeft = false; } }); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a @@ -18518,7 +18558,7 @@

Your drawers

return [...SHARED_KEYS].filter((k) => k in q && p[k] === q[k]); } function leaveFor(url) { - hashReady = false; clearTimeout(hashSaveT); // no save of this page's may land after + hashReady = false; freshLeft = false; clearTimeout(hashSaveT); // no save of this page's may land after location.href = url; } /* Swapped rather than copied over: what is here now goes aside in its place, so putting diff --git a/index.html b/index.html index b96063e..2d55a45 100644 --- a/index.html +++ b/index.html @@ -7061,8 +7061,15 @@

Your drawers

nothing it has saved since has gone through: the change is still the address's alone. */ let refusedIn = false; const refused = () => failing || refusedIn; - // the refusal the page holds, by the id its records hand on (see carry and land), or '' - let rid = ''; + /* The refusals the page holds, by the ids its records hand on (see carry and land): any + it arrived with, and one of its own for its refused saves since it last handed them + on, by a hand-over or as it went into the back-forward cache (`sealed` till then). + With one id for the whole chain, a save that went through on an earlier page counted + a later page's change as landed too, though it had never been saved: Forward to that + page lost a depth refused there. */ + let rids = []; + let sealed = true; + const drop = () => { rids = []; sealed = true; }; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -7091,34 +7098,47 @@

Your drawers

/* For a page whose design holds a change the drawer refused (`on`), what the page that takes the design on needs to know of it, in a note, a mark or a raced reload's record: that it does, and the page's base `b`, its shared keys as it last saved them - or arrived with them, which its changes are counted from (see attach). With an id - for the refusal, the same in every record of it, so that once it lands they can all - be told apart from one still to save (see land). Nothing for any other page, whose - base is the design it hands on. */ + or arrived with them, which its changes are counted from (see attach). With the ids + of the refusals it holds (see rids), so that once they land, its records can be told + apart from one still to save (see land). Nothing for any other page, whose base is + the design it hands on. */ + const newRid = () => Math.random().toString(36).slice(2, 10) || 'r'; const carry = (on, b) => { if (!(on && b)) return {}; - rid = rid || Math.random().toString(36).slice(2, 10); - return { refused: true, base: [...b], rid }; + if (!rids.length) rids = [newRid()]; + return { refused: true, base: [...b], rids: rids.slice() }; }; - /* A save that went through while the page held a refusal: the change is in the drawer - now, or was changed again before it got there, and either way every record of the - refusal is older than the drawer. Kept for this tab, the last few, so Back to one of + // ids as a record or the tab's note has them, what carry makes and no more + const ridsIn = (v) => strings(v).filter((id) => /^[a-z0-9]{1,16}$/.test(id)); + const landedIds = () => ridsIn(readNote('sessionStorage', TAB).landed); + /* A save that went through while the page held refusals: their changes are in the + drawer now, or were changed again before they got there, and either way every record + of them is older than the drawer. Kept for this tab, the last 128, so Back to one of them is a page of yours come back, caught up with the drawer. Counted from its base, a size set back on the other page since was taken for the change, and saved over the one set back. */ function land() { - if (!rid) return; - const n = readNote('sessionStorage', TAB); - const done = strings(n.landed); - if (!done.includes(rid)) { n.landed = done.concat(rid).slice(-32); writeNote('sessionStorage', TAB, n); } - rid = ''; + if (rids.length) { + const done = landedIds(); + const add = rids.filter((id) => !done.includes(id)); + if (add.length) { + const n = readNote('sessionStorage', TAB); + n.landed = done.concat(add).slice(-128); + writeNote('sessionStorage', TAB, n); + } + } + drop(); } - const landed = (id) => strings(readNote('sessionStorage', TAB).landed).includes(id); + // whether every one of `ids` has landed, so a record holding them has nothing left to save + const allLanded = (ids) => { + const done = landedIds(); + return ids.length > 0 && ids.every((id) => done.includes(id)); + }; /* The base a note, mark or record carries (see carry), or null for one that carries - none, or whose refusal has landed since. */ + none, or whose refusals have all landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; - if (typeof r.rid === 'string' && landed(r.rid)) return null; + if (allLanded(ridsIn(r.rids))) return null; return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); } @@ -7136,7 +7156,7 @@

Your drawers

n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base, rid: more.rid } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -7252,8 +7272,16 @@

Your drawers

const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); - // the page has the drawer's settings already: they are its base now, with nothing to reload - if (merged === h) { base = caught; return false; } + /* The page has the drawer's settings already: they are its base now, with nothing to + reload, and its address's mark says so. Left with the base before, a reload after + another tab set the size back took the page's size for its own change still to + save, and wrote it over the one set back. */ + if (merged === h) { + base = caught; + const m = refused() && markOf(h); + if (m) markHere(h, m.link, m.note); + return false; + } const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; @@ -7293,12 +7321,13 @@

Your drawers

the drawer's half lost it. A change the drawer refused goes with it, with its base (see carry): the half reloaded is this tool's alone. True if the page is reloading. */ const half = (k) => o.owns(k) && !SHARED.has(k) && !(o.given || []).includes(k); + // this tool's half of the design `s`, in one order, so two halves compare as strings + const halfPairs = (s) => (parsePairs(s) || []).filter(([k]) => half(k)) + .sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)); function restore(h, d, link, seen) { if (seen === d.marks[o.tool]) return false; - const mine = (s) => (parsePairs(s) || []).filter(([k]) => half(k)) - .sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)); - const stored = mine(d.hash); - if (encodePairs(stored) === encodePairs(mine(h))) return false; + const stored = halfPairs(d.hash); + if (encodePairs(stored) === encodePairs(halfPairs(h))) return false; const merged = encodePairs((parsePairs(h) || []).filter(([k]) => !half(k)).concat(stored)); const more = { link, marks: marksFor(d.marks[o.tool]), ...carry(refused(), base) }; if (!handOver(d.id, merged, false, more)) return false; @@ -7318,7 +7347,7 @@

Your drawers

function paintBar() { const d = attached && find(loadAll(), attached); // deleted on another tab - if (attached && !d) { attached = null; failing = false; refusedIn = false; } + if (attached && !d) { attached = null; failing = false; refusedIn = false; drop(); } /* A refused save leads, ahead of the name: on a phone the bar shows about a dozen characters, and the warning is the part that has to survive the cut. */ const shown = d ? (failing ? `not saving · ${d.name}` : d.name) : 'not saved'; @@ -7455,7 +7484,7 @@

Your drawers

return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; refusedIn = false; + base = sharedOf(h); failing = false; refusedIn = false; drop(); ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -7514,7 +7543,7 @@

Your drawers

s.drawers = s.drawers.filter((x) => x.id !== id); if (!saveAll(s)) { say('This browser would not store the change.', 'bad'); return; } const wasOpen = attached === id; - if (wasOpen) { attached = null; failing = false; refusedIn = false; } + if (wasOpen) { attached = null; failing = false; refusedIn = false; drop(); } armed = ''; render(); say(`Deleted “${d.name}”.` + (wasOpen @@ -7632,11 +7661,16 @@

Your drawers

const d = find(loadAll(), attached); if (!d) return; const h = o.design(); - // a refusal that has landed since, from a later page: this one is as any page come back - if (refused() && rid && landed(rid)) { - failing = false; refusedIn = false; rid = ''; base = sharedOf(h); + /* Refusals that have all landed since, from a later page: this one is as any page come + back. All but a change to its own half, which the later page did not save: refused + with the size, it is still this page's alone, and the bar still says so until a save + goes through. */ + if (refused() && allLanded(rids)) { + refusedIn = false; drop(); base = sharedOf(h); + if (encodePairs(halfPairs(h)) === encodePairs(halfPairs(d.hash))) failing = false; paintBar(); } + sealed = true; // its records so far went into the cache with it if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -7728,7 +7762,7 @@

Your drawers

ours = undefined; theirs = undefined; refusedIn = false; - rid = ''; + drop(); const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -7781,7 +7815,7 @@

Your drawers

a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; rid = typeof from.rid === 'string' ? from.rid : ''; } + if (kept) { refusedIn = true; base = kept; rids = ridsIn(from.rids).slice(-32); } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -7822,7 +7856,7 @@

Your drawers

const s = loadAll(); const d = find(s, attached); if (!d) { - attached = null; failing = false; refusedIn = false; + attached = null; failing = false; refusedIn = false; drop(); savedInto(''); leaving('', '', ours, link); paintBar(); return; } // nothing of this page's own to save, and a reload reopens onto the drawer @@ -7835,6 +7869,9 @@

Your drawers

if (ok) { if (refused()) land(); base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + } else if (sealed) { + // a refusal of this page's own, apart from any it arrived with (see rids) + rids = rids.concat(newRid()); sealed = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's @@ -7876,6 +7913,7 @@

Your drawers

moved on from comes back from the other page as the drawer has it (restore). */ handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, ...carry(!!d && refused(), base) }); + sealed = true; // handed on: a refusal after this, from the cache, is another }, }; } @@ -10094,7 +10132,7 @@

Your drawers

* stops that being your problem. */ let hashSaveT = 0, hashReady = false; -// saves were on when Start fresh left the page (see startFresh) +// saves were on when Start fresh left the page, and nothing has left it since (see startFresh) let freshLeft = false; /* Kept on this browser, so the work survives arriving without a link. * @@ -10131,11 +10169,12 @@

Your drawers

function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); - freshLeft = hashReady; + const saving = hashReady; /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ leaveFor(location.origin + location.pathname); // drop the hash and reload clean + freshLeft = saving; // after leaveFor, which clears it } /* Wired here, before the boot below reads any link: a link that throws there must not also take away the button that gets you out of it, or stop the next link working. What @@ -10143,9 +10182,10 @@

Your drawers

$('startFresh').addEventListener('click', startFresh); /* Left by Start fresh and shown again from the back-forward cache, as Back from the fresh page can be in some browsers: the page is yours again, and saves as it did before, as it - does loaded. Not after Put back or Try anyway, which leave by leaveFor too: loaded, that - page is a link again and sets your layout aside first, and from the cache its first save - would go over the layout just put back. */ + does loaded. Not after Put back or Try anyway, which leave by leaveFor too, even pressed + after Start fresh before the page has gone: loaded, that page is a link again and sets + your layout aside first, and from the cache its first save would go over the layout just + put back. */ addEventListener('pageshow', (e) => { if (e.persisted && freshLeft) { hashReady = true; freshLeft = false; } }); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a @@ -10215,7 +10255,7 @@

Your drawers

return [...SHARED_KEYS].filter((k) => k in q && p[k] === q[k]); } function leaveFor(url) { - hashReady = false; dropSave(); // no save of this page's may land after + hashReady = false; freshLeft = false; dropSave(); // no save of this page's may land after location.href = url; } /* Swapped rather than copied over: what is here now goes aside in its place, so putting diff --git a/src/bins/ui.js b/src/bins/ui.js index cb2d004..6de2edf 100644 --- a/src/bins/ui.js +++ b/src/bins/ui.js @@ -5325,7 +5325,7 @@ function cleanNote(n) { * stops that being your problem. */ let hashSaveT = 0, hashReady = false; -// saves were on when Start fresh left the page (see startFresh) +// saves were on when Start fresh left the page, and nothing has left it since (see startFresh) let freshLeft = false; /* Kept on this browser, so the work survives arriving without a link. * @@ -5362,11 +5362,12 @@ const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); - freshLeft = hashReady; + const saving = hashReady; /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ leaveFor(location.origin + location.pathname); // drop the hash and reload clean + freshLeft = saving; // after leaveFor, which clears it } /* Wired here, before the boot below reads any link: a link that throws there must not also take away the button that gets you out of it, or stop the next link working. What @@ -5374,9 +5375,10 @@ function startFresh() { $('startFresh').addEventListener('click', startFresh); /* Left by Start fresh and shown again from the back-forward cache, as Back from the fresh page can be in some browsers: the page is yours again, and saves as it did before, as it - does loaded. Not after Put back or Try anyway, which leave by leaveFor too: loaded, that - page is a link again and sets your layout aside first, and from the cache its first save - would go over the layout just put back. */ + does loaded. Not after Put back or Try anyway, which leave by leaveFor too, even pressed + after Start fresh before the page has gone: loaded, that page is a link again and sets + your layout aside first, and from the cache its first save would go over the layout just + put back. */ addEventListener('pageshow', (e) => { if (e.persisted && freshLeft) { hashReady = true; freshLeft = false; } }); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a @@ -5446,7 +5448,7 @@ function linkKeys(h, link) { return [...SHARED_KEYS].filter((k) => k in q && p[k] === q[k]); } function leaveFor(url) { - hashReady = false; clearTimeout(hashSaveT); // no save of this page's may land after + hashReady = false; freshLeft = false; clearTimeout(hashSaveT); // no save of this page's may land after location.href = url; } /* Swapped rather than copied over: what is here now goes aside in its place, so putting diff --git a/src/shared-ui/drawers.js b/src/shared-ui/drawers.js index f513e51..ee6fb52 100644 --- a/src/shared-ui/drawers.js +++ b/src/shared-ui/drawers.js @@ -330,8 +330,15 @@ const DRAWERS = (function () { nothing it has saved since has gone through: the change is still the address's alone. */ let refusedIn = false; const refused = () => failing || refusedIn; - // the refusal the page holds, by the id its records hand on (see carry and land), or '' - let rid = ''; + /* The refusals the page holds, by the ids its records hand on (see carry and land): any + it arrived with, and one of its own for its refused saves since it last handed them + on, by a hand-over or as it went into the back-forward cache (`sealed` till then). + With one id for the whole chain, a save that went through on an earlier page counted + a later page's change as landed too, though it had never been saved: Forward to that + page lost a depth refused there. */ + let rids = []; + let sealed = true; + const drop = () => { rids = []; sealed = true; }; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -360,34 +367,47 @@ const DRAWERS = (function () { /* For a page whose design holds a change the drawer refused (`on`), what the page that takes the design on needs to know of it, in a note, a mark or a raced reload's record: that it does, and the page's base `b`, its shared keys as it last saved them - or arrived with them, which its changes are counted from (see attach). With an id - for the refusal, the same in every record of it, so that once it lands they can all - be told apart from one still to save (see land). Nothing for any other page, whose - base is the design it hands on. */ + or arrived with them, which its changes are counted from (see attach). With the ids + of the refusals it holds (see rids), so that once they land, its records can be told + apart from one still to save (see land). Nothing for any other page, whose base is + the design it hands on. */ + const newRid = () => Math.random().toString(36).slice(2, 10) || 'r'; const carry = (on, b) => { if (!(on && b)) return {}; - rid = rid || Math.random().toString(36).slice(2, 10); - return { refused: true, base: [...b], rid }; + if (!rids.length) rids = [newRid()]; + return { refused: true, base: [...b], rids: rids.slice() }; }; - /* A save that went through while the page held a refusal: the change is in the drawer - now, or was changed again before it got there, and either way every record of the - refusal is older than the drawer. Kept for this tab, the last few, so Back to one of + // ids as a record or the tab's note has them, what carry makes and no more + const ridsIn = (v) => strings(v).filter((id) => /^[a-z0-9]{1,16}$/.test(id)); + const landedIds = () => ridsIn(readNote('sessionStorage', TAB).landed); + /* A save that went through while the page held refusals: their changes are in the + drawer now, or were changed again before they got there, and either way every record + of them is older than the drawer. Kept for this tab, the last 128, so Back to one of them is a page of yours come back, caught up with the drawer. Counted from its base, a size set back on the other page since was taken for the change, and saved over the one set back. */ function land() { - if (!rid) return; - const n = readNote('sessionStorage', TAB); - const done = strings(n.landed); - if (!done.includes(rid)) { n.landed = done.concat(rid).slice(-32); writeNote('sessionStorage', TAB, n); } - rid = ''; + if (rids.length) { + const done = landedIds(); + const add = rids.filter((id) => !done.includes(id)); + if (add.length) { + const n = readNote('sessionStorage', TAB); + n.landed = done.concat(add).slice(-128); + writeNote('sessionStorage', TAB, n); + } + } + drop(); } - const landed = (id) => strings(readNote('sessionStorage', TAB).landed).includes(id); + // whether every one of `ids` has landed, so a record holding them has nothing left to save + const allLanded = (ids) => { + const done = landedIds(); + return ids.length > 0 && ids.every((id) => done.includes(id)); + }; /* The base a note, mark or record carries (see carry), or null for one that carries - none, or whose refusal has landed since. */ + none, or whose refusals have all landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; - if (typeof r.rid === 'string' && landed(r.rid)) return null; + if (allLanded(ridsIn(r.rids))) return null; return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); } @@ -405,7 +425,7 @@ const DRAWERS = (function () { n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base, rid: more.rid } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids } : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -521,8 +541,16 @@ const DRAWERS = (function () { const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); - // the page has the drawer's settings already: they are its base now, with nothing to reload - if (merged === h) { base = caught; return false; } + /* The page has the drawer's settings already: they are its base now, with nothing to + reload, and its address's mark says so. Left with the base before, a reload after + another tab set the size back took the page's size for its own change still to + save, and wrote it over the one set back. */ + if (merged === h) { + base = caught; + const m = refused() && markOf(h); + if (m) markHere(h, m.link, m.note); + return false; + } const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; @@ -562,12 +590,13 @@ const DRAWERS = (function () { the drawer's half lost it. A change the drawer refused goes with it, with its base (see carry): the half reloaded is this tool's alone. True if the page is reloading. */ const half = (k) => o.owns(k) && !SHARED.has(k) && !(o.given || []).includes(k); + // this tool's half of the design `s`, in one order, so two halves compare as strings + const halfPairs = (s) => (parsePairs(s) || []).filter(([k]) => half(k)) + .sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)); function restore(h, d, link, seen) { if (seen === d.marks[o.tool]) return false; - const mine = (s) => (parsePairs(s) || []).filter(([k]) => half(k)) - .sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0)); - const stored = mine(d.hash); - if (encodePairs(stored) === encodePairs(mine(h))) return false; + const stored = halfPairs(d.hash); + if (encodePairs(stored) === encodePairs(halfPairs(h))) return false; const merged = encodePairs((parsePairs(h) || []).filter(([k]) => !half(k)).concat(stored)); const more = { link, marks: marksFor(d.marks[o.tool]), ...carry(refused(), base) }; if (!handOver(d.id, merged, false, more)) return false; @@ -587,7 +616,7 @@ const DRAWERS = (function () { function paintBar() { const d = attached && find(loadAll(), attached); // deleted on another tab - if (attached && !d) { attached = null; failing = false; refusedIn = false; } + if (attached && !d) { attached = null; failing = false; refusedIn = false; drop(); } /* A refused save leads, ahead of the name: on a phone the bar shows about a dozen characters, and the warning is the part that has to survive the cut. */ const shown = d ? (failing ? `not saving · ${d.name}` : d.name) : 'not saved'; @@ -724,7 +753,7 @@ const DRAWERS = (function () { return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; refusedIn = false; + base = sharedOf(h); failing = false; refusedIn = false; drop(); ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -783,7 +812,7 @@ const DRAWERS = (function () { s.drawers = s.drawers.filter((x) => x.id !== id); if (!saveAll(s)) { say('This browser would not store the change.', 'bad'); return; } const wasOpen = attached === id; - if (wasOpen) { attached = null; failing = false; refusedIn = false; } + if (wasOpen) { attached = null; failing = false; refusedIn = false; drop(); } armed = ''; render(); say(`Deleted “${d.name}”.` + (wasOpen @@ -901,11 +930,16 @@ const DRAWERS = (function () { const d = find(loadAll(), attached); if (!d) return; const h = o.design(); - // a refusal that has landed since, from a later page: this one is as any page come back - if (refused() && rid && landed(rid)) { - failing = false; refusedIn = false; rid = ''; base = sharedOf(h); + /* Refusals that have all landed since, from a later page: this one is as any page come + back. All but a change to its own half, which the later page did not save: refused + with the size, it is still this page's alone, and the bar still says so until a save + goes through. */ + if (refused() && allLanded(rids)) { + refusedIn = false; drop(); base = sharedOf(h); + if (encodePairs(halfPairs(h)) === encodePairs(halfPairs(d.hash))) failing = false; paintBar(); } + sealed = true; // its records so far went into the cache with it if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -997,7 +1031,7 @@ const DRAWERS = (function () { ours = undefined; theirs = undefined; refusedIn = false; - rid = ''; + drop(); const { next, left } = takeOnce(); // read every time, so a stale note never lingers if (arrivedWith) { const s = loadAll(); @@ -1050,7 +1084,7 @@ const DRAWERS = (function () { a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; rid = typeof from.rid === 'string' ? from.rid : ''; } + if (kept) { refusedIn = true; base = kept; rids = ridsIn(from.rids).slice(-32); } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -1091,7 +1125,7 @@ const DRAWERS = (function () { const s = loadAll(); const d = find(s, attached); if (!d) { - attached = null; failing = false; refusedIn = false; + attached = null; failing = false; refusedIn = false; drop(); savedInto(''); leaving('', '', ours, link); paintBar(); return; } // nothing of this page's own to save, and a reload reopens onto the drawer @@ -1104,6 +1138,9 @@ const DRAWERS = (function () { if (ok) { if (refused()) land(); base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + } else if (sealed) { + // a refusal of this page's own, apart from any it arrived with (see rids) + rids = rids.concat(newRid()); sealed = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's @@ -1145,6 +1182,7 @@ const DRAWERS = (function () { moved on from comes back from the other page as the drawer has it (restore). */ handOver(d ? d.id : '', h, false, { link, marks: d ? marksFor(ours) : {}, ...carry(!!d && refused(), base) }); + sealed = true; // handed on: a refusal after this, from the cache, is another }, }; } diff --git a/src/ui.js b/src/ui.js index 5448ad6..aeae283 100644 --- a/src/ui.js +++ b/src/ui.js @@ -2206,7 +2206,7 @@ function parseHash(h) { * stops that being your problem. */ let hashSaveT = 0, hashReady = false; -// saves were on when Start fresh left the page (see startFresh) +// saves were on when Start fresh left the page, and nothing has left it since (see startFresh) let freshLeft = false; /* Kept on this browser, so the work survives arriving without a link. * @@ -2243,11 +2243,12 @@ const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); - freshLeft = hashReady; + const saving = hashReady; /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ leaveFor(location.origin + location.pathname); // drop the hash and reload clean + freshLeft = saving; // after leaveFor, which clears it } /* Wired here, before the boot below reads any link: a link that throws there must not also take away the button that gets you out of it, or stop the next link working. What @@ -2255,9 +2256,10 @@ function startFresh() { $('startFresh').addEventListener('click', startFresh); /* Left by Start fresh and shown again from the back-forward cache, as Back from the fresh page can be in some browsers: the page is yours again, and saves as it did before, as it - does loaded. Not after Put back or Try anyway, which leave by leaveFor too: loaded, that - page is a link again and sets your layout aside first, and from the cache its first save - would go over the layout just put back. */ + does loaded. Not after Put back or Try anyway, which leave by leaveFor too, even pressed + after Start fresh before the page has gone: loaded, that page is a link again and sets + your layout aside first, and from the cache its first save would go over the layout just + put back. */ addEventListener('pageshow', (e) => { if (e.persisted && freshLeft) { hashReady = true; freshLeft = false; } }); /* A hash this page did not write means someone navigated to a link — pasted a share URL into the address bar, or picked a bookmark — and changing only the fragment is a @@ -2327,7 +2329,7 @@ function linkKeys(h, link) { return [...SHARED_KEYS].filter((k) => k in q && p[k] === q[k]); } function leaveFor(url) { - hashReady = false; dropSave(); // no save of this page's may land after + hashReady = false; freshLeft = false; dropSave(); // no save of this page's may land after location.href = url; } /* Swapped rather than copied over: what is here now goes aside in its place, so putting diff --git a/sw.js b/sw.js index 87cafbb..68d022d 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "975b272bfc97"; +const VERSION = "92af5522f95f"; const FILES = [ "./", "bins/", diff --git a/test/ui/drawers.spec.js b/test/ui/drawers.spec.js index fd03f95..222055d 100644 --- a/test/ui/drawers.spec.js +++ b/test/ui/drawers.spec.js @@ -1216,11 +1216,14 @@ for (const tool of ['plates', 'bins']) { - on the other page, by setting it back; - there, by a change of that page's own, before it is set back; - as the other page arrives, by the save it makes then; - - before the hand-over, by the hand-over's own save. */ + - before the hand-over, by the save this page makes as it leaves, after which its + address's mark no longer says the size was refused. That one passes with no ids at + all, and is here to keep it so. */ for (const tool of ['plates', 'bins']) { - for (const [room, saved] of [['set back', 'as it is set back'], ['a change first', 'by a change there first'], - ['arriving', 'as that page arrives'], ['before the hand-over', 'by the hand-over']]) { - test(`Back to a ${tool} page whose refused size was saved on the other page and set back there ` + + for (const [room, saved] of [['set back', 'there as it is set back'], + ['a change first', 'there by a change first'], ['arriving', 'there as that page arrives'], + ['before the hand-over', 'here as it leaves for that page']]) { + test(`Back to a ${tool} page whose refused size was saved, then set back on the other page, ` + `is the size set back, saved ${saved}`, async ({ page }) => { const there = tool === 'plates' ? 'bins' : 'plates'; await page.addInitScript(fullStorage); @@ -1289,6 +1292,219 @@ for (const tool of ['plates', 'bins']) { }); } +/* And its address's mark says the size is the drawer's too. Left saying the size was + refused from the size before, a reload after another tab set the size back took the + page's size for its own change still to save, and wrote it over the one set back once + there was room. */ +for (const tool of ['plates', 'bins']) { + test(`a ${tool} page that caught up a refused size another tab saved, reloaded after that tab ` + + 'set it back, has the size set back', async ({ page, context }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + await inOtherTab(context, there, errors, (other) => H.setField(other, 'drawerW', '410')); + await page.evaluate(() => dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true }))); + await settle(page); + await inOtherTab(context, there, errors, (other) => H.setField(other, 'drawerW', '400')); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await page.reload(); + await expect.poll(() => page.inputValue('#drawerW').catch(() => ''), + { message: 'reloaded, caught up', timeout: 20000 }).toBe('400'); + await (tool === 'bins' ? binsReady : platesReady)(page); + await settle(page); + expect((await stored(page)).Kitchen.w, 'the size set back stays').toBe('400'); + expect(errors).toEqual([]); + }); +} + +/* Two pages, each holding a change the drawer refused: the size here, then the depth on + the other page it was handed over to. Back here, with room again, a change saves the + size. Forward to the other page still has its depth to save: that save had only this + page's change in it. With one id for the refusals of both pages, the save here counted + the depth as landed too, and Forward showed the drawer's depth. */ +for (const tool of ['plates', 'bins']) { + test(`Forward to the page a ${tool} page handed its refused size to keeps a depth refused there, ` + + 'after the size is saved on Back', async ({ page }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + const ready = (t) => (t === 'bins' ? binsReady : platesReady); + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await ready(there)(page); + expect(await page.inputValue('#drawerW'), 'handed over').toBe('410'); + await H.setField(page, 'drawerD', '510'); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + await settle(page); + + await page.goBack(); + await ready(tool)(page); + expect(await page.inputValue('#drawerW'), 'Back').toBe('410'); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'the size saved here', timeout: 20000 }).toBe('410'); + await settle(page); + + await page.goForward(); + await ready(there)(page); + await settle(page); + expect(await page.inputValue('#drawerD'), 'the depth refused there').toBe('510'); + expect(await page.inputValue('#drawerW')).toBe('410'); + await changeOwn(page, there); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'saved with the next change', timeout: 20000 }).toMatchObject({ w: '410', d: '510' }); + expect(errors).toEqual([]); + }); +} + +/* A page holding a size the drawer refused, saved as another drawer, where a change is + refused and then saved. Back to the page that made the size, still Kitchen's, keeps the + size: it went into the other drawer, never Kitchen. The refusal in the other drawer + took the id of Kitchen's, and its landing counted Kitchen's size as saved, so Back showed + Kitchen's size from before. */ +for (const tool of ['plates', 'bins']) { + test(`Back to a ${tool} page whose refused size went on into another drawer keeps the size`, + async ({ page }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + const ready = (t) => (t === 'bins' ? binsReady : platesReady); + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await ready(there)(page); + expect(await page.inputValue('#drawerW'), 'handed over').toBe('410'); + await settle(page); // its save as it arrives refused too, so the size never reaches Kitchen + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await saveAs(page, 'Pantry'); + expect((await stored(page)).Kitchen.w, 'Kitchen as it was').toBe('400'); + await page.evaluate(() => sessionStorage.setItem('full', '1')); + await changeOwn(page, there); + await expect(page.locator('#drawerName')).toHaveText('not saving · Pantry'); + await settle(page); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, there, ownChange2(there)); + const [, , key] = ownChange(there); + await expect.poll(() => stored(page).then((s) => s.Pantry), + { message: 'saved into the other drawer', timeout: 20000 }).toMatchObject({ [key]: ownChange2(there) }); + await settle(page); + + await page.goBack(); + await ready(tool)(page); + await settle(page); + expect(await page.inputValue('#drawerW'), 'Back, the size kept').toBe('410'); + await changeOwn(page, tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'and saved into Kitchen, with room', timeout: 20000 }).toBe('410'); + expect(errors).toEqual([]); + }); +} + +/* Back from the back-forward cache, for real: the page does not load again, and only its + pageshow sees the return. The browser the tests are given has the cache turned off, and + the headless shell it runs keeps nothing in it, so these launch the full Chromium build + with the cache on. `run` gets a page in it, which counts each return from the cache. */ +async function withCache(playwright, launchOptions, run) { + const opts = { ...launchOptions, ignoreDefaultArgs: ['--disable-back-forward-cache'] }; + if (!opts.executablePath) opts.channel = 'chromium'; + const browser = await playwright.chromium.launch(opts); + try { + const context = await browser.newContext({ serviceWorkers: 'block', + viewport: { width: 1440, height: 1000 } }); + const page = await context.newPage(); + await page.addInitScript(() => addEventListener('pageshow', (e) => { + if (!e.persisted) return; + sessionStorage.setItem('cached', String(Number(sessionStorage.getItem('cached') || 0) + 1)); + })); + await run(page); + } finally { + await browser.close(); + } +} +// Back, from the cache: by the page's own history, since a return from it fires no load to wait for +async function backFromCache(page) { + const count = () => page.evaluate(() => Number(sessionStorage.getItem('cached') || 0)).catch(() => -1); + const was = await count(); + await page.evaluate(() => history.back()); + await expect.poll(count, { message: 'shown again from the cache', timeout: 20000 }).toBe(was + 1); +} + +/* The size refused here is saved on the other page and set back there, as in the tests + above, and the page comes Back from the cache. Its refusal has landed since, so it is a + page of yours come back and catches up with the drawer. It took the size for a change of + its own still to save, and saved it over the one set back at its next change. */ +for (const tool of ['plates', 'bins']) { + test(`Back from the cache to a ${tool} page whose refused size was saved, then set back on the ` + + 'other page, is the size set back', async ({ playwright, launchOptions }) => { + await withCache(playwright, launchOptions, async (page) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await (there === 'bins' ? binsReady : platesReady)(page); + expect(await page.inputValue('#drawerW'), 'handed over').toBe('410'); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await H.setField(page, 'drawerW', '400'); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'saved there, set back', timeout: 20000 }).toBe('400'); + await settle(page); + + await backFromCache(page); + await expect.poll(() => page.inputValue('#drawerW').catch(() => ''), + { message: 'Back, caught up', timeout: 20000 }).toBe('400'); + await (tool === 'bins' ? binsReady : platesReady)(page); + await changeOwn(page, tool); + const [, v, key] = ownChange(tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'the next change keeps the size set back', timeout: 20000 }) + .toMatchObject({ w: '400', [key]: v }); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + expect(errors).toEqual([]); + }); + }); +} + +/* And when a change to the page's own half was refused with the size. The other page + saves the size, not that change, so the page come Back from the cache still says it is + not saving into the drawer, until its next change saves it. It said the drawer had its + changes, while the drawer had its own half from before. */ +for (const tool of ['plates', 'bins']) { + test(`Back from the cache to a ${tool} page whose refused size was saved on the other page, but not ` + + 'its own refused change, still says it is not saving', async ({ playwright, launchOptions }) => { + await withCache(playwright, launchOptions, async (page) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + const [field, v, key] = ownChange(tool); + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + const was = (await stored(page)).Kitchen[key]; + await page.evaluate(() => sessionStorage.setItem('full', '1')); + await changeOwn(page, tool); + await refuseSize(page); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await (there === 'bins' ? binsReady : platesReady)(page); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, there); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'the size saved there', timeout: 20000 }).toBe('410'); + await settle(page); + + await backFromCache(page); + await settle(page); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + expect(await page.inputValue('#' + field), 'its own change').toBe(v); + expect((await stored(page)).Kitchen[key], 'not in the drawer').toBe(was); + await H.setField(page, 'drawerD', '420'); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'saved with the next change', timeout: 20000 }) + .toMatchObject({ w: '410', d: '420', [key]: v }); + await expect(page.locator('#drawerName')).toHaveText('Kitchen'); + expect(errors).toEqual([]); + }); + }); +} + /* Each page saves its whole half of the drawer, so one whose half is older than the drawer's would write it back: a reload of a tab after another tab of the same tool changed the drawer, or Back to a page of yours that a later page has moved on. Both diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index a1fdaba..aa2060b 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -1158,3 +1158,35 @@ for (const tool of ['plates', 'bins']) { expect(errors).toEqual([]); }); } + +/* Not once Put back is pressed after it, before the fresh page arrives: Put back leaves last, + and that page, shown again from the cache, would save over the layout just put back. */ +for (const tool of ['plates', 'bins']) { + test(`a ${tool} page left by Start fresh, then Put back, and shown again from the cache does not save`, + async ({ page }) => { + const errors = watch(page); + const url = tool === 'bins' ? binsUrl() : platesUrl(); + const key = tool === 'bins' ? BINS : PLATES; + await arrive(page, url); + await H.setField(page, 'drawerW', '451'); + await saved(page); + await arrive(page, url + '#w=333&d=444&v=2'); // a link, which sets that layout aside + await expect(page.locator('#putBack')).toBeVisible(); + await page.route((u) => u.href === url, async (route) => { + await new Promise((r) => setTimeout(r, 5000)); + await route.continue().catch(() => {}); + }); + await page.evaluate(() => { + document.getElementById('startFresh').click(); + document.getElementById('putBack').click(); + window.stop(); // neither page arrives + dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true })); + }); + const put = await stored(page, key); + expect(put, 'the layout put back').toMatch(/(^|&)w=451(&|$)/); + await H.setField(page, 'drawerW', '377'); + await settle(page); + expect(await stored(page, key), 'still the layout put back').toBe(put); + expect(errors).toEqual([]); + }); +} From d231b639ee0867385dfc738c8dfc2b5526b754b4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 10 Oct 2026 09:07:25 +0000 Subject: [PATCH 9/9] Count each refusal id from its own base Each id a page holds now carries the base its own changes count from: the page's shared keys as it last sealed, arriving, saving or coming back from the cache. A record counts from its first id still to land, and an id counts as landed once it or any id after it has, since the page that held the later one saved the earlier ones' changes with its own. Counted from the first page's base, a later page whose save as it arrived was refused took the size handed to it for its own change after that size was saved and set back, and Forward to it wrote the size over the one set back. A page keeps the last 32 ids, the first of them counted from the page's base, so a long chain of hand-overs still saves the first page's size, and Back to the page holding the first id finds it landed. From the cache, a page whose drawer was deleted on the other page says it is not saved, and Start fresh pressed twice before the page goes still saves again from the cache. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XjNf7SQF9o4ZVeWiSbWQmk --- bins/index.html | 116 ++++++++++++++------ index.html | 116 ++++++++++++++------ src/bins/ui.js | 2 +- src/shared-ui/drawers.js | 114 ++++++++++++++------ src/ui.js | 2 +- sw.js | 2 +- test/ui/drawers.spec.js | 198 +++++++++++++++++++++++++++++++++- test/ui/hostile-links.spec.js | 15 +-- 8 files changed, 458 insertions(+), 107 deletions(-) diff --git a/bins/index.html b/bins/index.html index 6fb7093..ce3936d 100644 --- a/bins/index.html +++ b/bins/index.html @@ -7385,10 +7385,17 @@

Your drawers

on, by a hand-over or as it went into the back-forward cache (`sealed` till then). With one id for the whole chain, a save that went through on an earlier page counted a later page's change as landed too, though it had never been saved: Forward to that - page lost a depth refused there. */ + page lost a depth refused there. + Each id has the base its own changes are counted from (`bases`): the page's shared keys + as it last sealed (`sealedAt`), arriving, saving or coming back from the cache. Counted + from the first page's base, a size that page's refusal held stayed a change of the + later page's after it was saved and set back since, and Forward to the later page + wrote it over the one set back. */ let rids = []; + let bases = new Map(); + let sealedAt = null; let sealed = true; - const drop = () => { rids = []; sealed = true; }; + const drop = () => { rids = []; bases = new Map(); sealed = true; }; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -7419,16 +7426,18 @@

Your drawers

record: that it does, and the page's base `b`, its shared keys as it last saved them or arrived with them, which its changes are counted from (see attach). With the ids of the refusals it holds (see rids), so that once they land, its records can be told - apart from one still to save (see land). Nothing for any other page, whose base is - the design it hands on. */ + apart from one still to save (see land), and each one's base (`bs`, the page's own + but for a catch-up's). Nothing for any other page, whose base is the design it hands + on. */ const newRid = () => Math.random().toString(36).slice(2, 10) || 'r'; - const carry = (on, b) => { + const carry = (on, b, bs = bases) => { if (!(on && b)) return {}; - if (!rids.length) rids = [newRid()]; - return { refused: true, base: [...b], rids: rids.slice() }; + if (!rids.length) { rids = [newRid()]; bases = new Map([[rids[0], b]]); } + return { refused: true, base: [...b], rids: rids.slice(), bases: rids.map((id) => [...(bs.get(id) || b)]) }; }; // ids as a record or the tab's note has them, what carry makes and no more - const ridsIn = (v) => strings(v).filter((id) => /^[a-z0-9]{1,16}$/.test(id)); + const RID = /^[a-z0-9]{1,16}$/; + const ridsIn = (v) => strings(v).filter((id) => RID.test(id)); const landedIds = () => ridsIn(readNote('sessionStorage', TAB).landed); /* A save that went through while the page held refusals: their changes are in the drawer now, or were changed again before they got there, and either way every record @@ -7448,18 +7457,46 @@

Your drawers

} drop(); } - // whether every one of `ids` has landed, so a record holding them has nothing left to save - const allLanded = (ids) => { - const done = landedIds(); - return ids.length > 0 && ids.every((id) => done.includes(id)); + /* How many of `ids`, oldest first, have landed: all up to the last one that has. The + page that held it held those before it too, with their changes, so its save that went + through saved them, or a change since replaced them. A page that keeps only the last + of a long chain's ids (see hold) lands them so, and Back to one holding the first + found it still to save. */ + const landedTo = (ids) => { + const done = new Set(landedIds()); + let n = ids.length; + while (n > 0 && !done.has(ids[n - 1])) n -= 1; + return n; }; - /* The base a note, mark or record carries (see carry), or null for one that carries - none, or whose refusals have all landed since. */ + // a base as a record has it, the shared keys and no more + const baseIn = (b) => new Map((Array.isArray(b) ? b : []).filter((p) => Array.isArray(p) && + p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); + /* What a note, mark or record carries (see carry): its refusals still to land, oldest + first, as [id, base], and the base the page's changes are counted from, the first + one's. Null for one that carries none, or whose refusals have all landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; - if (allLanded(ridsIn(r.rids))) return null; - return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && - typeof p[1] === 'string' && SHARED.has(p[0]))); + const all = baseIn(r.base); + const ids = Array.isArray(r.rids) ? r.rids : []; + // a record from before each id had a base: all of them have the record's + const bs = Array.isArray(r.bases) && r.bases.length === ids.length ? r.bases : null; + const held = []; + for (let i = Math.max(0, ids.length - 64); i < ids.length; i += 1) { // more than a page keeps + if (typeof ids[i] === 'string' && RID.test(ids[i])) held.push([ids[i], bs ? baseIn(bs[i]) : all]); + } + const n = landedTo(held.map(([id]) => id)); + if (held.length && n === held.length) return null; + const rest = held.slice(n); + return { base: rest.length ? rest[0][1] : all, held: rest }; + } + /* The refusals `held` ([id, base], oldest first) as the page's own: the last 32, the + first of them counted from the page's base, which is the first one's, so the changes + of those let go are still counted. */ + function hold(held) { + const kept = held.slice(-32); + if (kept.length < held.length) kept[0] = [kept[0][0], base]; + rids = kept.map(([id]) => id); + bases = new Map(kept); } /* The note for the next page to load in this tab: the drawer ('' for none), and the fingerprint of the design that page will arrive with. `link` lists the drawer, bed @@ -7475,7 +7512,8 @@

Your drawers

n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids, bases: more.bases } + : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -7590,18 +7628,21 @@

Your drawers

const pairs = parsePairs(h) || []; const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); - const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + // a base with the keys caught up as the drawer has them: the page's, and each refusal's + const catching = (b) => new Map([...b, ...moved.map((k) => [k, stored.get(k)])]); + const caught = catching(base); + const caughtEach = new Map([...bases].map(([id, b]) => [id, catching(b)])); /* The page has the drawer's settings already: they are its base now, with nothing to reload, and its address's mark says so. Left with the base before, a reload after another tab set the size back took the page's size for its own change still to save, and wrote it over the one set back. */ if (merged === h) { - base = caught; + base = caught; bases = caughtEach; const m = refused() && markOf(h); if (m) markHere(h, m.link, m.note); return false; } - const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; + const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught, caughtEach) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; go(merged); @@ -7803,7 +7844,7 @@

Your drawers

return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; refusedIn = false; drop(); + base = sharedOf(h); sealedAt = base; failing = false; refusedIn = false; drop(); ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -7978,18 +8019,24 @@

Your drawers

addEventListener('pageshow', (e) => { if (!e.persisted || !attached) return; const d = find(loadAll(), attached); - if (!d) return; + if (!d) { paintBar(); return; } // deleted since: the bar says so const h = o.design(); /* Refusals that have all landed since, from a later page: this one is as any page come back. All but a change to its own half, which the later page did not save: refused with the size, it is still this page's alone, and the bar still says so until a save - goes through. */ - if (refused() && allLanded(rids)) { + goes through. Some of them landed: its changes are counted from the first still to + land. */ + const n = refused() ? landedTo(rids) : 0; + if (n && n === rids.length) { refusedIn = false; drop(); base = sharedOf(h); if (encodePairs(halfPairs(h)) === encodePairs(halfPairs(d.hash))) failing = false; paintBar(); + } else if (n) { + hold(rids.slice(n).map((id) => [id, bases.get(id) || base])); + base = bases.get(rids[0]); } sealed = true; // its records so far went into the cache with it + sealedAt = sharedOf(h); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -8108,7 +8155,7 @@

Your drawers

} if (d) { attached = d.id; - base = sharedOf(arrivedWith); + base = sharedOf(arrivedWith); sealedAt = base; // the record the design came with: the note, this tab's own mark, or a raced reload's const from = noted ? next : mine && mine.id === d.id ? mine : gone && gone.id === d.id ? gone : null; @@ -8134,7 +8181,7 @@

Your drawers

a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; rids = ridsIn(from.rids).slice(-32); } + if (kept) { refusedIn = true; base = kept.base; hold(kept.held); } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -8151,7 +8198,7 @@

Your drawers

d.marks[o.tool] = fp; if (saveAll(s)) { if (refused()) land(); - ours = fp; base = sharedOf(arrivedWith); refusedIn = false; + ours = fp; base = sharedOf(arrivedWith); sealedAt = base; refusedIn = false; } savedInto(d.id); } @@ -8187,10 +8234,15 @@

Your drawers

const ok = saveAll(s); if (ok) { if (refused()) land(); - base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + base = sharedOf(h); sealedAt = base; ours = d.marks[o.tool]; refusedIn = false; } else if (sealed) { - // a refusal of this page's own, apart from any it arrived with (see rids) - rids = rids.concat(newRid()); sealed = false; + /* A refusal of this page's own, apart from any it arrived with (see rids), counted + from the page as it last sealed. The first the page holds counts from its base, + which can be older: a change it arrived with, from a record with no ids, is + still to save. */ + const at = rids.length ? sealedAt || base : base; + hold(rids.map((id) => [id, bases.get(id) || base]).concat([[newRid(), at]])); + sealed = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's @@ -18578,7 +18630,7 @@

Your drawers

function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); - const saving = hashReady; + const saving = hashReady || freshLeft; // pressed again before the page goes: as the first press found it /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ diff --git a/index.html b/index.html index 49cd174..51907d7 100644 --- a/index.html +++ b/index.html @@ -7172,10 +7172,17 @@

Your drawers

on, by a hand-over or as it went into the back-forward cache (`sealed` till then). With one id for the whole chain, a save that went through on an earlier page counted a later page's change as landed too, though it had never been saved: Forward to that - page lost a depth refused there. */ + page lost a depth refused there. + Each id has the base its own changes are counted from (`bases`): the page's shared keys + as it last sealed (`sealedAt`), arriving, saving or coming back from the cache. Counted + from the first page's base, a size that page's refusal held stayed a change of the + later page's after it was saved and set back since, and Forward to the later page + wrote it over the one set back. */ let rids = []; + let bases = new Map(); + let sealedAt = null; let sealed = true; - const drop = () => { rids = []; sealed = true; }; + const drop = () => { rids = []; bases = new Map(); sealed = true; }; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -7206,16 +7213,18 @@

Your drawers

record: that it does, and the page's base `b`, its shared keys as it last saved them or arrived with them, which its changes are counted from (see attach). With the ids of the refusals it holds (see rids), so that once they land, its records can be told - apart from one still to save (see land). Nothing for any other page, whose base is - the design it hands on. */ + apart from one still to save (see land), and each one's base (`bs`, the page's own + but for a catch-up's). Nothing for any other page, whose base is the design it hands + on. */ const newRid = () => Math.random().toString(36).slice(2, 10) || 'r'; - const carry = (on, b) => { + const carry = (on, b, bs = bases) => { if (!(on && b)) return {}; - if (!rids.length) rids = [newRid()]; - return { refused: true, base: [...b], rids: rids.slice() }; + if (!rids.length) { rids = [newRid()]; bases = new Map([[rids[0], b]]); } + return { refused: true, base: [...b], rids: rids.slice(), bases: rids.map((id) => [...(bs.get(id) || b)]) }; }; // ids as a record or the tab's note has them, what carry makes and no more - const ridsIn = (v) => strings(v).filter((id) => /^[a-z0-9]{1,16}$/.test(id)); + const RID = /^[a-z0-9]{1,16}$/; + const ridsIn = (v) => strings(v).filter((id) => RID.test(id)); const landedIds = () => ridsIn(readNote('sessionStorage', TAB).landed); /* A save that went through while the page held refusals: their changes are in the drawer now, or were changed again before they got there, and either way every record @@ -7235,18 +7244,46 @@

Your drawers

} drop(); } - // whether every one of `ids` has landed, so a record holding them has nothing left to save - const allLanded = (ids) => { - const done = landedIds(); - return ids.length > 0 && ids.every((id) => done.includes(id)); + /* How many of `ids`, oldest first, have landed: all up to the last one that has. The + page that held it held those before it too, with their changes, so its save that went + through saved them, or a change since replaced them. A page that keeps only the last + of a long chain's ids (see hold) lands them so, and Back to one holding the first + found it still to save. */ + const landedTo = (ids) => { + const done = new Set(landedIds()); + let n = ids.length; + while (n > 0 && !done.has(ids[n - 1])) n -= 1; + return n; }; - /* The base a note, mark or record carries (see carry), or null for one that carries - none, or whose refusals have all landed since. */ + // a base as a record has it, the shared keys and no more + const baseIn = (b) => new Map((Array.isArray(b) ? b : []).filter((p) => Array.isArray(p) && + p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); + /* What a note, mark or record carries (see carry): its refusals still to land, oldest + first, as [id, base], and the base the page's changes are counted from, the first + one's. Null for one that carries none, or whose refusals have all landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; - if (allLanded(ridsIn(r.rids))) return null; - return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && - typeof p[1] === 'string' && SHARED.has(p[0]))); + const all = baseIn(r.base); + const ids = Array.isArray(r.rids) ? r.rids : []; + // a record from before each id had a base: all of them have the record's + const bs = Array.isArray(r.bases) && r.bases.length === ids.length ? r.bases : null; + const held = []; + for (let i = Math.max(0, ids.length - 64); i < ids.length; i += 1) { // more than a page keeps + if (typeof ids[i] === 'string' && RID.test(ids[i])) held.push([ids[i], bs ? baseIn(bs[i]) : all]); + } + const n = landedTo(held.map(([id]) => id)); + if (held.length && n === held.length) return null; + const rest = held.slice(n); + return { base: rest.length ? rest[0][1] : all, held: rest }; + } + /* The refusals `held` ([id, base], oldest first) as the page's own: the last 32, the + first of them counted from the page's base, which is the first one's, so the changes + of those let go are still counted. */ + function hold(held) { + const kept = held.slice(-32); + if (kept.length < held.length) kept[0] = [kept[0][0], base]; + rids = kept.map(([id]) => id); + bases = new Map(kept); } /* The note for the next page to load in this tab: the drawer ('' for none), and the fingerprint of the design that page will arrive with. `link` lists the drawer, bed @@ -7262,7 +7299,8 @@

Your drawers

n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids, bases: more.bases } + : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -7377,18 +7415,21 @@

Your drawers

const pairs = parsePairs(h) || []; const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); - const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + // a base with the keys caught up as the drawer has them: the page's, and each refusal's + const catching = (b) => new Map([...b, ...moved.map((k) => [k, stored.get(k)])]); + const caught = catching(base); + const caughtEach = new Map([...bases].map(([id, b]) => [id, catching(b)])); /* The page has the drawer's settings already: they are its base now, with nothing to reload, and its address's mark says so. Left with the base before, a reload after another tab set the size back took the page's size for its own change still to save, and wrote it over the one set back. */ if (merged === h) { - base = caught; + base = caught; bases = caughtEach; const m = refused() && markOf(h); if (m) markHere(h, m.link, m.note); return false; } - const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; + const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught, caughtEach) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; go(merged); @@ -7590,7 +7631,7 @@

Your drawers

return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; refusedIn = false; drop(); + base = sharedOf(h); sealedAt = base; failing = false; refusedIn = false; drop(); ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -7765,18 +7806,24 @@

Your drawers

addEventListener('pageshow', (e) => { if (!e.persisted || !attached) return; const d = find(loadAll(), attached); - if (!d) return; + if (!d) { paintBar(); return; } // deleted since: the bar says so const h = o.design(); /* Refusals that have all landed since, from a later page: this one is as any page come back. All but a change to its own half, which the later page did not save: refused with the size, it is still this page's alone, and the bar still says so until a save - goes through. */ - if (refused() && allLanded(rids)) { + goes through. Some of them landed: its changes are counted from the first still to + land. */ + const n = refused() ? landedTo(rids) : 0; + if (n && n === rids.length) { refusedIn = false; drop(); base = sharedOf(h); if (encodePairs(halfPairs(h)) === encodePairs(halfPairs(d.hash))) failing = false; paintBar(); + } else if (n) { + hold(rids.slice(n).map((id) => [id, bases.get(id) || base])); + base = bases.get(rids[0]); } sealed = true; // its records so far went into the cache with it + sealedAt = sharedOf(h); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -7895,7 +7942,7 @@

Your drawers

} if (d) { attached = d.id; - base = sharedOf(arrivedWith); + base = sharedOf(arrivedWith); sealedAt = base; // the record the design came with: the note, this tab's own mark, or a raced reload's const from = noted ? next : mine && mine.id === d.id ? mine : gone && gone.id === d.id ? gone : null; @@ -7921,7 +7968,7 @@

Your drawers

a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; rids = ridsIn(from.rids).slice(-32); } + if (kept) { refusedIn = true; base = kept.base; hold(kept.held); } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -7938,7 +7985,7 @@

Your drawers

d.marks[o.tool] = fp; if (saveAll(s)) { if (refused()) land(); - ours = fp; base = sharedOf(arrivedWith); refusedIn = false; + ours = fp; base = sharedOf(arrivedWith); sealedAt = base; refusedIn = false; } savedInto(d.id); } @@ -7974,10 +8021,15 @@

Your drawers

const ok = saveAll(s); if (ok) { if (refused()) land(); - base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + base = sharedOf(h); sealedAt = base; ours = d.marks[o.tool]; refusedIn = false; } else if (sealed) { - // a refusal of this page's own, apart from any it arrived with (see rids) - rids = rids.concat(newRid()); sealed = false; + /* A refusal of this page's own, apart from any it arrived with (see rids), counted + from the page as it last sealed. The first the page holds counts from its base, + which can be older: a change it arrived with, from a record with no ids, is + still to save. */ + const at = rids.length ? sealedAt || base : base; + hold(rids.map((id) => [id, bases.get(id) || base]).concat([[newRid(), at]])); + sealed = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's @@ -10279,7 +10331,7 @@

Your drawers

function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); - const saving = hashReady; + const saving = hashReady || freshLeft; // pressed again before the page goes: as the first press found it /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ diff --git a/src/bins/ui.js b/src/bins/ui.js index 6de2edf..ce0dd88 100644 --- a/src/bins/ui.js +++ b/src/bins/ui.js @@ -5362,7 +5362,7 @@ const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); - const saving = hashReady; + const saving = hashReady || freshLeft; // pressed again before the page goes: as the first press found it /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ diff --git a/src/shared-ui/drawers.js b/src/shared-ui/drawers.js index ee6fb52..39f2ca3 100644 --- a/src/shared-ui/drawers.js +++ b/src/shared-ui/drawers.js @@ -335,10 +335,17 @@ const DRAWERS = (function () { on, by a hand-over or as it went into the back-forward cache (`sealed` till then). With one id for the whole chain, a save that went through on an earlier page counted a later page's change as landed too, though it had never been saved: Forward to that - page lost a depth refused there. */ + page lost a depth refused there. + Each id has the base its own changes are counted from (`bases`): the page's shared keys + as it last sealed (`sealedAt`), arriving, saving or coming back from the cache. Counted + from the first page's base, a size that page's refusal held stayed a change of the + later page's after it was saved and set back since, and Forward to the later page + wrote it over the one set back. */ let rids = []; + let bases = new Map(); + let sealedAt = null; let sealed = true; - const drop = () => { rids = []; sealed = true; }; + const drop = () => { rids = []; bases = new Map(); sealed = true; }; /* This tool's mark in the drawer at the page's last save into it that went through, or as it arrived: the page's half is that save or newer. Newer when the browser refused the saves since (storage full), and then the address is the one copy of the @@ -369,16 +376,18 @@ const DRAWERS = (function () { record: that it does, and the page's base `b`, its shared keys as it last saved them or arrived with them, which its changes are counted from (see attach). With the ids of the refusals it holds (see rids), so that once they land, its records can be told - apart from one still to save (see land). Nothing for any other page, whose base is - the design it hands on. */ + apart from one still to save (see land), and each one's base (`bs`, the page's own + but for a catch-up's). Nothing for any other page, whose base is the design it hands + on. */ const newRid = () => Math.random().toString(36).slice(2, 10) || 'r'; - const carry = (on, b) => { + const carry = (on, b, bs = bases) => { if (!(on && b)) return {}; - if (!rids.length) rids = [newRid()]; - return { refused: true, base: [...b], rids: rids.slice() }; + if (!rids.length) { rids = [newRid()]; bases = new Map([[rids[0], b]]); } + return { refused: true, base: [...b], rids: rids.slice(), bases: rids.map((id) => [...(bs.get(id) || b)]) }; }; // ids as a record or the tab's note has them, what carry makes and no more - const ridsIn = (v) => strings(v).filter((id) => /^[a-z0-9]{1,16}$/.test(id)); + const RID = /^[a-z0-9]{1,16}$/; + const ridsIn = (v) => strings(v).filter((id) => RID.test(id)); const landedIds = () => ridsIn(readNote('sessionStorage', TAB).landed); /* A save that went through while the page held refusals: their changes are in the drawer now, or were changed again before they got there, and either way every record @@ -398,18 +407,46 @@ const DRAWERS = (function () { } drop(); } - // whether every one of `ids` has landed, so a record holding them has nothing left to save - const allLanded = (ids) => { - const done = landedIds(); - return ids.length > 0 && ids.every((id) => done.includes(id)); + /* How many of `ids`, oldest first, have landed: all up to the last one that has. The + page that held it held those before it too, with their changes, so its save that went + through saved them, or a change since replaced them. A page that keeps only the last + of a long chain's ids (see hold) lands them so, and Back to one holding the first + found it still to save. */ + const landedTo = (ids) => { + const done = new Set(landedIds()); + let n = ids.length; + while (n > 0 && !done.has(ids[n - 1])) n -= 1; + return n; }; - /* The base a note, mark or record carries (see carry), or null for one that carries - none, or whose refusals have all landed since. */ + // a base as a record has it, the shared keys and no more + const baseIn = (b) => new Map((Array.isArray(b) ? b : []).filter((p) => Array.isArray(p) && + p.length === 2 && typeof p[0] === 'string' && typeof p[1] === 'string' && SHARED.has(p[0]))); + /* What a note, mark or record carries (see carry): its refusals still to land, oldest + first, as [id, base], and the base the page's changes are counted from, the first + one's. Null for one that carries none, or whose refusals have all landed since. */ function carried(r) { if (!isPlain(r) || r.refused !== true || !Array.isArray(r.base)) return null; - if (allLanded(ridsIn(r.rids))) return null; - return new Map(r.base.filter((p) => Array.isArray(p) && p.length === 2 && typeof p[0] === 'string' && - typeof p[1] === 'string' && SHARED.has(p[0]))); + const all = baseIn(r.base); + const ids = Array.isArray(r.rids) ? r.rids : []; + // a record from before each id had a base: all of them have the record's + const bs = Array.isArray(r.bases) && r.bases.length === ids.length ? r.bases : null; + const held = []; + for (let i = Math.max(0, ids.length - 64); i < ids.length; i += 1) { // more than a page keeps + if (typeof ids[i] === 'string' && RID.test(ids[i])) held.push([ids[i], bs ? baseIn(bs[i]) : all]); + } + const n = landedTo(held.map(([id]) => id)); + if (held.length && n === held.length) return null; + const rest = held.slice(n); + return { base: rest.length ? rest[0][1] : all, held: rest }; + } + /* The refusals `held` ([id, base], oldest first) as the page's own: the last 32, the + first of them counted from the page's base, which is the first one's, so the changes + of those let go are still counted. */ + function hold(held) { + const kept = held.slice(-32); + if (kept.length < held.length) kept[0] = [kept[0][0], base]; + rids = kept.map(([id]) => id); + bases = new Map(kept); } /* The note for the next page to load in this tab: the drawer ('' for none), and the fingerprint of the design that page will arrive with. `link` lists the drawer, bed @@ -425,7 +462,8 @@ const DRAWERS = (function () { n.next = { id, fp: fingerprint(h), caughtUp: !!caughtUp, link: (more && more.link) || [], open: !!(more && more.open), marks: (more && more.marks) || {}, - ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids } : {}) }; + ...(more && more.refused ? { refused: true, base: more.base, rids: more.rids, bases: more.bases } + : {}) }; return writeNote('sessionStorage', TAB, n); } /* With it, the record of the address this tool's last save left (see wrote), which is @@ -540,18 +578,21 @@ const DRAWERS = (function () { const pairs = parsePairs(h) || []; const merged = encodePairs(pairs.map(([k, v]) => [k, moved.includes(k) ? stored.get(k) : v]) .concat(moved.filter((k) => !pairs.some((p) => p[0] === k)).map((k) => [k, stored.get(k)]))); - const caught = new Map([...base, ...moved.map((k) => [k, stored.get(k)])]); + // a base with the keys caught up as the drawer has them: the page's, and each refusal's + const catching = (b) => new Map([...b, ...moved.map((k) => [k, stored.get(k)])]); + const caught = catching(base); + const caughtEach = new Map([...bases].map(([id, b]) => [id, catching(b)])); /* The page has the drawer's settings already: they are its base now, with nothing to reload, and its address's mark says so. Left with the base before, a reload after another tab set the size back took the page's size for its own change still to save, and wrote it over the one set back. */ if (merged === h) { - base = caught; + base = caught; bases = caughtEach; const m = refused() && markOf(h); if (m) markHere(h, m.link, m.note); return false; } - const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught) }; + const more = { open: true, marks: marksFor(ours), ...carry(refused(), caught, caughtEach) }; // without the note the reloaded page could not tell it is this drawer, so stay put if (!handOver(d.id, merged, true, more)) return false; go(merged); @@ -753,7 +794,7 @@ const DRAWERS = (function () { return; } attached = d.id; armed = ''; renaming = ''; - base = sharedOf(h); failing = false; refusedIn = false; drop(); + base = sharedOf(h); sealedAt = base; failing = false; refusedIn = false; drop(); ours = d.marks[o.tool]; theirs = undefined; // the other tool has no mark in a new drawer savedInto(d.id); @@ -928,18 +969,24 @@ const DRAWERS = (function () { addEventListener('pageshow', (e) => { if (!e.persisted || !attached) return; const d = find(loadAll(), attached); - if (!d) return; + if (!d) { paintBar(); return; } // deleted since: the bar says so const h = o.design(); /* Refusals that have all landed since, from a later page: this one is as any page come back. All but a change to its own half, which the later page did not save: refused with the size, it is still this page's alone, and the bar still says so until a save - goes through. */ - if (refused() && allLanded(rids)) { + goes through. Some of them landed: its changes are counted from the first still to + land. */ + const n = refused() ? landedTo(rids) : 0; + if (n && n === rids.length) { refusedIn = false; drop(); base = sharedOf(h); if (encodePairs(halfPairs(h)) === encodePairs(halfPairs(d.hash))) failing = false; paintBar(); + } else if (n) { + hold(rids.slice(n).map((id) => [id, bases.get(id) || base])); + base = bases.get(rids[0]); } sealed = true; // its records so far went into the cache with it + sealedAt = sharedOf(h); if (movedOn(d, h, ours)) reopen(d); // a later page saved into it else catchUp(h, d); }); @@ -1058,7 +1105,7 @@ const DRAWERS = (function () { } if (d) { attached = d.id; - base = sharedOf(arrivedWith); + base = sharedOf(arrivedWith); sealedAt = base; // the record the design came with: the note, this tab's own mark, or a raced reload's const from = noted ? next : mine && mine.id === d.id ? mine : gone && gone.id === d.id ? gone : null; @@ -1084,7 +1131,7 @@ const DRAWERS = (function () { a size that page had kept from before another tab moved it on was taken for the change, and its next save wrote it over that tab's. */ const kept = carried(from); - if (kept) { refusedIn = true; base = kept; rids = ridsIn(from.rids).slice(-32); } + if (kept) { refusedIn = true; base = kept.base; hold(kept.held); } /* A hand-over, onto a drawer this tool has saved into. One carrying a link's settings too: restore keeps the settings the other page sets, which are the link's, and this tool's half was saved into the drawer with them. */ @@ -1101,7 +1148,7 @@ const DRAWERS = (function () { d.marks[o.tool] = fp; if (saveAll(s)) { if (refused()) land(); - ours = fp; base = sharedOf(arrivedWith); refusedIn = false; + ours = fp; base = sharedOf(arrivedWith); sealedAt = base; refusedIn = false; } savedInto(d.id); } @@ -1137,10 +1184,15 @@ const DRAWERS = (function () { const ok = saveAll(s); if (ok) { if (refused()) land(); - base = sharedOf(h); ours = d.marks[o.tool]; refusedIn = false; + base = sharedOf(h); sealedAt = base; ours = d.marks[o.tool]; refusedIn = false; } else if (sealed) { - // a refusal of this page's own, apart from any it arrived with (see rids) - rids = rids.concat(newRid()); sealed = false; + /* A refusal of this page's own, apart from any it arrived with (see rids), counted + from the page as it last sealed. The first the page holds counts from its base, + which can be older: a change it arrived with, from a record with no ids, is + still to save. */ + const at = rids.length ? sealedAt || base : base; + hold(rids.map((id) => [id, bases.get(id) || base]).concat([[newRid(), at]])); + sealed = false; } savedInto(d.id); /* Refused, the drawer still holds the save before, and the address has the page's diff --git a/src/ui.js b/src/ui.js index c4f3d6c..ff41155 100644 --- a/src/ui.js +++ b/src/ui.js @@ -2247,7 +2247,7 @@ const isLayoutHash = (h) => /(^|&)[^&=]+=/.test(h); function startFresh() { try { localStorage.removeItem(SAVE_KEY); } catch (err) { /* nothing to clear */ } forgetPasted(); - const saving = hashReady; + const saving = hashReady || freshLeft; // pressed again before the page goes: as the first press found it /* Left as Put back leaves, by leaveFor: the page runs on until the bare one arrives, and a change that landed in between was saved after the clearing, and came back with the note that it had been restored. */ diff --git a/sw.js b/sw.js index 30fd897..fea3fab 100644 --- a/sw.js +++ b/sw.js @@ -52,7 +52,7 @@ load from this site, and the app's icons and manifest, as paths from the root. VERSION is a hash of all of those files, names and bytes, and of this script as it is served, FILES filled in and everything else but VERSION itself. */ -const VERSION = "fa20a51ceb4e"; +const VERSION = "2f013a52cdc1"; const FILES = [ "./", "bins/", diff --git a/test/ui/drawers.spec.js b/test/ui/drawers.spec.js index 222055d..d0a1bf8 100644 --- a/test/ui/drawers.spec.js +++ b/test/ui/drawers.spec.js @@ -1359,6 +1359,47 @@ for (const tool of ['plates', 'bins']) { }); } +/* And the size, saved on Back and then set back there, is not a change of the later page's. + That page's own save as it arrived was refused too, so it holds a refusal of its own, + still to save, with the size in what it arrived with. Counted from the first page's base, + the size stayed its change, and Forward to it wrote the size back over the one set back. */ +for (const tool of ['plates', 'bins']) { + test(`Forward to the page a ${tool} page handed its refused size to has the size set back on Back ` + + 'since', async ({ page }) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + const ready = (t) => (t === 'bins' ? binsReady : platesReady); + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await ready(there)(page); + expect(await page.inputValue('#drawerW'), 'handed over').toBe('410'); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + await settle(page); // its save as it arrives, refused + + await page.goBack(); + await ready(tool)(page); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'the size saved here', timeout: 20000 }).toBe('410'); + await H.setField(page, 'drawerW', '400'); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'and set back', timeout: 20000 }).toBe('400'); + await settle(page); + + await page.goForward(); + await expect.poll(() => page.inputValue('#drawerW').catch(() => ''), + { message: 'Forward, the size set back', timeout: 20000 }).toBe('400'); + await ready(there)(page); + await changeOwn(page, there); + const [, v, key] = ownChange(there); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'the next change keeps it', timeout: 20000 }).toMatchObject({ w: '400', [key]: v }); + expect(errors).toEqual([]); + }); +} + /* A page holding a size the drawer refused, saved as another drawer, where a change is refused and then saved. Back to the page that made the size, still Kitchen's, keeps the size: it went into the other drawer, never Kitchen. The refusal in the other drawer @@ -1401,6 +1442,94 @@ for (const tool of ['plates', 'bins']) { }); } +/* A long run of hand-overs with the storage full, each page's save as it arrives refused, so + each holds a refusal of its own: more than the 32 ids a page keeps. The last page still + counts the first page's size as a change, and saves it with room. Set back there, Back to + the page before it, which holds the first id, has the size set back: the page that saved + held the ids after it, with its change. Counted from the first id it kept, the last page + took the size for none of its own, and with the first id still to land, Back to the page + before wrote the size over the one set back. */ +test('after more hand-overs than a page keeps ids for, a refused size is saved, and stays set back', + async ({ page }) => { + test.setTimeout(300000); + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, 'plates'); + await refuseSize(page); + let tool = 'plates'; + for (let i = 0; i < 33; i += 1) { + tool = tool === 'plates' ? 'bins' : 'plates'; + await page.click(tool === 'bins' ? '#navBins' : '#navPlates'); + await (tool === 'bins' ? binsReady : platesReady)(page); + await expect(page.locator('#drawerName'), 'its save refused').toHaveText('not saving · Kitchen'); + } + expect(await page.inputValue('#drawerW')).toBe('410'); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'the size saved, with room', timeout: 20000 }).toBe('410'); + await H.setField(page, 'drawerW', '400'); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'and set back', timeout: 20000 }).toBe('400'); + await settle(page); + + const before = tool === 'plates' ? 'bins' : 'plates'; + await page.goBack(); + await expect.poll(() => page.inputValue('#drawerW').catch(() => ''), + { message: 'Back, the size set back', timeout: 20000 }).toBe('400'); + await (before === 'bins' ? binsReady : platesReady)(page); + await changeOwn(page, before); + const [, v, key] = ownChange(before); + await expect.poll(() => stored(page).then((s) => s.Kitchen), + { message: 'the next change keeps it', timeout: 20000 }).toMatchObject({ w: '400', [key]: v }); + expect(errors).toEqual([]); + }); + +/* The refusals' ids are only what the page makes, however a record or the tab's note has + them. A mark whose ids are not, landed in the tab's note as they are, is a refused size + still to save, and so after the page's own refused save, reloaded again, with none of + those ids, a long one among them, in its mark since. So is one whose bases are not lists, + with far more ids landed than the tab keeps. No error, and with room the size is saved, + and the tab keeps the last 128 landed. */ +test('a refused size outlives refusal ids and bases that are not what the page writes', async ({ page }) => { + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, 'plates'); + await refuseSize(page); + const reloaded = async (why) => { + await page.reload(); + await platesReady(page); + await settle(page); + expect(await page.inputValue('#drawerW'), why).toBe('410'); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + }; + const spoil = (bad) => page.evaluate((bad) => { + const st = history.state.drawerforge; + const K = 'drawerforge:drawers:tab'; + const n = JSON.parse(sessionStorage.getItem(K) || '{}'); + if (bad === 'ids') { + const rids = ['Zz', '__proto__', 5, null, 'z'.repeat(1000000)]; + history.replaceState({ drawerforge: { ...st, rids, bases: 'x' } }, ''); + n.landed = ['Zz', '__proto__', '5', 'null']; + } else { + history.replaceState({ drawerforge: { ...st, bases: [{}, 'x', [[1, 2]]] } }, ''); + n.landed = Array.from({ length: 100000 }, (_, i) => 'z' + i); + } + sessionStorage.setItem(K, JSON.stringify(n)); + }, bad); + await spoil('ids'); + await reloaded('ids that are not'); + await reloaded('and again, after its own save refused'); + expect(await page.evaluate(() => JSON.stringify(history.state).length), 'its mark').toBeLessThan(10000); + await spoil('bases'); + await reloaded('bases that are not'); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, 'plates'); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'saved, with room', timeout: 20000 }).toBe('410'); + const landed = await page.evaluate(() => JSON.parse(sessionStorage.getItem('drawerforge:drawers:tab')).landed); + expect(landed, 'the last 128 landed').toHaveLength(128); + expect(errors).toEqual([]); +}); + /* Back from the back-forward cache, for real: the page does not load again, and only its pageshow sees the return. The browser the tests are given has the cache turned off, and the headless shell it runs keeps nothing in it, so these launch the full Chromium build @@ -1422,11 +1551,11 @@ async function withCache(playwright, launchOptions, run) { await browser.close(); } } -// Back, from the cache: by the page's own history, since a return from it fires no load to wait for -async function backFromCache(page) { +// Back from the cache, or Forward: by the page's own history, as a return from it fires no load +async function backFromCache(page, forward) { const count = () => page.evaluate(() => Number(sessionStorage.getItem('cached') || 0)).catch(() => -1); const was = await count(); - await page.evaluate(() => history.back()); + await page.evaluate((f) => (f ? history.forward() : history.back()), !!forward); await expect.poll(count, { message: 'shown again from the cache', timeout: 20000 }).toBe(was + 1); } @@ -1505,6 +1634,69 @@ for (const tool of ['plates', 'bins']) { }); } +/* A refusal made after the page came back from the cache is another, with an id of its own, + counted from the page as it came back: its records so far went on with the hand-over, and + the other page holds them. Under the id that page held, its save counted this page's later + size as saved, and Back to it caught up with that page's. And with the first id landed, + counted from that one's base, the size had moved on since, and was caught up the same. */ +for (const tool of ['plates', 'bins']) { + test(`Back from the cache to a ${tool} page keeps a size refused after the cache gave it back, ` + + 'once the other page saves the one before', async ({ playwright, launchOptions }) => { + await withCache(playwright, launchOptions, async (page) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + await page.addInitScript(fullStorage); + const errors = await kitchenOn(page, tool); + await refuseSize(page); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await (there === 'bins' ? binsReady : platesReady)(page); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + await settle(page); + + await backFromCache(page); + await H.setField(page, 'drawerW', '420'); + await expect(page.locator('#drawerName')).toHaveText('not saving · Kitchen'); + await settle(page); + await backFromCache(page, true); + await page.evaluate(() => sessionStorage.setItem('full', '0')); + await changeOwn(page, there); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'the size saved there', timeout: 20000 }).toBe('410'); + await settle(page); + + await backFromCache(page); + await settle(page); + expect(await page.inputValue('#drawerW'), 'the size refused here').toBe('420'); + await changeOwn(page, tool); + await expect.poll(() => stored(page).then((s) => s.Kitchen.w), + { message: 'saved with the next change', timeout: 20000 }).toBe('420'); + expect(errors).toEqual([]); + }); + }); +} + +/* And from the cache after its drawer was deleted on the other page: it is not saved, and + the bar says so, as it does loaded. It still named the drawer. */ +for (const tool of ['plates', 'bins']) { + test(`Back from the cache to a ${tool} page whose drawer was deleted on the other page says it is ` + + 'not saved', async ({ playwright, launchOptions }) => { + await withCache(playwright, launchOptions, async (page) => { + const there = tool === 'plates' ? 'bins' : 'plates'; + const errors = await kitchenOn(page, tool); + await page.click(there === 'bins' ? '#navBins' : '#navPlates'); + await (there === 'bins' ? binsReady : platesReady)(page); + await openDialog(page); + await page.getByRole('button', { name: 'Delete Kitchen', exact: true }).click(); + await page.getByRole('button', { name: 'Really delete? Kitchen', exact: true }).click(); + await closeDialog(page); + await expect(page.locator('#drawerName')).toHaveText('not saved'); + + await backFromCache(page); + await expect(page.locator('#drawerName')).toHaveText('not saved'); + expect(errors).toEqual([]); + }); + }); +} + /* Each page saves its whole half of the drawer, so one whose half is older than the drawer's would write it back: a reload of a tab after another tab of the same tool changed the drawer, or Back to a page of yours that a later page has moved on. Both diff --git a/test/ui/hostile-links.spec.js b/test/ui/hostile-links.spec.js index aa2060b..b02ec6f 100644 --- a/test/ui/hostile-links.spec.js +++ b/test/ui/hostile-links.spec.js @@ -1134,9 +1134,12 @@ for (const tool of ['plates', 'bins']) { back-forward cache can show it again on Back. Shown again, it is a page of yours, and its changes save, as they did before Start fresh and as they do when it is loaded again. Chromium does not keep it, so here the page is stopped before the fresh one arrives and - shown again as the cache would. Both pages are covered. */ -for (const tool of ['plates', 'bins']) { - test(`a ${tool} page left by Start fresh and shown again from the cache saves again`, async ({ page }) => { + shown again as the cache would. Both pages are covered, and so is Start fresh pressed twice + before the page goes. The second press found saves already stopped by the first, and the + page shown again from the cache did not save. */ +for (const tool of ['plates', 'bins']) for (const presses of [1, 2]) { + test(`a ${tool} page left by Start fresh${presses === 2 ? ', pressed twice,' : ''} and shown again from ` + + 'the cache saves again', async ({ page }) => { const errors = watch(page); const url = tool === 'bins' ? binsUrl() : platesUrl(); await arrive(page, url); @@ -1147,11 +1150,11 @@ for (const tool of ['plates', 'bins']) { await new Promise((r) => setTimeout(r, 5000)); await route.continue().catch(() => {}); }); - await page.evaluate(() => { - document.getElementById('startFresh').click(); + await page.evaluate((presses) => { + for (let i = 0; i < presses; i += 1) document.getElementById('startFresh').click(); window.stop(); // the fresh page never arrives dispatchEvent(new PageTransitionEvent('pageshow', { persisted: true })); - }); + }, presses); await H.setField(page, 'drawerW', '377'); await saved(page); expect(await stored(page, tool === 'bins' ? BINS : PLATES)).toMatch(/(^|&)w=377(&|$)/);