4.1.0: paste pictures from the clipboard

Pasting an image (e.g. a screenshot) opens a preview with Send/Cancel
before anything is sent. Pasted pictures keep up to 1600 px / ~300 kB so
text in screenshots stays readable. Clipboard contents with text (office
apps put a picture of the text alongside) still paste as text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pascal Suter
2026-11-18 05:05:13 +01:00
co-authored by Claude Opus 5.5
parent 907a54eb57
commit ba3b6a30c6
4 changed files with 117 additions and 18 deletions
+57 -8
View File
@@ -1,5 +1,5 @@
#!/bin/sh
# Cabin Chat 4.0.1 - self-contained installer for OpenWrt / GL.iNet routers.
# Cabin Chat 4.1.0 - self-contained installer for OpenWrt / GL.iNet routers.
# Uses only what the firmware already ships (uhttpd, busybox, uci; nginx if there).
#
# sh install-cabinchat.sh install, or upgrade an existing install
@@ -18,7 +18,7 @@
#
# Generated by build.sh - edit www/ and installer.sh.in instead of this file.
VERSION="4.0.1"
VERSION="4.1.0"
APP=/www-chat
WEB=$APP/htdocs
DATA=$APP/data
@@ -2837,6 +2837,9 @@ footer{position:relative}
.items svg{flex:none;width:21px;height:21px;color:var(--muted)}
.items .danger,.items .danger svg{color:var(--bad)}
.primary.danger{background:var(--bad)}
.pasted{display:flex;flex-wrap:wrap;justify-content:center;gap:8px;margin:12px 0 4px}
.pasted img{display:block;max-width:100%;max-height:42vh;border-radius:12px;background:#fff}
.pasted.many img{max-width:calc(50% - 4px);max-height:24vh;object-fit:cover}
.ghost{width:100%;margin-top:8px;padding:11px;border:0;border-radius:14px;background:var(--panel2);font-size:15px}
#sheet input{text-align:left}
.qr{display:block;width:220px;height:220px;margin:4px auto 12px;border-radius:12px;background:#fff}
@@ -3517,11 +3520,11 @@ function loadImage(file) {
}
// scale down + re-encode as JPEG until it fits into ~100 kB
async function shrink(file, target = 100 * 1024) {
async function shrink(file, target = 100 * 1024, maxSide = 1280) {
const img = await loadImage(file); // browsers apply EXIF rotation here
const W = img.naturalWidth, H = img.naturalHeight;
const c = document.createElement('canvas'), ctx = c.getContext('2d');
let max = 1280, q = 0.8, blob = null;
let max = maxSide, q = 0.8, blob = null;
for (let i = 0; i < 12; i++) {
const s = Math.min(1, max / Math.max(W, H));
c.width = Math.max(1, Math.round(W * s)); c.height = Math.max(1, Math.round(H * s));
@@ -3547,12 +3550,13 @@ function pendingRow(blob, w, h) {
return () => { row.remove(); URL.revokeObjectURL(url); };
}
async function sendPhoto(file) {
// screenshots (pasted) usually show text: give them more pixels and bytes
async function sendPhoto(file, sharp = false) {
let done = null;
const re = replyTo;
cancelReply();
try {
const { blob, w, h } = await shrink(file);
const { blob, w, h } = sharp ? await shrink(file, 300 * 1024, 1600) : await shrink(file);
done = pendingRow(blob, w, h);
const extra = '&w=' + w + '&h=' + h + '&r=' + randHex(8) + (re ? '&re=' + re : '');
const r = await fetch(postUrl('img', extra), { method: 'POST', headers: { 'Content-Type': 'image/jpeg' }, body: blob });
@@ -3571,6 +3575,48 @@ $('file').addEventListener('change', () => {
if (f) sendPhoto(f);
});
/* paste a picture (e.g. a screenshot): preview first, a paste is easily done by accident */
function pastedImages(dt) {
if (!dt) return [];
let files = [...(dt.files || [])];
if (!files.length) files = [...(dt.items || [])].filter(i => i.kind === 'file').map(i => i.getAsFile()).filter(Boolean);
return files.filter(f => /^image\//.test(f.type));
}
let pasteUrls = [];
function confirmPhotos(files) {
sheet(s => {
const n = files.length;
s.appendChild(el('h2', null, n > 1 ? 'Send ' + n + ' pictures?' : 'Send this picture?'));
s.appendChild(el('p', null, 'To ' + (conv === ALL ? 'everyone' : nickOf(conv))
+ (replyTo && msgs.get(replyTo) ? ', as a reply' : '')));
const box = el('div', 'pasted' + (n > 1 ? ' many' : ''));
for (const f of files) {
const url = URL.createObjectURL(f);
pasteUrls.push(url);
const img = el('img'); img.src = url; img.alt = '';
box.appendChild(img);
}
const send = el('button', 'primary', 'Send'), cancel = el('button', 'ghost', 'Cancel');
send.type = cancel.type = 'button';
send.addEventListener('click', async () => {
closeSheet();
for (const f of files) await sendPhoto(f, true);
});
cancel.addEventListener('click', closeSheet);
s.append(box, send, cancel);
setTimeout(() => send.focus(), 60); // Enter sends, Escape cancels
});
}
document.addEventListener('paste', e => {
if (!me || !$('modal').hidden || !$('sheetBack').hidden) return;
const dt = e.clipboardData, files = pastedImages(dt);
if (!files.length) return;
// office apps copy text together with a picture of it: paste the text then
if (dt.types.includes('text/plain') && dt.getData('text/plain').trim()) return;
e.preventDefault();
confirmPhotos(files.slice(0, 10));
});
function openViewer(src) { $('viewer').firstElementChild.src = src; $('viewer').hidden = false; }
$('viewer').addEventListener('click', () => { $('viewer').hidden = true; });
addEventListener('keydown', e => { if (e.key === 'Escape') { $('viewer').hidden = true; closeMenu(); cancelReply(); closeSheet(); } });
@@ -3972,7 +4018,10 @@ function sheet(build) {
build(s);
$('sheetBack').hidden = false;
}
function closeSheet() { clearInterval(linkTimer); $('sheetBack').hidden = true; $('sheet').textContent = ''; }
function closeSheet() {
clearInterval(linkTimer); $('sheetBack').hidden = true; $('sheet').textContent = '';
pasteUrls.forEach(u => URL.revokeObjectURL(u)); pasteUrls = [];
}
$('sheetBack').addEventListener('click', e => { if (e.target === $('sheetBack')) closeSheet(); });
function item(icon, label, fn, side, cls) {
@@ -4310,7 +4359,7 @@ self.addEventListener('notificationclick', e => {
});
__CABINCHAT_EOF__
cat > "$STAGE/VERSION" <<'__CABINCHAT_EOF__'
4.0.1
4.1.0
__CABINCHAT_EOF__
[ -f "$STAGE/index.html" ] && [ -f "$STAGE/cgi-bin/chat" ] && [ -f "$STAGE/VERSION" ] \
|| die "installer payload is incomplete"