@clagentic/console 1.6.0-beta.1 → 1.6.0-beta.2

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 (37) hide show
  1. package/README.md +18 -0
  2. package/lib/daemon.js +94 -0
  3. package/lib/project-sessions.js +26 -0
  4. package/lib/public/css/base.css +18 -0
  5. package/lib/public/css/diagnostics.css +8 -0
  6. package/lib/public/css/icon-strip.css +201 -0
  7. package/lib/public/css/menus.css +2 -2
  8. package/lib/public/css/mobile-nav.css +24 -0
  9. package/lib/public/css/server-settings.css +100 -0
  10. package/lib/public/index.html +13 -0
  11. package/lib/public/modules/app-header.js +4 -7
  12. package/lib/public/modules/app-home-hub.js +31 -7
  13. package/lib/public/modules/app-messages.js +5 -0
  14. package/lib/public/modules/app-notifications.js +5 -1
  15. package/lib/public/modules/app-projects.js +10 -5
  16. package/lib/public/modules/command-palette.js +16 -5
  17. package/lib/public/modules/custom-icons-settings.js +399 -0
  18. package/lib/public/modules/custom-icons.js +114 -0
  19. package/lib/public/modules/diagnostics.js +10 -0
  20. package/lib/public/modules/emoji-picker.js +253 -6
  21. package/lib/public/modules/popover-position.js +192 -0
  22. package/lib/public/modules/project-icon.js +113 -0
  23. package/lib/public/modules/project-settings.js +2 -2
  24. package/lib/public/modules/project-switcher.js +2 -1
  25. package/lib/public/modules/scheduler-config.js +5 -8
  26. package/lib/public/modules/scheduler.js +5 -6
  27. package/lib/public/modules/server-settings.js +6 -0
  28. package/lib/public/modules/sidebar-mobile.js +4 -6
  29. package/lib/public/modules/sidebar-projects.js +35 -90
  30. package/lib/public/modules/sidebar-sessions.js +6 -20
  31. package/lib/public/modules/sticky-notes.js +4 -4
  32. package/lib/public/modules/terminal.js +5 -12
  33. package/lib/server-settings.js +221 -1
  34. package/lib/server.js +2 -0
  35. package/lib/sessions.js +16 -9
  36. package/lib/ws-schema.js +2 -0
  37. package/package.json +2 -1
@@ -2,8 +2,11 @@
2
2
  // Replaces hand-curated EMOJI_CATEGORIES in sidebar-projects.js / project-settings.js
3
3
 
4
4
  import { parseEmojis } from './markdown.js';
5
+ import { showToast } from './utils.js';
6
+ import { slugifyFilename, fetchCustomIconList, uploadCustomIcon, deleteCustomIcon, matchesCustomIconQuery } from './custom-icons.js';
5
7
 
6
- // Group metadata matching emojibase-data group numbers
8
+ // Group metadata matching emojibase-data group numbers.
9
+ // "custom" is a string key (not a numeric group) so it never collides with emojibase groups.
7
10
  var GROUP_META = [
8
11
  { g: 0, icon: "😀", label: "Smileys & Emotion" },
9
12
  { g: 1, icon: "🧑", label: "People & Body" },
@@ -14,6 +17,7 @@ var GROUP_META = [
14
17
  { g: 7, icon: "💡", label: "Objects" },
15
18
  { g: 8, icon: "❤️", label: "Symbols" },
16
19
  { g: 9, icon: "🏁", label: "Flags" },
20
+ { g: "custom", icon: "🖼", label: "Custom" },
17
21
  ];
18
22
 
19
23
  // Cached data promise — fetched once
@@ -137,10 +141,17 @@ export function buildEmojiPicker(opts) {
137
141
 
138
142
  var currentCatIdx = 0;
139
143
 
140
- function buildGrid(emojis) {
144
+ // customMatches (optional) — custom-icon {slug, url} entries to render as
145
+ // image tiles alongside plain-text emoji results (lr-0847: search previously
146
+ // never included custom icons at all, so "console" could never find
147
+ // ":clagentic-console:" regardless of query). Appended after the emoji
148
+ // results, reusing the same select-on-click behavior as the Custom tab.
149
+ function buildGrid(emojis, customMatches) {
141
150
  grid.innerHTML = "";
142
151
  noResults.style.display = "none";
143
- if (!emojis || emojis.length === 0) {
152
+ var hasEmojis = emojis && emojis.length > 0;
153
+ var hasCustom = customMatches && customMatches.length > 0;
154
+ if (!hasEmojis && !hasCustom) {
144
155
  noResults.style.display = "block";
145
156
  return;
146
157
  }
@@ -157,24 +168,260 @@ export function buildEmojiPicker(opts) {
157
168
  frag.appendChild(btn);
158
169
  })(emojis[i]);
159
170
  }
171
+ if (hasCustom) {
172
+ for (var ci = 0; ci < customMatches.length; ci++) {
173
+ (function (entry) {
174
+ var btn = document.createElement("button");
175
+ btn.className = "emoji-picker-item emoji-picker-item-custom";
176
+ btn.title = ":" + entry.slug + ":";
177
+ var img = document.createElement("img");
178
+ img.src = entry.url;
179
+ img.alt = ":" + entry.slug + ":";
180
+ img.className = "emoji-picker-custom-img";
181
+ btn.appendChild(img);
182
+ btn.addEventListener("click", function (e) {
183
+ e.stopPropagation();
184
+ onSelect(":" + entry.slug + ":");
185
+ });
186
+ frag.appendChild(btn);
187
+ })(customMatches[ci]);
188
+ }
189
+ }
160
190
  grid.appendChild(frag);
161
191
  parseEmojis(grid);
162
192
  scrollArea.scrollTop = 0;
163
193
  }
164
194
 
195
+ // --- Custom emoji panel ---
196
+
197
+ // Cached list of uploads from GET /api/custom-emoji
198
+ var customList = null; // null = not fetched yet
199
+
200
+ function fetchCustomList(cb) {
201
+ fetchCustomIconList().then(function (list) {
202
+ customList = list;
203
+ if (cb) cb(customList);
204
+ });
205
+ }
206
+
207
+ function buildCustomPanel() {
208
+ grid.innerHTML = "";
209
+ noResults.style.display = "none";
210
+
211
+ var panel = document.createElement("div");
212
+ panel.className = "emoji-picker-custom-panel";
213
+
214
+ // Hidden file input (opened by upload tile click)
215
+ var fileInput = document.createElement("input");
216
+ fileInput.type = "file";
217
+ fileInput.accept = "image/png,image/jpeg,image/gif,image/webp";
218
+ fileInput.style.display = "none";
219
+ panel.appendChild(fileInput);
220
+
221
+ // Slug confirm dialog (shown inline before upload)
222
+ var slugConfirm = document.createElement("div");
223
+ slugConfirm.className = "emoji-picker-slug-confirm hidden";
224
+ slugConfirm.innerHTML =
225
+ '<label class="emoji-picker-slug-label">Slug</label>' +
226
+ '<input type="text" class="emoji-picker-slug-input" maxlength="64" spellcheck="false" autocomplete="off">' +
227
+ '<div class="emoji-picker-slug-actions">' +
228
+ '<button class="emoji-picker-slug-cancel">Cancel</button>' +
229
+ '<button class="emoji-picker-slug-ok primary">Upload</button>' +
230
+ '</div>';
231
+ panel.appendChild(slugConfirm);
232
+
233
+ var slugInput = slugConfirm.querySelector(".emoji-picker-slug-input");
234
+ var slugOk = slugConfirm.querySelector(".emoji-picker-slug-ok");
235
+ var slugCancel = slugConfirm.querySelector(".emoji-picker-slug-cancel");
236
+ var pendingFile = null;
237
+
238
+ function showSlugConfirm(file) {
239
+ pendingFile = file;
240
+ slugInput.value = slugifyFilename(file.name);
241
+ slugConfirm.classList.remove("hidden");
242
+ slugInput.focus();
243
+ slugInput.select();
244
+ }
245
+
246
+ function hideSlugConfirm() {
247
+ slugConfirm.classList.add("hidden");
248
+ pendingFile = null;
249
+ }
250
+
251
+ function doUpload() {
252
+ if (!pendingFile) return;
253
+ var slug = slugInput.value.trim().toLowerCase()
254
+ .replace(/[^a-z0-9_-]+/g, "-")
255
+ .replace(/^-+|-+$/g, "")
256
+ .slice(0, 64);
257
+ if (!slug || !/^[a-z0-9_-]{1,64}$/.test(slug)) return;
258
+
259
+ // Check for duplicate in the already-fetched list
260
+ var existing = customList || [];
261
+ var hasDup = existing.some(function (e) { return e.slug === slug; });
262
+
263
+ function proceed() {
264
+ hideSlugConfirm();
265
+ uploadCustomIcon(slug, pendingFile).then(function (result) {
266
+ if (!result.ok) {
267
+ // lr-0847: the picker previously swallowed upload failures here
268
+ // (bare `return`), so a failed upload looked identical to a
269
+ // successful one from the user's perspective — nothing happened,
270
+ // no icon appeared, no explanation. Settings surfaces the same
271
+ // uploadCustomIcon() failure via window.alert; use the app's
272
+ // shared toast here since the picker is a transient popover, not
273
+ // a modal dialog.
274
+ showToast(result.error || "Upload failed", "warn");
275
+ return;
276
+ }
277
+ // Refresh the custom grid after upload
278
+ customList = null;
279
+ fetchCustomList(function () { buildCustomPanel(); renderCustomGrid(); });
280
+ });
281
+ }
282
+
283
+ if (hasDup) {
284
+ // Confirm overwrite
285
+ if (window.confirm(":" + slug + ": already exists — overwrite?")) {
286
+ proceed();
287
+ } else {
288
+ hideSlugConfirm();
289
+ }
290
+ } else {
291
+ proceed();
292
+ }
293
+ }
294
+
295
+ slugOk.addEventListener("click", function (e) { e.stopPropagation(); doUpload(); });
296
+ slugCancel.addEventListener("click", function (e) { e.stopPropagation(); hideSlugConfirm(); });
297
+ slugInput.addEventListener("keydown", function (e) {
298
+ e.stopPropagation();
299
+ if (e.key === "Enter") { e.preventDefault(); doUpload(); }
300
+ if (e.key === "Escape") { e.preventDefault(); hideSlugConfirm(); }
301
+ });
302
+ // Prevent slug input clicks from bubbling to the picker's document-click close handler
303
+ slugInput.addEventListener("click", function (e) { e.stopPropagation(); });
304
+
305
+ fileInput.addEventListener("change", function () {
306
+ var file = fileInput.files && fileInput.files[0];
307
+ fileInput.value = ""; // reset so same file can be re-selected
308
+ if (!file) return;
309
+ showSlugConfirm(file);
310
+ });
311
+
312
+ // Grid of existing uploads
313
+ var customGrid = document.createElement("div");
314
+ customGrid.className = "emoji-picker-grid";
315
+ panel.appendChild(customGrid);
316
+
317
+ function renderCustomGrid() {
318
+ customGrid.innerHTML = "";
319
+
320
+ // Upload tile (always first)
321
+ var uploadTile = document.createElement("button");
322
+ uploadTile.className = "emoji-picker-item emoji-picker-upload-tile";
323
+ uploadTile.title = "Upload image";
324
+ uploadTile.textContent = "+";
325
+ uploadTile.addEventListener("click", function (e) {
326
+ e.stopPropagation();
327
+ fileInput.click();
328
+ });
329
+ customGrid.appendChild(uploadTile);
330
+
331
+ var list = customList || [];
332
+ for (var ci = 0; ci < list.length; ci++) {
333
+ (function (entry) {
334
+ var tile = document.createElement("div");
335
+ tile.className = "emoji-picker-custom-tile";
336
+
337
+ var img = document.createElement("img");
338
+ img.src = entry.url;
339
+ img.alt = ":" + entry.slug + ":";
340
+ img.className = "emoji-picker-custom-img";
341
+ img.title = ":" + entry.slug + ":";
342
+ img.addEventListener("click", function (e) {
343
+ e.stopPropagation();
344
+ onSelect(":" + entry.slug + ":");
345
+ });
346
+ tile.appendChild(img);
347
+
348
+ var delBtn = document.createElement("button");
349
+ delBtn.className = "emoji-picker-custom-delete";
350
+ delBtn.title = "Delete";
351
+ delBtn.textContent = "×";
352
+ delBtn.addEventListener("click", function (e) {
353
+ e.stopPropagation();
354
+ deleteCustomIcon(entry.slug).then(function () {
355
+ customList = null;
356
+ fetchCustomList(function () { buildCustomPanel(); renderCustomGrid(); });
357
+ });
358
+ });
359
+ tile.appendChild(delBtn);
360
+
361
+ customGrid.appendChild(tile);
362
+ })(list[ci]);
363
+ }
364
+
365
+ if (list.length === 0) {
366
+ var emptyHint = document.createElement("div");
367
+ emptyHint.className = "emoji-picker-custom-empty";
368
+ emptyHint.textContent = "No custom icons yet — click + to upload";
369
+ emptyHint.style.gridColumn = "1 / -1";
370
+ customGrid.appendChild(emptyHint);
371
+ }
372
+ }
373
+
374
+ // Attach renderCustomGrid to the panel so switchCategory can re-invoke it
375
+ panel._renderCustomGrid = renderCustomGrid;
376
+
377
+ if (customList !== null) {
378
+ renderCustomGrid();
379
+ } else {
380
+ fetchCustomList(function () { renderCustomGrid(); });
381
+ }
382
+
383
+ grid.appendChild(panel);
384
+ scrollArea.scrollTop = 0;
385
+ }
386
+
165
387
  function switchCategory(idx) {
166
388
  currentCatIdx = idx;
167
389
  for (var j = 0; j < tabBtns.length; j++) {
168
390
  tabBtns[j].classList.toggle("active", j === idx);
169
391
  }
170
- if (_byGroup) {
171
- buildGrid(_byGroup[GROUP_META[idx].g] || []);
392
+ var meta = GROUP_META[idx];
393
+ if (meta.g === "custom") {
394
+ buildCustomPanel();
395
+ } else if (_byGroup) {
396
+ buildGrid(_byGroup[meta.g] || []);
397
+ }
398
+ }
399
+
400
+ function matchingCustomIcons(query) {
401
+ var list = customList || [];
402
+ var matches = [];
403
+ for (var i = 0; i < list.length; i++) {
404
+ if (matchesCustomIconQuery(list[i].slug, query)) matches.push(list[i]);
172
405
  }
406
+ return matches;
173
407
  }
174
408
 
175
409
  function showSearchResults(query) {
176
410
  tabBar.style.display = "none";
177
- buildGrid(searchEmojis(query));
411
+ var emojiMatches = searchEmojis(query);
412
+ if (customList === null) {
413
+ // Custom list not fetched yet (user hasn't opened the Custom tab this
414
+ // session) — show emoji matches immediately, then re-render with
415
+ // custom-icon matches once the list arrives so search never blocks on it.
416
+ buildGrid(emojiMatches, []);
417
+ fetchCustomList(function () {
418
+ if (searchInput.value.trim() === query.trim()) {
419
+ buildGrid(emojiMatches, matchingCustomIcons(query));
420
+ }
421
+ });
422
+ return;
423
+ }
424
+ buildGrid(emojiMatches, matchingCustomIcons(query));
178
425
  }
179
426
 
180
427
  function showCategory() {
@@ -0,0 +1,192 @@
1
+ // popover-position.js — Shared viewport-clamp positioning util for popovers,
2
+ // context menus, and dropdowns (lr-a10a).
3
+ //
4
+ // BACKGROUND: many popup call sites positioned themselves via
5
+ // getBoundingClientRect() + inline style.top/left, clamping only the
6
+ // right/bottom edges (or, in some cases, no edge at all). On mobile the
7
+ // anchor is often near the bottom or right edge of a narrow viewport (e.g.
8
+ // the fixed icon-strip, a low context-menu trigger, a synthetic
9
+ // contextmenu from a mobile action-sheet), so the popup could still land
10
+ // with its top or left edge off-screen. lr-149e fixed this one-off for the
11
+ // emoji picker (positionEmojiPicker in sidebar-projects.js); this module
12
+ // generalizes that fix into a single reusable helper so every affected
13
+ // popup routes through the same math instead of reinventing it.
14
+ //
15
+ // The clamp math (clampRect) is DOM-free and unit-testable directly from
16
+ // Node; positionPopover / positionAtPoint are thin DOM wrappers used by
17
+ // browser call sites.
18
+
19
+ // Mobile breakpoint mirrors the @media (max-width: 768px) blocks used
20
+ // throughout the CSS (see icon-strip.css, etc).
21
+ export var MOBILE_BREAKPOINT = 768;
22
+
23
+ /**
24
+ * True when the current viewport width is at or below the mobile breakpoint.
25
+ * @returns {boolean}
26
+ */
27
+ export function isMobileViewport() {
28
+ return window.innerWidth <= MOBILE_BREAKPOINT;
29
+ }
30
+
31
+ /**
32
+ * Clamp a popover rect to the viewport on all four edges.
33
+ *
34
+ * Pure function — no DOM access — so it can be unit-tested directly.
35
+ *
36
+ * @param {{top:number, left:number, width:number, height:number}} rect
37
+ * The popover's natural (unclamped) position and measured size.
38
+ * @param {{innerWidth:number, innerHeight:number}} viewport
39
+ * @param {object} [opts]
40
+ * @param {number} [opts.margin=8] Minimum gap to keep from every edge.
41
+ * @returns {{top:number, left:number}}
42
+ */
43
+ export function clampRect(rect, viewport, opts) {
44
+ var margin = (opts && typeof opts.margin === "number") ? opts.margin : 8;
45
+ var maxLeft = viewport.innerWidth - rect.width - margin;
46
+ var maxTop = viewport.innerHeight - rect.height - margin;
47
+
48
+ // Clamp left/right first.
49
+ var left = rect.left;
50
+ if (left > maxLeft) left = maxLeft;
51
+ if (left < margin) left = margin;
52
+
53
+ // Clamp top/bottom.
54
+ var top = rect.top;
55
+ if (top > maxTop) top = maxTop;
56
+ if (top < margin) top = margin;
57
+
58
+ return { top: top, left: left };
59
+ }
60
+
61
+ /**
62
+ * Compute a clamped position for a popover anchored to an element.
63
+ *
64
+ * Desktop behavior (preserves existing anchor-relative behavior): the
65
+ * popover opens at (preferredTop, preferredLeft) relative to the anchor
66
+ * rect, per `placement`, then gets flipped to the opposite side if it
67
+ * would overflow, then clamped on all four edges as a final safety net
68
+ * (this is the "prefer flip-then-clamp" requirement — flipping alone
69
+ * cannot leave the popover off-screen because the clamp always runs
70
+ * afterward).
71
+ *
72
+ * Mobile behavior: centers the popover in the viewport (matches the
73
+ * lr-149e emoji-picker fix) rather than anchoring, since low/edge anchors
74
+ * on narrow viewports frequently have no safe anchor-relative position.
75
+ *
76
+ * @param {{width:number, height:number}} popoverSize Measured popover size (getBoundingClientRect of the appended-but-unpositioned element).
77
+ * @param {{top:number, left:number, right:number, bottom:number}} anchorRect Anchor element's getBoundingClientRect().
78
+ * @param {{innerWidth:number, innerHeight:number}} viewport
79
+ * @param {object} [opts]
80
+ * @param {"right"|"below"|"below-right-aligned"} [opts.placement="right"]
81
+ * Preferred side: "right" of the anchor (default, matches existing
82
+ * ctx-menu behavior), "below" the anchor left-aligned, or
83
+ * "below-right-aligned" (below the anchor with the popover's right edge
84
+ * aligned to the anchor's right edge — matches session/loop ctx-menu
85
+ * `right: window.innerWidth - btnRect.right` anchoring).
86
+ * @param {number} [opts.gap=6] Gap between anchor and popover on the preferred side.
87
+ * @param {number} [opts.margin=8] Minimum gap from viewport edges.
88
+ * @param {boolean} [opts.mobile] Force mobile/desktop branch (defaults to viewport.innerWidth <= MOBILE_BREAKPOINT).
89
+ * @returns {{top:number, left:number}}
90
+ */
91
+ export function computePopoverPosition(popoverSize, anchorRect, viewport, opts) {
92
+ opts = opts || {};
93
+ var margin = typeof opts.margin === "number" ? opts.margin : 8;
94
+ var gap = typeof opts.gap === "number" ? opts.gap : 6;
95
+ var placement = opts.placement || "right";
96
+ var mobile = typeof opts.mobile === "boolean" ? opts.mobile : (viewport.innerWidth <= MOBILE_BREAKPOINT);
97
+
98
+ if (mobile) {
99
+ var left = Math.max(margin, (viewport.innerWidth - popoverSize.width) / 2);
100
+ var top = Math.max(margin, (viewport.innerHeight - popoverSize.height) / 2);
101
+ return clampRect({ top: top, left: left, width: popoverSize.width, height: popoverSize.height }, viewport, { margin: margin });
102
+ }
103
+
104
+ var top2, left2;
105
+ if (placement === "below-right-aligned") {
106
+ left2 = anchorRect.right - popoverSize.width;
107
+ top2 = anchorRect.bottom + gap;
108
+ // Flip above the anchor if it would overflow the bottom edge.
109
+ if (top2 + popoverSize.height > viewport.innerHeight - margin) {
110
+ top2 = anchorRect.top - popoverSize.height - gap;
111
+ }
112
+ } else if (placement === "below") {
113
+ left2 = anchorRect.left;
114
+ top2 = anchorRect.bottom + gap;
115
+ // Flip above the anchor if it would overflow the bottom edge.
116
+ if (top2 + popoverSize.height > viewport.innerHeight - margin) {
117
+ top2 = anchorRect.top - popoverSize.height - gap;
118
+ }
119
+ } else {
120
+ // placement === "right" (default)
121
+ left2 = anchorRect.right + gap;
122
+ top2 = anchorRect.top;
123
+ // Flip to the left of the anchor if it would overflow the right edge.
124
+ if (left2 + popoverSize.width > viewport.innerWidth - margin) {
125
+ left2 = anchorRect.left - popoverSize.width - gap;
126
+ }
127
+ }
128
+
129
+ // Final safety net: clamp on all four edges regardless of flip outcome,
130
+ // so a low/edge anchor (e.g. mobile bottom-sheet trigger) never leaves
131
+ // the popover with top < 0 or top + height > innerHeight (and same for
132
+ // left/right).
133
+ return clampRect({ top: top2, left: left2, width: popoverSize.width, height: popoverSize.height }, viewport, { margin: margin });
134
+ }
135
+
136
+ /**
137
+ * Compute a clamped position for a popover opened at an arbitrary point
138
+ * (e.g. a contextmenu event's clientX/clientY) rather than anchored to an
139
+ * element. Used by sites like terminal.js's right-click menu.
140
+ *
141
+ * @param {{width:number, height:number}} popoverSize
142
+ * @param {{x:number, y:number}} point
143
+ * @param {{innerWidth:number, innerHeight:number}} viewport
144
+ * @param {object} [opts]
145
+ * @param {number} [opts.margin=4]
146
+ * @returns {{top:number, left:number}}
147
+ */
148
+ export function computePointPosition(popoverSize, point, viewport, opts) {
149
+ var margin = (opts && typeof opts.margin === "number") ? opts.margin : 4;
150
+ return clampRect({ top: point.y, left: point.x, width: popoverSize.width, height: popoverSize.height }, viewport, { margin: margin });
151
+ }
152
+
153
+ /**
154
+ * DOM wrapper: position an already-appended (so it can be measured) popover
155
+ * element relative to an anchor element, applying the clamp math above, and
156
+ * write the result to inline styles.
157
+ *
158
+ * @param {HTMLElement} el The popover element (must already be appended to the DOM so getBoundingClientRect() reports real dimensions).
159
+ * @param {HTMLElement} anchorEl
160
+ * @param {object} [opts] See computePopoverPosition opts.
161
+ */
162
+ export function positionPopover(el, anchorEl, opts) {
163
+ var popRect = el.getBoundingClientRect();
164
+ var anchorRect = anchorEl.getBoundingClientRect();
165
+ var viewport = { innerWidth: window.innerWidth, innerHeight: window.innerHeight };
166
+ var pos = computePopoverPosition(
167
+ { width: popRect.width, height: popRect.height },
168
+ anchorRect,
169
+ viewport,
170
+ opts
171
+ );
172
+ el.style.position = "fixed";
173
+ el.style.left = pos.left + "px";
174
+ el.style.top = pos.top + "px";
175
+ }
176
+
177
+ /**
178
+ * DOM wrapper: position an already-appended popover element at a point
179
+ * (e.g. contextmenu clientX/clientY), applying the clamp math above.
180
+ *
181
+ * @param {HTMLElement} el
182
+ * @param {{x:number, y:number}} point
183
+ * @param {object} [opts] See computePointPosition opts.
184
+ */
185
+ export function positionAtPoint(el, point, opts) {
186
+ var popRect = el.getBoundingClientRect();
187
+ var viewport = { innerWidth: window.innerWidth, innerHeight: window.innerHeight };
188
+ var pos = computePointPosition({ width: popRect.width, height: popRect.height }, point, viewport, opts);
189
+ el.style.position = "fixed";
190
+ el.style.left = pos.left + "px";
191
+ el.style.top = pos.top + "px";
192
+ }
@@ -0,0 +1,113 @@
1
+ // project-icon.js — Shared helper for rendering project icons.
2
+ //
3
+ // A project icon is either:
4
+ // - A custom image, stored as `:slug:` in config.projects[].icon.
5
+ // Detect with isCustomIcon(); render as <img src="/api/custom-emoji/{slug}">.
6
+ // - A Unicode emoji (any other non-empty string).
7
+ // Render via the existing parseEmojis() path.
8
+ // - null / undefined / empty — caller shows an abbreviation fallback.
9
+ //
10
+ // The helpers here are DOM-free for the classification functions so they can
11
+ // be imported in Node test environments as well as the browser.
12
+
13
+ // Custom-icon sentinel pattern: :slug: where slug is [a-z0-9_-]{1,64}
14
+ var CUSTOM_ICON_RE = /^:[a-z0-9_-]{1,64}:$/;
15
+
16
+ /**
17
+ * Returns true when iconStr is a custom-image sentinel (:slug:).
18
+ * @param {string|null|undefined} iconStr
19
+ * @returns {boolean}
20
+ */
21
+ export function isCustomIcon(iconStr) {
22
+ if (!iconStr || typeof iconStr !== "string") return false;
23
+ return CUSTOM_ICON_RE.test(iconStr);
24
+ }
25
+
26
+ /**
27
+ * Extract the slug from a custom-icon sentinel.
28
+ * Returns null when iconStr is not a custom-icon sentinel.
29
+ * @param {string|null|undefined} iconStr
30
+ * @returns {string|null}
31
+ */
32
+ export function customIconSlug(iconStr) {
33
+ if (!isCustomIcon(iconStr)) return null;
34
+ return iconStr.slice(1, -1); // strip leading and trailing ":"
35
+ }
36
+
37
+ // Minimal HTML-escape for the string-composition path (projectIconHtml).
38
+ // Mirrors escapeHtml() from utils.js without importing it, so this module
39
+ // stays DOM-free / Node-importable (see file header).
40
+ function escapeForHtml(str) {
41
+ return String(str)
42
+ .replace(/&/g, "&amp;")
43
+ .replace(/</g, "&lt;")
44
+ .replace(/>/g, "&gt;")
45
+ .replace(/"/g, "&quot;")
46
+ .replace(/'/g, "&#39;");
47
+ }
48
+
49
+ /**
50
+ * Build a safe HTML string for a project/folder icon, for call sites that
51
+ * compose markup via string concatenation + innerHTML (e.g. command-palette.js)
52
+ * rather than direct DOM-API building.
53
+ *
54
+ * Behaviour mirrors renderProjectIcon():
55
+ * - Custom icon -> "<img src=... class="project-emoji-img">" markup.
56
+ * Safe to interpolate: customIconSlug() already validated the slug against
57
+ * CUSTOM_ICON_RE ([a-z0-9_-]{1,64}), so no raw user/icon string is ever
58
+ * concatenated into the returned markup.
59
+ * - Emoji string -> HTML-escaped text (defends against a non-sentinel icon
60
+ * value that isn't actually a safe emoji).
61
+ * - null/empty -> fallbackHtml (caller-supplied placeholder markup, e.g.
62
+ * a lucide `<i data-lucide="...">` icon).
63
+ *
64
+ * @param {string|null|undefined} iconStr
65
+ * @param {string} [fallbackHtml]
66
+ * @returns {string}
67
+ */
68
+ export function projectIconHtml(iconStr, fallbackHtml) {
69
+ if (!iconStr) return fallbackHtml || "";
70
+ var slug = customIconSlug(iconStr);
71
+ if (slug) {
72
+ // Inline onerror mirrors renderProjectIcon()'s delete-in-use fallback
73
+ // (hide the broken image) since this string-built markup has no JS
74
+ // event-listener wiring of its own. The handler is a fixed literal —
75
+ // nothing from iconStr is interpolated into it.
76
+ return '<img src="/api/custom-emoji/' + encodeURIComponent(slug) + '" alt=":' + slug + ':" class="project-emoji-img" onerror="this.style.display=&#39;none&#39;">';
77
+ }
78
+ return escapeForHtml(iconStr);
79
+ }
80
+
81
+ /**
82
+ * Render a project icon into the given DOM element.
83
+ *
84
+ * Behaviour:
85
+ * - Custom icon → replaces el content with <img src="/api/custom-emoji/{slug}"
86
+ * class="project-emoji-img"> (no innerHTML: safe against XSS).
87
+ * - Emoji string → sets el.textContent = iconStr, then calls parseEmojis(el).
88
+ * - null/empty → clears the element (caller may show an abbreviation).
89
+ *
90
+ * @param {string|null|undefined} iconStr The icon value from project config.
91
+ * @param {Element} el The container element to render into.
92
+ * @param {Function} [parseEmojisFn] parseEmojis from markdown.js (browser only).
93
+ */
94
+ export function renderProjectIcon(iconStr, el, parseEmojisFn) {
95
+ // Clear previous content
96
+ el.textContent = "";
97
+
98
+ if (!iconStr) return;
99
+
100
+ var slug = customIconSlug(iconStr);
101
+ if (slug) {
102
+ var img = document.createElement("img");
103
+ img.src = "/api/custom-emoji/" + encodeURIComponent(slug);
104
+ img.alt = ":" + slug + ":";
105
+ img.className = "project-emoji-img";
106
+ // Graceful fallback: if the image 404s (deleted-in-use), show nothing
107
+ img.addEventListener("error", function () { img.style.display = "none"; });
108
+ el.appendChild(img);
109
+ } else {
110
+ el.textContent = iconStr;
111
+ if (typeof parseEmojisFn === "function") parseEmojisFn(el);
112
+ }
113
+ }
@@ -3,6 +3,7 @@ import { refreshIcons } from './icons.js';
3
3
  import { buildEmojiPicker } from './emoji-picker.js';
4
4
  import { showToast } from './utils.js';
5
5
  import { parseEmojis } from './markdown.js';
6
+ import { renderProjectIcon } from './project-icon.js';
6
7
  import { pushOverlayState, popOverlayState } from './overlay-history.js';
7
8
  import { closeFileViewer } from './filebrowser.js';
8
9
  import { renderModelList, renderModeList, renderEffortBar, renderThinkingBar, renderBetaCard } from './settings-defaults.js';
@@ -546,8 +547,7 @@ function updateIconPreview(icon) {
546
547
  var preview = document.getElementById("ps-icon-preview");
547
548
  var removeBtn = document.getElementById("ps-icon-remove-btn");
548
549
  if (preview) {
549
- preview.textContent = icon || "";
550
- if (icon) parseEmojis(preview);
550
+ renderProjectIcon(icon || null, preview, parseEmojis);
551
551
  }
552
552
  if (removeBtn) {
553
553
  removeBtn.classList.toggle("hidden", !icon);
@@ -13,6 +13,7 @@ import { store } from './store.js';
13
13
  import { refreshIcons } from './icons.js';
14
14
  import { getCachedProjects, switchProject } from './app-projects.js';
15
15
  import { parseEmojis } from './markdown.js';
16
+ import { renderProjectIcon } from './project-icon.js';
16
17
 
17
18
  var _projectMru = []; // project slugs, most recent first
18
19
  var _overlay = null;
@@ -250,7 +251,7 @@ function buildEntryNode(entry, index) {
250
251
  var iconWrap = document.createElement('div');
251
252
  iconWrap.className = 'cmd-palette-item-icon';
252
253
  if (entry.icon) {
253
- iconWrap.textContent = entry.icon;
254
+ renderProjectIcon(entry.icon, iconWrap, parseEmojis);
254
255
  } else if (entry.avatarUrl) {
255
256
  var img = document.createElement('img');
256
257
  img.src = entry.avatarUrl;
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import { showToast } from './utils.js';
8
+ import { positionPopover } from './popover-position.js';
8
9
 
9
10
  // Constants (duplicated from scheduler.js — small arrays)
10
11
  var DAY_NAMES = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
@@ -313,15 +314,11 @@ export function setupCreateModal() {
313
314
  endBtn.addEventListener("click", function (e) {
314
315
  e.stopPropagation();
315
316
  if (endList.classList.contains("hidden")) {
316
- var r = endBtn.getBoundingClientRect();
317
- endList.style.left = r.left + "px";
318
- endList.style.top = (r.bottom + 4) + "px";
319
- // If it would overflow bottom, show above
320
317
  endList.classList.remove("hidden");
321
- var lr = endList.getBoundingClientRect();
322
- if (lr.bottom > window.innerHeight - 8) {
323
- endList.style.top = (r.top - lr.height - 4) + "px";
324
- }
318
+ // Four-edge clamp (lr-a10a): the prior bottom-flip-only logic could
319
+ // leave the flipped dropdown with top < 0 when the button sits near
320
+ // the top of a short viewport.
321
+ positionPopover(endList, endBtn, { placement: "below", gap: 4 });
325
322
  } else {
326
323
  endList.classList.add("hidden");
327
324
  }