-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
80 lines (68 loc) · 2.58 KB
/
Copy pathfunctions.js
File metadata and controls
80 lines (68 loc) · 2.58 KB
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
77
78
79
80
function applyStyles(element, styles) {
Object.assign(element.style, styles);
}
function savePosition(id, x, y) {
localStorage.setItem(id + '_pos', JSON.stringify({ x, y }));
}
function loadPosition(id) {
const pos = localStorage.getItem(id + '_pos');
return pos ? JSON.parse(pos) : null;
}
function adjustPosition(element) {
const rect = element.getBoundingClientRect();
const containerWidth = element.offsetWidth;
const containerHeight = element.offsetHeight;
if (rect.left < 0) {
applyStyles(element, { left: '0px' });
}
if (rect.top < 0) {
applyStyles(element, { top: '0px' });
}
if (rect.right > window.innerWidth) {
applyStyles(element, { left: `${window.innerWidth - containerWidth}px` });
}
if (rect.bottom > window.innerHeight) {
applyStyles(element, { top: `${window.innerHeight - containerHeight}px` });
}
}
function makeDraggable(element) {
let isDragging = false;
let startX, startY, initialX, initialY;
function isElementTextArea(e) {
console.log(e);
const elementAtCursor = document.elementFromPoint(e.clientX, e.clientY);
console.log(elementAtCursor);
return elementAtCursor.tagName === 'TEXTAREA';
}
element.addEventListener('mousedown', (e) => {
if (isElementTextArea(e)) return;
isDragging = true;
startX = e.clientX;
startY = e.clientY;
initialX = parseInt(element.style.left || 0, 10);
initialY = parseInt(element.style.top || 0, 10);
document.body.style.userSelect = 'none';
applyStyles(element, { transition: 'none' });
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
});
function onMouseMove(e) {
if (!isDragging) return;
const deltaX = e.clientX - startX;
const deltaY = e.clientY - startY;
const newLeft = Math.max(0, Math.min(initialX + deltaX, window.innerWidth - element.offsetWidth));
const newTop = Math.max(0, Math.min(initialY + deltaY, window.innerHeight - element.offsetHeight));
applyStyles(element, {
left: `${newLeft}px`,
top: `${newTop}px`
});
}
function onMouseUp() {
isDragging = false;
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
document.body.style.userSelect = '';
savePosition(element.id, element.style.left, element.style.top);
applyStyles(element, { transition: 'all 0.3s ease-in-out' });
}
}