zcode-pro 1.5.1 → 1.6.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.
@@ -893,6 +893,12 @@ let scanDebounce = 0;
893
893
  export function startWsRunningSpin() {
894
894
  if (installed || typeof document === 'undefined') return;
895
895
  installed = true;
896
+ // 加载中图标用默认灰压深一点点:既比默认灰显眼,又不像主题色那么跳
897
+ //(描边是 currentColor,改 color 即可)
898
+ const st = document.createElement('style');
899
+ st.id = '__zcodepro_ws_style__';
900
+ st.textContent = '.zcodepro-ws-loader{color:color-mix(in srgb, currentColor 85%, black) !important}';
901
+ document.head.append(st);
896
902
  void refreshConfig();
897
903
  pollConfig();
898
904
 
@@ -36,7 +36,19 @@ import { ensureStyle } from './ui.js';
36
36
  const boot = window.__ZCODEPRO__ || {};
37
37
  const sameGeneration = window.__zcodeproBootedToken && boot.token
38
38
  && window.__zcodeproBootedToken === boot.token;
39
- if (!sameGeneration) location.reload();
39
+ if (!sameGeneration) {
40
+ // 换代刷新限次(记在 sessionStorage,跨刷新存活):两个不同 token 的注入器
41
+ // 互相看对方不顺眼时,30 秒内最多刷 2 次,别进无限刷新
42
+ const key = 'zcodeproReloadLog';
43
+ let log = [];
44
+ try { log = JSON.parse(sessionStorage.getItem(key) || '[]'); } catch { /* ignore */ }
45
+ if (!Array.isArray(log)) log = [];
46
+ log = log.filter((ts) => Date.now() - ts < 30000);
47
+ if (log.length < 2) {
48
+ try { sessionStorage.setItem(key, JSON.stringify([...log, Date.now()])); } catch { /* ignore */ }
49
+ location.reload();
50
+ }
51
+ }
40
52
  return;
41
53
  }
42
54
  window.__zcodeproInjected = true;
package/src/inject/ui.js CHANGED
@@ -5,19 +5,40 @@ import { h, t } from './core.js';
5
5
 
6
6
  let toastTimer = null;
7
7
 
