@tldraw/commenting 5.4.2 → 5.5.0-canary.0241f11f2e99
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/commenting.css +0 -32
- package/dist-cjs/canvas/anchor-lifecycle.js +4 -5
- package/dist-cjs/canvas/anchor-lifecycle.js.map +2 -2
- package/dist-cjs/canvas/cluster-fade.js +1 -1
- package/dist-cjs/canvas/cluster-fade.js.map +2 -2
- package/dist-cjs/canvas/cluster-model.js +23 -31
- package/dist-cjs/canvas/cluster-model.js.map +2 -2
- package/dist-cjs/canvas/comment-render.js +19 -19
- package/dist-cjs/canvas/comment-render.js.map +2 -2
- package/dist-cjs/canvas/comment-store.js +10 -5
- package/dist-cjs/canvas/comment-store.js.map +2 -2
- package/dist-cjs/canvas/comments-overlay.js +12 -16
- package/dist-cjs/canvas/comments-overlay.js.map +2 -2
- package/dist-cjs/canvas/comments-sidebar.js +1 -7
- package/dist-cjs/canvas/comments-sidebar.js.map +2 -2
- package/dist-cjs/canvas/hooks.js.map +1 -1
- package/dist-cjs/canvas/thread-pin.js +14 -26
- package/dist-cjs/canvas/thread-pin.js.map +2 -2
- package/dist-cjs/canvas/thread-stack.js +2 -6
- package/dist-cjs/canvas/thread-stack.js.map +2 -2
- package/dist-cjs/canvas/thread-state.js +13 -0
- package/dist-cjs/canvas/thread-state.js.map +2 -2
- package/dist-cjs/canvas/thread-view.js +67 -78
- package/dist-cjs/canvas/thread-view.js.map +2 -2
- package/dist-cjs/clustering/computeClusterTable.js +2 -9
- package/dist-cjs/clustering/computeClusterTable.js.map +2 -2
- package/dist-cjs/clustering/replay.js +10 -23
- package/dist-cjs/clustering/replay.js.map +2 -2
- package/dist-cjs/clustering/runtime.js +9 -15
- package/dist-cjs/clustering/runtime.js.map +2 -2
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/ui/comment-composer.js +2 -4
- package/dist-cjs/ui/comment-composer.js.map +2 -2
- package/dist-cjs/ui/format-time.js +20 -8
- package/dist-cjs/ui/format-time.js.map +2 -2
- package/dist-cjs/ui/reaction.js +5 -15
- package/dist-cjs/ui/reaction.js.map +2 -2
- package/dist-esm/canvas/anchor-lifecycle.mjs +5 -6
- package/dist-esm/canvas/anchor-lifecycle.mjs.map +2 -2
- package/dist-esm/canvas/cluster-fade.mjs +1 -1
- package/dist-esm/canvas/cluster-fade.mjs.map +2 -2
- package/dist-esm/canvas/cluster-model.mjs +24 -32
- package/dist-esm/canvas/cluster-model.mjs.map +2 -2
- package/dist-esm/canvas/comment-render.mjs +19 -19
- package/dist-esm/canvas/comment-render.mjs.map +2 -2
- package/dist-esm/canvas/comment-store.mjs +10 -5
- package/dist-esm/canvas/comment-store.mjs.map +2 -2
- package/dist-esm/canvas/comments-overlay.mjs +13 -17
- package/dist-esm/canvas/comments-overlay.mjs.map +2 -2
- package/dist-esm/canvas/comments-sidebar.mjs +3 -14
- package/dist-esm/canvas/comments-sidebar.mjs.map +2 -2
- package/dist-esm/canvas/hooks.mjs.map +1 -1
- package/dist-esm/canvas/thread-pin.mjs +16 -26
- package/dist-esm/canvas/thread-pin.mjs.map +2 -2
- package/dist-esm/canvas/thread-stack.mjs +3 -7
- package/dist-esm/canvas/thread-stack.mjs.map +2 -2
- package/dist-esm/canvas/thread-state.mjs +13 -0
- package/dist-esm/canvas/thread-state.mjs.map +2 -2
- package/dist-esm/canvas/thread-view.mjs +67 -78
- package/dist-esm/canvas/thread-view.mjs.map +2 -2
- package/dist-esm/clustering/computeClusterTable.mjs +2 -9
- package/dist-esm/clustering/computeClusterTable.mjs.map +2 -2
- package/dist-esm/clustering/replay.mjs +10 -23
- package/dist-esm/clustering/replay.mjs.map +2 -2
- package/dist-esm/clustering/runtime.mjs +9 -15
- package/dist-esm/clustering/runtime.mjs.map +2 -2
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/ui/comment-composer.mjs +2 -4
- package/dist-esm/ui/comment-composer.mjs.map +2 -2
- package/dist-esm/ui/format-time.mjs +20 -8
- package/dist-esm/ui/format-time.mjs.map +2 -2
- package/dist-esm/ui/reaction.mjs +7 -15
- package/dist-esm/ui/reaction.mjs.map +2 -2
- package/package.json +5 -5
- package/src/canvas/anchor-lifecycle.ts +5 -6
- package/src/canvas/cluster-fade.ts +6 -6
- package/src/canvas/cluster-model.test.ts +64 -1
- package/src/canvas/cluster-model.ts +28 -33
- package/src/canvas/comment-render.ts +30 -28
- package/src/canvas/comment-store.ts +16 -6
- package/src/canvas/comments-overlay.tsx +18 -19
- package/src/canvas/comments-sidebar.tsx +3 -18
- package/src/canvas/hooks.ts +1 -1
- package/src/canvas/thread-pin.tsx +21 -31
- package/src/canvas/thread-stack.tsx +3 -7
- package/src/canvas/thread-state.test.ts +35 -0
- package/src/canvas/thread-state.ts +27 -0
- package/src/canvas/thread-view.tsx +93 -95
- package/src/clustering/computeClusterTable.ts +3 -20
- package/src/clustering/replay.ts +10 -23
- package/src/clustering/runtime.ts +9 -15
- package/src/ui/comment-composer.tsx +2 -4
- package/src/ui/comments.css +0 -32
- package/src/ui/format-time.ts +24 -11
- package/src/ui/reaction.tsx +11 -23
|
@@ -93,8 +93,6 @@ class ClusterState {
|
|
|
93
93
|
centroidY;
|
|
94
94
|
counts;
|
|
95
95
|
nodes;
|
|
96
|
-
memberLists;
|
|
97
|
-
minMemberIds;
|
|
98
96
|
// Per-cluster sums of member render offsets (screen px), maintained like the centroid sums.
|
|
99
97
|
// Null when no offsets were passed — offsetDelta() then answers null unconditionally, which
|
|
100
98
|
// routes every pricing call down the offset-unaware path.
|
|
@@ -111,8 +109,6 @@ class ClusterState {
|
|
|
111
109
|
this.centroidY = new Float64Array(n);
|
|
112
110
|
this.counts = new Int32Array(n);
|
|
113
111
|
this.nodes = new Array(n);
|
|
114
|
-
this.memberLists = new Array(n);
|
|
115
|
-
this.minMemberIds = new Array(n);
|
|
116
112
|
if (screenOffsets !== void 0 && screenOffsets.size > 0) {
|
|
117
113
|
this.offsetX = new Float64Array(n);
|
|
118
114
|
this.offsetY = new Float64Array(n);
|
|
@@ -137,8 +133,6 @@ class ClusterState {
|
|
|
137
133
|
this.centroidX[i] = leaf.point.x;
|
|
138
134
|
this.centroidY[i] = leaf.point.y;
|
|
139
135
|
this.counts[i] = 1;
|
|
140
|
-
this.memberLists[i] = [leaf.id];
|
|
141
|
-
this.minMemberIds[i] = leaf.id;
|
|
142
136
|
this.nodes[i] = {
|
|
143
137
|
id: leaf.id,
|
|
144
138
|
centroid: { x: leaf.point.x, y: leaf.point.y },
|
|
@@ -159,6 +153,7 @@ class ClusterState {
|
|
|
159
153
|
}
|
|
160
154
|
return root;
|
|
161
155
|
}
|
|
156
|
+
/** Allocation-free: this is the hot path when no offsets are passed. */
|
|
162
157
|
centroidDistance(aRoot, bRoot) {
|
|
163
158
|
return Math.hypot(
|
|
164
159
|
this.centroidX[aRoot] - this.centroidX[bRoot],
|
|
@@ -189,12 +184,12 @@ class ClusterState {
|
|
|
189
184
|
return Math.hypot(maxX - minX, maxY - minY);
|
|
190
185
|
}
|
|
191
186
|
merge(aRoot, bRoot, z) {
|
|
192
|
-
const leftRoot = this.
|
|
187
|
+
const leftRoot = this.nodes[aRoot].members[0] < this.nodes[bRoot].members[0] ? aRoot : bRoot;
|
|
193
188
|
const rightRoot = leftRoot === aRoot ? bRoot : aRoot;
|
|
194
189
|
const left = this.nodes[leftRoot];
|
|
195
190
|
const right = this.nodes[rightRoot];
|
|
196
191
|
const count = this.counts[leftRoot] + this.counts[rightRoot];
|
|
197
|
-
const members = mergeSortedMembers(
|
|
192
|
+
const members = mergeSortedMembers(left.members, right.members);
|
|
198
193
|
const minX = Math.min(this.minX[leftRoot], this.minX[rightRoot]);
|
|
199
194
|
const minY = Math.min(this.minY[leftRoot], this.minY[rightRoot]);
|
|
200
195
|
const maxX = Math.max(this.maxX[leftRoot], this.maxX[rightRoot]);
|
|
@@ -216,8 +211,6 @@ class ClusterState {
|
|
|
216
211
|
this.centroidY[leftRoot] = centroidY;
|
|
217
212
|
this.counts[leftRoot] = count;
|
|
218
213
|
this.nodes[leftRoot] = result;
|
|
219
|
-
this.memberLists[leftRoot] = members;
|
|
220
|
-
this.minMemberIds[leftRoot] = members[0];
|
|
221
214
|
if (this.offsetX !== null && this.offsetY !== null) {
|
|
222
215
|
this.offsetX[leftRoot] += this.offsetX[rightRoot];
|
|
223
216
|
this.offsetY[leftRoot] += this.offsetY[rightRoot];
|
|
@@ -243,10 +236,11 @@ function mergeSortedMembers(a, b) {
|
|
|
243
236
|
}
|
|
244
237
|
class EdgeMaxHeap {
|
|
245
238
|
items = [];
|
|
246
|
-
// Per-edge
|
|
247
|
-
//
|
|
248
|
-
//
|
|
249
|
-
//
|
|
239
|
+
// Per-edge (lo, hi) id pair for the z tie-break, precomputed once so comparisons allocate
|
|
240
|
+
// nothing. Relies on MstEdge's invariant that leaves[a].id < leaves[b].id; the heap does not
|
|
241
|
+
// re-normalize. With coincident anchors every edge prices to the same z (+Infinity), so the
|
|
242
|
+
// tie-break runs on nearly every comparison of a rebuild: allocating the pair there churned
|
|
243
|
+
// millions of short-lived tuples.
|
|
250
244
|
loIds;
|
|
251
245
|
hiIds;
|
|
252
246
|
constructor(edges, leaves) {
|
|
@@ -254,15 +248,8 @@ class EdgeMaxHeap {
|
|
|
254
248
|
this.loIds = new Array(n);
|
|
255
249
|
this.hiIds = new Array(n);
|
|
256
250
|
for (let i = 0; i < n; i++) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
if (aId < bId) {
|
|
260
|
-
this.loIds[i] = aId;
|
|
261
|
-
this.hiIds[i] = bId;
|
|
262
|
-
} else {
|
|
263
|
-
this.loIds[i] = bId;
|
|
264
|
-
this.hiIds[i] = aId;
|
|
265
|
-
}
|
|
251
|
+
this.loIds[i] = leaves[edges[i].a].id;
|
|
252
|
+
this.hiIds[i] = leaves[edges[i].b].id;
|
|
266
253
|
}
|
|
267
254
|
}
|
|
268
255
|
peek() {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/clustering/replay.ts"],
|
|
4
|
-
"sourcesContent": ["import type { ClusterNode, LeafInput, LeafScreenOffsets, MstEdge, RawMergeEvent } from './types'\n\nexport const D_FLOOR = 1e-9\n\nexport function cappedReplay(\n\tleaves: readonly LeafInput[],\n\tedges: readonly MstEdge[],\n\topts: { Tc: number; Dmax: number },\n\t// Render offsets for markers that draw off their anchor (imprecise pins). Omitted or empty,\n\t// every code path below is the offset-unaware original \u2014 pricing, events, and floats alike.\n\tscreenOffsets?: LeafScreenOffsets\n): RawMergeEvent[] {\n\tvalidateOptions(opts)\n\n\tif (edges.length === 0) return []\n\n\tconst clusters = new ClusterState(leaves, screenOffsets)\n\tconst heap = new EdgeMaxHeap(edges, leaves)\n\t// Edges incident to each current cluster root, for eager repricing: a merge\n\t// moves the result's centroid, which can RAISE an incident edge's key (the\n\t// centroid may move toward a neighbor), so the lazy pop-time recheck alone\n\t// is not enough \u2014 every price change pushes a fresh heap entry.\n\tconst incident: number[][] = leaves.map(() => [])\n\tfor (let i = 0; i < edges.length; i++) {\n\t\theap.push({ edgeIndex: i, z: zForEdge(edges[i], clusters, opts) })\n\t\tincident[edges[i].a].push(i)\n\t\tincident[edges[i].b].push(i)\n\t}\n\n\tconst events: RawMergeEvent[] = []\n\t// Emitted thresholds are clamped non-increasing: with centroid pricing a\n\t// later merge's raw price can in principle exceed an earlier one's, and the\n\t// table's descending sort is load-bearing for the runtime cursor.\n\tlet lastZ = Number.POSITIVE_INFINITY\n\twhile (events.length < edges.length) {\n\t\tconst entry = heap.pop()\n\t\tif (!entry) break\n\n\t\tconst edge = edges[entry.edgeIndex]\n\t\tconst aRoot = clusters.find(edge.a)\n\t\tconst bRoot = clusters.find(edge.b)\n\t\tif (aRoot === bRoot) continue // stale duplicate of an already-fired edge\n\n\t\tconst current = { edgeIndex: entry.edgeIndex, z: zForRoots(edge, aRoot, bRoot, clusters, opts) }\n\t\tconst next = heap.peek()\n\t\tif (next && heap.higherPriority(next, current)) {\n\t\t\theap.push(current)\n\t\t\tcontinue\n\t\t}\n\n\t\tconst z = Math.min(current.z, lastZ)\n\t\tlastZ = z\n\t\tevents.push(clusters.merge(aRoot, bRoot, z))\n\n\t\t// eager reprice: the new cluster's centroid and bbox changed, so every\n\t\t// surviving incident edge gets a fresh entry at its current price\n\t\tconst root = clusters.find(edge.a)\n\t\tconst other = root === aRoot ? bRoot : aRoot\n\t\tconst survivors: number[] = []\n\t\tfor (const idx of [...incident[root], ...incident[other]]) {\n\t\t\tconst e2 = edges[idx]\n\t\t\tconst ra = clusters.find(e2.a)\n\t\t\tconst rb = clusters.find(e2.b)\n\t\t\tif (ra === rb) continue\n\t\t\tsurvivors.push(idx)\n\t\t\theap.push({ edgeIndex: idx, z: zForRoots(e2, ra, rb, clusters, opts) })\n\t\t}\n\t\tincident[root] = survivors\n\t}\n\n\treturn events\n}\n\nfunction validateOptions(opts: { Tc: number; Dmax: number }) {\n\tif (!Number.isFinite(opts.Tc) || opts.Tc <= 0) {\n\t\tthrow new Error('Tc must be greater than 0')\n\t}\n\tif (!Number.isFinite(opts.Dmax) || opts.Dmax <= 0) {\n\t\tthrow new Error('Dmax must be greater than 0')\n\t}\n\tif (opts.Dmax < opts.Tc) {\n\t\tthrow new Error('Dmax must be greater than or equal to Tc')\n\t}\n}\n\nfunction zForEdge(\n\tedge: MstEdge,\n\tclusters: ClusterState,\n\topts: { Tc: number; Dmax: number }\n): number {\n\treturn zForRoots(edge, clusters.find(edge.a), clusters.find(edge.b), clusters, opts)\n}\n\nfunction zForRoots(\n\tedge: MstEdge,\n\taRoot: number,\n\tbRoot: number,\n\tclusters: ClusterState,\n\topts: { Tc: number; Dmax: number }\n): number {\n\t// Null unless offset pricing is active AND this pair's mean render offsets differ. Every\n\t// other pair \u2014 including every pair when no offsets were passed \u2014 takes the branch below,\n\t// which is the offset-unaware pricing verbatim.\n\tconst off = clusters.offsetDelta(aRoot, bRoot)\n\tif (off === null) {\n\t\tif (edge.d < D_FLOOR) return Number.POSITIVE_INFINITY\n\t\t// Badge-anchored gap pricing: clusters render as badges at their centroids,\n\t\t// so the merge is priced by the distance between the rendered centers, not\n\t\t// the nearest members. For leaves the two are identical; for clusters the\n\t\t// centroid distance is larger, so groups merge later than their closest\n\t\t// members would suggest \u2014 matching what the user actually sees.\n\t\t// (Coincident CENTROIDS with non-coincident members leave the gap term\n\t\t// Infinity and the fit term finite \u2014 the min stays finite, no special case.)\n\t\tconst gap = opts.Tc / clusters.centroidDistance(aRoot, bRoot)\n\t\tconst fit = opts.Dmax / clusters.unionBboxDiag(aRoot, bRoot)\n\t\treturn Math.min(gap, fit)\n\t}\n\n\t// Offset-aware gap pricing: these markers render at `z\u00B7centroid + meanOffset`, so their\n\t// visual distance at zoom z is |z\u00B7\u0394C + \u0394\u014D| and the merge prices at its Tc crossing. When\n\t// that distance never reaches Tc (the constant offsets hold the visuals apart harder than\n\t// the anchors close), the pair prices at 0 \u2014 visually never mergeable, pruned by finalize's\n\t// minZoom cut. No D_FLOOR fast path here: coincident anchors with differing offsets are a\n\t// constant |\u0394\u014D| apart on screen, which is exactly the \u0394C = 0 case below.\n\tconst dc = clusters.centroidDelta(aRoot, bRoot)\n\tif (dc.x === 0 && dc.y === 0) {\n\t\treturn Math.hypot(off.x, off.y) < opts.Tc ? Number.POSITIVE_INFINITY : 0\n\t}\n\tconst gap = largestVisualCrossing(dc.x, dc.y, off.x, off.y, opts.Tc)\n\tif (gap === null) return 0\n\tconst fit = opts.Dmax / clusters.unionBboxDiag(aRoot, bRoot)\n\treturn Math.min(gap, fit)\n}\n\n/**\n * The largest positive root of `|z\u00B7\u0394C + \u0394\u014D| = level`, i.e. of\n * `|\u0394C|\u00B2\u00B7z\u00B2 + 2(\u0394C\u00B7\u0394\u014D)\u00B7z + (|\u0394\u014D|\u00B2 \u2212 level\u00B2) = 0` \u2014 the zoom at which two offset markers are\n * exactly `level` screen px apart, with the distance below the level for every smaller zoom\n * (matching the \"merged at z \u2264 threshold\" model; a lower second crossing, where opposed offsets\n * push the visuals back above the level near z = 0, is deliberately collapsed). Null when the\n * visual distance never reaches the level. Callers handle \u0394C = 0 (constant distance) themselves.\n */\nfunction largestVisualCrossing(\n\tdcx: number,\n\tdcy: number,\n\tdox: number,\n\tdoy: number,\n\tlevel: number\n): number | null {\n\tconst a = dcx * dcx + dcy * dcy\n\tconst b = 2 * (dcx * dox + dcy * doy)\n\tconst c = dox * dox + doy * doy - level * level\n\tconst disc = b * b - 4 * a * c\n\tif (disc < 0) return null\n\tconst z = (-b + Math.sqrt(disc)) / (2 * a)\n\treturn z > 0 ? z : null\n}\n\nclass ClusterState {\n\tprivate readonly parent: Int32Array\n\tprivate readonly minX: Float64Array\n\tprivate readonly minY: Float64Array\n\tprivate readonly maxX: Float64Array\n\tprivate readonly maxY: Float64Array\n\tprivate readonly centroidX: Float64Array\n\tprivate readonly centroidY: Float64Array\n\tprivate readonly counts: Int32Array\n\tprivate readonly nodes: ClusterNode[]\n\tprivate readonly memberLists: string[][]\n\tprivate readonly minMemberIds: string[]\n\t// Per-cluster sums of member render offsets (screen px), maintained like the centroid sums.\n\t// Null when no offsets were passed \u2014 offsetDelta() then answers null unconditionally, which\n\t// routes every pricing call down the offset-unaware path.\n\tprivate readonly offsetX: Float64Array | null\n\tprivate readonly offsetY: Float64Array | null\n\n\tconstructor(leaves: readonly LeafInput[], screenOffsets?: LeafScreenOffsets) {\n\t\tconst n = leaves.length\n\t\tthis.parent = new Int32Array(n)\n\t\tthis.minX = new Float64Array(n)\n\t\tthis.minY = new Float64Array(n)\n\t\tthis.maxX = new Float64Array(n)\n\t\tthis.maxY = new Float64Array(n)\n\t\tthis.centroidX = new Float64Array(n)\n\t\tthis.centroidY = new Float64Array(n)\n\t\tthis.counts = new Int32Array(n)\n\t\tthis.nodes = new Array(n)\n\t\tthis.memberLists = new Array(n)\n\t\tthis.minMemberIds = new Array(n)\n\n\t\tif (screenOffsets !== undefined && screenOffsets.size > 0) {\n\t\t\tthis.offsetX = new Float64Array(n)\n\t\t\tthis.offsetY = new Float64Array(n)\n\t\t\tfor (let i = 0; i < n; i++) {\n\t\t\t\tconst offset = screenOffsets.get(leaves[i].id)\n\t\t\t\tif (offset) {\n\t\t\t\t\tthis.offsetX[i] = offset.x\n\t\t\t\t\tthis.offsetY[i] = offset.y\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tthis.offsetX = null\n\t\t\tthis.offsetY = null\n\t\t}\n\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst leaf = leaves[i]\n\t\t\tthis.parent[i] = i\n\t\t\tthis.minX[i] = leaf.point.x\n\t\t\tthis.minY[i] = leaf.point.y\n\t\t\tthis.maxX[i] = leaf.point.x\n\t\t\tthis.maxY[i] = leaf.point.y\n\t\t\tthis.centroidX[i] = leaf.point.x\n\t\t\tthis.centroidY[i] = leaf.point.y\n\t\t\tthis.counts[i] = 1\n\t\t\tthis.memberLists[i] = [leaf.id]\n\t\t\tthis.minMemberIds[i] = leaf.id\n\t\t\tthis.nodes[i] = {\n\t\t\t\tid: leaf.id,\n\t\t\t\tcentroid: { x: leaf.point.x, y: leaf.point.y },\n\t\t\t\tcount: 1,\n\t\t\t\tmembers: [leaf.id],\n\t\t\t}\n\t\t}\n\t}\n\n\tfind(index: number): number {\n\t\tlet root = index\n\t\twhile (this.parent[root] !== root) {\n\t\t\troot = this.parent[root]\n\t\t}\n\t\twhile (this.parent[index] !== index) {\n\t\t\tconst next = this.parent[index]\n\t\t\tthis.parent[index] = root\n\t\t\tindex = next\n\t\t}\n\t\treturn root\n\t}\n\n\tcentroidDistance(aRoot: number, bRoot: number): number {\n\t\treturn Math.hypot(\n\t\t\tthis.centroidX[aRoot] - this.centroidX[bRoot],\n\t\t\tthis.centroidY[aRoot] - this.centroidY[bRoot]\n\t\t)\n\t}\n\n\tcentroidDelta(aRoot: number, bRoot: number): { x: number; y: number } {\n\t\treturn {\n\t\t\tx: this.centroidX[aRoot] - this.centroidX[bRoot],\n\t\t\ty: this.centroidY[aRoot] - this.centroidY[bRoot],\n\t\t}\n\t}\n\n\t/** The difference of the two clusters' mean render offsets (screen px), or null when it's\n\t * zero \u2014 including always when no offsets were passed. Null routes pricing down the\n\t * offset-unaware path. */\n\toffsetDelta(aRoot: number, bRoot: number): { x: number; y: number } | null {\n\t\tif (this.offsetX === null || this.offsetY === null) return null\n\t\tconst x = this.offsetX[aRoot] / this.counts[aRoot] - this.offsetX[bRoot] / this.counts[bRoot]\n\t\tconst y = this.offsetY[aRoot] / this.counts[aRoot] - this.offsetY[bRoot] / this.counts[bRoot]\n\t\tif (x === 0 && y === 0) return null\n\t\treturn { x, y }\n\t}\n\n\tunionBboxDiag(aRoot: number, bRoot: number): number {\n\t\tconst minX = Math.min(this.minX[aRoot], this.minX[bRoot])\n\t\tconst minY = Math.min(this.minY[aRoot], this.minY[bRoot])\n\t\tconst maxX = Math.max(this.maxX[aRoot], this.maxX[bRoot])\n\t\tconst maxY = Math.max(this.maxY[aRoot], this.maxY[bRoot])\n\t\treturn Math.hypot(maxX - minX, maxY - minY)\n\t}\n\n\tmerge(aRoot: number, bRoot: number, z: number): RawMergeEvent {\n\t\tconst leftRoot = this.minMemberIds[aRoot] < this.minMemberIds[bRoot] ? aRoot : bRoot\n\t\tconst rightRoot = leftRoot === aRoot ? bRoot : aRoot\n\t\tconst left = this.nodes[leftRoot]\n\t\tconst right = this.nodes[rightRoot]\n\t\tconst count = this.counts[leftRoot] + this.counts[rightRoot]\n\t\tconst members = mergeSortedMembers(this.memberLists[leftRoot], this.memberLists[rightRoot])\n\n\t\tconst minX = Math.min(this.minX[leftRoot], this.minX[rightRoot])\n\t\tconst minY = Math.min(this.minY[leftRoot], this.minY[rightRoot])\n\t\tconst maxX = Math.max(this.maxX[leftRoot], this.maxX[rightRoot])\n\t\tconst maxY = Math.max(this.maxY[leftRoot], this.maxY[rightRoot])\n\t\tconst centroidX =\n\t\t\t(this.counts[leftRoot] * this.centroidX[leftRoot] +\n\t\t\t\tthis.counts[rightRoot] * this.centroidX[rightRoot]) /\n\t\t\tcount\n\t\tconst centroidY =\n\t\t\t(this.counts[leftRoot] * this.centroidY[leftRoot] +\n\t\t\t\tthis.counts[rightRoot] * this.centroidY[rightRoot]) /\n\t\t\tcount\n\n\t\t// Leaf ids are assumed not to start with `cluster:`; see the step 2 contract.\n\t\tconst result: ClusterNode = {\n\t\t\tid: `cluster:${count}:${members[0]}`,\n\t\t\tcentroid: { x: centroidX, y: centroidY },\n\t\t\tcount,\n\t\t\tmembers,\n\t\t}\n\n\t\tthis.parent[rightRoot] = leftRoot\n\t\tthis.minX[leftRoot] = minX\n\t\tthis.minY[leftRoot] = minY\n\t\tthis.maxX[leftRoot] = maxX\n\t\tthis.maxY[leftRoot] = maxY\n\t\tthis.centroidX[leftRoot] = centroidX\n\t\tthis.centroidY[leftRoot] = centroidY\n\t\tthis.counts[leftRoot] = count\n\t\tthis.nodes[leftRoot] = result\n\t\tthis.memberLists[leftRoot] = members\n\t\tthis.minMemberIds[leftRoot] = members[0]\n\t\tif (this.offsetX !== null && this.offsetY !== null) {\n\t\t\tthis.offsetX[leftRoot] += this.offsetX[rightRoot]\n\t\t\tthis.offsetY[leftRoot] += this.offsetY[rightRoot]\n\t\t}\n\n\t\treturn { z, children: [left, right], result }\n\t}\n}\n\nfunction mergeSortedMembers(a: readonly string[], b: readonly string[]): string[] {\n\tconst out = new Array<string>(a.length + b.length)\n\tlet i = 0\n\tlet j = 0\n\tlet k = 0\n\twhile (i < a.length && j < b.length) {\n\t\tif (a[i] < b[j]) {\n\t\t\tout[k++] = a[i++]\n\t\t} else {\n\t\t\tout[k++] = b[j++]\n\t\t}\n\t}\n\twhile (i < a.length) out[k++] = a[i++]\n\twhile (j < b.length) out[k++] = b[j++]\n\treturn out\n}\n\ninterface HeapEntry {\n\tedgeIndex: number\n\tz: number\n}\n\nclass EdgeMaxHeap {\n\tprivate readonly items: HeapEntry[] = []\n\t// Per-edge normalized (lo, hi) id pair for the z tie-break, precomputed once so comparisons\n\t// allocate nothing. With coincident anchors every edge prices to the same z (+Infinity), so\n\t// the tie-break runs on nearly every comparison of a rebuild \u2014 allocating the pair there\n\t// churned millions of short-lived tuples.\n\tprivate readonly loIds: string[]\n\tprivate readonly hiIds: string[]\n\n\tconstructor(edges: readonly MstEdge[], leaves: readonly LeafInput[]) {\n\t\tconst n = edges.length\n\t\tthis.loIds = new Array(n)\n\t\tthis.hiIds = new Array(n)\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst aId = leaves[edges[i].a].id\n\t\t\tconst bId = leaves[edges[i].b].id\n\t\t\tif (aId < bId) {\n\t\t\t\tthis.loIds[i] = aId\n\t\t\t\tthis.hiIds[i] = bId\n\t\t\t} else {\n\t\t\t\tthis.loIds[i] = bId\n\t\t\t\tthis.hiIds[i] = aId\n\t\t\t}\n\t\t}\n\t}\n\n\tpeek(): HeapEntry | undefined {\n\t\treturn this.items[0]\n\t}\n\n\tpush(entry: HeapEntry) {\n\t\tthis.items.push(entry)\n\t\tthis.siftUp(this.items.length - 1)\n\t}\n\n\tpop(): HeapEntry | undefined {\n\t\tif (this.items.length === 0) return undefined\n\t\tconst first = this.items[0]\n\t\tconst last = this.items.pop()!\n\t\tif (this.items.length > 0) {\n\t\t\tthis.items[0] = last\n\t\t\tthis.siftDown(0)\n\t\t}\n\t\treturn first\n\t}\n\n\thigherPriority(a: HeapEntry, b: HeapEntry): boolean {\n\t\tif (a.z > b.z) return true\n\t\tif (a.z < b.z) return false\n\t\t// Tie-break on the normalized leaf-id pair, ascending.\n\t\tconst aLo = this.loIds[a.edgeIndex]\n\t\tconst bLo = this.loIds[b.edgeIndex]\n\t\tif (aLo !== bLo) return aLo < bLo\n\t\treturn this.hiIds[a.edgeIndex] < this.hiIds[b.edgeIndex]\n\t}\n\n\tprivate siftUp(index: number) {\n\t\twhile (index > 0) {\n\t\t\tconst parent = (index - 1) >> 1\n\t\t\tif (!this.higherPriority(this.items[index], this.items[parent])) break\n\t\t\t;[this.items[index], this.items[parent]] = [this.items[parent], this.items[index]]\n\t\t\tindex = parent\n\t\t}\n\t}\n\n\tprivate siftDown(index: number) {\n\t\twhile (true) {\n\t\t\tconst left = index * 2 + 1\n\t\t\tconst right = left + 1\n\t\t\tlet best = index\n\t\t\tif (left < this.items.length && this.higherPriority(this.items[left], this.items[best])) {\n\t\t\t\tbest = left\n\t\t\t}\n\t\t\tif (right < this.items.length && this.higherPriority(this.items[right], this.items[best])) {\n\t\t\t\tbest = right\n\t\t\t}\n\t\t\tif (best === index) break\n\t\t\t;[this.items[index], this.items[best]] = [this.items[best], this.items[index]]\n\t\t\tindex = best\n\t\t}\n\t}\n}\n"],
|
|
5
|
-
"mappings": "AAEO,MAAM,UAAU;AAEhB,SAAS,aACf,QACA,OACA,MAGA,eACkB;AAClB,kBAAgB,IAAI;AAEpB,MAAI,MAAM,WAAW,EAAG,QAAO,CAAC;AAEhC,QAAM,WAAW,IAAI,aAAa,QAAQ,aAAa;AACvD,QAAM,OAAO,IAAI,YAAY,OAAO,MAAM;AAK1C,QAAM,WAAuB,OAAO,IAAI,MAAM,CAAC,CAAC;AAChD,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACtC,SAAK,KAAK,EAAE,WAAW,GAAG,GAAG,SAAS,MAAM,CAAC,GAAG,UAAU,IAAI,EAAE,CAAC;AACjE,aAAS,MAAM,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC;AAC3B,aAAS,MAAM,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC;AAAA,EAC5B;AAEA,QAAM,SAA0B,CAAC;AAIjC,MAAI,QAAQ,OAAO;AACnB,SAAO,OAAO,SAAS,MAAM,QAAQ;AACpC,UAAM,QAAQ,KAAK,IAAI;AACvB,QAAI,CAAC,MAAO;AAEZ,UAAM,OAAO,MAAM,MAAM,SAAS;AAClC,UAAM,QAAQ,SAAS,KAAK,KAAK,CAAC;AAClC,UAAM,QAAQ,SAAS,KAAK,KAAK,CAAC;AAClC,QAAI,UAAU,MAA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AAAA,EACA;AAAA,EACA;AAAA
|
|
4
|
+
"sourcesContent": ["import type { ClusterNode, LeafInput, LeafScreenOffsets, MstEdge, RawMergeEvent } from './types'\n\nexport const D_FLOOR = 1e-9\n\nexport function cappedReplay(\n\tleaves: readonly LeafInput[],\n\tedges: readonly MstEdge[],\n\topts: { Tc: number; Dmax: number },\n\t// Render offsets for markers that draw off their anchor (imprecise pins). Omitted or empty,\n\t// every code path below is the offset-unaware original \u2014 pricing, events, and floats alike.\n\tscreenOffsets?: LeafScreenOffsets\n): RawMergeEvent[] {\n\tvalidateOptions(opts)\n\n\tif (edges.length === 0) return []\n\n\tconst clusters = new ClusterState(leaves, screenOffsets)\n\tconst heap = new EdgeMaxHeap(edges, leaves)\n\t// Edges incident to each current cluster root, for eager repricing: a merge\n\t// moves the result's centroid, which can RAISE an incident edge's key (the\n\t// centroid may move toward a neighbor), so the lazy pop-time recheck alone\n\t// is not enough \u2014 every price change pushes a fresh heap entry.\n\tconst incident: number[][] = leaves.map(() => [])\n\tfor (let i = 0; i < edges.length; i++) {\n\t\theap.push({ edgeIndex: i, z: zForEdge(edges[i], clusters, opts) })\n\t\tincident[edges[i].a].push(i)\n\t\tincident[edges[i].b].push(i)\n\t}\n\n\tconst events: RawMergeEvent[] = []\n\t// Emitted thresholds are clamped non-increasing: with centroid pricing a\n\t// later merge's raw price can in principle exceed an earlier one's, and the\n\t// table's descending sort is load-bearing for the runtime cursor.\n\tlet lastZ = Number.POSITIVE_INFINITY\n\twhile (events.length < edges.length) {\n\t\tconst entry = heap.pop()\n\t\tif (!entry) break\n\n\t\tconst edge = edges[entry.edgeIndex]\n\t\tconst aRoot = clusters.find(edge.a)\n\t\tconst bRoot = clusters.find(edge.b)\n\t\tif (aRoot === bRoot) continue // stale duplicate of an already-fired edge\n\n\t\tconst current = { edgeIndex: entry.edgeIndex, z: zForRoots(edge, aRoot, bRoot, clusters, opts) }\n\t\tconst next = heap.peek()\n\t\tif (next && heap.higherPriority(next, current)) {\n\t\t\theap.push(current)\n\t\t\tcontinue\n\t\t}\n\n\t\tconst z = Math.min(current.z, lastZ)\n\t\tlastZ = z\n\t\tevents.push(clusters.merge(aRoot, bRoot, z))\n\n\t\t// eager reprice: the new cluster's centroid and bbox changed, so every\n\t\t// surviving incident edge gets a fresh entry at its current price\n\t\tconst root = clusters.find(edge.a)\n\t\tconst other = root === aRoot ? bRoot : aRoot\n\t\tconst survivors: number[] = []\n\t\tfor (const idx of [...incident[root], ...incident[other]]) {\n\t\t\tconst e2 = edges[idx]\n\t\t\tconst ra = clusters.find(e2.a)\n\t\t\tconst rb = clusters.find(e2.b)\n\t\t\tif (ra === rb) continue\n\t\t\tsurvivors.push(idx)\n\t\t\theap.push({ edgeIndex: idx, z: zForRoots(e2, ra, rb, clusters, opts) })\n\t\t}\n\t\tincident[root] = survivors\n\t}\n\n\treturn events\n}\n\nfunction validateOptions(opts: { Tc: number; Dmax: number }) {\n\tif (!Number.isFinite(opts.Tc) || opts.Tc <= 0) {\n\t\tthrow new Error('Tc must be greater than 0')\n\t}\n\tif (!Number.isFinite(opts.Dmax) || opts.Dmax <= 0) {\n\t\tthrow new Error('Dmax must be greater than 0')\n\t}\n\tif (opts.Dmax < opts.Tc) {\n\t\tthrow new Error('Dmax must be greater than or equal to Tc')\n\t}\n}\n\nfunction zForEdge(\n\tedge: MstEdge,\n\tclusters: ClusterState,\n\topts: { Tc: number; Dmax: number }\n): number {\n\treturn zForRoots(edge, clusters.find(edge.a), clusters.find(edge.b), clusters, opts)\n}\n\nfunction zForRoots(\n\tedge: MstEdge,\n\taRoot: number,\n\tbRoot: number,\n\tclusters: ClusterState,\n\topts: { Tc: number; Dmax: number }\n): number {\n\t// Null unless offset pricing is active AND this pair's mean render offsets differ. Every\n\t// other pair \u2014 including every pair when no offsets were passed \u2014 takes the branch below,\n\t// which is the offset-unaware pricing verbatim.\n\tconst off = clusters.offsetDelta(aRoot, bRoot)\n\tif (off === null) {\n\t\tif (edge.d < D_FLOOR) return Number.POSITIVE_INFINITY\n\t\t// Badge-anchored gap pricing: clusters render as badges at their centroids,\n\t\t// so the merge is priced by the distance between the rendered centers, not\n\t\t// the nearest members. For leaves the two are identical; for clusters the\n\t\t// centroid distance is larger, so groups merge later than their closest\n\t\t// members would suggest \u2014 matching what the user actually sees.\n\t\t// (Coincident CENTROIDS with non-coincident members leave the gap term\n\t\t// Infinity and the fit term finite \u2014 the min stays finite, no special case.)\n\t\tconst gap = opts.Tc / clusters.centroidDistance(aRoot, bRoot)\n\t\tconst fit = opts.Dmax / clusters.unionBboxDiag(aRoot, bRoot)\n\t\treturn Math.min(gap, fit)\n\t}\n\n\t// Offset-aware gap pricing: these markers render at `z\u00B7centroid + meanOffset`, so their\n\t// visual distance at zoom z is |z\u00B7\u0394C + \u0394\u014D| and the merge prices at its Tc crossing. When\n\t// that distance never reaches Tc (the constant offsets hold the visuals apart harder than\n\t// the anchors close), the pair prices at 0 \u2014 visually never mergeable, pruned by finalize's\n\t// minZoom cut. No D_FLOOR fast path here: coincident anchors with differing offsets are a\n\t// constant |\u0394\u014D| apart on screen, which is exactly the \u0394C = 0 case below.\n\tconst dc = clusters.centroidDelta(aRoot, bRoot)\n\tif (dc.x === 0 && dc.y === 0) {\n\t\treturn Math.hypot(off.x, off.y) < opts.Tc ? Number.POSITIVE_INFINITY : 0\n\t}\n\tconst gap = largestVisualCrossing(dc.x, dc.y, off.x, off.y, opts.Tc)\n\tif (gap === null) return 0\n\tconst fit = opts.Dmax / clusters.unionBboxDiag(aRoot, bRoot)\n\treturn Math.min(gap, fit)\n}\n\n/**\n * The largest positive root of `|z\u00B7\u0394C + \u0394\u014D| = level`, i.e. of\n * `|\u0394C|\u00B2\u00B7z\u00B2 + 2(\u0394C\u00B7\u0394\u014D)\u00B7z + (|\u0394\u014D|\u00B2 \u2212 level\u00B2) = 0` \u2014 the zoom at which two offset markers are\n * exactly `level` screen px apart, with the distance below the level for every smaller zoom\n * (matching the \"merged at z \u2264 threshold\" model; a lower second crossing, where opposed offsets\n * push the visuals back above the level near z = 0, is deliberately collapsed). Null when the\n * visual distance never reaches the level. Callers handle \u0394C = 0 (constant distance) themselves.\n */\nfunction largestVisualCrossing(\n\tdcx: number,\n\tdcy: number,\n\tdox: number,\n\tdoy: number,\n\tlevel: number\n): number | null {\n\tconst a = dcx * dcx + dcy * dcy\n\tconst b = 2 * (dcx * dox + dcy * doy)\n\tconst c = dox * dox + doy * doy - level * level\n\tconst disc = b * b - 4 * a * c\n\tif (disc < 0) return null\n\tconst z = (-b + Math.sqrt(disc)) / (2 * a)\n\treturn z > 0 ? z : null\n}\n\nclass ClusterState {\n\tprivate readonly parent: Int32Array\n\tprivate readonly minX: Float64Array\n\tprivate readonly minY: Float64Array\n\tprivate readonly maxX: Float64Array\n\tprivate readonly maxY: Float64Array\n\tprivate readonly centroidX: Float64Array\n\tprivate readonly centroidY: Float64Array\n\tprivate readonly counts: Int32Array\n\tprivate readonly nodes: ClusterNode[]\n\t// Per-cluster sums of member render offsets (screen px), maintained like the centroid sums.\n\t// Null when no offsets were passed \u2014 offsetDelta() then answers null unconditionally, which\n\t// routes every pricing call down the offset-unaware path.\n\tprivate readonly offsetX: Float64Array | null\n\tprivate readonly offsetY: Float64Array | null\n\n\tconstructor(leaves: readonly LeafInput[], screenOffsets?: LeafScreenOffsets) {\n\t\tconst n = leaves.length\n\t\tthis.parent = new Int32Array(n)\n\t\tthis.minX = new Float64Array(n)\n\t\tthis.minY = new Float64Array(n)\n\t\tthis.maxX = new Float64Array(n)\n\t\tthis.maxY = new Float64Array(n)\n\t\tthis.centroidX = new Float64Array(n)\n\t\tthis.centroidY = new Float64Array(n)\n\t\tthis.counts = new Int32Array(n)\n\t\tthis.nodes = new Array(n)\n\n\t\tif (screenOffsets !== undefined && screenOffsets.size > 0) {\n\t\t\tthis.offsetX = new Float64Array(n)\n\t\t\tthis.offsetY = new Float64Array(n)\n\t\t\tfor (let i = 0; i < n; i++) {\n\t\t\t\tconst offset = screenOffsets.get(leaves[i].id)\n\t\t\t\tif (offset) {\n\t\t\t\t\tthis.offsetX[i] = offset.x\n\t\t\t\t\tthis.offsetY[i] = offset.y\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tthis.offsetX = null\n\t\t\tthis.offsetY = null\n\t\t}\n\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst leaf = leaves[i]\n\t\t\tthis.parent[i] = i\n\t\t\tthis.minX[i] = leaf.point.x\n\t\t\tthis.minY[i] = leaf.point.y\n\t\t\tthis.maxX[i] = leaf.point.x\n\t\t\tthis.maxY[i] = leaf.point.y\n\t\t\tthis.centroidX[i] = leaf.point.x\n\t\t\tthis.centroidY[i] = leaf.point.y\n\t\t\tthis.counts[i] = 1\n\t\t\tthis.nodes[i] = {\n\t\t\t\tid: leaf.id,\n\t\t\t\tcentroid: { x: leaf.point.x, y: leaf.point.y },\n\t\t\t\tcount: 1,\n\t\t\t\tmembers: [leaf.id],\n\t\t\t}\n\t\t}\n\t}\n\n\tfind(index: number): number {\n\t\tlet root = index\n\t\twhile (this.parent[root] !== root) {\n\t\t\troot = this.parent[root]\n\t\t}\n\t\twhile (this.parent[index] !== index) {\n\t\t\tconst next = this.parent[index]\n\t\t\tthis.parent[index] = root\n\t\t\tindex = next\n\t\t}\n\t\treturn root\n\t}\n\n\t/** Allocation-free: this is the hot path when no offsets are passed. */\n\tcentroidDistance(aRoot: number, bRoot: number): number {\n\t\treturn Math.hypot(\n\t\t\tthis.centroidX[aRoot] - this.centroidX[bRoot],\n\t\t\tthis.centroidY[aRoot] - this.centroidY[bRoot]\n\t\t)\n\t}\n\n\tcentroidDelta(aRoot: number, bRoot: number): { x: number; y: number } {\n\t\treturn {\n\t\t\tx: this.centroidX[aRoot] - this.centroidX[bRoot],\n\t\t\ty: this.centroidY[aRoot] - this.centroidY[bRoot],\n\t\t}\n\t}\n\n\t/** The difference of the two clusters' mean render offsets (screen px), or null when it's\n\t * zero \u2014 including always when no offsets were passed. Null routes pricing down the\n\t * offset-unaware path. */\n\toffsetDelta(aRoot: number, bRoot: number): { x: number; y: number } | null {\n\t\tif (this.offsetX === null || this.offsetY === null) return null\n\t\tconst x = this.offsetX[aRoot] / this.counts[aRoot] - this.offsetX[bRoot] / this.counts[bRoot]\n\t\tconst y = this.offsetY[aRoot] / this.counts[aRoot] - this.offsetY[bRoot] / this.counts[bRoot]\n\t\tif (x === 0 && y === 0) return null\n\t\treturn { x, y }\n\t}\n\n\tunionBboxDiag(aRoot: number, bRoot: number): number {\n\t\tconst minX = Math.min(this.minX[aRoot], this.minX[bRoot])\n\t\tconst minY = Math.min(this.minY[aRoot], this.minY[bRoot])\n\t\tconst maxX = Math.max(this.maxX[aRoot], this.maxX[bRoot])\n\t\tconst maxY = Math.max(this.maxY[aRoot], this.maxY[bRoot])\n\t\treturn Math.hypot(maxX - minX, maxY - minY)\n\t}\n\n\tmerge(aRoot: number, bRoot: number, z: number): RawMergeEvent {\n\t\tconst leftRoot = this.nodes[aRoot].members[0] < this.nodes[bRoot].members[0] ? aRoot : bRoot\n\t\tconst rightRoot = leftRoot === aRoot ? bRoot : aRoot\n\t\tconst left = this.nodes[leftRoot]\n\t\tconst right = this.nodes[rightRoot]\n\t\tconst count = this.counts[leftRoot] + this.counts[rightRoot]\n\t\tconst members = mergeSortedMembers(left.members, right.members)\n\n\t\tconst minX = Math.min(this.minX[leftRoot], this.minX[rightRoot])\n\t\tconst minY = Math.min(this.minY[leftRoot], this.minY[rightRoot])\n\t\tconst maxX = Math.max(this.maxX[leftRoot], this.maxX[rightRoot])\n\t\tconst maxY = Math.max(this.maxY[leftRoot], this.maxY[rightRoot])\n\t\tconst centroidX =\n\t\t\t(this.counts[leftRoot] * this.centroidX[leftRoot] +\n\t\t\t\tthis.counts[rightRoot] * this.centroidX[rightRoot]) /\n\t\t\tcount\n\t\tconst centroidY =\n\t\t\t(this.counts[leftRoot] * this.centroidY[leftRoot] +\n\t\t\t\tthis.counts[rightRoot] * this.centroidY[rightRoot]) /\n\t\t\tcount\n\n\t\t// Leaf ids are assumed not to start with `cluster:`; see the step 2 contract.\n\t\tconst result: ClusterNode = {\n\t\t\tid: `cluster:${count}:${members[0]}`,\n\t\t\tcentroid: { x: centroidX, y: centroidY },\n\t\t\tcount,\n\t\t\tmembers,\n\t\t}\n\n\t\tthis.parent[rightRoot] = leftRoot\n\t\tthis.minX[leftRoot] = minX\n\t\tthis.minY[leftRoot] = minY\n\t\tthis.maxX[leftRoot] = maxX\n\t\tthis.maxY[leftRoot] = maxY\n\t\tthis.centroidX[leftRoot] = centroidX\n\t\tthis.centroidY[leftRoot] = centroidY\n\t\tthis.counts[leftRoot] = count\n\t\tthis.nodes[leftRoot] = result\n\t\tif (this.offsetX !== null && this.offsetY !== null) {\n\t\t\tthis.offsetX[leftRoot] += this.offsetX[rightRoot]\n\t\t\tthis.offsetY[leftRoot] += this.offsetY[rightRoot]\n\t\t}\n\n\t\treturn { z, children: [left, right], result }\n\t}\n}\n\nfunction mergeSortedMembers(a: readonly string[], b: readonly string[]): string[] {\n\tconst out = new Array<string>(a.length + b.length)\n\tlet i = 0\n\tlet j = 0\n\tlet k = 0\n\twhile (i < a.length && j < b.length) {\n\t\tif (a[i] < b[j]) {\n\t\t\tout[k++] = a[i++]\n\t\t} else {\n\t\t\tout[k++] = b[j++]\n\t\t}\n\t}\n\twhile (i < a.length) out[k++] = a[i++]\n\twhile (j < b.length) out[k++] = b[j++]\n\treturn out\n}\n\ninterface HeapEntry {\n\tedgeIndex: number\n\tz: number\n}\n\nclass EdgeMaxHeap {\n\tprivate readonly items: HeapEntry[] = []\n\t// Per-edge (lo, hi) id pair for the z tie-break, precomputed once so comparisons allocate\n\t// nothing. Relies on MstEdge's invariant that leaves[a].id < leaves[b].id; the heap does not\n\t// re-normalize. With coincident anchors every edge prices to the same z (+Infinity), so the\n\t// tie-break runs on nearly every comparison of a rebuild: allocating the pair there churned\n\t// millions of short-lived tuples.\n\tprivate readonly loIds: string[]\n\tprivate readonly hiIds: string[]\n\n\tconstructor(edges: readonly MstEdge[], leaves: readonly LeafInput[]) {\n\t\tconst n = edges.length\n\t\tthis.loIds = new Array(n)\n\t\tthis.hiIds = new Array(n)\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tthis.loIds[i] = leaves[edges[i].a].id\n\t\t\tthis.hiIds[i] = leaves[edges[i].b].id\n\t\t}\n\t}\n\n\tpeek(): HeapEntry | undefined {\n\t\treturn this.items[0]\n\t}\n\n\tpush(entry: HeapEntry) {\n\t\tthis.items.push(entry)\n\t\tthis.siftUp(this.items.length - 1)\n\t}\n\n\tpop(): HeapEntry | undefined {\n\t\tif (this.items.length === 0) return undefined\n\t\tconst first = this.items[0]\n\t\tconst last = this.items.pop()!\n\t\tif (this.items.length > 0) {\n\t\t\tthis.items[0] = last\n\t\t\tthis.siftDown(0)\n\t\t}\n\t\treturn first\n\t}\n\n\thigherPriority(a: HeapEntry, b: HeapEntry): boolean {\n\t\tif (a.z > b.z) return true\n\t\tif (a.z < b.z) return false\n\t\t// Tie-break on the normalized leaf-id pair, ascending.\n\t\tconst aLo = this.loIds[a.edgeIndex]\n\t\tconst bLo = this.loIds[b.edgeIndex]\n\t\tif (aLo !== bLo) return aLo < bLo\n\t\treturn this.hiIds[a.edgeIndex] < this.hiIds[b.edgeIndex]\n\t}\n\n\tprivate siftUp(index: number) {\n\t\twhile (index > 0) {\n\t\t\tconst parent = (index - 1) >> 1\n\t\t\tif (!this.higherPriority(this.items[index], this.items[parent])) break\n\t\t\t;[this.items[index], this.items[parent]] = [this.items[parent], this.items[index]]\n\t\t\tindex = parent\n\t\t}\n\t}\n\n\tprivate siftDown(index: number) {\n\t\twhile (true) {\n\t\t\tconst left = index * 2 + 1\n\t\t\tconst right = left + 1\n\t\t\tlet best = index\n\t\t\tif (left < this.items.length && this.higherPriority(this.items[left], this.items[best])) {\n\t\t\t\tbest = left\n\t\t\t}\n\t\t\tif (right < this.items.length && this.higherPriority(this.items[right], this.items[best])) {\n\t\t\t\tbest = right\n\t\t\t}\n\t\t\tif (best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M,MAAM,GAAG,KAAK,MAAM,KAAK,CAAC;AACjF,cAAQ;AAAA,IACT;AAAA,EACD;AAAA,EAEQ,SAAS,OAAe;AAC/B,WAAO,MAAM;AACZ,YAAM,OAAO,QAAQ,IAAI;AACzB,YAAM,QAAQ,OAAO;AACrB,UAAI,OAAO;AACX,UAAI,OAAO,KAAK,MAAM,UAAU,KAAK,eAAe,KAAK,MAAM,IAAI,GAAG,KAAK,MAAM,IAAI,CAAC,GAAG;AACxF,eAAO;AAAA,MACR;AACA,UAAI,QAAQ,KAAK,MAAM,UAAU,KAAK,eAAe,KAAK,MAAM,KAAK,GAAG,KAAK,MAAM,IAAI,CAAC,GAAG;AAC1F,eAAO;AAAA,MACR;AACA,UAAI,SAAS,MAAO;AACnB,OAAC,KAAK,MAAM,KAAK,GAAG,KAAK,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,MAAM,IAAI,GAAG,KAAK,MAAM,KAAK,CAAC;AAC7E,cAAQ;AAAA,IACT;AAAA,EACD;AACD;",
|
|
6
6
|
"names": ["gap", "fit"]
|
|
7
7
|
}
|
|
@@ -4,7 +4,7 @@ function createClusterRuntime(table) {
|
|
|
4
4
|
class ClusterRuntimeImpl {
|
|
5
5
|
constructor(table) {
|
|
6
6
|
this.table = table;
|
|
7
|
-
this.
|
|
7
|
+
this.reset();
|
|
8
8
|
}
|
|
9
9
|
table;
|
|
10
10
|
// mutable internally; readonly through the ClusterRuntime interface
|
|
@@ -35,10 +35,7 @@ class ClusterRuntimeImpl {
|
|
|
35
35
|
seed(zoom) {
|
|
36
36
|
validateZoom(zoom);
|
|
37
37
|
this.k = seedCount(this.table.events, zoom);
|
|
38
|
-
this.
|
|
39
|
-
this.detached.clear();
|
|
40
|
-
this.patched.clear();
|
|
41
|
-
this.resetVisible();
|
|
38
|
+
this.reset();
|
|
42
39
|
for (let i = 0; i < this.k; i++) {
|
|
43
40
|
applyEvent(this.visible, this.table.events[i]);
|
|
44
41
|
}
|
|
@@ -59,10 +56,7 @@ class ClusterRuntimeImpl {
|
|
|
59
56
|
k++;
|
|
60
57
|
}
|
|
61
58
|
this.k = k;
|
|
62
|
-
this.
|
|
63
|
-
this.detached.clear();
|
|
64
|
-
this.patched.clear();
|
|
65
|
-
this.resetVisible();
|
|
59
|
+
this.reset();
|
|
66
60
|
for (let i = 0; i < k; i++) {
|
|
67
61
|
const event = events[i];
|
|
68
62
|
if (zoom > event.zMerge && !wasMergedTogether(event.result.members, ownerByMember)) {
|
|
@@ -152,8 +146,8 @@ class ClusterRuntimeImpl {
|
|
|
152
146
|
this.patched.set(id, null);
|
|
153
147
|
}
|
|
154
148
|
const patch = (node) => {
|
|
155
|
-
if (node.members.length === 1 || !node.members.some((m) => this.detached.has(m))) return;
|
|
156
149
|
const members = node.members.filter((m) => !this.detached.has(m));
|
|
150
|
+
if (members.length === node.members.length) return;
|
|
157
151
|
if (members.length === 0) {
|
|
158
152
|
this.patched.set(node.id, null);
|
|
159
153
|
} else if (members.length === 1) {
|
|
@@ -179,11 +173,11 @@ class ClusterRuntimeImpl {
|
|
|
179
173
|
for (const child of event.children) patch(child);
|
|
180
174
|
}
|
|
181
175
|
}
|
|
182
|
-
|
|
183
|
-
this.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
176
|
+
reset() {
|
|
177
|
+
this.suppressed.clear();
|
|
178
|
+
this.detached.clear();
|
|
179
|
+
this.patched.clear();
|
|
180
|
+
this.visible = new Map(this.table.leaves.map((leaf) => [leaf.id, leaf]));
|
|
187
181
|
}
|
|
188
182
|
}
|
|
189
183
|
function wasMergedTogether(members, ownerByMember) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/clustering/runtime.ts"],
|
|
4
|
-
"sourcesContent": ["import type { ClusterNode, ClusterTable, MergeEvent } from './types'\n\n/** @internal */\nexport interface ClusterRuntime {\n\t/** Events[0..k) are in the cursor (applied unless suppressed). Exposed for tests/debugging. */\n\treadonly k: number\n\t/**\n\t * Increments whenever the displayed partition changes. Subscribe to this, not `k`: detaches\n\t * and suppressed-event healing change the partition without moving the cursor.\n\t */\n\treadonly version: number\n\t/** Number of suppressed band events currently inside the cursor. Exposed for tests. */\n\tgetSuppressedCount(): number\n\t/** Number of leaves currently detached from the displayed partition. Exposed for tests. */\n\tgetDetachedCount(): number\n\t/** Reset state from scratch for the given zoom (cold start / after rebuild). Clears detaches. */\n\tseed(zoom: number): void\n\t/**\n\t * Reset state for the given zoom, carrying hysteresis state over from a previous partition.\n\t * Threshold-forced events ignore history: zoom \\<= zMerge is always merged, zoom \\>= zSplit always\n\t * split. An event inside its band keeps its previous state; one that was unmerged (or has no\n\t * history) stays suppressed inside the cursor until a zoom-out crosses its own zMerge. Carryover is\n\t * exact \u2014 no group changes state because of the swap alone. Clears detaches.\n\t */\n\tseedFrom(zoom: number, previous: ReadonlyMap<string, ClusterNode>): void\n\t/** Advance/retreat the cursor for a camera change. No-op if zoom sits inside all bands. */\n\tonCamera(zoom: number): void\n\t/**\n\t * Remove one leaf from the displayed partition without touching the event table. Local by\n\t * construction: only the nodes containing the leaf change, so a deletion, pop-out, or thread-open\n\t * never re-flows the rest of the document. The table's thresholds around the detached leaf go\n\t * stale; the caller is expected to adopt a corrected rebuild (with seedFrom) at the next zoom-out.\n\t * Unknown or already-detached ids are no-ops.\n\t */\n\tdetachLeaf(leafId: string): void\n\t/**\n\t * Batch form of {@link ClusterRuntime.detachLeaf}: detach every given leaf with a single\n\t * patch rebuild and a single version bump. Ids that are unknown or already detached are\n\t * skipped; if nothing new detaches, nothing changes.\n\t */\n\tdetachLeaves(leafIds: Iterable<string>): void\n\t/** The displayed partition: cluster id \u2192 node, with detaches applied. Do not mutate. */\n\tgetVisible(): ReadonlyMap<string, ClusterNode>\n}\n\n/** @internal */\nexport function createClusterRuntime(table: ClusterTable): ClusterRuntime {\n\treturn new ClusterRuntimeImpl(table)\n}\n\nclass ClusterRuntimeImpl implements ClusterRuntime {\n\t// mutable internally; readonly through the ClusterRuntime interface\n\tk = 0\n\tversion = 0\n\t// Structural partition: exactly leaves + applied events, never patched. The cursor invariants\n\t// live here, untouched by detaches \u2014 resolution to the displayed partition happens at read\n\t// time in getVisible().\n\tprivate visible = new Map<string, ClusterNode>()\n\tprivate seeded = false\n\t// Indices (< k) of band events held unmerged by seedFrom carryover. The single cursor can only\n\t// express \"merged up to here\", but a carried-over partition can be \"merged except these\". Self-\n\t// draining: an entry leaves via onCamera when the zoom crosses its zMerge or its zSplit.\n\tprivate suppressed = new Set<number>()\n\t// Leaves removed from the displayed partition (deleted / popped out / opened). Patches map\n\t// each structural node containing a detached leaf to its displayed replacement (or null to\n\t// drop it). Rebuilt from the detached set; cleared by seed/seedFrom.\n\tprivate detached = new Set<string>()\n\tprivate patched = new Map<string, ClusterNode | null>()\n\tprivate leafById: Map<string, ClusterNode> | null = null\n\tprivate resolvedCache: { version: number; map: Map<string, ClusterNode> } | null = null\n\n\tconstructor(private readonly table: ClusterTable) {\n\t\tthis.resetVisible()\n\t}\n\n\tgetSuppressedCount(): number {\n\t\treturn this.suppressed.size\n\t}\n\n\tgetDetachedCount(): number {\n\t\treturn this.detached.size\n\t}\n\n\tseed(zoom: number): void {\n\t\tvalidateZoom(zoom)\n\t\tthis.k = seedCount(this.table.events, zoom)\n\t\tthis.suppressed.clear()\n\t\tthis.detached.clear()\n\t\tthis.patched.clear()\n\t\tthis.resetVisible()\n\t\tfor (let i = 0; i < this.k; i++) {\n\t\t\tapplyEvent(this.visible, this.table.events[i])\n\t\t}\n\t\tthis.seeded = true\n\t\tthis.version++\n\t}\n\n\tseedFrom(zoom: number, previous: ReadonlyMap<string, ClusterNode>): void {\n\t\tvalidateZoom(zoom)\n\t\tconst ownerByMember = new Map<string, string>()\n\t\tfor (const node of previous.values()) {\n\t\t\tfor (const member of node.members) {\n\t\t\t\townerByMember.set(member, node.id)\n\t\t\t}\n\t\t}\n\n\t\t// Cut the cursor purely on split thresholds. zSplit is non-increasing down the table, so\n\t\t// everything past the first mandatory split (zoom >= zSplit) is also mandatorily split \u2014\n\t\t// this cut can never conflict with an event that should be merged.\n\t\tconst events = this.table.events\n\t\tlet k = 0\n\t\twhile (k < events.length && zoom < events[k].zSplit) {\n\t\t\tk++\n\t\t}\n\n\t\tthis.k = k\n\t\tthis.suppressed.clear()\n\t\tthis.detached.clear()\n\t\tthis.patched.clear()\n\t\tthis.resetVisible()\n\t\tfor (let i = 0; i < k; i++) {\n\t\t\tconst event = events[i]\n\t\t\tif (zoom > event.zMerge && !wasMergedTogether(event.result.members, ownerByMember)) {\n\t\t\t\t// In its band and previously unmerged: keep it unmerged, as an exception inside the cursor.\n\t\t\t\t// Dependency-safe, since an applied event can never consume a suppressed result.\n\t\t\t\tthis.suppressed.add(i)\n\t\t\t} else {\n\t\t\t\tapplyEvent(this.visible, event)\n\t\t\t}\n\t\t}\n\t\tthis.seeded = true\n\t\tthis.version++\n\t}\n\n\tonCamera(zoom: number): void {\n\t\tvalidateZoom(zoom)\n\t\tif (!this.seeded) {\n\t\t\tthrow new Error('Cluster runtime must be seeded before onCamera')\n\t\t}\n\n\t\tlet changed = false\n\t\t// Heal suppressed events at their own merge threshold BEFORE the merge walk, so a zoom-out past\n\t\t// zMerge merges a held-out band event as if it had still been ahead of the cursor. Healing must come\n\t\t// first: one zoom jump can cross both a suppressed event and an event consuming its result, and\n\t\t// applying the consumer first would double-count the producer's leaves. Iterated live \u2014 deleting only\n\t\t// the entry being visited is safe, and Set iteration still yields the rest in insertion order.\n\t\tif (this.suppressed.size > 0) {\n\t\t\tfor (const i of this.suppressed) {\n\t\t\t\tif (zoom <= this.table.events[i].zMerge) {\n\t\t\t\t\tthis.suppressed.delete(i)\n\t\t\t\t\tapplyEvent(this.visible, this.table.events[i])\n\t\t\t\t\tchanged = true\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile (this.k < this.table.events.length && zoom <= this.table.events[this.k].zMerge) {\n\t\t\tapplyEvent(this.visible, this.table.events[this.k])\n\t\t\tthis.k++\n\t\t\tchanged = true\n\t\t}\n\t\twhile (this.k > 0 && zoom >= this.table.events[this.k - 1].zSplit) {\n\t\t\tthis.k--\n\t\t\t// A suppressed event was never applied \u2014 retreating past it is bookkeeping only.\n\t\t\tif (!this.suppressed.delete(this.k)) {\n\t\t\t\tunapplyEvent(this.visible, this.table.events[this.k])\n\t\t\t\tchanged = true\n\t\t\t}\n\t\t}\n\t\tif (changed) this.version++\n\t}\n\n\tdetachLeaf(leafId: string): void {\n\t\tthis.detachLeaves([leafId])\n\t}\n\n\tdetachLeaves(leafIds: Iterable<string>): void {\n\t\tconst leafById = this.getLeafById()\n\t\tlet changed = false\n\t\tfor (const leafId of leafIds) {\n\t\t\tif (this.detached.has(leafId)) continue\n\t\t\tif (!leafById.has(leafId)) continue\n\t\t\tthis.detached.add(leafId)\n\t\t\tchanged = true\n\t\t}\n\t\tif (!changed) return\n\t\tthis.rebuildPatches()\n\t\tthis.version++\n\t}\n\n\tgetVisible(): ReadonlyMap<string, ClusterNode> {\n\t\tif (this.resolvedCache && this.resolvedCache.version === this.version) {\n\t\t\treturn this.resolvedCache.map\n\t\t}\n\t\tlet map: Map<string, ClusterNode>\n\t\tif (this.patched.size === 0) {\n\t\t\tmap = this.visible\n\t\t} else {\n\t\t\tmap = new Map()\n\t\t\tfor (const node of this.visible.values()) {\n\t\t\t\tconst resolved = this.patched.has(node.id) ? this.patched.get(node.id)! : node\n\t\t\t\tif (resolved) map.set(resolved.id, resolved)\n\t\t\t}\n\t\t}\n\t\tthis.resolvedCache = { version: this.version, map }\n\t\treturn map\n\t}\n\n\tprivate getLeafById(): Map<string, ClusterNode> {\n\t\tif (!this.leafById) {\n\t\t\tthis.leafById = new Map(this.table.leaves.map((leaf) => [leaf.id, leaf]))\n\t\t}\n\t\treturn this.leafById\n\t}\n\n\t/** Recompute the patch map from the detached set. A node is patched iff it contains a detached\n\t * member; the patch drops those members and recomputes count/centroid, collapsing to the surviving\n\t * leaf at count 1 and to nothing at count 0. Patched nodes keep their structural id. */\n\tprivate rebuildPatches() {\n\t\tthis.patched.clear()\n\t\tconst leafById = this.getLeafById()\n\t\tfor (const id of this.detached) {\n\t\t\tthis.patched.set(id, null)\n\t\t}\n\t\tconst patch = (node: ClusterNode) => {\n\t\t\tif (node.members.length === 1 || !node.members.some((m) => this.detached.has(m))) return\n\t\t\tconst members = node.members.filter((m) => !this.detached.has(m))\n\t\t\tif (members.length === 0) {\n\t\t\t\tthis.patched.set(node.id, null)\n\t\t\t} else if (members.length === 1) {\n\t\t\t\tthis.patched.set(node.id, leafById.get(members[0])!)\n\t\t\t} else {\n\t\t\t\tlet x = 0\n\t\t\t\tlet y = 0\n\t\t\t\tfor (const m of members) {\n\t\t\t\t\tconst leaf = leafById.get(m)!\n\t\t\t\t\tx += leaf.centroid.x\n\t\t\t\t\ty += leaf.centroid.y\n\t\t\t\t}\n\t\t\t\tthis.patched.set(node.id, {\n\t\t\t\t\tid: node.id,\n\t\t\t\t\tcentroid: { x: x / members.length, y: y / members.length },\n\t\t\t\t\tcount: members.length,\n\t\t\t\t\tmembers,\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t\tfor (const event of this.table.events) {\n\t\t\tpatch(event.result)\n\t\t\tfor (const child of event.children) patch(child)\n\t\t}\n\t}\n\n\tprivate resetVisible() {\n\t\tthis.visible = new Map()\n\t\tfor (const leaf of this.table.leaves) {\n\t\t\tthis.visible.set(leaf.id, leaf)\n\t\t}\n\t}\n}\n\n/** True iff every member id belonged to the same node of the previous partition. */\nfunction wasMergedTogether(\n\tmembers: readonly string[],\n\townerByMember: ReadonlyMap<string, string>\n): boolean {\n\tconst owner = ownerByMember.get(members[0])\n\tif (owner === undefined) return false\n\tfor (let i = 1; i < members.length; i++) {\n\t\tif (ownerByMember.get(members[i]) !== owner) return false\n\t}\n\treturn true\n}\n\nfunction seedCount(events: readonly MergeEvent[], zoom: number): number {\n\tlet lo = 0\n\tlet hi = events.length\n\twhile (lo < hi) {\n\t\tconst mid = (lo + hi) >> 1\n\t\tif (zoom <= seedThreshold(events[mid])) {\n\t\t\tlo = mid + 1\n\t\t} else {\n\t\t\thi = mid\n\t\t}\n\t}\n\treturn lo\n}\n\nfunction seedThreshold(event: MergeEvent): number {\n\treturn Math.sqrt(event.zMerge * event.zSplit)\n}\n\nfunction applyEvent(visible: Map<string, ClusterNode>, event: MergeEvent) {\n\tfor (const child of event.children) {\n\t\tvisible.delete(child.id)\n\t}\n\tvisible.set(event.result.id, event.result)\n}\n\nfunction unapplyEvent(visible: Map<string, ClusterNode>, event: MergeEvent) {\n\tvisible.delete(event.result.id)\n\tfor (const child of event.children) {\n\t\tvisible.set(child.id, child)\n\t}\n}\n\nfunction validateZoom(zoom: number) {\n\tif (!Number.isFinite(zoom) || zoom <= 0) {\n\t\tthrow new Error('zoom must be finite and greater than 0')\n\t}\n}\n"],
|
|
5
|
-
"mappings": "AA8CO,SAAS,qBAAqB,OAAqC;AACzE,SAAO,IAAI,mBAAmB,KAAK;AACpC;AAEA,MAAM,mBAA6C;AAAA,EAqBlD,YAA6B,OAAqB;AAArB;AAC5B,SAAK,
|
|
4
|
+
"sourcesContent": ["import type { ClusterNode, ClusterTable, MergeEvent } from './types'\n\n/** @internal */\nexport interface ClusterRuntime {\n\t/** Events[0..k) are in the cursor (applied unless suppressed). Exposed for tests/debugging. */\n\treadonly k: number\n\t/**\n\t * Increments whenever the displayed partition changes. Subscribe to this, not `k`: detaches\n\t * and suppressed-event healing change the partition without moving the cursor.\n\t */\n\treadonly version: number\n\t/** Number of suppressed band events currently inside the cursor. Exposed for tests. */\n\tgetSuppressedCount(): number\n\t/** Number of leaves currently detached from the displayed partition. Exposed for tests. */\n\tgetDetachedCount(): number\n\t/** Reset state from scratch for the given zoom (cold start / after rebuild). Clears detaches. */\n\tseed(zoom: number): void\n\t/**\n\t * Reset state for the given zoom, carrying hysteresis state over from a previous partition.\n\t * Threshold-forced events ignore history: zoom \\<= zMerge is always merged, zoom \\>= zSplit always\n\t * split. An event inside its band keeps its previous state; one that was unmerged (or has no\n\t * history) stays suppressed inside the cursor until a zoom-out crosses its own zMerge. Carryover is\n\t * exact \u2014 no group changes state because of the swap alone. Clears detaches.\n\t */\n\tseedFrom(zoom: number, previous: ReadonlyMap<string, ClusterNode>): void\n\t/** Advance/retreat the cursor for a camera change. No-op if zoom sits inside all bands. */\n\tonCamera(zoom: number): void\n\t/**\n\t * Remove one leaf from the displayed partition without touching the event table. Local by\n\t * construction: only the nodes containing the leaf change, so a deletion, pop-out, or thread-open\n\t * never re-flows the rest of the document. The table's thresholds around the detached leaf go\n\t * stale; the caller is expected to adopt a corrected rebuild (with seedFrom) at the next zoom-out.\n\t * Unknown or already-detached ids are no-ops.\n\t */\n\tdetachLeaf(leafId: string): void\n\t/**\n\t * Batch form of {@link ClusterRuntime.detachLeaf}: detach every given leaf with a single\n\t * patch rebuild and a single version bump. Ids that are unknown or already detached are\n\t * skipped; if nothing new detaches, nothing changes.\n\t */\n\tdetachLeaves(leafIds: Iterable<string>): void\n\t/** The displayed partition: cluster id \u2192 node, with detaches applied. Do not mutate. */\n\tgetVisible(): ReadonlyMap<string, ClusterNode>\n}\n\n/** @internal */\nexport function createClusterRuntime(table: ClusterTable): ClusterRuntime {\n\treturn new ClusterRuntimeImpl(table)\n}\n\nclass ClusterRuntimeImpl implements ClusterRuntime {\n\t// mutable internally; readonly through the ClusterRuntime interface\n\tk = 0\n\tversion = 0\n\t// Structural partition: exactly leaves + applied events, never patched. The cursor invariants\n\t// live here, untouched by detaches \u2014 resolution to the displayed partition happens at read\n\t// time in getVisible().\n\tprivate visible = new Map<string, ClusterNode>()\n\tprivate seeded = false\n\t// Indices (< k) of band events held unmerged by seedFrom carryover. The single cursor can only\n\t// express \"merged up to here\", but a carried-over partition can be \"merged except these\". Self-\n\t// draining: an entry leaves via onCamera when the zoom crosses its zMerge or its zSplit.\n\tprivate suppressed = new Set<number>()\n\t// Leaves removed from the displayed partition (deleted / popped out / opened). Patches map\n\t// each structural node containing a detached leaf to its displayed replacement (or null to\n\t// drop it). Rebuilt from the detached set; cleared by seed/seedFrom.\n\tprivate detached = new Set<string>()\n\tprivate patched = new Map<string, ClusterNode | null>()\n\tprivate leafById: Map<string, ClusterNode> | null = null\n\tprivate resolvedCache: { version: number; map: Map<string, ClusterNode> } | null = null\n\n\tconstructor(private readonly table: ClusterTable) {\n\t\tthis.reset()\n\t}\n\n\tgetSuppressedCount(): number {\n\t\treturn this.suppressed.size\n\t}\n\n\tgetDetachedCount(): number {\n\t\treturn this.detached.size\n\t}\n\n\tseed(zoom: number): void {\n\t\tvalidateZoom(zoom)\n\t\tthis.k = seedCount(this.table.events, zoom)\n\t\tthis.reset()\n\t\tfor (let i = 0; i < this.k; i++) {\n\t\t\tapplyEvent(this.visible, this.table.events[i])\n\t\t}\n\t\tthis.seeded = true\n\t\tthis.version++\n\t}\n\n\tseedFrom(zoom: number, previous: ReadonlyMap<string, ClusterNode>): void {\n\t\tvalidateZoom(zoom)\n\t\tconst ownerByMember = new Map<string, string>()\n\t\tfor (const node of previous.values()) {\n\t\t\tfor (const member of node.members) {\n\t\t\t\townerByMember.set(member, node.id)\n\t\t\t}\n\t\t}\n\n\t\t// Cut the cursor purely on split thresholds. zSplit is non-increasing down the table, so\n\t\t// everything past the first mandatory split (zoom >= zSplit) is also mandatorily split \u2014\n\t\t// this cut can never conflict with an event that should be merged.\n\t\tconst events = this.table.events\n\t\tlet k = 0\n\t\twhile (k < events.length && zoom < events[k].zSplit) {\n\t\t\tk++\n\t\t}\n\n\t\tthis.k = k\n\t\tthis.reset()\n\t\tfor (let i = 0; i < k; i++) {\n\t\t\tconst event = events[i]\n\t\t\tif (zoom > event.zMerge && !wasMergedTogether(event.result.members, ownerByMember)) {\n\t\t\t\t// In its band and previously unmerged: keep it unmerged, as an exception inside the cursor.\n\t\t\t\t// Dependency-safe, since an applied event can never consume a suppressed result.\n\t\t\t\tthis.suppressed.add(i)\n\t\t\t} else {\n\t\t\t\tapplyEvent(this.visible, event)\n\t\t\t}\n\t\t}\n\t\tthis.seeded = true\n\t\tthis.version++\n\t}\n\n\tonCamera(zoom: number): void {\n\t\tvalidateZoom(zoom)\n\t\tif (!this.seeded) {\n\t\t\tthrow new Error('Cluster runtime must be seeded before onCamera')\n\t\t}\n\n\t\tlet changed = false\n\t\t// Heal suppressed events at their own merge threshold BEFORE the merge walk, so a zoom-out past\n\t\t// zMerge merges a held-out band event as if it had still been ahead of the cursor. Healing must come\n\t\t// first: one zoom jump can cross both a suppressed event and an event consuming its result, and\n\t\t// applying the consumer first would double-count the producer's leaves. Iterated live \u2014 deleting only\n\t\t// the entry being visited is safe, and Set iteration still yields the rest in insertion order.\n\t\tif (this.suppressed.size > 0) {\n\t\t\tfor (const i of this.suppressed) {\n\t\t\t\tif (zoom <= this.table.events[i].zMerge) {\n\t\t\t\t\tthis.suppressed.delete(i)\n\t\t\t\t\tapplyEvent(this.visible, this.table.events[i])\n\t\t\t\t\tchanged = true\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile (this.k < this.table.events.length && zoom <= this.table.events[this.k].zMerge) {\n\t\t\tapplyEvent(this.visible, this.table.events[this.k])\n\t\t\tthis.k++\n\t\t\tchanged = true\n\t\t}\n\t\twhile (this.k > 0 && zoom >= this.table.events[this.k - 1].zSplit) {\n\t\t\tthis.k--\n\t\t\t// A suppressed event was never applied \u2014 retreating past it is bookkeeping only.\n\t\t\tif (!this.suppressed.delete(this.k)) {\n\t\t\t\tunapplyEvent(this.visible, this.table.events[this.k])\n\t\t\t\tchanged = true\n\t\t\t}\n\t\t}\n\t\tif (changed) this.version++\n\t}\n\n\tdetachLeaf(leafId: string): void {\n\t\tthis.detachLeaves([leafId])\n\t}\n\n\tdetachLeaves(leafIds: Iterable<string>): void {\n\t\tconst leafById = this.getLeafById()\n\t\tlet changed = false\n\t\tfor (const leafId of leafIds) {\n\t\t\tif (this.detached.has(leafId)) continue\n\t\t\tif (!leafById.has(leafId)) continue\n\t\t\tthis.detached.add(leafId)\n\t\t\tchanged = true\n\t\t}\n\t\tif (!changed) return\n\t\tthis.rebuildPatches()\n\t\tthis.version++\n\t}\n\n\tgetVisible(): ReadonlyMap<string, ClusterNode> {\n\t\tif (this.resolvedCache && this.resolvedCache.version === this.version) {\n\t\t\treturn this.resolvedCache.map\n\t\t}\n\t\tlet map: Map<string, ClusterNode>\n\t\tif (this.patched.size === 0) {\n\t\t\tmap = this.visible\n\t\t} else {\n\t\t\tmap = new Map()\n\t\t\tfor (const node of this.visible.values()) {\n\t\t\t\tconst resolved = this.patched.has(node.id) ? this.patched.get(node.id)! : node\n\t\t\t\tif (resolved) map.set(resolved.id, resolved)\n\t\t\t}\n\t\t}\n\t\tthis.resolvedCache = { version: this.version, map }\n\t\treturn map\n\t}\n\n\tprivate getLeafById(): Map<string, ClusterNode> {\n\t\tif (!this.leafById) {\n\t\t\tthis.leafById = new Map(this.table.leaves.map((leaf) => [leaf.id, leaf]))\n\t\t}\n\t\treturn this.leafById\n\t}\n\n\t/** Recompute the patch map from the detached set. A node is patched iff it contains a detached\n\t * member; the patch drops those members and recomputes count/centroid, collapsing to the surviving\n\t * leaf at count 1 and to nothing at count 0. Patched nodes keep their structural id. */\n\tprivate rebuildPatches() {\n\t\tthis.patched.clear()\n\t\tconst leafById = this.getLeafById()\n\t\tfor (const id of this.detached) {\n\t\t\tthis.patched.set(id, null)\n\t\t}\n\t\tconst patch = (node: ClusterNode) => {\n\t\t\tconst members = node.members.filter((m) => !this.detached.has(m))\n\t\t\tif (members.length === node.members.length) return\n\t\t\tif (members.length === 0) {\n\t\t\t\tthis.patched.set(node.id, null)\n\t\t\t} else if (members.length === 1) {\n\t\t\t\tthis.patched.set(node.id, leafById.get(members[0])!)\n\t\t\t} else {\n\t\t\t\tlet x = 0\n\t\t\t\tlet y = 0\n\t\t\t\tfor (const m of members) {\n\t\t\t\t\tconst leaf = leafById.get(m)!\n\t\t\t\t\tx += leaf.centroid.x\n\t\t\t\t\ty += leaf.centroid.y\n\t\t\t\t}\n\t\t\t\tthis.patched.set(node.id, {\n\t\t\t\t\tid: node.id,\n\t\t\t\t\tcentroid: { x: x / members.length, y: y / members.length },\n\t\t\t\t\tcount: members.length,\n\t\t\t\t\tmembers,\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t\tfor (const event of this.table.events) {\n\t\t\tpatch(event.result)\n\t\t\tfor (const child of event.children) patch(child)\n\t\t}\n\t}\n\n\tprivate reset() {\n\t\tthis.suppressed.clear()\n\t\tthis.detached.clear()\n\t\tthis.patched.clear()\n\t\tthis.visible = new Map(this.table.leaves.map((leaf) => [leaf.id, leaf]))\n\t}\n}\n\n/** True iff every member id belonged to the same node of the previous partition. */\nfunction wasMergedTogether(\n\tmembers: readonly string[],\n\townerByMember: ReadonlyMap<string, string>\n): boolean {\n\tconst owner = ownerByMember.get(members[0])\n\tif (owner === undefined) return false\n\tfor (let i = 1; i < members.length; i++) {\n\t\tif (ownerByMember.get(members[i]) !== owner) return false\n\t}\n\treturn true\n}\n\nfunction seedCount(events: readonly MergeEvent[], zoom: number): number {\n\tlet lo = 0\n\tlet hi = events.length\n\twhile (lo < hi) {\n\t\tconst mid = (lo + hi) >> 1\n\t\tif (zoom <= seedThreshold(events[mid])) {\n\t\t\tlo = mid + 1\n\t\t} else {\n\t\t\thi = mid\n\t\t}\n\t}\n\treturn lo\n}\n\nfunction seedThreshold(event: MergeEvent): number {\n\treturn Math.sqrt(event.zMerge * event.zSplit)\n}\n\nfunction applyEvent(visible: Map<string, ClusterNode>, event: MergeEvent) {\n\tfor (const child of event.children) {\n\t\tvisible.delete(child.id)\n\t}\n\tvisible.set(event.result.id, event.result)\n}\n\nfunction unapplyEvent(visible: Map<string, ClusterNode>, event: MergeEvent) {\n\tvisible.delete(event.result.id)\n\tfor (const child of event.children) {\n\t\tvisible.set(child.id, child)\n\t}\n}\n\nfunction validateZoom(zoom: number) {\n\tif (!Number.isFinite(zoom) || zoom <= 0) {\n\t\tthrow new Error('zoom must be finite and greater than 0')\n\t}\n}\n"],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AC,MAAM,MAAO,QAAO;AAAA,EACrD;AACA,SAAO;AACR;AAEA,SAAS,UAAU,QAA+B,MAAsB;AACvE,MAAI,KAAK;AACT,MAAI,KAAK,OAAO;AAChB,SAAO,KAAK,IAAI;AACf,UAAM,MAAO,KAAK,MAAO;AACzB,QAAI,QAAQ,cAAc,OAAO,GAAG,CAAC,GAAG;AACvC,WAAK,MAAM;AAAA,IACZ,OAAO;AACN,WAAK;AAAA,IACN;AAAA,EACD;AACA,SAAO;AACR;AAEA,SAAS,cAAc,OAA2B;AACjD,SAAO,KAAK,KAAK,MAAM,SAAS,MAAM,MAAM;AAC7C;AAEA,SAAS,WAAW,SAAmC,OAAmB;AACzE,aAAW,SAAS,MAAM,UAAU;AACnC,YAAQ,OAAO,MAAM,EAAE;AAAA,EACxB;AACA,UAAQ,IAAI,MAAM,OAAO,IAAI,MAAM,MAAM;AAC1C;AAEA,SAAS,aAAa,SAAmC,OAAmB;AAC3E,UAAQ,OAAO,MAAM,OAAO,EAAE;AAC9B,aAAW,SAAS,MAAM,UAAU;AACnC,YAAQ,IAAI,MAAM,IAAI,KAAK;AAAA,EAC5B;AACD;AAEA,SAAS,aAAa,MAAc;AACnC,MAAI,CAAC,OAAO,SAAS,IAAI,KAAK,QAAQ,GAAG;AACxC,UAAM,IAAI,MAAM,wCAAwC;AAAA,EACzD;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist-esm/index.mjs
CHANGED
|
@@ -66,8 +66,7 @@ function CommentComposer({
|
|
|
66
66
|
if (!expandedRef.current) setExpanded(true);
|
|
67
67
|
return;
|
|
68
68
|
}
|
|
69
|
-
const
|
|
70
|
-
const send = field ? field.querySelector(".tlui-cmt-send") : null;
|
|
69
|
+
const send = wrap.parentElement?.querySelector(".tlui-cmt-send");
|
|
71
70
|
const input = wrap.querySelector(".tlui-cmt-input");
|
|
72
71
|
if (!send || !input) return;
|
|
73
72
|
mirror.innerHTML = input.innerHTML;
|
|
@@ -172,8 +171,7 @@ function CommentComposer({
|
|
|
172
171
|
return () => cancelAnimationFrame(raf);
|
|
173
172
|
}, [autoFocus, editor]);
|
|
174
173
|
const focusEditorFromField = (e) => {
|
|
175
|
-
|
|
176
|
-
if (target.closest(".tlui-cmt-input") || target.closest(".tlui-cmt-send")) return;
|
|
174
|
+
if (e.target.closest(".tlui-cmt-input, .tlui-cmt-send")) return;
|
|
177
175
|
e.preventDefault();
|
|
178
176
|
editor?.commands.focus("end");
|
|
179
177
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/ui/comment-composer.tsx"],
|
|
4
|
-
"sourcesContent": ["import { JSONContent } from '@tiptap/core'\nimport { EditorContent, useEditor } from '@tiptap/react'\nimport {\n\tAvatar,\n\ttype CommentAuthor,\n\tcreateMentionExtension,\n\tcreateMentionSuggestion,\n\tisMentionPickerOpen,\n\tMentionMember,\n} from '@tldraw/mentions'\nimport {\n\ttype MouseEvent as ReactMouseEvent,\n\tReactNode,\n\tuseEffect,\n\tuseLayoutEffect,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n} from 'react'\nimport { isEqual, TLRichText, useMaybeEditor } from 'tldraw'\nimport { commentTipTapExtensions, EMPTY_COMMENT, isCommentEmpty } from './comment-extensions'\nimport { SendButton } from './send-button'\n\n/** @public */\nexport interface CommentComposerProps {\n\tauthor: CommentAuthor\n\tplaceholder: string\n\t/** Controlled rich-text value. Omit for the presentational (display-only) composer. */\n\tvalue?: TLRichText\n\tonChange?(value: TLRichText): void\n\t/** Called on Send click or Enter. When set, the composer is interactive. */\n\tonSubmit?(): void\n\tsendLabel?: string\n\t/** Called when Up is pressed in an empty composer \u2014 e.g. to start editing the comment above,\n\t * the way chat apps edit your last message. With content in the field, Up moves the cursor. */\n\tonArrowUpWhenEmpty?(): void\n\tdisabled?: boolean\n\tautoFocus?: boolean\n\t/** The leading element before the field. Defaults to the author's avatar. */\n\tleading?: ReactNode\n\t/** Resolve the members matching an `@`-query (sync or async). Provide to enable mentions. */\n\tgetMentionSuggestions?(query: string): MentionMember[] | Promise<MentionMember[]>\n\t/** Override a picker row's content. Defaults to avatar + name (+ secondary). */\n\trenderMentionSuggestion?(member: MentionMember): ReactNode\n}\n\n/**\n * The input for writing a comment: a TipTap rich-text editor restricted to the comment extension\n * set (bold, italic, lists, links, code, highlight \u2014 no headings), with a Send button. Formatting\n * is applied through markdown and keyboard shortcuts (e.g. `**bold**`, `- `, Cmd+B); there's no\n * floating toolbar. Presentational by default; pass value/onChange/onSubmit to drive it as a form.\n * @public @react\n */\nexport function CommentComposer({\n\tauthor,\n\tplaceholder,\n\tvalue,\n\tonChange,\n\tonSubmit,\n\tsendLabel = 'Send',\n\tonArrowUpWhenEmpty,\n\tdisabled,\n\tautoFocus,\n\tleading,\n\tgetMentionSuggestions,\n\trenderMentionSuggestion,\n}: CommentComposerProps) {\n\tconst interactive = !!onChange || !!onSubmit\n\t// Lets the mention popup track the camera. Null outside a tldraw editor.\n\tconst tlEditor = useMaybeEditor()\n\n\t// Read through refs so the editor isn't recreated when callback identity changes.\n\tconst onChangeRef = useRef(onChange)\n\tonChangeRef.current = onChange\n\tconst onSubmitRef = useRef(onSubmit)\n\tonSubmitRef.current = onSubmit\n\tconst onArrowUpWhenEmptyRef = useRef(onArrowUpWhenEmpty)\n\tonArrowUpWhenEmptyRef.current = onArrowUpWhenEmpty\n\tconst disabledRef = useRef(disabled)\n\tdisabledRef.current = disabled\n\tconst getMentionSuggestionsRef = useRef(getMentionSuggestions)\n\tgetMentionSuggestionsRef.current = getMentionSuggestions\n\tconst renderMentionSuggestionRef = useRef(renderMentionSuggestion)\n\trenderMentionSuggestionRef.current = renderMentionSuggestion\n\n\tconst [isEmpty, setIsEmpty] = useState(() => !value || isCommentEmpty(value))\n\t// Whether the field has grown to two rows: input across the full width, send button below.\n\t// Flips when the (single-line) content width reaches the space left beside the send button.\n\tconst [expanded, setExpanded] = useState(false)\n\tconst expandedRef = useRef(expanded)\n\texpandedRef.current = expanded\n\tconst inputWrapRef = useRef<HTMLDivElement>(null)\n\tconst mirrorRef = useRef<HTMLDivElement>(null)\n\n\t// Measure whether the content still fits on one line beside the send button, against a hidden\n\t// nowrap clone so marks and mention chips measure at their true width. The gap between the\n\t// expand and collapse thresholds is a dead zone, so the layout can't flap at the boundary.\n\tconst remeasure = () => {\n\t\tconst wrap = inputWrapRef.current\n\t\tconst mirror = mirrorRef.current\n\t\tconst editor = editorRef.current\n\t\tif (!wrap || !mirror || !editor) return\n\t\t// Empty always fits \u2014 and measuring before TipTap has laid out reads zero widths, flashing\n\t\t// the expanded layout for a frame on mount.\n\t\tif (editor.isEmpty) {\n\t\t\tif (expandedRef.current) setExpanded(false)\n\t\t\treturn\n\t\t}\n\t\tif (wrap.clientWidth === 0) return\n\t\tconst doc = editor.state.doc\n\t\tconst multiBlock =\n\t\t\tdoc.childCount > 1 || (doc.firstChild !== null && doc.firstChild.type.name !== 'paragraph')\n\t\tif (multiBlock) {\n\t\t\tif (!expandedRef.current) setExpanded(true)\n\t\t\treturn\n\t\t}\n\t\tconst field = wrap.parentElement\n\t\tconst send = field ? field.querySelector<HTMLElement>('.tlui-cmt-send') : null\n\t\tconst input = wrap.querySelector('.tlui-cmt-input')\n\t\tif (!send || !input) return\n\t\tmirror.innerHTML = input.innerHTML\n\t\tconst textWidth = mirror.offsetWidth\n\t\t// Measured against the wrap's *content* box: text wraps at clientWidth minus padding, so\n\t\t// without subtracting it the input grows a line before expansion fires and then snaps.\n\t\tconst wrapStyle = getComputedStyle(wrap)\n\t\tconst wrapPadX = parseFloat(wrapStyle.paddingLeft) + parseFloat(wrapStyle.paddingRight)\n\t\tconst collapsedAvailable =\n\t\t\twrap.clientWidth - wrapPadX - (expandedRef.current ? send.offsetWidth + 6 : 0)\n\t\tif (!expandedRef.current && textWidth > collapsedAvailable - 8) {\n\t\t\tsetExpanded(true)\n\t\t} else if (expandedRef.current && textWidth < collapsedAvailable - 24) {\n\t\t\tsetExpanded(false)\n\t\t}\n\t}\n\tconst remeasureRef = useRef(remeasure)\n\tremeasureRef.current = remeasure\n\n\t// Reachable from `handleKeyDown`, which is created before `useEditor` returns.\n\tconst editorRef = useRef<ReturnType<typeof useEditor>>(null)\n\n\t// `commands.enter()` re-dispatches through `handleKeyDown`; without this guard our own handler\n\t// would catch the synthetic Enter and submit.\n\tconst replayingEnter = useRef(false)\n\n\t// The suggestion plugin is built once and runs outside React, so it reads the mention callbacks\n\t// through refs \u2014 otherwise it queries the roster present at mount forever, never seeing a member\n\t// who joins later. Whether mentions are wired at all is fixed at mount; the callbacks are live.\n\tconst mentionsEnabled = !!getMentionSuggestions\n\tconst hasCustomRow = !!renderMentionSuggestion\n\tconst extensions = useMemo(() => {\n\t\tconst list = [...commentTipTapExtensions]\n\t\t// The mention node is always registered, or ProseMirror strips existing mentions when an\n\t\t// edited body loads. Only the `@` trigger is gated on the host providing a resolver.\n\t\tif (mentionsEnabled) {\n\t\t\tconst resolveSuggestions = (query: string) => getMentionSuggestionsRef.current?.(query) ?? []\n\t\t\tconst renderRow = hasCustomRow\n\t\t\t\t? (member: MentionMember) => renderMentionSuggestionRef.current?.(member)\n\t\t\t\t: undefined\n\t\t\tlist.push(\n\t\t\t\tcreateMentionExtension({\n\t\t\t\t\tsuggestion: createMentionSuggestion(resolveSuggestions, {\n\t\t\t\t\t\trenderMember: renderRow,\n\t\t\t\t\t\teditor: tlEditor,\n\t\t\t\t\t}),\n\t\t\t\t})\n\t\t\t)\n\t\t} else {\n\t\t\tlist.push(createMentionExtension({ suggestion: { char: '@', allow: () => false } }))\n\t\t}\n\t\treturn list\n\t}, [mentionsEnabled, hasCustomRow, tlEditor])\n\n\tconst editor = useEditor(\n\t\t{\n\t\t\textensions,\n\t\t\tcontent: (value ?? EMPTY_COMMENT) as JSONContent,\n\t\t\teditable: interactive,\n\t\t\t// tldraw ships its own TextDirection extension, so TipTap's core one would warn about a\n\t\t\t// duplicate. Mirrors RichTextArea's setup.\n\t\t\tenableCoreExtensions: { textDirection: false },\n\t\t\ttextDirection: 'auto',\n\t\t\teditorProps: {\n\t\t\t\tattributes: {\n\t\t\t\t\tclass: 'tlui-cmt-input',\n\t\t\t\t\t'aria-label': placeholder,\n\t\t\t\t\trole: 'textbox',\n\t\t\t\t\t'aria-multiline': 'true',\n\t\t\t\t},\n\t\t\t\t// Runs before every keymap plugin, so it can distinguish Shift+Enter from Enter \u2014 an\n\t\t\t\t// `Enter` keymap binding also fires on Shift+Enter and would otherwise swallow it.\n\t\t\t\thandleKeyDown: (_view, event) => {\n\t\t\t\t\t// Let the keymaps handle the synthetic Enter we replay for a Shift+Enter newline.\n\t\t\t\t\tif (replayingEnter.current) return false\n\t\t\t\t\t// Up in an empty composer hands off to the host. With content it stays cursor\n\t\t\t\t\t// movement, and with the picker open it navigates the roster.\n\t\t\t\t\tif (event.key === 'ArrowUp' && !event.isComposing) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tonArrowUpWhenEmptyRef.current &&\n\t\t\t\t\t\t\teditorRef.current?.isEmpty &&\n\t\t\t\t\t\t\t!isMentionPickerOpen()\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tonArrowUpWhenEmptyRef.current()\n\t\t\t\t\t\t\treturn true\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn false\n\t\t\t\t\t}\n\t\t\t\t\tif (event.key !== 'Enter' || event.isComposing) return false\n\t\t\t\t\t// While the @-mention picker is open, Enter selects the highlighted member \u2014 defer.\n\t\t\t\t\tif (isMentionPickerOpen()) return false\n\t\t\t\t\t// Shift+Enter inserts a new line by replaying a plain Enter through the keymaps, reusing\n\t\t\t\t\t// the editor's list-aware handling. tldraw doesn't do soft breaks.\n\t\t\t\t\tif (event.shiftKey && !event.metaKey && !event.ctrlKey && !event.altKey) {\n\t\t\t\t\t\t// An empty field would open the comment with a stray leading blank line.\n\t\t\t\t\t\tif (editorRef.current?.isEmpty) return true\n\t\t\t\t\t\treplayingEnter.current = true\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\teditorRef.current?.commands.enter()\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\treplayingEnter.current = false\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn true\n\t\t\t\t\t}\n\t\t\t\t\t// Enter, Cmd+Enter, and Ctrl+Enter submit.\n\t\t\t\t\tif (!disabledRef.current) onSubmitRef.current?.()\n\t\t\t\t\treturn true\n\t\t\t\t},\n\t\t\t},\n\t\t\tonUpdate: ({ editor }) => {\n\t\t\t\tsetIsEmpty(editor.isEmpty)\n\t\t\t\t// Same-value state sets don't re-render, and the DOM is already updated here.\n\t\t\t\tremeasureRef.current()\n\t\t\t\tonChangeRef.current?.(editor.getJSON() as TLRichText)\n\t\t\t},\n\t\t},\n\t\t[interactive, placeholder]\n\t)\n\teditorRef.current = editor\n\n\t// Sync controlled resets (e.g. the parent clearing to EMPTY_COMMENT after posting) into the\n\t// editor without echoing back the value the editor itself just emitted.\n\tuseEffect(() => {\n\t\tif (!editor || value === undefined) return\n\t\tif (isEqual(editor.getJSON(), value)) return\n\t\teditor.commands.setContent(value as JSONContent)\n\t\tsetIsEmpty(editor.isEmpty)\n\t}, [editor, value])\n\n\t// Measure once the editor's content is in the DOM, so a composer that mounts pre-filled (the\n\t// edit-in-place composer) starts in the right layout.\n\tuseLayoutEffect(() => {\n\t\tremeasureRef.current()\n\t}, [editor, value])\n\n\t// Focus on the next frame rather than via TipTap's autofocus: the composer often mounts from a\n\t// canvas pointer event whose default focus handling would otherwise steal it back.\n\tuseEffect(() => {\n\t\tif (!autoFocus || !editor) return\n\t\tconst raf = requestAnimationFrame(() => editor.commands.focus('end'))\n\t\treturn () => cancelAnimationFrame(raf)\n\t}, [autoFocus, editor])\n\n\t// The whole field behaves like the text input: clicking its empty area (the padding, or the\n\t// space beside/below a short line) focuses the editor rather than only the text glyphs being\n\t// clickable. The input (caret placement) and the send button keep their own click handling.\n\tconst focusEditorFromField = (e: ReactMouseEvent<HTMLDivElement>) => {\n\t\tconst target = e.target as HTMLElement\n\t\tif (target.closest('.tlui-cmt-input') || target.closest('.tlui-cmt-send')) return\n\t\te.preventDefault()\n\t\teditor?.commands.focus('end')\n\t}\n\n\treturn (\n\t\t<div className=\"tlui-cmt-composer\">\n\t\t\t{leading ?? <Avatar author={author} />}\n\t\t\t<div\n\t\t\t\tclassName={[\n\t\t\t\t\t'tlui-cmt-composer__field',\n\t\t\t\t\tinteractive && expanded && 'tlui-cmt-composer__field--expanded',\n\t\t\t\t]\n\t\t\t\t\t.filter(Boolean)\n\t\t\t\t\t.join(' ')}\n\t\t\t\tonMouseDown={interactive ? focusEditorFromField : undefined}\n\t\t\t>\n\t\t\t\t<div className=\"tlui-cmt-composer__input-wrap\" ref={inputWrapRef}>\n\t\t\t\t\t<EditorContent editor={editor} />\n\t\t\t\t\t{isEmpty && (\n\t\t\t\t\t\t<div className=\"tlui-cmt-input__placeholder\" aria-hidden=\"true\">\n\t\t\t\t\t\t\t{placeholder}\n\t\t\t\t\t\t</div>\n\t\t\t\t\t)}\n\t\t\t\t\t<div\n\t\t\t\t\t\tclassName=\"tlui-cmt-composer__mirror tlui-cmt-input\"\n\t\t\t\t\t\tref={mirrorRef}\n\t\t\t\t\t\taria-hidden=\"true\"\n\t\t\t\t\t/>\n\t\t\t\t</div>\n\t\t\t\t{interactive && <SendButton label={sendLabel} onClick={onSubmit} disabled={disabled} />}\n\t\t\t</div>\n\t\t</div>\n\t)\n}\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import { JSONContent } from '@tiptap/core'\nimport { EditorContent, useEditor } from '@tiptap/react'\nimport {\n\tAvatar,\n\ttype CommentAuthor,\n\tcreateMentionExtension,\n\tcreateMentionSuggestion,\n\tisMentionPickerOpen,\n\tMentionMember,\n} from '@tldraw/mentions'\nimport {\n\ttype MouseEvent as ReactMouseEvent,\n\tReactNode,\n\tuseEffect,\n\tuseLayoutEffect,\n\tuseMemo,\n\tuseRef,\n\tuseState,\n} from 'react'\nimport { isEqual, TLRichText, useMaybeEditor } from 'tldraw'\nimport { commentTipTapExtensions, EMPTY_COMMENT, isCommentEmpty } from './comment-extensions'\nimport { SendButton } from './send-button'\n\n/** @public */\nexport interface CommentComposerProps {\n\tauthor: CommentAuthor\n\tplaceholder: string\n\t/** Controlled rich-text value. Omit for the presentational (display-only) composer. */\n\tvalue?: TLRichText\n\tonChange?(value: TLRichText): void\n\t/** Called on Send click or Enter. When set, the composer is interactive. */\n\tonSubmit?(): void\n\tsendLabel?: string\n\t/** Called when Up is pressed in an empty composer \u2014 e.g. to start editing the comment above,\n\t * the way chat apps edit your last message. With content in the field, Up moves the cursor. */\n\tonArrowUpWhenEmpty?(): void\n\tdisabled?: boolean\n\tautoFocus?: boolean\n\t/** The leading element before the field. Defaults to the author's avatar. */\n\tleading?: ReactNode\n\t/** Resolve the members matching an `@`-query (sync or async). Provide to enable mentions. */\n\tgetMentionSuggestions?(query: string): MentionMember[] | Promise<MentionMember[]>\n\t/** Override a picker row's content. Defaults to avatar + name (+ secondary). */\n\trenderMentionSuggestion?(member: MentionMember): ReactNode\n}\n\n/**\n * The input for writing a comment: a TipTap rich-text editor restricted to the comment extension\n * set (bold, italic, lists, links, code, highlight \u2014 no headings), with a Send button. Formatting\n * is applied through markdown and keyboard shortcuts (e.g. `**bold**`, `- `, Cmd+B); there's no\n * floating toolbar. Presentational by default; pass value/onChange/onSubmit to drive it as a form.\n * @public @react\n */\nexport function CommentComposer({\n\tauthor,\n\tplaceholder,\n\tvalue,\n\tonChange,\n\tonSubmit,\n\tsendLabel = 'Send',\n\tonArrowUpWhenEmpty,\n\tdisabled,\n\tautoFocus,\n\tleading,\n\tgetMentionSuggestions,\n\trenderMentionSuggestion,\n}: CommentComposerProps) {\n\tconst interactive = !!onChange || !!onSubmit\n\t// Lets the mention popup track the camera. Null outside a tldraw editor.\n\tconst tlEditor = useMaybeEditor()\n\n\t// Read through refs so the editor isn't recreated when callback identity changes.\n\tconst onChangeRef = useRef(onChange)\n\tonChangeRef.current = onChange\n\tconst onSubmitRef = useRef(onSubmit)\n\tonSubmitRef.current = onSubmit\n\tconst onArrowUpWhenEmptyRef = useRef(onArrowUpWhenEmpty)\n\tonArrowUpWhenEmptyRef.current = onArrowUpWhenEmpty\n\tconst disabledRef = useRef(disabled)\n\tdisabledRef.current = disabled\n\tconst getMentionSuggestionsRef = useRef(getMentionSuggestions)\n\tgetMentionSuggestionsRef.current = getMentionSuggestions\n\tconst renderMentionSuggestionRef = useRef(renderMentionSuggestion)\n\trenderMentionSuggestionRef.current = renderMentionSuggestion\n\n\tconst [isEmpty, setIsEmpty] = useState(() => !value || isCommentEmpty(value))\n\t// Whether the field has grown to two rows: input across the full width, send button below.\n\t// Flips when the (single-line) content width reaches the space left beside the send button.\n\tconst [expanded, setExpanded] = useState(false)\n\tconst expandedRef = useRef(expanded)\n\texpandedRef.current = expanded\n\tconst inputWrapRef = useRef<HTMLDivElement>(null)\n\tconst mirrorRef = useRef<HTMLDivElement>(null)\n\n\t// Measure whether the content still fits on one line beside the send button, against a hidden\n\t// nowrap clone so marks and mention chips measure at their true width. The gap between the\n\t// expand and collapse thresholds is a dead zone, so the layout can't flap at the boundary.\n\tconst remeasure = () => {\n\t\tconst wrap = inputWrapRef.current\n\t\tconst mirror = mirrorRef.current\n\t\tconst editor = editorRef.current\n\t\tif (!wrap || !mirror || !editor) return\n\t\t// Empty always fits \u2014 and measuring before TipTap has laid out reads zero widths, flashing\n\t\t// the expanded layout for a frame on mount.\n\t\tif (editor.isEmpty) {\n\t\t\tif (expandedRef.current) setExpanded(false)\n\t\t\treturn\n\t\t}\n\t\tif (wrap.clientWidth === 0) return\n\t\tconst doc = editor.state.doc\n\t\tconst multiBlock =\n\t\t\tdoc.childCount > 1 || (doc.firstChild !== null && doc.firstChild.type.name !== 'paragraph')\n\t\tif (multiBlock) {\n\t\t\tif (!expandedRef.current) setExpanded(true)\n\t\t\treturn\n\t\t}\n\t\tconst send = wrap.parentElement?.querySelector<HTMLElement>('.tlui-cmt-send')\n\t\tconst input = wrap.querySelector('.tlui-cmt-input')\n\t\tif (!send || !input) return\n\t\tmirror.innerHTML = input.innerHTML\n\t\tconst textWidth = mirror.offsetWidth\n\t\t// Measured against the wrap's *content* box: text wraps at clientWidth minus padding, so\n\t\t// without subtracting it the input grows a line before expansion fires and then snaps.\n\t\tconst wrapStyle = getComputedStyle(wrap)\n\t\tconst wrapPadX = parseFloat(wrapStyle.paddingLeft) + parseFloat(wrapStyle.paddingRight)\n\t\tconst collapsedAvailable =\n\t\t\twrap.clientWidth - wrapPadX - (expandedRef.current ? send.offsetWidth + 6 : 0)\n\t\tif (!expandedRef.current && textWidth > collapsedAvailable - 8) {\n\t\t\tsetExpanded(true)\n\t\t} else if (expandedRef.current && textWidth < collapsedAvailable - 24) {\n\t\t\tsetExpanded(false)\n\t\t}\n\t}\n\tconst remeasureRef = useRef(remeasure)\n\tremeasureRef.current = remeasure\n\n\t// Reachable from `handleKeyDown`, which is created before `useEditor` returns.\n\tconst editorRef = useRef<ReturnType<typeof useEditor>>(null)\n\n\t// `commands.enter()` re-dispatches through `handleKeyDown`; without this guard our own handler\n\t// would catch the synthetic Enter and submit.\n\tconst replayingEnter = useRef(false)\n\n\t// The suggestion plugin is built once and runs outside React, so it reads the mention callbacks\n\t// through refs \u2014 otherwise it queries the roster present at mount forever, never seeing a member\n\t// who joins later. Whether mentions are wired at all is fixed at mount; the callbacks are live.\n\tconst mentionsEnabled = !!getMentionSuggestions\n\tconst hasCustomRow = !!renderMentionSuggestion\n\tconst extensions = useMemo(() => {\n\t\tconst list = [...commentTipTapExtensions]\n\t\t// The mention node is always registered, or ProseMirror strips existing mentions when an\n\t\t// edited body loads. Only the `@` trigger is gated on the host providing a resolver.\n\t\tif (mentionsEnabled) {\n\t\t\tconst resolveSuggestions = (query: string) => getMentionSuggestionsRef.current?.(query) ?? []\n\t\t\tconst renderRow = hasCustomRow\n\t\t\t\t? (member: MentionMember) => renderMentionSuggestionRef.current?.(member)\n\t\t\t\t: undefined\n\t\t\tlist.push(\n\t\t\t\tcreateMentionExtension({\n\t\t\t\t\tsuggestion: createMentionSuggestion(resolveSuggestions, {\n\t\t\t\t\t\trenderMember: renderRow,\n\t\t\t\t\t\teditor: tlEditor,\n\t\t\t\t\t}),\n\t\t\t\t})\n\t\t\t)\n\t\t} else {\n\t\t\tlist.push(createMentionExtension({ suggestion: { char: '@', allow: () => false } }))\n\t\t}\n\t\treturn list\n\t}, [mentionsEnabled, hasCustomRow, tlEditor])\n\n\tconst editor = useEditor(\n\t\t{\n\t\t\textensions,\n\t\t\tcontent: (value ?? EMPTY_COMMENT) as JSONContent,\n\t\t\teditable: interactive,\n\t\t\t// tldraw ships its own TextDirection extension, so TipTap's core one would warn about a\n\t\t\t// duplicate. Mirrors RichTextArea's setup.\n\t\t\tenableCoreExtensions: { textDirection: false },\n\t\t\ttextDirection: 'auto',\n\t\t\teditorProps: {\n\t\t\t\tattributes: {\n\t\t\t\t\tclass: 'tlui-cmt-input',\n\t\t\t\t\t'aria-label': placeholder,\n\t\t\t\t\trole: 'textbox',\n\t\t\t\t\t'aria-multiline': 'true',\n\t\t\t\t},\n\t\t\t\t// Runs before every keymap plugin, so it can distinguish Shift+Enter from Enter \u2014 an\n\t\t\t\t// `Enter` keymap binding also fires on Shift+Enter and would otherwise swallow it.\n\t\t\t\thandleKeyDown: (_view, event) => {\n\t\t\t\t\t// Let the keymaps handle the synthetic Enter we replay for a Shift+Enter newline.\n\t\t\t\t\tif (replayingEnter.current) return false\n\t\t\t\t\t// Up in an empty composer hands off to the host. With content it stays cursor\n\t\t\t\t\t// movement, and with the picker open it navigates the roster.\n\t\t\t\t\tif (event.key === 'ArrowUp' && !event.isComposing) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tonArrowUpWhenEmptyRef.current &&\n\t\t\t\t\t\t\teditorRef.current?.isEmpty &&\n\t\t\t\t\t\t\t!isMentionPickerOpen()\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\tonArrowUpWhenEmptyRef.current()\n\t\t\t\t\t\t\treturn true\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn false\n\t\t\t\t\t}\n\t\t\t\t\tif (event.key !== 'Enter' || event.isComposing) return false\n\t\t\t\t\t// While the @-mention picker is open, Enter selects the highlighted member \u2014 defer.\n\t\t\t\t\tif (isMentionPickerOpen()) return false\n\t\t\t\t\t// Shift+Enter inserts a new line by replaying a plain Enter through the keymaps, reusing\n\t\t\t\t\t// the editor's list-aware handling. tldraw doesn't do soft breaks.\n\t\t\t\t\tif (event.shiftKey && !event.metaKey && !event.ctrlKey && !event.altKey) {\n\t\t\t\t\t\t// An empty field would open the comment with a stray leading blank line.\n\t\t\t\t\t\tif (editorRef.current?.isEmpty) return true\n\t\t\t\t\t\treplayingEnter.current = true\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\teditorRef.current?.commands.enter()\n\t\t\t\t\t\t} finally {\n\t\t\t\t\t\t\treplayingEnter.current = false\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn true\n\t\t\t\t\t}\n\t\t\t\t\t// Enter, Cmd+Enter, and Ctrl+Enter submit.\n\t\t\t\t\tif (!disabledRef.current) onSubmitRef.current?.()\n\t\t\t\t\treturn true\n\t\t\t\t},\n\t\t\t},\n\t\t\tonUpdate: ({ editor }) => {\n\t\t\t\tsetIsEmpty(editor.isEmpty)\n\t\t\t\t// Same-value state sets don't re-render, and the DOM is already updated here.\n\t\t\t\tremeasureRef.current()\n\t\t\t\tonChangeRef.current?.(editor.getJSON() as TLRichText)\n\t\t\t},\n\t\t},\n\t\t[interactive, placeholder]\n\t)\n\teditorRef.current = editor\n\n\t// Sync controlled resets (e.g. the parent clearing to EMPTY_COMMENT after posting) into the\n\t// editor without echoing back the value the editor itself just emitted.\n\tuseEffect(() => {\n\t\tif (!editor || value === undefined) return\n\t\tif (isEqual(editor.getJSON(), value)) return\n\t\teditor.commands.setContent(value as JSONContent)\n\t\tsetIsEmpty(editor.isEmpty)\n\t}, [editor, value])\n\n\t// Measure once the editor's content is in the DOM, so a composer that mounts pre-filled (the\n\t// edit-in-place composer) starts in the right layout.\n\tuseLayoutEffect(() => {\n\t\tremeasureRef.current()\n\t}, [editor, value])\n\n\t// Focus on the next frame rather than via TipTap's autofocus: the composer often mounts from a\n\t// canvas pointer event whose default focus handling would otherwise steal it back.\n\tuseEffect(() => {\n\t\tif (!autoFocus || !editor) return\n\t\tconst raf = requestAnimationFrame(() => editor.commands.focus('end'))\n\t\treturn () => cancelAnimationFrame(raf)\n\t}, [autoFocus, editor])\n\n\t// The whole field behaves like the text input: clicking its empty area (the padding, or the\n\t// space beside/below a short line) focuses the editor rather than only the text glyphs being\n\t// clickable. The input (caret placement) and the send button keep their own click handling.\n\tconst focusEditorFromField = (e: ReactMouseEvent<HTMLDivElement>) => {\n\t\tif ((e.target as HTMLElement).closest('.tlui-cmt-input, .tlui-cmt-send')) return\n\t\te.preventDefault()\n\t\teditor?.commands.focus('end')\n\t}\n\n\treturn (\n\t\t<div className=\"tlui-cmt-composer\">\n\t\t\t{leading ?? <Avatar author={author} />}\n\t\t\t<div\n\t\t\t\tclassName={[\n\t\t\t\t\t'tlui-cmt-composer__field',\n\t\t\t\t\tinteractive && expanded && 'tlui-cmt-composer__field--expanded',\n\t\t\t\t]\n\t\t\t\t\t.filter(Boolean)\n\t\t\t\t\t.join(' ')}\n\t\t\t\tonMouseDown={interactive ? focusEditorFromField : undefined}\n\t\t\t>\n\t\t\t\t<div className=\"tlui-cmt-composer__input-wrap\" ref={inputWrapRef}>\n\t\t\t\t\t<EditorContent editor={editor} />\n\t\t\t\t\t{isEmpty && (\n\t\t\t\t\t\t<div className=\"tlui-cmt-input__placeholder\" aria-hidden=\"true\">\n\t\t\t\t\t\t\t{placeholder}\n\t\t\t\t\t\t</div>\n\t\t\t\t\t)}\n\t\t\t\t\t<div\n\t\t\t\t\t\tclassName=\"tlui-cmt-composer__mirror tlui-cmt-input\"\n\t\t\t\t\t\tref={mirrorRef}\n\t\t\t\t\t\taria-hidden=\"true\"\n\t\t\t\t\t/>\n\t\t\t\t</div>\n\t\t\t\t{interactive && <SendButton label={sendLabel} onClick={onSubmit} disabled={disabled} />}\n\t\t\t</div>\n\t\t</div>\n\t)\n}\n"],
|
|
5
|
+
"mappings": "AA+Qe,cAUX,YAVW;AA9Qf,SAAS,eAAe,iBAAiB;AACzC;AAAA,EACC;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,OAEM;AACP;AAAA,EAGC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,SAAqB,sBAAsB;AACpD,SAAS,yBAAyB,eAAe,sBAAsB;AACvE,SAAS,kBAAkB;AAgCpB,SAAS,gBAAgB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAyB;AACxB,QAAM,cAAc,CAAC,CAAC,YAAY,CAAC,CAAC;AAEpC,QAAM,WAAW,eAAe;AAGhC,QAAM,cAAc,OAAO,QAAQ;AACnC,cAAY,UAAU;AACtB,QAAM,cAAc,OAAO,QAAQ;AACnC,cAAY,UAAU;AACtB,QAAM,wBAAwB,OAAO,kBAAkB;AACvD,wBAAsB,UAAU;AAChC,QAAM,cAAc,OAAO,QAAQ;AACnC,cAAY,UAAU;AACtB,QAAM,2BAA2B,OAAO,qBAAqB;AAC7D,2BAAyB,UAAU;AACnC,QAAM,6BAA6B,OAAO,uBAAuB;AACjE,6BAA2B,UAAU;AAErC,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,MAAM,CAAC,SAAS,eAAe,KAAK,CAAC;AAG5E,QAAM,CAAC,UAAU,WAAW,IAAI,SAAS,KAAK;AAC9C,QAAM,cAAc,OAAO,QAAQ;AACnC,cAAY,UAAU;AACtB,QAAM,eAAe,OAAuB,IAAI;AAChD,QAAM,YAAY,OA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BAAuB;AAAA,QAElD;AAAA,+BAAC,SAAI,WAAU,iCAAgC,KAAK,cACnD;AAAA,gCAAC,iBAAc,QAAgB;AAAA,YAC9B,WACA,oBAAC,SAAI,WAAU,+BAA8B,eAAY,QACvD,uBACF;AAAA,YAED;AAAA,cAAC;AAAA;AAAA,gBACA,WAAU;AAAA,gBACV,KAAK;AAAA,gBACL,eAAY;AAAA;AAAA,YACb;AAAA,aACD;AAAA,UACC,eAAe,oBAAC,cAAW,OAAO,WAAW,SAAS,UAAU,UAAoB;AAAA;AAAA;AAAA,IACtF;AAAA,KACD;AAEF;",
|
|
6
6
|
"names": ["editor"]
|
|
7
7
|
}
|
|
@@ -7,10 +7,24 @@ const DIVISIONS = [
|
|
|
7
7
|
{ amount: 12, unit: "month" },
|
|
8
8
|
{ amount: Number.POSITIVE_INFINITY, unit: "year" }
|
|
9
9
|
];
|
|
10
|
+
const relativeTimeFormatters = /* @__PURE__ */ new Map();
|
|
11
|
+
const fullDateTimeFormatters = /* @__PURE__ */ new Map();
|
|
12
|
+
function getCached(cache, locale, create) {
|
|
13
|
+
let formatter = cache.get(locale);
|
|
14
|
+
if (!formatter) {
|
|
15
|
+
formatter = create();
|
|
16
|
+
cache.set(locale, formatter);
|
|
17
|
+
}
|
|
18
|
+
return formatter;
|
|
19
|
+
}
|
|
10
20
|
function formatRelativeTime(iso, locale = "en") {
|
|
11
21
|
const then = new Date(iso).getTime();
|
|
12
22
|
if (Number.isNaN(then)) return "";
|
|
13
|
-
const rtf =
|
|
23
|
+
const rtf = getCached(
|
|
24
|
+
relativeTimeFormatters,
|
|
25
|
+
locale,
|
|
26
|
+
() => new Intl.RelativeTimeFormat(locale, { numeric: "auto", style: "narrow" })
|
|
27
|
+
);
|
|
14
28
|
let duration = (then - Date.now()) / 1e3;
|
|
15
29
|
if (Math.abs(duration) < 60) {
|
|
16
30
|
return rtf.format(0, "second");
|
|
@@ -23,16 +37,14 @@ function formatRelativeTime(iso, locale = "en") {
|
|
|
23
37
|
}
|
|
24
38
|
return "";
|
|
25
39
|
}
|
|
26
|
-
const fullDateTimeFormatters = /* @__PURE__ */ new Map();
|
|
27
40
|
function formatFullDateTime(iso, locale = "en") {
|
|
28
41
|
const date = new Date(iso);
|
|
29
42
|
if (Number.isNaN(date.getTime())) return "";
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return formatter.format(date);
|
|
43
|
+
return getCached(
|
|
44
|
+
fullDateTimeFormatters,
|
|
45
|
+
locale,
|
|
46
|
+
() => new Intl.DateTimeFormat(locale, { dateStyle: "full", timeStyle: "short" })
|
|
47
|
+
).format(date);
|
|
36
48
|
}
|
|
37
49
|
export {
|
|
38
50
|
formatFullDateTime,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/ui/format-time.ts"],
|
|
4
|
-
"sourcesContent": ["const DIVISIONS = [\n\t{ amount: 60, unit: 'second' },\n\t{ amount: 60, unit: 'minute' },\n\t{ amount: 24, unit: 'hour' },\n\t{ amount: 7, unit: 'day' },\n\t{ amount: 4.34524, unit: 'week' },\n\t{ amount: 12, unit: 'month' },\n\t{ amount: Number.POSITIVE_INFINITY, unit: 'year' },\n] as const\n\n/**\n * Format an ISO datetime as compact relative time (\"2h ago\", \"yesterday\", \"last wk.\").\n * Locale-aware via Intl.RelativeTimeFormat.\n * @public\n */\nexport function formatRelativeTime(iso: string, locale = 'en'): string {\n\tconst then = new Date(iso).getTime()\n\tif (Number.isNaN(then)) return ''\n\n\tconst rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto', style: 'narrow' })\n\tlet duration = (then - Date.now()) / 1000\n\t// Under a minute is just \"now\" \u2014 with second granularity the label visibly ticks while\n\t// typing a reply (every keystroke re-renders the card).\n\tif (Math.abs(duration) < 60) {\n\t\treturn rtf.format(0, 'second')\n\t}\n\tfor (const division of DIVISIONS) {\n\t\tif (Math.abs(duration) < division.amount) {\n\t\t\treturn rtf.format(Math.round(duration), division.unit)\n\t\t}\n\t\tduration /= division.amount\n\t}\n\treturn ''\n}\n\n
|
|
5
|
-
"mappings": "AAAA,MAAM,YAAY;AAAA,EACjB,EAAE,QAAQ,IAAI,MAAM,SAAS;AAAA,EAC7B,EAAE,QAAQ,IAAI,MAAM,SAAS;AAAA,EAC7B,EAAE,QAAQ,IAAI,MAAM,OAAO;AAAA,EAC3B,EAAE,QAAQ,GAAG,MAAM,MAAM;AAAA,EACzB,EAAE,QAAQ,SAAS,MAAM,OAAO;AAAA,EAChC,EAAE,QAAQ,IAAI,MAAM,QAAQ;AAAA,EAC5B,EAAE,QAAQ,OAAO,mBAAmB,MAAM,OAAO;AAClD;AAOO,SAAS,mBAAmB,KAAa,SAAS,MAAc;AACtE,QAAM,OAAO,IAAI,KAAK,GAAG,EAAE,QAAQ;AACnC,MAAI,OAAO,MAAM,IAAI,EAAG,QAAO;AAE/B,QAAM,MAAM,IAAI,KAAK,mBAAmB,QAAQ,EAAE,SAAS,QAAQ,OAAO,SAAS,CAAC;
|
|
4
|
+
"sourcesContent": ["const DIVISIONS = [\n\t{ amount: 60, unit: 'second' },\n\t{ amount: 60, unit: 'minute' },\n\t{ amount: 24, unit: 'hour' },\n\t{ amount: 7, unit: 'day' },\n\t{ amount: 4.34524, unit: 'week' },\n\t{ amount: 12, unit: 'month' },\n\t{ amount: Number.POSITIVE_INFINITY, unit: 'year' },\n] as const\n\n// Both formatters are cached per locale \u2014 comment cards re-render on every reply keystroke, and\n// constructing an Intl formatter is far more expensive than formatting with one.\nconst relativeTimeFormatters = new Map<string, Intl.RelativeTimeFormat>()\nconst fullDateTimeFormatters = new Map<string, Intl.DateTimeFormat>()\n\nfunction getCached<T>(cache: Map<string, T>, locale: string, create: () => T): T {\n\tlet formatter = cache.get(locale)\n\tif (!formatter) {\n\t\tformatter = create()\n\t\tcache.set(locale, formatter)\n\t}\n\treturn formatter\n}\n\n/**\n * Format an ISO datetime as compact relative time (\"2h ago\", \"yesterday\", \"last wk.\").\n * Locale-aware via Intl.RelativeTimeFormat.\n * @public\n */\nexport function formatRelativeTime(iso: string, locale = 'en'): string {\n\tconst then = new Date(iso).getTime()\n\tif (Number.isNaN(then)) return ''\n\n\tconst rtf = getCached(\n\t\trelativeTimeFormatters,\n\t\tlocale,\n\t\t() => new Intl.RelativeTimeFormat(locale, { numeric: 'auto', style: 'narrow' })\n\t)\n\tlet duration = (then - Date.now()) / 1000\n\t// Under a minute is just \"now\" \u2014 with second granularity the label visibly ticks while\n\t// typing a reply (every keystroke re-renders the card).\n\tif (Math.abs(duration) < 60) {\n\t\treturn rtf.format(0, 'second')\n\t}\n\tfor (const division of DIVISIONS) {\n\t\tif (Math.abs(duration) < division.amount) {\n\t\t\treturn rtf.format(Math.round(duration), division.unit)\n\t\t}\n\t\tduration /= division.amount\n\t}\n\treturn ''\n}\n\n/**\n * Format an ISO datetime as a full date and time (\"Tuesday, July 22, 2025 at 4:44 PM\").\n * Locale-aware via Intl.DateTimeFormat.\n * @public\n */\nexport function formatFullDateTime(iso: string, locale = 'en'): string {\n\tconst date = new Date(iso)\n\tif (Number.isNaN(date.getTime())) return ''\n\treturn getCached(\n\t\tfullDateTimeFormatters,\n\t\tlocale,\n\t\t() => new Intl.DateTimeFormat(locale, { dateStyle: 'full', timeStyle: 'short' })\n\t).format(date)\n}\n"],
|
|
5
|
+
"mappings": "AAAA,MAAM,YAAY;AAAA,EACjB,EAAE,QAAQ,IAAI,MAAM,SAAS;AAAA,EAC7B,EAAE,QAAQ,IAAI,MAAM,SAAS;AAAA,EAC7B,EAAE,QAAQ,IAAI,MAAM,OAAO;AAAA,EAC3B,EAAE,QAAQ,GAAG,MAAM,MAAM;AAAA,EACzB,EAAE,QAAQ,SAAS,MAAM,OAAO;AAAA,EAChC,EAAE,QAAQ,IAAI,MAAM,QAAQ;AAAA,EAC5B,EAAE,QAAQ,OAAO,mBAAmB,MAAM,OAAO;AAClD;AAIA,MAAM,yBAAyB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
|
const names = reactors.map(
|
|
38
38
|
(reactor) => reactor.you ? msg("comments.mention-you") : reactor.name
|
|
39
39
|
);
|
|
40
|
+
if (names.length === 0) return null;
|
|
40
41
|
const [a, b, c] = names;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return /* @__PURE__ */ jsx(Fragment, { children: msg("comments.reacted-2").replace("{a}", a).replace("{b}", b) });
|
|
48
|
-
case 3:
|
|
49
|
-
return /* @__PURE__ */ jsx(Fragment, { children: msg("comments.reacted-3").replace("{a}", a).replace("{b}", b).replace("{c}", c) });
|
|
50
|
-
default: {
|
|
51
|
-
const others = names.length - 3;
|
|
52
|
-
const template = others === 1 ? "comments.reacted-more-one" : "comments.reacted-more";
|
|
53
|
-
return /* @__PURE__ */ jsx(Fragment, { children: msg(template).replace("{a}", a).replace("{b}", b).replace("{c}", c).replace("{count}", String(others)) });
|
|
54
|
-
}
|
|
55
|
-
}
|
|
42
|
+
const others = names.length - 3;
|
|
43
|
+
const key = names.length <= 3 ? `comments.reacted-${names.length}` : others === 1 ? "comments.reacted-more-one" : "comments.reacted-more";
|
|
44
|
+
const vars = { a, b, c, count: String(others) };
|
|
45
|
+
return (
|
|
46
|
+
/* @__PURE__ */ jsx(Fragment, { children: msg(key).replace(/\{(a|b|c|count)\}/g, (_, k) => vars[k] ?? "") })
|
|
47
|
+
);
|
|
56
48
|
}
|
|
57
49
|
export {
|
|
58
50
|
DefaultReactionTooltip,
|