@bigmistqke/whiteboard 0.3.0 → 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 (155) 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/README.md +12 -0
  32. package/dist/plugins/toc/index.js +9 -5
  33. package/dist/plugins/toc/public/client.js +1 -0
  34. package/dist/plugins/toc/public/style.css +93 -0
  35. package/dist/plugins/window/README.md +29 -0
  36. package/dist/plugins/window/index.js +64 -0
  37. package/dist/solid/index.js +14 -11
  38. package/dist/src/client/page/base.css +1078 -199
  39. package/dist/src/client/page/main.js +174 -32
  40. package/dist/src/loader.js +7 -2
  41. package/dist/src/package.js +3 -6
  42. package/dist/src/pages/blocks.js +8 -13
  43. package/dist/src/pages/markdown.js +1 -1
  44. package/dist/src/pages/render.js +73 -13
  45. package/dist/src/pages/window.js +30 -0
  46. package/dist/src/public.js +24 -0
  47. package/dist/src/server.js +37 -15
  48. package/package.json +3 -1
  49. package/plugins/callout/index.ts +3 -1
  50. package/plugins/callout/public/style.css +30 -0
  51. package/plugins/chat/autosize.ts +43 -9
  52. package/plugins/chat/client.tsx +22 -5
  53. package/plugins/chat/index.ts +22 -8
  54. package/plugins/chat/public/style.css +98 -0
  55. package/plugins/claude-code/index.ts +49 -14
  56. package/plugins/code/diff.ts +28 -8
  57. package/plugins/code/index.ts +30 -20
  58. package/plugins/code/public/style.css +68 -0
  59. package/plugins/comments/client.ts +139 -71
  60. package/plugins/comments/index.ts +9 -7
  61. package/plugins/comments/public/style.css +185 -0
  62. package/plugins/details/index.ts +7 -2
  63. package/plugins/details/public/style.css +7 -0
  64. package/plugins/forge/index.ts +16 -3
  65. package/plugins/form/index.ts +75 -27
  66. package/plugins/form/public/style.css +126 -0
  67. package/plugins/gallery/client.tsx +9 -3
  68. package/plugins/gallery/index.ts +31 -9
  69. package/plugins/gallery/public/style.css +90 -0
  70. package/plugins/grid/index.ts +19 -5
  71. package/plugins/grid/public/style.css +71 -0
  72. package/plugins/iframe/index.ts +20 -23
  73. package/plugins/iframe/public/style.css +13 -0
  74. package/plugins/index.ts +16 -1
  75. package/plugins/mermaid/index.ts +3 -1
  76. package/plugins/mermaid/{style.css → public/style.css} +7 -2
  77. package/plugins/terminal/client.ts +105 -48
  78. package/plugins/terminal/host.ts +131 -29
  79. package/plugins/terminal/index.ts +84 -25
  80. package/plugins/terminal/public/style.css +225 -0
  81. package/plugins/terminal/server.ts +64 -13
  82. package/plugins/terminal/sizes.ts +13 -3
  83. package/plugins/toc/README.md +12 -0
  84. package/plugins/toc/client.ts +58 -29
  85. package/plugins/toc/index.ts +22 -5
  86. package/plugins/toc/public/style.css +93 -0
  87. package/plugins/window/README.md +29 -0
  88. package/plugins/window/index.ts +85 -0
  89. package/skills/whiteboard/SKILL.md +3 -1
  90. package/solid/index.tsx +24 -9
  91. package/src/auth.ts +25 -7
  92. package/src/client/forms.ts +74 -28
  93. package/src/client/markdown.ts +15 -4
  94. package/src/client/page/base.css +1078 -199
  95. package/src/client/page/elsewhere.ts +32 -15
  96. package/src/client/page/fresh.ts +91 -23
  97. package/src/client/page/hold.ts +11 -2
  98. package/src/client/page/live.ts +10 -6
  99. package/src/client/page/main.ts +106 -34
  100. package/src/client/page/orphans.ts +25 -10
  101. package/src/client/page/sort.ts +14 -4
  102. package/src/client/page/sticky.ts +11 -3
  103. package/src/client/page/theme.ts +11 -7
  104. package/src/client/page/waiting.ts +141 -34
  105. package/src/client/page/window.ts +40 -0
  106. package/src/client/reactive.ts +19 -0
  107. package/src/client/read.ts +17 -10
  108. package/src/client/runtime.ts +130 -35
  109. package/src/client/scroll.ts +14 -4
  110. package/src/client/toast.ts +19 -5
  111. package/src/config.ts +20 -5
  112. package/src/define.ts +67 -23
  113. package/src/events.ts +111 -24
  114. package/src/html.ts +6 -2
  115. package/src/icons.ts +4 -2
  116. package/src/load.ts +10 -2
  117. package/src/loader.ts +75 -11
  118. package/src/package.ts +9 -8
  119. package/src/pages/blocks.ts +20 -14
  120. package/src/pages/markdown.ts +113 -34
  121. package/src/pages/render.ts +198 -37
  122. package/src/pages/window.ts +71 -0
  123. package/src/plugins.ts +90 -24
  124. package/src/public.ts +39 -0
  125. package/src/running.ts +8 -2
  126. package/src/serve.ts +29 -7
  127. package/src/server.ts +351 -108
  128. package/src/status.ts +15 -9
  129. package/dist/plugins/callout/style.css +0 -12
  130. package/dist/plugins/chat/style.css +0 -24
  131. package/dist/plugins/code/style.css +0 -29
  132. package/dist/plugins/comments/client.js +0 -10
  133. package/dist/plugins/comments/style.css +0 -40
  134. package/dist/plugins/details/style.css +0 -3
  135. package/dist/plugins/form/style.css +0 -44
  136. package/dist/plugins/gallery/client.js +0 -1
  137. package/dist/plugins/gallery/style.css +0 -27
  138. package/dist/plugins/grid/style.css +0 -25
  139. package/dist/plugins/iframe/client.js +0 -1
  140. package/dist/plugins/iframe/style.css +0 -29
  141. package/dist/plugins/toc/client.js +0 -1
  142. package/dist/plugins/toc/style.css +0 -19
  143. package/plugins/callout/style.css +0 -12
  144. package/plugins/chat/style.css +0 -24
  145. package/plugins/code/style.css +0 -29
  146. package/plugins/comments/style.css +0 -40
  147. package/plugins/details/style.css +0 -3
  148. package/plugins/form/style.css +0 -44
  149. package/plugins/gallery/style.css +0 -27
  150. package/plugins/grid/style.css +0 -25
  151. package/plugins/iframe/client.ts +0 -46
  152. package/plugins/iframe/style.css +0 -29
  153. package/plugins/terminal/style.css +0 -71
  154. package/plugins/toc/style.css +0 -19
  155. /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
  }
