rvlib-shipkit 2.20.0 → 2.22.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/CHANGELOG.md +35 -0
- package/package.json +1 -1
- package/src/asks/asks-queue.ts +8 -7
- package/src/assets/kits/identity-kit.ts +158 -2
- package/src/checks/SK004-gitignore-policy.check.ts +6 -3
- package/src/cli/cmd_Bump.ts +3 -3
- package/src/cli/cmd_GlobalProcesses.ts +2 -107
- package/src/cli/cmd_Home.ts +1 -1
- package/src/cli/cmd_Overview.ts +2 -1
- package/src/cli/cmd_Rec.ts +349 -0
- package/src/cli/cmd_Releases.ts +42 -3
- package/src/cli/cmd_Review.ts +22 -13
- package/src/cli/cmd_Secret.ts +10 -3
- package/src/cli/cmd_Sign.ts +24 -0
- package/src/cli/cmd_Social.ts +57 -8
- package/src/cli/cmd_Sound.ts +17 -4
- package/src/cli/cmd_Trailer.ts +67 -1
- package/src/cli/family-pages.ts +1 -0
- package/src/cli/registry.ts +6 -1
- package/src/collections/collection-types.ts +1 -1
- package/src/collections/review.ts +23 -4
- package/src/core/agent-spawn.ts +4 -17
- package/src/core/code-lines.ts +49 -0
- package/src/core/discover-checks.ts +27 -3
- package/src/core/hard-reset.ts +34 -0
- package/src/core/layout.ts +6 -0
- package/src/core/raw-access.ts +3 -13
- package/src/core/throwaway-worktree.ts +72 -0
- package/src/home/app-sign.ts +98 -0
- package/src/home/gui-app/ShipkitGui.swift +35 -0
- package/src/home/gui-app.ts +31 -11
- package/src/home/home-routes.ts +6 -0
- package/src/home/pm-switch.ts +112 -0
- package/src/home/serve-web.ts +1 -1
- package/src/identities/identities.ts +17 -6
- package/src/media-kinds.ts +5 -3
- package/src/media-store/forget.ts +54 -0
- package/src/media-store/put.ts +4 -3
- package/src/media-store/space.ts +63 -24
- package/src/messages/brief.ts +3 -3
- package/src/messages/score.ts +2 -0
- package/src/recordings/delete.ts +19 -0
- package/src/recordings/edit.ts +231 -0
- package/src/recordings/load.ts +37 -0
- package/src/recordings/marks-draw.ts +111 -0
- package/src/recordings/marks-png.ts +25 -0
- package/src/recordings/marks.ts +138 -0
- package/src/recordings/media-kind.ts +67 -0
- package/src/recordings/model.ts +170 -0
- package/src/recordings/open.ts +33 -0
- package/src/recordings/place.ts +89 -0
- package/src/recordings/presets.ts +99 -0
- package/src/recordings/recorder-app/Capture.swift +510 -0
- package/src/recordings/recorder-app/Clip.swift +107 -0
- package/src/recordings/recorder-app/Editor.swift +39 -0
- package/src/recordings/recorder-app/Tray.swift +298 -0
- package/src/recordings/recorder-app/main.swift +859 -0
- package/src/recordings/recorder-app.ts +46 -0
- package/src/recordings/recorder.shipkit.ts +11 -0
- package/src/recordings/routes.ts +140 -0
- package/src/recordings/run-recorder.ts +40 -0
- package/src/recordings/share.ts +107 -0
- package/src/recordings/take.ts +146 -0
- package/src/recordings/view.ts +40 -0
- package/src/release/gate-checkout.ts +74 -0
- package/src/release/release-scope.ts +35 -26
- package/src/release/run-bump.ts +79 -37
- package/src/release/run-release.ts +67 -53
- package/src/releases/add.ts +38 -5
- package/src/releases/before.ts +31 -0
- package/src/releases/from-card.ts +105 -20
- package/src/releases/model.ts +100 -18
- package/src/releases/pictures.ts +29 -8
- package/src/repo-config.ts +7 -3
- package/src/review/http.ts +1 -1
- package/src/review/model.ts +12 -2
- package/src/review/stage-card.ts +1 -1
- package/src/review/store.ts +12 -2
- package/src/review/trailer-desk.ts +20 -0
- package/src/review/trailer-model.ts +67 -0
- package/src/review/trailer-routes.ts +98 -1
- package/src/review/trailer-voices.ts +144 -0
- package/src/review/watchers.ts +58 -1
- package/src/secrets/manager.ts +18 -1
- package/src/serve/hub-app/benign-errors.ts +22 -0
- package/src/serve/hub-app/hash-route.ts +27 -15
- package/src/serve/hub-app/home-st.ts +23 -5
- package/src/serve/hub-app/home.html +1 -0
- package/src/serve/hub-app/hub-app.tsx +8 -1
- package/src/serve/hub-app/icons.tsx +2 -0
- package/src/serve/hub-app/palette-st.ts +1 -0
- package/src/serve/hub-app/project-list.css +7 -2
- package/src/serve/hub-app/project-list.tsx +10 -0
- package/src/serve/hub-app/projects-svc.ts +17 -7
- package/src/serve/hub-app/recordings-st.ts +641 -0
- package/src/serve/hub-app/recordings-view.tsx +504 -0
- package/src/serve/hub-app/recordings.css +82 -0
- package/src/serve/hub-app/releases/releases-page.tsx +121 -28
- package/src/serve/hub-app/releases/releases-st.ts +46 -4
- package/src/serve/hub-app/releases.css +30 -3
- package/src/serve/hub-app/repo-sections.ts +1 -1
- package/src/serve/hub-app/repo-st.ts +16 -11
- package/src/serve/hub-app/review/clip-st.ts +3 -1
- package/src/serve/hub-app/review/kit.tsx +3 -3
- package/src/serve/hub-app/review/trailer-page.tsx +32 -6
- package/src/serve/hub-app/review/trailer-st.ts +184 -8
- package/src/serve/hub-app/review/trailer-timeline.tsx +39 -4
- package/src/serve/hub-app/review/trailer-voices-st.ts +370 -0
- package/src/serve/hub-app/review/trailer-voices.tsx +574 -0
- package/src/serve/hub-app/review.css +90 -0
- package/src/serve/hub-app/social/comms-page.tsx +10 -5
- package/src/serve/hub-app/social/next-release.tsx +118 -0
- package/src/serve/hub-app/social/post-card.tsx +31 -3
- package/src/serve/hub-app/social/social-model.ts +6 -0
- package/src/serve/hub-app/social/social-st.ts +50 -1
- package/src/serve/hub-app/social.css +12 -0
- package/src/serve/hub-app/tree-build.ts +2 -0
- package/src/serve/project-gutter-route.ts +8 -0
- package/src/serve/project-gutter.ts +4 -2
- package/src/shipkit.ts +2 -2
- package/src/shot/box.ts +49 -22
- package/src/shot/browser-slot.ts +13 -3
- package/src/shot/client.ts +1 -1
- package/src/shot/film-box.ts +193 -117
- package/src/shot/shooter.ts +20 -8
- package/src/smoke/run-smoke.ts +8 -6
- package/src/social/compose.ts +14 -0
- package/src/social/desk-index.ts +49 -8
- package/src/social/discord-layout.ts +6 -2
- package/src/social/drafts.ts +1 -1
- package/src/social/kinds.ts +3 -2
- package/src/social/load.ts +42 -9
- package/src/social/publish/index.ts +27 -10
- package/src/social/publish/secret.ts +12 -4
- package/src/social/release-card.ts +69 -0
- package/src/social/release-stage.ts +173 -30
- package/src/social/routes.ts +7 -1
- package/src/social/stage.ts +45 -22
- package/src/trailer/bed.ts +111 -0
- package/src/trailer/build.ts +81 -38
- package/src/trailer/ffmpeg.ts +14 -0
- package/src/trailer/grammar.ts +32 -4
- package/src/trailer/graph.ts +12 -1
- package/src/trailer/look.ts +7 -0
- package/src/trailer/shot-plan.ts +75 -0
- package/src/trailer/shot-state.ts +191 -0
- package/src/trailer/sound/audio.ts +28 -0
- package/src/trailer/sound/brand.ts +53 -3
- package/src/trailer/sound/gen.ts +61 -2
- package/src/trailer/sound/index.ts +25 -11
- package/src/trailer/sound/presets.ts +86 -5
- package/src/trailer/sound/run.ts +1 -1
- package/src/trailer/sound/takes.ts +94 -0
- package/src/trailer/titles.ts +36 -0
- package/src/trailer/url.ts +1 -1
- package/src/veille/chrome.ts +6 -3
package/src/media-store/space.ts
CHANGED
|
@@ -48,8 +48,9 @@ export interface Space {
|
|
|
48
48
|
head(key: string): Promise<{ size: number } | null>
|
|
49
49
|
/** a GET URL good for `seconds`, on the CDN edge when the Space has one: signed for the origin host, which the edge
|
|
50
50
|
* checks it against (a URL signed for the edge's own host answers SignatureDoesNotMatch). `as` signs a download
|
|
51
|
-
* under that file name: `response-content-disposition` and `response-content-type` ride in the signed query
|
|
52
|
-
|
|
51
|
+
* under that file name: `response-content-disposition` and `response-content-type` ride in the signed query;
|
|
52
|
+
* `inline` plays it in the browser that opens the link, the name kept for a save */
|
|
53
|
+
signed(key: string, seconds?: number, as?: { name: string; type: string; inline?: boolean }): string
|
|
53
54
|
/** the permanent URL of a `public/` object, on the CDN edge when the Space has one; throws for any other key */
|
|
54
55
|
publicUrl(key: string): string
|
|
55
56
|
/** the object written at `to`, through a file beside it renamed: no reader sees half of it */
|
|
@@ -70,26 +71,46 @@ const missing = (e: unknown): boolean => {
|
|
|
70
71
|
/** the longest a signed URL can last: the SigV4 limit */
|
|
71
72
|
export const SIGNED_MAX_S = 7 * 86_400
|
|
72
73
|
|
|
73
|
-
/** `attachment` under the name: a plain ascii fallback, the exact name in `filename*` when it is not ascii */
|
|
74
|
-
export function attachment(name: string): string {
|
|
74
|
+
/** `attachment` (or `inline`) under the name: a plain ascii fallback, the exact name in `filename*` when it is not ascii */
|
|
75
|
+
export function attachment(name: string, inline = false): string {
|
|
76
|
+
const how = inline ? 'inline' : 'attachment'
|
|
75
77
|
const plain = name.replace(/[^\x20-\x7e]|["\\]/g, '_')
|
|
76
|
-
return plain === name ?
|
|
78
|
+
return plain === name ? `${how}; filename="${name}"` : `${how}; filename="${plain}"; filename*=UTF-8''${encodeURIComponent(name)}`
|
|
77
79
|
}
|
|
78
80
|
|
|
79
81
|
const hex = (b: ArrayBuffer | Uint8Array): string => Buffer.from(b instanceof Uint8Array ? b : new Uint8Array(b)).toString('hex')
|
|
80
82
|
const sha256 = (s: string): string => new Bun.CryptoHasher('sha256').update(s).digest('hex')
|
|
81
83
|
const hmac = (key: string | Uint8Array, s: string): Uint8Array => new Uint8Array(new Bun.CryptoHasher('sha256', key).update(s).digest())
|
|
82
84
|
|
|
83
|
-
/**
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
85
|
+
/** S3's own encoding of a path or query part: every byte but the unreserved ones, `/` kept in a path */
|
|
86
|
+
const s3enc = (v: string, path = false): string => {
|
|
87
|
+
const e = encodeURIComponent(v).replace(/[!'()*]/g, (ch) => `%${ch.charCodeAt(0).toString(16).toUpperCase()}`)
|
|
88
|
+
return path ? e.replace(/%2F/g, '/') : e
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** one call Bun's S3 client has no verb for, signed by hand (SigV4, header auth): on the bucket (`key` null) or on an
|
|
92
|
+
* object, with its query (a sub resource is a name with an empty value, `{ versioning: '' }`). Answers its text.
|
|
93
|
+
* No DELETE here: the one delete of the Space is signedDelete, which forget.ts alone calls */
|
|
94
|
+
export function signedCall(c: SpaceConfig, method: 'GET' | 'PUT', key: string | null, query: Record<string, string>, body = ''): Promise<string> {
|
|
95
|
+
return signed(c, method, key, query, body)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** an object's version deleted for good: forget.ts alone calls it (tests/media-store/no-remote-delete.test.ts) */
|
|
99
|
+
export function signedDelete(c: SpaceConfig, key: string, query: Record<string, string>): Promise<string> {
|
|
100
|
+
return signed(c, 'DELETE', key, query)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function signed(c: SpaceConfig, method: 'GET' | 'PUT' | 'DELETE', key: string | null, query: Record<string, string>, body = ''): Promise<string> {
|
|
104
|
+
const base = c.path === true ? `${c.endpoint}/${c.bucket}` : c.endpoint
|
|
105
|
+
const url = new URL(key === null ? (c.path === true ? base : `${base}/`) : `${base}/${s3enc(key, true)}`)
|
|
106
|
+
const qs = Object.keys(query).sort().map((k) => `${s3enc(k)}=${s3enc(query[k]!)}`).join('&')
|
|
107
|
+
url.search = qs
|
|
87
108
|
const now = new Date().toISOString().replace(/[-:]|\.\d{3}/g, '')
|
|
88
109
|
const day = now.slice(0, 8)
|
|
89
110
|
const payload = sha256(body)
|
|
90
111
|
const headers: Record<string, string> = { host: url.host, 'x-amz-content-sha256': payload, 'x-amz-date': now }
|
|
91
112
|
const names = Object.keys(headers).sort()
|
|
92
|
-
const canonical = [method, url.pathname,
|
|
113
|
+
const canonical = [method, url.pathname, qs, ...names.map((n) => `${n}:${headers[n]}`), '', names.join(';'), payload].join('\n')
|
|
93
114
|
const scope = `${day}/${c.region}/s3/aws4_request`
|
|
94
115
|
const toSign = ['AWS4-HMAC-SHA256', now, scope, sha256(canonical)].join('\n')
|
|
95
116
|
const k = hmac(hmac(hmac(hmac(`AWS4${c.creds.secretAccessKey}`, day), c.region), 's3'), 'aws4_request')
|
|
@@ -98,10 +119,12 @@ async function bucketCall(c: SpaceConfig, method: 'GET' | 'PUT', sub: string, bo
|
|
|
98
119
|
const md5: Record<string, string> = body !== '' ? { 'Content-MD5': new Bun.CryptoHasher('md5').update(body).digest('base64') } : {}
|
|
99
120
|
const r = await fetch(url, { method, headers: { ...sent, ...md5, Authorization: auth }, ...(body !== '' ? { body } : {}) })
|
|
100
121
|
const text = await r.text()
|
|
101
|
-
if (!r.ok) throw new Error(`${method} ?${
|
|
122
|
+
if (!r.ok) throw new Error(`${method} ${key ?? ''}?${qs} on ${c.bucket}: ${r.status} ${/<Code>([^<]+)<\/Code>/.exec(text)?.[1] ?? text.slice(0, 200)}`)
|
|
102
123
|
return text
|
|
103
124
|
}
|
|
104
125
|
|
|
126
|
+
const bucketCall = (c: SpaceConfig, method: 'GET' | 'PUT', sub: string, body = ''): Promise<string> => signedCall(c, method, null, { [sub]: '' }, body)
|
|
127
|
+
|
|
105
128
|
export function spaceOf(c: SpaceConfig): Space {
|
|
106
129
|
const client = new S3Client({ ...c.creds, bucket: c.bucket, region: c.region, endpoint: c.endpoint, virtualHostedStyle: c.path !== true })
|
|
107
130
|
const origin = new URL(c.endpoint).host
|
|
@@ -121,7 +144,7 @@ export function spaceOf(c: SpaceConfig): Space {
|
|
|
121
144
|
}
|
|
122
145
|
},
|
|
123
146
|
signed(key, seconds = SIGNED_S, as) {
|
|
124
|
-
const url = new URL(client.presign(key, { expiresIn: seconds, method: 'GET', ...(as ? { contentDisposition: attachment(as.name), type: as.type } : {}) }))
|
|
147
|
+
const url = new URL(client.presign(key, { expiresIn: seconds, method: 'GET', ...(as ? { contentDisposition: attachment(as.name, as.inline === true), type: as.type } : {}) }))
|
|
125
148
|
if (edge && url.host === origin) url.host = edge
|
|
126
149
|
return url.toString()
|
|
127
150
|
},
|
|
@@ -168,30 +191,46 @@ export function machineSpace(): Promise<{ space: Space | null; why: string }> {
|
|
|
168
191
|
return held
|
|
169
192
|
}
|
|
170
193
|
|
|
171
|
-
/** the house's Space signed with the key pair of `secret` (the config's own key when none is given)
|
|
172
|
-
*
|
|
173
|
-
export function spaceFromHouse(secret?: string): Promise<{ space: Space | null; why: string }> {
|
|
174
|
-
|
|
175
|
-
|
|
194
|
+
/** the house's Space signed with the key pair of `secret` (the config's own key when none is given). A one time key
|
|
195
|
+
* with more rights (turning versioning on) is read anew and never held; the house's own is held for the process */
|
|
196
|
+
export async function spaceFromHouse(secret?: string): Promise<{ space: Space | null; why: string }> {
|
|
197
|
+
const c = await houseSpaceConfig(secret)
|
|
198
|
+
return { space: c.config ? spaceOf(c.config) : null, why: c.why }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let config: { config: SpaceConfig | null; why: string } | null = null
|
|
202
|
+
|
|
203
|
+
/** the house's Space as a config, the key pair read: what a hand signed call needs (forget.ts) */
|
|
204
|
+
export async function houseSpaceConfig(secret?: string): Promise<{ config: SpaceConfig | null; why: string }> {
|
|
205
|
+
if (secret === undefined && config) return config
|
|
206
|
+
const out = await (async (): Promise<{ config: SpaceConfig | null; why: string }> => {
|
|
207
|
+
if (process.env.SHIPKIT_SPACES === 'off') return { config: null, why: 'SHIPKIT_SPACES=off' }
|
|
176
208
|
const { houseRootOf } = await import('../home/home-start.ts')
|
|
177
209
|
const { loadRepoConfig } = await import('../repo-config.ts')
|
|
178
210
|
const house = houseRootOf(process.cwd())
|
|
179
|
-
if (!house) return {
|
|
211
|
+
if (!house) return { config: null, why: `no house repo seen from ${process.cwd()}` }
|
|
180
212
|
const c = await loadRepoConfig(house)
|
|
181
213
|
const r = c.serve?.mediaSpace
|
|
182
|
-
if (!r) return {
|
|
183
|
-
if (!c.credsReadCommand) return {
|
|
214
|
+
if (!r) return { config: null, why: `the house config (${house}) names no serve.mediaSpace` }
|
|
215
|
+
if (!c.credsReadCommand) return { config: null, why: `the house config (${house}) declares no credsReadCommand to read ${r.secret}` }
|
|
184
216
|
const name = secret ?? r.secret
|
|
185
217
|
let v = ''
|
|
186
218
|
try {
|
|
187
219
|
v = readSecret(c.credsReadCommand, name)
|
|
188
220
|
} catch (e) {
|
|
189
|
-
return {
|
|
221
|
+
return { config: null, why: (e as Error).message }
|
|
190
222
|
}
|
|
191
223
|
const at = v.indexOf(':')
|
|
192
|
-
if (at <= 0) return {
|
|
193
|
-
return {
|
|
194
|
-
})().catch((e: unknown) => ({
|
|
224
|
+
if (at <= 0) return { config: null, why: `the secret ${name} is no KEY:SECRET pair` }
|
|
225
|
+
return { config: { ...r, cdn: r.cdn ?? null, creds: { accessKeyId: v.slice(0, at), secretAccessKey: v.slice(at + 1) } }, why: '' }
|
|
226
|
+
})().catch((e: unknown) => ({ config: null, why: `the house config does not load: ${(e as Error).message}` }))
|
|
227
|
+
if (secret === undefined && out.config) config = out
|
|
228
|
+
return out
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** a test's own Space config in place of the house's (forget.ts); undefined reads the house's again */
|
|
232
|
+
export function useSpaceConfig(c: SpaceConfig | null | undefined): void {
|
|
233
|
+
config = c === undefined ? null : { config: c, why: c ? '' : 'no Space in this test' }
|
|
195
234
|
}
|
|
196
235
|
|
|
197
236
|
/** a test's own Space in place of the machine's; undefined gives the machine's back */
|
package/src/messages/brief.ts
CHANGED
|
@@ -34,14 +34,14 @@ export function briefOf(s: Store, kind: string, run: string, who: string): strin
|
|
|
34
34
|
const gs = personas.filter((p) => p.kind === 'gatekeeper')
|
|
35
35
|
if (!gs.length) throw new Error('messages: no gatekeeper in personas.md')
|
|
36
36
|
return [
|
|
37
|
-
`You play ${gs.length} gatekeepers in a message test, each judged on its own. Read ONLY ${join(dir, 'packets', `${gs[0]!.id}.md`)}. Open no other file.`,
|
|
37
|
+
`You play ${gs.length} gatekeepers in a message test, each judged on its own: ${gs.map((g) => g.id).join(', ')}. Read ONLY ${join(dir, 'packets', `${gs[0]!.id}.md`)}: that one sheet holds the lines every one of them judges. Open no other file.`,
|
|
38
38
|
'',
|
|
39
39
|
...para(b.gatekeepers),
|
|
40
40
|
...gs.map((g) => `- **${g.id}**: ${g.who}`),
|
|
41
41
|
'',
|
|
42
|
-
`For each one, judge EVERY line and write a JSON array of {"id", "verdict", "words": what drives it or ""}
|
|
42
|
+
`For each one, judge EVERY line and write a JSON array of {"id", "verdict", "words": what drives it or ""} with the Write tool, one file a gatekeeper: ${gs.map((g) => join(dir, 'verdicts', `${g.id}.json`)).join(', ')}. Be realistic: reserve the harsh verdicts for what would truly trigger them.`,
|
|
43
43
|
'',
|
|
44
|
-
|
|
44
|
+
`Reply with ${gs.length} lines, one a gatekeeper: its id, the ids it refuses, then the ids it flags.`,
|
|
45
45
|
].join('\n')
|
|
46
46
|
}
|
|
47
47
|
if (kind === 'judge') {
|
package/src/messages/score.ts
CHANGED
|
@@ -50,6 +50,8 @@ export function matrix(messages: Message[], personas: Persona[], dir: string): s
|
|
|
50
50
|
for (const f of jsonsIn(join(dir, 'judged'))) for (const j of readJson<Judged[]>(join(dir, 'judged', f))) judged.set(`${j.id}/${j.persona}`, j)
|
|
51
51
|
const verdicts = new Map<string, Map<string, Verdict>>()
|
|
52
52
|
for (const f of jsonsIn(join(dir, 'verdicts'))) verdicts.set(f.slice(0, -5), new Map(readJson<Verdict[]>(join(dir, 'verdicts', f)).map((v) => [v.id, v])))
|
|
53
|
+
const unjudged = verdicts.size ? personas.filter((p) => p.kind === 'gatekeeper' && !verdicts.has(p.id)).map((p) => p.id) : []
|
|
54
|
+
if (unjudged.length) throw new Error(`messages: no verdicts of ${unjudged.join(', ')} in ${join(dir, 'verdicts')}: the gatekeepers brief writes one file a gatekeeper`)
|
|
53
55
|
|
|
54
56
|
const cell = (c: Candidate, p: string): number | undefined => {
|
|
55
57
|
const r = readings.get(p)?.get(c.id)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// a recording deleted for good (wiki tools/shipkit/recordings.md, Delete): every Space key it ever had (its files, an
|
|
2
|
+
// older export, every public copy) off the Space, every version, so a private link dies at once (a public one when the
|
|
3
|
+
// CDN's copy expires, an hour at most); then its folder to the Trash. The Space goes first: a failure
|
|
4
|
+
// there keeps the take on the disk, so the delete can run again
|
|
5
|
+
import { mediaKey } from '../media-store/content.ts'
|
|
6
|
+
import { forgetForGood } from '../media-store/forget.ts'
|
|
7
|
+
import { RECORDING_KIND } from './media-kind.ts'
|
|
8
|
+
import type { Recording } from './model.ts'
|
|
9
|
+
import { trash } from './open.ts'
|
|
10
|
+
import { publicKeyOf } from './share.ts'
|
|
11
|
+
|
|
12
|
+
export async function deleteRecording(r: Recording, o: { trash?: (dir: string) => Promise<void> } = {}): Promise<{ gone: number; kept: string[] }> {
|
|
13
|
+
// what it holds now, every key it held before (an older export, an older public copy), and its public copy
|
|
14
|
+
const keys = new Set([...Object.entries(r.files).map(([name, f]) => mediaKey(RECORDING_KIND, f, name)), ...(r.former ?? [])])
|
|
15
|
+
if (r.publicUrl) keys.add(publicKeyOf(r.publicUrl))
|
|
16
|
+
const res = await forgetForGood([...keys], { owner: r.dir })
|
|
17
|
+
await (o.trash ?? trash)(r.dir)
|
|
18
|
+
return { gone: res.gone.length, kept: res.kept.map((k) => `${k.key}: ${k.why}`) }
|
|
19
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// the edit of a recording as text he reads and types (wiki tools/shipkit/recordings.md, The edit): `edit.txt` beside
|
|
2
|
+
// the take, never a change to the take. One line per verb:
|
|
3
|
+
// keep 0:01.5-0:12, 0:15-0:40 the parts that stay, in the take's time (none: all of it)
|
|
4
|
+
// speed 2x 0:20-0:30 a part played faster or slower (in the take's time, inside what is kept)
|
|
5
|
+
// crop 1280x720+40+80 WxH+X+Y of the take's frame
|
|
6
|
+
// mute no sound
|
|
7
|
+
// arrow, rect, text, step, blur the marks drawn on it, each with an optional span (marks.ts)
|
|
8
|
+
// The export is one ffmpeg pass into `out.<ext>`; the hub's editor writes the same text
|
|
9
|
+
import { boundsOf, MARK_VERBS, parseMark, printMark, style, type Mark, type Span } from './marks.ts'
|
|
10
|
+
|
|
11
|
+
export type { Span }
|
|
12
|
+
export type Crop = { w: number; h: number; x: number; y: number }
|
|
13
|
+
export type Edit = { keep: Span[]; speed: (Span & { factor: number })[]; crop: Crop | null; mute: boolean; marks: Mark[] }
|
|
14
|
+
|
|
15
|
+
export const EMPTY_EDIT: Edit = { keep: [], speed: [], crop: null, mute: false, marks: [] }
|
|
16
|
+
|
|
17
|
+
/** `75.5`, `1:15.5` or `0:01:15.5` as seconds */
|
|
18
|
+
export function parseTime(t: string): number {
|
|
19
|
+
if (!/^\d+(:\d{1,2}){0,2}(\.\d+)?$/.test(t)) throw new Error(`${t}: a time is seconds (75.5) or m:ss (1:15.5)`)
|
|
20
|
+
return t.split(':').reduce((acc, p) => acc * 60 + Number(p), 0)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** seconds as `m:ss.s`, the tenth only when there is one */
|
|
24
|
+
export function printTime(s: number): string {
|
|
25
|
+
const tenths = Math.round(s * 10)
|
|
26
|
+
const m = Math.floor(tenths / 600)
|
|
27
|
+
const rest = (tenths - m * 600) / 10
|
|
28
|
+
const ss = rest < 10 ? `0${rest}` : String(rest)
|
|
29
|
+
return `${m}:${ss}`
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const span = (w: string): Span => {
|
|
33
|
+
const m = /^([\d:.]+)-([\d:.]+)$/.exec(w)
|
|
34
|
+
if (!m) throw new Error(`${w}: a part is <from>-<to>, like 0:01-0:12`)
|
|
35
|
+
const s = { from: parseTime(m[1]!), to: parseTime(m[2]!) }
|
|
36
|
+
if (!(s.to > s.from)) throw new Error(`${w}: it ends before it starts`)
|
|
37
|
+
return s
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** the text as an edit; every error names its line */
|
|
41
|
+
export function parseEdit(text: string): Edit {
|
|
42
|
+
const e: Edit = { keep: [], speed: [], crop: null, mute: false, marks: [] }
|
|
43
|
+
text.split('\n').forEach((raw, i) => {
|
|
44
|
+
// a comment starts at a # outside quotes: a text mark may hold one
|
|
45
|
+
const line = raw.replace(/^((?:[^"#]|"(?:[^"\\]|\\.)*")*)#.*$/, '$1').trim()
|
|
46
|
+
if (line === '') return
|
|
47
|
+
const [verb, ...rest] = line.split(/\s+/)
|
|
48
|
+
const at = (msg: string): Error => new Error(`edit line ${i + 1}: ${msg}`)
|
|
49
|
+
try {
|
|
50
|
+
if (verb === 'keep') {
|
|
51
|
+
e.keep.push(...rest.join(' ').split(',').map((w) => w.trim()).filter((w) => w !== '').map(span))
|
|
52
|
+
} else if (verb === 'speed') {
|
|
53
|
+
const f = /^(\d+(?:\.\d+)?)x$/.exec(rest[0] ?? '')
|
|
54
|
+
if (!f || !(Number(f[1]) >= 0.25 && Number(f[1]) <= 16)) throw new Error('speed <factor>x <from>-<to>, the factor 0.25 to 16')
|
|
55
|
+
if (rest.length !== 2) throw new Error('speed <factor>x <from>-<to>')
|
|
56
|
+
e.speed.push({ ...span(rest[1]!), factor: Number(f[1]) })
|
|
57
|
+
} else if (verb === 'crop') {
|
|
58
|
+
const c = /^(\d+)x(\d+)\+(\d+)\+(\d+)$/.exec(rest[0] ?? '')
|
|
59
|
+
if (!c || rest.length !== 1) throw new Error('crop WxH+X+Y, like 1280x720+40+80')
|
|
60
|
+
e.crop = { w: Number(c[1]), h: Number(c[2]), x: Number(c[3]), y: Number(c[4]) }
|
|
61
|
+
} else if (verb === 'mute') {
|
|
62
|
+
if (rest.length > 0) throw new Error('mute takes no word')
|
|
63
|
+
e.mute = true
|
|
64
|
+
} else if ((MARK_VERBS as readonly string[]).includes(verb!)) {
|
|
65
|
+
e.marks.push(parseMark(line, parseTime))
|
|
66
|
+
} else throw new Error(`${verb}: the verbs are keep, speed, crop, mute, ${MARK_VERBS.join(', ')}`)
|
|
67
|
+
} catch (err) {
|
|
68
|
+
throw at((err as Error).message)
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
e.keep.sort((a, b) => a.from - b.from)
|
|
72
|
+
for (let i = 1; i < e.keep.length; i++) if (e.keep[i]!.from < e.keep[i - 1]!.to) throw new Error(`keep: ${printTime(e.keep[i]!.from)} overlaps the part before it`)
|
|
73
|
+
e.speed.sort((a, b) => a.from - b.from)
|
|
74
|
+
for (let i = 1; i < e.speed.length; i++) if (e.speed[i]!.from < e.speed[i - 1]!.to) throw new Error(`speed: ${printTime(e.speed[i]!.from)} overlaps the part before it`)
|
|
75
|
+
return e
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function printEdit(e: Edit): string {
|
|
79
|
+
const l: string[] = []
|
|
80
|
+
const sp = (s: Span): string => `${printTime(s.from)}-${printTime(s.to)}`
|
|
81
|
+
if (e.keep.length > 0) l.push(`keep ${e.keep.map(sp).join(', ')}`)
|
|
82
|
+
for (const s of e.speed) l.push(`speed ${s.factor}x ${sp(s)}`)
|
|
83
|
+
if (e.crop) l.push(`crop ${e.crop.w}x${e.crop.h}+${e.crop.x}+${e.crop.y}`)
|
|
84
|
+
if (e.mute) l.push('mute')
|
|
85
|
+
for (const m of e.marks) l.push(printMark(m, printTime))
|
|
86
|
+
return l.length === 0 ? '' : `${l.join('\n')}\n`
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const isEmpty = (e: Edit): boolean => e.keep.length === 0 && e.speed.length === 0 && e.crop === null && !e.mute && e.marks.length === 0
|
|
90
|
+
|
|
91
|
+
/** the pieces of the take that play, in order, each at its factor: the kept parts cut at every speed change */
|
|
92
|
+
export function pieces(e: Edit, length: number): (Span & { factor: number })[] {
|
|
93
|
+
const keep = e.keep.length === 0 ? [{ from: 0, to: length }] : e.keep.map((k) => ({ from: Math.max(0, k.from), to: Math.min(length, k.to) })).filter((k) => k.to > k.from)
|
|
94
|
+
if (keep.length === 0) throw new Error(`keep: nothing of the take's ${printTime(length)} is kept`)
|
|
95
|
+
const out: (Span & { factor: number })[] = []
|
|
96
|
+
for (const k of keep) {
|
|
97
|
+
const cuts = new Set([k.from, k.to])
|
|
98
|
+
for (const s of e.speed) for (const t of [s.from, s.to]) if (t > k.from && t < k.to) cuts.add(t)
|
|
99
|
+
const ts = [...cuts].sort((a, b) => a - b)
|
|
100
|
+
for (let i = 0; i + 1 < ts.length; i++) {
|
|
101
|
+
const from = ts[i]!
|
|
102
|
+
const to = ts[i + 1]!
|
|
103
|
+
const f = e.speed.find((s) => s.from <= from && s.to >= to)?.factor ?? 1
|
|
104
|
+
out.push({ from, to, factor: f })
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return out
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** the length of the export */
|
|
111
|
+
export const editedLength = (e: Edit, length: number): number => pieces(e, length).reduce((a, p) => a + (p.to - p.from) / p.factor, 0)
|
|
112
|
+
|
|
113
|
+
/** atempo takes 0.5 to 100: a slower factor is a chain of halves */
|
|
114
|
+
function atempo(f: number): string {
|
|
115
|
+
const parts: string[] = []
|
|
116
|
+
let rest = f
|
|
117
|
+
while (rest < 0.5) {
|
|
118
|
+
parts.push('atempo=0.5')
|
|
119
|
+
rest /= 0.5
|
|
120
|
+
}
|
|
121
|
+
parts.push(`atempo=${rest}`)
|
|
122
|
+
return parts.join(',')
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const even = (n: number): number => Math.max(2, Math.floor(n / 2) * 2)
|
|
126
|
+
|
|
127
|
+
/** the crop kept inside the frame, its size even (h264 refuses an odd one) */
|
|
128
|
+
export function fitCrop(c: Crop, w: number, h: number): Crop {
|
|
129
|
+
const x = Math.min(Math.max(0, c.x), w - 2)
|
|
130
|
+
const y = Math.min(Math.max(0, c.y), h - 2)
|
|
131
|
+
return { x, y, w: even(Math.min(c.w, w - x)), h: even(Math.min(c.h, h - y)) }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** the canvas an export draws on, in the take's pixels: the take, grown to hold every drawn mark that reaches past it
|
|
135
|
+
* with a margin (x and y at most 0), its size even (h264 refuses an odd one) */
|
|
136
|
+
export function canvasOf(e: Pick<Edit, 'marks'>, frame: { width: number; height: number }): { x: number; y: number; w: number; h: number } {
|
|
137
|
+
let x0 = 0
|
|
138
|
+
let y0 = 0
|
|
139
|
+
let x1 = frame.width
|
|
140
|
+
let y1 = frame.height
|
|
141
|
+
const st = style(frame)
|
|
142
|
+
const pad = st.stroke * 3
|
|
143
|
+
for (const m of e.marks) {
|
|
144
|
+
if (m.kind === 'blur') continue
|
|
145
|
+
// the mark's own extent (an arrow's two points, a box, a step's disc, a text's estimate): a side grows only where
|
|
146
|
+
// the mark itself goes past the take, then by a margin its stroke and its head fit in
|
|
147
|
+
const b = m.kind === 'arrow' ? { x: Math.min(m.from.x, m.to.x), y: Math.min(m.from.y, m.to.y), w: Math.abs(m.to.x - m.from.x), h: Math.abs(m.to.y - m.from.y) } : boundsOf(m, frame)
|
|
148
|
+
if (b.x < 0) x0 = Math.min(x0, Math.floor(b.x - pad))
|
|
149
|
+
if (b.y < 0) y0 = Math.min(y0, Math.floor(b.y - pad))
|
|
150
|
+
if (b.x + b.w > frame.width) x1 = Math.max(x1, Math.ceil(b.x + b.w + pad))
|
|
151
|
+
if (b.y + b.h > frame.height) y1 = Math.max(y1, Math.ceil(b.y + b.h + pad))
|
|
152
|
+
}
|
|
153
|
+
const w = x1 - x0 + ((x1 - x0) % 2)
|
|
154
|
+
const h = y1 - y0 + ((y1 - y0) % 2)
|
|
155
|
+
return { x: x0, y: y0, w, h }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** the take placed on its canvas, when the canvas grew: transparent around a still, black around a video */
|
|
159
|
+
function grown(input: string, frame: { width: number; height: number }, c: { x: number; y: number; w: number; h: number }, still: boolean): { filters: string[]; out: string } {
|
|
160
|
+
if (c.x === 0 && c.y === 0 && c.w === frame.width && c.h === frame.height) return { filters: [], out: input }
|
|
161
|
+
return { filters: [`[${input}]${still ? 'format=rgba,' : ''}pad=${c.w}:${c.h}:${-c.x}:${-c.y}:color=${still ? '0x00000000' : 'black'}[cv]`], out: 'cv' }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** the box kept inside the frame, its size even and at least 2 */
|
|
165
|
+
const fitBox = (b: Crop, w: number, h: number): Crop => fitCrop(b, w, h)
|
|
166
|
+
|
|
167
|
+
/** the marks burnt into `[in]`, in their order: a drawn mark is the overlay of its PNG (input `inputs[j]`, the whole
|
|
168
|
+
* frame), a blur is its box blurred in place; each only inside its span when it has one, in the take's time, so this
|
|
169
|
+
* runs before any trim. Returns the filters and the label of the result */
|
|
170
|
+
export function markFilters(e: Edit, frame: { width: number; height: number }, input: string, overlayInputs: readonly number[], o: { still?: boolean } = {}): { filters: string[]; out: string } {
|
|
171
|
+
const cv = canvasOf(e, frame)
|
|
172
|
+
const g = grown(input, frame, cv, o.still === true)
|
|
173
|
+
const f: string[] = [...g.filters]
|
|
174
|
+
let cur = g.out
|
|
175
|
+
let j = 0
|
|
176
|
+
e.marks.forEach((m, i) => {
|
|
177
|
+
const en = m.span ? `:enable='between(t,${m.span.from},${m.span.to})'` : ''
|
|
178
|
+
if (m.kind === 'blur') {
|
|
179
|
+
// a blur blurs the take, never the space around it: kept inside the take, placed on the canvas
|
|
180
|
+
const t = fitBox(m.box, frame.width, frame.height)
|
|
181
|
+
const b = { ...t, x: t.x - cv.x, y: t.y - cv.y }
|
|
182
|
+
const r = Math.max(1, Math.min(Math.round(Math.min(frame.width, frame.height) / 60), Math.floor(Math.min(b.w, b.h) / 2) - 1))
|
|
183
|
+
const c = Math.max(1, Math.min(r, Math.floor(Math.min(b.w, b.h) / 4) - 1))
|
|
184
|
+
f.push(`[${cur}]split[ma${i}][mb${i}]`)
|
|
185
|
+
f.push(`[mb${i}]crop=${b.w}:${b.h}:${b.x}:${b.y},boxblur=${r}:3:${c}:3[mc${i}]`)
|
|
186
|
+
f.push(`[ma${i}][mc${i}]overlay=${b.x}:${b.y}${en}[m${i}]`)
|
|
187
|
+
} else {
|
|
188
|
+
f.push(`[${cur}][${overlayInputs[j++]}:v]overlay=0:0${en}[m${i}]`)
|
|
189
|
+
}
|
|
190
|
+
cur = `m${i}`
|
|
191
|
+
})
|
|
192
|
+
return { filters: f, out: cur }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** the marks that are drawn (all but blur), each to its own PNG */
|
|
196
|
+
export const drawnMarks = (e: Edit): number[] => e.marks.flatMap((m, i) => (m.kind === 'blur' ? [] : [i]))
|
|
197
|
+
|
|
198
|
+
/** the crop on the canvas: the take's box moved by the canvas's own place, kept inside it */
|
|
199
|
+
function cropOn(e: Edit, frame: { width: number; height: number }): Crop | null {
|
|
200
|
+
if (!e.crop) return null
|
|
201
|
+
const c = canvasOf(e, frame)
|
|
202
|
+
return fitCrop({ ...e.crop, x: e.crop.x - c.x, y: e.crop.y - c.y }, c.w, c.h)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** ffmpeg's arguments for the export of a video, `-i` to the output; `overlays`: the PNG of each drawn mark, in order */
|
|
206
|
+
export function exportArgs(e: Edit, src: { file: string; seconds: number; width: number; height: number; audio: boolean }, out: string, codec: string[], overlays: readonly string[] = []): string[] {
|
|
207
|
+
const ps = pieces(e, src.seconds)
|
|
208
|
+
const audio = src.audio && !e.mute
|
|
209
|
+
const marked = markFilters(e, src, '0:v', overlays.map((_, k) => k + 1))
|
|
210
|
+
const f: string[] = [...marked.filters]
|
|
211
|
+
const vin = (i: number): string => (ps.length === 1 ? marked.out : `s${i}`)
|
|
212
|
+
if (ps.length > 1) f.push(`[${marked.out}]split=${ps.length}${ps.map((_, i) => `[s${i}]`).join('')}`)
|
|
213
|
+
ps.forEach((p, i) => {
|
|
214
|
+
f.push(`[${vin(i)}]trim=start=${p.from}:end=${p.to},setpts=(PTS-STARTPTS)/${p.factor}[v${i}]`)
|
|
215
|
+
if (audio) f.push(`[0:a]atrim=start=${p.from}:end=${p.to},asetpts=PTS-STARTPTS${p.factor === 1 ? '' : `,${atempo(p.factor)}`}[a${i}]`)
|
|
216
|
+
})
|
|
217
|
+
const ins = ps.map((_, i) => (audio ? `[v${i}][a${i}]` : `[v${i}]`)).join('')
|
|
218
|
+
const crop = cropOn(e, src)
|
|
219
|
+
f.push(`${ins}concat=n=${ps.length}:v=1:a=${audio ? 1 : 0}[vc]${audio ? '[ac]' : ''}`)
|
|
220
|
+
f.push(`[vc]${crop ? `crop=${crop.w}:${crop.h}:${crop.x}:${crop.y},` : ''}format=yuv420p[vo]`)
|
|
221
|
+
return ['-i', src.file, ...overlays.flatMap((o) => ['-i', o]), '-filter_complex', f.join(';'), '-map', '[vo]', ...(audio ? ['-map', '[ac]', '-c:a', 'aac', '-b:a', '160k'] : ['-an']), ...codec, '-movflags', '+faststart', out]
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** ffmpeg's arguments for the export of a still: its marks, then its crop, to a png */
|
|
225
|
+
export function stillArgs(e: Edit, src: { file: string; width: number; height: number }, out: string, overlays: readonly string[] = []): string[] {
|
|
226
|
+
if (e.keep.length || e.speed.length || e.mute) throw new Error('a still takes crop and marks only')
|
|
227
|
+
const marked = markFilters(e, src, '0:v', overlays.map((_, k) => k + 1), { still: true })
|
|
228
|
+
const crop = cropOn(e, src)
|
|
229
|
+
const f = [...marked.filters, `[${marked.out}]${crop ? `crop=${crop.w}:${crop.h}:${crop.x}:${crop.y}` : 'null'}[vo]`]
|
|
230
|
+
return ['-i', src.file, ...overlays.flatMap((o) => ['-i', o]), '-filter_complex', f.join(';'), '-map', '[vo]', '-frames:v', '1', '-f', 'image2', '-c:v', 'png', out]
|
|
231
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// what the recorder app and the hub read of the machine: the presets (the house's, then each repo's own), and the
|
|
2
|
+
// folders a take can be filed under: unassigned first (the default), then every house repo. The app's pick lasts
|
|
3
|
+
// until it restarts, never longer
|
|
4
|
+
import { homedir } from 'node:os'
|
|
5
|
+
import { basename } from 'node:path'
|
|
6
|
+
import { UNASSIGNED } from './place.ts'
|
|
7
|
+
import { mergePresets, type NamedPreset } from './presets.ts'
|
|
8
|
+
|
|
9
|
+
export type RecorderState = { presets: NamedPreset[]; problems: string[]; repos: string[]; repo: string; home: string | null }
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
/** the house repos and every preset, read from their configs side by side */
|
|
13
|
+
export async function recorderState(cwd = process.cwd()): Promise<RecorderState> {
|
|
14
|
+
const { houseRoot } = await import('../home/home-start.ts')
|
|
15
|
+
const { loadRepoConfig } = await import('../repo-config.ts')
|
|
16
|
+
const { houseRepos } = await import('../serve/project-gutter.ts')
|
|
17
|
+
const { homeUrl } = await import('../home/home-paths.ts')
|
|
18
|
+
const house = houseRoot(cwd)
|
|
19
|
+
const repos = house ? houseRepos(house, homedir()).repos.map((r) => r.path) : []
|
|
20
|
+
const broken: string[] = []
|
|
21
|
+
// a repo whose config does not load keeps its presets out of the menu, said in the problems
|
|
22
|
+
const read = async (root: string) =>
|
|
23
|
+
(
|
|
24
|
+
await loadRepoConfig(root).catch((e: Error) => {
|
|
25
|
+
broken.push(`${basename(root)}: its repo.config.ts does not load, so its presets are left out: ${e.message}`)
|
|
26
|
+
return null
|
|
27
|
+
})
|
|
28
|
+
)?.recorder
|
|
29
|
+
const own = house ? (await loadRepoConfig(house)).recorder : undefined
|
|
30
|
+
const others = repos.filter((r) => r !== house)
|
|
31
|
+
const each = await Promise.all(others.map(read))
|
|
32
|
+
const merged = mergePresets(own, others.map((root, i) => ({ root, recorder: each[i] })))
|
|
33
|
+
const { presets } = merged
|
|
34
|
+
const problems = [...broken, ...merged.problems]
|
|
35
|
+
const names = [UNASSIGNED, ...repos.map((r) => basename(r)).sort()]
|
|
36
|
+
return { presets, problems, repos: names, repo: UNASSIGNED, home: house ? homeUrl() : null }
|
|
37
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// a mark drawn on a 2D canvas, the ONE drawing of every mark: the page draws its preview with it on a browser canvas,
|
|
2
|
+
// the export draws the PNG it burns in with it on @napi-rs/canvas, so what he sees is what ships. Blur is no drawing
|
|
3
|
+
// (ffmpeg blurs the box): here it is only a dashed frame, for the page
|
|
4
|
+
import { HEX, stepNumber, style, type Mark, type Pt } from './marks.ts'
|
|
5
|
+
|
|
6
|
+
/** what both canvases offer that the marks use */
|
|
7
|
+
export interface Ctx2D {
|
|
8
|
+
lineWidth: number
|
|
9
|
+
lineCap: string
|
|
10
|
+
lineJoin: string
|
|
11
|
+
strokeStyle: unknown
|
|
12
|
+
fillStyle: unknown
|
|
13
|
+
font: string
|
|
14
|
+
textBaseline: string
|
|
15
|
+
textAlign: string
|
|
16
|
+
beginPath(): void
|
|
17
|
+
moveTo(x: number, y: number): void
|
|
18
|
+
lineTo(x: number, y: number): void
|
|
19
|
+
closePath(): void
|
|
20
|
+
stroke(): void
|
|
21
|
+
fill(): void
|
|
22
|
+
arc(x: number, y: number, r: number, a: number, b: number): void
|
|
23
|
+
strokeRect(x: number, y: number, w: number, h: number): void
|
|
24
|
+
fillText(t: string, x: number, y: number): void
|
|
25
|
+
strokeText(t: string, x: number, y: number): void
|
|
26
|
+
setLineDash(d: number[]): void
|
|
27
|
+
save(): void
|
|
28
|
+
restore(): void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const FONT = '600 {px}px -apple-system, "Helvetica Neue", Helvetica, Arial, sans-serif'
|
|
32
|
+
|
|
33
|
+
/** the arrow's head: a filled triangle whose tip is the point, sized from the stroke */
|
|
34
|
+
function head(ctx: Ctx2D, from: Pt, to: Pt, stroke: number): Pt {
|
|
35
|
+
const a = Math.atan2(to.y - from.y, to.x - from.x)
|
|
36
|
+
const len = stroke * 4.5
|
|
37
|
+
const wide = stroke * 2.6
|
|
38
|
+
const back = { x: to.x - Math.cos(a) * len, y: to.y - Math.sin(a) * len }
|
|
39
|
+
ctx.beginPath()
|
|
40
|
+
ctx.moveTo(to.x, to.y)
|
|
41
|
+
ctx.lineTo(back.x + Math.sin(a) * wide, back.y - Math.cos(a) * wide)
|
|
42
|
+
ctx.lineTo(back.x - Math.sin(a) * wide, back.y + Math.cos(a) * wide)
|
|
43
|
+
ctx.closePath()
|
|
44
|
+
ctx.fill()
|
|
45
|
+
return back
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** the mark at `i` of `marks` in the take's pixels (a step counts the steps before it) */
|
|
49
|
+
export function drawMark(ctx: Ctx2D, marks: readonly Mark[], i: number, frame: { width: number; height: number }, o: { preview?: boolean } = {}): void {
|
|
50
|
+
const m = marks[i]!
|
|
51
|
+
const s = style(frame)
|
|
52
|
+
ctx.save()
|
|
53
|
+
ctx.lineCap = 'round'
|
|
54
|
+
ctx.lineJoin = 'round'
|
|
55
|
+
if (m.kind === 'blur') {
|
|
56
|
+
if (o.preview) {
|
|
57
|
+
ctx.setLineDash([s.stroke * 2, s.stroke * 2])
|
|
58
|
+
ctx.lineWidth = Math.max(2, s.stroke / 2)
|
|
59
|
+
ctx.strokeStyle = '#ffffff'
|
|
60
|
+
ctx.strokeRect(m.box.x, m.box.y, m.box.w, m.box.h)
|
|
61
|
+
}
|
|
62
|
+
ctx.restore()
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
const color = HEX[m.color]
|
|
66
|
+
// a dark edge under every mark: it reads on a white page and on a dark one
|
|
67
|
+
const edge = m.color === 'black' ? 'rgba(255,255,255,0.8)' : 'rgba(0,0,0,0.45)'
|
|
68
|
+
if (m.kind === 'arrow') {
|
|
69
|
+
for (const [w, c] of [[s.stroke + 2, edge], [s.stroke, color]] as const) {
|
|
70
|
+
ctx.strokeStyle = c
|
|
71
|
+
ctx.fillStyle = c
|
|
72
|
+
ctx.lineWidth = w
|
|
73
|
+
const back = head(ctx, m.from, m.to, s.stroke)
|
|
74
|
+
ctx.beginPath()
|
|
75
|
+
ctx.moveTo(m.from.x, m.from.y)
|
|
76
|
+
ctx.lineTo(back.x, back.y)
|
|
77
|
+
ctx.stroke()
|
|
78
|
+
}
|
|
79
|
+
} else if (m.kind === 'rect') {
|
|
80
|
+
for (const [w, c] of [[s.stroke + 2, edge], [s.stroke, color]] as const) {
|
|
81
|
+
ctx.strokeStyle = c
|
|
82
|
+
ctx.lineWidth = w
|
|
83
|
+
ctx.strokeRect(m.box.x, m.box.y, m.box.w, m.box.h)
|
|
84
|
+
}
|
|
85
|
+
} else if (m.kind === 'text') {
|
|
86
|
+
const size = m.size ?? s.text
|
|
87
|
+
ctx.font = FONT.replace('{px}', String(size))
|
|
88
|
+
ctx.textBaseline = 'top'
|
|
89
|
+
ctx.textAlign = 'left'
|
|
90
|
+
ctx.lineWidth = Math.max(3, size / 7)
|
|
91
|
+
ctx.strokeStyle = edge
|
|
92
|
+
ctx.strokeText(m.text, m.at.x, m.at.y)
|
|
93
|
+
ctx.fillStyle = color
|
|
94
|
+
ctx.fillText(m.text, m.at.x, m.at.y)
|
|
95
|
+
} else {
|
|
96
|
+
const n = String(stepNumber(marks, i))
|
|
97
|
+
ctx.fillStyle = color
|
|
98
|
+
ctx.beginPath()
|
|
99
|
+
ctx.arc(m.at.x, m.at.y, s.step, 0, Math.PI * 2)
|
|
100
|
+
ctx.fill()
|
|
101
|
+
ctx.lineWidth = Math.max(2, s.step / 8)
|
|
102
|
+
ctx.strokeStyle = '#ffffff'
|
|
103
|
+
ctx.stroke()
|
|
104
|
+
ctx.fillStyle = m.color === 'white' || m.color === 'yellow' ? '#111111' : '#ffffff'
|
|
105
|
+
ctx.font = FONT.replace('{px}', String(Math.round(s.step * 1.15)))
|
|
106
|
+
ctx.textBaseline = 'middle'
|
|
107
|
+
ctx.textAlign = 'center'
|
|
108
|
+
ctx.fillText(n, m.at.x, m.at.y + s.step * 0.06)
|
|
109
|
+
}
|
|
110
|
+
ctx.restore()
|
|
111
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// each drawn mark of an edit as a PNG the size of the frame, transparent but for the mark: the export overlays them
|
|
2
|
+
// in order, each inside its span. Drawn by marks-draw.ts, the same drawing the page previews
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import type { Edit } from './edit.ts'
|
|
5
|
+
import { canvasOf, drawnMarks } from './edit.ts'
|
|
6
|
+
import { drawMark, type Ctx2D } from './marks-draw.ts'
|
|
7
|
+
|
|
8
|
+
export async function markPngs(e: Edit, frame: { width: number; height: number }, dir: string): Promise<string[]> {
|
|
9
|
+
const idx = drawnMarks(e)
|
|
10
|
+
if (idx.length === 0) return []
|
|
11
|
+
const { createCanvas } = await import('@napi-rs/canvas')
|
|
12
|
+
const out: string[] = []
|
|
13
|
+
// each PNG is the whole canvas: the take's place on it moved by the canvas's own corner
|
|
14
|
+
const cv = canvasOf(e, frame)
|
|
15
|
+
for (const i of idx) {
|
|
16
|
+
const c = createCanvas(cv.w, cv.h)
|
|
17
|
+
const ctx = c.getContext('2d')
|
|
18
|
+
ctx.translate(-cv.x, -cv.y)
|
|
19
|
+
drawMark(ctx as unknown as Ctx2D, e.marks, i, frame)
|
|
20
|
+
const file = join(dir, `.mark-${i}.png`)
|
|
21
|
+
await Bun.write(file, await c.encode('png'))
|
|
22
|
+
out.push(file)
|
|
23
|
+
}
|
|
24
|
+
return out
|
|
25
|
+
}
|