@bigmistqke/whiteboard 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +609 -2
- package/bin/whiteboard.ts +431 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +471 -0
- package/dist/client/runtime.js +391 -0
- package/dist/define.js +7 -0
- package/dist/plugins/callout/README.md +13 -0
- package/dist/plugins/callout/index.js +27 -0
- package/dist/plugins/callout/style.css +12 -0
- package/dist/plugins/chat/README.md +45 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +46 -0
- package/dist/plugins/chat/style.css +17 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +64 -0
- package/dist/plugins/code/index.js +77 -0
- package/dist/plugins/code/style.css +29 -0
- package/dist/plugins/comments/client.js +10 -0
- package/dist/plugins/comments/index.js +51 -0
- package/dist/plugins/comments/style.css +32 -0
- package/dist/plugins/details/README.md +17 -0
- package/dist/plugins/details/index.js +17 -0
- package/dist/plugins/details/style.css +3 -0
- package/dist/plugins/form/README.md +111 -0
- package/dist/plugins/form/index.js +102 -0
- package/dist/plugins/form/style.css +41 -0
- package/dist/plugins/gallery/README.md +30 -0
- package/dist/plugins/gallery/client.js +1 -0
- package/dist/plugins/gallery/index.js +74 -0
- package/dist/plugins/gallery/style.css +27 -0
- package/dist/plugins/grid/README.md +33 -0
- package/dist/plugins/grid/index.js +33 -0
- package/dist/plugins/grid/style.css +25 -0
- package/dist/plugins/index.js +26 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +34 -0
- package/dist/plugins/mermaid/style.css +16 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +17 -0
- package/dist/plugins/terminal/client.js +41 -0
- package/dist/plugins/terminal/host.js +332 -0
- package/dist/plugins/terminal/index.js +171 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +57 -0
- package/dist/plugins/terminal/style.css +357 -0
- package/dist/solid/index.js +35 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +274 -0
- package/dist/src/client/page/main.js +1004 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +162 -0
- package/dist/src/html.js +34 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +120 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +49 -0
- package/dist/src/pages/index.js +14 -0
- package/dist/src/pages/markdown.js +307 -0
- package/dist/src/pages/render.js +257 -0
- package/dist/src/plugins.js +238 -0
- package/dist/src/running.js +24 -0
- package/dist/src/serve.js +71 -0
- package/dist/src/server.js +700 -0
- package/dist/src/status.js +43 -0
- package/package.json +131 -4
- package/plugins/callout/README.md +13 -0
- package/plugins/callout/index.ts +25 -0
- package/plugins/callout/style.css +12 -0
- package/plugins/chat/README.md +45 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +81 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +17 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +58 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +448 -0
- package/plugins/comments/index.ts +52 -0
- package/plugins/comments/style.css +32 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/form/README.md +111 -0
- package/plugins/form/index.ts +104 -0
- package/plugins/form/style.css +41 -0
- package/plugins/gallery/README.md +30 -0
- package/plugins/gallery/client.tsx +64 -0
- package/plugins/gallery/index.ts +67 -0
- package/plugins/gallery/style.css +27 -0
- package/plugins/grid/README.md +33 -0
- package/plugins/grid/index.ts +36 -0
- package/plugins/grid/style.css +25 -0
- package/plugins/index.ts +19 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +39 -0
- package/plugins/mermaid/style.css +16 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +27 -0
- package/plugins/terminal/client.ts +317 -0
- package/plugins/terminal/host.ts +326 -0
- package/plugins/terminal/index.ts +138 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/server.ts +215 -0
- package/plugins/terminal/sizes.ts +60 -0
- package/plugins/terminal/style.css +71 -0
- package/skills/whiteboard/SKILL.md +292 -0
- package/solid/index.tsx +56 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +186 -0
- package/src/client/page/base.css +274 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/main.ts +249 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +109 -0
- package/src/client/runtime.ts +170 -0
- package/src/config.ts +96 -0
- package/src/define.ts +546 -0
- package/src/events.ts +194 -0
- package/src/html.ts +40 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +109 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +67 -0
- package/src/pages/index.ts +36 -0
- package/src/pages/markdown.ts +276 -0
- package/src/pages/render.ts +196 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +644 -0
- package/src/status.ts +69 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
// Comments on a page's text, on every page. The reader selects a passage
|
|
2
|
+
// of the page's content (its <main>, else all of it), a "Comment" button appears next to it,
|
|
3
|
+
// and what they write goes into the page's log; the agent replies there too (`whiteboard append`),
|
|
4
|
+
// or edits the page. Threads stay until the reader closes them. In a code block, the reader can
|
|
5
|
+
// also comment on whole lines: a "+" beside the line, pressed, or dragged over several.
|
|
6
|
+
//
|
|
7
|
+
// Everything is an event on the page, its type named after the plugin:
|
|
8
|
+
// comments:new { quote, before, after, block, at?, text } the event's id is the thread's id
|
|
9
|
+
// comments:reply { replyTo, text } from the reader, or from the command line
|
|
10
|
+
// comments:resolve { replyTo } the reader closed the thread
|
|
11
|
+
//
|
|
12
|
+
// A block can say where its parts are from with `data-wb-at` (a diff's lines: `src/a.ts:12`); a
|
|
13
|
+
// comment records those of the passage as `at` (the first, and the last when it differs), so the
|
|
14
|
+
// agent knows what it is about.
|
|
15
|
+
//
|
|
16
|
+
// Passages are found again by their quote and the text around it, so they survive edits elsewhere
|
|
17
|
+
// on the page, and highlighted with the CSS Highlight API: the page's own markup is never touched,
|
|
18
|
+
// so updates and new-content marks don't see comments at all.
|
|
19
|
+
import type { Api, PluginMount, PageHandle, WbEvent } from '../../src/define'
|
|
20
|
+
import { html } from '../../src/html.ts'
|
|
21
|
+
|
|
22
|
+
const NEW = 'comments:new', REPLY = 'comments:reply', RESOLVE = 'comments:resolve'
|
|
23
|
+
|
|
24
|
+
interface Thread {
|
|
25
|
+
id: string
|
|
26
|
+
comment: WbEvent
|
|
27
|
+
replies: WbEvent[]
|
|
28
|
+
range: Range | null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const CONTEXT = 32
|
|
32
|
+
|
|
33
|
+
let root: HTMLElement
|
|
34
|
+
let api: Api
|
|
35
|
+
// the page's content: its <main> when it has one. Looked up afresh, since a live update may replace it
|
|
36
|
+
let main: HTMLElement
|
|
37
|
+
let threads: Thread[] = []
|
|
38
|
+
let openThread: string | null = null
|
|
39
|
+
|
|
40
|
+
const layer = document.createElement('div')
|
|
41
|
+
layer.className = 'wb-comments'
|
|
42
|
+
|
|
43
|
+
const startButton = document.createElement('button')
|
|
44
|
+
startButton.type = 'button'
|
|
45
|
+
startButton.className = 'wb-comment-start'
|
|
46
|
+
startButton.textContent = 'Comment'
|
|
47
|
+
startButton.hidden = true
|
|
48
|
+
|
|
49
|
+
const popover = document.createElement('div')
|
|
50
|
+
popover.className = 'wb-thread'
|
|
51
|
+
popover.hidden = true
|
|
52
|
+
|
|
53
|
+
// ---- the page's text ---------------------------------------------------------------------------
|
|
54
|
+
|
|
55
|
+
/** The text of the page's content as one string, with where each text node starts in it. */
|
|
56
|
+
function textIndex() {
|
|
57
|
+
const nodes: { node: Text; start: number }[] = []
|
|
58
|
+
let text = ''
|
|
59
|
+
const walker = document.createTreeWalker(main, NodeFilter.SHOW_TEXT, {
|
|
60
|
+
acceptNode: n => (n.parentElement?.closest('script, style, textarea') ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT),
|
|
61
|
+
})
|
|
62
|
+
for (let n = walker.nextNode() as Text | null; n; n = walker.nextNode() as Text | null) {
|
|
63
|
+
nodes.push({ node: n, start: text.length })
|
|
64
|
+
text += n.data
|
|
65
|
+
}
|
|
66
|
+
return { text, nodes }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A position in the index → the text node and offset in it. */
|
|
70
|
+
function point(index: ReturnType<typeof textIndex>, at: number, end: boolean) {
|
|
71
|
+
for (let i = 0; i < index.nodes.length; i++) {
|
|
72
|
+
const { node, start } = index.nodes[i]
|
|
73
|
+
const stop = start + node.data.length
|
|
74
|
+
if (at < stop || (end && at === stop) || i === index.nodes.length - 1) return { node, offset: Math.min(Math.max(at - start, 0), node.data.length) }
|
|
75
|
+
}
|
|
76
|
+
return null
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Where a range starts and ends in the index. */
|
|
80
|
+
function offsetsOf(index: ReturnType<typeof textIndex>, range: Range) {
|
|
81
|
+
const find = (container: Node, offset: number) => {
|
|
82
|
+
if (container.nodeType === Node.TEXT_NODE) {
|
|
83
|
+
const hit = index.nodes.find(n => n.node === container)
|
|
84
|
+
return hit ? hit.start + offset : null
|
|
85
|
+
}
|
|
86
|
+
// an element boundary: the end of the last text node before it
|
|
87
|
+
const prefix = document.createRange()
|
|
88
|
+
prefix.setStart(main, 0)
|
|
89
|
+
prefix.setEnd(container, offset)
|
|
90
|
+
let at = 0
|
|
91
|
+
for (const { node, start } of index.nodes) if (prefix.intersectsNode(node)) at = start + node.data.length
|
|
92
|
+
return at
|
|
93
|
+
}
|
|
94
|
+
const start = find(range.startContainer, range.startOffset)
|
|
95
|
+
const end = find(range.endContainer, range.endOffset)
|
|
96
|
+
return start == null || end == null ? null : { start, end }
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** The passage of a comment on the page as it is now: its quote, the occurrence whose surroundings match best. */
|
|
100
|
+
function locate(index: ReturnType<typeof textIndex>, data: Record<string, unknown>): Range | null {
|
|
101
|
+
const quote = String(data.quote ?? '')
|
|
102
|
+
if (!quote) return null
|
|
103
|
+
const before = String(data.before ?? ''), after = String(data.after ?? '')
|
|
104
|
+
let best = -1, bestScore = -1
|
|
105
|
+
for (let at = index.text.indexOf(quote); at >= 0; at = index.text.indexOf(quote, at + 1)) {
|
|
106
|
+
const b = index.text.slice(Math.max(0, at - before.length), at)
|
|
107
|
+
const a = index.text.slice(at + quote.length, at + quote.length + after.length)
|
|
108
|
+
let score = 0
|
|
109
|
+
while (score < b.length && b[b.length - 1 - score] === before[before.length - 1 - score]) score++
|
|
110
|
+
let s2 = 0
|
|
111
|
+
while (s2 < a.length && a[s2] === after[s2]) s2++
|
|
112
|
+
if (score + s2 > bestScore) { best = at; bestScore = score + s2 }
|
|
113
|
+
}
|
|
114
|
+
if (best < 0) return null
|
|
115
|
+
const s = point(index, best, false), e = point(index, best + quote.length, true)
|
|
116
|
+
if (!s || !e) return null
|
|
117
|
+
const range = document.createRange()
|
|
118
|
+
range.setStart(s.node, s.offset)
|
|
119
|
+
range.setEnd(e.node, e.offset)
|
|
120
|
+
return range
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ---- threads -----------------------------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
function collect(): Thread[] {
|
|
126
|
+
const events = api.events()
|
|
127
|
+
const resolved = new Set(events.filter(e => e.type === RESOLVE).map(e => String(e.data.replyTo)))
|
|
128
|
+
const index = textIndex()
|
|
129
|
+
return events
|
|
130
|
+
.filter(e => e.type === NEW && !resolved.has(e.id))
|
|
131
|
+
.map(comment => ({
|
|
132
|
+
id: comment.id,
|
|
133
|
+
comment,
|
|
134
|
+
replies: events.filter(e => e.type === REPLY && e.data.replyTo === comment.id),
|
|
135
|
+
range: locate(index, comment.data),
|
|
136
|
+
}))
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const lastFromAgent = (t: Thread) => t.replies.at(-1)?.from === 'cli'
|
|
140
|
+
|
|
141
|
+
/** Highlight every open thread's passage, and put its badge in the margin beside it. */
|
|
142
|
+
function draw() {
|
|
143
|
+
const highlights = (CSS as unknown as { highlights?: Map<string, unknown> }).highlights
|
|
144
|
+
const HighlightCtor = (window as unknown as { Highlight?: new (...r: Range[]) => unknown }).Highlight
|
|
145
|
+
if (highlights && HighlightCtor) {
|
|
146
|
+
highlights.set('wb-comment', new HighlightCtor(...threads.filter(t => t.range && t.id !== openThread).map(t => t.range!)))
|
|
147
|
+
const active = threads.find(t => t.id === openThread)?.range
|
|
148
|
+
if (active) highlights.set('wb-comment-active', new HighlightCtor(active))
|
|
149
|
+
else highlights.delete('wb-comment-active')
|
|
150
|
+
}
|
|
151
|
+
const column = main.getBoundingClientRect()
|
|
152
|
+
layer.replaceChildren(...threads.map((t, i) => {
|
|
153
|
+
const badge = document.createElement('button')
|
|
154
|
+
badge.type = 'button'
|
|
155
|
+
badge.className = 'wb-comment-badge'
|
|
156
|
+
badge.dataset.thread = t.id
|
|
157
|
+
badge.toggleAttribute('data-agent', lastFromAgent(t))
|
|
158
|
+
badge.title = lastFromAgent(t) ? 'the agent replied' : 'comment'
|
|
159
|
+
badge.textContent = String(1 + t.replies.length)
|
|
160
|
+
const rect = t.range?.getClientRects()[0]
|
|
161
|
+
// a passage that is gone from the page: its badge waits at the top, still open until closed
|
|
162
|
+
const top = rect ? rect.top + scrollY : column.top + scrollY + i * 30
|
|
163
|
+
const left = Math.min(column.right + 8 + scrollX, document.documentElement.clientWidth - 36 + scrollX)
|
|
164
|
+
badge.style.top = `${top}px`
|
|
165
|
+
badge.style.left = `${left}px`
|
|
166
|
+
if (!rect) badge.dataset.detached = ''
|
|
167
|
+
return badge
|
|
168
|
+
}))
|
|
169
|
+
if (openThread) placePopover()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Called after the page re-rendered, and whenever the log changed. */
|
|
173
|
+
function refreshComments() {
|
|
174
|
+
if (!root) return
|
|
175
|
+
main = root.querySelector('main') ?? root
|
|
176
|
+
threads = collect()
|
|
177
|
+
if (openThread && !threads.some(t => t.id === openThread)) closePopover()
|
|
178
|
+
else if (openThread) renderPopover()
|
|
179
|
+
draw()
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ---- the thread popover ------------------------------------------------------------------------
|
|
183
|
+
|
|
184
|
+
function renderPopover() {
|
|
185
|
+
const t = threads.find(x => x.id === openThread)
|
|
186
|
+
if (!t) return
|
|
187
|
+
const who = (e: WbEvent) => (e.from === 'cli' ? 'agent' : 'you')
|
|
188
|
+
// the quote and every message are text from the reader or the agent: `html` escapes them
|
|
189
|
+
const message = (e: WbEvent) =>
|
|
190
|
+
html`<div class="wb-thread-message" data-from="${who(e)}"><span class="wb-thread-who">${who(e)}</span><div class="wb-thread-text">${e.data.text ?? ''}</div></div>`
|
|
191
|
+
const draft = popover.querySelector('textarea')?.value ?? ''
|
|
192
|
+
popover.innerHTML = String(html`
|
|
193
|
+
<blockquote class="wb-thread-quote">${t.comment.data.quote ?? ''}</blockquote>
|
|
194
|
+
${t.comment.data.at && html`<p class="wb-thread-at">${t.comment.data.at}</p>`}
|
|
195
|
+
${!t.range && html`<p class="wb-thread-detached">This passage is no longer on the page.</p>`}
|
|
196
|
+
${[t.comment, ...t.replies].map(message)}
|
|
197
|
+
<form class="wb-thread-reply">
|
|
198
|
+
<textarea rows="2" placeholder="Reply"></textarea>
|
|
199
|
+
<div class="wb-thread-actions"><button type="submit" class="wb-comment-button wb-comment-primary">Reply</button><button type="button" class="wb-comment-button" data-close>Close thread</button></div>
|
|
200
|
+
</form>`)
|
|
201
|
+
popover.querySelector('textarea')!.value = draft
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function placePopover() {
|
|
205
|
+
const badge = layer.querySelector<HTMLElement>(`[data-thread="${openThread}"]`)
|
|
206
|
+
if (!badge) return
|
|
207
|
+
const top = parseFloat(badge.style.top) + 28
|
|
208
|
+
const width = Math.min(360, document.documentElement.clientWidth - 32)
|
|
209
|
+
const left = Math.max(16 + scrollX, Math.min(parseFloat(badge.style.left) + 28 - width, document.documentElement.clientWidth - width - 16 + scrollX))
|
|
210
|
+
popover.style.top = `${top}px`
|
|
211
|
+
popover.style.left = `${left}px`
|
|
212
|
+
popover.style.width = `${width}px`
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function openPopover(id: string) {
|
|
216
|
+
openThread = id
|
|
217
|
+
popover.hidden = false
|
|
218
|
+
popover.querySelector('textarea')?.replaceWith(document.createElement('textarea'))
|
|
219
|
+
renderPopover()
|
|
220
|
+
draw()
|
|
221
|
+
popover.querySelector('textarea')?.focus({ preventScroll: true })
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function closePopover() {
|
|
225
|
+
openThread = null
|
|
226
|
+
popover.hidden = true
|
|
227
|
+
draw()
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ---- starting a comment ------------------------------------------------------------------------
|
|
231
|
+
|
|
232
|
+
let pending: { quote: string; before: string; after: string; block: number; at?: string } | null = null
|
|
233
|
+
|
|
234
|
+
/** A selection inside the page's text (not in a form) → where the Comment button goes. */
|
|
235
|
+
function onSelection() {
|
|
236
|
+
main = root.querySelector('main') ?? root
|
|
237
|
+
const sel = getSelection()
|
|
238
|
+
if (!sel || sel.isCollapsed || !sel.rangeCount) { if (!startButton.matches(':focus')) startButton.hidden = true; return }
|
|
239
|
+
const range = sel.getRangeAt(0)
|
|
240
|
+
const inPage = (n: Node) => main.contains(n) && !(n instanceof Element ? n : n.parentElement)?.closest('form, input, textarea, select, button')
|
|
241
|
+
if (!inPage(range.startContainer) || !inPage(range.endContainer)) { startButton.hidden = true; return }
|
|
242
|
+
startButton.hidden = !prepare(range)
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* What a comment on `range` records (`pending`), and the Comment button put under its end. False
|
|
247
|
+
* when there is nothing to comment on.
|
|
248
|
+
*/
|
|
249
|
+
function prepare(range: Range): boolean {
|
|
250
|
+
const quote = range.toString().trim()
|
|
251
|
+
if (!quote) return false
|
|
252
|
+
const index = textIndex()
|
|
253
|
+
const at = offsetsOf(index, range)
|
|
254
|
+
if (!at) return false
|
|
255
|
+
// the quote as it appears in the page's text, trimmed of the whitespace the selection had around it
|
|
256
|
+
const start = index.text.indexOf(quote, at.start)
|
|
257
|
+
const s = start >= 0 && start <= at.end ? start : at.start
|
|
258
|
+
let block: Element | null = (range.startContainer instanceof Element ? range.startContainer : range.startContainer.parentElement)
|
|
259
|
+
while (block && block.parentElement !== main) block = block.parentElement
|
|
260
|
+
pending = {
|
|
261
|
+
quote,
|
|
262
|
+
before: index.text.slice(Math.max(0, s - CONTEXT), s),
|
|
263
|
+
after: index.text.slice(s + quote.length, s + quote.length + CONTEXT),
|
|
264
|
+
block: block ? [...main.children].indexOf(block) : -1,
|
|
265
|
+
}
|
|
266
|
+
// where the passage is from, when its block says (see the top of this file)
|
|
267
|
+
const places = [...main.querySelectorAll<HTMLElement>('[data-wb-at]')].filter(el => range.intersectsNode(el) && el.textContent).map(el => el.dataset.wbAt!)
|
|
268
|
+
if (places.length) pending.at = places.length > 1 && places.at(-1) !== places[0] ? `${places[0]} – ${places.at(-1)}` : places[0]
|
|
269
|
+
const rects = range.getClientRects()
|
|
270
|
+
const last = rects[rects.length - 1] ?? range.getBoundingClientRect()
|
|
271
|
+
startButton.style.top = `${last.bottom + scrollY + 6}px`
|
|
272
|
+
startButton.style.left = `${Math.min(last.right + scrollX - 40, document.documentElement.clientWidth - 110 + scrollX)}px`
|
|
273
|
+
return true
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ---- comments on lines of code -----------------------------------------------------------------
|
|
277
|
+
|
|
278
|
+
// A code block's lines (Shiki writes each as a .line): hovering one shows "+" at its left; pressing
|
|
279
|
+
// it comments on the line, dragging it down or up takes the lines it passes over.
|
|
280
|
+
const lineButton = document.createElement('button')
|
|
281
|
+
lineButton.type = 'button'
|
|
282
|
+
lineButton.className = 'wb-line-comment'
|
|
283
|
+
lineButton.textContent = '+'
|
|
284
|
+
lineButton.title = 'Comment on this line (drag for several)'
|
|
285
|
+
lineButton.hidden = true
|
|
286
|
+
|
|
287
|
+
let hovered: HTMLElement | null = null
|
|
288
|
+
let drag: { pre: HTMLElement; from: HTMLElement; to: HTMLElement } | null = null
|
|
289
|
+
|
|
290
|
+
const codeLine = (el: EventTarget | null) => {
|
|
291
|
+
const line = el instanceof Element ? el.closest<HTMLElement>('pre .line') : null
|
|
292
|
+
return line && main.contains(line) && !line.closest('form') ? line : null
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** The lines from `a` to `b`, either way round, as one range. */
|
|
296
|
+
function linesRange(a: HTMLElement, b: HTMLElement): Range {
|
|
297
|
+
const [first, last] = a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_PRECEDING ? [b, a] : [a, b]
|
|
298
|
+
const range = document.createRange()
|
|
299
|
+
range.setStart(first, 0)
|
|
300
|
+
range.setEnd(last, last.childNodes.length)
|
|
301
|
+
return range
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function showLineButton(line: HTMLElement) {
|
|
305
|
+
hovered = line
|
|
306
|
+
const rect = line.getBoundingClientRect()
|
|
307
|
+
const pre = line.closest('pre')!.getBoundingClientRect()
|
|
308
|
+
lineButton.style.top = `${rect.top + scrollY + rect.height / 2 - 10}px`
|
|
309
|
+
lineButton.style.left = `${Math.max(scrollX + 2, pre.left + scrollX - 24)}px`
|
|
310
|
+
lineButton.hidden = false
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function onLinePointer(e: MouseEvent) {
|
|
314
|
+
if (drag) {
|
|
315
|
+
// the line at the pointer's height, inside the block the drag started in
|
|
316
|
+
const pre = drag.pre.getBoundingClientRect()
|
|
317
|
+
const x = drag.from.getBoundingClientRect().left + 4
|
|
318
|
+
const line = codeLine(document.elementFromPoint(x, Math.min(Math.max(e.clientY, pre.top + 1), pre.bottom - 1)))
|
|
319
|
+
if (line && drag.pre.contains(line) && line !== drag.to) {
|
|
320
|
+
drag.to = line
|
|
321
|
+
const highlights = (CSS as unknown as { highlights?: Map<string, unknown> }).highlights
|
|
322
|
+
const HighlightCtor = (window as unknown as { Highlight?: new (...r: Range[]) => unknown }).Highlight
|
|
323
|
+
if (highlights && HighlightCtor) highlights.set('wb-comment-active', new HighlightCtor(linesRange(drag.from, drag.to)))
|
|
324
|
+
}
|
|
325
|
+
return
|
|
326
|
+
}
|
|
327
|
+
if (e.target === lineButton) return
|
|
328
|
+
const line = codeLine(e.target)
|
|
329
|
+
if (line) return showLineButton(line)
|
|
330
|
+
if (lineButton.hidden || !hovered) return
|
|
331
|
+
// on the way to the "+" (through the block's padding, or diagonally across other lines): the "+"
|
|
332
|
+
// is for the line at the pointer's height, as long as the pointer is between the "+" and the
|
|
333
|
+
// block's right edge
|
|
334
|
+
const pre = hovered.closest('pre')!
|
|
335
|
+
const box = pre.getBoundingClientRect()
|
|
336
|
+
const plus = lineButton.getBoundingClientRect()
|
|
337
|
+
const inside = e.clientX >= plus.left - 4 && e.clientX <= box.right && e.clientY >= box.top && e.clientY <= box.bottom
|
|
338
|
+
const atHeight = inside ? codeLine(document.elementFromPoint(hovered.getBoundingClientRect().left + 4, e.clientY)) : null
|
|
339
|
+
if (atHeight && pre.contains(atHeight)) showLineButton(atHeight)
|
|
340
|
+
else if (!inside || !(e.clientY >= plus.top - 4 && e.clientY <= plus.bottom + 4)) { lineButton.hidden = true; hovered = null }
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const composer = document.createElement('form')
|
|
344
|
+
composer.className = 'wb-thread wb-comment-new'
|
|
345
|
+
composer.hidden = true
|
|
346
|
+
composer.innerHTML = String(html`<blockquote class="wb-thread-quote"></blockquote><textarea rows="3" placeholder="Your comment"></textarea>
|
|
347
|
+
<div class="wb-thread-actions"><button type="submit" class="wb-comment-button wb-comment-primary">Comment</button><button type="button" class="wb-comment-button" data-cancel>Cancel</button></div>`)
|
|
348
|
+
|
|
349
|
+
function startComment() {
|
|
350
|
+
if (!pending) return
|
|
351
|
+
composer.querySelector('blockquote')!.textContent = pending.at ? `${pending.at}\n${pending.quote}` : pending.quote
|
|
352
|
+
composer.style.top = startButton.style.top
|
|
353
|
+
const width = Math.min(360, document.documentElement.clientWidth - 32)
|
|
354
|
+
composer.style.width = `${width}px`
|
|
355
|
+
composer.style.left = `${Math.max(16 + scrollX, Math.min(parseFloat(startButton.style.left), document.documentElement.clientWidth - width - 16 + scrollX))}px`
|
|
356
|
+
composer.hidden = false
|
|
357
|
+
startButton.hidden = true
|
|
358
|
+
composer.querySelector('textarea')!.focus({ preventScroll: true })
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ---- entry -------------------------------------------------------------------------------------
|
|
362
|
+
|
|
363
|
+
const mount: PluginMount = (el: HTMLElement, handle: PageHandle, core: Api) => {
|
|
364
|
+
// the overview and folders are lists, not text to comment on
|
|
365
|
+
if (handle.kind !== 'page' && handle.kind !== 'file') return
|
|
366
|
+
root = el
|
|
367
|
+
main = root.querySelector('main') ?? root
|
|
368
|
+
api = core
|
|
369
|
+
document.body.append(layer, startButton, lineButton, popover, composer)
|
|
370
|
+
|
|
371
|
+
document.addEventListener('mousemove', onLinePointer)
|
|
372
|
+
// the pointer left the page: no line is hovered any more (a drag keeps its lines until mouseup)
|
|
373
|
+
document.documentElement.addEventListener('mouseleave', () => {
|
|
374
|
+
if (drag) return
|
|
375
|
+
lineButton.hidden = true
|
|
376
|
+
hovered = null
|
|
377
|
+
})
|
|
378
|
+
lineButton.addEventListener('mousedown', e => {
|
|
379
|
+
if (!hovered || e.button !== 0) return
|
|
380
|
+
e.preventDefault()
|
|
381
|
+
const highlights = (CSS as unknown as { highlights?: Map<string, unknown> }).highlights
|
|
382
|
+
const HighlightCtor = (window as unknown as { Highlight?: new (...r: Range[]) => unknown }).Highlight
|
|
383
|
+
if (highlights && HighlightCtor) highlights.set('wb-comment-active', new HighlightCtor(linesRange(hovered, hovered)))
|
|
384
|
+
drag = { pre: hovered.closest('pre')!, from: hovered, to: hovered }
|
|
385
|
+
})
|
|
386
|
+
document.addEventListener('mouseup', () => {
|
|
387
|
+
if (!drag) return
|
|
388
|
+
const { from, to } = drag
|
|
389
|
+
drag = null
|
|
390
|
+
lineButton.hidden = true
|
|
391
|
+
if (prepare(linesRange(from, to))) startComment()
|
|
392
|
+
else draw()
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
let selTimer = 0
|
|
396
|
+
document.addEventListener('selectionchange', () => { clearTimeout(selTimer); selTimer = window.setTimeout(onSelection, 150) })
|
|
397
|
+
// keep the selection when the button is pressed
|
|
398
|
+
startButton.addEventListener('mousedown', e => e.preventDefault())
|
|
399
|
+
startButton.addEventListener('click', startComment)
|
|
400
|
+
|
|
401
|
+
composer.addEventListener('submit', async e => {
|
|
402
|
+
e.preventDefault()
|
|
403
|
+
const text = composer.querySelector('textarea')!.value.trim()
|
|
404
|
+
if (!text || !pending) return
|
|
405
|
+
const saved = await api.append(NEW, { ...pending, text })
|
|
406
|
+
composer.querySelector('textarea')!.value = ''
|
|
407
|
+
composer.hidden = true
|
|
408
|
+
pending = null
|
|
409
|
+
getSelection()?.removeAllRanges()
|
|
410
|
+
refreshComments()
|
|
411
|
+
openPopover(saved.id)
|
|
412
|
+
})
|
|
413
|
+
composer.querySelector('[data-cancel]')!.addEventListener('click', () => { composer.hidden = true; pending = null })
|
|
414
|
+
|
|
415
|
+
layer.addEventListener('click', e => {
|
|
416
|
+
const id = (e.target as HTMLElement).closest<HTMLElement>('[data-thread]')?.dataset.thread
|
|
417
|
+
if (id) (openThread === id ? closePopover() : openPopover(id))
|
|
418
|
+
})
|
|
419
|
+
popover.addEventListener('submit', async e => {
|
|
420
|
+
e.preventDefault()
|
|
421
|
+
const box = popover.querySelector('textarea')!
|
|
422
|
+
const text = box.value.trim()
|
|
423
|
+
if (!text || !openThread) return
|
|
424
|
+
await api.append(REPLY, { replyTo: openThread, text })
|
|
425
|
+
box.value = ''
|
|
426
|
+
refreshComments()
|
|
427
|
+
})
|
|
428
|
+
popover.addEventListener('click', async e => {
|
|
429
|
+
if (!(e.target as HTMLElement).closest('[data-close]') || !openThread) return
|
|
430
|
+
const id = openThread
|
|
431
|
+
closePopover()
|
|
432
|
+
await api.append(RESOLVE, { replyTo: id })
|
|
433
|
+
refreshComments()
|
|
434
|
+
})
|
|
435
|
+
document.addEventListener('keydown', e => {
|
|
436
|
+
if (e.key !== 'Escape') return
|
|
437
|
+
if (!composer.hidden) { composer.hidden = true; pending = null }
|
|
438
|
+
else if (openThread) closePopover()
|
|
439
|
+
})
|
|
440
|
+
|
|
441
|
+
api.onEvents(refreshComments)
|
|
442
|
+
api.onRendered(refreshComments)
|
|
443
|
+
new ResizeObserver(() => draw()).observe(root)
|
|
444
|
+
addEventListener('resize', () => draw())
|
|
445
|
+
refreshComments()
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export default mount
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Comments, as a whiteboard plugin: on every page the reader selects a passage and comments on it,
|
|
2
|
+
// and the agent replies in the thread or edits the page. Built in: every project has it.
|
|
3
|
+
//
|
|
4
|
+
// A thread is events on the page (see client.ts), so the server has nothing to do but serve the
|
|
5
|
+
// browser module and the styles, and tell agents how to answer (`whiteboard docs`).
|
|
6
|
+
import { packagePath } from '../../src/package.ts'
|
|
7
|
+
import type { Plugin } from '../../src/define.ts'
|
|
8
|
+
|
|
9
|
+
const CLIENT = packagePath('dist/plugins/comments/client.js')
|
|
10
|
+
const STYLE = packagePath('dist/plugins/comments/style.css')
|
|
11
|
+
|
|
12
|
+
const DOCS = `## Comments
|
|
13
|
+
|
|
14
|
+
The reader can select text on any page and comment on it, or comment on
|
|
15
|
+
whole lines of a code block (a "+" beside each line; dragging it takes
|
|
16
|
+
several). Each comment comes to you as an event on the page, like a form
|
|
17
|
+
answer:
|
|
18
|
+
|
|
19
|
+
\`\`\`json
|
|
20
|
+
{"id":"…","page":"plan","type":"comments:new","data":{"quote":"the selected text","before":"…","after":"…","block":3,"text":"their comment"}}
|
|
21
|
+
\`\`\`
|
|
22
|
+
|
|
23
|
+
On lines that say where they are from, \`data.at\` says it too: in a
|
|
24
|
+
\`diff\` fence, \`"src/a.ts:12"\` (a line of the new file), \`"src/a.ts (old):9"\`
|
|
25
|
+
(a removed line), or \`"src/a.ts:12 – src/a.ts:15"\` for several. Any block
|
|
26
|
+
can say where its parts are from with a \`data-wb-at\` attribute.
|
|
27
|
+
|
|
28
|
+
Replies to a thread come as \`comments:reply\` (\`data.replyTo\` is the comment's
|
|
29
|
+
id), and \`comments:resolve\` when the reader closed it. A thread stays on the
|
|
30
|
+
page until the reader closes it. Answer a comment the way it asks:
|
|
31
|
+
|
|
32
|
+
- **It asks for a change:** edit the page. The thread stays, so say what you
|
|
33
|
+
changed in a reply if it is not obvious.
|
|
34
|
+
- **It asks a question:** reply in the thread:
|
|
35
|
+
\`whiteboard append --page <file> --type comments:reply --data '{"replyTo":"<comment id>","text":"…"}'\`
|
|
36
|
+
|
|
37
|
+
Your replies show as the agent's, and \`wait\` and \`watch\` skip what you
|
|
38
|
+
append, so you don't hear your own replies.`
|
|
39
|
+
|
|
40
|
+
export default function comments(): Plugin {
|
|
41
|
+
return {
|
|
42
|
+
name: 'comments',
|
|
43
|
+
docs: () => DOCS,
|
|
44
|
+
events: {
|
|
45
|
+
'comments:new': { handOut: true, description: 'a comment on a passage or lines of code: `{ quote, before, after, block, at?, text }`; its id is the thread\'s' },
|
|
46
|
+
'comments:reply': { handOut: true, description: 'a reply in a thread: `{ replyTo, text }`' },
|
|
47
|
+
'comments:resolve': { description: 'the reader closed a thread: `{ replyTo }`' },
|
|
48
|
+
},
|
|
49
|
+
style: STYLE,
|
|
50
|
+
setup: () => ({ client: CLIENT }),
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Comments: highlights through the CSS Highlight API, badges and threads above the page. Colors
|
|
3
|
+
* and fonts come from the page's theme variables, else from
|
|
4
|
+
* the fallbacks here.
|
|
5
|
+
*/
|
|
6
|
+
::highlight(wb-comment) { background-color: color-mix(in srgb, var(--accent, #1d5bbf) 18%, transparent); }
|
|
7
|
+
::highlight(wb-comment-active) { background-color: color-mix(in srgb, var(--accent, #1d5bbf) 34%, transparent); }
|
|
8
|
+
.wb-comment-start, .wb-comment-badge, .wb-thread { position: absolute; z-index: 20; font-family: var(--sans, sans-serif); }
|
|
9
|
+
.wb-comment-start[hidden], .wb-thread[hidden], .wb-line-comment[hidden] { display: none; }
|
|
10
|
+
.wb-comment-start { background: var(--accent, #1d5bbf); color: var(--accent-text, #fff); border-color: var(--accent, #1d5bbf); border-radius: 99px; font-size: .78rem; padding: 3px 10px; box-shadow: 0 2px 8px rgb(0 0 0 / .15); }
|
|
11
|
+
.wb-comment-badge { min-width: 24px; height: 24px; padding: 0 6px; border-radius: 12px 12px 12px 3px; font-size: .72rem; font-weight: 600; background: var(--bg, Canvas); color: var(--accent, #1d5bbf); border: 1px solid color-mix(in srgb, var(--accent, #1d5bbf) 50%, transparent); }
|
|
12
|
+
.wb-comment-badge[data-agent] { background: var(--accent, #1d5bbf); color: var(--accent-text, #fff); }
|
|
13
|
+
.wb-comment-badge[data-detached] { border-style: dashed; }
|
|
14
|
+
.wb-thread { background: var(--bg, Canvas); border: 1px solid var(--border, #e3e5e9); border-radius: 8px; padding: 12px 14px; box-shadow: 0 8px 28px rgb(0 0 0 / .16); display: grid; gap: 10px; font-size: .9rem; }
|
|
15
|
+
.wb-thread-quote { margin: 0; padding-left: 10px; border-left: 3px solid color-mix(in srgb, var(--accent, #1d5bbf) 50%, transparent); color: var(--muted, #696e76); font-size: .85rem; max-height: 6.5em; overflow: auto; white-space: pre-wrap; }
|
|
16
|
+
.wb-thread-detached { margin: 0; color: var(--muted, #696e76); font-size: .8rem; }
|
|
17
|
+
.wb-thread-message { display: grid; gap: 2px; }
|
|
18
|
+
.wb-thread-who { font-size: .7rem; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--muted, #696e76); }
|
|
19
|
+
.wb-thread-message[data-from=agent] .wb-thread-who { color: var(--accent, #1d5bbf); }
|
|
20
|
+
.wb-thread-text { white-space: pre-wrap; line-height: 1.45; }
|
|
21
|
+
.wb-thread textarea { width: 100%; resize: vertical; font: inherit; }
|
|
22
|
+
.wb-thread-reply { display: grid; gap: 8px; margin: 0; }
|
|
23
|
+
.wb-thread-actions { display: flex; gap: 8px; }
|
|
24
|
+
/* its own buttons, styled here rather than borrowed from the page */
|
|
25
|
+
.wb-comment-button { font: 500 .85rem/1.3 var(--sans, sans-serif); padding: 6px 12px; border-radius: 6px; cursor: pointer; border: 1px solid var(--border, #e3e5e9); background: var(--surface, Canvas); color: var(--text, CanvasText); }
|
|
26
|
+
.wb-comment-button:hover { border-color: var(--muted, #696e76); }
|
|
27
|
+
.wb-comment-primary { background: var(--accent, #1d5bbf); color: var(--accent-text, #fff); border-color: var(--accent, #1d5bbf); }
|
|
28
|
+
.wb-comment-start, .wb-comment-badge { cursor: pointer; border-style: solid; border-width: 1px; }
|
|
29
|
+
|
|
30
|
+
/* "+" beside a line of code: comment on it, or drag over several */
|
|
31
|
+
.wb-line-comment { position: absolute; z-index: 20; width: 20px; height: 20px; padding: 0; border-radius: 4px; cursor: pointer; font: 600 14px/1 var(--sans, sans-serif); background: var(--accent, #1d5bbf); color: var(--accent-text, #fff); border: none; box-shadow: 0 1px 4px rgb(0 0 0 / .2); }
|
|
32
|
+
.wb-thread-at { margin: -4px 0 0; font: .78rem var(--mono, monospace); color: var(--muted, #696e76); }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
A collapsible section. The label is the summary line; the body is any
|
|
2
|
+
markdown. Use it to keep in-depth explanation out of the way of the main read.
|
|
3
|
+
Unlike a raw `<details>` tag, the body needs no blank-line care.
|
|
4
|
+
|
|
5
|
+
Attributes: `open` (start expanded).
|
|
6
|
+
|
|
7
|
+
```markdown
|
|
8
|
+
:::details[Why remark-directive instead of MDX?]
|
|
9
|
+
MDX fails the **whole page** on one stray `<` or `{`. Directives that no
|
|
10
|
+
block handles still render as plain markdown.
|
|
11
|
+
:::
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
:::details[Why remark-directive instead of MDX?]
|
|
15
|
+
MDX fails the **whole page** on one stray `<` or `{`. Directives that no
|
|
16
|
+
block handles still render as plain markdown.
|
|
17
|
+
:::
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import { definePlugin, type Plugin } from '@bigmistqke/whiteboard'
|
|
3
|
+
import { html, raw, type DirectiveContext } from '@bigmistqke/whiteboard'
|
|
4
|
+
|
|
5
|
+
export async function render(ctx: DirectiveContext) {
|
|
6
|
+
// the label and content are HTML already
|
|
7
|
+
return html`<details class="wb-details"${'open' in ctx.attrs && raw(' open')}><summary>${ctx.label ? raw(ctx.label) : 'Details'}</summary><div class="wb-details-body">${raw(await ctx.toHtml(ctx.children))}</div></details>`
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function details(): Plugin {
|
|
11
|
+
return definePlugin({
|
|
12
|
+
name: 'details',
|
|
13
|
+
blocks: {
|
|
14
|
+
details: {
|
|
15
|
+
html: render,
|
|
16
|
+
style: new URL('./style.css', import.meta.url),
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
|
|
20
|
+
})
|
|
21
|
+
}
|