@pseudolux-ui/media-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/src/rows.ts ADDED
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Pictures laid out in rows at their own shape: nothing is cropped to fit a
3
+ * tile, so what is in the library is what is on the screen.
4
+ *
5
+ * Worked out from width and height alone, which arrive with the listing, so
6
+ * every row has its place before a single image has loaded.
7
+ */
8
+
9
+ import { startOf } from "./sentence.js"
10
+
11
+ export type Size = "S" | "M" | "L"
12
+
13
+ export const TARGET_HEIGHT: Record<Size, number> = { S: 160, M: 230, L: 320 }
14
+
15
+ export const GAP = 6
16
+
17
+ /** A banner ten times wider than tall would take a row to itself at a sliver
18
+ * of the height, and a tall strip would be a line. Held to shapes a row can
19
+ * carry; the picture is still drawn whole inside its tile. */
20
+ const WIDEST = 3.2
21
+ const NARROWEST = 0.45
22
+
23
+ export const aspectOf = (item: { width?: number; height?: number }): number => {
24
+ if (!item.width || !item.height) return 1.5
25
+ return Math.min(WIDEST, Math.max(NARROWEST, item.width / item.height))
26
+ }
27
+
28
+ export interface Row<T> {
29
+ items: T[]
30
+ height: number
31
+ /** The last row keeps the target height rather than stretching to fill. */
32
+ last: boolean
33
+ }
34
+
35
+ /**
36
+ * Pictures are added to a row until it would be at least as wide as the
37
+ * space at the target height, then the row is scaled to fit exactly.
38
+ */
39
+ export const justify = <T extends { aspect: number }>(items: T[], width: number, target: number, gap = GAP): Row<T>[] => {
40
+ const rows: Row<T>[] = []
41
+ if (width <= 0) return rows
42
+ let current: T[] = []
43
+ let sum = 0
44
+ for (const item of items) {
45
+ current.push(item)
46
+ sum += item.aspect
47
+ const gaps = gap * (current.length - 1)
48
+ if (sum * target + gaps >= width) {
49
+ rows.push({ items: current, height: Math.round((width - gaps) / sum), last: false })
50
+ current = []
51
+ sum = 0
52
+ }
53
+ }
54
+ if (current.length) {
55
+ const gaps = gap * (current.length - 1)
56
+ // A last row wider than the space (one very wide picture on a narrow
57
+ // screen) is scaled down like any other.
58
+ const height = Math.min(target, Math.round((width - gaps) / sum))
59
+ rows.push({ items: current, height, last: true })
60
+ }
61
+ return rows
62
+ }
63
+
64
+ export interface Section<T> {
65
+ key: string
66
+ label: string
67
+ items: T[]
68
+ }
69
+
70
+ /**
71
+ * Pictures under the time they were added: this week, earlier this month,
72
+ * earlier this year, then a heading per year. The order given is kept, so a
73
+ * listing that is newest first reads top to bottom.
74
+ */
75
+ export const byDate = <T extends { createdAt?: string }>(items: T[], now: Date): Section<T>[] => {
76
+ const week = startOf("week", now)
77
+ const month = startOf("month", now)
78
+ const year = startOf("year", now)
79
+ const sections = new Map<string, Section<T>>()
80
+ for (const item of items) {
81
+ const added = item.createdAt ? Date.parse(item.createdAt) : NaN
82
+ const [key, label] = Number.isNaN(added)
83
+ ? ["undated", "No date"]
84
+ : added >= week
85
+ ? ["week", "This week"]
86
+ : added >= month
87
+ ? ["month", "Earlier this month"]
88
+ : added >= year
89
+ ? ["year", "Earlier this year"]
90
+ : [String(new Date(added).getFullYear()), String(new Date(added).getFullYear())]
91
+ const section = sections.get(key) ?? { key, label, items: [] }
92
+ section.items.push(item)
93
+ sections.set(key, section)
94
+ }
95
+ return [...sections.values()]
96
+ }
97
+
98
+ export interface Placed {
99
+ id: string
100
+ row: number
101
+ /** The horizontal middle of the tile, in pixels from the left. */
102
+ middle: number
103
+ }
104
+
105
+ /** Where each tile sits, over every row on the page in order. */
106
+ export const place = <T extends { id: string; aspect: number }>(rows: Row<T>[], gap = GAP): Placed[] => {
107
+ const placed: Placed[] = []
108
+ rows.forEach((row, index) => {
109
+ let x = 0
110
+ for (const item of row.items) {
111
+ const width = item.aspect * row.height
112
+ placed.push({ id: item.id, row: index, middle: x + width / 2 })
113
+ x += width + gap
114
+ }
115
+ })
116
+ return placed
117
+ }
118
+
119
+ export type Move = "left" | "right" | "up" | "down" | "home" | "end"
120
+
121
+ /**
122
+ * The tile an arrow key lands on. Left and right follow reading order across
123
+ * rows; up and down go to whichever tile in the next row sits most nearly
124
+ * above or below, since rows do not share columns.
125
+ */
126
+ export const neighbour = (placed: Placed[], id: string, move: Move): string => {
127
+ const at = placed.findIndex((one) => one.id === id)
128
+ const first = placed[0]
129
+ const from = placed[at]
130
+ if (!first) return ""
131
+ if (!from) return first.id
132
+ const idAt = (index: number): string => placed[Math.max(0, Math.min(placed.length - 1, index))]?.id ?? id
133
+ if (move === "home") return first.id
134
+ if (move === "end") return idAt(placed.length - 1)
135
+ if (move === "left") return idAt(at - 1)
136
+ if (move === "right") return idAt(at + 1)
137
+
138
+ const target = from.row + (move === "down" ? 1 : -1)
139
+ const candidates = placed.filter((one) => one.row === target)
140
+ if (!candidates.length) return id
141
+ return candidates.reduce((best, one) => (Math.abs(one.middle - from.middle) < Math.abs(best.middle - from.middle) ? one : best)).id
142
+ }
@@ -0,0 +1,164 @@
1
+ /**
2
+ * The sentence the library is read through: "Portrait pictures of the dome in
3
+ * Events that need words, added this month." Each part is one narrowing, and
4
+ * a part left alone reads as "any".
5
+ *
6
+ * Kept apart from the component that draws it so typed text, the option rows
7
+ * and the rows of pictures all agree on what a sentence means.
8
+ */
9
+
10
+ export type Shape = "" | "landscape" | "portrait" | "square"
11
+ export type Condition = "" | "needs" | "unused" | "used" | "flagged"
12
+ export type When = "" | "week" | "month" | "year"
13
+
14
+ export interface Sentence {
15
+ shape: Shape
16
+ /** Free text: names, alt text, labels. */
17
+ about: string
18
+ /** One group, or "" for any the person can see. */
19
+ group: string
20
+ condition: Condition
21
+ when: When
22
+ }
23
+
24
+ export const BLANK: Sentence = { shape: "", about: "", group: "", condition: "", when: "" }
25
+
26
+ export const SHAPES: Array<[Shape, string]> = [
27
+ ["", "Any shape"],
28
+ ["landscape", "Landscape"],
29
+ ["portrait", "Portrait"],
30
+ ["square", "Square"],
31
+ ]
32
+
33
+ export const CONDITIONS: Array<[Condition, string]> = [
34
+ ["", "are in any state"],
35
+ ["needs", "need words"],
36
+ ["unused", "aren't used anywhere"],
37
+ ["used", "are used on pages"],
38
+ ["flagged", "have quality flags"],
39
+ ]
40
+
41
+ export const WHENS: Array<[When, string]> = [
42
+ ["", "any time"],
43
+ ["week", "this week"],
44
+ ["month", "this month"],
45
+ ["year", "this year"],
46
+ ]
47
+
48
+ export const isSet = (sentence: Sentence): boolean =>
49
+ Boolean(sentence.shape || sentence.about || sentence.group || sentence.condition || sentence.when)
50
+
51
+ export const sameSentence = (a: Sentence, b: Sentence): boolean =>
52
+ a.shape === b.shape && a.about === b.about && a.group === b.group && a.condition === b.condition && a.when === b.when
53
+
54
+ /** A picture within 5% of square is square: nobody calls 1000 × 1010 a portrait. */
55
+ export const shapeOf = (width: number | undefined, height: number | undefined): Shape => {
56
+ if (!width || !height) return ""
57
+ const aspect = width / height
58
+ return aspect > 1.05 ? "landscape" : aspect < 0.95 ? "portrait" : "square"
59
+ }
60
+
61
+ /** When "this week", "this month" or "this year" began, local time. Weeks start on Monday. */
62
+ export const startOf = (when: When, now: Date): number => {
63
+ if (when === "year") return new Date(now.getFullYear(), 0, 1).getTime()
64
+ if (when === "month") return new Date(now.getFullYear(), now.getMonth(), 1).getTime()
65
+ if (when === "week") {
66
+ const sinceMonday = (now.getDay() + 6) % 7
67
+ return new Date(now.getFullYear(), now.getMonth(), now.getDate() - sinceMonday).getTime()
68
+ }
69
+ return 0
70
+ }
71
+
72
+ const FILLER = /\b(pictures?|photos?|images?|of|in|from|that|the|added|with|and|show|me|all|are|is)\b/g
73
+
74
+ const escaped = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
75
+
76
+ /**
77
+ * What somebody typed, as a sentence: "portraits in events without alt text"
78
+ * sets three parts, and whatever is left over is what the pictures are of.
79
+ * `usage` says whether "unused" means anything here; where it does not, the
80
+ * word stays in the search.
81
+ */
82
+ export const parse = (text: string, options: { groups?: string[]; usage?: boolean } = {}): Sentence => {
83
+ let rest = " " + text.toLowerCase() + " "
84
+ const out: Sentence = { ...BLANK }
85
+ const take = (pattern: RegExp, found: (match: RegExpMatchArray) => void) => {
86
+ const match = rest.match(pattern)
87
+ if (!match) return
88
+ found(match)
89
+ rest = rest.replace(pattern, " ")
90
+ }
91
+
92
+ // Longest first, so "events archive" is not read as "events".
93
+ const groups = (options.groups ?? []).slice().sort((a, b) => b.length - a.length)
94
+ for (const group of groups) {
95
+ if (out.group) break
96
+ take(new RegExp("(?<![\\w-])" + escaped(group.toLowerCase()) + "(?![\\w-])"), () => (out.group = group))
97
+ }
98
+ take(/\bportraits?\b/, () => (out.shape = "portrait"))
99
+ take(/\blandscapes?\b/, () => (out.shape = "landscape"))
100
+ take(/\bsquares?\b/, () => (out.shape = "square"))
101
+ take(/\b(without|no|missing) alt( text)?\b|\bneeds? words\b|\bneeds? alt( text)?\b|\bundescribed\b/, () => (out.condition = "needs"))
102
+ if (options.usage) {
103
+ take(/\b(unused|not used( anywhere)?)\b/, () => (out.condition = "unused"))
104
+ }
105
+ take(/\b(too small|low quality|quality flags?|flagged)\b/, () => (out.condition = "flagged"))
106
+ take(/\bthis (week|month|year)\b/, (match) => (out.when = (match[1] ?? "") as When))
107
+ out.about = rest.replace(FILLER, " ").replace(/\s+/g, " ").trim()
108
+ return out
109
+ }
110
+
111
+ export interface Judged {
112
+ width?: number
113
+ height?: number
114
+ createdAt?: string
115
+ /** The alt text as the current view reads it. */
116
+ altText?: string
117
+ filename?: string
118
+ id?: string
119
+ /** Whether its file has a quality flag. */
120
+ flagged?: boolean
121
+ }
122
+
123
+ /**
124
+ * Whether a picture fits the parts a listing cannot answer for itself: its
125
+ * shape, whether it has words, and when it was added. The group and the free
126
+ * text are asked of the store; `aboutHere` judges the text on the name
127
+ * instead, for a store that cannot search.
128
+ */
129
+ export const fits = (item: Judged, sentence: Sentence, now: Date, aboutHere = false): boolean => {
130
+ if (sentence.shape && shapeOf(item.width, item.height) !== sentence.shape) return false
131
+ if (sentence.condition === "needs" && item.altText) return false
132
+ if (sentence.condition === "flagged" && !item.flagged) return false
133
+ if (sentence.when) {
134
+ const added = item.createdAt ? Date.parse(item.createdAt) : NaN
135
+ if (!(added >= startOf(sentence.when, now))) return false
136
+ }
137
+ if (aboutHere && sentence.about) {
138
+ const name = (item.filename || item.id || "").toLowerCase()
139
+ if (!sentence.about.split(" ").every((word) => name.includes(word))) return false
140
+ }
141
+ return true
142
+ }
143
+
144
+ /** Whether any part is judged on the pictures already loaded. */
145
+ export const judgedHere = (sentence: Sentence, aboutHere = false): boolean =>
146
+ Boolean(sentence.shape || sentence.condition === "needs" || sentence.condition === "flagged" || sentence.when || (aboutHere && sentence.about))
147
+
148
+ const labelOf = <T extends string>(options: Array<[T, string]>, value: T): string =>
149
+ options.find(([key]) => key === value)?.[1] ?? options[0]?.[1] ?? ""
150
+
151
+ /** Each part as the words on its button. */
152
+ export const words = (sentence: Sentence): Record<"shape" | "about" | "group" | "condition" | "when", string> => ({
153
+ shape: labelOf(SHAPES, sentence.shape),
154
+ about: sentence.about ? "“" + sentence.about + "”" : "anything",
155
+ group: sentence.group || "any group",
156
+ condition: labelOf(CONDITIONS, sentence.condition),
157
+ when: labelOf(WHENS, sentence.when),
158
+ })
159
+
160
+ /** The whole sentence as text, for a screen reader and a saved name. */
161
+ export const said = (sentence: Sentence): string => {
162
+ const part = words(sentence)
163
+ return `${part.shape} pictures of ${part.about} in ${part.group} that ${part.condition}, added ${part.when}.`
164
+ }
@@ -0,0 +1,152 @@
1
+ /* Pseudolux media library: tokens and parts.
2
+ Every visual value is a --px-* token with a neutral default, on `.px`.
3
+ A host themes the library only by re-pointing the tokens on an element
4
+ that contains it, with a more specific selector than `.px` (Tela uses
5
+ `html .px`); it never overrides these classes. Dark when the system asks,
6
+ unless the host says `data-theme="light"`, or when `data-theme="dark"`.
7
+ The theme selectors sit in `:where()` so every default weighs the same as
8
+ `.px`, and a host's mapping wins in dark as well as light. */
9
+
10
+ .px {
11
+ --px-font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
12
+ --px-font-sentence: "Source Serif 4", Georgia, "Times New Roman", serif;
13
+ --px-fs-xs: 12px; --px-fs-s: 13px; --px-fs-m: 15px; --px-fs-l: 17px; --px-fs-h: 22px;
14
+ --px-sp: 4px;
15
+ --px-r-s: 6px; --px-r-m: 8px; --px-r-l: 12px;
16
+ --px-target: 44px;
17
+
18
+ --px-bg: #f4f4f4;
19
+ --px-surface: #ffffff;
20
+ --px-surface-2: #eeeeee;
21
+ --px-hover: #e9e9e9;
22
+ --px-border: #d9d9d9;
23
+ --px-border-strong: #8c8c8c;
24
+ --px-text: #1a1a1a;
25
+ --px-text-2: #4d4d4d;
26
+ --px-text-3: #636363;
27
+ --px-accent: #2f4fc9;
28
+ --px-accent-soft: #e6ebfa;
29
+ --px-accent-text: #213fae;
30
+ --px-on-accent: #ffffff;
31
+ --px-focus: #2f4fc9;
32
+ --px-ok: #1d7343; --px-ok-soft: #e3f2e8;
33
+ --px-warn: #8a5300; --px-warn-soft: #fcefd2;
34
+ --px-danger: #b3261e; --px-danger-soft: #fce9e7;
35
+ --px-scrim: rgba(15, 15, 15, .55);
36
+ --px-shadow: 0 10px 30px rgba(0, 0, 0, .16), 0 2px 6px rgba(0, 0, 0, .08);
37
+
38
+ font-family: var(--px-font);
39
+ font-size: var(--px-fs-m);
40
+ line-height: 1.45;
41
+ color: var(--px-text);
42
+ -webkit-font-smoothing: antialiased;
43
+ }
44
+ @media (prefers-color-scheme: dark) {
45
+ .px:where(:not([data-theme="light"])) {
46
+ --px-bg: #141414; --px-surface: #1c1c1c; --px-surface-2: #242424; --px-hover: #2b2b2b;
47
+ --px-border: #353535; --px-border-strong: #707070;
48
+ --px-text: #ededed; --px-text-2: #b9b9b9; --px-text-3: #a0a0a0;
49
+ --px-accent: #88a2ff; --px-accent-soft: #1f2a4e; --px-accent-text: #aabdff; --px-on-accent: #0c1433; --px-focus: #a3b9ff;
50
+ --px-ok: #6fd09a; --px-ok-soft: #15301f; --px-warn: #f2bd57; --px-warn-soft: #33270e; --px-danger: #ff8f86; --px-danger-soft: #3b1917;
51
+ --px-scrim: rgba(0, 0, 0, .65);
52
+ --px-shadow: 0 10px 30px rgba(0, 0, 0, .5);
53
+ }
54
+ }
55
+ .px:where([data-theme="dark"]) {
56
+ --px-bg: #141414; --px-surface: #1c1c1c; --px-surface-2: #242424; --px-hover: #2b2b2b;
57
+ --px-border: #353535; --px-border-strong: #707070;
58
+ --px-text: #ededed; --px-text-2: #b9b9b9; --px-text-3: #a0a0a0;
59
+ --px-accent: #88a2ff; --px-accent-soft: #1f2a4e; --px-accent-text: #aabdff; --px-on-accent: #0c1433; --px-focus: #a3b9ff;
60
+ --px-ok: #6fd09a; --px-ok-soft: #15301f; --px-warn: #f2bd57; --px-warn-soft: #33270e; --px-danger: #ff8f86; --px-danger-soft: #3b1917;
61
+ --px-scrim: rgba(0, 0, 0, .65);
62
+ --px-shadow: 0 10px 30px rgba(0, 0, 0, .5);
63
+ }
64
+ .px *, .px *::before, .px *::after { box-sizing: border-box; }
65
+ .px h1, .px h2, .px h3, .px h4, .px p { margin: 0; }
66
+ .px h2 { font-size: var(--px-fs-h); font-weight: 600; }
67
+ .px h3 { font-size: var(--px-fs-l); font-weight: 600; }
68
+ .px h4 { font-size: var(--px-fs-s); font-weight: 600; }
69
+ .px a { color: var(--px-accent-text); }
70
+ .px :focus-visible, .px .is-focus { outline: 3px solid var(--px-focus); outline-offset: 2px; }
71
+ .px-muted { color: var(--px-text-2); }
72
+ .px-subtle { color: var(--px-text-3); font-size: var(--px-fs-s); }
73
+ .px-eyebrow, .px .px-eyebrow { font-size: var(--px-fs-xs); font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--px-text-3); }
74
+ .px-mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12px; }
75
+
76
+ /* Buttons */
77
+ .px-btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: var(--px-target); padding: 0 16px; border-radius: var(--px-r-m); border: 1px solid transparent; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; text-decoration: none; }
78
+ .px-btn-primary { background: var(--px-accent); color: var(--px-on-accent); }
79
+ .px-btn-secondary { background: var(--px-surface); color: var(--px-text); border-color: var(--px-border-strong); }
80
+ .px-btn-ghost { background: transparent; color: var(--px-text-2); }
81
+ .px-btn-ghost:hover { background: var(--px-hover); color: var(--px-text); }
82
+ .px-btn-danger { background: var(--px-danger); color: var(--px-surface); }
83
+ .px-btn-danger-quiet { background: transparent; color: var(--px-danger); border-color: var(--px-border); }
84
+ .px-btn-sm { min-height: 36px; padding: 0 12px; font-size: var(--px-fs-s); }
85
+ .px-btn-icon { width: var(--px-target); padding: 0; }
86
+ .px-btn-sm.px-btn-icon { width: 36px; }
87
+ .px-btn[disabled] { opacity: .45; cursor: not-allowed; }
88
+ .px-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
89
+
90
+ /* Fields */
91
+ .px-field { display: flex; flex-direction: column; gap: 6px; }
92
+ .px-label { font-weight: 600; font-size: var(--px-fs-s); display: flex; align-items: baseline; gap: 8px; }
93
+ .px-hint { font-size: var(--px-fs-xs); color: var(--px-text-3); }
94
+ .px-input, .px-select, .px-textarea { width: 100%; min-height: var(--px-target); padding: 9px 12px; font: inherit; font-size: 14px; color: var(--px-text); background: var(--px-surface); border: 1px solid var(--px-border-strong); border-radius: var(--px-r-m); }
95
+ .px-textarea { min-height: 76px; resize: vertical; }
96
+ .px-input[readonly], .px-textarea[readonly] { background: var(--px-surface-2); border-color: var(--px-border); color: var(--px-text-2); }
97
+ .px-search { display: flex; align-items: center; gap: 8px; min-height: var(--px-target); padding: 0 12px; border: 1px solid var(--px-border-strong); border-radius: var(--px-r-m); background: var(--px-surface); color: var(--px-text-3); }
98
+ .px-search input { border: 0; background: transparent; font: inherit; font-size: 14px; color: var(--px-text); flex: 1; min-width: 0; outline: none; }
99
+
100
+ /* Badges and chips */
101
+ .px-badge { display: inline-flex; align-items: center; gap: 5px; height: 22px; padding: 0 8px; border-radius: 999px; font-size: 11px; font-weight: 600; white-space: nowrap; border: 1px solid transparent; }
102
+ .px-badge-warn { background: var(--px-warn-soft); color: var(--px-warn); }
103
+ .px-badge-ok { background: var(--px-ok-soft); color: var(--px-ok); }
104
+ .px-badge-danger { background: var(--px-danger-soft); color: var(--px-danger); }
105
+ .px-badge-neutral { background: var(--px-surface); color: var(--px-text-2); border-color: var(--px-border); }
106
+ .px-badge-accent { background: var(--px-accent-soft); color: var(--px-accent-text); }
107
+ .px-chip { display: inline-flex; align-items: center; gap: 6px; min-height: 32px; padding: 0 4px 0 10px; border-radius: 999px; background: var(--px-surface-2); border: 1px solid var(--px-border); font-size: var(--px-fs-s); font-weight: 500; }
108
+ .px-chip.is-internal { background: var(--px-surface); border-style: dashed; }
109
+ .px-chip.is-suggested { background: var(--px-accent-soft); border: 1px dashed var(--px-accent); color: var(--px-accent-text); }
110
+ .px-chip button { width: 28px; height: 28px; border: 0; background: transparent; border-radius: 50%; color: inherit; display: grid; place-items: center; cursor: pointer; }
111
+
112
+ /* Pictures (placeholders stand in for real images) */
113
+ .px-pic { background: linear-gradient(135deg, #b8bcc4, #8f96a3); position: relative; overflow: hidden; }
114
+
115
+ /* Grid and tiles */
116
+
117
+ /* Dense list view */
118
+
119
+ /* Layout parts */
120
+ .px-seg { display: inline-flex; padding: 3px; border: 1px solid var(--px-border); border-radius: var(--px-r-m); background: var(--px-surface-2); }
121
+ .px-seg button { min-height: 36px; padding: 0 12px; border: 0; border-radius: calc(var(--px-r-m) - 2px); background: transparent; font: inherit; font-size: var(--px-fs-s); color: var(--px-text-2); cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
122
+ .px-seg button[aria-pressed="true"], .px-seg button[aria-checked="true"] { background: var(--px-surface); color: var(--px-text); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }
123
+
124
+
125
+
126
+ .px-focal { position: absolute; width: 28px; height: 28px; margin: -14px 0 0 -14px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 2px rgba(0,0,0,.55); display: grid; place-items: center; }
127
+ .px-focal::after { content: ""; width: 6px; height: 6px; border-radius: 50%; background: #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.55); }
128
+
129
+ .px-crop { flex: none; display: flex; flex-direction: column; gap: 4px; padding: 0; border: 0; background: transparent; font: inherit; color: inherit; text-align: left; cursor: pointer; }
130
+ .px-crop .px-pic { border-radius: var(--px-r-s); border: 1px solid var(--px-border); }
131
+ .px-crop.is-current .px-pic { outline: 3px solid var(--px-accent); outline-offset: 1px; }
132
+
133
+
134
+ .px-skel { background: linear-gradient(90deg, var(--px-surface-2), var(--px-hover), var(--px-surface-2)); border-radius: var(--px-r-s); }
135
+ .px-empty { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 10px; padding: 32px; }
136
+
137
+ .px-dialog { background: var(--px-surface); border: 1px solid var(--px-border); border-radius: var(--px-r-l); box-shadow: var(--px-shadow); display: flex; flex-direction: column; overflow: hidden; }
138
+ .px-dialog-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px; border-bottom: 1px solid var(--px-border); }
139
+ .px-dialog-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--px-border); background: var(--px-surface-2); }
140
+ .px-callout { display: flex; gap: 10px; align-items: flex-start; padding: 12px; border-radius: var(--px-r-m); border: 1px solid var(--px-border); background: var(--px-surface-2); font-size: var(--px-fs-s); }
141
+ .px-callout-warn { background: var(--px-warn-soft); border-color: var(--px-warn); }
142
+ .px-callout-danger { background: var(--px-danger-soft); border-color: var(--px-danger); }
143
+ .px-callout-accent { background: var(--px-accent-soft); border-color: var(--px-accent); }
144
+
145
+ .px-bar { height: 6px; border-radius: 3px; background: var(--px-surface-2); overflow: hidden; }
146
+ .px-bar > span { display: block; height: 100%; background: var(--px-accent); }
147
+
148
+ .px-cropper { position: relative; overflow: hidden; border-radius: var(--px-r-m); }
149
+ .px-cropbox { position: absolute; border: 2px solid #fff; box-shadow: 0 0 0 9999px rgba(0,0,0,.55); }
150
+ .px-cropbox .h { position: absolute; width: 14px; height: 14px; background: #fff; border: 1px solid rgba(0,0,0,.5); border-radius: 2px; }
151
+ .px-cropbox .g { position: absolute; background: rgba(255,255,255,.45); }
152
+ .px-kbd { display: inline-flex; align-items: center; height: 22px; padding: 0 6px; border: 1px solid var(--px-border-strong); border-bottom-width: 2px; border-radius: 5px; font-size: 11px; color: var(--px-text-2); background: var(--px-surface); }
package/src/survey.ts ADDED
@@ -0,0 +1,75 @@
1
+ /**
2
+ * A look over the newest pictures in a group, for the parts of the library
3
+ * that count things: the desk and the group cards.
4
+ *
5
+ * A listing gives no totals, so a count is only ever of what was read. Each
6
+ * survey says whether it reached the end, and whoever shows a number says
7
+ * "at least" when it did not.
8
+ */
9
+
10
+ import type { LibraryItem, LibraryPort } from "./port.js"
11
+ import { describedFor } from "./config.js"
12
+ import { startOf } from "./sentence.js"
13
+
14
+ export interface Survey {
15
+ group: string
16
+ items: LibraryItem[]
17
+ /** Whether every picture in the group was read. */
18
+ complete: boolean
19
+ }
20
+
21
+ export interface Tally {
22
+ count: number
23
+ complete: boolean
24
+ /** Pictures with no alt text as the group reads them. */
25
+ needs: LibraryItem[]
26
+ week: LibraryItem[]
27
+ month: LibraryItem[]
28
+ }
29
+
30
+ const PAGE = 100
31
+ const PAGES = 3
32
+ const FRESH_FOR = 60_000
33
+
34
+ const cache = new Map<string, { at: number; survey: Promise<Survey> }>()
35
+
36
+ /** The newest pictures in a group, up to a few hundred. Kept for a minute so
37
+ * the desk and the group cards ask once between them. */
38
+ export const survey = (port: LibraryPort, group: string, pages = PAGES): Promise<Survey> => {
39
+ const key = group + ":" + pages
40
+ const kept = cache.get(key)
41
+ if (kept && Date.now() - kept.at < FRESH_FOR) return kept.survey
42
+ const asked = (async (): Promise<Survey> => {
43
+ const items: LibraryItem[] = []
44
+ let cursor: string | undefined
45
+ for (let page = 0; page < pages; page++) {
46
+ const answer = await port.list({ limit: PAGE, ...(group ? { group } : {}), ...(cursor ? { cursor } : {}) })
47
+ items.push(...answer.items)
48
+ cursor = answer.nextCursor
49
+ if (!cursor) break
50
+ }
51
+ return { group, items, complete: !cursor }
52
+ })()
53
+ cache.set(key, { at: Date.now(), survey: asked })
54
+ asked.catch(() => cache.delete(key))
55
+ return asked
56
+ }
57
+
58
+ /** Something changed the library: the next survey reads it again. */
59
+ export const forgetSurveys = (): void => cache.clear()
60
+
61
+ export const tally = (found: Survey, now: Date): Tally => {
62
+ const week = startOf("week", now)
63
+ const month = startOf("month", now)
64
+ const added = (item: LibraryItem): number => (item.createdAt ? Date.parse(item.createdAt) : NaN)
65
+ return {
66
+ count: found.items.length,
67
+ complete: found.complete,
68
+ needs: found.items.filter((item) => !describedFor(item, found.group || undefined).altText),
69
+ week: found.items.filter((item) => added(item) >= week),
70
+ month: found.items.filter((item) => added(item) >= month),
71
+ }
72
+ }
73
+
74
+ /** "12", or "at least 12" when there may be more than were read. */
75
+ export const atLeast = (n: number, complete: boolean): string => (complete ? String(n) : "at least " + n)
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.