@netnodeag/kraftwerk 0.82.0 → 0.83.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.
Files changed (77) hide show
  1. package/README.md +175 -0
  2. package/dist/cli/init.js +1 -0
  3. package/dist/cli/kraftwerk.js +3 -0
  4. package/dist/cli/presentations.d.ts +2 -0
  5. package/dist/cli/presentations.js +261 -0
  6. package/dist/client/routes.d.ts +18 -0
  7. package/dist/client/routes.js +18 -0
  8. package/dist/config.d.ts +16 -0
  9. package/dist/config.js +10 -2
  10. package/dist/core/boards.d.ts +14 -1
  11. package/dist/core/boards.js +34 -1
  12. package/dist/core/chat/context.js +11 -2
  13. package/dist/core/git.js +3 -1
  14. package/dist/core/presentations/browser.d.ts +43 -0
  15. package/dist/core/presentations/browser.js +53 -0
  16. package/dist/core/presentations/builtin.d.ts +3 -0
  17. package/dist/core/presentations/builtin.js +142 -0
  18. package/dist/core/presentations/chart.d.ts +34 -0
  19. package/dist/core/presentations/chart.js +159 -0
  20. package/dist/core/presentations/deck.d.ts +63 -0
  21. package/dist/core/presentations/deck.js +198 -0
  22. package/dist/core/presentations/export.d.ts +21 -0
  23. package/dist/core/presentations/export.js +75 -0
  24. package/dist/core/presentations/html.d.ts +135 -0
  25. package/dist/core/presentations/html.js +389 -0
  26. package/dist/core/presentations/index.d.ts +252 -0
  27. package/dist/core/presentations/index.js +753 -0
  28. package/dist/core/presentations/layout.d.ts +47 -0
  29. package/dist/core/presentations/layout.js +147 -0
  30. package/dist/core/presentations/markdown.d.ts +49 -0
  31. package/dist/core/presentations/markdown.js +158 -0
  32. package/dist/core/presentations/merge.d.ts +26 -0
  33. package/dist/core/presentations/merge.js +28 -0
  34. package/dist/core/presentations/presence.d.ts +31 -0
  35. package/dist/core/presentations/presence.js +131 -0
  36. package/dist/core/presentations/render.d.ts +33 -0
  37. package/dist/core/presentations/render.js +318 -0
  38. package/dist/core/presentations/slots.d.ts +33 -0
  39. package/dist/core/presentations/slots.js +114 -0
  40. package/dist/core/presentations/template.d.ts +253 -0
  41. package/dist/core/presentations/template.js +141 -0
  42. package/dist/core/presentations/versions.d.ts +30 -0
  43. package/dist/core/presentations/versions.js +75 -0
  44. package/dist/core/refs.d.ts +1 -1
  45. package/dist/core/refs.js +9 -1
  46. package/dist/core/search.d.ts +10 -0
  47. package/dist/core/search.js +6 -0
  48. package/dist/core/settings.d.ts +2 -0
  49. package/dist/core/settings.js +12 -1
  50. package/dist/core/trash.d.ts +1 -1
  51. package/dist/server/api/index.d.ts +62 -1
  52. package/dist/server/api/index.js +2 -0
  53. package/dist/server/api/presentations.d.ts +66 -0
  54. package/dist/server/api/presentations.js +185 -0
  55. package/dist/server/api/socket.js +24 -1
  56. package/dist/server/api/workspace.d.ts +5 -0
  57. package/dist/server/api/workspace.js +2 -0
  58. package/package.json +6 -2
  59. package/schema/kraftwerk.schema.json +18 -0
  60. package/web/dist/assets/{dist-DvkrCSGL.js → dist-5-ibIscm.js} +1 -1
  61. package/web/dist/assets/{dist-Bie1ebP9.js → dist-5lgY5B-M.js} +1 -1
  62. package/web/dist/assets/{dist-BuwdjX6z.js → dist-BJaPgnhj.js} +1 -1
  63. package/web/dist/assets/{dist-C_rJjRj7.js → dist-Bar1_NBq.js} +1 -1
  64. package/web/dist/assets/{dist-DtUB2Uu2.js → dist-BxCxboU8.js} +1 -1
  65. package/web/dist/assets/dist-CDitSY16.js +1 -0
  66. package/web/dist/assets/{dist-C16bXf8c.js → dist-CRXT8IL6.js} +1 -1
  67. package/web/dist/assets/{dist-CH73jsgO.js → dist-CXRjiHpl.js} +1 -1
  68. package/web/dist/assets/{dist-BBFUIftm.js → dist-CpnB7MfK.js} +1 -1
  69. package/web/dist/assets/{dist-C0ve4Tka.js → dist-D1VK1YdB.js} +1 -1
  70. package/web/dist/assets/{dist-CDUZdAAS.js → dist-D52LvyO8.js} +1 -1
  71. package/web/dist/assets/{editor-3z92i0tV.js → editor-HCNTXWWs.js} +3 -3
  72. package/web/dist/assets/index-Dnlu16AL.css +2 -0
  73. package/web/dist/assets/index-kwWnQOhD.js +473 -0
  74. package/web/dist/index.html +2 -2
  75. package/web/dist/assets/dist-DYICNp09.js +0 -1
  76. package/web/dist/assets/index-DRhNELK_.js +0 -232
  77. package/web/dist/assets/index-unGEux3Y.css +0 -2
