Public: anyone can read this; the crew can change it.
The feed composer sends on Ctrl/Cmd+Enter and keeps an unsent draft across a reload #13550
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13521-the-feed-composer-sends-on-ctrl-cmd-ente → main 7 commits fork 3 threads · 3 unresolved
docs/features/README.md +1
| @@ -87,6 +87,7 @@ Read with the guide page [Your first app in five minutes](../guide-first-app.md) | ||
| 87 | 87 | Read with the guide page [](../guide-tell-the-crew.md). |
| 88 | 88 | |
| 89 | 89 | - [](speak.md) |
| 90 | - [](composer-send-key.md) | |
| 90 | 91 | - [](bug-close.md) |
| 91 | 92 | - [](atlas-plan.md) |
| 92 | 93 | - [](crew-maintainer.md) |
docs/features/composer-send-key.md added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | # Ctrl/Cmd+Enter sends from the feed composer | |
| 2 | ||
| 3 | This page is for anyone who tells the crew from the feed. In the "Tell the crew" box, Ctrl+Enter (Cmd+Enter on a Mac) sends the tell, the same as the button. | |
| 4 | ||
| 5 | ## How to use it | |
| 6 | ||
| 7 | 1. Open the feed and type in "Tell the crew". | |
| 8 | 2. Press Ctrl+Enter or Cmd+Enter. The box clears and the tell is sent. | |
| 9 | 3. Enter alone still starts a new line. | |
| 10 | ||
| 11 | The hint "Ctrl/Cmd+Enter sends" sits beside the Post button, and the box announces the shortcut to screen readers (`aria-keyshortcuts`). | |
| 12 | ||
| 13 | If the box is empty, nothing is sent and the browser shows its usual "fill out this field" note, which is the same answer the button gives. | |
| 14 | ||
| 15 | ## Your words are kept | |
| 16 | ||
| 17 | Unsent words in the box are saved in this browser (never sent anywhere) and put back after a reload; sending drops them. A refused send puts them back. This was already so; see `kept-tell.md` for the toolbar's own draft. | |
| 18 | ||
| 19 | ## The terminal | |
| 20 | ||
| 21 | The terminal has no equivalent and needs none: its tell is a command line, where Enter sends. Nothing about it changed. |
src/web/draft.ts +19
| @@ -62,3 +62,22 @@ function forgeDraft(storage, box, key, later) { | ||
| 62 | 62 | }; |
| 63 | 63 | } |
| 64 | 64 | `; |
| 65 | ||
| 66 | /** | |
| 67 | * Ctrl/Cmd+Enter sends the tell from inside the composer (#13521): a pure | |
| 68 | * test of a key event. Plain Enter stays a newline; a composing (IME) Enter | |
| 69 | * is never a send. | |
| 70 | */ | |
| 71 | export const sendChordJs = /* js */ ` | |
| 72 | function forgeSendChord(e) { | |
| 73 | return e.key === 'Enter' && !!(e.ctrlKey || e.metaKey) && !e.altKey && !e.shiftKey && !e.isComposing; | |
| 74 | } | |
| 75 | // Bind the chord on the box to submitting its own form: the same submit path as the button. | |
| 76 | function forgeSendKey(box, form) { | |
| 77 | box.addEventListener('keydown', function (e) { | |
| 78 | if (!forgeSendChord(e)) return; | |
| 79 | e.preventDefault(); | |
| 80 | if (form.requestSubmit) form.requestSubmit(); else form.submit(); | |
| 81 | }); | |
| 82 | } | |
| 83 | `; |
src/web/studio/feed.ts +5 −2
| @@ -31,7 +31,7 @@ import { dividers } from "./day_divider.ts"; | ||
| 31 | 31 | import { html, raw, type Html } from "../html.ts"; |
| 32 | 32 | import { DISMISS_KEY } from "../script_keys.ts"; |
| 33 | 33 | import { fragment } from "../layout.ts"; |
| 34 | import { draftJs } from "../draft.ts"; | |
| 34 | import { draftJs, sendChordJs } from "../draft.ts"; | |
| 35 | 35 | import { outboxJs } from "../outbox.ts"; |
| 36 | 36 | import { avatar, btn, card, disclosure, empty, hintAttr, nameMark, row, stateBadge, time } from "../ui.ts"; |
| 37 | 37 | import { heldPostsJs } from "./held_posts.ts"; |
| @@ -284,7 +284,7 @@ export function composer(ctx: Ctx, user: User): Html { | ||
| 284 | 284 | const form = html`<form id="composer" class="stack" method="post" action="/tell"> |
| 285 | 285 | <input type="hidden" name="${COMPOSER_FIELDS.back}" value="/feed" /> |
| 286 | 286 | ${shared === "" ? "" : html`<input type="hidden" name="${COMPOSER_FIELDS.once}" value="${shareToken(user.id, shared)}" />`} |
| 287 | <textarea class="input" name="${COMPOSER_FIELDS.text}" rows="2" maxlength="${String(TELL_MAX)}" required placeholder="Tell the crew what should change" aria-label="Tell the crew" data-draft="${user.id}">${shared}</textarea> | |
| 287 | <textarea class="input" name="${COMPOSER_FIELDS.text}" rows="2" maxlength="${String(TELL_MAX)}" required placeholder="Tell the crew what should change" aria-label="Tell the crew" aria-keyshortcuts="Control+Enter Meta+Enter" data-draft="${user.id}">${shared}</textarea> | |
| 288 | 288 | <div class="bar"> |
| 289 | 289 | ${repos.length === 0 |
| 290 | 290 | ? html`<input type="hidden" name="${COMPOSER_FIELDS.app}" value="${FORGE_TARGET}" /><span class="muted small">the forge</span>` |
| @@ -295,6 +295,7 @@ export function composer(ctx: Ctx, user: User): Html { | ||
| 295 | 295 | <option value="${FORGE_TARGET}">the forge</option> |
| 296 | 296 | </select>`} |
| 297 | 297 | <span class="spacer"></span> |
| 298 | <span class="muted small">Ctrl/Cmd+Enter sends</span> | |
| 298 | 299 | ${btn("Post", { variant: "primary" })} |
| 299 | 300 | </div> |
| 300 | 301 | </form>`; |
| @@ -545,6 +546,7 @@ function keepDay(card) { | ||
| 545 | 546 | export const feedJs = /* js */ ` |
| 546 | 547 | ${outboxJs} |
| 547 | 548 | ${draftJs} |
| 549 | ${sendChordJs} | |
| 548 | 550 | ${dayKeepJs} |
| 549 | 551 | ${heldPostsJs} |
| 550 | 552 | (function () { |
| @@ -697,6 +699,7 @@ ${heldPostsJs} | ||
| 697 | 699 | draft = forgeDraft(window.localStorage, draftBox, 'draft.' + draftBox.dataset.draft + location.pathname, function (fn, ms) { setTimeout(fn, ms); }); |
| 698 | 700 | draft.restore(); |
| 699 | 701 | draftBox.addEventListener('input', draft.typed); |
| 702 | forgeSendKey(draftBox, composer); | |
| 700 | 703 | } |
| 701 | 704 | if (composer) composer.addEventListener('submit', function (e) { |
outdated
No how-to for this shortcut in docs/features/ (README is capped, so not there). Add it to the feed composer doc and state whether the terminal composer gets the same send chord (both doors). Also, requestSubmit on an empty required box shows the browser's validation bubble; confirm that is intended. |
||
| 702 | 705 | var box = composer.elements.text; |
test/feed-composer.test.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | /** | |
| 2 | * The composer sends on Ctrl/Cmd+Enter (#13521). The binder that ships to | |
| 3 | * the page is run here against a stub box and form: the keydown reaches the | |
| 4 | * form's own submit, and only for the chord. The draft's behaviour is in | |
| 5 | * composer-draft.test.ts. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createSession } from "../src/core/sessions.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { sendChordJs } from "../src/web/draft.ts"; | |
| 16 | import { feedJs } from "../src/web/studio/feed.ts"; | |
| 17 | import { promote } from "./helpers/members.ts"; | |
| 18 | ||
| 19 | let app: App; | |
| 20 | let servers: Servers; | |
| 21 | let cookie: string; | |
| 22 | ||
| 23 | beforeAll(async () => { | |
outdated
Substring checks pass even if the keydown handler is bound to the wrong element. Exercise the handler (stub composer and box, dispatch a keydown, assert requestSubmit was called and preventDefault ran). The draft.restore()/clear() lines (25-26) already pass on main, so they are not a test of this change. |
||
| 24 | app = createApp({ dataDir: await mkdtemp(join(tmpdir(), "sendkey.")), port: 0 }); | |
| 25 | servers = await serve(app); | |
| 26 | const r = await createUser(app.db, { | |
| 27 | origin: "seed", trusted: true, username: "teller", email: "[email protected]", | |
| 28 | password: "a-good-passphrase", adminIfFirst: true, | |
| 29 | }); | |
| 30 | if (!r.ok) throw new Error(r.error); | |
| 31 | promote(app.db, r.value); | |
| 32 | cookie = createSession(app.db, r.value.id).token; | |
| 33 | }); | |
| 34 | afterAll(() => servers.stop()); | |
| 35 | ||
| 36 | const make = new Function(`${sendChordJs}; return { forgeSendChord, forgeSendKey };`)() as { | |
| 37 | forgeSendChord: (e: object) => boolean; | |
| 38 | forgeSendKey: (box: unknown, form: unknown) => void; | |
| 39 | }; | |
| 40 | ||
| 41 | const rig = (formExtra: object = {}) => { | |
| 42 | let handler: ((e: any) => void) | undefined; | |
| 43 | const box = { addEventListener: (t: string, fn: (e: any) => void) => { if (t === "keydown") handler = fn; } }; | |
| 44 | const calls = { submit: 0, requestSubmit: 0 }; | |
| 45 | const form = { requestSubmit: () => void calls.requestSubmit++, submit: () => void calls.submit++, ...formExtra }; | |
| 46 | make.forgeSendKey(box, form); | |
| 47 | const press = (e: object) => { | |
| 48 | const ev = { prevented: false, preventDefault() { this.prevented = true; }, ...e }; | |
| 49 | handler!(ev); | |
| 50 | return ev; | |
| 51 | }; | |
| 52 | return { calls, press }; | |
| 53 | }; | |
| 54 | ||
| 55 | test("Ctrl+Enter and Cmd+Enter submit the form and stop the newline", () => { | |
| 56 | const { calls, press } = rig(); | |
| 57 | expect(press({ key: "Enter", ctrlKey: true }).prevented).toBe(true); | |
| 58 | expect(press({ key: "Enter", metaKey: true }).prevented).toBe(true); | |
| 59 | expect(calls.requestSubmit).toBe(2); | |
| 60 | }); | |
| 61 | ||
| 62 | test("plain, shifted, alt and composing Enter and other chords do nothing", () => { | |
| 63 | const { calls, press } = rig(); | |
| 64 | for (const e of [ | |
| 65 | { key: "Enter" }, { key: "Enter", ctrlKey: true, shiftKey: true }, { key: "Enter", ctrlKey: true, altKey: true }, | |
| 66 | { key: "Enter", ctrlKey: true, isComposing: true }, { key: "a", ctrlKey: true }, | |
| 67 | ]) expect(press(e).prevented).toBe(false); | |
| 68 | expect(calls.requestSubmit + calls.submit).toBe(0); | |
| 69 | }); | |
| 70 | ||
| 71 | test("a browser without requestSubmit falls back to submit", () => { | |
| 72 | const { calls, press } = rig({ requestSubmit: undefined }); | |
| 73 | press({ key: "Enter", ctrlKey: true }); | |
| 74 | expect(calls.submit).toBe(1); | |
| 75 | }); | |
| 76 | ||
| 77 | test("the feed page ships the binder and calls it with the composer's own box and form", () => { | |
| 78 | expect(feedJs).toContain("function forgeSendKey("); | |
| 79 | expect(feedJs).toContain("forgeSendKey(draftBox, composer);"); | |
| 80 | }); | |
| 81 | ||
| 82 | test("the shortcut is announced: aria-keyshortcuts on the box and a hint by the button, in the first paint", async () => { | |
| 83 | const res = await fetch(`${servers.url}/feed`, { headers: { cookie: `forge_session=${cookie}; forge_mode=studio` } }); | |
| 84 | const page = await res.text(); | |
| 85 | const form = page.slice(page.indexOf('<form id="composer"'), page.indexOf("</form>", page.indexOf('<form id="composer"'))); | |
| 86 | expect(form).toMatch(/<textarea[^>]*aria-keyshortcuts="Control\+Enter Meta\+Enter"/); | |
| 87 | expect(form).toContain('<span class="muted small">Ctrl/Cmd+Enter sends</span>'); | |
| 88 | }); |
Principle 8 / keyboard users: the chord is bound but never announced. Add aria-keyshortcuts to the textarea and a visible muted hint by the button.