@stefan.inc/feedback-engine 0.0.0-stage → 1.0.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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -2
  3. package/package.json +57 -5
  4. package/src/core/annotations.ts +104 -0
  5. package/src/core/cardLayout.ts +82 -0
  6. package/src/core/describe.ts +143 -0
  7. package/src/core/index.ts +9 -0
  8. package/src/core/model.ts +133 -0
  9. package/src/core/problem.ts +51 -0
  10. package/src/core/report.ts +74 -0
  11. package/src/core/session.ts +184 -0
  12. package/src/core/taskTitle.ts +35 -0
  13. package/src/core/tree.ts +117 -0
  14. package/src/dom/capture.ts +78 -0
  15. package/src/dom/image.ts +316 -0
  16. package/src/dom/procrastimate.ts +205 -0
  17. package/src/dom/scene.ts +286 -0
  18. package/src/dom/screenshot.ts +118 -0
  19. package/src/dom/share.ts +71 -0
  20. package/src/dom/tags.ts +21 -0
  21. package/src/dom/webhook.ts +89 -0
  22. package/src/dom/zip.ts +52 -0
  23. package/src/editor/Button.svelte +125 -0
  24. package/src/editor/Editor.svelte +1078 -0
  25. package/src/editor/Host.svelte +31 -0
  26. package/src/editor/ItemsSheet.svelte +233 -0
  27. package/src/editor/Pill.svelte +65 -0
  28. package/src/editor/ProblemCard.svelte +67 -0
  29. package/src/editor/ProcrastimateSheet.svelte +304 -0
  30. package/src/editor/WebhookSheet.svelte +132 -0
  31. package/src/editor/engine.ts +154 -0
  32. package/src/editor/setup.ts +106 -0
  33. package/src/next/index.d.ts +13 -0
  34. package/src/next/index.js +69 -0
  35. package/src/next/loader.js +23 -0
  36. package/src/react/components.ts +101 -0
  37. package/src/react/index.ts +44 -0
  38. package/src/react/public.d.ts +107 -0
  39. package/src/svelte/index.ts +32 -0
  40. package/src/vite/index.d.ts +44 -0
  41. package/src/vite/index.js +207 -0
  42. package/src/vite/react.js +172 -0
