diff --git a/README.md b/README.md index 988c579..fecff8d 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,11 @@ Browser-based group chat for a GL.iNet travel router (or any OpenWrt box). Nothing to install on the phones, no extra packages on the router: it runs on the built-in `uhttpd` web server with one small shell CGI script. -Group chat and private chats, photos, replies, emoji, @mentions, typing -notices, accounts with passwords (one account on several devices at once), -profile pictures, device linking by QR code, installable as an app (PWA) and -browser notifications. Messages disappear after 24 hours. +Group chat and private chats, photos (from the camera, files or pasted from +the clipboard), replies, emoji, @mentions, typing notices, accounts with +passwords (one account on several devices at once), profile pictures, device +linking by QR code, installable as an app (PWA) and browser notifications. +Messages disappear after 24 hours. ## Install on the router diff --git a/dist/install-cabinchat.sh b/dist/install-cabinchat.sh index 503cca4..ea94b8a 100755 --- a/dist/install-cabinchat.sh +++ b/dist/install-cabinchat.sh @@ -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" diff --git a/www/VERSION b/www/VERSION index 1454f6e..ee74734 100644 --- a/www/VERSION +++ b/www/VERSION @@ -1 +1 @@ -4.0.1 +4.1.0 diff --git a/www/index.html b/www/index.html index 8a0473f..ff0dc31 100644 --- a/www/index.html +++ b/www/index.html @@ -255,6 +255,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} @@ -935,11 +938,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)); @@ -965,12 +968,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 }); @@ -989,6 +993,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(); } }); @@ -1390,7 +1436,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) {