@bigmistqke/whiteboard 0.3.1 → 0.5.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 (174) hide show
  1. package/README.md +119 -18
  2. package/bin/whiteboard.ts +300 -71
  3. package/dist/bin/whiteboard.js +21 -0
  4. package/dist/client/runtime.js +132 -54
  5. package/dist/define.js +21 -2
  6. package/dist/plugins/callout/index.js +3 -1
  7. package/dist/plugins/callout/public/style.css +30 -0
  8. package/dist/plugins/chat/index.js +7 -4
  9. package/dist/plugins/chat/{client.js → public/client.js} +1 -1
  10. package/dist/plugins/chat/public/style.css +98 -0
  11. package/dist/plugins/code/index.js +3 -1
  12. package/dist/plugins/code/public/style.css +68 -0
  13. package/dist/plugins/comments/index.js +4 -5
  14. package/dist/plugins/comments/public/client.js +19 -0
  15. package/dist/plugins/comments/public/style.css +185 -0
  16. package/dist/plugins/details/index.js +7 -2
  17. package/dist/plugins/details/public/style.css +7 -0
  18. package/dist/plugins/draft/README.md +14 -0
  19. package/dist/plugins/draft/index.js +21 -0
  20. package/dist/plugins/draft/public/style.css +21 -0
  21. package/dist/plugins/form/index.js +37 -8
  22. package/dist/plugins/form/public/style.css +126 -0
  23. package/dist/plugins/gallery/index.js +4 -3
  24. package/dist/plugins/gallery/public/client.js +1 -0
  25. package/dist/plugins/gallery/public/style.css +90 -0
  26. package/dist/plugins/grid/index.js +9 -3
  27. package/dist/plugins/grid/public/style.css +71 -0
  28. package/dist/plugins/iframe/index.js +13 -14
  29. package/dist/plugins/iframe/public/style.css +13 -0
  30. package/dist/plugins/index.js +7 -1
  31. package/dist/plugins/mermaid/index.js +3 -1
  32. package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
  33. package/dist/plugins/steps/README.md +42 -0
  34. package/dist/plugins/steps/index.js +99 -0
  35. package/dist/plugins/steps/public/client.js +1 -0
  36. package/dist/plugins/steps/public/style.css +77 -0
  37. package/dist/plugins/terminal/host.js +9 -3
  38. package/dist/plugins/terminal/index.js +10 -9
  39. package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
  40. package/dist/plugins/terminal/sizes.js +2 -1
  41. package/dist/plugins/toc/README.md +21 -3
  42. package/dist/plugins/toc/index.js +12 -5
  43. package/dist/plugins/toc/public/client.js +1 -0
  44. package/dist/plugins/toc/public/style.css +99 -0
  45. package/dist/plugins/window/README.md +29 -0
  46. package/dist/plugins/window/index.js +64 -0
  47. package/dist/solid/index.js +14 -11
  48. package/dist/src/auth.js +2 -1
  49. package/dist/src/client/page/base.css +1078 -199
  50. package/dist/src/client/page/main.js +188 -39
  51. package/dist/src/loader.js +14 -3
  52. package/dist/src/package.js +3 -6
  53. package/dist/src/pages/blocks.js +8 -13
  54. package/dist/src/pages/markdown.js +1 -1
  55. package/dist/src/pages/render.js +106 -20
  56. package/dist/src/pages/window.js +31 -0
  57. package/dist/src/public.js +24 -0
  58. package/dist/src/server.js +251 -23
  59. package/package.json +5 -2
  60. package/plugins/callout/index.ts +3 -1
  61. package/plugins/callout/public/style.css +30 -0
  62. package/plugins/chat/autosize.ts +43 -9
  63. package/plugins/chat/client.tsx +22 -5
  64. package/plugins/chat/index.ts +22 -8
  65. package/plugins/chat/public/style.css +98 -0
  66. package/plugins/claude-code/index.ts +49 -14
  67. package/plugins/code/diff.ts +28 -8
  68. package/plugins/code/index.ts +33 -21
  69. package/plugins/code/public/style.css +68 -0
  70. package/plugins/comments/client.ts +139 -71
  71. package/plugins/comments/index.ts +9 -7
  72. package/plugins/comments/public/style.css +185 -0
  73. package/plugins/details/index.ts +7 -2
  74. package/plugins/details/public/style.css +7 -0
  75. package/plugins/draft/README.md +14 -0
  76. package/plugins/draft/index.ts +23 -0
  77. package/plugins/draft/public/style.css +21 -0
  78. package/plugins/forge/index.ts +16 -3
  79. package/plugins/form/index.ts +75 -27
  80. package/plugins/form/public/style.css +126 -0
  81. package/plugins/gallery/client.tsx +9 -3
  82. package/plugins/gallery/index.ts +31 -9
  83. package/plugins/gallery/public/style.css +90 -0
  84. package/plugins/grid/index.ts +19 -5
  85. package/plugins/grid/public/style.css +71 -0
  86. package/plugins/iframe/index.ts +20 -23
  87. package/plugins/iframe/public/style.css +13 -0
  88. package/plugins/index.ts +20 -1
  89. package/plugins/mermaid/index.ts +7 -3
  90. package/plugins/mermaid/{style.css → public/style.css} +7 -2
  91. package/plugins/steps/README.md +42 -0
  92. package/plugins/steps/client.ts +66 -0
  93. package/plugins/steps/index.ts +127 -0
  94. package/plugins/steps/public/style.css +77 -0
  95. package/plugins/terminal/client.ts +105 -48
  96. package/plugins/terminal/host.ts +143 -35
  97. package/plugins/terminal/index.ts +84 -25
  98. package/plugins/terminal/public/style.css +225 -0
  99. package/plugins/terminal/server.ts +64 -13
  100. package/plugins/terminal/sizes.ts +16 -4
  101. package/plugins/toc/README.md +21 -3
  102. package/plugins/toc/client.ts +90 -30
  103. package/plugins/toc/index.ts +26 -10
  104. package/plugins/toc/public/style.css +99 -0
  105. package/plugins/window/README.md +29 -0
  106. package/plugins/window/index.ts +85 -0
  107. package/skills/whiteboard/SKILL.md +3 -1
  108. package/solid/index.tsx +24 -9
  109. package/src/auth.ts +28 -9
  110. package/src/client/forms.ts +74 -28
  111. package/src/client/markdown.ts +15 -4
  112. package/src/client/page/base.css +1078 -199
  113. package/src/client/page/elsewhere.ts +32 -15
  114. package/src/client/page/fresh.ts +91 -23
  115. package/src/client/page/hold.ts +11 -2
  116. package/src/client/page/live.ts +10 -6
  117. package/src/client/page/main.ts +124 -42
  118. package/src/client/page/orphans.ts +25 -10
  119. package/src/client/page/sort.ts +14 -4
  120. package/src/client/page/sticky.ts +11 -3
  121. package/src/client/page/theme.ts +11 -7
  122. package/src/client/page/waiting.ts +141 -34
  123. package/src/client/page/window.ts +40 -0
  124. package/src/client/reactive.ts +19 -0
  125. package/src/client/read.ts +17 -10
  126. package/src/client/runtime.ts +170 -40
  127. package/src/client/scroll.ts +14 -4
  128. package/src/client/toast.ts +19 -5
  129. package/src/config.ts +20 -5
  130. package/src/define.ts +119 -31
  131. package/src/events.ts +111 -24
  132. package/src/html.ts +6 -2
  133. package/src/icons.ts +4 -2
  134. package/src/load.ts +10 -2
  135. package/src/loader.ts +81 -12
  136. package/src/package.ts +9 -8
  137. package/src/pages/blocks.ts +20 -14
  138. package/src/pages/index.ts +2 -2
  139. package/src/pages/markdown.ts +113 -34
  140. package/src/pages/render.ts +233 -41
  141. package/src/pages/window.ts +78 -0
  142. package/src/plugins.ts +90 -24
  143. package/src/public.ts +39 -0
  144. package/src/running.ts +8 -2
  145. package/src/serve.ts +29 -7
  146. package/src/server.ts +591 -110
  147. package/src/status.ts +15 -9
  148. package/dist/plugins/callout/style.css +0 -12
  149. package/dist/plugins/chat/style.css +0 -24
  150. package/dist/plugins/code/style.css +0 -29
  151. package/dist/plugins/comments/client.js +0 -10
  152. package/dist/plugins/comments/style.css +0 -40
  153. package/dist/plugins/details/style.css +0 -3
  154. package/dist/plugins/form/style.css +0 -44
  155. package/dist/plugins/gallery/client.js +0 -1
  156. package/dist/plugins/gallery/style.css +0 -27
  157. package/dist/plugins/grid/style.css +0 -25
  158. package/dist/plugins/iframe/client.js +0 -1
  159. package/dist/plugins/iframe/style.css +0 -29
  160. package/dist/plugins/toc/client.js +0 -1
  161. package/dist/plugins/toc/style.css +0 -19
  162. package/plugins/callout/style.css +0 -12
  163. package/plugins/chat/style.css +0 -24
  164. package/plugins/code/style.css +0 -29
  165. package/plugins/comments/style.css +0 -40
  166. package/plugins/details/style.css +0 -3
  167. package/plugins/form/style.css +0 -44
  168. package/plugins/gallery/style.css +0 -27
  169. package/plugins/grid/style.css +0 -25
  170. package/plugins/iframe/client.ts +0 -46
  171. package/plugins/iframe/style.css +0 -29
  172. package/plugins/terminal/style.css +0 -71
  173. package/plugins/toc/style.css +0 -19
  174. /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
