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