@bigmistqke/whiteboard 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +609 -2
- package/bin/whiteboard.ts +431 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +471 -0
- package/dist/client/runtime.js +391 -0
- package/dist/define.js +7 -0
- package/dist/plugins/callout/README.md +13 -0
- package/dist/plugins/callout/index.js +27 -0
- package/dist/plugins/callout/style.css +12 -0
- package/dist/plugins/chat/README.md +45 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +46 -0
- package/dist/plugins/chat/style.css +17 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +64 -0
- package/dist/plugins/code/index.js +77 -0
- package/dist/plugins/code/style.css +29 -0
- package/dist/plugins/comments/client.js +10 -0
- package/dist/plugins/comments/index.js +51 -0
- package/dist/plugins/comments/style.css +32 -0
- package/dist/plugins/details/README.md +17 -0
- package/dist/plugins/details/index.js +17 -0
- package/dist/plugins/details/style.css +3 -0
- package/dist/plugins/form/README.md +111 -0
- package/dist/plugins/form/index.js +102 -0
- package/dist/plugins/form/style.css +41 -0
- package/dist/plugins/gallery/README.md +30 -0
- package/dist/plugins/gallery/client.js +1 -0
- package/dist/plugins/gallery/index.js +74 -0
- package/dist/plugins/gallery/style.css +27 -0
- package/dist/plugins/grid/README.md +33 -0
- package/dist/plugins/grid/index.js +33 -0
- package/dist/plugins/grid/style.css +25 -0
- package/dist/plugins/index.js +26 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +34 -0
- package/dist/plugins/mermaid/style.css +16 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +17 -0
- package/dist/plugins/terminal/client.js +41 -0
- package/dist/plugins/terminal/host.js +332 -0
- package/dist/plugins/terminal/index.js +171 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +57 -0
- package/dist/plugins/terminal/style.css +357 -0
- package/dist/solid/index.js +35 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +274 -0
- package/dist/src/client/page/main.js +1004 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +162 -0
- package/dist/src/html.js +34 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +120 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +49 -0
- package/dist/src/pages/index.js +14 -0
- package/dist/src/pages/markdown.js +307 -0
- package/dist/src/pages/render.js +257 -0
- package/dist/src/plugins.js +238 -0
- package/dist/src/running.js +24 -0
- package/dist/src/serve.js +71 -0
- package/dist/src/server.js +700 -0
- package/dist/src/status.js +43 -0
- package/package.json +131 -4
- package/plugins/callout/README.md +13 -0
- package/plugins/callout/index.ts +25 -0
- package/plugins/callout/style.css +12 -0
- package/plugins/chat/README.md +45 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +81 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +17 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +58 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +448 -0
- package/plugins/comments/index.ts +52 -0
- package/plugins/comments/style.css +32 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/form/README.md +111 -0
- package/plugins/form/index.ts +104 -0
- package/plugins/form/style.css +41 -0
- package/plugins/gallery/README.md +30 -0
- package/plugins/gallery/client.tsx +64 -0
- package/plugins/gallery/index.ts +67 -0
- package/plugins/gallery/style.css +27 -0
- package/plugins/grid/README.md +33 -0
- package/plugins/grid/index.ts +36 -0
- package/plugins/grid/style.css +25 -0
- package/plugins/index.ts +19 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +39 -0
- package/plugins/mermaid/style.css +16 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +27 -0
- package/plugins/terminal/client.ts +317 -0
- package/plugins/terminal/host.ts +326 -0
- package/plugins/terminal/index.ts +138 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/server.ts +215 -0
- package/plugins/terminal/sizes.ts +60 -0
- package/plugins/terminal/style.css +71 -0
- package/skills/whiteboard/SKILL.md +292 -0
- package/solid/index.tsx +56 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +186 -0
- package/src/client/page/base.css +274 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/main.ts +249 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +109 -0
- package/src/client/runtime.ts +170 -0
- package/src/config.ts +96 -0
- package/src/define.ts +546 -0
- package/src/events.ts +194 -0
- package/src/html.ts +40 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +109 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +67 -0
- package/src/pages/index.ts +36 -0
- package/src/pages/markdown.ts +276 -0
- package/src/pages/render.ts +196 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +644 -0
- package/src/status.ts +69 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
Content that is never marked as new. Changed blocks get a red bar until the
|
|
2
|
+
reader has seen them; what is inside `quiet` does not count as a change. Use
|
|
3
|
+
it for what changes without being news to the reader: repeating back what
|
|
4
|
+
they answered, a status line, a timestamp. Anything with substance (an
|
|
5
|
+
explanation, a question back, a different reading of their answer) belongs
|
|
6
|
+
outside it, so it is marked.
|
|
7
|
+
|
|
8
|
+
A whole block, a line, or a few words:
|
|
9
|
+
|
|
10
|
+
```markdown
|
|
11
|
+
:::quiet
|
|
12
|
+
Decided: **this week**, as you answered.
|
|
13
|
+
:::
|
|
14
|
+
|
|
15
|
+
::quiet[Last checked 12:04]
|
|
16
|
+
|
|
17
|
+
The build is green :quiet[(as of 12:04)].
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Inside a larger block, such as a card, only the quiet part is ignored: other
|
|
21
|
+
changes in the block still mark it. Hand-written HTML can do the same with a
|
|
22
|
+
`data-wb-quiet` attribute.
|
|
23
|
+
|
|
24
|
+
:::quiet
|
|
25
|
+
This block is quiet: editing it never marks it as new.
|
|
26
|
+
:::
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import { definePlugin, type Plugin } from '@bigmistqke/whiteboard'
|
|
3
|
+
// Content the new-content marking leaves alone: whatever is inside a quiet wrapper never marks its
|
|
4
|
+
// block as new, and a block that is quiet as a whole is never marked. For what changes without
|
|
5
|
+
// being news to the reader: an echo of their answer, a status line, a timestamp. The mechanism is
|
|
6
|
+
// the data-wb-quiet attribute, which any block or hand-written HTML can use as well.
|
|
7
|
+
import { html, raw, type DirectiveContext } from '@bigmistqke/whiteboard'
|
|
8
|
+
|
|
9
|
+
export async function render(ctx: DirectiveContext) {
|
|
10
|
+
// the label and content are HTML already
|
|
11
|
+
const label = ctx.label && raw(ctx.label)
|
|
12
|
+
if (ctx.kind === 'textDirective') return html`<span data-wb-quiet>${label}</span>`
|
|
13
|
+
if (ctx.kind === 'leafDirective') return html`<div data-wb-quiet>${label && html`<p>${label}</p>`}</div>`
|
|
14
|
+
return html`<div data-wb-quiet>${raw(await ctx.toHtml(ctx.children))}</div>`
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default function quiet(): Plugin {
|
|
18
|
+
return definePlugin({
|
|
19
|
+
name: 'quiet',
|
|
20
|
+
blocks: {
|
|
21
|
+
quiet: {
|
|
22
|
+
html: render,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
|
|
26
|
+
})
|
|
27
|
+
}
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
// The terminal drawer at the bottom of every page (for a browser with the project's token; see
|
|
2
|
+
// server.ts). The server writes its markup and sets its open state before the first render, so
|
|
3
|
+
// the browser can morph it from the page before (a view transition across the page load). This module
|
|
4
|
+
// connects it: an xterm that shows the session of the current tab, keystrokes and size sent back,
|
|
5
|
+
// and a tab per session, with "+" for a shell next to them.
|
|
6
|
+
//
|
|
7
|
+
// The sessions live on the server. Each page load connects again and first gets the whole screen of
|
|
8
|
+
// its tab, so switching pages loses nothing.
|
|
9
|
+
import { Terminal } from '@xterm/xterm'
|
|
10
|
+
import { FitAddon } from '@xterm/addon-fit'
|
|
11
|
+
|
|
12
|
+
const store = {
|
|
13
|
+
get: (k: string) => { try { return sessionStorage.getItem(k) } catch { return null } },
|
|
14
|
+
set: (k: string, v: string) => { try { sessionStorage.setItem(k, v) } catch { /* storage unavailable */ } },
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Session { id: string; label: string; running: boolean; exited: number | null }
|
|
18
|
+
|
|
19
|
+
function init(drawer: HTMLElement) {
|
|
20
|
+
const screen = drawer.querySelector<HTMLElement>('.wb-term-screen')!
|
|
21
|
+
const tabs = drawer.querySelector<HTMLElement>('.wb-term-tabs')!
|
|
22
|
+
const status = drawer.querySelector<HTMLElement>('.wb-term-status')!
|
|
23
|
+
const actions = drawer.querySelector<HTMLElement>('.wb-term-actions')!
|
|
24
|
+
const toggle = drawer.querySelector<HTMLButtonElement>('.wb-term-toggle')!
|
|
25
|
+
const grip = drawer.querySelector<HTMLElement>('.wb-term-grip')!
|
|
26
|
+
// the sessions as the server wrote them into the page, and the tab this browser tab shows
|
|
27
|
+
let sessions: Session[] = []
|
|
28
|
+
try { ({ sessions } = JSON.parse(drawer.dataset.sessions ?? '{}')); sessions ??= [] } catch { /* none */ }
|
|
29
|
+
let current: string | null = drawer.dataset.tab ?? sessions[0]?.id ?? null
|
|
30
|
+
|
|
31
|
+
const term = new Terminal({
|
|
32
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
33
|
+
fontSize: 13,
|
|
34
|
+
cursorBlink: true,
|
|
35
|
+
scrollback: 5000,
|
|
36
|
+
// xterm scales every wheel event smaller than 50 pixels down to 0.3 of its lines, and a trackpad
|
|
37
|
+
// sends almost only those: a swipe moved a program that handles the mouse itself (scrolling its
|
|
38
|
+
// own view) at a third of the speed of a native terminal. This undoes that factor.
|
|
39
|
+
scrollSensitivity: 3,
|
|
40
|
+
allowProposedApi: true,
|
|
41
|
+
theme: { background: '#0f1115', foreground: '#d7dae0', cursor: '#d7dae0', selectionBackground: '#3a4150' },
|
|
42
|
+
})
|
|
43
|
+
const fit = new FitAddon()
|
|
44
|
+
term.loadAddon(fit)
|
|
45
|
+
term.open(screen)
|
|
46
|
+
|
|
47
|
+
const isOpen = () => drawer.dataset.state === 'open'
|
|
48
|
+
// drop the ?terminal=open the auth link adds, now that the state is in this tab
|
|
49
|
+
if (new URLSearchParams(location.search).get('terminal') === 'open') {
|
|
50
|
+
store.set('wb-terminal-open', '1')
|
|
51
|
+
const url = new URL(location.href)
|
|
52
|
+
url.searchParams.delete('terminal')
|
|
53
|
+
history.replaceState(history.state, '', url)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ---- size ----------------------------------------------------------------------------------
|
|
57
|
+
// A session has one size for every drawer showing it (every browser window): the size of the
|
|
58
|
+
// drawer the reader used last, its active viewer (see host.ts). This drawer reports the size that
|
|
59
|
+
// fits it, claims the session when the reader uses it (opening it, focus, a keystroke), and
|
|
60
|
+
// otherwise shows the session at the session's size: with a margin, or cut off at the right.
|
|
61
|
+
const viewer = crypto.randomUUID()
|
|
62
|
+
// the session's active viewer and size, as the host last said
|
|
63
|
+
let active: string | null = null
|
|
64
|
+
let session: { cols: number; rows: number } | null = null
|
|
65
|
+
// this browser tab was the one in use: its next page claims the session again
|
|
66
|
+
let claim = store.get('wb-terminal-active') === '1'
|
|
67
|
+
let sent = ''
|
|
68
|
+
const mine = () => active === viewer
|
|
69
|
+
|
|
70
|
+
/** Tell the host the size that fits this drawer, and whether the reader uses it now. */
|
|
71
|
+
function refit() {
|
|
72
|
+
if (!isOpen() || !current) return
|
|
73
|
+
const dims = fit.proposeDimensions()
|
|
74
|
+
if (!dims?.cols || !dims?.rows) return
|
|
75
|
+
const key = `${current} ${dims.cols}x${dims.rows} ${claim}`
|
|
76
|
+
if (key !== sent) {
|
|
77
|
+
sent = key
|
|
78
|
+
post('/api/terminal/resize', { id: current, viewer, cols: dims.cols, rows: dims.rows, active: claim })
|
|
79
|
+
// it is ours from now on; the host confirms with a `size`
|
|
80
|
+
if (claim) { active = viewer; claim = false }
|
|
81
|
+
}
|
|
82
|
+
show()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The terminal at the size it has here: its own when this drawer is the active one, else the session's. */
|
|
86
|
+
function show() {
|
|
87
|
+
const dims = fit.proposeDimensions()
|
|
88
|
+
const size = mine() || !session ? dims : session
|
|
89
|
+
if (size?.cols && size?.rows && (size.cols !== term.cols || size.rows !== term.rows)) term.resize(size.cols, size.rows)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The reader uses this drawer: the session takes its size. */
|
|
93
|
+
function use() {
|
|
94
|
+
if (mine() || !current) return
|
|
95
|
+
claim = true
|
|
96
|
+
refit()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
new ResizeObserver(() => refit()).observe(screen)
|
|
100
|
+
term.textarea?.addEventListener('focus', use)
|
|
101
|
+
|
|
102
|
+
// ---- open and closed -----------------------------------------------------------------------
|
|
103
|
+
function setOpen(open: boolean) {
|
|
104
|
+
const apply = () => {
|
|
105
|
+
drawer.dataset.state = open ? 'open' : 'collapsed'
|
|
106
|
+
store.set('wb-terminal-open', open ? '1' : '0')
|
|
107
|
+
}
|
|
108
|
+
// the drawer morphs between its two states, as between pages
|
|
109
|
+
const vt = (document as any).startViewTransition
|
|
110
|
+
if (vt && !matchMedia('(prefers-reduced-motion: reduce)').matches) vt.call(document, apply)
|
|
111
|
+
else apply()
|
|
112
|
+
requestAnimationFrame(() => { if (open) use(); refit(); if (open) term.focus() })
|
|
113
|
+
}
|
|
114
|
+
toggle.addEventListener('click', () => setOpen(!isOpen()))
|
|
115
|
+
addEventListener('keydown', e => {
|
|
116
|
+
if (e.ctrlKey && e.key === '`') { e.preventDefault(); setOpen(!isOpen()) }
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
// ---- height --------------------------------------------------------------------------------
|
|
120
|
+
grip.addEventListener('pointerdown', e => {
|
|
121
|
+
e.preventDefault()
|
|
122
|
+
grip.setPointerCapture(e.pointerId)
|
|
123
|
+
if (!isOpen()) setOpen(true)
|
|
124
|
+
const bar = drawer.querySelector<HTMLElement>('.wb-term-bar')!.offsetHeight
|
|
125
|
+
const move = (ev: PointerEvent) => {
|
|
126
|
+
const h = Math.max(120, Math.min(innerHeight - 80, innerHeight - ev.clientY - bar))
|
|
127
|
+
document.documentElement.style.setProperty('--wb-term-h', `${Math.round(h)}px`)
|
|
128
|
+
}
|
|
129
|
+
const up = () => {
|
|
130
|
+
grip.removeEventListener('pointermove', move)
|
|
131
|
+
grip.removeEventListener('pointerup', up)
|
|
132
|
+
const h = getComputedStyle(document.documentElement).getPropertyValue('--wb-term-h')
|
|
133
|
+
store.set('wb-terminal-height', String(parseInt(h) || ''))
|
|
134
|
+
refit()
|
|
135
|
+
}
|
|
136
|
+
grip.addEventListener('pointermove', move)
|
|
137
|
+
grip.addEventListener('pointerup', up)
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
// ---- input ---------------------------------------------------------------------------------
|
|
141
|
+
// keystrokes in order, several typed at once in one request
|
|
142
|
+
let pending = ''
|
|
143
|
+
let chain = Promise.resolve()
|
|
144
|
+
term.onData(d => {
|
|
145
|
+
if (!current) return
|
|
146
|
+
use()
|
|
147
|
+
const id = current
|
|
148
|
+
if (!pending) queueMicrotask(() => {
|
|
149
|
+
const data = pending
|
|
150
|
+
pending = ''
|
|
151
|
+
chain = chain.then(() => post('/api/terminal/input', { id, data })).then(() => {}, () => {})
|
|
152
|
+
})
|
|
153
|
+
pending += d
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
// ---- the session ---------------------------------------------------------------------------
|
|
157
|
+
function setStatus(text: string, buttons: [string, () => void][] = []) {
|
|
158
|
+
status.textContent = text
|
|
159
|
+
actions.replaceChildren(...buttons.map(([label, run]) => {
|
|
160
|
+
const b = document.createElement('button')
|
|
161
|
+
b.type = 'button'
|
|
162
|
+
b.textContent = label
|
|
163
|
+
b.addEventListener('click', run)
|
|
164
|
+
return b
|
|
165
|
+
}))
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ---- tabs ----------------------------------------------------------------------------------
|
|
169
|
+
function drawTabs() {
|
|
170
|
+
tabs.replaceChildren(...sessions.map(x => {
|
|
171
|
+
const tab = document.createElement('span')
|
|
172
|
+
tab.className = 'wb-term-tab'
|
|
173
|
+
tab.dataset.id = x.id
|
|
174
|
+
tab.classList.toggle('wb-current', x.id === current)
|
|
175
|
+
tab.toggleAttribute('data-exited', !x.running)
|
|
176
|
+
const name = document.createElement('button')
|
|
177
|
+
name.type = 'button'
|
|
178
|
+
name.className = 'wb-term-tab-name'
|
|
179
|
+
name.textContent = x.label
|
|
180
|
+
name.title = x.running ? x.label : `${x.label}: exited (${x.exited})`
|
|
181
|
+
name.addEventListener('click', () => select(x.id))
|
|
182
|
+
tab.append(name)
|
|
183
|
+
const close = document.createElement('button')
|
|
184
|
+
close.type = 'button'
|
|
185
|
+
close.className = 'wb-term-tab-close'
|
|
186
|
+
close.title = 'Close this tab (ends its program)'
|
|
187
|
+
close.textContent = '×'
|
|
188
|
+
close.addEventListener('click', () => closeTab(x.id))
|
|
189
|
+
tab.append(close)
|
|
190
|
+
return tab
|
|
191
|
+
}))
|
|
192
|
+
showStatus()
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// "+" at the bar's right end, after the tabs, outside them so it stays in view when they scroll
|
|
196
|
+
const add = document.createElement('button')
|
|
197
|
+
add.type = 'button'
|
|
198
|
+
add.className = 'wb-term-add'
|
|
199
|
+
add.title = 'A new tab with a shell'
|
|
200
|
+
add.textContent = '+'
|
|
201
|
+
add.addEventListener('click', () => start({}))
|
|
202
|
+
tabs.after(add)
|
|
203
|
+
|
|
204
|
+
function showStatus() {
|
|
205
|
+
const x = sessions.find(y => y.id === current)
|
|
206
|
+
// no tab yet: "+" opens one
|
|
207
|
+
if (!x) return setStatus('', [])
|
|
208
|
+
if (x.running) return setStatus('', [])
|
|
209
|
+
setStatus(x.exited == null ? 'stopped' : `exited (${x.exited})`, [['Start again', () => start({ id: x.id, restart: true })]])
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function select(id: string | null) {
|
|
213
|
+
// the tab that shows already: just give it the keyboard back
|
|
214
|
+
if (id === current && stream) { if (isOpen()) term.focus(); return }
|
|
215
|
+
current = id
|
|
216
|
+
if (id) store.set('wb-terminal-tab', id)
|
|
217
|
+
drawTabs()
|
|
218
|
+
connect()
|
|
219
|
+
if (isOpen()) term.focus()
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async function closeTab(id: string) {
|
|
223
|
+
await post('/api/terminal/close', { id })
|
|
224
|
+
sessions = sessions.filter(x => x.id !== id)
|
|
225
|
+
if (current === id) select(sessions.at(-1)?.id ?? null)
|
|
226
|
+
else drawTabs()
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Open a tab with the shell, or start tab `id` again as it was. */
|
|
230
|
+
async function start(body: { id?: string; restart?: boolean }) {
|
|
231
|
+
setStatus('starting…')
|
|
232
|
+
const res = await post('/api/terminal/start', body)
|
|
233
|
+
const reply = await res.json().catch(() => ({}))
|
|
234
|
+
if (!res.ok) return setStatus(reply.error ?? 'could not start', [['Try again', () => start(body)]])
|
|
235
|
+
await loadSessions()
|
|
236
|
+
if (!isOpen()) setOpen(true)
|
|
237
|
+
select(reply.session)
|
|
238
|
+
term.focus()
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async function loadSessions() {
|
|
242
|
+
try {
|
|
243
|
+
const res = await fetch('/api/terminal/sessions')
|
|
244
|
+
if (res.ok) ({ sessions } = await res.json())
|
|
245
|
+
} catch { /* the server is away: the stream reconnects */ }
|
|
246
|
+
drawTabs()
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
let stream: EventSource | null = null
|
|
250
|
+
function connect() {
|
|
251
|
+
stream?.close()
|
|
252
|
+
term.reset()
|
|
253
|
+
sent = ''
|
|
254
|
+
if (!current) showStatus()
|
|
255
|
+
// without a tab too: the host tells it when one appears (started from the command line, say)
|
|
256
|
+
stream = new EventSource(`/api/terminal/stream?${current ? `id=${encodeURIComponent(current)}&` : ''}viewer=${viewer}`)
|
|
257
|
+
stream.addEventListener('sessions', e => {
|
|
258
|
+
sessions = JSON.parse((e as MessageEvent).data).sessions
|
|
259
|
+
// the current tab is gone, or there was none and one appeared: show the last
|
|
260
|
+
if (current ? !sessions.some(x => x.id === current) : sessions.length) return select(sessions.at(-1)?.id ?? null)
|
|
261
|
+
drawTabs()
|
|
262
|
+
})
|
|
263
|
+
stream.addEventListener('none', () => {
|
|
264
|
+
term.reset()
|
|
265
|
+
screen.querySelectorAll('.wb-term-placeholder').forEach(p => p.remove())
|
|
266
|
+
showStatus()
|
|
267
|
+
})
|
|
268
|
+
stream.addEventListener('screen', e => {
|
|
269
|
+
const { d, cols, rows, active: a } = JSON.parse((e as MessageEvent).data)
|
|
270
|
+
term.reset()
|
|
271
|
+
// the screen as the host has it, at the session's size; then the size this drawer shows it at
|
|
272
|
+
session = cols && rows ? { cols, rows } : null
|
|
273
|
+
active = a ?? null
|
|
274
|
+
if (session) term.resize(session.cols, session.rows)
|
|
275
|
+
term.write(d, () => {
|
|
276
|
+
refit()
|
|
277
|
+
// the live terminal takes over from the screens the server wrote into the page
|
|
278
|
+
screen.querySelectorAll('.wb-term-placeholder').forEach(p => p.remove())
|
|
279
|
+
})
|
|
280
|
+
showStatus()
|
|
281
|
+
if (isOpen() && store.get('wb-terminal-focus') === '1') term.focus()
|
|
282
|
+
})
|
|
283
|
+
stream.addEventListener('data', e => term.write(JSON.parse((e as MessageEvent).data).d))
|
|
284
|
+
// a program in this tab asked to show a page here (`whiteboard terminal open`): the drawer comes along
|
|
285
|
+
stream.addEventListener('navigate', e => {
|
|
286
|
+
const { path } = JSON.parse((e as MessageEvent).data)
|
|
287
|
+
if (typeof path === 'string' && path.startsWith('/') && !path.startsWith('//')) location.assign(path)
|
|
288
|
+
})
|
|
289
|
+
// the session's size changed, or another drawer became the active one
|
|
290
|
+
stream.addEventListener('size', e => {
|
|
291
|
+
const { cols, rows, active: a } = JSON.parse((e as MessageEvent).data)
|
|
292
|
+
session = { cols, rows }
|
|
293
|
+
active = a ?? null
|
|
294
|
+
store.set('wb-terminal-active', mine() ? '1' : '0')
|
|
295
|
+
show()
|
|
296
|
+
})
|
|
297
|
+
stream.addEventListener('exit', e => {
|
|
298
|
+
const code = JSON.parse((e as MessageEvent).data).code
|
|
299
|
+
const x = sessions.find(y => y.id === current)
|
|
300
|
+
if (x) { x.running = false; x.exited = code }
|
|
301
|
+
drawTabs()
|
|
302
|
+
})
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// whether the terminal had the keyboard, for the next page
|
|
306
|
+
addEventListener('pagehide', () => store.set('wb-terminal-focus', drawer.contains(document.activeElement) ? '1' : '0'))
|
|
307
|
+
|
|
308
|
+
drawTabs()
|
|
309
|
+
connect()
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function post(url: string, body: unknown) {
|
|
313
|
+
return fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const drawer = document.getElementById('wb-terminal')
|
|
317
|
+
if (drawer) init(drawer)
|