Page features: directory and blog post tabs, image converter, version names
- Featured event badge: above the event's card from lg up, left out on phones - Version names from the tag (The Region Cards Update) in the site and admin footers - Tools -> Image converter (JPG, PNG, WebP in the browser) and Download as in Media - People directory sections take several tags, a tab each (migration 078) - Tab blocks split a blog post into tabs; contents follow the open tab (migration 079) - The side nav sun fades out as the page scrolls into the footer Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a6c66d4607
commit
31afa889d6
28 changed files with 878 additions and 70 deletions
|
|
@ -1,17 +1,42 @@
|
|||
import { mediaUrl } from "../media.ts";
|
||||
import { PUBLISHED_FIELD, LISTED_FIELD, blocksChild } from "./shared.ts";
|
||||
import { PUBLISHED_FIELD, LISTED_FIELD, BLOCK_TYPES, TEXT_TYPES, blocksChild } from "./shared.ts";
|
||||
|
||||
/* ── Blog (031_blog.sql) ─────────────────────────────────────────
|
||||
Posts at /blog/:id. The Community page's blog section is edited
|
||||
with that page (sitePages.ts). */
|
||||
|
||||
/* A post's body is the content blocks without the card/body choice:
|
||||
a post only has a body. */
|
||||
a post only has a body. Posts also take Tab blocks (079), which
|
||||
split the post into tabs (BlogPost.tsx, splitTabs). */
|
||||
const TAB_TYPE = [
|
||||
"tab",
|
||||
"Tab",
|
||||
"Starts a tab, titled by its text, holding the blocks after it up to the next Tab. Blocks before the first Tab are the intro above the tabs. The contents list follows the open tab.",
|
||||
];
|
||||
const POST_BLOCK_TYPES = [...BLOCK_TYPES, TAB_TYPE];
|
||||
|
||||
const blogBlocksChild = {
|
||||
...blocksChild,
|
||||
label: "Post body",
|
||||
note: "The post, top to bottom. The first paragraph doubles as the excerpt when that's left blank.",
|
||||
fields: blocksChild.fields.filter((field) => field.path !== "slot"),
|
||||
note: "The post, top to bottom. The first paragraph doubles as the excerpt when that's left blank. Add Tab blocks to split it into tabs.",
|
||||
fields: blocksChild.fields
|
||||
.filter((field) => field.path !== "slot")
|
||||
.map((field) =>
|
||||
field.path === "type"
|
||||
? {
|
||||
...field,
|
||||
options: POST_BLOCK_TYPES.map(([value, label]) => [value, label]),
|
||||
help: (row) => POST_BLOCK_TYPES.find(([value]) => value === row?.type)?.[2],
|
||||
}
|
||||
: field.path === "text"
|
||||
? {
|
||||
...field,
|
||||
label: (row) => (row?.type === "tab" ? "Tab title" : (field.label as any)(row)),
|
||||
when: { path: "type", value: [...TEXT_TYPES, "tab"] },
|
||||
help: (row) => (row?.type === "tab" ? "eg. Overview" : (field.help as any)(row)),
|
||||
}
|
||||
: field,
|
||||
),
|
||||
};
|
||||
|
||||
export const blogPosts = {
|
||||
|
|
|
|||
|
|
@ -102,19 +102,6 @@ const sectionsChild = (presets: string[]) => ({
|
|||
},
|
||||
]
|
||||
: []),
|
||||
...(presets.includes("people_directory")
|
||||
? [
|
||||
{
|
||||
path: "tag_id",
|
||||
label: "Directory: tag",
|
||||
widget: "select",
|
||||
optionsFrom: "person_tag_types",
|
||||
blankLabel: "Default: none (shows nothing)",
|
||||
help: "Shows the first of its people and links to the full directory",
|
||||
when: { path: "preset", value: "people_directory" },
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(presets.includes("partner_list")
|
||||
? [
|
||||
{
|
||||
|
|
@ -146,6 +133,16 @@ const sectionsChild = (presets: string[]) => ({
|
|||
: []),
|
||||
],
|
||||
children: [
|
||||
{
|
||||
key: "tags",
|
||||
label: "Directory: tags",
|
||||
note: "Each People tag is a tab, in this order, eg. Musicians, Authors, Creators: the first of its people, and a link to its full directory. One tag shows without tabs. Tags are made under Data → People tags.",
|
||||
addLabel: "Add tag",
|
||||
when: { path: "preset", value: "people_directory" },
|
||||
title: (row, options) => options?.person_tag_types?.find((t) => t.id === row.tag_id)?.label ?? "New tag",
|
||||
blank: { tag_id: "" },
|
||||
fields: [{ path: "tag_id", label: "Tag", widget: "select", optionsFrom: "person_tag_types", required: true }],
|
||||
},
|
||||
{
|
||||
key: "orgs",
|
||||
label: "Cards: organizations",
|
||||
|
|
|
|||
|
|
@ -25,8 +25,9 @@ const slug = (text: string) =>
|
|||
.slice(0, 60) || 'section'
|
||||
|
||||
/* One entry per heading or subheading block, in order, and the id
|
||||
for each block index that is one. */
|
||||
export function headingAnchors(blocks?: ContentBlock[] | null): {
|
||||
for each block index that is one. prefix keeps a blog post's tabs
|
||||
apart (splitTabs): "Overview" in two tabs is two ids. */
|
||||
export function headingAnchors(blocks?: ContentBlock[] | null, prefix = ''): {
|
||||
entries: ContentsEntry[]
|
||||
idAt: Map<number, string>
|
||||
} {
|
||||
|
|
@ -40,9 +41,39 @@ export function headingAnchors(blocks?: ContentBlock[] | null): {
|
|||
const base = slug(text)
|
||||
const n = (seen.get(base) ?? 0) + 1
|
||||
seen.set(base, n)
|
||||
const id = n === 1 ? base : `${base}-${n}`
|
||||
const id = `${prefix}${n === 1 ? base : `${base}-${n}`}`
|
||||
idAt.set(index, id)
|
||||
entries.push({ id, text, level: block.type === 'heading' ? 1 : 2 })
|
||||
})
|
||||
return { entries, idAt }
|
||||
}
|
||||
|
||||
/* ── A blog post's tabs (079) ──────────────────────────────────
|
||||
A 'tab' block starts a tab, titled by its text, holding the
|
||||
blocks after it up to the next; the blocks before the first are
|
||||
the intro above the tabs. Each tab's heading ids start with its
|
||||
own, so the same heading in two tabs is two ids; the intro's
|
||||
keep their plain ids. */
|
||||
|
||||
export type PostTab = { id: string; title: string; prefix: string; blocks: ContentBlock[] }
|
||||
|
||||
export function splitTabs(blocks?: ContentBlock[] | null): { intro: ContentBlock[]; tabs: PostTab[] } {
|
||||
const intro: ContentBlock[] = []
|
||||
const tabs: PostTab[] = []
|
||||
const seen = new Map<string, number>()
|
||||
for (const block of blocks ?? []) {
|
||||
if (block.type === 'tab') {
|
||||
const title = (block.text ?? '').trim() || `Part ${tabs.length + 1}`
|
||||
const base = `tab-${slug(title)}`
|
||||
const n = (seen.get(base) ?? 0) + 1
|
||||
seen.set(base, n)
|
||||
const id = n === 1 ? base : `${base}-${n}`
|
||||
tabs.push({ id, title, prefix: `${id}-`, blocks: [] })
|
||||
} else if (tabs.length) {
|
||||
tabs[tabs.length - 1].blocks.push(block)
|
||||
} else {
|
||||
intro.push(block)
|
||||
}
|
||||
}
|
||||
return { intro, tabs }
|
||||
}
|
||||
|
|
|
|||
72
src/lib/convertImage.ts
Normal file
72
src/lib/convertImage.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
/* ═══════════════════════════════════════════════════════════════
|
||||
IMAGE CONVERSION, in the browser
|
||||
|
||||
Tools → Image converter and the Media library's Download as: a
|
||||
picture redrawn on a canvas and saved as another format. Nothing
|
||||
is uploaded. Reads whatever this browser can show (JPEG, PNG,
|
||||
WebP, GIF's first frame, AVIF, BMP, SVG; HEIC in Safari only).
|
||||
JPEG has no transparency, so a JPEG's see-through parts are
|
||||
filled white.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
export const OUTPUT_FORMATS = [
|
||||
{ type: 'image/jpeg', label: 'JPG', ext: 'jpg', lossy: true },
|
||||
{ type: 'image/png', label: 'PNG', ext: 'png', lossy: false },
|
||||
{ type: 'image/webp', label: 'WebP', ext: 'webp', lossy: true },
|
||||
] as const
|
||||
|
||||
export type OutputType = (typeof OUTPUT_FORMATS)[number]['type']
|
||||
|
||||
/* An SVG with no size of its own is drawn this wide. */
|
||||
const SVG_EDGE = 2048
|
||||
|
||||
function loadImage(blob: Blob): Promise<HTMLImageElement> {
|
||||
const url = URL.createObjectURL(blob)
|
||||
return new Promise((resolve, reject) => {
|
||||
const img = new Image()
|
||||
img.onload = () => resolve(img)
|
||||
img.onerror = () => reject(new Error("This browser can't read that picture."))
|
||||
img.src = url
|
||||
}).finally(() => URL.revokeObjectURL(url)) as Promise<HTMLImageElement>
|
||||
}
|
||||
|
||||
/** The picture as `type`; quality 0 to 1 for JPG and WebP. */
|
||||
export async function convertImage(blob: Blob, type: OutputType, quality = 0.9): Promise<Blob> {
|
||||
const img = await loadImage(blob)
|
||||
const width = img.naturalWidth || SVG_EDGE
|
||||
const height = img.naturalHeight || SVG_EDGE
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = width
|
||||
canvas.height = height
|
||||
const context = canvas.getContext('2d')
|
||||
if (!context) throw new Error("This browser can't draw the picture.")
|
||||
if (type === 'image/jpeg') {
|
||||
context.fillStyle = '#ffffff'
|
||||
context.fillRect(0, 0, width, height)
|
||||
}
|
||||
context.drawImage(img, 0, 0, width, height)
|
||||
const out = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, type, quality))
|
||||
// A browser that can't write the format (old Safari and WebP)
|
||||
// hands back a PNG, or nothing.
|
||||
if (!out || out.type !== type) throw new Error(`This browser can't save ${type.split('/')[1].toUpperCase()}.`)
|
||||
return out
|
||||
}
|
||||
|
||||
/** A file name with the new format's extension. */
|
||||
export function renamed(name: string, type: OutputType): string {
|
||||
const ext = OUTPUT_FORMATS.find((f) => f.type === type)?.ext ?? 'img'
|
||||
const stem = name.replace(/\.[^./]+$/, '') || 'image'
|
||||
return `${stem}.${ext}`
|
||||
}
|
||||
|
||||
/** Hands the browser a file to save. */
|
||||
export function saveBlob(blob: Blob, filename: string) {
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = filename
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
setTimeout(() => URL.revokeObjectURL(url), 1000)
|
||||
}
|
||||
|
|
@ -38,6 +38,7 @@ export type ContentBlock = {
|
|||
| 'callout'
|
||||
| 'schedule'
|
||||
| 'table'
|
||||
| 'tab'
|
||||
| 'cards'
|
||||
| 'people'
|
||||
text?: string | null
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ export type SitePageSection = {
|
|||
scope_id?: string | null
|
||||
event_type?: string | null
|
||||
view?: string | null
|
||||
/** A people_directory preset's tag (Data → People tags). */
|
||||
tag_id?: string | null
|
||||
/** A people_directory preset's tags (Data → People tags), a tab each, in order. */
|
||||
tag_ids: string[]
|
||||
/** Organization cards: the kinds listed ('partner', 'ministry' or both, comma separated). */
|
||||
org_kinds?: string | null
|
||||
/** Organization cards: a hand-picked list, in order, shown instead. */
|
||||
|
|
|
|||
|
|
@ -2,9 +2,27 @@
|
|||
SITE VERSION: src/lib/version.ts
|
||||
|
||||
One string, bumped by hand when the working directory name
|
||||
changes. The admin footer is the only thing reading it today;
|
||||
keep it here rather than in a component so the panel, an about
|
||||
box or a build banner can read the same value later.
|
||||
changes. Read by the admin's footer and home, and the site's
|
||||
footer.
|
||||
|
||||
Each version has a name, Minecraft style: the tag's words after
|
||||
the number, "v1.13.2-region-cards" → "The Region Cards Update".
|
||||
A version with no words after its number has no name.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
export const SITE_VERSION = "NGU-Web.v1.13.2-region-cards";
|
||||
|
||||
/** The number, eg. v1.13.2. */
|
||||
export const VERSION_NUMBER: string = SITE_VERSION.match(/v\d+(?:\.\d+)*/)?.[0] ?? SITE_VERSION
|
||||
|
||||
/** The version's name from its tag, eg. The Region Cards Update, or null. */
|
||||
export const VERSION_NAME: string | null = (() => {
|
||||
const words = SITE_VERSION.match(/v\d+(?:\.\d+)*-(.+)$/)?.[1]
|
||||
if (!words) return null
|
||||
const title = words
|
||||
.split('-')
|
||||
.filter(Boolean)
|
||||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
||||
.join(' ')
|
||||
return `The ${title} Update`
|
||||
})()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue