@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.
Files changed (153) hide show
  1. package/README.md +67 -17
  2. package/bin/whiteboard.ts +268 -71
  3. package/dist/client/runtime.js +88 -41
  4. package/dist/define.js +21 -2
  5. package/dist/plugins/callout/index.js +3 -1
  6. package/dist/plugins/callout/public/style.css +30 -0
  7. package/dist/plugins/chat/index.js +7 -4
  8. package/dist/plugins/chat/{client.js → public/client.js} +1 -1
  9. package/dist/plugins/chat/public/style.css +98 -0
  10. package/dist/plugins/code/index.js +3 -1
  11. package/dist/plugins/code/public/style.css +68 -0
  12. package/dist/plugins/comments/index.js +4 -5
  13. package/dist/plugins/comments/public/client.js +19 -0
  14. package/dist/plugins/comments/public/style.css +185 -0
  15. package/dist/plugins/details/index.js +7 -2
  16. package/dist/plugins/details/public/style.css +7 -0
  17. package/dist/plugins/form/index.js +37 -8
  18. package/dist/plugins/form/public/style.css +126 -0
  19. package/dist/plugins/gallery/index.js +4 -3
  20. package/dist/plugins/gallery/public/client.js +1 -0
  21. package/dist/plugins/gallery/public/style.css +90 -0
  22. package/dist/plugins/grid/index.js +9 -3
  23. package/dist/plugins/grid/public/style.css +71 -0
  24. package/dist/plugins/iframe/index.js +13 -14
  25. package/dist/plugins/iframe/public/style.css +13 -0
  26. package/dist/plugins/index.js +3 -1
  27. package/dist/plugins/mermaid/index.js +3 -1
  28. package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
  29. package/dist/plugins/terminal/index.js +10 -9
  30. package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
  31. package/dist/plugins/toc/index.js +4 -3
  32. package/dist/plugins/toc/public/client.js +1 -0
  33. package/dist/plugins/toc/public/style.css +93 -0
  34. package/dist/plugins/window/README.md +29 -0
  35. package/dist/plugins/window/index.js +64 -0
  36. package/dist/solid/index.js +14 -11
  37. package/dist/src/client/page/base.css +1078 -199
  38. package/dist/src/client/page/main.js +174 -33
  39. package/dist/src/loader.js +7 -2
  40. package/dist/src/package.js +3 -6
  41. package/dist/src/pages/blocks.js +8 -13
  42. package/dist/src/pages/markdown.js +1 -1
  43. package/dist/src/pages/render.js +74 -15
  44. package/dist/src/pages/window.js +30 -0
  45. package/dist/src/public.js +24 -0
  46. package/dist/src/server.js +36 -14
  47. package/package.json +3 -1
  48. package/plugins/callout/index.ts +3 -1
  49. package/plugins/callout/public/style.css +30 -0
  50. package/plugins/chat/autosize.ts +43 -9
  51. package/plugins/chat/client.tsx +22 -5
  52. package/plugins/chat/index.ts +22 -8
  53. package/plugins/chat/public/style.css +98 -0
  54. package/plugins/claude-code/index.ts +49 -14
  55. package/plugins/code/diff.ts +28 -8
  56. package/plugins/code/index.ts +30 -20
  57. package/plugins/code/public/style.css +68 -0
  58. package/plugins/comments/client.ts +139 -71
  59. package/plugins/comments/index.ts +9 -7
  60. package/plugins/comments/public/style.css +185 -0
  61. package/plugins/details/index.ts +7 -2
  62. package/plugins/details/public/style.css +7 -0
  63. package/plugins/forge/index.ts +16 -3
  64. package/plugins/form/index.ts +75 -27
  65. package/plugins/form/public/style.css +126 -0
  66. package/plugins/gallery/client.tsx +9 -3
  67. package/plugins/gallery/index.ts +31 -9
  68. package/plugins/gallery/public/style.css +90 -0
  69. package/plugins/grid/index.ts +19 -5
  70. package/plugins/grid/public/style.css +71 -0
  71. package/plugins/iframe/index.ts +20 -23
  72. package/plugins/iframe/public/style.css +13 -0
  73. package/plugins/index.ts +16 -1
  74. package/plugins/mermaid/index.ts +3 -1
  75. package/plugins/mermaid/{style.css → public/style.css} +7 -2
  76. package/plugins/terminal/client.ts +105 -48
  77. package/plugins/terminal/host.ts +131 -29
  78. package/plugins/terminal/index.ts +84 -25
  79. package/plugins/terminal/public/style.css +225 -0
  80. package/plugins/terminal/server.ts +64 -13
  81. package/plugins/terminal/sizes.ts +13 -3
  82. package/plugins/toc/client.ts +45 -26
  83. package/plugins/toc/index.ts +9 -6
  84. package/plugins/toc/public/style.css +93 -0
  85. package/plugins/window/README.md +29 -0
  86. package/plugins/window/index.ts +85 -0
  87. package/skills/whiteboard/SKILL.md +3 -1
  88. package/solid/index.tsx +24 -9
  89. package/src/auth.ts +25 -7
  90. package/src/client/forms.ts +74 -28
  91. package/src/client/markdown.ts +15 -4
  92. package/src/client/page/base.css +1078 -199
  93. package/src/client/page/elsewhere.ts +32 -15
  94. package/src/client/page/fresh.ts +91 -23
  95. package/src/client/page/hold.ts +11 -2
  96. package/src/client/page/live.ts +10 -6
  97. package/src/client/page/main.ts +106 -36
  98. package/src/client/page/orphans.ts +25 -10
  99. package/src/client/page/sort.ts +14 -4
  100. package/src/client/page/sticky.ts +11 -3
  101. package/src/client/page/theme.ts +11 -7
  102. package/src/client/page/waiting.ts +141 -34
  103. package/src/client/page/window.ts +40 -0
  104. package/src/client/reactive.ts +19 -0
  105. package/src/client/read.ts +17 -10
  106. package/src/client/runtime.ts +130 -35
  107. package/src/client/scroll.ts +14 -4
  108. package/src/client/toast.ts +19 -5
  109. package/src/config.ts +20 -5
  110. package/src/define.ts +65 -25
  111. package/src/events.ts +111 -24
  112. package/src/html.ts +6 -2
  113. package/src/icons.ts +4 -2
  114. package/src/load.ts +10 -2
  115. package/src/loader.ts +75 -11
  116. package/src/package.ts +9 -8
  117. package/src/pages/blocks.ts +20 -14
  118. package/src/pages/markdown.ts +113 -34
  119. package/src/pages/render.ts +199 -39
  120. package/src/pages/window.ts +71 -0
  121. package/src/plugins.ts +90 -24
  122. package/src/public.ts +39 -0
  123. package/src/running.ts +8 -2
  124. package/src/serve.ts +29 -7
  125. package/src/server.ts +348 -107
  126. package/src/status.ts +15 -9
  127. package/dist/plugins/callout/style.css +0 -12
  128. package/dist/plugins/chat/style.css +0 -24
  129. package/dist/plugins/code/style.css +0 -29
  130. package/dist/plugins/comments/client.js +0 -10
  131. package/dist/plugins/comments/style.css +0 -40
  132. package/dist/plugins/details/style.css +0 -3
  133. package/dist/plugins/form/style.css +0 -44
  134. package/dist/plugins/gallery/client.js +0 -1
  135. package/dist/plugins/gallery/style.css +0 -27
  136. package/dist/plugins/grid/style.css +0 -25
  137. package/dist/plugins/iframe/client.js +0 -1
  138. package/dist/plugins/iframe/style.css +0 -29
  139. package/dist/plugins/toc/client.js +0 -1
  140. package/dist/plugins/toc/style.css +0 -19
  141. package/plugins/callout/style.css +0 -12
  142. package/plugins/chat/style.css +0 -24
  143. package/plugins/code/style.css +0 -29
  144. package/plugins/comments/style.css +0 -40
  145. package/plugins/details/style.css +0 -3
  146. package/plugins/form/style.css +0 -44
  147. package/plugins/gallery/style.css +0 -27
  148. package/plugins/grid/style.css +0 -25
  149. package/plugins/iframe/client.ts +0 -46
  150. package/plugins/iframe/style.css +0 -29
  151. package/plugins/terminal/style.css +0 -71
  152. package/plugins/toc/style.css +0 -19
  153. /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
