@bigmistqke/whiteboard 0.3.1 → 0.4.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 +67 -17
- package/bin/whiteboard.ts +268 -71
- package/dist/client/runtime.js +88 -41
- package/dist/define.js +21 -2
- package/dist/plugins/callout/index.js +3 -1
- package/dist/plugins/callout/public/style.css +30 -0
- package/dist/plugins/chat/index.js +7 -4
- package/dist/plugins/chat/{client.js → public/client.js} +1 -1
- package/dist/plugins/chat/public/style.css +98 -0
- package/dist/plugins/code/index.js +3 -1
- package/dist/plugins/code/public/style.css +68 -0
- package/dist/plugins/comments/index.js +4 -5
- package/dist/plugins/comments/public/client.js +19 -0
- package/dist/plugins/comments/public/style.css +185 -0
- package/dist/plugins/details/index.js +7 -2
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/form/index.js +37 -8
- package/dist/plugins/form/public/style.css +126 -0
- package/dist/plugins/gallery/index.js +4 -3
- package/dist/plugins/gallery/public/client.js +1 -0
- package/dist/plugins/gallery/public/style.css +90 -0
- package/dist/plugins/grid/index.js +9 -3
- package/dist/plugins/grid/public/style.css +71 -0
- package/dist/plugins/iframe/index.js +13 -14
- package/dist/plugins/iframe/public/style.css +13 -0
- package/dist/plugins/index.js +3 -1
- package/dist/plugins/mermaid/index.js +3 -1
- package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/dist/plugins/terminal/index.js +10 -9
- package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
- package/dist/plugins/toc/index.js +4 -3
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +93 -0
- package/dist/plugins/window/README.md +29 -0
- package/dist/plugins/window/index.js +64 -0
- package/dist/solid/index.js +14 -11
- package/dist/src/client/page/base.css +1078 -199
- package/dist/src/client/page/main.js +174 -33
- package/dist/src/loader.js +7 -2
- package/dist/src/package.js +3 -6
- package/dist/src/pages/blocks.js +8 -13
- package/dist/src/pages/markdown.js +1 -1
- package/dist/src/pages/render.js +74 -15
- package/dist/src/pages/window.js +30 -0
- package/dist/src/public.js +24 -0
- package/dist/src/server.js +36 -14
- package/package.json +3 -1
- package/plugins/callout/index.ts +3 -1
- package/plugins/callout/public/style.css +30 -0
- package/plugins/chat/autosize.ts +43 -9
- package/plugins/chat/client.tsx +22 -5
- package/plugins/chat/index.ts +22 -8
- package/plugins/chat/public/style.css +98 -0
- package/plugins/claude-code/index.ts +49 -14
- package/plugins/code/diff.ts +28 -8
- package/plugins/code/index.ts +30 -20
- package/plugins/code/public/style.css +68 -0
- package/plugins/comments/client.ts +139 -71
- package/plugins/comments/index.ts +9 -7
- package/plugins/comments/public/style.css +185 -0
- package/plugins/details/index.ts +7 -2
- package/plugins/details/public/style.css +7 -0
- package/plugins/forge/index.ts +16 -3
- package/plugins/form/index.ts +75 -27
- package/plugins/form/public/style.css +126 -0
- package/plugins/gallery/client.tsx +9 -3
- package/plugins/gallery/index.ts +31 -9
- package/plugins/gallery/public/style.css +90 -0
- package/plugins/grid/index.ts +19 -5
- package/plugins/grid/public/style.css +71 -0
- package/plugins/iframe/index.ts +20 -23
- package/plugins/iframe/public/style.css +13 -0
- package/plugins/index.ts +16 -1
- package/plugins/mermaid/index.ts +3 -1
- package/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/plugins/terminal/client.ts +105 -48
- package/plugins/terminal/host.ts +131 -29
- package/plugins/terminal/index.ts +84 -25
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +64 -13
- package/plugins/terminal/sizes.ts +13 -3
- package/plugins/toc/client.ts +45 -26
- package/plugins/toc/index.ts +9 -6
- package/plugins/toc/public/style.css +93 -0
- package/plugins/window/README.md +29 -0
- package/plugins/window/index.ts +85 -0
- package/skills/whiteboard/SKILL.md +3 -1
- package/solid/index.tsx +24 -9
- package/src/auth.ts +25 -7
- package/src/client/forms.ts +74 -28
- package/src/client/markdown.ts +15 -4
- package/src/client/page/base.css +1078 -199
- package/src/client/page/elsewhere.ts +32 -15
- package/src/client/page/fresh.ts +91 -23
- package/src/client/page/hold.ts +11 -2
- package/src/client/page/live.ts +10 -6
- package/src/client/page/main.ts +106 -36
- package/src/client/page/orphans.ts +25 -10
- package/src/client/page/sort.ts +14 -4
- package/src/client/page/sticky.ts +11 -3
- package/src/client/page/theme.ts +11 -7
- package/src/client/page/waiting.ts +141 -34
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +17 -10
- package/src/client/runtime.ts +130 -35
- package/src/client/scroll.ts +14 -4
- package/src/client/toast.ts +19 -5
- package/src/config.ts +20 -5
- package/src/define.ts +65 -25
- package/src/events.ts +111 -24
- package/src/html.ts +6 -2
- package/src/icons.ts +4 -2
- package/src/load.ts +10 -2
- package/src/loader.ts +75 -11
- package/src/package.ts +9 -8
- package/src/pages/blocks.ts +20 -14
- package/src/pages/markdown.ts +113 -34
- package/src/pages/render.ts +199 -39
- package/src/pages/window.ts +71 -0
- package/src/plugins.ts +90 -24
- package/src/public.ts +39 -0
- package/src/running.ts +8 -2
- package/src/serve.ts +29 -7
- package/src/server.ts +348 -107
- package/src/status.ts +15 -9
- package/dist/plugins/callout/style.css +0 -12
- package/dist/plugins/chat/style.css +0 -24
- package/dist/plugins/code/style.css +0 -29
- package/dist/plugins/comments/client.js +0 -10
- package/dist/plugins/comments/style.css +0 -40
- package/dist/plugins/details/style.css +0 -3
- package/dist/plugins/form/style.css +0 -44
- package/dist/plugins/gallery/client.js +0 -1
- package/dist/plugins/gallery/style.css +0 -27
- package/dist/plugins/grid/style.css +0 -25
- package/dist/plugins/iframe/client.js +0 -1
- package/dist/plugins/iframe/style.css +0 -29
- package/dist/plugins/toc/client.js +0 -1
- package/dist/plugins/toc/style.css +0 -19
- package/plugins/callout/style.css +0 -12
- package/plugins/chat/style.css +0 -24
- package/plugins/code/style.css +0 -29
- package/plugins/comments/style.css +0 -40
- package/plugins/details/style.css +0 -3
- package/plugins/form/style.css +0 -44
- package/plugins/gallery/style.css +0 -27
- package/plugins/grid/style.css +0 -25
- package/plugins/iframe/client.ts +0 -46
- package/plugins/iframe/style.css +0 -29
- package/plugins/terminal/style.css +0 -71
- package/plugins/toc/style.css +0 -19
- /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Windows (src/pages/window.ts): a header with a label, a button that fills the browser window, and
|
|
2
|
+
// one that opens what it shows on its own. `:::window[label] … :::` puts any markdown in one (a
|
|
3
|
+
// screenshot, a diagram, a table to look at large); `::image[label]{src}` and `::video[label]{src}`
|
|
4
|
+
// are one image or video in one. `bare` leaves the header out.
|
|
5
|
+
import fs from 'node:fs'
|
|
6
|
+
import { definePlugin, html, raw, windowFrame, type DirectiveContext, type Plugin } from '@bigmistqke/whiteboard'
|
|
7
|
+
|
|
8
|
+
interface Node {
|
|
9
|
+
type: string
|
|
10
|
+
url?: string
|
|
11
|
+
value?: string
|
|
12
|
+
children?: Node[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** What it shows on its own: the file of one image or video, its only content. */
|
|
16
|
+
export function openOf(children: Node[]): string | null {
|
|
17
|
+
const content = children.filter(n => !(n.type === 'text' && !n.value?.trim()))
|
|
18
|
+
if (content.length !== 1) return null
|
|
19
|
+
const only = content[0]
|
|
20
|
+
if (only.type === 'paragraph') return openOf(only.children ?? [])
|
|
21
|
+
if (only.type === 'image') return only.url ?? null
|
|
22
|
+
if (only.type === 'html') return /^\s*<(?:video|img|audio)\b[^>]*\bsrc="([^"]+)"/i.exec(only.value ?? '')?.[1] ?? null
|
|
23
|
+
return null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A height in px (a bare number) or a CSS length; else none. */
|
|
27
|
+
const height = (value: string | undefined) =>
|
|
28
|
+
value && /^\d+(\.\d+)?$/.test(value)
|
|
29
|
+
? `${value}px`
|
|
30
|
+
: value && /^\d+(\.\d+)?(px|em|rem|vh|svh|dvh)$/.test(value)
|
|
31
|
+
? value
|
|
32
|
+
: undefined
|
|
33
|
+
|
|
34
|
+
/** http(s), protocol-relative, or a path on this server; never `javascript:` or `data:`. */
|
|
35
|
+
const safeSrc = (src: string) => /^(https?:)?\/\//i.test(src) || !/^[a-z][a-z0-9+.-]*:/i.test(src)
|
|
36
|
+
|
|
37
|
+
/** ::image[label]{src} and ::video[label]{src loop autoplay muted poster}: one file, in a window. */
|
|
38
|
+
const media = (kind: 'image' | 'video') => (ctx: DirectiveContext) => {
|
|
39
|
+
const src = (ctx.attrs.src ?? '').trim()
|
|
40
|
+
if (!src)
|
|
41
|
+
return html`<pre class="wb-error">
|
|
42
|
+
${kind}: needs a src, like ::${kind}[a label]{src="${kind === 'image' ? 'shot.png' : 'demo.mp4'}"}</pre>`
|
|
43
|
+
if (!safeSrc(src))
|
|
44
|
+
return html`<pre class="wb-error">${kind}: src must be an http(s) URL or a path, not ${src.split(':')[0]}:</pre>`
|
|
45
|
+
const alt = ctx.labelText ?? ctx.attrs.alt ?? ''
|
|
46
|
+
const flag = (name: string) => name in ctx.attrs && raw(` ${name}`)
|
|
47
|
+
const poster = ctx.attrs.poster && safeSrc(ctx.attrs.poster) ? html` poster="${ctx.attrs.poster}"` : ''
|
|
48
|
+
const body =
|
|
49
|
+
kind === 'image'
|
|
50
|
+
? html`<img src="${src}" alt="${alt}" loading="lazy" />`
|
|
51
|
+
: html`<video
|
|
52
|
+
src="${src}"
|
|
53
|
+
controls${flag('loop')}${flag('autoplay')}${'autoplay' in ctx.attrs || 'muted' in ctx.attrs ? raw(' muted') : ''}
|
|
54
|
+
playsinline
|
|
55
|
+
preload="metadata"
|
|
56
|
+
${poster}${alt ? html` aria-label="${alt}"` : ''}
|
|
57
|
+
></video>`
|
|
58
|
+
return windowFrame({
|
|
59
|
+
label: ctx.label,
|
|
60
|
+
open: src,
|
|
61
|
+
wide: 'wide' in ctx.attrs,
|
|
62
|
+
bare: 'bare' in ctx.attrs,
|
|
63
|
+
height: height(ctx.attrs.height),
|
|
64
|
+
body,
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function render(ctx: DirectiveContext) {
|
|
69
|
+
return windowFrame({
|
|
70
|
+
label: ctx.label,
|
|
71
|
+
open: openOf(ctx.children as Node[]),
|
|
72
|
+
wide: 'wide' in ctx.attrs,
|
|
73
|
+
bare: 'bare' in ctx.attrs,
|
|
74
|
+
height: height(ctx.attrs.height),
|
|
75
|
+
body: await ctx.toHtml(ctx.children),
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export default function windows(): Plugin {
|
|
80
|
+
return definePlugin({
|
|
81
|
+
name: 'window',
|
|
82
|
+
blocks: { window: { html: render }, image: { html: media('image') }, video: { html: media('video') } },
|
|
83
|
+
docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
|
|
84
|
+
})
|
|
85
|
+
}
|
|
@@ -256,7 +256,9 @@ your wait runs doesn't change that, so answers given while none runs wait in
|
|
|
256
256
|
the log for you. So:
|
|
257
257
|
|
|
258
258
|
- An answer counts as picked up once your `wait` or `watch` hands it to you;
|
|
259
|
-
the form then folds into
|
|
259
|
+
the form then folds into a card showing the answer, as decided, with its
|
|
260
|
+
chat folded into "💬 n messages" (your messages there still open it for
|
|
261
|
+
the user). Answers no wait
|
|
260
262
|
has handed out show as waiting for you to pick them up. Waiting on a
|
|
261
263
|
decided form by name (`--type`) asks the user again: it is open again.
|
|
262
264
|
- Keep a `wait` or `watch` running for as long as you want answers, and only
|
package/solid/index.tsx
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// the api's events and theme as signals through context.
|
|
10
10
|
import { createContext, createSignal, onSettled, useContext, type Component } from 'solid-js'
|
|
11
11
|
import { render } from '@solidjs/web'
|
|
12
|
-
import type { ClientApi, Mount, Theme, WbEvent } from '@bigmistqke/whiteboard'
|
|
12
|
+
import type { ClientApi, FormStatus, Listener, Mount, Reactive, Theme, WbEvent } from '@bigmistqke/whiteboard'
|
|
13
13
|
|
|
14
14
|
export interface Whiteboard {
|
|
15
15
|
/** this page's id */
|
|
@@ -22,32 +22,47 @@ export interface Whiteboard {
|
|
|
22
22
|
append: ClientApi['append']
|
|
23
23
|
/** the theme in effect. Reactive. */
|
|
24
24
|
theme(): Theme
|
|
25
|
+
/** the agents waiting for answers on this page. Reactive. */
|
|
26
|
+
listeners(): Listener[]
|
|
27
|
+
/** the page's named forms and where each stands. Reactive. */
|
|
28
|
+
forms(): FormStatus[]
|
|
29
|
+
/** the agent's messages the reader hasn't read. Reactive. */
|
|
30
|
+
unread(): WbEvent[]
|
|
25
31
|
/** a message's markdown as HTML, rendered by the server (see Api.markdown) */
|
|
26
32
|
markdown: ClientApi['markdown']
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
const WhiteboardContext = createContext<Whiteboard>()
|
|
30
36
|
|
|
31
|
-
/** The page's events and
|
|
37
|
+
/** The page's events, theme, listeners, forms and unread messages, as signals. Use inside a component passed to solid(). */
|
|
32
38
|
export const useWhiteboard = (): Whiteboard => useContext(WhiteboardContext)
|
|
33
39
|
|
|
34
40
|
/** Turn a Solid component into a template client: `export default solid(MyComponent)`. */
|
|
35
41
|
export function solid<Props extends object>(Component: Component<Props>): Mount<Props> {
|
|
36
42
|
return (element: HTMLElement, props: Props, api: ClientApi) =>
|
|
37
43
|
render(() => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
// each of the api's changing values (Reactive) as a signal, set on each change; what .on
|
|
45
|
+
// returns stops it when the component goes
|
|
46
|
+
// (boxed: a T that is a function would read as createSignal's function form)
|
|
47
|
+
const signal = <T,>(value: Reactive<T>) => {
|
|
48
|
+
const [get, set] = createSignal({ v: value() })
|
|
49
|
+
onSettled(() => value.on(next => set({ v: next })))
|
|
50
|
+
return () => get().v
|
|
51
|
+
}
|
|
52
|
+
const events = signal(api.events)
|
|
53
|
+
const theme = signal(api.theme)
|
|
54
|
+
const listeners = signal(api.listeners)
|
|
55
|
+
const forms = signal(api.forms)
|
|
56
|
+
const unread = signal(api.unread)
|
|
45
57
|
const value: Whiteboard = {
|
|
46
58
|
page: api.page,
|
|
47
59
|
events,
|
|
48
60
|
last: type => events().findLast(e => e.type === type),
|
|
49
61
|
append: api.append,
|
|
50
62
|
theme,
|
|
63
|
+
listeners,
|
|
64
|
+
forms,
|
|
65
|
+
unread,
|
|
51
66
|
markdown: api.markdown,
|
|
52
67
|
}
|
|
53
68
|
return (
|
package/src/auth.ts
CHANGED
|
@@ -24,7 +24,9 @@ const COOKIE = 'wb-token'
|
|
|
24
24
|
/** The project's token, made on first use. */
|
|
25
25
|
export function projectToken(wb: string): string {
|
|
26
26
|
const file = path.join(wb, 'token')
|
|
27
|
-
try {
|
|
27
|
+
try {
|
|
28
|
+
return fs.readFileSync(file, 'utf8').trim()
|
|
29
|
+
} catch {}
|
|
28
30
|
const token = crypto.randomBytes(24).toString('base64url')
|
|
29
31
|
fs.writeFileSync(file, token + '\n', { mode: 0o600 })
|
|
30
32
|
return token
|
|
@@ -32,9 +34,15 @@ export function projectToken(wb: string): string {
|
|
|
32
34
|
|
|
33
35
|
export function createAuth(wb: string) {
|
|
34
36
|
const token = projectToken(wb)
|
|
35
|
-
const same = (a: unknown, b: string): boolean =>
|
|
37
|
+
const same = (a: unknown, b: string): boolean =>
|
|
38
|
+
typeof a === 'string' && a.length === b.length && crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b))
|
|
36
39
|
const cookies = (req: IncomingMessage): Record<string, string | undefined> =>
|
|
37
|
-
Object.fromEntries(
|
|
40
|
+
Object.fromEntries(
|
|
41
|
+
(req.headers.cookie ?? '')
|
|
42
|
+
.split(';')
|
|
43
|
+
.map(c => c.trim().split('='))
|
|
44
|
+
.filter(([k]) => k),
|
|
45
|
+
)
|
|
38
46
|
|
|
39
47
|
/** Has this request the token, from this origin? */
|
|
40
48
|
function authorized(req: IncomingMessage): boolean {
|
|
@@ -49,15 +57,25 @@ export function createAuth(wb: string) {
|
|
|
49
57
|
}
|
|
50
58
|
|
|
51
59
|
/** Opening a page as the tab's document: no frame, no fetch, and not an API route. */
|
|
52
|
-
const opensPage = (req: IncomingMessage) =>
|
|
53
|
-
|
|
60
|
+
const opensPage = (req: IncomingMessage) =>
|
|
61
|
+
req.method === 'GET' &&
|
|
62
|
+
req.headers['sec-fetch-mode'] === 'navigate' &&
|
|
63
|
+
req.headers['sec-fetch-dest'] === 'document' &&
|
|
64
|
+
!(req.url ?? '').startsWith('/api/')
|
|
54
65
|
|
|
55
66
|
/** The /_/auth route; false for any other path. */
|
|
56
67
|
function handle(req: IncomingMessage, res: ServerResponse, p: string, q: Record<string, unknown>): boolean {
|
|
57
68
|
if (p !== '/_/auth') return false
|
|
58
|
-
if (!same(q.token, token)) {
|
|
69
|
+
if (!same(q.token, token)) {
|
|
70
|
+
res.writeHead(403, { 'content-type': 'text/plain' })
|
|
71
|
+
res.end('wrong token')
|
|
72
|
+
return true
|
|
73
|
+
}
|
|
59
74
|
const to = typeof q.to === 'string' && q.to.startsWith('/') && !q.to.startsWith('//') ? q.to : '/'
|
|
60
|
-
res.writeHead(302, {
|
|
75
|
+
res.writeHead(302, {
|
|
76
|
+
'set-cookie': `${COOKIE}=${token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=31536000`,
|
|
77
|
+
location: to,
|
|
78
|
+
})
|
|
61
79
|
res.end()
|
|
62
80
|
return true
|
|
63
81
|
}
|
package/src/client/forms.ts
CHANGED
|
@@ -15,21 +15,22 @@
|
|
|
15
15
|
// data-wb-decided an agent picked up its latest answer; data-wb-outcome quotes it in one line
|
|
16
16
|
// and, for a few seconds after a send went through, `data-wb-just-sent`, to confirm it
|
|
17
17
|
//
|
|
18
|
-
// How that looks is the page's: it styles the attributes, and can follow `api.
|
|
18
|
+
// How that looks is the page's: it styles the attributes, and can follow `api.forms.on`. A form
|
|
19
19
|
// marked `data-wb-self` (or inside an element that is) keeps its own fields: no drafts and no last
|
|
20
20
|
// answer are written into it, for a block's component that shows them itself.
|
|
21
21
|
import type { Api, FormState, FormStatus } from '../define'
|
|
22
22
|
import { STATES, isQuestionName, questionState } from '../status.ts'
|
|
23
|
+
import { reactive } from './reactive'
|
|
23
24
|
|
|
24
25
|
type Data = Record<string, unknown>
|
|
25
26
|
|
|
26
27
|
let root: HTMLElement
|
|
27
28
|
let api: Api
|
|
28
29
|
let page = ''
|
|
29
|
-
const listeners = new Set<(forms: FormStatus[]) => void>()
|
|
30
30
|
|
|
31
31
|
/** The forms the core looks after: named (a name without a colon: see isQuestionName), without an action. */
|
|
32
|
-
const managed = (form: HTMLFormElement) =>
|
|
32
|
+
const managed = (form: HTMLFormElement) =>
|
|
33
|
+
isQuestionName(form.getAttribute('name') ?? '') && !form.hasAttribute('action')
|
|
33
34
|
const forms = () => [...root.querySelectorAll('form')].filter(managed)
|
|
34
35
|
const keepsOwnFields = (form: HTMLFormElement) => !!form.closest('[data-wb-self]')
|
|
35
36
|
|
|
@@ -39,7 +40,8 @@ const keepsOwnFields = (form: HTMLFormElement) => !!form.closest('[data-wb-self]
|
|
|
39
40
|
// marked `data-wb-other`, with a text box (`data-wb-other-text`, no name of its own) in its label.
|
|
40
41
|
// It sends what was typed; typing picks it; an answer that is none of the other options goes back
|
|
41
42
|
// into its box.
|
|
42
|
-
const otherText = (option: HTMLInputElement) =>
|
|
43
|
+
const otherText = (option: HTMLInputElement) =>
|
|
44
|
+
option.closest('label')?.querySelector<HTMLInputElement>('[data-wb-other-text]') ?? null
|
|
43
45
|
const isOther = (el: Element): boolean => el instanceof HTMLInputElement && el.hasAttribute('data-wb-other')
|
|
44
46
|
|
|
45
47
|
function readForm(form: HTMLFormElement): Data {
|
|
@@ -49,12 +51,16 @@ function readForm(form: HTMLFormElement): Data {
|
|
|
49
51
|
for (const el of elements) if (el.name && el.type === 'checkbox') boxes[el.name] = (boxes[el.name] ?? 0) + 1
|
|
50
52
|
for (const el of elements) {
|
|
51
53
|
if (!el.name || el.type === 'submit' || el.type === 'button' || el.type === 'reset' || el.type === 'file') continue
|
|
52
|
-
|
|
54
|
+
// an other-option sends what was typed in its box: read, not written into the option (for a
|
|
55
|
+
// radio or checkbox that would change its value attribute, and so the page's HTML)
|
|
56
|
+
const value = isOther(el) ? (otherText(el)?.value.trim() ?? '') : el.value
|
|
53
57
|
if (el.type === 'checkbox') {
|
|
54
|
-
if (boxes[el.name] > 1) {
|
|
55
|
-
|
|
58
|
+
if (boxes[el.name] > 1) {
|
|
59
|
+
const list = (data[el.name] ??= []) as string[]
|
|
60
|
+
if (el.checked) list.push(value)
|
|
61
|
+
} else data[el.name] = el.checked
|
|
56
62
|
} else if (el.type === 'radio') {
|
|
57
|
-
if (el.checked) data[el.name] =
|
|
63
|
+
if (el.checked) data[el.name] = value
|
|
58
64
|
else data[el.name] ??= null
|
|
59
65
|
} else if ((el as unknown) instanceof HTMLSelectElement && (el as unknown as HTMLSelectElement).multiple) {
|
|
60
66
|
data[el.name] = [...(el as unknown as HTMLSelectElement).selectedOptions].map(o => o.value)
|
|
@@ -66,13 +72,18 @@ function readForm(form: HTMLFormElement): Data {
|
|
|
66
72
|
function writeForm(form: HTMLFormElement, data: Data) {
|
|
67
73
|
const elements = [...form.elements] as HTMLInputElement[]
|
|
68
74
|
// what the listed options of a name send, so an answer that is none of them is the other one's
|
|
69
|
-
const listed = (name: string) =>
|
|
75
|
+
const listed = (name: string) =>
|
|
76
|
+
elements
|
|
77
|
+
.filter(e => e.name === name && !isOther(e) && (e.type === 'radio' || e.type === 'checkbox'))
|
|
78
|
+
.map(e => e.value)
|
|
70
79
|
for (const el of elements) {
|
|
71
80
|
if (!el.name || !(el.name in data) || ['submit', 'button', 'hidden', 'file'].includes(el.type)) continue
|
|
72
81
|
const v = data[el.name]
|
|
73
82
|
if (isOther(el)) {
|
|
74
83
|
const own = listed(el.name)
|
|
75
|
-
const rest = (Array.isArray(v) ? v : [v]).filter(
|
|
84
|
+
const rest = (Array.isArray(v) ? v : [v]).filter(
|
|
85
|
+
x => typeof x === 'string' && x !== '' && !own.includes(x),
|
|
86
|
+
) as string[]
|
|
76
87
|
el.checked = rest.length > 0
|
|
77
88
|
const text = otherText(el)
|
|
78
89
|
if (text && text !== document.activeElement) text.value = rest.join(', ')
|
|
@@ -101,7 +112,11 @@ const draftKey = (form: HTMLFormElement) => `wb-draft:${page}:${form.getAttribut
|
|
|
101
112
|
function saveDraft(e: Event) {
|
|
102
113
|
const form = (e.target as Element | null)?.closest?.('form')
|
|
103
114
|
if (!form || !managed(form) || !root.contains(form) || keepsOwnFields(form)) return
|
|
104
|
-
try {
|
|
115
|
+
try {
|
|
116
|
+
localStorage.setItem(draftKey(form), JSON.stringify(readForm(form)))
|
|
117
|
+
} catch {
|
|
118
|
+
/* storage unavailable */
|
|
119
|
+
}
|
|
105
120
|
}
|
|
106
121
|
|
|
107
122
|
/** Unsent drafts back into their forms; a form being typed in is left alone. */
|
|
@@ -109,12 +124,22 @@ function restoreDrafts() {
|
|
|
109
124
|
for (const form of forms()) {
|
|
110
125
|
if (keepsOwnFields(form) || form.contains(document.activeElement)) continue
|
|
111
126
|
let draft: Data | null = null
|
|
112
|
-
try {
|
|
127
|
+
try {
|
|
128
|
+
draft = JSON.parse(localStorage.getItem(draftKey(form)) ?? 'null')
|
|
129
|
+
} catch {
|
|
130
|
+
/* storage unavailable */
|
|
131
|
+
}
|
|
113
132
|
if (draft) writeForm(form, draft)
|
|
114
133
|
}
|
|
115
134
|
}
|
|
116
135
|
|
|
117
|
-
const dropDraft = (form: HTMLFormElement) => {
|
|
136
|
+
const dropDraft = (form: HTMLFormElement) => {
|
|
137
|
+
try {
|
|
138
|
+
localStorage.removeItem(draftKey(form))
|
|
139
|
+
} catch {
|
|
140
|
+
/* storage unavailable */
|
|
141
|
+
}
|
|
142
|
+
}
|
|
118
143
|
|
|
119
144
|
/** Each form's last answer: its button in data-wb-sent, and with `prefill` its fields. */
|
|
120
145
|
function restoreLast(prefill: boolean) {
|
|
@@ -142,7 +167,10 @@ async function onSubmit(e: SubmitEvent) {
|
|
|
142
167
|
// folds because an agent picked the answer up at once
|
|
143
168
|
form.toggleAttribute('data-wb-just-sent', true)
|
|
144
169
|
clearTimeout(justSent.get(form))
|
|
145
|
-
justSent.set(
|
|
170
|
+
justSent.set(
|
|
171
|
+
form,
|
|
172
|
+
setTimeout(() => form.removeAttribute('data-wb-just-sent'), 3000),
|
|
173
|
+
)
|
|
146
174
|
} catch (err) {
|
|
147
175
|
form.dataset.wbState = 'failed'
|
|
148
176
|
console.error(err)
|
|
@@ -154,17 +182,34 @@ async function onSubmit(e: SubmitEvent) {
|
|
|
154
182
|
const ATTR: Record<FormState, string> = { open: 'data-wb-open', pending: 'data-wb-pending', decided: 'data-wb-decided' }
|
|
155
183
|
|
|
156
184
|
/** One line quoting an answer: what was chosen, then what was written. */
|
|
157
|
-
|
|
158
|
-
|
|
185
|
+
/**
|
|
186
|
+
* An answer, told apart: the button it was sent with (none for a plain Send), what was chosen
|
|
187
|
+
* (options, short values), and what was written.
|
|
188
|
+
*/
|
|
189
|
+
export function answerParts(
|
|
190
|
+
data: Data,
|
|
191
|
+
action: string | null,
|
|
192
|
+
): { action: string | null; chosen: string[]; written: string[] } {
|
|
193
|
+
const chosen: string[] = [],
|
|
194
|
+
written: string[] = []
|
|
159
195
|
for (const v of Object.values(data)) {
|
|
160
196
|
if (v == null || v === '' || v === false) continue
|
|
161
|
-
if (Array.isArray(v)) {
|
|
162
|
-
|
|
197
|
+
if (Array.isArray(v)) {
|
|
198
|
+
if (v.length) chosen.push(v.join(', '))
|
|
199
|
+
} else if (v === true) continue
|
|
163
200
|
else if (String(v).length > 60 || String(v).includes('\n')) written.push(String(v))
|
|
164
201
|
else chosen.push(String(v))
|
|
165
202
|
}
|
|
166
|
-
|
|
167
|
-
|
|
203
|
+
return { action: action && !['Send', 'Submit'].includes(action) ? action : null, chosen, written }
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** One line quoting an answer: what was chosen, then what was written. */
|
|
207
|
+
function outcome(data: Data, action: string | null) {
|
|
208
|
+
const { action: button, chosen, written } = answerParts(data, action)
|
|
209
|
+
return [
|
|
210
|
+
[button, ...chosen].filter(Boolean).join(' · ') || 'sent',
|
|
211
|
+
...written.map(w => `“${w.length > 240 ? w.slice(0, 240) + '…' : w}”`),
|
|
212
|
+
].join('\n')
|
|
168
213
|
}
|
|
169
214
|
|
|
170
215
|
let current: FormStatus[] = []
|
|
@@ -181,7 +226,7 @@ function mark() {
|
|
|
181
226
|
else delete form.dataset.wbOutcome
|
|
182
227
|
return { form, name, state, last }
|
|
183
228
|
})
|
|
184
|
-
|
|
229
|
+
forms_.notify()
|
|
185
230
|
}
|
|
186
231
|
|
|
187
232
|
/** After the page re-rendered: answers, drafts and marks back on its forms. */
|
|
@@ -202,15 +247,16 @@ export function trackForms(el: HTMLElement, pageId: string, core: Api) {
|
|
|
202
247
|
document.addEventListener('change', saveDraft)
|
|
203
248
|
document.addEventListener('submit', onSubmit)
|
|
204
249
|
api.onRendered(refresh)
|
|
205
|
-
api.
|
|
206
|
-
api.
|
|
250
|
+
api.listeners.on(mark)
|
|
251
|
+
api.events.on(() => {
|
|
252
|
+
restoreLast(false)
|
|
253
|
+
mark()
|
|
254
|
+
})
|
|
207
255
|
restoreLast(true)
|
|
208
256
|
restoreDrafts()
|
|
209
257
|
mark()
|
|
210
258
|
}
|
|
211
259
|
|
|
212
|
-
/** For the api: every named form and where it stands,
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
onForms(fn: (forms: FormStatus[]) => void) { listeners.add(fn); return () => { listeners.delete(fn) } },
|
|
216
|
-
}
|
|
260
|
+
/** For the api: every named form and where it stands, as it changes. */
|
|
261
|
+
const forms_ = reactive((): FormStatus[] => current)
|
|
262
|
+
export const formsApi = { forms: forms_ }
|
package/src/client/markdown.ts
CHANGED
|
@@ -10,16 +10,27 @@ async function flush() {
|
|
|
10
10
|
const batch = queue
|
|
11
11
|
queue = []
|
|
12
12
|
try {
|
|
13
|
-
const res = await fetch('/api/markdown', {
|
|
13
|
+
const res = await fetch('/api/markdown', {
|
|
14
|
+
method: 'POST',
|
|
15
|
+
headers: { 'content-type': 'application/json' },
|
|
16
|
+
body: JSON.stringify({ texts: batch.map(b => b.text) }),
|
|
17
|
+
})
|
|
14
18
|
if (!res.ok) throw new Error(await res.text())
|
|
15
|
-
const { html, styles } = await res.json() as { html: string[]; styles: string[] }
|
|
19
|
+
const { html, styles } = (await res.json()) as { html: string[]; styles: string[] }
|
|
16
20
|
for (const href of styles) {
|
|
17
|
-
if (
|
|
21
|
+
if (
|
|
22
|
+
![...document.querySelectorAll<HTMLLinkElement>('link[rel=stylesheet]')].some(
|
|
23
|
+
l => new URL(l.href).pathname === new URL(href, location.href).pathname,
|
|
24
|
+
)
|
|
25
|
+
)
|
|
18
26
|
document.head.insertAdjacentHTML('beforeend', `<link rel="stylesheet" href="${href.replace(/"/g, '"')}">`)
|
|
19
27
|
}
|
|
20
28
|
batch.forEach((b, i) => b.resolve(html[i] ?? ''))
|
|
21
29
|
} catch (err) {
|
|
22
|
-
for (const b of batch) {
|
|
30
|
+
for (const b of batch) {
|
|
31
|
+
cache.delete(b.text)
|
|
32
|
+
b.reject(err)
|
|
33
|
+
}
|
|
23
34
|
}
|
|
24
35
|
}
|
|
25
36
|
|