package/src/public.ts ADDED
@@ -0,0 +1,39 @@
1
+ // A plugin's public folder: the one place its browser files are served from (its client modules,
2
+ // stylesheets, images, fonts; TypeScript there is served with its types stripped). The server
3
+ // serves what is inside a plugin's public folder, and nothing else of the plugin: not its server
4
+ // code, not the project's files beside it.
5
+ //
6
+ // A plugin says where (`public`); definePlugin defaults it to `public/` next to the module that
7
+ // calls it; a plugin that is a plain object (one written inline in the config, say) has the
8
+ // project's `.whiteboard/public/`. Its files (`client`, `style`, a block's) are named within it.
9
+ import path from 'node:path'
10
+ import { fileURLToPath } from 'node:url'
11
+ import type { Plugin } from './define.ts'
12
+
13
+ const asPath = (ref: URL | string): string => (ref instanceof URL || ref.startsWith('file:') ? fileURLToPath(ref) : ref)
14
+
15
+ /** The folder a plugin's browser files are served from. */
16
+ export function publicDirOf(plugin: Plugin, wb: string): string {
17
+ return path.resolve(plugin.public != null ? asPath(plugin.public) : path.join(wb, 'public'))
18
+ }
19
+
20
+ /** Is `file` inside `dir`? */
21
+ export const within = (file: string, dir: string): boolean => {
22
+ const rel = path.relative(dir, file)
23
+ return !!rel && !rel.startsWith('..') && !path.isAbsolute(rel)
24
+ }
25
+
26
+ /**
27
+ * A file a plugin names, as a path: a name within its public folder (`style.css`,
28
+ * `client/main.js`), or a URL or absolute path, which must be inside it. Anything else is an
29
+ * error, said when the config loads, rather than a file that quietly isn't served.
30
+ */
31
+ export function inPublic(dir: string, ref: URL | string, what: string): string {
32
+ const file =
33
+ typeof ref === 'string' && !ref.startsWith('file:') && !path.isAbsolute(ref)
34
+ ? path.resolve(dir, ref)
35
+ : path.resolve(asPath(ref))
36
+ if (!within(file, dir))
37
+ throw new Error(`${what}: ${file} is not in its public folder (${dir}); a plugin's browser files live there`)
38
+ return file
39
+ }
package/src/running.ts CHANGED
@@ -6,7 +6,11 @@ import type { Project } from './config.ts'
6
6
 
