@blueshed/joined 0.2.2 → 0.3.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/.claude/skills/joined/SKILL.md +9 -1
- package/.claude/skills/joined/reference.md +316 -63
- package/.claude/skills/joined-door/SKILL.md +57 -0
- package/README.md +19 -50
- package/docs/design.md +158 -37
- package/docs/introduction.md +27 -27
- package/docs/light-and-shadow.svg +2 -1
- package/docs/links-and-trust.svg +1 -0
- package/docs/stories-to-code.svg +2 -1
- package/docs/wire-op.svg +1 -0
- package/examples/README.md +20 -0
- package/examples/account/account.json +1 -0
- package/examples/account/derive.ts +41 -0
- package/examples/account/generated/elements.ts +52 -0
- package/examples/account/generated/rows.ts +22 -0
- package/examples/account/generated/rules.ts +32 -0
- package/examples/account/routes.ts +5 -0
- package/examples/app.css +11 -1
- package/examples/cafe/books.html +2 -2
- package/examples/cafe/cafe.css +96 -85
- package/examples/cafe/generated/books.rows.ts +22 -0
- package/examples/cafe/generated/books.rules.ts +3 -2
- package/examples/cafe/generated/kitchen.rows.ts +27 -0
- package/examples/cafe/generated/kitchen.rules.ts +11 -1
- package/examples/cafe/generated/menu.rows.ts +22 -0
- package/examples/cafe/generated/menu.rules.ts +3 -2
- package/examples/cafe/generated/order.rows.ts +27 -0
- package/examples/cafe/generated/order.rules.ts +11 -1
- package/examples/cafe/generated/staff.elements.ts +7 -2
- package/examples/cafe/generated/staff.rows.ts +22 -0
- package/examples/cafe/generated/staff.rules.ts +9 -1
- package/examples/cafe/generated/supplies.queries.ts +4 -2
- package/examples/cafe/generated/supplies.rows.ts +22 -0
- package/examples/cafe/generated/supplies.rules.ts +17 -1
- package/examples/cafe/generated/till.queries.ts +2 -0
- package/examples/cafe/generated/till.rows.ts +27 -0
- package/examples/cafe/generated/till.rules.ts +11 -1
- package/examples/cafe/index.html +14 -15
- package/examples/cafe/kitchen.css +11 -19
- package/examples/cafe/kitchen.ts +7 -3
- package/examples/cafe/look.ts +30 -0
- package/examples/cafe/menu.html +10 -26
- package/examples/cafe/menu.ts +8 -80
- package/examples/cafe/migrations/002-guard-supplies.ts +15 -0
- package/examples/cafe/order.html +8 -7
- package/examples/cafe/order.ts +6 -2
- package/examples/cafe/routes.ts +0 -1
- package/examples/cafe/staff.json +5 -0
- package/examples/cafe/stories.jsonl +6 -2
- package/examples/cafe/supplies.html +35 -34
- package/examples/cafe/supplies.ts +59 -39
- package/examples/cafe/till.ts +19 -11
- package/examples/cafe/views.ts +7 -5
- package/examples/chart/arches.ts +150 -0
- package/examples/chart/chart.css +11 -0
- package/examples/chart/chart.json +87 -0
- package/examples/chart/generated/elements.ts +105 -0
- package/examples/chart/generated/queries.ts +36 -0
- package/examples/chart/generated/rows.ts +27 -0
- package/examples/chart/generated/rules.ts +111 -0
- package/examples/chart/grid.ts +118 -0
- package/examples/chart/index.html +78 -0
- package/examples/chart/migrations/001-chart.ts +6 -0
- package/examples/chart/migrations/002-create-conditions.ts +6 -0
- package/examples/chart/page.ts +20 -0
- package/examples/chart/routes.ts +7 -0
- package/examples/chart/stories.jsonl +14 -0
- package/examples/chart/teeth.ts +57 -0
- package/examples/desk/desk.css +6 -10
- package/examples/desk/generated/elements.ts +1 -1
- package/examples/desk/generated/rows.ts +22 -0
- package/examples/desk/generated/rules.ts +34 -1
- package/examples/desk/index.html +10 -3
- package/examples/desk/lanes.css +21 -19
- package/examples/desk/routes.ts +0 -2
- package/examples/desk/stories.jsonl +4 -3
- package/examples/desk/tally.css +1 -1
- package/examples/desk/thumbs.ts +2 -2
- package/examples/docs.ts +7 -4
- package/examples/hello/generated/elements.ts +2 -2
- package/examples/hello/generated/rules.ts +3 -2
- package/examples/hello/hello.css +2 -4
- package/examples/hello/index.html +10 -3
- package/examples/hello/page.ts +4 -4
- package/examples/index.html +20 -12
- package/examples/notes/generated/elements.ts +112 -0
- package/examples/notes/generated/queries.ts +42 -0
- package/examples/notes/generated/rows.ts +27 -0
- package/examples/notes/generated/rules.ts +133 -0
- package/examples/notes/index.html +32 -0
- package/examples/notes/look.ts +41 -0
- package/examples/notes/migrations/001-notes.ts +7 -0
- package/examples/notes/notes.json +78 -0
- package/examples/notes/page.ts +21 -0
- package/examples/notes/routes.ts +6 -0
- package/examples/notes/stories.jsonl +24 -0
- package/examples/page.ts +9 -2
- package/examples/people/generated/rows.ts +22 -0
- package/examples/people/generated/rules.ts +11 -1
- package/examples/people/links.ts +8 -9
- package/examples/people/people.css +1 -1
- package/examples/people/routes.ts +1 -3
- package/examples/people/stories.jsonl +1 -0
- package/examples/people/view.ts +5 -2
- package/examples/trip/generated/elements.ts +5 -3
- package/examples/trip/generated/queries.ts +12 -1
- package/examples/trip/generated/rows.ts +27 -0
- package/examples/trip/generated/rules.ts +56 -2
- package/examples/trip/index.html +58 -12
- package/examples/trip/line.css +186 -0
- package/examples/trip/line.ts +51 -40
- package/examples/trip/page.ts +65 -71
- package/examples/trip/routes.ts +9 -5
- package/examples/trip/stays.css +13 -13
- package/examples/trip/stays.ts +15 -10
- package/examples/trip/stories.jsonl +2 -0
- package/examples/trip/told.ts +3 -26
- package/examples/trip/trip.css +16 -0
- package/examples/trips/generated/elements.ts +1 -1
- package/examples/trips/generated/rows.ts +22 -0
- package/examples/trips/generated/rules.ts +9 -1
- package/examples/trips/index.html +10 -10
- package/examples/trips/map.ts +3 -14
- package/examples/trips/new-trip.ts +14 -16
- package/examples/trips/trips.css +24 -23
- package/examples/weeks/generated/elements.ts +1 -1
- package/examples/weeks/generated/queries.ts +11 -0
- package/examples/weeks/generated/rows.ts +22 -0
- package/examples/weeks/generated/rules.ts +11 -5
- package/examples/weeks/routes.ts +3 -2
- package/examples/weeks/stories.jsonl +1 -0
- package/examples/welcome.css +43 -33
- package/package.json +8 -2
- package/src/attachment.ts +17 -2
- package/src/awesome.ts +103 -0
- package/src/board.css +76 -0
- package/src/board.ts +173 -0
- package/src/cli.ts +28 -3
- package/src/compile.ts +821 -109
- package/src/definitions.ts +113 -27
- package/src/demo.css +2 -5
- package/src/draft.ts +48 -27
- package/src/drawer.ts +248 -0
- package/src/engine.ts +253 -170
- package/src/fields.ts +341 -0
- package/src/file-value.ts +12 -2
- package/src/files.ts +113 -33
- package/src/ground.ts +110 -0
- package/src/icon-names.ts +19 -2
- package/src/keep.ts +16 -3
- package/src/kit.ts +51 -0
- package/src/link.ts +3 -3
- package/src/map.css +140 -0
- package/src/map.ts +200 -0
- package/src/mcp.ts +129 -24
- package/src/members/demo.ts +13 -12
- package/src/members/generated/rows.ts +22 -0
- package/src/members/generated/rules.ts +3 -2
- package/src/members/group.ts +105 -55
- package/src/members/keys.ts +95 -79
- package/src/members/panel.css +40 -35
- package/src/members/panel.ts +58 -37
- package/src/migrate.ts +178 -125
- package/src/migrations/005-change-kept.ts +7 -0
- package/src/month.css +129 -0
- package/src/month.ts +179 -0
- package/src/ops.ts +40 -1
- package/src/page.css +11 -2
- package/src/page.ts +68 -17
- package/src/parts.css +614 -230
- package/src/parts.ts +259 -36
- package/src/plane.css +61 -0
- package/src/plane.ts +153 -0
- package/src/postgres-server.ts +60 -0
- package/src/reading.css +73 -0
- package/src/reading.ts +238 -0
- package/src/rows.css +289 -0
- package/src/rows.ts +864 -0
- package/src/search.css +80 -0
- package/src/search.ts +130 -0
- package/src/server.ts +355 -137
- package/src/settings.ts +9 -4
- package/src/sheet.ts +116 -0
- package/src/skill.ts +40 -0
- package/src/slots.css +90 -0
- package/src/slots.ts +213 -0
- package/src/specs.ts +38 -0
- package/src/starter.ts +19 -2
- package/src/store.ts +224 -161
- package/src/stories.ts +43 -11
- package/src/surface.ts +164 -0
- package/src/time.ts +2 -0
- package/src/timeline.css +111 -0
- package/src/timeline.ts +235 -0
- package/src/toast.ts +21 -48
- package/src/together.ts +15 -24
- package/src/tokens.css +89 -3
- package/src/tokens.json +287 -7
- package/src/tool.ts +210 -0
- package/src/upkeep.ts +47 -22
- package/src/views.css +43 -0
- package/src/views.ts +111 -0
- package/src/week.css +150 -0
- package/src/week.ts +254 -0
- package/src/write.ts +326 -78
- package/starter/CHANGELOG.md +8 -0
- package/starter/CLAUDE.md +9 -3
- package/starter/bunfig +5 -0
- package/starter/githooks/pre-commit +13 -0
- package/starter/prettierignore +5 -0
- package/starter/tests/hello.test.ts +2 -0
- package/starter/tests/records.test.ts +40 -0
- package/starter/todo.jsonl +0 -0
- package/docs/learning.md +0 -76
- package/examples/cafe/trust.ts +0 -20
- package/examples/desk/trust.ts +0 -24
- package/examples/people/trust.ts +0 -14
- package/examples/trip/answer-rows.ts +0 -70
- package/examples/trip/bar.ts +0 -89
- package/examples/trip/calendar-map.css +0 -711
- package/examples/trip/calendar-map.ts +0 -249
- package/examples/trip/drags.ts +0 -143
- package/examples/trip/drawer.ts +0 -250
- package/examples/trip/event-form.ts +0 -200
- package/examples/trip/holding.ts +0 -61
- package/examples/trip/map.ts +0 -234
- package/examples/trip/month.ts +0 -87
- package/examples/trip/other-weeks.ts +0 -67
- package/examples/trip/peek.ts +0 -71
- package/examples/trip/people-chips.ts +0 -74
- package/examples/trip/shell.ts +0 -39
- package/examples/trip/trust.ts +0 -51
- package/examples/trip/week.ts +0 -237
- package/examples/trip/writes.ts +0 -91
- package/examples/trip/zones.ts +0 -16
- package/examples/weeks/derive.ts +0 -23
- package/src/scrim.ts +0 -81
package/src/tool.ts
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// <j-tool> (joined/kit): a map's tool, chosen first and then tapped, as a pencil was. Put in a map (<j-plane>,
|
|
2
|
+
// <j-slots>), it draws nothing itself: the map draws its tools in a bar, one chosen of each kind, and a tap on the map
|
|
3
|
+
// makes the write the chosen ones say.
|
|
4
|
+
//
|
|
5
|
+
// <j-tool adds="crossings" finished="true">Finish</j-tool> an item added to that collection, for what's tapped
|
|
6
|
+
// <j-tool sets="material" value="amalgam">Amalgam</j-tool> that field of what's tapped written, or of a new one
|
|
7
|
+
// <j-tool sets="status" value="existing" default></j-tool> no button: what's set while none of its kind is chosen
|
|
8
|
+
// <j-tool for="conditions" sets="state" value="crown">Crown</j-tool> another collection's item written instead: the
|
|
9
|
+
// one the tapped place names that is this tool's (the tooth's crown), or a new one
|
|
10
|
+
// <j-tool clears>Clear</j-tool> what's tapped taken off: its charting, or (with a tool for another chosen) that item
|
|
11
|
+
//
|
|
12
|
+
// Any other attribute is a value the write gives, typed by its field (finished="true" a yes). What may be written is
|
|
13
|
+
// the stories': the server refuses in the story's words. Nothing is overwritten that isn't the tool's own: a tooth's
|
|
14
|
+
// crown and its root filling are two items. The bar ends with Undo: the map's last write put back, then the one before.
|
|
15
|
+
|
|
16
|
+
import { el } from "./fields.ts";
|
|
17
|
+
import { undo, type Answer } from "./page.ts";
|
|
18
|
+
import type { Reading } from "./reading.ts";
|
|
19
|
+
import type { Field, Item, Spec } from "./rows.ts";
|
|
20
|
+
import { send } from "./sheet.ts";
|
|
21
|
+
import { specOf } from "./specs.ts";
|
|
22
|
+
|
|
23
|
+
/** A collection given to the kit, once it is. */
|
|
24
|
+
const specNamed = (name: string) => new Promise<Spec>((ready) => specOf(name, ready));
|
|
25
|
+
|
|
26
|
+
/** A value said in an attribute, as its field has it: a number, a yes or no, or words. */
|
|
27
|
+
export const typed = (f: Field | undefined, raw: string): unknown =>
|
|
28
|
+
!f ? raw : f.type === "boolean" ? raw === "true" : f.type === "integer" || f.type === "decimal" ? Number(raw) : raw;
|
|
29
|
+
/** Values said in attributes, each as its field in a collection has it; those it hasn't, left out. */
|
|
30
|
+
const typedFor = (s: Spec, given: Record<string, string>) =>
|
|
31
|
+
Object.fromEntries(
|
|
32
|
+
Object.entries(given).flatMap(([k, v]) => {
|
|
33
|
+
const f = s.fields.find((f) => f.name === k);
|
|
34
|
+
return f ? [[k, typed(f, v)]] : [];
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
export class JTool extends HTMLElement {
|
|
39
|
+
connectedCallback() {
|
|
40
|
+
this.hidden = true;
|
|
41
|
+
}
|
|
42
|
+
get words() {
|
|
43
|
+
return this.textContent?.trim() ?? "";
|
|
44
|
+
}
|
|
45
|
+
/** The collection it adds to, if it adds. */
|
|
46
|
+
get adds() {
|
|
47
|
+
return this.getAttribute("adds") ?? "";
|
|
48
|
+
}
|
|
49
|
+
/** The field it writes, if it sets one. */
|
|
50
|
+
get sets() {
|
|
51
|
+
return this.getAttribute("sets") ?? "";
|
|
52
|
+
}
|
|
53
|
+
/** Another collection it writes in place of the map's own, if it does. */
|
|
54
|
+
get for() {
|
|
55
|
+
return this.getAttribute("for") ?? "";
|
|
56
|
+
}
|
|
57
|
+
/** Whether it's what's set while nothing of its kind is chosen, with no button. */
|
|
58
|
+
get default() {
|
|
59
|
+
return this.hasAttribute("default");
|
|
60
|
+
}
|
|
61
|
+
/** Whether it takes off what's tapped. */
|
|
62
|
+
get clears() {
|
|
63
|
+
return this.hasAttribute("clears");
|
|
64
|
+
}
|
|
65
|
+
/** Its kind: one tool of each is chosen at once (each field set, what's added, or a clearing). */
|
|
66
|
+
get kind() {
|
|
67
|
+
return this.clears ? "clears" : this.sets ? `sets:${this.sets}` : "adds";
|
|
68
|
+
}
|
|
69
|
+
/** The values its write gives, by field, as said: the field it sets, and every other attribute. */
|
|
70
|
+
get given(): Record<string, string> {
|
|
71
|
+
const out: Record<string, string> = {};
|
|
72
|
+
for (const a of this.attributes)
|
|
73
|
+
if (!["adds", "sets", "value", "hidden", "for", "default", "clears"].includes(a.name)) out[a.name] = a.value;
|
|
74
|
+
if (this.sets) out[this.sets] = this.getAttribute("value") ?? "";
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A tap on a map with tools chosen: the writes they say, for the item tapped, or for a new one at `at` (a place on
|
|
80
|
+
* the water, a slot's fields). Nothing chosen, nothing done: the tap opens what's tapped instead. */
|
|
81
|
+
export async function applied(map: Reading, tools: Tools, it: Item | null, at: Record<string, unknown>) {
|
|
82
|
+
const chosen = tools.chosen,
|
|
83
|
+
own = map.spec!,
|
|
84
|
+
said = (why: Answer) => void (why && (map.line.textContent = `Refused: ${why}`)),
|
|
85
|
+
values = (s: Spec) => typedFor(s, Object.assign({}, ...chosen.filter((t) => t.sets).map((t) => t.given))),
|
|
86
|
+
clears = chosen.some((t) => t.clears);
|
|
87
|
+
if (!chosen.length) return false;
|
|
88
|
+
map.line.textContent = "";
|
|
89
|
+
// a tool for another collection: its item for the tapped place, by the place's fields and the tool's own value
|
|
90
|
+
const other = chosen.find((t) => t.for);
|
|
91
|
+
if (other) {
|
|
92
|
+
const s = await specNamed(other.for),
|
|
93
|
+
has = (k: string) => s.fields.some((f) => f.name === k),
|
|
94
|
+
named = {
|
|
95
|
+
...Object.fromEntries(Object.entries(at).filter(([k]) => has(k))),
|
|
96
|
+
...typedFor(s, { [other.sets]: other.getAttribute("value") ?? "" }),
|
|
97
|
+
},
|
|
98
|
+
item = [...(document.getElementsByTagName(s.tag) as HTMLCollectionOf<Item>)].find(
|
|
99
|
+
(x) => x.isConnected && Object.entries(named).every(([k, v]) => x.data[k] === v),
|
|
100
|
+
);
|
|
101
|
+
if (clears) said(item ? await tools.kept(item.removeItem!()) : undefined);
|
|
102
|
+
else if (item) said(await tools.kept(item.replace!(values(s))));
|
|
103
|
+
else
|
|
104
|
+
said(
|
|
105
|
+
await tools.kept(
|
|
106
|
+
send(document.querySelector(s.place)!, s.doc, [
|
|
107
|
+
{ op: "add", path: `/${s.name}/-`, value: { ...named, ...values(s) } },
|
|
108
|
+
]),
|
|
109
|
+
),
|
|
110
|
+
);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
if (clears) {
|
|
114
|
+
if (it) said(await tools.kept(it.removeItem!()));
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
for (const t of chosen.filter((t) => t.adds)) {
|
|
118
|
+
if (t.adds === own.name) {
|
|
119
|
+
map.sheet?.show("new", at as Record<string, number>, typedFor(own, t.given));
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const s = await specNamed(t.adds),
|
|
123
|
+
link = s.fields.find((f) => f.to?.tag === own.tag);
|
|
124
|
+
if (it && link)
|
|
125
|
+
said(
|
|
126
|
+
await tools.kept(
|
|
127
|
+
send(document.querySelector(s.place)!, s.doc, [
|
|
128
|
+
{ op: "add", path: `/${s.name}/-`, value: { ...typedFor(s, t.given), [link.name]: it.key } },
|
|
129
|
+
]),
|
|
130
|
+
),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (chosen.some((t) => t.sets)) {
|
|
134
|
+
if (it) said(await tools.kept(it.replace!(values(own))));
|
|
135
|
+
else
|
|
136
|
+
said(
|
|
137
|
+
await tools.kept(
|
|
138
|
+
send(document.querySelector(own.place)!, own.doc, [
|
|
139
|
+
{ op: "add", path: `/${own.name}/-`, value: { ...at, ...values(own) } },
|
|
140
|
+
]),
|
|
141
|
+
),
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A map's tools: its <j-tool>s drawn as buttons in a bar, one chosen of each kind, the chosen one pressed again let
|
|
148
|
+
* go; and Undo, the map's writes put back, last first. What's chosen is the map's alone, read as a tap lands. */
|
|
149
|
+
export class Tools {
|
|
150
|
+
bar = el("div", { className: "tools", role: "toolbar" });
|
|
151
|
+
#host: Reading;
|
|
152
|
+
#chosen = new Map<string, JTool>();
|
|
153
|
+
/** The map's writes, each by its document and the version it made, last last. */
|
|
154
|
+
#made: { doc: string; version: number }[] = [];
|
|
155
|
+
/** The map's write not yet answered: Undo waits for it, so it never puts back the one before. */
|
|
156
|
+
#sending: Promise<unknown> = Promise.resolve();
|
|
157
|
+
|
|
158
|
+
constructor(host: Reading) {
|
|
159
|
+
this.#host = host;
|
|
160
|
+
}
|
|
161
|
+
get all() {
|
|
162
|
+
return [...this.#host.querySelectorAll<JTool>(":scope > j-tool")];
|
|
163
|
+
}
|
|
164
|
+
/** The tools chosen, one of each kind; once one is, each default of a kind none of whose tools is. */
|
|
165
|
+
get chosen() {
|
|
166
|
+
const pressed = [...this.#chosen.values()].filter((t) => t.isConnected);
|
|
167
|
+
if (!pressed.length) return [];
|
|
168
|
+
const kinds = new Set(pressed.map((t) => t.kind));
|
|
169
|
+
return [...pressed, ...this.all.filter((t) => t.default && !kinds.has(t.kind))];
|
|
170
|
+
}
|
|
171
|
+
/** A write of the map's, its version kept as it lands (its `landed`, heard on the document), so Undo can put it back. */
|
|
172
|
+
async kept(request: Promise<Answer>) {
|
|
173
|
+
const landed = (e: Event) => this.#made.push((e as CustomEvent<{ doc: string; version: number }>).detail);
|
|
174
|
+
document.addEventListener("landed", landed);
|
|
175
|
+
this.#sending = request.catch(() => {});
|
|
176
|
+
try {
|
|
177
|
+
return await request;
|
|
178
|
+
} finally {
|
|
179
|
+
document.removeEventListener("landed", landed);
|
|
180
|
+
this.draw();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
draw() {
|
|
184
|
+
const shown = this.all.filter((t) => !t.default);
|
|
185
|
+
this.bar.hidden = !shown.length;
|
|
186
|
+
const back = el("button", { type: "button", className: "act tool undo", textContent: "Undo" });
|
|
187
|
+
back.disabled = !this.#made.length;
|
|
188
|
+
back.addEventListener("click", async () => {
|
|
189
|
+
await this.#sending;
|
|
190
|
+
const last = this.#made.pop();
|
|
191
|
+
if (!last) return;
|
|
192
|
+
const { why } = await undo(last.doc, last.version);
|
|
193
|
+
this.#host.line.textContent = why ? `Refused: ${why}` : "";
|
|
194
|
+
this.draw();
|
|
195
|
+
});
|
|
196
|
+
this.bar.replaceChildren(
|
|
197
|
+
...shown.map((t) => {
|
|
198
|
+
const b = el("button", { type: "button", className: "act tool", textContent: t.words });
|
|
199
|
+
b.ariaPressed = String(this.#chosen.get(t.kind) === t);
|
|
200
|
+
b.addEventListener("click", () => {
|
|
201
|
+
if (this.#chosen.get(t.kind) === t) this.#chosen.delete(t.kind);
|
|
202
|
+
else this.#chosen.set(t.kind, t);
|
|
203
|
+
this.draw();
|
|
204
|
+
});
|
|
205
|
+
return b;
|
|
206
|
+
}),
|
|
207
|
+
back,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
}
|
package/src/upkeep.ts
CHANGED
|
@@ -1,11 +1,24 @@
|
|
|
1
|
-
// What a kept store needs that
|
|
2
|
-
//
|
|
1
|
+
// What a kept store needs that it never does for itself: old changes and files nothing names let go, on any store;
|
|
2
|
+
// and on SQLite, which never gives back the pages a removed row left, gathers what the planner reads, or copies the
|
|
3
|
+
// file anywhere, those too (Postgres has its own autovacuum and backups). joined does it when it's idle: a VACUUM and
|
|
3
4
|
// PRAGMA optimize once enough has been written (what's let go first: `trim`, the old changes), and a backup once a day, a copy the store's own size
|
|
4
|
-
// (VACUUM INTO), the last seven kept. A backup is
|
|
5
|
+
// (VACUUM INTO), the last seven kept. A backup is copied to a name of its own (`.part`) and given its name only once
|
|
6
|
+
// it's whole, so a copy cut short (the disk full, joined killed) is never counted as one, nor lets a good one go; one a
|
|
7
|
+
// crash left is cleared as the upkeep starts. A backup is a store: to go back to one, stop joined and put it in the
|
|
5
8
|
// store's place (JOINED_DB), with no -wal or -shm beside it.
|
|
6
9
|
|
|
7
10
|
import type { Engine } from "./engine.ts";
|
|
8
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
closeSync,
|
|
13
|
+
fsyncSync,
|
|
14
|
+
mkdirSync,
|
|
15
|
+
openSync,
|
|
16
|
+
readdirSync,
|
|
17
|
+
renameSync,
|
|
18
|
+
rmSync,
|
|
19
|
+
statSync,
|
|
20
|
+
unlinkSync,
|
|
21
|
+
} from "node:fs";
|
|
9
22
|
|
|
10
23
|
const IDLE = 10_000; // no write for this long: idle
|
|
11
24
|
const WRITES = 500; // vacuum after this many writes, or an hour after the first
|
|
@@ -23,37 +36,53 @@ export function upkeep(
|
|
|
23
36
|
log: (line: string) => void,
|
|
24
37
|
{
|
|
25
38
|
every = 60_000,
|
|
26
|
-
trim = () => 0,
|
|
39
|
+
trim = async () => 0,
|
|
27
40
|
letGo = async () => 0,
|
|
28
|
-
|
|
41
|
+
sqlite = true,
|
|
42
|
+
}: { every?: number; trim?: () => Promise<number>; letGo?: () => Promise<number>; sqlite?: boolean } = {},
|
|
29
43
|
) {
|
|
30
44
|
let lastWrite = 0,
|
|
31
45
|
writes = 0,
|
|
32
46
|
vacuumedAt = Date.now();
|
|
33
|
-
|
|
34
|
-
|
|
47
|
+
if (sqlite) {
|
|
48
|
+
mkdirSync(backups, { recursive: true });
|
|
49
|
+
for (const part of readdirSync(backups).filter((f) => f.endsWith(PART))) unlinkSync(`${backups}/${part}`);
|
|
50
|
+
}
|
|
51
|
+
const kept = sqlite ? files(backups) : [];
|
|
35
52
|
let backedUpAt = kept.length ? statSync(`${backups}/${kept.at(-1)}`).mtimeMs : 0;
|
|
36
53
|
|
|
37
54
|
/** Now, or when idle: vacuum if enough was written, back up if a day has passed. */
|
|
38
|
-
function run(now = false) {
|
|
55
|
+
async function run(now = false) {
|
|
39
56
|
const at = Date.now();
|
|
40
57
|
const done: { vacuumed?: boolean; backup?: string; files?: Promise<number> } = {};
|
|
41
58
|
if (!now && at - lastWrite < IDLE) return done;
|
|
42
59
|
if (now || writes >= WRITES || (writes > 0 && at - vacuumedAt > HOUR)) {
|
|
43
|
-
const gone = trim();
|
|
60
|
+
const gone = await trim();
|
|
44
61
|
if (gone) log(`let go ${gone} old changes`);
|
|
45
62
|
done.files = letGo().then(
|
|
46
63
|
(n) => (n && log(`let go ${n} files nothing names`), n),
|
|
47
64
|
(e) => (log(`letting files go failed: ${e}`), 0),
|
|
48
65
|
);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
66
|
+
if (sqlite) {
|
|
67
|
+
await db.run("VACUUM");
|
|
68
|
+
await db.run("PRAGMA optimize");
|
|
69
|
+
log(`vacuumed after ${writes} writes`);
|
|
70
|
+
}
|
|
52
71
|
[writes, vacuumedAt, done.vacuumed] = [0, at, true];
|
|
53
72
|
}
|
|
54
|
-
if (now || at - backedUpAt > DAY) {
|
|
73
|
+
if (sqlite && (now || at - backedUpAt > DAY)) {
|
|
55
74
|
done.backup = `${backups}/${new Date(at).toISOString().replace(/[:.]/g, "-")}.sqlite`;
|
|
56
|
-
|
|
75
|
+
const part = done.backup + PART;
|
|
76
|
+
try {
|
|
77
|
+
await db.run("VACUUM INTO ?", [part]);
|
|
78
|
+
// on the disk before it has its name: a power cut never leaves a backup's name on an empty file
|
|
79
|
+
const fd = openSync(part, "r");
|
|
80
|
+
fsyncSync(fd);
|
|
81
|
+
closeSync(fd);
|
|
82
|
+
renameSync(part, done.backup);
|
|
83
|
+
} finally {
|
|
84
|
+
rmSync(part, { force: true });
|
|
85
|
+
}
|
|
57
86
|
backedUpAt = at;
|
|
58
87
|
for (const old of files(backups).slice(0, -KEEP)) unlinkSync(`${backups}/${old}`);
|
|
59
88
|
log(`backed up to ${done.backup}`);
|
|
@@ -61,13 +90,7 @@ export function upkeep(
|
|
|
61
90
|
return done;
|
|
62
91
|
}
|
|
63
92
|
|
|
64
|
-
const timer = setInterval(() => {
|
|
65
|
-
try {
|
|
66
|
-
run();
|
|
67
|
-
} catch (e) {
|
|
68
|
-
log(`upkeep failed: ${e}`);
|
|
69
|
-
}
|
|
70
|
-
}, every);
|
|
93
|
+
const timer = setInterval(() => run().catch((e) => log(`upkeep failed: ${e}`)), every);
|
|
71
94
|
timer.unref();
|
|
72
95
|
return {
|
|
73
96
|
run,
|
|
@@ -79,6 +102,8 @@ export function upkeep(
|
|
|
79
102
|
};
|
|
80
103
|
}
|
|
81
104
|
|
|
105
|
+
/** A backup being copied: not one until it's renamed. */
|
|
106
|
+
const PART = ".part";
|
|
82
107
|
// Backups, oldest first: their names are their times.
|
|
83
108
|
const files = (dir: string) =>
|
|
84
109
|
readdirSync(dir)
|
package/src/views.css
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/* <j-views> (./views.ts): its tabs (Web Awesome's wa-tab-group, its nav alone), a row at the head of the readings,
|
|
2
|
+
the chosen one marked; under them the chosen reading. On a phone, every tab a finger tall, and every one but the
|
|
3
|
+
chosen its icon alone (its word is its name), so the row fits the screen; where even so they don't fit, it scrolls. */
|
|
4
|
+
:host {
|
|
5
|
+
display: block;
|
|
6
|
+
}
|
|
7
|
+
/* the tabs in the list's own width, in the middle of a wide page, as the search over them and the rows under them
|
|
8
|
+
are (rows.css, search.css): one column, its edges shared; a wider reading (a week, a month) runs on past it */
|
|
9
|
+
.tabs {
|
|
10
|
+
max-width: var(--size-list);
|
|
11
|
+
margin-inline: auto;
|
|
12
|
+
}
|
|
13
|
+
.tabs::part(body) {
|
|
14
|
+
display: none;
|
|
15
|
+
}
|
|
16
|
+
.tab {
|
|
17
|
+
display: inline-flex;
|
|
18
|
+
align-items: center;
|
|
19
|
+
gap: var(--space-6);
|
|
20
|
+
font: var(--font-tool);
|
|
21
|
+
}
|
|
22
|
+
/* its icon and its word, a space apart: they're laid out in its part, where they're slotted */
|
|
23
|
+
.tab::part(tab) {
|
|
24
|
+
gap: var(--space-6);
|
|
25
|
+
min-height: var(--size-touch);
|
|
26
|
+
padding-block: 0;
|
|
27
|
+
padding-inline: var(--space-12);
|
|
28
|
+
}
|
|
29
|
+
@media (max-width: 40rem), (pointer: coarse) {
|
|
30
|
+
.tab::part(tab) {
|
|
31
|
+
min-height: var(--size-finger);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
@media (max-width: 40rem) {
|
|
35
|
+
.tab:not([active])::part(tab) {
|
|
36
|
+
justify-content: center;
|
|
37
|
+
min-width: var(--size-finger);
|
|
38
|
+
padding: 0;
|
|
39
|
+
}
|
|
40
|
+
.tab:not([active]) > span {
|
|
41
|
+
display: none;
|
|
42
|
+
}
|
|
43
|
+
}
|
package/src/views.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// <j-views> (joined/kit): one collection read several ways, one at a time: a tab for each reading it holds (a list, a
|
|
2
|
+
// week, a month, a timeline, a map, a board), its icon and its word, the one chosen shown and the rest hidden. The
|
|
3
|
+
// choice is the page's alone, kept in the address (?view=week), so a reload or a link keeps it. Each reading in it is
|
|
4
|
+
// the way into the same sheet.
|
|
5
|
+
//
|
|
6
|
+
// <j-views>
|
|
7
|
+
// <j-plans-rows label="List"><j-plans></j-plans></j-plans-rows>
|
|
8
|
+
// <j-week for="plans"></j-week>
|
|
9
|
+
// <j-map for="plans"></j-map>
|
|
10
|
+
// <my-reading label="Papers" icon="read-only"></my-reading>
|
|
11
|
+
// </j-views>
|
|
12
|
+
//
|
|
13
|
+
// A reading of the app's own says its word (`label`) and its icon (`icon`, one of joined's names). Each tab is a part,
|
|
14
|
+
// `tab` and `tab-<its word>` (j-views::part(tab-papers)); and a reading hidden stays hidden, whatever display its own
|
|
15
|
+
// stylesheet gives it (parts.css).
|
|
16
|
+
|
|
17
|
+
import { el } from "./fields.ts";
|
|
18
|
+
import { icon } from "./icons.ts";
|
|
19
|
+
import { ICONS, type IconName } from "./icon-names.ts";
|
|
20
|
+
import { adopt } from "./parts.ts";
|
|
21
|
+
import STYLE from "./views.css" with { type: "text" };
|
|
22
|
+
|
|
23
|
+
const SHEET = new CSSStyleSheet();
|
|
24
|
+
SHEET.replaceSync(STYLE);
|
|
25
|
+
/** Each reading's word and icon, where it doesn't say its own (`label`, `icon`). */
|
|
26
|
+
const READINGS: Record<string, [string, IconName]> = {
|
|
27
|
+
"j-month": ["Month", "month"],
|
|
28
|
+
"j-week": ["Week", "week"],
|
|
29
|
+
"j-timeline": ["Timeline", "timeline"],
|
|
30
|
+
"j-map": ["Map", "map"],
|
|
31
|
+
"j-board": ["Board", "board"],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export class JViews extends HTMLElement {
|
|
35
|
+
root = this.attachShadow({ mode: "open" });
|
|
36
|
+
#tabs = el("wa-tab-group" as "div", { className: "tabs", part: "tabs", ariaLabel: "Read as" });
|
|
37
|
+
#watch = new MutationObserver(() => this.#draw());
|
|
38
|
+
|
|
39
|
+
get #param() {
|
|
40
|
+
return this.getAttribute("param") ?? "view";
|
|
41
|
+
}
|
|
42
|
+
connectedCallback() {
|
|
43
|
+
if (!this.root.childElementCount) {
|
|
44
|
+
adopt(this.root, SHEET);
|
|
45
|
+
this.root.append(this.#tabs, el("slot"));
|
|
46
|
+
// a tab chosen: its reading shown, and kept in the address
|
|
47
|
+
this.#tabs.addEventListener("wa-tab-show", (e) => {
|
|
48
|
+
const name = (e as CustomEvent<{ name: string }>).detail.name,
|
|
49
|
+
r = [...this.children].find((r) => this.#word(r).toLowerCase() === name);
|
|
50
|
+
if (r && r !== this.chosen) this.choose(r);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
this.#watch.observe(this, { childList: true });
|
|
54
|
+
this.#draw();
|
|
55
|
+
}
|
|
56
|
+
disconnectedCallback() {
|
|
57
|
+
this.#watch.disconnect();
|
|
58
|
+
}
|
|
59
|
+
/** Each reading's word, its own or its kind's. */
|
|
60
|
+
#word(r: Element) {
|
|
61
|
+
return r.getAttribute("label") ?? READINGS[r.localName]?.[0] ?? "List";
|
|
62
|
+
}
|
|
63
|
+
/** Each reading's icon: its own (one of joined's names), its kind's, else a list's. */
|
|
64
|
+
#icon(r: Element): IconName {
|
|
65
|
+
const own = r.getAttribute("icon");
|
|
66
|
+
return own && own in ICONS ? (own as IconName) : (READINGS[r.localName]?.[1] ?? "list");
|
|
67
|
+
}
|
|
68
|
+
/** The reading chosen: the address's, else the first. */
|
|
69
|
+
get chosen() {
|
|
70
|
+
const said = new URLSearchParams(location.search).get(this.#param),
|
|
71
|
+
all = [...this.children];
|
|
72
|
+
return all.find((r) => this.#word(r).toLowerCase() === said) ?? all[0];
|
|
73
|
+
}
|
|
74
|
+
/** A reading chosen: shown, and kept in the address. */
|
|
75
|
+
choose(r: Element) {
|
|
76
|
+
const url = new URL(location.href);
|
|
77
|
+
if (r === this.children[0]) url.searchParams.delete(this.#param);
|
|
78
|
+
else url.searchParams.set(this.#param, this.#word(r).toLowerCase());
|
|
79
|
+
history.replaceState(history.state, "", url);
|
|
80
|
+
this.#draw();
|
|
81
|
+
}
|
|
82
|
+
#draw() {
|
|
83
|
+
const chosen = this.chosen,
|
|
84
|
+
all = [...this.children];
|
|
85
|
+
// a tab each (Web Awesome's), made again only when the readings change
|
|
86
|
+
const words = all.map((r) => this.#word(r)),
|
|
87
|
+
shows = all.map((r, i) => `${words[i]} ${this.#icon(r)}`).join();
|
|
88
|
+
if (this.#tabs.dataset.shows !== shows) {
|
|
89
|
+
this.#tabs.dataset.shows = shows;
|
|
90
|
+
this.#tabs.replaceChildren(
|
|
91
|
+
...all.map((r, i) =>
|
|
92
|
+
el(
|
|
93
|
+
"wa-tab" as "div",
|
|
94
|
+
{
|
|
95
|
+
slot: "nav",
|
|
96
|
+
className: "tab",
|
|
97
|
+
part: `tab tab-${words[i]!.toLowerCase().replaceAll(/[^a-z0-9]+/g, "-")}`,
|
|
98
|
+
ariaLabel: words[i],
|
|
99
|
+
title: words[i],
|
|
100
|
+
},
|
|
101
|
+
icon(this.#icon(r)),
|
|
102
|
+
el("span", {}, words[i]!),
|
|
103
|
+
),
|
|
104
|
+
),
|
|
105
|
+
);
|
|
106
|
+
for (const [i, t] of [...this.#tabs.children].entries()) t.setAttribute("panel", words[i]!.toLowerCase());
|
|
107
|
+
}
|
|
108
|
+
this.#tabs.setAttribute("active", this.#word(chosen!).toLowerCase());
|
|
109
|
+
for (const r of all) (r as HTMLElement).hidden = r !== chosen;
|
|
110
|
+
}
|
|
111
|
+
}
|
package/src/week.css
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/* <j-week> (./week.ts), in its shadow: its head, the days' strip over their columns, the hours down the side, and
|
|
2
|
+
each item a block placed by its time (--top, in hours from the first shown), its length (--len) and its lane among
|
|
3
|
+
those at once (--lane of --lanes). An hour is --space-48 tall. On a phone, one day: the strip to choose it. */
|
|
4
|
+
:host {
|
|
5
|
+
--hour: var(--space-48);
|
|
6
|
+
--gutter-w: var(--space-48);
|
|
7
|
+
}
|
|
8
|
+
.strip {
|
|
9
|
+
display: grid;
|
|
10
|
+
grid-template-columns: var(--gutter-w) repeat(7, minmax(0, 1fr));
|
|
11
|
+
border-bottom: 1px solid var(--line);
|
|
12
|
+
}
|
|
13
|
+
.strip::before {
|
|
14
|
+
content: "";
|
|
15
|
+
}
|
|
16
|
+
.dayhead {
|
|
17
|
+
display: flex;
|
|
18
|
+
flex-direction: column;
|
|
19
|
+
align-items: center;
|
|
20
|
+
gap: var(--space-4);
|
|
21
|
+
min-width: 0;
|
|
22
|
+
padding: var(--space-4) 0 var(--space-8);
|
|
23
|
+
border: 0;
|
|
24
|
+
background: none;
|
|
25
|
+
color: var(--muted);
|
|
26
|
+
font: var(--font-tiny);
|
|
27
|
+
cursor: pointer;
|
|
28
|
+
}
|
|
29
|
+
.dayhead .dn {
|
|
30
|
+
display: grid;
|
|
31
|
+
place-items: center;
|
|
32
|
+
min-width: var(--space-24);
|
|
33
|
+
height: var(--space-24);
|
|
34
|
+
border: 1px solid transparent;
|
|
35
|
+
border-radius: var(--radius-pill);
|
|
36
|
+
color: var(--ink);
|
|
37
|
+
font: var(--font-small);
|
|
38
|
+
font-variant-numeric: tabular-nums;
|
|
39
|
+
}
|
|
40
|
+
.dayhead[today] .dn {
|
|
41
|
+
border-color: var(--ink);
|
|
42
|
+
}
|
|
43
|
+
.dayhead .mark {
|
|
44
|
+
width: var(--space-4);
|
|
45
|
+
height: var(--space-4);
|
|
46
|
+
border-radius: var(--radius-dot);
|
|
47
|
+
background: var(--accent);
|
|
48
|
+
}
|
|
49
|
+
.dayhead .mark.none {
|
|
50
|
+
visibility: hidden;
|
|
51
|
+
}
|
|
52
|
+
.body {
|
|
53
|
+
display: grid;
|
|
54
|
+
grid-template-columns: var(--gutter-w) minmax(0, 1fr);
|
|
55
|
+
}
|
|
56
|
+
.hours {
|
|
57
|
+
display: grid;
|
|
58
|
+
grid-auto-rows: var(--hour);
|
|
59
|
+
color: var(--muted);
|
|
60
|
+
font: var(--font-tiny);
|
|
61
|
+
font-variant-numeric: tabular-nums;
|
|
62
|
+
}
|
|
63
|
+
.hours span {
|
|
64
|
+
translate: 0 calc(var(--space-6) * -1);
|
|
65
|
+
padding-inline-end: var(--space-6);
|
|
66
|
+
text-align: end;
|
|
67
|
+
}
|
|
68
|
+
.hours span:first-child {
|
|
69
|
+
visibility: hidden;
|
|
70
|
+
}
|
|
71
|
+
.cols {
|
|
72
|
+
display: grid;
|
|
73
|
+
grid-template-columns: repeat(7, minmax(0, 1fr));
|
|
74
|
+
}
|
|
75
|
+
/* a day: its hours' lines drawn on its ground; an empty hour tapped adds there */
|
|
76
|
+
.col {
|
|
77
|
+
position: relative;
|
|
78
|
+
height: calc(var(--hour) * var(--hours, 12));
|
|
79
|
+
border-left: 1px solid var(--line);
|
|
80
|
+
background: repeating-linear-gradient(to bottom, var(--line) 0 1px, transparent 1px var(--hour));
|
|
81
|
+
}
|
|
82
|
+
.block {
|
|
83
|
+
position: absolute;
|
|
84
|
+
top: calc(var(--hour) * var(--top));
|
|
85
|
+
left: calc(100% * var(--lane) / var(--lanes));
|
|
86
|
+
width: calc(100% / var(--lanes) - var(--space-4));
|
|
87
|
+
height: calc(var(--hour) * var(--len) - var(--space-4) / 2);
|
|
88
|
+
margin-inline-start: calc(var(--space-4) / 2);
|
|
89
|
+
white-space: normal;
|
|
90
|
+
line-height: 1.25;
|
|
91
|
+
}
|
|
92
|
+
.block .at {
|
|
93
|
+
display: block;
|
|
94
|
+
}
|
|
95
|
+
.block[short] .at {
|
|
96
|
+
display: inline;
|
|
97
|
+
}
|
|
98
|
+
.block[short] {
|
|
99
|
+
white-space: nowrap;
|
|
100
|
+
}
|
|
101
|
+
/* its foot: dragged, it lasts longer or shorter */
|
|
102
|
+
.foot {
|
|
103
|
+
position: absolute;
|
|
104
|
+
inset: auto 0 0;
|
|
105
|
+
height: var(--size-grip);
|
|
106
|
+
cursor: ns-resize;
|
|
107
|
+
}
|
|
108
|
+
.now {
|
|
109
|
+
position: absolute;
|
|
110
|
+
inset-inline: 0;
|
|
111
|
+
top: calc(var(--hour) * var(--at));
|
|
112
|
+
height: 0;
|
|
113
|
+
border-top: calc(var(--space-4) / 2) solid var(--danger);
|
|
114
|
+
pointer-events: none;
|
|
115
|
+
}
|
|
116
|
+
.now::before {
|
|
117
|
+
content: "";
|
|
118
|
+
position: absolute;
|
|
119
|
+
left: calc(var(--space-4) * -1);
|
|
120
|
+
top: calc(var(--space-4) * -1 - 1px);
|
|
121
|
+
width: var(--space-8);
|
|
122
|
+
height: var(--space-8);
|
|
123
|
+
border-radius: var(--radius-dot);
|
|
124
|
+
background: var(--danger);
|
|
125
|
+
}
|
|
126
|
+
@media (max-width: 40rem) {
|
|
127
|
+
.strip {
|
|
128
|
+
grid-template-columns: repeat(7, minmax(0, 1fr));
|
|
129
|
+
}
|
|
130
|
+
.strip::before {
|
|
131
|
+
display: none;
|
|
132
|
+
}
|
|
133
|
+
.dayhead {
|
|
134
|
+
min-height: var(--size-finger);
|
|
135
|
+
}
|
|
136
|
+
.dayhead[aria-selected="true"] .dn {
|
|
137
|
+
border-color: var(--accent);
|
|
138
|
+
background: var(--accent);
|
|
139
|
+
color: var(--card);
|
|
140
|
+
}
|
|
141
|
+
.cols {
|
|
142
|
+
grid-template-columns: minmax(0, 1fr);
|
|
143
|
+
}
|
|
144
|
+
.col:not([chosen]) {
|
|
145
|
+
display: none;
|
|
146
|
+
}
|
|
147
|
+
.block {
|
|
148
|
+
min-height: var(--size-finger);
|
|
149
|
+
}
|
|
150
|
+
}
|