@bigmistqke/whiteboard 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugins/toc/README.md +12 -0
- package/dist/plugins/toc/client.js +1 -1
- package/dist/plugins/toc/index.js +6 -3
- package/dist/src/client/page/main.js +1 -0
- package/dist/src/pages/render.js +2 -1
- package/dist/src/server.js +1 -1
- package/package.json +1 -1
- package/plugins/toc/README.md +12 -0
- package/plugins/toc/client.ts +14 -4
- package/plugins/toc/index.ts +17 -3
- package/src/client/page/main.ts +2 -0
- package/src/define.ts +5 -1
- package/src/pages/render.ts +2 -1
- package/src/server.ts +4 -2
|
@@ -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`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=document.createElement(`nav`);e.className=`wb-toc`,e.setAttribute(`aria-label`,`Contents`);const t=document.createElement(`ol`);e.append(t);const n=document.createElement(`button`);n.type=`button`,n.className=`wb-toc-toggle`,n.title=n.ariaLabel=`Contents`,n.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>`;let r,i,a=[];const o=()=>document.querySelector(`.wb-nav`)?.getBoundingClientRect().height??0;function s(){return r.getBoundingClientRect().left>=230}function c(){a=[...r.querySelectorAll(`:scope > h2, :scope > h3`)];let i=0,o=0;t.replaceChildren(...a.map((e,t)=>{let n=e.tagName===`H2`?`${++i}`:`${i}.${++o}`;e.tagName===`H2`&&(o=0);let r=document.createElement(`li`);r.dataset.level=e.tagName===`H2`?`2`:`3`;let a=document.createElement(`a`);return a.href=`#`,a.dataset.index=String(t),a.innerHTML=`<span class="wb-toc-number"></span><span class="wb-toc-text"></span>`,a.querySelector(`.wb-toc-number`).textContent=n,a.querySelector(`.wb-toc-text`).textContent=e.textContent??``,r.append(a),r}));let c=a.length>1;n.hidden=!c||s(),e.hidden=!c||!s()&&!e.classList.contains(`wb-toc-open`),e.classList.toggle(`wb-toc-docked`,s()),
|
|
1
|
+
const e=document.createElement(`nav`);e.className=`wb-toc`,e.setAttribute(`aria-label`,`Contents`);const t=document.createElement(`ol`);e.append(t);const n=document.createElement(`button`);n.type=`button`,n.className=`wb-toc-toggle`,n.title=n.ariaLabel=`Contents`,n.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>`;let r,i,a=[];const o=()=>document.querySelector(`.wb-nav`)?.getBoundingClientRect().height??0;function s(){return r.getBoundingClientRect().left>=230}let c;function l(){let e=(c.data?.frontmatter??{}).toc?.trim().toLowerCase();return document.querySelector(`meta[name=wb-toc]`)?.getAttribute(`content`)===`always`?e!==`false`:e===`true`}function u(){a=l()?[...r.querySelectorAll(`:scope > h2, :scope > h3`)]:[];let i=0,o=0;t.replaceChildren(...a.map((e,t)=>{let n=e.tagName===`H2`?`${++i}`:`${i}.${++o}`;e.tagName===`H2`&&(o=0);let r=document.createElement(`li`);r.dataset.level=e.tagName===`H2`?`2`:`3`;let a=document.createElement(`a`);return a.href=`#`,a.dataset.index=String(t),a.innerHTML=`<span class="wb-toc-number"></span><span class="wb-toc-text"></span>`,a.querySelector(`.wb-toc-number`).textContent=n,a.querySelector(`.wb-toc-text`).textContent=e.textContent??``,r.append(a),r}));let c=a.length>1;n.hidden=!c||s(),e.hidden=!c||!s()&&!e.classList.contains(`wb-toc-open`),e.classList.toggle(`wb-toc-docked`,s()),f(),p()}function d(e){let t=-1;for(let n=0;n<a.length&&a[n].compareDocumentPosition(e)&Node.DOCUMENT_POSITION_FOLLOWING;n++)t=n;return t}function f(){let e=/* @__PURE__ */ new Set,n=new Set(i.unread().map(e=>e.id)),o=[...r.querySelectorAll(`form[data-wb-open]`),...[...r.querySelectorAll(`[data-wb-message]`)].filter(e=>n.has(e.dataset.wbMessage))];for(let t of o)e.add(d(t));for(let t of document.querySelectorAll(`[data-wb-holds]`)){if(!t.dataset.wbHolds.split(` `).some(e=>n.has(e)))continue;let r=t.getBoundingClientRect().top,i=-1;a.forEach((e,t)=>{e.getBoundingClientRect().top<=r&&(i=t)}),e.add(i)}t.querySelectorAll(`a`).forEach((t,n)=>t.toggleAttribute(`data-waiting`,e.has(n)))}function p(){let e=o()+40,n=0;a.forEach((t,r)=>{t.getBoundingClientRect().top<=e&&(n=r)}),t.querySelectorAll(`a`).forEach((e,t)=>e.toggleAttribute(`aria-current`,t===n&&a.length>0))}function m(t){let n=a[t];n&&(scrollTo({top:Math.max(0,scrollY+n.getBoundingClientRect().top-o()-12),behavior:`smooth`}),s()||(e.classList.remove(`wb-toc-open`),e.hidden=!0))}const h=(a,o,s)=>{if(o.kind!==`page`&&o.kind!==`file`)return;c=o,r=a.querySelector(`#wb-main`)??a,i=s,document.body.append(e),a.querySelector(`.wb-nav .wb-theme`)?.before(n),t.addEventListener(`click`,e=>{let t=e.target.closest(`a[data-index]`);t&&(e.preventDefault(),m(Number(t.dataset.index)))}),n.addEventListener(`click`,()=>{let t=!e.classList.contains(`wb-toc-open`);e.classList.toggle(`wb-toc-open`,t),e.hidden=!t}),document.addEventListener(`keydown`,t=>{t.key===`Escape`&&e.classList.contains(`wb-toc-open`)&&n.click()});let l=0,d=e=>()=>{cancelAnimationFrame(l),l=requestAnimationFrame(e)};addEventListener(`scroll`,d(p),{passive:!0}),addEventListener(`resize`,d(u)),i.onRendered(u),i.onForms(()=>f()),i.onEvents(()=>requestAnimationFrame(f)),u()};export{h as default};
|
|
@@ -6,12 +6,15 @@ Pages here have a table of contents beside them, made from their \`##\` and
|
|
|
6
6
|
\`###\` headings: give a long page headings, and the reader can find their way
|
|
7
7
|
in it. A section with an unanswered question or an unread message has a dot.
|
|
8
8
|
`;
|
|
9
|
-
function toc() {
|
|
9
|
+
function toc(options = {}) {
|
|
10
10
|
return {
|
|
11
11
|
name: "toc",
|
|
12
|
-
docs: () => DOCS,
|
|
12
|
+
docs: () => options.always ? DOCS + "\nIn this project every page has it; a page without it says `toc: false` in its frontmatter.\n" : DOCS + "\nA page has it when its frontmatter says `toc: true`:\n\n```markdown\n---\ntoc: true\n---\n# A long page\n```\n",
|
|
13
13
|
style: packagePath("dist/plugins/toc/style.css"),
|
|
14
|
-
setup: () => ({
|
|
14
|
+
setup: () => ({
|
|
15
|
+
client: packagePath("dist/plugins/toc/client.js"),
|
|
16
|
+
shell: () => ({ head: `<meta name="wb-toc" content="${options.always ? "always" : "opt-in"}">` })
|
|
17
|
+
})
|
|
15
18
|
};
|
|
16
19
|
}
|
|
17
20
|
export { toc as default };
|
|
@@ -1332,6 +1332,7 @@ async function refresh() {
|
|
|
1332
1332
|
throw err;
|
|
1333
1333
|
}
|
|
1334
1334
|
const data = next.data;
|
|
1335
|
+
currentPage.data = data;
|
|
1335
1336
|
islands = data.islands;
|
|
1336
1337
|
for (const href of data.styles) if (![...document.querySelectorAll("link[rel=stylesheet]")].some((l) => new URL(l.href).pathname === new URL(href, location.href).pathname)) document.head.insertAdjacentHTML("beforeend", String(html`<link rel="stylesheet" href="${href}">`));
|
|
1337
1338
|
const content = new DOMParser().parseFromString(next.html, "text/html").getElementById("wb-main")?.innerHTML ?? "";
|
package/dist/src/pages/render.js
CHANGED
package/dist/src/server.js
CHANGED
|
@@ -398,7 +398,7 @@ function createServer(project) {
|
|
|
398
398
|
};
|
|
399
399
|
}
|
|
400
400
|
const json = (s) => JSON.stringify(s).replace(/</g, "\\u003c");
|
|
401
|
-
const DOCK_STYLE = "#wb-dock{position:fixed;right:16px;bottom:calc(16px + var(--wb-inset-bottom,0px));z-index:
|
|
401
|
+
const DOCK_STYLE = "#wb-dock{position:fixed;right:16px;bottom:calc(16px + var(--wb-inset-bottom,0px));z-index:30;display:flex;flex-direction:column;align-items:flex-end;gap:6px}#wb-dock:empty{display:none}.wb-toasts{display:flex;flex-direction:column;align-items:flex-end;gap:6px}.wb-toasts:empty{display:none}.wb-toast{display:inline-flex;align-items:center;gap:8px;cursor:pointer;padding:5px 12px;border-radius:99px;font:600 .8rem/1.4 var(--sans,system-ui,sans-serif);background:var(--accent,#1d5bbf);color:var(--accent-text,#fff);border:1px solid transparent;box-shadow:0 2px 10px rgb(0 0 0/.18)}.wb-toast[hidden]{display:none}.wb-toast[data-kind=warn]{background:var(--warn,#6b4fc0)}.wb-toast[data-kind=alert]{background:var(--bad,#c4352b);color:#fff}@keyframes wb-flash{from{outline-color:var(--accent,#1d5bbf)}to{outline-color:transparent}}.wb-flash{outline:3px solid transparent;outline-offset:6px;border-radius:4px;animation:wb-flash 2s ease-out}.wb-toast-dismiss{margin-right:-4px;padding:0 4px;border-left:1px solid rgb(255 255 255/.35);opacity:.75}.wb-toast-dismiss:hover{opacity:1}";
|
|
402
402
|
/** `extras`: what plugins add to <head> and <body> (see shellExtras). */
|
|
403
403
|
function shell(page, { page: rendered, out, notices }, extras = {
|
|
404
404
|
head: [],
|
package/package.json
CHANGED
package/plugins/toc/README.md
CHANGED
|
@@ -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`.
|
package/plugins/toc/client.ts
CHANGED
|
@@ -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'
|
|
@@ -28,8 +28,17 @@ function fits() {
|
|
|
28
28
|
return left >= 230
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
let handle: PageHandle
|
|
32
|
+
|
|
33
|
+
/** Does this page have it: what its frontmatter says (`toc:`), and whether it is on everywhere. */
|
|
34
|
+
function wanted() {
|
|
35
|
+
const said = ((handle.data as { frontmatter?: Record<string, string> } | null)?.frontmatter ?? {}).toc?.trim().toLowerCase()
|
|
36
|
+
const always = document.querySelector('meta[name=wb-toc]')?.getAttribute('content') === 'always'
|
|
37
|
+
return always ? said !== 'false' : said === 'true'
|
|
38
|
+
}
|
|
39
|
+
|
|
31
40
|
function build() {
|
|
32
|
-
headings = [...main.querySelectorAll<HTMLElement>(':scope > h2, :scope > h3')]
|
|
41
|
+
headings = wanted() ? [...main.querySelectorAll<HTMLElement>(':scope > h2, :scope > h3')] : []
|
|
33
42
|
let h2 = 0, h3 = 0
|
|
34
43
|
list.replaceChildren(...headings.map((h, i) => {
|
|
35
44
|
// the page's numbers: 1, 1.1, 1.2, 2 (base.css counts ## and ### alike)
|
|
@@ -99,8 +108,9 @@ function go(i: number) {
|
|
|
99
108
|
if (!fits()) { panel.classList.remove('wb-toc-open'); panel.hidden = true }
|
|
100
109
|
}
|
|
101
110
|
|
|
102
|
-
const mount: PluginMount = (root,
|
|
103
|
-
if (
|
|
111
|
+
const mount: PluginMount = (root, page, core) => {
|
|
112
|
+
if (page.kind !== 'page' && page.kind !== 'file') return
|
|
113
|
+
handle = page
|
|
104
114
|
main = root.querySelector<HTMLElement>('#wb-main') ?? root
|
|
105
115
|
api = core
|
|
106
116
|
document.body.append(panel)
|
package/plugins/toc/index.ts
CHANGED
|
@@ -5,6 +5,9 @@
|
|
|
5
5
|
//
|
|
6
6
|
// import toc from '@bigmistqke/whiteboard/toc'
|
|
7
7
|
// export default defineConfig({ plugins: [toc()] })
|
|
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`.
|
|
8
11
|
import { packagePath } from '../../src/package.ts'
|
|
9
12
|
import type { Plugin } from '../../src/define.ts'
|
|
10
13
|
|
|
@@ -15,11 +18,22 @@ 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
|
|
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: () =>
|
|
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',
|
|
22
32
|
style: packagePath('dist/plugins/toc/style.css'),
|
|
23
|
-
setup: () => ({
|
|
33
|
+
setup: () => ({
|
|
34
|
+
client: packagePath('dist/plugins/toc/client.js'),
|
|
35
|
+
// how the browser part knows: on every page, or only where the frontmatter asks
|
|
36
|
+
shell: () => ({ head: `<meta name="wb-toc" content="${options.always ? 'always' : 'opt-in'}">` }),
|
|
37
|
+
}),
|
|
24
38
|
}
|
|
25
39
|
}
|
package/src/client/page/main.ts
CHANGED
|
@@ -273,6 +273,8 @@ async function refresh() {
|
|
|
273
273
|
throw err
|
|
274
274
|
}
|
|
275
275
|
const data = next.data as Data
|
|
276
|
+
// the handle the plugins hold too: what they read of the page (its frontmatter) stays current
|
|
277
|
+
currentPage.data = data
|
|
276
278
|
islands = data.islands
|
|
277
279
|
for (const href of data.styles) {
|
|
278
280
|
if (![...document.querySelectorAll<HTMLLinkElement>('link[rel=stylesheet]')].some(l => new URL(l.href).pathname === new URL(href, location.href).pathname))
|
package/src/define.ts
CHANGED
|
@@ -343,7 +343,11 @@ export type PluginMount = (root: HTMLElement, page: PageHandle, api: Api) => voi
|
|
|
343
343
|
export interface PageHandle {
|
|
344
344
|
id: string
|
|
345
345
|
kind: StoredPage['kind']
|
|
346
|
-
/**
|
|
346
|
+
/**
|
|
347
|
+
* what the server render returned with the HTML now in the root, kept current on each live update:
|
|
348
|
+
* for a markdown page, `{ islands, styles, mtime, frontmatter }`, the frontmatter's fields as
|
|
349
|
+
* strings (`toc: true` is `'true'`)
|
|
350
|
+
*/
|
|
347
351
|
data: unknown
|
|
348
352
|
/** called when the stored page changed; returns an unsubscribe */
|
|
349
353
|
onChange(fn: () => void): () => void
|
package/src/pages/render.ts
CHANGED
|
@@ -206,7 +206,8 @@ ${links.map(href => html`<link rel="stylesheet" href="${href}">`)}`,
|
|
|
206
206
|
// the page's content is HTML already: markdown's output, or what was built above
|
|
207
207
|
html: html`${nav}<main id="wb-main">${raw(out.html)}</main>`,
|
|
208
208
|
// the client mounts `islands` and links `styles`
|
|
209
|
-
|
|
209
|
+
// its frontmatter too, for the plugins in the browser (handle.data.frontmatter)
|
|
210
|
+
data: { islands, styles, mtime: page.mtime, frontmatter: page.kind === 'page' ? page.frontmatter : {} },
|
|
210
211
|
}
|
|
211
212
|
}
|
|
212
213
|
}
|
package/src/server.ts
CHANGED
|
@@ -383,8 +383,10 @@ export function createServer(project: Project): WhiteboardServer {
|
|
|
383
383
|
const json = (s: unknown) => JSON.stringify(s).replace(/</g, '\\u003c')
|
|
384
384
|
|
|
385
385
|
// the page's corner (api.corner()): the one thing the core puts on every page, empty, for the
|
|
386
|
-
// page's and the plugins' floating buttons; above what covers the bottom of the window
|
|
387
|
-
|
|
386
|
+
// page's and the plugins' floating buttons and toasts; above what covers the bottom of the window.
|
|
387
|
+
// Above the page and what floats on it (comment threads: 20), under what fills the window (a
|
|
388
|
+
// maximized frame: 40, a lightbox or the terminal drawer: 50)
|
|
389
|
+
const DOCK_STYLE = '#wb-dock{position:fixed;right:16px;bottom:calc(16px + var(--wb-inset-bottom,0px));z-index:30;display:flex;flex-direction:column;align-items:flex-end;gap:6px}#wb-dock:empty{display:none}'
|
|
388
390
|
// toasts (api.toast): labels in the corner, in the page's theme colours where it has them
|
|
389
391
|
+ '.wb-toasts{display:flex;flex-direction:column;align-items:flex-end;gap:6px}.wb-toasts:empty{display:none}'
|
|
390
392
|
+ '.wb-toast{display:inline-flex;align-items:center;gap:8px;cursor:pointer;padding:5px 12px;border-radius:99px;font:600 .8rem/1.4 var(--sans,system-ui,sans-serif);background:var(--accent,#1d5bbf);color:var(--accent-text,#fff);border:1px solid transparent;box-shadow:0 2px 10px rgb(0 0 0/.18)}'
|