diff --git a/web/dist/app.js b/web/dist/app.js index d7b982a..df3c6b0 100644 --- a/web/dist/app.js +++ b/web/dist/app.js @@ -98,6 +98,13 @@ function append(parent, children) { function clear(el) { while (el.firstChild) el.removeChild(el.firstChild); } +function sendOnCtrlEnter(area) { + area.addEventListener("keydown", (ev) => { + if (ev.key !== "Enter" || !(ev.ctrlKey || ev.metaKey) || ev.isComposing || area.disabled) return; + ev.preventDefault(); + area.form?.requestSubmit(); + }); +} // src/i18n.ts var de = { @@ -150,6 +157,7 @@ var de = { "contact.intro": "Deine Nachricht wird verschl\xFCsselt. Das Netzwerk sieht nur, dass jemand zu dieser Anzeige geschrieben hat, nicht wer und nicht was. Du brauchst kein Konto. Die genaue Adresse gibt der Anbieter selbst frei.", "contact.rules": "Die erste Nachricht muss kurz sein und darf keine Links enthalten. Das sch\xFCtzt vor Betrug; nach der ersten Antwort des Anbieters entf\xE4llt es.", "contact.label": "Deine Nachricht", + "compose.hint": "Strg+Enter sendet", "contact.counter": "{used} / {max} Bytes", "contact.send": "Verschl\xFCsselt senden", "contact.sending": "Wird vorbereitet \u2026 (eine kleine Rechenaufgabe gegen Spam)", @@ -373,6 +381,7 @@ var en = { "contact.intro": "Your message is encrypted. The network only sees that someone wrote about this listing, not who and not what. You need no account. The owner releases the exact address themselves.", "contact.rules": "The first message must be short and must not contain links. This protects against scams; it ends once the owner has replied.", "contact.label": "Your message", + "compose.hint": "Ctrl+Enter sends", "contact.counter": "{used} / {max} bytes", "contact.send": "Send encrypted", "contact.sending": "Preparing \u2026 (a small puzzle against spam)", @@ -4324,7 +4333,8 @@ function describe(e2) { return t("send.network"); } function composer(opts) { - const area = h("textarea", { name: "message", rows: 5, "aria-label": t("contact.label"), required: true }); + const area = h("textarea", { name: "message", rows: 5, "aria-label": t("contact.label"), placeholder: t("compose.hint"), required: true }); + sendOnCtrlEnter(area); const counter = h("span", { class: "muted" }); const problem = h("p", { class: "error", role: "alert" }); const button = h("button", { type: "submit", class: "btn primary" }, t(opts.button)); @@ -5116,7 +5126,8 @@ function bubble2(m) { return h("div", { class: `msg ${mine2 ? "out" : "in"}` }, head, h("p", { class: "pre" }, m.text ?? "")); } function composer2(listingId, thread, done, release) { - const area = h("textarea", { rows: 3, "aria-label": t("own.reply"), name: "reply" }); + const area = h("textarea", { rows: 3, "aria-label": t("own.reply"), name: "reply", placeholder: t("compose.hint") }); + sendOnCtrlEnter(area); const problem = h("p", { class: "error", role: "alert" }); const send = h("button", { type: "submit", class: "btn primary" }, t("own.send")); const rel = h("button", { type: "button", class: "btn", onclick: () => release(rel) }, t("own.release")); diff --git a/web/src/chat.ts b/web/src/chat.ts index e2d89e1..8ae8d29 100644 --- a/web/src/chat.ts +++ b/web/src/chat.ts @@ -3,7 +3,7 @@ // only, and owner messages only after message.ts verified them. import { HttpError, getListing, pollMailbox, postMessage } from "./api.ts"; -import { append, clear, h } from "./dom.ts"; +import { append, clear, h, sendOnCtrlEnter } from "./dom.ts"; import { date } from "./format.ts"; import { t, type Key } from "./i18n.ts"; import { @@ -63,7 +63,8 @@ function composer(opts: { button: Key; send: (text: string) => Promise; }): HTMLElement { - const area = h("textarea", { name: "message", rows: 5, "aria-label": t("contact.label"), required: true }); + const area = h("textarea", { name: "message", rows: 5, "aria-label": t("contact.label"), placeholder: t("compose.hint"), required: true }); + sendOnCtrlEnter(area); const counter = h("span", { class: "muted" }); const problem = h("p", { class: "error", role: "alert" }); const button = h("button", { type: "submit", class: "btn primary" }, t(opts.button)); diff --git a/web/src/dom.ts b/web/src/dom.ts index 78a4163..95aefae 100644 --- a/web/src/dom.ts +++ b/web/src/dom.ts @@ -34,3 +34,12 @@ export function append(parent: Node, children: Child[]): void { export function clear(el: Element): void { while (el.firstChild) el.removeChild(el.firstChild); } + +/** Lets Ctrl+Enter (Cmd+Enter on a Mac) send the form a text box belongs to, like its Send button. */ +export function sendOnCtrlEnter(area: HTMLTextAreaElement): void { + area.addEventListener("keydown", (ev) => { + if (ev.key !== "Enter" || !(ev.ctrlKey || ev.metaKey) || ev.isComposing || area.disabled) return; + ev.preventDefault(); + area.form?.requestSubmit(); + }); +} diff --git a/web/src/i18n.ts b/web/src/i18n.ts index 1437378..030e7f3 100644 --- a/web/src/i18n.ts +++ b/web/src/i18n.ts @@ -53,6 +53,7 @@ const de = { "contact.rules": "Die erste Nachricht muss kurz sein und darf keine Links enthalten. Das schützt vor Betrug; nach der ersten Antwort des Anbieters entfällt es.", "contact.label": "Deine Nachricht", + "compose.hint": "Strg+Enter sendet", "contact.counter": "{used} / {max} Bytes", "contact.send": "Verschlüsselt senden", "contact.sending": "Wird vorbereitet … (eine kleine Rechenaufgabe gegen Spam)", @@ -299,6 +300,7 @@ const en: Record = { "contact.rules": "The first message must be short and must not contain links. This protects against scams; it ends once the owner has replied.", "contact.label": "Your message", + "compose.hint": "Ctrl+Enter sends", "contact.counter": "{used} / {max} bytes", "contact.send": "Send encrypted", "contact.sending": "Preparing … (a small puzzle against spam)", diff --git a/web/src/owner.ts b/web/src/owner.ts index e6212bb..8ca1e10 100644 --- a/web/src/owner.ts +++ b/web/src/owner.ts @@ -7,7 +7,7 @@ // with textContent only, never as links. import { HttpError } from "./api.ts"; -import { append, clear, h } from "./dom.ts"; +import { append, clear, h, sendOnCtrlEnter } from "./dom.ts"; import { date, price, when } from "./format.ts"; import { t, type Key } from "./i18n.ts"; import * as o from "./ownerApi.ts"; @@ -405,7 +405,8 @@ function bubble(m: o.OwnerMessage): HTMLElement { } function composer(listingId: string, thread: string, done: () => void, release: (opener: HTMLElement) => void): HTMLElement { - const area = h("textarea", { rows: 3, "aria-label": t("own.reply"), name: "reply" }); + const area = h("textarea", { rows: 3, "aria-label": t("own.reply"), name: "reply", placeholder: t("compose.hint") }); + sendOnCtrlEnter(area); const problem = h("p", { class: "error", role: "alert" }); const send = h("button", { type: "submit", class: "btn primary" }, t("own.send")); const rel = h("button", { type: "button", class: "btn", onclick: () => release(rel) }, t("own.release")); diff --git a/web/test/chat.test.ts b/web/test/chat.test.ts index e35918e..9e8625b 100644 --- a/web/test/chat.test.ts +++ b/web/test/chat.test.ts @@ -62,6 +62,26 @@ test("a first message is encrypted, mined and sent; the conversation opens", asy } }); +test("Ctrl+Enter sends the first message like the button does", async () => { + const before = gw.posted.length; + const p = openPage(gw.url, detail); + try { + await p.waitFor(() => p.document.querySelector("textarea") !== null, "the contact form"); + const area = p.document.querySelector("textarea")!; + const press = (init: KeyboardEventInit) => area.dispatchEvent(new p.window.KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...init })); + area.value = "Per Tastatur gesendet"; + assert.equal(press({}), true, "a plain Enter only adds a line"); + await new Promise((r) => setTimeout(r, 50)); + assert.equal(gw.posted.length, before); + press({ ctrlKey: true }); + await p.waitFor(() => p.window.location.hash.startsWith("#/c/"), "the conversation view"); + assert.equal(gw.posted.length, before + 1); + assert.equal(openAsOwner(JSON.parse(gw.posted[before]!) as Envelope, owner).text, "Per Tastatur gesendet"); + } finally { + p.close(); + } +}); + test("links are refused in a first message, and nothing is sent", async () => { const before = gw.posted.length; const p = openPage(gw.url, detail); diff --git a/web/test/owner.test.ts b/web/test/owner.test.ts index 331be10..afe22ee 100644 --- a/web/test/owner.test.ts +++ b/web/test/owner.test.ts @@ -190,6 +190,36 @@ test("a reply is sent as text and the box is cleared", async () => { assert.equal(posts().length, 1); }); +test("Ctrl+Enter sends a reply; plain Enter only adds a line", async () => { + open(`#/login/${TOKEN}`); + await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings"); + page!.window.location.hash = `#/mine/${L1}/${T_NEW}`; + await page!.waitFor(() => qa(".pane-thread .msg").length === 1, "the conversation"); + const area = q(".pane-thread textarea")!; + const press = (init: KeyboardEventInit) => area.dispatchEvent(new page!.window.KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...init })); + + area.value = "Hallo"; + assert.equal(press({}), true, "a plain Enter is left alone, so it adds a line"); + assert.equal(press({ shiftKey: true }), true); + await new Promise((r) => setTimeout(r, 50)); + assert.equal(posts().length, 0, "nothing sent without Ctrl"); + assert.match(area.placeholder, /Ctrl\+Enter/); + + assert.equal(press({ ctrlKey: true }), false, "Ctrl+Enter is handled"); + await page!.waitFor(() => posts().length === 1, "the reply"); + assert.deepEqual(JSON.parse(posts()[0]!.body), { type: "text", text: "Hallo" }); + await page!.waitFor(() => area.value === "", "the box to clear"); + + area.value = "Von einem Mac"; + press({ metaKey: true }); + await page!.waitFor(() => posts().length === 2, "Cmd+Enter works too"); + + area.value = ""; + press({ ctrlKey: true }); + await new Promise((r) => setTimeout(r, 50)); + assert.equal(posts().length, 2, "an empty box sends nothing"); +}); + test("releasing the address: prefilled sheet, signed grant request, closes", async () => { open(`#/login/${TOKEN}`); await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");