8
+ // 提示条:设置弹窗里触发的提示居中叠在弹窗上,其余居中显示在 ZCode 窗体中间。
9
+ // kind 区分成功 / 警告 / 失败 / 信息('ok' 是旧调用点的成功写法,按成功处理);
10
+ // 配色走 ensureStyle 的自有样式,不依赖宿主 Tailwind 生成配色工具类
11
+ const TOAST_KINDS = {
12
+ success: { name: 'success', glyph: '✓', ms: 2600 },
13
+ warning: { name: 'warning', glyph: '!', ms: 3600 },
14
+ error: { name: 'error', glyph: '✕', ms: 5000 },
15
+ info: { name: 'info', glyph: 'ⓘ', ms: 2600 },
16
+ };
17
+
8
18
  export function showToast(text, kind = 'info') {
9
19
  document.getElementById('__zcodepro_toast__')?.remove();
10
- const el = h('div', {
11
- id: '__zcodepro_toast__',
12
- class: 'fixed bottom-6 z-[100] flex max-w-md items-center gap-2 rounded-lg border px-4 py-3 text-ui-sm shadow-lg ' +
13
- (kind === 'error' ? 'border-destructive/40 bg-menu text-foreground' : 'border-border bg-menu text-foreground'),
14
- // left 用内联样式:ZCode 的 Tailwind 未编译 left-6/right-6 工具类(bottom-6 有)
15
- style: 'animation:zcodepro-fade-in .18s ease; left: 1.5rem;',
20
+ clearTimeout(toastTimer);
21
+ const v = TOAST_KINDS[kind === 'ok' ? 'success' : kind] || TOAST_KINDS.info;
22
+ const chip = h('div', {
23
+ class: `zcodepro-toast zcodepro-toast-${v.name}`,
24
+ style: 'animation:zcodepro-fade-in .18s ease;',
16
25
  },
26
+ h('span', { class: 'zcodepro-toast-glyph' }, v.glyph),
17
27
  h('span', { class: 'truncate' }, text));
18
- document.body.append(el);
19
- clearTimeout(toastTimer);
20
- toastTimer = setTimeout(() => el.remove(), kind === 'error' ? 5000 : 2600);
28
+ const wrap = h('div', { id: '__zcodepro_toast__', class: 'pointer-events-none flex items-center justify-center' });
29
+ // 设置弹窗可拖动、内部可滚动:按弹窗当前盒子定位(fixed),
30
+ // 提示始终落在可见弹窗的正中间;不属于弹窗的提示落到窗体正中间
31
+ const host = document.querySelector('[data-zcodepro-settings]');
32
+ if (host) {
33
+ const r = host.getBoundingClientRect();
34
+ wrap.style.cssText = `position:fixed; left:${Math.round(r.left)}px; top:${Math.round(r.top)}px;`
35
+ + ` width:${Math.round(r.width)}px; height:${Math.round(r.height)}px; z-index:100;`;
36
+ } else {
37
+ wrap.style.cssText = 'position:fixed; inset:0; z-index:100;';
38
+ }
39
+ wrap.append(chip);
40
+ document.body.append(wrap);
41
+ toastTimer = setTimeout(() => wrap.remove(), v.ms);
21
42
  }
22
43
 
23
44
  // 复用应用自身 AlertDialog 的真实类名组合(逆向自 app.asar 渲染层源码):
@@ -45,6 +66,10 @@ export function openDialog({ title, description, onMount, onClose, width = 'sm:m
45
66
  const prevActive = document.activeElement;
46
67
  let cleanupDrag = null;
47
68
  const close = () => {
69
+ // 先 blur 弹窗内焦点元素:输入框靠 change 提交,直接 remove 不触发 change,
70
+ // Esc 关弹窗会把没失焦的输入悄悄丢掉
71
+ const active = document.activeElement;
72
+ if (active && overlayEl.contains(active)) { try { active.blur(); } catch { /* ignore */ } }
48
73
  overlayEl.remove();
49
74
  dialogStack = dialogStack.filter((e) => e !== overlayEl);
50
75
  document.removeEventListener('keydown', onKey);
@@ -102,14 +127,40 @@ export function openDialog({ title, description, onMount, onClose, width = 'sm:m
102
127
  titleEl.style.cursor = 'move';
103
128
  titleEl.style.userSelect = 'none';
104
129
  let dragging = false, sx = 0, sy = 0, ox = 0, oy = 0;
105
- // 边界按遮罩实际范围算(Linux 下遮罩顶部让出自绘标题栏),不能按窗口算:
106
- // 否则卡片能被拖进标题栏下面,拖动把手和关闭按钮被标题栏盖住就都点不到了
130
+ // 应用自绘标题栏高度:标题栏带 -webkit-app-region:drag,是合成层区域、
131
+ // 在所有页面内容之上——卡片钻到它下面后拖动把手和关闭按钮就再也点不到。
132
+ // 实测 3.16.2 起宿主样式表不再编译 platform-linux-desktop:top-12,遮罩
133
+ // 让位旧机制失效,因此运行时实测标题栏(结果短缓存,视图切换后能跟上)
134
+ let tbCache = { at: 0, h: 0 };
135
+ const titleBarHeight = () => {
136
+ if (Date.now() - tbCache.at < 2000) return tbCache.h;
137
+ let h = 0;
138
+ try {
139
+ for (const el of document.querySelectorAll('[class*="app-region"], [style*="app-region"], header')) {
140
+ const st = getComputedStyle(el);
141
+ if ((st.webkitAppRegion || st.appRegion) !== 'drag') continue;
142
+ const r = el.getBoundingClientRect();
143
+ if (r.top <= 2 && r.height > 8 && r.height < 120) h = Math.max(h, r.bottom);
144
+ }
145
+ } catch { /* ignore */ }
146
+ tbCache = { at: Date.now(), h };
147
+ return h;
148
+ };
149
+ // 卡片未平移时的矩形(当前矩形扣除平移量)。平移边界按卡片实际边缘不出
150
+ // 窗体算(旧算法按遮罩内居中对称取值:顶部能钻进标题栏、底部也够不到边)
151
+ const baseRect = () => {
152
+ const r = content.getBoundingClientRect();
153
+ return { left: r.left - ox, top: r.top - oy, right: r.right - ox, bottom: r.bottom - oy };
154
+ };
155
+ // 单轴夹取:范围倒挂(卡片比可用空间还宽/高)时取中点,保证两边均可见
156
+ const clampAxis = (v, lo, hi) => (lo > hi ? (lo + hi) / 2 : Math.min(hi, Math.max(lo, v)));
107
157
  const clampPos = (x, y) => {
108
- const ob = overlayEl.getBoundingClientRect();
109
- // 卡片在遮罩内居中,平移量不能超过卡片边缘到遮罩边缘的距离(留 8px 余量)
110
- const mx = Math.max(0, (ob.width - content.offsetWidth) / 2 - 8);
111
- const my = Math.max(0, (ob.height - content.offsetHeight) / 2 - 8);
112
- return [Math.min(mx, Math.max(-mx, x)), Math.min(my, Math.max(-my, y))];
158
+ const M = 8; // 距窗体边缘的留白
159
+ const b = baseRect();
160
+ return [
161
+ clampAxis(x, M - b.left, (window.innerWidth - M) - b.right),
162
+ clampAxis(y, (titleBarHeight() + M) - b.top, (window.innerHeight - M) - b.bottom),
163
+ ];
113
164
  };
114
165
  const applyPos = () => {
115
166
  content.style.transform = (ox || oy) ? `translate(${ox}px, ${oy}px)` : '';
@@ -379,6 +430,23 @@ export function ensureStyle() {
379
430
  if (!root) return;
380
431
  root.append(h('style', { id: '__zcodepro_style__' }, `
381
432
  @keyframes zcodepro-fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
433
+ /* 提示条四态(成功/警告/失败/信息):配色用主题变量加字面量兜底,
434
+ 不依赖宿主 Tailwind 生成配色工具类 */
435
+ .zcodepro-toast {
436
+ display: flex; align-items: center; gap: .5rem;
437
+ max-width: 28rem; padding: .75rem 1rem;
438
+ border-radius: .5rem; border: 1px solid var(--color-border, #555);
439
+ background: var(--color-menu, #1f1f1f); color: var(--color-foreground, #eee);
440
+ font-size: .875rem; box-shadow: 0 10px 15px -3px rgb(0 0 0 / .3);
441
+ }
442
+ .zcodepro-toast .zcodepro-toast-glyph { font-weight: 600; }
443
+ .zcodepro-toast-success { border-color: #22c55e; }
444
+ .zcodepro-toast-success .zcodepro-toast-glyph { color: #22c55e; }
445
+ .zcodepro-toast-warning { border-color: #f59e0b; }
446
+ .zcodepro-toast-warning .zcodepro-toast-glyph { color: #f59e0b; }
447
+ .zcodepro-toast-error { border-color: var(--color-destructive, #ef4444); }
448
+ .zcodepro-toast-error .zcodepro-toast-glyph { color: var(--color-destructive, #ef4444); }
449
+ .zcodepro-toast-info .zcodepro-toast-glyph { color: var(--color-foreground-subtle, #999); }
382
450
  /* 弹窗遮罩/面板兜底:不依赖应用 Tailwind 类是否仍存在;主题色走 --color-* 变量,缺失时回退字面量 */
383
451
  .zcodepro-overlay { background-color: rgba(0, 0, 0, 0.6); }
384
452
  .zcodepro-overlay[data-overlay="none"] { background-color: transparent; }
@@ -482,7 +550,7 @@ export function ensureStyle() {
482
550
  }
483
551
  .zcodepro-bg-card:hover { filter: brightness(1.15); }
484
552
  .zcodepro-bg-card[data-state="active"] { border-color: var(--color-primary, #333); }
485
- .zcodepro-bg-card img { width: 100%; height: 100%; object-fit: cover; display: block; }
553
+ .zcodepro-bg-card img, .zcodepro-bg-card video { width: 100%; height: 100%; object-fit: cover; display: block; }
486
554
  .zcodepro-bg-del {
487
555
  position: absolute; top: 4px; right: 4px; width: 20px; height: 20px;
488
556
  border-radius: 6px; border: none; cursor: pointer; font-size: 11px; line-height: 1;