@bigmistqke/workpad 0.0.0-stage → 0.18.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/LICENSE +21 -0
- package/README.md +781 -2
- package/bin/workpad.ts +1526 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1202 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2121 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +213 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +348 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +761 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +592 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +134 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +166 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +240 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +435 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +845 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// The shared server's overview as columns (src/pages/board.ts): both layouts are on the page, a
|
|
2
|
+
// column per state and a column per project, and the reader's switch shows one. The choice is
|
|
3
|
+
// the page's address (`?columns=project`), so a link keeps it, and kept in this browser for the next
|
|
4
|
+
// visit to the bare address. It lives on <html>, outside what a live update writes again, so an
|
|
5
|
+
// update never switches back.
|
|
6
|
+
import { storageKey } from '../base.ts'
|
|
7
|
+
|
|
8
|
+
const KEY = storageKey('wp-board')
|
|
9
|
+
const VIEWS = ['state', 'project'] as const
|
|
10
|
+
type View = (typeof VIEWS)[number]
|
|
11
|
+
const isView = (v: string | null | undefined): v is View => VIEWS.includes(v as View)
|
|
12
|
+
|
|
13
|
+
function chosen(): View {
|
|
14
|
+
const asked = new URLSearchParams(location.search).get('columns')
|
|
15
|
+
if (isView(asked)) return asked
|
|
16
|
+
try {
|
|
17
|
+
const kept = localStorage.getItem(KEY)
|
|
18
|
+
if (isView(kept)) return kept
|
|
19
|
+
} catch {
|
|
20
|
+
/* private window */
|
|
21
|
+
}
|
|
22
|
+
return 'state'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Show `view`, and say which on the switch (written again by a live update, so asked each time). */
|
|
26
|
+
export function applyBoard(view: View = (document.documentElement.dataset.wpBoard as View) ?? chosen()) {
|
|
27
|
+
if (!document.querySelector('.wp-board')) return
|
|
28
|
+
document.documentElement.dataset.wpBoard = view
|
|
29
|
+
for (const b of document.querySelectorAll<HTMLButtonElement>('button[data-wp-board]'))
|
|
30
|
+
b.setAttribute('aria-pressed', String(b.dataset.wpBoard === view))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ---- search -----------------------------------------------------------------------------------
|
|
34
|
+
// A query narrows both layouts to what matches every word of it: a page by its title and its
|
|
35
|
+
// project, a file by its title and name, a project by its name (then all of it stays). Columns
|
|
36
|
+
// left empty go; folds with a match open while it lasts. The query is the address's `?q=`, and
|
|
37
|
+
// lives here, not in the input a live update writes again, so it is applied again after each.
|
|
38
|
+
|
|
39
|
+
let query = new URLSearchParams(location.search).get('q') ?? ''
|
|
40
|
+
|
|
41
|
+
const ITEMS = '.wp-board-card, .wp-folded-documents .wp-index li, .wp-board-empty li'
|
|
42
|
+
|
|
43
|
+
/** Narrow the overview to `query` (all of it with none); asked again after each live update. */
|
|
44
|
+
export function applySearch() {
|
|
45
|
+
const input = document.querySelector<HTMLInputElement>('.wp-board-search')
|
|
46
|
+
if (!input) return
|
|
47
|
+
if (input.value !== query && document.activeElement !== input) input.value = query
|
|
48
|
+
const words = query.toLowerCase().split(/\s+/).filter(Boolean)
|
|
49
|
+
const hit = (text: string) => words.every(w => text.includes(w))
|
|
50
|
+
document.documentElement.toggleAttribute('data-wp-searching', words.length > 0)
|
|
51
|
+
for (const board of document.querySelectorAll<HTMLElement>('.wp-board')) {
|
|
52
|
+
let columns = 0
|
|
53
|
+
for (const col of board.querySelectorAll<HTMLElement>(':scope > .wp-board-column')) {
|
|
54
|
+
const head = (col.querySelector('.wp-board-head, :scope > summary')?.textContent ?? '').toLowerCase()
|
|
55
|
+
const whole = words.length > 0 && hit(head)
|
|
56
|
+
let shown = 0
|
|
57
|
+
for (const item of col.querySelectorAll<HTMLElement>(ITEMS)) {
|
|
58
|
+
const show = !words.length || whole || hit(`${item.textContent ?? ''} ${head}`.toLowerCase())
|
|
59
|
+
item.hidden = !show
|
|
60
|
+
if (show) shown++
|
|
61
|
+
}
|
|
62
|
+
// a fold with a match opens while searching, and closes again after, as it was
|
|
63
|
+
const folds = [...col.querySelectorAll<HTMLDetailsElement>('details')]
|
|
64
|
+
// the column of projects without pages is a fold itself
|
|
65
|
+
if (col instanceof HTMLDetailsElement) folds.unshift(col)
|
|
66
|
+
for (const d of folds) {
|
|
67
|
+
const has = [...d.querySelectorAll<HTMLElement>(ITEMS)].some(i => !i.hidden)
|
|
68
|
+
if (words.length && has && !d.open) {
|
|
69
|
+
d.open = true
|
|
70
|
+
d.dataset.wpSearchOpened = ''
|
|
71
|
+
} else if ((!words.length || !has) && 'wpSearchOpened' in d.dataset) {
|
|
72
|
+
d.open = false
|
|
73
|
+
delete d.dataset.wpSearchOpened
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
col.hidden = words.length > 0 && !whole && shown === 0
|
|
77
|
+
if (!col.hidden) columns++
|
|
78
|
+
}
|
|
79
|
+
board.dataset.wpMatches = String(columns)
|
|
80
|
+
}
|
|
81
|
+
const none = document.querySelector<HTMLElement>('.wp-board-nomatch')
|
|
82
|
+
const current = document.querySelector<HTMLElement>(`.wp-board[data-board="${document.documentElement.dataset.wpBoard ?? 'state'}"]`)
|
|
83
|
+
if (none) none.hidden = !words.length || current?.dataset.wpMatches !== '0'
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function setQuery(q: string) {
|
|
87
|
+
query = q
|
|
88
|
+
const url = new URL(location.href)
|
|
89
|
+
if (q) url.searchParams.set('q', q)
|
|
90
|
+
else url.searchParams.delete('q')
|
|
91
|
+
history.replaceState(history.state, '', url)
|
|
92
|
+
applySearch()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function trackSearch() {
|
|
96
|
+
document.addEventListener('input', e => {
|
|
97
|
+
const input = e.target as HTMLInputElement
|
|
98
|
+
if (input.matches?.('.wp-board-search')) setQuery(input.value)
|
|
99
|
+
})
|
|
100
|
+
document.addEventListener('keydown', e => {
|
|
101
|
+
const input = document.querySelector<HTMLInputElement>('.wp-board-search')
|
|
102
|
+
if (!input) return
|
|
103
|
+
const typing = (e.target as HTMLElement).closest?.('input, textarea, select, [contenteditable]')
|
|
104
|
+
// `/` anywhere but in a field: to the search
|
|
105
|
+
if (e.key === '/' && !typing && !e.metaKey && !e.ctrlKey && !e.altKey) {
|
|
106
|
+
e.preventDefault()
|
|
107
|
+
input.focus()
|
|
108
|
+
input.select()
|
|
109
|
+
} else if (e.key === 'Escape' && e.target === input) {
|
|
110
|
+
input.value = ''
|
|
111
|
+
setQuery('')
|
|
112
|
+
input.blur()
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
applySearch()
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function trackBoard() {
|
|
119
|
+
document.addEventListener('click', e => {
|
|
120
|
+
const button = (e.target as Element).closest?.<HTMLButtonElement>('button[data-wp-board]')
|
|
121
|
+
const view = button?.dataset.wpBoard
|
|
122
|
+
if (!isView(view)) return
|
|
123
|
+
try {
|
|
124
|
+
localStorage.setItem(KEY, view)
|
|
125
|
+
} catch {
|
|
126
|
+
/* private window */
|
|
127
|
+
}
|
|
128
|
+
const url = new URL(location.href)
|
|
129
|
+
if (view === 'state') url.searchParams.delete('columns')
|
|
130
|
+
else url.searchParams.set('columns', view)
|
|
131
|
+
history.replaceState(history.state, '', url)
|
|
132
|
+
applyBoard(view)
|
|
133
|
+
applySearch()
|
|
134
|
+
})
|
|
135
|
+
// a project's files fold without its files in it: the server lists them once it is opened
|
|
136
|
+
document.addEventListener(
|
|
137
|
+
'toggle',
|
|
138
|
+
e => {
|
|
139
|
+
const fold = e.target as HTMLDetailsElement
|
|
140
|
+
if (fold.dataset?.wpFiles && fold.open) void loadFiles(fold)
|
|
141
|
+
},
|
|
142
|
+
true,
|
|
143
|
+
)
|
|
144
|
+
applyBoard(chosen())
|
|
145
|
+
trackSearch()
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function loadFiles(fold: HTMLDetailsElement) {
|
|
149
|
+
if (fold.dataset.wpLoaded) return
|
|
150
|
+
fold.dataset.wpLoaded = 'loading'
|
|
151
|
+
const count = fold.querySelector('summary .wp-meta')
|
|
152
|
+
if (count) count.textContent = '…'
|
|
153
|
+
try {
|
|
154
|
+
const res = await fetch(`/api/files?base=${encodeURIComponent(fold.dataset.wpFiles!)}`)
|
|
155
|
+
const body = (await res.json()) as { count?: number; html?: string; error?: string }
|
|
156
|
+
if (!res.ok) throw new Error(body.error ?? res.statusText)
|
|
157
|
+
if (count) count.textContent = String(body.count)
|
|
158
|
+
fold.insertAdjacentHTML('beforeend', body.count ? body.html! : '<p class="wp-meta">No markdown files.</p>')
|
|
159
|
+
fold.dataset.wpLoaded = 'yes'
|
|
160
|
+
applySearch()
|
|
161
|
+
} catch (err) {
|
|
162
|
+
delete fold.dataset.wpLoaded
|
|
163
|
+
if (count) count.textContent = ''
|
|
164
|
+
fold.insertAdjacentHTML('beforeend', `<p class="wp-meta">Couldn't list the files: ${String((err as Error).message)}</p>`)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// What waits for the reader on other pages: a menu in the page's header, there only while another
|
|
2
|
+
// page has a question they haven't answered or a message from the agent they haven't read. It
|
|
3
|
+
// lists those pages, each with how many; a click goes to the page, which goes to the first of them
|
|
4
|
+
// (`#wp-waiting`, see waiting.ts). This page's own are in its corner, not here.
|
|
5
|
+
import type { Api, PageHandle, PageStatus } from '../../define.ts'
|
|
6
|
+
import { INBOX_ICON, awaitingLabel } from '../../icons.ts'
|
|
7
|
+
import { awaiting } from '../../status.ts'
|
|
8
|
+
import { base, url } from '../base.ts'
|
|
9
|
+
|
|
10
|
+
/** A link to a page that scrolls it to its first waiting question. */
|
|
11
|
+
export const WAITING_HASH = '#wp-waiting'
|
|
12
|
+
|
|
13
|
+
const box = document.createElement('div')
|
|
14
|
+
box.className = 'wp-elsewhere'
|
|
15
|
+
box.hidden = true
|
|
16
|
+
box.innerHTML =
|
|
17
|
+
'<button type="button" class="wp-elsewhere-toggle wp-awaiting" aria-haspopup="true" aria-expanded="false"></button><ul class="wp-elsewhere-list" hidden></ul>'
|
|
18
|
+
const toggle = box.querySelector('button')!
|
|
19
|
+
// an inbox, and how many questions wait in it
|
|
20
|
+
toggle.innerHTML = `${INBOX_ICON}<span class="wp-elsewhere-count"></span>`
|
|
21
|
+
const count = toggle.querySelector('span')!
|
|
22
|
+
const list = box.querySelector('ul')!
|
|
23
|
+
|
|
24
|
+
function open(yes: boolean) {
|
|
25
|
+
list.hidden = !yes
|
|
26
|
+
toggle.setAttribute('aria-expanded', String(yes))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let current = ''
|
|
30
|
+
|
|
31
|
+
/** A page with something waiting, in a group of the menu (its project, under a shared server). */
|
|
32
|
+
interface Waiting {
|
|
33
|
+
href: string
|
|
34
|
+
title: string
|
|
35
|
+
n: number
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** This project's pages with something waiting, but this one. */
|
|
39
|
+
async function here(): Promise<Waiting[]> {
|
|
40
|
+
const pages = (await (await fetch(url('/api/status'), { cache: 'no-store' })).json()) as PageStatus[]
|
|
41
|
+
return pages
|
|
42
|
+
.filter(p => p.page !== current)
|
|
43
|
+
.map(p => ({
|
|
44
|
+
href: `${base}/${p.page.split('/').map(encodeURIComponent).join('/')}${WAITING_HASH}`,
|
|
45
|
+
title: p.title,
|
|
46
|
+
n: awaiting(p),
|
|
47
|
+
}))
|
|
48
|
+
.filter(w => w.n > 0)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Under a shared server (see Entries in DESIGN.md): the other projects of this one's entry, by
|
|
53
|
+
* project, as the server's index has them; empty on a server of its own, or when it can't say.
|
|
54
|
+
*/
|
|
55
|
+
async function others(): Promise<{ label: string; pages: Waiting[] }[]> {
|
|
56
|
+
if (!base) return []
|
|
57
|
+
const entry = base.split('/')[1]
|
|
58
|
+
try {
|
|
59
|
+
const res = await fetch(`/api/index?entry=${encodeURIComponent(entry ?? '')}`, { cache: 'no-store' })
|
|
60
|
+
if (!res.ok) return []
|
|
61
|
+
const projects = (await res.json()) as {
|
|
62
|
+
label: string
|
|
63
|
+
base: string
|
|
64
|
+
pages: { title: string; url: string; open: string[]; unread: number }[]
|
|
65
|
+
}[]
|
|
66
|
+
return projects
|
|
67
|
+
.filter(p => p.base !== base)
|
|
68
|
+
.map(p => ({
|
|
69
|
+
label: p.label,
|
|
70
|
+
pages: p.pages
|
|
71
|
+
.map(x => ({ href: `${x.url}${WAITING_HASH}`, title: x.title, n: x.open.length + x.unread }))
|
|
72
|
+
.filter(w => w.n > 0),
|
|
73
|
+
}))
|
|
74
|
+
.filter(g => g.pages.length)
|
|
75
|
+
} catch {
|
|
76
|
+
return []
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function load() {
|
|
81
|
+
let mine: Waiting[]
|
|
82
|
+
let rest: { label: string; pages: Waiting[] }[]
|
|
83
|
+
try {
|
|
84
|
+
;[mine, rest] = await Promise.all([here(), others()])
|
|
85
|
+
} catch {
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
const all = [...mine, ...rest.flatMap(g => g.pages)]
|
|
89
|
+
const total = all.reduce((sum, w) => sum + w.n, 0)
|
|
90
|
+
box.hidden = total === 0
|
|
91
|
+
if (!total) open(false)
|
|
92
|
+
count.textContent = String(total)
|
|
93
|
+
toggle.title = awaitingLabel(total, ` on ${all.length === 1 ? 'another page' : `${all.length} other pages`}`)
|
|
94
|
+
toggle.setAttribute('aria-label', toggle.title)
|
|
95
|
+
const item = (w: Waiting) => {
|
|
96
|
+
const li = document.createElement('li')
|
|
97
|
+
const a = document.createElement('a')
|
|
98
|
+
a.href = w.href
|
|
99
|
+
a.textContent = `${w.title} (${w.n})`
|
|
100
|
+
li.append(a)
|
|
101
|
+
return li
|
|
102
|
+
}
|
|
103
|
+
const heading = (text: string) =>
|
|
104
|
+
Object.assign(document.createElement('li'), { className: 'wp-elsewhere-group', textContent: text, title: text })
|
|
105
|
+
// this project's own first; the others by project, named only when there are others
|
|
106
|
+
list.replaceChildren(
|
|
107
|
+
...(rest.length && mine.length ? [heading('here')] : []),
|
|
108
|
+
...mine.map(item),
|
|
109
|
+
...rest.flatMap(g => [heading(g.label), ...g.pages.map(item)]),
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// several changes at once (an answer is an event and a listener less) load once; a hidden tab
|
|
114
|
+
// loads once it is shown, and not before (the index asks every project of the server)
|
|
115
|
+
let timer = 0
|
|
116
|
+
let stale = false
|
|
117
|
+
const reload = () => {
|
|
118
|
+
clearTimeout(timer)
|
|
119
|
+
if (document.hidden) return void (stale = true)
|
|
120
|
+
timer = window.setTimeout(load, 100)
|
|
121
|
+
}
|
|
122
|
+
document.addEventListener('visibilitychange', () => {
|
|
123
|
+
if (document.hidden || !stale) return
|
|
124
|
+
stale = false
|
|
125
|
+
reload()
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
export function trackElsewhere(root: HTMLElement, handle: PageHandle, core: Api) {
|
|
129
|
+
current = handle.id
|
|
130
|
+
const nav = root.querySelector('.wp-nav')
|
|
131
|
+
if (!nav) return
|
|
132
|
+
// before the plugins' place in the header bar (its header slot), and the theme button after it
|
|
133
|
+
;(nav.querySelector('[data-wp-slot=header]') ?? nav.querySelector('.wp-theme'))?.before(box)
|
|
134
|
+
toggle.addEventListener('click', () => open(!!list.hidden))
|
|
135
|
+
document.addEventListener('click', e => {
|
|
136
|
+
if (!box.contains(e.target as Node)) open(false)
|
|
137
|
+
})
|
|
138
|
+
document.addEventListener('keydown', e => {
|
|
139
|
+
if (e.key === 'Escape' && !list.hidden) {
|
|
140
|
+
open(false)
|
|
141
|
+
toggle.focus()
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
// what waits changes with an answer (events), a wait starting or ending (listeners), and a page's
|
|
145
|
+
// forms (a page changed)
|
|
146
|
+
core.events.on(reload)
|
|
147
|
+
core.listeners.on(reload)
|
|
148
|
+
handle.onChange(reload)
|
|
149
|
+
load()
|
|
150
|
+
// another project's pages, log or listeners changed: the shared server says so on the live
|
|
151
|
+
// channel (and the channel opening again may have missed some)
|
|
152
|
+
if (base) document.addEventListener('wp-elsewhere', reload)
|
|
153
|
+
}
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
// New-content tracking. A top-level block of the page is "new" when its content was not on the
|
|
2
|
+
// page last time the reader saw it: since the previous live update, or since their previous
|
|
3
|
+
// visit (remembered per page in localStorage). New blocks get .wp-new (a left border) until they
|
|
4
|
+
// have been in view for a moment with the tab visible. Floating buttons jump to unseen blocks.
|
|
5
|
+
|
|
6
|
+
import { html } from '../../html.ts'
|
|
7
|
+
import type { Api, Toast } from '../../define.ts'
|
|
8
|
+
import { viewBottom } from './view'
|
|
9
|
+
import { storageKey } from '../base.ts'
|
|
10
|
+
|
|
11
|
+
const DWELL_MS = 600
|
|
12
|
+
const FADE_MS = 2500
|
|
13
|
+
|
|
14
|
+
const sigOf = new WeakMap<Element, string>()
|
|
15
|
+
const unseen = new Set<Element>()
|
|
16
|
+
const timers = new WeakMap<Element, number>()
|
|
17
|
+
let storeKey = ''
|
|
18
|
+
// set by trackInitial: a page that tracks nothing (the overview) has no bubbles
|
|
19
|
+
let core: Api | undefined
|
|
20
|
+
|
|
21
|
+
// the data-wp-* attributes the server writes into a page; every other one is the browser's (a
|
|
22
|
+
// form's state, a mark, a key), and not part of what the server sent
|
|
23
|
+
const SERVER_MARKS = new Set([
|
|
24
|
+
'data-wp-at',
|
|
25
|
+
'data-wp-key',
|
|
26
|
+
'data-wp-maximize',
|
|
27
|
+
'data-wp-other',
|
|
28
|
+
'data-wp-other-text',
|
|
29
|
+
'data-wp-quiet',
|
|
30
|
+
'data-wp-self',
|
|
31
|
+
'data-wp-sort',
|
|
32
|
+
])
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* FNV-1a over the block's HTML as the server sent it, ignoring anything quiet: content inside
|
|
36
|
+
* [data-wp-quiet] can appear, change or go without the block counting as changed. What the browser
|
|
37
|
+
* added is left out, so a block in the page and the same block in a new render have one signature:
|
|
38
|
+
* an island's content (it draws itself; its props are in its key), the page's own elements (a folded
|
|
39
|
+
* form's card), its marks (data-wp-* the server doesn't write: a form's state) and its classes for
|
|
40
|
+
* new content.
|
|
41
|
+
*/
|
|
42
|
+
export function signature(el: Element): string {
|
|
43
|
+
const clone = el.cloneNode(true) as Element
|
|
44
|
+
for (const q of clone.querySelectorAll('[data-wp-quiet], .wp-folded')) q.remove()
|
|
45
|
+
for (const n of [clone, ...clone.querySelectorAll('*')]) {
|
|
46
|
+
for (const { name } of [...n.attributes])
|
|
47
|
+
if (name.startsWith('data-wp-') && !SERVER_MARKS.has(name)) n.removeAttribute(name)
|
|
48
|
+
// a tab's state, which the browser sets as tabs switch (plugins/tabs): which shows, which takes the focus
|
|
49
|
+
if (n.getAttribute('role') === 'tab') {
|
|
50
|
+
n.removeAttribute('aria-selected')
|
|
51
|
+
n.removeAttribute('tabindex')
|
|
52
|
+
}
|
|
53
|
+
n.classList.remove('wp-new', 'wp-new-seen')
|
|
54
|
+
if (!n.classList.length) n.removeAttribute('class')
|
|
55
|
+
}
|
|
56
|
+
for (const island of clone.querySelectorAll('[data-wp-key]')) island.replaceChildren()
|
|
57
|
+
const s = clone.outerHTML
|
|
58
|
+
let h = 0x811c9dc5
|
|
59
|
+
for (let i = 0; i < s.length; i++) h = Math.imul(h ^ s.charCodeAt(i), 0x01000193)
|
|
60
|
+
return (h >>> 0).toString(36)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Signatures of the top-level blocks in an HTML string, in order. */
|
|
64
|
+
export function signaturesOf(html: string): string[] {
|
|
65
|
+
const t = document.createElement('template')
|
|
66
|
+
t.innerHTML = html
|
|
67
|
+
return [...t.content.children].map(signature)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function loadSeen(): Set<string> | null {
|
|
71
|
+
try {
|
|
72
|
+
const raw = localStorage.getItem(storeKey)
|
|
73
|
+
return raw ? new Set(JSON.parse(raw)) : null
|
|
74
|
+
} catch {
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function saveSeen(main: HTMLElement) {
|
|
80
|
+
try {
|
|
81
|
+
const seen = [...main.children]
|
|
82
|
+
.filter(el => !unseen.has(el) && ![...unseen].some(u => el.contains(u)))
|
|
83
|
+
.map(el => sigOf.get(el))
|
|
84
|
+
.filter(Boolean)
|
|
85
|
+
localStorage.setItem(storeKey, JSON.stringify(seen))
|
|
86
|
+
} catch {
|
|
87
|
+
/* storage unavailable: tracking still works for this visit */
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ---- seen detection ----------------------------------------------------------------------------
|
|
92
|
+
// Measured directly on scroll, resize and tab switches (and every half second while anything is
|
|
93
|
+
// unseen, since layout also moves without scrolling). An IntersectionObserver only reports at fixed
|
|
94
|
+
// fractions of a block's own height, which a block taller than twice the screen never reaches.
|
|
95
|
+
|
|
96
|
+
let mainEl: HTMLElement
|
|
97
|
+
|
|
98
|
+
/** In view enough to count as read: half the block, or half the screen for blocks taller than that. */
|
|
99
|
+
function inView(el: Element) {
|
|
100
|
+
const r = el.getBoundingClientRect()
|
|
101
|
+
const bottom = viewBottom()
|
|
102
|
+
const overlap = Math.min(r.bottom, bottom) - Math.max(r.top, 0)
|
|
103
|
+
return overlap > 0 && overlap >= Math.min(r.height, bottom) * 0.5
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Start the dwell timer of unseen blocks in view; stop it for blocks that left the view. */
|
|
107
|
+
function check() {
|
|
108
|
+
const active = document.visibilityState === 'visible'
|
|
109
|
+
for (const el of unseen) {
|
|
110
|
+
if (!el.isConnected) {
|
|
111
|
+
unseen.delete(el)
|
|
112
|
+
continue
|
|
113
|
+
}
|
|
114
|
+
const pending = timers.has(el)
|
|
115
|
+
if (active && inView(el)) {
|
|
116
|
+
if (!pending)
|
|
117
|
+
timers.set(
|
|
118
|
+
el,
|
|
119
|
+
window.setTimeout(() => {
|
|
120
|
+
timers.delete(el)
|
|
121
|
+
markSeen(el)
|
|
122
|
+
}, DWELL_MS),
|
|
123
|
+
)
|
|
124
|
+
} else if (pending) {
|
|
125
|
+
clearTimeout(timers.get(el))
|
|
126
|
+
timers.delete(el)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let checkFrame = 0
|
|
132
|
+
const scheduleCheck = () => {
|
|
133
|
+
cancelAnimationFrame(checkFrame)
|
|
134
|
+
checkFrame = requestAnimationFrame(check)
|
|
135
|
+
}
|
|
136
|
+
addEventListener('scroll', scheduleCheck, { passive: true })
|
|
137
|
+
addEventListener('resize', scheduleCheck)
|
|
138
|
+
document.addEventListener('visibilitychange', scheduleCheck)
|
|
139
|
+
setInterval(() => {
|
|
140
|
+
if (unseen.size) check()
|
|
141
|
+
}, 500)
|
|
142
|
+
|
|
143
|
+
function markSeen(el: Element) {
|
|
144
|
+
if (!unseen.delete(el)) return
|
|
145
|
+
el.classList.add('wp-new-seen')
|
|
146
|
+
setTimeout(() => el.classList.remove('wp-new', 'wp-new-seen'), FADE_MS)
|
|
147
|
+
saveSeen(mainEl)
|
|
148
|
+
update()
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* An item in a list: its bar where the bars of blocks are, not in the list's indent by its bullet.
|
|
153
|
+
* Set inline, so set again after every update (which takes the page's style attributes back).
|
|
154
|
+
*/
|
|
155
|
+
function placeBar(el: Element) {
|
|
156
|
+
if (el.parentElement === mainEl || !mainEl?.contains(el)) return
|
|
157
|
+
const item = el as HTMLElement
|
|
158
|
+
let block: Element = el
|
|
159
|
+
while (block.parentElement && block.parentElement !== mainEl) block = block.parentElement
|
|
160
|
+
item.style.removeProperty('--wp-new-at')
|
|
161
|
+
const base = parseFloat(getComputedStyle(item).getPropertyValue('--wp-new-at')) || 14
|
|
162
|
+
const indent = item.getBoundingClientRect().left - block.getBoundingClientRect().left
|
|
163
|
+
item.style.setProperty('--wp-new-at', `${base + indent}px`)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function markNew(el: Element) {
|
|
167
|
+
// a block that is quiet as a whole is never news
|
|
168
|
+
if (el.matches('[data-wp-quiet]')) return
|
|
169
|
+
el.classList.remove('wp-new-seen')
|
|
170
|
+
el.classList.add('wp-new')
|
|
171
|
+
placeBar(el)
|
|
172
|
+
unseen.add(el)
|
|
173
|
+
scheduleCheck()
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Something that changed in place, outside a page update (an agent's chat message, edited: it says
|
|
178
|
+
* so with `wp-changed`): new until the reader has seen it, as a changed block is.
|
|
179
|
+
*/
|
|
180
|
+
export function markChanged(el: Element) {
|
|
181
|
+
markNew(el)
|
|
182
|
+
update()
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ---- navigator ---------------------------------------------------------------------------------
|
|
186
|
+
|
|
187
|
+
// the "new" toasts, in the page's corner (api.toast): one for what is new above, one for below,
|
|
188
|
+
// each going to the nearest; their ✕ counts everything new as seen. Their count is in the tab title.
|
|
189
|
+
let upToast: Toast | null = null
|
|
190
|
+
let downToast: Toast | null = null
|
|
191
|
+
|
|
192
|
+
function split() {
|
|
193
|
+
const above: Element[] = [],
|
|
194
|
+
below: Element[] = []
|
|
195
|
+
for (const el of unseen) {
|
|
196
|
+
if (!el.isConnected) {
|
|
197
|
+
unseen.delete(el)
|
|
198
|
+
continue
|
|
199
|
+
}
|
|
200
|
+
const r = el.getBoundingClientRect()
|
|
201
|
+
if (r.bottom < 60) above.push(el)
|
|
202
|
+
else if (r.top > viewBottom()) below.push(el)
|
|
203
|
+
}
|
|
204
|
+
const top = (el: Element) => el.getBoundingClientRect().top
|
|
205
|
+
above.sort((a, b) => top(b) - top(a)) // nearest first
|
|
206
|
+
below.sort((a, b) => top(a) - top(b))
|
|
207
|
+
return { above, below }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let raf = 0
|
|
211
|
+
function update() {
|
|
212
|
+
cancelAnimationFrame(raf)
|
|
213
|
+
raf = requestAnimationFrame(() => {
|
|
214
|
+
const { above, below } = split()
|
|
215
|
+
for (const [toast, list, arrow] of [
|
|
216
|
+
[upToast, above, '↑'],
|
|
217
|
+
[downToast, below, '↓'],
|
|
218
|
+
] as const) {
|
|
219
|
+
if (!toast) continue
|
|
220
|
+
if (list.length) {
|
|
221
|
+
toast.update(`${arrow} new (${list.length})`)
|
|
222
|
+
toast.show()
|
|
223
|
+
} else toast.hide()
|
|
224
|
+
}
|
|
225
|
+
core?.badge('new', unseen.size ? String(unseen.size) : null)
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function go(dir: 'up' | 'down') {
|
|
230
|
+
const { above, below } = split()
|
|
231
|
+
;(dir === 'up' ? above : below)[0]?.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
|
232
|
+
}
|
|
233
|
+
const seeAll = () => {
|
|
234
|
+
for (const el of [...unseen]) markSeen(el)
|
|
235
|
+
}
|
|
236
|
+
addEventListener('scroll', update, { passive: true })
|
|
237
|
+
addEventListener('resize', update)
|
|
238
|
+
|
|
239
|
+
// ---- entry points ------------------------------------------------------------------------------
|
|
240
|
+
|
|
241
|
+
/** Below this share of blocks recognised from the last visit, the page counts as new to the reader. */
|
|
242
|
+
const RECOGNISED_MIN = 0.25
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* First render: blocks the reader has not seen on a previous visit are new. On a first visit, or
|
|
246
|
+
* when so little is recognised that the page is effectively a different one (a rewrite, say),
|
|
247
|
+
* nothing is marked: marking every block tells the reader nothing.
|
|
248
|
+
*/
|
|
249
|
+
export function trackInitial(main: HTMLElement, page: string, api: Api) {
|
|
250
|
+
core = api
|
|
251
|
+
upToast = api.toast('', {
|
|
252
|
+
ms: 0,
|
|
253
|
+
kind: 'alert',
|
|
254
|
+
name: 'new-up',
|
|
255
|
+
order: 1,
|
|
256
|
+
onClick: () => go('up'),
|
|
257
|
+
onDismiss: seeAll,
|
|
258
|
+
})
|
|
259
|
+
downToast = api.toast('', {
|
|
260
|
+
ms: 0,
|
|
261
|
+
kind: 'alert',
|
|
262
|
+
name: 'new-down',
|
|
263
|
+
order: 2,
|
|
264
|
+
onClick: () => go('down'),
|
|
265
|
+
onDismiss: seeAll,
|
|
266
|
+
})
|
|
267
|
+
upToast.hide()
|
|
268
|
+
downToast.hide()
|
|
269
|
+
mainEl = main
|
|
270
|
+
storeKey = storageKey(`wp-seen:${page}`)
|
|
271
|
+
const blocks = [...main.children]
|
|
272
|
+
const sigs = blocks.map(signature)
|
|
273
|
+
blocks.forEach((el, i) => sigOf.set(el, sigs[i]))
|
|
274
|
+
const seen = loadSeen()
|
|
275
|
+
const recognised = seen ? sigs.filter(s => seen.has(s)).length : 0
|
|
276
|
+
if (seen && recognised >= blocks.length * RECOGNISED_MIN)
|
|
277
|
+
blocks.forEach((el, i) => {
|
|
278
|
+
if (!seen.has(sigs[i])) markNew(el)
|
|
279
|
+
})
|
|
280
|
+
saveSeen(main)
|
|
281
|
+
update()
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// ---- items --------------------------------------------------------------------------------------
|
|
285
|
+
// A list is often one block (the overview's pages, a long list on a page): one item new in it would
|
|
286
|
+
// mark the whole list. In a block that changed, the items whose content was not on the page before
|
|
287
|
+
// are what is new, and only they are marked; the block only when no item of it is new (what changed
|
|
288
|
+
// is outside its lists, or an item went).
|
|
289
|
+
|
|
290
|
+
/** An item's signature: its content, not its own attributes (its place in a sort) nor the times shown in it. */
|
|
291
|
+
function itemSignature(li: Element) {
|
|
292
|
+
const clone = li.cloneNode(true) as Element
|
|
293
|
+
for (const { name } of [...clone.attributes]) clone.removeAttribute(name)
|
|
294
|
+
for (const t of clone.querySelectorAll('time[data-ts]')) t.textContent = ''
|
|
295
|
+
return signature(clone)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The page's list items, but those in something quiet. */
|
|
299
|
+
const itemsOf = (root: Element) => [...root.querySelectorAll('li')].filter(li => !li.closest('[data-wp-quiet]'))
|
|
300
|
+
|
|
301
|
+
/** How many of each item signature are on the page: what the next update compares with. */
|
|
302
|
+
let itemsBefore = new Map<string, number>()
|
|
303
|
+
export function countItems(main: HTMLElement) {
|
|
304
|
+
mainEl = main
|
|
305
|
+
itemsBefore = new Map()
|
|
306
|
+
for (const li of itemsOf(main)) {
|
|
307
|
+
const sig = itemSignature(li)
|
|
308
|
+
itemsBefore.set(sig, (itemsBefore.get(sig) ?? 0) + 1)
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** The new items in a block that changed: the innermost ones, as an item new inside makes its parent differ too. */
|
|
313
|
+
function newItems(block: Element, before: Map<string, number>) {
|
|
314
|
+
const fresh = itemsOf(block).filter(li => {
|
|
315
|
+
const sig = itemSignature(li)
|
|
316
|
+
const left = before.get(sig) ?? 0
|
|
317
|
+
if (left > 0) before.set(sig, left - 1)
|
|
318
|
+
return left === 0
|
|
319
|
+
})
|
|
320
|
+
return fresh.filter(li => !fresh.some(other => other !== li && li.contains(other)))
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** After a live update: blocks whose content was not on the page before are new, or in a list, the items. */
|
|
324
|
+
export function trackUpdate(main: HTMLElement, previous: string[], next: string[]) {
|
|
325
|
+
const before = new Map<string, number>()
|
|
326
|
+
for (const s of previous) before.set(s, (before.get(s) ?? 0) + 1)
|
|
327
|
+
const items = new Map(itemsBefore)
|
|
328
|
+
const blocks = [...main.children]
|
|
329
|
+
blocks.forEach((el, i) => {
|
|
330
|
+
const sig = next[i]
|
|
331
|
+
sigOf.set(el, sig)
|
|
332
|
+
const left = before.get(sig) ?? 0
|
|
333
|
+
if (left > 0) return before.set(sig, left - 1)
|
|
334
|
+
const fresh = newItems(el, items)
|
|
335
|
+
if (fresh.length) fresh.forEach(markNew)
|
|
336
|
+
else markNew(el)
|
|
337
|
+
})
|
|
338
|
+
countItems(main)
|
|
339
|
+
for (const el of [...unseen]) if (!el.isConnected) unseen.delete(el)
|
|
340
|
+
for (const el of unseen) placeBar(el)
|
|
341
|
+
saveSeen(main)
|
|
342
|
+
update()
|
|
343
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// A click on a section's number (the `1.2` hanging before one of the page's own headings) copies the
|
|
2
|
+
// heading's link and puts it in the address bar, without scrolling. The number is the heading's
|
|
3
|
+
// ::before (base.css), so a click on it lands on the heading itself: it is a click on the number when
|
|
4
|
+
// it falls before where the heading's text starts. For a moment the number shows it was copied.
|
|
5
|
+
|
|
6
|
+
/** The heading whose number was clicked, if the click was on one. */
|
|
7
|
+
function numberOf(main: HTMLElement, e: MouseEvent): HTMLElement | null {
|
|
8
|
+
const heading = e.target as HTMLElement
|
|
9
|
+
if (!(heading instanceof HTMLElement) || heading.parentElement !== main) return null
|
|
10
|
+
if (!/^H[2-6]$/.test(heading.tagName) || !heading.id) return null
|
|
11
|
+
const range = document.createRange()
|
|
12
|
+
range.selectNodeContents(heading)
|
|
13
|
+
const first = range.getClientRects()[0]
|
|
14
|
+
return first && e.clientX < first.left ? heading : null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function trackHeadingLinks(main: HTMLElement) {
|
|
18
|
+
main.addEventListener('click', async e => {
|
|
19
|
+
const heading = numberOf(main, e)
|
|
20
|
+
if (!heading) return
|
|
21
|
+
const url = new URL(location.href)
|
|
22
|
+
url.hash = heading.id
|
|
23
|
+
history.replaceState(history.state, '', url)
|
|
24
|
+
try {
|
|
25
|
+
await navigator.clipboard.writeText(url.href)
|
|
26
|
+
heading.dataset.wpCopied = ''
|
|
27
|
+
setTimeout(() => delete heading.dataset.wpCopied, 1200)
|
|
28
|
+
} catch {
|
|
29
|
+
// not copied (no permission): the link is still in the address bar
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
}
|