pi-pocket 0.25.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +2 -0
- package/README.md +5 -3
- package/docs/architecture.md +21 -0
- package/docs/design/design-mode.md +108 -0
- package/docs/design/notes-a.md +106 -0
- package/docs/design/notes-b.md +123 -0
- package/docs/design/notes-c.md +92 -0
- package/docs/design/notes-d.md +78 -0
- package/docs/design/release-notes-design.md +46 -0
- package/docs/extensions/browser-modules.md +2 -0
- package/docs/extensions/policies/api-stability.md +2 -0
- package/docs/extensions/reference/api-versions.md +1 -0
- package/docs/extensions/reference/host.md +1 -1
- package/docs/extensions/reference/limits.md +4 -0
- package/docs/extensions/reference/names.md +2 -2
- package/docs/extensions/reference/pocket.md +38 -0
- package/docs/features.md +19 -5
- package/docs/map.md +75 -54
- package/docs/release-notes.md +1 -1
- package/docs/self-editing.md +1 -1
- package/docs/systemd.md +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/src/launcher/main.ts +20 -9
- package/src/server/app.ts +113 -4
- package/src/server/background.ts +3 -3
- package/src/server/cache-keeper.ts +605 -0
- package/src/server/compaction.ts +68 -15
- package/src/server/design/bundle.ts +65 -0
- package/src/server/design/export.ts +118 -0
- package/src/server/design/files.ts +49 -0
- package/src/server/design/guide.ts +56 -0
- package/src/server/design/index.ts +232 -0
- package/src/server/design/pptx.ts +304 -0
- package/src/server/design/render.ts +593 -0
- package/src/server/design/system.ts +248 -0
- package/src/server/docs.ts +55 -0
- package/src/server/extensions/artifacts.ts +202 -18
- package/src/server/extensions/design/canvas.js +304 -0
- package/src/server/extensions/design/comments.js +15 -0
- package/src/server/extensions/design/data.js +263 -0
- package/src/server/extensions/design/devices.js +70 -0
- package/src/server/extensions/design/docks.js +647 -0
- package/src/server/extensions/design/edits.js +37 -0
- package/src/server/extensions/design/index.js +326 -0
- package/src/server/extensions/design/messages.js +92 -0
- package/src/server/extensions/design/parts.js +160 -0
- package/src/server/extensions/design/state.js +146 -0
- package/src/server/extensions/design/style.css +1332 -0
- package/src/server/extensions/design/tweaks.js +161 -0
- package/src/server/extensions/design/views.js +375 -0
- package/src/server/extensions/design/workspace.js +723 -0
- package/src/server/extensions/design.ts +620 -0
- package/src/server/extensions/plan.ts +11 -9
- package/src/server/extensions/prompt.ts +11 -3
- package/src/server/extensions/subagents.ts +7 -1
- package/src/server/extensions/updates/index.js +16 -6
- package/src/server/extensions/updates.ts +2 -2
- package/src/server/http/artifact-assets.ts +216 -0
- package/src/server/http/artifact-inspect.ts +1281 -0
- package/src/server/http/artifacts.ts +89 -14
- package/src/server/http/conversation-routes.ts +46 -1
- package/src/server/http.ts +12 -4
- package/src/server/main.ts +4 -0
- package/src/server/npm-install.ts +82 -0
- package/src/server/npm-update.ts +100 -0
- package/src/server/reload.ts +5 -0
- package/src/server/room.ts +2 -0
- package/src/server/spend.ts +73 -9
- package/src/server/update-state.ts +257 -8
- package/src/server/updates.ts +268 -33
- package/src/server/zip.ts +163 -0
- package/types/pocket.d.ts +32 -0
- package/web/activity-fold.js +28 -0
- package/web/artifact-bridge.js +506 -82
- package/web/extension-ui.js +21 -1
- package/web/pip.js +46 -9
- package/web/sessions.js +1 -1
- package/web/sheets/appearance.js +10 -0
- package/web/sheets/viewers.js +26 -1
- package/web/style.css +11 -0
- package/web/taps.js +26 -0
- package/web/theme.js +6 -0
- package/web/transcript.js +19 -10
- package/src/server/http/public.ts +0 -30
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// The canvas: a design's page in the artifact sandbox frame (the same frame and bridge as the artifact viewer), scaled to
|
|
2
|
+
// fit, with the page's side of Design mode (inspect, edit text, tweaks, pins, slides) spoken through the bridge.
|
|
3
|
+
import { useArtifactBridge } from "/artifact-bridge.js";
|
|
4
|
+
import { percent, scaleOf, stepZoom } from "./devices.js";
|
|
5
|
+
import { pinsOf } from "./comments.js";
|
|
6
|
+
import { addEdit } from "./edits.js";
|
|
7
|
+
import { forPage, normalizeKnobs, usable } from "./tweaks.js";
|
|
8
|
+
|
|
9
|
+
/** As the artifact viewer's frame (`web/sheets/viewers.js`): scripts and forms run; the app and its cookies are out of reach. */
|
|
10
|
+
export const SANDBOX =
|
|
11
|
+
"allow-scripts allow-forms allow-modals allow-popups allow-pointer-lock allow-downloads";
|
|
12
|
+
export const ALLOW = "fullscreen; clipboard-write; accelerometer; gyroscope";
|
|
13
|
+
|
|
14
|
+
export const frameUrl = (conversation, id, version) =>
|
|
15
|
+
`/a/${conversation}/${encodeURIComponent(id)}/${version}`;
|
|
16
|
+
|
|
17
|
+
const NO_KNOBS = [];
|
|
18
|
+
const NO_VALUES = {};
|
|
19
|
+
const num = (value) => typeof value === "number" && Number.isFinite(value);
|
|
20
|
+
|
|
21
|
+
export function createCanvas(env) {
|
|
22
|
+
const { pocket, store, data, parts } = env;
|
|
23
|
+
const { html, useEffect, useRef, useState } = pocket;
|
|
24
|
+
const { Ico, useSize } = parts;
|
|
25
|
+
|
|
26
|
+
/** A page at its own size, scaled by `scale`: the box takes the scaled room, the frame inside the full one. */
|
|
27
|
+
function Stage({ size, scale, children, label }) {
|
|
28
|
+
return html`<div
|
|
29
|
+
class="dsn-stage"
|
|
30
|
+
style=${`width:${Math.round(size.w * scale)}px;height:${Math.round(size.h * scale)}px`}
|
|
31
|
+
title=${label}
|
|
32
|
+
>
|
|
33
|
+
<div
|
|
34
|
+
class="dsn-stage-in"
|
|
35
|
+
style=${`width:${size.w}px;height:${size.h}px;transform:scale(${scale})`}
|
|
36
|
+
>
|
|
37
|
+
${children}
|
|
38
|
+
</div>
|
|
39
|
+
</div>`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A frame that only shows (compare, thumbnails, the deck's present mode before it is wired): no bridge. */
|
|
43
|
+
function Still({ src, title, interactive = true, lazy = false }) {
|
|
44
|
+
return html`<iframe
|
|
45
|
+
class="dsn-frame"
|
|
46
|
+
src=${src}
|
|
47
|
+
sandbox=${SANDBOX}
|
|
48
|
+
allow=${ALLOW}
|
|
49
|
+
title=${title}
|
|
50
|
+
loading=${lazy ? "lazy" : "eager"}
|
|
51
|
+
style=${interactive ? "" : "pointer-events:none"}
|
|
52
|
+
tabindex=${interactive ? undefined : "-1"}
|
|
53
|
+
></iframe>`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The zoom, the size, and the backdrop: a small bar over the canvas. */
|
|
57
|
+
function ZoomBar({ s, size, scale }) {
|
|
58
|
+
const dark = s.backdrop === "dark" || (s.backdrop === "auto" && env.darkTheme());
|
|
59
|
+
|
|
60
|
+
return html`<div class="dsn-zoom" role="group" aria-label="Zoom">
|
|
61
|
+
<button
|
|
62
|
+
type="button"
|
|
63
|
+
title="Zoom out"
|
|
64
|
+
aria-label="Zoom out"
|
|
65
|
+
onClick=${() => env.setZoom(stepZoom(scale, -1))}
|
|
66
|
+
>
|
|
67
|
+
<${Ico} name="minus" size=${14} />
|
|
68
|
+
</button>
|
|
69
|
+
<button
|
|
70
|
+
type="button"
|
|
71
|
+
class="dsn-zoom-value"
|
|
72
|
+
title=${s.zoom === "fit" ? "Fits the room. Tap for 100%." : "Fit to the room"}
|
|
73
|
+
onClick=${() => env.setZoom(s.zoom === "fit" ? 1 : "fit")}
|
|
74
|
+
>
|
|
75
|
+
${s.zoom === "fit" ? "Fit " : ""}${percent(scale)}
|
|
76
|
+
</button>
|
|
77
|
+
<button
|
|
78
|
+
type="button"
|
|
79
|
+
title="Zoom in"
|
|
80
|
+
aria-label="Zoom in"
|
|
81
|
+
onClick=${() => env.setZoom(stepZoom(scale, 1))}
|
|
82
|
+
>
|
|
83
|
+
<${Ico} name="plus" size=${14} />
|
|
84
|
+
</button>
|
|
85
|
+
<span class="dsn-zoom-size mono">${size.w}×${size.h}</span>
|
|
86
|
+
<button
|
|
87
|
+
type="button"
|
|
88
|
+
title=${dark ? "Light backdrop" : "Dark backdrop"}
|
|
89
|
+
aria-label=${dark ? "Light backdrop" : "Dark backdrop"}
|
|
90
|
+
onClick=${() => env.setBackdrop(dark ? "light" : "dark")}
|
|
91
|
+
>
|
|
92
|
+
<${Ico} name=${dark ? "sun" : "moon"} size=${14} />
|
|
93
|
+
</button>
|
|
94
|
+
</div>`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The design's page, in the frame with the bridge, scaled to the room. */
|
|
98
|
+
function CanvasView({ s, design, version, size, readOnly }) {
|
|
99
|
+
const room = useRef(null);
|
|
100
|
+
const frame = useRef(null);
|
|
101
|
+
const roomSize = useSize(room);
|
|
102
|
+
const [ready, setReady] = useState(0);
|
|
103
|
+
const [loaded, setLoaded] = useState(false);
|
|
104
|
+
const sent = useRef({ at: "", values: {} });
|
|
105
|
+
// The slide to go back to once a page that loads again says it is a deck (`pocket-deck`).
|
|
106
|
+
const pendingSlide = useRef(0);
|
|
107
|
+
// The zoom bar floats over the bottom of the room: the page fits above it.
|
|
108
|
+
const scale = scaleOf(s.zoom, size, { w: roomSize.w, h: roomSize.h - 40 });
|
|
109
|
+
const src = frameUrl(s.conversation, design.id, version);
|
|
110
|
+
const key = `${src}#${s.reload}`;
|
|
111
|
+
const notes = s.notes[design.id];
|
|
112
|
+
const comments = notes?.comments ?? [];
|
|
113
|
+
const pins = s.mode === "comment" || s.dock === "comments" ? pinsOf(comments, version) : [];
|
|
114
|
+
// The knobs are this frame's: a page that was just replaced has none until it says so.
|
|
115
|
+
const knobs = s.knobsKey === key ? s.knobs : NO_KNOBS;
|
|
116
|
+
const values = s.tweaks[design.id] ?? NO_VALUES;
|
|
117
|
+
|
|
118
|
+
// What the page says: the module's own handlers, current at every call (the bridge keeps the latest ones).
|
|
119
|
+
const hear = useRef(null);
|
|
120
|
+
|
|
121
|
+
hear.current = (message) => {
|
|
122
|
+
if (!message || typeof message !== "object") {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const state = store.get();
|
|
127
|
+
|
|
128
|
+
if (message.type === "pocket-picked" && state.mode === "comment" && !readOnly) {
|
|
129
|
+
store.set({
|
|
130
|
+
draft: { artifact: design.id, version, target: message.target },
|
|
131
|
+
dock: "comments",
|
|
132
|
+
});
|
|
133
|
+
} else if (
|
|
134
|
+
message.type === "pocket-text-edited" &&
|
|
135
|
+
state.mode === "edit" &&
|
|
136
|
+
!readOnly &&
|
|
137
|
+
message.target &&
|
|
138
|
+
typeof message.before === "string" &&
|
|
139
|
+
typeof message.after === "string"
|
|
140
|
+
) {
|
|
141
|
+
const at = `${design.id}@${version}`;
|
|
142
|
+
|
|
143
|
+
store.set((now) => ({
|
|
144
|
+
edits: {
|
|
145
|
+
...now.edits,
|
|
146
|
+
[at]: addEdit(now.edits[at] ?? [], {
|
|
147
|
+
target: message.target,
|
|
148
|
+
before: message.before,
|
|
149
|
+
after: message.after,
|
|
150
|
+
}),
|
|
151
|
+
},
|
|
152
|
+
}));
|
|
153
|
+
} else if (message.type === "pocket-tweaks") {
|
|
154
|
+
store.set({ knobs: normalizeKnobs(message.knobs), knobsKey: key });
|
|
155
|
+
} else if (
|
|
156
|
+
message.type === "pocket-deck" &&
|
|
157
|
+
num(message.slides) &&
|
|
158
|
+
message.slides > 0 &&
|
|
159
|
+
num(message.current)
|
|
160
|
+
) {
|
|
161
|
+
const slides = Math.min(500, Math.round(message.slides));
|
|
162
|
+
const want = pendingSlide.current;
|
|
163
|
+
let current = Math.max(0, Math.round(message.current));
|
|
164
|
+
|
|
165
|
+
// A page that loaded again starts on its first slide: go back to the one the person was on, once.
|
|
166
|
+
pendingSlide.current = 0;
|
|
167
|
+
|
|
168
|
+
if (
|
|
169
|
+
want > 0 &&
|
|
170
|
+
want < slides &&
|
|
171
|
+
current !== want &&
|
|
172
|
+
send({ type: "pocket-goto", slide: want })
|
|
173
|
+
) {
|
|
174
|
+
current = want;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
store.set({ deck: { slides, current }, slide: current });
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const bridge = useArtifactBridge(frame, {
|
|
182
|
+
artifact: design.id,
|
|
183
|
+
source: `the design “${design.title}”`,
|
|
184
|
+
enabled: true,
|
|
185
|
+
onMessage: (message) => hear.current(message),
|
|
186
|
+
onReady: () => setReady((count) => count + 1),
|
|
187
|
+
});
|
|
188
|
+
const supported = typeof bridge?.send === "function";
|
|
189
|
+
const send = (message) => (supported ? bridge.send(message) === true : false);
|
|
190
|
+
|
|
191
|
+
// For other parts of the workspace (the slide strip, the dock): a way to talk to this frame.
|
|
192
|
+
env.frame.current = {
|
|
193
|
+
send,
|
|
194
|
+
supported,
|
|
195
|
+
goto: (slide) => send({ type: "pocket-goto", slide }),
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
useEffect(() => () => (env.frame.current = null), []);
|
|
199
|
+
useEffect(() => void store.set({ bridge: supported }), [supported]);
|
|
200
|
+
|
|
201
|
+
// A new page: what it said about itself is gone until it says it again.
|
|
202
|
+
useEffect(() => {
|
|
203
|
+
setLoaded(false);
|
|
204
|
+
pendingSlide.current = store.get().slide;
|
|
205
|
+
store.set({ knobs: [], deck: null });
|
|
206
|
+
}, [key]);
|
|
207
|
+
|
|
208
|
+
// The mode: the page's inspector for a comment, contenteditable text for an edit.
|
|
209
|
+
useEffect(() => {
|
|
210
|
+
send({ type: "pocket-inspect", on: s.mode === "comment" && !readOnly });
|
|
211
|
+
send({ type: "pocket-edit-text", on: s.mode === "edit" && !readOnly });
|
|
212
|
+
}, [ready, s.mode, readOnly]);
|
|
213
|
+
|
|
214
|
+
// Numbered pins on the elements that have open comments on this version.
|
|
215
|
+
const pinKey = pins.map((comment) => comment.id).join();
|
|
216
|
+
|
|
217
|
+
useEffect(() => {
|
|
218
|
+
send({
|
|
219
|
+
type: "pocket-highlight",
|
|
220
|
+
targets: pins.slice(0, 100).map((comment, index) => ({
|
|
221
|
+
...(comment.target.pid ? { pid: comment.target.pid } : {}),
|
|
222
|
+
selector: comment.target.selector,
|
|
223
|
+
label: index + 1,
|
|
224
|
+
})),
|
|
225
|
+
});
|
|
226
|
+
}, [ready, pinKey]);
|
|
227
|
+
|
|
228
|
+
// The saved tweaks, once the page has said what its knobs are and the server what was saved.
|
|
229
|
+
useEffect(() => {
|
|
230
|
+
if (knobs.length > 0 && notes?.loaded && s.tweaks[design.id] === undefined) {
|
|
231
|
+
store.set((now) => ({
|
|
232
|
+
tweaks: { ...now.tweaks, [design.id]: usable(knobs, notes.tweaks) },
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
}, [knobs, notes?.loaded]);
|
|
236
|
+
|
|
237
|
+
// Tweaks to the page: only what differs from what it was last told (a fresh page has been told nothing).
|
|
238
|
+
useEffect(() => {
|
|
239
|
+
if (sent.current.at !== `${key}:${ready}`) {
|
|
240
|
+
sent.current = { at: `${key}:${ready}`, values: {} };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const out = {};
|
|
244
|
+
const next = { ...sent.current.values };
|
|
245
|
+
|
|
246
|
+
for (const knob of knobs) {
|
|
247
|
+
const own = Object.hasOwn(values, knob.name);
|
|
248
|
+
const want = own ? forPage(knob, values[knob.name]) : undefined;
|
|
249
|
+
|
|
250
|
+
if (want !== sent.current.values[knob.name]) {
|
|
251
|
+
out[knob.name] = want === undefined ? forPage(knob, knob.value) : want;
|
|
252
|
+
next[knob.name] = want;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (Object.keys(out).length > 0 && send({ type: "pocket-tweak-set", values: out })) {
|
|
257
|
+
sent.current.values = next;
|
|
258
|
+
}
|
|
259
|
+
}, [key, ready, knobs, values]);
|
|
260
|
+
|
|
261
|
+
return html`<div
|
|
262
|
+
class="dsn-canvas has-zoom"
|
|
263
|
+
ref=${room}
|
|
264
|
+
data-backdrop=${s.backdrop}
|
|
265
|
+
data-mode=${s.mode}
|
|
266
|
+
>
|
|
267
|
+
<div class="dsn-scroll" data-no-swipe>
|
|
268
|
+
<${Stage} size=${size} scale=${scale} label=${design.title}>
|
|
269
|
+
<iframe
|
|
270
|
+
ref=${frame}
|
|
271
|
+
key=${key}
|
|
272
|
+
class=${`dsn-frame wait ${loaded ? "loaded" : ""}`}
|
|
273
|
+
src=${src}
|
|
274
|
+
sandbox=${SANDBOX}
|
|
275
|
+
allow=${ALLOW}
|
|
276
|
+
title=${design.title}
|
|
277
|
+
onLoad=${() => setLoaded(true)}
|
|
278
|
+
></iframe>
|
|
279
|
+
<//>
|
|
280
|
+
</div>
|
|
281
|
+
${!loaded && html`<div class="dsn-loading"><${pocket.Spinner} label="Loading the design" /></div>`}
|
|
282
|
+
${
|
|
283
|
+
s.mode === "comment" &&
|
|
284
|
+
!s.draft &&
|
|
285
|
+
html`<div class="dsn-hint" key=${s.mode}>
|
|
286
|
+
${
|
|
287
|
+
supported
|
|
288
|
+
? "Tap an element to comment"
|
|
289
|
+
: "Pointing needs the frame's inspector"
|
|
290
|
+
}
|
|
291
|
+
</div>`
|
|
292
|
+
}
|
|
293
|
+
${
|
|
294
|
+
s.mode === "edit" &&
|
|
295
|
+
html`<div class="dsn-hint" key=${s.mode}>
|
|
296
|
+
${supported ? "Tap text to change it" : "Editing needs the frame's inspector"}
|
|
297
|
+
</div>`
|
|
298
|
+
}
|
|
299
|
+
<${ZoomBar} s=${s} size=${size} scale=${scale} />
|
|
300
|
+
</div>`;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return { Stage, Still, CanvasView, ZoomBar };
|
|
304
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Comments, as the canvas lists them: which are open, which have a pin on the page, and in what order. Pure functions.
|
|
2
|
+
|
|
3
|
+
/** The comments nobody marked done, oldest first. */
|
|
4
|
+
export const openOf = (comments) => comments.filter((comment) => !comment.resolved);
|
|
5
|
+
|
|
6
|
+
/** The comments with a pin on the page of `version`: open ones made on it. Their order is the pins' numbers. */
|
|
7
|
+
export const pinsOf = (comments, version) =>
|
|
8
|
+
openOf(comments).filter((comment) => comment.version === version);
|
|
9
|
+
|
|
10
|
+
/** A comment's number on the page, or 0 when it has no pin there (done, or made on another version). */
|
|
11
|
+
export function pinNumber(comments, comment, version) {
|
|
12
|
+
const at = pinsOf(comments, version).findIndex((each) => each.id === comment.id);
|
|
13
|
+
|
|
14
|
+
return at + 1;
|
|
15
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// What the workspace asks of the server, and keeps in its store: the session's designs, a design's comments and this
|
|
2
|
+
// person's tweaks, "go back to this one", and a file export.
|
|
3
|
+
import { savePick } from "./state.js";
|
|
4
|
+
|
|
5
|
+
/** The latest version number of a listed design. */
|
|
6
|
+
export const latestOf = (artifact) => artifact?.versions?.at(-1)?.version ?? 1;
|
|
7
|
+
|
|
8
|
+
const timeOf = (artifact) => artifact.versions.at(-1)?.createdAt ?? 0;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The designs of the session, newest first: the app's own list (live: it changes when Pi publishes) with what only the
|
|
12
|
+
* server's list has (group, kind, restores).
|
|
13
|
+
*/
|
|
14
|
+
export function designsOf(appView, listed) {
|
|
15
|
+
const extras = new Map((listed ?? []).map((each) => [each.id, each]));
|
|
16
|
+
const live = (appView?.artifacts ?? []).filter((each) => each.type === "html");
|
|
17
|
+
|
|
18
|
+
return live
|
|
19
|
+
.map((each) => {
|
|
20
|
+
const more = extras.get(each.id);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
id: each.id,
|
|
24
|
+
title: each.title,
|
|
25
|
+
group: more?.group,
|
|
26
|
+
kind: more?.kind ?? "page",
|
|
27
|
+
versions: each.versions.map((version) => ({
|
|
28
|
+
...version,
|
|
29
|
+
restoredFrom: more?.versions.find((other) => other.version === version.version)
|
|
30
|
+
?.restoredFrom,
|
|
31
|
+
})),
|
|
32
|
+
};
|
|
33
|
+
})
|
|
34
|
+
.sort((a, b) => timeOf(b) - timeOf(a));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The designs in the same group as `artifact` (itself included), in the list's order; none when it has no group. */
|
|
38
|
+
export function siblingsOf(designs, artifact) {
|
|
39
|
+
if (!artifact?.group) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return designs.filter((each) => each.group === artifact.group);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function createData(pocket, store) {
|
|
47
|
+
const conversationOf = () => store.get().conversation;
|
|
48
|
+
let listing = 0;
|
|
49
|
+
/** The newest read of each design's notes: an older answer that comes later is dropped. */
|
|
50
|
+
const reading = {};
|
|
51
|
+
|
|
52
|
+
/** Whether the answer to a call made in `conversation` still matters: the tab may have moved on. */
|
|
53
|
+
const current = (conversation) => store.get().conversation === conversation;
|
|
54
|
+
|
|
55
|
+
const fail = (error) => pocket.notify("error", `Design: ${error?.message ?? error}`);
|
|
56
|
+
|
|
57
|
+
const data = {
|
|
58
|
+
/** Read the server's list of designs (their groups and kinds). */
|
|
59
|
+
async list() {
|
|
60
|
+
const conversation = conversationOf();
|
|
61
|
+
|
|
62
|
+
if (conversation === null) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const mine = ++listing;
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const { artifacts } = await pocket.api(`c/${conversation}/artifacts`);
|
|
70
|
+
|
|
71
|
+
if (current(conversation) && mine === listing) {
|
|
72
|
+
store.set({ listed: artifacts });
|
|
73
|
+
}
|
|
74
|
+
} catch (error) {
|
|
75
|
+
if (current(conversation) && mine === listing) {
|
|
76
|
+
store.set({ listed: store.get().listed ?? [] });
|
|
77
|
+
console.warn("Design: could not read the list", error);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
/** Read one design's comments and this person's tweaks. */
|
|
83
|
+
async notes(artifact) {
|
|
84
|
+
const conversation = conversationOf();
|
|
85
|
+
|
|
86
|
+
if (conversation === null || !artifact) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
const result = await pocket.api(
|
|
92
|
+
`c/${conversation}/designs/${encodeURIComponent(artifact)}`,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
if (current(conversation)) {
|
|
96
|
+
store.set((state) => ({
|
|
97
|
+
notes: { ...state.notes, [artifact]: { ...result, loaded: true } },
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (current(conversation) && error?.status !== 404) {
|
|
102
|
+
console.warn("Design: could not read the comments", error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
async addComment(artifact, version, target, text) {
|
|
108
|
+
const conversation = conversationOf();
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
await pocket.api(
|
|
112
|
+
`c/${conversation}/designs/${encodeURIComponent(artifact)}/comments`,
|
|
113
|
+
{
|
|
114
|
+
version,
|
|
115
|
+
target,
|
|
116
|
+
text,
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
await data.notes(artifact);
|
|
120
|
+
|
|
121
|
+
return true;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
fail(error);
|
|
124
|
+
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
async patchComment(artifact, comment, fields) {
|
|
130
|
+
const conversation = conversationOf();
|
|
131
|
+
|
|
132
|
+
try {
|
|
133
|
+
await pocket.api(
|
|
134
|
+
`c/${conversation}/designs/${encodeURIComponent(artifact)}/comments/${comment}`,
|
|
135
|
+
fields,
|
|
136
|
+
);
|
|
137
|
+
await data.notes(artifact);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
fail(error);
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
async removeComment(artifact, comment) {
|
|
144
|
+
const conversation = conversationOf();
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
await pocket.api(
|
|
148
|
+
`c/${conversation}/designs/${encodeURIComponent(artifact)}/comments/${comment}`,
|
|
149
|
+
undefined,
|
|
150
|
+
{ method: "DELETE" },
|
|
151
|
+
);
|
|
152
|
+
await data.notes(artifact);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
fail(error);
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
async resolveMany(artifact, ids) {
|
|
159
|
+
const conversation = conversationOf();
|
|
160
|
+
|
|
161
|
+
try {
|
|
162
|
+
await pocket.api(
|
|
163
|
+
`c/${conversation}/designs/${encodeURIComponent(artifact)}/resolve`,
|
|
164
|
+
{
|
|
165
|
+
ids,
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
await data.notes(artifact);
|
|
169
|
+
} catch (error) {
|
|
170
|
+
fail(error);
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
/** Save this person's tweaks for a design, a moment after the last change. */
|
|
175
|
+
saveTweaks(artifact, values) {
|
|
176
|
+
const conversation = conversationOf();
|
|
177
|
+
|
|
178
|
+
clearTimeout(data.saving?.[artifact]);
|
|
179
|
+
data.saving ??= {};
|
|
180
|
+
data.saving[artifact] = setTimeout(() => {
|
|
181
|
+
pocket
|
|
182
|
+
.api(`c/${conversation}/designs/${encodeURIComponent(artifact)}/tweaks`, {
|
|
183
|
+
values: values === null || Object.keys(values).length === 0 ? null : values,
|
|
184
|
+
})
|
|
185
|
+
.catch((error) => {
|
|
186
|
+
if (error?.status !== 403) {
|
|
187
|
+
console.warn("Design: could not save the tweaks", error);
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}, 600);
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
/** "Go back to this one": a new version with the content of `version`. Returns the new version number. */
|
|
194
|
+
async restore(artifact, version) {
|
|
195
|
+
const conversation = conversationOf();
|
|
196
|
+
const key = crypto.randomUUID();
|
|
197
|
+
|
|
198
|
+
try {
|
|
199
|
+
const result = await pocket.api(
|
|
200
|
+
`c/${conversation}/designs/${encodeURIComponent(artifact)}/restore`,
|
|
201
|
+
{ version, key },
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
await data.list();
|
|
205
|
+
|
|
206
|
+
return result.version;
|
|
207
|
+
} catch (error) {
|
|
208
|
+
fail(error);
|
|
209
|
+
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Make a file on the server (PNG, PDF, PPTX, or an HTML zip: `design/index.ts`'s export route writes it in the session's
|
|
216
|
+
* `exports/` folder), then save it through core's download route.
|
|
217
|
+
*/
|
|
218
|
+
async download({ artifact, version, format, viewport }) {
|
|
219
|
+
const conversation = conversationOf();
|
|
220
|
+
|
|
221
|
+
try {
|
|
222
|
+
const made = await pocket.api(`c/${conversation}/export`, {
|
|
223
|
+
id: artifact,
|
|
224
|
+
version,
|
|
225
|
+
format,
|
|
226
|
+
...(viewport ? { viewport } : {}),
|
|
227
|
+
});
|
|
228
|
+
// The core route streams the file from the session's folder as an attachment.
|
|
229
|
+
const link = document.createElement("a");
|
|
230
|
+
|
|
231
|
+
link.href = `/api/c/${encodeURIComponent(conversation)}/download?${new URLSearchParams({ path: made.relative })}`;
|
|
232
|
+
link.download = made.name;
|
|
233
|
+
document.body.append(link);
|
|
234
|
+
link.click();
|
|
235
|
+
link.remove();
|
|
236
|
+
pocket.notify("info", `Saved ${made.name}. It is also at ${made.path}`);
|
|
237
|
+
|
|
238
|
+
return true;
|
|
239
|
+
} catch (error) {
|
|
240
|
+
fail(error);
|
|
241
|
+
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// Keep the panel's choice of design for a reload of this tab.
|
|
248
|
+
let picked = "";
|
|
249
|
+
|
|
250
|
+
store.subscribe((state) => {
|
|
251
|
+
if (state.conversation !== null && state.artifact !== null) {
|
|
252
|
+
const pick = { artifact: state.artifact, version: state.version };
|
|
253
|
+
const text = `${state.conversation}:${JSON.stringify(pick)}`;
|
|
254
|
+
|
|
255
|
+
if (text !== picked) {
|
|
256
|
+
picked = text;
|
|
257
|
+
savePick(state.conversation, pick);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
return data;
|
|
263
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Frame sizes and zoom for the canvas: pure functions, no DOM, so a test can run them.
|
|
2
|
+
|
|
3
|
+
/** The sizes the canvas shows a design at. `slide` is for decks. */
|
|
4
|
+
export const DEVICES = {
|
|
5
|
+
mobile: { label: "Phone", w: 390, h: 844, icon: "phone" },
|
|
6
|
+
tablet: { label: "Tablet", w: 820, h: 1180, icon: "tablet" },
|
|
7
|
+
desktop: { label: "Desktop", w: 1280, h: 800, icon: "monitor" },
|
|
8
|
+
slide: { label: "Slide", w: 1280, h: 720, icon: "monitor" },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** The sizes an export asks for (`viewport` of `design_export`): a custom or slide size exports as the nearest. */
|
|
12
|
+
export function exportViewport(device, size) {
|
|
13
|
+
if (device === "mobile" || device === "tablet" || device === "desktop") {
|
|
14
|
+
return device;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return size.w <= 500 ? "mobile" : size.w <= 1000 ? "tablet" : "desktop";
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const MIN_SIZE = 200;
|
|
21
|
+
export const MAX_SIZE = 4000;
|
|
22
|
+
|
|
23
|
+
export const clampSize = (value, fallback = 800) => {
|
|
24
|
+
const number = Math.round(Number(value));
|
|
25
|
+
|
|
26
|
+
return Number.isFinite(number) ? Math.min(MAX_SIZE, Math.max(MIN_SIZE, number)) : fallback;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** The pixels a device name stands for: `custom` is the person's own. */
|
|
30
|
+
export function sizeOf(device, custom) {
|
|
31
|
+
if (device === "custom") {
|
|
32
|
+
return { w: clampSize(custom?.w, 800), h: clampSize(custom?.h, 600) };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const known = DEVICES[device] ?? DEVICES.desktop;
|
|
36
|
+
|
|
37
|
+
return { w: known.w, h: known.h };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The zoom steps the buttons walk through. */
|
|
41
|
+
export const ZOOMS = [0.25, 0.33, 0.5, 0.67, 0.75, 1, 1.25, 1.5, 2];
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* How much a frame of `size` is scaled in an area of `room` pixels: `zoom` is "fit" (as large as fits, never past
|
|
45
|
+
* 100%) or a number. `gap` is the margin kept around the frame when it fits.
|
|
46
|
+
*/
|
|
47
|
+
export function scaleOf(zoom, size, room, gap = 16) {
|
|
48
|
+
if (zoom !== "fit") {
|
|
49
|
+
return Number(zoom) > 0 ? Number(zoom) : 1;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (!(room.w > 0) || !(room.h > 0)) {
|
|
53
|
+
return 1;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const fit = Math.min((room.w - gap * 2) / size.w, (room.h - gap * 2) / size.h, 1);
|
|
57
|
+
|
|
58
|
+
return Math.max(0.1, Math.floor(fit * 100) / 100);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The next zoom step up (`direction` 1) or down (-1) from `scale`, as a number: it leaves "fit". */
|
|
62
|
+
export function stepZoom(scale, direction) {
|
|
63
|
+
if (direction > 0) {
|
|
64
|
+
return ZOOMS.find((step) => step > scale + 0.005) ?? ZOOMS.at(-1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return [...ZOOMS].reverse().find((step) => step < scale - 0.005) ?? ZOOMS[0];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const percent = (scale) => `${Math.round(scale * 100)}%`;
|