@mccaskie-labs/workbench-library 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +62 -2
- package/dist/access.d.ts +7 -0
- package/dist/access.js +46 -0
- package/dist/cli.d.ts +12 -0
- package/dist/cli.js +142 -0
- package/dist/config.d.ts +7 -0
- package/dist/config.js +84 -0
- package/dist/embed.d.ts +6 -0
- package/dist/embed.js +53 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +11 -0
- package/dist/feed.d.ts +14 -0
- package/dist/feed.js +74 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +16 -0
- package/dist/library.d.ts +156 -0
- package/dist/library.js +422 -0
- package/dist/links.d.ts +37 -0
- package/dist/links.js +68 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +619 -0
- package/dist/sponsors.d.ts +3 -0
- package/dist/sponsors.js +8 -0
- package/dist/starters.d.ts +20 -0
- package/dist/starters.js +16 -0
- package/dist/styles.d.ts +33 -0
- package/dist/styles.js +86 -0
- package/dist/text.d.ts +3 -0
- package/dist/text.js +26 -0
- package/dist/time.d.ts +6 -0
- package/dist/time.js +36 -0
- package/dist/types.d.ts +245 -0
- package/dist/types.js +3 -0
- package/dist/ui.d.ts +42 -0
- package/dist/ui.js +148 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +128 -0
- package/package.json +52 -4
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** One CSS custom property the rules read, with its default. */
|
|
2
|
+
export interface StyleVariable {
|
|
3
|
+
/** `--wb-<tool>-<what>`. */
|
|
4
|
+
name: string;
|
|
5
|
+
value: string;
|
|
6
|
+
about: string;
|
|
7
|
+
}
|
|
8
|
+
/** A rule for a class in another state, or for an element inside it. */
|
|
9
|
+
export interface StyleState {
|
|
10
|
+
selector: string;
|
|
11
|
+
about: string;
|
|
12
|
+
css: string;
|
|
13
|
+
}
|
|
14
|
+
/** One class the tool puts on an element, with its default rule. */
|
|
15
|
+
export interface StyleClass {
|
|
16
|
+
name: string;
|
|
17
|
+
element: string;
|
|
18
|
+
about: string;
|
|
19
|
+
css: string;
|
|
20
|
+
states?: readonly StyleState[];
|
|
21
|
+
}
|
|
22
|
+
/** Every class and variable the tool draws with. */
|
|
23
|
+
export interface ToolStyles {
|
|
24
|
+
format: 1;
|
|
25
|
+
tool: string;
|
|
26
|
+
package: string;
|
|
27
|
+
title: string;
|
|
28
|
+
prefix: string;
|
|
29
|
+
variables: readonly StyleVariable[];
|
|
30
|
+
classes: readonly StyleClass[];
|
|
31
|
+
}
|
|
32
|
+
/** The style hooks of everything '@mccaskie-labs/workbench-library/ui' draws. */
|
|
33
|
+
export declare const LIBRARY_STYLES: ToolStyles;
|
package/dist/styles.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// The look the collection page, item page, locked notice and member's list
|
|
2
|
+
// start with (docs/capabilities-manifest.md §10). Every element the
|
|
3
|
+
// ui functions draw carries one of these classes, and every class has a
|
|
4
|
+
// default rule here, so they look finished out of the box and a project
|
|
5
|
+
// restyles them from its own CSS by the same names. `npx workbench-ui styles`
|
|
6
|
+
// writes these into the project's public/styles/workbench.css inside
|
|
7
|
+
// `@layer workbench`. The names are public: renaming one is a breaking change.
|
|
8
|
+
const v = (name) => `var(--wb-library-${name})`;
|
|
9
|
+
const box = `color:${v("text")};background:${v("surface")};border:1px solid ${v("line")};border-radius:${v("radius")}`;
|
|
10
|
+
const quiet = `color:${v("muted")};font-size:.875rem`;
|
|
11
|
+
/** The style hooks of everything '@mccaskie-labs/workbench-library/ui' draws. */
|
|
12
|
+
export const LIBRARY_STYLES = {
|
|
13
|
+
format: 1,
|
|
14
|
+
tool: "library",
|
|
15
|
+
package: "@mccaskie-labs/workbench-library",
|
|
16
|
+
title: "Library: courses, shows and resources",
|
|
17
|
+
prefix: "wb-library-",
|
|
18
|
+
variables: [
|
|
19
|
+
{ name: "--wb-library-text", value: "var(--wb-text, #1f2937)", about: "Text: titles, lessons, notes" },
|
|
20
|
+
{ name: "--wb-library-muted", value: "var(--wb-muted, #5b6472)", about: "Quieter text: running times, dates, counts" },
|
|
21
|
+
{ name: "--wb-library-surface", value: "var(--wb-surface, #f7f8f8)", about: "Background of the lists, notices and player" },
|
|
22
|
+
{ name: "--wb-library-line", value: "var(--wb-line, #d7d8db)", about: "Borders and dividers" },
|
|
23
|
+
{ name: "--wb-library-accent", value: "var(--wb-accent, #2563eb)", about: "Links, buttons and the preview badge" },
|
|
24
|
+
{ name: "--wb-library-good", value: "var(--wb-success, #15803d)", about: "Done ticks and the done badge" },
|
|
25
|
+
{ name: "--wb-library-warn", value: "var(--wb-warning, #a15c07)", about: "The edge of a locked notice" },
|
|
26
|
+
{ name: "--wb-library-radius", value: "var(--wb-radius, 8px)", about: "Corner rounding" },
|
|
27
|
+
{ name: "--wb-library-measure", value: "42rem", about: "The widest a page's text runs" },
|
|
28
|
+
],
|
|
29
|
+
classes: [
|
|
30
|
+
{ name: "wb-library-collection", element: "section", about: "A course, show or shelf page", css: `max-width:${v("measure")};color:${v("text")}` },
|
|
31
|
+
{ name: "wb-library-title", element: "h1", about: "The collection's or item's title", css: "margin:0 0 .5rem;font-size:1.75rem;line-height:1.2;font-weight:700" },
|
|
32
|
+
{ name: "wb-library-summary", element: "p", about: "The collection's summary", css: `margin:0 0 1rem;color:${v("muted")};line-height:1.5` },
|
|
33
|
+
{ name: "wb-library-count", element: "p", about: "\"3 of 8 done\"", css: `margin:0 0 .5rem;${quiet}` },
|
|
34
|
+
{ name: "wb-library-items", element: "ol", about: "The list of lessons, episodes or resources", css: `margin:0;padding:0;list-style:none;${box}` },
|
|
35
|
+
{
|
|
36
|
+
name: "wb-library-item",
|
|
37
|
+
element: "li",
|
|
38
|
+
about: "One lesson, episode or resource",
|
|
39
|
+
css: `display:flex;flex-wrap:wrap;align-items:baseline;gap:.25rem .75rem;padding:.75rem 1rem;border-top:1px solid ${v("line")}`,
|
|
40
|
+
states: [{ selector: ":first-child", about: "No divider above the first", css: "border-top:0" }],
|
|
41
|
+
},
|
|
42
|
+
{ name: "wb-library-item-locked", element: "li", about: "An item the viewer can't open yet (added to wb-library-item)", css: `color:${v("muted")}` },
|
|
43
|
+
{ name: "wb-library-item-done", element: "li", about: "A finished item (added to wb-library-item)", css: `border-left:3px solid ${v("good")}` },
|
|
44
|
+
{ name: "wb-library-item-link", element: "a", about: "An open item's title", css: `flex:1 1 auto;color:${v("text")};font-weight:600;text-decoration:none`, states: [{ selector: ":hover", about: "Hovered", css: `color:${v("accent")};text-decoration:underline` }] },
|
|
45
|
+
{ name: "wb-library-item-title", element: "span", about: "A locked item's title", css: "flex:1 1 auto" },
|
|
46
|
+
{ name: "wb-library-item-meta", element: "span", about: "Episode number, running time, \"Opens on\"", css: `${quiet};white-space:nowrap` },
|
|
47
|
+
{ name: "wb-library-badge", element: "span", about: "\"Free preview\"", css: `font-size:.75rem;padding:.1rem .45rem;border:1px solid ${v("accent")};border-radius:999px;color:${v("accent")}` },
|
|
48
|
+
{ name: "wb-library-badge-done", element: "span", about: "\"Done\" (added to wb-library-badge)", css: `border-color:${v("good")};color:${v("good")}` },
|
|
49
|
+
{ name: "wb-library-locked", element: "p", about: "Why something is locked", css: `margin:.75rem 0;padding:.6rem .8rem;${box};border-left:4px solid ${v("warn")}` },
|
|
50
|
+
{ name: "wb-library-locked-link", element: "a", about: "Sign in, Buy now or See the plans", css: `margin-left:.5rem;color:${v("accent")};font-weight:600` },
|
|
51
|
+
{ name: "wb-library-start", element: "form", about: "The Start the course form", css: "margin:.75rem 0" },
|
|
52
|
+
{ name: "wb-library-done", element: "form", about: "The Mark as done form", css: "margin:1.25rem 0" },
|
|
53
|
+
{
|
|
54
|
+
name: "wb-library-button",
|
|
55
|
+
element: "button",
|
|
56
|
+
about: "Start the course and Mark as done",
|
|
57
|
+
css: `font:inherit;font-weight:600;padding:.55rem 1rem;border:0;border-radius:${v("radius")};background:${v("accent")};color:#fff;cursor:pointer`,
|
|
58
|
+
states: [{ selector: ":focus-visible", about: "Keyboard focus", css: `outline:2px solid ${v("text")};outline-offset:2px` }],
|
|
59
|
+
},
|
|
60
|
+
{ name: "wb-library-page", element: "article", about: "An item's page", css: `max-width:${v("measure")};color:${v("text")}` },
|
|
61
|
+
{ name: "wb-library-crumb", element: "p", about: "The way back to the collection", css: `margin:0 0 .25rem;${quiet}` },
|
|
62
|
+
{ name: "wb-library-crumb-link", element: "a", about: "The collection's title", css: `color:${v("muted")}` },
|
|
63
|
+
{ name: "wb-library-player", element: "div", about: "The video, audio, picture or download", css: "margin:1rem 0;display:flex;flex-direction:column;gap:.5rem" },
|
|
64
|
+
{ name: "wb-library-embed", element: "iframe", about: "The YouTube or Vimeo player", css: `width:100%;aspect-ratio:16/9;border:0;border-radius:${v("radius")};background:#000` },
|
|
65
|
+
{ name: "wb-library-audio", element: "audio", about: "The audio player", css: "width:100%" },
|
|
66
|
+
{ name: "wb-library-image", element: "img", about: "A picture", css: `max-width:100%;height:auto;border-radius:${v("radius")}` },
|
|
67
|
+
{ name: "wb-library-download", element: "a", about: "The download link", css: `color:${v("accent")};font-size:.9rem` },
|
|
68
|
+
{ name: "wb-library-body", element: "div", about: "The words", css: "line-height:1.6" },
|
|
69
|
+
{ name: "wb-library-p", element: "p", about: "One paragraph", css: "margin:0 0 1rem" },
|
|
70
|
+
{ name: "wb-library-link", element: "a", about: "A web address in the words", css: `color:${v("accent")};overflow-wrap:anywhere` },
|
|
71
|
+
{ name: "wb-library-br", element: "br", about: "A line break in the words", css: "line-height:inherit" },
|
|
72
|
+
{ name: "wb-library-sponsor", element: "p", about: "A sponsor's line", css: `margin:1rem 0;padding:.6rem .8rem;${box}` },
|
|
73
|
+
{ name: "wb-library-sponsor-label", element: "span", about: "\"Sponsor\"", css: `${quiet};text-transform:uppercase;letter-spacing:.06em;margin-right:.25rem` },
|
|
74
|
+
{ name: "wb-library-sponsor-link", element: "a", about: "The sponsor's name, linked", css: `color:${v("accent")}` },
|
|
75
|
+
{ name: "wb-library-done-note", element: "p", about: "\"Done\" on a finished item", css: `margin:1.25rem 0;color:${v("good")};font-weight:600` },
|
|
76
|
+
{ name: "wb-library-nav", element: "nav", about: "Back and Next", css: `display:flex;justify-content:space-between;gap:1rem;margin-top:1.5rem;padding-top:1rem;border-top:1px solid ${v("line")}` },
|
|
77
|
+
{ name: "wb-library-nav-link", element: "a", about: "Back: … or Next: …", css: `color:${v("accent")}` },
|
|
78
|
+
{ name: "wb-library-mine", element: "section", about: "The member's courses and shows", css: `max-width:${v("measure")};padding:1rem;${box}` },
|
|
79
|
+
{ name: "wb-library-mine-title", element: "h2", about: "\"Your courses and shows\"", css: "margin:0 0 .75rem;font-size:1.125rem;font-weight:600" },
|
|
80
|
+
{ name: "wb-library-mine-list", element: "ul", about: "The list", css: "margin:0;padding:0;list-style:none" },
|
|
81
|
+
{ name: "wb-library-mine-item", element: "li", about: "One course or show", css: `display:flex;flex-wrap:wrap;justify-content:space-between;gap:.25rem 1rem;padding:.5rem 0;border-top:1px solid ${v("line")}` },
|
|
82
|
+
{ name: "wb-library-mine-link", element: "a", about: "Its title", css: `color:${v("text")};font-weight:600` },
|
|
83
|
+
{ name: "wb-library-mine-since", element: "span", about: "\"Started 3 Nov 2026\"", css: quiet },
|
|
84
|
+
{ name: "wb-library-empty", element: "p", about: "\"Nothing here yet.\"", css: `margin:0;color:${v("muted")}` },
|
|
85
|
+
],
|
|
86
|
+
};
|
package/dist/text.d.ts
ADDED
package/dist/text.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// An item's words: plain text, where a blank line starts a paragraph, a line
|
|
2
|
+
// break stays a line break, and web addresses become links. Everything else
|
|
3
|
+
// is escaped, so nothing the owner types can run in a visitor's browser.
|
|
4
|
+
export const esc = (s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
5
|
+
const URL_IN_TEXT = /\bhttps?:\/\/[^\s<>"']+[^\s<>"'.,;:!?)\]]/g;
|
|
6
|
+
const cls = (c) => (c ? ` class="${c}"` : "");
|
|
7
|
+
function linked(line, linkClass) {
|
|
8
|
+
let out = "";
|
|
9
|
+
let last = 0;
|
|
10
|
+
for (const m of line.matchAll(URL_IN_TEXT)) {
|
|
11
|
+
out += esc(line.slice(last, m.index));
|
|
12
|
+
out += `<a${cls(linkClass)} href="${esc(m[0])}" rel="noopener nofollow">${esc(m[0])}</a>`;
|
|
13
|
+
last = m.index + m[0].length;
|
|
14
|
+
}
|
|
15
|
+
return out + esc(line.slice(last));
|
|
16
|
+
}
|
|
17
|
+
/** The words as paragraphs, each with the given classes (none for the feed). */
|
|
18
|
+
export function paragraphs(body, pClass = "", linkClass = "", brClass = "") {
|
|
19
|
+
return body
|
|
20
|
+
.replace(/\r\n/g, "\n")
|
|
21
|
+
.split(/\n\s*\n/)
|
|
22
|
+
.map((p) => p.trim())
|
|
23
|
+
.filter(Boolean)
|
|
24
|
+
.map((p) => `<p${cls(pClass)}>${p.split("\n").map((l) => linked(l, linkClass)).join(`<br${cls(brClass)}>`)}</p>`)
|
|
25
|
+
.join("");
|
|
26
|
+
}
|
package/dist/time.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The day an instant falls on in the zone, YYYY-MM-DD. */
|
|
2
|
+
export declare function dayIn(at: Date | string, timeZone: string): string;
|
|
3
|
+
/** A real calendar day, YYYY-MM-DD. */
|
|
4
|
+
export declare function validDate(text: unknown): text is string;
|
|
5
|
+
/** Midnight in the zone, `days` calendar days after the day `start` fell on. */
|
|
6
|
+
export declare function opensAt(start: Date | string, days: number, timeZone: string): Date;
|
package/dist/time.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Calendar days in the project's time zone: when a dripped lesson opens
|
|
2
|
+
// (midnight, local time, the given number of days after the person started)
|
|
3
|
+
// and which day an episode came out on, for sponsor slots. The zone helpers
|
|
4
|
+
// are workbench-bookings' own.
|
|
5
|
+
function localParts(at, timeZone) {
|
|
6
|
+
const p = Object.fromEntries(new Intl.DateTimeFormat("en-CA", { timeZone, year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", hourCycle: "h23" })
|
|
7
|
+
.formatToParts(at)
|
|
8
|
+
.filter((x) => x.type !== "literal")
|
|
9
|
+
.map((x) => [x.type, Number(x.value)]));
|
|
10
|
+
return { year: p.year, month: p.month, day: p.day, hour: p.hour, minute: p.minute, second: p.second };
|
|
11
|
+
}
|
|
12
|
+
function offsetAt(at, timeZone) {
|
|
13
|
+
const p = localParts(at, timeZone);
|
|
14
|
+
return Date.UTC(p.year, p.month - 1, p.day, p.hour, p.minute, p.second) - Math.floor(at.getTime() / 1000) * 1000;
|
|
15
|
+
}
|
|
16
|
+
function fromLocal(wall, timeZone) {
|
|
17
|
+
const guess = wall - offsetAt(new Date(wall), timeZone);
|
|
18
|
+
return new Date(wall - offsetAt(new Date(guess), timeZone));
|
|
19
|
+
}
|
|
20
|
+
/** The day an instant falls on in the zone, YYYY-MM-DD. */
|
|
21
|
+
export function dayIn(at, timeZone) {
|
|
22
|
+
const p = localParts(new Date(at), timeZone);
|
|
23
|
+
return `${p.year}-${String(p.month).padStart(2, "0")}-${String(p.day).padStart(2, "0")}`;
|
|
24
|
+
}
|
|
25
|
+
/** A real calendar day, YYYY-MM-DD. */
|
|
26
|
+
export function validDate(text) {
|
|
27
|
+
if (typeof text !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(text))
|
|
28
|
+
return false;
|
|
29
|
+
const [y, m, d] = text.split("-").map(Number);
|
|
30
|
+
return new Date(Date.UTC(y, m - 1, d)).toISOString().slice(0, 10) === text;
|
|
31
|
+
}
|
|
32
|
+
/** Midnight in the zone, `days` calendar days after the day `start` fell on. */
|
|
33
|
+
export function opensAt(start, days, timeZone) {
|
|
34
|
+
const [y, m, d] = dayIn(start, timeZone).split("-").map(Number);
|
|
35
|
+
return fromLocal(Date.UTC(y, m - 1, d + days), timeZone);
|
|
36
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
export type CollectionKind = "course" | "show" | "shelf";
|
|
2
|
+
export type EmbedHost = "youtube" | "vimeo";
|
|
3
|
+
/** Who may see a collection's items (each item can also be a free preview). */
|
|
4
|
+
export type AccessRule = "everyone" | "signed_in" | "feature" | "bought";
|
|
5
|
+
export interface LibraryConfig {
|
|
6
|
+
format: 1;
|
|
7
|
+
/** Two-letter code: AU (the default) or PH. For dates and money. */
|
|
8
|
+
country: string;
|
|
9
|
+
/** For dates shown to visitors and for sponsor slots. From workbench/country.json. */
|
|
10
|
+
time_zone: string;
|
|
11
|
+
/** ISO currency of collections sold one by one: AUD, PHP. */
|
|
12
|
+
currency: string;
|
|
13
|
+
/** Which kinds the owner can make. */
|
|
14
|
+
kinds: readonly CollectionKind[];
|
|
15
|
+
files: {
|
|
16
|
+
/** The largest upload, in megabytes. 1 to 500. */
|
|
17
|
+
max_mb: number;
|
|
18
|
+
/** Content types allowed, such as audio/mpeg or application/pdf. No video: videos are links (Card 4). */
|
|
19
|
+
types: readonly string[];
|
|
20
|
+
};
|
|
21
|
+
/** Video hosts an item may embed. */
|
|
22
|
+
embeds: readonly EmbedHost[];
|
|
23
|
+
/** How long a signed link to a gated file lasts, in minutes. 5 to 1440. */
|
|
24
|
+
link_minutes: number;
|
|
25
|
+
paths: {
|
|
26
|
+
/** Where the app shows collections: <base>/<collection>/<item>. */
|
|
27
|
+
base: string;
|
|
28
|
+
/** Where the app's route calls serveFile: <files>/<file id>. */
|
|
29
|
+
files: string;
|
|
30
|
+
};
|
|
31
|
+
retention: {
|
|
32
|
+
/** Days an ended or revoked access row is kept before cleanup() deletes it. */
|
|
33
|
+
ended_access_days: number;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Who is looking. The app fills it from its sign-in and from workbench-plans. */
|
|
37
|
+
export interface Viewer {
|
|
38
|
+
/** The app's own id for the signed-in member, or null for a visitor. */
|
|
39
|
+
member: string | null;
|
|
40
|
+
/** The plan features the member has now (plans.can), for "feature" collections. */
|
|
41
|
+
features?: readonly string[];
|
|
42
|
+
/** The owner or staff: sees drafts, scheduled items and everything locked. */
|
|
43
|
+
owner?: boolean;
|
|
44
|
+
}
|
|
45
|
+
export interface ShowDetails {
|
|
46
|
+
/** The name the feed gives as the author. */
|
|
47
|
+
author: string | null;
|
|
48
|
+
/** An Apple Podcasts category, such as "Business" or "Education". */
|
|
49
|
+
category: string | null;
|
|
50
|
+
explicit: boolean;
|
|
51
|
+
/** For Apple Podcasts' ownership check. The owner's own details, if they give them. */
|
|
52
|
+
owner_name: string | null;
|
|
53
|
+
owner_email: string | null;
|
|
54
|
+
/** en, tl or another language code for the feed. */
|
|
55
|
+
language: string | null;
|
|
56
|
+
}
|
|
57
|
+
export interface Collection {
|
|
58
|
+
id: string;
|
|
59
|
+
slug: string;
|
|
60
|
+
kind: CollectionKind;
|
|
61
|
+
title: string;
|
|
62
|
+
summary: string | null;
|
|
63
|
+
/** A picture: the id of a file in library_files (the show's cover art). */
|
|
64
|
+
cover_file: string | null;
|
|
65
|
+
access: AccessRule;
|
|
66
|
+
/** The plan feature, when access is "feature". */
|
|
67
|
+
feature: string | null;
|
|
68
|
+
/** The price when access is "bought". */
|
|
69
|
+
price_cents: number | null;
|
|
70
|
+
/** How long a purchase lasts, in months, or null for good. */
|
|
71
|
+
access_months: number | null;
|
|
72
|
+
status: "draft" | "published";
|
|
73
|
+
show: ShowDetails | null;
|
|
74
|
+
created_at: string;
|
|
75
|
+
updated_at: string;
|
|
76
|
+
}
|
|
77
|
+
export type Media = {
|
|
78
|
+
kind: "embed";
|
|
79
|
+
url: string;
|
|
80
|
+
} | {
|
|
81
|
+
kind: "file";
|
|
82
|
+
file: string;
|
|
83
|
+
} | null;
|
|
84
|
+
export interface Item {
|
|
85
|
+
id: string;
|
|
86
|
+
collection: string;
|
|
87
|
+
slug: string;
|
|
88
|
+
position: number;
|
|
89
|
+
title: string;
|
|
90
|
+
/** Plain text: a blank line starts a paragraph; web addresses become links. */
|
|
91
|
+
body: string;
|
|
92
|
+
media: Media;
|
|
93
|
+
/** Running time in seconds, for the list and the feed. */
|
|
94
|
+
seconds: number | null;
|
|
95
|
+
/** Open to everyone, even in a gated collection. */
|
|
96
|
+
preview: boolean;
|
|
97
|
+
/** Opens this many days after the person started the course. */
|
|
98
|
+
drip_days: number | null;
|
|
99
|
+
/** Appears at this time. null: as soon as it's published. */
|
|
100
|
+
publish_at: string | null;
|
|
101
|
+
status: "draft" | "published";
|
|
102
|
+
episode: number | null;
|
|
103
|
+
season: number | null;
|
|
104
|
+
created_at: string;
|
|
105
|
+
updated_at: string;
|
|
106
|
+
}
|
|
107
|
+
export interface LibraryFile {
|
|
108
|
+
id: string;
|
|
109
|
+
/** The key in the app's R2 bucket. */
|
|
110
|
+
key: string;
|
|
111
|
+
name: string;
|
|
112
|
+
type: string;
|
|
113
|
+
bytes: number;
|
|
114
|
+
created_at: string;
|
|
115
|
+
}
|
|
116
|
+
export interface Sponsor {
|
|
117
|
+
id: string;
|
|
118
|
+
collection: string;
|
|
119
|
+
name: string;
|
|
120
|
+
url: string | null;
|
|
121
|
+
/** The line read or shown: "Brewed by Bean & Leaf, Sydney's best beans." */
|
|
122
|
+
message: string;
|
|
123
|
+
/** YYYY-MM-DD in the project's time zone, both days included. */
|
|
124
|
+
starts_on: string;
|
|
125
|
+
ends_on: string;
|
|
126
|
+
}
|
|
127
|
+
/** Why an item can't be opened. Every one has words in the UI. */
|
|
128
|
+
export type LockReason = "sign_in" | "plan" | "buy" | "not_started" | "opens_later";
|
|
129
|
+
export interface Locked {
|
|
130
|
+
ok: false;
|
|
131
|
+
reason: LockReason;
|
|
132
|
+
/** For opens_later: when it opens. */
|
|
133
|
+
opens_at?: string;
|
|
134
|
+
/** For plan: the feature needed. */
|
|
135
|
+
feature?: string;
|
|
136
|
+
/** For buy. */
|
|
137
|
+
price_cents?: number;
|
|
138
|
+
currency?: string;
|
|
139
|
+
}
|
|
140
|
+
/** One item in a collection's list, as this viewer sees it. */
|
|
141
|
+
export interface ItemEntry {
|
|
142
|
+
item: Item;
|
|
143
|
+
/** open: they can open it. locked: shown, with why. */
|
|
144
|
+
state: "open" | "locked";
|
|
145
|
+
locked: Locked | null;
|
|
146
|
+
/** The app says progress has it as done. */
|
|
147
|
+
done: boolean;
|
|
148
|
+
}
|
|
149
|
+
export interface CollectionView {
|
|
150
|
+
collection: Collection;
|
|
151
|
+
items: ItemEntry[];
|
|
152
|
+
/** When this member started or bought it, or null. */
|
|
153
|
+
started_at: string | null;
|
|
154
|
+
/** The viewer may see the collection's items (previews aside). */
|
|
155
|
+
allowed: boolean;
|
|
156
|
+
/** Why not, when not. */
|
|
157
|
+
locked: Locked | null;
|
|
158
|
+
}
|
|
159
|
+
/** An item opened: its media resolved, ready to draw. */
|
|
160
|
+
export interface OpenItem {
|
|
161
|
+
ok: true;
|
|
162
|
+
collection: Collection;
|
|
163
|
+
item: Item;
|
|
164
|
+
/** embed: the player's address. file: a link (signed when gated), the file's name, type and size. */
|
|
165
|
+
media: {
|
|
166
|
+
kind: "embed";
|
|
167
|
+
src: string;
|
|
168
|
+
host: EmbedHost;
|
|
169
|
+
} | {
|
|
170
|
+
kind: "file";
|
|
171
|
+
url: string;
|
|
172
|
+
name: string;
|
|
173
|
+
type: string;
|
|
174
|
+
bytes: number;
|
|
175
|
+
} | null;
|
|
176
|
+
/** Sponsor lines for an episode published inside a slot. */
|
|
177
|
+
sponsors: Sponsor[];
|
|
178
|
+
previous: Item | null;
|
|
179
|
+
next: Item | null;
|
|
180
|
+
done: boolean;
|
|
181
|
+
}
|
|
182
|
+
export type Opened = OpenItem | (Locked & {
|
|
183
|
+
collection: Collection;
|
|
184
|
+
item: Item;
|
|
185
|
+
});
|
|
186
|
+
/** What progress' mark() takes: the library keeps no ticks of its own. */
|
|
187
|
+
export interface ProgressMark {
|
|
188
|
+
track: string;
|
|
189
|
+
skill: string;
|
|
190
|
+
person: {
|
|
191
|
+
type: "member";
|
|
192
|
+
id: string;
|
|
193
|
+
};
|
|
194
|
+
source: {
|
|
195
|
+
type: "library_item";
|
|
196
|
+
id: string;
|
|
197
|
+
};
|
|
198
|
+
at: string;
|
|
199
|
+
}
|
|
200
|
+
/** One line of the member area (workbench-ui's "My account" page lists every tool's). */
|
|
201
|
+
export interface MineEntry {
|
|
202
|
+
tool: "library";
|
|
203
|
+
kind: CollectionKind;
|
|
204
|
+
id: string;
|
|
205
|
+
title: string;
|
|
206
|
+
href: string;
|
|
207
|
+
/** When they started or bought it. */
|
|
208
|
+
started_at: string;
|
|
209
|
+
/** When bought access ends, or null for good (or not bought). */
|
|
210
|
+
ends_at: string | null;
|
|
211
|
+
bought: boolean;
|
|
212
|
+
}
|
|
213
|
+
export interface Purchase {
|
|
214
|
+
source: {
|
|
215
|
+
type: "library";
|
|
216
|
+
id: string;
|
|
217
|
+
};
|
|
218
|
+
currency: string;
|
|
219
|
+
line: {
|
|
220
|
+
label: string;
|
|
221
|
+
quantity: 1;
|
|
222
|
+
unit_cents: number;
|
|
223
|
+
total_cents: number;
|
|
224
|
+
};
|
|
225
|
+
/** How long the access lasts, for grant(). */
|
|
226
|
+
access_months: number | null;
|
|
227
|
+
}
|
|
228
|
+
export interface Opening {
|
|
229
|
+
member: string;
|
|
230
|
+
collection: string;
|
|
231
|
+
item: string;
|
|
232
|
+
at: string;
|
|
233
|
+
}
|
|
234
|
+
export interface TickResult {
|
|
235
|
+
/** Drip lessons that opened for a member since the last tick. */
|
|
236
|
+
opened: Opening[];
|
|
237
|
+
/** Scheduled items that went live since the last tick. */
|
|
238
|
+
published: {
|
|
239
|
+
collection: string;
|
|
240
|
+
item: string;
|
|
241
|
+
at: string;
|
|
242
|
+
}[];
|
|
243
|
+
/** Access rows cleanup() deleted. */
|
|
244
|
+
deleted: number;
|
|
245
|
+
}
|
package/dist/types.js
ADDED
package/dist/ui.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CollectionView, LibraryConfig, Locked, MineEntry, OpenItem } from "./types.ts";
|
|
2
|
+
export { LIBRARY_WORDS, LIBRARY_WORD_KEYS, type LibraryWordKey } from "./words.ts";
|
|
3
|
+
/** The project's strings lookup: `t('ui.wblibrary.done')`. Undefined falls back to the English. */
|
|
4
|
+
export type Words = (key: string, params?: Readonly<Record<string, string | number>>) => string | undefined;
|
|
5
|
+
export interface ViewOptions {
|
|
6
|
+
/** The project's strings lookup; English when left out. */
|
|
7
|
+
t?: Words;
|
|
8
|
+
/** en, tl or taglish: for dates and money. */
|
|
9
|
+
lang?: string;
|
|
10
|
+
}
|
|
11
|
+
/** Where a locked notice's button goes. Left out: no button. */
|
|
12
|
+
export interface LockedActions {
|
|
13
|
+
sign_in?: string;
|
|
14
|
+
buy?: string;
|
|
15
|
+
plan?: string;
|
|
16
|
+
}
|
|
17
|
+
type Config = Pick<LibraryConfig, "country" | "time_zone" | "currency" | "paths">;
|
|
18
|
+
/** Cents as money, always with 2 decimal places: $49.00, ₱1,500.00. */
|
|
19
|
+
export declare function formatMoney(cents: number, currency: string, country?: string, lang?: string): string;
|
|
20
|
+
/** "3 Dec 2026" in the project's time zone. */
|
|
21
|
+
export declare function formatDate(iso: string, config: Pick<LibraryConfig, "time_zone" | "country">, lang?: string): string;
|
|
22
|
+
/** 12.5 MB, 820 KB. */
|
|
23
|
+
export declare function formatSize(bytes: number): string;
|
|
24
|
+
/** Why something is locked, with a button when the app gives one. */
|
|
25
|
+
export declare function renderLocked(l: Locked, config: Config, o?: ViewOptions & {
|
|
26
|
+
actions?: LockedActions;
|
|
27
|
+
}): string;
|
|
28
|
+
export interface CollectionPageOptions extends ViewOptions {
|
|
29
|
+
/** Where the Start button posts (the app's route calls enrol()). Shown on a course the member may see but hasn't started. */
|
|
30
|
+
startAction?: string;
|
|
31
|
+
actions?: LockedActions;
|
|
32
|
+
}
|
|
33
|
+
/** A course, show or shelf: its title and summary, then its items, each a link when open. */
|
|
34
|
+
export declare function renderCollection(v: CollectionView, config: Config, o?: CollectionPageOptions): string;
|
|
35
|
+
export interface ItemPageOptions extends ViewOptions {
|
|
36
|
+
/** Where the Done button posts (the app's route calls finished() and progress' mark()). Left out: no button. */
|
|
37
|
+
doneAction?: string;
|
|
38
|
+
}
|
|
39
|
+
/** One lesson, episode or resource. */
|
|
40
|
+
export declare function renderItem(it: OpenItem, config: Config, o?: ItemPageOptions): string;
|
|
41
|
+
/** The member's courses and shows, for the "My account" page. */
|
|
42
|
+
export declare function renderMine(list: readonly MineEntry[], config: Config, o?: ViewOptions): string;
|
package/dist/ui.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// '@mccaskie-labs/workbench-library/ui': the collection page (the list of
|
|
2
|
+
// lessons or episodes with ticks, previews and "Opens on"), the item page
|
|
3
|
+
// (the player, the words, the sponsor line, Done, Next and Back), the locked
|
|
4
|
+
// notice and the member's list. Plain HTML strings with a `wb-library-*`
|
|
5
|
+
// class on every element (LIBRARY_STYLES), no script. Forms post to the
|
|
6
|
+
// app's own routes, which call enrol() and finished().
|
|
7
|
+
import { esc, paragraphs } from "./text.js";
|
|
8
|
+
import { LIBRARY_WORDS } from "./words.js";
|
|
9
|
+
export { LIBRARY_WORDS, LIBRARY_WORD_KEYS } from "./words.js";
|
|
10
|
+
function fill(text, params = {}) {
|
|
11
|
+
return text.replace(/\{(\w+)\}/g, (m, k) => (Object.hasOwn(params, k) ? String(params[k]) : m));
|
|
12
|
+
}
|
|
13
|
+
function word(t, key, params = {}) {
|
|
14
|
+
return t?.(`ui.wblibrary.${key}`, params) ?? fill(LIBRARY_WORDS.en[key], params);
|
|
15
|
+
}
|
|
16
|
+
function locale(country, lang = "en") {
|
|
17
|
+
if (lang === "tl")
|
|
18
|
+
return "fil-PH";
|
|
19
|
+
return country === "PH" ? "en-PH" : country === "AU" ? "en-AU" : "en-GB";
|
|
20
|
+
}
|
|
21
|
+
/** Cents as money, always with 2 decimal places: $49.00, ₱1,500.00. */
|
|
22
|
+
export function formatMoney(cents, currency, country = "AU", lang = "en") {
|
|
23
|
+
return new Intl.NumberFormat(locale(country, lang), { style: "currency", currency, minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(cents / 100);
|
|
24
|
+
}
|
|
25
|
+
/** "3 Dec 2026" in the project's time zone. */
|
|
26
|
+
export function formatDate(iso, config, lang = "en") {
|
|
27
|
+
return new Intl.DateTimeFormat(locale(config.country, lang), { day: "numeric", month: "short", year: "numeric", timeZone: config.time_zone }).format(new Date(iso));
|
|
28
|
+
}
|
|
29
|
+
/** 12.5 MB, 820 KB. */
|
|
30
|
+
export function formatSize(bytes) {
|
|
31
|
+
if (bytes >= 1024 * 1024)
|
|
32
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
33
|
+
return `${Math.max(1, Math.round(bytes / 1024))} KB`;
|
|
34
|
+
}
|
|
35
|
+
/** Why something is locked, with a button when the app gives one. */
|
|
36
|
+
export function renderLocked(l, config, o = {}) {
|
|
37
|
+
let text;
|
|
38
|
+
let action = null;
|
|
39
|
+
if (l.reason === "buy") {
|
|
40
|
+
text = l.price_cents !== undefined ? word(o.t, "buy", { price: formatMoney(l.price_cents, l.currency ?? config.currency, config.country, o.lang) }) : word(o.t, "buy_no_price");
|
|
41
|
+
if (o.actions?.buy)
|
|
42
|
+
action = { href: o.actions.buy, label: word(o.t, "action_buy") };
|
|
43
|
+
}
|
|
44
|
+
else if (l.reason === "opens_later")
|
|
45
|
+
text = word(o.t, "opens_later", { date: formatDate(l.opens_at, config, o.lang) });
|
|
46
|
+
else {
|
|
47
|
+
text = word(o.t, l.reason);
|
|
48
|
+
if (l.reason === "sign_in" && o.actions?.sign_in)
|
|
49
|
+
action = { href: o.actions.sign_in, label: word(o.t, "action_sign_in") };
|
|
50
|
+
if (l.reason === "plan" && o.actions?.plan)
|
|
51
|
+
action = { href: o.actions.plan, label: word(o.t, "action_plan") };
|
|
52
|
+
}
|
|
53
|
+
const button = action ? ` <a class="wb-library-locked-link" href="${esc(action.href)}">${esc(action.label)}</a>` : "";
|
|
54
|
+
return `<p class="wb-library-locked" role="status">${esc(text)}${button}</p>`;
|
|
55
|
+
}
|
|
56
|
+
function meta(e, config, o) {
|
|
57
|
+
const bits = [];
|
|
58
|
+
if (e.item.episode !== null)
|
|
59
|
+
bits.push(word(o.t, "episode", { n: e.item.episode }));
|
|
60
|
+
if (e.item.seconds)
|
|
61
|
+
bits.push(word(o.t, "minutes", { minutes: Math.max(1, Math.round(e.item.seconds / 60)) }));
|
|
62
|
+
if (e.locked?.reason === "opens_later")
|
|
63
|
+
bits.push(word(o.t, "opens_on", { date: formatDate(e.locked.opens_at, config, o.lang) }));
|
|
64
|
+
const badge = e.done
|
|
65
|
+
? `<span class="wb-library-badge wb-library-badge-done">${esc(word(o.t, "done"))}</span>`
|
|
66
|
+
: e.item.preview
|
|
67
|
+
? `<span class="wb-library-badge">${esc(word(o.t, "preview"))}</span>`
|
|
68
|
+
: "";
|
|
69
|
+
return `${bits.length ? `<span class="wb-library-item-meta">${esc(bits.join(" · "))}</span>` : ""}${badge}`;
|
|
70
|
+
}
|
|
71
|
+
/** A course, show or shelf: its title and summary, then its items, each a link when open. */
|
|
72
|
+
export function renderCollection(v, config, o = {}) {
|
|
73
|
+
const c = v.collection;
|
|
74
|
+
const base = `${config.paths.base}/${c.slug}`;
|
|
75
|
+
const done = v.items.filter((e) => e.done).length;
|
|
76
|
+
const count = c.kind === "course" && v.items.length ? `<p class="wb-library-count">${esc(word(o.t, "items_done", { done, total: v.items.length }))}</p>` : "";
|
|
77
|
+
const lock = v.locked ? renderLocked(v.locked, config, o) : "";
|
|
78
|
+
const drips = v.items.some((e) => e.locked?.reason === "not_started");
|
|
79
|
+
const start = c.kind === "course" && v.allowed && v.started_at === null && drips && o.startAction
|
|
80
|
+
? `<form class="wb-library-start" method="post" action="${esc(o.startAction)}"><button class="wb-library-button" type="submit">${esc(word(o.t, "start"))}</button></form>`
|
|
81
|
+
: "";
|
|
82
|
+
const rows = v.items
|
|
83
|
+
.map((e) => {
|
|
84
|
+
const cls = `wb-library-item${e.state === "locked" ? " wb-library-item-locked" : ""}${e.done ? " wb-library-item-done" : ""}`;
|
|
85
|
+
const title = e.state === "open"
|
|
86
|
+
? `<a class="wb-library-item-link" href="${esc(`${base}/${e.item.slug}`)}">${esc(e.item.title)}</a>`
|
|
87
|
+
: `<span class="wb-library-item-title">${esc(e.item.title)}</span>`;
|
|
88
|
+
return `<li class="${cls}">${title}${meta(e, config, o)}</li>`;
|
|
89
|
+
})
|
|
90
|
+
.join("");
|
|
91
|
+
const summary = c.summary ? `<p class="wb-library-summary">${esc(c.summary)}</p>` : "";
|
|
92
|
+
return `<section class="wb-library-collection"><h1 class="wb-library-title">${esc(c.title)}</h1>${summary}${lock}${start}${count}<ol class="wb-library-items">${rows}</ol></section>`;
|
|
93
|
+
}
|
|
94
|
+
function player(it, o) {
|
|
95
|
+
const m = it.media;
|
|
96
|
+
if (!m)
|
|
97
|
+
return "";
|
|
98
|
+
if (m.kind === "embed") {
|
|
99
|
+
return `<div class="wb-library-player"><iframe class="wb-library-embed" src="${esc(m.src)}" title="${esc(word(o.t, "embed_title", { title: it.item.title }))}" loading="lazy" allow="autoplay; encrypted-media; fullscreen; picture-in-picture" allowfullscreen referrerpolicy="strict-origin-when-cross-origin"></iframe></div>`;
|
|
100
|
+
}
|
|
101
|
+
const download = `<a class="wb-library-download" href="${esc(m.url)}" download>${esc(word(o.t, "download", { name: m.name, size: formatSize(m.bytes) }))}</a>`;
|
|
102
|
+
if (m.type.startsWith("audio/")) {
|
|
103
|
+
return `<div class="wb-library-player"><audio class="wb-library-audio" controls preload="metadata" src="${esc(m.url)}">${esc(word(o.t, "audio_fallback"))}</audio>${download}</div>`;
|
|
104
|
+
}
|
|
105
|
+
if (m.type.startsWith("image/"))
|
|
106
|
+
return `<div class="wb-library-player"><img class="wb-library-image" src="${esc(m.url)}" alt="${esc(it.item.title)}">${download}</div>`;
|
|
107
|
+
return `<div class="wb-library-player">${download}</div>`;
|
|
108
|
+
}
|
|
109
|
+
/** One lesson, episode or resource. */
|
|
110
|
+
export function renderItem(it, config, o = {}) {
|
|
111
|
+
const c = it.collection;
|
|
112
|
+
const base = `${config.paths.base}/${c.slug}`;
|
|
113
|
+
const sponsors = it.sponsors
|
|
114
|
+
.map((s) => `<p class="wb-library-sponsor"><span class="wb-library-sponsor-label">${esc(word(o.t, "sponsor_label"))}</span> ${esc(s.message)}${s.url ? ` <a class="wb-library-sponsor-link" href="${esc(s.url)}" rel="noopener sponsored">${esc(s.name)}</a>` : ""}</p>`)
|
|
115
|
+
.join("");
|
|
116
|
+
const done = it.done
|
|
117
|
+
? `<p class="wb-library-done-note" role="status">${esc(word(o.t, "done"))}</p>`
|
|
118
|
+
: o.doneAction
|
|
119
|
+
? `<form class="wb-library-done" method="post" action="${esc(o.doneAction)}"><button class="wb-library-button" type="submit">${esc(word(o.t, "mark_done"))}</button></form>`
|
|
120
|
+
: "";
|
|
121
|
+
const links = [
|
|
122
|
+
it.previous ? `<a class="wb-library-nav-link" href="${esc(`${base}/${it.previous.slug}`)}" rel="prev">${esc(word(o.t, "previous", { title: it.previous.title }))}</a>` : "",
|
|
123
|
+
it.next ? `<a class="wb-library-nav-link" href="${esc(`${base}/${it.next.slug}`)}" rel="next">${esc(word(o.t, "next", { title: it.next.title }))}</a>` : "",
|
|
124
|
+
].join("");
|
|
125
|
+
const nav = links ? `<nav class="wb-library-nav">${links}</nav>` : "";
|
|
126
|
+
const body = it.item.body.trim() ? `<div class="wb-library-body">${paragraphs(it.item.body, "wb-library-p", "wb-library-link", "wb-library-br")}</div>` : "";
|
|
127
|
+
return (`<article class="wb-library-page">` +
|
|
128
|
+
`<p class="wb-library-crumb"><a class="wb-library-crumb-link" href="${esc(base)}">${esc(c.title)}</a></p>` +
|
|
129
|
+
`<h1 class="wb-library-title">${esc(it.item.title)}</h1>` +
|
|
130
|
+
player(it, o) +
|
|
131
|
+
body +
|
|
132
|
+
sponsors +
|
|
133
|
+
done +
|
|
134
|
+
nav +
|
|
135
|
+
`</article>`);
|
|
136
|
+
}
|
|
137
|
+
/** The member's courses and shows, for the "My account" page. */
|
|
138
|
+
export function renderMine(list, config, o = {}) {
|
|
139
|
+
const d = (v) => formatDate(v, config, o.lang);
|
|
140
|
+
const rows = list
|
|
141
|
+
.map((m) => {
|
|
142
|
+
const since = m.bought ? (m.ends_at ? word(o.t, "bought_until", { date: d(m.started_at), until: d(m.ends_at) }) : word(o.t, "bought", { date: d(m.started_at) })) : word(o.t, "started", { date: d(m.started_at) });
|
|
143
|
+
return `<li class="wb-library-mine-item"><a class="wb-library-mine-link" href="${esc(m.href)}">${esc(m.title)}</a> <span class="wb-library-mine-since">${esc(since)}</span></li>`;
|
|
144
|
+
})
|
|
145
|
+
.join("");
|
|
146
|
+
const inner = rows ? `<ul class="wb-library-mine-list">${rows}</ul>` : `<p class="wb-library-empty">${esc(word(o.t, "mine_empty"))}</p>`;
|
|
147
|
+
return `<section class="wb-library-mine"><h2 class="wb-library-mine-title">${esc(word(o.t, "mine_title"))}</h2>${inner}</section>`;
|
|
148
|
+
}
|
package/dist/words.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const LIBRARY_WORD_KEYS: readonly ["items_done", "preview", "opens_on", "minutes", "episode", "start", "mark_done", "done", "next", "previous", "download", "audio_fallback", "embed_title", "sponsor_label", "sign_in", "plan", "buy", "buy_no_price", "not_started", "opens_later", "action_sign_in", "action_buy", "action_plan", "mine_title", "mine_empty", "started", "bought", "bought_until"];
|
|
2
|
+
export type LibraryWordKey = (typeof LIBRARY_WORD_KEYS)[number];
|
|
3
|
+
/** The words by language, as `ui.wblibrary.<key>` would hold them. */
|
|
4
|
+
export declare const LIBRARY_WORDS: Readonly<Record<"en" | "tl" | "taglish", Readonly<Record<LibraryWordKey, string>>>>;
|