Public: anyone can read this; the crew can change it.
small: Escape leaves the tell box and keeps its draft per page #16774
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15031-small-escape-leaves-the-tell-box-and-kee → main 0 commits fork
4 files
changed +37
−1
src/web/draft.ts +6
| @@ -38,6 +38,12 @@ function forgeDraft(storage, box, key, later) { | ||
| 38 | 38 | var mine = round; |
| 39 | 39 | later(function () { if (mine === round) save(); }, DELAY); |
| 40 | 40 | }, |
| 41 | // Leaving the box (Escape, a click away): save now, so a reload inside the delay keeps the words. | |
| 42 | leave: function () { | |
| 43 | if (!pending) return; | |
| 44 | round++; | |
| 45 | save(); | |
| 46 | }, | |
| 41 | 47 | // A refused send: give the words back unless new ones have been typed. |
| 42 | 48 | refused: function (text) { |
| 43 | 49 | if (!box.value) box.value = text; |
src/web/studio/feed.ts +1
| @@ -792,6 +792,7 @@ ${returnTargetJs} | ||
| 792 | 792 | draft = forgeDraft(window.localStorage, draftBox, 'draft.' + draftBox.dataset.draft + location.pathname, function (fn, ms) { setTimeout(fn, ms); }); |
| 793 | 793 | draft.restore(); |
| 794 | 794 | draftBox.addEventListener('input', draft.typed); |
| 795 | draftBox.addEventListener('blur', draft.leave); | |
| 795 | 796 | forgeSendKey(draftBox, composer); |
| 796 | 797 | } |
| 797 | 798 | // An empty feed offers the box: the cursor goes in only where there is a keyboard and a |
test/composer-draft.test.ts +13
| @@ -232,3 +232,16 @@ test("a tell sent from the feed's composer comes back to the feed, the forge's t | ||
| 232 | 232 | expect(new URL(res.headers.get("location") ?? "", servers.url).pathname).toBe("/feed"); |
| 233 | 233 | } |
| 234 | 234 | }); |
| 235 | ||
| 236 | test("leaving the box saves a waiting draft at once, so Escape then reload keeps it", () => { | |
| 237 | const { store, box, timers, draft } = rig(); | |
| 238 | box.value = "make the gate green"; draft.typed(); | |
| 239 | expect(store.getItem("draft.u1/")).toBeNull(); | |
| 240 | (draft as unknown as { leave(): void }).leave(); // Escape blurs the box | |
| 241 | expect(store.getItem("draft.u1/")).toBe("make the gate green"); | |
| 242 | timers.shift()!(); // the old timer is spent: no second save | |
| 243 | box.value = ""; | |
| 244 | (draft as unknown as { leave(): void }).leave(); // nothing typed since: leaving saves nothing | |
| 245 | expect(store.getItem("draft.u1/")).toBe("make the gate green"); | |
| 246 | expect(feedJs).toContain("addEventListener('blur', draft.leave)"); | |
| 247 | }); |
test/tell-key.test.ts +17 −1
| @@ -103,7 +103,7 @@ test("Escape keeps the draft per page in the browser's store; the next visit's t | ||
| 103 | 103 | const make = (w: Window, path: string) => |
| 104 | 104 | new Function("storage", "box", "key", "later", `${draftJs}\nreturn forgeDraft(storage, box, key, later);`)( |
| 105 | 105 | store, w.document.querySelector("textarea"), `draft.u1${path}`, later, |
| 106 | ) as { restore: () => void; typed: () => void }; | |
| 106 | ) as { restore: () => void; typed: () => void; leave: () => void }; | |
| 107 | 107 | const box = first.w.document.querySelector("textarea") as unknown as { value: string }; |
| 108 | 108 | make(first.w, "/feed").restore(); |
| 109 | 109 | first.press("t"); |
| @@ -124,6 +124,22 @@ test("Escape keeps the draft per page in the browser's store; the next visit's t | ||
| 124 | 124 | expect((other.w.document.querySelector("textarea") as unknown as { value: string }).value).toBe(""); |
| 125 | 125 | }); |
| 126 | 126 | |
| 127 | test("type, Escape at once, reload: the draft is back without waiting for the save timer", () => { | |
| 128 | const first = page("/feed", FEED); | |
| 129 | const store = first.w.localStorage; | |
| 130 | const draft = new Function("storage", "box", "key", "later", `${draftJs}\nreturn forgeDraft(storage, box, key, later);`)( | |
| 131 | store, first.w.document.querySelector("textarea"), "draft.u1/feed", () => {}, | |
| 132 | ) as { restore: () => void; typed: () => void; leave: () => void }; | |
| 133 | const box = first.w.document.querySelector("textarea") as unknown as { value: string; addEventListener: (n: string, f: () => void) => void }; | |
| 134 | box.addEventListener("input", draft.typed); | |
| 135 | box.addEventListener("blur", draft.leave); | |
| 136 | first.press("t"); | |
| 137 | box.value = "make the gate green"; | |
| 138 | draft.typed(); // the timer never fires | |
| 139 | first.press("Escape", box); | |
| 140 | expect(store.getItem("draft.u1/feed")).toBe("make the gate green"); | |
| 141 | }); | |
| 142 | ||
| 127 | 143 | test("a page that owns t keeps it: the shell's row never fires", () => { |
| 128 | 144 | const { w, press, active } = page("/next", `${FEED}<a href="#top" data-shortcut="t">Top</a>`); |
| 129 | 145 | let clicked = 0; |
new above
offline