@@ -0,0 +1,225 @@
1
+ /* The terminal drawer (see terminal.ts). Pages morph into each other on navigation, and the drawer is
2
+ named, so it keeps its place and size while the page around it changes. */
3
+ @view-transition {
4
+ navigation: auto;
5
+ }
6
+
7
+ /* How much of the window the drawer takes at the bottom, for the page to keep clear: its content
8
+ scrolls to above it, and its own fixed elements sit above it (the page's buttons at the bottom).
9
+ Registered as a length so that it computes to pixels a script can read. */
10
+ @property --wb-inset-bottom {
11
+ syntax: '<length>';
12
+ inherits: true;
13
+ initial-value: 0px;
14
+ }
15
+
16
+ :root {
17
+ --wb-term-h: 320px;
18
+ --wb-term-bar: 34px;
19
+ }
20
+ :root:has(body > #wb-terminal) {
21
+ --wb-inset-bottom: var(--wb-term-bar);
22
+ }
23
+ :root:has(body > #wb-terminal[data-state='open']) {
24
+ --wb-inset-bottom: calc(var(--wb-term-bar) + var(--wb-term-h));
25
+ }
26
+
27
+ #wb-terminal {
28
+ view-transition-name: wb-terminal;
29
+ position: fixed;
30
+ left: 0;
31
+ right: 0;
32
+ bottom: 0;
33
+ z-index: var(--layer-top);
34
+ display: flex;
35
+ flex-direction: column;
36
+ background: #0f1115;
37
+ color: #d7dae0;
38
+ border-top: var(--hairline) solid #2a2f3a;
39
+ box-shadow: 0 -6px 24px rgb(0 0 0 / 0.18);
40
+ font:
41
+ 500 12px/1 ui-sans-serif,
42
+ system-ui,
43
+ sans-serif;
44
+ }
45
+ body:has(> #wb-terminal) {
46
+ padding-bottom: var(--wb-inset-bottom);
47
+ }
48
+
49
+ .wb-term-grip {
50
+ position: absolute;
51
+ left: 0;
52
+ right: 0;
53
+ top: -4px;
54
+ height: 8px;
55
+ cursor: ns-resize;
56
+ touch-action: none;
57
+ }
58
+ .wb-term-bar {
59
+ height: var(--wb-term-bar);
60
+ display: flex;
61
+ align-items: center;
62
+ gap: var(--space-xl);
63
+ padding: 0 var(--space-lg) 0 var(--space-sm);
64
+ flex: none;
65
+ }
66
+ .wb-term-bar button {
67
+ font: inherit;
68
+ color: inherit;
69
+ background: transparent;
70
+ border: var(--hairline) solid transparent;
71
+ border-radius: var(--radius-sm);
72
+ padding: var(--space-xs) var(--space-md);
73
+ cursor: pointer;
74
+ }
75
+ .wb-term-bar button:hover {
76
+ background: #1c2029;
77
+ border-color: #2a2f3a;
78
+ }
79
+ .wb-term-toggle {
80
+ display: inline-flex;
81
+ align-items: center;
82
+ gap: var(--space-sm);
83
+ letter-spacing: var(--caps-spacing);
84
+ text-transform: uppercase;
85
+ }
86
+ /* points up while closed (it opens upward), down while open */
87
+ .wb-term-chevron {
88
+ width: 7px;
89
+ height: 7px;
90
+ border: solid currentColor;
91
+ border-width: 0 1.5px 1.5px 0;
92
+ rotate: 45deg;
93
+ translate: 0 -2px;
94
+ transition:
95
+ rotate 0.2s,
96
+ translate 0.2s;
97
+ }
98
+ #wb-terminal[data-state='collapsed'] .wb-term-chevron {
99
+ rotate: -135deg;
100
+ translate: 0 2px;
101
+ }
102
+ .wb-term-status {
103
+ color: #8b93a3;
104
+ flex: 1;
105
+ min-width: 0;
106
+ overflow: hidden;
107
+ text-overflow: ellipsis;
108
+ white-space: nowrap;
109
+ }
110
+ .wb-term-actions {
111
+ display: flex;
112
+ gap: var(--space-xs);
113
+ }
114
+
115
+ /* the tabs: one per session, the current one lit */
116
+ /* the bar: the toggle, what the current tab says (status, actions), then the tabs and "+" at the right */
117
+ .wb-term-tabs {
118
+ display: flex;
119
+ align-items: center;
120
+ gap: var(--space-2xs);
121
+ min-width: 0;
122
+ flex: 0 1 auto;
123
+ overflow-x: auto;
124
+ scrollbar-width: none;
125
+ }
126
+ .wb-term-bar .wb-term-add {
127
+ flex: none;
128
+ }
129
+ .wb-term-tab {
130
+ display: inline-flex;
131
+ align-items: center;
132
+ flex: none;
133
+ border-radius: var(--radius-sm);
134
+ }
135
+ .wb-term-bar .wb-term-tab button {
136
+ padding: var(--space-xs) var(--space-xs) var(--space-xs) var(--space-md);
137
+ }
138
+ .wb-term-bar .wb-term-tab .wb-term-tab-close {
139
+ padding: var(--space-xs) var(--space-sm) var(--space-xs) var(--space-2xs);
140
+ opacity: 0.45;
141
+ }
142
+ .wb-term-bar .wb-term-tab .wb-term-tab-close:hover {
143
+ opacity: 1;
144
+ background: transparent;
145
+ border-color: transparent;
146
+ }
147
+ .wb-term-bar .wb-term-tab button:hover {
148
+ background: transparent;
149
+ border-color: transparent;
150
+ }
151
+ .wb-term-tab:hover {
152
+ background: #1c2029;
153
+ }
154
+ .wb-term-tab.wb-current {
155
+ background: #232836;
156
+ color: #fff;
157
+ }
158
+ .wb-term-tab[data-exited] {
159
+ color: #8b93a3;
160
+ }
161
+ .wb-term-bar .wb-term-add {
162
+ padding: var(--space-2xs) var(--space-md);
163
+ font-size: 15px;
164
+ line-height: 1;
165
+ color: #8b93a3;
166
+ }
167
+ .wb-term-bar .wb-term-add:hover {
168
+ color: #d7dae0;
169
+ }
170
+
171
+ /* content-box: the fit addon reads this height as the room for rows, so the padding must not be in it */
172
+ .wb-term-screen {
173
+ height: calc(var(--wb-term-h) - 10px);
174
+ padding: var(--space-xs) 0 var(--space-sm) var(--space-lg);
175
+ box-sizing: content-box;
176
+ overflow: hidden;
177
+ }
178
+ /* the screen as the server wrote it into the page, over the terminal until that has drawn it */
179
+ .wb-term-screen {
180
+ position: relative;
181
+ }
182
+ .wb-term-placeholder {
183
+ position: absolute;
184
+ inset: 4px 0 6px 10px;
185
+ z-index: 1;
186
+ overflow: hidden;
187
+ background: #0f1115;
188
+ font:
189
+ 13px/1.2 ui-monospace,
190
+ SFMono-Regular,
191
+ Menlo,
192
+ monospace;
193
+ white-space: pre;
194
+ color: #d7dae0;
195
+ }
196
+ .wb-term-placeholder > div {
197
+ height: 1.2em;
198
+ }
199
+ /* the server writes every tab's screen; the current tab's shows */
200
+ .wb-term-placeholder:not(.wb-current) {
201
+ display: none;
202
+ }
203
+ #wb-terminal[data-state='collapsed'] .wb-term-screen {
204
+ height: 0;
205
+ padding: 0;
206
+ visibility: hidden;
207
+ }
208
+
209
+ ::view-transition-group(wb-terminal) {
210
+ animation-duration: 0.22s;
211
+ animation-timing-function: cubic-bezier(0.2, 0.7, 0.2, 1);
212
+ }
213
+ /* opening and closing: the pictures keep their scale and stay anchored at the bar, so the text
214
+ doesn't stretch while the drawer grows */
215
+ ::view-transition-old(wb-terminal),
216
+ ::view-transition-new(wb-terminal) {
217
+ height: 100%;
218
+ object-fit: none;
219
+ object-position: top;
220
+ }
221
+ @media (prefers-reduced-motion: reduce) {
222
+ ::view-transition-group(*) {
223
+ animation: none !important;
224
+ }
225
+ }
@@ -52,7 +52,14 @@ type Reply = Record<string, any>
52
52
  const require = createRequire(import.meta.url)