@@ -8,3 +8,15 @@ room; else a button in the header opens it. Opt in, in the config:
8
8
  import toc from '@bigmistqke/whiteboard/toc'
9
9
  export default defineConfig({ plugins: [toc()] })
10
10
  ```
11
+
12
+ A page has it when its frontmatter says `toc: true`:
13
+
14
+ ```markdown
15
+ ---
16
+ toc: true
17
+ ---
18
+ # A long page
19
+ ```
20
+
21
+ To have it on every page, `toc({ always: true })`; a page then goes without
22
+ with `toc: false`.
@@ -2,7 +2,7 @@
2
2
  // update; the section being read is the last heading above the top of the view; a section is
3
3
  // marked when an open question (data-wb-open) or an unread message (data-wb-message, or the element
4
4
  // holding one, data-wb-holds) is in it. A click scrolls to the heading, below the header.
5
- import type { Api, PluginMount } from '../../src/define.ts'
5
+ import type { Api, PageHandle, PluginMount } from '../../src/define.ts'
6
6
 
7
7
  const panel = document.createElement('nav')
8
8
  panel.className = 'wb-toc'
@@ -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
@@ -28,24 +29,36 @@ function fits() {
28
29
  return left >= 230
29
30
  }
30
31
 
32
+ let handle: PageHandle
33
+
34
+ /** Does this page have it: what its frontmatter says (`toc:`), and whether it is on everywhere. */
35
+ function wanted() {
36
+ const said = handle.frontmatter().toc?.trim().toLowerCase()
37
+ const always = document.querySelector('meta[name=wb-toc]')?.getAttribute('content') === 'always'
38
+ return always ? said !== 'false' : said === 'true'
39
+ }
40
+
31
41
  function build() {
32
- headings = [...main.querySelectorAll<HTMLElement>(':scope > h2, :scope > h3')]
33
- let h2 = 0, h3 = 0
34
- list.replaceChildren(...headings.map((h, i) => {
35
- // the page's numbers: 1, 1.1, 1.2, 2 (base.css counts ## and ### alike)
36
- const number = h.tagName === 'H2' ? `${++h2}` : `${h2}.${++h3}`
37
- if (h.tagName === 'H2') h3 = 0
38
- const li = document.createElement('li')
39
- li.dataset.level = h.tagName === 'H2' ? '2' : '3'
40
- const a = document.createElement('a')
41
- a.href = '#'
42
- a.dataset.index = String(i)
43
- a.innerHTML = `<span class="wb-toc-number"></span><span class="wb-toc-text"></span>`
44
- a.querySelector('.wb-toc-number')!.textContent = number
45
- a.querySelector('.wb-toc-text')!.textContent = h.textContent ?? ''
46
- li.append(a)
47
- return li
48
- }))
42
+ headings = wanted() ? [...main.querySelectorAll<HTMLElement>(':scope > h2, :scope > h3')] : []
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
+ )
49
62
  const any = headings.length > 1
50
63
  toggle.hidden = !any || fits()
51
64
  panel.hidden = !any || (!fits() && !panel.classList.contains('wb-toc-open'))
@@ -78,7 +91,9 @@ function mark() {
78
91
  if (!badge.dataset.wbHolds!.split(' ').some(id => unread.has(id))) continue
79
92
  const y = badge.getBoundingClientRect().top
80
93
  let at = -1
81
- 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
+ })
82
97
  waiting.add(at)
83
98
  }
84
99
  list.querySelectorAll<HTMLElement>('a').forEach((a, i) => a.toggleAttribute('data-waiting', waiting.has(i)))
@@ -88,19 +103,27 @@ function mark() {
88
103
  function current() {
89
104
  const line = navHeight() + 40
90
105
  let at = 0
91
- headings.forEach((h, i) => { if (h.getBoundingClientRect().top <= line) at = i })
92
- 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))
93
112
  }
94
113
 
95
114
  function go(i: number) {
96
115
  const h = headings[i]
97
116
  if (!h) return
98
117
  scrollTo({ top: Math.max(0, scrollY + h.getBoundingClientRect().top - navHeight() - 12), behavior: 'smooth' })
99
- 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
+ }
100
122
  }
101
123
 
102
- const mount: PluginMount = (root, handle, core) => {
103
- if (handle.kind !== 'page' && handle.kind !== 'file') return
124
+ const mount: PluginMount = (root, page, core) => {
125
+ if (page.kind !== 'page' && page.kind !== 'file') return
126
+ handle = page
104
127
  main = root.querySelector<HTMLElement>('#wb-main') ?? root
105
128
  api = core
106
129
  document.body.append(panel)
@@ -116,14 +139,20 @@ const mount: PluginMount = (root, handle, core) => {
116
139
  panel.classList.toggle('wb-toc-open', open)
117
140
  panel.hidden = !open
118
141
  })
119
- 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
+ })
120
145
  let frame = 0
121
- const soon = (fn: () => void) => () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(fn) }
146
+ const soon = (fn: () => void) => () => {
147
+ cancelAnimationFrame(frame)
148
+ frame = requestAnimationFrame(fn)
149
+ }
122
150
  addEventListener('scroll', soon(current), { passive: true })
123
151
  addEventListener('resize', soon(build))
124
152
  api.onRendered(build)
125
- api.onForms(() => mark())
126
- api.onEvents(() => requestAnimationFrame(mark))
153
+ page.frontmatter.on(build)
154
+ api.forms.on(() => mark())
155
+ api.events.on(() => requestAnimationFrame(mark))
127
156
  build()
128
157
  }
129
158
 
@@ -5,7 +5,10 @@
5
5
  //
6
6
  // import toc from '@bigmistqke/whiteboard/toc'
7
7
  // export default defineConfig({ plugins: [toc()] })
8
- import { packagePath } from '../../src/package.ts'
8
+ //
9
+ // A page has it when its frontmatter says `toc: true`. With `toc({ always: true })`, every page has
10
+ // it, unless its frontmatter says `toc: false`.
11
+ import { builtinPublic } from '../../src/package.ts'
9
12
  import type { Plugin } from '../../src/define.ts'
10
13
 
11
14
  const DOCS = `## Table of contents
@@ -15,11 +18,25 @@ Pages here have a table of contents beside them, made from their \`##\` and
15
18
  in it. A section with an unanswered question or an unread message has a dot.
16
19
  `
17
20
 
18
- export default function toc(): Plugin {
21
+ export interface TocOptions {
22
+ /** on every page, not only the ones whose frontmatter says `toc: true` (`toc: false` still goes without) */
23
+ always?: boolean
24
+ }
25
+
26
+ export default function toc(options: TocOptions = {}): Plugin {
19
27
  return {
20
28
  name: 'toc',
21
- docs: () => DOCS,
22
- style: packagePath('dist/plugins/toc/style.css'),
23
- setup: () => ({ client: packagePath('dist/plugins/toc/client.js') }),
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',
36
+ setup: () => ({
37
+ client: 'client.js',
38
+ // how the browser part knows: on every page, or only where the frontmatter asks
39
+ shell: () => ({ head: `<meta name="wb-toc" content="${options.always ? 'always' : 'opt-in'}">` }),
40
+ }),
24
41
  }
25
42
  }