customer-chat-sdk 1.1.12 → 1.1.15
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 +559 -19
- package/dist/customer-sdk.esm.js +559 -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.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// 直接使用base64字符串,避免打包后路径问题
|
|
2
2
|
const iconImage = 'data:image/png;base64,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';
|
|
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
|
-
|
|
103
|
-
|
|
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字符串,避免打包后路径问题)
|
|
@@ -144,6 +200,36 @@ class IconManager {
|
|
|
144
200
|
this.iconElement.appendChild(imgContainer);
|
|
145
201
|
// 添加到目标元素(如果 target 是字符串,需要重新查找,因为可能在初始化时元素还不存在)
|
|
146
202
|
const targetElement = this.getTargetElement();
|
|
203
|
+
// 确保目标容器有 overflow-x: hidden,防止图标超出边界时出现横向滚动条
|
|
204
|
+
// 注意:只在启用侧边吸附时才设置,避免影响其他场景
|
|
205
|
+
if (this.sideAttach && targetElement !== document.body) {
|
|
206
|
+
const computedStyle = window.getComputedStyle(targetElement);
|
|
207
|
+
const currentOverflowX = computedStyle.overflowX;
|
|
208
|
+
const currentOverflowY = computedStyle.overflowY;
|
|
209
|
+
// 如果当前 overflow-x 不是 hidden,设置 overflow-x: hidden
|
|
210
|
+
// 同时设置 overflow-y: hidden 或 auto,避免纵向滚动条
|
|
211
|
+
if (currentOverflowX !== 'hidden') {
|
|
212
|
+
// 保存原始 overflow 值(如果还没有保存)
|
|
213
|
+
if (!targetElement.dataset.originalOverflowX) {
|
|
214
|
+
targetElement.dataset.originalOverflowX = currentOverflowX || 'visible';
|
|
215
|
+
targetElement.dataset.originalOverflowY = currentOverflowY || 'visible';
|
|
216
|
+
}
|
|
217
|
+
// 设置 overflow-x: hidden,防止横向滚动条
|
|
218
|
+
targetElement.style.overflowX = 'hidden';
|
|
219
|
+
// 如果当前 overflow-y 是 visible,设置为 auto,避免纵向滚动条
|
|
220
|
+
// 如果已经是 auto 或 scroll,保持不变
|
|
221
|
+
if (currentOverflowY === 'visible') {
|
|
222
|
+
targetElement.style.overflowY = 'auto';
|
|
223
|
+
}
|
|
224
|
+
if (this.debug) {
|
|
225
|
+
console.log('[IconManager] Set overflow-x: hidden on target container', {
|
|
226
|
+
target: this.target,
|
|
227
|
+
originalOverflowX: currentOverflowX,
|
|
228
|
+
originalOverflowY: currentOverflowY
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
147
233
|
if (targetElement) {
|
|
148
234
|
targetElement.appendChild(this.iconElement);
|
|
149
235
|
}
|
|
@@ -156,6 +242,72 @@ class IconManager {
|
|
|
156
242
|
}
|
|
157
243
|
// 设置拖动事件
|
|
158
244
|
this.setupDragEvents();
|
|
245
|
+
// 如果启用磁性吸附且没有指定位置,初始化后自动吸附到右边(默认显示一半)
|
|
246
|
+
if (this.magnetic && !this.iconPosition) {
|
|
247
|
+
// 等待图标渲染完成后再执行吸附
|
|
248
|
+
setTimeout(() => {
|
|
249
|
+
if (this.iconElement) {
|
|
250
|
+
// 先转换位置(从 right/bottom 到 left/top)
|
|
251
|
+
const container = this.getTargetElement();
|
|
252
|
+
if (container) {
|
|
253
|
+
const containerRect = container.getBoundingClientRect();
|
|
254
|
+
const iconWidth = this.iconElement.offsetWidth || 30;
|
|
255
|
+
const iconHeight = this.iconElement.offsetHeight || 30;
|
|
256
|
+
// 计算默认右下角位置对应的 left/top
|
|
257
|
+
const defaultRight = 20; // 默认 right: 20px
|
|
258
|
+
const defaultBottom = 80; // 默认 bottom: 80px
|
|
259
|
+
// 转换为 left/top(相对于容器)
|
|
260
|
+
const initialLeft = containerRect.width - iconWidth - defaultRight;
|
|
261
|
+
const initialTop = containerRect.height - iconHeight - defaultBottom;
|
|
262
|
+
// 临时设置位置,用于磁性吸附计算
|
|
263
|
+
this.iconElement.style.left = `${initialLeft}px`;
|
|
264
|
+
this.iconElement.style.top = `${initialTop}px`;
|
|
265
|
+
this.iconElement.style.right = 'auto';
|
|
266
|
+
this.iconElement.style.bottom = 'auto';
|
|
267
|
+
// 如果启用侧边吸附,强制吸附到右边并显示一半
|
|
268
|
+
if (this.sideAttach) {
|
|
269
|
+
this.isAttachedToSide = true;
|
|
270
|
+
this.attachedSide = 'right';
|
|
271
|
+
const hideDistance = iconWidth * this.sideHideRatio; // 隐藏部分(在容器外)
|
|
272
|
+
// right = -hideDistance,让图标向右移动 hideDistance,这样隐藏部分在容器外
|
|
273
|
+
this.iconElement.style.right = `-${hideDistance}px`; // 负值,让图标超出容器右边缘
|
|
274
|
+
this.iconElement.style.left = 'auto';
|
|
275
|
+
this.iconElement.style.bottom = `${defaultBottom}px`; // 保持底部位置
|
|
276
|
+
this.iconElement.style.top = 'auto';
|
|
277
|
+
this.iconElement.style.transform = 'translateX(0)';
|
|
278
|
+
this.iconElement.style.transition = 'all 0.3s ease';
|
|
279
|
+
if (this.debug) {
|
|
280
|
+
console.log('[IconManager] Auto side attach on init', {
|
|
281
|
+
isAttachedToSide: this.isAttachedToSide,
|
|
282
|
+
attachedSide: this.attachedSide,
|
|
283
|
+
hideDistance,
|
|
284
|
+
right: `-${hideDistance}px`
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
// 执行磁性吸附(不侧边吸附,只磁性吸附)
|
|
290
|
+
this.magneticSnap();
|
|
291
|
+
if (this.debug) {
|
|
292
|
+
console.log('[IconManager] Auto magnetic snap on init', {
|
|
293
|
+
isAttachedToSide: this.isAttachedToSide,
|
|
294
|
+
attachedSide: this.attachedSide
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}, 200); // 延迟 200ms,确保容器和图标都已渲染完成
|
|
301
|
+
}
|
|
302
|
+
// 启动自动吸附定时器(如果启用侧边吸附且当前未吸附)
|
|
303
|
+
// 注意:初始化时如果已经吸附到侧边,不会启动定时器
|
|
304
|
+
// 但如果图标从隐藏状态恢复显示,且未吸附,应该启动定时器
|
|
305
|
+
if (this.sideAttach && this.autoAttachDelay > 0 && !this.isAttachedToSide) {
|
|
306
|
+
// 延迟启动,确保图标已经完全渲染
|
|
307
|
+
setTimeout(() => {
|
|
308
|
+
this.startAutoAttachTimer();
|
|
309
|
+
}, 300); // 延迟 300ms,确保初始化吸附逻辑已完成
|
|
310
|
+
}
|
|
159
311
|
if (this.debug) {
|
|
160
312
|
console.log('CustomerSDK icon displayed');
|
|
161
313
|
}
|
|
@@ -172,6 +324,8 @@ class IconManager {
|
|
|
172
324
|
hide() {
|
|
173
325
|
// 清理拖动事件
|
|
174
326
|
this.cleanupDragEvents();
|
|
327
|
+
// 清除自动吸附定时器
|
|
328
|
+
this.clearAutoAttachTimer();
|
|
175
329
|
if (this.iconElement) {
|
|
176
330
|
// 在隐藏前保存当前位置(如果图标已经被拖动过)
|
|
177
331
|
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
@@ -233,6 +387,110 @@ class IconManager {
|
|
|
233
387
|
setStyle(style) {
|
|
234
388
|
// 现在样式写死,不做处理
|
|
235
389
|
}
|
|
390
|
+
/**
|
|
391
|
+
* 启动自动吸附定时器
|
|
392
|
+
*/
|
|
393
|
+
startAutoAttachTimer() {
|
|
394
|
+
// 清除之前的定时器
|
|
395
|
+
this.clearAutoAttachTimer();
|
|
396
|
+
// 如果已经吸附到侧边,不需要启动定时器
|
|
397
|
+
if (this.isAttachedToSide) {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
this.autoAttachTimer = window.setTimeout(() => {
|
|
401
|
+
if (this.iconElement && !this.isDragging && !this.dragStarted) {
|
|
402
|
+
// 执行自动吸附到侧边
|
|
403
|
+
this.autoAttachToSide();
|
|
404
|
+
}
|
|
405
|
+
this.autoAttachTimer = null;
|
|
406
|
+
}, this.autoAttachDelay);
|
|
407
|
+
if (this.debug) {
|
|
408
|
+
console.log(`[IconManager] Auto attach timer started, will attach in ${this.autoAttachDelay}ms`);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* 清除自动吸附定时器
|
|
413
|
+
*/
|
|
414
|
+
clearAutoAttachTimer() {
|
|
415
|
+
if (this.autoAttachTimer !== null) {
|
|
416
|
+
window.clearTimeout(this.autoAttachTimer);
|
|
417
|
+
this.autoAttachTimer = null;
|
|
418
|
+
if (this.debug) {
|
|
419
|
+
console.log('[IconManager] Auto attach timer cleared');
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* 自动吸附到侧边
|
|
425
|
+
*/
|
|
426
|
+
autoAttachToSide() {
|
|
427
|
+
if (!this.iconElement || !this.sideAttach)
|
|
428
|
+
return;
|
|
429
|
+
try {
|
|
430
|
+
const container = this.getTargetElement();
|
|
431
|
+
if (!container)
|
|
432
|
+
return;
|
|
433
|
+
const containerRect = container.getBoundingClientRect();
|
|
434
|
+
const iconWidth = this.iconElement.offsetWidth || 30;
|
|
435
|
+
// 获取当前图标位置
|
|
436
|
+
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
437
|
+
const currentLeft = parseFloat(computedStyle.left) || 0;
|
|
438
|
+
const currentRight = computedStyle.right;
|
|
439
|
+
// 判断应该吸附到哪一边(根据当前位置)
|
|
440
|
+
const containerWidth = containerRect.width;
|
|
441
|
+
let shouldAttachToRight = true;
|
|
442
|
+
// 如果使用 right 定位,计算实际 left 位置
|
|
443
|
+
if (currentRight !== 'auto' && currentRight !== '') {
|
|
444
|
+
const rightValue = parseFloat(currentRight);
|
|
445
|
+
if (!isNaN(rightValue)) {
|
|
446
|
+
const actualLeft = containerWidth - iconWidth - rightValue;
|
|
447
|
+
shouldAttachToRight = actualLeft >= containerWidth / 2;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
shouldAttachToRight = currentLeft >= containerWidth / 2;
|
|
452
|
+
}
|
|
453
|
+
if (shouldAttachToRight) {
|
|
454
|
+
// 吸附到右边
|
|
455
|
+
this.isAttachedToSide = true;
|
|
456
|
+
this.attachedSide = 'right';
|
|
457
|
+
const hideDistance = iconWidth * this.sideHideRatio;
|
|
458
|
+
this.iconElement.style.right = `-${hideDistance}px`;
|
|
459
|
+
this.iconElement.style.left = 'auto';
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
// 吸附到左边
|
|
463
|
+
this.isAttachedToSide = true;
|
|
464
|
+
this.attachedSide = 'left';
|
|
465
|
+
const hideDistance = iconWidth * this.sideHideRatio;
|
|
466
|
+
this.iconElement.style.left = `-${hideDistance}px`;
|
|
467
|
+
this.iconElement.style.right = 'auto';
|
|
468
|
+
}
|
|
469
|
+
// 保持 Y 轴位置不变
|
|
470
|
+
const computedStyleAfter = window.getComputedStyle(this.iconElement);
|
|
471
|
+
if (computedStyleAfter.top !== 'auto' && computedStyleAfter.top !== '') {
|
|
472
|
+
this.iconElement.style.top = computedStyleAfter.top;
|
|
473
|
+
this.iconElement.style.bottom = 'auto';
|
|
474
|
+
}
|
|
475
|
+
else if (computedStyleAfter.bottom !== 'auto' && computedStyleAfter.bottom !== '') {
|
|
476
|
+
this.iconElement.style.bottom = computedStyleAfter.bottom;
|
|
477
|
+
this.iconElement.style.top = 'auto';
|
|
478
|
+
}
|
|
479
|
+
this.iconElement.style.transform = 'translateX(0)';
|
|
480
|
+
this.iconElement.style.transition = 'all 0.3s ease';
|
|
481
|
+
if (this.debug) {
|
|
482
|
+
console.log('[IconManager] Auto attached to side', {
|
|
483
|
+
attachedSide: this.attachedSide,
|
|
484
|
+
hideDistance: iconWidth * this.sideHideRatio
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
catch (error) {
|
|
489
|
+
if (this.debug) {
|
|
490
|
+
console.error('[IconManager] Error in autoAttachToSide:', error);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
236
494
|
/**
|
|
237
495
|
* 设置点击回调
|
|
238
496
|
*/
|
|
@@ -300,6 +558,37 @@ class IconManager {
|
|
|
300
558
|
startDrag(e) {
|
|
301
559
|
if (!this.iconElement || !this.isClickEnabled)
|
|
302
560
|
return;
|
|
561
|
+
// 清除自动吸附定时器(用户开始拖动)
|
|
562
|
+
this.clearAutoAttachTimer();
|
|
563
|
+
// 如果正在停止拖动,等待完成后再允许新的拖动
|
|
564
|
+
if (this.isStoppingDrag) {
|
|
565
|
+
if (this.debug) {
|
|
566
|
+
console.warn('[IconManager] startDrag called while stopping, ignoring');
|
|
567
|
+
}
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
// 如果已经在拖动中,忽略新的拖动开始事件(防止重复调用)
|
|
571
|
+
// 注意:只检查实际的拖动状态,不检查可能残留的资源(这些应该在 cleanup 中清理)
|
|
572
|
+
if (this.isDragging || this.dragStarted) {
|
|
573
|
+
if (this.debug) {
|
|
574
|
+
console.warn('[IconManager] startDrag called but already dragging, ignoring', {
|
|
575
|
+
isDragging: this.isDragging,
|
|
576
|
+
dragStarted: this.dragStarted
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
// 如果发现残留的资源,先清理它们(防御性编程)
|
|
582
|
+
if (this.dragTimeoutId !== null || this.activeDetectionRafId !== null || this.rafId !== null) {
|
|
583
|
+
if (this.debug) {
|
|
584
|
+
console.warn('[IconManager] startDrag found leftover resources, cleaning up', {
|
|
585
|
+
hasTimeout: this.dragTimeoutId !== null,
|
|
586
|
+
hasActiveDetection: this.activeDetectionRafId !== null,
|
|
587
|
+
hasRaf: this.rafId !== null
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
this.cleanupDragEvents();
|
|
591
|
+
}
|
|
303
592
|
// 检查事件目标:如果是 iframe 相关元素,不处理
|
|
304
593
|
const target = e.target;
|
|
305
594
|
if (target && (target.tagName === 'IFRAME' ||
|
|
@@ -364,32 +653,59 @@ class IconManager {
|
|
|
364
653
|
}
|
|
365
654
|
// 添加处理 iframe 上事件丢失的机制
|
|
366
655
|
// 1. 监听 mouseleave 和 pointerleave 事件(鼠标离开窗口时停止拖动)
|
|
656
|
+
// 注意:这些事件可能会在拖动过程中多次触发,需要添加防抖机制
|
|
367
657
|
this.mouseLeaveHandler = (e) => {
|
|
368
658
|
// 只有当鼠标真正离开窗口时才停止拖动
|
|
369
659
|
if (!e.relatedTarget && (e.clientY <= 0 || e.clientX <= 0 ||
|
|
370
660
|
e.clientX >= window.innerWidth || e.clientY >= window.innerHeight)) {
|
|
371
|
-
|
|
372
|
-
|
|
661
|
+
// 添加防抖,避免重复触发
|
|
662
|
+
if (this.mouseLeaveTimeout) {
|
|
663
|
+
clearTimeout(this.mouseLeaveTimeout);
|
|
373
664
|
}
|
|
374
|
-
this.
|
|
665
|
+
this.mouseLeaveTimeout = window.setTimeout(() => {
|
|
666
|
+
if ((this.isDragging || this.dragStarted) && !this.isStoppingDrag) {
|
|
667
|
+
if (this.debug) {
|
|
668
|
+
console.log('[IconManager] Mouse left window, stopping drag');
|
|
669
|
+
}
|
|
670
|
+
this.stopDrag();
|
|
671
|
+
}
|
|
672
|
+
this.mouseLeaveTimeout = null;
|
|
673
|
+
}, 50); // 50ms 防抖
|
|
375
674
|
}
|
|
376
675
|
};
|
|
377
676
|
this.pointerLeaveHandler = (e) => {
|
|
378
677
|
// 只有当指针真正离开窗口时才停止拖动
|
|
379
678
|
if (!e.relatedTarget && (e.clientY <= 0 || e.clientX <= 0 ||
|
|
380
679
|
e.clientX >= window.innerWidth || e.clientY >= window.innerHeight)) {
|
|
381
|
-
|
|
382
|
-
|
|
680
|
+
// 添加防抖,避免重复触发
|
|
681
|
+
if (this.pointerLeaveTimeout) {
|
|
682
|
+
clearTimeout(this.pointerLeaveTimeout);
|
|
383
683
|
}
|
|
384
|
-
this.
|
|
684
|
+
this.pointerLeaveTimeout = window.setTimeout(() => {
|
|
685
|
+
if ((this.isDragging || this.dragStarted) && !this.isStoppingDrag) {
|
|
686
|
+
if (this.debug) {
|
|
687
|
+
console.log('[IconManager] Pointer left window, stopping drag');
|
|
688
|
+
}
|
|
689
|
+
this.stopDrag();
|
|
690
|
+
}
|
|
691
|
+
this.pointerLeaveTimeout = null;
|
|
692
|
+
}, 50); // 50ms 防抖
|
|
385
693
|
}
|
|
386
694
|
};
|
|
387
695
|
document.addEventListener('mouseleave', this.mouseLeaveHandler);
|
|
388
696
|
document.addEventListener('pointerleave', this.pointerLeaveHandler);
|
|
389
697
|
// 2. 监听 blur 事件(窗口失去焦点时停止拖动)
|
|
390
698
|
this.blurHandler = () => {
|
|
699
|
+
// 如果已经在停止拖动,直接返回
|
|
700
|
+
if (this.isStoppingDrag) {
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
// 检查是否真的在拖动中
|
|
704
|
+
if (!this.isDragging && !this.dragStarted) {
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
391
707
|
if (this.debug) {
|
|
392
|
-
console.log('Window lost focus, stopping drag');
|
|
708
|
+
console.log('[IconManager] Window lost focus, stopping drag');
|
|
393
709
|
}
|
|
394
710
|
this.stopDrag();
|
|
395
711
|
};
|
|
@@ -529,6 +845,14 @@ class IconManager {
|
|
|
529
845
|
// 开始真正的拖拽
|
|
530
846
|
this.dragStarted = true;
|
|
531
847
|
this.isDragging = true;
|
|
848
|
+
// 拖拽开始时重置侧边吸附状态
|
|
849
|
+
if (this.isAttachedToSide) {
|
|
850
|
+
this.isAttachedToSide = false;
|
|
851
|
+
this.attachedSide = '';
|
|
852
|
+
if (this.iconElement) {
|
|
853
|
+
this.iconElement.style.transform = '';
|
|
854
|
+
}
|
|
855
|
+
}
|
|
532
856
|
// 如果是第一次拖动,需要转换位置(从 right/bottom 到 left/top)
|
|
533
857
|
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
534
858
|
if (computedStyle.right !== 'auto' || computedStyle.bottom !== 'auto') {
|
|
@@ -629,6 +953,21 @@ class IconManager {
|
|
|
629
953
|
* 停止拖动
|
|
630
954
|
*/
|
|
631
955
|
stopDrag(_e) {
|
|
956
|
+
// 防止重复调用
|
|
957
|
+
if (this.isStoppingDrag) {
|
|
958
|
+
if (this.debug) {
|
|
959
|
+
console.warn('[IconManager] stopDrag already in progress, ignoring duplicate call');
|
|
960
|
+
}
|
|
961
|
+
return;
|
|
962
|
+
}
|
|
963
|
+
// 如果已经停止且没有相关资源,直接返回
|
|
964
|
+
if (!this.isDragging && !this.dragStarted && !this.dragTimeoutId && !this.activeDetectionRafId) {
|
|
965
|
+
if (this.debug) {
|
|
966
|
+
console.warn('[IconManager] stopDrag called but not dragging, ignoring');
|
|
967
|
+
}
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
970
|
+
this.isStoppingDrag = true;
|
|
632
971
|
const stopReason = this.isDragging ? 'drag_stopped' : 'not_dragging';
|
|
633
972
|
const finalPosition = this.iconElement ? {
|
|
634
973
|
left: this.iconElement.style.left,
|
|
@@ -637,9 +976,16 @@ class IconManager {
|
|
|
637
976
|
? { left: window.getComputedStyle(this.iconElement).left, top: window.getComputedStyle(this.iconElement).top }
|
|
638
977
|
: null
|
|
639
978
|
} : null;
|
|
979
|
+
// 立即清理事件监听器,防止后续事件触发
|
|
640
980
|
this.cleanupDragEvents();
|
|
641
|
-
if (!this.iconElement)
|
|
981
|
+
if (!this.iconElement) {
|
|
982
|
+
// 即使没有 iconElement,也要重置状态,确保可以再次拖动
|
|
983
|
+
this.hasMoved = false;
|
|
984
|
+
this.isDragging = false;
|
|
985
|
+
this.dragStarted = false;
|
|
986
|
+
this.isStoppingDrag = false;
|
|
642
987
|
return;
|
|
988
|
+
}
|
|
643
989
|
// 恢复样式
|
|
644
990
|
this.iconElement.style.transition = 'transform 0.2s ease';
|
|
645
991
|
this.iconElement.style.cursor = 'pointer';
|
|
@@ -659,6 +1005,10 @@ class IconManager {
|
|
|
659
1005
|
}
|
|
660
1006
|
// 先保存点击状态,然后重置拖动状态
|
|
661
1007
|
const wasClick = isValidClick;
|
|
1008
|
+
// 如果真正拖动过,执行磁性吸附
|
|
1009
|
+
if (this.dragStarted && this.magnetic && this.iconElement) {
|
|
1010
|
+
this.magneticSnap();
|
|
1011
|
+
}
|
|
662
1012
|
// 如果真正拖动过,保存当前位置到 iconPosition
|
|
663
1013
|
if (this.dragStarted && this.isDragging && this.iconElement) {
|
|
664
1014
|
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
@@ -683,6 +1033,11 @@ class IconManager {
|
|
|
683
1033
|
this.hasMoved = false;
|
|
684
1034
|
this.isDragging = false;
|
|
685
1035
|
this.dragStarted = false;
|
|
1036
|
+
this.isStoppingDrag = false; // 重置停止标志
|
|
1037
|
+
// 如果拖动后没有吸附到侧边,启动自动吸附定时器
|
|
1038
|
+
if (!this.isAttachedToSide && this.sideAttach && this.autoAttachDelay > 0) {
|
|
1039
|
+
this.startAutoAttachTimer();
|
|
1040
|
+
}
|
|
686
1041
|
if (wasClick) {
|
|
687
1042
|
// 是点击,触发点击事件
|
|
688
1043
|
// 延迟触发,确保拖动状态已完全重置,并且没有新的拖动开始
|
|
@@ -709,9 +1064,10 @@ class IconManager {
|
|
|
709
1064
|
console.log('[IconManager] Starting active detection mechanism');
|
|
710
1065
|
}
|
|
711
1066
|
const checkMousePosition = () => {
|
|
712
|
-
|
|
1067
|
+
// 检查是否还在拖动中,如果不在拖动中或正在停止,则停止检测
|
|
1068
|
+
if (!this.isDragging || this.isStoppingDrag) {
|
|
713
1069
|
if (this.debug) {
|
|
714
|
-
console.log('[IconManager] Active detection stopped (drag ended)');
|
|
1070
|
+
console.log('[IconManager] Active detection stopped (drag ended or stopping)');
|
|
715
1071
|
}
|
|
716
1072
|
this.activeDetectionRafId = null;
|
|
717
1073
|
return;
|
|
@@ -719,15 +1075,19 @@ class IconManager {
|
|
|
719
1075
|
// 检查是否长时间没有收到 mousemove 事件(超过 50ms)
|
|
720
1076
|
const now = Date.now();
|
|
721
1077
|
const timeSinceLastMove = now - this.lastMousePosition.timestamp;
|
|
1078
|
+
// 如果超过 50ms 没有收到事件,进行检测
|
|
722
1079
|
if (timeSinceLastMove > 50) {
|
|
723
1080
|
// 长时间没有收到事件,可能鼠标已经进入 iframe
|
|
724
1081
|
// 使用最后记录的鼠标位置进行检测
|
|
725
1082
|
const clientX = this.lastMousePosition.x;
|
|
726
1083
|
const clientY = this.lastMousePosition.y;
|
|
727
|
-
if (this.debug) {
|
|
728
|
-
|
|
1084
|
+
if (this.debug && timeSinceLastMove > 100) {
|
|
1085
|
+
// 只在超过 100ms 时记录警告,避免日志过多
|
|
1086
|
+
console.warn(`[IconManager] Active detection: No mousemove event for ${timeSinceLastMove}ms`, {
|
|
729
1087
|
lastMousePosition: { x: clientX, y: clientY },
|
|
730
|
-
timestamp: this.lastMousePosition.timestamp
|
|
1088
|
+
timestamp: this.lastMousePosition.timestamp,
|
|
1089
|
+
isDragging: this.isDragging,
|
|
1090
|
+
dragStarted: this.dragStarted
|
|
731
1091
|
});
|
|
732
1092
|
}
|
|
733
1093
|
// 更新 iframe 缓存(更频繁,每帧更新)
|
|
@@ -799,6 +1159,7 @@ class IconManager {
|
|
|
799
1159
|
document.removeEventListener('touchend', this.stopDragHandler);
|
|
800
1160
|
}
|
|
801
1161
|
// 清理处理 iframe 事件丢失的监听器
|
|
1162
|
+
// 注意:必须先移除监听器,再清理防抖定时器,防止事件在清理过程中触发
|
|
802
1163
|
if (this.mouseLeaveHandler) {
|
|
803
1164
|
document.removeEventListener('mouseleave', this.mouseLeaveHandler);
|
|
804
1165
|
this.mouseLeaveHandler = null;
|
|
@@ -811,6 +1172,15 @@ class IconManager {
|
|
|
811
1172
|
window.removeEventListener('blur', this.blurHandler);
|
|
812
1173
|
this.blurHandler = null;
|
|
813
1174
|
}
|
|
1175
|
+
// 清理防抖定时器(在移除监听器之后)
|
|
1176
|
+
if (this.mouseLeaveTimeout !== null) {
|
|
1177
|
+
window.clearTimeout(this.mouseLeaveTimeout);
|
|
1178
|
+
this.mouseLeaveTimeout = null;
|
|
1179
|
+
}
|
|
1180
|
+
if (this.pointerLeaveTimeout !== null) {
|
|
1181
|
+
window.clearTimeout(this.pointerLeaveTimeout);
|
|
1182
|
+
this.pointerLeaveTimeout = null;
|
|
1183
|
+
}
|
|
814
1184
|
// 清理超时定时器
|
|
815
1185
|
if (this.dragTimeoutId !== null) {
|
|
816
1186
|
window.clearTimeout(this.dragTimeoutId);
|
|
@@ -821,13 +1191,31 @@ class IconManager {
|
|
|
821
1191
|
cancelAnimationFrame(this.rafId);
|
|
822
1192
|
this.rafId = null;
|
|
823
1193
|
}
|
|
824
|
-
//
|
|
1194
|
+
// 清理主动检测机制(必须在最后,确保完全清理)
|
|
825
1195
|
this.stopActiveDetection();
|
|
1196
|
+
// 确保 activeDetectionRafId 被清理(双重保险)
|
|
1197
|
+
if (this.activeDetectionRafId !== null) {
|
|
1198
|
+
if (this.debug) {
|
|
1199
|
+
console.warn('[IconManager] Active detection RAF ID still exists, force cleaning');
|
|
1200
|
+
}
|
|
1201
|
+
cancelAnimationFrame(this.activeDetectionRafId);
|
|
1202
|
+
this.activeDetectionRafId = null;
|
|
1203
|
+
}
|
|
826
1204
|
// 清理缓存
|
|
827
1205
|
this.cachedContainer = null;
|
|
828
1206
|
this.cachedContainerRect = null;
|
|
829
1207
|
this.cachedIframes = []; // 清理 iframe 缓存
|
|
830
1208
|
this.pendingPosition.needsUpdate = false;
|
|
1209
|
+
if (this.debug) {
|
|
1210
|
+
console.log('[IconManager] Cleanup completed', {
|
|
1211
|
+
isDragging: this.isDragging,
|
|
1212
|
+
dragStarted: this.dragStarted,
|
|
1213
|
+
isStoppingDrag: this.isStoppingDrag,
|
|
1214
|
+
hasTimeout: this.dragTimeoutId !== null,
|
|
1215
|
+
hasActiveDetection: this.activeDetectionRafId !== null,
|
|
1216
|
+
hasRaf: this.rafId !== null
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
831
1219
|
}
|
|
832
1220
|
/**
|
|
833
1221
|
* 处理点击事件
|
|
@@ -837,6 +1225,8 @@ class IconManager {
|
|
|
837
1225
|
if (!this.isClickEnabled) {
|
|
838
1226
|
return;
|
|
839
1227
|
}
|
|
1228
|
+
// 清除自动吸附定时器(用户有操作)
|
|
1229
|
+
this.clearAutoAttachTimer();
|
|
840
1230
|
if (this.onClickCallback) {
|
|
841
1231
|
this.onClickCallback();
|
|
842
1232
|
}
|
|
@@ -879,6 +1269,142 @@ class IconManager {
|
|
|
879
1269
|
this.iconElement.style.cursor = 'pointer';
|
|
880
1270
|
}
|
|
881
1271
|
}
|
|
1272
|
+
/**
|
|
1273
|
+
* 磁性吸附
|
|
1274
|
+
*/
|
|
1275
|
+
magneticSnap() {
|
|
1276
|
+
if (!this.iconElement)
|
|
1277
|
+
return;
|
|
1278
|
+
try {
|
|
1279
|
+
const container = this.getTargetElement();
|
|
1280
|
+
if (!container)
|
|
1281
|
+
return;
|
|
1282
|
+
const containerRect = container.getBoundingClientRect();
|
|
1283
|
+
const iconWidth = this.iconElement.offsetWidth || 30;
|
|
1284
|
+
const iconHeight = this.iconElement.offsetHeight || 30;
|
|
1285
|
+
// 获取当前图标位置
|
|
1286
|
+
const computedStyle = window.getComputedStyle(this.iconElement);
|
|
1287
|
+
const currentLeft = parseFloat(computedStyle.left) || 0;
|
|
1288
|
+
const currentTop = parseFloat(computedStyle.top) || 0;
|
|
1289
|
+
let newX = currentLeft;
|
|
1290
|
+
let newY = currentTop;
|
|
1291
|
+
// X 轴磁性吸附
|
|
1292
|
+
if (this.magneticDirection === 'x' || this.magneticDirection === 'both') {
|
|
1293
|
+
const containerWidth = containerRect.width;
|
|
1294
|
+
const centerX = containerWidth / 2;
|
|
1295
|
+
if (currentLeft < centerX) {
|
|
1296
|
+
// 吸附到左边
|
|
1297
|
+
newX = this.margin;
|
|
1298
|
+
// 侧边吸附逻辑
|
|
1299
|
+
if (this.sideAttach && currentLeft < iconWidth / 2) {
|
|
1300
|
+
this.isAttachedToSide = true;
|
|
1301
|
+
this.attachedSide = 'left';
|
|
1302
|
+
// 计算隐藏部分:图标宽度 * sideHideRatio
|
|
1303
|
+
// 例如:30px * 0.5 = 15px 隐藏,15px 显示
|
|
1304
|
+
const hideDistance = iconWidth * this.sideHideRatio;
|
|
1305
|
+
// 图标左边缘对齐容器左边缘,隐藏部分在容器外
|
|
1306
|
+
newX = -hideDistance; // 负值,让图标超出容器左边缘
|
|
1307
|
+
}
|
|
1308
|
+
else {
|
|
1309
|
+
this.isAttachedToSide = false;
|
|
1310
|
+
this.attachedSide = '';
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
else {
|
|
1314
|
+
// 吸附到右边
|
|
1315
|
+
newX = containerWidth - iconWidth - this.margin;
|
|
1316
|
+
// 侧边吸附逻辑:如果启用侧边吸附,默认吸附到右边并显示一半
|
|
1317
|
+
if (this.sideAttach) {
|
|
1318
|
+
// 如果靠近右边(距离右边 < 图标宽度的一半),或者从默认位置初始化
|
|
1319
|
+
// 默认位置(右下角)应该自动侧边吸附
|
|
1320
|
+
const distanceFromRight = containerWidth - currentLeft - iconWidth;
|
|
1321
|
+
if (distanceFromRight < iconWidth / 2 ||
|
|
1322
|
+
(isNaN(currentLeft) || currentLeft === 0) ||
|
|
1323
|
+
currentLeft >= containerWidth - iconWidth - this.margin - iconWidth / 2) {
|
|
1324
|
+
this.isAttachedToSide = true;
|
|
1325
|
+
this.attachedSide = 'right';
|
|
1326
|
+
// 计算显示部分:图标宽度 * (1 - sideHideRatio)
|
|
1327
|
+
// 例如:30px * (1 - 0.5) = 15px 显示,15px 隐藏
|
|
1328
|
+
const visibleWidth = iconWidth * (1 - this.sideHideRatio);
|
|
1329
|
+
// 图标右边缘对齐容器右边缘,但只显示 visibleWidth
|
|
1330
|
+
newX = containerWidth - visibleWidth;
|
|
1331
|
+
}
|
|
1332
|
+
else {
|
|
1333
|
+
this.isAttachedToSide = false;
|
|
1334
|
+
this.attachedSide = '';
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
else {
|
|
1338
|
+
this.isAttachedToSide = false;
|
|
1339
|
+
this.attachedSide = '';
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
// Y 轴磁性吸附
|
|
1344
|
+
if (this.magneticDirection === 'y' || this.magneticDirection === 'both') {
|
|
1345
|
+
const containerHeight = containerRect.height;
|
|
1346
|
+
const centerY = containerHeight / 2;
|
|
1347
|
+
if (currentTop < centerY) {
|
|
1348
|
+
newY = this.margin; // 吸附到上边
|
|
1349
|
+
}
|
|
1350
|
+
else {
|
|
1351
|
+
newY = containerHeight - iconHeight - this.margin; // 吸附到下边
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
// 应用新位置
|
|
1355
|
+
// 如果侧边吸附到右边,使用 right 属性(负值),让图标超出容器右边缘
|
|
1356
|
+
if (this.isAttachedToSide && this.attachedSide === 'right') {
|
|
1357
|
+
const hideDistance = iconWidth * this.sideHideRatio; // 隐藏部分(在容器外)
|
|
1358
|
+
this.iconElement.style.right = `-${hideDistance}px`; // 负值,让图标超出容器右边缘
|
|
1359
|
+
this.iconElement.style.left = 'auto';
|
|
1360
|
+
}
|
|
1361
|
+
else if (this.isAttachedToSide && this.attachedSide === 'left') {
|
|
1362
|
+
// 左边吸附使用 left(负值),让图标超出容器左边缘
|
|
1363
|
+
const hideDistance = iconWidth * this.sideHideRatio;
|
|
1364
|
+
this.iconElement.style.left = `-${hideDistance}px`;
|
|
1365
|
+
this.iconElement.style.right = 'auto';
|
|
1366
|
+
}
|
|
1367
|
+
else {
|
|
1368
|
+
// 正常位置使用 left
|
|
1369
|
+
this.iconElement.style.left = `${newX}px`;
|
|
1370
|
+
this.iconElement.style.right = 'auto';
|
|
1371
|
+
}
|
|
1372
|
+
this.iconElement.style.top = `${newY}px`;
|
|
1373
|
+
this.iconElement.style.bottom = 'auto';
|
|
1374
|
+
// 如果侧边吸附,应用 transform
|
|
1375
|
+
if (this.isAttachedToSide && this.attachedSide) {
|
|
1376
|
+
this.iconElement.style.transform = 'translateX(0)';
|
|
1377
|
+
this.iconElement.style.transition = 'all 0.3s ease';
|
|
1378
|
+
}
|
|
1379
|
+
else {
|
|
1380
|
+
this.iconElement.style.transform = '';
|
|
1381
|
+
}
|
|
1382
|
+
// 保存位置(如果是右边吸附,保存为 right 值,否则保存为 left 值)
|
|
1383
|
+
if (this.isAttachedToSide && this.attachedSide === 'right') {
|
|
1384
|
+
// 右边吸附时,不保存到 iconPosition(因为使用的是 right 属性)
|
|
1385
|
+
// 或者可以保存一个特殊标记
|
|
1386
|
+
this.iconPosition = null; // 清除位置,让下次显示时重新计算
|
|
1387
|
+
}
|
|
1388
|
+
else {
|
|
1389
|
+
this.iconPosition = {
|
|
1390
|
+
x: newX,
|
|
1391
|
+
y: newY
|
|
1392
|
+
};
|
|
1393
|
+
}
|
|
1394
|
+
if (this.debug) {
|
|
1395
|
+
console.log('[IconManager] Magnetic snap applied', {
|
|
1396
|
+
newPosition: { x: newX, y: newY },
|
|
1397
|
+
isAttachedToSide: this.isAttachedToSide,
|
|
1398
|
+
attachedSide: this.attachedSide
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
}
|
|
1402
|
+
catch (error) {
|
|
1403
|
+
if (this.debug) {
|
|
1404
|
+
console.error('[IconManager] Error in magneticSnap:', error);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
882
1408
|
/**
|
|
883
1409
|
* 获取目标元素(支持动态查找)
|
|
884
1410
|
*/
|
|
@@ -21342,7 +21868,14 @@ class CustomerServiceSDK {
|
|
|
21342
21868
|
console.log('Icon config changed, recreating icon manager');
|
|
21343
21869
|
}
|
|
21344
21870
|
}
|
|
21345
|
-
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget
|
|
21871
|
+
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget, {
|
|
21872
|
+
sideAttach: options?.sideAttach !== undefined ? options.sideAttach : true,
|
|
21873
|
+
sideHideRatio: options?.sideHideRatio !== undefined ? options.sideHideRatio : 0.5,
|
|
21874
|
+
magnetic: options?.magnetic !== undefined ? options.magnetic : true,
|
|
21875
|
+
magneticDirection: options?.magneticDirection || 'x',
|
|
21876
|
+
margin: options?.margin !== undefined ? options.margin : 10,
|
|
21877
|
+
autoAttachDelay: options?.autoAttachDelay !== undefined ? options.autoAttachDelay : 3000
|
|
21878
|
+
});
|
|
21346
21879
|
await this.iconManager.show();
|
|
21347
21880
|
// 保存新的配置
|
|
21348
21881
|
this.lastIconConfig = { position: iconPosition, target: iconTarget };
|
|
@@ -21351,7 +21884,14 @@ class CustomerServiceSDK {
|
|
|
21351
21884
|
// 配置没变化,保留图标管理器(避免闪烁)
|
|
21352
21885
|
if (!this.iconManager) {
|
|
21353
21886
|
// 如果不存在,创建新的
|
|
21354
|
-
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget
|
|
21887
|
+
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget, {
|
|
21888
|
+
sideAttach: options?.sideAttach !== undefined ? options.sideAttach : true,
|
|
21889
|
+
sideHideRatio: options?.sideHideRatio !== undefined ? options.sideHideRatio : 0.5,
|
|
21890
|
+
magnetic: options?.magnetic !== undefined ? options.magnetic : true,
|
|
21891
|
+
magneticDirection: options?.magneticDirection || 'x',
|
|
21892
|
+
margin: options?.margin !== undefined ? options.margin : 10,
|
|
21893
|
+
autoAttachDelay: options?.autoAttachDelay !== undefined ? options.autoAttachDelay : 3000
|
|
21894
|
+
});
|
|
21355
21895
|
await this.iconManager.show();
|
|
21356
21896
|
}
|
|
21357
21897
|
else {
|