@kiosko/sdk 0.0.0-stage → 0.2.1
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/dist/context.d.ts +20 -0
- package/dist/context.js +24 -0
- package/dist/filter.d.ts +26 -0
- package/dist/filter.js +53 -0
- package/dist/format.d.ts +59 -0
- package/dist/format.js +87 -0
- package/dist/i18n.d.ts +9 -0
- package/dist/i18n.js +8 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +11 -0
- package/dist/list.d.ts +133 -0
- package/dist/list.js +126 -0
- package/dist/location.d.ts +26 -0
- package/dist/location.js +33 -0
- package/dist/plugin.d.ts +419 -0
- package/dist/plugin.js +24 -0
- package/dist/schema.d.ts +155 -0
- package/dist/schema.js +169 -0
- package/dist/system.d.ts +35 -0
- package/dist/system.js +80 -0
- package/dist/theme.d.ts +47 -0
- package/dist/theme.js +49 -0
- package/dist/workday.d.ts +48 -0
- package/dist/workday.js +40 -0
- package/package.json +47 -4
- package/src/context.ts +26 -0
- package/src/filter.ts +56 -0
- package/src/format.ts +123 -0
- package/src/i18n.ts +13 -0
- package/src/index.ts +11 -0
- package/src/list.ts +217 -0
- package/src/location.ts +41 -0
- package/src/plugin.ts +429 -0
- package/src/schema.ts +264 -0
- package/src/system.ts +87 -0
- package/src/theme.ts +98 -0
- package/src/workday.ts +79 -0
- package/README.md +0 -3
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of a plugin context that don't depend on the host: language,
|
|
3
|
+
* texts, formatters, place and working week. Kiosko builds every context on
|
|
4
|
+
* top of this; plugin tests can build theirs with it instead of by hand:
|
|
5
|
+
*
|
|
6
|
+
* const ctx = { ...createBaseContext({ now, locale: 'es' }), options, signal, intervalMs: 60_000, timeoutMs: 20_000 };
|
|
7
|
+
*/
|
|
8
|
+
import { type TimeFormat } from './format.ts';
|
|
9
|
+
import type { BaseContext, Location, Logger } from './plugin.ts';
|
|
10
|
+
import { type Workday } from './workday.ts';
|
|
11
|
+
export declare function createBaseContext(o: {
|
|
12
|
+
now: number;
|
|
13
|
+
locale?: string;
|
|
14
|
+
timeFormat?: TimeFormat;
|
|
15
|
+
workday?: Workday;
|
|
16
|
+
location?: Location;
|
|
17
|
+
log?: Logger;
|
|
18
|
+
dataDir?: string;
|
|
19
|
+
cacheDir?: string;
|
|
20
|
+
}): BaseContext;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of a plugin context that don't depend on the host: language,
|
|
3
|
+
* texts, formatters, place and working week. Kiosko builds every context on
|
|
4
|
+
* top of this; plugin tests can build theirs with it instead of by hand:
|
|
5
|
+
*
|
|
6
|
+
* const ctx = { ...createBaseContext({ now, locale: 'es' }), options, signal, intervalMs: 60_000, timeoutMs: 20_000 };
|
|
7
|
+
*/
|
|
8
|
+
import { createFormatters } from "./format.js";
|
|
9
|
+
import { localize } from "./i18n.js";
|
|
10
|
+
import { DEFAULT_WORKDAY } from "./workday.js";
|
|
11
|
+
export function createBaseContext(o) {
|
|
12
|
+
const locale = o.locale ?? 'en';
|
|
13
|
+
return {
|
|
14
|
+
now: o.now,
|
|
15
|
+
locale,
|
|
16
|
+
t: (text) => localize(text, locale),
|
|
17
|
+
fmt: createFormatters({ locale, timeFormat: o.timeFormat ?? 'auto', now: o.now }),
|
|
18
|
+
log: o.log ?? (() => undefined),
|
|
19
|
+
dataDir: o.dataDir ?? '',
|
|
20
|
+
cacheDir: o.cacheDir ?? o.dataDir ?? '',
|
|
21
|
+
...(o.location ? { location: o.location } : {}),
|
|
22
|
+
workday: o.workday ?? DEFAULT_WORKDAY,
|
|
23
|
+
};
|
|
24
|
+
}
|
package/dist/filter.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Item filters, the same in every widget that lists projects, repos,
|
|
3
|
+
* calendars, servers or people: a list of patterns, edited with `t.filter()`
|
|
4
|
+
* and applied with `matchFilter`.
|
|
5
|
+
*
|
|
6
|
+
* [] everything
|
|
7
|
+
* ["web", "api"] only these (by full name or by its last part)
|
|
8
|
+
* ["acme/*"] everything under acme/ (`*` matches anything, `/` too)
|
|
9
|
+
* ["!legacy-*"] everything except what starts with legacy-
|
|
10
|
+
* ["acme/*", "!*-old"] under acme/, but not the *-old ones
|
|
11
|
+
*
|
|
12
|
+
* Case doesn't matter, `?` is one character, and `*text*` is "contains". A
|
|
13
|
+
* value matches a pattern by its full form (`group/sub/project`) or by its
|
|
14
|
+
* last `/` segment (`project`), like `aliases` do.
|
|
15
|
+
*/
|
|
16
|
+
/** A filter: patterns as a list, or one text with commas or line breaks between them. */
|
|
17
|
+
export type Filter = readonly string[] | string;
|
|
18
|
+
/** The patterns of a filter, trimmed, without empty ones. */
|
|
19
|
+
export declare function filterPatterns(filter: Filter | undefined): string[];
|
|
20
|
+
/**
|
|
21
|
+
* Whether `value` passes `filter`. `value` may be several names of the same
|
|
22
|
+
* item (a repo's full name and its alias): it passes if any of them matches
|
|
23
|
+
* an include pattern and none matches an exclude one. An empty filter (or
|
|
24
|
+
* one with only exclusions) lets everything else through.
|
|
25
|
+
*/
|
|
26
|
+
export declare function matchFilter(value: string | readonly string[], filter: Filter | undefined): boolean;
|
package/dist/filter.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Item filters, the same in every widget that lists projects, repos,
|
|
3
|
+
* calendars, servers or people: a list of patterns, edited with `t.filter()`
|
|
4
|
+
* and applied with `matchFilter`.
|
|
5
|
+
*
|
|
6
|
+
* [] everything
|
|
7
|
+
* ["web", "api"] only these (by full name or by its last part)
|
|
8
|
+
* ["acme/*"] everything under acme/ (`*` matches anything, `/` too)
|
|
9
|
+
* ["!legacy-*"] everything except what starts with legacy-
|
|
10
|
+
* ["acme/*", "!*-old"] under acme/, but not the *-old ones
|
|
11
|
+
*
|
|
12
|
+
* Case doesn't matter, `?` is one character, and `*text*` is "contains". A
|
|
13
|
+
* value matches a pattern by its full form (`group/sub/project`) or by its
|
|
14
|
+
* last `/` segment (`project`), like `aliases` do.
|
|
15
|
+
*/
|
|
16
|
+
const cache = new Map();
|
|
17
|
+
function globRe(pattern) {
|
|
18
|
+
let re = cache.get(pattern);
|
|
19
|
+
if (!re) {
|
|
20
|
+
const body = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.');
|
|
21
|
+
re = new RegExp(`^${body}$`, 'i');
|
|
22
|
+
if (cache.size > 500)
|
|
23
|
+
cache.clear();
|
|
24
|
+
cache.set(pattern, re);
|
|
25
|
+
}
|
|
26
|
+
return re;
|
|
27
|
+
}
|
|
28
|
+
/** The patterns of a filter, trimmed, without empty ones. */
|
|
29
|
+
export function filterPatterns(filter) {
|
|
30
|
+
const list = typeof filter === 'string' ? filter.split(/[,\n]/) : (filter ?? []);
|
|
31
|
+
return list.map((p) => p.trim()).filter((p) => p && p !== '!');
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Whether `value` passes `filter`. `value` may be several names of the same
|
|
35
|
+
* item (a repo's full name and its alias): it passes if any of them matches
|
|
36
|
+
* an include pattern and none matches an exclude one. An empty filter (or
|
|
37
|
+
* one with only exclusions) lets everything else through.
|
|
38
|
+
*/
|
|
39
|
+
export function matchFilter(value, filter) {
|
|
40
|
+
const patterns = filterPatterns(filter);
|
|
41
|
+
if (!patterns.length)
|
|
42
|
+
return true;
|
|
43
|
+
const names = (typeof value === 'string' ? [value] : [...value]).flatMap((v) => {
|
|
44
|
+
const last = v.split('/').pop() ?? v;
|
|
45
|
+
return last === v ? [v] : [v, last];
|
|
46
|
+
});
|
|
47
|
+
const hit = (p) => names.some((n) => globRe(p).test(n));
|
|
48
|
+
const includes = patterns.filter((p) => !p.startsWith('!'));
|
|
49
|
+
const excludes = patterns.filter((p) => p.startsWith('!')).map((p) => p.slice(1).trim());
|
|
50
|
+
if (excludes.some(hit))
|
|
51
|
+
return false;
|
|
52
|
+
return !includes.length || includes.some(hit);
|
|
53
|
+
}
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Times and lengths of time, written the same way by every widget:
|
|
3
|
+
* `ctx.fmt.time(at)`, `ctx.fmt.ago(at)` and `ctx.fmt.duration(ms)`.
|
|
4
|
+
*
|
|
5
|
+
* They follow the panel's language (English and Spanish words; any other
|
|
6
|
+
* language gets the English ones) and the config's `timeFormat`. Hand-written
|
|
7
|
+
* rather than `Intl.RelativeTimeFormat`: its short forms differ between ICU
|
|
8
|
+
* versions (Node, Chromium, WebKit), and a panel must paint the same thing in
|
|
9
|
+
* the editor, the browser demo and on the glass.
|
|
10
|
+
*/
|
|
11
|
+
/** How clock times are written: `auto` follows the language (24 h in Spanish, 12 h in US English). */
|
|
12
|
+
export type TimeFormat = 'auto' | '24h' | '12h';
|
|
13
|
+
export declare const TIME_FORMATS: readonly TimeFormat[];
|
|
14
|
+
export interface Formatters {
|
|
15
|
+
/** True when clock times are written on a 12-hour clock. */
|
|
16
|
+
readonly hour12: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* A clock time: `"09:05"` on a 24-hour clock, `"9:05 AM"` (`"9:05 a. m."`)
|
|
19
|
+
* on a 12-hour one. `seconds` adds `":07"`; `ampm: false` leaves out
|
|
20
|
+
* AM/PM (a big clock that paints it apart).
|
|
21
|
+
*/
|
|
22
|
+
time(at: number | Date, o?: {
|
|
23
|
+
seconds?: boolean;
|
|
24
|
+
ampm?: boolean;
|
|
25
|
+
}): string;
|
|
26
|
+
/**
|
|
27
|
+
* How long ago `at` (epoch ms, a Date or an ISO string) was, from `ctx.now`:
|
|
28
|
+
* `"just now"`, `"5 min ago"`, `"3 h ago"`, `"4 d ago"`, `"2 wk ago"`,
|
|
29
|
+
* `"1 yr ago"` (`"hace 5 min"`…). `suffix: false` gives the bare age for
|
|
30
|
+
* a meta line: `"now"`, `"5 min"`, `"3 h"`. A future `at` is "now".
|
|
31
|
+
*/
|
|
32
|
+
ago(at: number | Date | string, o?: {
|
|
33
|
+
suffix?: boolean;
|
|
34
|
+
now?: number;
|
|
35
|
+
}): string;
|
|
36
|
+
/**
|
|
37
|
+
* A length of time: `"45 s"` (only with `seconds`), `"12 min"`,
|
|
38
|
+
* `"1 h 05"`, `"2 d 3 h"`. Rounded down by default; `round: 'up'` for
|
|
39
|
+
* countdowns ("in 0 min" would be a lie until it starts). `compact`
|
|
40
|
+
* drops a zero remainder: `"1 h"` rather than `"1 h 00"`, `"2 d"` rather
|
|
41
|
+
* than `"2 d 0 h"` (a span such as "the last 1 h", not a running timer).
|
|
42
|
+
*/
|
|
43
|
+
duration(ms: number, o?: {
|
|
44
|
+
seconds?: boolean;
|
|
45
|
+
round?: 'down' | 'up' | 'nearest';
|
|
46
|
+
compact?: boolean;
|
|
47
|
+
}): string;
|
|
48
|
+
}
|
|
49
|
+
/** Whether `locale` writes times on a 12-hour clock, per `timeFormat`. */
|
|
50
|
+
export declare function usesHour12(locale: string, timeFormat?: TimeFormat): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* The formatters of a context. Kiosko builds them for every plugin context;
|
|
53
|
+
* plugin tests can build their own: `createFormatters({ locale: 'es', now })`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function createFormatters(o: {
|
|
56
|
+
locale: string;
|
|
57
|
+
timeFormat?: TimeFormat;
|
|
58
|
+
now: number;
|
|
59
|
+
}): Formatters;
|
package/dist/format.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Times and lengths of time, written the same way by every widget:
|
|
3
|
+
* `ctx.fmt.time(at)`, `ctx.fmt.ago(at)` and `ctx.fmt.duration(ms)`.
|
|
4
|
+
*
|
|
5
|
+
* They follow the panel's language (English and Spanish words; any other
|
|
6
|
+
* language gets the English ones) and the config's `timeFormat`. Hand-written
|
|
7
|
+
* rather than `Intl.RelativeTimeFormat`: its short forms differ between ICU
|
|
8
|
+
* versions (Node, Chromium, WebKit), and a panel must paint the same thing in
|
|
9
|
+
* the editor, the browser demo and on the glass.
|
|
10
|
+
*/
|
|
11
|
+
export const TIME_FORMATS = ['auto', '24h', '12h'];
|
|
12
|
+
const WORDS = {
|
|
13
|
+
en: { now: 'now', justNow: 'just now', ago: (v) => `${v} ago`, wk: 'wk', yr: () => 'yr', am: 'AM', pm: 'PM' },
|
|
14
|
+
es: { now: 'ahora', justNow: 'ahora mismo', ago: (v) => `hace ${v}`, wk: 'sem', yr: (n) => (n === 1 ? 'año' : 'años'), am: 'a. m.', pm: 'p. m.' },
|
|
15
|
+
};
|
|
16
|
+
const langOf = (locale) => locale.toLowerCase().split(/[-_]/)[0] ?? 'en';
|
|
17
|
+
const wordsFor = (locale) => WORDS[langOf(locale)] ?? WORDS.en;
|
|
18
|
+
const pad2 = (n) => String(n).padStart(2, '0');
|
|
19
|
+
const cycles = new Map();
|
|
20
|
+
/** Whether `locale` writes times on a 12-hour clock, per `timeFormat`. */
|
|
21
|
+
export function usesHour12(locale, timeFormat = 'auto') {
|
|
22
|
+
if (timeFormat !== 'auto')
|
|
23
|
+
return timeFormat === '12h';
|
|
24
|
+
let h12 = cycles.get(locale);
|
|
25
|
+
if (h12 === undefined) {
|
|
26
|
+
try {
|
|
27
|
+
const cycle = new Intl.DateTimeFormat(locale, { hour: 'numeric' }).resolvedOptions().hourCycle;
|
|
28
|
+
h12 = cycle === 'h11' || cycle === 'h12';
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
h12 = false;
|
|
32
|
+
}
|
|
33
|
+
cycles.set(locale, h12);
|
|
34
|
+
}
|
|
35
|
+
return h12;
|
|
36
|
+
}
|
|
37
|
+
function toMs(at) {
|
|
38
|
+
if (typeof at === 'number')
|
|
39
|
+
return at;
|
|
40
|
+
if (typeof at === 'string')
|
|
41
|
+
return Date.parse(at);
|
|
42
|
+
return at.getTime();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The formatters of a context. Kiosko builds them for every plugin context;
|
|
46
|
+
* plugin tests can build their own: `createFormatters({ locale: 'es', now })`.
|
|
47
|
+
*/
|
|
48
|
+
export function createFormatters(o) {
|
|
49
|
+
const w = wordsFor(o.locale);
|
|
50
|
+
const hour12 = usesHour12(o.locale, o.timeFormat);
|
|
51
|
+
return {
|
|
52
|
+
hour12,
|
|
53
|
+
time(at, opts = {}) {
|
|
54
|
+
const d = new Date(toMs(at));
|
|
55
|
+
const h = d.getHours();
|
|
56
|
+
const rest = `:${pad2(d.getMinutes())}${opts.seconds ? `:${pad2(d.getSeconds())}` : ''}`;
|
|
57
|
+
if (!hour12)
|
|
58
|
+
return `${pad2(h)}${rest}`;
|
|
59
|
+
const base = `${h % 12 || 12}${rest}`;
|
|
60
|
+
return opts.ampm === false ? base : `${base} ${h < 12 ? w.am : w.pm}`;
|
|
61
|
+
},
|
|
62
|
+
ago(at, opts = {}) {
|
|
63
|
+
const suffix = opts.suffix !== false;
|
|
64
|
+
const ms = (opts.now ?? o.now) - toMs(at);
|
|
65
|
+
const min = Math.floor(ms / 60_000);
|
|
66
|
+
if (!Number.isFinite(min) || min < 1)
|
|
67
|
+
return suffix ? w.justNow : w.now;
|
|
68
|
+
const h = Math.floor(min / 60);
|
|
69
|
+
const d = Math.floor(h / 24);
|
|
70
|
+
const v = min < 60 ? `${min} min` : h < 24 ? `${h} h` : d < 14 ? `${d} d` : d < 365 ? `${Math.floor(d / 7)} ${w.wk}` : `${Math.floor(d / 365)} ${w.yr(Math.floor(d / 365))}`;
|
|
71
|
+
return suffix ? w.ago(v) : v;
|
|
72
|
+
},
|
|
73
|
+
duration(ms, opts = {}) {
|
|
74
|
+
const round = opts.round === 'up' ? Math.ceil : opts.round === 'nearest' ? Math.round : Math.floor;
|
|
75
|
+
const safe = Number.isFinite(ms) ? Math.max(0, ms) : 0;
|
|
76
|
+
if (opts.seconds && safe < 60_000)
|
|
77
|
+
return `${round(safe / 1000)} s`;
|
|
78
|
+
const min = round(safe / 60_000);
|
|
79
|
+
if (min < 60)
|
|
80
|
+
return `${min} min`;
|
|
81
|
+
const h = Math.floor(min / 60);
|
|
82
|
+
if (h < 24)
|
|
83
|
+
return opts.compact && min % 60 === 0 ? `${h} h` : `${h} h ${pad2(min % 60)}`;
|
|
84
|
+
return opts.compact && h % 24 === 0 ? `${h / 24} d` : `${Math.floor(h / 24)} d ${h % 24} h`;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin texts can be a plain string (shown as is) or a map by language.
|
|
3
|
+
* The editor and the panel pick the user's locale, then English, then the
|
|
4
|
+
* first one available.
|
|
5
|
+
*/
|
|
6
|
+
export type Localized = string | ({
|
|
7
|
+
en: string;
|
|
8
|
+
} & Partial<Record<string, string>>);
|
|
9
|
+
export declare function localize(text: Localized | undefined, locale?: string): string;
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function localize(text, locale = 'en') {
|
|
2
|
+
if (text === undefined)
|
|
3
|
+
return '';
|
|
4
|
+
if (typeof text === 'string')
|
|
5
|
+
return text;
|
|
6
|
+
const lang = locale.toLowerCase().split(/[-_]/)[0];
|
|
7
|
+
return text[locale] ?? text[lang] ?? text.en ?? Object.values(text).find((v) => typeof v === 'string') ?? '';
|
|
8
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './plugin.ts';
|
|
2
|
+
export * from './schema.ts';
|
|
3
|
+
export * from './theme.ts';
|
|
4
|
+
export * from './i18n.ts';
|
|
5
|
+
export * from './format.ts';
|
|
6
|
+
export * from './workday.ts';
|
|
7
|
+
export * from './list.ts';
|
|
8
|
+
export * from './filter.ts';
|
|
9
|
+
export * from './context.ts';
|
|
10
|
+
export * from './location.ts';
|
|
11
|
+
export * from './system.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./plugin.js";
|
|
2
|
+
export * from "./schema.js";
|
|
3
|
+
export * from "./theme.js";
|
|
4
|
+
export * from "./i18n.js";
|
|
5
|
+
export * from "./format.js";
|
|
6
|
+
export * from "./workday.js";
|
|
7
|
+
export * from "./list.js";
|
|
8
|
+
export * from "./filter.js";
|
|
9
|
+
export * from "./context.js";
|
|
10
|
+
export * from "./location.js";
|
|
11
|
+
export * from "./system.js";
|
package/dist/list.d.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists, the same in every widget: two shared options (`rows`, `layout`),
|
|
3
|
+
* one layout helper that decides how many rows fit and how tall they are,
|
|
4
|
+
* and one "+N more" line.
|
|
5
|
+
*
|
|
6
|
+
* options: t.object({
|
|
7
|
+
* ...listOptions({ layouts: ['one-line', 'detailed'] }), // rows, layout
|
|
8
|
+
* projects: t.filter({ label: 'Projects' }),
|
|
9
|
+
* }),
|
|
10
|
+
* render(g, ctx) {
|
|
11
|
+
* const items = …;
|
|
12
|
+
* if (!items.length) return drawEmpty(g, ctx, ctx.t({ en: 'Nothing to review', es: 'Nada que revisar' }));
|
|
13
|
+
* const plan = listLayout(ctx.box, items.length, ctx.options, {
|
|
14
|
+
* sizes: { 'one-line': { min: 22, max: 46 }, detailed: { min: 50, max: 80 } },
|
|
15
|
+
* auto: (box, n) => (box.h >= box.w * 0.7 && box.h / n >= 50 ? 'detailed' : 'one-line'),
|
|
16
|
+
* });
|
|
17
|
+
* plan.rows.forEach((row, i) => drawItem(items[i], row, plan.layout, plan.textSize));
|
|
18
|
+
* drawMore(g, ctx, plan);
|
|
19
|
+
* },
|
|
20
|
+
*
|
|
21
|
+
* `rows` = 0 fits as many rows as the box holds, each between the layout's
|
|
22
|
+
* `min` and `max` px. `rows` = N shows N items, in rows of the box's height
|
|
23
|
+
* divided by N but never taller than `max` (the rest of the box stays empty,
|
|
24
|
+
* below them) nor shorter than `min` (then fewer rows). Items left out are
|
|
25
|
+
* counted in "+N more", a compact line under the last row.
|
|
26
|
+
*/
|
|
27
|
+
import type { Localized } from './i18n.ts';
|
|
28
|
+
import type { Box, Graphics, RenderContext } from './plugin.ts';
|
|
29
|
+
import { type EnumField, type NumberField } from './schema.ts';
|
|
30
|
+
/** How a list row is drawn: one line per item, or a title with details under it. */
|
|
31
|
+
export type ListLayout = 'one-line' | 'detailed';
|
|
32
|
+
/** The `layout` option: a fixed layout, or `auto` (the widget's own rule). */
|
|
33
|
+
export type ListLayoutChoice = 'auto' | ListLayout;
|
|
34
|
+
/**
|
|
35
|
+
* The shared list options, to spread into the widget's `t.object({...})`:
|
|
36
|
+
* `rows` (0 = auto, up to 30) and, when the widget draws two layouts,
|
|
37
|
+
* `layout` (`auto` plus those). `rows` is the default of `rows`: 0 unless
|
|
38
|
+
* the widget had a fixed count before.
|
|
39
|
+
*/
|
|
40
|
+
export declare function listOptions(o?: {
|
|
41
|
+
rows?: number;
|
|
42
|
+
}): {
|
|
43
|
+
rows: NumberField;
|
|
44
|
+
};
|
|
45
|
+
export declare function listOptions<L extends ListLayout>(o: {
|
|
46
|
+
rows?: number;
|
|
47
|
+
layouts: readonly [L, L, ...L[]];
|
|
48
|
+
layout?: 'auto' | L;
|
|
49
|
+
}): {
|
|
50
|
+
rows: NumberField;
|
|
51
|
+
layout: EnumField<'auto' | L>;
|
|
52
|
+
};
|
|
53
|
+
/** Row heights of one layout, in px of the widget's box. */
|
|
54
|
+
export interface RowSize {
|
|
55
|
+
/** Smallest row that can still be read. */
|
|
56
|
+
min: number;
|
|
57
|
+
/** Tallest row: rows never grow past it, however few they are (the rest of the box stays empty). */
|
|
58
|
+
max: number;
|
|
59
|
+
}
|
|
60
|
+
export interface ListSpec<L extends ListLayout = ListLayout> {
|
|
61
|
+
/** One entry per layout the widget draws: its row heights. */
|
|
62
|
+
sizes: {
|
|
63
|
+
[K in L]: RowSize;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* The layout `auto` picks: the widget's own rule (e.g. two lines on tall
|
|
67
|
+
* tiles). `count` is how many rows the box is sized for: `rows` when the
|
|
68
|
+
* user set it (and there are that many items), otherwise every item.
|
|
69
|
+
* Default: the first layout in `sizes`.
|
|
70
|
+
*/
|
|
71
|
+
auto?: (box: Box, count: number) => L;
|
|
72
|
+
/** In auto mode, size rows as if there were at least this many items, so one item doesn't get a giant row. Default 1. */
|
|
73
|
+
minRows?: number;
|
|
74
|
+
/** Height of the "+N more" line, in px. Default: `moreHeight(rowH)`. Never more than a row. */
|
|
75
|
+
moreH?: number;
|
|
76
|
+
}
|
|
77
|
+
export interface ListPlan<L extends ListLayout = ListLayout> {
|
|
78
|
+
/** The layout to draw (`auto` already resolved). */
|
|
79
|
+
layout: L;
|
|
80
|
+
/** Height of every row, in px: between the layout's `min` and `max` (less only in a box shorter than one row). */
|
|
81
|
+
rowH: number;
|
|
82
|
+
/** One box per row to draw, top to bottom from the top of the box: item i goes in `rows[i]`. */
|
|
83
|
+
rows: Box[];
|
|
84
|
+
/** Items drawn (`rows.length`). */
|
|
85
|
+
shown: number;
|
|
86
|
+
/** Items left out, counted in "+N more". */
|
|
87
|
+
hidden: number;
|
|
88
|
+
/** Where "+N more" goes, right under the last row; null when everything fits (or a tiny box leaves no room for it). */
|
|
89
|
+
more: Box | null;
|
|
90
|
+
/** Suggested size of a row's main text, px: 60 % of a one-line row, 42 % of a detailed one (its first line). */
|
|
91
|
+
textSize: number;
|
|
92
|
+
/** Suggested size of the secondary text: a detailed row's second line (30 % of the row), or the smaller bits of a one-line row (80 % of `textSize`). */
|
|
93
|
+
detailSize: number;
|
|
94
|
+
}
|
|
95
|
+
/** Height of the "+N more" line under rows of `rowH` px: 60 % of a row, between 18 and 28 px, never more than a row. */
|
|
96
|
+
export declare function moreHeight(rowH: number): number;
|
|
97
|
+
/**
|
|
98
|
+
* Plans a list of `count` items in `box` from the shared options (`rows`,
|
|
99
|
+
* `layout`) and the widget's row heights. Pure: no drawing, no canvas.
|
|
100
|
+
*
|
|
101
|
+
* - `rows` 0: rows of `box.h / count` (at least `minRows`), clamped to the
|
|
102
|
+
* layout's `min`–`max`; as many as fit.
|
|
103
|
+
* - `rows` N: N items in rows of `box.h / N` clamped the same way, so they
|
|
104
|
+
* never balloon; fewer when N rows don't fit at `min`.
|
|
105
|
+
* - Items left out: a compact "+N more" line (`more`) right under the last
|
|
106
|
+
* row, reserved only then. A box with room for one row but not for the row
|
|
107
|
+
* and that line still gets the row, with the line squeezed or left out.
|
|
108
|
+
*/
|
|
109
|
+
export declare function listLayout<L extends ListLayout>(box: Box, count: number, options: {
|
|
110
|
+
rows?: number;
|
|
111
|
+
layout?: string;
|
|
112
|
+
}, spec: ListSpec<L>): ListPlan<L>;
|
|
113
|
+
/** `"+3 more"` / `"+3 más"`. */
|
|
114
|
+
export declare function moreLabel(n: number, tr: (text: Localized) => string): string;
|
|
115
|
+
type DrawCtx = Pick<RenderContext<unknown, unknown>, 'kit' | 'theme' | 't'>;
|
|
116
|
+
/**
|
|
117
|
+
* Paints "+N more" in the plan's `more` box, dim, the same in every list.
|
|
118
|
+
* `indent` lines it up with the rows' text (after a status dot, say);
|
|
119
|
+
* `align: 'right'` puts it at the right edge instead.
|
|
120
|
+
*/
|
|
121
|
+
export declare function drawMore(g: Graphics, ctx: DrawCtx, plan: Pick<ListPlan, 'hidden' | 'more'>, o?: {
|
|
122
|
+
indent?: number;
|
|
123
|
+
align?: 'left' | 'right';
|
|
124
|
+
}): void;
|
|
125
|
+
/**
|
|
126
|
+
* The message of an empty list ("Nothing to review"), dim, at the top left
|
|
127
|
+
* of the box, the same in every list. Paint it unless the user asked to hide
|
|
128
|
+
* the widget when it's empty (see `WidgetDefinition.empty`).
|
|
129
|
+
*/
|
|
130
|
+
export declare function drawEmpty(g: Graphics, ctx: DrawCtx & {
|
|
131
|
+
box: Box;
|
|
132
|
+
}, message: string): void;
|
|
133
|
+
export {};
|
package/dist/list.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { t } from "./schema.js";
|
|
2
|
+
const LAYOUT_LABELS = {
|
|
3
|
+
auto: { en: 'Automatic', es: 'Automático' },
|
|
4
|
+
'one-line': { en: 'One line', es: 'Una línea' },
|
|
5
|
+
detailed: { en: 'Detailed', es: 'Detallado' },
|
|
6
|
+
};
|
|
7
|
+
const rowsField = (def) => t.integer({
|
|
8
|
+
default: def,
|
|
9
|
+
min: 0,
|
|
10
|
+
max: 30,
|
|
11
|
+
label: { en: 'Rows', es: 'Filas' },
|
|
12
|
+
help: {
|
|
13
|
+
en: '0 = as many as fit. A number shows that many, in rows of a readable size (fewer when the tile is too small for them); the rest are counted in "+N more".',
|
|
14
|
+
es: '0 = las que quepan. Con un número se ven esas, en filas de un tamaño legible (menos si la tesela no da para ellas); el resto se cuenta en «+N más».',
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
export function listOptions(o = {}) {
|
|
18
|
+
const rows = rowsField(o.rows ?? 0);
|
|
19
|
+
if (!o.layouts || o.layouts.length < 2)
|
|
20
|
+
return { rows };
|
|
21
|
+
const values = ['auto', ...o.layouts];
|
|
22
|
+
return {
|
|
23
|
+
rows,
|
|
24
|
+
layout: t.enum(values, {
|
|
25
|
+
default: o.layout ?? 'auto',
|
|
26
|
+
label: { en: 'Row layout', es: 'Formato de fila' },
|
|
27
|
+
labels: Object.fromEntries(values.map((v) => [v, LAYOUT_LABELS[v]])),
|
|
28
|
+
help: { en: 'Automatic: the widget picks by the tile\'s shape.', es: 'Automático: el widget elige según la forma de la tesela.' },
|
|
29
|
+
}),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** Rows may overflow the box by this fraction of a row (the descenders of the last one), as widgets always did. */
|
|
33
|
+
const TOLERANCE = 0.1;
|
|
34
|
+
/** A "+N more" line shorter than this is left out (its text would be under 8 px). */
|
|
35
|
+
const MIN_MORE_H = 12;
|
|
36
|
+
const clamp = (v, lo, hi) => Math.max(lo, Math.min(hi, v));
|
|
37
|
+
/** Height of the "+N more" line under rows of `rowH` px: 60 % of a row, between 18 and 28 px, never more than a row. */
|
|
38
|
+
export function moreHeight(rowH) {
|
|
39
|
+
return Math.min(clamp(Math.round(rowH * 0.6), 18, 28), rowH);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Plans a list of `count` items in `box` from the shared options (`rows`,
|
|
43
|
+
* `layout`) and the widget's row heights. Pure: no drawing, no canvas.
|
|
44
|
+
*
|
|
45
|
+
* - `rows` 0: rows of `box.h / count` (at least `minRows`), clamped to the
|
|
46
|
+
* layout's `min`–`max`; as many as fit.
|
|
47
|
+
* - `rows` N: N items in rows of `box.h / N` clamped the same way, so they
|
|
48
|
+
* never balloon; fewer when N rows don't fit at `min`.
|
|
49
|
+
* - Items left out: a compact "+N more" line (`more`) right under the last
|
|
50
|
+
* row, reserved only then. A box with room for one row but not for the row
|
|
51
|
+
* and that line still gets the row, with the line squeezed or left out.
|
|
52
|
+
*/
|
|
53
|
+
export function listLayout(box, count, options, spec) {
|
|
54
|
+
const declared = Object.keys(spec.sizes);
|
|
55
|
+
const n = Math.max(0, Math.floor(count));
|
|
56
|
+
const fixed = Math.max(0, Math.round(options.rows ?? 0));
|
|
57
|
+
const want = options.layout;
|
|
58
|
+
const layout = want && want !== 'auto' && declared.includes(want) ? want : (spec.auto?.(box, fixed > 0 ? Math.min(fixed, n) : n) ?? declared[0]);
|
|
59
|
+
const size = spec.sizes[layout] ?? spec.sizes[declared[0]];
|
|
60
|
+
const min = Math.max(1, size.min);
|
|
61
|
+
const max = Math.max(min, size.max);
|
|
62
|
+
const h = Math.max(0, box.h);
|
|
63
|
+
const text = (rowH) => {
|
|
64
|
+
const textSize = Math.round(rowH * (layout === 'detailed' ? 0.42 : 0.6));
|
|
65
|
+
return { textSize, detailSize: Math.round(layout === 'detailed' ? rowH * 0.3 : textSize * 0.8) };
|
|
66
|
+
};
|
|
67
|
+
if (h <= 0)
|
|
68
|
+
return { layout, rowH: 0, rows: [], shown: 0, hidden: n, more: null, ...text(0) };
|
|
69
|
+
// Items to show and the rows the height is divided by.
|
|
70
|
+
const target = fixed > 0 ? Math.min(fixed, n) : n;
|
|
71
|
+
const divisor = fixed > 0 ? fixed : Math.max(n, spec.minRows ?? 1, 1);
|
|
72
|
+
const fits = (space, rowH) => Math.floor(space / rowH + TOLERANCE);
|
|
73
|
+
let rowH = clamp(h / divisor, min, max);
|
|
74
|
+
let shown = Math.min(target, fits(h, rowH));
|
|
75
|
+
let more = null;
|
|
76
|
+
if (shown < n) {
|
|
77
|
+
// Items left out: the same rows over a compact "+N more" line.
|
|
78
|
+
let moreH = Math.min(spec.moreH ?? moreHeight(rowH), rowH);
|
|
79
|
+
rowH = clamp((h - moreH) / divisor, min, max);
|
|
80
|
+
shown = Math.min(target, fits(h - moreH, rowH));
|
|
81
|
+
if (shown < 1) {
|
|
82
|
+
// Room for a row but not for the row and the line: the row first (squeezed in a box shorter than one), the line in what's left.
|
|
83
|
+
shown = Math.min(1, n);
|
|
84
|
+
rowH = Math.min(h, min);
|
|
85
|
+
moreH = Math.min(moreH, h - rowH);
|
|
86
|
+
}
|
|
87
|
+
if (moreH >= MIN_MORE_H)
|
|
88
|
+
more = { x: box.x, y: box.y + shown * rowH, w: box.w, h: moreH };
|
|
89
|
+
}
|
|
90
|
+
const rows = Array.from({ length: shown }, (_, i) => ({ x: box.x, y: box.y + i * rowH, w: box.w, h: rowH }));
|
|
91
|
+
return { layout, rowH, rows, shown, hidden: n - shown, more, ...text(rowH) };
|
|
92
|
+
}
|
|
93
|
+
/** `"+3 more"` / `"+3 más"`. */
|
|
94
|
+
export function moreLabel(n, tr) {
|
|
95
|
+
return tr({ en: `+${n} more`, es: `+${n} más` });
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Paints "+N more" in the plan's `more` box, dim, the same in every list.
|
|
99
|
+
* `indent` lines it up with the rows' text (after a status dot, say);
|
|
100
|
+
* `align: 'right'` puts it at the right edge instead.
|
|
101
|
+
*/
|
|
102
|
+
export function drawMore(g, ctx, plan, o = {}) {
|
|
103
|
+
const m = plan.more;
|
|
104
|
+
if (!m || plan.hidden <= 0)
|
|
105
|
+
return;
|
|
106
|
+
const size = Math.max(8, Math.round(m.h * 0.62));
|
|
107
|
+
const indent = o.indent ?? 0;
|
|
108
|
+
const font = { size, family: ctx.theme.fonts.sans, color: ctx.theme.colors.inkDim };
|
|
109
|
+
const text = ctx.kit.truncate(g, moreLabel(plan.hidden, ctx.t), Math.max(1, m.w - indent), font);
|
|
110
|
+
const y = m.y + (m.h + size * 0.7) / 2;
|
|
111
|
+
if (o.align === 'right')
|
|
112
|
+
ctx.kit.drawTracked(g, text, m.x + m.w, y, { ...font, align: 'right' });
|
|
113
|
+
else
|
|
114
|
+
ctx.kit.drawTracked(g, text, m.x + indent, y, font);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The message of an empty list ("Nothing to review"), dim, at the top left
|
|
118
|
+
* of the box, the same in every list. Paint it unless the user asked to hide
|
|
119
|
+
* the widget when it's empty (see `WidgetDefinition.empty`).
|
|
120
|
+
*/
|
|
121
|
+
export function drawEmpty(g, ctx, message) {
|
|
122
|
+
const { box, kit, theme } = ctx;
|
|
123
|
+
const size = Math.max(10, Math.round(Math.min(32, box.h * 0.3)));
|
|
124
|
+
const font = { size, family: theme.fonts.sans, color: theme.colors.inkDim };
|
|
125
|
+
kit.drawTracked(g, kit.truncate(g, message, box.w, font), box.x, box.y + size * 1.1, font);
|
|
126
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The user's place is set once, in Settings, and reaches every plugin as
|
|
3
|
+
* `ctx.location`. A widget or source that can look somewhere else (the
|
|
4
|
+
* weather of another city) offers `locationOptions()`, advanced, and picks
|
|
5
|
+
* with `placeOf`: its own place when both coordinates are set, otherwise the
|
|
6
|
+
* user's.
|
|
7
|
+
*/
|
|
8
|
+
import type { Location } from './plugin.ts';
|
|
9
|
+
import { type NumberField, type StringField } from './schema.ts';
|
|
10
|
+
/** Advanced latitude, longitude and name fields; 0, 0 means "the place in Settings". */
|
|
11
|
+
export declare function locationOptions(): {
|
|
12
|
+
latitude: NumberField;
|
|
13
|
+
longitude: NumberField;
|
|
14
|
+
placeName: StringField;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* The place to use: `own` when it has both coordinates (not 0, 0), with its
|
|
18
|
+
* name or the user's; otherwise the user's (`ctx.location`), which may be
|
|
19
|
+
* unset.
|
|
20
|
+
*/
|
|
21
|
+
export declare function placeOf(own: {
|
|
22
|
+
latitude?: number;
|
|
23
|
+
longitude?: number;
|
|
24
|
+
placeName?: string;
|
|
25
|
+
name?: string;
|
|
26
|
+
} | undefined, user: Location | undefined): Location | undefined;
|
package/dist/location.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { t } from "./schema.js";
|
|
2
|
+
/** Advanced latitude, longitude and name fields; 0, 0 means "the place in Settings". */
|
|
3
|
+
export function locationOptions() {
|
|
4
|
+
return {
|
|
5
|
+
latitude: t.number({
|
|
6
|
+
default: 0,
|
|
7
|
+
min: -90,
|
|
8
|
+
max: 90,
|
|
9
|
+
step: 0.0001,
|
|
10
|
+
unit: '°',
|
|
11
|
+
advanced: true,
|
|
12
|
+
label: { en: 'Latitude', es: 'Latitud' },
|
|
13
|
+
help: { en: 'Leave latitude and longitude at 0 to use the place in Settings.', es: 'Con latitud y longitud a 0 se usa el lugar de los ajustes.' },
|
|
14
|
+
}),
|
|
15
|
+
longitude: t.number({ default: 0, min: -180, max: 180, step: 0.0001, unit: '°', advanced: true, label: { en: 'Longitude', es: 'Longitud' } }),
|
|
16
|
+
placeName: t.string({ advanced: true, placeholder: 'Madrid', label: { en: 'Place name', es: 'Nombre del lugar' }, help: { en: 'Empty: the one in Settings.', es: 'Vacío: el de los ajustes.' } }),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The place to use: `own` when it has both coordinates (not 0, 0), with its
|
|
21
|
+
* name or the user's; otherwise the user's (`ctx.location`), which may be
|
|
22
|
+
* unset.
|
|
23
|
+
*/
|
|
24
|
+
export function placeOf(own, user) {
|
|
25
|
+
const lat = own?.latitude ?? 0;
|
|
26
|
+
const lon = own?.longitude ?? 0;
|
|
27
|
+
const name = (own?.placeName ?? own?.name ?? '').trim();
|
|
28
|
+
if (Number.isFinite(lat) && Number.isFinite(lon) && (lat !== 0 || lon !== 0))
|
|
29
|
+
return { latitude: lat, longitude: lon, ...(name ? { name } : {}) };
|
|
30
|
+
if (!user)
|
|
31
|
+
return undefined;
|
|
32
|
+
return name ? { ...user, name } : user;
|
|
33
|
+
}
|