@quicktvui/web-cli 1.0.1 → 1.0.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.
@@ -15,6 +15,7 @@ import {
15
15
  APP_NAME,
16
16
  IJKPlayerComponent,
17
17
  } from '@quicktvui/web-renderer'
18
+ import { setupGlobalErrorHandler } from './error-badge.js'
18
19
 
19
20
  // 初始化 async localStorage
20
21
  initAsyncLocalStorage()
@@ -39,6 +40,14 @@ applyAllPatches(engine)
39
40
  global.__TV_FOCUS_MANAGER__ = new TVFocusManager()
40
41
  console.log('[Web Renderer] TVFocusManager initialized')
41
42
 
43
+ // 初始化错误提示组件(在 DOM 准备好后)
44
+ if (document.readyState === 'loading') {
45
+ document.addEventListener('DOMContentLoaded', () => setupGlobalErrorHandler())
46
+ } else {
47
+ setupGlobalErrorHandler()
48
+ }
49
+ console.log('[Web Renderer] Error badge initialized')
50
+
42
51
  // 注入全局 CSS
43
52
  const styleEl = document.createElement('style')
44
53
  styleEl.id = 'web-platform-reset'
@@ -0,0 +1,410 @@
1
+ /**
2
+ * 自定义错误提示组件
3
+ * 在右上角显示红色角标,点击可查看错误详情
4
+ */
5
+
6
+ class ErrorBadge {
7
+ constructor() {
8
+ this.errors = []
9
+ this.isExpanded = false
10
+ this.badge = null
11
+ this.panel = null
12
+ this.container = null
13
+ this.init()
14
+ }
15
+
16
+ init() {
17
+ // 创建容器
18
+ this.container = document.createElement('div')
19
+ this.container.id = 'qt-error-badge-container'
20
+ this.container.innerHTML = `
21
+ <style>
22
+ #qt-error-badge-container {
23
+ position: fixed;
24
+ top: 0;
25
+ right: 0;
26
+ z-index: 999999;
27
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
28
+ }
29
+ #qt-error-badge {
30
+ display: none;
31
+ position: fixed;
32
+ top: 16px;
33
+ right: 16px;
34
+ width: 32px;
35
+ height: 32px;
36
+ background: linear-gradient(135deg, #ff4444, #cc0000);
37
+ border-radius: 50%;
38
+ cursor: pointer;
39
+ box-shadow: 0 4px 12px rgba(204, 0, 0, 0.4);
40
+ transition: transform 0.2s ease, box-shadow 0.2s ease;
41
+ z-index: 999999;
42
+ }
43
+ #qt-error-badge:hover {
44
+ transform: scale(1.1);
45
+ box-shadow: 0 6px 16px rgba(204, 0, 0, 0.5);
46
+ }
47
+ #qt-error-badge.visible {
48
+ display: flex;
49
+ align-items: center;
50
+ justify-content: center;
51
+ }
52
+ #qt-error-count {
53
+ color: white;
54
+ font-size: 14px;
55
+ font-weight: bold;
56
+ min-width: 20px;
57
+ text-align: center;
58
+ }
59
+ #qt-error-badge-icon {
60
+ display: none;
61
+ color: white;
62
+ font-size: 18px;
63
+ font-weight: bold;
64
+ }
65
+ #qt-error-panel {
66
+ display: none;
67
+ position: fixed;
68
+ top: 56px;
69
+ right: 16px;
70
+ width: 420px;
71
+ max-height: 400px;
72
+ background: #1e1e1e;
73
+ border-radius: 8px;
74
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
75
+ overflow: hidden;
76
+ z-index: 999998;
77
+ }
78
+ #qt-error-panel.visible {
79
+ display: block;
80
+ }
81
+ #qt-error-panel-header {
82
+ display: flex;
83
+ align-items: center;
84
+ justify-content: space-between;
85
+ padding: 12px 16px;
86
+ background: #2d2d2d;
87
+ border-bottom: 1px solid #404040;
88
+ }
89
+ #qt-error-panel-title {
90
+ color: #ff6b6b;
91
+ font-size: 14px;
92
+ font-weight: 600;
93
+ }
94
+ #qt-error-panel-close {
95
+ background: none;
96
+ border: none;
97
+ color: #888;
98
+ font-size: 20px;
99
+ cursor: pointer;
100
+ padding: 0;
101
+ line-height: 1;
102
+ }
103
+ #qt-error-panel-close:hover {
104
+ color: #fff;
105
+ }
106
+ #qt-error-panel-clear {
107
+ background: none;
108
+ border: 1px solid #666;
109
+ color: #aaa;
110
+ font-size: 12px;
111
+ cursor: pointer;
112
+ padding: 4px 8px;
113
+ border-radius: 4px;
114
+ margin-right: 8px;
115
+ }
116
+ #qt-error-panel-clear:hover {
117
+ border-color: #ff6b6b;
118
+ color: #ff6b6b;
119
+ }
120
+ #qt-error-list {
121
+ max-height: 340px;
122
+ overflow-y: auto;
123
+ padding: 0;
124
+ margin: 0;
125
+ list-style: none;
126
+ }
127
+ #qt-error-list::-webkit-scrollbar {
128
+ width: 6px;
129
+ }
130
+ #qt-error-list::-webkit-scrollbar-track {
131
+ background: #1e1e1e;
132
+ }
133
+ #qt-error-list::-webkit-scrollbar-thumb {
134
+ background: #444;
135
+ border-radius: 3px;
136
+ }
137
+ #qt-error-list::-webkit-scrollbar-thumb:hover {
138
+ background: #555;
139
+ }
140
+ .qt-error-item {
141
+ padding: 12px 16px;
142
+ border-bottom: 1px solid #2d2d2d;
143
+ cursor: pointer;
144
+ transition: background 0.2s ease;
145
+ }
146
+ .qt-error-item:hover {
147
+ background: #2a2a2a;
148
+ }
149
+ .qt-error-item:last-child {
150
+ border-bottom: none;
151
+ }
152
+ .qt-error-time {
153
+ color: #888;
154
+ font-size: 11px;
155
+ margin-bottom: 4px;
156
+ }
157
+ .qt-error-message {
158
+ color: #ff6b6b;
159
+ font-size: 13px;
160
+ line-height: 1.4;
161
+ word-break: break-all;
162
+ }
163
+ .qt-error-stack {
164
+ display: none;
165
+ color: #aaa;
166
+ font-size: 11px;
167
+ margin-top: 8px;
168
+ padding: 8px;
169
+ background: #252525;
170
+ border-radius: 4px;
171
+ white-space: pre-wrap;
172
+ word-break: break-all;
173
+ max-height: 120px;
174
+ overflow-y: auto;
175
+ }
176
+ .qt-error-item.expanded .qt-error-stack {
177
+ display: block;
178
+ }
179
+ .qt-error-empty {
180
+ padding: 24px;
181
+ text-align: center;
182
+ color: #666;
183
+ font-size: 13px;
184
+ }
185
+ </style>
186
+ <div id="qt-error-badge">
187
+ <span id="qt-error-badge-icon">!</span>
188
+ <span id="qt-error-count">0</span>
189
+ </div>
190
+ <div id="qt-error-panel">
191
+ <div id="qt-error-panel-header">
192
+ <span id="qt-error-panel-title">运行时错误</span>
193
+ <div>
194
+ <button id="qt-error-panel-clear">清除全部</button>
195
+ <button id="qt-error-panel-close">&times;</button>
196
+ </div>
197
+ </div>
198
+ <ul id="qt-error-list"></ul>
199
+ </div>
200
+ `
201
+
202
+ document.head.appendChild(this.container.querySelector('style'))
203
+ document.body.appendChild(this.container)
204
+
205
+ // 获取元素引用
206
+ this.badge = document.getElementById('qt-error-badge')
207
+ this.panel = document.getElementById('qt-error-panel')
208
+ this.countEl = document.getElementById('qt-error-count')
209
+ this.listEl = document.getElementById('qt-error-list')
210
+ this.iconEl = document.getElementById('qt-error-badge-icon')
211
+
212
+ // 绑定事件
213
+ this.badge.addEventListener('click', () => this.toggle())
214
+ document.getElementById('qt-error-panel-close').addEventListener('click', () => this.hide())
215
+ document.getElementById('qt-error-panel-clear').addEventListener('click', () => this.clear())
216
+
217
+ // 点击外部关闭
218
+ document.addEventListener('click', (e) => {
219
+ if (this.isExpanded && !this.container.contains(e.target)) {
220
+ this.hide()
221
+ }
222
+ })
223
+ }
224
+
225
+ addError(error) {
226
+ const errorInfo = {
227
+ id: Date.now(),
228
+ time: new Date().toLocaleTimeString(),
229
+ message: this.getErrorMessage(error),
230
+ stack: this.getErrorStack(error),
231
+ }
232
+
233
+ this.errors.push(errorInfo)
234
+ this.updateBadge()
235
+ this.renderErrors()
236
+
237
+ // 首次错误时闪烁提示
238
+ if (this.errors.length === 1) {
239
+ this.flash()
240
+ }
241
+ }
242
+
243
+ getErrorMessage(error) {
244
+ if (typeof error === 'string') return error
245
+ if (error && typeof error === 'object') {
246
+ if (error.message) return error.message
247
+ if (error.error && error.error.message) return error.error.message
248
+ try {
249
+ return JSON.stringify(error)
250
+ } catch (e) {
251
+ return '[object Object]'
252
+ }
253
+ }
254
+ return String(error)
255
+ }
256
+
257
+ getErrorStack(error) {
258
+ if (error && error.error && error.error.stack) return error.error.stack
259
+ if (error && error.stack) return error.stack
260
+ return ''
261
+ }
262
+
263
+ updateBadge() {
264
+ const count = this.errors.length
265
+ if (count > 0) {
266
+ this.badge.classList.add('visible')
267
+ this.countEl.textContent = count > 99 ? '99+' : count
268
+ this.countEl.style.display = 'inline'
269
+ this.iconEl.style.display = 'none'
270
+ } else {
271
+ this.badge.classList.remove('visible')
272
+ }
273
+ }
274
+
275
+ renderErrors() {
276
+ if (this.errors.length === 0) {
277
+ this.listEl.innerHTML = '<li class="qt-error-empty">暂无错误</li>'
278
+ return
279
+ }
280
+
281
+ this.listEl.innerHTML = this.errors
282
+ .map(
283
+ (err) => `
284
+ <li class="qt-error-item" data-id="${err.id}">
285
+ <div class="qt-error-time">${err.time}</div>
286
+ <div class="qt-error-message">${this.escapeHtml(err.message)}</div>
287
+ ${
288
+ err.stack
289
+ ? `<div class="qt-error-stack">${this.escapeHtml(this.formatStack(err.stack))}</div>`
290
+ : ''
291
+ }
292
+ </li>
293
+ `
294
+ )
295
+ .join('')
296
+
297
+ // 点击展开/收起详情
298
+ this.listEl.querySelectorAll('.qt-error-item').forEach((item) => {
299
+ item.addEventListener('click', () => {
300
+ item.classList.toggle('expanded')
301
+ })
302
+ })
303
+ }
304
+
305
+ formatStack(stack) {
306
+ // 简化堆栈信息,只保留关键部分
307
+ const lines = stack.split('\n')
308
+ const relevantLines = lines.filter((line) => {
309
+ return (
310
+ line.includes('.vue') ||
311
+ line.includes('.js') ||
312
+ line.includes('.ts') ||
313
+ line.includes('at ') ||
314
+ !line.trim()
315
+ )
316
+ })
317
+ return relevantLines.slice(0, 10).join('\n')
318
+ }
319
+
320
+ escapeHtml(str) {
321
+ const div = document.createElement('div')
322
+ div.textContent = str
323
+ return div.innerHTML
324
+ }
325
+
326
+ toggle() {
327
+ if (this.isExpanded) {
328
+ this.hide()
329
+ } else {
330
+ this.show()
331
+ }
332
+ }
333
+
334
+ show() {
335
+ this.isExpanded = true
336
+ this.panel.classList.add('visible')
337
+ }
338
+
339
+ hide() {
340
+ this.isExpanded = false
341
+ this.panel.classList.remove('visible')
342
+ }
343
+
344
+ clear() {
345
+ this.errors = []
346
+ this.updateBadge()
347
+ this.renderErrors()
348
+ this.hide()
349
+ }
350
+
351
+ flash() {
352
+ // 首次错误时闪烁提示
353
+ let count = 0
354
+ const flash = () => {
355
+ if (count >= 6) return
356
+ this.badge.style.transform = count % 2 === 0 ? 'scale(1.2)' : 'scale(1)'
357
+ count++
358
+ setTimeout(flash, 100)
359
+ }
360
+ flash()
361
+ }
362
+ }
363
+
364
+ // 创建全局实例
365
+ let errorBadgeInstance = null
366
+
367
+ function initErrorBadge() {
368
+ if (!errorBadgeInstance) {
369
+ errorBadgeInstance = new ErrorBadge()
370
+ }
371
+ return errorBadgeInstance
372
+ }
373
+
374
+ // 捕获全局错误
375
+ function setupGlobalErrorHandler() {
376
+ const badge = initErrorBadge()
377
+
378
+ // 捕获 JavaScript 错误
379
+ window.addEventListener('error', (event) => {
380
+ badge.addError({
381
+ message: event.message,
382
+ stack: event.error?.stack || '',
383
+ })
384
+ // 不阻止默认行为,错误仍会输出到控制台
385
+ })
386
+
387
+ // 捕获 Promise 未处理错误
388
+ window.addEventListener('unhandledrejection', (event) => {
389
+ badge.addError({
390
+ message: event.reason?.message || String(event.reason),
391
+ stack: event.reason?.stack || '',
392
+ })
393
+ })
394
+
395
+ // 捕获 Vue 错误
396
+ if (window.Vue) {
397
+ window.Vue.config.errorHandler = (err, vm, info) => {
398
+ badge.addError({
399
+ message: `[Vue] ${err.message}`,
400
+ stack: err.stack || '',
401
+ })
402
+ console.error('[Vue Error]', err, info)
403
+ }
404
+ }
405
+
406
+ return badge
407
+ }
408
+
409
+ // 导出
410
+ export { ErrorBadge, initErrorBadge, setupGlobalErrorHandler }
@@ -183,7 +183,7 @@ module.exports = {
183
183
  },
184
184
  ],
185
185
  client: {
186
- overlay: { errors: true, warnings: false },
186
+ overlay: false,
187
187
  },
188
188
  },
189
189
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quicktvui/web-cli",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "CLI tool for QuickTVUI web development - zero configuration",
5
5
  "author": "QuickTVUI Team",
6
6
  "license": "Apache-2.0",