@bigmistqke/whiteboard 0.4.0 → 0.6.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 (128) hide show
  1. package/README.md +177 -26
  2. package/bin/whiteboard.ts +331 -16
  3. package/dist/bin/whiteboard.js +268 -17
  4. package/dist/client/runtime.js +66 -23
  5. package/dist/plugins/ansi/README.md +36 -0
  6. package/dist/plugins/ansi/index.js +64 -0
  7. package/dist/plugins/ansi/public/style.css +28 -0
  8. package/dist/plugins/badge/README.md +28 -0
  9. package/dist/plugins/badge/index.js +24 -0
  10. package/dist/plugins/badge/public/style.css +30 -0
  11. package/dist/plugins/chat/public/style.css +7 -2
  12. package/dist/plugins/code/README.md +4 -2
  13. package/dist/plugins/code/diff.js +12 -2
  14. package/dist/plugins/draft/README.md +14 -0
  15. package/dist/plugins/draft/index.js +21 -0
  16. package/dist/plugins/draft/public/style.css +21 -0
  17. package/dist/plugins/grid/README.md +12 -12
  18. package/dist/plugins/grid/index.js +7 -19
  19. package/dist/plugins/grid/public/style.css +4 -33
  20. package/dist/plugins/iframe/README.md +7 -7
  21. package/dist/plugins/iframe/index.js +11 -11
  22. package/dist/plugins/iframe/public/style.css +15 -0
  23. package/dist/plugins/index.js +11 -3
  24. package/dist/plugins/lightbox/README.md +22 -0
  25. package/dist/plugins/lightbox/index.js +20 -0
  26. package/dist/plugins/lightbox/public/client.js +4 -0
  27. package/dist/plugins/lightbox/public/style.css +66 -0
  28. package/dist/plugins/steps/README.md +42 -0
  29. package/dist/plugins/steps/index.js +60 -0
  30. package/dist/plugins/steps/public/client.js +1 -0
  31. package/dist/plugins/steps/public/style.css +77 -0
  32. package/dist/plugins/terminal/host.js +9 -3
  33. package/dist/plugins/terminal/public/client.js +1 -1
  34. package/dist/plugins/terminal/sizes.js +2 -1
  35. package/dist/plugins/toc/README.md +21 -3
  36. package/dist/plugins/toc/index.js +8 -2
  37. package/dist/plugins/toc/public/client.js +1 -1
  38. package/dist/plugins/toc/public/style.css +10 -4
  39. package/dist/plugins/window/README.md +34 -14
  40. package/dist/plugins/window/index.js +21 -13
  41. package/dist/src/auth.js +6 -4
  42. package/dist/src/client/page/base.css +61 -1
  43. package/dist/src/client/page/main.js +74 -20
  44. package/dist/src/entries.js +211 -0
  45. package/dist/src/events.js +1 -1
  46. package/dist/src/hub.js +272 -0
  47. package/dist/src/loader.js +12 -3
  48. package/dist/src/package.js +23 -1
  49. package/dist/src/pages/blocks.js +1 -0
  50. package/dist/src/pages/index.js +1 -1
  51. package/dist/src/pages/markdown.js +87 -33
  52. package/dist/src/pages/render.js +157 -28
  53. package/dist/src/pages/window.js +17 -18
  54. package/dist/src/running.js +37 -2
  55. package/dist/src/serve.js +111 -2
  56. package/dist/src/server.js +264 -23
  57. package/package.json +3 -2
  58. package/plugins/ansi/README.md +36 -0
  59. package/plugins/ansi/index.ts +95 -0
  60. package/plugins/ansi/public/style.css +28 -0
  61. package/plugins/badge/README.md +28 -0
  62. package/plugins/badge/index.ts +35 -0
  63. package/plugins/badge/public/style.css +30 -0
  64. package/plugins/chat/public/style.css +7 -2
  65. package/plugins/code/README.md +4 -2
  66. package/plugins/code/diff.ts +14 -2
  67. package/plugins/code/index.ts +3 -1
  68. package/plugins/draft/README.md +14 -0
  69. package/plugins/draft/index.ts +23 -0
  70. package/plugins/draft/public/style.css +21 -0
  71. package/plugins/grid/README.md +12 -12
  72. package/plugins/grid/index.ts +11 -26
  73. package/plugins/grid/public/style.css +4 -33
  74. package/plugins/iframe/README.md +7 -7
  75. package/plugins/iframe/index.ts +15 -15
  76. package/plugins/iframe/public/style.css +15 -0
  77. package/plugins/index.ts +10 -2
  78. package/plugins/lightbox/README.md +22 -0
  79. package/plugins/lightbox/client.ts +99 -0
  80. package/plugins/lightbox/index.ts +25 -0
  81. package/plugins/lightbox/public/style.css +66 -0
  82. package/plugins/mermaid/index.ts +4 -2
  83. package/plugins/steps/README.md +42 -0
  84. package/plugins/steps/client.ts +75 -0
  85. package/plugins/steps/index.ts +84 -0
  86. package/plugins/steps/public/style.css +77 -0
  87. package/plugins/terminal/client.ts +16 -7
  88. package/plugins/terminal/host.ts +12 -6
  89. package/plugins/terminal/sizes.ts +3 -1
  90. package/plugins/toc/README.md +21 -3
  91. package/plugins/toc/client.ts +47 -6
  92. package/plugins/toc/index.ts +19 -6
  93. package/plugins/toc/public/style.css +10 -4
  94. package/plugins/window/README.md +34 -14
  95. package/plugins/window/index.ts +20 -13
  96. package/skills/whiteboard/SKILL.md +18 -10
  97. package/src/auth.ts +8 -5
  98. package/src/client/base.ts +19 -0
  99. package/src/client/forms.ts +2 -1
  100. package/src/client/markdown.ts +2 -1
  101. package/src/client/page/base.css +61 -1
  102. package/src/client/page/elsewhere.ts +72 -16
  103. package/src/client/page/fresh.ts +2 -1
  104. package/src/client/page/main.ts +18 -6
  105. package/src/client/page/sort.ts +2 -1
  106. package/src/client/runtime.ts +62 -25
  107. package/src/define.ts +126 -19
  108. package/src/entries.ts +258 -0
  109. package/src/events.ts +2 -1
  110. package/src/hub.ts +355 -0
  111. package/src/loader.ts +14 -3
  112. package/src/package.ts +22 -0
  113. package/src/pages/blocks.ts +3 -0
  114. package/src/pages/index.ts +6 -4
  115. package/src/pages/markdown.ts +89 -32
  116. package/src/pages/render.ts +181 -28
  117. package/src/pages/window.ts +23 -25
  118. package/src/running.ts +47 -1
  119. package/src/serve.ts +114 -1
  120. package/src/server.ts +332 -26
  121. package/dist/plugins/gallery/README.md +0 -30
  122. package/dist/plugins/gallery/index.js +0 -75
  123. package/dist/plugins/gallery/public/client.js +0 -1
  124. package/dist/plugins/gallery/public/style.css +0 -90
  125. package/plugins/gallery/README.md +0 -30
  126. package/plugins/gallery/client.tsx +0 -70
  127. package/plugins/gallery/index.ts +0 -89
  128. package/plugins/gallery/public/style.css +0 -90
