customer-chat-sdk 1.1.12 → 1.1.13
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.
- package/dist/core/IconManager.d.ts +36 -1
- package/dist/core/IconManager.d.ts.map +1 -1
- package/dist/customer-sdk.cjs.js +529 -19
- package/dist/customer-sdk.esm.js +529 -19
- package/dist/customer-sdk.min.js +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/customer-sdk.cjs.js
CHANGED
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
// 直接使用base64字符串,避免打包后路径问题
|
|
6
6
|
const iconImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAYAAAA5ZDbSAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAArKSURBVHgB7Z1NbFTXFcf/93mwaGyEabLoIgFTqWRFMZUitSoIu80ial01SFWrKgsGKXYpWdRtFilRK4xUgbKICosmCKIySKCo2UAE6i61XVroDuOoi2YT02RZVFNwQTAzN+e8N88ej9+8j5n3ce5wf5JhPvyY4f3f/9xzz73vXoUeYKish6oljCiFYQ2MaAebVR3D/Jzf1xpDUPTTggIW6feXoOlH0Q9wS9exRK/Pl6qYX6qoJRiOgoEMvqpHWUjlYJ9iQeEJmTb07y6y2CT6HP99/z01C8MwQmB26OMSyuTIH9LTkSA35oLn9FmKCB/WHmP2YUUtQjhiBV4jqsIoJKJJbOC8ZLHFCczhl75UWXvCFuPUDiBXV5TGeWlhXIzALCx9m6Ni3RqfeRL71PJZVYEAChfYdayDc1klSkXBCRr9caxooQsTuIccG4qbiddxsKjQnbvAlDwNVzfgXK8L2wq30ZSMHcs7GctV4IFX9ZTyXGtM8pQq2i2qnCQ3H0NO5CLwk+radnDYrj3CWB5udpAx7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constructor(position, debug = false, target, options) {
|
|
9
9
|
this.iconElement = null;
|
|
10
10
|
this.badgeElement = null;
|
|
11
11
|
this.onClickCallback = null;
|
|
@@ -46,10 +46,32 @@ class IconManager {
|
|
|
46
46
|
this.pointerLeaveHandler = null;
|
|
47
47
|
this.blurHandler = null;
|
|
48
48
|
this.dragTimeoutId = null; // 拖动超时定时器
|
|
49
|
+
this.mouseLeaveTimeout = null; // 鼠标离开防抖定时器
|
|
50
|
+
this.pointerLeaveTimeout = null; // 指针离开防抖定时器
|
|
51
|
+
this.isStoppingDrag = false; // 防止 stopDrag 重复调用
|
|
52
|
+
// 侧边吸附相关状态
|
|
53
|
+
this.sideAttach = true; // 是否启用侧边吸附(默认 true)
|
|
54
|
+
this.sideHideRatio = 0.5; // 侧边吸附时的隐藏比例(默认 0.5,显示一半)
|
|
55
|
+
this.magnetic = true; // 是否启用磁性吸附(默认 true)
|
|
56
|
+
this.magneticDirection = 'x'; // 磁性吸附方向(默认 'x')
|
|
57
|
+
this.margin = 10; // 边距(默认 10px)
|
|
58
|
+
this.isAttachedToSide = false; // 是否吸附到侧边
|
|
59
|
+
this.attachedSide = ''; // 吸附到哪一边
|
|
60
|
+
this.autoAttachDelay = 3000; // 自动吸附延迟时间(默认 3000ms,3秒)
|
|
61
|
+
this.autoAttachTimer = null; // 自动吸附定时器
|
|
49
62
|
this.iconPosition = position || null;
|
|
50
63
|
this.debug = debug;
|
|
51
64
|
// 保存 target(可以是 HTMLElement 或字符串选择器)
|
|
52
65
|
this.target = target || null;
|
|
66
|
+
// 侧边吸附配置
|
|
67
|
+
if (options) {
|
|
68
|
+
this.sideAttach = options.sideAttach !== undefined ? options.sideAttach : true;
|
|
69
|
+
this.sideHideRatio = options.sideHideRatio !== undefined ? options.sideHideRatio : 0.5;
|
|
70
|
+
this.magnetic = options.magnetic !== undefined ? options.magnetic : true;
|
|
71
|
+
this.magneticDirection = options.magneticDirection || 'x';
|
|
72
|
+
this.margin = options.margin !== undefined ? options.margin : 10;
|
|
73
|
+
this.autoAttachDelay = options.autoAttachDelay !== undefined ? options.autoAttachDelay : 3000;
|
|
74
|
+
}
|
|
53
75
|
}
|
|
54
76
|
/**
|
|
55
77
|
* 显示悬浮图标
|
|
@@ -103,8 +125,42 @@ class IconManager {
|
|
|
103
125
|
}
|
|
104
126
|
else {
|
|
105
127
|
// 默认位置:右下角
|
|
106
|
-
|
|
107
|
-
|
|
128
|
+
// 如果启用侧边吸附且没有指定位置,初始化时直接应用右边吸附样式
|
|
129
|
+
if (this.sideAttach && this.magnetic) {
|
|
130
|
+
const iconWidth = 30; // 图标宽度
|
|
131
|
+
const hideDistance = iconWidth * this.sideHideRatio; // 隐藏部分(在容器外)
|
|
132
|
+
// 右边吸附:图标右边缘对齐容器右边缘,隐藏部分在容器外
|
|
133
|
+
// right = -hideDistance,让图标向右移动 hideDistance,这样隐藏部分在容器外
|
|
134
|
+
defaultStyle.right = `-${hideDistance}px`; // 负值,让图标超出容器右边缘
|
|
135
|
+
defaultStyle.left = 'auto';
|
|
136
|
+
defaultStyle.bottom = '80px';
|
|
137
|
+
defaultStyle.transform = 'translateX(0)';
|
|
138
|
+
// 标记为已吸附到右边
|
|
139
|
+
this.isAttachedToSide = true;
|
|
140
|
+
this.attachedSide = 'right';
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
defaultStyle.bottom = '80px';
|
|
144
|
+
defaultStyle.right = '20px';
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
// 如果侧边吸附已启用且已吸附,应用侧边吸附样式
|
|
148
|
+
if (this.sideAttach && this.isAttachedToSide && this.attachedSide) {
|
|
149
|
+
const iconWidth = 30; // 图标宽度
|
|
150
|
+
const hideDistance = iconWidth * this.sideHideRatio; // 隐藏部分(在容器外)
|
|
151
|
+
if (this.attachedSide === 'left') {
|
|
152
|
+
// 左边吸附:图标左边缘对齐容器左边缘,隐藏部分在容器外
|
|
153
|
+
defaultStyle.left = `-${hideDistance}px`; // 负值,让图标超出容器左边缘
|
|
154
|
+
defaultStyle.right = 'auto';
|
|
155
|
+
defaultStyle.transform = 'translateX(0)';
|
|
156
|
+
}
|
|
157
|
+
else if (this.attachedSide === 'right') {
|
|
158
|
+
// 右边吸附:图标右边缘对齐容器右边缘,隐藏部分在容器外
|
|
159
|
+
// right = -hideDistance,让图标向右移动 hideDistance,这样隐藏部分在容器外
|
|
160
|
+
defaultStyle.right = `-${hideDistance}px`; // 负值,让图标超出容器右边缘
|
|
161
|
+
defaultStyle.left = 'auto';
|
|
162
|
+
defaultStyle.transform = 'translateX(0)';
|
|
163
|
+
}
|
|
108
164
|
}
|
|
109
165
|
Object.assign(this.iconElement.style, defaultStyle);
|
|
110
166
|
// 添加图标图片(直接使用base64字符串,避免打包后路径问题)
|
|
@@ -160,6 +216,72 @@ class IconManager {
|
|
|
160
216
|
}
|
|
161
217
|
// 设置拖动事件
|
|
162
218
|
this.setupDragEvents();
|
|
219
|
+
// 如果启用磁性吸附且没有指定位置,初始化后自动吸附到右边(默认显示一半)
|
|
220
|
+
if (this.magnetic && !this.iconPosition) {
|
|
221
|
+
// 等待图标渲染完成后再执行吸附
|
|
222
|
+
setTimeout(() => {
|
|
223
|
+
if (this.iconElement) {
|
|
224
|
+
// 先转换位置(从 right/bottom 到 left/top)
|
|
225
|
+
const container = this.getTargetElement();
|
|
226
|
+
if (container) {
|
|
227
|
+
const containerRect = container.getBoundingClientRect();
|
|
228
|
+
const iconWidth = this.iconElement.offsetWidth || 30;
|
|
229
|
+
const iconHeight = this.iconElement.offsetHeight || 30;
|
|
230
|
+
// 计算默认右下角位置对应的 left/top
|
|
231
|
+
const defaultRight = 20; // 默认 right: 20px
|
|
232
|
+
const defaultBottom = 80; // 默认 bottom: 80px
|
|
233
|
+
// 转换为 left/top(相对于容器)
|
|
234
|
+
const initialLeft = containerRect.width - iconWidth - defaultRight;
|
|
235
|
+
const initialTop = containerRect.height - iconHeight - defaultBottom;
|
|
236
|
+
// 临时设置位置,用于磁性吸附计算
|
|
237
|
+
this.iconElement.style.left = `${initialLeft}px`;
|
|
238
|
+
this.iconElement.style.top = `${initialTop}px`;
|
|
239
|
+
this.iconElement.style.right = 'auto';
|
|
240
|
+
this.iconElement.style.bottom = 'auto';
|
|
241
|
+
// 如果启用侧边吸附,强制吸附到右边并显示一半
|
|
242
|
+
if (this.sideAttach) {
|
|
243
|
+
this.isAttachedToSide = true;
|
|
244
|
+
this.attachedSide = 'right';
|
|
245
|
+
const hideDistance = iconWidth * this.sideHideRatio; // 隐藏部分(在容器外)
|
|
246
|
+
// right = -hideDistance,让图标向右移动 hideDistance,这样隐藏部分在容器外
|
|
247
|
+
this.iconElement.style.right = `-${hideDistance}px`; // 负值,让图标超出容器右边缘
|
|
248
|
+
this.iconElement.style.left = 'auto';
|
|
249
|
+
this.iconElement.style.bottom = `${defaultBottom}px`; // 保持底部位置
|
|
250
|
+
this.iconElement.style.top = 'auto';
|
|
251
|
+
this.iconElement.style.transform = 'translateX(0)';
|
|
252
|
+
this.iconElement.style.transition = 'all 0.3s ease';
|
|
253
|
+
if (this.debug) {
|
|
254
|
+
console.log('[IconManager] Auto side attach on init', {
|
|
255
|
+
isAttachedToSide: this.isAttachedToSide,
|
|
256
|
+
attachedSide: this.attachedSide,
|
|
257
|
+
hideDistance,
|
|
258
|
+
right: `-${hideDistance}px`
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
// 执行磁性吸附(不侧边吸附,只磁性吸附)
|
|
264
|
+
this.magneticSnap();
|
|
265
|
+
if (this.debug) {
|
|
266
|
+
console.log('[IconManager] Auto magnetic snap on init', {
|
|
267
|
+
isAttachedToSide: this.isAttachedToSide,
|
|
268
|
+
attachedSide: this.attachedSide
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}, 200); // 延迟 200ms,确保容器和图标都已渲染完成
|
|
275
|
+
}
|
|
276
|
+
// 启动自动吸附定时器(如果启用侧边吸附且当前未吸附)
|
|
277
|
+
// 注意:初始化时如果已经吸附到侧边,不会启动定时器
|
|
278
|
+
// 但如果图标从隐藏状态恢复显示,且未吸附,应该启动定时器
|
|
279
|
+
if (this.sideAttach && this.autoAttachDelay > 0 && !this.isAttachedToSide) {
|
|
280
|
+
// 延迟启动,确保图标已经完全渲染
|
|
281
|
+
setTimeout(() => {
|
|
282
|
+
this.startAutoAttachTimer();
|
|
283
|
+
}, 300); // 延迟 300ms,确保初始化吸附逻辑已完成
|
|
284
|
+
}
|
|
163
285
|
if (this.debug) {
|
|
164
286
|
console.log('CustomerSDK icon displayed');
|
|
165
287
|
}
|
|
@@ -176,6 +298,8 @@ class IconManager {
|
|
|
176
298
|
hide() {
|
|
177
299
|
// 清理拖动事件
|
|
178
300
|
this.cleanupDragEvents();
|
|
301
|
+
// 清除自动吸附定时器
|
|
302
|
+
this.clearAutoAttachTimer();
|
|
179
303
|
if (this.iconElement) {
|
|
180
304
|
// 在隐藏前保存当前位置(如果图标已经被拖动过)
|
|
181
305
|
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
@@ -237,6 +361,110 @@ class IconManager {
|
|
|
237
361
|
setStyle(style) {
|
|
238
362
|
// 现在样式写死,不做处理
|
|
239
363
|
}
|
|
364
|
+
/**
|
|
365
|
+
* 启动自动吸附定时器
|
|
366
|
+
*/
|
|
367
|
+
startAutoAttachTimer() {
|
|
368
|
+
// 清除之前的定时器
|
|
369
|
+
this.clearAutoAttachTimer();
|
|
370
|
+
// 如果已经吸附到侧边,不需要启动定时器
|
|
371
|
+
if (this.isAttachedToSide) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
this.autoAttachTimer = window.setTimeout(() => {
|
|
375
|
+
if (this.iconElement && !this.isDragging && !this.dragStarted) {
|
|
376
|
+
// 执行自动吸附到侧边
|
|
377
|
+
this.autoAttachToSide();
|
|
378
|
+
}
|
|
379
|
+
this.autoAttachTimer = null;
|
|
380
|
+
}, this.autoAttachDelay);
|
|
381
|
+
if (this.debug) {
|
|
382
|
+
console.log(`[IconManager] Auto attach timer started, will attach in ${this.autoAttachDelay}ms`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* 清除自动吸附定时器
|
|
387
|
+
*/
|
|
388
|
+
clearAutoAttachTimer() {
|
|
389
|
+
if (this.autoAttachTimer !== null) {
|
|
390
|
+
window.clearTimeout(this.autoAttachTimer);
|
|
391
|
+
this.autoAttachTimer = null;
|
|
392
|
+
if (this.debug) {
|
|
393
|
+
console.log('[IconManager] Auto attach timer cleared');
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* 自动吸附到侧边
|
|
399
|
+
*/
|
|
400
|
+
autoAttachToSide() {
|
|
401
|
+
if (!this.iconElement || !this.sideAttach)
|
|
402
|
+
return;
|
|
403
|
+
try {
|
|
404
|
+
const container = this.getTargetElement();
|
|
405
|
+
if (!container)
|
|
406
|
+
return;
|
|
407
|
+
const containerRect = container.getBoundingClientRect();
|
|
408
|
+
const iconWidth = this.iconElement.offsetWidth || 30;
|
|
409
|
+
// 获取当前图标位置
|
|
410
|
+
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
411
|
+
const currentLeft = parseFloat(computedStyle.left) || 0;
|
|
412
|
+
const currentRight = computedStyle.right;
|
|
413
|
+
// 判断应该吸附到哪一边(根据当前位置)
|
|
414
|
+
const containerWidth = containerRect.width;
|
|
415
|
+
let shouldAttachToRight = true;
|
|
416
|
+
// 如果使用 right 定位,计算实际 left 位置
|
|
417
|
+
if (currentRight !== 'auto' && currentRight !== '') {
|
|
418
|
+
const rightValue = parseFloat(currentRight);
|
|
419
|
+
if (!isNaN(rightValue)) {
|
|
420
|
+
const actualLeft = containerWidth - iconWidth - rightValue;
|
|
421
|
+
shouldAttachToRight = actualLeft >= containerWidth / 2;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
shouldAttachToRight = currentLeft >= containerWidth / 2;
|
|
426
|
+
}
|
|
427
|
+
if (shouldAttachToRight) {
|
|
428
|
+
// 吸附到右边
|
|
429
|
+
this.isAttachedToSide = true;
|
|
430
|
+
this.attachedSide = 'right';
|
|
431
|
+
const hideDistance = iconWidth * this.sideHideRatio;
|
|
432
|
+
this.iconElement.style.right = `-${hideDistance}px`;
|
|
433
|
+
this.iconElement.style.left = 'auto';
|
|
434
|
+
}
|
|
435
|
+
else {
|
|
436
|
+
// 吸附到左边
|
|
437
|
+
this.isAttachedToSide = true;
|
|
438
|
+
this.attachedSide = 'left';
|
|
439
|
+
const hideDistance = iconWidth * this.sideHideRatio;
|
|
440
|
+
this.iconElement.style.left = `-${hideDistance}px`;
|
|
441
|
+
this.iconElement.style.right = 'auto';
|
|
442
|
+
}
|
|
443
|
+
// 保持 Y 轴位置不变
|
|
444
|
+
const computedStyleAfter = window.getComputedStyle(this.iconElement);
|
|
445
|
+
if (computedStyleAfter.top !== 'auto' && computedStyleAfter.top !== '') {
|
|
446
|
+
this.iconElement.style.top = computedStyleAfter.top;
|
|
447
|
+
this.iconElement.style.bottom = 'auto';
|
|
448
|
+
}
|
|
449
|
+
else if (computedStyleAfter.bottom !== 'auto' && computedStyleAfter.bottom !== '') {
|
|
450
|
+
this.iconElement.style.bottom = computedStyleAfter.bottom;
|
|
451
|
+
this.iconElement.style.top = 'auto';
|
|
452
|
+
}
|
|
453
|
+
this.iconElement.style.transform = 'translateX(0)';
|
|
454
|
+
this.iconElement.style.transition = 'all 0.3s ease';
|
|
455
|
+
if (this.debug) {
|
|
456
|
+
console.log('[IconManager] Auto attached to side', {
|
|
457
|
+
attachedSide: this.attachedSide,
|
|
458
|
+
hideDistance: iconWidth * this.sideHideRatio
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
catch (error) {
|
|
463
|
+
if (this.debug) {
|
|
464
|
+
console.error('[IconManager] Error in autoAttachToSide:', error);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
240
468
|
/**
|
|
241
469
|
* 设置点击回调
|
|
242
470
|
*/
|
|
@@ -304,6 +532,37 @@ class IconManager {
|
|
|
304
532
|
startDrag(e) {
|
|
305
533
|
if (!this.iconElement || !this.isClickEnabled)
|
|
306
534
|
return;
|
|
535
|
+
// 清除自动吸附定时器(用户开始拖动)
|
|
536
|
+
this.clearAutoAttachTimer();
|
|
537
|
+
// 如果正在停止拖动,等待完成后再允许新的拖动
|
|
538
|
+
if (this.isStoppingDrag) {
|
|
539
|
+
if (this.debug) {
|
|
540
|
+
console.warn('[IconManager] startDrag called while stopping, ignoring');
|
|
541
|
+
}
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
// 如果已经在拖动中,忽略新的拖动开始事件(防止重复调用)
|
|
545
|
+
// 注意:只检查实际的拖动状态,不检查可能残留的资源(这些应该在 cleanup 中清理)
|
|
546
|
+
if (this.isDragging || this.dragStarted) {
|
|
547
|
+
if (this.debug) {
|
|
548
|
+
console.warn('[IconManager] startDrag called but already dragging, ignoring', {
|
|
549
|
+
isDragging: this.isDragging,
|
|
550
|
+
dragStarted: this.dragStarted
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
// 如果发现残留的资源,先清理它们(防御性编程)
|
|
556
|
+
if (this.dragTimeoutId !== null || this.activeDetectionRafId !== null || this.rafId !== null) {
|
|
557
|
+
if (this.debug) {
|
|
558
|
+
console.warn('[IconManager] startDrag found leftover resources, cleaning up', {
|
|
559
|
+
hasTimeout: this.dragTimeoutId !== null,
|
|
560
|
+
hasActiveDetection: this.activeDetectionRafId !== null,
|
|
561
|
+
hasRaf: this.rafId !== null
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
this.cleanupDragEvents();
|
|
565
|
+
}
|
|
307
566
|
// 检查事件目标:如果是 iframe 相关元素,不处理
|
|
308
567
|
const target = e.target;
|
|
309
568
|
if (target && (target.tagName === 'IFRAME' ||
|
|
@@ -368,32 +627,59 @@ class IconManager {
|
|
|
368
627
|
}
|
|
369
628
|
// 添加处理 iframe 上事件丢失的机制
|
|
370
629
|
// 1. 监听 mouseleave 和 pointerleave 事件(鼠标离开窗口时停止拖动)
|
|
630
|
+
// 注意:这些事件可能会在拖动过程中多次触发,需要添加防抖机制
|
|
371
631
|
this.mouseLeaveHandler = (e) => {
|
|
372
632
|
// 只有当鼠标真正离开窗口时才停止拖动
|
|
373
633
|
if (!e.relatedTarget && (e.clientY <= 0 || e.clientX <= 0 ||
|
|
374
634
|
e.clientX >= window.innerWidth || e.clientY >= window.innerHeight)) {
|
|
375
|
-
|
|
376
|
-
|
|
635
|
+
// 添加防抖,避免重复触发
|
|
636
|
+
if (this.mouseLeaveTimeout) {
|
|
637
|
+
clearTimeout(this.mouseLeaveTimeout);
|
|
377
638
|
}
|
|
378
|
-
this.
|
|
639
|
+
this.mouseLeaveTimeout = window.setTimeout(() => {
|
|
640
|
+
if ((this.isDragging || this.dragStarted) && !this.isStoppingDrag) {
|
|
641
|
+
if (this.debug) {
|
|
642
|
+
console.log('[IconManager] Mouse left window, stopping drag');
|
|
643
|
+
}
|
|
644
|
+
this.stopDrag();
|
|
645
|
+
}
|
|
646
|
+
this.mouseLeaveTimeout = null;
|
|
647
|
+
}, 50); // 50ms 防抖
|
|
379
648
|
}
|
|
380
649
|
};
|
|
381
650
|
this.pointerLeaveHandler = (e) => {
|
|
382
651
|
// 只有当指针真正离开窗口时才停止拖动
|
|
383
652
|
if (!e.relatedTarget && (e.clientY <= 0 || e.clientX <= 0 ||
|
|
384
653
|
e.clientX >= window.innerWidth || e.clientY >= window.innerHeight)) {
|
|
385
|
-
|
|
386
|
-
|
|
654
|
+
// 添加防抖,避免重复触发
|
|
655
|
+
if (this.pointerLeaveTimeout) {
|
|
656
|
+
clearTimeout(this.pointerLeaveTimeout);
|
|
387
657
|
}
|
|
388
|
-
this.
|
|
658
|
+
this.pointerLeaveTimeout = window.setTimeout(() => {
|
|
659
|
+
if ((this.isDragging || this.dragStarted) && !this.isStoppingDrag) {
|
|
660
|
+
if (this.debug) {
|
|
661
|
+
console.log('[IconManager] Pointer left window, stopping drag');
|
|
662
|
+
}
|
|
663
|
+
this.stopDrag();
|
|
664
|
+
}
|
|
665
|
+
this.pointerLeaveTimeout = null;
|
|
666
|
+
}, 50); // 50ms 防抖
|
|
389
667
|
}
|
|
390
668
|
};
|
|
391
669
|
document.addEventListener('mouseleave', this.mouseLeaveHandler);
|
|
392
670
|
document.addEventListener('pointerleave', this.pointerLeaveHandler);
|
|
393
671
|
// 2. 监听 blur 事件(窗口失去焦点时停止拖动)
|
|
394
672
|
this.blurHandler = () => {
|
|
673
|
+
// 如果已经在停止拖动,直接返回
|
|
674
|
+
if (this.isStoppingDrag) {
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
// 检查是否真的在拖动中
|
|
678
|
+
if (!this.isDragging && !this.dragStarted) {
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
395
681
|
if (this.debug) {
|
|
396
|
-
console.log('Window lost focus, stopping drag');
|
|
682
|
+
console.log('[IconManager] Window lost focus, stopping drag');
|
|
397
683
|
}
|
|
398
684
|
this.stopDrag();
|
|
399
685
|
};
|
|
@@ -533,6 +819,14 @@ class IconManager {
|
|
|
533
819
|
// 开始真正的拖拽
|
|
534
820
|
this.dragStarted = true;
|
|
535
821
|
this.isDragging = true;
|
|
822
|
+
// 拖拽开始时重置侧边吸附状态
|
|
823
|
+
if (this.isAttachedToSide) {
|
|
824
|
+
this.isAttachedToSide = false;
|
|
825
|
+
this.attachedSide = '';
|
|
826
|
+
if (this.iconElement) {
|
|
827
|
+
this.iconElement.style.transform = '';
|
|
828
|
+
}
|
|
829
|
+
}
|
|
536
830
|
// 如果是第一次拖动,需要转换位置(从 right/bottom 到 left/top)
|
|
537
831
|
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
538
832
|
if (computedStyle.right !== 'auto' || computedStyle.bottom !== 'auto') {
|
|
@@ -633,6 +927,21 @@ class IconManager {
|
|
|
633
927
|
* 停止拖动
|
|
634
928
|
*/
|
|
635
929
|
stopDrag(_e) {
|
|
930
|
+
// 防止重复调用
|
|
931
|
+
if (this.isStoppingDrag) {
|
|
932
|
+
if (this.debug) {
|
|
933
|
+
console.warn('[IconManager] stopDrag already in progress, ignoring duplicate call');
|
|
934
|
+
}
|
|
935
|
+
return;
|
|
936
|
+
}
|
|
937
|
+
// 如果已经停止且没有相关资源,直接返回
|
|
938
|
+
if (!this.isDragging && !this.dragStarted && !this.dragTimeoutId && !this.activeDetectionRafId) {
|
|
939
|
+
if (this.debug) {
|
|
940
|
+
console.warn('[IconManager] stopDrag called but not dragging, ignoring');
|
|
941
|
+
}
|
|
942
|
+
return;
|
|
943
|
+
}
|
|
944
|
+
this.isStoppingDrag = true;
|
|
636
945
|
const stopReason = this.isDragging ? 'drag_stopped' : 'not_dragging';
|
|
637
946
|
const finalPosition = this.iconElement ? {
|
|
638
947
|
left: this.iconElement.style.left,
|
|
@@ -641,9 +950,16 @@ class IconManager {
|
|
|
641
950
|
? { left: window.getComputedStyle(this.iconElement).left, top: window.getComputedStyle(this.iconElement).top }
|
|
642
951
|
: null
|
|
643
952
|
} : null;
|
|
953
|
+
// 立即清理事件监听器,防止后续事件触发
|
|
644
954
|
this.cleanupDragEvents();
|
|
645
|
-
if (!this.iconElement)
|
|
955
|
+
if (!this.iconElement) {
|
|
956
|
+
// 即使没有 iconElement,也要重置状态,确保可以再次拖动
|
|
957
|
+
this.hasMoved = false;
|
|
958
|
+
this.isDragging = false;
|
|
959
|
+
this.dragStarted = false;
|
|
960
|
+
this.isStoppingDrag = false;
|
|
646
961
|
return;
|
|
962
|
+
}
|
|
647
963
|
// 恢复样式
|
|
648
964
|
this.iconElement.style.transition = 'transform 0.2s ease';
|
|
649
965
|
this.iconElement.style.cursor = 'pointer';
|
|
@@ -663,6 +979,10 @@ class IconManager {
|
|
|
663
979
|
}
|
|
664
980
|
// 先保存点击状态,然后重置拖动状态
|
|
665
981
|
const wasClick = isValidClick;
|
|
982
|
+
// 如果真正拖动过,执行磁性吸附
|
|
983
|
+
if (this.dragStarted && this.magnetic && this.iconElement) {
|
|
984
|
+
this.magneticSnap();
|
|
985
|
+
}
|
|
666
986
|
// 如果真正拖动过,保存当前位置到 iconPosition
|
|
667
987
|
if (this.dragStarted && this.isDragging && this.iconElement) {
|
|
668
988
|
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
@@ -687,6 +1007,11 @@ class IconManager {
|
|
|
687
1007
|
this.hasMoved = false;
|
|
688
1008
|
this.isDragging = false;
|
|
689
1009
|
this.dragStarted = false;
|
|
1010
|
+
this.isStoppingDrag = false; // 重置停止标志
|
|
1011
|
+
// 如果拖动后没有吸附到侧边,启动自动吸附定时器
|
|
1012
|
+
if (!this.isAttachedToSide && this.sideAttach && this.autoAttachDelay > 0) {
|
|
1013
|
+
this.startAutoAttachTimer();
|
|
1014
|
+
}
|
|
690
1015
|
if (wasClick) {
|
|
691
1016
|
// 是点击,触发点击事件
|
|
692
1017
|
// 延迟触发,确保拖动状态已完全重置,并且没有新的拖动开始
|
|
@@ -713,9 +1038,10 @@ class IconManager {
|
|
|
713
1038
|
console.log('[IconManager] Starting active detection mechanism');
|
|
714
1039
|
}
|
|
715
1040
|
const checkMousePosition = () => {
|
|
716
|
-
|
|
1041
|
+
// 检查是否还在拖动中,如果不在拖动中或正在停止,则停止检测
|
|
1042
|
+
if (!this.isDragging || this.isStoppingDrag) {
|
|
717
1043
|
if (this.debug) {
|
|
718
|
-
console.log('[IconManager] Active detection stopped (drag ended)');
|
|
1044
|
+
console.log('[IconManager] Active detection stopped (drag ended or stopping)');
|
|
719
1045
|
}
|
|
720
1046
|
this.activeDetectionRafId = null;
|
|
721
1047
|
return;
|
|
@@ -723,15 +1049,19 @@ class IconManager {
|
|
|
723
1049
|
// 检查是否长时间没有收到 mousemove 事件(超过 50ms)
|
|
724
1050
|
const now = Date.now();
|
|
725
1051
|
const timeSinceLastMove = now - this.lastMousePosition.timestamp;
|
|
1052
|
+
// 如果超过 50ms 没有收到事件,进行检测
|
|
726
1053
|
if (timeSinceLastMove > 50) {
|
|
727
1054
|
// 长时间没有收到事件,可能鼠标已经进入 iframe
|
|
728
1055
|
// 使用最后记录的鼠标位置进行检测
|
|
729
1056
|
const clientX = this.lastMousePosition.x;
|
|
730
1057
|
const clientY = this.lastMousePosition.y;
|
|
731
|
-
if (this.debug) {
|
|
732
|
-
|
|
1058
|
+
if (this.debug && timeSinceLastMove > 100) {
|
|
1059
|
+
// 只在超过 100ms 时记录警告,避免日志过多
|
|
1060
|
+
console.warn(`[IconManager] Active detection: No mousemove event for ${timeSinceLastMove}ms`, {
|
|
733
1061
|
lastMousePosition: { x: clientX, y: clientY },
|
|
734
|
-
timestamp: this.lastMousePosition.timestamp
|
|
1062
|
+
timestamp: this.lastMousePosition.timestamp,
|
|
1063
|
+
isDragging: this.isDragging,
|
|
1064
|
+
dragStarted: this.dragStarted
|
|
735
1065
|
});
|
|
736
1066
|
}
|
|
737
1067
|
// 更新 iframe 缓存(更频繁,每帧更新)
|
|
@@ -803,6 +1133,7 @@ class IconManager {
|
|
|
803
1133
|
document.removeEventListener('touchend', this.stopDragHandler);
|
|
804
1134
|
}
|
|
805
1135
|
// 清理处理 iframe 事件丢失的监听器
|
|
1136
|
+
// 注意:必须先移除监听器,再清理防抖定时器,防止事件在清理过程中触发
|
|
806
1137
|
if (this.mouseLeaveHandler) {
|
|
807
1138
|
document.removeEventListener('mouseleave', this.mouseLeaveHandler);
|
|
808
1139
|
this.mouseLeaveHandler = null;
|
|
@@ -815,6 +1146,15 @@ class IconManager {
|
|
|
815
1146
|
window.removeEventListener('blur', this.blurHandler);
|
|
816
1147
|
this.blurHandler = null;
|
|
817
1148
|
}
|
|
1149
|
+
// 清理防抖定时器(在移除监听器之后)
|
|
1150
|
+
if (this.mouseLeaveTimeout !== null) {
|
|
1151
|
+
window.clearTimeout(this.mouseLeaveTimeout);
|
|
1152
|
+
this.mouseLeaveTimeout = null;
|
|
1153
|
+
}
|
|
1154
|
+
if (this.pointerLeaveTimeout !== null) {
|
|
1155
|
+
window.clearTimeout(this.pointerLeaveTimeout);
|
|
1156
|
+
this.pointerLeaveTimeout = null;
|
|
1157
|
+
}
|
|
818
1158
|
// 清理超时定时器
|
|
819
1159
|
if (this.dragTimeoutId !== null) {
|
|
820
1160
|
window.clearTimeout(this.dragTimeoutId);
|
|
@@ -825,13 +1165,31 @@ class IconManager {
|
|
|
825
1165
|
cancelAnimationFrame(this.rafId);
|
|
826
1166
|
this.rafId = null;
|
|
827
1167
|
}
|
|
828
|
-
//
|
|
1168
|
+
// 清理主动检测机制(必须在最后,确保完全清理)
|
|
829
1169
|
this.stopActiveDetection();
|
|
1170
|
+
// 确保 activeDetectionRafId 被清理(双重保险)
|
|
1171
|
+
if (this.activeDetectionRafId !== null) {
|
|
1172
|
+
if (this.debug) {
|
|
1173
|
+
console.warn('[IconManager] Active detection RAF ID still exists, force cleaning');
|
|
1174
|
+
}
|
|
1175
|
+
cancelAnimationFrame(this.activeDetectionRafId);
|
|
1176
|
+
this.activeDetectionRafId = null;
|
|
1177
|
+
}
|
|
830
1178
|
// 清理缓存
|
|
831
1179
|
this.cachedContainer = null;
|
|
832
1180
|
this.cachedContainerRect = null;
|
|
833
1181
|
this.cachedIframes = []; // 清理 iframe 缓存
|
|
834
1182
|
this.pendingPosition.needsUpdate = false;
|
|
1183
|
+
if (this.debug) {
|
|
1184
|
+
console.log('[IconManager] Cleanup completed', {
|
|
1185
|
+
isDragging: this.isDragging,
|
|
1186
|
+
dragStarted: this.dragStarted,
|
|
1187
|
+
isStoppingDrag: this.isStoppingDrag,
|
|
1188
|
+
hasTimeout: this.dragTimeoutId !== null,
|
|
1189
|
+
hasActiveDetection: this.activeDetectionRafId !== null,
|
|
1190
|
+
hasRaf: this.rafId !== null
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
835
1193
|
}
|
|
836
1194
|
/**
|
|
837
1195
|
* 处理点击事件
|
|
@@ -841,6 +1199,8 @@ class IconManager {
|
|
|
841
1199
|
if (!this.isClickEnabled) {
|
|
842
1200
|
return;
|
|
843
1201
|
}
|
|
1202
|
+
// 清除自动吸附定时器(用户有操作)
|
|
1203
|
+
this.clearAutoAttachTimer();
|
|
844
1204
|
if (this.onClickCallback) {
|
|
845
1205
|
this.onClickCallback();
|
|
846
1206
|
}
|
|
@@ -883,6 +1243,142 @@ class IconManager {
|
|
|
883
1243
|
this.iconElement.style.cursor = 'pointer';
|
|
884
1244
|
}
|
|
885
1245
|
}
|
|
1246
|
+
/**
|
|
1247
|
+
* 磁性吸附
|
|
1248
|
+
*/
|
|
1249
|
+
magneticSnap() {
|
|
1250
|
+
if (!this.iconElement)
|
|
1251
|
+
return;
|
|
1252
|
+
try {
|
|
1253
|
+
const container = this.getTargetElement();
|
|
1254
|
+
if (!container)
|
|
1255
|
+
return;
|
|
1256
|
+
const containerRect = container.getBoundingClientRect();
|
|
1257
|
+
const iconWidth = this.iconElement.offsetWidth || 30;
|
|
1258
|
+
const iconHeight = this.iconElement.offsetHeight || 30;
|
|
1259
|
+
// 获取当前图标位置
|
|
1260
|
+
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
1261
|
+
const currentLeft = parseFloat(computedStyle.left) || 0;
|
|
1262
|
+
const currentTop = parseFloat(computedStyle.top) || 0;
|
|
1263
|
+
let newX = currentLeft;
|
|
1264
|
+
let newY = currentTop;
|
|
1265
|
+
// X 轴磁性吸附
|
|
1266
|
+
if (this.magneticDirection === 'x' || this.magneticDirection === 'both') {
|
|
1267
|
+
const containerWidth = containerRect.width;
|
|
1268
|
+
const centerX = containerWidth / 2;
|
|
1269
|
+
if (currentLeft < centerX) {
|
|
1270
|
+
// 吸附到左边
|
|
1271
|
+
newX = this.margin;
|
|
1272
|
+
// 侧边吸附逻辑
|
|
1273
|
+
if (this.sideAttach && currentLeft < iconWidth / 2) {
|
|
1274
|
+
this.isAttachedToSide = true;
|
|
1275
|
+
this.attachedSide = 'left';
|
|
1276
|
+
// 计算隐藏部分:图标宽度 * sideHideRatio
|
|
1277
|
+
// 例如:30px * 0.5 = 15px 隐藏,15px 显示
|
|
1278
|
+
const hideDistance = iconWidth * this.sideHideRatio;
|
|
1279
|
+
// 图标左边缘对齐容器左边缘,隐藏部分在容器外
|
|
1280
|
+
newX = -hideDistance; // 负值,让图标超出容器左边缘
|
|
1281
|
+
}
|
|
1282
|
+
else {
|
|
1283
|
+
this.isAttachedToSide = false;
|
|
1284
|
+
this.attachedSide = '';
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
else {
|
|
1288
|
+
// 吸附到右边
|
|
1289
|
+
newX = containerWidth - iconWidth - this.margin;
|
|
1290
|
+
// 侧边吸附逻辑:如果启用侧边吸附,默认吸附到右边并显示一半
|
|
1291
|
+
if (this.sideAttach) {
|
|
1292
|
+
// 如果靠近右边(距离右边 < 图标宽度的一半),或者从默认位置初始化
|
|
1293
|
+
// 默认位置(右下角)应该自动侧边吸附
|
|
1294
|
+
const distanceFromRight = containerWidth - currentLeft - iconWidth;
|
|
1295
|
+
if (distanceFromRight < iconWidth / 2 ||
|
|
1296
|
+
(isNaN(currentLeft) || currentLeft === 0) ||
|
|
1297
|
+
currentLeft >= containerWidth - iconWidth - this.margin - iconWidth / 2) {
|
|
1298
|
+
this.isAttachedToSide = true;
|
|
1299
|
+
this.attachedSide = 'right';
|
|
1300
|
+
// 计算显示部分:图标宽度 * (1 - sideHideRatio)
|
|
1301
|
+
// 例如:30px * (1 - 0.5) = 15px 显示,15px 隐藏
|
|
1302
|
+
const visibleWidth = iconWidth * (1 - this.sideHideRatio);
|
|
1303
|
+
// 图标右边缘对齐容器右边缘,但只显示 visibleWidth
|
|
1304
|
+
newX = containerWidth - visibleWidth;
|
|
1305
|
+
}
|
|
1306
|
+
else {
|
|
1307
|
+
this.isAttachedToSide = false;
|
|
1308
|
+
this.attachedSide = '';
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
else {
|
|
1312
|
+
this.isAttachedToSide = false;
|
|
1313
|
+
this.attachedSide = '';
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
// Y 轴磁性吸附
|
|
1318
|
+
if (this.magneticDirection === 'y' || this.magneticDirection === 'both') {
|
|
1319
|
+
const containerHeight = containerRect.height;
|
|
1320
|
+
const centerY = containerHeight / 2;
|
|
1321
|
+
if (currentTop < centerY) {
|
|
1322
|
+
newY = this.margin; // 吸附到上边
|
|
1323
|
+
}
|
|
1324
|
+
else {
|
|
1325
|
+
newY = containerHeight - iconHeight - this.margin; // 吸附到下边
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
// 应用新位置
|
|
1329
|
+
// 如果侧边吸附到右边,使用 right 属性(负值),让图标超出容器右边缘
|
|
1330
|
+
if (this.isAttachedToSide && this.attachedSide === 'right') {
|
|
1331
|
+
const hideDistance = iconWidth * this.sideHideRatio; // 隐藏部分(在容器外)
|
|
1332
|
+
this.iconElement.style.right = `-${hideDistance}px`; // 负值,让图标超出容器右边缘
|
|
1333
|
+
this.iconElement.style.left = 'auto';
|
|
1334
|
+
}
|
|
1335
|
+
else if (this.isAttachedToSide && this.attachedSide === 'left') {
|
|
1336
|
+
// 左边吸附使用 left(负值),让图标超出容器左边缘
|
|
1337
|
+
const hideDistance = iconWidth * this.sideHideRatio;
|
|
1338
|
+
this.iconElement.style.left = `-${hideDistance}px`;
|
|
1339
|
+
this.iconElement.style.right = 'auto';
|
|
1340
|
+
}
|
|
1341
|
+
else {
|
|
1342
|
+
// 正常位置使用 left
|
|
1343
|
+
this.iconElement.style.left = `${newX}px`;
|
|
1344
|
+
this.iconElement.style.right = 'auto';
|
|
1345
|
+
}
|
|
1346
|
+
this.iconElement.style.top = `${newY}px`;
|
|
1347
|
+
this.iconElement.style.bottom = 'auto';
|
|
1348
|
+
// 如果侧边吸附,应用 transform
|
|
1349
|
+
if (this.isAttachedToSide && this.attachedSide) {
|
|
1350
|
+
this.iconElement.style.transform = 'translateX(0)';
|
|
1351
|
+
this.iconElement.style.transition = 'all 0.3s ease';
|
|
1352
|
+
}
|
|
1353
|
+
else {
|
|
1354
|
+
this.iconElement.style.transform = '';
|
|
1355
|
+
}
|
|
1356
|
+
// 保存位置(如果是右边吸附,保存为 right 值,否则保存为 left 值)
|
|
1357
|
+
if (this.isAttachedToSide && this.attachedSide === 'right') {
|
|
1358
|
+
// 右边吸附时,不保存到 iconPosition(因为使用的是 right 属性)
|
|
1359
|
+
// 或者可以保存一个特殊标记
|
|
1360
|
+
this.iconPosition = null; // 清除位置,让下次显示时重新计算
|
|
1361
|
+
}
|
|
1362
|
+
else {
|
|
1363
|
+
this.iconPosition = {
|
|
1364
|
+
x: newX,
|
|
1365
|
+
y: newY
|
|
1366
|
+
};
|
|
1367
|
+
}
|
|
1368
|
+
if (this.debug) {
|
|
1369
|
+
console.log('[IconManager] Magnetic snap applied', {
|
|
1370
|
+
newPosition: { x: newX, y: newY },
|
|
1371
|
+
isAttachedToSide: this.isAttachedToSide,
|
|
1372
|
+
attachedSide: this.attachedSide
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
catch (error) {
|
|
1377
|
+
if (this.debug) {
|
|
1378
|
+
console.error('[IconManager] Error in magneticSnap:', error);
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
886
1382
|
/**
|
|
887
1383
|
* 获取目标元素(支持动态查找)
|
|
888
1384
|
*/
|
|
@@ -21346,7 +21842,14 @@ class CustomerServiceSDK {
|
|
|
21346
21842
|
console.log('Icon config changed, recreating icon manager');
|
|
21347
21843
|
}
|
|
21348
21844
|
}
|
|
21349
|
-
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget
|
|
21845
|
+
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget, {
|
|
21846
|
+
sideAttach: options?.sideAttach !== undefined ? options.sideAttach : true,
|
|
21847
|
+
sideHideRatio: options?.sideHideRatio !== undefined ? options.sideHideRatio : 0.5,
|
|
21848
|
+
magnetic: options?.magnetic !== undefined ? options.magnetic : true,
|
|
21849
|
+
magneticDirection: options?.magneticDirection || 'x',
|
|
21850
|
+
margin: options?.margin !== undefined ? options.margin : 10,
|
|
21851
|
+
autoAttachDelay: options?.autoAttachDelay !== undefined ? options.autoAttachDelay : 3000
|
|
21852
|
+
});
|
|
21350
21853
|
await this.iconManager.show();
|
|
21351
21854
|
// 保存新的配置
|
|
21352
21855
|
this.lastIconConfig = { position: iconPosition, target: iconTarget };
|
|
@@ -21355,7 +21858,14 @@ class CustomerServiceSDK {
|
|
|
21355
21858
|
// 配置没变化,保留图标管理器(避免闪烁)
|
|
21356
21859
|
if (!this.iconManager) {
|
|
21357
21860
|
// 如果不存在,创建新的
|
|
21358
|
-
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget
|
|
21861
|
+
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget, {
|
|
21862
|
+
sideAttach: options?.sideAttach !== undefined ? options.sideAttach : true,
|
|
21863
|
+
sideHideRatio: options?.sideHideRatio !== undefined ? options.sideHideRatio : 0.5,
|
|
21864
|
+
magnetic: options?.magnetic !== undefined ? options.magnetic : true,
|
|
21865
|
+
magneticDirection: options?.magneticDirection || 'x',
|
|
21866
|
+
margin: options?.margin !== undefined ? options.margin : 10,
|
|
21867
|
+
autoAttachDelay: options?.autoAttachDelay !== undefined ? options.autoAttachDelay : 3000
|
|
21868
|
+
});
|
|
21359
21869
|
await this.iconManager.show();
|
|
21360
21870
|
}
|
|
21361
21871
|
else {
|