@@ -0,0 +1,47 @@
1
+ import type { LayoutDef, SlotDef, Template } from "./template.js";
2
+ /**
3
+ * The pure half of the template engine: canvas, colors, which layout and
4
+ * which slots a slide really has. No imports at runtime — the web editor
5
+ * bundles this file (and html.ts) to draw slides itself; template.ts keeps
6
+ * the schema and the files.
7
+ */
8
+ export declare const SLOT_KINDS: readonly ["text", "markdown", "image", "chart", "table", "html", "embed"];
9
+ export type SlotKind = (typeof SLOT_KINDS)[number];
10
+ /** Canvas in inches per aspect ratio. */
11
+ export declare const CANVAS: Record<"16:9" | "4:3", {
12
+ w: number;
13
+ h: number;
14
+ }>;
15
+ /** The HTML stage: 1600 px wide whatever the ratio, so 160 px per inch. */
16
+ export declare const STAGE_W = 1600;
17
+ export declare const PPI: number;
18
+ /** The colors every template has, whatever it names itself. */
19
+ export declare const BASE_COLORS: Record<string, string>;
20
+ /** The layout a slide uses: its own, else the template's default, else the first. */
21
+ export declare function layoutFor(template: Pick<Template, "layouts" | "default">, name: string | undefined): {
22
+ name: string;
23
+ layout: LayoutDef;
24
+ } | null;
25
+ /** Where a slide's unmarked content (after its heading) goes: the layout's `default`, else its markdown slot, else its required table/chart/picture slot, else its only other slot, else a text slot. */
26
+ export declare function defaultSlotOf(layout: LayoutDef): string | undefined;
27
+ /** A color name of the template resolved to #RRGGBB (a #hex passes through). Unknown names: null. */
28
+ export declare function resolveColor(template: Pick<Template, "colors">, value: string | undefined, depth?: number): string | null;
29
+ /** The template's named colors resolved to #RRGGBB, the base names included. */
30
+ export declare function templateColors(template: Pick<Template, "colors">): Record<string, string>;
31
+ /** A font key (`heading`, `body`) resolved to the family; a family name passes through. */
32
+ export declare const resolveFont: (template: Pick<Template, "fonts">, value: string | undefined) => string;
33
+ /** What a slide may change of a slot (`slots:` in its YAML head). */
34
+ export type SlotPatch = Partial<Pick<SlotDef, "kind" | "x" | "y" | "w" | "h" | "size" | "bold" | "italic" | "color" | "font" | "align" | "valign" | "fill" | "underline" | "strike" | "rotate" | "radius" | "border" | "borderColor" | "shadow" | "opacity" | "z" | "fit" | "class">>;
35
+ /**
36
+ * The slots a slide really has: its layout's, each with the slide's own
37
+ * changes (`slots: {title: {y: 1.2, size: 44}}` moves and resizes the
38
+ * title on this slide only), plus the slots the slide adds itself
39
+ * (`slots: {text1: {kind: markdown, x, y, w, h}}`, filled by `::text1::`).
40
+ */
41
+ export declare function slideSlots(layout: LayoutDef, overrides: unknown): {
42
+ slots: Record<string, SlotDef & {
43
+ own?: boolean;
44
+ changed?: boolean;
45
+ }>;
46
+ problems: string[];
47
+ };
@@ -0,0 +1,147 @@
1
+ /**
2
+ * The pure half of the template engine: canvas, colors, which layout and
3
+ * which slots a slide really has. No imports at runtime — the web editor
4
+ * bundles this file (and html.ts) to draw slides itself; template.ts keeps
5
+ * the schema and the files.
6
+ */
7
+ export const SLOT_KINDS = ["text", "markdown", "image", "chart", "table", "html", "embed"];
8
+ /** Canvas in inches per aspect ratio. */
9
+ export const CANVAS = { "16:9": { w: 10, h: 5.625 }, "4:3": { w: 10, h: 7.5 } };
10
+ /** The HTML stage: 1600 px wide whatever the ratio, so 160 px per inch. */
11
+ export const STAGE_W = 1600;
12
+ export const PPI = STAGE_W / 10;
13
+ /** The colors every template has, whatever it names itself. */
14
+ export const BASE_COLORS = {
15
+ background: "#F9F9F7",
16
+ surface: "#FFFFFF",
17
+ text: "#161310",
18
+ muted: "#6F6D67",
19
+ accent: "#0284C7",
20
+ onAccent: "#FFFFFF",
21
+ line: "#E1E0D9",
22
+ };
23
+ /** The layout a slide uses: its own, else the template's default, else the first. */
24
+ export function layoutFor(template, name) {
25
+ const key = name ?? template.default ?? (template.layouts.content ? "content" : Object.keys(template.layouts)[0]);
26
+ const layout = key ? template.layouts[key] : undefined;
27
+ return layout ? { name: key, layout } : null;
28
+ }
29
+ /** Where a slide's unmarked content (after its heading) goes: the layout's `default`, else its markdown slot, else its required table/chart/picture slot, else its only other slot, else a text slot. */
30
+ export function defaultSlotOf(layout) {
31
+ if (layout.default)
32
+ return layout.default;
33
+ const others = Object.entries(layout.slots).filter(([k]) => k !== "title");
34
+ return others.find(([, v]) => v.kind === "markdown")?.[0] ?? others.find(([, v]) => v.required && v.kind !== "text")?.[0] ?? (others.length === 1 ? others[0][0] : undefined) ?? others.find(([, v]) => v.kind === "text")?.[0];
35
+ }
36
+ /** A color name of the template resolved to #RRGGBB (a #hex passes through). Unknown names: null. */
37
+ export function resolveColor(template, value, depth = 0) {
38
+ if (!value || depth > 4)
39
+ return null;
40
+ const v = value.trim();
41
+ if (/^#?[0-9a-f]{6}$/i.test(v))
42
+ return v.startsWith("#") ? v.toUpperCase() : `#${v.toUpperCase()}`;
43
+ if (/^#[0-9a-f]{3}$/i.test(v))
44
+ return `#${v.slice(1).split("").map((c) => c + c).join("")}`.toUpperCase();
45
+ const named = template.colors[v] ?? BASE_COLORS[v];
46
+ return named ? resolveColor(template, named, depth + 1) : null;
47
+ }
48
+ /** The template's named colors resolved to #RRGGBB, the base names included. */
49
+ export function templateColors(template) {
50
+ const out = {};
51
+ for (const name of new Set([...Object.keys(BASE_COLORS), ...Object.keys(template.colors)])) {
52
+ const hex = resolveColor(template, name);
53
+ if (hex)
54
+ out[name] = hex;
55
+ }
56
+ return out;
57
+ }
58
+ /** A font key (`heading`, `body`) resolved to the family; a family name passes through. */
59
+ export const resolveFont = (template, value) => (value === "heading" ? template.fonts.heading : value === "body" || !value ? template.fonts.body : value);
60
+ const PATCH_KEYS = {
61
+ kind: "kind",
62
+ x: "number",
63
+ y: "number",
64
+ w: "positive",
65
+ h: "positive",
66
+ size: "positive",
67
+ bold: "boolean",
68
+ italic: "boolean",
69
+ color: "string",
70
+ font: "string",
71
+ align: "align",
72
+ valign: "valign",
73
+ fill: "string",
74
+ class: "string",
75
+ underline: "boolean",
76
+ strike: "boolean",
77
+ rotate: "number",
78
+ radius: "number",
79
+ border: "number",
80
+ borderColor: "string",
81
+ shadow: "boolean",
82
+ opacity: "number",
83
+ z: "number",
84
+ fit: "string",
85
+ };
86
+ function checkPatch(raw) {
87
+ if (raw === null || raw === undefined)
88
+ return { patch: {} };
89
+ if (typeof raw !== "object" || Array.isArray(raw))
90
+ return { patch: {}, problem: "must be a mapping" };
91
+ const patch = {};
92
+ for (const [k, v] of Object.entries(raw)) {
93
+ const t = PATCH_KEYS[k];
94
+ if (!t)
95
+ return { patch: {}, problem: `unknown key "${k}" (one of ${Object.keys(PATCH_KEYS).join(", ")})` };
96
+ const ok = t === "number"
97
+ ? typeof v === "number" && Number.isFinite(v)
98
+ : t === "positive"
99
+ ? typeof v === "number" && v > 0
100
+ : t === "boolean"
101
+ ? typeof v === "boolean"
102
+ : t === "string"
103
+ ? typeof v === "string"
104
+ : t === "kind"
105
+ ? SLOT_KINDS.includes(v)
106
+ : t === "align"
107
+ ? ["left", "center", "right"].includes(v)
108
+ : ["top", "middle", "bottom"].includes(v);
109
+ if (!ok)
110
+ return { patch: {}, problem: `${k}: ${JSON.stringify(v)} is not valid` };
111
+ patch[k] = v;
112
+ }
113
+ return { patch: patch };
114
+ }
115
+ /**
116
+ * The slots a slide really has: its layout's, each with the slide's own
117
+ * changes (`slots: {title: {y: 1.2, size: 44}}` moves and resizes the
118
+ * title on this slide only), plus the slots the slide adds itself
119
+ * (`slots: {text1: {kind: markdown, x, y, w, h}}`, filled by `::text1::`).
120
+ */
121
+ export function slideSlots(layout, overrides) {
122
+ const slots = { ...layout.slots };
123
+ const problems = [];
124
+ if (overrides === undefined || overrides === null)
125
+ return { slots, problems };
126
+ if (typeof overrides !== "object" || Array.isArray(overrides))
127
+ return { slots, problems: ["`slots` must be a mapping of slot name to its changes"] };
128
+ for (const [name, raw] of Object.entries(overrides)) {
129
+ if (!/^[a-z][a-z0-9_-]*$/i.test(name)) {
130
+ problems.push(`slot name "${name}" is not valid`);
131
+ continue;
132
+ }
133
+ const { patch, problem } = checkPatch(raw);
134
+ if (problem) {
135
+ problems.push(`slots.${name}: ${problem}`);
136
+ continue;
137
+ }
138
+ const base = layout.slots[name];
139
+ if (base)
140
+ slots[name] = { ...base, ...patch, changed: true };
141
+ else if (patch.x === undefined || patch.y === undefined || patch.w === undefined || patch.h === undefined)
142
+ problems.push(`slots.${name}: a slot the layout does not have needs x, y, w and h`);
143
+ else
144
+ slots[name] = { kind: patch.kind ?? "text", ...patch, own: true };
145
+ }
146
+ return { slots, problems };
147
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The markdown a slot holds, read into blocks the renderer can place on a
3
+ * slide: paragraphs and headings as runs, lists as items with a level, an
4
+ * image, a table, a fenced `chart` block. The subset is what a slide can
5
+ * show — anything else (blockquotes, html, horizontal rules) becomes a
6
+ * paragraph of its text, so nothing silently disappears.
7
+ */
8
+ export interface Run {
9
+ text: string;
10
+ bold?: boolean;
11
+ italic?: boolean;
12
+ code?: boolean;
13
+ href?: string;
14
+ }
15
+ export interface ListItem {
16
+ level: number;
17
+ ordered: boolean;
18
+ runs: Run[];
19
+ }
20
+ export type Block = {
21
+ kind: "heading";
22
+ level: number;
23
+ runs: Run[];
24
+ } | {
25
+ kind: "paragraph";
26
+ runs: Run[];
27
+ } | {
28
+ kind: "list";
29
+ items: ListItem[];
30
+ } | {
31
+ kind: "image";
32
+ src: string;
33
+ alt: string;
34
+ } | {
35
+ kind: "table";
36
+ rows: Run[][][];
37
+ } | {
38
+ kind: "chart";
39
+ source: string;
40
+ } | {
41
+ kind: "code";
42
+ lang: string;
43
+ text: string;
44
+ };
45
+ export declare function parseMarkdown(md: string): Block[];
46
+ /** The runs of a one-line slot (a title): the first block's runs, headings included. */
47
+ export declare function inlineRuns(md: string): Run[];
48
+ /** The plain text of runs. */
49
+ export declare const plain: (runs: Run[]) => string;
@@ -0,0 +1,158 @@
1
+ import { lexer } from "marked";
2
+ export function parseMarkdown(md) {
3
+ const out = [];
4
+ for (const t of lexer(md, { gfm: true }))
5
+ out.push(...blockOf(t));
6
+ return out;
7
+ }
8
+ /** The runs of a one-line slot (a title): the first block's runs, headings included. */
9
+ export function inlineRuns(md) {
10
+ const runs = [];
11
+ for (const b of parseMarkdown(md)) {
12
+ if (b.kind === "heading" || b.kind === "paragraph") {
13
+ if (runs.length)
14
+ runs.push({ text: " " });
15
+ runs.push(...b.runs);
16
+ }
17
+ else if (b.kind === "list") {
18
+ for (const item of b.items) {
19
+ if (runs.length)
20
+ runs.push({ text: " " });
21
+ runs.push(...item.runs);
22
+ }
23
+ }
24
+ }
25
+ return runs;
26
+ }
27
+ /** The plain text of runs. */
28
+ export const plain = (runs) => runs.map((r) => r.text).join("");
29
+ function blockOf(t) {
30
+ switch (t.type) {
31
+ case "heading":
32
+ return [{ kind: "heading", level: t.depth, runs: inline(t.tokens) }];
33
+ case "paragraph": {
34
+ const p = t;
35
+ const only = p.tokens.filter((x) => !(x.type === "text" && !x.text.trim()));
36
+ if (only.length === 1 && only[0].type === "image") {
37
+ const img = only[0];
38
+ return [{ kind: "image", src: img.href, alt: img.text ?? "" }];
39
+ }
40
+ return [{ kind: "paragraph", runs: inline(p.tokens) }];
41
+ }
42
+ case "list":
43
+ return [{ kind: "list", items: listItems(t, 0) }];
44
+ case "table": {
45
+ const tb = t;
46
+ const rows = [tb.header.map((c) => inline(c.tokens)), ...tb.rows.map((r) => r.map((c) => inline(c.tokens)))];
47
+ return [{ kind: "table", rows }];
48
+ }
49
+ case "code": {
50
+ const c = t;
51
+ if ((c.lang ?? "").trim().toLowerCase() === "chart")
52
+ return [{ kind: "chart", source: c.text }];
53
+ return [{ kind: "code", lang: (c.lang ?? "").trim(), text: c.text }];
54
+ }
55
+ case "blockquote":
56
+ return t.tokens.flatMap(blockOf);
57
+ case "html":
58
+ case "text": {
59
+ const text = (t.text ?? "").trim();
60
+ return text ? [{ kind: "paragraph", runs: [{ text }] }] : [];
61
+ }
62
+ case "space":
63
+ case "hr":
64
+ case "def":
65
+ return [];
66
+ default: {
67
+ const text = ("raw" in t ? String(t.raw) : "").trim();
68
+ return text ? [{ kind: "paragraph", runs: [{ text }] }] : [];
69
+ }
70
+ }
71
+ }
72
+ function listItems(list, level) {
73
+ const out = [];
74
+ for (const item of list.items) {
75
+ const runs = [];
76
+ const nested = [];
77
+ for (const t of item.tokens) {
78
+ if (t.type === "list")
79
+ nested.push(...listItems(t, level + 1));
80
+ else if (t.type === "text" || t.type === "paragraph") {
81
+ if (runs.length)
82
+ runs.push({ text: " " });
83
+ runs.push(...inline(t.tokens ?? [{ type: "text", raw: t.text, text: t.text }]));
84
+ }
85
+ else {
86
+ for (const b of blockOf(t)) {
87
+ if (b.kind === "paragraph" || b.kind === "heading") {
88
+ if (runs.length)
89
+ runs.push({ text: " " });
90
+ runs.push(...b.runs);
91
+ }
92
+ }
93
+ }
94
+ }
95
+ out.push({ level, ordered: !!list.ordered, runs });
96
+ out.push(...nested);
97
+ }
98
+ return out;
99
+ }
100
+ function inline(tokens, base = {}) {
101
+ const runs = [];
102
+ for (const t of tokens ?? []) {
103
+ switch (t.type) {
104
+ case "text":
105
+ case "escape": {
106
+ const text = unescapeHtml(t.text);
107
+ if (t.tokens?.length)
108
+ runs.push(...inline(t.tokens, base));
109
+ else if (text)
110
+ runs.push({ ...base, text });
111
+ break;
112
+ }
113
+ case "strong":
114
+ runs.push(...inline(t.tokens, { ...base, bold: true }));
115
+ break;
116
+ case "em":
117
+ runs.push(...inline(t.tokens, { ...base, italic: true }));
118
+ break;
119
+ case "del":
120
+ runs.push(...inline(t.tokens, base));
121
+ break;
122
+ case "codespan":
123
+ runs.push({ ...base, code: true, text: unescapeHtml(t.text) });
124
+ break;
125
+ case "link": {
126
+ const l = t;
127
+ runs.push(...inline(l.tokens, { ...base, href: l.href }));
128
+ break;
129
+ }
130
+ case "image":
131
+ runs.push({ ...base, text: t.text || t.href });
132
+ break;
133
+ case "br":
134
+ runs.push({ ...base, text: "\n" });
135
+ break;
136
+ case "html":
137
+ runs.push({ ...base, text: t.text });
138
+ break;
139
+ default:
140
+ if ("raw" in t && t.raw)
141
+ runs.push({ ...base, text: String(t.raw) });
142
+ }
143
+ }
144
+ return mergeRuns(runs);
145
+ }
146
+ /** Adjacent runs with the same format become one, so a sentence is one run, not a run per word. */
147
+ function mergeRuns(runs) {
148
+ const out = [];
149
+ for (const r of runs) {
150
+ const last = out[out.length - 1];
151
+ if (last && !!last.bold === !!r.bold && !!last.italic === !!r.italic && !!last.code === !!r.code && last.href === r.href)
152
+ last.text += r.text;
153
+ else
154
+ out.push({ ...r });
155
+ }
156
+ return out.filter((r) => r.text !== "");
157
+ }
158
+ const unescapeHtml = (s) => s.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'");
@@ -0,0 +1,26 @@
1
+ import type { SlideDef } from "./deck.js";
2
+ /**
3
+ * Two editors on one deck (a person and an agent, two windows): the deck as
4
+ * both last saw it (`base`), this side's version (`mine`) and the one that
5
+ * reached the file first (`theirs`). Pure, so the web editor and the tests
6
+ * share it.
7
+ */
8
+ export interface DeckDraft {
9
+ title: string;
10
+ template: string;
11
+ css: string;
12
+ /** false: the deck shows no slide numbers. */
13
+ numbers?: boolean;
14
+ slides: SlideDef[];
15
+ }
16
+ type Draft = DeckDraft;
17
+ /**
18
+ * Someone else saved the deck while this editor had changes (an agent, the
19
+ * CLI, another window): their version plus what was changed here, slide by
20
+ * slide — a slide changed on both sides keeps theirs and is reported.
21
+ */
22
+ export declare function mergeDrafts(base: Draft, mine: Draft, theirs: Draft): {
23
+ draft: Draft;
24
+ lost: number[];
25
+ };
26
+ export {};
@@ -0,0 +1,28 @@
1
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
2
+ /**
3
+ * Someone else saved the deck while this editor had changes (an agent, the
4
+ * CLI, another window): their version plus what was changed here, slide by
5
+ * slide — a slide changed on both sides keeps theirs and is reported.
6
+ */
7
+ export function mergeDrafts(base, mine, theirs) {
8
+ const pick = (b, m, t) => (same(m, b) ? t : m);
9
+ const head = { title: pick(base.title, mine.title, theirs.title), template: pick(base.template, mine.template, theirs.template), css: pick(base.css, mine.css, theirs.css), numbers: pick(base.numbers, mine.numbers, theirs.numbers) };
10
+ if (same(base.slides, theirs.slides))
11
+ return { draft: { ...head, slides: mine.slides }, lost: [] };
12
+ if (same(base.slides, mine.slides))
13
+ return { draft: { ...head, slides: theirs.slides }, lost: [] };
14
+ const lost = [];
15
+ if (mine.slides.length !== base.slides.length)
16
+ return { draft: { ...head, slides: theirs.slides }, lost: mine.slides.map((_, i) => i + 1) };
17
+ const slides = [...theirs.slides];
18
+ mine.slides.forEach((m, i) => {
19
+ if (same(m, base.slides[i]))
20
+ return;
21
+ const j = slides.findIndex((t) => same(t, base.slides[i]));
22
+ if (j >= 0)
23
+ slides[j] = m;
24
+ else
25
+ lost.push(i + 1);
26
+ });
27
+ return { draft: { ...head, slides }, lost };
28
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Who has a deck open, and where: each editor (a browser tab, an app)
3
+ * reports its slide and selected box every few seconds; everyone else on
4
+ * the deck sees it live, like the cursors in a shared document. Kept in
5
+ * memory per deck folder — nothing is written, a silent editor drops out
6
+ * after EXPIRE_MS.
7
+ */
8
+ export interface Viewer {
9
+ /** The editor's own id (one per tab), not a person: one person may have two tabs open. */
10
+ client: string;
11
+ who: string;
12
+ /** 1-based slide in view, and the box selected there. */
13
+ slide: number;
14
+ slot?: string;
15
+ at: number;
16
+ }
17
+ export declare const EXPIRE_MS = 20000;
18
+ /** An editor is here (or moved): its slide and box. Returns everyone on the deck. */
19
+ export declare function touchPresence(dir: string, v: Omit<Viewer, "at">): Viewer[];
20
+ /** An editor closed the deck. */
21
+ export declare function leavePresence(dir: string, client: string): void;
22
+ export declare function viewersOf(dir: string): Viewer[];
23
+ /** Follow who is on the deck: called now and on every arrival, move and departure. */
24
+ export declare function subscribePresence(dir: string, fn: (viewers: Viewer[]) => void): () => void;
25
+ /**
26
+ * Call `notify` soon after a file in the deck's folder changes (an agent
27
+ * writing deck.md, the CLI resolving a comment): one fs.watch per open
28
+ * deck, shared, closed with its last listener. Editors then get the new
29
+ * deck at once instead of on their next poll.
30
+ */
31
+ export declare function watchDeckFiles(dir: string, notify: () => void): () => void;
@@ -0,0 +1,131 @@
1
+ import { readFileSync, watch } from "node:fs";
2
+ import path from "node:path";
3
+ import { snapshot } from "./versions.js";
4
+ export const EXPIRE_MS = 20_000;
5
+ const decks = new Map();
6
+ const listeners = new Map();
7
+ const list = (dir) => [...(decks.get(dir)?.values() ?? [])].sort((a, b) => a.who.localeCompare(b.who) || a.client.localeCompare(b.client));
8
+ const tell = (dir) => {
9
+ const now = list(dir);
10
+ for (const fn of listeners.get(dir) ?? [])
11
+ fn(now);
12
+ };
13
+ /** Drop the editors that stopped reporting; tells the deck's listeners when one went. */
14
+ function sweep(dir, now = Date.now()) {
15
+ const m = decks.get(dir);
16
+ if (!m)
17
+ return;
18
+ let gone = false;
19
+ for (const [k, v] of m)
20
+ if (now - v.at > EXPIRE_MS)
21
+ (m.delete(k), (gone = true));
22
+ if (!m.size)
23
+ decks.delete(dir);
24
+ if (gone)
25
+ tell(dir);
26
+ }
27
+ let timer;
28
+ const ensureTimer = () => {
29
+ if (timer)
30
+ return;
31
+ timer = setInterval(() => {
32
+ for (const dir of [...decks.keys()])
33
+ sweep(dir);
34
+ if (!decks.size)
35
+ (clearInterval(timer), (timer = undefined));
36
+ }, 5_000);
37
+ timer.unref?.();
38
+ };
39
+ /** An editor is here (or moved): its slide and box. Returns everyone on the deck. */
40
+ export function touchPresence(dir, v) {
41
+ sweep(dir);
42
+ const m = decks.get(dir) ?? new Map();
43
+ decks.set(dir, m);
44
+ const prev = m.get(v.client);
45
+ const next = { client: v.client, who: v.who, slide: v.slide, ...(v.slot ? { slot: v.slot } : {}), at: Date.now() };
46
+ m.set(v.client, next);
47
+ ensureTimer();
48
+ if (!prev || prev.slide !== next.slide || prev.slot !== next.slot || prev.who !== next.who)
49
+ tell(dir);
50
+ return list(dir);
51
+ }
52
+ /** An editor closed the deck. */
53
+ export function leavePresence(dir, client) {
54
+ const m = decks.get(dir);
55
+ if (!m?.delete(client))
56
+ return;
57
+ if (!m.size)
58
+ decks.delete(dir);
59
+ tell(dir);
60
+ }
61
+ export function viewersOf(dir) {
62
+ sweep(dir);
63
+ return list(dir);
64
+ }
65
+ /** Follow who is on the deck: called now and on every arrival, move and departure. */
66
+ export function subscribePresence(dir, fn) {
67
+ const set = listeners.get(dir) ?? new Set();
68
+ listeners.set(dir, set);
69
+ set.add(fn);
70
+ fn(viewersOf(dir));
71
+ return () => {
72
+ set.delete(fn);
73
+ if (!set.size)
74
+ listeners.delete(dir);
75
+ };
76
+ }
77
+ /* ---------- the deck's files, while someone has it open ---------- */
78
+ const fileWatches = new Map();
79
+ const readDeck = (dir) => {
80
+ try {
81
+ return readFileSync(path.join(dir, "deck.md"), "utf8");
82
+ }
83
+ catch {
84
+ return null;
85
+ }
86
+ };
87
+ /**
88
+ * Call `notify` soon after a file in the deck's folder changes (an agent
89
+ * writing deck.md, the CLI resolving a comment): one fs.watch per open
90
+ * deck, shared, closed with its last listener. Editors then get the new
91
+ * deck at once instead of on their next poll.
92
+ */
93
+ export function watchDeckFiles(dir, notify) {
94
+ let w = fileWatches.get(dir);
95
+ if (!w) {
96
+ try {
97
+ const entry = { watcher: null, notify: new Set(), timer: undefined, known: readDeck(dir) };
98
+ entry.watcher = watch(dir, { persistent: false }, (_event, file) => {
99
+ if (file && !/^(deck\.md|comments\.json)$/.test(String(file)))
100
+ return;
101
+ clearTimeout(entry.timer);
102
+ entry.timer = setTimeout(() => {
103
+ // deck.md changed: the version it replaced goes into the history (a save from the editor kept it already).
104
+ const now = readDeck(dir);
105
+ if (entry.known !== null && now !== null && now !== entry.known)
106
+ void snapshot(dir, entry.known, true).catch(() => { });
107
+ if (now !== null)
108
+ entry.known = now;
109
+ for (const fn of entry.notify)
110
+ fn();
111
+ }, 80);
112
+ });
113
+ entry.watcher.on("error", () => { });
114
+ w = entry;
115
+ fileWatches.set(dir, w);
116
+ }
117
+ catch {
118
+ return () => { };
119
+ }
120
+ }
121
+ const entry = w;
122
+ entry.notify.add(notify);
123
+ return () => {
124
+ entry.notify.delete(notify);
125
+ if (entry.notify.size)
126
+ return;
127
+ clearTimeout(entry.timer);
128
+ entry.watcher.close();
129
+ fileWatches.delete(dir);
130
+ };
131
+ }
@@ -0,0 +1,33 @@
1
+ import * as ok from "@office-kit/pptx";
2
+ import { type DeckDoc } from "./deck.js";
3
+ import { type Template } from "./template.js";
4
+ /**
5
+ * Deck + template → the PowerPoint model (@office-kit/pptx). Every slide
6
+ * picks a layout; every slot of the layout is drawn from the slide's
7
+ * markdown for it: a text box with paragraphs, a picture, a native chart
8
+ * or a table. Nothing is guessed: content a slot cannot show is reported
9
+ * as a warning with the slide and slot, so the editor and `presentations
10
+ * check` can point at it.
11
+ */
12
+ export interface RenderWarning {
13
+ /** 1-based slide number. */
14
+ slide: number;
15
+ slot?: string;
16
+ level: "error" | "warning";
17
+ message: string;
18
+ }
19
+ export interface AssetReader {
20
+ /** The bytes of a file referenced from a slide (`assets/arch.png`), null when it does not exist. */
21
+ read(src: string): Promise<Uint8Array | null>;
22
+ }
23
+ export interface RenderOptions {
24
+ /** Pictures of the slots a PowerPoint shape cannot carry (HTML, embeds), keyed "<slide>:<slot>" (1-based slide), as the browser drew them. */
25
+ images?: Map<string, Uint8Array>;
26
+ }
27
+ /** A slot whose markdown carries raw HTML: PowerPoint gets it as a picture when there is one. */
28
+ export declare const hasHtml: (md: string) => boolean;
29
+ export interface Rendered {
30
+ pres: ok.PresentationData;
31
+ warnings: RenderWarning[];
32
+ }
33
+ export declare function renderDeck(deck: DeckDoc, template: Template, assets: AssetReader, options?: RenderOptions): Promise<Rendered>;