@bigmistqke/workpad 0.0.0-stage → 0.18.1
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 +781 -2
- package/bin/workpad.ts +1526 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1202 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2121 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +213 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +348 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +761 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +592 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +134 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +166 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +240 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +435 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +845 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
|
@@ -0,0 +1,391 @@
|
|
|
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
|
+
import type { PluginMount } from '../../src/define.ts'
|
|
12
|
+
|
|
13
|
+
const store = {
|
|
14
|
+
get: (k: string) => {
|
|
15
|
+
try {
|
|
16
|
+
return sessionStorage.getItem(k)
|
|
17
|
+
} catch {
|
|
18
|
+
return null
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
set: (k: string, v: string) => {
|
|
22
|
+
try {
|
|
23
|
+
sessionStorage.setItem(k, v)
|
|
24
|
+
} catch {
|
|
25
|
+
/* storage unavailable */
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface Session {
|
|
31
|
+
id: string
|
|
32
|
+
label: string
|
|
33
|
+
running: boolean
|
|
34
|
+
exited: number | null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// the path the project is served under (a shared server serves it below one: see Entries)
|
|
38
|
+
const BASE: string = (() => {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(document.getElementById('wp-page')?.textContent ?? '{}').base ?? ''
|
|
41
|
+
} catch {
|
|
42
|
+
return ''
|
|
43
|
+
}
|
|
44
|
+
})()
|
|
45
|
+
|
|
46
|
+
function init(drawer: HTMLElement) {
|
|
47
|
+
const screen = drawer.querySelector<HTMLElement>('.wp-term-screen')!
|
|
48
|
+
const tabs = drawer.querySelector<HTMLElement>('.wp-term-tabs')!
|
|
49
|
+
const status = drawer.querySelector<HTMLElement>('.wp-term-status')!
|
|
50
|
+
const actions = drawer.querySelector<HTMLElement>('.wp-term-actions')!
|
|
51
|
+
const toggle = drawer.querySelector<HTMLButtonElement>('.wp-term-toggle')!
|
|
52
|
+
const grip = drawer.querySelector<HTMLElement>('.wp-term-grip')!
|
|
53
|
+
// the sessions as the server wrote them into the page, and the tab this browser tab shows
|
|
54
|
+
let sessions: Session[] = []
|
|
55
|
+
try {
|
|
56
|
+
;({ sessions } = JSON.parse(drawer.dataset.sessions ?? '{}'))
|
|
57
|
+
sessions ??= []
|
|
58
|
+
} catch {
|
|
59
|
+
/* none */
|
|
60
|
+
}
|
|
61
|
+
let current: string | null = drawer.dataset.tab ?? sessions[0]?.id ?? null
|
|
62
|
+
|
|
63
|
+
const term = new Terminal({
|
|
64
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
65
|
+
fontSize: 13,
|
|
66
|
+
cursorBlink: true,
|
|
67
|
+
scrollback: 5000,
|
|
68
|
+
// xterm scales every wheel event smaller than 50 pixels down to 0.3 of its lines, and a trackpad
|
|
69
|
+
// sends almost only those: a swipe moved a program that handles the mouse itself (scrolling its
|
|
70
|
+
// own view) at a third of the speed of a native terminal. This undoes that factor.
|
|
71
|
+
scrollSensitivity: 3,
|
|
72
|
+
allowProposedApi: true,
|
|
73
|
+
theme: { background: '#0f1115', foreground: '#d7dae0', cursor: '#d7dae0', selectionBackground: '#3a4150' },
|
|
74
|
+
})
|
|
75
|
+
const fit = new FitAddon()
|
|
76
|
+
term.loadAddon(fit)
|
|
77
|
+
term.open(screen)
|
|
78
|
+
|
|
79
|
+
const isOpen = () => drawer.dataset.state === 'open'
|
|
80
|
+
// drop the ?terminal=open the auth link adds, now that the state is in this tab
|
|
81
|
+
if (new URLSearchParams(location.search).get('terminal') === 'open') {
|
|
82
|
+
store.set('wp-terminal-open', '1')
|
|
83
|
+
const url = new URL(location.href)
|
|
84
|
+
url.searchParams.delete('terminal')
|
|
85
|
+
history.replaceState(history.state, '', url)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ---- size ----------------------------------------------------------------------------------
|
|
89
|
+
// A session has one size for every drawer showing it (every browser window): the size of the
|
|
90
|
+
// drawer the reader used last, its active viewer (see host.ts). This drawer reports the size that
|
|
91
|
+
// fits it, claims the session when the reader uses it (opening it, focus, a keystroke), and
|
|
92
|
+
// otherwise shows the session at the session's size: with a margin, or cut off at the right.
|
|
93
|
+
const viewer = crypto.randomUUID()
|
|
94
|
+
// the session's active viewer and size, as the host last said
|
|
95
|
+
let active: string | null = null
|
|
96
|
+
let session: { cols: number; rows: number } | null = null
|
|
97
|
+
// this browser tab was the one in use: its next page claims the session again
|
|
98
|
+
let claim = store.get('wp-terminal-active') === '1'
|
|
99
|
+
let sent = ''
|
|
100
|
+
const mine = () => active === viewer
|
|
101
|
+
|
|
102
|
+
/** Tell the host the size that fits this drawer, and whether the reader uses it now. */
|
|
103
|
+
function refit() {
|
|
104
|
+
if (!isOpen() || !current) return
|
|
105
|
+
const dims = fit.proposeDimensions()
|
|
106
|
+
if (!dims?.cols || !dims?.rows) return
|
|
107
|
+
const key = `${current} ${dims.cols}x${dims.rows} ${claim}`
|
|
108
|
+
if (key !== sent) {
|
|
109
|
+
sent = key
|
|
110
|
+
post(BASE + '/api/terminal/resize', { id: current, viewer, cols: dims.cols, rows: dims.rows, active: claim })
|
|
111
|
+
// it is ours from now on; the host confirms with a `size`
|
|
112
|
+
if (claim) {
|
|
113
|
+
active = viewer
|
|
114
|
+
claim = false
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
show()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The terminal at the size it has here: its own when this drawer is the active one, else the session's. */
|
|
121
|
+
function show() {
|
|
122
|
+
const dims = fit.proposeDimensions()
|
|
123
|
+
const size = mine() || !session ? dims : session
|
|
124
|
+
if (size?.cols && size?.rows && (size.cols !== term.cols || size.rows !== term.rows))
|
|
125
|
+
term.resize(size.cols, size.rows)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The reader uses this drawer: the session takes its size. */
|
|
129
|
+
function use() {
|
|
130
|
+
if (mine() || !current) return
|
|
131
|
+
claim = true
|
|
132
|
+
refit()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
new ResizeObserver(() => refit()).observe(screen)
|
|
136
|
+
term.textarea?.addEventListener('focus', use)
|
|
137
|
+
|
|
138
|
+
// ---- open and closed -----------------------------------------------------------------------
|
|
139
|
+
function setOpen(open: boolean) {
|
|
140
|
+
const apply = () => {
|
|
141
|
+
drawer.dataset.state = open ? 'open' : 'collapsed'
|
|
142
|
+
store.set('wp-terminal-open', open ? '1' : '0')
|
|
143
|
+
}
|
|
144
|
+
// the drawer morphs between its two states, as between pages
|
|
145
|
+
const vt = (document as any).startViewTransition
|
|
146
|
+
if (vt && !matchMedia('(prefers-reduced-motion: reduce)').matches) vt.call(document, apply)
|
|
147
|
+
else apply()
|
|
148
|
+
requestAnimationFrame(() => {
|
|
149
|
+
if (open) use()
|
|
150
|
+
refit()
|
|
151
|
+
if (open) term.focus()
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
toggle.addEventListener('click', () => setOpen(!isOpen()))
|
|
155
|
+
addEventListener('keydown', e => {
|
|
156
|
+
if (e.ctrlKey && e.key === '`') {
|
|
157
|
+
e.preventDefault()
|
|
158
|
+
setOpen(!isOpen())
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
// ---- height --------------------------------------------------------------------------------
|
|
163
|
+
grip.addEventListener('pointerdown', e => {
|
|
164
|
+
e.preventDefault()
|
|
165
|
+
grip.setPointerCapture(e.pointerId)
|
|
166
|
+
if (!isOpen()) setOpen(true)
|
|
167
|
+
const bar = drawer.querySelector<HTMLElement>('.wp-term-bar')!.offsetHeight
|
|
168
|
+
const move = (ev: PointerEvent) => {
|
|
169
|
+
const h = Math.max(120, Math.min(innerHeight - 80, innerHeight - ev.clientY - bar))
|
|
170
|
+
document.documentElement.style.setProperty('--wp-term-h', `${Math.round(h)}px`)
|
|
171
|
+
}
|
|
172
|
+
const up = () => {
|
|
173
|
+
grip.removeEventListener('pointermove', move)
|
|
174
|
+
grip.removeEventListener('pointerup', up)
|
|
175
|
+
const h = getComputedStyle(document.documentElement).getPropertyValue('--wp-term-h')
|
|
176
|
+
store.set('wp-terminal-height', String(parseInt(h) || ''))
|
|
177
|
+
refit()
|
|
178
|
+
}
|
|
179
|
+
grip.addEventListener('pointermove', move)
|
|
180
|
+
grip.addEventListener('pointerup', up)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
// ---- input ---------------------------------------------------------------------------------
|
|
184
|
+
// keystrokes in order, several typed at once in one request
|
|
185
|
+
let pending = ''
|
|
186
|
+
let chain = Promise.resolve()
|
|
187
|
+
term.onData(d => {
|
|
188
|
+
if (!current) return
|
|
189
|
+
use()
|
|
190
|
+
const id = current
|
|
191
|
+
if (!pending)
|
|
192
|
+
queueMicrotask(() => {
|
|
193
|
+
const data = pending
|
|
194
|
+
pending = ''
|
|
195
|
+
chain = chain
|
|
196
|
+
.then(() => post(BASE + '/api/terminal/input', { id, data }))
|
|
197
|
+
.then(
|
|
198
|
+
() => {},
|
|
199
|
+
() => {},
|
|
200
|
+
)
|
|
201
|
+
})
|
|
202
|
+
pending += d
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
// ---- the session ---------------------------------------------------------------------------
|
|
206
|
+
function setStatus(text: string, buttons: [string, () => void][] = []) {
|
|
207
|
+
status.textContent = text
|
|
208
|
+
actions.replaceChildren(
|
|
209
|
+
...buttons.map(([label, run]) => {
|
|
210
|
+
const b = document.createElement('button')
|
|
211
|
+
b.type = 'button'
|
|
212
|
+
b.textContent = label
|
|
213
|
+
b.addEventListener('click', run)
|
|
214
|
+
return b
|
|
215
|
+
}),
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ---- tabs ----------------------------------------------------------------------------------
|
|
220
|
+
function drawTabs() {
|
|
221
|
+
tabs.replaceChildren(
|
|
222
|
+
...sessions.map(x => {
|
|
223
|
+
const tab = document.createElement('span')
|
|
224
|
+
tab.className = 'wp-term-tab'
|
|
225
|
+
tab.dataset.id = x.id
|
|
226
|
+
tab.classList.toggle('wp-current', x.id === current)
|
|
227
|
+
tab.toggleAttribute('data-exited', !x.running)
|
|
228
|
+
const name = document.createElement('button')
|
|
229
|
+
name.type = 'button'
|
|
230
|
+
name.className = 'wp-term-tab-name'
|
|
231
|
+
name.textContent = x.label
|
|
232
|
+
name.title = x.running ? x.label : `${x.label}: exited (${x.exited})`
|
|
233
|
+
name.addEventListener('click', () => select(x.id))
|
|
234
|
+
tab.append(name)
|
|
235
|
+
const close = document.createElement('button')
|
|
236
|
+
close.type = 'button'
|
|
237
|
+
close.className = 'wp-term-tab-close'
|
|
238
|
+
close.title = 'Close this tab (ends its program)'
|
|
239
|
+
close.textContent = '×'
|
|
240
|
+
close.addEventListener('click', () => closeTab(x.id))
|
|
241
|
+
tab.append(close)
|
|
242
|
+
return tab
|
|
243
|
+
}),
|
|
244
|
+
)
|
|
245
|
+
showStatus()
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// "+" at the bar's right end, after the tabs, outside them so it stays in view when they scroll
|
|
249
|
+
const add = document.createElement('button')
|
|
250
|
+
add.type = 'button'
|
|
251
|
+
add.className = 'wp-term-add'
|
|
252
|
+
add.title = 'A new tab with a shell'
|
|
253
|
+
add.textContent = '+'
|
|
254
|
+
add.addEventListener('click', () => start({}))
|
|
255
|
+
tabs.after(add)
|
|
256
|
+
|
|
257
|
+
function showStatus() {
|
|
258
|
+
const x = sessions.find(y => y.id === current)
|
|
259
|
+
// no tab yet: "+" opens one
|
|
260
|
+
if (!x) return setStatus('', [])
|
|
261
|
+
if (x.running) return setStatus('', [])
|
|
262
|
+
setStatus(x.exited == null ? 'stopped' : `exited (${x.exited})`, [
|
|
263
|
+
['Start again', () => start({ id: x.id, restart: true })],
|
|
264
|
+
])
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function select(id: string | null) {
|
|
268
|
+
// the tab that shows already: just give it the keyboard back
|
|
269
|
+
if (id === current && stream) {
|
|
270
|
+
if (isOpen()) term.focus()
|
|
271
|
+
return
|
|
272
|
+
}
|
|
273
|
+
current = id
|
|
274
|
+
if (id) store.set('wp-terminal-tab', id)
|
|
275
|
+
drawTabs()
|
|
276
|
+
connect()
|
|
277
|
+
if (isOpen()) term.focus()
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async function closeTab(id: string) {
|
|
281
|
+
await post(BASE + '/api/terminal/close', { id })
|
|
282
|
+
sessions = sessions.filter(x => x.id !== id)
|
|
283
|
+
if (current === id) select(sessions.at(-1)?.id ?? null)
|
|
284
|
+
else drawTabs()
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Open a tab with the shell, or start tab `id` again as it was. */
|
|
288
|
+
async function start(body: { id?: string; restart?: boolean }) {
|
|
289
|
+
setStatus('starting…')
|
|
290
|
+
const res = await post(BASE + '/api/terminal/start', body)
|
|
291
|
+
const reply = await res.json().catch(() => ({}))
|
|
292
|
+
if (!res.ok) return setStatus(reply.error ?? 'could not start', [['Try again', () => start(body)]])
|
|
293
|
+
await loadSessions()
|
|
294
|
+
if (!isOpen()) setOpen(true)
|
|
295
|
+
select(reply.session)
|
|
296
|
+
term.focus()
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async function loadSessions() {
|
|
300
|
+
try {
|
|
301
|
+
const res = await fetch(BASE + '/api/terminal/sessions')
|
|
302
|
+
if (res.ok) ({ sessions } = await res.json())
|
|
303
|
+
} catch {
|
|
304
|
+
/* the server is away: the stream reconnects */
|
|
305
|
+
}
|
|
306
|
+
drawTabs()
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
let stream: EventSource | null = null
|
|
310
|
+
function connect() {
|
|
311
|
+
stream?.close()
|
|
312
|
+
term.reset()
|
|
313
|
+
sent = ''
|
|
314
|
+
if (!current) showStatus()
|
|
315
|
+
// without a tab too: the host tells it when one appears (started from the command line, say)
|
|
316
|
+
stream = new EventSource(
|
|
317
|
+
`${BASE}/api/terminal/stream?${current ? `id=${encodeURIComponent(current)}&` : ''}viewer=${viewer}`,
|
|
318
|
+
)
|
|
319
|
+
stream.addEventListener('sessions', e => {
|
|
320
|
+
sessions = JSON.parse((e as MessageEvent).data).sessions
|
|
321
|
+
// the current tab is gone, or there was none and one appeared: show the last
|
|
322
|
+
if (current ? !sessions.some(x => x.id === current) : sessions.length) return select(sessions.at(-1)?.id ?? null)
|
|
323
|
+
drawTabs()
|
|
324
|
+
})
|
|
325
|
+
stream.addEventListener('none', () => {
|
|
326
|
+
term.reset()
|
|
327
|
+
screen.querySelectorAll('.wp-term-placeholder').forEach(p => p.remove())
|
|
328
|
+
showStatus()
|
|
329
|
+
})
|
|
330
|
+
stream.addEventListener('screen', e => {
|
|
331
|
+
const { d, cols, rows, active: a } = JSON.parse((e as MessageEvent).data)
|
|
332
|
+
term.reset()
|
|
333
|
+
// the screen as the host has it, at the session's size; then the size this drawer shows it at
|
|
334
|
+
session = cols && rows ? { cols, rows } : null
|
|
335
|
+
active = a ?? null
|
|
336
|
+
if (session) term.resize(session.cols, session.rows)
|
|
337
|
+
term.write(d, () => {
|
|
338
|
+
refit()
|
|
339
|
+
// the live terminal takes over from the screens the server wrote into the page
|
|
340
|
+
screen.querySelectorAll('.wp-term-placeholder').forEach(p => p.remove())
|
|
341
|
+
})
|
|
342
|
+
showStatus()
|
|
343
|
+
if (isOpen() && store.get('wp-terminal-focus') === '1') term.focus()
|
|
344
|
+
})
|
|
345
|
+
stream.addEventListener('data', e => term.write(JSON.parse((e as MessageEvent).data).d))
|
|
346
|
+
// a program in this tab asked to show a page here (`workpad terminal open`): the drawer comes along
|
|
347
|
+
stream.addEventListener('navigate', e => {
|
|
348
|
+
const { path } = JSON.parse((e as MessageEvent).data)
|
|
349
|
+
if (typeof path === 'string' && path.startsWith('/') && !path.startsWith('//')) location.assign(BASE + path)
|
|
350
|
+
})
|
|
351
|
+
// the session's size changed, or another drawer became the active one
|
|
352
|
+
stream.addEventListener('size', e => {
|
|
353
|
+
const { cols, rows, active: a } = JSON.parse((e as MessageEvent).data)
|
|
354
|
+
session = { cols, rows }
|
|
355
|
+
active = a ?? null
|
|
356
|
+
store.set('wp-terminal-active', mine() ? '1' : '0')
|
|
357
|
+
show()
|
|
358
|
+
})
|
|
359
|
+
stream.addEventListener('exit', e => {
|
|
360
|
+
const code = JSON.parse((e as MessageEvent).data).code
|
|
361
|
+
const x = sessions.find(y => y.id === current)
|
|
362
|
+
if (x) {
|
|
363
|
+
x.running = false
|
|
364
|
+
x.exited = code
|
|
365
|
+
}
|
|
366
|
+
drawTabs()
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// whether the terminal had the keyboard, for the next page
|
|
371
|
+
addEventListener('pagehide', () =>
|
|
372
|
+
store.set('wp-terminal-focus', drawer.contains(document.activeElement) ? '1' : '0'),
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
drawTabs()
|
|
376
|
+
connect()
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function post(url: string, body: unknown) {
|
|
380
|
+
return fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** Mounts on the drawer the server rendered into this plugin's element in the bottom slot. */
|
|
384
|
+
const mount: PluginMount = (_root, _page, api) => {
|
|
385
|
+
api.slot('bottom', el => {
|
|
386
|
+
const drawer = el.querySelector<HTMLElement>('#wp-terminal')
|
|
387
|
+
if (drawer) init(drawer)
|
|
388
|
+
})
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export default mount
|