@@ -0,0 +1,66 @@
1
+ /* The lightbox (index.ts, client.ts): pictures in a container composed with it open on a click */
2
+ [data-lightbox] img:not(a img) {
3
+ cursor: zoom-in;
4
+ }
5
+ /* over the page: the picture or video as large as fits, its caption under it */
6
+ .wb-lightbox {
7
+ position: fixed;
8
+ inset: 0;
9
+ z-index: var(--layer-top);
10
+ background: rgb(0 0 0 / 0.86);
11
+ display: grid;
12
+ place-items: center;
13
+ grid-template-rows: 1fr auto;
14
+ padding: var(--space-3xl) var(--space-2xl);
15
+ }
16
+ .wb-lightbox-stage {
17
+ display: grid;
18
+ place-items: center;
19
+ min-height: 0;
20
+ width: 100%;
21
+ height: 100%;
22
+ }
23
+ .wb-lightbox-stage > :is(img, video) {
24
+ max-width: 100%;
25
+ max-height: calc(100vh - 110px);
26
+ object-fit: contain;
27
+ border-radius: var(--radius-sm);
28
+ }
29
+ .wb-lightbox-caption {
30
+ color: #eee;
31
+ text-align: center;
32
+ padding-top: var(--space-lg);
33
+ }
34
+ .wb-lightbox button {
35
+ position: absolute;
36
+ display: grid;
37
+ place-items: center;
38
+ background: rgb(255 255 255 / 0.12);
39
+ color: #fff;
40
+ border: 0;
41
+ width: 44px;
42
+ height: 44px;
43
+ border-radius: 50%;
44
+ padding: 0;
45
+ cursor: pointer;
46
+ }
47
+ .wb-lightbox button:hover {
48
+ background: rgb(255 255 255 / 0.22);
49
+ }
50
+ .wb-lightbox-prev {
51
+ left: 12px;
52
+ top: 50%;
53
+ translate: 0 -50%;
54
+ }
55
+ .wb-lightbox-next {
56
+ right: 12px;
57
+ top: 50%;
58
+ translate: 0 -50%;
59
+ }
60
+ .wb-lightbox button[hidden] {
61
+ display: none;
62
+ }
63
+ .wb-lightbox-close {
64
+ right: 12px;
65
+ top: 12px;
66
+ }
@@ -3,11 +3,13 @@ import { definePlugin, type Plugin } from '@bigmistqke/whiteboard'
3
3
  // Mermaid diagrams rendered to SVG on the server with Pele (https://pele.run): no JavaScript on
4
4
  // the page, and colours come from CSS variables, so diagrams follow the light/dark theme.
5
5
  import crypto from 'node:crypto'
