Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

### Changed

- **Share shows the workout image first, with a transparent background and Copy.** Share on the Workout Complete sheet opens a preview, and nothing leaves the app until you tap Share. Transparent drops the background, to put the card over a photo in a story, and Copy puts the image on the clipboard to paste it there (in a browser, over HTTPS).

### Security

- **Tokens, keys and passwords saved in Settings are stored encrypted, and the ones only the server uses never reach the app** (from [cooktrace#92](https://github.com/TraceApps/cooktrace/issues/92), thanks @stupleb). The NutriTrace link token, the ntfy and Gotify tokens and the music server's sign-in stay on the server: Settings shows a saved one as dots, saving the dots keeps it, and typing a new one replaces it. The AI key, ExerciseDB key and music server password still go to your own signed-in app, which uses them itself. All of them are stored encrypted with `TOKEN_ENC_KEY`, so the database, a backup or an export never holds them in plain text; ones saved before are encrypted when the server starts. Changing `TOKEN_ENC_KEY`, or `JWT_SECRET` when it isn't set, means entering them again.
Expand Down
87 changes: 83 additions & 4 deletions src/components/diary/WorkoutSummary.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import { weightUnit, caloriesBurnedEnabled, heightCm, currentWeightKg } from '../../stores/settings.js';
import { currentUser } from '../../stores/auth.js';
import { DB } from '../../lib/db.js';
import { shareWorkoutCard } from '../../lib/workoutCard.js';
import { renderWorkoutCard, shareWorkoutCard, canCopyImage, copyWorkoutCard } from '../../lib/workoutCard.js';
import { exportWorkoutCsv } from '../../lib/workoutCsv.js';
import { LtApi } from '../../lib/api.js';
import { showError } from '../../stores/toast.js';
import { showError, showSuccess } from '../../stores/toast.js';
import { estimateWorkoutCalories, toKg, ageFromDob, exerciseVolume, fmtSetLabel, distanceUnitOf } from '../../lib/workout.js';
import { timerState, timerMs } from '../../stores/workoutTimer.js';

Expand Down Expand Up @@ -60,14 +60,49 @@
);
})();

