@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/dist/client.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { C as TextAnchor, S as PageAnchor, _ as CommentUser, a as CommentDeleteInput, b as ElementAnchor, c as CommentGetInput, d as CommentMessage, f as CommentReplyInput, g as CommentThread, h as CommentSubject, i as CommentCreateInput, l as CommentListCursor, m as CommentStatusInput, n as CommentActor, o as CommentEditInput, p as CommentStatus, r as CommentAnchor, s as CommentEvent, t as AreaAnchor, u as CommentListQuery, v as CommentUsersInput, x as ImageAnchor, y as CommentsTransport } from "./types-Cj8K7Vx7.js";
|
|
2
|
+
import { t as commentsErrorDiagnostics } from "./errors-BAQYcy3_.js";
|
|
3
|
+
//#region src/client-anchor.d.ts
|
|
4
|
+
/** A point in client (viewport) coordinates, such as `event.clientX` and `event.clientY`. */
|
|
5
|
+
interface AnchorPoint {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
}
|
|
9
|
+
interface AnchorRect {
|
|
10
|
+
left: number;
|
|
11
|
+
top: number;
|
|
12
|
+
width: number;
|
|
13
|
+
height: number;
|
|
14
|
+
}
|
|
15
|
+
type AnchorTarget = Element | Range | Selection | AnchorPoint | "page";
|
|
16
|
+
interface CreateAnchorOptions {
|
|
17
|
+
/** Where selectors are looked up. Defaults to the document of the target. */
|
|
18
|
+
root?: ParentNode;
|
|
19
|
+
/** For an element target: where the click landed, in client coordinates. Defaults to the middle. */
|
|
20
|
+
point?: AnchorPoint;
|
|
21
|
+
/** For an element target: a rectangle in client coordinates. It makes an `area` anchor, or an `image` area on an image. */
|
|
22
|
+
area?: AnchorRect;
|
|
23
|
+
/** Skip elements that are not the page, such as the comment layer. */
|
|
24
|
+
ignore?: (element: Element) => boolean;
|
|
25
|
+
/** How many characters of text to keep before and after a quote. Default 32. */
|
|
26
|
+
context?: number;
|
|
27
|
+
}
|
|
28
|
+
interface ResolvedAnchor {
|
|
29
|
+
detached: false;
|
|
30
|
+
/** A scroll container or an `overflow: hidden` ancestor cuts the spot off. Hide the pin. */
|
|
31
|
+
clipped: boolean;
|
|
32
|
+
kind: CommentAnchor["kind"];
|
|
33
|
+
/** The pin position in client coordinates. For a `page` anchor it is `0, 0`. */
|
|
34
|
+
x: number;
|
|
35
|
+
y: number;
|
|
36
|
+
/** The size of an area or a quote in pixels. `0` for a point. */
|
|
37
|
+
width: number;
|
|
38
|
+
height: number;
|
|
39
|
+
/** Missing for a `page` anchor. */
|
|
40
|
+
element?: Element;
|
|
41
|
+
}
|
|
42
|
+
interface DetachedAnchor {
|
|
43
|
+
detached: true;
|
|
44
|
+
}
|
|
45
|
+
type AnchorPosition = ResolvedAnchor | DetachedAnchor;
|
|
46
|
+
/**
|
|
47
|
+
* The shortest selector that names the element: a stable attribute, else a path from the nearest stable
|
|
48
|
+
* ancestor or from the top of the page, cut as soon as it is unique.
|
|
49
|
+
*/
|
|
50
|
+
export declare function selectorFor(element: Element, root?: ParentNode): string;
|
|
51
|
+
/**
|
|
52
|
+
* Describes a spot of the page so it can be found again: an element with an offset, a text quote, an area,
|
|
53
|
+
* a point on an image, or the whole page. Selectors and offsets survive layout changes, because offsets are
|
|
54
|
+
* thousandths of the element box.
|
|
55
|
+
*/
|
|
56
|
+
export declare function createAnchor(target: AnchorTarget, options?: CreateAnchorOptions): CommentAnchor;
|
|
57
|
+
/**
|
|
58
|
+
* Finds an anchor on the page again. It returns the pin position in client coordinates, or `{ detached: true }`
|
|
59
|
+
* when the element is gone, hidden, ambiguous, or no longer holds the quote. A selector that names more than one
|
|
60
|
+
* element is ambiguous, so a pin never lands on the wrong one. A text anchor tries every match and takes the one
|
|
61
|
+
* that holds the quote best.
|
|
62
|
+
*/
|
|
63
|
+
export declare function resolveAnchor(anchor: CommentAnchor, root?: ParentNode): AnchorPosition;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/client.d.ts
|
|
66
|
+
export type CommentsClientStatus = "idle" | "loading" | "ready" | "error";
|
|
67
|
+
export interface CommentsClientOptions {
|
|
68
|
+
transport: CommentsTransport;
|
|
69
|
+
/** The signed-in user. The UI uses it for the "mine" filter and to know which messages the user may edit. */
|
|
70
|
+
user?: CommentUser;
|
|
71
|
+
/** What `refresh()` lists. For example `{ page: "/pricing" }`. Without an explicit limit, loads all pages. */
|
|
72
|
+
query?: CommentListQuery;
|
|
73
|
+
/** Refresh when the window gets focus and the transport cannot push updates. Default `true`. */
|
|
74
|
+
refreshOnFocus?: boolean;
|
|
75
|
+
}
|
|
76
|
+
export interface CommentsClient {
|
|
77
|
+
readonly transport: CommentsTransport;
|
|
78
|
+
readonly user: CommentUser | undefined;
|
|
79
|
+
/** The threads that the last `refresh()` and every change since then produced, ordered by `n`. A new array after each change. */
|
|
80
|
+
readonly threads: readonly CommentThread[];
|
|
81
|
+
readonly status: CommentsClientStatus;
|
|
82
|
+
readonly error: unknown;
|
|
83
|
+
query: CommentListQuery;
|
|
84
|
+
refresh: () => Promise<readonly CommentThread[]>;
|
|
85
|
+
get: (input: CommentGetInput) => Promise<CommentThread | null>;
|
|
86
|
+
create: (input: CommentCreateInput) => Promise<CommentThread>;
|
|
87
|
+
reply: (input: CommentReplyInput) => Promise<CommentThread>;
|
|
88
|
+
edit: (input: CommentEditInput) => Promise<CommentThread>;
|
|
89
|
+
delete: (input: CommentDeleteInput) => Promise<CommentThread | null>;
|
|
90
|
+
resolve: (input: CommentStatusInput) => Promise<CommentThread>;
|
|
91
|
+
reopen: (input: CommentStatusInput) => Promise<CommentThread>;
|
|
92
|
+
users: (input: CommentUsersInput) => Promise<CommentUser[]>;
|
|
93
|
+
/** Calls `listener` after every change of `threads`, `status`, or `error`. Returns a function that stops it. */
|
|
94
|
+
onChange: (listener: () => void) => () => void;
|
|
95
|
+
/** Loads the threads, then keeps them fresh: pushed events when the transport has `subscribe`, else a refresh on window focus. Returns a function that stops it. */
|
|
96
|
+
start: () => () => void;
|
|
97
|
+
}
|
|
98
|
+
/** A cache and a change signal over a transport. It has no framework in it. */
|
|
99
|
+
export declare function createCommentsClient(options: CommentsClientOptions): CommentsClient;
|
|
100
|
+
//#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 };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
import { t as commentsErrorDiagnostics } from "./errors-DeD6wTCl.js";
|
|
2
|
+
//#region src/client-anchor.ts
|
|
3
|
+
const defaultContext = 32;
|
|
4
|
+
const clamp = (value) => Math.min(1e3, Math.max(0, value));
|
|
5
|
+
function thousandths(value, size) {
|
|
6
|
+
return size > 0 ? clamp(Math.round(value / size * 1e3)) : 0;
|
|
7
|
+
}
|
|
8
|
+
function ownerDocument(node) {
|
|
9
|
+
return node.nodeType === 9 ? node : node.ownerDocument;
|
|
10
|
+
}
|
|
11
|
+
function defaultRoot(node) {
|
|
12
|
+
const root = node.getRootNode();
|
|
13
|
+
return root.nodeType === 11 ? root : ownerDocument(node) ?? document;
|
|
14
|
+
}
|
|
15
|
+
function attr(value) {
|
|
16
|
+
return `"${value.replace(/["\\]/g, "\\$&")}"`;
|
|
17
|
+
}
|
|
18
|
+
function lookup(root, selector) {
|
|
19
|
+
if (selector === ":scope" && root.nodeType === 1) return [root];
|
|
20
|
+
try {
|
|
21
|
+
return [...root.querySelectorAll(selector)];
|
|
22
|
+
} catch {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function isOnly(root, selector, element) {
|
|
27
|
+
const found = lookup(root, selector);
|
|
28
|
+
return found.length === 1 && found[0] === element;
|
|
29
|
+
}
|
|
30
|
+
const autoIds = [
|
|
31
|
+
/^:[\w-]+:$/,
|
|
32
|
+
/^«[^»]*»$/,
|
|
33
|
+
/^_r_[\w-]+_$/,
|
|
34
|
+
/^radix-/,
|
|
35
|
+
/^headlessui-/,
|
|
36
|
+
/^reka-/,
|
|
37
|
+
/^v-\d/,
|
|
38
|
+
/^data-v-/,
|
|
39
|
+
/^ember\d/,
|
|
40
|
+
/^[\da-f-]{8,}$/i
|
|
41
|
+
];
|
|
42
|
+
function isAutoId(id) {
|
|
43
|
+
if (autoIds.some((pattern) => pattern.test(id))) return true;
|
|
44
|
+
const digits = id.replace(/\D/g, "").length;
|
|
45
|
+
return id.length >= 6 && digits / id.length > .5;
|
|
46
|
+
}
|
|
47
|
+
function stableSelector(element, root) {
|
|
48
|
+
const candidates = [];
|
|
49
|
+
const commentId = element.getAttribute("data-comment-id");
|
|
50
|
+
if (commentId) candidates.push(`[data-comment-id=${attr(commentId)}]`);
|
|
51
|
+
const id = element.getAttribute("id");
|
|
52
|
+
if (id && !isAutoId(id)) candidates.push(/^[A-Za-z][\w-]*$/.test(id) ? `#${id}` : `[id=${attr(id)}]`);
|
|
53
|
+
const testId = element.getAttribute("data-testid");
|
|
54
|
+
if (testId) candidates.push(`[data-testid=${attr(testId)}]`);
|
|
55
|
+
return candidates.find((candidate) => isOnly(root, candidate, element));
|
|
56
|
+
}
|
|
57
|
+
function segment(element) {
|
|
58
|
+
const tag = element.localName;
|
|
59
|
+
const parent = element.parentElement;
|
|
60
|
+
if (!parent) return tag;
|
|
61
|
+
const same = [...parent.children].filter((child) => child.localName === tag);
|
|
62
|
+
return same.length > 1 ? `${tag}:nth-of-type(${same.indexOf(element) + 1})` : tag;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The shortest selector that names the element: a stable attribute, else a path from the nearest stable
|
|
66
|
+
* ancestor or from the top of the page, cut as soon as it is unique.
|
|
67
|
+
*/
|
|
68
|
+
function selectorFor(element, root = defaultRoot(element)) {
|
|
69
|
+
if (element === root) return ":scope";
|
|
70
|
+
const own = stableSelector(element, root);
|
|
71
|
+
if (own) return own;
|
|
72
|
+
const parts = [];
|
|
73
|
+
let node = element;
|
|
74
|
+
while (node && node !== root) {
|
|
75
|
+
const stable = node === element ? void 0 : stableSelector(node, root);
|
|
76
|
+
parts.unshift(stable ?? segment(node));
|
|
77
|
+
const path = parts.join(" > ");
|
|
78
|
+
if (stable || isOnly(root, path, element)) return path;
|
|
79
|
+
node = node.parentElement;
|
|
80
|
+
}
|
|
81
|
+
return parts.join(" > ");
|
|
82
|
+
}
|
|
83
|
+
function boxOf(element) {
|
|
84
|
+
const { left, top, width, height } = element.getBoundingClientRect();
|
|
85
|
+
return {
|
|
86
|
+
left,
|
|
87
|
+
top,
|
|
88
|
+
width,
|
|
89
|
+
height
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function pointTarget(point, options) {
|
|
93
|
+
const node = options.root;
|
|
94
|
+
const scope = node?.nodeType === 11 ? node : node && ownerDocument(node) || document;
|
|
95
|
+
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));
|
|
96
|
+
}
|
|
97
|
+
function textNodes(node, into = []) {
|
|
98
|
+
for (const child of node.childNodes) if (child.nodeType === 3) into.push(child);
|
|
99
|
+
else if (child.nodeType === 1) textNodes(child, into);
|
|
100
|
+
return into;
|
|
101
|
+
}
|
|
102
|
+
const FOLLOWING = 4;
|
|
103
|
+
function textOffset(element, container, offset) {
|
|
104
|
+
let total = 0;
|
|
105
|
+
const next = container.nodeType === 3 ? void 0 : container.childNodes[offset];
|
|
106
|
+
for (const node of textNodes(element)) {
|
|
107
|
+
if (container.nodeType === 3) {
|
|
108
|
+
if (node === container) return total + offset;
|
|
109
|
+
} else if (next ? node === next || next.compareDocumentPosition(node) & FOLLOWING : !container.contains(node) && container.compareDocumentPosition(node) & FOLLOWING) break;
|
|
110
|
+
total += node.data.length;
|
|
111
|
+
}
|
|
112
|
+
return total;
|
|
113
|
+
}
|
|
114
|
+
function rangeOf(target) {
|
|
115
|
+
if ("rangeCount" in target) return target.rangeCount > 0 && !target.isCollapsed ? target.getRangeAt(0) : void 0;
|
|
116
|
+
return target.collapsed ? void 0 : target;
|
|
117
|
+
}
|
|
118
|
+
function elementOfNode(node) {
|
|
119
|
+
return node.nodeType === 1 ? node : node.parentElement;
|
|
120
|
+
}
|
|
121
|
+
function textAnchor(target, options) {
|
|
122
|
+
const range = rangeOf(target);
|
|
123
|
+
const exact = range?.toString() ?? "";
|
|
124
|
+
const container = range && elementOfNode(range.commonAncestorContainer);
|
|
125
|
+
if (!range || !container || exact === "") throw commentsErrorDiagnostics.COMMENTS_R0001({ target: "an empty selection" });
|
|
126
|
+
const element = ignoreUp(container, options) ?? container;
|
|
127
|
+
if (options.root && (!options.root.contains(range.startContainer) || !options.root.contains(range.endContainer) || !options.root.contains(element))) throw commentsErrorDiagnostics.COMMENTS_R0001({ target: "a selection outside the configured root" });
|
|
128
|
+
const start = textOffset(element, range.startContainer, range.startOffset);
|
|
129
|
+
const text = textNodes(element).map((node) => node.data).join("");
|
|
130
|
+
const context = options.context ?? defaultContext;
|
|
131
|
+
return {
|
|
132
|
+
kind: "text",
|
|
133
|
+
selector: selectorFor(element, options.root ?? defaultRoot(element)),
|
|
134
|
+
exact,
|
|
135
|
+
prefix: text.slice(Math.max(0, start - context), start),
|
|
136
|
+
suffix: text.slice(start + exact.length, start + exact.length + context)
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function ignoreUp(element, options) {
|
|
140
|
+
let node = element;
|
|
141
|
+
while (node && options.ignore?.(node)) node = node.parentElement;
|
|
142
|
+
return node ?? void 0;
|
|
143
|
+
}
|
|
144
|
+
function isImage(element) {
|
|
145
|
+
return element.localName === "img";
|
|
146
|
+
}
|
|
147
|
+
function boxAnchor(element, options, point) {
|
|
148
|
+
if (options.root && !options.root.contains(element)) throw commentsErrorDiagnostics.COMMENTS_R0001({ target: "an element outside the configured root" });
|
|
149
|
+
const box = boxOf(element);
|
|
150
|
+
const selector = selectorFor(element, options.root ?? defaultRoot(element));
|
|
151
|
+
const kind = isImage(element) ? "image" : void 0;
|
|
152
|
+
if (options.area) {
|
|
153
|
+
const x = thousandths(options.area.left - box.left, box.width);
|
|
154
|
+
const y = thousandths(options.area.top - box.top, box.height);
|
|
155
|
+
const area = {
|
|
156
|
+
selector,
|
|
157
|
+
x,
|
|
158
|
+
y,
|
|
159
|
+
width: Math.max(0, thousandths(options.area.left + options.area.width - box.left, box.width) - x),
|
|
160
|
+
height: Math.max(0, thousandths(options.area.top + options.area.height - box.top, box.height) - y)
|
|
161
|
+
};
|
|
162
|
+
if (area.width > 0 && area.height > 0) return kind ? {
|
|
163
|
+
kind,
|
|
164
|
+
...area
|
|
165
|
+
} : {
|
|
166
|
+
kind: "area",
|
|
167
|
+
...area
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const x = point ? thousandths(point.x - box.left, box.width) : 500;
|
|
171
|
+
const y = point ? thousandths(point.y - box.top, box.height) : 500;
|
|
172
|
+
return kind ? {
|
|
173
|
+
kind,
|
|
174
|
+
selector,
|
|
175
|
+
x,
|
|
176
|
+
y
|
|
177
|
+
} : {
|
|
178
|
+
kind: "element",
|
|
179
|
+
selector,
|
|
180
|
+
x,
|
|
181
|
+
y
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Describes a spot of the page so it can be found again: an element with an offset, a text quote, an area,
|
|
186
|
+
* a point on an image, or the whole page. Selectors and offsets survive layout changes, because offsets are
|
|
187
|
+
* thousandths of the element box.
|
|
188
|
+
*/
|
|
189
|
+
function createAnchor(target, options = {}) {
|
|
190
|
+
if (target === "page") return { kind: "page" };
|
|
191
|
+
if ("nodeType" in target) return boxAnchor(target, options, options.point);
|
|
192
|
+
if ("rangeCount" in target || "startContainer" in target) return textAnchor(target, options);
|
|
193
|
+
const element = pointTarget(target, options);
|
|
194
|
+
if (!element) throw commentsErrorDiagnostics.COMMENTS_R0001({ target: `the point ${Math.round(target.x)}, ${Math.round(target.y)}` });
|
|
195
|
+
return boxAnchor(element, options, target);
|
|
196
|
+
}
|
|
197
|
+
const minimumContext = 4;
|
|
198
|
+
function findQuote(text, anchor) {
|
|
199
|
+
const matches = [];
|
|
200
|
+
for (let at = text.indexOf(anchor.exact); at !== -1; at = text.indexOf(anchor.exact, at + 1)) {
|
|
201
|
+
const before = text.slice(0, at);
|
|
202
|
+
const after = text.slice(at + anchor.exact.length);
|
|
203
|
+
let score = 0;
|
|
204
|
+
while (score < anchor.prefix.length && before.at(-1 - score) === anchor.prefix.at(-1 - score)) score++;
|
|
205
|
+
let tail = 0;
|
|
206
|
+
while (tail < anchor.suffix.length && after[tail] === anchor.suffix[tail]) tail++;
|
|
207
|
+
matches.push({
|
|
208
|
+
start: at,
|
|
209
|
+
score: score + tail,
|
|
210
|
+
ambiguous: false
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
if (matches.length === 1) return matches[0];
|
|
214
|
+
const needed = Math.min(minimumContext, anchor.prefix.length + anchor.suffix.length);
|
|
215
|
+
const best = matches.reduce((top, match) => !top || match.score > top.score ? match : top, void 0);
|
|
216
|
+
if (!best || needed === 0 || best.score < needed) return void 0;
|
|
217
|
+
return {
|
|
218
|
+
...best,
|
|
219
|
+
ambiguous: matches.filter((match) => match.score === best.score).length > 1
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
function boundary(nodes, offset, end) {
|
|
223
|
+
let total = 0;
|
|
224
|
+
for (const node of nodes) {
|
|
225
|
+
const next = total + node.data.length;
|
|
226
|
+
if (end ? offset > total && offset <= next : offset >= total && offset < next) return {
|
|
227
|
+
node,
|
|
228
|
+
offset: offset - total
|
|
229
|
+
};
|
|
230
|
+
total = next;
|
|
231
|
+
}
|
|
232
|
+
const last = nodes.at(-1);
|
|
233
|
+
return last && offset === total ? {
|
|
234
|
+
node: last,
|
|
235
|
+
offset: last.data.length
|
|
236
|
+
} : void 0;
|
|
237
|
+
}
|
|
238
|
+
function visible(rect) {
|
|
239
|
+
return rect.width > 0 || rect.height > 0;
|
|
240
|
+
}
|
|
241
|
+
function resolveText(anchor, element) {
|
|
242
|
+
const nodes = textNodes(element);
|
|
243
|
+
const match = findQuote(nodes.map((node) => node.data).join(""), anchor);
|
|
244
|
+
if (!match) return void 0;
|
|
245
|
+
const start = match.start;
|
|
246
|
+
const from = boundary(nodes, start, false);
|
|
247
|
+
const to = boundary(nodes, start + anchor.exact.length, true);
|
|
248
|
+
const own = boxOf(element);
|
|
249
|
+
let rect = own;
|
|
250
|
+
let first = own;
|
|
251
|
+
if (from && to) {
|
|
252
|
+
const range = (ownerDocument(element) ?? document).createRange();
|
|
253
|
+
range.setStart(from.node, from.offset);
|
|
254
|
+
range.setEnd(to.node, to.offset);
|
|
255
|
+
if (typeof range.getBoundingClientRect === "function") {
|
|
256
|
+
const { left, top, width, height } = range.getBoundingClientRect();
|
|
257
|
+
if (visible({
|
|
258
|
+
left,
|
|
259
|
+
top,
|
|
260
|
+
width,
|
|
261
|
+
height
|
|
262
|
+
})) rect = {
|
|
263
|
+
left,
|
|
264
|
+
top,
|
|
265
|
+
width,
|
|
266
|
+
height
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
const line = typeof range.getClientRects === "function" ? range.getClientRects()[0] : void 0;
|
|
270
|
+
first = line && visible(line) ? {
|
|
271
|
+
left: line.left,
|
|
272
|
+
top: line.top,
|
|
273
|
+
width: line.width,
|
|
274
|
+
height: line.height
|
|
275
|
+
} : rect;
|
|
276
|
+
}
|
|
277
|
+
if (!visible(rect)) return void 0;
|
|
278
|
+
const x = first.left;
|
|
279
|
+
const y = first.top;
|
|
280
|
+
return {
|
|
281
|
+
position: {
|
|
282
|
+
clipped: isClipped(element, x, y),
|
|
283
|
+
detached: false,
|
|
284
|
+
kind: "text",
|
|
285
|
+
x,
|
|
286
|
+
y,
|
|
287
|
+
width: rect.width,
|
|
288
|
+
height: rect.height,
|
|
289
|
+
element
|
|
290
|
+
},
|
|
291
|
+
score: match.score,
|
|
292
|
+
ambiguous: match.ambiguous
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
function composedParent(element) {
|
|
296
|
+
if (element.parentElement) return element.parentElement;
|
|
297
|
+
const root = element.getRootNode();
|
|
298
|
+
return root instanceof ShadowRoot ? root.host : null;
|
|
299
|
+
}
|
|
300
|
+
function isClipped(element, x, y) {
|
|
301
|
+
const view = element.ownerDocument.defaultView;
|
|
302
|
+
if (!view) return false;
|
|
303
|
+
for (let node = composedParent(element); node; node = composedParent(node)) {
|
|
304
|
+
const { overflow, overflowX, overflowY } = view.getComputedStyle(node);
|
|
305
|
+
if (![
|
|
306
|
+
overflow,
|
|
307
|
+
overflowX,
|
|
308
|
+
overflowY
|
|
309
|
+
].some((value) => value !== "" && value !== "visible")) continue;
|
|
310
|
+
const box = boxOf(node);
|
|
311
|
+
if (!visible(box)) continue;
|
|
312
|
+
if (x < box.left - 1 || x > box.left + box.width + 1 || y < box.top - 1 || y > box.top + box.height + 1) return true;
|
|
313
|
+
}
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Finds an anchor on the page again. It returns the pin position in client coordinates, or `{ detached: true }`
|
|
318
|
+
* when the element is gone, hidden, ambiguous, or no longer holds the quote. A selector that names more than one
|
|
319
|
+
* element is ambiguous, so a pin never lands on the wrong one. A text anchor tries every match and takes the one
|
|
320
|
+
* that holds the quote best.
|
|
321
|
+
*/
|
|
322
|
+
function resolveAnchor(anchor, root = document) {
|
|
323
|
+
if (anchor.kind === "page") return {
|
|
324
|
+
clipped: false,
|
|
325
|
+
detached: false,
|
|
326
|
+
kind: "page",
|
|
327
|
+
x: 0,
|
|
328
|
+
y: 0,
|
|
329
|
+
width: 0,
|
|
330
|
+
height: 0
|
|
331
|
+
};
|
|
332
|
+
const found = lookup(root, anchor.selector);
|
|
333
|
+
if (anchor.kind === "text") {
|
|
334
|
+
let best;
|
|
335
|
+
for (const element of found) {
|
|
336
|
+
const resolution = resolveText(anchor, element);
|
|
337
|
+
if (resolution && (!best || resolution.score > best.score)) best = resolution;
|
|
338
|
+
else if (resolution && best && resolution.score === best.score) best.ambiguous = true;
|
|
339
|
+
}
|
|
340
|
+
return best && !best.ambiguous ? best.position : { detached: true };
|
|
341
|
+
}
|
|
342
|
+
const element = found.length === 1 ? found[0] : void 0;
|
|
343
|
+
if (!element) return { detached: true };
|
|
344
|
+
const box = boxOf(element);
|
|
345
|
+
if (!visible(box)) return { detached: true };
|
|
346
|
+
const width = "width" in anchor && anchor.width ? anchor.width / 1e3 * box.width : 0;
|
|
347
|
+
const height = "height" in anchor && anchor.height ? anchor.height / 1e3 * box.height : 0;
|
|
348
|
+
const x = box.left + anchor.x / 1e3 * box.width;
|
|
349
|
+
const y = box.top + anchor.y / 1e3 * box.height;
|
|
350
|
+
return {
|
|
351
|
+
clipped: isClipped(element, x, y),
|
|
352
|
+
detached: false,
|
|
353
|
+
kind: anchor.kind,
|
|
354
|
+
x,
|
|
355
|
+
y,
|
|
356
|
+
width,
|
|
357
|
+
height,
|
|
358
|
+
element
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
//#endregion
|
|
362
|
+
//#region src/client.ts
|
|
363
|
+
/** A cache and a change signal over a transport. It has no framework in it. */
|
|
364
|
+
function createCommentsClient(options) {
|
|
365
|
+
const { transport } = options;
|
|
366
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
367
|
+
const byId = /* @__PURE__ */ new Map();
|
|
368
|
+
let listed = /* @__PURE__ */ new Set();
|
|
369
|
+
let changed = /* @__PURE__ */ new Set();
|
|
370
|
+
let removed = /* @__PURE__ */ new Set();
|
|
371
|
+
let threads = [];
|
|
372
|
+
let status = "idle";
|
|
373
|
+
let error;
|
|
374
|
+
let query = options.query ?? {};
|
|
375
|
+
let generation = 0;
|
|
376
|
+
const transportOwnsMembership = () => !options.user && Boolean(query.mine || query.mentions);
|
|
377
|
+
const notify = () => {
|
|
378
|
+
for (const listener of listeners) listener();
|
|
379
|
+
};
|
|
380
|
+
const matchesQuery = (thread) => {
|
|
381
|
+
const user = options.user;
|
|
382
|
+
if (query.subject && (thread.subject.kind !== query.subject.kind || thread.subject.id !== query.subject.id)) return false;
|
|
383
|
+
if (query.page !== void 0 && (thread.subject.kind !== "page" || thread.subject.id !== query.page)) return false;
|
|
384
|
+
if (query.status && thread.status !== query.status) return false;
|
|
385
|
+
if (user && query.mine && thread.author.id !== user.id) return false;
|
|
386
|
+
if (user && query.mentions && !thread.messages.some((message) => message.mentions.includes(user.id))) return false;
|
|
387
|
+
if (transportOwnsMembership() && !listed.has(thread.id)) return false;
|
|
388
|
+
return true;
|
|
389
|
+
};
|
|
390
|
+
const publish = () => {
|
|
391
|
+
threads = [...byId.values()].filter(matchesQuery).sort((a, b) => a.n - b.n);
|
|
392
|
+
notify();
|
|
393
|
+
};
|
|
394
|
+
const upsert = (thread) => {
|
|
395
|
+
const stored = byId.get(thread.id);
|
|
396
|
+
if (removed.has(thread.id) || stored && stored.updatedAt > thread.updatedAt) return stored ?? thread;
|
|
397
|
+
byId.set(thread.id, thread);
|
|
398
|
+
changed.add(thread.id);
|
|
399
|
+
publish();
|
|
400
|
+
if (transportOwnsMembership()) refreshQuietly();
|
|
401
|
+
return thread;
|
|
402
|
+
};
|
|
403
|
+
const drop = (threadId) => {
|
|
404
|
+
removed.add(threadId);
|
|
405
|
+
changed.delete(threadId);
|
|
406
|
+
if (byId.delete(threadId)) publish();
|
|
407
|
+
};
|
|
408
|
+
const mutations = /* @__PURE__ */ new Map();
|
|
409
|
+
const mutate = async (threadId, operation) => {
|
|
410
|
+
const previous = mutations.get(threadId);
|
|
411
|
+
const request = (async () => {
|
|
412
|
+
await previous?.catch(() => void 0);
|
|
413
|
+
return operation();
|
|
414
|
+
})();
|
|
415
|
+
mutations.set(threadId, request);
|
|
416
|
+
try {
|
|
417
|
+
return await request;
|
|
418
|
+
} finally {
|
|
419
|
+
if (mutations.get(threadId) === request) mutations.delete(threadId);
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
const refresh = async () => {
|
|
423
|
+
const current = ++generation;
|
|
424
|
+
changed = /* @__PURE__ */ new Set();
|
|
425
|
+
removed = /* @__PURE__ */ new Set();
|
|
426
|
+
status = "loading";
|
|
427
|
+
notify();
|
|
428
|
+
try {
|
|
429
|
+
const requested = { ...query };
|
|
430
|
+
const pageSize = requested.limit ?? 200;
|
|
431
|
+
let list = [];
|
|
432
|
+
let previousSnapshot;
|
|
433
|
+
for (let scan = 0;; scan++) {
|
|
434
|
+
list = [];
|
|
435
|
+
let before = requested.before;
|
|
436
|
+
let pages = 0;
|
|
437
|
+
while (true) {
|
|
438
|
+
const page = await transport.list({
|
|
439
|
+
...requested,
|
|
440
|
+
limit: pageSize,
|
|
441
|
+
...before ? { before } : {}
|
|
442
|
+
});
|
|
443
|
+
if (current !== generation) return threads;
|
|
444
|
+
pages++;
|
|
445
|
+
list.push(...page);
|
|
446
|
+
if (requested.limit !== void 0 || page.length < pageSize) break;
|
|
447
|
+
const last = page.at(-1);
|
|
448
|
+
before = {
|
|
449
|
+
id: last.id,
|
|
450
|
+
updatedAt: last.updatedAt
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
if (requested.limit !== void 0 || pages === 1 && previousSnapshot === void 0) break;
|
|
454
|
+
const snapshot = JSON.stringify(list);
|
|
455
|
+
if (snapshot === previousSnapshot) break;
|
|
456
|
+
if (scan === 2) throw commentsErrorDiagnostics.COMMENTS_R0015();
|
|
457
|
+
previousSnapshot = snapshot;
|
|
458
|
+
}
|
|
459
|
+
if (current !== generation) return threads;
|
|
460
|
+
const next = /* @__PURE__ */ new Map();
|
|
461
|
+
for (const thread of list) {
|
|
462
|
+
const local = byId.get(thread.id);
|
|
463
|
+
if (removed.has(thread.id)) continue;
|
|
464
|
+
next.set(thread.id, local && (local.updatedAt > thread.updatedAt || changed.has(thread.id) && local.updatedAt === thread.updatedAt) ? local : thread);
|
|
465
|
+
}
|
|
466
|
+
for (const id of changed) {
|
|
467
|
+
const local = byId.get(id);
|
|
468
|
+
if (local && !next.has(id)) next.set(id, local);
|
|
469
|
+
}
|
|
470
|
+
byId.clear();
|
|
471
|
+
for (const [id, thread] of next) byId.set(id, thread);
|
|
472
|
+
listed = new Set(list.map((thread) => thread.id));
|
|
473
|
+
status = "ready";
|
|
474
|
+
error = void 0;
|
|
475
|
+
publish();
|
|
476
|
+
} catch (cause) {
|
|
477
|
+
if (current === generation) {
|
|
478
|
+
status = "error";
|
|
479
|
+
error = cause;
|
|
480
|
+
notify();
|
|
481
|
+
}
|
|
482
|
+
throw cause;
|
|
483
|
+
}
|
|
484
|
+
return threads;
|
|
485
|
+
};
|
|
486
|
+
const refreshQuietly = () => {
|
|
487
|
+
refresh().catch(() => void 0);
|
|
488
|
+
};
|
|
489
|
+
const apply = (event) => {
|
|
490
|
+
if (event.type === "thread") {
|
|
491
|
+
if (!options.user && (query.mine || query.mentions)) return refreshQuietly();
|
|
492
|
+
if (byId.get(event.thread.id)?.updatedAt === event.thread.updatedAt) refreshQuietly();
|
|
493
|
+
else upsert(event.thread);
|
|
494
|
+
} else if (event.type === "deleted") drop(event.threadId);
|
|
495
|
+
else refreshQuietly();
|
|
496
|
+
};
|
|
497
|
+
return {
|
|
498
|
+
transport,
|
|
499
|
+
user: options.user,
|
|
500
|
+
get threads() {
|
|
501
|
+
return threads;
|
|
502
|
+
},
|
|
503
|
+
get status() {
|
|
504
|
+
return status;
|
|
505
|
+
},
|
|
506
|
+
get error() {
|
|
507
|
+
return error;
|
|
508
|
+
},
|
|
509
|
+
get query() {
|
|
510
|
+
return query;
|
|
511
|
+
},
|
|
512
|
+
set query(next) {
|
|
513
|
+
query = next;
|
|
514
|
+
generation++;
|
|
515
|
+
listed.clear();
|
|
516
|
+
publish();
|
|
517
|
+
},
|
|
518
|
+
refresh,
|
|
519
|
+
get: (input) => transport.get(input),
|
|
520
|
+
create: async (input) => upsert(await transport.create(input)),
|
|
521
|
+
reply: (input) => mutate(input.threadId, async () => upsert(await transport.reply(input))),
|
|
522
|
+
edit: (input) => mutate(input.threadId, async () => upsert(await transport.edit(input))),
|
|
523
|
+
delete: (input) => mutate(input.threadId, async () => {
|
|
524
|
+
const next = await transport.delete(input);
|
|
525
|
+
if (next) upsert(next);
|
|
526
|
+
else drop(input.threadId);
|
|
527
|
+
return next;
|
|
528
|
+
}),
|
|
529
|
+
resolve: (input) => mutate(input.threadId, async () => upsert(await transport.resolve(input))),
|
|
530
|
+
reopen: (input) => mutate(input.threadId, async () => upsert(await transport.reopen(input))),
|
|
531
|
+
users: (input) => transport.users(input),
|
|
532
|
+
onChange(listener) {
|
|
533
|
+
listeners.add(listener);
|
|
534
|
+
return () => {
|
|
535
|
+
listeners.delete(listener);
|
|
536
|
+
};
|
|
537
|
+
},
|
|
538
|
+
start() {
|
|
539
|
+
const stops = [];
|
|
540
|
+
if (transport.subscribe) stops.push(transport.subscribe(apply));
|
|
541
|
+
else if (options.refreshOnFocus !== false && typeof window !== "undefined") {
|
|
542
|
+
window.addEventListener("focus", refreshQuietly);
|
|
543
|
+
stops.push(() => window.removeEventListener("focus", refreshQuietly));
|
|
544
|
+
}
|
|
545
|
+
refreshQuietly();
|
|
546
|
+
return () => {
|
|
547
|
+
for (const stop of stops.splice(0)) stop();
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
//#endregion
|
|
553
|
+
export { commentsErrorDiagnostics, createAnchor, createCommentsClient, resolveAnchor, selectorFor };
|