6
- // pele loads the first time a page has a diagram, not with the plugin
7
- let pele: typeof import('pele') | undefined
6
+ import type * as Pele from 'pele'
8
7
  import { html, raw, type FenceContext } from '@bigmistqke/whiteboard'
9
8
  import { builtinPublic } from '../../src/package.ts'
10
9
 
10
+ // loaded the first time a page has a diagram, not with the plugin: the command line loads plugins too
11
+ let pele: typeof Pele | undefined
12
+
11
13
  const FONT = 'ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif'
12
14
 
13
15
  export async function render(ctx: FenceContext) {
@@ -0,0 +1,42 @@
1
+ One pane, one item at a time, with ‹ › between them: an A/B, the steps of a
2
+ procedure, versions of a design. It is a window, so ⤢ fills the browser
3
+ window with it (for going through steps, large).
4
+
5
+ A heading at the block's shallowest heading level starts an item, titled by
6
+ it, and so does a `---`, untitled. With up to four items, all titled, the header shows the
7
+ titles as tabs; else it counts (`2 / 5`) and each item shows its title.
8
+
9
+ The items are laid over each other, so the pane is as tall as the tallest
10
+ and flipping doesn't move the page. ← and → step when it has the focus (a
11
+ click in it) or fills the window. Which item shows stays when the page
12
+ updates, even when a step is added before it.
13
+
14
+ Attributes: `count` (count, never tabs), `wide`, `height`.
15
+
16
+ ```markdown
17
+ :::steps[Two headers]
18
+ ### A
19
+ The label on the left, the buttons on the right.
20
+
21
+ ### B
22
+ Everything centred.
23
+ :::
24
+ ```
25
+
26
+ :::steps[Two headers]
27
+ ### A
28
+ The label on the left, the buttons on the right.
29
+
30
+ ### B
31
+ Everything centred.
32
+ :::
33
+
34
+ ```markdown
35
+ :::steps[The migration]
36
+ ### Copy the table
37
+ `migrate --dry-run` on staging first.
38
+
39
+ ### Switch the reads
40
+ …
41
+ :::
42
+ ```
@@ -0,0 +1,75 @@
1
+ // Steps in the browser: which item shows, by the ‹ › buttons, the tabs and ← →, and kept across
2
+ // the page's updates (which write the block anew): the block by its place among the page's steps,
3
+ // the step by its key (a step the agent adds before it doesn't move what you see). It
4
+ // marks only data-wb-* attributes, which a block's signature leaves out: flipping through items is
5
+ // not a change to the page.
6
+ import type { Api, PageHandle, PluginMount } from '../../src/define'
7
+
8
+ const windows = () => [...document.querySelectorAll<HTMLElement>('#wb-main .wb-steps-window')]
9
+ const items = (win: HTMLElement) => [
10
+ ...win.querySelectorAll<HTMLElement>(':scope > .wb-window-body > .wb-steps > .wb-steps-item'),
11
+ ]
12
+ const marks = (win: HTMLElement) => [...win.querySelectorAll<HTMLElement>(':scope > .wb-window-bar .wb-steps-mark')]
13
+
14
+ /** which item shows, by the block's place: its key (Item.key), so a step added before it keeps it showing, and where it was */
15
+ const at = new Map<number, { key: string | null; index: number }>()
16
+
17
+ /** Where the step it showed is now: the one with its key, else the same place. */
18
+ function current(win: HTMLElement, place: number): number {
19
+ const was = at.get(place)
20
+ if (!was) return 0
21
+ const byKey = was.key ? items(win).findIndex(item => item.dataset.key === was.key) : -1
22
+ return byKey >= 0 ? byKey : was.index
23
+ }
24
+
25
+ function show(win: HTMLElement, place: number, i: number) {
26
+ const all = items(win)
27
+ const n = Math.max(all.length, 1)
28
+ i = Math.min(Math.max(i, 0), n - 1)
29
+ at.set(place, { key: all[i]?.dataset.key ?? null, index: i })
30
+ win.setAttribute('data-wb-step', String(i))
31
+ all.forEach((item, k) => item.toggleAttribute('data-wb-current', k === i))
32
+ marks(win).forEach((mark, k) => mark.toggleAttribute('data-wb-current', k === i))
33
+ win.toggleAttribute('data-wb-first', i === 0)
34
+ win.toggleAttribute('data-wb-last', i === n - 1)
35
+ }
36
+
37
+ const apply = () => windows().forEach((win, place) => show(win, place, current(win, place)))
38
+
39
+ function step(win: HTMLElement, to: 'prev' | 'next' | number) {
40
+ const place = windows().indexOf(win)
41
+ if (place < 0) return
42
+ const now = current(win, place)
43
+ show(win, place, to === 'prev' ? now - 1 : to === 'next' ? now + 1 : to)
44
+ }
45
+
46
+ const typing = (el: Element | null) =>
47
+ !!el?.closest?.('input, textarea, select, [contenteditable]:not([contenteditable="false"])')
48
+
49
+ let listening = false
50
+ const mount: PluginMount = (_el: HTMLElement, _handle: PageHandle, core: Api) => {
51
+ apply()
52
+ core.onRendered(apply)
53
+ if (listening) return
54
+ listening = true
55
+ document.addEventListener('click', e => {
56
+ const button = (e.target as Element).closest?.<HTMLElement>('[data-steps]')
57
+ const win = button?.closest<HTMLElement>('.wb-steps-window')
58
+ if (!button || !win) return
59
+ const to = button.dataset.steps!
60
+ step(win, to === 'prev' || to === 'next' ? to : Number(to))
61
+ })
62
+ document.addEventListener('keydown', e => {
63
+ if ((e.key !== 'ArrowLeft' && e.key !== 'ArrowRight') || e.metaKey || e.ctrlKey || e.altKey) return
64
+ const target = e.target as Element
65
+ if (typing(target)) return
66
+ // the steps that have the focus, or the one filling the window
67
+ const win =
68
+ target.closest?.<HTMLElement>('.wb-steps-window') ??
69
+ document.querySelector<HTMLElement>('#wb-main .wb-steps-window[data-wb-maximized]')
70
+ if (!win) return
71
+ e.preventDefault()
72
+ step(win, e.key === 'ArrowLeft' ? 'prev' : 'next')
73
+ })
74
+ }
75
+ export default mount
@@ -0,0 +1,84 @@
1
+ // Steps: one pane, one item at a time, ‹ › between them: an A/B, a sequence of steps, versions of a
2
+ // design. In a window (src/pages/window.ts), so it fills the browser window too. The items are laid
3
+ // over each other, so the pane is as tall as the tallest and flipping doesn't move the page; which
4
+ // one shows is the browser's (client.ts), kept across the page's updates.
5
+ import fs from 'node:fs'
6
+ import { definePlugin, html, raw, windowFrame, type DirectiveContext, type Plugin } from '@bigmistqke/whiteboard'
7
+ import { builtinPublic } from '../../src/package.ts'
8
+
9
+ /** A height in px (a bare number) or a CSS length; else none. */
10
+ const height = (value: string | undefined) =>
11
+ value && /^\d+(\.\d+)?$/.test(value)
12
+ ? `${value}px`
13
+ : value && /^\d+(\.\d+)?(px|em|rem|vh|svh|dvh)$/.test(value)
14
+ ? value
15
+ : undefined
16
+
17
+ const svg = (d: string) =>
18
+ raw(
19
+ `<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"><path d="${d}"/></svg>`,
20
+ )
21
+ const PREV = svg('M10 3.5 5.5 8l4.5 4.5')
22
+ const NEXT = svg('M6 3.5 10.5 8 6 12.5')
23
+
24
+ /** With this many items or fewer, all titled, the header shows their titles as tabs. */
25
+ const TABS = 4
26
+
27
+ export async function render(ctx: DirectiveContext) {
28
+ // one per item (DirectiveContext.items): a heading, or a `---`, starts one
29
+ const items = await ctx.items()
30
+ const tabs = !('count' in ctx.attrs) && items.length <= TABS && items.every(i => i.title)
31
+ const n = items.length
32
+ const marks = tabs
33
+ ? items.map(
34
+ (item, i) =>
35
+ html`<button type="button" class="wb-steps-tab wb-steps-mark" data-steps="${String(i)}">
36
+ ${raw(item.title!)}
37
+ </button>`,
38
+ )
39
+ : html`<span class="wb-steps-count">
40
+ ${items.map((_, i) => html`<span class="wb-steps-mark">${String(i + 1)} / ${String(n)}</span>`)}
41
+ </span>`
42
+ const controls = html`<span class="wb-steps-nav">
43
+ <button
44
+ type="button"
45
+ class="wb-window-button wb-steps-prev"
46
+ data-steps="prev"
47
+ title="Previous (←)"
48
+ aria-label="Previous"
49
+ >
50
+ ${PREV}
51
+ </button>
52
+ ${marks}
53
+ <button type="button" class="wb-window-button wb-steps-next" data-steps="next" title="Next (→)" aria-label="Next">
54
+ ${NEXT}
55
+ </button>
56
+ </span>`
57
+ const body = html`<div class="wb-steps" tabindex="0">
58
+ ${items.map(
59
+ item =>
60
+ html`<section class="wb-steps-item" data-key="${item.key}">
61
+ ${!tabs && item.title ? html`<div class="wb-steps-title">${raw(item.title)}</div>` : ''} ${raw(item.html)}
62
+ </section>`,
63
+ )}
64
+ </div>`
65
+ return windowFrame({
66
+ label: ctx.label,
67
+ className: 'wb-steps-window',
68
+ controls,
69
+ wide: 'wide' in ctx.attrs,
70
+ height: height(ctx.attrs.height),
71
+ body,
72
+ })
73
+ }
74
+
75
+ export default function steps(): Plugin {
76
+ return definePlugin({
77
+ name: 'steps',
78
+ public: builtinPublic('steps'),
79
+ style: 'style.css',
80
+ setup: () => ({ client: 'client.js' }),
81
+ blocks: { steps: { html: render } },
82
+ docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
83
+ })
84
+ }
@@ -0,0 +1,77 @@
1
+ /* Steps (index.ts): the items over each other in one grid cell, so the pane is as tall as the
2
+ tallest; one shows. Before the browser marks one (data-wb-step), the first. */
3
+ .wb-steps {
4
+ display: grid;
5
+ outline: none;
6
+ }
7
+ .wb-steps:focus-visible {
8
+ box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--accent) var(--tint-strong), transparent);
9
+ }
10
+ .wb-steps > .wb-steps-item {
11
+ grid-area: 1 / 1;
12
+ min-width: 0;
13
+ }
14
+ .wb-steps > .wb-steps-item > :first-child {
15
+ margin-top: 0;
16
+ }
17
+ .wb-steps > .wb-steps-item > :last-child {
18
+ margin-bottom: 0;
19
+ }
20
+ .wb-steps-window:not([data-wb-step]) > .wb-window-body > .wb-steps > .wb-steps-item:not(:first-child),
21
+ .wb-steps-window[data-wb-step] > .wb-window-body > .wb-steps > .wb-steps-item:not([data-wb-current]) {
22
+ visibility: hidden;
23
+ }
24
+ .wb-steps-title {
25
+ font: var(--weight-strong) var(--ui-small) / var(--ui-line-height) var(--sans);
26
+ color: var(--muted);
27
+ letter-spacing: var(--caps-spacing);
28
+ text-transform: uppercase;
29
+ margin-bottom: var(--space-md);
30
+ }
31
+
32
+ /* the header: ‹ 2 / 5 › or ‹ A B › */
33
+ .wb-steps-nav {
34
+ display: flex;
35
+ align-items: center;
36
+ gap: var(--space-2xs);
37
+ flex: none;
38
+ }
39
+ .wb-steps-count {
40
+ font: var(--ui-tiny) / 1 var(--sans);
41
+ font-variant-numeric: tabular-nums;
42
+ color: var(--muted);
43
+ padding: 0 var(--space-2xs);
44
+ }
45
+ .wb-steps-window:not([data-wb-step]) > .wb-window-bar .wb-steps-count > .wb-steps-mark:not(:first-child),
46
+ .wb-steps-window[data-wb-step] > .wb-window-bar .wb-steps-count > .wb-steps-mark:not([data-wb-current]) {
47
+ display: none;
48
+ }
49
+ .wb-steps-tab {
50
+ font: var(--ui-tiny) / 1 var(--sans);
51
+ color: var(--muted);
52
+ background: none;
53
+ border: var(--hairline) solid transparent;
54
+ border-radius: var(--radius-pill);
55
+ padding: var(--space-xs) var(--space-md);
56
+ cursor: pointer;
57
+ white-space: nowrap;
58
+ }
59
+ .wb-steps-tab > * {
60
+ margin: 0;
61
+ }
62
+ .wb-steps-tab:hover {
63
+ color: var(--text);
64
+ background: color-mix(in srgb, var(--text) var(--tint-faint), transparent);
65
+ }
66
+ .wb-steps-window:not([data-wb-step]) > .wb-window-bar .wb-steps-tab:first-of-type,
67
+ .wb-steps-window > .wb-window-bar .wb-steps-tab[data-wb-current] {
68
+ color: var(--accent);
69
+ border-color: color-mix(in srgb, var(--accent) var(--tint-strong), transparent);
70
+ background: color-mix(in srgb, var(--accent) var(--tint-faint), transparent);
71
+ }
72
+ .wb-steps-window:not([data-wb-step]) > .wb-window-bar .wb-steps-prev,
73
+ .wb-steps-window[data-wb-first] > .wb-window-bar .wb-steps-prev,
74
+ .wb-steps-window[data-wb-last] > .wb-window-bar .wb-steps-next {
75
+ opacity: 0.35;
76
+ pointer-events: none;
77
+ }
@@ -33,6 +33,15 @@ interface Session {
33
33
  exited: number | null
34
34
  }
