@noy4/docserve 0.3.3 → 0.3.4
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/widget/index.js +3266 -2618
- package/package.json +2 -2
- package/src/plugins/collapse/client.js +45 -0
- package/src/plugins/collapse/collapse.css +74 -0
- package/src/plugins/collapse/commands.ts +81 -0
- package/src/plugins/collapse/model.ts +123 -0
- package/src/plugins/collapse/sections.ts +105 -0
- package/src/plugins/collapse/view.ts +84 -0
- package/src/plugins/sidebar/client.js +1 -0
- package/src/plugins/sidebar/icons.js +1 -1
- package/src/plugins/toc/client.js +55 -0
- package/src/plugins/toc/commands.ts +49 -0
- package/src/plugins/toc/heading-html.ts +40 -0
- package/src/plugins/toc/model.ts +178 -0
- package/src/plugins/toc/outline.js +94 -0
- package/src/plugins/toc/scroll-spy.ts +35 -0
- package/src/plugins/toc/toc.css +214 -0
- package/src/plugins/toc/ui/TocPanel.tsx +87 -0
- package/src/widget/index.js +9 -1
- package/src/widget/lib/headings.js +132 -0
- package/src/widget/widget.css +15 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noy4/docserve",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "Serves a folder of HTML as a card gallery with live reload.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"dist/widget/*.js"
|
|
22
22
|
],
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@iconify
|
|
24
|
+
"@iconify/json": "^2.2.558",
|
|
25
25
|
"@preact/signals-react": "^3.12.0",
|
|
26
26
|
"@preact/signals-react-transform": "^0.8.3",
|
|
27
27
|
"@rolldown/plugin-babel": "^0.2.4",
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createDisposables } from "../../widget/lib/disposables.js"
|
|
2
|
+
import { el } from "../../widget/lib/dom.js"
|
|
3
|
+
import { registerCollapseCommands, registerCollapseDock } from "./commands.js"
|
|
4
|
+
import { CollapseModel } from "./model.js"
|
|
5
|
+
import { collectSections } from "./sections.js"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @returns {DocservePlugin}
|
|
9
|
+
*/
|
|
10
|
+
export function collapsePlugin() {
|
|
11
|
+
return {
|
|
12
|
+
id: "collapse",
|
|
13
|
+
setup(ctx) {
|
|
14
|
+
// sections
|
|
15
|
+
const collect = collectSections(document)
|
|
16
|
+
|
|
17
|
+
// state
|
|
18
|
+
const model = CollapseModel.create(ctx.page)
|
|
19
|
+
const cleanup = createDisposables()
|
|
20
|
+
|
|
21
|
+
// styles
|
|
22
|
+
const link = el("link", {
|
|
23
|
+
rel: "stylesheet",
|
|
24
|
+
href: "/__docserve/plugins/collapse/collapse.css",
|
|
25
|
+
"data-docserve-collapse": "",
|
|
26
|
+
})
|
|
27
|
+
document.head.append(link)
|
|
28
|
+
cleanup.add(() => link.remove())
|
|
29
|
+
|
|
30
|
+
// sections
|
|
31
|
+
model.load(collect)
|
|
32
|
+
cleanup.add(model.subscribe())
|
|
33
|
+
|
|
34
|
+
// commands
|
|
35
|
+
cleanup.add(registerCollapseCommands(ctx, model))
|
|
36
|
+
cleanup.add(registerCollapseDock(ctx, model))
|
|
37
|
+
|
|
38
|
+
// rpc
|
|
39
|
+
cleanup.add(ctx.rpc.register("plugin:collapse:is-collapsed", (id) => model.isCollapsed(id)))
|
|
40
|
+
cleanup.add(ctx.rpc.register("plugin:collapse:expand-ancestors", (id) => model.expandAncestors(id)))
|
|
41
|
+
|
|
42
|
+
return cleanup.dispose
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
.docserve-collapse__heading {
|
|
2
|
+
position: relative;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.docserve-collapse__toggle {
|
|
6
|
+
position: absolute;
|
|
7
|
+
top: 50%;
|
|
8
|
+
left: -24px;
|
|
9
|
+
display: grid;
|
|
10
|
+
place-items: center;
|
|
11
|
+
width: 18px;
|
|
12
|
+
height: 18px;
|
|
13
|
+
padding: 0;
|
|
14
|
+
border: 0;
|
|
15
|
+
border-radius: 4px;
|
|
16
|
+
background: transparent;
|
|
17
|
+
color: inherit;
|
|
18
|
+
font: inherit;
|
|
19
|
+
line-height: 0;
|
|
20
|
+
opacity: 0;
|
|
21
|
+
cursor: pointer;
|
|
22
|
+
appearance: none;
|
|
23
|
+
transform: translateY(-50%);
|
|
24
|
+
transition:
|
|
25
|
+
opacity 0.15s ease,
|
|
26
|
+
background-color 0.15s ease;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.docserve-collapse__heading:hover .docserve-collapse__toggle,
|
|
30
|
+
.docserve-collapse__heading:focus-within .docserve-collapse__toggle {
|
|
31
|
+
opacity: 0.55;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.docserve-collapse__toggle svg {
|
|
35
|
+
width: 15px;
|
|
36
|
+
height: 15px;
|
|
37
|
+
transition: transform 0.15s ease;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.docserve-collapse__toggle[aria-expanded="true"] svg {
|
|
41
|
+
transform: rotate(90deg);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.docserve-collapse__toggle:hover {
|
|
45
|
+
background: color-mix(in srgb, currentColor 10%, transparent);
|
|
46
|
+
opacity: 1;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.docserve-collapse__toggle:focus-visible {
|
|
50
|
+
outline: 2px solid currentColor;
|
|
51
|
+
outline-offset: 1px;
|
|
52
|
+
opacity: 1;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@media (hover: none) {
|
|
56
|
+
.docserve-collapse__toggle {
|
|
57
|
+
opacity: 0.45;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.docserve-collapse__hidden {
|
|
62
|
+
display: none !important;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
section.docserve-collapse__collapsed + section {
|
|
66
|
+
margin-top: var(--docserve-collapse-section-gap, 12px);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@media (prefers-reduced-motion: reduce) {
|
|
70
|
+
.docserve-collapse__toggle,
|
|
71
|
+
.docserve-collapse__toggle svg {
|
|
72
|
+
transition: none;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import ICON_FOLD_RAW from "~icons/lucide/fold-vertical?raw&width=18&height=18"
|
|
2
|
+
import ICON_UNFOLD_RAW from "~icons/lucide/unfold-vertical?raw&width=18&height=18"
|
|
3
|
+
import { iconSvg } from "../../widget/lib/dom.js"
|
|
4
|
+
import type { CollapseModel } from "./model.js"
|
|
5
|
+
|
|
6
|
+
const ICON_FOLD = iconSvg(ICON_FOLD_RAW)
|
|
7
|
+
const ICON_UNFOLD = iconSvg(ICON_UNFOLD_RAW)
|
|
8
|
+
|
|
9
|
+
export function registerCollapseCommands(ctx: DocserveContext, model: CollapseModel): () => void {
|
|
10
|
+
const offEnabled = ctx.commands.register({
|
|
11
|
+
id: "plugin:collapse:toggle-enabled",
|
|
12
|
+
title: "Enable/disable collapse",
|
|
13
|
+
category: "View",
|
|
14
|
+
icon: ICON_FOLD,
|
|
15
|
+
keys: ["Alt+K"],
|
|
16
|
+
run: () => model.toggleEnabled(),
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const offToggle = ctx.commands.register({
|
|
20
|
+
id: "plugin:collapse:toggle-all",
|
|
21
|
+
title: "Toggle all sections",
|
|
22
|
+
category: "View",
|
|
23
|
+
icon: ICON_FOLD,
|
|
24
|
+
keys: ["k"],
|
|
25
|
+
when: () => model.enabled.value,
|
|
26
|
+
run: () => model.toggleAll(),
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const offExpand = ctx.commands.register({
|
|
30
|
+
id: "plugin:collapse:expand-all",
|
|
31
|
+
title: "Expand all sections",
|
|
32
|
+
category: "View",
|
|
33
|
+
icon: ICON_UNFOLD,
|
|
34
|
+
when: () => model.enabled.value,
|
|
35
|
+
run: () => model.expandAll(),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
const offCollapse = ctx.commands.register({
|
|
39
|
+
id: "plugin:collapse:collapse-all",
|
|
40
|
+
title: "Collapse all sections",
|
|
41
|
+
category: "View",
|
|
42
|
+
icon: ICON_FOLD,
|
|
43
|
+
when: () => model.enabled.value,
|
|
44
|
+
run: () => model.collapseAll(),
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
return () => {
|
|
48
|
+
offCollapse()
|
|
49
|
+
offExpand()
|
|
50
|
+
offToggle()
|
|
51
|
+
offEnabled()
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function registerCollapseDock(ctx: DocserveContext, model: CollapseModel): () => void {
|
|
56
|
+
/** @type {DocksItemHandle | null} */
|
|
57
|
+
let dock = null
|
|
58
|
+
|
|
59
|
+
const offEnabled = model.enabled.subscribe(sync)
|
|
60
|
+
|
|
61
|
+
return () => {
|
|
62
|
+
offEnabled()
|
|
63
|
+
dock?.dispose()
|
|
64
|
+
dock = null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function sync(): void {
|
|
68
|
+
if (model.enabled.value && !dock) {
|
|
69
|
+
dock = ctx.docks.register({
|
|
70
|
+
id: "collapse",
|
|
71
|
+
title: "Toggle all sections",
|
|
72
|
+
icon: ICON_FOLD,
|
|
73
|
+
category: "actions",
|
|
74
|
+
command: "plugin:collapse:toggle-all",
|
|
75
|
+
})
|
|
76
|
+
} else if (!model.enabled.value && dock) {
|
|
77
|
+
dock.dispose()
|
|
78
|
+
dock = null
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { effect, signal } from "@preact/signals-react"
|
|
2
|
+
import type { Signal } from "@preact/signals-react"
|
|
3
|
+
import { persisted, persistedBool } from "../../widget/lib/persisted-signal.js"
|
|
4
|
+
import type { CollapseCollect, CollapseSection } from "./sections.js"
|
|
5
|
+
import { applyState, mountSections } from "./view.js"
|
|
6
|
+
|
|
7
|
+
export class CollapseModel {
|
|
8
|
+
readonly collapsed: Signal<string[]>
|
|
9
|
+
readonly enabled: Signal<boolean>
|
|
10
|
+
readonly sections = signal<CollapseSection[]>([])
|
|
11
|
+
|
|
12
|
+
#wrappers: HTMLElement[] = []
|
|
13
|
+
#mounted: CollapseSection[] | null = null
|
|
14
|
+
#unmount: (() => void) | null = null
|
|
15
|
+
|
|
16
|
+
constructor(page: string) {
|
|
17
|
+
this.collapsed = persisted<string[]>(`docserve:collapse:${page}`, [])
|
|
18
|
+
this.enabled = persistedBool("docserve:collapse:enabled", true)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static create(page: string): CollapseModel {
|
|
22
|
+
return new CollapseModel(page)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
load(collect: CollapseCollect): void {
|
|
26
|
+
const ids = new Set(collect.sections.map((section) => section.id))
|
|
27
|
+
const current = this.collapsed.value
|
|
28
|
+
const next = current.filter((id) => ids.has(id))
|
|
29
|
+
if (next.length !== current.length) this.collapsed.value = next
|
|
30
|
+
|
|
31
|
+
this.#wrappers = collect.wrappers
|
|
32
|
+
this.sections.value = collect.sections
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
subscribe(): () => void {
|
|
36
|
+
const stop = effect(() => {
|
|
37
|
+
const sections = this.sections.value
|
|
38
|
+
const enabled = this.enabled.value
|
|
39
|
+
|
|
40
|
+
if (enabled) {
|
|
41
|
+
if (this.#mounted !== sections) {
|
|
42
|
+
this.#unmount?.()
|
|
43
|
+
this.#unmount = mountSections(sections, (id) => this.toggle(id))
|
|
44
|
+
this.#mounted = sections
|
|
45
|
+
}
|
|
46
|
+
} else if (this.#unmount) {
|
|
47
|
+
this.#unmount()
|
|
48
|
+
this.#unmount = null
|
|
49
|
+
this.#mounted = null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
applyState(sections, this.collapsed.value, enabled)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
return () => {
|
|
56
|
+
stop()
|
|
57
|
+
this.#unmount?.()
|
|
58
|
+
this.#unmount = null
|
|
59
|
+
this.#mounted = null
|
|
60
|
+
|
|
61
|
+
const wrappers = this.#wrappers
|
|
62
|
+
this.#wrappers = []
|
|
63
|
+
for (const wrapper of wrappers.reverse()) {
|
|
64
|
+
if (wrapper.isConnected) wrapper.replaceWith(...wrapper.childNodes)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
toggleEnabled(): void {
|
|
70
|
+
this.enabled.value = !this.enabled.value
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
toggle(id: string): void {
|
|
74
|
+
const collapsed = new Set(this.collapsed.value)
|
|
75
|
+
if (collapsed.has(id)) collapsed.delete(id)
|
|
76
|
+
else collapsed.add(id)
|
|
77
|
+
this.collapsed.value = [...collapsed]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
expand(id: string): void {
|
|
81
|
+
this.collapsed.value = this.collapsed.value.filter((item) => item !== id)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
collapse(id: string): void {
|
|
85
|
+
if (this.isCollapsed(id)) return
|
|
86
|
+
this.collapsed.value = [...this.collapsed.value, id]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
expandAncestors(id: string): void {
|
|
90
|
+
if (!this.enabled.value) return
|
|
91
|
+
|
|
92
|
+
let section = this.sections.value.find((item) => item.id === id)?.parent ?? null
|
|
93
|
+
if (!section) return
|
|
94
|
+
|
|
95
|
+
const collapsed = new Set(this.collapsed.value)
|
|
96
|
+
let changed = false
|
|
97
|
+
while (section) {
|
|
98
|
+
if (collapsed.delete(section.id)) changed = true
|
|
99
|
+
section = section.parent
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (changed) this.collapsed.value = [...collapsed]
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
toggleAll(): void {
|
|
106
|
+
const sections = this.sections.value
|
|
107
|
+
const collapsed = new Set(this.collapsed.value)
|
|
108
|
+
const allCollapsed = sections.every((section) => collapsed.has(section.id))
|
|
109
|
+
this.collapsed.value = allCollapsed ? [] : sections.map((section) => section.id)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
collapseAll(): void {
|
|
113
|
+
this.collapsed.value = this.sections.value.map((section) => section.id)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
expandAll(): void {
|
|
117
|
+
this.collapsed.value = []
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
isCollapsed(id: string): boolean {
|
|
121
|
+
return this.enabled.value && this.collapsed.value.includes(id)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { collectHeadings, ensureIds, findRoot } from "../../widget/lib/headings.js"
|
|
2
|
+
|
|
3
|
+
export interface CollapseSection {
|
|
4
|
+
id: string
|
|
5
|
+
heading: HTMLHeadingElement
|
|
6
|
+
level: number
|
|
7
|
+
body: Element[]
|
|
8
|
+
button?: HTMLButtonElement
|
|
9
|
+
parent: CollapseSection | null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface CollapseCollect {
|
|
13
|
+
sections: CollapseSection[]
|
|
14
|
+
wrappers: HTMLElement[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const EXCLUDED = "nav, aside, header, footer, [data-collapse-ignore], #docserve-dock-host, [data-docserve-widget]"
|
|
18
|
+
const DEFAULT_LEVELS: [number, number] = [2, 4]
|
|
19
|
+
const SKIP = new Set(["SCRIPT", "STYLE", "LINK", "TEMPLATE", "NOSCRIPT"])
|
|
20
|
+
|
|
21
|
+
export function collectSections(doc: Document = document): CollapseCollect {
|
|
22
|
+
const source = doc.body ?? doc.documentElement
|
|
23
|
+
if (!source) return { sections: [], wrappers: [] }
|
|
24
|
+
|
|
25
|
+
const root = findRoot(doc, "data-collapse-root")
|
|
26
|
+
if (!root) return { sections: [], wrappers: [] }
|
|
27
|
+
if ((root.getAttribute("data-collapse") ?? source.getAttribute("data-collapse")) === "false")
|
|
28
|
+
return { sections: [], wrappers: [] }
|
|
29
|
+
|
|
30
|
+
const raw = root.getAttribute("data-collapse-levels") ?? source.getAttribute("data-collapse-levels")
|
|
31
|
+
const range = parseLevels(raw)
|
|
32
|
+
if (!range) return { sections: [], wrappers: [] }
|
|
33
|
+
|
|
34
|
+
const headings = collectHeadings(root, range, EXCLUDED)
|
|
35
|
+
ensureIds(headings, doc)
|
|
36
|
+
|
|
37
|
+
const wrappers: HTMLElement[] = []
|
|
38
|
+
const sections: CollapseSection[] = headings
|
|
39
|
+
.map((heading) => ({
|
|
40
|
+
id: heading.id,
|
|
41
|
+
heading,
|
|
42
|
+
level: Number(heading.tagName[1]),
|
|
43
|
+
body: bodyNodes(heading, wrappers, doc),
|
|
44
|
+
parent: null,
|
|
45
|
+
}))
|
|
46
|
+
.filter((section) => section.body.length)
|
|
47
|
+
|
|
48
|
+
const stack: CollapseSection[] = []
|
|
49
|
+
for (const section of sections) {
|
|
50
|
+
while (stack.length && stack[stack.length - 1].level >= section.level) stack.pop()
|
|
51
|
+
section.parent = stack[stack.length - 1] ?? null
|
|
52
|
+
stack.push(section)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return { sections, wrappers }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parseLevels(value: string | null | undefined): [number, number] | null {
|
|
59
|
+
if (value === "false") return null
|
|
60
|
+
if (!value || value.trim() === "") return [...DEFAULT_LEVELS]
|
|
61
|
+
if (value === "deep") return [2, 6]
|
|
62
|
+
|
|
63
|
+
const parts = value.split(/[,-]/).map((part) => Number(part.trim())).filter(Number.isFinite)
|
|
64
|
+
if (!parts.length) return [...DEFAULT_LEVELS]
|
|
65
|
+
|
|
66
|
+
const min = clampLevel(parts[0])
|
|
67
|
+
const max = clampLevel(parts.length > 1 ? parts[1] : parts[0])
|
|
68
|
+
return min <= max ? [min, max] : [max, min]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function clampLevel(value: number): number {
|
|
72
|
+
return Math.min(6, Math.max(1, Math.round(value)))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function bodyNodes(heading: HTMLHeadingElement, wrappers: HTMLElement[], doc: Document): Element[] {
|
|
76
|
+
const level = Number(heading.tagName[1])
|
|
77
|
+
const nodes: Element[] = []
|
|
78
|
+
|
|
79
|
+
let node = heading.nextSibling
|
|
80
|
+
while (node) {
|
|
81
|
+
const next = node.nextSibling
|
|
82
|
+
|
|
83
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
84
|
+
const element = node as Element
|
|
85
|
+
if (element.matches("#docserve-dock-host, [data-docserve-widget]")) break
|
|
86
|
+
if (/^H[1-6]$/.test(element.tagName) && Number(element.tagName[1]) <= level) break
|
|
87
|
+
if (SKIP.has(element.tagName) || element.matches("[data-collapse-ignore]")) {
|
|
88
|
+
node = next
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
nodes.push(element)
|
|
92
|
+
} else if (node.nodeType === Node.TEXT_NODE && node.textContent?.trim()) {
|
|
93
|
+
const wrapper = doc.createElement("span")
|
|
94
|
+
wrapper.className = "docserve-collapse__text"
|
|
95
|
+
node.replaceWith(wrapper)
|
|
96
|
+
wrapper.append(node)
|
|
97
|
+
wrappers.push(wrapper)
|
|
98
|
+
nodes.push(wrapper)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
node = next
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return nodes
|
|
105
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import ICON_CHEVRON_RAW from "~icons/lucide/chevron-right?raw&width=16&height=16"
|
|
2
|
+
import { el, iconSvg } from "../../widget/lib/dom.js"
|
|
3
|
+
import type { CollapseSection } from "./sections.js"
|
|
4
|
+
|
|
5
|
+
const ICON_CHEVRON = iconSvg(ICON_CHEVRON_RAW)
|
|
6
|
+
const HIDDEN_CLASS = "docserve-collapse__hidden"
|
|
7
|
+
const SECTION_COLLAPSED_CLASS = "docserve-collapse__collapsed"
|
|
8
|
+
|
|
9
|
+
export function mountSections(sections: CollapseSection[], onToggle: (id: string) => void): () => void {
|
|
10
|
+
const controller = new AbortController()
|
|
11
|
+
|
|
12
|
+
for (const section of sections) {
|
|
13
|
+
const button = el("button", {
|
|
14
|
+
type: "button",
|
|
15
|
+
class: "docserve-collapse__toggle",
|
|
16
|
+
"data-collapse-toggle": "",
|
|
17
|
+
"aria-expanded": "true",
|
|
18
|
+
"aria-label": "Collapse section",
|
|
19
|
+
title: "Collapse section",
|
|
20
|
+
html: ICON_CHEVRON,
|
|
21
|
+
}) as HTMLButtonElement
|
|
22
|
+
|
|
23
|
+
button.addEventListener("click", () => onToggle(section.id), { signal: controller.signal })
|
|
24
|
+
|
|
25
|
+
section.heading.classList.add("docserve-collapse__heading")
|
|
26
|
+
section.heading.prepend(button)
|
|
27
|
+
section.button = button
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return () => {
|
|
31
|
+
controller.abort()
|
|
32
|
+
|
|
33
|
+
for (const section of sections) {
|
|
34
|
+
section.button?.remove()
|
|
35
|
+
section.button = undefined
|
|
36
|
+
section.heading.classList.remove("docserve-collapse__heading")
|
|
37
|
+
section.heading.parentElement?.classList.remove(SECTION_COLLAPSED_CLASS)
|
|
38
|
+
for (const node of section.body) node.classList.remove(HIDDEN_CLASS)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function applyState(sections: CollapseSection[], collapsed: string[], enabled: boolean): void {
|
|
44
|
+
const collapsedIds = new Set(collapsed)
|
|
45
|
+
const hidden = new Set<Element>()
|
|
46
|
+
|
|
47
|
+
if (enabled) {
|
|
48
|
+
for (const section of sections) {
|
|
49
|
+
if (!collapsedIds.has(section.id)) continue
|
|
50
|
+
for (const node of section.body) hidden.add(node)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
for (const section of sections) {
|
|
55
|
+
for (const node of section.body)
|
|
56
|
+
node.classList.toggle(HIDDEN_CLASS, enabled && hidden.has(node))
|
|
57
|
+
|
|
58
|
+
section.heading.classList.toggle("docserve-collapse__heading", enabled)
|
|
59
|
+
|
|
60
|
+
const parent = section.heading.parentElement
|
|
61
|
+
if (!enabled)
|
|
62
|
+
parent?.classList.remove(SECTION_COLLAPSED_CLASS)
|
|
63
|
+
else if (parent?.tagName === "SECTION" && isSectionHead(section.heading))
|
|
64
|
+
parent.classList.toggle(SECTION_COLLAPSED_CLASS, collapsedIds.has(section.id))
|
|
65
|
+
|
|
66
|
+
const button = section.button
|
|
67
|
+
if (!button) continue
|
|
68
|
+
|
|
69
|
+
const open = !collapsedIds.has(section.id)
|
|
70
|
+
const label = open ? "Collapse section" : "Expand section"
|
|
71
|
+
button.setAttribute("aria-expanded", String(open))
|
|
72
|
+
button.title = label
|
|
73
|
+
button.setAttribute("aria-label", label)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function isSectionHead(heading: HTMLHeadingElement): boolean {
|
|
78
|
+
let node = heading.previousElementSibling
|
|
79
|
+
while (node) {
|
|
80
|
+
if (/^H[1-6]$/.test(node.tagName)) return false
|
|
81
|
+
node = node.previousElementSibling
|
|
82
|
+
}
|
|
83
|
+
return true
|
|
84
|
+
}
|
|
@@ -5,7 +5,7 @@ import ICON_CHECK_RAW from "~icons/lucide/check?raw&width=18&height=18"
|
|
|
5
5
|
import ICON_CHEVRON_RAW from "~icons/lucide/chevron-right?raw&width=18&height=18"
|
|
6
6
|
import ICON_CHEVRON_DOWN_RAW from "~icons/lucide/chevron-down?raw&width=18&height=18"
|
|
7
7
|
import ICON_FILE_RAW from "~icons/lucide/file-text?raw&width=18&height=18"
|
|
8
|
-
import ICON_FOLDER_RAW from "~icons/
|
|
8
|
+
import ICON_FOLDER_RAW from "~icons/mdi/folder-open-outline?raw&width=18&height=18"
|
|
9
9
|
import ICON_GRID_RAW from "~icons/lucide/layout-grid?raw&width=18&height=18"
|
|
10
10
|
import ICON_LIST_RAW from "~icons/lucide/list?raw&width=18&height=18"
|
|
11
11
|
import ICON_UNGROUP_RAW from "~icons/lucide/ungroup?raw&width=18&height=18"
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createElement } from "react"
|
|
2
|
+
import { createRoot } from "react-dom/client"
|
|
3
|
+
import { createDisposables } from "../../widget/lib/disposables.js"
|
|
4
|
+
import { registerTocCommands, registerTocDock } from "./commands.js"
|
|
5
|
+
import { TocModel } from "./model.js"
|
|
6
|
+
import { startScrollSpy } from "./scroll-spy.js"
|
|
7
|
+
import { TocPanel } from "./ui/TocPanel.js"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @returns {DocservePlugin}
|
|
11
|
+
*/
|
|
12
|
+
export function tocPlugin() {
|
|
13
|
+
return {
|
|
14
|
+
id: "toc",
|
|
15
|
+
setup(ctx) {
|
|
16
|
+
// state
|
|
17
|
+
const html = document.documentElement
|
|
18
|
+
const model = TocModel.create(ctx.rpc)
|
|
19
|
+
const cleanup = createDisposables()
|
|
20
|
+
|
|
21
|
+
// styles
|
|
22
|
+
ctx.ui.style("/__docserve/plugins/toc/toc.css")
|
|
23
|
+
|
|
24
|
+
// panel
|
|
25
|
+
const mount = ctx.ui.mountPoint()
|
|
26
|
+
const root = createRoot(mount)
|
|
27
|
+
root.render(createElement(TocPanel, { model }))
|
|
28
|
+
cleanup.add(() => {
|
|
29
|
+
root.unmount()
|
|
30
|
+
mount.remove()
|
|
31
|
+
})
|
|
32
|
+
cleanup.add(model.subscribe())
|
|
33
|
+
|
|
34
|
+
// open state
|
|
35
|
+
cleanup.add(model.open.subscribe(() => {
|
|
36
|
+
html.toggleAttribute("data-docserve-panel-right", model.open.value)
|
|
37
|
+
}))
|
|
38
|
+
|
|
39
|
+
// commands
|
|
40
|
+
cleanup.add(registerTocCommands(ctx, model))
|
|
41
|
+
cleanup.add(registerTocDock(ctx, model))
|
|
42
|
+
|
|
43
|
+
// scroll spy
|
|
44
|
+
cleanup.add(startScrollSpy(model))
|
|
45
|
+
|
|
46
|
+
// teardown
|
|
47
|
+
cleanup.add(() => html.removeAttribute("data-docserve-panel-right"))
|
|
48
|
+
|
|
49
|
+
// data
|
|
50
|
+
model.load()
|
|
51
|
+
|
|
52
|
+
return cleanup.dispose
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import ICON_TOC_RAW from "~icons/lucide/list-tree?raw&width=18&height=18"
|
|
2
|
+
import { iconSvg } from "../../widget/lib/dom.js"
|
|
3
|
+
import type { TocModel } from "./model.js"
|
|
4
|
+
|
|
5
|
+
const ICON_TOC = iconSvg(ICON_TOC_RAW)
|
|
6
|
+
|
|
7
|
+
export function registerTocCommands(ctx: DocserveContext, model: TocModel): () => void {
|
|
8
|
+
const offToggle = ctx.commands.register({
|
|
9
|
+
id: "plugin:toc:toggle",
|
|
10
|
+
title: "Toggle table of contents",
|
|
11
|
+
category: "View",
|
|
12
|
+
icon: ICON_TOC,
|
|
13
|
+
keys: ["t"],
|
|
14
|
+
run: () => model.toggle(),
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const offClose = ctx.commands.register({
|
|
18
|
+
id: "plugin:toc:close",
|
|
19
|
+
title: "Close table of contents",
|
|
20
|
+
keys: ["Escape"],
|
|
21
|
+
showInPalette: false,
|
|
22
|
+
when: () => model.open.value,
|
|
23
|
+
run: () => model.setOpen(false),
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
return () => {
|
|
27
|
+
offClose()
|
|
28
|
+
offToggle()
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function registerTocDock(ctx: DocserveContext, model: TocModel): () => void {
|
|
33
|
+
const dock = ctx.docks.register({
|
|
34
|
+
id: "toc",
|
|
35
|
+
title: "Toggle table of contents",
|
|
36
|
+
icon: ICON_TOC,
|
|
37
|
+
category: "main",
|
|
38
|
+
order: 1,
|
|
39
|
+
command: "plugin:toc:toggle",
|
|
40
|
+
isActive: () => model.open.value,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const offOpen = model.open.subscribe(() => dock.update())
|
|
44
|
+
|
|
45
|
+
return () => {
|
|
46
|
+
offOpen()
|
|
47
|
+
dock.dispose()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const BLOCKED = [
|
|
2
|
+
"script",
|
|
3
|
+
"style",
|
|
4
|
+
"link",
|
|
5
|
+
"iframe",
|
|
6
|
+
"object",
|
|
7
|
+
"embed",
|
|
8
|
+
"button",
|
|
9
|
+
"input",
|
|
10
|
+
"select",
|
|
11
|
+
"textarea",
|
|
12
|
+
"template",
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
export function headingHtml(element: HTMLHeadingElement): string {
|
|
16
|
+
const clone = element.cloneNode(true) as HTMLElement
|
|
17
|
+
|
|
18
|
+
for (const node of [clone, ...clone.querySelectorAll("*")]) {
|
|
19
|
+
node.removeAttribute("id")
|
|
20
|
+
node.removeAttribute("tabindex")
|
|
21
|
+
node.removeAttribute("contenteditable")
|
|
22
|
+
for (const attribute of [...node.attributes]) {
|
|
23
|
+
if (attribute.name.toLowerCase().startsWith("on"))
|
|
24
|
+
node.removeAttribute(attribute.name)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
for (const anchor of [...clone.querySelectorAll("a")])
|
|
29
|
+
anchor.replaceWith(...anchor.childNodes)
|
|
30
|
+
|
|
31
|
+
for (const blocked of clone.querySelectorAll(BLOCKED.join(",")))
|
|
32
|
+
blocked.remove()
|
|
33
|
+
|
|
34
|
+
for (const icon of clone.querySelectorAll("iconify-icon, svg, img")) {
|
|
35
|
+
icon.removeAttribute("width")
|
|
36
|
+
icon.removeAttribute("height")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return clone.innerHTML
|
|
40
|
+
}
|