7
7
  /** The port the last server of this project recorded, or null. */
8
8
  export function recordedPort(project: Project): number | null {
9
- try { return Number(JSON.parse(fs.readFileSync(project.serverFile, 'utf8')).port) || null } catch { return null }
9
+ try {
10
+ return Number(JSON.parse(fs.readFileSync(project.serverFile, 'utf8')).port) || null
11
+ } catch {
12
+ return null
13
+ }
10
14
  }
11
15
 
12
16
  /** The base URL of this project's running server, or null. `except`: a process to not count (the asking server itself). */
@@ -18,5 +22,7 @@ export async function runningServer(project: Project, { except }: { except?: num
18
22
  const res = await fetch(`${base}/api/health`, { signal: AbortSignal.timeout(800) })
19
23
  const health = res.ok ? await res.json() : null
20
24
  return health?.dir === project.dir && health.pid !== except ? base : null
21
- } catch { return null }
25
+ } catch {
26
+ return null
27
+ }
22
28
  }
package/src/serve.ts CHANGED
@@ -19,12 +19,26 @@ export async function start(project: Project): Promise<number> {
19
19
  fs.mkdirSync(project.pagesDir, { recursive: true })
20
20
  const wb = createServer(project)
21
21
  await wb.config()
22
- const server = http.createServer((req, res) => { void wb.handle(req, res) })
23
- const candidates = [...new Set([recordedPort(project), DEFAULT_PORT, ...Array.from({ length: 50 }, (_, i) => DEFAULT_PORT + 1 + i)].filter((p): p is number => !!p))]
22
+ const server = http.createServer((req, res) => {
23
+ void wb.handle(req, res)
24
+ })
25
+ const candidates = [
26
+ ...new Set(
27
+ [recordedPort(project), DEFAULT_PORT, ...Array.from({ length: 50 }, (_, i) => DEFAULT_PORT + 1 + i)].filter(
28
+ (p): p is number => !!p,
29
+ ),
30
+ ),
31
+ ]
24
32
  for (const port of candidates) {
25
33
  const ok = await new Promise<boolean>((resolve, reject) => {
26
- const onError = (err: NodeJS.ErrnoException) => { server.off('listening', onListening); err.code === 'EADDRINUSE' ? resolve(false) : reject(err) }
27
- const onListening = () => { server.off('error', onError); resolve(true) }
34
+ const onError = (err: NodeJS.ErrnoException) => {
35
+ server.off('listening', onListening)
36
+ err.code === 'EADDRINUSE' ? resolve(false) : reject(err)
37
+ }
38
+ const onListening = () => {
39
+ server.off('error', onError)
40
+ resolve(true)
41
+ }
28
42
  server.once('error', onError)
29
43
  server.once('listening', onListening)
30
44
  server.listen(port, '127.0.0.1')
@@ -32,15 +46,23 @@ export async function start(project: Project): Promise<number> {
32
46
  if (!ok) continue
33
47
  fs.writeFileSync(project.serverFile, JSON.stringify({ port, pid: process.pid }, null, 2) + '\n')
34
48
  // the command that started this server waits for it; others can now find it
35
- try { fs.unlinkSync(path.join(project.wb, 'server.lock')) } catch {}
49
+ try {
50
+ fs.unlinkSync(path.join(project.wb, 'server.lock'))
51
+ } catch {}
36
52
  const cleanup = () => {
37
- try { if (JSON.parse(fs.readFileSync(project.serverFile, 'utf8')).pid === process.pid) fs.unlinkSync(project.serverFile) } catch {}
53
+ try {
54
+ if (JSON.parse(fs.readFileSync(project.serverFile, 'utf8')).pid === process.pid)
55
+ fs.unlinkSync(project.serverFile)
56
+ } catch {}
38
57
  }
39
58
  process.on('exit', cleanup)
40
59
  // one server per project: should another have started at the same moment, whichever one
41
60
  // server.json names last stays, and the others leave (their clients reconnect to it)
42
61
  const yieldToOther = async () => {
43
- if (await runningServer(project, { except: process.pid })) { console.log('whiteboard: another server of this project took over'); process.exit(0) }
62
+ if (await runningServer(project, { except: process.pid })) {
63
+ console.log('whiteboard: another server of this project took over')
64
+ process.exit(0)
65
+ }
44
66
  }
45
67
  setTimeout(yieldToOther, 1500).unref()
46
68
  setInterval(yieldToOther, 30000).unref()