@@ -0,0 +1,31 @@
1
+ <svelte:options css="injected" />
2
+
3
+ <script lang="ts">
4
+ import Button from './Button.svelte';
5
+ import Editor from './Editor.svelte';
6
+ import type { Engine } from './engine.ts';
7
+ import Pill from './Pill.svelte';
8
+
9
+ let { engine }: { engine: Engine } = $props();
10
+
11
+ // The engine is not reactive by itself, so it reports each change and this reads it again.
12
+ let version = $state(0);
13
+ // svelte-ignore state_referenced_locally
14
+ engine.onchange = () => (version += 1);
15
+ const shown = $derived.by(() => {
16
+ void version;
17
+ return { pill: engine.pill, remaining: engine.remaining, screen: engine.screen };
18
+ });
19
+ </script>
20
+
21
+ {#if engine.options.button && !shown.screen}
22
+ <Button onpress={() => void engine.present()} />
23
+ {/if}
24
+ {#if shown.pill}
25
+ <Pill remaining={shown.remaining} onpress={() => void engine.present()} />
26
+ {/if}
27
+ {#if shown.screen}
28
+ {#key shown.screen}
29
+ <Editor {engine} screen={shown.screen} />
30
+ {/key}
31
+ {/if}
@@ -0,0 +1,233 @@
1
+ <svelte:options css="injected" />
2
+
3
+ <script lang="ts">
4
+ import { paletteColor, title, type Session } from '../core/index.ts';
5
+
6
+ let {
7
+ session,
8
+ version,
9
+ onclose,
10
+ onselect,
11
+ onremove,
12
+ }: {
13
+ session: Session;
14
+ /** Changes whenever the session does, since the session is not reactive by itself. */
15
+ version: number;
16
+ onclose: () => void;
17
+ onselect: (id: number) => void;
18
+ onremove: (id: string) => void;
19
+ } = $props();
20
+
21
+ // svelte-ignore state_referenced_locally
22
+ const { capture } = session;
23
+ const items = $derived.by(() => {
24
+ void version;
25
+ return session.items.map(item => ({
26
+ ...item,
27
+ number: session.annotations.number(item.target),
28
+ color: paletteColor(session.colorIndex(item.target)),
29
+ segments: session.annotations.segments(item.text),
30
+ }));
31
+ });
32
+ </script>
33
+
34
+ <div class="scrim" role="presentation" onclick={event => event.target === event.currentTarget && onclose()}>
35
+ <div class="sheet" role="dialog" tabindex="-1" aria-label="Feedback list">
36
+ <header>
37
+ <h2>Feedback</h2>
38
+ <button type="button" class="done" onclick={onclose}>Done</button>
39
+ </header>
40
+ <div class="scroll">
41
+ {#if !items.length}
42
+ <p class="empty">No feedback yet. Click a component, type, and press the arrow.</p>
43
+ {/if}
44
+ <ul>
45
+ {#each items as item (item.id)}
46
+ <li>
47
+ <button type="button" class="row" onclick={() => onselect(item.target)}>
48
+ <span class="number" style="background:{item.color}">{item.number}</span>
49
+ <span class="text">
50
+ <strong>{title(capture.nodes[item.target]!)}</strong>
51
+ <span>
52
+ {#each item.segments as segment, index (index)}
53
+ {#if segment.reference}<span
54
+ class="token"
55
+ style="background:{paletteColor(session.colorIndex(segment.reference.target))}"
56
+ >{segment.text}</span
57
+ >{:else}{segment.text}{/if}
58
+ {/each}
59
+ </span>
60
+ </span>
61
+ </button>
62
+ <button
63
+ type="button"
64
+ class="remove"
65
+ aria-label="Remove item {item.number}"
66
+ onclick={() => onremove(item.id)}
67
+ >
68
+ <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 7h14M10 7V4h4v3M7 7l1 13h8l1-13" /></svg>
69
+ </button>
70
+ </li>
71
+ {/each}
72
+ </ul>
73
+ <h3>Attached context</h3>
74
+ {#each [...capture.environment, ...capture.context] as entry, index (index)}
75
+ <details>
76
+ <summary>{entry.name}</summary>
77
+ <pre>{entry.value}</pre>
78
+ </details>
79
+ {/each}
80
+ </div>
81
+ </div>
82
+ </div>
83
+
84
+ <style>
85
+ .scrim {
86
+ position: absolute;
87
+ inset: 0;
88
+ z-index: 5;
89
+ display: grid;
90
+ place-items: end center;
91
+ background: rgb(0 0 0 / 0.5);
92
+ }
93
+
94
+ .sheet {
95
+ display: flex;
96
+ flex-direction: column;
97
+ width: min(100%, 560px);
98
+ max-height: 80%;
99
+ border-radius: 16px 16px 0 0;
100
+ box-sizing: border-box;
101
+ background: #1f1f1f;
102
+ }
103
+
104
+ header {
105
+ display: flex;
106
+ align-items: center;
107
+ justify-content: space-between;
108
+ padding: 14px 16px 8px;
109
+ }
110
+
111
+ h2 {
112
+ margin: 0;
113
+ font-size: 17px;
114
+ }
115
+
116
+ h3 {
117
+ margin: 20px 0 6px;
118
+ color: #aeaeb2;
119
+ font-size: 12px;
120
+ font-weight: 600;
121
+ text-transform: uppercase;
122
+ }
123
+
124
+ button {
125
+ border: 0;
126
+ background: none;
127
+ color: inherit;
128
+ font: inherit;
129
+ cursor: pointer;
130
+ }
131
+
132
+ .done {
133
+ color: #0a84ff;
134
+ font-weight: 600;
135
+ }
136
+
137
+ .scroll {
138
+ padding: 0 16px 16px;
139
+ overflow: auto;
140
+ }
141
+
142
+ .empty {
143
+ color: #aeaeb2;
144
+ }
145
+
146
+ ul {
147
+ margin: 0;
148
+ padding: 0;
149
+ list-style: none;
150
+ }
151
+
152
+ li {
153
+ display: flex;
154
+ align-items: flex-start;
155
+ gap: 4px;
156
+ border-bottom: 1px solid rgb(255 255 255 / 0.08);
157
+ }
158
+
159
+ .row {
160
+ display: flex;
161
+ flex: 1;
162
+ gap: 10px;
163
+ min-width: 0;
164
+ padding: 10px 0;
165
+ text-align: left;
166
+ }
167
+
168
+ .number {
169
+ display: grid;
170
+ flex: none;
171
+ place-items: center;
172
+ width: 22px;
173
+ height: 22px;
174
+ border-radius: 11px;
175
+ font-size: 12px;
176
+ font-weight: 700;
177
+ }
178
+
179
+ .text {
180
+ display: grid;
181
+ gap: 2px;
182
+ min-width: 0;
183
+ white-space: pre-wrap;
184
+ }
185
+
186
+ .token {
187
+ border-radius: 3px;
188
+ padding: 0 4px;
189
+ font-weight: 600;
190
+ }
191
+
192
+ .remove {
193
+ padding: 10px 4px;
194
+ color: #ff453a;
195
+ }
196
+
197
+ svg {
198
+ width: 18px;
199
+ height: 18px;
200
+ fill: none;
201
+ stroke: currentColor;
202
+ stroke-width: 2;
203
+ stroke-linecap: round;
204
+ stroke-linejoin: round;
205
+ }
206
+
207
+ summary {
208
+ padding: 8px 0;
209
+ cursor: pointer;
210
+ }
211
+
212
+ pre {
213
+ margin: 0 0 8px;
214
+ color: #aeaeb2;
215
+ font:
216
+ 12px/1.35 ui-monospace,
217
+ 'SF Mono',
218
+ Menlo,
219
+ monospace;
220
+ white-space: pre-wrap;
221
+ overflow-wrap: anywhere;
222
+ }
223
+
224
+ @media (min-width: 900px) {
225
+ .scrim {
226
+ place-items: center;
227
+ }
228
+
229
+ .sheet {
230
+ border-radius: 16px;
231
+ }
232
+ }
233
+ </style>
@@ -0,0 +1,65 @@
1
+ <svelte:options css="injected" />
2
+
3
+ <script lang="ts">
4
+ let { remaining, onpress }: { remaining: number | undefined; onpress: () => void } = $props();
5
+ </script>
6
+
7
+ <!-- A click must not take focus from the field the user was typing in. -->
8
+ <button class="pill" type="button" onmousedown={event => event.preventDefault()} onclick={onpress}>
9
+ <svg viewBox="0 0 256 256" aria-hidden="true">
10
+ <path d="M40 56h120l24 24v64H96l-32 28v-28H40z" />
11
+ <path d="M112 172v20h64l32 28v-28h20v-88l-16-16" />
12
+ </svg>
13
+ {remaining === undefined ? 'Add Feedback' : `Feedback in ${remaining}`}
14
+ </button>
15
+
16
+ <style>
17
+ .pill {
18
+ position: fixed;
19
+ top: max(10px, env(safe-area-inset-top));
20
+ left: 50%;
21
+ z-index: 2147483647;
22
+ display: flex;
23
+ align-items: center;
24
+ gap: 8px;
25
+ border: 1px solid rgb(255 255 255 / 0.35);
26
+ border-radius: 999px;
27
+ padding: 10px 18px 10px 14px;
28
+ background: rgb(40 40 44 / 0.72);
29
+ backdrop-filter: blur(18px) saturate(1.6);
30
+ -webkit-backdrop-filter: blur(18px) saturate(1.6);
31
+ box-shadow: 0 8px 28px rgb(0 0 0 / 0.28);
32
+ color: white;
33
+ font:
34
+ 600 15px/1 system-ui,
35
+ sans-serif;
36
+ /* The pill keeps its width while a countdown's digit changes. */
37
+ font-variant-numeric: tabular-nums;
38
+ cursor: pointer;
39
+ translate: -50% 0;
40
+ animation: drop 0.35s cubic-bezier(0.2, 0.9, 0.3, 1.2);
41
+ }
42
+
43
+ svg {
44
+ width: 22px;
45
+ height: 22px;
46
+ fill: none;
47
+ stroke: currentColor;
48
+ stroke-width: 16;
49
+ stroke-linejoin: round;
50
+ }
51
+
52
+ @keyframes drop {
53
+ from {
54
+ opacity: 0;
55
+ scale: 0.5 0.6;
56
+ translate: -50% -24px;
57
+ }
58
+ }
59
+
60
+ @media (prefers-reduced-motion: reduce) {
61
+ .pill {
62
+ animation: none;
63
+ }
64
+ }
65
+ </style>
@@ -0,0 +1,67 @@
1
+ <svelte:options css="injected" />
2
+
3
+ <script lang="ts">
4
+ import type { Problem } from '../core/index.ts';
5
+
6
+ let { problem }: { problem: Problem } = $props();
7
+ </script>
8
+
9
+ <div class="problem" role="alert">
10
+ <p class="message">{problem.message}</p>
11
+ {#if problem.facts.length}
12
+ <details>
13
+ <summary>Details</summary>
14
+ <pre>{problem.details}</pre>
15
+ </details>
16
+ {/if}
17
+ <button
18
+ type="button"
19
+ onclick={async event => {
20
+ const button = event.currentTarget;
21
+ await navigator.clipboard.writeText(problem.text);
22
+ button.textContent = 'Copied';
23
+ setTimeout(() => (button.textContent = 'Copy Error'), 2000);
24
+ }}>Copy Error</button
25
+ >
26
+ </div>
27
+
28
+ <style>
29
+ .problem {
30
+ display: grid;
31
+ justify-items: start;
32
+ gap: 6px;
33
+ border-radius: 10px;
34
+ padding: 10px 12px;
35
+ background: #2c2c2e;
36
+ }
37
+
38
+ .message {
39
+ margin: 0;
40
+ color: #ff453a;
41
+ }
42
+
43
+ details {
44
+ width: 100%;
45
+ }
46
+
47
+ summary {
48
+ color: #aeaeb2;
49
+ cursor: pointer;
50
+ }
51
+
52
+ pre {
53
+ margin: 6px 0 0;
54
+ font-size: 12px;
55
+ white-space: pre-wrap;
56
+ overflow-wrap: anywhere;
57
+ user-select: text;
58
+ }
59
+
60
+ button {
61
+ border: 0;
62
+ background: none;
63
+ color: #0a84ff;
64
+ font: inherit;
65
+ cursor: pointer;
66
+ }
67
+ </style>
@@ -0,0 +1,304 @@
1
+ <svelte:options css="injected" />
2
+
3
+ <script lang="ts">
4
+ import { Problem, taskTitle, type Fact, type FeedbackItem, type Session, type Toast } from '../core/index.ts';
5
+ import type { Screen } from '../dom/capture.ts';
6
+ import { annotatedImage } from '../dom/image.ts';
7
+ import {
8
+ cachedProjects,
9
+ lastProject,
10
+ loadProjects,
11
+ postTask,
12
+ taskLink,
13
+ type Place,
14
+ type ProcrastimateConfig,
15
+ } from '../dom/procrastimate.ts';
16
+ import ProblemCard from './ProblemCard.svelte';
17
+
18
+ let {
19
+ screen,
20
+ session,
21
+ config,
22
+ onclose,
23
+ onposted,
24
+ }: {
25
+ screen: Screen;
26
+ session: Session;
27
+ config: ProcrastimateConfig;
28
+ onclose: () => void;
29
+ onposted: (items: FeedbackItem[], toast: Toast) => void;
30
+ } = $props();
31
+
32
+ // svelte-ignore state_referenced_locally
33
+ const names = taskTitle(session.capture, session.items);
34
+ let name = $state(names.placeholder);
35
+ let projects = $state(cachedProjects());
36
+ let place = $state<Place>({ projectId: lastProject() });
37
+ let loading = $state(true);
38
+ let loadProblem = $state<Problem>();
39
+ let postProblem = $state<Problem>();
40
+ /** What the post is doing, while it runs. */
41
+ let step = $state<string>();
42
+ const posting = $derived(step !== undefined);
43
+ let status = $state<HTMLElement>();
44
+ // svelte-ignore state_referenced_locally
45
+ const environment: Fact[] = session.capture.environment.map(entry => ({ label: entry.name, value: entry.value }));
46
+
47
+ $effect(() => {
48
+ loadProjects(config)
49
+ .then(loaded => {
50
+ projects = loaded;
51
+ const project = loaded.find(candidate => candidate.id === place.projectId);
52
+ if (!project) place = {};
53
+ else if (!project.sections.some(section => section.id === place.sectionId)) place = { projectId: project.id };
54
+ })
55
+ .catch(
56
+ (error: unknown) =>
57
+ (loadProblem = Problem.from(error).with([{ label: 'Step', value: 'Loading the projects' }], environment)),
58
+ )
59
+ .finally(() => (loading = false));
60
+ });
61
+
62
+ async function post() {
63
+ step = 'Drawing the image';
64
+ postProblem = undefined;
65
+ const started = performance.now();
66
+ const items = [...session.items];
67
+ const project = projects.find(candidate => candidate.id === place.projectId);
68
+ const section = project?.sections.find(candidate => candidate.id === place.sectionId);
69
+ const report = session.compactJSON;
70
+ const facts: Fact[] = [
71
+ {
72
+ label: 'Place',
73
+ value: [project ? `${project.name} (${project.id})` : 'Inbox', section && `${section.name} (${section.id})`]
74
+ .filter(Boolean)
75
+ .join(' / '),
76
+ },
77
+ { label: 'Report', value: `${new TextEncoder().encode(report).length} bytes` },
78
+ { label: 'Server', value: config.url },
79
+ ];
80
+ try {
81
+ const image = annotatedImage(screen, session.annotations);
82
+ facts.push({ label: 'Image', value: `${image.width} × ${image.height} px` });
83
+ const id = await postTask(config, name.trim(), place, image, report, text => (step = text));
84
+ const destination = [project?.name ?? 'Inbox', section?.name].filter(Boolean).join(' / ');
85
+ onposted(items, { text: `Added to ${destination} in ProcrastiMate`, link: taskLink(config, id) });
86
+ } catch (error) {
87
+ const elapsed = { label: 'Elapsed', value: `${((performance.now() - started) / 1000).toFixed(1)} s` };
88
+ postProblem = Problem.from(error).with(
89
+ [{ label: 'Step', value: step ?? 'Posting' }, elapsed],
90
+ [...facts, ...environment],
91
+ );
92
+ step = undefined;
93
+ }
94
+ }
95
+
96
+ // Above the project list, which can be longer than the sheet: an error under it goes unseen.
97
+ $effect(() => {
98
+ if (postProblem || step) status?.scrollIntoView({ block: 'nearest' });
99
+ });
100
+ </script>
101
+
102
+ {#snippet row(label: string, picked: boolean, indented: boolean, pick: () => void)}
103
+ <button
104
+ type="button"
105
+ class="row"
106
+ class:indented
107
+ role="option"
108
+ aria-selected={picked}
109
+ disabled={posting}
110
+ onclick={pick}
111
+ >
112
+ <span>{label}</span>
113
+ {#if picked}<span class="check" aria-hidden="true">✓</span>{/if}
114
+ </button>
115
+ {/snippet}
116
+
117
+ <div class="scrim" role="presentation">
118
+ <form
119
+ class="sheet"
120
+ aria-label="Add to ProcrastiMate"
121
+ onsubmit={event => {
122
+ event.preventDefault();
123
+ void post();
124
+ }}
125
+ >
126
+ <header>
127
+ <button type="button" class="link" disabled={posting} onclick={onclose}>Cancel</button>
128
+ <h2>Add to ProcrastiMate</h2>
129
+ <button type="submit" class="link strong" disabled={posting || !name.trim()}>{posting ? 'Adding…' : 'Add'}</button
130
+ >
131
+ </header>
132
+ <div class="scroll">
133
+ <div class="status" bind:this={status}>
134
+ {#if step}<p class="step" role="status">{step}…</p>{/if}
135
+ {#if postProblem}<ProblemCard problem={postProblem} />{/if}
136
+ {#if loadProblem}<ProblemCard problem={loadProblem} />{/if}
137
+ </div>
138
+ <label>
139
+ <span class="caption">Name</span>
140
+ <input bind:value={name} aria-label="Task name" disabled={posting} />
141
+ </label>
142
+ <span class="caption">Project{loading ? ' · loading…' : ''}</span>
143
+ <div class="list" role="listbox" aria-label="Project">
144
+ {@render row('Inbox', !place.projectId, false, () => (place = {}))}
145
+ {#each projects as project (project.id)}
146
+ {@const open = project.id === place.projectId && project.sections.length > 0}
147
+ {@render row(
148
+ project.name,
149
+ project.id === place.projectId && !open,
150
+ false,
151
+ () => (place = { projectId: project.id }),
152
+ )}
153
+ {#if open}
154
+ {@render row('No section', !place.sectionId, true, () => (place = { projectId: project.id }))}
155
+ {#each project.sections as section (section.id)}
156
+ {@render row(
157
+ section.name,
158
+ place.sectionId === section.id,
159
+ true,
160
+ () => (place = { projectId: project.id, sectionId: section.id }),
161
+ )}
162
+ {/each}
163
+ {/if}
164
+ {/each}
165
+ </div>
166
+ </div>
167
+ </form>
168
+ </div>
169
+
170
+ <style>
171
+ .scrim {
172
+ position: absolute;
173
+ inset: 0;
174
+ z-index: 5;
175
+ display: grid;
176
+ place-items: end center;
177
+ background: rgb(0 0 0 / 0.5);
178
+ }
179
+
180
+ .sheet {
181
+ display: flex;
182
+ flex-direction: column;
183
+ width: min(100%, 480px);
184
+ max-height: 85%;
185
+ border-radius: 16px 16px 0 0;
186
+ box-sizing: border-box;
187
+ background: #1f1f1f;
188
+ }
189
+
190
+ header {
191
+ display: flex;
192
+ align-items: center;
193
+ gap: 8px;
194
+ padding: 14px 16px 8px;
195
+ }
196
+
197
+ h2 {
198
+ flex: 1;
199
+ margin: 0;
200
+ font-size: 16px;
201
+ text-align: center;
202
+ }
203
+
204
+ button,
205
+ input {
206
+ color: inherit;
207
+ font: inherit;
208
+ }
209
+
210
+ button {
211
+ border: 0;
212
+ background: none;
213
+ cursor: pointer;
214
+ }
215
+
216
+ button:disabled {
217
+ cursor: default;
218
+ opacity: 0.5;
219
+ }
220
+
221
+ .link {
222
+ color: #0a84ff;
223
+ }
224
+
225
+ .strong {
226
+ font-weight: 600;
227
+ }
228
+
229
+ .scroll {
230
+ display: grid;
231
+ gap: 8px;
232
+ padding: 8px 16px 16px;
233
+ overflow: auto;
234
+ }
235
+
236
+ .caption {
237
+ color: #aeaeb2;
238
+ font-size: 12px;
239
+ font-weight: 600;
240
+ text-transform: uppercase;
241
+ }
242
+
243
+ label {
244
+ display: grid;
245
+ gap: 6px;
246
+ margin-bottom: 8px;
247
+ }
248
+
249
+ input {
250
+ border: 0;
251
+ border-radius: 10px;
252
+ padding: 10px 12px;
253
+ background: #2e2e30;
254
+ }
255
+
256
+ .list {
257
+ display: grid;
258
+ border-radius: 10px;
259
+ background: #2c2c2e;
260
+ overflow: hidden;
261
+ }
262
+
263
+ .row {
264
+ display: flex;
265
+ justify-content: space-between;
266
+ gap: 8px;
267
+ border-bottom: 1px solid rgb(255 255 255 / 0.08);
268
+ padding: 11px 12px;
269
+ text-align: left;
270
+ }
271
+
272
+ .row.indented {
273
+ padding-left: 40px;
274
+ }
275
+
276
+ .check {
277
+ color: #0a84ff;
278
+ font-weight: 700;
279
+ }
280
+
281
+ .status {
282
+ display: grid;
283
+ gap: 8px;
284
+ }
285
+
286
+ .status:empty {
287
+ display: none;
288
+ }
289
+
290
+ .step {
291
+ margin: 0;
292
+ color: #aeaeb2;
293
+ }
294
+
295
+ @media (min-width: 900px) {
296
+ .scrim {
297
+ place-items: center;
298
+ }
299
+
300
+ .sheet {
301
+ border-radius: 16px;
302
+ }
303
+ }
304
+ </style>