@stefan.inc/feedback-engine 1.8.0 → 1.10.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/README.md +13 -14
- package/package.json +1 -1
- package/src/dom/procrastimate.ts +18 -218
- package/src/dom/procrastimatePopup.ts +194 -0
- package/src/editor/Editor.svelte +45 -25
- package/src/editor/WebhookSheet.svelte +1 -0
- package/src/editor/engine.ts +2 -2
- package/src/editor/setup.ts +3 -13
- package/src/react/public.d.ts +11 -10
- package/src/vite/index.d.ts +0 -20
- package/src/vite/index.js +0 -73
- package/src/editor/ProcrastimateSheet.svelte +0 -257
package/README.md
CHANGED
|
@@ -198,7 +198,7 @@ export function Feedback() {
|
|
|
198
198
|
- Client components are in the capture under their own names, as in a Vite app. A server component leaves no component in the page, so it is not a node. The elements it draws are, each with its own `file:line`.
|
|
199
199
|
- On a deployed preview, such as a Vercel preview link, pass `withFeedbackEngine(config, { elementSources: true })` so a production build keeps every element's `file:line`. Components keep theirs in every build without it. The attribute puts your file paths in the page's HTML, so leave it off a public production deploy, for example with `elementSources: process.env.VERCEL_ENV === 'preview'`.
|
|
200
200
|
- To keep Feedback off a production deploy, render `<Feedback />` only where you want it, for example behind `process.env.NEXT_PUBLIC_VERCEL_ENV !== 'production'`.
|
|
201
|
-
-
|
|
201
|
+
- [Post to ProcrastiMate](#post-to-procrastimate) works the same in Next.js as in Vite: pass `procrastimate: { projects: ['Your Project'] }` to `setupFeedbackEngine()`.
|
|
202
202
|
|
|
203
203
|
## Component data and untagged content
|
|
204
204
|
|
|
@@ -227,25 +227,24 @@ The editor works as on iOS, with these differences:
|
|
|
227
227
|
|
|
228
228
|
## Post to ProcrastiMate
|
|
229
229
|
|
|
230
|
-
Optional, and off until
|
|
230
|
+
Optional, and off until you configure it. [ProcrastiMate](https://github.com/smirea/procrastimate) is the task app that Feedback's author files feedback into.
|
|
231
231
|
|
|
232
|
-
|
|
233
|
-
// vite.config.ts
|
|
234
|
-
import { feedbackProcrastimate } from '@stefan.inc/feedback-engine/vite';
|
|
232
|
+
Pass the whitelisted `projects` to `setupFeedbackEngine()`:
|
|
235
233
|
|
|
236
|
-
|
|
237
|
-
|
|
234
|
+
```ts
|
|
235
|
+
setupFeedbackEngine({
|
|
236
|
+
app: 'My App',
|
|
237
|
+
procrastimate: { projects: ['Website'] },
|
|
238
238
|
});
|
|
239
239
|
```
|
|
240
240
|
|
|
241
|
-
|
|
241
|
+
Choosing **ProcrastiMate** opens a popup on `https://procrastimate.stf.lol/feedback` (or `procrastimate.app`) and hands over the annotated screenshot and `feedback.json` via `postMessage`. Because the popup runs on ProcrastiMate's own origin, it uses whoever is signed into ProcrastiMate in that browser (or lets them sign in there) with no hardcoded token, no backend proxy, and no CORS setup, so it works on deployed static sites, Vite, and Next.js alike. Anyone with an `edit` share on a listed project posts under their own account.
|
|
242
242
|
|
|
243
|
-
-
|
|
244
|
-
- `
|
|
245
|
-
-
|
|
246
|
-
- `
|
|
247
|
-
-
|
|
248
|
-
- For a proxy of your own, pass its address to the page instead: `setupFeedbackEngine({ procrastimate: { url: '/my-proxy' } })`.
|
|
243
|
+
- `projects` is required and non-empty: the only projects Feedback posts to, by name (ignoring case) or id, in the picker's order. There is no way to allow every project, and the Inbox is never offered. ProcrastiMate's `/feedback` popup enforces the list and reports any missing name above the picker.
|
|
244
|
+
- `project` names the project the picker opens on, by name (ignoring case) or id, so an app's feedback lands in its own project. It has to be one of `projects`. Without it, the picker opens on the last project posted to, or the first listed.
|
|
245
|
+
- `space` names another user's ProcrastiMate user id when two spaces share the same project name; leave it out to match across the signed-in user's own and edit-shared projects.
|
|
246
|
+
- `app` points at another ProcrastiMate origin if you self-host or run locally.
|
|
247
|
+
- Each project in the picker has a ↗ link to its page in ProcrastiMate (`/projects/<id>`), which opens in a new tab.
|
|
249
248
|
|
|
250
249
|
## How capture works
|
|
251
250
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stefan.inc/feedback-engine",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "In-app feedback for Svelte and React: point at a component, write what should change, and export its name, file:line, and data for an agent.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"accessibility",
|
package/src/dom/procrastimate.ts
CHANGED
|
@@ -1,173 +1,34 @@
|
|
|
1
|
-
import { Problem } from '../core/problem.ts';
|
|
2
|
-
|
|
3
1
|
export type ProcrastimateConfig = {
|
|
4
|
-
/**
|
|
5
|
-
url: string;
|
|
6
|
-
/** Sent as a bearer token. Leave it out behind the proxy, which adds its own. */
|
|
7
|
-
token?: string;
|
|
8
|
-
/** The ProcrastiMate web app, for the link to a posted task. */
|
|
2
|
+
/** The ProcrastiMate web app whose `/feedback` popup opens. Defaults to `https://procrastimate.stf.lol`. */
|
|
9
3
|
app?: string;
|
|
10
4
|
/** The project the picker opens on, by name (ignoring case) or id, in place of the last one posted to. */
|
|
11
5
|
project?: string;
|
|
12
6
|
/**
|
|
13
|
-
* The only projects the
|
|
14
|
-
*
|
|
7
|
+
* The only projects the `/feedback` popup offers, by name (ignoring case) or id, in this order. The Inbox is not
|
|
8
|
+
* offered. Required, and not empty.
|
|
9
|
+
*/
|
|
10
|
+
projects: string[];
|
|
11
|
+
/**
|
|
12
|
+
* Another user's space (their ProcrastiMate user id) whose shared projects the popup lists. Leave it out to match
|
|
13
|
+
* across the signed-in user's own and edit-shared projects.
|
|
15
14
|
*/
|
|
16
|
-
|
|
15
|
+
space?: string;
|
|
17
16
|
};
|
|
18
17
|
|
|
19
|
-
export type Section = { id: string; name: string };
|
|
20
|
-
export type Project = { id: string; name: string; sections: Section[] };
|
|
21
|
-
/** Where a task goes: the Inbox without a project, and the project's top, above its sections, without a section. */
|
|
22
|
-
export type Place = { projectId?: string; sectionId?: string };
|
|
23
|
-
|
|
24
18
|
/** ProcrastiMate's `FILE_PART_BYTES` (`shared/images.ts` there): the most it takes as a file in one request. */
|
|
25
|
-
const fileMaxBytes = 16 * 1024 * 1024;
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const projectsKey = 'feedback.procrastimate.projects';
|
|
29
|
-
const lastProjectKey = 'feedback.procrastimate.project';
|
|
30
|
-
|
|
31
|
-
function stored<T>(key: string): T | undefined {
|
|
32
|
-
try {
|
|
33
|
-
const value = localStorage.getItem(key);
|
|
34
|
-
return value === null ? undefined : (JSON.parse(value) as T);
|
|
35
|
-
} catch {
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const store = (key: string, value: unknown) => {
|
|
41
|
-
try {
|
|
42
|
-
if (value === undefined) localStorage.removeItem(key);
|
|
43
|
-
else localStorage.setItem(key, JSON.stringify(value));
|
|
44
|
-
} catch {
|
|
45
|
-
// Storage can be off, as in a private window. The picker then opens empty.
|
|
46
|
-
}
|
|
47
|
-
};
|
|
19
|
+
export const fileMaxBytes = 16 * 1024 * 1024;
|
|
20
|
+
export const defaultProcrastimateApp = 'https://procrastimate.stf.lol';
|
|
48
21
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
/** The project of the last post. Its section is not kept, so a new post starts at the project's top. */
|
|
52
|
-
export const lastProject = () => stored<string>(lastProjectKey);
|
|
22
|
+
export const procrastimateAppURL = (config: ProcrastimateConfig) =>
|
|
23
|
+
(config.app ?? defaultProcrastimateApp).replace(/\/$/, '');
|
|
53
24
|
|
|
54
|
-
|
|
55
|
-
export const namedProject = (projects: Project[], name: string) =>
|
|
56
|
-
projects.find(project => project.id === name) ??
|
|
57
|
-
projects.find(project => project.name.toLowerCase() === name.toLowerCase());
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* The projects the picker lists: every one, or only those `names` lists, in its order. `missing` has the names
|
|
61
|
-
* ProcrastiMate does not have.
|
|
62
|
-
*/
|
|
63
|
-
export function listedProjects(all: Project[], names: string[] | undefined) {
|
|
64
|
-
if (!names) return { projects: all, missing: [] as string[] };
|
|
65
|
-
const projects: Project[] = [];
|
|
66
|
-
const missing: string[] = [];
|
|
67
|
-
for (const name of names) {
|
|
68
|
-
const project = namedProject(all, name);
|
|
69
|
-
if (!project) missing.push(name);
|
|
70
|
-
else if (!projects.includes(project)) projects.push(project);
|
|
71
|
-
}
|
|
72
|
-
return { projects, missing };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* The project the picker opens on, among the listed ones: the one the app names, else the last one posted to, else
|
|
77
|
-
* the first when the app lists its projects, as the Inbox is not offered then. `undefined` is the Inbox.
|
|
78
|
-
*/
|
|
79
|
-
export function startingProject(listed: Project[], config: ProcrastimateConfig, last: string | undefined) {
|
|
80
|
-
if (config.project) return namedProject(listed, config.project)?.id;
|
|
81
|
-
if (listed.some(project => project.id === last)) return last;
|
|
82
|
-
return config.projects ? listed[0]?.id : undefined;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const appURL = (config: ProcrastimateConfig) => (config.app ?? defaultApp).replace(/\/$/, '');
|
|
86
|
-
|
|
87
|
-
/** The project's page in the ProcrastiMate web app, which opens for someone signed in to its space. */
|
|
88
|
-
export const projectLink = (config: ProcrastimateConfig, id: string) =>
|
|
89
|
-
`${appURL(config)}/projects/${encodeURIComponent(id)}`;
|
|
90
|
-
|
|
91
|
-
export const taskLink = (config: ProcrastimateConfig, id: string) =>
|
|
92
|
-
`${appURL(config)}/inbox?task=${encodeURIComponent(id)}`;
|
|
93
|
-
|
|
94
|
-
async function send(
|
|
95
|
-
config: ProcrastimateConfig,
|
|
96
|
-
method: string,
|
|
97
|
-
path: string,
|
|
98
|
-
query: Record<string, string> = {},
|
|
99
|
-
body?: BodyInit,
|
|
100
|
-
type?: string,
|
|
101
|
-
) {
|
|
102
|
-
const search = new URLSearchParams(Object.entries(query).toSorted(([a], [b]) => a.localeCompare(b)));
|
|
103
|
-
const url = `${config.url.replace(/\/$/, '')}/${path}${search.size ? `?${search}` : ''}`;
|
|
104
|
-
let response: Response;
|
|
105
|
-
try {
|
|
106
|
-
response = await fetch(url, {
|
|
107
|
-
method,
|
|
108
|
-
body,
|
|
109
|
-
headers: {
|
|
110
|
-
...(config.token ? { Authorization: `Bearer ${config.token}` } : {}),
|
|
111
|
-
...(type ? { 'Content-Type': type } : {}),
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
} catch (error) {
|
|
115
|
-
throw Problem.request(method, url, undefined, undefined, error);
|
|
116
|
-
}
|
|
117
|
-
if (response.ok) return response;
|
|
118
|
-
const text = await response.text().catch(() => '');
|
|
119
|
-
if (response.status === 401) {
|
|
120
|
-
throw Problem.request(
|
|
121
|
-
method,
|
|
122
|
-
url,
|
|
123
|
-
response.status,
|
|
124
|
-
text,
|
|
125
|
-
new Error('ProcrastiMate rejected the token. Check the token Feedback was set up with.'),
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
let message: string | undefined;
|
|
129
|
-
try {
|
|
130
|
-
message = (JSON.parse(text) as { message?: string }).message;
|
|
131
|
-
} catch {
|
|
132
|
-
// A proxy's HTML error page, which the details show as it came.
|
|
133
|
-
}
|
|
134
|
-
throw Problem.request(
|
|
135
|
-
method,
|
|
136
|
-
url,
|
|
137
|
-
response.status,
|
|
138
|
-
text,
|
|
139
|
-
new Error(message ? `ProcrastiMate: ${message}` : `ProcrastiMate answered ${response.status}.`),
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
type Row = { id: string; name: string; order: number; createdAt: number };
|
|
144
|
-
const inOrder = <T extends Row>(rows: T[]) => rows.toSorted((a, b) => a.order - b.order || a.createdAt - b.createdAt);
|
|
145
|
-
|
|
146
|
-
/** The space's projects and their sections in the app's order. `/api/view` is the only route that lists them. */
|
|
147
|
-
export async function loadProjects(config: ProcrastimateConfig): Promise<Project[]> {
|
|
148
|
-
const view = (await (await send(config, 'GET', 'api/view')).json()) as {
|
|
149
|
-
projects: Row[];
|
|
150
|
-
sections: (Row & { projectId: string })[];
|
|
151
|
-
};
|
|
152
|
-
const projects = inOrder(view.projects).map(project => ({
|
|
153
|
-
id: project.id,
|
|
154
|
-
name: project.name,
|
|
155
|
-
sections: inOrder(view.sections.filter(section => section.projectId === project.id)).map(({ id, name }) => ({
|
|
156
|
-
id,
|
|
157
|
-
name,
|
|
158
|
-
})),
|
|
159
|
-
}));
|
|
160
|
-
store(projectsKey, projects);
|
|
161
|
-
return projects;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
type Encoded = { blob: Blob; width: number; height: number };
|
|
25
|
+
export type EncodedImage = { blob: Blob; width: number; height: number };
|
|
165
26
|
|
|
166
27
|
/**
|
|
167
28
|
* A JPEG of the image no wider than `width` and no larger than `maxBytes`. ProcrastiMate's own clients fit the long
|
|
168
29
|
* side. A full-length capture is many times taller than wide, so this fits the width, which keeps its text readable.
|
|
169
30
|
*/
|
|
170
|
-
async function jpeg(image: HTMLCanvasElement, width: number, maxBytes: number): Promise<
|
|
31
|
+
async function jpeg(image: HTMLCanvasElement, width: number, maxBytes: number): Promise<EncodedImage> {
|
|
171
32
|
for (let target = Math.min(width, image.width); target >= 64; target = Math.floor((target * 3) / 4)) {
|
|
172
33
|
const scaled = document.createElement('canvas');
|
|
173
34
|
scaled.width = target;
|
|
@@ -183,70 +44,9 @@ async function jpeg(image: HTMLCanvasElement, width: number, maxBytes: number):
|
|
|
183
44
|
throw new Error('The image could not be made small enough to upload.');
|
|
184
45
|
}
|
|
185
46
|
|
|
186
|
-
/**
|
|
187
|
-
|
|
188
|
-
* before the task that lists them, as ProcrastiMate's own clients do, so the task never names bytes the server
|
|
189
|
-
* cannot serve.
|
|
190
|
-
*/
|
|
191
|
-
export async function postTask(
|
|
192
|
-
config: ProcrastimateConfig,
|
|
193
|
-
title: string,
|
|
194
|
-
place: Place,
|
|
195
|
-
image: HTMLCanvasElement,
|
|
196
|
-
report: string,
|
|
197
|
-
step: (text: string) => void,
|
|
198
|
-
) {
|
|
199
|
-
const bytes = new TextEncoder().encode(report);
|
|
200
|
-
if (bytes.length > fileMaxBytes)
|
|
201
|
-
throw new Error('The JSON report is over the 16 MB ProcrastiMate takes in one upload.');
|
|
202
|
-
step('Encoding the image');
|
|
47
|
+
/** Encodes the full-size image and thumbnail JPEGs within ProcrastiMate's upload caps. */
|
|
48
|
+
export async function encodeTaskImages(image: HTMLCanvasElement): Promise<{ full: EncodedImage; thumb: EncodedImage }> {
|
|
203
49
|
const full = await jpeg(image, 2048, 1_500_000);
|
|
204
50
|
const thumb = await jpeg(image, 320, 200_000);
|
|
205
|
-
|
|
206
|
-
const reportId = crypto.randomUUID();
|
|
207
|
-
step('Uploading the image');
|
|
208
|
-
await send(config, 'PUT', 'api/images', { id: imageId, size: 'full' }, full.blob, 'image/jpeg');
|
|
209
|
-
step('Uploading the thumbnail');
|
|
210
|
-
await send(config, 'PUT', 'api/images', { id: imageId, size: 'thumb' }, thumb.blob, 'image/jpeg');
|
|
211
|
-
step('Uploading feedback.json');
|
|
212
|
-
await send(
|
|
213
|
-
config,
|
|
214
|
-
'PUT',
|
|
215
|
-
'api/images',
|
|
216
|
-
{ id: reportId, size: 'file', name: 'feedback.json', type: 'application/json' },
|
|
217
|
-
bytes,
|
|
218
|
-
'application/octet-stream',
|
|
219
|
-
);
|
|
220
|
-
step('Creating the task');
|
|
221
|
-
const created = await send(
|
|
222
|
-
config,
|
|
223
|
-
'POST',
|
|
224
|
-
'api/tasks',
|
|
225
|
-
{},
|
|
226
|
-
JSON.stringify({ title, projectId: place.projectId ?? null, sectionId: place.sectionId ?? null }),
|
|
227
|
-
'application/json',
|
|
228
|
-
);
|
|
229
|
-
const { id } = (await created.json()) as { id: string };
|
|
230
|
-
// The agent API cannot create a task with attachments, so a failed attach would leave a task without them, and a retry would add a second one.
|
|
231
|
-
step('Attaching the files');
|
|
232
|
-
try {
|
|
233
|
-
await send(
|
|
234
|
-
config,
|
|
235
|
-
'PATCH',
|
|
236
|
-
'api/tasks',
|
|
237
|
-
{ id },
|
|
238
|
-
JSON.stringify({
|
|
239
|
-
attachments: [
|
|
240
|
-
{ id: imageId, width: full.width, height: full.height },
|
|
241
|
-
{ id: reportId, name: 'feedback.json', type: 'application/json', bytes: bytes.length },
|
|
242
|
-
],
|
|
243
|
-
}),
|
|
244
|
-
'application/json',
|
|
245
|
-
);
|
|
246
|
-
} catch (error) {
|
|
247
|
-
await send(config, 'DELETE', 'api/tasks', { id }).catch(() => {});
|
|
248
|
-
throw error;
|
|
249
|
-
}
|
|
250
|
-
store(lastProjectKey, place.projectId);
|
|
251
|
-
return id;
|
|
51
|
+
return { full, thumb };
|
|
252
52
|
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import type { Annotations } from '../core/annotations.ts';
|
|
2
|
+
import { Problem, type Fact } from '../core/problem.ts';
|
|
3
|
+
import type { Screen } from './capture.ts';
|
|
4
|
+
import { annotatedImage } from './image.ts';
|
|
5
|
+
import { encodeTaskImages, fileMaxBytes, procrastimateAppURL, type ProcrastimateConfig } from './procrastimate.ts';
|
|
6
|
+
import { postId } from './webhook.ts';
|
|
7
|
+
|
|
8
|
+
export const FEEDBACK_READY = 'feedback:ready';
|
|
9
|
+
export const FEEDBACK_DRAFT = 'feedback:draft';
|
|
10
|
+
export const FEEDBACK_POSTED = 'feedback:posted';
|
|
11
|
+
export const FEEDBACK_CANCELLED = 'feedback:cancelled';
|
|
12
|
+
|
|
13
|
+
export type PopupReply =
|
|
14
|
+
| { kind: 'ready' }
|
|
15
|
+
| { kind: 'posted'; id: string; destination: string; url: string }
|
|
16
|
+
| { kind: 'cancelled' };
|
|
17
|
+
|
|
18
|
+
function isHttpURL(value: unknown): value is string {
|
|
19
|
+
if (typeof value !== 'string') return false;
|
|
20
|
+
try {
|
|
21
|
+
const parsed = new URL(value);
|
|
22
|
+
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Validates a message posted back from ProcrastiMate's `/feedback` popup. Every message must come from the configured
|
|
30
|
+
* ProcrastiMate origin, from the popup window we opened, and carry this handoff's nonce.
|
|
31
|
+
*/
|
|
32
|
+
export function parsePopupReply(
|
|
33
|
+
event: { origin: string; source: unknown; data: unknown },
|
|
34
|
+
expected: { origin: string; source: unknown; nonce: string },
|
|
35
|
+
): PopupReply | null {
|
|
36
|
+
if (event.origin !== expected.origin || event.source !== expected.source) return null;
|
|
37
|
+
const data = event.data;
|
|
38
|
+
if (!data || typeof data !== 'object') return null;
|
|
39
|
+
const record = data as Record<string, unknown>;
|
|
40
|
+
if (record.nonce !== expected.nonce) return null;
|
|
41
|
+
if (record.type === FEEDBACK_READY) return { kind: 'ready' };
|
|
42
|
+
if (record.type === FEEDBACK_CANCELLED) return { kind: 'cancelled' };
|
|
43
|
+
if (
|
|
44
|
+
record.type === FEEDBACK_POSTED &&
|
|
45
|
+
typeof record.id === 'string' &&
|
|
46
|
+
typeof record.destination === 'string' &&
|
|
47
|
+
isHttpURL(record.url)
|
|
48
|
+
) {
|
|
49
|
+
return { kind: 'posted', id: record.id, destination: record.destination, url: record.url };
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function popupFeatures() {
|
|
55
|
+
const width = 460;
|
|
56
|
+
const height = 680;
|
|
57
|
+
const left = Math.max(0, Math.round((window.screenX ?? 0) + (window.outerWidth - width) / 2));
|
|
58
|
+
const top = Math.max(0, Math.round((window.screenY ?? 0) + (window.outerHeight - height) / 2));
|
|
59
|
+
return `popup,width=${width},height=${height},left=${left},top=${top}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Opens ProcrastiMate's `/feedback` popup synchronously (so the browser's click gesture allows `window.open`) while
|
|
64
|
+
* encoding the screenshot in parallel, then hands over the draft over `postMessage` once the popup signals ready.
|
|
65
|
+
* No credential ever leaves ProcrastiMate's own origin.
|
|
66
|
+
*/
|
|
67
|
+
export function openProcrastimatePopup({
|
|
68
|
+
config,
|
|
69
|
+
title,
|
|
70
|
+
app,
|
|
71
|
+
screen,
|
|
72
|
+
annotations,
|
|
73
|
+
report,
|
|
74
|
+
onposted,
|
|
75
|
+
onerror,
|
|
76
|
+
onclose,
|
|
77
|
+
}: {
|
|
78
|
+
config: ProcrastimateConfig;
|
|
79
|
+
title: string;
|
|
80
|
+
app?: string;
|
|
81
|
+
screen: Screen;
|
|
82
|
+
annotations: Annotations;
|
|
83
|
+
report: string;
|
|
84
|
+
onposted: (result: { id: string; destination: string; url: string }) => void;
|
|
85
|
+
onerror: (problem: Problem) => void;
|
|
86
|
+
onclose?: () => void;
|
|
87
|
+
}): { cancel: () => void } {
|
|
88
|
+
const base = procrastimateAppURL(config);
|
|
89
|
+
const expectedOrigin = new URL(base, location.href).origin;
|
|
90
|
+
const nonce = postId();
|
|
91
|
+
const url = `${base}/feedback?nonce=${encodeURIComponent(nonce)}`;
|
|
92
|
+
const environment: Fact[] = screen.capture.environment.map(entry => ({ label: entry.name, value: entry.value }));
|
|
93
|
+
const reportBytes = new TextEncoder().encode(report).byteLength;
|
|
94
|
+
if (reportBytes > fileMaxBytes) {
|
|
95
|
+
onerror(
|
|
96
|
+
Problem.from(new Error('The JSON report is over the 16 MB ProcrastiMate takes in one upload.')).with(
|
|
97
|
+
[{ label: 'Report', value: `${reportBytes} bytes` }],
|
|
98
|
+
environment,
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
return { cancel() {} };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const popup = window.open(url, 'feedback-procrastimate', popupFeatures());
|
|
105
|
+
if (!popup) {
|
|
106
|
+
onerror(
|
|
107
|
+
new Problem('The browser blocked ProcrastiMate’s window. Allow pop-ups for this site and try again.', [
|
|
108
|
+
{ label: 'URL', value: url },
|
|
109
|
+
...environment,
|
|
110
|
+
]),
|
|
111
|
+
);
|
|
112
|
+
return { cancel() {} };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Render synchronously before any await; encode JPEGs while the popup loads.
|
|
116
|
+
let prepared: Promise<{ full: Blob; thumb: Blob; width: number; height: number }>;
|
|
117
|
+
try {
|
|
118
|
+
const canvas = annotatedImage(screen, annotations);
|
|
119
|
+
prepared = encodeTaskImages(canvas).then(({ full, thumb }) => ({
|
|
120
|
+
full: full.blob,
|
|
121
|
+
thumb: thumb.blob,
|
|
122
|
+
width: full.width,
|
|
123
|
+
height: full.height,
|
|
124
|
+
}));
|
|
125
|
+
} catch (error) {
|
|
126
|
+
prepared = Promise.reject(error);
|
|
127
|
+
}
|
|
128
|
+
// Prevent an unhandled rejection if the user closes the popup before encoding settles.
|
|
129
|
+
prepared.catch(() => {});
|
|
130
|
+
|
|
131
|
+
let done = false;
|
|
132
|
+
const cleanup = () => {
|
|
133
|
+
if (done) return;
|
|
134
|
+
done = true;
|
|
135
|
+
window.removeEventListener('message', onMessage);
|
|
136
|
+
clearInterval(closedCheck);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const onMessage = (event: MessageEvent) => {
|
|
140
|
+
const reply = parsePopupReply(event, { origin: expectedOrigin, source: popup, nonce });
|
|
141
|
+
if (!reply) return;
|
|
142
|
+
if (reply.kind === 'ready') {
|
|
143
|
+
void prepared
|
|
144
|
+
.then(image => {
|
|
145
|
+
if (done || popup.closed) return;
|
|
146
|
+
popup.postMessage(
|
|
147
|
+
{
|
|
148
|
+
type: FEEDBACK_DRAFT,
|
|
149
|
+
nonce,
|
|
150
|
+
title,
|
|
151
|
+
app,
|
|
152
|
+
project: config.project,
|
|
153
|
+
projects: config.projects,
|
|
154
|
+
space: config.space,
|
|
155
|
+
image,
|
|
156
|
+
report,
|
|
157
|
+
},
|
|
158
|
+
expectedOrigin,
|
|
159
|
+
);
|
|
160
|
+
})
|
|
161
|
+
.catch((error: unknown) => {
|
|
162
|
+
cleanup();
|
|
163
|
+
try {
|
|
164
|
+
popup.close();
|
|
165
|
+
} catch {
|
|
166
|
+
// Ignore if already closed.
|
|
167
|
+
}
|
|
168
|
+
onerror(
|
|
169
|
+
Problem.from(error).with(
|
|
170
|
+
[
|
|
171
|
+
{ label: 'Step', value: 'Encoding the image' },
|
|
172
|
+
{ label: 'URL', value: url },
|
|
173
|
+
],
|
|
174
|
+
environment,
|
|
175
|
+
),
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
cleanup();
|
|
181
|
+
if (reply.kind === 'posted') onposted(reply);
|
|
182
|
+
else onclose?.();
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
window.addEventListener('message', onMessage);
|
|
186
|
+
const closedCheck = setInterval(() => {
|
|
187
|
+
if (popup.closed) {
|
|
188
|
+
cleanup();
|
|
189
|
+
onclose?.();
|
|
190
|
+
}
|
|
191
|
+
}, 400);
|
|
192
|
+
|
|
193
|
+
return { cancel: cleanup };
|
|
194
|
+
}
|
package/src/editor/Editor.svelte
CHANGED
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
Problem,
|
|
10
10
|
Session,
|
|
11
11
|
tag,
|
|
12
|
+
taskTitle,
|
|
12
13
|
title,
|
|
13
14
|
type Frame,
|
|
14
15
|
type SavedWebhook,
|
|
15
16
|
} from '../core/index.ts';
|
|
16
17
|
import type { Screen } from '../dom/capture.ts';
|
|
18
|
+
import { openProcrastimatePopup } from '../dom/procrastimatePopup.ts';
|
|
17
19
|
import {
|
|
18
20
|
buildExport,
|
|
19
21
|
canShare,
|
|
@@ -29,7 +31,6 @@
|
|
|
29
31
|
import { destinations, pinnedDestinations, type Destination } from './destinations.ts';
|
|
30
32
|
import type { Engine } from './engine.ts';
|
|
31
33
|
import ProblemCard from './ProblemCard.svelte';
|
|
32
|
-
import ProcrastimateSheet from './ProcrastimateSheet.svelte';
|
|
33
34
|
import WebhookDialog from './WebhookDialog.svelte';
|
|
34
35
|
import WebhookSheet from './WebhookSheet.svelte';
|
|
35
36
|
|
|
@@ -97,8 +98,10 @@
|
|
|
97
98
|
/** The list of items starts open beside the screenshot, and closed under it on a phone, where it would cover the picture. */
|
|
98
99
|
let showsItems = $state(matchMedia('(min-width: 900px)').matches);
|
|
99
100
|
let showsShare = $state(false);
|
|
100
|
-
let
|
|
101
|
+
let popup = { cancel() {} };
|
|
101
102
|
let posting = $state<Delivery>();
|
|
103
|
+
|
|
104
|
+
$effect(() => () => popup.cancel());
|
|
102
105
|
let saved = $state(loadSavedWebhooks());
|
|
103
106
|
/** The dialog for a saved webhook, `webhook` unset for a new one. */
|
|
104
107
|
let editing = $state<{ webhook?: SavedWebhook }>();
|
|
@@ -263,8 +266,10 @@
|
|
|
263
266
|
// The error is drawn over the other sheets, so it is the one to leave first.
|
|
264
267
|
if (failure) {
|
|
265
268
|
failure = undefined;
|
|
266
|
-
} else if (
|
|
267
|
-
|
|
269
|
+
} else if (confirmsClose) {
|
|
270
|
+
confirmsClose = false;
|
|
271
|
+
} else if (showsInfo || showsShare || posting || pending || editing) {
|
|
272
|
+
showsInfo = showsShare = false;
|
|
268
273
|
posting = pending = editing = undefined;
|
|
269
274
|
} else if (session.isTyping || session.unshared.length) {
|
|
270
275
|
confirmsClose = true;
|
|
@@ -275,8 +280,32 @@
|
|
|
275
280
|
|
|
276
281
|
function send(destination: Destination) {
|
|
277
282
|
showsShare = false;
|
|
278
|
-
if (destination.kind === 'procrastimate')
|
|
279
|
-
|
|
283
|
+
if (destination.kind === 'procrastimate') {
|
|
284
|
+
const config = engine.options.procrastimate;
|
|
285
|
+
if (!config) return;
|
|
286
|
+
popup.cancel();
|
|
287
|
+
const items = [...session.items];
|
|
288
|
+
const names = taskTitle(session.capture, items);
|
|
289
|
+
popup = openProcrastimatePopup({
|
|
290
|
+
config,
|
|
291
|
+
title: names.placeholder,
|
|
292
|
+
app: engine.options.app ?? (document.title.trim() || undefined),
|
|
293
|
+
screen,
|
|
294
|
+
annotations: session.annotations,
|
|
295
|
+
report: session.compactJSON,
|
|
296
|
+
onposted: ({ destination: where, url }) => {
|
|
297
|
+
act(() => {
|
|
298
|
+
session.markShared(items);
|
|
299
|
+
session.toast = { text: `Added to ${where} in ProcrastiMate`, link: url };
|
|
300
|
+
});
|
|
301
|
+
},
|
|
302
|
+
onerror: problem => {
|
|
303
|
+
act(() => {
|
|
304
|
+
session.toast = { text: 'Couldn’t open ProcrastiMate', problem };
|
|
305
|
+
});
|
|
306
|
+
},
|
|
307
|
+
});
|
|
308
|
+
} else if (destination.kind === 'saved') edit(destination.saved);
|
|
280
309
|
else posting = destination.delivery;
|
|
281
310
|
}
|
|
282
311
|
|
|
@@ -345,7 +374,10 @@
|
|
|
345
374
|
class="editor"
|
|
346
375
|
aria-label="Feedback"
|
|
347
376
|
onpointerdown={event => {
|
|
348
|
-
|
|
377
|
+
const target = event.target as Element;
|
|
378
|
+
if (showsInfo && !target.closest('.title')) showsInfo = false;
|
|
379
|
+
// The close button leaves the menu itself, as Escape does, instead of the editor behind it.
|
|
380
|
+
if (showsShare && !target.closest('.menu, .close')) showsShare = false;
|
|
349
381
|
}}
|
|
350
382
|
oncancel={event => {
|
|
351
383
|
event.preventDefault();
|
|
@@ -353,7 +385,7 @@
|
|
|
353
385
|
}}
|
|
354
386
|
>
|
|
355
387
|
<header>
|
|
356
|
-
<button type="button" class="nav" aria-label="Close" onclick={requestClose}>
|
|
388
|
+
<button type="button" class="nav close" aria-label="Close" title="Close" onclick={requestClose}>
|
|
357
389
|
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6 6l12 12M18 6L6 18" /></svg>
|
|
358
390
|
</button>
|
|
359
391
|
<h1>Feedback</h1>
|
|
@@ -632,22 +664,6 @@
|
|
|
632
664
|
</div>
|
|
633
665
|
{/if}
|
|
634
666
|
|
|
635
|
-
{#if showsProcrastimate && engine.options.procrastimate}
|
|
636
|
-
<ProcrastimateSheet
|
|
637
|
-
{screen}
|
|
638
|
-
{session}
|
|
639
|
-
config={engine.options.procrastimate}
|
|
640
|
-
onclose={() => (showsProcrastimate = false)}
|
|
641
|
-
onposted={(items, toast) => {
|
|
642
|
-
showsProcrastimate = false;
|
|
643
|
-
act(() => {
|
|
644
|
-
session.markShared(items);
|
|
645
|
-
session.toast = toast;
|
|
646
|
-
});
|
|
647
|
-
}}
|
|
648
|
-
/>
|
|
649
|
-
{/if}
|
|
650
|
-
|
|
651
667
|
{#if editing}
|
|
652
668
|
{@const webhook = editing.webhook}
|
|
653
669
|
<WebhookDialog
|
|
@@ -696,7 +712,11 @@
|
|
|
696
712
|
{/if}
|
|
697
713
|
|
|
698
714
|
{#if confirmsClose}
|
|
699
|
-
<div
|
|
715
|
+
<div
|
|
716
|
+
class="scrim"
|
|
717
|
+
role="presentation"
|
|
718
|
+
onclick={event => event.target === event.currentTarget && (confirmsClose = false)}
|
|
719
|
+
>
|
|
700
720
|
<div class="sheet actions" role="alertdialog" tabindex="-1" aria-label="Discard feedback?">
|
|
701
721
|
<h2>Discard feedback?</h2>
|
|
702
722
|
<p>
|
package/src/editor/engine.ts
CHANGED
|
@@ -26,8 +26,8 @@ export type FeedbackOptions = {
|
|
|
26
26
|
*/
|
|
27
27
|
threeFingerHold?: boolean;
|
|
28
28
|
/**
|
|
29
|
-
* Adds ProcrastiMate to the Share menu.
|
|
30
|
-
*
|
|
29
|
+
* Adds ProcrastiMate to the Share menu. Pass `{ projects: ['Website'] }` to post through a popup on the
|
|
30
|
+
* ProcrastiMate web app (`/feedback`) using whoever is signed in there, with no token and no proxy.
|
|
31
31
|
*/
|
|
32
32
|
procrastimate?: ProcrastimateConfig;
|
|
33
33
|
/**
|
package/src/editor/setup.ts
CHANGED
|
@@ -55,19 +55,9 @@ export function setupFeedbackEngine(options: FeedbackOptions = {}): FeedbackEngi
|
|
|
55
55
|
}
|
|
56
56
|
current?.destroy();
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const proxy = (import.meta as Meta).env && (import.meta as Meta).env!.FEEDBACK_PROCRASTIMATE;
|
|
62
|
-
const project = (import.meta as Meta).env && (import.meta as Meta).env!.FEEDBACK_PROCRASTIMATE_PROJECT;
|
|
63
|
-
const projects = (import.meta as Meta).env && (import.meta as Meta).env!.FEEDBACK_PROCRASTIMATE_PROJECTS;
|
|
64
|
-
const listed = projects ? (JSON.parse(projects) as string[]) : [];
|
|
65
|
-
const engine = new Engine({
|
|
66
|
-
...options,
|
|
67
|
-
procrastimate:
|
|
68
|
-
options.procrastimate ??
|
|
69
|
-
(proxy ? { url: proxy, project: project || undefined, projects: listed.length ? listed : undefined } : undefined),
|
|
70
|
-
});
|
|
58
|
+
if (options.procrastimate && !options.procrastimate.projects?.length)
|
|
59
|
+
throw new Error('setupFeedbackEngine({ procrastimate }) needs the projects Feedback may post to, by name or id.');
|
|
60
|
+
const engine = new Engine(options);
|
|
71
61
|
const host = document.createElement('div');
|
|
72
62
|
host.setAttribute(hostAttribute, '');
|
|
73
63
|
// The shadow root keeps the app's styles out of the editor and the editor's styles out of the app.
|
package/src/react/public.d.ts
CHANGED
|
@@ -24,23 +24,24 @@ export type FeedbackOptions = {
|
|
|
24
24
|
*/
|
|
25
25
|
threeFingerHold?: boolean;
|
|
26
26
|
/**
|
|
27
|
-
* Adds ProcrastiMate to the Share menu.
|
|
28
|
-
*
|
|
27
|
+
* Adds ProcrastiMate to the Share menu. Pass `{ projects: ['Website'] }` to post through a popup on the
|
|
28
|
+
* ProcrastiMate web app (`/feedback`) using whoever is signed in there, with no token and no proxy.
|
|
29
29
|
*/
|
|
30
30
|
procrastimate?: {
|
|
31
|
-
/**
|
|
32
|
-
url: string;
|
|
33
|
-
/** Sent as a bearer token. Leave it out behind the proxy, which adds its own. */
|
|
34
|
-
token?: string;
|
|
35
|
-
/** The ProcrastiMate web app, for the link to a posted task. */
|
|
31
|
+
/** The ProcrastiMate web app whose `/feedback` popup opens. Defaults to `https://procrastimate.stf.lol`. */
|
|
36
32
|
app?: string;
|
|
37
33
|
/** The project the picker opens on, by name (ignoring case) or id, in place of the last one posted to. */
|
|
38
34
|
project?: string;
|
|
39
35
|
/**
|
|
40
|
-
* The only projects the
|
|
41
|
-
*
|
|
36
|
+
* The only projects the `/feedback` popup offers, by name (ignoring case) or id, in this order. The Inbox is not
|
|
37
|
+
* offered. Required, and not empty.
|
|
42
38
|
*/
|
|
43
|
-
projects
|
|
39
|
+
projects: string[];
|
|
40
|
+
/**
|
|
41
|
+
* Another user's space (their ProcrastiMate user id) whose shared projects the popup lists. Leave it out to match
|
|
42
|
+
* across the signed-in user's own and edit-shared projects.
|
|
43
|
+
*/
|
|
44
|
+
space?: string;
|
|
44
45
|
};
|
|
45
46
|
/**
|
|
46
47
|
* Adds one entry to the Share menu per webhook. Choosing it posts the image export and the JSON export to the
|
package/src/vite/index.d.ts
CHANGED
|
@@ -12,26 +12,6 @@ export function feedbackEngine(options?: {
|
|
|
12
12
|
root?: string;
|
|
13
13
|
}): { name: string; markup(input: { content: string; filename?: string }): { code: string } | undefined };
|
|
14
14
|
|
|
15
|
-
/**
|
|
16
|
-
* A Vite plugin that forwards `path` on the dev and preview servers to ProcrastiMate and adds the token there. With a
|
|
17
|
-
* token, the Share menu lists ProcrastiMate. Without one it does not.
|
|
18
|
-
*/
|
|
19
|
-
export function feedbackProcrastimate(options?: {
|
|
20
|
-
/** ProcrastiMate's origin. Defaults to `https://procrastimate.stf.lol`. */
|
|
21
|
-
url?: string;
|
|
22
|
-
/** A ProcrastiMate API token. Pass `process.env.PROCRASTIMATE_TOKEN`, so the entry shows only where it is set. */
|
|
23
|
-
token?: string;
|
|
24
|
-
/** Defaults to `/__feedback/procrastimate`. */
|
|
25
|
-
path?: string;
|
|
26
|
-
/** The project the picker opens on, by name (ignoring case) or id, in place of the last one posted to. */
|
|
27
|
-
project?: string;
|
|
28
|
-
/**
|
|
29
|
-
* The only projects the picker offers, by name (ignoring case) or id, in this order, each with a link to its page
|
|
30
|
-
* in ProcrastiMate. The Inbox is not offered with it. Without it, the picker lists the Inbox and every project.
|
|
31
|
-
*/
|
|
32
|
-
projects?: string[];
|
|
33
|
-
}): Plugin;
|
|
34
|
-
|
|
35
15
|
/**
|
|
36
16
|
* Registers a React module's components, rewrites its `feedback(` calls to carry their `file:line`, and, with
|
|
37
17
|
* `elementSources`, writes each HTML element's `file:line` into an attribute. `undefined` when nothing changed.
|
package/src/vite/index.js
CHANGED
|
@@ -5,11 +5,6 @@ export { addReact, feedbackEngineReact, sourceAttribute } from './react.js';
|
|
|
5
5
|
|
|
6
6
|
const entry = '@stefan.inc/feedback-engine/svelte';
|
|
7
7
|
|
|
8
|
-
/** What `setupFeedbackEngine()` reads for the proxy's path. `editor/setup.ts` spells the same name. */
|
|
9
|
-
const procrastimateDefine = 'import.meta.env.FEEDBACK_PROCRASTIMATE';
|
|
10
|
-
const procrastimateProjectDefine = 'import.meta.env.FEEDBACK_PROCRASTIMATE_PROJECT';
|
|
11
|
-
const procrastimateProjectsDefine = 'import.meta.env.FEEDBACK_PROCRASTIMATE_PROJECTS';
|
|
12
|
-
|
|
13
8
|
/**
|
|
14
9
|
* The name a tag gets without one: the file's, as `.feedback(self)` uses the type's on iOS. A SvelteKit route file is
|
|
15
10
|
* named after its folder, so `routes/lists/[id]/+page.svelte` is `ListsIdPage`.
|
|
@@ -147,71 +142,3 @@ export function feedbackEngine(options = {}) {
|
|
|
147
142
|
},
|
|
148
143
|
};
|
|
149
144
|
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* A Vite plugin that lets the page post to ProcrastiMate. A browser on another origin cannot call ProcrastiMate, and a
|
|
153
|
-
* token in the page would be public, so the dev and preview servers forward `path` to ProcrastiMate and add the
|
|
154
|
-
* token there.
|
|
155
|
-
*
|
|
156
|
-
* The token is the switch. With one, the plugin tells the page where the proxy is, and the Share menu lists
|
|
157
|
-
* ProcrastiMate. Without one the page is told nothing, so the entry is not there. A page that names the path itself
|
|
158
|
-
* then gets a 501 that says why.
|
|
159
|
-
*
|
|
160
|
-
* @param {{ url?: string, token?: string, path?: string, project?: string, projects?: string[] }} [options]
|
|
161
|
-
*/
|
|
162
|
-
export function feedbackProcrastimate(options = {}) {
|
|
163
|
-
if (options.projects && !options.projects.length)
|
|
164
|
-
throw new Error('feedbackProcrastimate({ projects }) lists no projects. Leave it out to offer every project.');
|
|
165
|
-
const path = options.path ?? '/__feedback/procrastimate';
|
|
166
|
-
const target = (options.url ?? 'https://procrastimate.stf.lol').replace(/\/$/, '');
|
|
167
|
-
/** @type {import('vite').Connect.NextHandleFunction} */
|
|
168
|
-
const handler = async (request, response, next) => {
|
|
169
|
-
if (!request.url?.startsWith(`${path}/`)) return next();
|
|
170
|
-
const reply = (/** @type {number} */ status, /** @type {string} */ message) => {
|
|
171
|
-
response.statusCode = status;
|
|
172
|
-
response.setHeader('Content-Type', 'application/json');
|
|
173
|
-
response.end(JSON.stringify({ message }));
|
|
174
|
-
};
|
|
175
|
-
if (!options.token)
|
|
176
|
-
return reply(501, 'the dev server has no ProcrastiMate token. Pass one to feedbackProcrastimate().');
|
|
177
|
-
try {
|
|
178
|
-
const chunks = [];
|
|
179
|
-
for await (const chunk of request) chunks.push(chunk);
|
|
180
|
-
const body = Buffer.concat(chunks);
|
|
181
|
-
const upstream = await fetch(target + request.url.slice(path.length), {
|
|
182
|
-
method: request.method,
|
|
183
|
-
headers: {
|
|
184
|
-
Authorization: `Bearer ${options.token}`,
|
|
185
|
-
'User-Agent': 'Feedback',
|
|
186
|
-
...(request.headers['content-type'] ? { 'Content-Type': request.headers['content-type'] } : {}),
|
|
187
|
-
},
|
|
188
|
-
body: body.length ? body : undefined,
|
|
189
|
-
});
|
|
190
|
-
response.statusCode = upstream.status;
|
|
191
|
-
response.setHeader('Content-Type', upstream.headers.get('content-type') ?? 'application/json');
|
|
192
|
-
response.end(Buffer.from(await upstream.arrayBuffer()));
|
|
193
|
-
} catch (error) {
|
|
194
|
-
reply(502, `the dev server could not reach ${target}: ${/** @type {Error} */ (error).message}`);
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
return {
|
|
198
|
-
name: 'feedback-engine-procrastimate',
|
|
199
|
-
config() {
|
|
200
|
-
return {
|
|
201
|
-
define: {
|
|
202
|
-
[procrastimateDefine]: JSON.stringify(options.token ? path : ''),
|
|
203
|
-
[procrastimateProjectDefine]: JSON.stringify(options.project ?? ''),
|
|
204
|
-
[procrastimateProjectsDefine]: JSON.stringify(JSON.stringify(options.projects ?? [])),
|
|
205
|
-
},
|
|
206
|
-
};
|
|
207
|
-
},
|
|
208
|
-
/** @param {import('vite').ViteDevServer} server */
|
|
209
|
-
configureServer(server) {
|
|
210
|
-
server.middlewares.use(handler);
|
|
211
|
-
},
|
|
212
|
-
/** @param {import('vite').PreviewServer} server */
|
|
213
|
-
configurePreviewServer(server) {
|
|
214
|
-
server.middlewares.use(handler);
|
|
215
|
-
},
|
|
216
|
-
};
|
|
217
|
-
}
|
|
@@ -1,257 +0,0 @@
|
|
|
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
|
-
listedProjects,
|
|
11
|
-
loadProjects,
|
|
12
|
-
postTask,
|
|
13
|
-
projectLink,
|
|
14
|
-
startingProject,
|
|
15
|
-
taskLink,
|
|
16
|
-
type Place,
|
|
17
|
-
type ProcrastimateConfig,
|
|
18
|
-
} from '../dom/procrastimate.ts';
|
|
19
|
-
import ProblemCard from './ProblemCard.svelte';
|
|
20
|
-
import Sheet from './Sheet.svelte';
|
|
21
|
-
|
|
22
|
-
let {
|
|
23
|
-
screen,
|
|
24
|
-
session,
|
|
25
|
-
config,
|
|
26
|
-
onclose,
|
|
27
|
-
onposted,
|
|
28
|
-
}: {
|
|
29
|
-
screen: Screen;
|
|
30
|
-
session: Session;
|
|
31
|
-
config: ProcrastimateConfig;
|
|
32
|
-
onclose: () => void;
|
|
33
|
-
onposted: (items: FeedbackItem[], toast: Toast) => void;
|
|
34
|
-
} = $props();
|
|
35
|
-
|
|
36
|
-
// svelte-ignore state_referenced_locally
|
|
37
|
-
const names = taskTitle(session.capture, session.items);
|
|
38
|
-
let name = $state(names.placeholder);
|
|
39
|
-
let all = $state(cachedProjects());
|
|
40
|
-
const projects = $derived(listedProjects(all, config.projects).projects);
|
|
41
|
-
// svelte-ignore state_referenced_locally
|
|
42
|
-
const inbox = !config.projects;
|
|
43
|
-
// svelte-ignore state_referenced_locally
|
|
44
|
-
let place = $state<Place>({ projectId: startingProject(projects, config, lastProject()) });
|
|
45
|
-
let loading = $state(true);
|
|
46
|
-
/** Whether the user chose a place, which a project list that finishes loading must not undo. */
|
|
47
|
-
let chosen = false;
|
|
48
|
-
let loadProblem = $state<Problem>();
|
|
49
|
-
let postProblem = $state<Problem>();
|
|
50
|
-
/** What the post is doing, while it runs. */
|
|
51
|
-
let step = $state<string>();
|
|
52
|
-
const posting = $derived(step !== undefined);
|
|
53
|
-
/** Without a project, a post would land in the Inbox, which a project the app named or listed rules out. */
|
|
54
|
-
const unresolved = $derived(!place.projectId && (!inbox || (loading && !!config.project)));
|
|
55
|
-
let status = $state<HTMLElement>();
|
|
56
|
-
// svelte-ignore state_referenced_locally
|
|
57
|
-
const environment: Fact[] = session.capture.environment.map(entry => ({ label: entry.name, value: entry.value }));
|
|
58
|
-
|
|
59
|
-
$effect(() => {
|
|
60
|
-
loadProjects(config)
|
|
61
|
-
.then(loaded => {
|
|
62
|
-
all = loaded;
|
|
63
|
-
const listed = listedProjects(loaded, config.projects);
|
|
64
|
-
if (!chosen) place = { projectId: startingProject(listed.projects, config, lastProject()) };
|
|
65
|
-
const project = listed.projects.find(candidate => candidate.id === place.projectId);
|
|
66
|
-
if (!project)
|
|
67
|
-
place = { projectId: startingProject(listed.projects, { ...config, project: undefined }, undefined) };
|
|
68
|
-
else if (!project.sections.some(section => section.id === place.sectionId)) place = { projectId: project.id };
|
|
69
|
-
const missing = [...listed.missing];
|
|
70
|
-
if (config.project && !startingProject(listed.projects, config, undefined)) missing.push(config.project);
|
|
71
|
-
if (missing.length) {
|
|
72
|
-
const names = [...new Set(missing)].map(name => `"${name}"`).join(', ');
|
|
73
|
-
throw new Error(
|
|
74
|
-
config.projects
|
|
75
|
-
? `ProcrastiMate has no project named ${names} among the projects Feedback was set up with`
|
|
76
|
-
: `ProcrastiMate has no project named ${names}`,
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
.catch(
|
|
81
|
-
(error: unknown) =>
|
|
82
|
-
(loadProblem = Problem.from(error).with([{ label: 'Step', value: 'Loading the projects' }], environment)),
|
|
83
|
-
)
|
|
84
|
-
.finally(() => (loading = false));
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
async function post() {
|
|
88
|
-
step = 'Drawing the image';
|
|
89
|
-
postProblem = undefined;
|
|
90
|
-
const started = performance.now();
|
|
91
|
-
const items = [...session.items];
|
|
92
|
-
const project = projects.find(candidate => candidate.id === place.projectId);
|
|
93
|
-
const section = project?.sections.find(candidate => candidate.id === place.sectionId);
|
|
94
|
-
const report = session.compactJSON;
|
|
95
|
-
const facts: Fact[] = [
|
|
96
|
-
{
|
|
97
|
-
label: 'Place',
|
|
98
|
-
value: [project ? `${project.name} (${project.id})` : 'Inbox', section && `${section.name} (${section.id})`]
|
|
99
|
-
.filter(Boolean)
|
|
100
|
-
.join(' / '),
|
|
101
|
-
},
|
|
102
|
-
{ label: 'Report', value: `${new TextEncoder().encode(report).length} bytes` },
|
|
103
|
-
{ label: 'Server', value: config.url },
|
|
104
|
-
];
|
|
105
|
-
try {
|
|
106
|
-
const image = annotatedImage(screen, session.annotations);
|
|
107
|
-
facts.push({ label: 'Image', value: `${image.width} × ${image.height} px` });
|
|
108
|
-
const id = await postTask(config, name.trim(), place, image, report, text => (step = text));
|
|
109
|
-
const destination = [project?.name ?? 'Inbox', section?.name].filter(Boolean).join(' / ');
|
|
110
|
-
onposted(items, { text: `Added to ${destination} in ProcrastiMate`, link: taskLink(config, id) });
|
|
111
|
-
} catch (error) {
|
|
112
|
-
const elapsed = { label: 'Elapsed', value: `${((performance.now() - started) / 1000).toFixed(1)} s` };
|
|
113
|
-
postProblem = Problem.from(error).with(
|
|
114
|
-
[{ label: 'Step', value: step ?? 'Posting' }, elapsed],
|
|
115
|
-
[...facts, ...environment],
|
|
116
|
-
);
|
|
117
|
-
step = undefined;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// Above the project list, which can be longer than the sheet: an error under it goes unseen.
|
|
122
|
-
$effect(() => {
|
|
123
|
-
if (postProblem || step) status?.scrollIntoView({ block: 'nearest' });
|
|
124
|
-
});
|
|
125
|
-
</script>
|
|
126
|
-
|
|
127
|
-
{#snippet row(label: string, picked: boolean, indented: boolean, pick: () => void, link?: string)}
|
|
128
|
-
<div class="row" class:indented>
|
|
129
|
-
<button
|
|
130
|
-
type="button"
|
|
131
|
-
role="option"
|
|
132
|
-
aria-selected={picked}
|
|
133
|
-
disabled={posting}
|
|
134
|
-
onclick={() => {
|
|
135
|
-
chosen = true;
|
|
136
|
-
pick();
|
|
137
|
-
}}
|
|
138
|
-
>
|
|
139
|
-
<span>{label}</span>
|
|
140
|
-
{#if picked}<span class="check" aria-hidden="true">✓</span>{/if}
|
|
141
|
-
</button>
|
|
142
|
-
{#if link}
|
|
143
|
-
<a
|
|
144
|
-
class="open"
|
|
145
|
-
href={link}
|
|
146
|
-
target="_blank"
|
|
147
|
-
rel="noopener"
|
|
148
|
-
aria-label="Open {label} in ProcrastiMate"
|
|
149
|
-
title="Open in ProcrastiMate">↗</a
|
|
150
|
-
>
|
|
151
|
-
{/if}
|
|
152
|
-
</div>
|
|
153
|
-
{/snippet}
|
|
154
|
-
|
|
155
|
-
<Sheet label="Add to ProcrastiMate" onsubmit={() => void post()}>
|
|
156
|
-
{#snippet header()}
|
|
157
|
-
<button type="button" class="link" disabled={posting} onclick={onclose}>Cancel</button>
|
|
158
|
-
<h2>Add to ProcrastiMate</h2>
|
|
159
|
-
<button type="submit" class="link strong" disabled={posting || unresolved || !name.trim()}
|
|
160
|
-
>{posting ? 'Adding…' : 'Add'}</button
|
|
161
|
-
>
|
|
162
|
-
{/snippet}
|
|
163
|
-
|
|
164
|
-
<div class="status" bind:this={status}>
|
|
165
|
-
{#if step}<p class="step" role="status">{step}…</p>{/if}
|
|
166
|
-
{#if postProblem}<ProblemCard problem={postProblem} />{/if}
|
|
167
|
-
{#if loadProblem}<ProblemCard problem={loadProblem} />{/if}
|
|
168
|
-
</div>
|
|
169
|
-
<label>
|
|
170
|
-
<span class="caption">Name</span>
|
|
171
|
-
<input bind:value={name} aria-label="Task name" disabled={posting} />
|
|
172
|
-
</label>
|
|
173
|
-
<span class="caption">Project{loading ? ' · loading…' : ''}</span>
|
|
174
|
-
<div class="list" role="listbox" aria-label="Project">
|
|
175
|
-
{#if inbox}{@render row('Inbox', !place.projectId, false, () => (place = {}))}{/if}
|
|
176
|
-
{#each projects as project (project.id)}
|
|
177
|
-
{@const open = project.id === place.projectId && project.sections.length > 0}
|
|
178
|
-
{@render row(
|
|
179
|
-
project.name,
|
|
180
|
-
project.id === place.projectId && !open,
|
|
181
|
-
false,
|
|
182
|
-
() => (place = { projectId: project.id }),
|
|
183
|
-
projectLink(config, project.id),
|
|
184
|
-
)}
|
|
185
|
-
{#if open}
|
|
186
|
-
{@render row('No section', !place.sectionId, true, () => (place = { projectId: project.id }))}
|
|
187
|
-
{#each project.sections as section (section.id)}
|
|
188
|
-
{@render row(
|
|
189
|
-
section.name,
|
|
190
|
-
place.sectionId === section.id,
|
|
191
|
-
true,
|
|
192
|
-
() => (place = { projectId: project.id, sectionId: section.id }),
|
|
193
|
-
)}
|
|
194
|
-
{/each}
|
|
195
|
-
{/if}
|
|
196
|
-
{/each}
|
|
197
|
-
</div>
|
|
198
|
-
</Sheet>
|
|
199
|
-
|
|
200
|
-
<style>
|
|
201
|
-
.strong {
|
|
202
|
-
font-weight: 600;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
.list {
|
|
206
|
-
display: grid;
|
|
207
|
-
border-radius: 10px;
|
|
208
|
-
background: #2c2c2e;
|
|
209
|
-
overflow: hidden;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
.row {
|
|
213
|
-
display: flex;
|
|
214
|
-
border-bottom: 1px solid rgb(255 255 255 / 0.08);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
.row > button {
|
|
218
|
-
display: flex;
|
|
219
|
-
flex: 1;
|
|
220
|
-
justify-content: space-between;
|
|
221
|
-
gap: 8px;
|
|
222
|
-
padding: 11px 12px;
|
|
223
|
-
text-align: left;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
.row.indented > button {
|
|
227
|
-
padding-left: 40px;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
.open {
|
|
231
|
-
display: grid;
|
|
232
|
-
place-items: center;
|
|
233
|
-
width: 44px;
|
|
234
|
-
border-left: 1px solid rgb(255 255 255 / 0.08);
|
|
235
|
-
color: #0a84ff;
|
|
236
|
-
text-decoration: none;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
.check {
|
|
240
|
-
color: #0a84ff;
|
|
241
|
-
font-weight: 700;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
.status {
|
|
245
|
-
display: grid;
|
|
246
|
-
gap: 8px;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
.status:empty {
|
|
250
|
-
display: none;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
.step {
|
|
254
|
-
margin: 0;
|
|
255
|
-
color: #aeaeb2;
|
|
256
|
-
}
|
|
257
|
-
</style>
|