chore: 保留ai-chat-popup 及 ai-chat-flaot组件

This commit is contained in:
2025-11-04 16:16:03 +08:00
parent 1c2fee28ec
commit 29280f6f57
12 changed files with 1735 additions and 661 deletions

112
common/js/event-safety.js Normal file
View File

@@ -0,0 +1,112 @@
// 事件安全处理工具
export class EventSafety {
// 创建安全的事件对象
static createSafeEvent(originalEvent = {}) {
const safeEvent = {
type: originalEvent.type || 'unknown',
timeStamp: originalEvent.timeStamp || Date.now(),
detail: originalEvent.detail || {},
// 安全的目标对象
get target() {
return EventSafety.createSafeTarget(originalEvent.target)
},
get currentTarget() {
return EventSafety.createSafeTarget(originalEvent.currentTarget)
},
// 安全的 matches 方法
matches(selector) {
return EventSafety.safeMatches(originalEvent.target, selector)
}
}
return new Proxy(safeEvent, {
get(obj, prop) {
// 防止访问不存在的属性
if (prop in obj) {
return obj[prop]
}
return undefined
}
})
}
// 创建安全的目标对象
static createSafeTarget(target) {
if (!target || typeof target !== 'object') {
return EventSafety.getFallbackTarget()
}
const safeTarget = {
// 基础属性
tagName: target.tagName || '',
id: target.id || '',
className: target.className || '',
// 安全的方法
matches: (selector) => EventSafety.safeMatches(target, selector),
// 数据集
dataset: target.dataset || {}
}
return safeTarget
}
// 安全的 matches 检查
static safeMatches(element, selector) {
if (!element || typeof element.matches !== 'function') {
return false
}
try {
return element.matches(selector)
} catch (error) {
console.warn('matches 检查失败:', error)
return false
}
}
// 回退目标对象
static getFallbackTarget() {
return {
tagName: '',
id: '',
className: '',
matches: () => false,
dataset: {}
}
}
// 包装事件处理器
static wrapEventHandler(handler, options = {}) {
return function(event) {
try {
// 创建安全的事件对象
const safeEvent = EventSafety.createSafeEvent(event)
return handler.call(this, safeEvent)
} catch (error) {
console.error('事件处理错误:', error)
// 可选的错误处理
if (options.onError) {
options.onError(error, event, this)
}
}
}
}
// 验证事件类型
static isValidEventType(event, expectedType) {
return event && event.type === expectedType
}
// 提取安全的事件数据
static extractEventData(event, fields = ['type', 'timeStamp', 'detail']) {
const result = {}
fields.forEach(field => {
if (event && field in event) {
result[field] = event[field]
}
})
return result
}
}