@kiosko/sdk 0.0.0-stage → 0.1.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.
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Themes are design tokens, not CSS: every widget paints with `ctx.theme`, so
3
+ * switching the theme restyles the whole panel without touching a plugin.
4
+ *
5
+ * The default (`midnight`) is the art direction of the original aimonitor:
6
+ * the normal state is darkness, and colour is spent only on what claims you.
7
+ */
8
+ export interface ThemeColors {
9
+ /** Panel background. Kept near black: it's the backlight that wears out. */
10
+ bg: string;
11
+ /** Card background and its 1 px edge. */
12
+ surface: string;
13
+ surfaceEdge: string;
14
+ /** Text: primary, secondary and hints. */
15
+ ink: string;
16
+ inkDim: string;
17
+ inkFaint: string;
18
+ /** Brand/selection colour. */
19
+ accent: string;
20
+ /** Semaphore shared by every widget: needs you now / soon / informative / fine. */
21
+ alert: string;
22
+ warn: string;
23
+ info: string;
24
+ ok: string;
25
+ }
26
+ export interface ThemeFonts {
27
+ /** Condensed sans: titles, numbers. */
28
+ sans: string;
29
+ sansBold: string;
30
+ mono: string;
31
+ monoBold: string;
32
+ }
33
+ export interface Theme {
34
+ id: string;
35
+ name: string;
36
+ colors: ThemeColors;
37
+ fonts: ThemeFonts;
38
+ /** Card corner radius in px. */
39
+ radius: number;
40
+ /** Card title size in px (scaled for small panels by the compositor). */
41
+ titleSize: number;
42
+ }
43
+ /** Font families registered by the runtime (see core/fonts.ts). */
44
+ export declare const FONT_FAMILIES: ThemeFonts;
45
+ export declare const THEMES: Theme[];
46
+ export declare const DEFAULT_THEME: Theme;
47
+ export declare function themeById(id: string | undefined): Theme;
package/dist/theme.js ADDED
@@ -0,0 +1,49 @@
1
+ /** Font families registered by the runtime (see core/fonts.ts). */
2
+ export const FONT_FAMILIES = {
3
+ sans: 'KioskoSans',
4
+ sansBold: 'KioskoSansBold',
5
+ mono: 'KioskoMono',
6
+ monoBold: 'KioskoMonoBold',
7
+ };
8
+ const base = { fonts: FONT_FAMILIES, radius: 12, titleSize: 30 };
9
+ export const THEMES = [
10
+ {
11
+ id: 'midnight',
12
+ name: 'Midnight',
13
+ ...base,
14
+ colors: { bg: '#05080D', surface: '#0D131A', surfaceEdge: '#1C2631', ink: '#D3DFEA', inkDim: '#627180', inkFaint: '#384450', accent: '#4DA3FF', alert: '#F5453D', warn: '#FFB020', info: '#4DA3FF', ok: '#3FCB7E' },
15
+ },
16
+ {
17
+ id: 'graphite',
18
+ name: 'Graphite',
19
+ ...base,
20
+ radius: 6,
21
+ colors: { bg: '#111214', surface: '#1A1C1F', surfaceEdge: '#2A2D31', ink: '#ECEDEE', inkDim: '#8B9096', inkFaint: '#4A4F55', accent: '#E8E8E8', alert: '#FF5F57', warn: '#FEBC2E', info: '#64B5F6', ok: '#28C840' },
22
+ },
23
+ {
24
+ id: 'aurora',
25
+ name: 'Aurora',
26
+ ...base,
27
+ radius: 16,
28
+ colors: { bg: '#0B0A1A', surface: '#15132B', surfaceEdge: '#2A2550', ink: '#EDE9FF', inkDim: '#8D86B8', inkFaint: '#4A4475', accent: '#9F7AEA', alert: '#FF4D8D', warn: '#FFC857', info: '#4CC9F0', ok: '#43E5A0' },
29
+ },
30
+ {
31
+ id: 'phosphor',
32
+ name: 'Phosphor',
33
+ ...base,
34
+ radius: 2,
35
+ fonts: { ...FONT_FAMILIES, sans: FONT_FAMILIES.mono, sansBold: FONT_FAMILIES.monoBold },
36
+ colors: { bg: '#020602', surface: '#061006', surfaceEdge: '#0F2A0F', ink: '#7CFF7C', inkDim: '#2F9E3A', inkFaint: '#165C1E', accent: '#7CFF7C', alert: '#FF5A3C', warn: '#FFD23C', info: '#5CE1E6', ok: '#7CFF7C' },
37
+ },
38
+ {
39
+ id: 'paper',
40
+ name: 'Paper',
41
+ ...base,
42
+ radius: 10,
43
+ colors: { bg: '#F2EFE8', surface: '#FFFFFF', surfaceEdge: '#DDD8CC', ink: '#1F2328', inkDim: '#6B6F76', inkFaint: '#B4B0A6', accent: '#2F6FEB', alert: '#D92D20', warn: '#C27803', info: '#2F6FEB', ok: '#1E8E3E' },
44
+ },
45
+ ];
46
+ export const DEFAULT_THEME = THEMES[0];
47
+ export function themeById(id) {
48
+ return THEMES.find((t) => t.id === id) ?? DEFAULT_THEME;
49
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * The user's working week, set once in Settings and handed to every plugin
3
+ * as `ctx.workday`: which days, from when to when, how many hours are due,
4
+ * days with other hours (a short Friday) and days off. Plugins default to it
5
+ * instead of asking for their own start, end and holidays.
6
+ */
7
+ /** Other hours on one weekday. Unset fields are the week's. */
8
+ export interface DayHours {
9
+ start?: string;
10
+ end?: string;
11
+ hours?: number;
12
+ }
13
+ export interface Workday {
14
+ /** Working days, 0 = Sunday … 6 = Saturday. */
15
+ days: number[];
16
+ /** `HH:MM`, 24 h. */
17
+ start: string;
18
+ end: string;
19
+ /** Hours due on a working day (what a timesheet expects), not the span from start to end. */
20
+ hours: number;
21
+ /** Days off, `YYYY-MM-DD`: no work hours, nothing due. */
22
+ holidays: string[];
23
+ /** Days with other hours, by weekday (`"5"` = Friday): `{ "5": { "end": "15:00", "hours": 6 } }`. */
24
+ byDay: Partial<Record<string, DayHours>>;
25
+ }
26
+ export declare const DEFAULT_WORKDAY: Workday;
27
+ /** `"09:30"` → 570. Anything unreadable is 0. */
28
+ export declare function minutesOf(hhmm: string | undefined): number;
29
+ /** `YYYY-MM-DD` of the local day `at` falls in. */
30
+ export declare function dayKey(at: number | Date): string;
31
+ export interface DaySchedule {
32
+ /** A working day that is not a holiday. */
33
+ working: boolean;
34
+ holiday: boolean;
35
+ start: string;
36
+ end: string;
37
+ /** Minutes from local midnight. */
38
+ startMin: number;
39
+ endMin: number;
40
+ /** Hours due that day: 0 on days off. */
41
+ hours: number;
42
+ }
43
+ /** The schedule of the local day `at` falls in. */
44
+ export declare function scheduleOn(w: Workday, at: number | Date): DaySchedule;
45
+ /** Inside work hours at `at`: a working day, not a holiday, between its start and end. */
46
+ export declare function isWorkingAt(w: Workday, at: number | Date): boolean;
47
+ /** Hours due in a normal week (no holidays): the sum over the working days. */
48
+ export declare function weeklyHours(w: Workday): number;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The user's working week, set once in Settings and handed to every plugin
3
+ * as `ctx.workday`: which days, from when to when, how many hours are due,
4
+ * days with other hours (a short Friday) and days off. Plugins default to it
5
+ * instead of asking for their own start, end and holidays.
6
+ */
7
+ export const DEFAULT_WORKDAY = { days: [1, 2, 3, 4, 5], start: '09:00', end: '17:00', hours: 8, holidays: [], byDay: {} };
8
+ /** `"09:30"` → 570. Anything unreadable is 0. */
9
+ export function minutesOf(hhmm) {
10
+ const m = /^(\d{1,2}):(\d{2})/.exec(hhmm ?? '');
11
+ return m ? Number(m[1]) * 60 + Number(m[2]) : 0;
12
+ }
13
+ /** `YYYY-MM-DD` of the local day `at` falls in. */
14
+ export function dayKey(at) {
15
+ const d = new Date(at);
16
+ return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
17
+ }
18
+ /** The schedule of the local day `at` falls in. */
19
+ export function scheduleOn(w, at) {
20
+ const day = new Date(at).getDay();
21
+ const own = w.byDay?.[String(day)] ?? {};
22
+ const start = own.start || w.start;
23
+ const end = own.end || w.end;
24
+ const holiday = (w.holidays ?? []).includes(dayKey(at));
25
+ const working = w.days.includes(day) && !holiday;
26
+ return { working, holiday, start, end, startMin: minutesOf(start), endMin: minutesOf(end), hours: working ? (own.hours ?? w.hours ?? 0) : 0 };
27
+ }
28
+ /** Inside work hours at `at`: a working day, not a holiday, between its start and end. */
29
+ export function isWorkingAt(w, at) {
30
+ const s = scheduleOn(w, at);
31
+ if (!s.working)
32
+ return false;
33
+ const d = new Date(at);
34
+ const mins = d.getHours() * 60 + d.getMinutes();
35
+ return mins >= s.startMin && mins < s.endMin;
36
+ }
37
+ /** Hours due in a normal week (no holidays): the sum over the working days. */
38
+ export function weeklyHours(w) {
39
+ return w.days.reduce((sum, d) => sum + (w.byDay?.[String(d)]?.hours ?? w.hours ?? 0), 0);
40
+ }
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@kiosko/sdk",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Plugin SDK for Kiosko: define widgets, data sources and interludes for small always-on LCD panels.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "exports": {
8
+ ".": {
9
+ "source": "./src/index.ts",
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./workday": {
14
+ "source": "./src/workday.ts",
15
+ "types": "./dist/workday.d.ts",
16
+ "default": "./dist/workday.js"
17
+ },
18
+ "./format": {
19
+ "source": "./src/format.ts",
20
+ "types": "./dist/format.d.ts",
21
+ "default": "./dist/format.js"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist",
26
+ "src",
27
+ "!src/**/*.test.ts",
28
+ "README.md",
29
+ "LICENSE"
30
+ ],
31
+ "engines": {
32
+ "node": ">=22"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc -p tsconfig.json",
36
+ "check": "tsc -p tsconfig.json --noEmit",
37
+ "prepublishOnly": "npm run build"
38
+ },
39
+ "peerDependencies": {
40
+ "@napi-rs/canvas": ">=0.1.65 <2",
41
+ "@loksly/lcd-canvas-kit": "*"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/kiosko-lcd/kiosko.git",
46
+ "directory": "packages/sdk"
47
+ },
48
+ "author": "Loksly"
49
+ }
package/src/context.ts ADDED
@@ -0,0 +1,25 @@
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, type TimeFormat } from './format.ts';
9
+ import { localize, type Localized } from './i18n.ts';
10
+ import type { BaseContext, Location, Logger } from './plugin.ts';
11
+ import { DEFAULT_WORKDAY, type Workday } from './workday.ts';
12
+
13
+ export function createBaseContext(o: { now: number; locale?: string; timeFormat?: TimeFormat; workday?: Workday; location?: Location; log?: Logger; dataDir?: string }): BaseContext {
14
+ const locale = o.locale ?? 'en';
15
+ return {
16
+ now: o.now,
17
+ locale,
18
+ t: (text: Localized) => localize(text, locale),
19
+ fmt: createFormatters({ locale, timeFormat: o.timeFormat ?? 'auto', now: o.now }),
20
+ log: o.log ?? (() => undefined),
21
+ dataDir: o.dataDir ?? '',
22
+ ...(o.location ? { location: o.location } : {}),
23
+ workday: o.workday ?? DEFAULT_WORKDAY,
24
+ };
25
+ }
package/src/filter.ts ADDED
@@ -0,0 +1,56 @@
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
+
17
+ /** A filter: patterns as a list, or one text with commas or line breaks between them. */
18
+ export type Filter = readonly string[] | string;
19
+
20
+ const cache = new Map<string, RegExp>();
21
+ function globRe(pattern: string): RegExp {
22
+ let re = cache.get(pattern);
23
+ if (!re) {
24
+ const body = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.');
25
+ re = new RegExp(`^${body}$`, 'i');
26
+ if (cache.size > 500) cache.clear();
27
+ cache.set(pattern, re);
28
+ }
29
+ return re;
30
+ }
31
+
32
+ /** The patterns of a filter, trimmed, without empty ones. */
33
+ export function filterPatterns(filter: Filter | undefined): string[] {
34
+ const list = typeof filter === 'string' ? filter.split(/[,\n]/) : (filter ?? []);
35
+ return list.map((p) => p.trim()).filter((p) => p && p !== '!');
36
+ }
37
+
38
+ /**
39
+ * Whether `value` passes `filter`. `value` may be several names of the same
40
+ * item (a repo's full name and its alias): it passes if any of them matches
41
+ * an include pattern and none matches an exclude one. An empty filter (or
42
+ * one with only exclusions) lets everything else through.
43
+ */
44
+ export function matchFilter(value: string | readonly string[], filter: Filter | undefined): boolean {
45
+ const patterns = filterPatterns(filter);
46
+ if (!patterns.length) return true;
47
+ const names = (typeof value === 'string' ? [value] : [...value]).flatMap((v) => {
48
+ const last = v.split('/').pop() ?? v;
49
+ return last === v ? [v] : [v, last];
50
+ });
51
+ const hit = (p: string) => names.some((n) => globRe(p).test(n));
52
+ const includes = patterns.filter((p) => !p.startsWith('!'));
53
+ const excludes = patterns.filter((p) => p.startsWith('!')).map((p) => p.slice(1).trim());
54
+ if (excludes.some(hit)) return false;
55
+ return !includes.length || includes.some(hit);
56
+ }
package/src/format.ts ADDED
@@ -0,0 +1,123 @@
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
+
12
+ /** How clock times are written: `auto` follows the language (24 h in Spanish, 12 h in US English). */
13
+ export type TimeFormat = 'auto' | '24h' | '12h';
14
+ export const TIME_FORMATS: readonly TimeFormat[] = ['auto', '24h', '12h'];
15
+
16
+ export interface Formatters {
17
+ /** True when clock times are written on a 12-hour clock. */
18
+ readonly hour12: boolean;
19
+ /**
20
+ * A clock time: `"09:05"` on a 24-hour clock, `"9:05 AM"` (`"9:05 a. m."`)
21
+ * on a 12-hour one. `seconds` adds `":07"`; `ampm: false` leaves out
22
+ * AM/PM (a big clock that paints it apart).
23
+ */
24
+ time(at: number | Date, o?: { seconds?: boolean; ampm?: boolean }): string;
25
+ /**
26
+ * How long ago `at` (epoch ms, a Date or an ISO string) was, from `ctx.now`:
27
+ * `"just now"`, `"5 min ago"`, `"3 h ago"`, `"4 d ago"`, `"2 wk ago"`,
28
+ * `"1 yr ago"` (`"hace 5 min"`…). `suffix: false` gives the bare age for
29
+ * a meta line: `"now"`, `"5 min"`, `"3 h"`. A future `at` is "now".
30
+ */
31
+ ago(at: number | Date | string, o?: { suffix?: boolean; now?: number }): string;
32
+ /**
33
+ * A length of time: `"45 s"` (only with `seconds`), `"12 min"`,
34
+ * `"1 h 05"`, `"2 d 3 h"`. Rounded down by default; `round: 'up'` for
35
+ * countdowns ("in 0 min" would be a lie until it starts). `compact`
36
+ * drops a zero remainder: `"1 h"` rather than `"1 h 00"`, `"2 d"` rather
37
+ * than `"2 d 0 h"` (a span such as "the last 1 h", not a running timer).
38
+ */
39
+ duration(ms: number, o?: { seconds?: boolean; round?: 'down' | 'up' | 'nearest'; compact?: boolean }): string;
40
+ }
41
+
42
+ interface Words {
43
+ now: string;
44
+ justNow: string;
45
+ ago: (v: string) => string;
46
+ wk: string;
47
+ yr: (n: number) => string;
48
+ am: string;
49
+ pm: string;
50
+ }
51
+
52
+ const WORDS: Record<string, Words> = {
53
+ en: { now: 'now', justNow: 'just now', ago: (v) => `${v} ago`, wk: 'wk', yr: () => 'yr', am: 'AM', pm: 'PM' },
54
+ 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.' },
55
+ };
56
+
57
+ const langOf = (locale: string) => locale.toLowerCase().split(/[-_]/)[0] ?? 'en';
58
+ const wordsFor = (locale: string): Words => WORDS[langOf(locale)] ?? WORDS.en!;
59
+ const pad2 = (n: number) => String(n).padStart(2, '0');
60
+
61
+ const cycles = new Map<string, boolean>();
62
+
63
+ /** Whether `locale` writes times on a 12-hour clock, per `timeFormat`. */
64
+ export function usesHour12(locale: string, timeFormat: TimeFormat = 'auto'): boolean {
65
+ if (timeFormat !== 'auto') return timeFormat === '12h';
66
+ let h12 = cycles.get(locale);
67
+ if (h12 === undefined) {
68
+ try {
69
+ const cycle = new Intl.DateTimeFormat(locale, { hour: 'numeric' }).resolvedOptions().hourCycle;
70
+ h12 = cycle === 'h11' || cycle === 'h12';
71
+ } catch {
72
+ h12 = false;
73
+ }
74
+ cycles.set(locale, h12);
75
+ }
76
+ return h12;
77
+ }
78
+
79
+ function toMs(at: number | Date | string): number {
80
+ if (typeof at === 'number') return at;
81
+ if (typeof at === 'string') return Date.parse(at);
82
+ return at.getTime();
83
+ }
84
+
85
+ /**
86
+ * The formatters of a context. Kiosko builds them for every plugin context;
87
+ * plugin tests can build their own: `createFormatters({ locale: 'es', now })`.
88
+ */
89
+ export function createFormatters(o: { locale: string; timeFormat?: TimeFormat; now: number }): Formatters {
90
+ const w = wordsFor(o.locale);
91
+ const hour12 = usesHour12(o.locale, o.timeFormat);
92
+ return {
93
+ hour12,
94
+ time(at, opts = {}) {
95
+ const d = new Date(toMs(at));
96
+ const h = d.getHours();
97
+ const rest = `:${pad2(d.getMinutes())}${opts.seconds ? `:${pad2(d.getSeconds())}` : ''}`;
98
+ if (!hour12) return `${pad2(h)}${rest}`;
99
+ const base = `${h % 12 || 12}${rest}`;
100
+ return opts.ampm === false ? base : `${base} ${h < 12 ? w.am : w.pm}`;
101
+ },
102
+ ago(at, opts = {}) {
103
+ const suffix = opts.suffix !== false;
104
+ const ms = (opts.now ?? o.now) - toMs(at);
105
+ const min = Math.floor(ms / 60_000);
106
+ if (!Number.isFinite(min) || min < 1) return suffix ? w.justNow : w.now;
107
+ const h = Math.floor(min / 60);
108
+ const d = Math.floor(h / 24);
109
+ 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))}`;
110
+ return suffix ? w.ago(v) : v;
111
+ },
112
+ duration(ms, opts = {}) {
113
+ const round = opts.round === 'up' ? Math.ceil : opts.round === 'nearest' ? Math.round : Math.floor;
114
+ const safe = Number.isFinite(ms) ? Math.max(0, ms) : 0;
115
+ if (opts.seconds && safe < 60_000) return `${round(safe / 1000)} s`;
116
+ const min = round(safe / 60_000);
117
+ if (min < 60) return `${min} min`;
118
+ const h = Math.floor(min / 60);
119
+ if (h < 24) return opts.compact && min % 60 === 0 ? `${h} h` : `${h} h ${pad2(min % 60)}`;
120
+ return opts.compact && h % 24 === 0 ? `${h / 24} d` : `${Math.floor(h / 24)} d ${h % 24} h`;
121
+ },
122
+ };
123
+ }
package/src/i18n.ts ADDED
@@ -0,0 +1,13 @@
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 | ({ en: string } & Partial<Record<string, string>>);
7
+
8
+ export function localize(text: Localized | undefined, locale = 'en'): string {
9
+ if (text === undefined) return '';
10
+ if (typeof text === 'string') return text;
11
+ const lang = locale.toLowerCase().split(/[-_]/)[0]!;
12
+ return text[locale] ?? text[lang] ?? text.en ?? Object.values(text).find((v) => typeof v === 'string') ?? '';
13
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
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';