-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraggrip.js
76 lines (66 loc) · 2.37 KB
/
draggrip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class DragGrip extends HTMLElement {
constructor() {
super();
this.isDragging = false;
this.initialMousePosition = { x: 0, y: 0 };
this.initialParentPosition = { x: 0, y: 0 };
}
connectedCallback() {
this.style.cursor = 'grab';
this.style.display = 'inline-flex';
this.style.backgroundColor = 'skyblue';
this.style.padding = '0.1rem';
this.style.userSelect = 'none';
this.style.position = 'absolute';
this.style.top = '0';
this.style.left = '0';
this.style.width = 'auto';
this.style.height = '1.5rem';
this.textContent = `⁞⁝⁚⁚`
this.style.fontSize = '0.75rem';
// Ensure the parent is positioned absolutely for movement to work
const parent = this.parentElement;
if (parent) {
parent.style.position = 'absolute';
}
// Attach drag event listeners
this.addEventListener('mousedown', this.onMouseDown.bind(this));
document.addEventListener('mousemove', this.onMouseMove.bind(this));
document.addEventListener('mouseup', this.onMouseUp.bind(this));
}
onMouseDown(e) {
this.isDragging = true;
this.initialMousePosition.x = e.clientX;
this.initialMousePosition.y = e.clientY;
this.style.cursor = 'grabbing';
const parent = this.parentElement;
if (parent) {
this.initialParentPosition.x = parent.offsetLeft;
this.initialParentPosition.y = parent.offsetTop;
}
e.preventDefault(); // Prevent text selection
}
onMouseMove(e) {
if (!this.isDragging){
this.removeEventListener('mousemove', this.onMouseMove);
return;
}
const deltaX = e.clientX - this.initialMousePosition.x;
const deltaY = e.clientY - this.initialMousePosition.y;
const newX = this.initialParentPosition.x + deltaX;
const newY = this.initialParentPosition.y + deltaY;
const parent = this.parentElement;
if (parent) {
parent.style.left = `${newX}px`;
parent.style.top = `${newY}px`;
}
}
onMouseUp() {
if (this.isDragging) {
this.isDragging = false;
this.style.cursor = 'grab';
}
}
}
// Define the custom element
customElements.define('drag-grip', DragGrip);