diff --git a/e2e/documents-sort.test.ts b/e2e/documents-sort.test.ts new file mode 100644 index 0000000..73e411a --- /dev/null +++ b/e2e/documents-sort.test.ts @@ -0,0 +1,99 @@ +import { expect, test, type Page } from '@playwright/test'; + +const doc = (id: string, name: string, createdAt: number, updatedAt: number) => ({ + id, + schemaVersion: 1, + name, + createdAt, + updatedAt, + pages: { + p1: { + id: 'p1', + boundaries: { horizontal: { start: 100, end: 700 }, vertical: { start: 100, end: 1000 } }, + elements: {} + } + } +}); + +// Names are chosen so each sort order gives a different, easy to read list +const docs = [ + doc('a', 'Invoice 10', 100, 5000), + doc('b', 'invoice 2', 300, 9000), + doc('c', 'Zebra', 200, 1000), + doc('d', 'apple', 400, 7000) +]; + +async function seed(page: Page) { + await page.goto('/documents'); + await page.getByText('No documents').waitFor(); + await page.evaluate( + (records) => + new Promise((resolve, reject) => { + const open = indexedDB.open('lienzo-db', 1); + open.onsuccess = () => { + const tx = open.result.transaction('documents', 'readwrite'); + for (const record of records) tx.objectStore('documents').put(record); + tx.oncomplete = () => resolve(); + tx.onerror = () => reject(tx.error); + }; + open.onerror = () => reject(open.error); + }), + docs + ); + await page.reload(); + await page.waitForLoadState('networkidle'); +} + +const names = (page: Page) => page.locator('main h3').allTextContents(); + +test.describe('Documents sorting', () => { + test('starts with the most recently updated, and each option reorders the list', async ({ + page + }) => { + await seed(page); + const select = page.getByTestId('sort-select'); + await expect(select).toHaveValue('updated-desc'); + expect(await names(page)).toEqual(['invoice 2', 'apple', 'Invoice 10', 'Zebra']); + + await select.selectOption('updated-asc'); + expect(await names(page)).toEqual(['Zebra', 'Invoice 10', 'apple', 'invoice 2']); + + await select.selectOption('created-desc'); + expect(await names(page)).toEqual(['apple', 'invoice 2', 'Zebra', 'Invoice 10']); + + await select.selectOption('name-asc'); + expect(await names(page)).toEqual(['apple', 'invoice 2', 'Invoice 10', 'Zebra']); + + await select.selectOption('name-desc'); + expect(await names(page)).toEqual(['Zebra', 'Invoice 10', 'invoice 2', 'apple']); + }); + + test('the choice is remembered, and the search keeps the order', async ({ page }) => { + await seed(page); + await page.getByTestId('sort-select').selectOption('name-asc'); + + await page.reload(); + await page.waitForLoadState('networkidle'); + await expect(page.getByTestId('sort-select')).toHaveValue('name-asc'); + expect(await names(page)).toEqual(['apple', 'invoice 2', 'Invoice 10', 'Zebra']); + + await page.getByLabel('Search documents').fill('invoice'); + await expect.poll(() => names(page)).toEqual(['invoice 2', 'Invoice 10']); + }); + + test('on a phone the control sits on the right', async ({ browser }) => { + const context = await browser.newContext({ viewport: { width: 375, height: 812 } }); + const page = await context.newPage(); + await seed(page); + const box = await page.getByTestId('sort-select').boundingBox(); + expect(box).not.toBeNull(); + expect(box!.x + box!.width).toBeGreaterThan(375 - 24); // against the right edge, not the left + await context.close(); + }); + + test('an unknown saved choice falls back to the default', async ({ page }) => { + await page.addInitScript(() => localStorage.setItem('lienzo-documents-sort', 'sideways')); + await seed(page); + await expect(page.getByTestId('sort-select')).toHaveValue('updated-desc'); + }); +}); diff --git a/e2e/seo.test.ts b/e2e/seo.test.ts index c6c1a87..04ef09d 100644 --- a/e2e/seo.test.ts +++ b/e2e/seo.test.ts @@ -44,7 +44,7 @@ test.describe('SEO', () => { }); } - test('sitemap lists only the landing page', async ({ request }) => { + test('sitemap lists the landing page, not the private pages', async ({ request }) => { const res = await request.get('/sitemap.xml'); expect(res.ok()).toBe(true); const body = await res.text(); @@ -52,6 +52,48 @@ test.describe('SEO', () => { expect(body).not.toContain('/canvas'); }); + test('every template has its own indexable page, listed in the sitemap', async ({ + page, + request + }) => { + const body = await (await request.get('/sitemap.xml')).text(); + expect(body).toContain(`${SITE}/templates`); + expect(body).toContain(`${SITE}/templates/resume`); + const listed = body.match(/\/templates\/[a-z-]+ n['@type'])).toEqual([ + 'CreativeWork', + 'BreadcrumbList' + ]); + + // links in the prerendered page are absolute: relative ones pointed at /templates/templates + const hrefs = await page + .locator('main a') + .evaluateAll((all) => all.map((a) => a.getAttribute('href'))); + expect(hrefs.length).toBeGreaterThan(0); + expect(hrefs.every((h) => h?.startsWith('/'))).toBe(true); + + await page.getByTestId('use-this-template').click(); + await expect(page).toHaveURL(/\/canvas$/); + await expect(page.getByTestId('document-title')).toHaveText('Invoice'); + }); + + test('an unknown template is a 404', async ({ request }) => { + expect((await request.get('/templates/not-a-template')).status()).toBe(404); + }); + test('robots.txt points to the sitemap', async ({ request }) => { const res = await request.get('/robots.txt'); expect(await res.text()).toContain(`Sitemap: ${SITE}/sitemap.xml`); diff --git a/e2e/templates.test.ts b/e2e/templates.test.ts new file mode 100644 index 0000000..b03229a --- /dev/null +++ b/e2e/templates.test.ts @@ -0,0 +1,110 @@ +import { expect, test } from '@playwright/test'; + +test.describe('Template gallery', () => { + test('lists every template, and the blank document first', async ({ page }) => { + await page.goto('/templates'); + await expect(page.getByRole('heading', { level: 1 })).toHaveText('Start from a template'); + await expect(page.getByTestId('template-blank')).toBeVisible(); + expect(await page.getByTestId('template-card').count()).toBeGreaterThanOrEqual(22); + }); + + test('filters by category and by search', async ({ page }) => { + await page.goto('/templates'); + await page.waitForLoadState('networkidle'); + const cards = page.getByTestId('template-card'); + const all = await cards.count(); + + await page.getByTestId('category-Events').click(); + await expect(page.getByTestId('category-Events')).toHaveAttribute('aria-pressed', 'true'); + const events = await cards.count(); + expect(events).toBeGreaterThan(0); + expect(events).toBeLessThan(all); + await expect(page.getByTestId('use-resume')).toHaveCount(0); + + await page.getByTestId('category-All').click(); + await page.getByLabel('Search templates').fill('invoice'); + await expect(cards).toHaveCount(1); + await expect(page.getByTestId('use-invoice')).toBeVisible(); + + await page.getByLabel('Search templates').fill('zzzz'); + await expect(cards).toHaveCount(0); + await expect(page.getByRole('status')).toContainText('No templates match'); + }); + + test('a template opens in the editor as its own editable document', async ({ page }) => { + await page.goto('/templates'); + await page.waitForLoadState('networkidle'); + await page.getByTestId('use-resume').click(); + + await expect(page).toHaveURL(/\/canvas$/); + await expect(page.getByTestId('document-title')).toHaveText('Resume'); + const texts = page.locator('textarea'); + await expect(texts.first()).toBeVisible(); + expect(await texts.count()).toBeGreaterThan(8); + const values = await texts.evaluateAll((all) => + all.map((t) => (t as HTMLTextAreaElement).value) + ); + expect(values).toContain('Alex Morgan'); + }); + + test('each card links to the template’s own page', async ({ page }) => { + await page.goto('/templates'); + await page.getByRole('link', { name: 'Resume', exact: true }).click(); + await expect(page).toHaveURL(/\/templates\/resume$/); + await expect(page.getByRole('heading', { level: 1 })).toHaveText('Resume template'); + await expect(page.getByRole('navigation', { name: 'Breadcrumb' })).toContainText('Templates'); + }); + + test('the blank document starts empty', async ({ page }) => { + await page.goto('/templates'); + await page.waitForLoadState('networkidle'); + await page.getByTestId('template-blank').click(); + + await expect(page).toHaveURL(/\/canvas$/); + await expect(page.locator('textarea')).toHaveCount(0); + }); + + test('the home page shows a few of them, and links to all', async ({ page, request }) => { + await page.goto('/'); + await page.waitForLoadState('networkidle'); + await expect(page.getByRole('heading', { name: 'Start from a template' })).toBeVisible(); + await expect(page.getByTestId('featured-templates').getByRole('listitem')).toHaveCount(6); + await expect(page.getByTestId('browse-templates')).toHaveAttribute('href', '/templates'); + + const sitemap = await (await request.get('/sitemap.xml')).text(); + expect(sitemap).toContain('/templates'); + }); + + test('a template on the home page opens in the editor', async ({ page }) => { + await page.goto('/'); + await page.waitForLoadState('networkidle'); + await page.getByTestId('featured-invoice').click(); + + await expect(page).toHaveURL(/\/canvas$/); + await expect(page.getByTestId('document-title')).toHaveText('Invoice'); + }); + + test('fits a phone: no sideways scroll, and the previews stay white in the dark theme', async ({ + browser + }) => { + const context = await browser.newContext({ + viewport: { width: 375, height: 812 }, + colorScheme: 'dark' + }); + const page = await context.newPage(); + await page.goto('/templates'); + await page.waitForLoadState('networkidle'); + const overflow = await page.evaluate( + () => document.documentElement.scrollWidth - document.documentElement.clientWidth + ); + expect(overflow).toBeLessThanOrEqual(0); + const paper = await page + .getByTestId('template-card') + .nth(1) + .locator('[aria-hidden="true"]') + .first() + .evaluate((el) => getComputedStyle(el).backgroundColor); + expect(paper).toBe('rgb(255, 255, 255)'); + await context.close(); + }); +}); diff --git a/src/lib/templates/TemplatePreview.svelte b/src/lib/templates/TemplatePreview.svelte new file mode 100644 index 0000000..f6ce526 --- /dev/null +++ b/src/lib/templates/TemplatePreview.svelte @@ -0,0 +1,56 @@ + + + + diff --git a/src/lib/templates/builders.ts b/src/lib/templates/builders.ts new file mode 100644 index 0000000..d86e9c4 --- /dev/null +++ b/src/lib/templates/builders.ts @@ -0,0 +1,123 @@ +import { DISPLAY_SCALE } from '../const/dimension'; +import type { TemplateElement } from './types'; + +/** + * Templates are laid out in screen pixels on an A4 page that is 868 wide (the editor's default zoom), + * so a 16 px font here is a 16 px font in the editor. Everything is converted to page units. + */ +export const PAGE_W = 868; +export const PAGE_H = 1228; +const u = (px: number) => Math.round(px / DISPLAY_SCALE); + +/** Side margin, and the width between the margins */ +export const MARGIN = 56; +export const CONTENT_W = PAGE_W - MARGIN * 2; + +export const ink = '#242424'; +export const muted = '#616161'; +export const rule = '#d1d1d1'; + +interface TextOptions { + size?: number; + weight?: string; + color?: string; + align?: 'left' | 'center' | 'right' | 'justify'; + font?: string; + italic?: boolean; + /** How many lines the text takes, to size the box (default: the number of line breaks) */ + lines?: number; + transform?: 'uppercase' | 'none'; +} + +/** A text box. The editor insets text by 8 px, so x is the edge of the box, not of the letters. */ +export function text( + x: number, + y: number, + w: number, + content: string, + { + size = 14, + weight = '400', + color = ink, + align = 'left', + font = 'Inter', + italic = false, + lines, + transform = 'none' + }: TextOptions = {} +): TemplateElement { + const count = lines ?? content.split('\n').length; + return { + type: 'text', + x: u(x - 8), + y: u(y - 8), + width: u(w + 16), + height: u(count * size * 1.3 + 16), + text: content, + fontFamily: font, + fontSize: size, + fontWeight: weight, + fontStyle: italic ? 'italic' : 'normal', + textDecoration: 'none', + textTransform: transform, + textAlign: align, + color, + backgroundColor: '#ffffff', + backgroundOpacity: 0 + }; +} + +interface BoxOptions { + radius?: number; + /** Outline instead of a fill */ + outline?: string; + lineWidth?: number; + opacity?: number; +} + +function shape( + shapeType: 'rectangle' | 'ellipse', + x: number, + y: number, + w: number, + h: number, + fill: string, + { radius = 0, outline, lineWidth = 1, opacity = 1 }: BoxOptions = {} +): TemplateElement { + return { + type: 'shape', + shapeType, + x: u(x), + y: u(y), + width: u(w), + height: u(h), + strokeColor: outline ?? fill, + strokeWidth: u(outline ? lineWidth : 0.5), + strokeOpacity: 1, + strokeStyle: 'solid', + fillColor: outline ? '#ffffff' : fill, + fillOpacity: outline ? 0 : opacity, + cornerRadius: radius, + rotation: 0 + }; +} + +export const rect = (x: number, y: number, w: number, h: number, fill: string, o?: BoxOptions) => + shape('rectangle', x, y, w, h, fill, o); + +export const ellipse = (x: number, y: number, w: number, h: number, fill: string, o?: BoxOptions) => + shape('ellipse', x, y, w, h, fill, o); + +/** A horizontal rule: a thin filled bar, because a line shape is drawn smaller than its box when it is this thin */ +export const line = (x: number, y: number, w: number, color = rule, thickness = 1) => + rect(x, y, w, thickness, color); + +/** Rows of text at an even step, e.g. the lines of an invoice */ +export const rows = ( + x: number, + y: number, + w: number, + step: number, + items: string[], + options: TextOptions = {} +) => items.map((item, i) => text(x, y + i * step, w, item, options)); diff --git a/src/lib/templates/catalog.ts b/src/lib/templates/catalog.ts new file mode 100644 index 0000000..ae5aef0 --- /dev/null +++ b/src/lib/templates/catalog.ts @@ -0,0 +1,643 @@ +import { PAGE_H, PAGE_W, ellipse, line, muted, rect, rows, text } from './builders'; +import { moreTemplates } from './catalogMore'; +import type { Template } from './types'; + +const M = 56; // side margin +const W = PAGE_W - M * 2; // width between the margins + +const entry = (y: number, role: string, where: string, when: string, note: string) => [ + text(M, y, 520, role, { size: 15, weight: '600' }), + text(M + 520, y, W - 520, when, { size: 12, color: muted, align: 'right' }), + text(M, y + 22, W, where, { size: 13, color: muted }), + text(M, y + 46, W, note, { size: 13, lines: 2 }) +]; + +const heading = (y: number, label: string, accent: string) => [ + text(M, y, W, label, { size: 12, weight: '700', color: accent, transform: 'uppercase' }), + line(M, y + 24, W) +]; + +const resume: Template = { + id: 'resume', + name: 'Resume', + description: 'A clean one-page resume with experience, education and skills.', + category: 'Career', + accent: '#0f6cbd', + elements: [ + rect(0, 0, PAGE_W, 150, '#0f6cbd'), + text(M, 44, W, 'Alex Morgan', { size: 40, weight: '700', color: '#ffffff' }), + text(M, 98, W, 'Product designer', { size: 16, color: '#dcebf7' }), + text(M, 170, W, 'alex@example.com · +1 555 010 2030 · Lisbon, Portugal', { + size: 12, + color: muted + }), + ...heading(214, 'Profile', '#0f6cbd'), + text( + M, + 250, + W, + 'Designer with eight years of experience turning complex products into clear, friendly interfaces. I work closely with engineers and researchers, and care about the details.', + { size: 13, lines: 3 } + ), + ...heading(346, 'Experience', '#0f6cbd'), + ...entry( + 384, + 'Senior product designer', + 'Northwind Studio', + '2021 – now', + 'Led the redesign of the booking flow, lifting completed bookings by 18%.\nBuilt and maintain a design system used by six product teams.' + ), + ...entry( + 480, + 'Product designer', + 'Fabrikam', + '2018 – 2021', + 'Shipped the first mobile app and ran weekly usability sessions.\nMentored two junior designers.' + ), + ...entry( + 576, + 'Interaction designer', + 'Contoso Labs', + '2016 – 2018', + 'Prototyped dashboards for logistics teams and tested them on site.\nIntroduced a shared research library.' + ), + ...heading(684, 'Education', '#0f6cbd'), + ...entry(722, 'BA in Interaction Design', 'University of the Arts', '2012 – 2016', ''), + ...heading(804, 'Skills', '#0f6cbd'), + ...rows(M, 842, 260, 24, ['Figma and prototyping', 'Design systems', 'User research'], { + size: 13 + }), + ...rows( + M + 300, + 842, + 260, + 24, + ['Accessibility (WCAG)', 'HTML and CSS', 'Workshop facilitation'], + { + size: 13 + } + ) + ] +}; + +const coverLetter: Template = { + id: 'cover-letter', + name: 'Cover letter', + description: 'A formal letter layout with your details, the date and room for the body.', + category: 'Career', + accent: '#0f6cbd', + elements: [ + rect(M, 56, 6, 64, '#0f6cbd'), + text(M + 22, 56, 400, 'Alex Morgan', { size: 26, weight: '700' }), + text(M + 22, 94, 400, 'Product designer', { size: 13, color: muted }), + text(M, 56, W, 'alex@example.com\n+1 555 010 2030\nLisbon, Portugal', { + size: 12, + color: muted, + align: 'right' + }), + line(M, 150, W), + text(M, 190, W, 'March 3, 2026', { size: 13 }), + text(M, 236, W, 'Jordan Lee\nHiring manager, Fabrikam\n12 Harbour Street, Lisbon', { + size: 13, + lines: 3 + }), + text(M, 340, W, 'Dear Jordan,', { size: 14 }), + text( + M, + 378, + W, + 'I am writing to apply for the Senior Designer role. Over eight years I have shipped products people use every day, and I would love to bring that experience to your team.\n\nIn my current role I led the redesign of a booking flow used by two million people and built the design system behind it. I enjoy the whole loop, from the first interview to the final detail in the code review.\n\nI would welcome the chance to talk about how I can help. Thank you for your time and for considering my application.', + { size: 13, lines: 13 } + ), + text(M, 740, W, 'Kind regards,', { size: 14 }), + text(M, 800, W, 'Alex Morgan', { + size: 18, + weight: '600', + italic: true, + font: 'Playfair Display' + }) + ] +}; + +const invoice: Template = { + id: 'invoice', + name: 'Invoice', + description: 'An invoice with line items, totals and payment details.', + category: 'Business', + accent: '#0f6cbd', + elements: [ + text(M, 56, 360, 'Invoice', { size: 44, weight: '700', color: '#0f6cbd' }), + text(M + 400, 60, W - 400, 'Northwind Studio\n12 Harbour Street\nLisbon, Portugal', { + size: 12, + color: muted, + align: 'right', + lines: 3 + }), + line(M, 140, W, '#0f6cbd', 2), + text(M, 170, 240, 'Billed to', { + size: 11, + weight: '700', + color: muted, + transform: 'uppercase' + }), + text(M, 192, 240, 'Fabrikam Ltd\n8 Market Square\nLondon', { size: 13, lines: 3 }), + text(M + 340, 170, 240, 'Invoice no.', { + size: 11, + weight: '700', + color: muted, + transform: 'uppercase' + }), + text(M + 340, 192, 240, 'INV-0042', { size: 13 }), + text(M + 560, 170, 240, 'Due', { + size: 11, + weight: '700', + color: muted, + transform: 'uppercase' + }), + text(M + 560, 192, 240, 'April 2, 2026', { size: 13 }), + rect(M, 300, W, 34, '#0f6cbd', { opacity: 0.12 }), + text(M + 8, 308, 380, 'Description', { size: 12, weight: '700' }), + text(M + 400, 308, 80, 'Qty', { size: 12, weight: '700', align: 'right' }), + text(M + 500, 308, 120, 'Price', { size: 12, weight: '700', align: 'right' }), + text(M + 640, 308, W - 648, 'Amount', { size: 12, weight: '700', align: 'right' }), + ...[ + ['Brand identity', '1', '1,800.00', '1,800.00'], + ['Website design', '1', '2,400.00', '2,400.00'], + ['Design review sessions', '4', '150.00', '600.00'] + ].flatMap(([d, q, p, a], i) => [ + text(M + 8, 352 + i * 44, 380, d, { size: 13 }), + text(M + 400, 352 + i * 44, 80, q, { size: 13, align: 'right' }), + text(M + 500, 352 + i * 44, 120, p, { size: 13, align: 'right' }), + text(M + 640, 352 + i * 44, W - 648, a, { size: 13, align: 'right' }), + line(M, 384 + i * 44, W) + ]), + text(M + 460, 520, 160, 'Subtotal\nTax (20%)', { + size: 13, + color: muted, + align: 'right', + lines: 2 + }), + text(M + 640, 520, W - 648, '4,800.00\n960.00', { size: 13, align: 'right', lines: 2 }), + rect(M + 460, 580, W - 460, 44, '#0f6cbd'), + text(M + 476, 590, 140, 'Total due', { size: 14, weight: '600', color: '#ffffff' }), + text(M + 640, 588, W - 648, '€5,760.00', { + size: 18, + weight: '700', + color: '#ffffff', + align: 'right' + }), + text(M, 700, W, 'Payment', { size: 11, weight: '700', color: muted, transform: 'uppercase' }), + text( + M, + 722, + 420, + 'Bank transfer to Northwind Studio\nIBAN PT50 0000 0000 0000 0000 0000 0\nPlease quote the invoice number.', + { + size: 12, + lines: 3 + } + ), + text(M, 1130, W, 'Thank you for your business.', { size: 12, color: muted, align: 'center' }) + ] +}; + +const letterhead: Template = { + id: 'letterhead', + name: 'Letterhead', + description: 'Branded stationery with your details in the header and footer.', + category: 'Business', + accent: '#0f6cbd', + elements: [ + ellipse(M, 52, 40, 40, '#0f6cbd'), + text(M + 54, 58, 320, 'Northwind Studio', { size: 20, weight: '700' }), + text(M + 54, 84, 320, 'Design and brand consultancy', { size: 11, color: muted }), + text( + M + 400, + 56, + W - 400, + '12 Harbour Street, Lisbon\nhello@northwind.example\n+351 21 000 0000', + { + size: 11, + color: muted, + align: 'right' + } + ), + line(M, 130, W, '#0f6cbd', 2), + text( + M, + 190, + W, + 'Start writing here. Click the text to edit it, or add a new text box from the left.', + { + size: 13, + color: muted, + lines: 2 + } + ), + line(M, PAGE_H - 90, W), + text(M, PAGE_H - 70, W, 'Northwind Studio Lda · Registered in Portugal · VAT PT000000000', { + size: 10, + color: muted, + align: 'center' + }) + ] +}; + +const report: Template = { + id: 'one-page-report', + name: 'One-page report', + description: 'A tidy summary with headline numbers, findings and next steps.', + category: 'Business', + accent: '#0f6cbd', + elements: [ + text(M, 56, W, 'Quarterly summary', { + size: 11, + weight: '700', + color: '#0f6cbd', + transform: 'uppercase' + }), + text(M, 80, W, 'Customers grew 24% in Q1', { size: 34, weight: '700' }), + text(M, 132, W, 'A short review of what changed, what worked and what comes next.', { + size: 14, + color: muted + }), + ...[ + ['12,480', 'Active customers'], + ['€84k', 'Monthly revenue'], + ['96%', 'Satisfaction'] + ].flatMap(([n, label], i) => [ + rect(M + i * 258, 190, 240, 100, '#f5f5f5', { radius: 4 }), + text(M + 18 + i * 258, 208, 210, n, { size: 30, weight: '700', color: '#0f6cbd' }), + text(M + 18 + i * 258, 252, 210, label, { size: 12, color: muted }) + ]), + text(M, 340, W, 'What we found', { size: 18, weight: '600' }), + text( + M, + 376, + W, + 'Referrals became our largest source of new customers, ahead of search for the first time. Support response times fell below two hours, and the share of customers who stay after the first month rose to 91%.', + { size: 13, lines: 4 } + ), + rect(M, 480, W, 180, '#0f6cbd', { opacity: 0.08, radius: 4 }), + ...[60, 104, 148].map((h, i) => + rect(M + 40 + i * 120, 640 - h, 64, h, '#0f6cbd', { radius: 2 }) + ), + ...['Jan', 'Feb', 'Mar'].map((m, i) => + text(M + 40 + i * 120, 646, 64, m, { size: 10, color: muted, align: 'center' }) + ), + text(M + 420, 504, W - 440, 'Growth by month', { size: 12, weight: '600' }), + text( + M + 420, + 530, + W - 440, + 'Each month added more customers than the one before, and none were lost to price.', + { + size: 12, + color: muted, + lines: 3 + } + ), + text(M, 700, W, 'Next steps', { size: 18, weight: '600' }), + ...rows( + M, + 740, + W, + 32, + [ + '1. Open a second support shift in the afternoons', + '2. Launch the referral reward in April', + '3. Review pricing for teams of ten or more' + ], + { + size: 13 + } + ) + ] +}; + +const flyer: Template = { + id: 'event-flyer', + name: 'Event flyer', + description: 'A bold flyer for a party, market or community event.', + category: 'Events', + accent: '#c239b3', + elements: [ + rect(0, 0, PAGE_W, PAGE_H, '#1f1f1f'), + ellipse(470, 60, 470, 470, '#c239b3', { opacity: 0.9 }), + ellipse(560, 880, 420, 420, '#0f6cbd', { opacity: 0.9 }), + text(M, 80, 420, 'Summer\nNight\nMarket', { + size: 84, + weight: '800', + color: '#ffffff', + lines: 3 + }), + text(M, 440, 420, 'Food, music and local makers', { size: 18, color: '#f2f2f2' }), + rect(M, 760, W, 3, '#ffffff'), + text(M, 790, 360, 'Saturday\nJuly 12', { size: 34, weight: '700', color: '#ffffff', lines: 2 }), + text(M + 360, 790, W - 360, '6 pm until late\nHarbour Square\nFree entry', { + size: 18, + color: '#f2f2f2', + lines: 3 + }), + rect(M, 1040, 220, 52, '#ffffff', { radius: 8 }), + text(M, 1054, 220, 'See you there', { + size: 16, + weight: '700', + color: '#1f1f1f', + align: 'center' + }) + ] +}; + +const poster: Template = { + id: 'poster', + name: 'Poster', + description: 'A big-headline poster with one strong message.', + category: 'Events', + accent: '#0f6cbd', + elements: [ + rect(0, 0, PAGE_W, PAGE_H, '#fef6e4'), + ellipse(220, 520, 420, 420, '#0f6cbd'), + ellipse(600, 110, 170, 170, '#f5a623'), + rect(M, 960, 130, 130, '#e8453c', { radius: 6 }), + text(M, 56, W, 'Make\nsomething\nworth\nseeing', { + size: 96, + weight: '800', + color: '#1b1b1b', + lines: 4 + }), + text(M + 170, 980, W - 170, 'Open studio week\nMay 5 – 11, every day from 10 am', { + size: 20, + weight: '600', + color: '#1b1b1b', + lines: 2 + }), + text(M + 170, 1052, W - 170, 'Free for everyone. Bring a friend.', { + size: 14, + color: '#4a4a4a' + }) + ] +}; + +const invitation: Template = { + id: 'invitation', + name: 'Invitation', + description: 'An elegant invitation for a dinner, birthday or celebration.', + category: 'Events', + accent: '#8a6d3b', + elements: [ + rect(32, 32, PAGE_W - 64, PAGE_H - 64, '#8a6d3b', { outline: '#8a6d3b', lineWidth: 2 }), + rect(48, 48, PAGE_W - 96, PAGE_H - 96, '#8a6d3b', { outline: '#8a6d3b', lineWidth: 1 }), + text(M, 190, W, 'You are invited', { + size: 14, + color: '#8a6d3b', + align: 'center', + transform: 'uppercase' + }), + text(M, 250, W, 'Maya’s\nBirthday Dinner', { + size: 56, + weight: '600', + align: 'center', + font: 'Playfair Display', + lines: 2 + }), + line(PAGE_W / 2 - 40, 450, 80, '#8a6d3b', 2), + text(M, 490, W, 'Saturday, October 18', { + size: 22, + align: 'center', + font: 'Playfair Display' + }), + text(M, 530, W, 'at seven in the evening', { size: 15, color: muted, align: 'center' }), + text(M, 640, W, 'The Garden Room\n24 Orchard Lane', { size: 16, align: 'center', lines: 2 }), + text( + M, + 800, + W, + 'Dinner and dancing to follow.\nPlease reply by October 1 to maya@example.com', + { + size: 13, + color: muted, + align: 'center', + lines: 2 + } + ) + ] +}; + +const menu: Template = { + id: 'menu', + name: 'Menu', + description: 'A two-column menu for a café or restaurant.', + category: 'Events', + accent: '#107c10', + elements: [ + text(M, 64, W, 'Olive & Fig', { + size: 44, + weight: '600', + align: 'center', + font: 'Playfair Display' + }), + text(M, 124, W, 'Kitchen and café', { + size: 12, + color: muted, + align: 'center', + transform: 'uppercase' + }), + line(M, 170, W, '#107c10', 2), + ...[ + { + x: M, + title: 'Starters', + items: [ + ['Roasted tomato soup', '7'], + ['Whipped feta, honey, toast', '9'], + ['Charred greens, lemon', '8'] + ] + }, + { + x: M + 420, + title: 'Mains', + items: [ + ['Mushroom and thyme risotto', '16'], + ['Grilled sea bass, fennel', '21'], + ['Roast squash, tahini', '15'] + ] + } + ].flatMap(({ x, title, items }) => [ + text(x, 214, 336, title, { + size: 20, + weight: '600', + color: '#107c10', + font: 'Playfair Display' + }), + ...items.flatMap(([dish, price], i) => [ + text(x, 262 + i * 62, 276, dish, { size: 14, weight: '500' }), + text(x + 276, 262 + i * 62, 60, price, { size: 14, align: 'right' }), + text(x, 284 + i * 62, 336, 'Made fresh each morning', { size: 11, color: muted }) + ]) + ]), + text(M, 540, 336, 'Desserts', { + size: 20, + weight: '600', + color: '#107c10', + font: 'Playfair Display' + }), + ...[ + ['Fig and almond tart', '8'], + ['Dark chocolate pot', '7'] + ].flatMap(([dish, price], i) => [ + text(M, 588 + i * 44, 276, dish, { size: 14, weight: '500' }), + text(M + 276, 588 + i * 44, 60, price, { size: 14, align: 'right' }) + ]), + text(M + 420, 540, 336, 'Drinks', { + size: 20, + weight: '600', + color: '#107c10', + font: 'Playfair Display' + }), + ...[ + ['Filter coffee', '3'], + ['Fresh mint tea', '3.5'], + ['House lemonade', '4'] + ].flatMap(([dish, price], i) => [ + text(M + 420, 588 + i * 44, 276, dish, { size: 14, weight: '500' }), + text(M + 420 + 276, 588 + i * 44, 60, price, { size: 14, align: 'right' }) + ]), + text(M, 1120, W, 'Please tell us about allergies before you order.', { + size: 11, + color: muted, + align: 'center' + }) + ] +}; + +const certificate: Template = { + id: 'certificate', + name: 'Certificate', + description: 'A certificate of achievement with a border and signature lines.', + category: 'Education', + accent: '#8a6d3b', + elements: [ + rect(28, 28, PAGE_W - 56, PAGE_H - 56, '#8a6d3b', { outline: '#8a6d3b', lineWidth: 6 }), + rect(46, 46, PAGE_W - 92, PAGE_H - 92, '#8a6d3b', { outline: '#8a6d3b', lineWidth: 1 }), + text(M, 210, W, 'Certificate', { + size: 58, + weight: '600', + align: 'center', + font: 'Playfair Display' + }), + text(M, 286, W, 'of achievement', { + size: 16, + color: '#8a6d3b', + align: 'center', + transform: 'uppercase' + }), + text(M, 400, W, 'This certificate is proudly presented to', { + size: 14, + color: muted, + align: 'center' + }), + text(M, 450, W, 'Jordan Lee', { + size: 46, + align: 'center', + italic: true, + font: 'Playfair Display' + }), + line(M + 140, 530, W - 280, '#8a6d3b', 1), + text( + M + 60, + 570, + W - 120, + 'for outstanding effort and for completing the Introduction to Design course with distinction.', + { + size: 15, + align: 'center', + lines: 2 + } + ), + ellipse(PAGE_W / 2 - 48, 720, 96, 96, '#8a6d3b', { opacity: 0.9 }), + text(M, 744, W, '★', { size: 40, color: '#ffffff', align: 'center' }), + line(M + 60, 980, 240), + line(PAGE_W - M - 300, 980, 240), + text(M + 60, 992, 240, 'Date', { size: 12, color: muted, align: 'center' }), + text(PAGE_W - M - 300, 992, 240, 'Instructor', { size: 12, color: muted, align: 'center' }) + ] +}; + +const handout: Template = { + id: 'lesson-handout', + name: 'Lesson handout', + description: 'A handout with objectives, key ideas and practice questions.', + category: 'Education', + accent: '#107c10', + elements: [ + rect(0, 0, PAGE_W, 140, '#107c10'), + text(M, 36, W, 'Lesson 4', { + size: 12, + weight: '700', + color: '#dff6dd', + transform: 'uppercase' + }), + text(M, 60, W, 'How plants make food', { size: 34, weight: '700', color: '#ffffff' }), + text(M, 180, W, 'Name: ______________________ Date: ______________', { + size: 13, + color: muted + }), + rect(M, 232, W, 130, '#dff6dd', { radius: 4 }), + text(M + 20, 248, W - 40, 'By the end of this lesson you can', { + size: 14, + weight: '700', + color: '#0b5a08' + }), + ...rows( + M + 20, + 280, + W - 40, + 26, + [ + '• Name the three things a plant needs for photosynthesis', + '• Explain where the process happens inside a leaf', + '• Describe why plants matter for all other life' + ], + { + size: 13 + } + ), + text(M, 400, W, 'Key ideas', { size: 20, weight: '600' }), + text( + M, + 440, + W, + 'Plants take in sunlight, water and carbon dioxide. Inside the leaf, chlorophyll captures the light, and the plant turns these into sugar for energy and oxygen for the air.', + { size: 13, lines: 4 } + ), + text(M, 560, W, 'Practice', { size: 20, weight: '600' }), + ...[0, 1, 2].flatMap((i) => [ + text( + M, + 604 + i * 120, + W, + `${i + 1}. Write your own question here, then leave space for the answer.`, + { size: 13 } + ), + line(M, 664 + i * 120, W) + ]) + ] +}; + +export const templates: Template[] = [ + resume, + coverLetter, + invoice, + letterhead, + report, + flyer, + poster, + invitation, + menu, + certificate, + handout, + ...moreTemplates +]; + +export const templateById = (id: string) => templates.find((t) => t.id === id); diff --git a/src/lib/templates/catalogMore.ts b/src/lib/templates/catalogMore.ts new file mode 100644 index 0000000..af5bb64 --- /dev/null +++ b/src/lib/templates/catalogMore.ts @@ -0,0 +1,493 @@ +import { + CONTENT_W as W, + MARGIN as M, + PAGE_H, + PAGE_W, + ellipse, + line, + muted, + rect, + rows, + text +} from './builders'; +import type { Template } from './types'; + +const label = (x: number, y: number, w: number, content: string, color = muted) => + text(x, y, w, content, { size: 11, weight: '700', color, transform: 'uppercase' }); + +const agenda: Template = { + id: 'meeting-agenda', + name: 'Meeting agenda', + description: 'An agenda with times, topics and owners, plus space for notes and actions.', + category: 'Business', + accent: '#0f6cbd', + elements: [ + rect(M, 56, 6, 76, '#0f6cbd'), + text(M + 22, 52, W - 22, 'Team meeting', { size: 34, weight: '700' }), + text(M + 22, 100, W - 22, 'Thursday, March 5 · 10:00 – 11:00 · Room 3 or video call', { + size: 13, + color: muted + }), + label(M, 176, 200, 'Attendees'), + text(M, 198, W, 'Alex, Jordan, Sam, Priya, Chen', { size: 13 }), + rect(M, 252, W, 34, '#0f6cbd', { opacity: 0.12 }), + text(M + 8, 260, 80, 'Time', { size: 12, weight: '700' }), + text(M + 100, 260, 380, 'Topic', { size: 12, weight: '700' }), + text(M + 560, 260, 180, 'Owner', { size: 12, weight: '700' }), + ...[ + ['10:00', 'Welcome and last week’s actions', 'Alex'], + ['10:10', 'Project status and blockers', 'Jordan'], + ['10:30', 'Launch plan for April', 'Priya'], + ['10:45', 'Open questions', 'Everyone'], + ['10:55', 'Summary and next steps', 'Alex'] + ].flatMap(([time, topic, owner], i) => [ + text(M + 8, 304 + i * 44, 80, time, { size: 13, color: muted }), + text(M + 100, 304 + i * 44, 440, topic, { size: 13 }), + text(M + 560, 304 + i * 44, 180, owner, { size: 13 }), + line(M, 334 + i * 44, W) + ]), + label(M, 560, 200, 'Notes'), + line(M, 620, W), + line(M, 668, W), + line(M, 716, W), + label(M, 780, 200, 'Actions'), + ...[0, 1, 2].flatMap((i) => [ + rect(M, 818 + i * 44, 16, 16, '#616161', { outline: '#616161', lineWidth: 1 }), + line(M + 30, 842 + i * 44, W - 30) + ]) + ] +}; + +const quotation: Template = { + id: 'quotation', + name: 'Quotation', + description: 'A price quote with scope, itemised costs and how long it is valid.', + category: 'Business', + accent: '#107c10', + elements: [ + text(M, 56, 400, 'Quotation', { size: 44, weight: '700', color: '#107c10' }), + text(M + 400, 60, W - 400, 'Harbour Builders\n12 Dock Road\nBristol', { + size: 12, + color: muted, + align: 'right' + }), + line(M, 140, W, '#107c10', 2), + label(M, 170, 240, 'Prepared for'), + text(M, 192, 300, 'Sam Rivera\n41 Elm Street\nBristol', { size: 13 }), + label(M + 400, 170, 160, 'Quote no.'), + text(M + 400, 192, 160, 'Q-1087', { size: 13 }), + label(M + 580, 170, 176, 'Valid until'), + text(M + 580, 192, 176, 'April 15, 2026', { size: 13 }), + label(M, 300, 200, 'Scope of work'), + text( + M, + 322, + W, + 'Strip and replace the kitchen floor, fit new skirting and paint the walls in two coats. Materials and waste removal are included.', + { size: 13, lines: 2 } + ), + rect(M, 406, W, 34, '#107c10', { opacity: 0.12 }), + text(M + 8, 414, 440, 'Item', { size: 12, weight: '700' }), + text(M + 560, 414, W - 568, 'Cost', { size: 12, weight: '700', align: 'right' }), + ...[ + ['Labour, 3 days', '900.00'], + ['Flooring, 14 m²', '640.00'], + ['Paint and materials', '180.00'], + ['Waste removal', '120.00'] + ].flatMap(([item, cost], i) => [ + text(M + 8, 458 + i * 42, 440, item, { size: 13 }), + text(M + 560, 458 + i * 42, W - 568, cost, { size: 13, align: 'right' }), + line(M, 488 + i * 42, W) + ]), + rect(M + 440, 650, W - 440, 44, '#107c10'), + text(M + 456, 660, 140, 'Estimated total', { size: 14, weight: '600', color: '#ffffff' }), + text(M + 600, 658, W - 608, '£1,840.00', { + size: 18, + weight: '700', + color: '#ffffff', + align: 'right' + }), + label(M, 760, 300, 'Terms'), + text( + M, + 782, + 560, + 'A 25% deposit confirms the booking. The balance is due on completion.\nPrices include VAT.', + { + size: 12, + color: muted, + lines: 2 + } + ), + line(M, 940, 240), + text(M, 950, 240, 'Accepted by (signature and date)', { size: 11, color: muted }) + ] +}; + +const receipt: Template = { + id: 'receipt', + name: 'Receipt', + description: 'A simple receipt that confirms a payment.', + category: 'Business', + accent: '#0f6cbd', + elements: [ + text(M, 56, W, 'Receipt', { + size: 14, + weight: '700', + color: '#0f6cbd', + transform: 'uppercase' + }), + text(M, 82, W, 'Payment received', { size: 38, weight: '700' }), + text(M, 138, W, 'Thank you. This confirms that your payment has been received in full.', { + size: 14, + color: muted + }), + rect(M, 196, W, 110, '#f5f5f5', { radius: 4 }), + label(M + 24, 218, 200, 'Amount paid'), + text(M + 24, 238, 400, '€240.00', { size: 40, weight: '700', color: '#0f6cbd' }), + label(M + 480, 218, 200, 'Paid on'), + text(M + 480, 238, 240, 'March 3, 2026', { size: 16, weight: '600' }), + ...[ + ['Receipt no.', 'R-2026-0318'], + ['Received from', 'Jordan Lee'], + ['For', 'Photography session, 2 hours'], + ['Payment method', 'Card ending 4242'], + ['Received by', 'Northwind Studio'] + ].flatMap(([k, v], i) => [ + text(M, 350 + i * 52, 240, k, { size: 13, color: muted }), + text(M + 260, 350 + i * 52, W - 260, v, { size: 14, weight: '500' }), + line(M, 382 + i * 52, W) + ]), + text(M, 680, W, 'Keep this receipt for your records. Questions? hello@northwind.example', { + size: 12, + color: muted + }) + ] +}; + +const proposal: Template = { + id: 'proposal-cover', + name: 'Proposal cover', + description: 'A strong cover page for a proposal, report or pitch.', + category: 'Business', + accent: '#0f6cbd', + elements: [ + rect(0, 0, PAGE_W, PAGE_H, '#0b3a66'), + ellipse(420, 560, 640, 640, '#0f6cbd', { opacity: 0.55 }), + ellipse(560, 760, 360, 360, '#2b88d8', { opacity: 0.6 }), + text(M, 70, W, 'Northwind Studio', { size: 16, weight: '600', color: '#cfe4f7' }), + text(M, 340, 640, 'Website redesign\nproposal', { + size: 62, + weight: '800', + color: '#ffffff', + lines: 2 + }), + rect(M, 520, 90, 5, '#ffffff'), + text(M, 550, 560, 'Prepared for Fabrikam Ltd\nMarch 2026', { + size: 18, + color: '#cfe4f7', + lines: 2 + }), + text(M, 1110, W, 'Confidential · Valid for 30 days', { size: 12, color: '#9ec3e6' }) + ] +}; + +const saveTheDate: Template = { + id: 'save-the-date', + name: 'Save the date', + description: 'A romantic save-the-date for a wedding or milestone.', + category: 'Events', + accent: '#b4546b', + elements: [ + rect(0, 0, PAGE_W, PAGE_H, '#fdf3f4'), + rect(40, 40, PAGE_W - 80, PAGE_H - 80, '#b4546b', { outline: '#b4546b', lineWidth: 1.5 }), + text(M, 260, W, 'Save the date', { + size: 18, + color: '#b4546b', + align: 'center', + transform: 'uppercase' + }), + text(M, 340, W, 'Anna\n&\nMarco', { + size: 72, + align: 'center', + italic: true, + font: 'Playfair Display', + color: '#3b1f27', + lines: 3 + }), + rect(PAGE_W / 2 - 50, 650, 100, 2, '#b4546b'), + text(M, 690, W, 'September 12, 2026', { size: 26, align: 'center', font: 'Playfair Display' }), + text(M, 740, W, 'Villa Rosa, Tuscany', { size: 15, color: muted, align: 'center' }), + text(M, 960, W, 'Invitation to follow', { + size: 13, + color: muted, + align: 'center', + italic: true + }) + ] +}; + +const voucher: Template = { + id: 'gift-voucher', + name: 'Gift voucher', + description: 'A gift voucher with an amount, a message and a code.', + category: 'Events', + accent: '#c239b3', + elements: [ + rect(M, 120, W, 420, '#fdf0fb', { radius: 3 }), + rect(M, 120, W, 420, '#c239b3', { outline: '#c239b3', lineWidth: 3, radius: 3 }), + ellipse(PAGE_W - M - 150, 120, 150, 150, '#c239b3', { opacity: 0.18 }), + text(M + 40, 160, 400, 'Gift voucher', { + size: 44, + weight: '700', + color: '#7a1f6f', + font: 'Playfair Display' + }), + text(M + 40, 226, 400, 'From Olive & Fig', { size: 14, color: muted }), + text(M + 40, 300, 300, '€50', { size: 90, weight: '800', color: '#c239b3' }), + label(M + 400, 316, 300, 'To'), + line(M + 400, 358, 296, '#7a1f6f'), + label(M + 400, 384, 300, 'From'), + line(M + 400, 426, 296, '#7a1f6f'), + text( + M + 40, + 480, + W - 80, + 'Code OF-7342-ZQ9 · Valid until December 31, 2026 · Cannot be exchanged for cash', + { + size: 11, + color: muted + } + ) + ] +}; + +const resignation: Template = { + id: 'resignation-letter', + name: 'Resignation letter', + description: 'A short, polite letter of resignation with a notice period.', + category: 'Career', + accent: '#616161', + elements: [ + text(M, 56, W, 'Alex Morgan\n14 Rua do Sol\nLisbon, Portugal', { + size: 13, + color: muted, + lines: 3 + }), + text(M, 170, W, 'March 3, 2026', { size: 13 }), + text(M, 216, W, 'Jordan Lee\nHead of Design, Fabrikam', { size: 13, lines: 2 }), + text(M, 306, W, 'Subject: Resignation', { size: 14, weight: '700' }), + text(M, 350, W, 'Dear Jordan,', { size: 14 }), + text( + M, + 388, + W, + 'Please accept this letter as formal notice that I am resigning from my role as Senior Designer. My last day will be April 3, 2026, in line with my one-month notice period.\n\nThank you for the support and the opportunities I have had over the past five years. I am proud of what the team has built, and I will do everything I can to make the handover smooth.\n\nPlease let me know how I can help in the coming weeks.', + { size: 13, lines: 12 } + ), + text(M, 680, W, 'Yours sincerely,', { size: 14 }), + text(M, 740, W, 'Alex Morgan', { + size: 18, + weight: '600', + italic: true, + font: 'Playfair Display' + }) + ] +}; + +const weekly: Template = { + id: 'weekly-planner', + name: 'Weekly planner', + description: 'A week at a glance with a space for each day and a focus box.', + category: 'Personal', + accent: '#0f6cbd', + elements: [ + rect(0, 0, PAGE_W, 120, '#0f6cbd'), + text(M, 34, 400, 'Week of ____ / ____', { size: 30, weight: '700', color: '#ffffff' }), + text(M + 440, 44, W - 440, 'Plan the week, then do it.', { + size: 13, + color: '#dcebf7', + align: 'right' + }), + rect(M, 148, W, 90, '#f5f5f5', { radius: 4 }), + label(M + 18, 164, 300, 'Focus this week', '#0f6cbd'), + line(M + 18, 214, W - 36), + ...['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'].flatMap((day, i) => [ + text(M, 268 + i * 100, 140, day, { size: 14, weight: '700' }), + line(M + 140, 290 + i * 100, W - 140), + line(M + 140, 330 + i * 100, W - 140) + ]), + text(M, 772, 140, 'Weekend', { size: 14, weight: '700' }), + line(M + 140, 794, W - 140), + line(M + 140, 834, W - 140) + ] +}; + +const recipe: Template = { + id: 'recipe-card', + name: 'Recipe card', + description: 'A recipe with ingredients, steps and a few key details.', + category: 'Personal', + accent: '#c4511a', + elements: [ + rect(0, 0, PAGE_W, 140, '#c4511a'), + text(M, 36, W, 'Recipe', { size: 12, weight: '700', color: '#fbe3d6', transform: 'uppercase' }), + text(M, 62, W, 'Lemon & herb roast chicken', { size: 34, weight: '700', color: '#ffffff' }), + ...[ + ['Prep', '15 min'], + ['Cook', '1 hr 20'], + ['Serves', '4'] + ].flatMap(([k, v], i) => [ + text(M + i * 220, 170, 200, k, { + size: 11, + weight: '700', + color: muted, + transform: 'uppercase' + }), + text(M + i * 220, 190, 200, v, { size: 20, weight: '600' }) + ]), + line(M, 246, W), + text(M, 276, 260, 'Ingredients', { + size: 20, + weight: '600', + color: '#c4511a', + font: 'Playfair Display' + }), + ...rows( + M, + 322, + 260, + 34, + [ + '1 whole chicken, 1.5 kg', + '2 lemons', + '4 cloves of garlic', + 'A handful of thyme', + '3 tbsp olive oil', + 'Salt and pepper' + ], + { size: 14 } + ), + text(M + 320, 276, W - 320, 'Method', { + size: 20, + weight: '600', + color: '#c4511a', + font: 'Playfair Display' + }), + ...rows( + M + 320, + 322, + W - 320, + 46, + [ + '1. Heat the oven to 200°C.', + '2. Rub the chicken with oil, salt and pepper.', + '3. Fill with lemon, garlic and thyme.', + '4. Roast for 80 minutes, then rest for 10.' + ], + { size: 14, lines: 2 } + ) + ] +}; + +const timetable: Template = { + id: 'class-timetable', + name: 'Class timetable', + description: 'A weekly timetable for school or study, in colour.', + category: 'Education', + accent: '#107c10', + elements: [ + text(M, 56, W, 'Class timetable', { size: 34, weight: '700' }), + text(M, 106, W, 'Autumn term · Name: ____________________', { size: 13, color: muted }), + rect(M, 160, W, 38, '#107c10'), + ...['Mon', 'Tue', 'Wed', 'Thu', 'Fri'].map((d, i) => + text(M + 92 + i * 132, 169, 120, d, { + size: 13, + weight: '700', + color: '#ffffff', + align: 'center' + }) + ), + ...['9:00', '10:00', '11:00', '12:00', '1:30', '2:30'].flatMap((time, r) => [ + text(M + 8, 220 + r * 86, 76, time, { size: 12, color: muted }), + line(M, 208 + r * 86, W) + ]), + ...[ + [0, 0, 'Maths', '#dff6dd'], + [1, 0, 'English', '#deecf9'], + [2, 0, 'Science', '#fdf3c8'], + [3, 0, 'Maths', '#dff6dd'], + [4, 0, 'Art', '#fbe0ee'], + [0, 1, 'Science', '#fdf3c8'], + [1, 1, 'History', '#e8e0f7'], + [2, 1, 'English', '#deecf9'], + [3, 1, 'Music', '#fde3d6'], + [4, 1, 'Maths', '#dff6dd'], + [1, 3, 'Sport', '#d9f2f0'], + [3, 3, 'Sport', '#d9f2f0'] + ].flatMap(([col, row, subject, color]) => [ + rect(M + 92 + (col as number) * 132, 216 + (row as number) * 86, 124, 74, color as string, { + radius: 6 + }), + text(M + 92 + (col as number) * 132, 238 + (row as number) * 86, 124, subject as string, { + size: 14, + weight: '600', + align: 'center', + color: '#242424' + }) + ]) + ] +}; + +const thankYou: Template = { + id: 'thank-you-card', + name: 'Thank-you card', + description: 'A warm thank-you note with room for a personal message.', + category: 'Events', + accent: '#107c10', + elements: [ + rect(0, 0, PAGE_W, PAGE_H, '#f3faf2'), + ellipse(-140, -140, 420, 420, '#107c10', { opacity: 0.16 }), + ellipse(PAGE_W - 280, PAGE_H - 280, 520, 520, '#107c10', { opacity: 0.16 }), + text(M, 300, W, 'Thank you', { + size: 84, + align: 'center', + italic: true, + font: 'Playfair Display', + color: '#0b5a08' + }), + rect(PAGE_W / 2 - 40, 450, 80, 3, '#107c10'), + text( + M + 80, + 500, + W - 160, + 'Your kindness made our day. We are so grateful to have you in our lives.', + { + size: 18, + align: 'center', + color: '#2b4a2a', + lines: 3 + } + ), + text(M, 720, W, 'With love,\nThe Rivera family', { + size: 16, + align: 'center', + color: '#0b5a08', + lines: 2 + }) + ] +}; + +export const moreTemplates: Template[] = [ + agenda, + quotation, + receipt, + proposal, + saveTheDate, + voucher, + resignation, + weekly, + recipe, + timetable, + thankYou +]; diff --git a/src/lib/templates/index.ts b/src/lib/templates/index.ts new file mode 100644 index 0000000..813b732 --- /dev/null +++ b/src/lib/templates/index.ts @@ -0,0 +1,36 @@ +import { CANVAS_HEIGHT, CANVAS_WIDTH } from '../const/dimension'; +import { CURRENT_SCHEMA_VERSION } from '../stores/migrations'; +import type { DocumentData, TCanvasElement } from '../types/canvas'; +import { uuidv4 } from '../utils/uuid'; +import type { Template } from './types'; + +export { templates, templateById } from './catalog'; +export { TEMPLATE_CATEGORIES, type Template, type TemplateCategory } from './types'; + +/** A new document with the template's page. Ids are new, so each use is its own document. */ +export function documentFromTemplate(template: Template): DocumentData { + const pageId = uuidv4(); + const elements: Record = {}; + template.elements.forEach((element, zIndex) => { + const id = uuidv4(); + elements[id] = { ...element, id, pageId, zIndex, parentElementId: null } as TCanvasElement; + }); + const now = Date.now(); + return { + id: uuidv4(), + schemaVersion: CURRENT_SCHEMA_VERSION, + name: template.name, + pages: { + [pageId]: { + id: pageId, + elements, + boundaries: { + horizontal: { start: 100, end: CANVAS_WIDTH - 100 }, + vertical: { start: 100, end: CANVAS_HEIGHT - 100 } + } + } + }, + createdAt: now, + updatedAt: now + }; +} diff --git a/src/lib/templates/open.ts b/src/lib/templates/open.ts new file mode 100644 index 0000000..7fa663e --- /dev/null +++ b/src/lib/templates/open.ts @@ -0,0 +1,11 @@ +import { goto } from '$app/navigation'; +import { resolve } from '$app/paths'; +import { appStore } from '$lib/stores/appStore.svelte.ts'; +import { documentFromTemplate } from '.'; +import type { Template } from './types'; + +/** Open a new document made from the template in the editor. */ +export async function openTemplate(template: Template) { + appStore.setCurrentDocument(documentFromTemplate(template)); + await goto(resolve('/canvas')); +} diff --git a/src/lib/templates/templates.spec.ts b/src/lib/templates/templates.spec.ts new file mode 100644 index 0000000..d4127eb --- /dev/null +++ b/src/lib/templates/templates.spec.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest'; +import { CANVAS_HEIGHT, CANVAS_WIDTH } from '../const/dimension'; +import { documentFromTemplate, templates } from '.'; + +describe('templates', () => { + it('have unique ids and a name, description and category each', () => { + expect(new Set(templates.map((t) => t.id)).size).toBe(templates.length); + for (const t of templates) { + expect(t.name && t.description && t.category).toBeTruthy(); + expect(t.elements.length).toBeGreaterThan(3); + } + }); + + it('keep all their text on the page', () => { + for (const t of templates) { + for (const el of t.elements) { + if (el.type !== 'text') continue; + expect(el.x, `${t.id}: "${el.text}"`).toBeGreaterThanOrEqual(0); + expect(el.y, `${t.id}: "${el.text}"`).toBeGreaterThanOrEqual(0); + expect(el.x + el.width, `${t.id}: "${el.text}"`).toBeLessThanOrEqual(CANVAS_WIDTH); + expect(el.y + el.height, `${t.id}: "${el.text}"`).toBeLessThanOrEqual(CANVAS_HEIGHT); + } + } + }); + + it('make a new document each time, with ids that point at the right page', () => { + const a = documentFromTemplate(templates[0]); + const b = documentFromTemplate(templates[0]); + expect(a.id).not.toBe(b.id); + expect(a.name).toBe(templates[0].name); + + const [page] = Object.values(a.pages); + const elements = Object.values(page.elements); + expect(elements).toHaveLength(templates[0].elements.length); + expect(elements.every((el) => el.pageId === page.id && page.elements[el.id] === el)).toBe(true); + // stacked in the order the template lists them + expect(elements.map((el) => el.zIndex)).toEqual(elements.map((_, i) => i)); + }); +}); diff --git a/src/lib/templates/types.ts b/src/lib/templates/types.ts new file mode 100644 index 0000000..750c66b --- /dev/null +++ b/src/lib/templates/types.ts @@ -0,0 +1,29 @@ +import type { TCanvasElement } from '../types/canvas'; + +export const TEMPLATE_CATEGORIES = [ + 'Career', + 'Business', + 'Events', + 'Education', + 'Personal' +] as const; +export type TemplateCategory = (typeof TEMPLATE_CATEGORIES)[number]; + +type Omitted = 'id' | 'pageId' | 'zIndex'; +/** An element as a template describes it: ids and stacking are given when a document is made from it */ +export type TemplateElement = TCanvasElement extends infer E + ? E extends TCanvasElement + ? Omit + : never + : never; + +export interface Template { + id: string; + name: string; + description: string; + category: TemplateCategory; + /** Colour of the preview's main accent */ + accent: string; + /** The elements of its single A4 page, back to front */ + elements: TemplateElement[]; +} diff --git a/src/lib/utils/sortDocuments.spec.ts b/src/lib/utils/sortDocuments.spec.ts new file mode 100644 index 0000000..d758f37 --- /dev/null +++ b/src/lib/utils/sortDocuments.spec.ts @@ -0,0 +1,49 @@ +import { describe, expect, it } from 'vitest'; +import { sampleDoc } from './testDocument'; +import { sortDocuments } from './sortDocuments'; + +const doc = (id: string, name: string, createdAt: number, updatedAt: number) => ({ + ...sampleDoc(id, name), + createdAt, + updatedAt +}); + +const docs = [ + doc('a', 'Invoice 10', 1, 50), + doc('b', 'invoice 2', 3, 90), + doc('c', 'Zebra', 2, 10), + doc('d', 'apple', 4, 70) +]; +const ids = (list: { id: string }[]) => list.map((d) => d.id).join(''); + +describe('sortDocuments', () => { + it('puts the most recently updated first, and the least recent first when asked', () => { + expect(ids(sortDocuments(docs, 'updated-desc'))).toBe('bdac'); + expect(ids(sortDocuments(docs, 'updated-asc'))).toBe('cadb'); + }); + + it('puts the newest created first', () => { + expect(ids(sortDocuments(docs, 'created-desc'))).toBe('dbca'); + }); + + it('sorts names the way people read them: numbers by value, case ignored', () => { + expect(sortDocuments(docs, 'name-asc').map((d) => d.name)).toEqual([ + 'apple', + 'invoice 2', + 'Invoice 10', + 'Zebra' + ]); + expect(ids(sortDocuments(docs, 'name-desc'))).toBe('cabd'); + }); + + it('breaks ties by the latest update, then by id, so the order is stable', () => { + const same = [doc('x', 'Same', 1, 10), doc('y', 'same', 1, 20), doc('w', 'SAME', 1, 20)]; + expect(ids(sortDocuments(same, 'name-asc'))).toBe('wyx'); + }); + + it('does not change the list it is given', () => { + const copy = [...docs]; + sortDocuments(docs, 'name-asc'); + expect(docs).toEqual(copy); + }); +}); diff --git a/src/lib/utils/sortDocuments.ts b/src/lib/utils/sortDocuments.ts new file mode 100644 index 0000000..de19477 --- /dev/null +++ b/src/lib/utils/sortDocuments.ts @@ -0,0 +1,32 @@ +import type { DocumentData } from '../types/canvas'; + +export const SORT_OPTIONS = [ + { value: 'updated-desc', label: 'Recently updated' }, + { value: 'updated-asc', label: 'Least recently updated' }, + { value: 'created-desc', label: 'Newest created' }, + { value: 'name-asc', label: 'Name (A–Z)' }, + { value: 'name-desc', label: 'Name (Z–A)' } +] as const; + +export type SortKey = (typeof SORT_OPTIONS)[number]['value']; +export const DEFAULT_SORT: SortKey = 'updated-desc'; + +export const isSortKey = (value: unknown): value is SortKey => + SORT_OPTIONS.some((option) => option.value === value); + +// Names compare the way people read them: "Invoice 2" before "Invoice 10", and "apple" next to "Apple" +const byName = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }); + +/** A sorted copy. Ties fall back to the most recently updated, then the id, so the order never jumps around. */ +export function sortDocuments(documents: DocumentData[], key: SortKey): DocumentData[] { + const compare: Record number> = { + 'updated-desc': (a, b) => b.updatedAt - a.updatedAt, + 'updated-asc': (a, b) => a.updatedAt - b.updatedAt, + 'created-desc': (a, b) => b.createdAt - a.createdAt, + 'name-asc': (a, b) => byName.compare(a.name, b.name), + 'name-desc': (a, b) => byName.compare(b.name, a.name) + }; + return [...documents].sort( + (a, b) => compare[key](a, b) || b.updatedAt - a.updatedAt || a.id.localeCompare(b.id) + ); +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index cf6fc57..6afe9cc 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -4,8 +4,23 @@ import { Icons } from '$lib/icons'; import { ChevronDown } from '@lucide/svelte'; import { resolve } from '$app/paths'; + import { templateById } from '$lib/templates'; + import { openTemplate } from '$lib/templates/open'; + import TemplatePreview from '$lib/templates/TemplatePreview.svelte'; import { AUTHOR_NAME, AUTHOR_URL, SITE_DESCRIPTION, SITE_NAME, SITE_URL } from '$lib/const/seo'; + // A few of the templates, in the order they are shown. Only ones set in Inter, the one font this page loads. + const featured = [ + 'resume', + 'invoice', + 'event-flyer', + 'one-page-report', + 'proposal-cover', + 'class-timetable' + ] + .map((id) => templateById(id)) + .filter((t) => t !== undefined); + const currentYear: number = new Date().getFullYear(); const comparison = [ @@ -133,6 +148,11 @@ >Why Lienzo +