Send messages with Ctrl+Enter

Ctrl+Enter (Cmd+Enter on a Mac) submits the buyer's and the owner's message
boxes through the normal form, so the usual checks apply. Plain Enter still
adds a line. The empty box hints at the shortcut. Not added to the publish
description or the address-release note, where it could submit by accident.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 20:55:30 +01:00
co-authored by Claude Sonnet 5.5
parent 19fea7f524
commit 71e6dc8658
7 changed files with 80 additions and 6 deletions
+13 -2
View File
@@ -98,6 +98,13 @@ function append(parent, children) {
function clear(el) { function clear(el) {
while (el.firstChild) el.removeChild(el.firstChild); 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 // src/i18n.ts
var de = { 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.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.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", "contact.label": "Deine Nachricht",
"compose.hint": "Strg+Enter sendet",
"contact.counter": "{used} / {max} Bytes", "contact.counter": "{used} / {max} Bytes",
"contact.send": "Verschl\xFCsselt senden", "contact.send": "Verschl\xFCsselt senden",
"contact.sending": "Wird vorbereitet \u2026 (eine kleine Rechenaufgabe gegen Spam)", "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.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.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", "contact.label": "Your message",
"compose.hint": "Ctrl+Enter sends",
"contact.counter": "{used} / {max} bytes", "contact.counter": "{used} / {max} bytes",
"contact.send": "Send encrypted", "contact.send": "Send encrypted",
"contact.sending": "Preparing \u2026 (a small puzzle against spam)", "contact.sending": "Preparing \u2026 (a small puzzle against spam)",
@@ -4324,7 +4333,8 @@ function describe(e2) {
return t("send.network"); return t("send.network");
} }
function composer(opts) { 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 counter = h("span", { class: "muted" });
const problem = h("p", { class: "error", role: "alert" }); const problem = h("p", { class: "error", role: "alert" });
const button = h("button", { type: "submit", class: "btn primary" }, t(opts.button)); 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 ?? "")); return h("div", { class: `msg ${mine2 ? "out" : "in"}` }, head, h("p", { class: "pre" }, m.text ?? ""));
} }
function composer2(listingId, thread, done, release) { 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 problem = h("p", { class: "error", role: "alert" });
const send = h("button", { type: "submit", class: "btn primary" }, t("own.send")); 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")); const rel = h("button", { type: "button", class: "btn", onclick: () => release(rel) }, t("own.release"));
+3 -2
View File
@@ -3,7 +3,7 @@
// only, and owner messages only after message.ts verified them. // only, and owner messages only after message.ts verified them.
import { HttpError, getListing, pollMailbox, postMessage } from "./api.ts"; 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 { date } from "./format.ts";
import { t, type Key } from "./i18n.ts"; import { t, type Key } from "./i18n.ts";
import { import {
@@ -63,7 +63,8 @@ function composer(opts: {
button: Key; button: Key;
send: (text: string) => Promise<void>; send: (text: string) => Promise<void>;
}): HTMLElement { }): 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 counter = h("span", { class: "muted" });
const problem = h("p", { class: "error", role: "alert" }); const problem = h("p", { class: "error", role: "alert" });
const button = h("button", { type: "submit", class: "btn primary" }, t(opts.button)); const button = h("button", { type: "submit", class: "btn primary" }, t(opts.button));
+9
View File
@@ -34,3 +34,12 @@ export function append(parent: Node, children: Child[]): void {
export function clear(el: Element): void { export function clear(el: Element): void {
while (el.firstChild) el.removeChild(el.firstChild); 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();
});
}
+2
View File
@@ -53,6 +53,7 @@ const de = {
"contact.rules": "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.", "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", "contact.label": "Deine Nachricht",
"compose.hint": "Strg+Enter sendet",
"contact.counter": "{used} / {max} Bytes", "contact.counter": "{used} / {max} Bytes",
"contact.send": "Verschlüsselt senden", "contact.send": "Verschlüsselt senden",
"contact.sending": "Wird vorbereitet … (eine kleine Rechenaufgabe gegen Spam)", "contact.sending": "Wird vorbereitet … (eine kleine Rechenaufgabe gegen Spam)",
@@ -299,6 +300,7 @@ const en: Record<keyof typeof de, string> = {
"contact.rules": "contact.rules":
"The first message must be short and must not contain links. This protects against scams; it ends once the owner has replied.", "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", "contact.label": "Your message",
"compose.hint": "Ctrl+Enter sends",
"contact.counter": "{used} / {max} bytes", "contact.counter": "{used} / {max} bytes",
"contact.send": "Send encrypted", "contact.send": "Send encrypted",
"contact.sending": "Preparing … (a small puzzle against spam)", "contact.sending": "Preparing … (a small puzzle against spam)",
+3 -2
View File
@@ -7,7 +7,7 @@
// with textContent only, never as links. // with textContent only, never as links.
import { HttpError } from "./api.ts"; 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 { date, price, when } from "./format.ts";
import { t, type Key } from "./i18n.ts"; import { t, type Key } from "./i18n.ts";
import * as o from "./ownerApi.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 { 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 problem = h("p", { class: "error", role: "alert" });
const send = h("button", { type: "submit", class: "btn primary" }, t("own.send")); 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")); const rel = h("button", { type: "button", class: "btn", onclick: () => release(rel) }, t("own.release"));
+20
View File
@@ -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 () => { test("links are refused in a first message, and nothing is sent", async () => {
const before = gw.posted.length; const before = gw.posted.length;
const p = openPage(gw.url, detail); const p = openPage(gw.url, detail);
+30
View File
@@ -190,6 +190,36 @@ test("a reply is sent as text and the box is cleared", async () => {
assert.equal(posts().length, 1); 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<HTMLTextAreaElement>(".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 () => { test("releasing the address: prefilled sheet, signed grant request, closes", async () => {
open(`#/login/${TOKEN}`); open(`#/login/${TOKEN}`);
await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings"); await page!.waitFor(() => qa(".pane-listings .ocard").length === 2, "the listings");