53
53
  const HOST = fileURLToPath(new URL(`./host${path.extname(fileURLToPath(import.meta.url))}`, import.meta.url))
54
54
 
55
- const hasPty = () => { try { require.resolve('node-pty'); return true } catch { return false } }
55
+ const hasPty = () => {
56
+ try {
57
+ require.resolve('node-pty')
58
+ return true
59
+ } catch {
60
+ return false
61
+ }
62
+ }
56
63
 
57
64
  /**
58
65
  * Where the host listens: `host.sock` in the terminal's folder, or, when that path is too long for
@@ -60,7 +67,9 @@ const hasPty = () => { try { require.resolve('node-pty'); return true } catch {
60
67
  */
61
68
  export function hostSocket(home: string): string {
62
69
  const own = path.join(home, 'host.sock')
63
- return Buffer.byteLength(own) < 100 ? own : path.join('/tmp', `wb-term-${crypto.createHash('sha1').update(home).digest('hex').slice(0, 12)}.sock`)
70
+ return Buffer.byteLength(own) < 100
71
+ ? own
72
+ : path.join('/tmp', `wb-term-${crypto.createHash('sha1').update(home).digest('hex').slice(0, 12)}.sock`)
64
73
  }
65
74
 
66
75
  /** `dir` the folder the sessions run in, `home` where the socket and log go */
