@clagentic/console 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/CREDITS.md +3 -0
  2. package/LICENSE +21 -0
  3. package/README.md +250 -0
  4. package/bin/cli.js +2812 -0
  5. package/lib/agents-favorites.js +164 -0
  6. package/lib/agents.js +282 -0
  7. package/lib/ask-user-mcp-server.js +120 -0
  8. package/lib/browser-mcp-server.js +484 -0
  9. package/lib/build-user-env.js +63 -0
  10. package/lib/certs/fullchain.pem +47 -0
  11. package/lib/certs/privkey.pem +5 -0
  12. package/lib/cli-sessions.js +265 -0
  13. package/lib/codex-defaults.js +18 -0
  14. package/lib/config.js +332 -0
  15. package/lib/crisis-safety.js +60 -0
  16. package/lib/daemon-projects.js +164 -0
  17. package/lib/daemon.js +1602 -0
  18. package/lib/dm.js +128 -0
  19. package/lib/ipc.js +125 -0
  20. package/lib/ipv4-only.js +39 -0
  21. package/lib/mcp-local.js +385 -0
  22. package/lib/notes.js +141 -0
  23. package/lib/os-users.js +529 -0
  24. package/lib/pages.js +1325 -0
  25. package/lib/project-connection.js +302 -0
  26. package/lib/project-external-trigger.js +236 -0
  27. package/lib/project-file-watch.js +120 -0
  28. package/lib/project-filesystem.js +528 -0
  29. package/lib/project-http.js +753 -0
  30. package/lib/project-image.js +95 -0
  31. package/lib/project-knowledge.js +88 -0
  32. package/lib/project-loop.js +1304 -0
  33. package/lib/project-mcp.js +374 -0
  34. package/lib/project-memory.js +39 -0
  35. package/lib/project-notifications.js +262 -0
  36. package/lib/project-sessions.js +1581 -0
  37. package/lib/project-user-mention.js +193 -0
  38. package/lib/project-user-message.js +628 -0
  39. package/lib/project.js +1303 -0
  40. package/lib/public/app.js +1031 -0
  41. package/lib/public/apple-touch-icon.png +0 -0
  42. package/lib/public/claude-code-avatar.png +0 -0
  43. package/lib/public/codex-avatar.png +0 -0
  44. package/lib/public/console-icon.png +0 -0
  45. package/lib/public/css/admin.css +736 -0
  46. package/lib/public/css/agent-picker.css +119 -0
  47. package/lib/public/css/base.css +235 -0
  48. package/lib/public/css/channel-layout.css +441 -0
  49. package/lib/public/css/command-palette.css +559 -0
  50. package/lib/public/css/debate.css +1609 -0
  51. package/lib/public/css/diff.css +139 -0
  52. package/lib/public/css/filebrowser.css +1976 -0
  53. package/lib/public/css/highlight.css +144 -0
  54. package/lib/public/css/home-hub.css +545 -0
  55. package/lib/public/css/icon-strip.css +1856 -0
  56. package/lib/public/css/input.css +1349 -0
  57. package/lib/public/css/loop.css +1248 -0
  58. package/lib/public/css/mention.css +317 -0
  59. package/lib/public/css/menus.css +857 -0
  60. package/lib/public/css/messages.css +1833 -0
  61. package/lib/public/css/mobile-nav.css +1332 -0
  62. package/lib/public/css/notifications-center.css +296 -0
  63. package/lib/public/css/overlays.css +1273 -0
  64. package/lib/public/css/playbook.css +264 -0
  65. package/lib/public/css/profile.css +397 -0
  66. package/lib/public/css/rewind.css +671 -0
  67. package/lib/public/css/scheduler-modal.css +1495 -0
  68. package/lib/public/css/scheduler.css +1420 -0
  69. package/lib/public/css/server-settings.css +909 -0
  70. package/lib/public/css/session-search.css +159 -0
  71. package/lib/public/css/sidebar.css +2012 -0
  72. package/lib/public/css/skills.css +789 -0
  73. package/lib/public/css/sticky-notes.css +854 -0
  74. package/lib/public/css/stt.css +155 -0
  75. package/lib/public/css/title-bar.css +674 -0
  76. package/lib/public/css/tooltip.css +67 -0
  77. package/lib/public/css/user-settings.css +702 -0
  78. package/lib/public/emoji-data.json +1 -0
  79. package/lib/public/favicon-banded-32.png +0 -0
  80. package/lib/public/favicon-banded.png +0 -0
  81. package/lib/public/favicon-dark.svg +32 -0
  82. package/lib/public/favicon.svg +32 -0
  83. package/lib/public/icon-192.png +0 -0
  84. package/lib/public/icon-512.png +0 -0
  85. package/lib/public/icon-banded-76.png +0 -0
  86. package/lib/public/icon-banded-96.png +0 -0
  87. package/lib/public/icon-mono.svg +25 -0
  88. package/lib/public/icons/email/gmail.svg +7 -0
  89. package/lib/public/icons/email/outlook.svg +35 -0
  90. package/lib/public/icons/email/yahoo.svg +1 -0
  91. package/lib/public/index.html +1946 -0
  92. package/lib/public/manifest.json +27 -0
  93. package/lib/public/markdown-worker.js +43 -0
  94. package/lib/public/marked.min.js +6 -0
  95. package/lib/public/modules/admin.js +954 -0
  96. package/lib/public/modules/agent-picker.js +429 -0
  97. package/lib/public/modules/app-connection.js +216 -0
  98. package/lib/public/modules/app-cursors.js +492 -0
  99. package/lib/public/modules/app-dm.js +265 -0
  100. package/lib/public/modules/app-favicon.js +231 -0
  101. package/lib/public/modules/app-header.js +242 -0
  102. package/lib/public/modules/app-home-hub.js +432 -0
  103. package/lib/public/modules/app-loop-ui.js +691 -0
  104. package/lib/public/modules/app-loop-wizard.js +439 -0
  105. package/lib/public/modules/app-messages.js +1415 -0
  106. package/lib/public/modules/app-misc.js +463 -0
  107. package/lib/public/modules/app-notifications.js +651 -0
  108. package/lib/public/modules/app-panels.js +1061 -0
  109. package/lib/public/modules/app-projects.js +875 -0
  110. package/lib/public/modules/app-rate-limit.js +529 -0
  111. package/lib/public/modules/app-rendering.js +739 -0
  112. package/lib/public/modules/app-skills-install.js +285 -0
  113. package/lib/public/modules/avatar.js +36 -0
  114. package/lib/public/modules/command-palette.js +491 -0
  115. package/lib/public/modules/console-logo.js +162 -0
  116. package/lib/public/modules/context-sources.js +365 -0
  117. package/lib/public/modules/diff.js +412 -0
  118. package/lib/public/modules/dom-refs.js +21 -0
  119. package/lib/public/modules/emoji-picker.js +222 -0
  120. package/lib/public/modules/events.js +21 -0
  121. package/lib/public/modules/filebrowser.js +1966 -0
  122. package/lib/public/modules/fileicons.js +172 -0
  123. package/lib/public/modules/icons.js +80 -0
  124. package/lib/public/modules/input.js +1102 -0
  125. package/lib/public/modules/longpress.js +74 -0
  126. package/lib/public/modules/markdown.js +486 -0
  127. package/lib/public/modules/mcp-ui.js +295 -0
  128. package/lib/public/modules/mention.js +1093 -0
  129. package/lib/public/modules/notifications.js +613 -0
  130. package/lib/public/modules/playbook.js +578 -0
  131. package/lib/public/modules/profile.js +594 -0
  132. package/lib/public/modules/project-settings.js +849 -0
  133. package/lib/public/modules/project-switcher.js +375 -0
  134. package/lib/public/modules/qrcode.js +87 -0
  135. package/lib/public/modules/rewind.js +381 -0
  136. package/lib/public/modules/scheduler-config.js +1611 -0
  137. package/lib/public/modules/scheduler-history.js +131 -0
  138. package/lib/public/modules/scheduler.js +1689 -0
  139. package/lib/public/modules/server-settings.js +842 -0
  140. package/lib/public/modules/session-search.js +456 -0
  141. package/lib/public/modules/settings-defaults.js +253 -0
  142. package/lib/public/modules/sidebar-mobile.js +1425 -0
  143. package/lib/public/modules/sidebar-projects.js +2022 -0
  144. package/lib/public/modules/sidebar-sessions.js +1548 -0
  145. package/lib/public/modules/sidebar-users.js +449 -0
  146. package/lib/public/modules/sidebar.js +348 -0
  147. package/lib/public/modules/skills.js +793 -0
  148. package/lib/public/modules/state.js +3 -0
  149. package/lib/public/modules/sticky-notes.js +1221 -0
  150. package/lib/public/modules/store.js +34 -0
  151. package/lib/public/modules/stt.js +266 -0
  152. package/lib/public/modules/terminal.js +987 -0
  153. package/lib/public/modules/theme.js +812 -0
  154. package/lib/public/modules/tool-palette.js +562 -0
  155. package/lib/public/modules/tools.js +2285 -0
  156. package/lib/public/modules/tooltip.js +150 -0
  157. package/lib/public/modules/user-settings.js +544 -0
  158. package/lib/public/modules/utils.js +56 -0
  159. package/lib/public/modules/ws-ref.js +26 -0
  160. package/lib/public/style.css +34 -0
  161. package/lib/public/sw.js +159 -0
  162. package/lib/push.js +149 -0
  163. package/lib/scheduler.js +471 -0
  164. package/lib/sdk-bridge.js +1792 -0
  165. package/lib/sdk-message-processor.js +740 -0
  166. package/lib/sdk-message-queue.js +42 -0
  167. package/lib/sdk-skill-discovery.js +131 -0
  168. package/lib/sdk-worker.js +677 -0
  169. package/lib/server-admin.js +712 -0
  170. package/lib/server-auth.js +737 -0
  171. package/lib/server-dm.js +170 -0
  172. package/lib/server-palette.js +104 -0
  173. package/lib/server-settings.js +553 -0
  174. package/lib/server-skills.js +280 -0
  175. package/lib/server.js +1390 -0
  176. package/lib/session-search.js +693 -0
  177. package/lib/sessions.js +946 -0
  178. package/lib/smtp.js +221 -0
  179. package/lib/terminal-manager.js +219 -0
  180. package/lib/terminal.js +39 -0
  181. package/lib/themes/ayu-light.json +9 -0
  182. package/lib/themes/catppuccin-latte.json +9 -0
  183. package/lib/themes/catppuccin-mocha.json +9 -0
  184. package/lib/themes/clagentic-dark.json +10 -0
  185. package/lib/themes/clagentic-light.json +10 -0
  186. package/lib/themes/dracula.json +9 -0
  187. package/lib/themes/everforest-light.json +9 -0
  188. package/lib/themes/everforest.json +9 -0
  189. package/lib/themes/github-light.json +9 -0
  190. package/lib/themes/gruvbox-dark.json +9 -0
  191. package/lib/themes/gruvbox-light.json +9 -0
  192. package/lib/themes/monokai.json +9 -0
  193. package/lib/themes/nord-light.json +9 -0
  194. package/lib/themes/nord.json +9 -0
  195. package/lib/themes/one-dark.json +9 -0
  196. package/lib/themes/one-light.json +9 -0
  197. package/lib/themes/rose-pine-dawn.json +9 -0
  198. package/lib/themes/rose-pine.json +9 -0
  199. package/lib/themes/solarized-dark.json +9 -0
  200. package/lib/themes/solarized-light.json +9 -0
  201. package/lib/themes/tokyo-night-light.json +9 -0
  202. package/lib/themes/tokyo-night.json +9 -0
  203. package/lib/updater.js +124 -0
  204. package/lib/user-presence.js +92 -0
  205. package/lib/users-auth.js +146 -0
  206. package/lib/users-permissions.js +118 -0
  207. package/lib/users-preferences.js +254 -0
  208. package/lib/users.js +465 -0
  209. package/lib/utils.js +64 -0
  210. package/lib/worktree.js +134 -0
  211. package/lib/ws-schema.js +464 -0
  212. package/lib/yoke/adapters/claude-worker.js +759 -0
  213. package/lib/yoke/adapters/claude.js +1599 -0
  214. package/lib/yoke/adapters/codex.js +1559 -0
  215. package/lib/yoke/codex-app-server.js +352 -0
  216. package/lib/yoke/index.js +319 -0
  217. package/lib/yoke/instructions.js +61 -0
  218. package/lib/yoke/interface.js +98 -0
  219. package/lib/yoke/mcp-bridge-server.js +302 -0
  220. package/lib/yoke/package.json +7 -0
  221. package/package.json +72 -0
