@vite-hub/comments 0.1.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -31
- package/dist/anchor.d.ts +7 -4
- package/dist/anchor.js +10 -5
- package/dist/client-diagnostics-CcFpkbAk.js +40 -0
- package/dist/client.d.ts +78 -3
- package/dist/client.js +198 -13
- package/dist/{console-BIz9t2cO.d.ts → console-h4KlhIik.d.ts} +97 -8
- package/dist/context-BqIsmYP1.js +61 -0
- package/dist/database/postgres.d.ts +738 -347
- package/dist/database/postgres.js +28 -7
- package/dist/{database-Cb2xjC1V.d.ts → database-CA1aHKN7.d.ts} +675 -276
- package/dist/database.d.ts +6 -98
- package/dist/database.js +2 -3
- package/dist/define-43mMR39K.js +89 -0
- package/dist/define-B3TF9zzn.d.ts +204 -0
- package/dist/errors-4gelGVmK.js +57 -0
- package/dist/errors-C6qptPoG.js +135 -0
- package/dist/errors-CyQyKAvy.d.ts +185 -0
- package/dist/fold-name-B7Zb3-MF.js +7 -0
- package/dist/{identity-3qtVC4rF.js → identity-CBOzgxSf.js} +7 -2
- package/dist/index-DNhy69Wt.d.ts +83 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.js +196 -5
- package/dist/install-BcMPgPk_.js +1154 -0
- package/dist/install-W9ghhGoh.d.ts +36 -0
- package/dist/installed-B-PfERBU.js +30 -0
- package/dist/invariant-D8_xBqmZ.js +22 -0
- package/dist/migrate-B0k_0swk.d.ts +54 -0
- package/dist/migrate-BDQ8xqUs.js +289 -0
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/{operations-0B0QTVWX.d.ts → operations-D4A1Fcgf.d.ts} +37 -15
- package/dist/operations-DMMIfYPW.js +1159 -0
- package/dist/operations-GLeOqgMt.d.ts +257 -0
- package/dist/operations.d.ts +3 -7
- package/dist/operations.js +3 -2
- package/dist/overlay/client-DmsSMOzi.js +4 -0
- package/dist/overlay/dist-BoHMOFax.js +14 -0
- package/dist/overlay/dist-yIZmvSaU.js +1 -0
- package/dist/overlay/overlay.js +183 -177
- package/dist/page/loader.js +138 -18
- package/dist/{page-BuroSMXO.js → page-B41ozN63.js} +8 -6
- package/dist/{page-BtXADR6d.d.ts → page-DtbMOEMd.d.ts} +6 -5
- package/dist/page-tools-shared-NHUjKI8E.js +117 -0
- package/dist/path--zk6PEMz.js +18 -0
- package/dist/plugin-PS8PMYus.js +74 -0
- package/dist/realtime.d.ts +6 -12
- package/dist/realtime.js +23 -24
- package/dist/rpc-HBcWtg0T.d.ts +17 -0
- package/dist/rpc-RKPAusHp.js +153 -0
- package/dist/rpc.d.ts +2 -29
- package/dist/rpc.js +2 -75
- package/dist/runtime/auth.d.ts +5 -3
- package/dist/runtime/auth.js +8 -5
- package/dist/runtime/console.d.ts +1 -1
- package/dist/runtime/console.js +26 -21
- package/dist/runtime/index.d.ts +108 -60
- package/dist/runtime/index.js +684 -236
- package/dist/{server-BomGg3Gb.js → server-D8k-0GXe.js} +16 -7
- package/dist/server.d.ts +14 -88
- package/dist/server.js +7 -326
- package/dist/service-CFVm23k7.d.ts +158 -0
- package/dist/service-GHhhyQj4.js +609 -0
- package/dist/session-DVx-1Tym.d.ts +14 -0
- package/dist/session-EkudzUmm.js +503 -0
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +14 -2
- package/dist/tokens-DSbwkVSy.js +58 -0
- package/dist/types-BIS0hCEl.d.ts +124 -0
- package/dist/types-CZujrMFd.d.ts +322 -0
- package/dist/vite.d.ts +91 -87
- package/dist/vite.js +535 -796
- package/package.json +24 -27
- package/dist/database-BSnVrwHn.js +0 -60
- package/dist/database-C-ktS9Do.js +0 -95
- package/dist/define-BzW2Ychn.d.ts +0 -46
- package/dist/define-Cbhl6QVJ.js +0 -32
- package/dist/errors-BAQYcy3_.d.ts +0 -89
- package/dist/errors-DeD6wTCl.js +0 -77
- package/dist/h3-v1-request-DDGXJ9E7.js +0 -166
- package/dist/index-DklG2Etf.d.ts +0 -111
- package/dist/operations-NZG5dB8y.js +0 -732
- package/dist/page-tools-shared-D-amu6sx.js +0 -53
- package/dist/plugin-QElKQYkA.js +0 -725
- package/dist/request-DVc3M_AG.js +0 -14
- package/dist/src-CRTwnaEG.js +0 -1362
- package/dist/types-Cj8K7Vx7.d.ts +0 -176
- package/dist/types-Un2dcJWD.d.ts +0 -288
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ pnpm add @vite-hub/comments
|
|
|
10
10
|
|
|
11
11
|
```ts
|
|
12
12
|
// vite.config.ts
|
|
13
|
+
import { defineConfig } from "vite"
|
|
13
14
|
import comments from "@vite-hub/comments/vite"
|
|
14
15
|
|
|
15
16
|
export default defineConfig({ plugins: [comments()] })
|
|
@@ -17,7 +18,7 @@ export default defineConfig({ plugins: [comments()] })
|
|
|
17
18
|
|
|
18
19
|
There is nothing else to set up. `comments()` adds Nitro as the server unless the app has it, serves the Comments API, and keeps threads in `.vitehub/data/comments.sqlite`, a file that it opens with Node's built-in `node:sqlite` and creates the tables in. On the dev server, anyone may comment as a guest. Open any page with `?vitehub`.
|
|
19
20
|
|
|
20
|
-
A production build connects to `DATABASE_URL`, `TURSO_DATABASE_URL`, or a Cloudflare D1 binding `DB`, whichever the server has. Or point it somewhere: `comments({ database: "d1:COMMENTS" })`, `"env:COMMENTS_DATABASE_URL"`, or `"file:./data/comments.sqlite"` on Node. Comments creates its tables there on first use, so there is no migration.
|
|
21
|
+
A production build connects to `DATABASE_URL`, `TURSO_DATABASE_URL`, or a Cloudflare D1 binding `DB`, whichever the server has. Or point it somewhere: `comments({ database: "d1:COMMENTS" })`, `"env:COMMENTS_DATABASE_URL"`, or `"file:./data/comments.sqlite"` on Node. Comments creates its tables there on first use, and moves the tables of an earlier version before its first query, so there is no migration to run.
|
|
21
22
|
|
|
22
23
|
### Sign in with GitHub
|
|
23
24
|
|
|
@@ -27,30 +28,55 @@ Create a GitHub App, install it on the repository, and list `https://<site>/api/
|
|
|
27
28
|
|
|
28
29
|
[`vite-hub`](https://www.npmjs.com/package/vite-hub) wires the package for you, with any ViteHub Database, presence over Realtime, Content edits in place, the Console, and MCP tools.
|
|
29
30
|
|
|
30
|
-
```ts
|
|
31
|
+
```ts [server/comments.ts]
|
|
31
32
|
// server/comments.ts
|
|
32
33
|
import { defineComments } from "vite-hub/comments"
|
|
33
34
|
|
|
34
35
|
export const comments = defineComments({
|
|
35
|
-
// Who may read, comment, and resolve. Without it, people signed in with GitHub who
|
|
36
|
-
//
|
|
36
|
+
// Who may read, comment, and resolve. Without it, a production build lets in only people signed in with GitHub who
|
|
37
|
+
// can push to the repository, and people that the Console admits to Comments; everyone may on the dev server.
|
|
37
38
|
authorize: ({ user }) => user?.role === "editor",
|
|
38
39
|
})
|
|
39
40
|
```
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
Without the file, `vitehub({ comments: true })` turns Comments on with `defineComments()`, and so does an object such as `{ database: "team" }`. The Console does not turn it on. `comments: false` turns it off. Docs sites of `@vite-hub/docs` on `vite-hub/nuxt` set it by default; `docs: { comments: false }` turns it off.
|
|
43
|
+
|
|
44
|
+
ViteHub then adds the `vitehub_comment_threads`, `vitehub_comment_messages`, `vitehub_comment_counters`, and `vitehub_comment_forge` tables to the default Database (or the only one, or `defineComments({ database: "name" })`; without a Database Definition, the implicit `default` Database of `@vite-hub/database`), serves `/api/_vitehub/comments/**`, and adds a Comments section to the Console. Apply the tables with `vitehub database generate` and `vitehub database migrate`.
|
|
42
45
|
|
|
43
|
-
|
|
46
|
+
A ViteHub Database with the tables of the earlier live-site model must move them once in a migration: see [Upgrade to one model](https://vitehub.dev/docs/comments/configure#upgrade-to-one-model). Until then every Comments request fails with `COMMENTS_R0019`, whose `fix` names `commentsMigrationSql({ from: "live" })`. `migrateComments(db)` from `@vite-hub/comments/database` moves them in place and keeps the old tables as a backup.
|
|
44
47
|
|
|
45
|
-
Comments is a ViteHub page tool. Open any page with `?vitehub`, or `?vitehub=comments`. The server adds the page tools loader to that page. The loader calls `GET /_vitehub/page/session`, which allows Comments when `authorize` allows `read` or offers GitHub sign-in or account switching, and sets the `vitehub-tools` cookie so later pages open in comment mode too. The loader then draws the ViteHub tools pill at the bottom of the page and imports the overlay. The overlay draws its toolbar in its segment of the pill, and its pins, cursors, and cards in `<vitehub-comments>`. The pill's **×** button turns the page tools off, so the toolbar has no close button of its own. The cookie is a hint, never a credential: every Comments route checks `authorize` again. A request without the hint gets the page
|
|
48
|
+
Comments is a ViteHub page tool. Open any page with `?vitehub`, or `?vitehub=comments`. The server adds the page tools loader to that page. The loader calls `GET /_vitehub/page/session`, which allows Comments when `authorize` allows `read` or offers GitHub sign-in or account switching, and sets the `vitehub-tools` cookie so later pages open in comment mode too. The loader then draws the ViteHub tools pill at the bottom of the page and imports the overlay. The overlay draws its toolbar in its segment of the pill, and its pins, cursors, and cards in `<vitehub-comments>`. The pill's **×** button turns the page tools off, so the toolbar has no close button of its own. A small ViteHub hexagon then stays at the pill's place, on every page, and pressing it turns them on again. The cookie is a hint, never a credential: every Comments route checks `authorize` again. A request without the hint gets the page with a short inline bootstrap that loads the loader in the browser only when the hint is there, so prerendered pages that a host serves as static files work too. Opening the Console calls the same session route, so the Console's people see the toolbar on the app's pages. The session route and the loader do not need the Console.
|
|
46
49
|
|
|
47
50
|
A single page app whose `index.html` Vite serves or builds gets a short inline check in that file. It imports the loader in the browser when the page has the query or the cookie, so a built `index.html` that a host serves as a static file also gets the tools. When the app has a Vite `base` or a Nuxt `app.baseURL`, such as `/docs/`, every path follows it: `/docs/_vitehub/page/session`, `/docs/_vitehub/page/assets/loader.js`, and `/docs/api/_vitehub/comments/**`. The loader and the overlay find the base from their own URL, including the overlay's Realtime presence and live update connections.
|
|
48
51
|
|
|
49
|
-
|
|
52
|
+
Custom `identify` names the user of every surface, and `authorize` decides; Comments then reads neither the Auth session nor the Console's access. Without `identify`, when the app has the Console, its Comments section controls access, and opening the Console enables the toolbar on the app's pages. Otherwise Comments reads the Auth session.
|
|
53
|
+
|
|
54
|
+
`authorize` gets `console: true` when the Console admitted the caller to its Comments section: the comment operations of the Console, the CLI, and ViteHub MCP, and `commentsMcpTools(comments, { console: true })`. With `identify`, these calls have no app user, so `authorize` must allow them itself:
|
|
55
|
+
|
|
56
|
+
```ts fragment
|
|
57
|
+
// Console callers, and the app's editors on the page.
|
|
58
|
+
authorize: ({ console, user }) => console === true || user?.role === "editor",
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
By default the overlay comments on the page itself, `{ kind: "page", id: "/pricing" }`. `page` maps a page to a subject of your own. The server decides: the overlay reads the subject from the session route, and a thread that the overlay starts must be on that subject. `authorize` still runs on it. A `page` given to the API, as the `?page=` query of a route such as `GET /threads?page=/pricing` or in the body of `POST /threads`, means that same subject.
|
|
62
|
+
|
|
63
|
+
```ts fragment
|
|
64
|
+
import { defineCommentSubject, defineComments } from "vite-hub/comments"
|
|
65
|
+
|
|
66
|
+
export const comments = defineComments({
|
|
67
|
+
subjects: [defineCommentSubject({ kind: "drop", scope: id => `drop:${id}` })],
|
|
68
|
+
page: async ({ url }) => {
|
|
69
|
+
const id = url.pathname.match(/^\/d\/([^/]+)/)?.[1]
|
|
70
|
+
return id ? { kind: "drop", id, revision: await latestRevision(id) } : undefined
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`url` has the origin and the normalized path, without query and hash. Return `undefined` or `null` for the page itself. With `revision`, new threads from the overlay start on the version that the person saw: the overlay sends the `revision` that `/session` gave for the page, and the server keeps it when it is not newer than the one `page` gives now. A missing or newer revision becomes the one from `page`.
|
|
50
76
|
|
|
51
77
|
Console `comments:read` access allows reading threads and joining presence. Writes also require full `comments` access and the Comments policy.
|
|
52
78
|
|
|
53
|
-
Press `C` and click the page to start a thread. Press `E` to edit text that `contentField()` marks:
|
|
79
|
+
Press `C` and click the page to start a thread, or press `B` to draw on the page with a marker, a pen, arrows, or text labels and comment with a picture of it. Press `E` to edit text that `contentField()` marks:
|
|
54
80
|
|
|
55
81
|
```vue
|
|
56
82
|
<script setup lang="ts">
|
|
@@ -69,10 +95,11 @@ An edit reads the draft, changes one string field, and saves the document with `
|
|
|
69
95
|
|
|
70
96
|
Every method returns `[error, data]`.
|
|
71
97
|
|
|
72
|
-
```ts
|
|
98
|
+
```ts fragment
|
|
73
99
|
import { comments } from "~/server/comments"
|
|
74
100
|
|
|
75
101
|
const [error, threads] = await comments.list({ page: "/pricing", status: "open" })
|
|
102
|
+
// threads[0].messages[0].body, threads[0].subject: { kind: "page", id: "/pricing" }
|
|
76
103
|
await comments.reply(threadId, { body: "Fixed in the draft", author: { id: "agent", name: "Docs Agent" } })
|
|
77
104
|
await comments.resolve(threadId, { by: { id: "agent", name: "Docs Agent" } })
|
|
78
105
|
```
|
|
@@ -89,14 +116,14 @@ On a preview, `forge: { provider: "github" }` posts each thread to the pull requ
|
|
|
89
116
|
- Without a Realtime authority, such as on Vercel, comments work without presence or cursors.
|
|
90
117
|
- Write routes compare `Origin` with the request origin, and honour `X-Forwarded-Proto` and `X-Forwarded-Host` from a proxy that ends TLS.
|
|
91
118
|
- Page context stores only the URL origin and pathname, validated against the request origin and thread page. Query parameters, URL credentials, and fragments are discarded. Nuxt component files come from its resolved component registry.
|
|
92
|
-
- Guests can comment only when `authorize` allows `user: null`. They name themselves in the browser, and their names and IDs are display data only. Guests cannot delete comments or threads.
|
|
119
|
+
- Guests can comment only when `authorize` allows `user: null`. They name themselves in the browser, and their names and IDs are display data only. Guests cannot delete comments or threads. `defineComments({ rateLimit: { limit: 5, window: "1m" } })` limits the writes of each guest address.
|
|
93
120
|
- The overlay is a custom element with a shadow root, so the page's CSS does not reach it.
|
|
94
121
|
|
|
95
122
|
## Public imports
|
|
96
123
|
|
|
97
124
|
| Import | Purpose |
|
|
98
125
|
| --- | --- |
|
|
99
|
-
| `@vite-hub/comments` | `defineComments()
|
|
126
|
+
| `@vite-hub/comments` | `defineComments()` and types. `installComments()` is in `@vite-hub/comments/server`. |
|
|
100
127
|
| `@vite-hub/comments/client` | Framework-free client, transport types, `createAnchor()`, and `resolveAnchor()`. |
|
|
101
128
|
| `@vite-hub/comments/testing` | In-memory client transport for tests and examples. |
|
|
102
129
|
| `@vite-hub/comments/anchor` | DOM anchoring: `createAnchor()`, `placeAnchor()`, `selectorOf()`, `labelOf()`. |
|
|
@@ -108,7 +135,17 @@ On a preview, `forge: { provider: "github" }` posts each thread to the pull requ
|
|
|
108
135
|
|
|
109
136
|
Generated files under `.vitehub/nitro/comments` are inspectable build output, not application imports.
|
|
110
137
|
|
|
111
|
-
`
|
|
138
|
+
`createCommentsHandler(options)` returns a Fetchable, `{ fetch(request) }`. Mount it as a Nitro or H3 route at `/api/_vitehub/comments/**`, or call `handler.fetch(request)` yourself. Every outcome is a `Response`, also the errors. The handler gives the Request to `identify` as `event`, and reads cookies, the Cloudflare bindings, and `waitUntil` from its Standard Context, `request["~standard"]`. In a ViteHub app, Nitro sets it. For a Request from elsewhere, the handler sets it.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { createCommentsHandler } from "@vite-hub/comments/runtime";
|
|
142
|
+
import { comments } from "./server/comments";
|
|
143
|
+
|
|
144
|
+
const handler = createCommentsHandler({ comments, realtime: false });
|
|
145
|
+
const response = await handler.fetch(new Request("https://site.test/api/_vitehub/comments/threads?page=/"));
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`commentsMcpTools(comments)` returns tools with `call(input, { user })`, where your server authenticates `user` or passes `null` for an anonymous caller. With a request `event`, such as the Request or an H3 event, a configured Comments `identify` or session reader takes precedence over `user`. The `page` filter of `comments_list_threads` and of `comments.threads` takes a path such as `/pricing` or a full URL of the page. When `authorize` hides threads from the caller, the list says how many and why, and its structured output has a `hidden` count. See [Fix comments with an Agent](https://vitehub.dev/docs/comments/configure#fix-comments-with-an-agent).
|
|
112
149
|
|
|
113
150
|
## Learn more
|
|
114
151
|
|
|
@@ -120,11 +157,13 @@ Generated files under `.vitehub/nitro/comments` are inspectable build output, no
|
|
|
120
157
|
|
|
121
158
|
Comments loads when its pill button is first used. Its thread list and replies open in the shared page-tools window. Expand or minimize it without replacing the reply form. Navigation updates the thread list to the current page. The pill asks before turning off tools with an unsent comment.
|
|
122
159
|
|
|
123
|
-
|
|
160
|
+
Select **Edit**, then a marked field, to edit plain text in place. A field that is not plain text says to edit it in the Console at console.vitehub.dev. The Content write and publish rules do not change.
|
|
161
|
+
|
|
162
|
+
## One model
|
|
124
163
|
|
|
125
|
-
|
|
164
|
+
The overlay, the server API, the comment operations, the browser client, the Console, and MCP share one model: threads on a subject, numbered `n` in a scope, with Markdown `messages`, mentions as ids, `metadata`, an optional subject `revision`, and attachments. One `authorize` policy decides on every surface.
|
|
126
165
|
|
|
127
|
-
|
|
166
|
+
`server/comments.ts` exports one definition, as `comments` or as the default export. Either form runs every surface: the overlay and the Comments API, presence rooms, the MCP tools, and the comment operations of the Console, the CLI, and MCP. See [Comment operations](https://vitehub.dev/docs/comments/operations/get-started). Threads live in `commentsSchema` from `/database`, or `commentsPostgresSchema` from `/database/postgres` with `vitehub({ comments: { dialect: "postgresql" } })`.
|
|
128
167
|
|
|
129
168
|
## Browser client
|
|
130
169
|
|
|
@@ -136,7 +175,7 @@ This package holds what the server and the UI share:
|
|
|
136
175
|
- `@vite-hub/comments/client` has no framework. It makes anchors, finds them again, and calls the comment operations through a transport.
|
|
137
176
|
- `@vite-hub/comments/testing` has `createMemoryTransport()` for tests and docs examples.
|
|
138
177
|
|
|
139
|
-
The Vue parts are in `@vite-hub/ui
|
|
178
|
+
The Vue parts are in `@vite-hub/ui`. Use `createRestTransport()` from `@vite-hub/comments/client` for the Comments API, which guests and share-link readers can use too, or `createRpcTransport()` from `@vite-hub/comments/rpc` for the operations RPC on a development server. A browser cannot sign Console calls, so the RPC transport works only with `console: { auth: false }` or without the `console` option. `transport.attachmentUrl(key)` of the REST transport is the URL of an attachment for people who may read its thread. The overlay uses the anchoring API of `@vite-hub/comments/anchor`, which writes `element` anchors with placement hints.
|
|
140
179
|
|
|
141
180
|
## Anchors
|
|
142
181
|
|
|
@@ -145,16 +184,18 @@ An anchor says where a comment sits, so the pin can find the spot again after th
|
|
|
145
184
|
```ts
|
|
146
185
|
import { createAnchor, resolveAnchor } from "@vite-hub/comments/client";
|
|
147
186
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
187
|
+
document.addEventListener("click", (event) => {
|
|
188
|
+
// From a click: the element under the point, plus where in its box the click landed.
|
|
189
|
+
const anchor = createAnchor({ x: event.clientX, y: event.clientY });
|
|
190
|
+
// { kind: "element", selector: "#pricing > div > button", x: 480, y: 310 }
|
|
191
|
+
|
|
192
|
+
const position = resolveAnchor(anchor);
|
|
193
|
+
if (position.detached) {
|
|
194
|
+
// The element is gone, hidden, or no longer holds the quote.
|
|
195
|
+
} else {
|
|
196
|
+
position.x; // The pin position in client coordinates.
|
|
197
|
+
}
|
|
198
|
+
});
|
|
158
199
|
```
|
|
159
200
|
|
|
160
201
|
| Kind | Made from | Keeps |
|
|
@@ -174,10 +215,10 @@ Offsets are in thousandths of the element box: `500` is the middle. A pin follow
|
|
|
174
215
|
## Client
|
|
175
216
|
|
|
176
217
|
```ts
|
|
177
|
-
import { createCommentsClient } from "@vite-hub/comments/client";
|
|
218
|
+
import { createAnchor, createCommentsClient, createRestTransport } from "@vite-hub/comments/client";
|
|
178
219
|
|
|
179
220
|
const client = createCommentsClient({
|
|
180
|
-
transport, // see below
|
|
221
|
+
transport: createRestTransport(), // the Comments API; see Transport below
|
|
181
222
|
user: { id: "ana", name: "Ana Ruiz" },
|
|
182
223
|
query: { page: location.pathname },
|
|
183
224
|
});
|
|
@@ -185,13 +226,15 @@ const client = createCommentsClient({
|
|
|
185
226
|
const stop = client.start(); // Loads the threads, then keeps them fresh.
|
|
186
227
|
await client.create({
|
|
187
228
|
subject: { kind: "page", id: location.pathname },
|
|
188
|
-
anchor,
|
|
229
|
+
anchor: createAnchor(document.querySelector("#pricing button")!),
|
|
189
230
|
body: "This button is too small.",
|
|
190
231
|
});
|
|
191
232
|
client.threads; // The cached threads, ordered by number.
|
|
192
|
-
client.onChange(() =>
|
|
233
|
+
client.onChange(() => console.log(`${client.threads.length} threads`));
|
|
193
234
|
```
|
|
194
235
|
|
|
236
|
+
To retry a create safely, give it a `requestId`, such as `crypto.randomUUID()`, and keep it for that draft. A second create with the same `requestId` in the same scope returns the first thread and creates no new thread. Only the author of the first thread gets it back, for the same subject, and only when that author may still read it. Anything else fails: the Operations and `POST /threads` answer `409` with `COMMENTS_R0022`, and the server API `comments.create()` returns `COMMENTS_INVALID_INPUT` with the reason. Make a new `requestId` for each new comment.
|
|
237
|
+
|
|
195
238
|
`start()` listens to the transport's `subscribe`, such as Realtime. Without `subscribe` it refreshes when the window gets focus. A list that comes back late never undoes a thread that was created, changed, or deleted while it was on its way, and an event that is older than the stored thread is ignored. A transport that lost its connection sends `{ type: "reconnected" }` when it has it again, and the client lists the threads once more.
|
|
196
239
|
|
|
197
240
|
Without an explicit query `limit`, refresh loads every page. Multi-page refreshes require two matching scans before replacing the cache. If three scans cannot agree because the list keeps changing, refresh raises `COMMENTS_R0015` and preserves the previous cache; refresh again to retry. An explicit `limit` makes one request.
|
|
@@ -201,7 +244,10 @@ Without an explicit query `limit`, refresh loads every page. Multi-page refreshe
|
|
|
201
244
|
A transport is the seam to the server. Each method has the name and the input of one `comments.*` operation.
|
|
202
245
|
|
|
203
246
|
```ts
|
|
247
|
+
import type { CommentCreateInput, CommentDeleteInput, CommentEditInput, CommentEvent, CommentGetInput, CommentListQuery, CommentReplyInput, CommentStatusInput, CommentThread, CommentUser, CommentUsersInput } from "@vite-hub/comments"
|
|
248
|
+
|
|
204
249
|
interface CommentsTransport {
|
|
250
|
+
supportsAttachments?: boolean; // Defaults to true for custom transports.
|
|
205
251
|
list(query: CommentListQuery): Promise<CommentThread[]>;
|
|
206
252
|
get(input: CommentGetInput): Promise<CommentThread | null>; // { id } or { scope, n }
|
|
207
253
|
create(input: CommentCreateInput): Promise<CommentThread>;
|
|
@@ -217,6 +263,8 @@ interface CommentsTransport {
|
|
|
217
263
|
|
|
218
264
|
A mutation returns the thread as it is afterwards. `delete` returns `null` when the thread is gone.
|
|
219
265
|
|
|
266
|
+
Transports accept attachment keys by default, including the shipped `createRpcTransport()`. Custom transports that reject attachment keys should set `supportsAttachments: false` to disable file controls in comment surfaces.
|
|
267
|
+
|
|
220
268
|
`createMemoryTransport()` keeps threads in memory. Use it in tests and docs examples:
|
|
221
269
|
|
|
222
270
|
```ts
|
package/dist/anchor.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { D as ElementAnchor } from "./types-CZujrMFd.js";
|
|
2
|
+
import "./types-BIS0hCEl.js";
|
|
2
3
|
//#region src/anchor.d.ts
|
|
4
|
+
/** The anchor of a pin that the overlay drops: an element anchor with every hint that `placeAnchor()` uses. */
|
|
5
|
+
export type PinAnchor = ElementAnchor & Required<Pick<ElementAnchor, "label" | "page" | "viewport">>;
|
|
3
6
|
/** The attribute that Content's `contentField()` writes on an element that renders a field. */
|
|
4
7
|
export declare const fieldAttribute = "data-content-field";
|
|
5
8
|
/**
|
|
@@ -12,7 +15,7 @@ export declare function labelOf(element: Element): string;
|
|
|
12
15
|
/** The closest element with a Content field, from the element itself up. */
|
|
13
16
|
export declare function fieldElementOf(element: Element): Element | undefined;
|
|
14
17
|
/** The anchor of a point on an element, in client coordinates. */
|
|
15
|
-
export declare function createAnchor(element: Element, clientX: number, clientY: number):
|
|
18
|
+
export declare function createAnchor(element: Element, clientX: number, clientY: number): PinAnchor;
|
|
16
19
|
export type PlacedAnchor =
|
|
17
20
|
/** On the anchored element. */
|
|
18
21
|
{
|
|
@@ -34,7 +37,7 @@ export type PlacedAnchor =
|
|
|
34
37
|
};
|
|
35
38
|
/**
|
|
36
39
|
* Where an anchor is now, in client coordinates: the Content field, then the CSS path if its label still matches,
|
|
37
|
-
* then any element of that tag with the same label, then the page point.
|
|
40
|
+
* then any element of that tag with the same label, then the page point. An anchor without a label trusts the path.
|
|
38
41
|
*/
|
|
39
|
-
export declare function placeAnchor(anchor:
|
|
42
|
+
export declare function placeAnchor(anchor: ElementAnchor): PlacedAnchor;
|
|
40
43
|
//#endregion
|
package/dist/anchor.js
CHANGED
|
@@ -59,6 +59,7 @@ function createAnchor(element, clientX, clientY) {
|
|
|
59
59
|
const box = element.getBoundingClientRect();
|
|
60
60
|
const field = fieldElementOf(element)?.getAttribute("data-content-field") ?? void 0;
|
|
61
61
|
return {
|
|
62
|
+
kind: "element",
|
|
62
63
|
...field ? { field } : {},
|
|
63
64
|
selector: selectorOf(element),
|
|
64
65
|
label: labelOf(element),
|
|
@@ -91,7 +92,7 @@ function findByLabel(label) {
|
|
|
91
92
|
}
|
|
92
93
|
/**
|
|
93
94
|
* Where an anchor is now, in client coordinates: the Content field, then the CSS path if its label still matches,
|
|
94
|
-
* then any element of that tag with the same label, then the page point.
|
|
95
|
+
* then any element of that tag with the same label, then the page point. An anchor without a label trusts the path.
|
|
95
96
|
*/
|
|
96
97
|
function placeAnchor(anchor) {
|
|
97
98
|
let element;
|
|
@@ -102,9 +103,9 @@ function placeAnchor(anchor) {
|
|
|
102
103
|
}
|
|
103
104
|
if (!element) {
|
|
104
105
|
const byPath = query(anchor.selector);
|
|
105
|
-
if (byPath && sameLabel(byPath, anchor.label)) element = byPath;
|
|
106
|
+
if (byPath && (anchor.label === void 0 || sameLabel(byPath, anchor.label))) element = byPath;
|
|
106
107
|
}
|
|
107
|
-
element ??= findByLabel(anchor.label);
|
|
108
|
+
if (anchor.label !== void 0) element ??= findByLabel(anchor.label);
|
|
108
109
|
if (element) {
|
|
109
110
|
const box = element.getBoundingClientRect();
|
|
110
111
|
if (!box.width && !box.height) return {
|
|
@@ -118,10 +119,14 @@ function placeAnchor(anchor) {
|
|
|
118
119
|
y: box.top + anchor.y / 1e3 * box.height
|
|
119
120
|
};
|
|
120
121
|
}
|
|
122
|
+
const page = anchor.page ?? {
|
|
123
|
+
x: 0,
|
|
124
|
+
y: 0
|
|
125
|
+
};
|
|
121
126
|
return {
|
|
122
127
|
state: "moved",
|
|
123
|
-
x:
|
|
124
|
-
y:
|
|
128
|
+
x: page.x / 1e3 * documentWidth() - window.scrollX,
|
|
129
|
+
y: page.y - window.scrollY
|
|
125
130
|
};
|
|
126
131
|
}
|
|
127
132
|
//#endregion
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Diagnostic, defineDiagnostics } from "nostics";
|
|
2
|
+
//#region src/client-diagnostics.ts
|
|
3
|
+
const commentsClientCodes = {
|
|
4
|
+
COMMENTS_R0001: {
|
|
5
|
+
why: ({ target }) => `createAnchor() found nothing to anchor to for ${target}.`,
|
|
6
|
+
fix: "Pass an element, a non-empty selection, or a point that is over an element of the page."
|
|
7
|
+
},
|
|
8
|
+
COMMENTS_R0013: {
|
|
9
|
+
why: "The comments request did not get an answer from the server.",
|
|
10
|
+
fix: "Check the connection and the app URL, then try again."
|
|
11
|
+
},
|
|
12
|
+
COMMENTS_R0015: {
|
|
13
|
+
why: "The comment list kept changing while its pages were loading.",
|
|
14
|
+
fix: "Refresh again. The previous cache is kept until two complete scans agree."
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const commentsClientDiagnostics = /*#__PURE__*/ defineDiagnostics({
|
|
18
|
+
docsBase: "https://vitehub.dev/docs/comments/diagnostics",
|
|
19
|
+
codes: commentsClientCodes
|
|
20
|
+
});
|
|
21
|
+
/** A failure that the server answered: a diagnostic with its code, why, and fix, and the HTTP status of the answer. */
|
|
22
|
+
function commentsAnswerError(failure) {
|
|
23
|
+
const { code, details, fix, status, why } = failure;
|
|
24
|
+
const docs = code.startsWith("COMMENTS_") ? `https://vitehub.dev/docs/comments/diagnostics/${code.toLowerCase()}` : void 0;
|
|
25
|
+
return Object.assign(new Diagnostic({
|
|
26
|
+
code,
|
|
27
|
+
why,
|
|
28
|
+
...fix ? { fix } : {},
|
|
29
|
+
...docs ? { docs } : {}
|
|
30
|
+
}), {
|
|
31
|
+
status,
|
|
32
|
+
...details ? { details } : {}
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/** `COMMENTS_R0013`: the request got no usable answer. `status` is 0 when it got no answer at all. */
|
|
36
|
+
function commentsUnansweredError(status, cause) {
|
|
37
|
+
return Object.assign(commentsClientDiagnostics.COMMENTS_R0013(cause === void 0 ? {} : { cause }), { status });
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { commentsUnansweredError as i, commentsClientCodes as n, commentsClientDiagnostics as r, commentsAnswerError as t };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as commentsErrorDiagnostics } from "./errors-
|
|
1
|
+
import { A as TextAnchor, C as CommentUploadInput, D as ElementAnchor, E as CommentsTransport, O as ImageAnchor, S as CommentThread, T as CommentUsersInput, _ as CommentMetadata, a as CommentBuild, b as CommentStatusInput, c as CommentCreateInput, d as CommentEvent, f as CommentForgeLink, g as CommentMessage, h as CommentListQuery, i as CommentAttachment, k as PageAnchor, l as CommentDeleteInput, m as CommentListCursor, n as CommentActor, o as CommentComponent, p as CommentGetInput, r as CommentAnchor, s as CommentContext, t as AreaAnchor, u as CommentEditInput, v as CommentReplyInput, w as CommentUser, x as CommentSubject, y as CommentStatus } from "./types-CZujrMFd.js";
|
|
2
|
+
import { t as commentsErrorDiagnostics } from "./errors-CyQyKAvy.js";
|
|
3
3
|
//#region src/client-anchor.d.ts
|
|
4
4
|
/** A point in client (viewport) coordinates, such as `event.clientX` and `event.clientY`. */
|
|
5
5
|
interface AnchorPoint {
|
|
@@ -54,6 +54,11 @@ export declare function selectorFor(element: Element, root?: ParentNode): string
|
|
|
54
54
|
* thousandths of the element box.
|
|
55
55
|
*/
|
|
56
56
|
export declare function createAnchor(target: AnchorTarget, options?: CreateAnchorOptions): CommentAnchor;
|
|
57
|
+
/**
|
|
58
|
+
* The range of the quote of a text anchor on the page, for a highlight. It finds the quote as `resolveAnchor()` does,
|
|
59
|
+
* and returns `undefined` when that would detach the anchor.
|
|
60
|
+
*/
|
|
61
|
+
export declare function anchorRange(anchor: TextAnchor, root?: ParentNode): Range | undefined;
|
|
57
62
|
/**
|
|
58
63
|
* Finds an anchor on the page again. It returns the pin position in client coordinates, or `{ detached: true }`
|
|
59
64
|
* when the element is gone, hidden, ambiguous, or no longer holds the quote. A selector that names more than one
|
|
@@ -62,6 +67,76 @@ export declare function createAnchor(target: AnchorTarget, options?: CreateAncho
|
|
|
62
67
|
*/
|
|
63
68
|
export declare function resolveAnchor(anchor: CommentAnchor, root?: ParentNode): AnchorPosition;
|
|
64
69
|
//#endregion
|
|
70
|
+
//#region src/fold-name.d.ts
|
|
71
|
+
/** Lower case without accents, so `jose` finds `José`. Mention suggestions match names this way, in the browser and on the server. */
|
|
72
|
+
export declare function foldName(text: string): string;
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/rest-transport.d.ts
|
|
75
|
+
/** A guest that named itself in the browser. The server keeps the name as display data only. */
|
|
76
|
+
interface CommentsGuest {
|
|
77
|
+
id: string;
|
|
78
|
+
name: string;
|
|
79
|
+
}
|
|
80
|
+
interface RestTransportOptions {
|
|
81
|
+
/**
|
|
82
|
+
* The app origin and mount, such as `https://app.example.com` or `/portal`. The routes are
|
|
83
|
+
* `<base>/api/_vitehub/comments/**`. Default: the page origin.
|
|
84
|
+
*/
|
|
85
|
+
base?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Headers for each request, such as a share-link token that `identify` reads. A function runs before each request,
|
|
88
|
+
* so it can read a token that changes.
|
|
89
|
+
*/
|
|
90
|
+
headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
91
|
+
/** The guest that writes when nobody is signed in, and the policy lets guests write. A function runs before each request. */
|
|
92
|
+
guest?: CommentsGuest | (() => CommentsGuest | undefined);
|
|
93
|
+
/** Default `same-origin`, so the browser sends the session cookie to the app's own origin. */
|
|
94
|
+
credentials?: RequestCredentials;
|
|
95
|
+
/** Replaces `fetch`, for tests or a server. */
|
|
96
|
+
fetch?: typeof fetch;
|
|
97
|
+
/** Runs after each answer that has data, such as to learn the names of the people that it mentions. */
|
|
98
|
+
onAnswer?: (answer: CommentsRestAnswer<unknown>) => void;
|
|
99
|
+
}
|
|
100
|
+
/** A thread as the API answers it: with what the reader may do with it. */
|
|
101
|
+
type RestCommentThread = CommentThread & {
|
|
102
|
+
can?: {
|
|
103
|
+
comment: boolean;
|
|
104
|
+
resolve: boolean;
|
|
105
|
+
delete?: boolean;
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
/** The answer of a route: its data, and the people that its threads mention, so a UI shows their names. */
|
|
109
|
+
interface CommentsRestAnswer<T> {
|
|
110
|
+
data: T;
|
|
111
|
+
people: CommentUser[];
|
|
112
|
+
/** The thread after a reply. */
|
|
113
|
+
thread?: RestCommentThread;
|
|
114
|
+
}
|
|
115
|
+
/** The request function of the Comments API that the overlay and `createRestTransport()` share. */
|
|
116
|
+
type CommentsRestRequest = <T>(path: string, init?: RequestInit) => Promise<CommentsRestAnswer<T>>;
|
|
117
|
+
/** The URL of an attachment key below the Comments API, such as `/api/_vitehub/comments/attachments/comments/<id>/shot.png`. */
|
|
118
|
+
export declare function commentAttachmentUrl(key: string, base?: string): string;
|
|
119
|
+
/**
|
|
120
|
+
* Requests to the Comments API at `<base>/api/_vitehub/comments`, with the guest headers and the session cookie. A
|
|
121
|
+
* failure throws a diagnostic with the `code`, `why`, and `fix` of the error object of the answer, and its `status`.
|
|
122
|
+
*/
|
|
123
|
+
export declare function createCommentsRestRequest(options?: RestTransportOptions & {
|
|
124
|
+
route?: string;
|
|
125
|
+
}): CommentsRestRequest;
|
|
126
|
+
/** A `CommentsTransport` over the Comments API, with `upload` and the URL of each attachment. */
|
|
127
|
+
interface RestTransport extends CommentsTransport {
|
|
128
|
+
upload: (input: CommentUploadInput) => Promise<CommentAttachment>;
|
|
129
|
+
/** The URL that serves the file of an attachment key to people who may read its thread, such as for a thumbnail. */
|
|
130
|
+
attachmentUrl: (key: string) => string;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* A transport over the Comments API at `<base>/api/_vitehub/comments`: the routes that the overlay uses. The server
|
|
134
|
+
* runs the `authorize` policy of `server/comments.ts` for the user of the request: the app session, or a guest that
|
|
135
|
+
* `guest` names. Guests and share-link readers can use it. It has no
|
|
136
|
+
* `subscribe`: the client refreshes when the window gets focus.
|
|
137
|
+
*/
|
|
138
|
+
export declare function createRestTransport(options?: RestTransportOptions): RestTransport;
|
|
139
|
+
//#endregion
|
|
65
140
|
//#region src/client.d.ts
|
|
66
141
|
export type CommentsClientStatus = "idle" | "loading" | "ready" | "error";
|
|
67
142
|
export interface CommentsClientOptions {
|
|
@@ -98,4 +173,4 @@ export interface CommentsClient {
|
|
|
98
173
|
/** A cache and a change signal over a transport. It has no framework in it. */
|
|
99
174
|
export declare function createCommentsClient(options: CommentsClientOptions): CommentsClient;
|
|
100
175
|
//#endregion
|
|
101
|
-
export { type AnchorPoint, type AnchorPosition, type AnchorRect, type AnchorTarget, type AreaAnchor, type CommentActor, type CommentAnchor, type CommentCreateInput, type CommentDeleteInput, type CommentEditInput, type CommentEvent, type CommentGetInput, type CommentListCursor, type CommentListQuery, type CommentMessage, type CommentReplyInput, type CommentStatus, type CommentStatusInput, type CommentSubject, type CommentThread, type CommentUser, type CommentUsersInput, type CommentsTransport, type CreateAnchorOptions, type DetachedAnchor, type ElementAnchor, type ImageAnchor, type PageAnchor, type ResolvedAnchor, type TextAnchor, commentsErrorDiagnostics };
|
|
176
|
+
export { type AnchorPoint, type AnchorPosition, type AnchorRect, type AnchorTarget, type AreaAnchor, type CommentActor, type CommentAnchor, type CommentAttachment, type CommentBuild, type CommentComponent, type CommentContext, type CommentCreateInput, type CommentDeleteInput, type CommentEditInput, type CommentEvent, type CommentForgeLink, type CommentGetInput, type CommentListCursor, type CommentListQuery, type CommentMessage, type CommentMetadata, type CommentReplyInput, type CommentStatus, type CommentStatusInput, type CommentSubject, type CommentThread, type CommentUploadInput, type CommentUser, type CommentUsersInput, type CommentsGuest, type CommentsRestAnswer, type CommentsRestRequest, type CommentsTransport, type CreateAnchorOptions, type DetachedAnchor, type ElementAnchor, type ImageAnchor, type PageAnchor, type ResolvedAnchor, type RestCommentThread, type RestTransport, type RestTransportOptions, type TextAnchor, commentsErrorDiagnostics };
|