@@ -85,10 +94,24 @@ export function createTerminal({ dir, home }: { dir: string; home: string }) {
85
94
  if (!conn) return null
86
95
  return new Promise(resolve => {
87
96
  let buf = ''
88
- const done = (value: Reply | null) => { clearTimeout(timer); conn.destroy(); resolve(value) }
97
+ const done = (value: Reply | null) => {
98
+ clearTimeout(timer)
99
+ conn.destroy()
100
+ resolve(value)
101
+ }
89
102
  const timer = setTimeout(() => done(null), timeout)
90
103
  conn.setEncoding('utf8')
91
- conn.on('data', (c: string) => { buf += c; const nl = buf.indexOf('\n'); if (nl >= 0) { try { done(JSON.parse(buf.slice(0, nl))) } catch { done(null) } } })
104
+ conn.on('data', (c: string) => {
105
+ buf += c
106
+ const nl = buf.indexOf('\n')
107
+ if (nl >= 0) {
108
+ try {
109
+ done(JSON.parse(buf.slice(0, nl)))
110
+ } catch {
111
+ done(null)
112
+ }
113
+ }
114
+ })
92
115
  conn.on('error', () => done(null))
93
116
  conn.on('end', () => done(null))
94
117
  conn.write(JSON.stringify(req) + '\n')
@@ -98,7 +121,12 @@ export function createTerminal({ dir, home }: { dir: string; home: string }) {
98
121
  /** Start the host (detached: it outlives this server) and wait until it answers. */
99
122
  async function startHost(): Promise<boolean> {
100
123
  const out = fs.openSync(log, 'a')
101
- const child = spawn(process.execPath, [HOST, dir, own], { cwd: dir, detached: true, stdio: ['ignore', out, out], env: process.env })
124
+ const child = spawn(process.execPath, [HOST, dir, own], {
125
+ cwd: dir,
126
+ detached: true,
127
+ stdio: ['ignore', out, out],
128
+ env: process.env,
129
+ })
102
130
  child.unref()
103
131
  fs.closeSync(out)
104
132
  for (let i = 0; i < 40; i++) {
@@ -119,7 +147,11 @@ export function createTerminal({ dir, home }: { dir: string; home: string }) {
119
147
  /** The screens as HTML rows by session id, for a page's first render. */
120
148
  async function screens(list: Session[]): Promise<Record<string, string>> {
121
149
  const out: Record<string, string> = {}
122
- await Promise.all(list.map(async s => { out[s.id] = (await ask({ t: 'html', id: s.id }, 300))?.html ?? '' }))
150
+ await Promise.all(
151
+ list.map(async s => {
152
+ out[s.id] = (await ask({ t: 'html', id: s.id }, 300))?.html ?? ''
153
+ }),
154
+ )
123
155
  return out
124
156
  }
125
157
 
@@ -134,7 +166,14 @@ export function createTerminal({ dir, home }: { dir: string; home: string }) {
134
166
  async function start({ id, program = [], restart = false }: StartOptions = {}) {
135
167
  if (!hasPty()) throw failure('the terminal needs node-pty: npm install node-pty')
136
168
  const request = program.length
137
- ? { t: 'start', id, command: shell, args: ['-c', '"$@"; exec "$0" -l', shell, ...program], label: path.basename(program[0]!), restart }
169
+ ? {
170
+ t: 'start',
171
+ id,
172
+ command: shell,
173
+ args: ['-c', '"$@"; exec "$0" -l', shell, ...program],
174
+ label: path.basename(program[0]!),
175
+ restart,
176
+ }
138
177
  : restart && id != null
139
178
  ? { t: 'start', id, restart }
140
179
  : { t: 'start', id, command: shell, args: ['-l'], label: path.basename(shell), restart }
@@ -147,9 +186,13 @@ export function createTerminal({ dir, home }: { dir: string; home: string }) {
147
186
  return { available: true, session: reply.session ?? '1', already: !!reply.already }
148
187
  }
149
188
 
150
- const input = async (id: string, data: string) => { await ask({ t: 'input', id, d: data }) }
189
+ const input = async (id: string, data: string) => {
190
+ await ask({ t: 'input', id, d: data })
191
+ }
151
192
  /** The size that fits drawer `viewer`; `active`: the reader is using that drawer now (see host.ts, Sizes). */
152
- const resize = async (id: string, viewer: string, cols: number, rows: number, active = false) => { await ask({ t: 'resize', id, viewer, cols, rows, active }) }
193
+ const resize = async (id: string, viewer: string, cols: number, rows: number, active = false) => {
194
+ await ask({ t: 'resize', id, viewer, cols, rows, active })
195
+ }
153
196
 
154
197
  /** End one session and drop its tab. */
155
198
  async function close(id: string) {
@@ -185,13 +228,20 @@ export function createTerminal({ dir, home }: { dir: string; home: string }) {
185
228
  try {
186
229
  const { t, ...data } = JSON.parse(line)
187
230
  res.write(`event: ${t}\ndata: ${JSON.stringify(data)}\n\n`)
188
- } catch { /* not a message */ }
231
+ } catch {
232
+ /* not a message */
233
+ }
189
234
  }
190
235
  })
191
236
  // the host went away (stopped, or its program ended and it left): the page shows it as stopped
192
- conn.on('close', () => { if (!res.writableEnded) res.write('event: exit\ndata: {"code":null}\n\n') })
237
+ conn.on('close', () => {
238
+ if (!res.writableEnded) res.write('event: exit\ndata: {"code":null}\n\n')
239
+ })
193
240
  conn.on('error', () => {})
194
- req.on('close', () => { clearInterval(ping); conn.destroy() })
241
+ req.on('close', () => {
242
+ clearInterval(ping)
243
+ conn.destroy()
244
+ })
195
245
  conn.write(JSON.stringify({ t: 'attach', id, viewer }) + '\n')
196
246
  }
197
247
 
@@ -205,7 +255,8 @@ export function createTerminal({ dir, home }: { dir: string; home: string }) {
205
255
  * last: for session `id`, or without one, of all sessions.
206
256
  */
207
257
  async function open(id: string | undefined, path: string) {
208
- if (!path.startsWith('/') || path.startsWith('//')) throw failure('open takes a path on this server, like /plan', 400)
258
+ if (!path.startsWith('/') || path.startsWith('//'))
259
+ throw failure('open takes a path on this server, like /plan', 400)
209
260
  const reply = await ask({ t: 'open', id, path })
210
261
  if (!reply) throw failure('no terminal host runs', 404)
211
262
  return { shown: Number(reply.shown ?? 0) }
@@ -20,9 +20,16 @@ export interface Sizes {
20
20
  * A drawer reports the size that fits it; `used`: the reader uses it now. Returns the size the
21
21
  * session should have: its own when the drawer is (now) the active one, else the session's as it is.
22
22
  */
23
- export function report(s: Sizes, viewer: string, cols: number, rows: number, used: boolean, now = Date.now()): { cols: number; rows: number } {
23
+ export function report(
24
+ s: Sizes,
25
+ viewer: string,
26
+ cols: number,
27
+ rows: number,
28
+ used: boolean,
29
+ now = Date.now(),
30
+ ): { cols: number; rows: number } {
24
31
  const known = s.viewers.get(viewer)
25
- s.viewers.set(viewer, { cols, rows, used: used ? now : known?.used ?? 0 })
32
+ s.viewers.set(viewer, { cols, rows, used: used ? now : (known?.used ?? 0) })
26
33
  if (used || !s.active || !s.viewers.has(s.active)) s.active = viewer
27
34
  return s.active === viewer ? { cols, rows } : { cols: s.cols, rows: s.rows }
28
35
  }
@@ -54,7 +61,10 @@ export function lastUsed<S extends Sizes>(sessions: S[]): S | undefined {
54
61
  let at = -1
55
62
  for (const s of sessions) {
56
63
  const viewer = s.active ? s.viewers.get(s.active) : undefined
57
- if (viewer && viewer.used > at) { best = s; at = viewer.used }
64
+ if (viewer && viewer.used > at) {
65
+ best = s
66
+ at = viewer.used
67
+ }
58
68
  }
59
69
  return best
60
70
  }
@@ -14,7 +14,8 @@ const toggle = document.createElement('button')
14
14
  toggle.type = 'button'
15
15
  toggle.className = 'wb-toc-toggle'
16
16
  toggle.title = toggle.ariaLabel = 'Contents'
17
- toggle.innerHTML = '<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><path d="M2.5 4h11M2.5 8h11M2.5 12h7"/></svg>'
17
+ toggle.innerHTML =
18
+ '<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><path d="M2.5 4h11M2.5 8h11M2.5 12h7"/></svg>'
18
19
 
19
20
  let main: HTMLElement
20
21
  let api: Api
@@ -32,29 +33,32 @@ let handle: PageHandle
32
33
 
33
34
  /** Does this page have it: what its frontmatter says (`toc:`), and whether it is on everywhere. */
34
35
  function wanted() {
35
- const said = ((handle.data as { frontmatter?: Record<string, string> } | null)?.frontmatter ?? {}).toc?.trim().toLowerCase()
36
+ const said = handle.frontmatter().toc?.trim().toLowerCase()
36
37
  const always = document.querySelector('meta[name=wb-toc]')?.getAttribute('content') === 'always'
37
38
  return always ? said !== 'false' : said === 'true'
38
39
  }
39
40
 
40
41
  function build() {
41
42
  headings = wanted() ? [...main.querySelectorAll<HTMLElement>(':scope > h2, :scope > h3')] : []
42
- let h2 = 0, h3 = 0
43
- list.replaceChildren(...headings.map((h, i) => {
44
- // the page's numbers: 1, 1.1, 1.2, 2 (base.css counts ## and ### alike)
45
- const number = h.tagName === 'H2' ? `${++h2}` : `${h2}.${++h3}`
46
- if (h.tagName === 'H2') h3 = 0
47
- const li = document.createElement('li')
48
- li.dataset.level = h.tagName === 'H2' ? '2' : '3'
49
- const a = document.createElement('a')
50
- a.href = '#'
51
- a.dataset.index = String(i)
52
- a.innerHTML = `<span class="wb-toc-number"></span><span class="wb-toc-text"></span>`
53
- a.querySelector('.wb-toc-number')!.textContent = number
54
- a.querySelector('.wb-toc-text')!.textContent = h.textContent ?? ''
55
- li.append(a)
56
- return li
57
- }))
43
+ let h2 = 0,
44
+ h3 = 0
45
+ list.replaceChildren(
46
+ ...headings.map((h, i) => {
47
+ // the page's numbers: 1, 1.1, 1.2, 2 (base.css counts ## and ### alike)
48
+ const number = h.tagName === 'H2' ? `${++h2}` : `${h2}.${++h3}`
49
+ if (h.tagName === 'H2') h3 = 0
50
+ const li = document.createElement('li')
51
+ li.dataset.level = h.tagName === 'H2' ? '2' : '3'
52
+ const a = document.createElement('a')
53
+ a.href = '#'
54
+ a.dataset.index = String(i)
55
+ a.innerHTML = `<span class="wb-toc-number"></span><span class="wb-toc-text"></span>`
56
+ a.querySelector('.wb-toc-number')!.textContent = number
57
+ a.querySelector('.wb-toc-text')!.textContent = h.textContent ?? ''
58
+ li.append(a)
59
+ return li
60
+ }),
61
+ )
58
62
  const any = headings.length > 1
59
63
  toggle.hidden = !any || fits()
60
64
  panel.hidden = !any || (!fits() && !panel.classList.contains('wb-toc-open'))
@@ -87,7 +91,9 @@ function mark() {
87
91
  if (!badge.dataset.wbHolds!.split(' ').some(id => unread.has(id))) continue
88
92
  const y = badge.getBoundingClientRect().top
89
93
  let at = -1
90
- headings.forEach((h, i) => { if (h.getBoundingClientRect().top <= y) at = i })
94
+ headings.forEach((h, i) => {
95
+ if (h.getBoundingClientRect().top <= y) at = i
96
+ })
91
97
  waiting.add(at)
92
98
  }
93
99
  list.querySelectorAll<HTMLElement>('a').forEach((a, i) => a.toggleAttribute('data-waiting', waiting.has(i)))
@@ -97,15 +103,22 @@ function mark() {
97
103
  function current() {
98
104
  const line = navHeight() + 40
99
105
  let at = 0
100
- headings.forEach((h, i) => { if (h.getBoundingClientRect().top <= line) at = i })
101
- list.querySelectorAll<HTMLElement>('a').forEach((a, i) => a.toggleAttribute('aria-current', i === at && headings.length > 0))
106
+ headings.forEach((h, i) => {
107
+ if (h.getBoundingClientRect().top <= line) at = i
108
+ })
109
+ list
110
+ .querySelectorAll<HTMLElement>('a')
111
+ .forEach((a, i) => a.toggleAttribute('aria-current', i === at && headings.length > 0))
102
112
  }
103
113
 
104
114
  function go(i: number) {
105
115
  const h = headings[i]
106
116
  if (!h) return
107
117
  scrollTo({ top: Math.max(0, scrollY + h.getBoundingClientRect().top - navHeight() - 12), behavior: 'smooth' })
108
- if (!fits()) { panel.classList.remove('wb-toc-open'); panel.hidden = true }
118
+ if (!fits()) {
119
+ panel.classList.remove('wb-toc-open')
120
+ panel.hidden = true
121
+ }
109
122
  }
110
123
 
111
124
  const mount: PluginMount = (root, page, core) => {
@@ -126,14 +139,20 @@ const mount: PluginMount = (root, page, core) => {
126
139
  panel.classList.toggle('wb-toc-open', open)
127
140
  panel.hidden = !open
128
141
  })
129
- document.addEventListener('keydown', e => { if (e.key === 'Escape' && panel.classList.contains('wb-toc-open')) toggle.click() })
142
+ document.addEventListener('keydown', e => {
143
+ if (e.key === 'Escape' && panel.classList.contains('wb-toc-open')) toggle.click()
144
+ })
130
145
  let frame = 0
131
- const soon = (fn: () => void) => () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(fn) }
146
+ const soon = (fn: () => void) => () => {
147
+ cancelAnimationFrame(frame)
148
+ frame = requestAnimationFrame(fn)
149
+ }
132
150
  addEventListener('scroll', soon(current), { passive: true })
133
151
  addEventListener('resize', soon(build))
134
152
  api.onRendered(build)
135
- api.onForms(() => mark())
136
- api.onEvents(() => requestAnimationFrame(mark))
153
+ page.frontmatter.on(build)
154
+ api.forms.on(() => mark())
155
+ api.events.on(() => requestAnimationFrame(mark))
137
156
  build()
138
157
  }
139
158
 
@@ -8,7 +8,7 @@
8
8
  //
9
9
  // A page has it when its frontmatter says `toc: true`. With `toc({ always: true })`, every page has
10
10
  // it, unless its frontmatter says `toc: false`.
11
- import { packagePath } from '../../src/package.ts'
11
+ import { builtinPublic } from '../../src/package.ts'
12
12
  import type { Plugin } from '../../src/define.ts'
13
13
 
14
14
  const DOCS = `## Table of contents
@@ -26,12 +26,15 @@ export interface TocOptions {
26
26
  export default function toc(options: TocOptions = {}): Plugin {
27
27
  return {
28
28
  name: 'toc',
29
- docs: () => options.always
30
- ? DOCS + '\nIn this project every page has it; a page without it says `toc: false` in its frontmatter.\n'
31
- : DOCS + '\nA page has it when its frontmatter says `toc: true`:\n\n```markdown\n---\ntoc: true\n---\n# A long page\n```\n',
32
- style: packagePath('dist/plugins/toc/style.css'),
29
+ docs: () =>
30
+ options.always
31
+ ? DOCS + '\nIn this project every page has it; a page without it says `toc: false` in its frontmatter.\n'
32
+ : DOCS +
33
+ '\nA page has it when its frontmatter says `toc: true`:\n\n```markdown\n---\ntoc: true\n---\n# A long page\n```\n',
34
+ public: builtinPublic('toc'),
35
+ style: 'style.css',
33
36
  setup: () => ({
34
- client: packagePath('dist/plugins/toc/client.js'),
37
+ client: 'client.js',
35
38
  // how the browser part knows: on every page, or only where the frontmatter asks
36
39
  shell: () => ({ head: `<meta name="wb-toc" content="${options.always ? 'always' : 'opt-in'}">` }),
37
40
  }),
@@ -0,0 +1,93 @@
1
+ /* the table of contents (client.ts): in the margin left of the text, or opened from the header */
2
+ .wb-toc {
3
+ position: fixed;
4
+ z-index: var(--layer-aside);
5
+ font: var(--ui-small)/var(--ui-line-height) var(--sans, sans-serif);
6
+ }
7
+ .wb-toc[hidden],
8
+ .wb-toc-toggle[hidden] {
9
+ display: none;
10
+ }
11
+ .wb-toc-docked {
12
+ top: 72px;
13
+ left: max(16px, calc((100vw - var(--measure, 680px)) / 2 - 250px));
14
+ width: 210px;
15
+ max-height: calc(100vh - 120px - var(--wb-inset-bottom, 0px));
16
+ overflow: auto;
17
+ }
18
+ .wb-toc-open:not(.wb-toc-docked) {
19
+ top: 52px;
20
+ left: 16px;
21
+ width: min(300px, 100vw - 32px);
22
+ max-height: calc(100vh - 100px - var(--wb-inset-bottom, 0px));
23
+ overflow: auto;
24
+ padding: var(--space-sm);
25
+ background: var(--bg, Canvas);
26
+ border: var(--hairline) solid var(--border, #e3e5e9);
27
+ border-radius: var(--radius);
28
+ box-shadow: var(--shadow-pop);
29
+ }
30
+ .wb-toc ol {
31
+ list-style: none;
32
+ margin: 0;
33
+ padding: 0;
34
+ }
35
+ .wb-toc li[data-level='3'] {
36
+ padding-left: var(--space-xl);
37
+ }
38
+ .wb-toc a {
39
+ display: flex;
40
+ gap: var(--space-md);
41
+ align-items: baseline;
42
+ padding: var(--space-2xs) var(--space-md);
43
+ border-radius: var(--radius-sm);
44
+ color: var(--muted, #696e76);
45
+ text-decoration: none;
46
+ border-left: 2px solid transparent;
47
+ }
48
+ .wb-toc a:hover {
49
+ color: var(--text, CanvasText);
50
+ background: color-mix(in srgb, var(--text, CanvasText) var(--tint-faint), transparent);
51
+ }
52
+ .wb-toc a[aria-current] {
53
+ color: var(--text, CanvasText);
54
+ border-left-color: var(--accent, #1d5bbf);
55
+ font-weight: var(--weight-strong);
56
+ }
57
+ .wb-toc-number {
58
+ font-variant-numeric: tabular-nums;
59
+ opacity: 0.7;
60
+ flex: none;
61
+ }
62
+ .wb-toc-text {
63
+ overflow: hidden;
64
+ text-overflow: ellipsis;
65
+ }
66
+ /* something waits for the reader in this section */
67
+ .wb-toc a[data-waiting]::after {
68
+ content: '';
69
+ flex: none;
70
+ width: 7px;
71
+ height: 7px;
72
+ margin-left: auto;
73
+ align-self: center;
74
+ border-radius: 50%;
75
+ background: var(--accent, #1d5bbf);
76
+ }
77
+ .wb-toc-toggle {
78
+ flex: none;
79
+ display: inline-grid;
80
+ place-items: center;
81
+ height: 24px;
82
+ box-sizing: border-box;
83
+ padding: 0 var(--space-sm);
84
+ border-radius: var(--radius-pill);
85
+ border: var(--hairline) solid var(--border, #e3e5e9);
86
+ background: none;
87
+ color: var(--muted, #696e76);
88
+ cursor: pointer;
89
+ }
90
+ .wb-toc-toggle:hover {
91
+ color: var(--text, CanvasText);
92
+ border-color: var(--muted, #696e76);
93
+ }
@@ -0,0 +1,29 @@
1
+ Windows: a header with a label, a button that fills the browser window with
2
+ what it shows (the same button, or Esc, puts it back), and one that opens it on
3
+ its own. `bare` leaves the header out; `wide` makes it wider than the text
4
+ column; `height` (pixels or a CSS length) fixes its height, scrolling inside.
5
+
6
+ **One image or video**, its label the title, ↗ opening the file:
7
+
8
+ ```markdown
9
+ ::image[The new onboarding]{src="onboarding.png"}
10
+
11
+ ::video[The bug, recorded]{src="bug.mp4" loop muted poster="bug.png"}
12
+ ```
13
+
14
+ A video has its controls; `loop`, `autoplay` (which starts it muted, as
15
+ browsers require) and `poster` are as for a `<video>`. A plain markdown image,
16
+ `![…](shot.png)`, stays an image without a window.
17
+
18
+ **Any markdown**, in `:::window`: a diagram, a table, a screenshot with words
19
+ under it. When it holds one image or video, ↗ opens that file.
20
+
21
+ ````markdown
22
+ :::window[How a page is served]{wide}
23
+ ```mermaid
24
+ graph LR; file --> server --> browser
25
+ ```
26
+ :::
27
+ ````
28
+
29
+ An iframe (`::iframe`) is a window around another page.