@@ -0,0 +1,492 @@
1
+ // app-cursors.js - Remote cursor presence, text selection sharing
2
+ // Extracted from app.js (PR-27)
3
+
4
+ import { avatarUrl } from './avatar.js';
5
+ import { getWs, sendWsQuiet } from './ws-ref.js';
6
+ import { store } from './store.js';
7
+ import { getMessagesEl } from './dom-refs.js';
8
+ import { registerTooltip } from './tooltip.js';
9
+ import { getSessionPresenceUsers } from './sidebar-sessions.js';
10
+
11
+ // --- Module-owned state ---
12
+ var cursorSharingEnabled = localStorage.getItem("cursorSharing") !== "off";
13
+ var remoteCursors = {}; // userId -> { el, indicator, timer, lastY, active }
14
+ var cursorThrottleTimer = null;
15
+ var CURSOR_THROTTLE_MS = 30;
16
+ var CURSOR_HIDE_TIMEOUT = 5000;
17
+
18
+ var cursorColors = [
19
+ "#F24822", "#FF7262", "#A259FF", "#1ABCFE",
20
+ "#0ACF83", "#FF6D00", "#E84393", "#6C5CE7",
21
+ "#00B894", "#FDCB6E", "#E17055", "#74B9FF",
22
+ ];
23
+ var userColorMap = {};
24
+ var nextColorIdx = 0;
25
+
26
+ var remoteSelections = {}; // userId -> { els: [], timer }
27
+ var selectionThrottleTimer = null;
28
+ var lastSelectionKey = "";
29
+
30
+ // --- Internal helpers ---
31
+
32
+ function getCursorColor(userId) {
33
+ if (!userColorMap[userId]) {
34
+ userColorMap[userId] = cursorColors[nextColorIdx % cursorColors.length];
35
+ nextColorIdx++;
36
+ }
37
+ return userColorMap[userId];
38
+ }
39
+
40
+ function createCursorElement(userId, displayName, color, avatarStyle, avatarSeed, avatarCustom) {
41
+ var wrapper = document.createElement("div");
42
+ wrapper.className = "remote-cursor";
43
+ wrapper.dataset.userId = userId;
44
+ wrapper.style.position = "absolute";
45
+ wrapper.style.zIndex = "9999";
46
+ wrapper.style.pointerEvents = "none";
47
+ wrapper.style.display = "none";
48
+ wrapper.style.transition = "left 30ms linear, top 30ms linear";
49
+ wrapper.style.willChange = "left, top";
50
+
51
+ // SVG cursor arrow
52
+ var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
53
+ svg.setAttribute("width", "16");
54
+ svg.setAttribute("height", "20");
55
+ svg.setAttribute("viewBox", "0 0 16 20");
56
+ svg.style.display = "block";
57
+ svg.style.filter = "drop-shadow(0 1px 2px rgba(0,0,0,0.3))";
58
+ var path = document.createElementNS("http://www.w3.org/2000/svg", "path");
59
+ path.setAttribute("d", "M0 0 L0 16 L4.5 12 L8 19 L10.5 18 L7 11 L13 11 Z");
60
+ path.setAttribute("fill", color);
61
+ path.setAttribute("stroke", "#fff");
62
+ path.setAttribute("stroke-width", "1");
63
+ svg.appendChild(path);
64
+ wrapper.appendChild(svg);
65
+
66
+ // Tag: avatar + name label together
67
+ var tag = document.createElement("div");
68
+ tag.className = "remote-cursor-tag";
69
+ tag.style.cssText = "position:absolute;left:14px;top:14px;display:flex;align-items:center;" +
70
+ "gap:3px;background:" + color + ";padding:1px 6px 1px 2px;border-radius:10px;" +
71
+ "pointer-events:none;white-space:nowrap;";
72
+
73
+ // Avatar
74
+ var avatarImg = document.createElement("img");
75
+ avatarImg.className = "remote-cursor-avatar";
76
+ avatarImg.src = avatarCustom ? avatarCustom : avatarUrl(avatarStyle || "thumbs", avatarSeed || userId, 16);
77
+ avatarImg.style.cssText = "width:14px;height:14px;border-radius:50%;background:#fff;flex-shrink:0;";
78
+ tag.appendChild(avatarImg);
79
+
80
+ // Name label
81
+ var label = document.createElement("span");
82
+ label.className = "remote-cursor-label";
83
+ label.textContent = displayName;
84
+ label.style.cssText = "color:#fff;font-size:11px;font-weight:500;line-height:16px;font-family:inherit;";
85
+ tag.appendChild(label);
86
+
87
+ wrapper.appendChild(tag);
88
+
89
+ return wrapper;
90
+ }
91
+
92
+ // Compute cumulative character offset within a container element
93
+ function getCharOffset(container, targetNode, targetOffset) {
94
+ var walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT, null, false);
95
+ var offset = 0;
96
+ var node;
97
+ while ((node = walker.nextNode())) {
98
+ if (node === targetNode) {
99
+ return offset + targetOffset;
100
+ }
101
+ offset += node.textContent.length;
102
+ }
103
+ return offset;
104
+ }
105
+
106
+ // Find text node + local offset for a given cumulative character offset
107
+ function getNodeAtCharOffset(container, charOffset) {
108
+ var walker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT, null, false);
109
+ var consumed = 0;
110
+ var node;
111
+ var lastNode = null;
112
+ while ((node = walker.nextNode())) {
113
+ lastNode = node;
114
+ var len = node.textContent.length;
115
+ if (consumed + len >= charOffset) {
116
+ return { node: node, offset: Math.min(charOffset - consumed, len) };
117
+ }
118
+ consumed += len;
119
+ }
120
+ if (lastNode) {
121
+ return { node: lastNode, offset: lastNode.textContent.length };
122
+ }
123
+ return null;
124
+ }
125
+
126
+ // Find parent [data-turn] element from a DOM node
127
+ function findParentTurn(node) {
128
+ var messagesEl = getMessagesEl();
129
+ var el = node.nodeType === 3 ? node.parentElement : node;
130
+ while (el && el !== messagesEl) {
131
+ if (el.dataset && el.dataset.turn != null) return el;
132
+ el = el.parentElement;
133
+ }
134
+ return null;
135
+ }
136
+
137
+ function clearRemoteSelection(userId) {
138
+ var sel = remoteSelections[userId];
139
+ if (!sel) return;
140
+ for (var i = 0; i < sel.els.length; i++) {
141
+ if (sel.els[i].parentNode) sel.els[i].parentNode.removeChild(sel.els[i]);
142
+ }
143
+ sel.els = [];
144
+ }
145
+
146
+ function createOffscreenIndicator(userId, displayName, color) {
147
+ var btn = document.createElement("button");
148
+ btn.className = "remote-cursor-offscreen";
149
+ btn.dataset.userId = userId;
150
+ btn.style.cssText =
151
+ "position:absolute;left:50%;transform:translateX(-50%);" +
152
+ "z-index:10000;display:none;cursor:pointer;border:none;outline:none;" +
153
+ "background:" + color + ";color:#fff;font-size:11px;font-weight:500;" +
154
+ "padding:3px 10px 3px 8px;border-radius:12px;white-space:nowrap;" +
155
+ "font-family:inherit;line-height:16px;opacity:0.9;" +
156
+ "box-shadow:0 2px 8px rgba(0,0,0,0.2);pointer-events:auto;" +
157
+ "transition:opacity 0.15s;";
158
+ btn.addEventListener("mouseenter", function () { btn.style.opacity = "1"; });
159
+ btn.addEventListener("mouseleave", function () { btn.style.opacity = "0.9"; });
160
+ return btn;
161
+ }
162
+
163
+ function updateCursorVisibility(entry) {
164
+ var messagesEl = getMessagesEl();
165
+ var visibleTop = messagesEl.scrollTop;
166
+ var visibleBottom = visibleTop + messagesEl.clientHeight;
167
+ var y = entry.lastY || 0;
168
+
169
+ if (y < visibleTop) {
170
+ entry.indicator.style.top = (visibleTop + 6) + "px";
171
+ entry.indicator.style.display = "";
172
+ } else if (y > visibleBottom) {
173
+ entry.indicator.style.top = (visibleBottom - 28) + "px";
174
+ entry.indicator.style.display = "";
175
+ } else {
176
+ entry.indicator.style.display = "none";
177
+ }
178
+ }
179
+
180
+ // Find the closest [data-turn] element to a given clientY
181
+ function findClosestTurn(clientY) {
182
+ var messagesEl = getMessagesEl();
183
+ var turns = messagesEl.querySelectorAll("[data-turn]");
184
+ if (!turns.length) return null;
185
+ // First: exact hit
186
+ for (var i = 0; i < turns.length; i++) {
187
+ var r = turns[i].getBoundingClientRect();
188
+ if (clientY >= r.top && clientY <= r.bottom) return turns[i];
189
+ }
190
+ // Second: closest by distance
191
+ var closest = null;
192
+ var closestDist = Infinity;
193
+ for (var j = 0; j < turns.length; j++) {
194
+ var rect = turns[j].getBoundingClientRect();
195
+ var mid = (rect.top + rect.bottom) / 2;
196
+ var dist = Math.abs(clientY - mid);
197
+ if (dist < closestDist) { closestDist = dist; closest = turns[j]; }
198
+ }
199
+ return closest;
200
+ }
201
+
202
+ function hasOtherSessionViewers() {
203
+ if (!store.get('isMultiUserMode')) return false;
204
+ var sessionId = store.get('activeSessionId');
205
+ if (!sessionId) return false;
206
+ var viewers = getSessionPresenceUsers(sessionId);
207
+ if (!viewers || viewers.length === 0) return false;
208
+ var myUserId = store.get('myUserId');
209
+ for (var i = 0; i < viewers.length; i++) {
210
+ if (!myUserId || viewers[i].id !== myUserId) return true;
211
+ }
212
+ return false;
213
+ }
214
+
215
+ function shouldBroadcastCollab() {
216
+ return cursorSharingEnabled && hasOtherSessionViewers();
217
+ }
218
+
219
+ // Cursor sharing toggle button in user island (multi-user only)
220
+ export function initCursorToggle() {
221
+ if (!store.get('isMultiUserMode')) return;
222
+ var actionsEl = document.querySelector(".user-island-actions");
223
+ if (!actionsEl) return;
224
+ if (document.getElementById("cursor-share-toggle")) return;
225
+
226
+ var btn = document.createElement("button");
227
+ btn.id = "cursor-share-toggle";
228
+ btn.className = "cursor-share-btn";
229
+ btn.innerHTML = '<i data-lucide="mouse-pointer-2"></i>';
230
+ var settingsBtn = document.getElementById("user-settings-btn");
231
+ if (settingsBtn) {
232
+ actionsEl.insertBefore(btn, settingsBtn);
233
+ } else {
234
+ actionsEl.appendChild(btn);
235
+ }
236
+
237
+ function updateToggleStyle() {
238
+ if (cursorSharingEnabled) {
239
+ btn.classList.remove("off");
240
+ btn.classList.add("on");
241
+ registerTooltip(btn, "Cursor sharing on");
242
+ } else {
243
+ btn.classList.remove("on");
244
+ btn.classList.add("off");
245
+ registerTooltip(btn, "Cursor sharing off");
246
+ }
247
+ }
248
+
249
+ updateToggleStyle();
250
+ lucide.createIcons({ nodes: [btn] });
251
+
252
+ btn.addEventListener("click", function () {
253
+ cursorSharingEnabled = !cursorSharingEnabled;
254
+ localStorage.setItem("cursorSharing", cursorSharingEnabled ? "on" : "off");
255
+ updateToggleStyle();
256
+ if (!cursorSharingEnabled) {
257
+ sendWsQuiet({ type: "cursor_leave" });
258
+ sendWsQuiet({ type: "text_select", ranges: [] });
259
+ }
260
+ });
261
+ }
262
+
263
+ // --- Exported functions ---
264
+
265
+ export function handleRemoteSelection(msg) {
266
+ var messagesEl = getMessagesEl();
267
+ var userId = msg.userId;
268
+ var color = getCursorColor(userId);
269
+
270
+ if (!remoteSelections[userId]) {
271
+ remoteSelections[userId] = { els: [], timer: null };
272
+ }
273
+
274
+ // Clear previous highlight
275
+ clearRemoteSelection(userId);
276
+
277
+ // If selection cleared, just remove
278
+ if (!msg.ranges || msg.ranges.length === 0) return;
279
+
280
+ var containerRect = messagesEl.getBoundingClientRect();
281
+
282
+ for (var r = 0; r < msg.ranges.length; r++) {
283
+ var sel = msg.ranges[r];
284
+ var startTurnEl = messagesEl.querySelector('[data-turn="' + sel.startTurn + '"]');
285
+ var endTurnEl = messagesEl.querySelector('[data-turn="' + sel.endTurn + '"]');
286
+ if (!startTurnEl || !endTurnEl) continue;
287
+
288
+ var startResult = getNodeAtCharOffset(startTurnEl, sel.startCh);
289
+ var endResult = getNodeAtCharOffset(endTurnEl, sel.endCh);
290
+ if (!startResult || !endResult) continue;
291
+
292
+ try {
293
+ var range = document.createRange();
294
+ range.setStart(startResult.node, startResult.offset);
295
+ range.setEnd(endResult.node, endResult.offset);
296
+ var rects = range.getClientRects();
297
+
298
+ for (var i = 0; i < rects.length; i++) {
299
+ var rect = rects[i];
300
+ if (rect.width === 0 && rect.height === 0) continue;
301
+ var highlight = document.createElement("div");
302
+ highlight.className = "remote-selection";
303
+ highlight.dataset.userId = userId;
304
+ highlight.style.cssText =
305
+ "position:absolute;pointer-events:none;z-index:9998;" +
306
+ "background:" + color + ";" +
307
+ "opacity:0.2;" +
308
+ "border-radius:2px;" +
309
+ "left:" + (rect.left - containerRect.left + messagesEl.scrollLeft) + "px;" +
310
+ "top:" + (rect.top - containerRect.top + messagesEl.scrollTop) + "px;" +
311
+ "width:" + rect.width + "px;" +
312
+ "height:" + rect.height + "px;";
313
+ messagesEl.appendChild(highlight);
314
+ remoteSelections[userId].els.push(highlight);
315
+ }
316
+ } catch (e) {}
317
+ }
318
+
319
+ // Auto-hide after timeout
320
+ if (remoteSelections[userId].timer) clearTimeout(remoteSelections[userId].timer);
321
+ remoteSelections[userId].timer = setTimeout(function () {
322
+ clearRemoteSelection(userId);
323
+ }, 10000);
324
+ }
325
+
326
+ export function handleRemoteCursorMove(msg) {
327
+ var messagesEl = getMessagesEl();
328
+ var userId = msg.userId;
329
+
330
+ var entry = remoteCursors[userId];
331
+ if (!entry) {
332
+ var color = getCursorColor(userId);
333
+ var el = createCursorElement(userId, msg.displayName, color, msg.avatarStyle, msg.avatarSeed, msg.avatarCustom);
334
+ messagesEl.appendChild(el);
335
+ var indicator = createOffscreenIndicator(userId, msg.displayName, color);
336
+ messagesEl.appendChild(indicator);
337
+ entry = { el: el, indicator: indicator, timer: null, lastY: 0, active: false };
338
+ remoteCursors[userId] = entry;
339
+
340
+ indicator.addEventListener("click", function () {
341
+ messagesEl.scrollTo({ top: entry.lastY - messagesEl.clientHeight / 2, behavior: "smooth" });
342
+ });
343
+ }
344
+
345
+ // Find the same turn element on this screen
346
+ var anchorEl = null;
347
+ if (msg.turn != null) {
348
+ anchorEl = messagesEl.querySelector('[data-turn="' + msg.turn + '"]');
349
+ }
350
+
351
+ if (anchorEl && msg.rx != null && msg.ry != null) {
352
+ var x = anchorEl.offsetLeft + msg.rx * anchorEl.offsetWidth;
353
+ var y = anchorEl.offsetTop + msg.ry * anchorEl.offsetHeight;
354
+ entry.lastY = y;
355
+ entry.active = true;
356
+
357
+ // Update indicator label (direction set by updateCursorVisibility)
358
+ entry.indicator.textContent = (y < messagesEl.scrollTop ? "▲ " : "▼ ") + (msg.displayName || userId);
359
+
360
+ entry.el.style.left = x + "px";
361
+ entry.el.style.top = y + "px";
362
+ entry.el.style.display = "";
363
+
364
+ updateCursorVisibility(entry);
365
+ }
366
+
367
+ // Reset hide timer
368
+ if (entry.timer) clearTimeout(entry.timer);
369
+ entry.timer = setTimeout(function () {
370
+ entry.el.style.display = "none";
371
+ entry.indicator.style.display = "none";
372
+ entry.active = false;
373
+ }, CURSOR_HIDE_TIMEOUT);
374
+ }
375
+
376
+ export function handleRemoteCursorLeave(msg) {
377
+ var entry = remoteCursors[msg.userId];
378
+ if (entry) {
379
+ entry.el.style.display = "none";
380
+ entry.indicator.style.display = "none";
381
+ entry.active = false;
382
+ if (entry.timer) clearTimeout(entry.timer);
383
+ }
384
+ }
385
+
386
+ export function clearRemoteCursors() {
387
+ for (var uid in remoteCursors) {
388
+ var entry = remoteCursors[uid];
389
+ if (entry.timer) clearTimeout(entry.timer);
390
+ if (entry.el.parentNode) entry.el.parentNode.removeChild(entry.el);
391
+ if (entry.indicator && entry.indicator.parentNode) entry.indicator.parentNode.removeChild(entry.indicator);
392
+ }
393
+ remoteCursors = {};
394
+ for (var uid2 in remoteSelections) {
395
+ clearRemoteSelection(uid2);
396
+ if (remoteSelections[uid2].timer) clearTimeout(remoteSelections[uid2].timer);
397
+ }
398
+ remoteSelections = {};
399
+ }
400
+
401
+ export function initCursors() {
402
+ var messagesEl = getMessagesEl();
403
+
404
+ initCursorToggle();
405
+
406
+ // Track local cursor and send to server
407
+ messagesEl.addEventListener("mousemove", function (e) {
408
+ if (!shouldBroadcastCollab()) return;
409
+ var ws = getWs();
410
+ if (!ws || ws.readyState !== 1) return;
411
+ if (cursorThrottleTimer) return;
412
+ cursorThrottleTimer = setTimeout(function () { cursorThrottleTimer = null; }, CURSOR_THROTTLE_MS);
413
+
414
+ // Find which turn element the cursor is over
415
+ var turnEl = findClosestTurn(e.clientY);
416
+ if (!turnEl) return;
417
+
418
+ // Calculate ratio within the turn element
419
+ var turnRect = turnEl.getBoundingClientRect();
420
+ var rx = turnRect.width > 0 ? (e.clientX - turnRect.left) / turnRect.width : 0;
421
+ var ry = turnRect.height > 0 ? (e.clientY - turnRect.top) / turnRect.height : 0;
422
+
423
+ sendWsQuiet({
424
+ type: "cursor_move",
425
+ turn: parseInt(turnEl.dataset.turn, 10),
426
+ rx: Math.max(0, Math.min(1, rx)),
427
+ ry: Math.max(0, Math.min(1, ry))
428
+ });
429
+ });
430
+
431
+ messagesEl.addEventListener("mouseleave", function () {
432
+ if (!shouldBroadcastCollab()) return;
433
+ sendWsQuiet({ type: "cursor_leave" });
434
+ });
435
+
436
+ // Update offscreen indicators on scroll
437
+ messagesEl.addEventListener("scroll", function () {
438
+ for (var uid in remoteCursors) {
439
+ var entry = remoteCursors[uid];
440
+ if (!entry.active) continue;
441
+ updateCursorVisibility(entry);
442
+ }
443
+ });
444
+
445
+ // Track local text selection and send to server
446
+ document.addEventListener("selectionchange", function () {
447
+ if (!shouldBroadcastCollab()) return;
448
+ var ws = getWs();
449
+ if (!ws || ws.readyState !== 1) return;
450
+ if (selectionThrottleTimer) return;
451
+ selectionThrottleTimer = setTimeout(function () { selectionThrottleTimer = null; }, 100);
452
+
453
+ var activeEl = document.activeElement;
454
+ if (activeEl && activeEl.id === "input" && lastSelectionKey === "") return;
455
+
456
+ var sel = window.getSelection();
457
+ if (!sel || sel.rangeCount === 0 || sel.isCollapsed) {
458
+ // Selection cleared
459
+ if (lastSelectionKey !== "") {
460
+ lastSelectionKey = "";
461
+ sendWsQuiet({ type: "text_select", ranges: [] });
462
+ }
463
+ return;
464
+ }
465
+
466
+ var ranges = [];
467
+ for (var i = 0; i < sel.rangeCount; i++) {
468
+ var range = sel.getRangeAt(i);
469
+ var startTurn = findParentTurn(range.startContainer);
470
+ var endTurn = findParentTurn(range.endContainer);
471
+ if (!startTurn || !endTurn) continue;
472
+ // Both must be inside messagesEl
473
+ if (!messagesEl.contains(startTurn)) continue;
474
+
475
+ var startCh = getCharOffset(startTurn, range.startContainer, range.startOffset);
476
+ var endCh = getCharOffset(endTurn, range.endContainer, range.endOffset);
477
+
478
+ ranges.push({
479
+ startTurn: parseInt(startTurn.dataset.turn, 10),
480
+ startCh: startCh,
481
+ endTurn: parseInt(endTurn.dataset.turn, 10),
482
+ endCh: endCh
483
+ });
484
+ }
485
+
486
+ var key = JSON.stringify(ranges);
487
+ if (key === lastSelectionKey) return;
488
+ lastSelectionKey = key;
489
+
490
+ sendWsQuiet({ type: "text_select", ranges: ranges });
491
+ });
492
+ }