electron-drag-plus 1.0.1 → 1.0.3
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 +45 -22
- package/dist/index.d.ts +2 -6
- package/dist/index.js +48 -59
- package/dist/renderer.d.ts +8 -9
- package/dist/renderer.js +82 -89
- package/package.json +3 -2
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
|
@@ -2,38 +2,61 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* electron-drag-plus 简化版API使用示例
|
|
4
4
|
*/
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
5
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
9
|
// 主进程使用示例(main.js 或 main.ts)
|
|
7
10
|
const electron_1 = require("electron");
|
|
8
|
-
const index_1 = require("./index");
|
|
11
|
+
const index_1 = __importDefault(require("./index"));
|
|
9
12
|
// 创建窗口
|
|
10
13
|
const win = new electron_1.BrowserWindow({
|
|
11
14
|
width: 800,
|
|
12
15
|
height: 600,
|
|
13
|
-
frame: false, //
|
|
16
|
+
frame: false, // 无边框窗口通常需要自定义拖拽
|
|
14
17
|
webPreferences: {
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
contextIsolation: false, // 确保能访问ipcRenderer
|
|
19
|
+
nodeIntegration: true
|
|
17
20
|
}
|
|
18
21
|
});
|
|
19
|
-
//
|
|
20
|
-
(0, index_1.
|
|
22
|
+
// 初始化拖拽功能 - 只需传递窗口实例
|
|
23
|
+
(0, index_1.default)(win);
|
|
21
24
|
// 加载页面
|
|
22
25
|
win.loadFile('index.html');
|
|
23
26
|
// 渲染进程使用示例(renderer.js 或 renderer.ts)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
//
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
// 在渲染进程文件中:
|
|
28
|
+
/*
|
|
29
|
+
import { enableWindowDrag, disableWindowDrag } from './renderer';
|
|
30
|
+
|
|
31
|
+
// 在DOM加载完成后调用
|
|
32
|
+
window.addEventListener('DOMContentLoaded', () => {
|
|
33
|
+
// 方式1:使用CSS选择器启用拖拽区域
|
|
34
|
+
enableWindowDrag('#title-bar');
|
|
35
|
+
|
|
36
|
+
// 方式2:直接传递DOM元素
|
|
37
|
+
const titleBar = document.getElementById('title-bar');
|
|
38
|
+
if (titleBar) {
|
|
39
|
+
enableWindowDrag(titleBar);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 为按钮禁用拖拽,确保可以正常点击
|
|
43
|
+
const minimizeButton = document.getElementById('minimize-button');
|
|
44
|
+
const maximizeButton = document.getElementById('maximize-button');
|
|
45
|
+
const closeButton = document.getElementById('close-button');
|
|
46
|
+
|
|
47
|
+
if (minimizeButton) disableWindowDrag(minimizeButton);
|
|
48
|
+
if (maximizeButton) disableWindowDrag(maximizeButton);
|
|
49
|
+
if (closeButton) disableWindowDrag(closeButton);
|
|
50
|
+
});
|
|
51
|
+
*/
|
|
52
|
+
// HTML示例
|
|
53
|
+
/*
|
|
54
|
+
<div id="title-bar" style="height: 30px; background: #333; color: white; display: flex; justify-content: space-between; align-items: center; padding: 0 10px;">
|
|
55
|
+
<div>窗口标题</div>
|
|
56
|
+
<div>
|
|
57
|
+
<button id="minimize-button">-</button>
|
|
58
|
+
<button id="maximize-button">□</button>
|
|
59
|
+
<button id="close-button">×</button>
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -2,10 +2,6 @@ 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
|
|
8
|
-
|
|
9
|
-
initialize: typeof initializeDragPlus;
|
|
10
|
-
};
|
|
11
|
-
export default _default;
|
|
6
|
+
export declare function initializeDragPlus(win: BrowserWindow): void;
|
|
7
|
+
export default initializeDragPlus;
|
package/dist/index.js
CHANGED
|
@@ -2,82 +2,71 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.initializeDragPlus = initializeDragPlus;
|
|
4
4
|
const electron_1 = require("electron");
|
|
5
|
-
//
|
|
6
|
-
|
|
5
|
+
// 存储当前拖拽的窗口实例
|
|
6
|
+
let currentWindow = null;
|
|
7
|
+
let dragData = null;
|
|
7
8
|
/**
|
|
8
9
|
* 主进程初始化拖拽功能
|
|
9
10
|
* @param win Electron窗口实例
|
|
10
|
-
* @param key 唯一标识,用于关联主进程和渲染进程
|
|
11
11
|
*/
|
|
12
|
-
function initializeDragPlus(win
|
|
13
|
-
if (!win
|
|
14
|
-
throw new Error('
|
|
12
|
+
function initializeDragPlus(win) {
|
|
13
|
+
if (!win) {
|
|
14
|
+
throw new Error('窗口实例不能为空');
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
};
|
|
23
|
-
// 监听窗口关闭事件进行清理
|
|
24
|
-
win.on('closed', cleanup);
|
|
25
|
-
// 发送key到渲染进程
|
|
26
|
-
webContents.on('did-finish-load', () => {
|
|
27
|
-
webContents.send('drag-plus:init', { key });
|
|
16
|
+
// 监听窗口关闭事件清理资源
|
|
17
|
+
win.on('closed', () => {
|
|
18
|
+
if (currentWindow === win) {
|
|
19
|
+
currentWindow = null;
|
|
20
|
+
dragData = null;
|
|
21
|
+
}
|
|
28
22
|
});
|
|
29
23
|
}
|
|
30
|
-
//
|
|
31
|
-
electron_1.ipcMain.on('drag
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
// 监听拖拽开始事件
|
|
25
|
+
electron_1.ipcMain.on('electron-drag:start', (event, { x, y }) => {
|
|
26
|
+
// 获取触发事件的窗口
|
|
27
|
+
const win = electron_1.BrowserWindow.fromWebContents(event.sender);
|
|
28
|
+
if (!win)
|
|
34
29
|
return;
|
|
35
|
-
|
|
36
|
-
|
|
30
|
+
// 设置当前窗口和拖拽数据
|
|
31
|
+
currentWindow = win;
|
|
32
|
+
dragData = {
|
|
37
33
|
isDragging: true,
|
|
38
34
|
startX: x,
|
|
39
35
|
startY: y,
|
|
40
|
-
originalResizable:
|
|
36
|
+
originalResizable: win.isResizable()
|
|
41
37
|
};
|
|
42
|
-
//
|
|
43
|
-
|
|
38
|
+
// 临时禁用窗口大小调整以优化拖拽体验
|
|
39
|
+
win.setResizable(false);
|
|
44
40
|
});
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (!
|
|
41
|
+
// 监听拖拽移动事件
|
|
42
|
+
electron_1.ipcMain.on('electron-drag:drag', (event, { x, y }) => {
|
|
43
|
+
if (!currentWindow || !dragData || !dragData.isDragging)
|
|
48
44
|
return;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
// 更新拖拽起始位置
|
|
64
|
-
dragData.startX = x;
|
|
65
|
-
dragData.startY = y;
|
|
45
|
+
try {
|
|
46
|
+
const winBounds = currentWindow.getBounds();
|
|
47
|
+
// 计算新位置
|
|
48
|
+
const newX = winBounds.x + (x - dragData.startX);
|
|
49
|
+
const newY = winBounds.y + (y - dragData.startY);
|
|
50
|
+
// 设置窗口位置
|
|
51
|
+
currentWindow.setPosition(newX, newY, false);
|
|
52
|
+
// 更新拖拽起始位置
|
|
53
|
+
dragData.startX = x;
|
|
54
|
+
dragData.startY = y;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.error('拖拽移动出错:', error);
|
|
58
|
+
}
|
|
66
59
|
});
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (!
|
|
60
|
+
// 监听拖拽结束事件
|
|
61
|
+
electron_1.ipcMain.on('electron-drag:stop', () => {
|
|
62
|
+
if (!currentWindow || !dragData)
|
|
70
63
|
return;
|
|
71
|
-
|
|
72
|
-
// 恢复原始的可缩放状态
|
|
64
|
+
// 恢复窗口原始大小调整状态
|
|
73
65
|
if (dragData.originalResizable !== undefined) {
|
|
74
|
-
|
|
66
|
+
currentWindow.setResizable(dragData.originalResizable);
|
|
75
67
|
}
|
|
76
|
-
//
|
|
77
|
-
|
|
68
|
+
// 清理拖拽状态
|
|
69
|
+
dragData = null;
|
|
78
70
|
});
|
|
79
|
-
// 确保Map类型正确
|
|
80
71
|
// 默认导出
|
|
81
|
-
exports.default =
|
|
82
|
-
initialize: initializeDragPlus
|
|
83
|
-
};
|
|
72
|
+
exports.default = initializeDragPlus;
|
package/dist/renderer.d.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param
|
|
4
|
-
* @param key 窗口标识,用于与主进程通信
|
|
2
|
+
* 为DOM元素启用窗口拖拽功能
|
|
3
|
+
* @param selector CSS选择器或HTMLElement对象
|
|
5
4
|
*/
|
|
6
|
-
export declare function enableWindowDrag(
|
|
5
|
+
export declare function enableWindowDrag(selector: string | HTMLElement): void;
|
|
7
6
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param
|
|
7
|
+
* 禁用DOM元素的窗口拖拽功能
|
|
8
|
+
* @param selector CSS选择器或HTMLElement对象
|
|
10
9
|
*/
|
|
11
|
-
export declare function disableWindowDrag(
|
|
10
|
+
export declare function disableWindowDrag(selector: string | HTMLElement): void;
|
|
12
11
|
declare const _default: {
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
enableWindowDrag: typeof enableWindowDrag;
|
|
13
|
+
disableWindowDrag: typeof disableWindowDrag;
|
|
15
14
|
};
|
|
16
15
|
export default _default;
|
package/dist/renderer.js
CHANGED
|
@@ -3,19 +3,13 @@ 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;
|
|
12
|
-
//
|
|
13
|
-
function
|
|
14
|
-
//
|
|
15
|
-
electron_1.ipcRenderer.on('drag-plus:init', (event, { key }) => {
|
|
16
|
-
currentWindowKey = key;
|
|
17
|
-
});
|
|
18
|
-
// 设置全局事件监听器
|
|
10
|
+
// 初始化拖拽事件系统
|
|
11
|
+
function initDragEvents() {
|
|
12
|
+
// 设置全局鼠标事件监听器
|
|
19
13
|
document.addEventListener('mousedown', handleMouseDown, true);
|
|
20
14
|
document.addEventListener('mousemove', handleMouseMove, true);
|
|
21
15
|
document.addEventListener('mouseup', handleMouseUp, true);
|
|
@@ -24,70 +18,70 @@ function initDragSystem() {
|
|
|
24
18
|
document.removeEventListener('mousedown', handleMouseDown, true);
|
|
25
19
|
document.removeEventListener('mousemove', handleMouseMove, true);
|
|
26
20
|
document.removeEventListener('mouseup', handleMouseUp, true);
|
|
27
|
-
electron_1.ipcRenderer.removeAllListeners('drag-plus:init');
|
|
28
21
|
};
|
|
29
22
|
}
|
|
30
|
-
//
|
|
23
|
+
// 初始化系统
|
|
31
24
|
let cleanupFunction = null;
|
|
32
|
-
|
|
33
|
-
cleanupFunction =
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
25
|
+
if (typeof document !== 'undefined') {
|
|
26
|
+
cleanupFunction = initDragEvents();
|
|
27
|
+
// 页面卸载时清理
|
|
28
|
+
window.addEventListener('beforeunload', () => {
|
|
29
|
+
if (cleanupFunction) {
|
|
30
|
+
cleanupFunction();
|
|
31
|
+
}
|
|
32
|
+
});
|
|
37
33
|
}
|
|
38
|
-
// 页面卸载时清理
|
|
39
|
-
window.addEventListener('beforeunload', () => {
|
|
40
|
-
if (cleanupFunction) {
|
|
41
|
-
cleanupFunction();
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
34
|
// 鼠标按下事件处理器
|
|
45
35
|
function handleMouseDown(e) {
|
|
46
36
|
const target = e.target;
|
|
47
|
-
|
|
48
|
-
|
|
37
|
+
if (!target)
|
|
38
|
+
return;
|
|
39
|
+
// 检查元素是否是可拖拽区域且不是no-drag区域
|
|
40
|
+
if (target.style.appRegion === 'drag' && !isElementNoDrag(target)) {
|
|
49
41
|
isDragging = true;
|
|
50
42
|
dragStartX = e.screenX;
|
|
51
43
|
dragStartY = e.screenY;
|
|
52
44
|
// 通知主进程开始拖拽
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
y: e.screenY
|
|
58
|
-
});
|
|
59
|
-
}
|
|
45
|
+
electron_1.ipcRenderer.send('electron-drag:start', {
|
|
46
|
+
x: e.screenX,
|
|
47
|
+
y: e.screenY
|
|
48
|
+
});
|
|
60
49
|
}
|
|
61
50
|
}
|
|
62
51
|
// 鼠标移动事件处理器
|
|
63
52
|
function handleMouseMove(e) {
|
|
64
|
-
if (isDragging
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
53
|
+
if (!isDragging)
|
|
54
|
+
return;
|
|
55
|
+
// 计算鼠标移动距离
|
|
56
|
+
const deltaX = e.screenX - dragStartX;
|
|
57
|
+
const deltaY = e.screenY - dragStartY;
|
|
58
|
+
// 只有在移动超过一定阈值时才发送拖拽事件,减少抖动
|
|
59
|
+
if (Math.abs(deltaX) > 1 || Math.abs(deltaY) > 1) {
|
|
60
|
+
// 通知主进程进行拖拽
|
|
61
|
+
electron_1.ipcRenderer.send('electron-drag:drag', {
|
|
62
|
+
x: e.screenX,
|
|
63
|
+
y: e.screenY
|
|
64
|
+
});
|
|
65
|
+
// 更新拖拽起始位置
|
|
66
|
+
dragStartX = e.screenX;
|
|
67
|
+
dragStartY = e.screenY;
|
|
76
68
|
}
|
|
77
69
|
}
|
|
78
70
|
// 鼠标释放事件处理器
|
|
79
71
|
function handleMouseUp() {
|
|
80
|
-
if (isDragging
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
72
|
+
if (!isDragging)
|
|
73
|
+
return;
|
|
74
|
+
// 重置拖拽状态
|
|
75
|
+
isDragging = false;
|
|
76
|
+
// 通知主进程结束拖拽
|
|
77
|
+
electron_1.ipcRenderer.send('electron-drag:stop');
|
|
85
78
|
}
|
|
86
|
-
//
|
|
87
|
-
function
|
|
79
|
+
// 检查元素是否是不可拖拽区域
|
|
80
|
+
function isElementNoDrag(element) {
|
|
88
81
|
if (element.style.appRegion === 'no-drag') {
|
|
89
82
|
return true;
|
|
90
83
|
}
|
|
84
|
+
// 检查父元素链
|
|
91
85
|
let parent = element.parentElement;
|
|
92
86
|
while (parent) {
|
|
93
87
|
if (parent.style.appRegion === 'no-drag') {
|
|
@@ -98,56 +92,55 @@ function isElementOrParentNoDrag(element) {
|
|
|
98
92
|
return false;
|
|
99
93
|
}
|
|
100
94
|
/**
|
|
101
|
-
*
|
|
102
|
-
* @param
|
|
103
|
-
* @param key 窗口标识,用于与主进程通信
|
|
95
|
+
* 为DOM元素启用窗口拖拽功能
|
|
96
|
+
* @param selector CSS选择器或HTMLElement对象
|
|
104
97
|
*/
|
|
105
|
-
function enableWindowDrag(
|
|
106
|
-
//
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
}
|
|
98
|
+
function enableWindowDrag(selector) {
|
|
99
|
+
// 确保在DOM环境中执行
|
|
100
|
+
if (typeof document === 'undefined')
|
|
101
|
+
return;
|
|
110
102
|
// 获取目标元素
|
|
111
|
-
const
|
|
112
|
-
? document.querySelector(
|
|
113
|
-
:
|
|
114
|
-
if (!
|
|
115
|
-
console.warn('元素不存在或不是有效的HTMLElement');
|
|
103
|
+
const element = typeof selector === 'string'
|
|
104
|
+
? document.querySelector(selector)
|
|
105
|
+
: selector;
|
|
106
|
+
if (!element || !(element instanceof HTMLElement)) {
|
|
116
107
|
return;
|
|
117
108
|
}
|
|
118
|
-
//
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
catch (error) {
|
|
132
|
-
console.warn('设置交互元素失败:', error);
|
|
133
|
-
}
|
|
109
|
+
// 设置拖拽区域样式
|
|
110
|
+
element.style.userSelect = 'none';
|
|
111
|
+
element.style.webkitAppRegion = 'drag';
|
|
112
|
+
element.style.appRegion = 'drag';
|
|
113
|
+
// 为交互元素设置不可拖拽,确保正常操作
|
|
114
|
+
const interactiveElements = element.querySelectorAll('button, input, select, textarea, a, [onclick], [href], [tabindex]:not([tabindex="-1"])');
|
|
115
|
+
interactiveElements.forEach(child => {
|
|
116
|
+
if (child instanceof HTMLElement) {
|
|
117
|
+
child.style.userSelect = 'none';
|
|
118
|
+
child.style.webkitAppRegion = 'no-drag';
|
|
119
|
+
child.style.appRegion = 'no-drag';
|
|
120
|
+
}
|
|
121
|
+
});
|
|
134
122
|
}
|
|
135
123
|
/**
|
|
136
|
-
*
|
|
137
|
-
* @param
|
|
124
|
+
* 禁用DOM元素的窗口拖拽功能
|
|
125
|
+
* @param selector CSS选择器或HTMLElement对象
|
|
138
126
|
*/
|
|
139
|
-
function disableWindowDrag(
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
127
|
+
function disableWindowDrag(selector) {
|
|
128
|
+
// 确保在DOM环境中执行
|
|
129
|
+
if (typeof document === 'undefined')
|
|
130
|
+
return;
|
|
131
|
+
// 获取目标元素
|
|
132
|
+
const element = typeof selector === 'string'
|
|
133
|
+
? document.querySelector(selector)
|
|
134
|
+
: selector;
|
|
135
|
+
if (!element || !(element instanceof HTMLElement)) {
|
|
144
136
|
return;
|
|
145
137
|
}
|
|
146
138
|
// 设置为不可拖拽区域
|
|
147
|
-
|
|
139
|
+
element.style.webkitAppRegion = 'no-drag';
|
|
140
|
+
element.style.appRegion = 'no-drag';
|
|
148
141
|
}
|
|
149
142
|
// 默认导出
|
|
150
143
|
exports.default = {
|
|
151
|
-
|
|
152
|
-
|
|
144
|
+
enableWindowDrag,
|
|
145
|
+
disableWindowDrag
|
|
153
146
|
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "electron-drag-plus",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "简单高效的Electron窗口拖拽库,支持自定义拖拽区域和交互元素,适用于无边框窗口应用",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
+
"browser": "dist/renderer.js",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"build": "tsc",
|