@noy4/docserve 0.3.2 → 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/bin.mjs +2 -2
- package/dist/widget/index.js +11803 -0
- package/package.json +24 -3
- package/src/node/adapter.mjs +67 -0
- package/src/node/assets.mjs +36 -0
- package/src/node/dates.mjs +73 -0
- package/src/node/docs-info.mjs +39 -0
- package/src/node/files.mjs +43 -0
- package/src/node/gallery.mjs +33 -0
- package/src/node/inject.mjs +43 -0
- package/src/node/meta.mjs +137 -0
- package/src/node/routes.mjs +105 -0
- package/src/node/scan.mjs +87 -0
- package/src/node/serve-static.mjs +158 -0
- package/src/node/server.mjs +69 -0
- package/src/node/watch.mjs +119 -0
- 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/copy/client.js +72 -0
- package/src/plugins/copy/copy.css +21 -0
- package/src/plugins/home/client.js +24 -0
- package/src/plugins/sidebar/client.js +106 -0
- package/src/plugins/sidebar/grouping.js +108 -0
- package/src/plugins/sidebar/icons.js +25 -0
- package/src/plugins/sidebar/model.ts +91 -0
- package/src/plugins/sidebar/resize-model.ts +47 -0
- package/src/plugins/sidebar/sidebar.css +663 -0
- package/src/plugins/sidebar/tree-model.ts +23 -0
- package/src/plugins/sidebar/tree.ts +69 -0
- package/src/plugins/sidebar/ui/PageList.tsx +153 -0
- package/src/plugins/sidebar/ui/RadioMenu.tsx +201 -0
- package/src/plugins/sidebar/ui/Sidebar.tsx +95 -0
- package/src/plugins/sidebar/ui/SidebarHeader.tsx +87 -0
- package/src/plugins/sidebar/ui/TreeList.tsx +121 -0
- package/src/plugins/sidebar/ui/preview-model.ts +134 -0
- package/src/plugins/sidebar/ui/useModel.ts +25 -0
- 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/shared/types.d.ts +193 -0
- package/src/widget/commands.js +214 -0
- package/src/widget/docks.js +127 -0
- package/src/widget/host.js +187 -0
- package/src/widget/index.js +22 -0
- package/src/widget/lib/disposables.js +44 -0
- package/src/widget/lib/dom.js +106 -0
- package/src/widget/lib/events.js +65 -0
- package/src/widget/lib/headings.js +132 -0
- package/src/widget/lib/persisted-signal.js +40 -0
- package/src/widget/lib/storage.js +70 -0
- package/src/widget/live.js +86 -0
- package/src/widget/pages.js +65 -0
- package/src/widget/palette.css +162 -0
- package/src/widget/palette.js +259 -0
- package/src/widget/placement.js +204 -0
- package/src/widget/plugins.js +39 -0
- package/src/widget/rpc.js +32 -0
- package/src/widget/styles.js +31 -0
- package/src/widget/ui.js +50 -0
- package/src/widget/widget.css +206 -0
- package/server/files.mjs +0 -261
- package/server/index.mjs +0 -198
- package/server/inject.html +0 -88
- package/server/inject.mjs +0 -28
- package/server/watch.mjs +0 -104
- /package/{server → src/node}/state.mjs +0 -0
- /package/{server → src/node}/websocket.mjs +0 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// Command palette overlay: filter, arrow keys and selection. It owns no
|
|
2
|
+
// state — both the open flag and the registry live in `commands.js`.
|
|
3
|
+
import { formatKey } from "./commands.js"
|
|
4
|
+
import { el } from "./lib/dom.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {object} options
|
|
8
|
+
* @param {ShadowRoot} options.root Where the overlay is mounted.
|
|
9
|
+
* @param {UiContext} options.ui
|
|
10
|
+
* @param {CommandsContext} options.commands
|
|
11
|
+
* @param {(listener: () => void) => () => void} options.subscribe
|
|
12
|
+
* @returns {{ open: () => void, close: () => void, destroy: () => void }}
|
|
13
|
+
*/
|
|
14
|
+
export function createPalette({ root, ui, commands, subscribe }) {
|
|
15
|
+
const input = /** @type {HTMLInputElement} */ (el("input", {
|
|
16
|
+
class: "docserve-palette__input",
|
|
17
|
+
type: "text",
|
|
18
|
+
role: "combobox",
|
|
19
|
+
"aria-expanded": "true",
|
|
20
|
+
"aria-controls": "docserve-palette-list",
|
|
21
|
+
"aria-autocomplete": "list",
|
|
22
|
+
placeholder: "Type a command…",
|
|
23
|
+
autocomplete: "off",
|
|
24
|
+
spellcheck: "false",
|
|
25
|
+
}))
|
|
26
|
+
const list = el("ul", { class: "docserve-palette__list", id: "docserve-palette-list", role: "listbox" })
|
|
27
|
+
const empty = el("p", { class: "docserve-palette__empty", text: "No matching commands" })
|
|
28
|
+
const dialog = el("div", { class: "docserve-palette__dialog", role: "dialog", "aria-modal": "true", "aria-label": "Commands" }, [
|
|
29
|
+
input,
|
|
30
|
+
list,
|
|
31
|
+
empty,
|
|
32
|
+
el("footer", { class: "docserve-palette__footer" }, [
|
|
33
|
+
hint("↑↓", "select"),
|
|
34
|
+
hint("↵", "run"),
|
|
35
|
+
hint("esc", "close"),
|
|
36
|
+
]),
|
|
37
|
+
])
|
|
38
|
+
const backdrop = el("div", { class: "docserve-palette__backdrop" })
|
|
39
|
+
const overlay = el("div", { class: "docserve-palette" }, [backdrop, dialog])
|
|
40
|
+
|
|
41
|
+
root.append(overlay)
|
|
42
|
+
ui.style("/__docserve/widget/palette.css")
|
|
43
|
+
|
|
44
|
+
/** @type {DocserveCommand[]} */
|
|
45
|
+
let items = []
|
|
46
|
+
let index = 0
|
|
47
|
+
let shown = false
|
|
48
|
+
/** @type {Element | null} */
|
|
49
|
+
let previous = null
|
|
50
|
+
|
|
51
|
+
input.addEventListener("input", filter)
|
|
52
|
+
input.addEventListener("keydown", onKeydown)
|
|
53
|
+
backdrop.addEventListener("click", close)
|
|
54
|
+
list.addEventListener("click", (event) => {
|
|
55
|
+
const row = /** @type {HTMLElement | null} */ (/** @type {HTMLElement} */ (event.target).closest("[data-command]"))
|
|
56
|
+
if (row?.dataset.command) activate(row.dataset.command)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
const off = subscribe(sync)
|
|
60
|
+
|
|
61
|
+
return { destroy, open: openPalette, close }
|
|
62
|
+
|
|
63
|
+
function openPalette() {
|
|
64
|
+
commands.palette.open()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function close() {
|
|
68
|
+
commands.palette.close()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Rebuild on any registry change and follow the open flag.
|
|
72
|
+
function sync() {
|
|
73
|
+
const open = commands.palette.isOpen()
|
|
74
|
+
if (open === shown) {
|
|
75
|
+
if (open) filter()
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
shown = open
|
|
79
|
+
overlay.toggleAttribute("data-open", open)
|
|
80
|
+
document.documentElement.toggleAttribute("data-docserve-palette", open)
|
|
81
|
+
|
|
82
|
+
if (open) {
|
|
83
|
+
previous = document.activeElement
|
|
84
|
+
input.value = ""
|
|
85
|
+
filter()
|
|
86
|
+
requestAnimationFrame(() => input.focus())
|
|
87
|
+
} else {
|
|
88
|
+
input.value = ""
|
|
89
|
+
restoreFocus()
|
|
90
|
+
previous = null
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function restoreFocus() {
|
|
95
|
+
if (!(previous instanceof HTMLElement) || !document.contains(previous)) return
|
|
96
|
+
try {
|
|
97
|
+
previous.focus()
|
|
98
|
+
} catch {}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function filter() {
|
|
102
|
+
const query = input.value.trim().toLowerCase()
|
|
103
|
+
items = commands
|
|
104
|
+
.list()
|
|
105
|
+
.filter((command) => command.showInPalette !== false)
|
|
106
|
+
.map((command) => ({ command, score: score(command, query) }))
|
|
107
|
+
.filter((entry) => entry.score > 0)
|
|
108
|
+
.sort((a, b) => b.score - a.score)
|
|
109
|
+
.map((entry) => entry.command)
|
|
110
|
+
|
|
111
|
+
index = 0
|
|
112
|
+
render()
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function render() {
|
|
116
|
+
list.replaceChildren(...items.map(row))
|
|
117
|
+
empty.toggleAttribute("data-visible", items.length === 0)
|
|
118
|
+
mark()
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {DocserveCommand} command
|
|
123
|
+
* @returns {HTMLElement}
|
|
124
|
+
*/
|
|
125
|
+
function row(command) {
|
|
126
|
+
const keys = command.keys ?? []
|
|
127
|
+
const node = el("li", {
|
|
128
|
+
class: "docserve-palette__row",
|
|
129
|
+
role: "option",
|
|
130
|
+
id: rowId(command.id),
|
|
131
|
+
"data-command": command.id,
|
|
132
|
+
"aria-selected": "false",
|
|
133
|
+
}, [
|
|
134
|
+
command.icon ? el("span", { class: "docserve-palette__icon", html: command.icon }) : null,
|
|
135
|
+
el("span", { class: "docserve-palette__text" }, [
|
|
136
|
+
el("span", { class: "docserve-palette__title", text: command.title }),
|
|
137
|
+
command.description ? el("span", { class: "docserve-palette__hint", text: command.description }) : null,
|
|
138
|
+
]),
|
|
139
|
+
keys.length
|
|
140
|
+
? el("span", { class: "docserve-palette__keys" }, keys.map((key) => el("kbd", { class: "docserve-palette__key", text: formatKey(key) })))
|
|
141
|
+
: null,
|
|
142
|
+
])
|
|
143
|
+
node.addEventListener("pointermove", () => {
|
|
144
|
+
index = items.indexOf(command)
|
|
145
|
+
mark()
|
|
146
|
+
})
|
|
147
|
+
return node
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function mark() {
|
|
151
|
+
const rows = list.children
|
|
152
|
+
for (let i = 0; i < rows.length; i++) rows[i].setAttribute("aria-selected", String(i === index))
|
|
153
|
+
input.setAttribute("aria-activedescendant", items[index] ? rowId(items[index].id) : "")
|
|
154
|
+
if (items[index]) select(items[index])
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @param {DocserveCommand} command
|
|
159
|
+
*/
|
|
160
|
+
function select(command) {
|
|
161
|
+
const node = list.querySelector(`#${CSS.escape(rowId(command.id))}`)
|
|
162
|
+
node?.scrollIntoView({ block: "nearest" })
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param {KeyboardEvent} event
|
|
167
|
+
*/
|
|
168
|
+
function onKeydown(event) {
|
|
169
|
+
if (event.key === "Escape") {
|
|
170
|
+
event.preventDefault()
|
|
171
|
+
event.stopPropagation()
|
|
172
|
+
close()
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
if (event.key === "Enter") {
|
|
176
|
+
event.preventDefault()
|
|
177
|
+
const command = items[index]
|
|
178
|
+
if (command) activate(command.id)
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const step =
|
|
183
|
+
event.key === "ArrowDown" ? 1
|
|
184
|
+
: event.key === "ArrowUp" ? -1
|
|
185
|
+
: event.key === "Home" ? -items.length
|
|
186
|
+
: event.key === "End" ? items.length
|
|
187
|
+
: 0
|
|
188
|
+
if (!step || !items.length) return
|
|
189
|
+
event.preventDefault()
|
|
190
|
+
index = (index + step + items.length) % items.length
|
|
191
|
+
mark()
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param {string} id
|
|
196
|
+
*/
|
|
197
|
+
function activate(id) {
|
|
198
|
+
close()
|
|
199
|
+
commands.run(id).catch((error) => console.error(`[docserve] command failed: ${id}`, error))
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function destroy() {
|
|
203
|
+
off()
|
|
204
|
+
overlay.remove()
|
|
205
|
+
document.documentElement.removeAttribute("data-docserve-palette")
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Rank a command against the query: title prefix beats word prefix beats
|
|
211
|
+
* substring beats a hint/group match beats a scattered subsequence.
|
|
212
|
+
* @param {DocserveCommand} command
|
|
213
|
+
* @param {string} query
|
|
214
|
+
* @returns {number}
|
|
215
|
+
*/
|
|
216
|
+
function score(command, query) {
|
|
217
|
+
if (!query) return 1
|
|
218
|
+
const title = command.title.toLowerCase()
|
|
219
|
+
if (title.startsWith(query)) return 100
|
|
220
|
+
if (title.split(/[\s:/_-]+/).some((word) => word.startsWith(query))) return 80
|
|
221
|
+
if (title.includes(query)) return 60
|
|
222
|
+
if (`${command.description ?? ""} ${command.category ?? ""}`.toLowerCase().includes(query)) return 40
|
|
223
|
+
return subsequence(query, title) ? 20 : 0
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* @param {string} needle
|
|
228
|
+
* @param {string} haystack
|
|
229
|
+
* @returns {boolean}
|
|
230
|
+
*/
|
|
231
|
+
function subsequence(needle, haystack) {
|
|
232
|
+
let at = 0
|
|
233
|
+
for (const char of needle) {
|
|
234
|
+
at = haystack.indexOf(char, at)
|
|
235
|
+
if (at === -1) return false
|
|
236
|
+
at += 1
|
|
237
|
+
}
|
|
238
|
+
return true
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @param {string} id
|
|
243
|
+
* @returns {string}
|
|
244
|
+
*/
|
|
245
|
+
function rowId(id) {
|
|
246
|
+
return `docserve-cmd-${id.replace(/[^\w-]/g, "_")}`
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @param {string} key
|
|
251
|
+
* @param {string} label
|
|
252
|
+
* @returns {HTMLElement}
|
|
253
|
+
*/
|
|
254
|
+
function hint(key, label) {
|
|
255
|
+
return el("span", { class: "docserve-palette__legend" }, [
|
|
256
|
+
el("kbd", { text: key }),
|
|
257
|
+
el("span", { text: label }),
|
|
258
|
+
])
|
|
259
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Placement: four viewport corners, drag the pill to move between them, and the
|
|
2
|
+
// choice is remembered across loads.
|
|
3
|
+
import { persisted } from "./lib/storage.js"
|
|
4
|
+
|
|
5
|
+
const STORAGE_KEY = "docserve:dock-corner"
|
|
6
|
+
/** Travel before a press counts as a drag, so clicks still click. */
|
|
7
|
+
const DRAG_THRESHOLD = 4
|
|
8
|
+
/** Gap from the viewport edge while dragging; the corner rest position is set in CSS. */
|
|
9
|
+
const DRAG_MARGIN = 8
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} DragState
|
|
13
|
+
* @property {number} id
|
|
14
|
+
* @property {number} x
|
|
15
|
+
* @property {number} y
|
|
16
|
+
* @property {number} left
|
|
17
|
+
* @property {number} top
|
|
18
|
+
* @property {number} width
|
|
19
|
+
* @property {number} height
|
|
20
|
+
* @property {boolean} moved
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {object} options
|
|
25
|
+
* @param {HTMLElement} options.host Shadow host; carries `data-corner`.
|
|
26
|
+
* @param {HTMLElement} options.handle The dock pill, used as the drag surface.
|
|
27
|
+
* @returns {{ corner: () => string, destroy: () => void }}
|
|
28
|
+
*/
|
|
29
|
+
export function createPlacement({ host, handle }) {
|
|
30
|
+
const saved = persisted(STORAGE_KEY, "br")
|
|
31
|
+
host.dataset.corner = saved.value
|
|
32
|
+
|
|
33
|
+
/** @type {DragState | null} */
|
|
34
|
+
let drag = null
|
|
35
|
+
// A drag that ends over a button must not also activate it.
|
|
36
|
+
let swallowClick = false
|
|
37
|
+
/** @type {ReturnType<typeof setTimeout> | null} */
|
|
38
|
+
let settleTimer = null
|
|
39
|
+
|
|
40
|
+
handle.addEventListener("pointerdown", onDown)
|
|
41
|
+
host.addEventListener("click", onClick, true)
|
|
42
|
+
window.addEventListener("blur", cancel)
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
corner() {
|
|
46
|
+
return host.dataset.corner ?? "br"
|
|
47
|
+
},
|
|
48
|
+
destroy,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @param {PointerEvent} event
|
|
53
|
+
*/
|
|
54
|
+
function onDown(event) {
|
|
55
|
+
if (drag || !event.isPrimary || event.button !== 0) return
|
|
56
|
+
swallowClick = false
|
|
57
|
+
if (settleTimer) settle()
|
|
58
|
+
|
|
59
|
+
const rect = host.getBoundingClientRect()
|
|
60
|
+
drag = {
|
|
61
|
+
id: event.pointerId,
|
|
62
|
+
x: event.clientX,
|
|
63
|
+
y: event.clientY,
|
|
64
|
+
left: rect.left,
|
|
65
|
+
top: rect.top,
|
|
66
|
+
width: rect.width,
|
|
67
|
+
height: rect.height,
|
|
68
|
+
moved: false,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Track on the window instead of capturing the pointer: an active pointer
|
|
72
|
+
// capture retargets the click to the capture element, which would swallow
|
|
73
|
+
// every button press.
|
|
74
|
+
window.addEventListener("pointermove", onMove)
|
|
75
|
+
window.addEventListener("pointerup", onUp)
|
|
76
|
+
window.addEventListener("pointercancel", cancel)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @param {PointerEvent} event
|
|
81
|
+
*/
|
|
82
|
+
function onMove(event) {
|
|
83
|
+
if (!drag || event.pointerId !== drag.id) return
|
|
84
|
+
|
|
85
|
+
const dx = event.clientX - drag.x
|
|
86
|
+
const dy = event.clientY - drag.y
|
|
87
|
+
if (!drag.moved) {
|
|
88
|
+
if (Math.hypot(dx, dy) < DRAG_THRESHOLD) return
|
|
89
|
+
drag.moved = true
|
|
90
|
+
host.toggleAttribute("data-dragging", true)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Follow the pointer in px; corners are re-applied on release.
|
|
94
|
+
const maxLeft = Math.max(DRAG_MARGIN, window.innerWidth - drag.width - DRAG_MARGIN)
|
|
95
|
+
const maxTop = Math.max(DRAG_MARGIN, window.innerHeight - drag.height - DRAG_MARGIN)
|
|
96
|
+
host.style.left = `${clamp(drag.left + dx, DRAG_MARGIN, maxLeft)}px`
|
|
97
|
+
host.style.top = `${clamp(drag.top + dy, DRAG_MARGIN, maxTop)}px`
|
|
98
|
+
host.style.right = "auto"
|
|
99
|
+
host.style.bottom = "auto"
|
|
100
|
+
event.preventDefault()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {PointerEvent} event
|
|
105
|
+
*/
|
|
106
|
+
function onUp(event) {
|
|
107
|
+
if (!drag || event.pointerId !== drag.id) return
|
|
108
|
+
const moved = drag.moved
|
|
109
|
+
const from = host.getBoundingClientRect()
|
|
110
|
+
finish()
|
|
111
|
+
if (!moved) return
|
|
112
|
+
swallowClick = true
|
|
113
|
+
|
|
114
|
+
// Snap to the corner nearest the drop point.
|
|
115
|
+
const vertical = from.top + from.height / 2 < window.innerHeight / 2 ? "t" : "b"
|
|
116
|
+
const horizontal = from.left + from.width / 2 < window.innerWidth / 2 ? "l" : "r"
|
|
117
|
+
snap(`${vertical}${horizontal}`, from)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Glide from the drop point to the corner. The resting rules switch the free
|
|
122
|
+
* axis to `auto`, which CSS cannot interpolate, so both ends are measured in
|
|
123
|
+
* px here and the travel is animated explicitly.
|
|
124
|
+
* @param {string} corner
|
|
125
|
+
* @param {DOMRect} from
|
|
126
|
+
*/
|
|
127
|
+
function snap(corner, from) {
|
|
128
|
+
host.style.transition = "none"
|
|
129
|
+
host.dataset.corner = corner
|
|
130
|
+
saved.value = corner
|
|
131
|
+
clearInline()
|
|
132
|
+
const to = host.getBoundingClientRect()
|
|
133
|
+
|
|
134
|
+
host.style.right = "auto"
|
|
135
|
+
host.style.bottom = "auto"
|
|
136
|
+
host.style.left = `${from.left}px`
|
|
137
|
+
host.style.top = `${from.top}px`
|
|
138
|
+
void host.offsetWidth // commit the start, so the change below animates
|
|
139
|
+
|
|
140
|
+
host.style.transition = ""
|
|
141
|
+
host.style.left = `${to.left}px`
|
|
142
|
+
host.style.top = `${to.top}px`
|
|
143
|
+
host.addEventListener("transitionend", settle, { once: true })
|
|
144
|
+
settleTimer = setTimeout(settle, 250)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Hand positioning back to the corner rules once the glide is over.
|
|
148
|
+
function settle() {
|
|
149
|
+
if (settleTimer) clearTimeout(settleTimer)
|
|
150
|
+
settleTimer = null
|
|
151
|
+
host.removeEventListener("transitionend", settle)
|
|
152
|
+
if (drag) return
|
|
153
|
+
host.style.removeProperty("transition")
|
|
154
|
+
clearInline()
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function cancel() {
|
|
158
|
+
if (!drag) return
|
|
159
|
+
const moved = drag.moved
|
|
160
|
+
finish()
|
|
161
|
+
if (moved) clearInline()
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function finish() {
|
|
165
|
+
if (!drag) return
|
|
166
|
+
window.removeEventListener("pointermove", onMove)
|
|
167
|
+
window.removeEventListener("pointerup", onUp)
|
|
168
|
+
window.removeEventListener("pointercancel", cancel)
|
|
169
|
+
drag = null
|
|
170
|
+
host.toggleAttribute("data-dragging", false)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function clearInline() {
|
|
174
|
+
for (const property of ["left", "top", "right", "bottom"]) host.style.removeProperty(property)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* @param {MouseEvent} event
|
|
179
|
+
*/
|
|
180
|
+
function onClick(event) {
|
|
181
|
+
if (!swallowClick) return
|
|
182
|
+
swallowClick = false
|
|
183
|
+
event.stopPropagation()
|
|
184
|
+
event.preventDefault()
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function destroy() {
|
|
188
|
+
cancel()
|
|
189
|
+
settle()
|
|
190
|
+
handle.removeEventListener("pointerdown", onDown)
|
|
191
|
+
host.removeEventListener("click", onClick, true)
|
|
192
|
+
window.removeEventListener("blur", cancel)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param {number} value
|
|
198
|
+
* @param {number} min
|
|
199
|
+
* @param {number} max
|
|
200
|
+
* @returns {number}
|
|
201
|
+
*/
|
|
202
|
+
function clamp(value, min, max) {
|
|
203
|
+
return Math.min(Math.max(value, min), max)
|
|
204
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Plugin registry: run `setup` once per id and keep its cleanup.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {() => DocserveContext} getContext Resolved when a plugin is set up,
|
|
5
|
+
* so the registry can exist before the context is fully assembled.
|
|
6
|
+
* @returns {{ use: (plugin: DocservePlugin) => DocserveContext, dispose: () => void }}
|
|
7
|
+
*/
|
|
8
|
+
export function createPlugins(getContext) {
|
|
9
|
+
/** @type {Map<string, () => void>} */
|
|
10
|
+
const cleanups = new Map()
|
|
11
|
+
|
|
12
|
+
return { use, dispose }
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {DocservePlugin} plugin
|
|
16
|
+
* @returns {DocserveContext}
|
|
17
|
+
*/
|
|
18
|
+
function use(plugin) {
|
|
19
|
+
const ctx = getContext()
|
|
20
|
+
if (!plugin || typeof plugin.id !== "string" || !plugin.id) throw new TypeError("use expects a plugin with an id")
|
|
21
|
+
if (cleanups.has(plugin.id)) return ctx
|
|
22
|
+
|
|
23
|
+
const cleanup = plugin.setup?.(ctx)
|
|
24
|
+
cleanups.set(plugin.id, typeof cleanup === "function" ? cleanup : () => {})
|
|
25
|
+
return ctx
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Run every cleanup in reverse registration order. */
|
|
29
|
+
function dispose() {
|
|
30
|
+
for (const cleanup of [...cleanups.values()].reverse()) {
|
|
31
|
+
try {
|
|
32
|
+
cleanup()
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error("[docserve] plugin cleanup failed", error)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
cleanups.clear()
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// In-page method bus. `docserve:` is reserved for core methods; plugins use
|
|
2
|
+
// `plugin:<id>:<method>`.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @returns {RpcContext}
|
|
6
|
+
*/
|
|
7
|
+
export function createRpc() {
|
|
8
|
+
/** @type {Map<string, (...args: any[]) => any>} */
|
|
9
|
+
const methods = new Map()
|
|
10
|
+
|
|
11
|
+
return {
|
|
12
|
+
register(name, handler) {
|
|
13
|
+
if (typeof name !== "string" || !name) throw new TypeError("rpc.register expects a method name")
|
|
14
|
+
if (typeof handler !== "function") throw new TypeError(`rpc.register(${name}) expects a function`)
|
|
15
|
+
if (methods.has(name)) throw new Error(`rpc method already registered: ${name}`)
|
|
16
|
+
methods.set(name, handler)
|
|
17
|
+
return () => {
|
|
18
|
+
if (methods.get(name) === handler) methods.delete(name)
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
has(name) {
|
|
23
|
+
return methods.has(name)
|
|
24
|
+
},
|
|
25
|
+
|
|
26
|
+
async call(name, ...args) {
|
|
27
|
+
const handler = methods.get(name)
|
|
28
|
+
if (!handler) throw new Error(`unknown rpc method: ${name}`)
|
|
29
|
+
return handler(...args)
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Widget stylesheets: waiting for them to load, and re-fetching them on change.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {HTMLLinkElement} link
|
|
5
|
+
* @returns {Promise<void>}
|
|
6
|
+
*/
|
|
7
|
+
export function settled(link) {
|
|
8
|
+
if (link.sheet) return Promise.resolve()
|
|
9
|
+
return new Promise((resolve) => {
|
|
10
|
+
link.addEventListener("load", () => resolve(), { once: true })
|
|
11
|
+
link.addEventListener("error", () => resolve(), { once: true })
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Re-fetch every same-origin stylesheet, page and shadow root alike, so edits
|
|
17
|
+
* apply without a reload.
|
|
18
|
+
* @param {ParentNode[]} scopes
|
|
19
|
+
*/
|
|
20
|
+
export function reloadStyles(scopes) {
|
|
21
|
+
const stamp = String(Date.now())
|
|
22
|
+
for (const scope of scopes) {
|
|
23
|
+
for (const link of scope.querySelectorAll('link[rel~="stylesheet"]')) {
|
|
24
|
+
if (!(link instanceof HTMLLinkElement)) continue
|
|
25
|
+
const url = new URL(link.href, location.href)
|
|
26
|
+
if (url.origin !== location.origin) continue
|
|
27
|
+
url.searchParams.set("__docserve_rev", stamp)
|
|
28
|
+
link.href = url.href
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/widget/ui.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// UI helpers handed to plugins: mount points, stylesheets, announcements.
|
|
2
|
+
import { el } from "./lib/dom.js"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {object} options
|
|
6
|
+
* @param {ShadowRoot} options.root
|
|
7
|
+
* @param {HTMLElement} options.status Live region for `announce`.
|
|
8
|
+
* @returns {UiContext}
|
|
9
|
+
*/
|
|
10
|
+
export function createUi({ root, status }) {
|
|
11
|
+
/** @type {Set<string>} */
|
|
12
|
+
const added = new Set()
|
|
13
|
+
|
|
14
|
+
style("/__docserve/widget/widget.css")
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
mountPoint,
|
|
18
|
+
style,
|
|
19
|
+
announce,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Layout-neutral host in the shadow root for plugin UIs.
|
|
24
|
+
* @returns {HTMLElement}
|
|
25
|
+
*/
|
|
26
|
+
function mountPoint() {
|
|
27
|
+
const element = el("div", { "data-docserve-mount": "" })
|
|
28
|
+
element.style.display = "contents"
|
|
29
|
+
root.append(element)
|
|
30
|
+
return element
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Add a stylesheet inside the shadow root once.
|
|
35
|
+
* @param {string} href
|
|
36
|
+
*/
|
|
37
|
+
function style(href) {
|
|
38
|
+
if (added.has(href)) return
|
|
39
|
+
added.add(href)
|
|
40
|
+
|
|
41
|
+
root.append(el("link", { rel: "stylesheet", href }))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} message
|
|
46
|
+
*/
|
|
47
|
+
function announce(message) {
|
|
48
|
+
status.textContent = message
|
|
49
|
+
}
|
|
50
|
+
}
|