@vite-hub/comments 0.0.0 → 0.3.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 +291 -0
- package/dist/anchor.d.ts +43 -0
- package/dist/anchor.js +133 -0
- package/dist/client-diagnostics-CcFpkbAk.js +40 -0
- package/dist/client.d.ts +176 -0
- package/dist/client.js +738 -0
- package/dist/console-h4KlhIik.d.ts +332 -0
- package/dist/context-BqIsmYP1.js +61 -0
- package/dist/database/postgres.d.ts +1402 -0
- package/dist/database/postgres.js +81 -0
- package/dist/database-CA1aHKN7.d.ts +1495 -0
- package/dist/database.d.ts +12 -0
- package/dist/database.js +2 -0
- 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-CBOzgxSf.js +39 -0
- package/dist/index-DNhy69Wt.d.ts +83 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +196 -0
- 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-D4A1Fcgf.d.ts +160 -0
- package/dist/operations-DMMIfYPW.js +1159 -0
- package/dist/operations-GLeOqgMt.d.ts +257 -0
- package/dist/operations.d.ts +3 -0
- package/dist/operations.js +3 -0
- 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 +201 -0
- package/dist/page/loader.js +142 -0
- package/dist/page-B41ozN63.js +40 -0
- package/dist/page-DtbMOEMd.d.ts +22 -0
- 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 +12 -0
- package/dist/realtime.js +47 -0
- package/dist/rpc-HBcWtg0T.d.ts +17 -0
- package/dist/rpc-RKPAusHp.js +153 -0
- package/dist/rpc.d.ts +2 -0
- package/dist/rpc.js +2 -0
- package/dist/runtime/auth.d.ts +8 -0
- package/dist/runtime/auth.js +18 -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 +64 -0
- package/dist/runtime/index.d.ts +270 -0
- package/dist/runtime/index.js +1113 -0
- package/dist/server-D8k-0GXe.js +72 -0
- package/dist/server.d.ts +20 -0
- package/dist/server.js +7 -0
- 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 +30 -0
- package/dist/testing.js +187 -0
- 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 +308 -0
- package/dist/vite.js +1660 -0
- package/package.json +153 -4
package/dist/client.js
ADDED
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
import { i as commentsUnansweredError, r as commentsClientDiagnostics, t as commentsAnswerError } from "./client-diagnostics-CcFpkbAk.js";
|
|
2
|
+
import { t as commentsErrorDiagnostics } from "./errors-C6qptPoG.js";
|
|
3
|
+
import { t as foldName } from "./fold-name-B7Zb3-MF.js";
|
|
4
|
+
import { i as commentsRoute } from "./page-B41ozN63.js";
|
|
5
|
+
import { n as isRecordOrFunction } from "./object-Da1zyPCR.js";
|
|
6
|
+
//#region src/client-anchor.ts
|
|
7
|
+
const defaultContext = 32;
|
|
8
|
+
const clamp = (value) => Math.min(1e3, Math.max(0, value));
|
|
9
|
+
function thousandths(value, size) {
|
|
10
|
+
return size > 0 ? clamp(Math.round(value / size * 1e3)) : 0;
|
|
11
|
+
}
|
|
12
|
+
function ownerDocument(node) {
|
|
13
|
+
return node.nodeType === 9 ? node : node.ownerDocument;
|
|
14
|
+
}
|
|
15
|
+
function defaultRoot(node) {
|
|
16
|
+
const root = node.getRootNode();
|
|
17
|
+
return root.nodeType === 11 ? root : ownerDocument(node) ?? document;
|
|
18
|
+
}
|
|
19
|
+
function attr(value) {
|
|
20
|
+
return `"${value.replace(/["\\]/g, "\\$&")}"`;
|
|
21
|
+
}
|
|
22
|
+
function lookup(root, selector) {
|
|
23
|
+
if (selector === ":scope" && root.nodeType === 1) return [root];
|
|
24
|
+
try {
|
|
25
|
+
return [...root.querySelectorAll(selector)];
|
|
26
|
+
} catch {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function isOnly(root, selector, element) {
|
|
31
|
+
const found = lookup(root, selector);
|
|
32
|
+
return found.length === 1 && found[0] === element;
|
|
33
|
+
}
|
|
34
|
+
const autoIds = [
|
|
35
|
+
/^:[\w-]+:$/,
|
|
36
|
+
/^«[^»]*»$/,
|
|
37
|
+
/^_r_[\w-]+_$/,
|
|
38
|
+
/^radix-/,
|
|
39
|
+
/^headlessui-/,
|
|
40
|
+
/^reka-/,
|
|
41
|
+
/^v-\d/,
|
|
42
|
+
/^data-v-/,
|
|
43
|
+
/^ember\d/,
|
|
44
|
+
/^[\da-f-]{8,}$/i
|
|
45
|
+
];
|
|
46
|
+
function isAutoId(id) {
|
|
47
|
+
if (autoIds.some((pattern) => pattern.test(id))) return true;
|
|
48
|
+
const digits = id.replace(/\D/g, "").length;
|
|
49
|
+
return id.length >= 6 && digits / id.length > .5;
|
|
50
|
+
}
|
|
51
|
+
function stableSelector(element, root) {
|
|
52
|
+
const candidates = [];
|
|
53
|
+
const commentId = element.getAttribute("data-comment-id");
|
|
54
|
+
if (commentId) candidates.push(`[data-comment-id=${attr(commentId)}]`);
|
|
55
|
+
const id = element.getAttribute("id");
|
|
56
|
+
if (id && !isAutoId(id)) candidates.push(/^[A-Za-z][\w-]*$/.test(id) ? `#${id}` : `[id=${attr(id)}]`);
|
|
57
|
+
const testId = element.getAttribute("data-testid");
|
|
58
|
+
if (testId) candidates.push(`[data-testid=${attr(testId)}]`);
|
|
59
|
+
return candidates.find((candidate) => isOnly(root, candidate, element));
|
|
60
|
+
}
|
|
61
|
+
function segment(element) {
|
|
62
|
+
const tag = element.localName;
|
|
63
|
+
const parent = element.parentElement;
|
|
64
|
+
if (!parent) return tag;
|
|
65
|
+
const same = [...parent.children].filter((child) => child.localName === tag);
|
|
66
|
+
return same.length > 1 ? `${tag}:nth-of-type(${same.indexOf(element) + 1})` : tag;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The shortest selector that names the element: a stable attribute, else a path from the nearest stable
|
|
70
|
+
* ancestor or from the top of the page, cut as soon as it is unique.
|
|
71
|
+
*/
|
|
72
|
+
function selectorFor(element, root = defaultRoot(element)) {
|
|
73
|
+
if (element === root) return ":scope";
|
|
74
|
+
const own = stableSelector(element, root);
|
|
75
|
+
if (own) return own;
|
|
76
|
+
const parts = [];
|
|
77
|
+
let node = element;
|
|
78
|
+
while (node && node !== root) {
|
|
79
|
+
const stable = node === element ? void 0 : stableSelector(node, root);
|
|
80
|
+
parts.unshift(stable ?? segment(node));
|
|
81
|
+
const path = parts.join(" > ");
|
|
82
|
+
if (stable || isOnly(root, path, element)) return path;
|
|
83
|
+
node = node.parentElement;
|
|
84
|
+
}
|
|
85
|
+
return parts.join(" > ");
|
|
86
|
+
}
|
|
87
|
+
function boxOf(element) {
|
|
88
|
+
const { left, top, width, height } = element.getBoundingClientRect();
|
|
89
|
+
return {
|
|
90
|
+
left,
|
|
91
|
+
top,
|
|
92
|
+
width,
|
|
93
|
+
height
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function pointTarget(point, options) {
|
|
97
|
+
const node = options.root;
|
|
98
|
+
const scope = node?.nodeType === 11 ? node : node && ownerDocument(node) || document;
|
|
99
|
+
return (typeof scope.elementsFromPoint === "function" ? scope.elementsFromPoint(point.x, point.y) : [scope.elementFromPoint(point.x, point.y)]).find((element) => element !== null && (!node || node.contains(element)) && !options.ignore?.(element));
|
|
100
|
+
}
|
|
101
|
+
function textNodes(node, into = []) {
|
|
102
|
+
for (const child of node.childNodes) if (child.nodeType === 3) into.push(child);
|
|
103
|
+
else if (child.nodeType === 1) textNodes(child, into);
|
|
104
|
+
return into;
|
|
105
|
+
}
|
|
106
|
+
const FOLLOWING = 4;
|
|
107
|
+
function textOffset(element, container, offset) {
|
|
108
|
+
let total = 0;
|
|
109
|
+
const next = container.nodeType === 3 ? void 0 : container.childNodes[offset];
|
|
110
|
+
for (const node of textNodes(element)) {
|
|
111
|
+
if (container.nodeType === 3) {
|
|
112
|
+
if (node === container) return total + offset;
|
|
113
|
+
} else if (next ? node === next || next.compareDocumentPosition(node) & FOLLOWING : !container.contains(node) && container.compareDocumentPosition(node) & FOLLOWING) break;
|
|
114
|
+
total += node.data.length;
|
|
115
|
+
}
|
|
116
|
+
return total;
|
|
117
|
+
}
|
|
118
|
+
function rangeOf(target) {
|
|
119
|
+
if ("rangeCount" in target) return target.rangeCount > 0 && !target.isCollapsed ? target.getRangeAt(0) : void 0;
|
|
120
|
+
return target.collapsed ? void 0 : target;
|
|
121
|
+
}
|
|
122
|
+
function elementOfNode(node) {
|
|
123
|
+
return node.nodeType === 1 ? node : node.parentElement;
|
|
124
|
+
}
|
|
125
|
+
function textAnchor(target, options) {
|
|
126
|
+
const range = rangeOf(target);
|
|
127
|
+
const exact = range?.toString() ?? "";
|
|
128
|
+
const container = range && elementOfNode(range.commonAncestorContainer);
|
|
129
|
+
if (!range || !container || exact === "") throw commentsClientDiagnostics.COMMENTS_R0001({ target: "an empty selection" });
|
|
130
|
+
const element = ignoreUp(container, options) ?? container;
|
|
131
|
+
if (options.root && (!options.root.contains(range.startContainer) || !options.root.contains(range.endContainer) || !options.root.contains(element))) throw commentsClientDiagnostics.COMMENTS_R0001({ target: "a selection outside the configured root" });
|
|
132
|
+
const start = textOffset(element, range.startContainer, range.startOffset);
|
|
133
|
+
const text = textNodes(element).map((node) => node.data).join("");
|
|
134
|
+
const context = options.context ?? defaultContext;
|
|
135
|
+
return {
|
|
136
|
+
kind: "text",
|
|
137
|
+
selector: selectorFor(element, options.root ?? defaultRoot(element)),
|
|
138
|
+
exact,
|
|
139
|
+
prefix: text.slice(Math.max(0, start - context), start),
|
|
140
|
+
suffix: text.slice(start + exact.length, start + exact.length + context)
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function ignoreUp(element, options) {
|
|
144
|
+
let node = element;
|
|
145
|
+
while (node && options.ignore?.(node)) node = node.parentElement;
|
|
146
|
+
return node ?? void 0;
|
|
147
|
+
}
|
|
148
|
+
function isImage(element) {
|
|
149
|
+
return element.localName === "img";
|
|
150
|
+
}
|
|
151
|
+
function boxAnchor(element, options, point) {
|
|
152
|
+
if (options.root && !options.root.contains(element)) throw commentsClientDiagnostics.COMMENTS_R0001({ target: "an element outside the configured root" });
|
|
153
|
+
const box = boxOf(element);
|
|
154
|
+
const selector = selectorFor(element, options.root ?? defaultRoot(element));
|
|
155
|
+
const kind = isImage(element) ? "image" : void 0;
|
|
156
|
+
if (options.area) {
|
|
157
|
+
const x = thousandths(options.area.left - box.left, box.width);
|
|
158
|
+
const y = thousandths(options.area.top - box.top, box.height);
|
|
159
|
+
const area = {
|
|
160
|
+
selector,
|
|
161
|
+
x,
|
|
162
|
+
y,
|
|
163
|
+
width: Math.max(0, thousandths(options.area.left + options.area.width - box.left, box.width) - x),
|
|
164
|
+
height: Math.max(0, thousandths(options.area.top + options.area.height - box.top, box.height) - y)
|
|
165
|
+
};
|
|
166
|
+
if (area.width > 0 && area.height > 0) return kind ? {
|
|
167
|
+
kind,
|
|
168
|
+
...area
|
|
169
|
+
} : {
|
|
170
|
+
kind: "area",
|
|
171
|
+
...area
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const x = point ? thousandths(point.x - box.left, box.width) : 500;
|
|
175
|
+
const y = point ? thousandths(point.y - box.top, box.height) : 500;
|
|
176
|
+
return kind ? {
|
|
177
|
+
kind,
|
|
178
|
+
selector,
|
|
179
|
+
x,
|
|
180
|
+
y
|
|
181
|
+
} : {
|
|
182
|
+
kind: "element",
|
|
183
|
+
selector,
|
|
184
|
+
x,
|
|
185
|
+
y
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Describes a spot of the page so it can be found again: an element with an offset, a text quote, an area,
|
|
190
|
+
* a point on an image, or the whole page. Selectors and offsets survive layout changes, because offsets are
|
|
191
|
+
* thousandths of the element box.
|
|
192
|
+
*/
|
|
193
|
+
function createAnchor(target, options = {}) {
|
|
194
|
+
if (target === "page") return { kind: "page" };
|
|
195
|
+
if ("nodeType" in target) return boxAnchor(target, options, options.point);
|
|
196
|
+
if ("rangeCount" in target || "startContainer" in target) return textAnchor(target, options);
|
|
197
|
+
const element = pointTarget(target, options);
|
|
198
|
+
if (!element) throw commentsClientDiagnostics.COMMENTS_R0001({ target: `the point ${Math.round(target.x)}, ${Math.round(target.y)}` });
|
|
199
|
+
return boxAnchor(element, options, target);
|
|
200
|
+
}
|
|
201
|
+
const minimumContext = 4;
|
|
202
|
+
function findQuote(text, anchor) {
|
|
203
|
+
const matches = [];
|
|
204
|
+
for (let at = text.indexOf(anchor.exact); at !== -1; at = text.indexOf(anchor.exact, at + 1)) {
|
|
205
|
+
const before = text.slice(0, at);
|
|
206
|
+
const after = text.slice(at + anchor.exact.length);
|
|
207
|
+
let score = 0;
|
|
208
|
+
while (score < anchor.prefix.length && before.at(-1 - score) === anchor.prefix.at(-1 - score)) score++;
|
|
209
|
+
let tail = 0;
|
|
210
|
+
while (tail < anchor.suffix.length && after[tail] === anchor.suffix[tail]) tail++;
|
|
211
|
+
matches.push({
|
|
212
|
+
start: at,
|
|
213
|
+
score: score + tail,
|
|
214
|
+
ambiguous: false
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
if (matches.length === 1) return matches[0];
|
|
218
|
+
const needed = Math.min(minimumContext, anchor.prefix.length + anchor.suffix.length);
|
|
219
|
+
const best = matches.reduce((top, match) => !top || match.score > top.score ? match : top, void 0);
|
|
220
|
+
if (!best || needed === 0 || best.score < needed) return void 0;
|
|
221
|
+
return {
|
|
222
|
+
...best,
|
|
223
|
+
ambiguous: matches.filter((match) => match.score === best.score).length > 1
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function boundary(nodes, offset, end) {
|
|
227
|
+
let total = 0;
|
|
228
|
+
for (const node of nodes) {
|
|
229
|
+
const next = total + node.data.length;
|
|
230
|
+
if (end ? offset > total && offset <= next : offset >= total && offset < next) return {
|
|
231
|
+
node,
|
|
232
|
+
offset: offset - total
|
|
233
|
+
};
|
|
234
|
+
total = next;
|
|
235
|
+
}
|
|
236
|
+
const last = nodes.at(-1);
|
|
237
|
+
return last && offset === total ? {
|
|
238
|
+
node: last,
|
|
239
|
+
offset: last.data.length
|
|
240
|
+
} : void 0;
|
|
241
|
+
}
|
|
242
|
+
function visible(rect) {
|
|
243
|
+
return rect.width > 0 || rect.height > 0;
|
|
244
|
+
}
|
|
245
|
+
/** The nearest block around an element, such as the paragraph around a `<strong>`: its right edge is the pin's gutter. */
|
|
246
|
+
function blockOf(element) {
|
|
247
|
+
const view = ownerDocument(element)?.defaultView;
|
|
248
|
+
for (let node = element; node; node = node.parentElement) {
|
|
249
|
+
const display = view?.getComputedStyle(node).display ?? "block";
|
|
250
|
+
if (!display.startsWith("inline") && display !== "contents") return node;
|
|
251
|
+
}
|
|
252
|
+
return element;
|
|
253
|
+
}
|
|
254
|
+
/** The space between a text block and its 28px pin, as in Drop. */
|
|
255
|
+
const PIN_GUTTER = 8;
|
|
256
|
+
/** The room that a pin takes right of its spot at the screen's edge: the pin and 2px. */
|
|
257
|
+
const PIN_ROOM = 30;
|
|
258
|
+
function resolveText(anchor, element) {
|
|
259
|
+
const nodes = textNodes(element);
|
|
260
|
+
const match = findQuote(nodes.map((node) => node.data).join(""), anchor);
|
|
261
|
+
if (!match) return void 0;
|
|
262
|
+
const start = match.start;
|
|
263
|
+
const from = boundary(nodes, start, false);
|
|
264
|
+
const to = boundary(nodes, start + anchor.exact.length, true);
|
|
265
|
+
const own = boxOf(element);
|
|
266
|
+
let rect = own;
|
|
267
|
+
let last = own;
|
|
268
|
+
if (from && to) {
|
|
269
|
+
const range = (ownerDocument(element) ?? document).createRange();
|
|
270
|
+
range.setStart(from.node, from.offset);
|
|
271
|
+
range.setEnd(to.node, to.offset);
|
|
272
|
+
if (typeof range.getBoundingClientRect === "function") {
|
|
273
|
+
const { left, top, width, height } = range.getBoundingClientRect();
|
|
274
|
+
if (visible({
|
|
275
|
+
left,
|
|
276
|
+
top,
|
|
277
|
+
width,
|
|
278
|
+
height
|
|
279
|
+
})) rect = {
|
|
280
|
+
left,
|
|
281
|
+
top,
|
|
282
|
+
width,
|
|
283
|
+
height
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
const line = (typeof range.getClientRects === "function" ? [...range.getClientRects()].filter(visible) : []).at(-1);
|
|
287
|
+
last = line ? {
|
|
288
|
+
left: line.left,
|
|
289
|
+
top: line.top,
|
|
290
|
+
width: line.width,
|
|
291
|
+
height: line.height
|
|
292
|
+
} : rect;
|
|
293
|
+
}
|
|
294
|
+
if (!visible(rect)) return void 0;
|
|
295
|
+
const view = ownerDocument(element)?.defaultView;
|
|
296
|
+
const block = boxOf(blockOf(element));
|
|
297
|
+
const x = Math.min(Math.max(block.left + block.width, last.left + last.width) + PIN_GUTTER, (view?.innerWidth ?? Infinity) - PIN_ROOM);
|
|
298
|
+
const y = last.top + last.height / 2;
|
|
299
|
+
return {
|
|
300
|
+
position: {
|
|
301
|
+
clipped: isClipped(element, last.left + Math.max(0, last.width - 1), y),
|
|
302
|
+
detached: false,
|
|
303
|
+
kind: "text",
|
|
304
|
+
x,
|
|
305
|
+
y,
|
|
306
|
+
width: rect.width,
|
|
307
|
+
height: rect.height,
|
|
308
|
+
element
|
|
309
|
+
},
|
|
310
|
+
score: match.score,
|
|
311
|
+
ambiguous: match.ambiguous
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
function composedParent(element) {
|
|
315
|
+
if (element.parentElement) return element.parentElement;
|
|
316
|
+
const root = element.getRootNode();
|
|
317
|
+
return root instanceof ShadowRoot ? root.host : null;
|
|
318
|
+
}
|
|
319
|
+
function isClipped(element, x, y) {
|
|
320
|
+
const view = element.ownerDocument.defaultView;
|
|
321
|
+
if (!view) return false;
|
|
322
|
+
for (let node = composedParent(element); node; node = composedParent(node)) {
|
|
323
|
+
const { overflow, overflowX, overflowY } = view.getComputedStyle(node);
|
|
324
|
+
if (![
|
|
325
|
+
overflow,
|
|
326
|
+
overflowX,
|
|
327
|
+
overflowY
|
|
328
|
+
].some((value) => value !== "" && value !== "visible")) continue;
|
|
329
|
+
const box = boxOf(node);
|
|
330
|
+
if (!visible(box)) continue;
|
|
331
|
+
if (x < box.left - 1 || x > box.left + box.width + 1 || y < box.top - 1 || y > box.top + box.height + 1) return true;
|
|
332
|
+
}
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* The range of the quote of a text anchor on the page, for a highlight. It finds the quote as `resolveAnchor()` does,
|
|
337
|
+
* and returns `undefined` when that would detach the anchor.
|
|
338
|
+
*/
|
|
339
|
+
function anchorRange(anchor, root = document) {
|
|
340
|
+
let best;
|
|
341
|
+
for (const element of lookup(root, anchor.selector)) {
|
|
342
|
+
const nodes = textNodes(element);
|
|
343
|
+
const match = findQuote(nodes.map((node) => node.data).join(""), anchor);
|
|
344
|
+
const from = match && boundary(nodes, match.start, false);
|
|
345
|
+
const to = match && boundary(nodes, match.start + anchor.exact.length, true);
|
|
346
|
+
if (!match || !from || !to) continue;
|
|
347
|
+
if (best && match.score === best.score) {
|
|
348
|
+
best.ambiguous = true;
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
if (best && match.score < best.score) continue;
|
|
352
|
+
const range = (ownerDocument(element) ?? document).createRange();
|
|
353
|
+
range.setStart(from.node, from.offset);
|
|
354
|
+
range.setEnd(to.node, to.offset);
|
|
355
|
+
best = {
|
|
356
|
+
range,
|
|
357
|
+
score: match.score,
|
|
358
|
+
ambiguous: match.ambiguous
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
return best && !best.ambiguous ? best.range : void 0;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Finds an anchor on the page again. It returns the pin position in client coordinates, or `{ detached: true }`
|
|
365
|
+
* when the element is gone, hidden, ambiguous, or no longer holds the quote. A selector that names more than one
|
|
366
|
+
* element is ambiguous, so a pin never lands on the wrong one. A text anchor tries every match and takes the one
|
|
367
|
+
* that holds the quote best.
|
|
368
|
+
*/
|
|
369
|
+
function resolveAnchor(anchor, root = document) {
|
|
370
|
+
if (anchor.kind === "page") return {
|
|
371
|
+
clipped: false,
|
|
372
|
+
detached: false,
|
|
373
|
+
kind: "page",
|
|
374
|
+
x: 0,
|
|
375
|
+
y: 0,
|
|
376
|
+
width: 0,
|
|
377
|
+
height: 0
|
|
378
|
+
};
|
|
379
|
+
const found = lookup(root, anchor.selector);
|
|
380
|
+
if (anchor.kind === "text") {
|
|
381
|
+
let best;
|
|
382
|
+
for (const element of found) {
|
|
383
|
+
const resolution = resolveText(anchor, element);
|
|
384
|
+
if (resolution && (!best || resolution.score > best.score)) best = resolution;
|
|
385
|
+
else if (resolution && best && resolution.score === best.score) best.ambiguous = true;
|
|
386
|
+
}
|
|
387
|
+
return best && !best.ambiguous ? best.position : { detached: true };
|
|
388
|
+
}
|
|
389
|
+
const element = found.length === 1 ? found[0] : void 0;
|
|
390
|
+
if (!element) return { detached: true };
|
|
391
|
+
const box = boxOf(element);
|
|
392
|
+
if (!visible(box)) return { detached: true };
|
|
393
|
+
const width = "width" in anchor && anchor.width ? anchor.width / 1e3 * box.width : 0;
|
|
394
|
+
const height = "height" in anchor && anchor.height ? anchor.height / 1e3 * box.height : 0;
|
|
395
|
+
const x = box.left + anchor.x / 1e3 * box.width;
|
|
396
|
+
const y = box.top + anchor.y / 1e3 * box.height;
|
|
397
|
+
return {
|
|
398
|
+
clipped: isClipped(element, x, y),
|
|
399
|
+
detached: false,
|
|
400
|
+
kind: anchor.kind,
|
|
401
|
+
x,
|
|
402
|
+
y,
|
|
403
|
+
width,
|
|
404
|
+
height,
|
|
405
|
+
element
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
//#endregion
|
|
409
|
+
//#region src/rest-transport.ts
|
|
410
|
+
/** The URL of an attachment key below the Comments API, such as `/api/_vitehub/comments/attachments/comments/<id>/shot.png`. */
|
|
411
|
+
function commentAttachmentUrl(key, base = "") {
|
|
412
|
+
return `${base.replace(/\/+$/, "")}${commentsRoute}/attachments/${key.split("/").map((part) => encodeURIComponent(part)).join("/")}`;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Requests to the Comments API at `<base>/api/_vitehub/comments`, with the guest headers and the session cookie. A
|
|
416
|
+
* failure throws a diagnostic with the `code`, `why`, and `fix` of the error object of the answer, and its `status`.
|
|
417
|
+
*/
|
|
418
|
+
function createCommentsRestRequest(options = {}) {
|
|
419
|
+
const route = options.route ?? `${(options.base ?? "").replace(/\/+$/, "")}/api/_vitehub/comments`;
|
|
420
|
+
return async (path, init = {}) => {
|
|
421
|
+
const send = options.fetch ?? globalThis.fetch;
|
|
422
|
+
const headers = new Headers(typeof options.headers === "function" ? await options.headers() : options.headers);
|
|
423
|
+
for (const [name, value] of new Headers(init.headers)) headers.set(name, value);
|
|
424
|
+
if (init.body !== void 0 && !headers.has("content-type")) headers.set("content-type", "application/json");
|
|
425
|
+
const guest = typeof options.guest === "function" ? options.guest() : options.guest;
|
|
426
|
+
if (guest) {
|
|
427
|
+
headers.set("x-vitehub-comments-guest-id", guest.id);
|
|
428
|
+
headers.set("x-vitehub-comments-guest-name", encodeURIComponent(guest.name));
|
|
429
|
+
}
|
|
430
|
+
let response;
|
|
431
|
+
try {
|
|
432
|
+
response = await send(`${route}${path}`, {
|
|
433
|
+
...init,
|
|
434
|
+
headers,
|
|
435
|
+
credentials: options.credentials ?? "same-origin",
|
|
436
|
+
cache: "no-store"
|
|
437
|
+
});
|
|
438
|
+
} catch (cause) {
|
|
439
|
+
throw commentsUnansweredError(0, cause);
|
|
440
|
+
}
|
|
441
|
+
const value = await response.json().catch(() => void 0);
|
|
442
|
+
if (isRecordOrFunction(value) && isRecordOrFunction(value.error)) {
|
|
443
|
+
const { code, why, fix } = value.error;
|
|
444
|
+
if (typeof code !== "string" || typeof why !== "string") throw commentsUnansweredError(response.status);
|
|
445
|
+
throw commentsAnswerError({
|
|
446
|
+
code,
|
|
447
|
+
why,
|
|
448
|
+
...typeof fix === "string" ? { fix } : {},
|
|
449
|
+
status: response.status
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
if (isRecordOrFunction(value) && "data" in value) {
|
|
453
|
+
const answer = {
|
|
454
|
+
data: value.data,
|
|
455
|
+
people: Array.isArray(value.people) ? value.people : [],
|
|
456
|
+
...isRecordOrFunction(value.thread) ? { thread: value.thread } : {}
|
|
457
|
+
};
|
|
458
|
+
options.onAnswer?.(answer);
|
|
459
|
+
return answer;
|
|
460
|
+
}
|
|
461
|
+
throw commentsUnansweredError(response.status);
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
function listSearch(query) {
|
|
465
|
+
const params = new URLSearchParams();
|
|
466
|
+
if (query.page !== void 0) params.set("page", query.page);
|
|
467
|
+
if (query.subject) {
|
|
468
|
+
params.set("subjectKind", query.subject.kind);
|
|
469
|
+
params.set("subjectId", query.subject.id);
|
|
470
|
+
} else if (query.subjectKind) params.set("subjectKind", query.subjectKind);
|
|
471
|
+
if (query.status) params.set("status", query.status);
|
|
472
|
+
if (query.mine !== void 0) params.set("mine", String(query.mine));
|
|
473
|
+
if (query.mentions !== void 0) params.set("mentions", String(query.mentions));
|
|
474
|
+
if (query.revision) params.set("revision", String(query.revision.lte));
|
|
475
|
+
if (query.limit !== void 0) params.set("limit", String(query.limit));
|
|
476
|
+
if (query.before) {
|
|
477
|
+
params.set("beforeId", query.before.id);
|
|
478
|
+
params.set("beforeUpdatedAt", String(query.before.updatedAt));
|
|
479
|
+
}
|
|
480
|
+
return params;
|
|
481
|
+
}
|
|
482
|
+
const thread = (id) => `/threads/${encodeURIComponent(id)}`;
|
|
483
|
+
const post = (body) => ({
|
|
484
|
+
method: "POST",
|
|
485
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) }
|
|
486
|
+
});
|
|
487
|
+
/**
|
|
488
|
+
* A transport over the Comments API at `<base>/api/_vitehub/comments`: the routes that the overlay uses. The server
|
|
489
|
+
* runs the `authorize` policy of `server/comments.ts` for the user of the request: the app session, or a guest that
|
|
490
|
+
* `guest` names. Guests and share-link readers can use it. It has no
|
|
491
|
+
* `subscribe`: the client refreshes when the window gets focus.
|
|
492
|
+
*/
|
|
493
|
+
function createRestTransport(options = {}) {
|
|
494
|
+
const request = createCommentsRestRequest(options);
|
|
495
|
+
const base = (options.base ?? "").replace(/\/+$/, "");
|
|
496
|
+
return {
|
|
497
|
+
async list(query) {
|
|
498
|
+
return (await request(`/threads?${listSearch(query)}`)).data;
|
|
499
|
+
},
|
|
500
|
+
async get(input) {
|
|
501
|
+
const path = "id" in input ? thread(input.id) : `/scopes/${encodeURIComponent(input.scope)}/threads/${input.n}`;
|
|
502
|
+
return (await request(path)).data;
|
|
503
|
+
},
|
|
504
|
+
async create(input) {
|
|
505
|
+
return (await request("/threads", post(input))).data;
|
|
506
|
+
},
|
|
507
|
+
async reply({ threadId, ...input }) {
|
|
508
|
+
const answer = await request(`${thread(threadId)}/comments`, post(input));
|
|
509
|
+
if (!answer.thread) throw commentsUnansweredError(200);
|
|
510
|
+
return answer.thread;
|
|
511
|
+
},
|
|
512
|
+
async edit({ threadId, messageId, ...input }) {
|
|
513
|
+
return (await request(`${thread(threadId)}/comments/${encodeURIComponent(messageId)}`, {
|
|
514
|
+
method: "PATCH",
|
|
515
|
+
body: JSON.stringify(input)
|
|
516
|
+
})).data;
|
|
517
|
+
},
|
|
518
|
+
async delete({ threadId, messageId }) {
|
|
519
|
+
const path = messageId === void 0 ? thread(threadId) : `${thread(threadId)}/comments/${encodeURIComponent(messageId)}`;
|
|
520
|
+
return (await request(path, { method: "DELETE" })).data;
|
|
521
|
+
},
|
|
522
|
+
async resolve({ threadId }) {
|
|
523
|
+
return (await request(`${thread(threadId)}/resolve`, post())).data;
|
|
524
|
+
},
|
|
525
|
+
async reopen({ threadId }) {
|
|
526
|
+
return (await request(`${thread(threadId)}/reopen`, post())).data;
|
|
527
|
+
},
|
|
528
|
+
async users({ query, limit, threadId, subject }) {
|
|
529
|
+
const params = new URLSearchParams({ q: query });
|
|
530
|
+
if (limit !== void 0) params.set("limit", String(limit));
|
|
531
|
+
if (threadId !== void 0) params.set("thread", threadId);
|
|
532
|
+
else if (subject) {
|
|
533
|
+
params.set("subjectKind", subject.kind);
|
|
534
|
+
params.set("subjectId", subject.id);
|
|
535
|
+
}
|
|
536
|
+
return (await request(`/people?${params}`)).data;
|
|
537
|
+
},
|
|
538
|
+
async upload(input) {
|
|
539
|
+
return (await request("/attachments", post(input))).data;
|
|
540
|
+
},
|
|
541
|
+
attachmentUrl: (key) => commentAttachmentUrl(key, base)
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
//#endregion
|
|
545
|
+
//#region src/client.ts
|
|
546
|
+
/** A cache and a change signal over a transport. It has no framework in it. */
|
|
547
|
+
function createCommentsClient(options) {
|
|
548
|
+
const { transport } = options;
|
|
549
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
550
|
+
const byId = /* @__PURE__ */ new Map();
|
|
551
|
+
let listed = /* @__PURE__ */ new Set();
|
|
552
|
+
let changed = /* @__PURE__ */ new Set();
|
|
553
|
+
let removed = /* @__PURE__ */ new Set();
|
|
554
|
+
let threads = [];
|
|
555
|
+
let status = "idle";
|
|
556
|
+
let error;
|
|
557
|
+
let query = options.query ?? {};
|
|
558
|
+
let generation = 0;
|
|
559
|
+
const transportOwnsMembership = () => !options.user && Boolean(query.mine || query.mentions);
|
|
560
|
+
const notify = () => {
|
|
561
|
+
for (const listener of listeners) listener();
|
|
562
|
+
};
|
|
563
|
+
const matchesQuery = (thread) => {
|
|
564
|
+
const user = options.user;
|
|
565
|
+
if (query.subject && (thread.subject.kind !== query.subject.kind || thread.subject.id !== query.subject.id)) return false;
|
|
566
|
+
if (query.subjectKind !== void 0 && thread.subject.kind !== query.subjectKind) return false;
|
|
567
|
+
if (query.page !== void 0 && (thread.subject.kind !== "page" || thread.subject.id !== query.page)) return false;
|
|
568
|
+
if (query.revision && (thread.revision === void 0 || thread.revision > query.revision.lte)) return false;
|
|
569
|
+
if (query.status && thread.status !== query.status) return false;
|
|
570
|
+
if (user && query.mine && thread.author.id !== user.id) return false;
|
|
571
|
+
if (user && query.mentions && !thread.messages.some((message) => message.mentions.includes(user.id))) return false;
|
|
572
|
+
if (transportOwnsMembership() && !listed.has(thread.id)) return false;
|
|
573
|
+
return true;
|
|
574
|
+
};
|
|
575
|
+
const publish = () => {
|
|
576
|
+
threads = [...byId.values()].filter(matchesQuery).sort((a, b) => a.n - b.n);
|
|
577
|
+
notify();
|
|
578
|
+
};
|
|
579
|
+
const upsert = (thread) => {
|
|
580
|
+
const stored = byId.get(thread.id);
|
|
581
|
+
if (removed.has(thread.id) || stored && stored.updatedAt > thread.updatedAt) return stored ?? thread;
|
|
582
|
+
byId.set(thread.id, thread);
|
|
583
|
+
changed.add(thread.id);
|
|
584
|
+
publish();
|
|
585
|
+
if (transportOwnsMembership()) refreshQuietly();
|
|
586
|
+
return thread;
|
|
587
|
+
};
|
|
588
|
+
const drop = (threadId) => {
|
|
589
|
+
removed.add(threadId);
|
|
590
|
+
changed.delete(threadId);
|
|
591
|
+
if (byId.delete(threadId)) publish();
|
|
592
|
+
};
|
|
593
|
+
const mutations = /* @__PURE__ */ new Map();
|
|
594
|
+
const mutate = async (threadId, operation) => {
|
|
595
|
+
const previous = mutations.get(threadId);
|
|
596
|
+
const request = (async () => {
|
|
597
|
+
await previous?.catch(() => void 0);
|
|
598
|
+
return operation();
|
|
599
|
+
})();
|
|
600
|
+
mutations.set(threadId, request);
|
|
601
|
+
try {
|
|
602
|
+
return await request;
|
|
603
|
+
} finally {
|
|
604
|
+
if (mutations.get(threadId) === request) mutations.delete(threadId);
|
|
605
|
+
}
|
|
606
|
+
};
|
|
607
|
+
const refresh = async () => {
|
|
608
|
+
const current = ++generation;
|
|
609
|
+
changed = /* @__PURE__ */ new Set();
|
|
610
|
+
removed = /* @__PURE__ */ new Set();
|
|
611
|
+
status = "loading";
|
|
612
|
+
notify();
|
|
613
|
+
try {
|
|
614
|
+
const requested = { ...query };
|
|
615
|
+
const pageSize = requested.limit ?? 200;
|
|
616
|
+
let list = [];
|
|
617
|
+
let previousSnapshot;
|
|
618
|
+
for (let scan = 0;; scan++) {
|
|
619
|
+
list = [];
|
|
620
|
+
let before = requested.before;
|
|
621
|
+
let pages = 0;
|
|
622
|
+
while (true) {
|
|
623
|
+
const page = await transport.list({
|
|
624
|
+
...requested,
|
|
625
|
+
limit: pageSize,
|
|
626
|
+
...before ? { before } : {}
|
|
627
|
+
});
|
|
628
|
+
if (current !== generation) return threads;
|
|
629
|
+
pages++;
|
|
630
|
+
list.push(...page);
|
|
631
|
+
if (requested.limit !== void 0 || page.length < pageSize) break;
|
|
632
|
+
const last = page.at(-1);
|
|
633
|
+
before = {
|
|
634
|
+
id: last.id,
|
|
635
|
+
updatedAt: last.updatedAt
|
|
636
|
+
};
|
|
637
|
+
}
|
|
638
|
+
if (requested.limit !== void 0 || pages === 1 && previousSnapshot === void 0) break;
|
|
639
|
+
const snapshot = JSON.stringify(list);
|
|
640
|
+
if (snapshot === previousSnapshot) break;
|
|
641
|
+
if (scan === 2) throw commentsClientDiagnostics.COMMENTS_R0015();
|
|
642
|
+
previousSnapshot = snapshot;
|
|
643
|
+
}
|
|
644
|
+
if (current !== generation) return threads;
|
|
645
|
+
const next = /* @__PURE__ */ new Map();
|
|
646
|
+
for (const thread of list) {
|
|
647
|
+
const local = byId.get(thread.id);
|
|
648
|
+
if (removed.has(thread.id)) continue;
|
|
649
|
+
next.set(thread.id, local && (local.updatedAt > thread.updatedAt || changed.has(thread.id) && local.updatedAt === thread.updatedAt) ? local : thread);
|
|
650
|
+
}
|
|
651
|
+
for (const id of changed) {
|
|
652
|
+
const local = byId.get(id);
|
|
653
|
+
if (local && !next.has(id)) next.set(id, local);
|
|
654
|
+
}
|
|
655
|
+
byId.clear();
|
|
656
|
+
for (const [id, thread] of next) byId.set(id, thread);
|
|
657
|
+
listed = new Set(list.map((thread) => thread.id));
|
|
658
|
+
status = "ready";
|
|
659
|
+
error = void 0;
|
|
660
|
+
publish();
|
|
661
|
+
} catch (cause) {
|
|
662
|
+
if (current === generation) {
|
|
663
|
+
status = "error";
|
|
664
|
+
error = cause;
|
|
665
|
+
notify();
|
|
666
|
+
}
|
|
667
|
+
throw cause;
|
|
668
|
+
}
|
|
669
|
+
return threads;
|
|
670
|
+
};
|
|
671
|
+
const refreshQuietly = () => {
|
|
672
|
+
refresh().catch(() => void 0);
|
|
673
|
+
};
|
|
674
|
+
const apply = (event) => {
|
|
675
|
+
if (event.type === "thread") {
|
|
676
|
+
if (!options.user && (query.mine || query.mentions)) return refreshQuietly();
|
|
677
|
+
if (byId.get(event.thread.id)?.updatedAt === event.thread.updatedAt) refreshQuietly();
|
|
678
|
+
else upsert(event.thread);
|
|
679
|
+
} else if (event.type === "deleted") drop(event.threadId);
|
|
680
|
+
else refreshQuietly();
|
|
681
|
+
};
|
|
682
|
+
return {
|
|
683
|
+
transport,
|
|
684
|
+
user: options.user,
|
|
685
|
+
get threads() {
|
|
686
|
+
return threads;
|
|
687
|
+
},
|
|
688
|
+
get status() {
|
|
689
|
+
return status;
|
|
690
|
+
},
|
|
691
|
+
get error() {
|
|
692
|
+
return error;
|
|
693
|
+
},
|
|
694
|
+
get query() {
|
|
695
|
+
return query;
|
|
696
|
+
},
|
|
697
|
+
set query(next) {
|
|
698
|
+
query = next;
|
|
699
|
+
generation++;
|
|
700
|
+
listed.clear();
|
|
701
|
+
publish();
|
|
702
|
+
},
|
|
703
|
+
refresh,
|
|
704
|
+
get: (input) => transport.get(input),
|
|
705
|
+
create: async (input) => upsert(await transport.create(input)),
|
|
706
|
+
reply: (input) => mutate(input.threadId, async () => upsert(await transport.reply(input))),
|
|
707
|
+
edit: (input) => mutate(input.threadId, async () => upsert(await transport.edit(input))),
|
|
708
|
+
delete: (input) => mutate(input.threadId, async () => {
|
|
709
|
+
const next = await transport.delete(input);
|
|
710
|
+
if (next) upsert(next);
|
|
711
|
+
else drop(input.threadId);
|
|
712
|
+
return next;
|
|
713
|
+
}),
|
|
714
|
+
resolve: (input) => mutate(input.threadId, async () => upsert(await transport.resolve(input))),
|
|
715
|
+
reopen: (input) => mutate(input.threadId, async () => upsert(await transport.reopen(input))),
|
|
716
|
+
users: (input) => transport.users(input),
|
|
717
|
+
onChange(listener) {
|
|
718
|
+
listeners.add(listener);
|
|
719
|
+
return () => {
|
|
720
|
+
listeners.delete(listener);
|
|
721
|
+
};
|
|
722
|
+
},
|
|
723
|
+
start() {
|
|
724
|
+
const stops = [];
|
|
725
|
+
if (transport.subscribe) stops.push(transport.subscribe(apply));
|
|
726
|
+
else if (options.refreshOnFocus !== false && typeof window !== "undefined") {
|
|
727
|
+
window.addEventListener("focus", refreshQuietly);
|
|
728
|
+
stops.push(() => window.removeEventListener("focus", refreshQuietly));
|
|
729
|
+
}
|
|
730
|
+
refreshQuietly();
|
|
731
|
+
return () => {
|
|
732
|
+
for (const stop of stops.splice(0)) stop();
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
//#endregion
|
|
738
|
+
export { anchorRange, commentAttachmentUrl, commentsErrorDiagnostics, createAnchor, createCommentsClient, createCommentsRestRequest, createRestTransport, foldName, resolveAnchor, selectorFor };
|