// Share shows the image first and sends it only on a second tap; the blob
// shown is the one shared or copied. The background can be dropped, to
// paste the card over a photo.
let sharing = false;
async function handleShare() {
let previewBlob = null;
let cardUrl = '';
let previewTransparent = false;
const canCopy = canCopyImage();
async function renderPreview(transparent) {
if (!workout || sharing) return;
sharing = true;
try { await shareWorkoutCard({ ...workout, duration_min: effectiveDurationMin }, $weightUnit); }
try {
const blob = await renderWorkoutCard({ ...workout, duration_min: effectiveDurationMin }, $weightUnit, { transparent });
if (cardUrl) URL.revokeObjectURL(cardUrl);
previewBlob = blob;
cardUrl = blob ? URL.createObjectURL(blob) : '';
previewTransparent = transparent;
}
catch (e) { showError(e.message || 'Share failed'); }
sharing = false;
}
function handleShare() { return renderPreview(false); }
function closePreview() {
if (cardUrl) URL.revokeObjectURL(cardUrl);
cardUrl = '';
previewBlob = null;
}
async function copyPreview() {
if (!previewBlob || sharing) return;
try { await copyWorkoutCard(previewBlob); showSuccess($_('workout_summary.image_copied')); }
catch (e) { showError(e.message || 'Copy failed'); }
}
async function confirmShare() {
if (!previewBlob || sharing) return;
sharing = true;
try {
// false = the share sheet was dismissed: stay on the preview.
if (await shareWorkoutCard({ ...workout, duration_min: effectiveDurationMin }, $weightUnit, previewBlob)) closePreview();
}
catch (e) { showError(e.message || 'Share failed'); }
sharing = false;
}
$: if (!open) closePreview();

let exportingCsv = false;
async function handleExportCsv() {
Expand Down Expand Up @@ -175,6 +210,31 @@
<span class="material-symbols-rounded">close</span>
</button>

{#if cardUrl}
<div class="ws-preview">
<h3 class="ws-title">{$_('workout_summary.share_title')}</h3>
<div class="ws-bg-row" role="group" aria-label={$_('workout_summary.share_background')}>
<button class="btn btn-secondary ws-bg-btn" class:active={!previewTransparent} aria-pressed={!previewTransparent}
on:click={() => renderPreview(false)} disabled={sharing}>{$_('workout_summary.share_bg_solid')}</button>
<button class="btn btn-secondary ws-bg-btn" class:active={previewTransparent} aria-pressed={previewTransparent}
on:click={() => renderPreview(true)} disabled={sharing}>{$_('workout_summary.share_bg_transparent')}</button>
</div>
<img class="ws-preview-img" class:transparent={previewTransparent} src={cardUrl} alt={$_('workout_summary.share_preview_alt')} />
</div>
<div class="ws-footer">
<button class="btn btn-secondary" on:click={closePreview} disabled={sharing}>{$_('common.cancel')}</button>
{#if canCopy}
<button class="btn btn-secondary ws-share-btn" on:click={copyPreview} disabled={sharing}>
<span class="material-symbols-rounded" style="font-size:18px">content_copy</span>
{$_('workout_summary.copy')}
</button>
{/if}
<button class="btn btn-primary ws-done-btn ws-share-btn" on:click={confirmShare} disabled={sharing}>
<span class="material-symbols-rounded" style="font-size:18px">share</span>
{$_('workout_summary.share')}
</button>
</div>
{:else}
<div class="ws-hero">
<!-- CSS confetti — 14 absolutely positioned dots that drop + drift
on open. Pure CSS, no library. Hidden when reduced-motion. -->
Expand Down Expand Up @@ -302,6 +362,7 @@
</button>
<button class="btn btn-primary ws-done-btn" on:click={() => open = false}>{$_('workout_summary.done')}</button>
</div>
{/if}
</div>
</div>
{/if}
Expand Down Expand Up @@ -546,4 +607,22 @@
}
.ws-icon-btn .material-symbols-rounded { font-size: 20px; }
.ws-done-btn { flex: 1; }

/* Share preview: the image at its own 4:5, as tall as the sheet allows. */
.ws-preview { padding: 20px 20px 4px; text-align: center; }
.ws-bg-row { display: flex; justify-content: center; gap: 8px; margin-top: 12px; }
.ws-bg-btn { height: 34px; padding: 0 14px; font-size: 13px; }
.ws-bg-btn.active { background: var(--accent-dim); border-color: var(--accent); color: var(--accent); }
.ws-preview-img {
display: block; margin: 12px auto 0;
aspect-ratio: 4 / 5;
height: min(48dvh, 450px); width: auto; max-width: 100%;
border-radius: var(--radius-lg);
border: 1px solid var(--border);
}
/* No background: a checkerboard behind the image says so. */
.ws-preview-img.transparent {
background: repeating-conic-gradient(#8a8f9a 0% 25%, #5d626d 0% 50%) 50% / 24px 24px;
}
.ws-preview + .ws-footer .ws-share-btn { justify-content: center; }
</style>
10 changes: 9 additions & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,15 @@
"exercises": "Exercises",
"duration": "Duration",
"custom": "Custom",
"done": "Done"
"done": "Done",
"share": "Share",
"share_title": "Share Workout",
"share_preview_alt": "Preview of the image to share",
"share_background": "Background",
"share_bg_solid": "Solid",
"share_bg_transparent": "Transparent",
"copy": "Copy",
"image_copied": "Image copied"
},
"exercise_detail": {
"deleted": "Exercise deleted",
Expand Down
56 changes: 39 additions & 17 deletions src/lib/workoutCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
//
// Pulls the current accent color from the CSS custom property so the card
// matches the user's theme.
//
// With { transparent: true } the card has no background, to paste over a
// photo: lighter greys, see-through tiles and a soft shadow keep it readable
// on whatever ends up behind it.

// workout.js is pure (no stores), so importing it keeps this renderer self-contained.
import { isRepSet, setKind, setAmount, fmtSetLabel, distanceUnitOf, setReps } from './workout.js';
Expand Down Expand Up @@ -76,9 +80,10 @@ function _computeStats(workout) {
/**
* @param {object} workout { name, date, duration_min, exercises: [...] }
* @param {string} unit weight unit label ('lbs' or 'kg')
* @param {{ transparent?: boolean }} [opts]
* @returns {Promise<Blob>} PNG blob
*/
export async function renderWorkoutCard(workout, unit = 'lbs') {
export async function renderWorkoutCard(workout, unit = 'lbs', { transparent = false } = {}) {
const canvas = document.createElement('canvas');
canvas.width = W;
canvas.height = H;
Expand All @@ -88,21 +93,27 @@ export async function renderWorkoutCard(workout, unit = 'lbs') {
const accent = _cssVar('--accent', '#FF7433');
const accent2 = _cssVar('--accent-2', accent);
const bg = '#0A0B0F';
const surface = '#16181E';
const surface = transparent ? 'rgba(0, 0, 0, 0.38)' : '#16181E';
const text1 = '#F5F6F8';
const text2 = '#A8AFBD';
const text3 = '#6B7280';
const text2 = transparent ? 'rgba(255, 255, 255, 0.88)' : '#A8AFBD';
const text3 = transparent ? 'rgba(255, 255, 255, 0.72)' : '#6B7280';
const line = transparent ? 'rgba(255, 255, 255, 0.28)' : '#23262E';

// ── Background ───────────────────────────────────────────────────────
ctx.fillStyle = bg;
ctx.fillRect(0, 0, W, H);
if (transparent) {
ctx.shadowColor = 'rgba(0, 0, 0, 0.55)';
ctx.shadowBlur = 10;
} else {
// ── Background ─────────────────────────────────────────────────────
ctx.fillStyle = bg;
ctx.fillRect(0, 0, W, H);

// Soft accent glow in the top-right corner
const grad = ctx.createRadialGradient(W - 100, -100, 50, W - 100, -100, 700);
grad.addColorStop(0, accent + '55'); // add ~33% alpha
grad.addColorStop(1, accent + '00');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, W, H);
// Soft accent glow in the top-right corner
const grad = ctx.createRadialGradient(W - 100, -100, 50, W - 100, -100, 700);
grad.addColorStop(0, accent + '55'); // add ~33% alpha
grad.addColorStop(1, accent + '00');
ctx.fillStyle = grad;
ctx.fillRect(0, 0, W, H);
}

// ── Header: LIFTTRACE wordmark ──────────────────────────────────────
ctx.font = '700 32px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif';
Expand Down Expand Up @@ -248,7 +259,7 @@ export async function renderWorkoutCard(workout, unit = 'lbs') {
ctx.fillStyle = accent;
ctx.fillRect(64, blockTop, 4, blockBottom - blockTop);
// Bottom thin divider for the block.
ctx.strokeStyle = '#23262E';
ctx.strokeStyle = line;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(64, y - rowGap + 22);
Expand All @@ -259,7 +270,7 @@ export async function renderWorkoutCard(workout, unit = 'lbs') {
const yBefore = y;
y = _renderRow(g.exercise, y, 0);
rendered++;
ctx.strokeStyle = '#23262E';
ctx.strokeStyle = line;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(64, yBefore + 18);
Expand Down Expand Up @@ -298,9 +309,10 @@ function _roundRect(ctx, x, y, w, h, r) {
/**
* Share a workout card using Web Share API if available (with file),
* otherwise trigger a download. Returns true if shared/downloaded.
* Pass the blob already rendered for a preview to share that exact image.
*/
export async function shareWorkoutCard(workout, unit = 'lbs') {
const blob = await renderWorkoutCard(workout, unit);
export async function shareWorkoutCard(workout, unit = 'lbs', rendered = null) {
const blob = rendered || await renderWorkoutCard(workout, unit);
if (!blob) return false;

const filename = `lifttrace-${(workout.name || 'workout').replace(/\s+/g, '-').toLowerCase()}-${workout.date || 'today'}.png`;
Expand Down Expand Up @@ -330,3 +342,13 @@ export async function shareWorkoutCard(workout, unit = 'lbs') {
setTimeout(() => URL.revokeObjectURL(url), 1000);
return true;
}

/** Whether this browser can put an image on the clipboard (needs HTTPS or localhost). */
export function canCopyImage() {
return typeof ClipboardItem !== 'undefined' && !!navigator.clipboard?.write;
}

/** Put a rendered card on the clipboard, to paste into a story or a chat. */
export async function copyWorkoutCard(blob) {
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })]);
}
Loading