electron-drag-plus 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -15
- package/dist/example.js +5 -5
- package/dist/index.d.ts +2 -3
- package/dist/index.js +22 -28
- package/dist/renderer.d.ts +1 -2
- package/dist/renderer.js +183 -75
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
|
|
7
7
|
- **极简 API**:主进程仅需一个方法,渲染进程仅需两个方法
|
|
8
8
|
- **窗口防抖**:内置窗口拖拽时的防抖优化,解决拖拽时窗口大小抖动问题
|
|
9
|
-
- **多窗口支持**:通过 key 参数支持多窗口场景
|
|
10
9
|
- **自动处理**:自动识别并排除交互元素,避免交互冲突
|
|
11
10
|
- **TypeScript 支持**:提供完整的类型定义
|
|
11
|
+
- **安全可靠**:内置错误处理和延迟初始化,确保在各种环境下稳定运行
|
|
12
12
|
|
|
13
13
|
## 安装
|
|
14
14
|
|
|
@@ -27,13 +27,12 @@ pnpm add electron-drag-plus
|
|
|
27
27
|
|
|
28
28
|
### 主进程 API
|
|
29
29
|
|
|
30
|
-
#### `initializeDragPlus(window
|
|
30
|
+
#### `initializeDragPlus(window)`
|
|
31
31
|
|
|
32
32
|
初始化窗口拖拽功能。
|
|
33
33
|
|
|
34
34
|
**参数:**
|
|
35
35
|
- `window: BrowserWindow` - Electron 窗口实例
|
|
36
|
-
- `key: string` - 窗口唯一标识,用于与渲染进程通信
|
|
37
36
|
|
|
38
37
|
**示例:**
|
|
39
38
|
```typescript
|
|
@@ -52,31 +51,30 @@ const mainWindow = new BrowserWindow({
|
|
|
52
51
|
}
|
|
53
52
|
});
|
|
54
53
|
|
|
55
|
-
// 初始化拖拽功能
|
|
56
|
-
initializeDragPlus(mainWindow
|
|
54
|
+
// 初始化拖拽功能 - 无需key参数
|
|
55
|
+
initializeDragPlus(mainWindow);
|
|
57
56
|
```
|
|
58
57
|
|
|
59
58
|
### 渲染进程 API
|
|
60
59
|
|
|
61
|
-
#### `enableWindowDrag(element
|
|
60
|
+
#### `enableWindowDrag(element)`
|
|
62
61
|
|
|
63
62
|
启用指定元素的窗口拖拽功能。
|
|
64
63
|
|
|
65
64
|
**参数:**
|
|
66
65
|
- `element: HTMLElement | string` - DOM 元素或 CSS 选择器
|
|
67
|
-
- `key?: string` - 窗口唯一标识(可选,也可在主进程中初始化时提供)
|
|
68
66
|
|
|
69
67
|
**示例:**
|
|
70
68
|
```typescript
|
|
71
69
|
import { enableWindowDrag } from 'electron-drag-plus/renderer';
|
|
72
70
|
|
|
73
71
|
// 使用 CSS 选择器
|
|
74
|
-
enableWindowDrag('#title-bar'
|
|
72
|
+
enableWindowDrag('#title-bar');
|
|
75
73
|
|
|
76
74
|
// 或使用 DOM 元素
|
|
77
75
|
const titleBar = document.getElementById('title-bar');
|
|
78
76
|
if (titleBar instanceof HTMLElement) {
|
|
79
|
-
enableWindowDrag(titleBar
|
|
77
|
+
enableWindowDrag(titleBar);
|
|
80
78
|
}
|
|
81
79
|
```
|
|
82
80
|
|
|
@@ -119,8 +117,8 @@ function createWindow() {
|
|
|
119
117
|
}
|
|
120
118
|
});
|
|
121
119
|
|
|
122
|
-
// 初始化拖拽功能
|
|
123
|
-
initializeDragPlus(mainWindow
|
|
120
|
+
// 初始化拖拽功能 - 简化版,无需key参数
|
|
121
|
+
initializeDragPlus(mainWindow);
|
|
124
122
|
|
|
125
123
|
// 加载页面
|
|
126
124
|
mainWindow.loadFile('index.html');
|
|
@@ -155,10 +153,10 @@ contextBridge.exposeInMainWorld('electron', {
|
|
|
155
153
|
```javascript
|
|
156
154
|
import { enableWindowDrag, disableWindowDrag } from 'electron-drag-plus/renderer';
|
|
157
155
|
|
|
158
|
-
// 启用标题栏拖拽功能
|
|
156
|
+
// 启用标题栏拖拽功能 - 简化版,无需key参数
|
|
159
157
|
const titleBar = document.getElementById('title-bar');
|
|
160
158
|
if (titleBar) {
|
|
161
|
-
enableWindowDrag(titleBar
|
|
159
|
+
enableWindowDrag(titleBar);
|
|
162
160
|
}
|
|
163
161
|
|
|
164
162
|
// 禁用控制按钮的拖拽
|
|
@@ -274,9 +272,9 @@ closeButton?.addEventListener('click', () => {
|
|
|
274
272
|
|
|
275
273
|
3. **元素识别**:库会自动识别并排除常见的交互元素(如按钮、输入框等),避免交互冲突
|
|
276
274
|
|
|
277
|
-
4.
|
|
275
|
+
4. **错误处理**:库内置了基本的错误处理和日志警告,便于调试
|
|
278
276
|
|
|
279
|
-
5.
|
|
277
|
+
5. **DOM 就绪**:库会自动检测 DOM 就绪状态,确保在合适的时机初始化
|
|
280
278
|
|
|
281
279
|
## 兼容性
|
|
282
280
|
|
package/dist/example.js
CHANGED
|
@@ -16,19 +16,19 @@ const win = new electron_1.BrowserWindow({
|
|
|
16
16
|
contextIsolation: false
|
|
17
17
|
}
|
|
18
18
|
});
|
|
19
|
-
//
|
|
20
|
-
(0, index_1.initializeDragPlus)(win
|
|
19
|
+
// 初始化拖拽功能
|
|
20
|
+
(0, index_1.initializeDragPlus)(win);
|
|
21
21
|
// 加载页面
|
|
22
22
|
win.loadFile('index.html');
|
|
23
23
|
// 渲染进程使用示例(renderer.js 或 renderer.ts)
|
|
24
24
|
const renderer_1 = require("./renderer");
|
|
25
|
-
//
|
|
25
|
+
// 为标题栏启用拖拽功能
|
|
26
26
|
// 方法1:使用CSS选择器
|
|
27
|
-
(0, renderer_1.enableWindowDrag)('#title-bar'
|
|
27
|
+
(0, renderer_1.enableWindowDrag)('#title-bar');
|
|
28
28
|
// 方法2:使用DOM元素
|
|
29
29
|
const titleBar = document.getElementById('title-bar');
|
|
30
30
|
if (titleBar instanceof HTMLElement) {
|
|
31
|
-
(0, renderer_1.enableWindowDrag)(titleBar
|
|
31
|
+
(0, renderer_1.enableWindowDrag)(titleBar);
|
|
32
32
|
}
|
|
33
33
|
// 禁用特定元素的拖拽
|
|
34
34
|
const renderer_2 = require("./renderer");
|
package/dist/index.d.ts
CHANGED
|
@@ -2,10 +2,9 @@ import { BrowserWindow } from 'electron';
|
|
|
2
2
|
/**
|
|
3
3
|
* 主进程初始化拖拽功能
|
|
4
4
|
* @param win Electron窗口实例
|
|
5
|
-
* @param key 唯一标识,用于关联主进程和渲染进程
|
|
6
5
|
*/
|
|
7
|
-
export declare function initializeDragPlus(win: BrowserWindow
|
|
6
|
+
export declare function initializeDragPlus(win: BrowserWindow): void;
|
|
8
7
|
declare const _default: {
|
|
9
|
-
|
|
8
|
+
initializeDragPlus: typeof initializeDragPlus;
|
|
10
9
|
};
|
|
11
10
|
export default _default;
|
package/dist/index.js
CHANGED
|
@@ -2,37 +2,33 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.initializeDragPlus = initializeDragPlus;
|
|
4
4
|
const electron_1 = require("electron");
|
|
5
|
-
//
|
|
6
|
-
|
|
5
|
+
// 当前活跃的窗口实例(不再需要Map存储)
|
|
6
|
+
let activeWindowInstance = null;
|
|
7
7
|
/**
|
|
8
8
|
* 主进程初始化拖拽功能
|
|
9
9
|
* @param win Electron窗口实例
|
|
10
|
-
* @param key 唯一标识,用于关联主进程和渲染进程
|
|
11
10
|
*/
|
|
12
|
-
function initializeDragPlus(win
|
|
13
|
-
if (!win
|
|
14
|
-
throw new Error('
|
|
11
|
+
function initializeDragPlus(win) {
|
|
12
|
+
if (!win) {
|
|
13
|
+
throw new Error('窗口实例不能为空');
|
|
15
14
|
}
|
|
16
15
|
const webContents = win.webContents;
|
|
17
|
-
//
|
|
18
|
-
|
|
16
|
+
// 设置为当前活跃窗口实例
|
|
17
|
+
activeWindowInstance = { window: win, webContents };
|
|
19
18
|
// 清理函数
|
|
20
19
|
const cleanup = () => {
|
|
21
|
-
|
|
20
|
+
if (activeWindowInstance?.window === win) {
|
|
21
|
+
activeWindowInstance = null;
|
|
22
|
+
}
|
|
22
23
|
};
|
|
23
24
|
// 监听窗口关闭事件进行清理
|
|
24
25
|
win.on('closed', cleanup);
|
|
25
|
-
// 发送key到渲染进程
|
|
26
|
-
webContents.on('did-finish-load', () => {
|
|
27
|
-
webContents.send('drag-plus:init', { key });
|
|
28
|
-
});
|
|
29
26
|
}
|
|
30
27
|
// 处理拖拽事件
|
|
31
|
-
electron_1.ipcMain.on('drag-plus:start', (event, {
|
|
32
|
-
|
|
33
|
-
if (!instance)
|
|
28
|
+
electron_1.ipcMain.on('drag-plus:start', (event, { x, y }) => {
|
|
29
|
+
if (!activeWindowInstance)
|
|
34
30
|
return;
|
|
35
|
-
const { window } =
|
|
31
|
+
const { window } = activeWindowInstance;
|
|
36
32
|
const data = {
|
|
37
33
|
isDragging: true,
|
|
38
34
|
startX: x,
|
|
@@ -40,13 +36,12 @@ electron_1.ipcMain.on('drag-plus:start', (event, { key, x, y }) => {
|
|
|
40
36
|
originalResizable: window.isResizable()
|
|
41
37
|
};
|
|
42
38
|
// 关联拖拽状态到窗口实例
|
|
43
|
-
|
|
39
|
+
activeWindowInstance.dragData = data;
|
|
44
40
|
});
|
|
45
|
-
electron_1.ipcMain.on('drag-plus:drag', (event, {
|
|
46
|
-
|
|
47
|
-
if (!instance || !instance.dragData?.isDragging)
|
|
41
|
+
electron_1.ipcMain.on('drag-plus:drag', (event, { x, y }) => {
|
|
42
|
+
if (!activeWindowInstance || !activeWindowInstance.dragData?.isDragging)
|
|
48
43
|
return;
|
|
49
|
-
const { window, dragData } =
|
|
44
|
+
const { window, dragData } = activeWindowInstance;
|
|
50
45
|
const winBounds = window.getBounds();
|
|
51
46
|
// 计算新位置
|
|
52
47
|
const newX = winBounds.x + (x - dragData.startX);
|
|
@@ -64,20 +59,19 @@ electron_1.ipcMain.on('drag-plus:drag', (event, { key, x, y }) => {
|
|
|
64
59
|
dragData.startX = x;
|
|
65
60
|
dragData.startY = y;
|
|
66
61
|
});
|
|
67
|
-
electron_1.ipcMain.on('drag-plus:stop', (
|
|
68
|
-
|
|
69
|
-
if (!instance || !instance.dragData)
|
|
62
|
+
electron_1.ipcMain.on('drag-plus:stop', () => {
|
|
63
|
+
if (!activeWindowInstance || !activeWindowInstance.dragData)
|
|
70
64
|
return;
|
|
71
|
-
const { window, dragData } =
|
|
65
|
+
const { window, dragData } = activeWindowInstance;
|
|
72
66
|
// 恢复原始的可缩放状态
|
|
73
67
|
if (dragData.originalResizable !== undefined) {
|
|
74
68
|
window.setResizable(dragData.originalResizable);
|
|
75
69
|
}
|
|
76
70
|
// 清理拖拽数据
|
|
77
|
-
delete
|
|
71
|
+
delete activeWindowInstance.dragData;
|
|
78
72
|
});
|
|
79
73
|
// 确保Map类型正确
|
|
80
74
|
// 默认导出
|
|
81
75
|
exports.default = {
|
|
82
|
-
|
|
76
|
+
initializeDragPlus
|
|
83
77
|
};
|
package/dist/renderer.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 渲染进程启用拖拽功能
|
|
3
3
|
* @param element 元素或元素选择器
|
|
4
|
-
* @param key 窗口标识,用于与主进程通信
|
|
5
4
|
*/
|
|
6
|
-
export declare function enableWindowDrag(element: HTMLElement | string
|
|
5
|
+
export declare function enableWindowDrag(element: HTMLElement | string): void;
|
|
7
6
|
/**
|
|
8
7
|
* 渲染进程禁用拖拽功能
|
|
9
8
|
* @param element 元素或元素选择器
|
package/dist/renderer.js
CHANGED
|
@@ -3,34 +3,60 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.enableWindowDrag = enableWindowDrag;
|
|
4
4
|
exports.disableWindowDrag = disableWindowDrag;
|
|
5
5
|
const electron_1 = require("electron");
|
|
6
|
-
// 存储当前窗口的key
|
|
7
|
-
let currentWindowKey = null;
|
|
8
6
|
// 拖拽状态
|
|
9
7
|
let isDragging = false;
|
|
10
8
|
let dragStartX = 0;
|
|
11
9
|
let dragStartY = 0;
|
|
10
|
+
// 初始化标志
|
|
11
|
+
let isInitialized = false;
|
|
12
12
|
// 初始化
|
|
13
13
|
function initDragSystem() {
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
14
|
+
// 如果已经初始化,不再重复初始化
|
|
15
|
+
if (isInitialized)
|
|
16
|
+
return;
|
|
17
|
+
try {
|
|
18
|
+
// 设置全局事件监听器 - 移除key相关的逻辑
|
|
19
|
+
document.addEventListener('mousedown', handleMouseDown, true);
|
|
20
|
+
document.addEventListener('mousemove', handleMouseMove, true);
|
|
21
|
+
document.addEventListener('mouseup', handleMouseUp, true);
|
|
22
|
+
isInitialized = true;
|
|
23
|
+
// 清理函数
|
|
24
|
+
return () => {
|
|
25
|
+
document.removeEventListener('mousedown', handleMouseDown, true);
|
|
26
|
+
document.removeEventListener('mousemove', handleMouseMove, true);
|
|
27
|
+
document.removeEventListener('mouseup', handleMouseUp, true);
|
|
28
|
+
isInitialized = false;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error('初始化拖拽系统失败:', error);
|
|
33
|
+
// 返回一个空的清理函数,避免在调用时出错
|
|
34
|
+
return () => { };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
// 延迟初始化,避免在DOM未准备好时初始化
|
|
38
|
+
function delayedInit() {
|
|
39
|
+
if (document.readyState === 'loading') {
|
|
40
|
+
document.addEventListener('DOMContentLoaded', () => {
|
|
41
|
+
// 保存返回的清理函数
|
|
42
|
+
const cleanup = initDragSystem();
|
|
43
|
+
if (cleanup) {
|
|
44
|
+
cleanupFunction = cleanup;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// 保存返回的清理函数
|
|
50
|
+
const cleanup = initDragSystem();
|
|
51
|
+
if (cleanup) {
|
|
52
|
+
cleanupFunction = cleanup;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
29
55
|
}
|
|
30
|
-
//
|
|
31
|
-
let cleanupFunction =
|
|
56
|
+
// 自动初始化,但添加错误处理和延迟
|
|
57
|
+
let cleanupFunction = () => { };
|
|
32
58
|
try {
|
|
33
|
-
|
|
59
|
+
delayedInit();
|
|
34
60
|
}
|
|
35
61
|
catch (error) {
|
|
36
62
|
console.warn('初始化拖拽系统失败:', error);
|
|
@@ -43,44 +69,81 @@ window.addEventListener('beforeunload', () => {
|
|
|
43
69
|
});
|
|
44
70
|
// 鼠标按下事件处理器
|
|
45
71
|
function handleMouseDown(e) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
72
|
+
try {
|
|
73
|
+
const target = e.target;
|
|
74
|
+
if (!target)
|
|
75
|
+
return;
|
|
76
|
+
// 检查元素是否是可拖拽区域
|
|
77
|
+
if (target.style.appRegion === 'drag' && !isElementOrParentNoDrag(target)) {
|
|
78
|
+
isDragging = true;
|
|
79
|
+
dragStartX = e.screenX;
|
|
80
|
+
dragStartY = e.screenY;
|
|
81
|
+
// 通知主进程开始拖拽,移除key参数
|
|
82
|
+
try {
|
|
83
|
+
electron_1.ipcRenderer.send('drag-plus:start', {
|
|
84
|
+
x: e.screenX,
|
|
85
|
+
y: e.screenY
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
console.warn('发送拖拽开始事件失败:', error);
|
|
90
|
+
// 重置拖拽状态
|
|
91
|
+
isDragging = false;
|
|
92
|
+
}
|
|
59
93
|
}
|
|
60
94
|
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
console.error('处理鼠标按下事件失败:', error);
|
|
97
|
+
isDragging = false;
|
|
98
|
+
}
|
|
61
99
|
}
|
|
62
100
|
// 鼠标移动事件处理器
|
|
63
101
|
function handleMouseMove(e) {
|
|
64
|
-
|
|
102
|
+
// 确保我们在拖拽状态
|
|
103
|
+
if (!isDragging)
|
|
104
|
+
return;
|
|
105
|
+
try {
|
|
65
106
|
// 计算鼠标移动距离
|
|
66
107
|
const deltaX = e.screenX - dragStartX;
|
|
67
108
|
const deltaY = e.screenY - dragStartY;
|
|
68
109
|
// 只有在移动超过一定阈值时才发送拖拽事件,减少抖动
|
|
69
110
|
if (Math.abs(deltaX) > 2 || Math.abs(deltaY) > 2) {
|
|
70
|
-
|
|
71
|
-
key
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
111
|
+
try {
|
|
112
|
+
// 移除key参数
|
|
113
|
+
electron_1.ipcRenderer.send('drag-plus:drag', {
|
|
114
|
+
x: e.screenX,
|
|
115
|
+
y: e.screenY
|
|
116
|
+
});
|
|
117
|
+
// 更新拖拽起始位置
|
|
118
|
+
dragStartX = e.screenX;
|
|
119
|
+
dragStartY = e.screenY;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
console.warn('发送拖拽移动事件失败:', error);
|
|
123
|
+
// 重置拖拽状态
|
|
124
|
+
isDragging = false;
|
|
125
|
+
}
|
|
75
126
|
}
|
|
76
127
|
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
console.error('处理鼠标移动事件失败:', error);
|
|
130
|
+
isDragging = false;
|
|
131
|
+
}
|
|
77
132
|
}
|
|
78
133
|
// 鼠标释放事件处理器
|
|
79
134
|
function handleMouseUp() {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
135
|
+
// 确保我们在拖拽状态
|
|
136
|
+
if (!isDragging) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
// 先重置拖拽状态,避免事件循环
|
|
140
|
+
isDragging = false;
|
|
141
|
+
// 通知主进程结束拖拽,移除key参数
|
|
142
|
+
try {
|
|
143
|
+
electron_1.ipcRenderer.send('drag-plus:stop');
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
console.warn('发送拖拽停止事件失败:', error);
|
|
84
147
|
}
|
|
85
148
|
}
|
|
86
149
|
// 检查元素或其父元素是否有no-drag样式
|
|
@@ -100,36 +163,65 @@ function isElementOrParentNoDrag(element) {
|
|
|
100
163
|
/**
|
|
101
164
|
* 渲染进程启用拖拽功能
|
|
102
165
|
* @param element 元素或元素选择器
|
|
103
|
-
* @param key 窗口标识,用于与主进程通信
|
|
104
166
|
*/
|
|
105
|
-
function enableWindowDrag(element
|
|
106
|
-
// 如果提供了key,使用它更新当前窗口的key
|
|
107
|
-
if (key) {
|
|
108
|
-
currentWindowKey = key;
|
|
109
|
-
}
|
|
110
|
-
// 获取目标元素
|
|
111
|
-
const target = typeof element === 'string'
|
|
112
|
-
? document.querySelector(element)
|
|
113
|
-
: element;
|
|
114
|
-
if (!target || !(target instanceof HTMLElement)) {
|
|
115
|
-
console.warn('元素不存在或不是有效的HTMLElement');
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
// 设置为可拖拽区域
|
|
119
|
-
target.style.appRegion = 'drag';
|
|
120
|
-
// 自动为子交互元素设置no-drag
|
|
167
|
+
function enableWindowDrag(element) {
|
|
121
168
|
try {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
169
|
+
// 确保在DOM环境中执行
|
|
170
|
+
if (typeof document === 'undefined' || !document.querySelector) {
|
|
171
|
+
console.warn('无法在当前环境中执行此方法: 缺少DOM支持');
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
// 获取目标元素
|
|
175
|
+
const target = typeof element === 'string'
|
|
176
|
+
? document.querySelector(element)
|
|
177
|
+
: element;
|
|
178
|
+
if (!target || !(target instanceof HTMLElement)) {
|
|
179
|
+
console.warn('元素不存在或不是有效的HTMLElement');
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
// 安全地设置拖拽样式
|
|
183
|
+
try {
|
|
184
|
+
// 先设置CSS样式,这应该不会影响窗口显示
|
|
185
|
+
target.style.userSelect = 'none';
|
|
186
|
+
target.style.webkitAppRegion = 'drag';
|
|
187
|
+
target.style.appRegion = 'drag';
|
|
188
|
+
}
|
|
189
|
+
catch (styleError) {
|
|
190
|
+
console.error('设置元素样式失败:', styleError);
|
|
191
|
+
// 继续执行,不要中断函数
|
|
192
|
+
}
|
|
193
|
+
// 自动为子交互元素设置no-drag,但不要因为这个过程的异常而中断
|
|
194
|
+
try {
|
|
195
|
+
const interactiveElements = target.querySelectorAll('button, input, select, textarea, a, [onclick], [href], [tabindex]:not([tabindex="-1"])');
|
|
196
|
+
// 分批处理,避免大量元素导致性能问题
|
|
197
|
+
const batchSize = 50;
|
|
198
|
+
for (let i = 0; i < interactiveElements.length; i += batchSize) {
|
|
199
|
+
const batch = Array.from(interactiveElements).slice(i, i + batchSize);
|
|
200
|
+
// 使用setTimeout避免阻塞主线程
|
|
201
|
+
setTimeout(() => {
|
|
202
|
+
batch.forEach(child => {
|
|
203
|
+
try {
|
|
204
|
+
if (child instanceof HTMLElement && !child.classList.contains('ignore-no-drag')) {
|
|
205
|
+
child.style.userSelect = 'none';
|
|
206
|
+
child.style.webkitAppRegion = 'no-drag';
|
|
207
|
+
child.style.appRegion = 'no-drag';
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch (itemError) {
|
|
211
|
+
// 忽略单个元素的错误,继续处理其他元素
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}, 0);
|
|
128
215
|
}
|
|
129
|
-
}
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
console.warn('设置子交互元素失败:', error);
|
|
219
|
+
// 不中断主函数
|
|
220
|
+
}
|
|
130
221
|
}
|
|
131
222
|
catch (error) {
|
|
132
|
-
console.
|
|
223
|
+
console.error('启用窗口拖拽失败:', error);
|
|
224
|
+
// 最外层捕获,确保即使发生严重错误也不会影响窗口显示
|
|
133
225
|
}
|
|
134
226
|
}
|
|
135
227
|
/**
|
|
@@ -137,14 +229,30 @@ function enableWindowDrag(element, key) {
|
|
|
137
229
|
* @param element 元素或元素选择器
|
|
138
230
|
*/
|
|
139
231
|
function disableWindowDrag(element) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
232
|
+
try {
|
|
233
|
+
// 确保在DOM环境中执行
|
|
234
|
+
if (typeof document === 'undefined' || !document.querySelector) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const target = typeof element === 'string'
|
|
238
|
+
? document.querySelector(element)
|
|
239
|
+
: element;
|
|
240
|
+
if (!target || !(target instanceof HTMLElement)) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
// 安全地设置为不可拖拽区域
|
|
244
|
+
try {
|
|
245
|
+
target.style.webkitAppRegion = 'no-drag';
|
|
246
|
+
target.style.appRegion = 'no-drag';
|
|
247
|
+
}
|
|
248
|
+
catch (styleError) {
|
|
249
|
+
console.error('设置元素不可拖拽样式失败:', styleError);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
console.error('禁用窗口拖拽失败:', error);
|
|
254
|
+
// 最外层捕获,确保即使发生严重错误也不会影响窗口显示
|
|
145
255
|
}
|
|
146
|
-
// 设置为不可拖拽区域
|
|
147
|
-
target.style.appRegion = 'no-drag';
|
|
148
256
|
}
|
|
149
257
|
// 默认导出
|
|
150
258
|
exports.default = {
|