35
35
 
36
+ // the path the project is served under (a shared server serves it below one: see Entries)
37
+ const BASE: string = (() => {
38
+ try {
39
+ return JSON.parse(document.getElementById('wb-page')?.textContent ?? '{}').base ?? ''
40
+ } catch {
41
+ return ''
42
+ }
43
+ })()
44
+
36
45
  function init(drawer: HTMLElement) {
37
46
  const screen = drawer.querySelector<HTMLElement>('.wb-term-screen')!
38
47
  const tabs = drawer.querySelector<HTMLElement>('.wb-term-tabs')!
@@ -97,7 +106,7 @@ function init(drawer: HTMLElement) {
97
106
  const key = `${current} ${dims.cols}x${dims.rows} ${claim}`
98
107
  if (key !== sent) {
99
108
  sent = key
100
- post('/api/terminal/resize', { id: current, viewer, cols: dims.cols, rows: dims.rows, active: claim })
109
+ post(BASE + '/api/terminal/resize', { id: current, viewer, cols: dims.cols, rows: dims.rows, active: claim })
101
110
  // it is ours from now on; the host confirms with a `size`
102
111
  if (claim) {
103
112
  active = viewer
@@ -183,7 +192,7 @@ function init(drawer: HTMLElement) {
183
192
  const data = pending
184
193
  pending = ''
185
194
  chain = chain
186
- .then(() => post('/api/terminal/input', { id, data }))
195
+ .then(() => post(BASE + '/api/terminal/input', { id, data }))
187
196
  .then(
188
197
  () => {},
189
198
  () => {},
@@ -268,7 +277,7 @@ function init(drawer: HTMLElement) {
268
277
  }
269
278
 
270
279
  async function closeTab(id: string) {
271
- await post('/api/terminal/close', { id })
280
+ await post(BASE + '/api/terminal/close', { id })
272
281
  sessions = sessions.filter(x => x.id !== id)
273
282
  if (current === id) select(sessions.at(-1)?.id ?? null)
274
283
  else drawTabs()
@@ -277,7 +286,7 @@ function init(drawer: HTMLElement) {
277
286
  /** Open a tab with the shell, or start tab `id` again as it was. */
278
287
  async function start(body: { id?: string; restart?: boolean }) {
279
288
  setStatus('starting…')
280
- const res = await post('/api/terminal/start', body)
289
+ const res = await post(BASE + '/api/terminal/start', body)
281
290
  const reply = await res.json().catch(() => ({}))
282
291
  if (!res.ok) return setStatus(reply.error ?? 'could not start', [['Try again', () => start(body)]])
283
292
  await loadSessions()
@@ -288,7 +297,7 @@ function init(drawer: HTMLElement) {
288
297
 
289
298
  async function loadSessions() {
290
299
  try {
291
- const res = await fetch('/api/terminal/sessions')
300
+ const res = await fetch(BASE + '/api/terminal/sessions')
292
301
  if (res.ok) ({ sessions } = await res.json())
293
302
  } catch {
294
303
  /* the server is away: the stream reconnects */
@@ -304,7 +313,7 @@ function init(drawer: HTMLElement) {
304
313
  if (!current) showStatus()
305
314
  // without a tab too: the host tells it when one appears (started from the command line, say)
306
315
  stream = new EventSource(
307
- `/api/terminal/stream?${current ? `id=${encodeURIComponent(current)}&` : ''}viewer=${viewer}`,
316
+ `${BASE}/api/terminal/stream?${current ? `id=${encodeURIComponent(current)}&` : ''}viewer=${viewer}`,
308
317
  )
309
318
  stream.addEventListener('sessions', e => {
310
319
  sessions = JSON.parse((e as MessageEvent).data).sessions
@@ -336,7 +345,7 @@ function init(drawer: HTMLElement) {
336
345
  // a program in this tab asked to show a page here (`whiteboard terminal open`): the drawer comes along
337
346
  stream.addEventListener('navigate', e => {
338
347
  const { path } = JSON.parse((e as MessageEvent).data)
339
- if (typeof path === 'string' && path.startsWith('/') && !path.startsWith('//')) location.assign(path)
348
+ if (typeof path === 'string' && path.startsWith('/') && !path.startsWith('//')) location.assign(BASE + path)
340
349
  })
341
350
  // the session's size changed, or another drawer became the active one
342
351
  stream.addEventListener('size', e => {
@@ -40,9 +40,10 @@ import fs from 'node:fs'
40
40
  import net from 'node:net'
41
41
  import path from 'node:path'
42
42
  import { createRequire } from 'node:module'
43
+ import type * as NodePty from 'node-pty'
43
44
  import type { IPty } from 'node-pty'
44
- import type { Terminal as HeadlessTerminal } from '@xterm/headless'
45
- import type { SerializeAddon as Serializer } from '@xterm/addon-serialize'
45
+ import xtermHeadless, { type Terminal as HeadlessTerminal } from '@xterm/headless'
46
+ import xtermSerialize, { type SerializeAddon as Serializer } from '@xterm/addon-serialize'
46
47
  import { followMouseEncoding } from './modes.ts'
47
48
  import { handOver, lastUsed, leave, report, type Sizes } from './sizes.ts'
48
49
 
@@ -53,8 +54,12 @@ if (!dir || !socket) {
53
54
  process.exit(2)
54
55
  }
55
56
 
56
- /** node-pty. Its prebuilt helper ships without execute permission (1.1.0): add it. */
57
- function loadPty(): typeof import('node-pty') {
57
+ /**
58
+ * node-pty, loaded when a session starts rather than imported: a native module that may not load on
59
+ * this machine, which then is an error to report, not a host that fails to start. Its prebuilt
60
+ * helper ships without execute permission (1.1.0): add it.
61
+ */
62
+ function loadPty(): typeof NodePty {
58
63
  const pty = require('node-pty')
59
64
  const helper = path.join(
60
65
  path.dirname(require.resolve('node-pty/package.json')),
@@ -68,8 +73,9 @@ function loadPty(): typeof import('node-pty') {
68
73
  return pty
69
74
  }
70
75
 
71
- const { Terminal } = (await import('@xterm/headless')).default
72
- const { SerializeAddon } = (await import('@xterm/addon-serialize')).default
76
+ // CommonJS packages: their exports are the default's
77
+ const { Terminal } = xtermHeadless
78
+ const { SerializeAddon } = xtermSerialize
73
79
 
74
80
  /** A program in a pseudo-terminal, and the headless terminal that keeps its screen. */
75
81
  interface Session extends Sizes {
@@ -47,7 +47,9 @@ export function leave(s: Sizes, viewer: string): boolean {
47
47
  */
48
48
  export function handOver(s: Sizes, viewer: string): { cols: number; rows: number } | null {
49
49
  if (s.active !== viewer || s.viewers.has(viewer)) return null
50
- const next = [...s.viewers].sort((a, b) => b[1].used - a[1].used)[0]
50
+ // the one used last: the first of them, as a stable sort by `used` would give
51
+ let next: [string, { cols: number; rows: number; used: number }] | undefined
52
+ for (const viewer of s.viewers) if (!next || viewer[1].used > next[1].used) next = viewer
51
53
  s.active = next?.[0] ?? null
52
54
  return next ? { cols: next[1].cols, rows: next[1].rows } : null
53
55
  }
@@ -9,14 +9,32 @@ import toc from '@bigmistqke/whiteboard/toc'
9
9
  export default defineConfig({ plugins: [toc()] })
10
10
  ```
11
11
 
12
- A page has it when its frontmatter says `toc: true`:
12
+ A page has it when its frontmatter asks, under `plugins:` (where every
13
+ plugin's settings for a page go, by the plugin's name):
13
14
 
14
15
  ```markdown
15
16
  ---
16
- toc: true
17
+ plugins:
18
+ toc: true
17
19
  ---
18
20
  # A long page
19
21
  ```
20
22
 
21
23
  To have it on every page, `toc({ always: true })`; a page then goes without
22
- with `toc: false`.
24
+ with `toc: false` there.
25
+
26
+ The panel fits its items, between a minimum and a maximum width (160px and
27
+ 320px by default). A project sets its own in the options, and a page in its
28
+ frontmatter, which is then also how it asks for the contents:
29
+
30
+ ```ts
31
+ toc({ minWidth: 180, maxWidth: '24rem' }) // pixels, or a CSS length
32
+ ```
33
+
34
+ ```markdown
35
+ ---
36
+ plugins:
37
+ toc:
38
+ maxWidth: 240px
39
+ ---
40
+ ```
@@ -23,19 +23,58 @@ let headings: HTMLElement[] = []
23
23
 
24
24
  const navHeight = () => document.querySelector<HTMLElement>('.wb-nav')?.getBoundingClientRect().height ?? 0
25
25
 
26
- /** Is there room left of the text for the panel? Else it opens from the header. */
26
+ /** The gap between the panel and the text, and between the panel and the window's edge. */
27
+ const GAP = 40
28
+ const EDGE = 16
29
+
30
+ /** Is there room left of the text for the panel at its narrowest? Else it opens from the header. */
27
31
  function fits() {
32
+ const narrowest = parseFloat(getComputedStyle(panel).minWidth) || 0
33
+ return main.getBoundingClientRect().left >= narrowest + GAP + EDGE
34
+ }
35
+
36
+ /** A width as the settings or the options give it: a CSS length, or a number of pixels. */
37
+ const length = (value: unknown): string | null => {
38
+ const m = /^(\d+(?:\.\d+)?)(px|em|rem|ch|vw)?$/.exec(String(value ?? '').trim())
39
+ return m && Number(m[1]) > 0 ? m[1] + (m[2] ?? 'px') : null
40
+ }
41
+
42
+ /**
43
+ * How wide the panel may be: it fits its items, between a minimum and a maximum, which the page's
44
+ * settings can set (`plugins: { toc: { minWidth: 160px, maxWidth: 320px } }`), else the project's options.
45
+ */
46
+ function bounds() {
47
+ const meta = document.querySelector<HTMLMetaElement>('meta[name=wb-toc]')
48
+ const said = handle.settings()
49
+ const own = said && typeof said === 'object' ? (said as Record<string, unknown>) : {}
50
+ for (const [name, prop] of [
51
+ ['minWidth', '--wb-toc-min-width'],
52
+ ['maxWidth', '--wb-toc-max-width'],
53
+ ] as const) {
54
+ const value = length(own[name]) ?? length(meta?.dataset[name])
55
+ if (value) panel.style.setProperty(prop, value)
56
+ else panel.style.removeProperty(prop)
57
+ }
58
+ }
59
+
60
+ /** Docked: its right edge a gap left of the text, so a wider panel grows away from it. */
61
+ function place() {
28
62
  const left = main.getBoundingClientRect().left
29
- return left >= 230
63
+ panel.style.setProperty('--wb-toc-right', `${document.documentElement.clientWidth - left + GAP}px`)
64
+ panel.style.setProperty('--wb-toc-room', `${left - GAP - EDGE}px`)
30
65
  }
31
66
 
32
67
  let handle: PageHandle
33
68
 
34
- /** Does this page have it: what its frontmatter says (`toc:`), and whether it is on everywhere. */
69
+ /** Does this page have it: what its settings say, and whether it is on everywhere. */
35
70
  function wanted() {
36
- const said = handle.frontmatter().toc?.trim().toLowerCase()
71
+ // its settings (`plugins: { toc: … }`): true, or { minWidth, maxWidth }: on; false: off; else as
72
+ // the project says
73
+ const said = handle.settings()
37
74
  const always = document.querySelector('meta[name=wb-toc]')?.getAttribute('content') === 'always'
38
- return always ? said !== 'false' : said === 'true'
75
+ if (said === false) return false
76
+ if (said === true || (said && typeof said === 'object')) return true
77
+ return always
39
78
  }
40
79
 
41
80
  function build() {
@@ -60,6 +99,8 @@ function build() {
60
99
  }),
61
100
  )
62
101
  const any = headings.length > 1
102
+ bounds()
103
+ place()
63
104
  toggle.hidden = !any || fits()
64
105
  panel.hidden = !any || (!fits() && !panel.classList.contains('wb-toc-open'))
65
106
  panel.classList.toggle('wb-toc-docked', fits())
@@ -150,7 +191,7 @@ const mount: PluginMount = (root, page, core) => {
150
191
  addEventListener('scroll', soon(current), { passive: true })
151
192
  addEventListener('resize', soon(build))
152
193
  api.onRendered(build)
153
- page.frontmatter.on(build)
194
+ page.settings.on(build)
154
195
  api.forms.on(() => mark())
155
196
  api.events.on(() => requestAnimationFrame(mark))
156
197
  build()
@@ -6,9 +6,10 @@
6
6
  // import toc from '@bigmistqke/whiteboard/toc'
7
7
  // export default defineConfig({ plugins: [toc()] })
8
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`.
9
+ // A page has it when its frontmatter says so, under `plugins:` (`plugins: { toc: true }`). With
10
+ // `toc({ always: true })`, every page has it, unless its frontmatter says `toc: false` there.
11
11
  import { builtinPublic } from '../../src/package.ts'
12
+ import { html } from '../../src/html.ts'
12
13
  import type { Plugin } from '../../src/define.ts'
13
14
 
14
15
  const DOCS = `## Table of contents
@@ -19,8 +20,12 @@ in it. A section with an unanswered question or an unread message has a dot.
19
20
  `
20
21
 
21
22
  export interface TocOptions {
22
- /** on every page, not only the ones whose frontmatter says `toc: true` (`toc: false` still goes without) */
23
+ /** on every page, not only the ones that ask (`plugins: { toc: true }`); `toc: false` there still goes without */
23
24
  always?: boolean
25
+ /** the panel fits its items, but never narrower than this: a CSS length, or pixels (default 160px) */
26
+ minWidth?: string | number
27
+ /** nor wider than this (default 320px); a page can say otherwise: `plugins: { toc: { maxWidth: 280px } }` */
28
+ maxWidth?: string | number
24
29
  }
25
30
 
26
31
  export default function toc(options: TocOptions = {}): Plugin {
@@ -28,15 +33,23 @@ export default function toc(options: TocOptions = {}): Plugin {
28
33
  name: 'toc',
29
34
  docs: () =>
30
35
  options.always
31
- ? DOCS + '\nIn this project every page has it; a page without it says `toc: false` in its frontmatter.\n'
36
+ ? DOCS +
37
+ '\nIn this project every page has it; a page goes without with `toc: false` under `plugins:` in its frontmatter:\n\n```markdown\n---\nplugins:\n toc: false\n---\n```\n'
32
38
  : 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',
39
+ '\nA page has it when its frontmatter asks, under `plugins:`:\n\n```markdown\n---\nplugins:\n toc: true\n---\n# A long page\n```\n',
34
40
  public: builtinPublic('toc'),
35
41
  style: 'style.css',
36
42
  setup: () => ({
37
43
  client: 'client.js',
38
44
  // 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'}">` }),
45
+ shell: () => ({
46
+ head: html`<meta
47
+ name="wb-toc"
48
+ content="${options.always ? 'always' : 'opt-in'}"
49
+ ${options.minWidth != null ? html` data-min-width="${String(options.minWidth)}"` : ''}
50
+ ${options.maxWidth != null ? html` data-max-width="${String(options.maxWidth)}"` : ''}
51
+ />`,
52
+ }),
40
53
  }),
41
54
  }
42
55
  }