@vite-hub/comments 0.0.0 → 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 +201 -0
- package/README.md +243 -0
- package/dist/anchor.d.ts +40 -0
- package/dist/anchor.js +128 -0
- package/dist/client.d.ts +101 -0
- package/dist/client.js +553 -0
- package/dist/console-BIz9t2cO.d.ts +243 -0
- package/dist/database/postgres.d.ts +1011 -0
- package/dist/database/postgres.js +60 -0
- package/dist/database-BSnVrwHn.js +60 -0
- package/dist/database-C-ktS9Do.js +95 -0
- package/dist/database-Cb2xjC1V.d.ts +1096 -0
- package/dist/database.d.ts +104 -0
- package/dist/database.js +3 -0
- package/dist/define-BzW2Ychn.d.ts +46 -0
- package/dist/define-Cbhl6QVJ.js +32 -0
- package/dist/errors-BAQYcy3_.d.ts +89 -0
- package/dist/errors-DeD6wTCl.js +77 -0
- package/dist/h3-v1-request-DDGXJ9E7.js +166 -0
- package/dist/identity-3qtVC4rF.js +34 -0
- package/dist/index-DklG2Etf.d.ts +111 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/operations-0B0QTVWX.d.ts +138 -0
- package/dist/operations-NZG5dB8y.js +732 -0
- package/dist/operations.d.ts +7 -0
- package/dist/operations.js +2 -0
- package/dist/overlay/overlay.js +195 -0
- package/dist/page/loader.js +22 -0
- package/dist/page-BtXADR6d.d.ts +21 -0
- package/dist/page-BuroSMXO.js +38 -0
- package/dist/page-tools-shared-D-amu6sx.js +53 -0
- package/dist/plugin-QElKQYkA.js +725 -0
- package/dist/realtime.d.ts +18 -0
- package/dist/realtime.js +48 -0
- package/dist/request-DVc3M_AG.js +14 -0
- package/dist/rpc.d.ts +29 -0
- package/dist/rpc.js +75 -0
- package/dist/runtime/auth.d.ts +6 -0
- package/dist/runtime/auth.js +15 -0
- package/dist/runtime/connect/node.d.ts +1 -0
- package/dist/runtime/connect/node.js +2 -0
- package/dist/runtime/connect.d.ts +1 -0
- package/dist/runtime/connect.js +2 -0
- package/dist/runtime/console.d.ts +5 -0
- package/dist/runtime/console.js +59 -0
- package/dist/runtime/index.d.ts +222 -0
- package/dist/runtime/index.js +665 -0
- package/dist/server-BomGg3Gb.js +63 -0
- package/dist/server.d.ts +94 -0
- package/dist/server.js +326 -0
- package/dist/src-CRTwnaEG.js +1362 -0
- package/dist/testing.d.ts +30 -0
- package/dist/testing.js +175 -0
- package/dist/types-Cj8K7Vx7.d.ts +176 -0
- package/dist/types-Un2dcJWD.d.ts +288 -0
- package/dist/vite.d.ts +304 -0
- package/dist/vite.js +1921 -0
- package/package.json +156 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# @vite-hub/comments
|
|
2
|
+
|
|
3
|
+
`@vite-hub/comments` lets a team comment on a live ViteHub site, Figma style. People pin comments to any element of any page, reply, mention each other, and resolve threads. Everyone on the same page sees the pins, the people, and their cursors live. Where an element renders a Content field, the same person can edit its text in place, and the edit saves a Content draft.
|
|
4
|
+
|
|
5
|
+
## Turn it on
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @vite-hub/comments
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// vite.config.ts
|
|
13
|
+
import comments from "@vite-hub/comments/vite"
|
|
14
|
+
|
|
15
|
+
export default defineConfig({ plugins: [comments()] })
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
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
|
+
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
|
+
|
|
22
|
+
### Sign in with GitHub
|
|
23
|
+
|
|
24
|
+
Create a GitHub App, install it on the repository, and list `https://<site>/api/_vitehub/comments/auth/callback/github` as its callback URL. Then set `VITEHUB_GITHUB_CLIENT_ID`, `VITEHUB_GITHUB_CLIENT_SECRET`, and `VITEHUB_SECRET` on the server. People who can push to the repository may comment. The session is an encrypted cookie, so the server stores no users.
|
|
25
|
+
|
|
26
|
+
### In a ViteHub app
|
|
27
|
+
|
|
28
|
+
[`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
|
+
```ts
|
|
31
|
+
// server/comments.ts
|
|
32
|
+
import { defineComments } from "vite-hub/comments"
|
|
33
|
+
|
|
34
|
+
export const comments = defineComments({
|
|
35
|
+
// Who may read, comment, and resolve. Without it, people signed in with GitHub who can push to the repository, and
|
|
36
|
+
// users of any other sign-in, may do everything; guests may on the dev server only.
|
|
37
|
+
authorize: ({ user }) => user?.role === "editor",
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
ViteHub then adds the `vitehub_comment_threads`, `vitehub_comments`, and `vitehub_comment_scope_counters` tables to the default Database (or the only one, or `defineComments({ database: "name" })`), serves `/api/_vitehub/comments/**`, and adds a Comments section to the Console. Apply the tables with `vitehub db generate` and `vitehub db migrate`.
|
|
42
|
+
|
|
43
|
+
Existing databases must follow [Upgrade existing Comments](https://vitehub.dev/docs/comments/configure#upgrade-existing-comments) before applying the scoped schema. Backfill thread numbers before creating the unique index, then initialize the durable scope counters.
|
|
44
|
+
|
|
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 unchanged. 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
|
+
|
|
47
|
+
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
|
+
|
|
49
|
+
When the app has the Console, its Comments section controls access, and opening the Console enables the toolbar on the app's pages. Custom `identify` runs after that access check. Without the Console, custom `identify` takes precedence over ViteHub Auth, and Comments reads the Auth session only when `identify` is absent.
|
|
50
|
+
|
|
51
|
+
Console `comments:read` access allows reading threads and joining presence. Writes also require full `comments` access and the Comments policy.
|
|
52
|
+
|
|
53
|
+
Press `C` and click the page to start a thread. Press `E` to edit text that `contentField()` marks:
|
|
54
|
+
|
|
55
|
+
```vue
|
|
56
|
+
<script setup lang="ts">
|
|
57
|
+
import { contentField } from "vite-hub/content/client"
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<template>
|
|
61
|
+
<h1 v-bind="contentField('about', 'hero.title')">{{ page.hero.title }}</h1>
|
|
62
|
+
<span v-bind="contentField('customers', customer.id, 'name')">{{ customer.name }}</span>
|
|
63
|
+
</template>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
An edit reads the draft, changes one string field, and saves the document with `ifDigest`. It needs `content.can({ action: "write" })`. Publishing stays in the Content publish flow.
|
|
67
|
+
|
|
68
|
+
## Server API
|
|
69
|
+
|
|
70
|
+
Every method returns `[error, data]`.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { comments } from "~/server/comments"
|
|
74
|
+
|
|
75
|
+
const [error, threads] = await comments.list({ page: "/pricing", status: "open" })
|
|
76
|
+
await comments.reply(threadId, { body: "Fixed in the draft", author: { id: "agent", name: "Docs Agent" } })
|
|
77
|
+
await comments.resolve(threadId, { by: { id: "agent", name: "Docs Agent" } })
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Post to the pull request
|
|
81
|
+
|
|
82
|
+
On a preview, `forge: { provider: "github" }` posts each thread to the pull request that the build previews: one summary comment that lists every thread, then one comment per comment, with the page, the element, the components, and the browser. Resolving a thread hides its comment. It posts with the GitHub App of `VITEHUB_GITHUB_APP_ID` and `VITEHUB_GITHUB_PRIVATE_KEY`, or as each person: people who signed in with GitHub post as themselves, and `token(author)` returns the token for any other sign-in. Each thread's `forge` field says whether it posted.
|
|
83
|
+
|
|
84
|
+
## Deployment and security boundaries
|
|
85
|
+
|
|
86
|
+
- Threads live only in the Database. Presence rooms hold no document, and connections are read-only. The server binds each person's presence to the user that `identify` or the Auth session resolves.
|
|
87
|
+
- Pins anchor to the Content field first, then a CSS path checked against the element text, then the text alone, then the page point. A pin whose element is gone shows dashed.
|
|
88
|
+
- A thread change tells the other tabs of the page through the Realtime events room, and they read the threads again. Changes from the server API reach open pages when they refetch: on focus and every 30 seconds.
|
|
89
|
+
- Without a Realtime authority, such as on Vercel, comments work without presence or cursors.
|
|
90
|
+
- Write routes compare `Origin` with the request origin, and honour `X-Forwarded-Proto` and `X-Forwarded-Host` from a proxy that ends TLS.
|
|
91
|
+
- 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.
|
|
93
|
+
- The overlay is a custom element with a shadow root, so the page's CSS does not reach it.
|
|
94
|
+
|
|
95
|
+
## Public imports
|
|
96
|
+
|
|
97
|
+
| Import | Purpose |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| `@vite-hub/comments` | `defineComments()`, `installComments()`, and types. |
|
|
100
|
+
| `@vite-hub/comments/client` | Framework-free client, transport types, `createAnchor()`, and `resolveAnchor()`. |
|
|
101
|
+
| `@vite-hub/comments/testing` | In-memory client transport for tests and examples. |
|
|
102
|
+
| `@vite-hub/comments/anchor` | DOM anchoring: `createAnchor()`, `placeAnchor()`, `selectorOf()`, `labelOf()`. |
|
|
103
|
+
| `@vite-hub/comments/database` | `commentsSchema`, the Drizzle tables, and `commentsTables`, the statements that create them. |
|
|
104
|
+
| `@vite-hub/comments/realtime` | `commentsRealtime()`, the presence room Realtime Definition. |
|
|
105
|
+
| `@vite-hub/comments/runtime` | `createCommentsHandler()` and `defineCommentsPlugin()` for a manual Nitro setup; `commentsMcpTools()` for an app's own MCP server. |
|
|
106
|
+
| `@vite-hub/comments/vite` | `comments()`, the plugin for apps without `vite-hub`, and `hubComments()`, the integration that `vitehub()` adds. |
|
|
107
|
+
| `@vite-hub/comments/runtime/connect` | `connectDatabase()` of `@vite-hub/database/connect`, for the files that `comments()` generates. `/node` also opens SQLite files. |
|
|
108
|
+
|
|
109
|
+
Generated files under `.vitehub/nitro/comments` are inspectable build output, not application imports.
|
|
110
|
+
|
|
111
|
+
`commentsMcpTools(comments)` returns tools with `call(input, { user })`, where your server authenticates `user` or passes `null` for an anonymous caller. With an H3 request `event`, a configured Comments `identify` or session reader takes precedence over `user`. See [Fix comments with an Agent](https://vitehub.dev/docs/comments/configure#fix-comments-with-an-agent).
|
|
112
|
+
|
|
113
|
+
## Learn more
|
|
114
|
+
|
|
115
|
+
- [Comments](https://vitehub.dev/docs/comments) covers access, guests, mentions, hosts, and errors.
|
|
116
|
+
- [Content](https://vitehub.dev/docs/content) covers collections, drafts, and publishing.
|
|
117
|
+
- [Realtime](https://vitehub.dev/docs/realtime) covers room authorities.
|
|
118
|
+
|
|
119
|
+
### Floating window
|
|
120
|
+
|
|
121
|
+
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
|
+
|
|
123
|
+
With access to the Content page tool, selecting **Edit** and a marked field opens that document in the existing Content editor. Otherwise Comments keeps its plain-text inline editor. The Content write and publish rules do not change.
|
|
124
|
+
|
|
125
|
+
## Comments through Operations
|
|
126
|
+
|
|
127
|
+
A default export from `server/comments.ts` selects numbered, policy-checked Comments Operations instead of the live-site API. See [Comment operations](https://vitehub.dev/docs/comments/operations/get-started). The browser client, RPC transport, and memory transport are available from `/client`, `/rpc`, and `/testing`. The operations schema is exported as `commentsOperationsSchema` from `/database` (or `commentsPostgresSchema` from `/database/postgres`).
|
|
128
|
+
|
|
129
|
+
## Browser client
|
|
130
|
+
|
|
131
|
+
Figma-style comments for your own running app. A signed-in team member drops a pin on any spot of a page, and the team replies in the thread.
|
|
132
|
+
|
|
133
|
+
This package holds what the server and the UI share:
|
|
134
|
+
|
|
135
|
+
- `@vite-hub/comments/client` exports the browser types: `CommentThread`, `CommentMessage`, `CommentAnchor`, `CommentSubject`, `CommentUser`, `CommentStatus`, and the `CommentsTransport` contract.
|
|
136
|
+
- `@vite-hub/comments/client` has no framework. It makes anchors, finds them again, and calls the comment operations through a transport.
|
|
137
|
+
- `@vite-hub/comments/testing` has `createMemoryTransport()` for tests and docs examples.
|
|
138
|
+
|
|
139
|
+
The Vue parts are in `@vite-hub/ui/comments`. Use `createRpcTransport()` from `@vite-hub/comments/rpc` with Comments Operations, or supply a transport for your backend. The live-site server and overlay use the server types from `@vite-hub/comments` and the anchoring API from `@vite-hub/comments/anchor`; the browser client has its own thread and anchor format. At the root entry, its two overlapping types are available as `ClientCommentThread` and `ClientCommentAnchor`.
|
|
140
|
+
|
|
141
|
+
## Anchors
|
|
142
|
+
|
|
143
|
+
An anchor says where a comment sits, so the pin can find the spot again after the layout changes.
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { createAnchor, resolveAnchor } from "@vite-hub/comments/client";
|
|
147
|
+
|
|
148
|
+
// From a click: the element under the point, plus where in its box the click landed.
|
|
149
|
+
const anchor = createAnchor({ x: event.clientX, y: event.clientY });
|
|
150
|
+
// { kind: "element", selector: "#pricing > div > button", x: 480, y: 310 }
|
|
151
|
+
|
|
152
|
+
const position = resolveAnchor(anchor);
|
|
153
|
+
if (position.detached) {
|
|
154
|
+
// The element is gone, hidden, or no longer holds the quote.
|
|
155
|
+
} else {
|
|
156
|
+
position.x; // The pin position in client coordinates.
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
| Kind | Made from | Keeps |
|
|
161
|
+
| --- | --- | --- |
|
|
162
|
+
| `page` | `"page"` | Nothing. The whole page. |
|
|
163
|
+
| `element` | an element, or a point | The element selector and a point in thousandths of its box. |
|
|
164
|
+
| `text` | a `Selection` or a `Range` | The selector of the nearest element, and the quote as `exact`, `prefix`, `suffix`. |
|
|
165
|
+
| `area` | an element and `options.area` | The selector and a rectangle in thousandths. |
|
|
166
|
+
| `image` | an `img`, with a point or `options.area` | The same as `element` or `area`, on an image. |
|
|
167
|
+
|
|
168
|
+
The selector uses the first of these that names exactly one element: `data-comment-id`, `id`, `data-testid`. Else it is a short path such as `#plans > div > p:nth-of-type(2)`. Ids that a framework makes up, such as React's `:r1:`, `radix-…`, `headlessui-…`, or Vue's `v-1`, and ids that are mostly digits or hashes, are skipped. Add `data-comment-id` to an element that must keep its comments when the markup changes.
|
|
169
|
+
|
|
170
|
+
`resolveAnchor()` never guesses. A selector that names more than one element is ambiguous, so the anchor is detached. A text anchor tries every match and takes the one that holds the quote. A quote that occurs more than once needs enough text around it to match, else the anchor is detached. A resolved anchor has `clipped: true` when a scroll container or an `overflow: hidden` ancestor cuts the spot off. Hide the pin then.
|
|
171
|
+
|
|
172
|
+
Offsets are in thousandths of the element box: `500` is the middle. A pin follows its element when the window resizes.
|
|
173
|
+
|
|
174
|
+
## Client
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { createCommentsClient } from "@vite-hub/comments/client";
|
|
178
|
+
|
|
179
|
+
const client = createCommentsClient({
|
|
180
|
+
transport, // see below
|
|
181
|
+
user: { id: "ana", name: "Ana Ruiz" },
|
|
182
|
+
query: { page: location.pathname },
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const stop = client.start(); // Loads the threads, then keeps them fresh.
|
|
186
|
+
await client.create({
|
|
187
|
+
subject: { kind: "page", id: location.pathname },
|
|
188
|
+
anchor,
|
|
189
|
+
body: "This button is too small.",
|
|
190
|
+
});
|
|
191
|
+
client.threads; // The cached threads, ordered by number.
|
|
192
|
+
client.onChange(() => render(client.threads));
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
`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
|
+
|
|
197
|
+
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.
|
|
198
|
+
|
|
199
|
+
### Transport
|
|
200
|
+
|
|
201
|
+
A transport is the seam to the server. Each method has the name and the input of one `comments.*` operation.
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
interface CommentsTransport {
|
|
205
|
+
list(query: CommentListQuery): Promise<CommentThread[]>;
|
|
206
|
+
get(input: CommentGetInput): Promise<CommentThread | null>; // { id } or { scope, n }
|
|
207
|
+
create(input: CommentCreateInput): Promise<CommentThread>;
|
|
208
|
+
reply(input: CommentReplyInput): Promise<CommentThread>;
|
|
209
|
+
edit(input: CommentEditInput): Promise<CommentThread>;
|
|
210
|
+
delete(input: CommentDeleteInput): Promise<CommentThread | null>;
|
|
211
|
+
resolve(input: CommentStatusInput): Promise<CommentThread>;
|
|
212
|
+
reopen(input: CommentStatusInput): Promise<CommentThread>;
|
|
213
|
+
users(input: CommentUsersInput): Promise<CommentUser[]>;
|
|
214
|
+
subscribe?(listener: (event: CommentEvent) => void): () => void;
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
A mutation returns the thread as it is afterwards. `delete` returns `null` when the thread is gone.
|
|
219
|
+
|
|
220
|
+
`createMemoryTransport()` keeps threads in memory. Use it in tests and docs examples:
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
import { createCommentsClient } from "@vite-hub/comments/client";
|
|
224
|
+
import { createMemoryTransport } from "@vite-hub/comments/testing";
|
|
225
|
+
|
|
226
|
+
const ana = { id: "ana", name: "Ana Ruiz" };
|
|
227
|
+
const transport = createMemoryTransport({ user: ana });
|
|
228
|
+
const client = createCommentsClient({ transport, user: ana });
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
`transport.as(user)` gives another person on the same store, and `transport.reconnect()` sends `reconnected` to the listeners.
|
|
232
|
+
|
|
233
|
+
## Errors
|
|
234
|
+
|
|
235
|
+
Errors have a stable code, a `why`, and a `fix`.
|
|
236
|
+
|
|
237
|
+
| Code | Meaning |
|
|
238
|
+
| --- | --- |
|
|
239
|
+
| `COMMENTS_R0001` | `createAnchor()` found nothing to anchor to. |
|
|
240
|
+
| `COMMENTS_R0002` | The thread does not exist. |
|
|
241
|
+
| `COMMENTS_R0003` | The message does not exist in the thread. |
|
|
242
|
+
| `COMMENTS_R0004` | Only the author can edit or delete a message. |
|
|
243
|
+
| `COMMENTS_R0005` | A comment needs some text. |
|
package/dist/anchor.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { n as CommentAnchor } from "./types-Un2dcJWD.js";
|
|
2
|
+
//#region src/anchor.d.ts
|
|
3
|
+
/** The attribute that Content's `contentField()` writes on an element that renders a field. */
|
|
4
|
+
export declare const fieldAttribute = "data-content-field";
|
|
5
|
+
/**
|
|
6
|
+
* CSS path of an element: tag and `:nth-of-type` per level, at most 10 levels, from the closest ancestor with an id or
|
|
7
|
+
* a Content field. Positional only, so `label` checks that it still finds the same element.
|
|
8
|
+
*/
|
|
9
|
+
export declare function selectorOf(element: Element): string;
|
|
10
|
+
/** Tag and the first 40 characters of the element text. */
|
|
11
|
+
export declare function labelOf(element: Element): string;
|
|
12
|
+
/** The closest element with a Content field, from the element itself up. */
|
|
13
|
+
export declare function fieldElementOf(element: Element): Element | undefined;
|
|
14
|
+
/** The anchor of a point on an element, in client coordinates. */
|
|
15
|
+
export declare function createAnchor(element: Element, clientX: number, clientY: number): CommentAnchor;
|
|
16
|
+
export type PlacedAnchor =
|
|
17
|
+
/** On the anchored element. */
|
|
18
|
+
{
|
|
19
|
+
state: "exact";
|
|
20
|
+
element: Element;
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
} |
|
|
24
|
+
/** At the page point, because the element is gone. The overlay shows the pin as approximate. */
|
|
25
|
+
{
|
|
26
|
+
state: "moved";
|
|
27
|
+
x: number;
|
|
28
|
+
y: number;
|
|
29
|
+
} |
|
|
30
|
+
/** The element exists but has no box, such as inside a closed menu. */
|
|
31
|
+
{
|
|
32
|
+
state: "hidden";
|
|
33
|
+
element: Element;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* 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.
|
|
38
|
+
*/
|
|
39
|
+
export declare function placeAnchor(anchor: CommentAnchor): PlacedAnchor;
|
|
40
|
+
//#endregion
|
package/dist/anchor.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
//#region src/anchor.ts
|
|
2
|
+
/** The attribute that Content's `contentField()` writes on an element that renders a field. */
|
|
3
|
+
const fieldAttribute = "data-content-field";
|
|
4
|
+
const maxDepth = 10;
|
|
5
|
+
function cssEscape(value) {
|
|
6
|
+
return typeof CSS !== "undefined" && CSS.escape ? CSS.escape(value) : value.replace(/[^\w-]/g, (character) => `\\${character}`);
|
|
7
|
+
}
|
|
8
|
+
function attributeSelector(name, value) {
|
|
9
|
+
return `[${name}="${value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}"]`;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* CSS path of an element: tag and `:nth-of-type` per level, at most 10 levels, from the closest ancestor with an id or
|
|
13
|
+
* a Content field. Positional only, so `label` checks that it still finds the same element.
|
|
14
|
+
*/
|
|
15
|
+
function selectorOf(element) {
|
|
16
|
+
const parts = [];
|
|
17
|
+
let current = element;
|
|
18
|
+
while (current && current !== document.documentElement && parts.length < maxDepth) {
|
|
19
|
+
if (current.id) {
|
|
20
|
+
parts.unshift(`#${cssEscape(current.id)}`);
|
|
21
|
+
return parts.join(" > ");
|
|
22
|
+
}
|
|
23
|
+
const field = current.getAttribute(fieldAttribute);
|
|
24
|
+
if (field) {
|
|
25
|
+
parts.unshift(attributeSelector(fieldAttribute, field));
|
|
26
|
+
return parts.join(" > ");
|
|
27
|
+
}
|
|
28
|
+
const tag = current.tagName.toLowerCase();
|
|
29
|
+
const parent = current.parentElement;
|
|
30
|
+
if (!parent) {
|
|
31
|
+
parts.unshift(tag);
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
const sameTag = [...parent.children].filter((child) => child.tagName === current.tagName);
|
|
35
|
+
parts.unshift(sameTag.length > 1 ? `${tag}:nth-of-type(${sameTag.indexOf(current) + 1})` : tag);
|
|
36
|
+
current = parent;
|
|
37
|
+
}
|
|
38
|
+
return parts.join(" > ");
|
|
39
|
+
}
|
|
40
|
+
/** Tag and the first 40 characters of the element text. */
|
|
41
|
+
function labelOf(element) {
|
|
42
|
+
const text = (element.textContent ?? "").replace(/\s+/g, " ").trim().slice(0, 40);
|
|
43
|
+
const tag = element.tagName.toLowerCase();
|
|
44
|
+
return text ? `${tag} ${text}` : tag;
|
|
45
|
+
}
|
|
46
|
+
function thousandths(value, size) {
|
|
47
|
+
if (!size) return 0;
|
|
48
|
+
return Math.min(1e3, Math.max(0, Math.round(value / size * 1e3)));
|
|
49
|
+
}
|
|
50
|
+
function documentWidth() {
|
|
51
|
+
return Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth, 1);
|
|
52
|
+
}
|
|
53
|
+
/** The closest element with a Content field, from the element itself up. */
|
|
54
|
+
function fieldElementOf(element) {
|
|
55
|
+
return element.closest(`[data-content-field]`) ?? void 0;
|
|
56
|
+
}
|
|
57
|
+
/** The anchor of a point on an element, in client coordinates. */
|
|
58
|
+
function createAnchor(element, clientX, clientY) {
|
|
59
|
+
const box = element.getBoundingClientRect();
|
|
60
|
+
const field = fieldElementOf(element)?.getAttribute("data-content-field") ?? void 0;
|
|
61
|
+
return {
|
|
62
|
+
...field ? { field } : {},
|
|
63
|
+
selector: selectorOf(element),
|
|
64
|
+
label: labelOf(element),
|
|
65
|
+
x: thousandths(clientX - box.left, box.width),
|
|
66
|
+
y: thousandths(clientY - box.top, box.height),
|
|
67
|
+
page: {
|
|
68
|
+
x: thousandths(clientX + window.scrollX, documentWidth()),
|
|
69
|
+
y: Math.max(0, Math.round(clientY + window.scrollY))
|
|
70
|
+
},
|
|
71
|
+
viewport: { width: window.innerWidth }
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function query(selector) {
|
|
75
|
+
try {
|
|
76
|
+
return document.querySelector(selector) ?? void 0;
|
|
77
|
+
} catch {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function sameLabel(element, label) {
|
|
82
|
+
return labelOf(element) === label;
|
|
83
|
+
}
|
|
84
|
+
/** An element of the same tag whose label matches, for anchors whose CSS path moved. */
|
|
85
|
+
function findByLabel(label) {
|
|
86
|
+
const tag = label.split(" ", 1)[0];
|
|
87
|
+
if (!tag || tag === label) return;
|
|
88
|
+
try {
|
|
89
|
+
for (const candidate of document.querySelectorAll(tag)) if (sameLabel(candidate, label)) return candidate;
|
|
90
|
+
} catch {}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* 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
|
+
*/
|
|
96
|
+
function placeAnchor(anchor) {
|
|
97
|
+
let element;
|
|
98
|
+
if (anchor.field) {
|
|
99
|
+
element = query(attributeSelector(fieldAttribute, anchor.field));
|
|
100
|
+
const byPath = query(anchor.selector);
|
|
101
|
+
if (element && byPath && fieldElementOf(byPath)?.getAttribute("data-content-field") === anchor.field) element = byPath;
|
|
102
|
+
}
|
|
103
|
+
if (!element) {
|
|
104
|
+
const byPath = query(anchor.selector);
|
|
105
|
+
if (byPath && sameLabel(byPath, anchor.label)) element = byPath;
|
|
106
|
+
}
|
|
107
|
+
element ??= findByLabel(anchor.label);
|
|
108
|
+
if (element) {
|
|
109
|
+
const box = element.getBoundingClientRect();
|
|
110
|
+
if (!box.width && !box.height) return {
|
|
111
|
+
state: "hidden",
|
|
112
|
+
element
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
state: "exact",
|
|
116
|
+
element,
|
|
117
|
+
x: box.left + anchor.x / 1e3 * box.width,
|
|
118
|
+
y: box.top + anchor.y / 1e3 * box.height
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
state: "moved",
|
|
123
|
+
x: anchor.page.x / 1e3 * documentWidth() - window.scrollX,
|
|
124
|
+
y: anchor.page.y - window.scrollY
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
//#endregion
|
|
128
|
+
export { createAnchor, fieldAttribute, fieldElementOf, labelOf, placeAnchor, selectorOf };
|