-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (89 loc) · 2.95 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const addTextBox = document.getElementById("addBtn");
const dropZone = document.getElementById("dropZone");
const dropZoneStyle = window.getComputedStyle(dropZone);
const marginTop = parseInt(dropZoneStyle.getPropertyValue("border-top-width"));
const marginBottom = parseInt(
dropZoneStyle.getPropertyValue("border-bottom-width")
);
const marginLeft = parseInt(
dropZoneStyle.getPropertyValue("border-left-width")
);
const marginRight = parseInt(
dropZoneStyle.getPropertyValue("border-right-width")
);
const dropZoneRightLimit =
dropZone.offsetWidth + dropZone.offsetLeft - marginRight;
const dropZoneLeftLimit = dropZone.offsetLeft + marginLeft;
const dropZoneTopLimit = dropZone.offsetTop + marginTop;
const dropZoneBottomLimit =
dropZone.offsetHeight + dropZone.offsetTop - marginBottom;
let startX = 0;
let startY = 0;
let offsetX = 0;
let offsetY = 0;
let dragElement = null;
document.addEventListener("mousedown", onMouseDown);
document.addEventListener("mousemove", onMouseMove);
document.addEventListener("mouseup", onMouseUp);
addTextBox.onclick = () => {
let textBox = document.createElement("div");
let inputTextBox = document.createElement("div");
let deleteButton = document.createElement("div");
textBox.classList.add("drag");
inputTextBox.classList.add("editText");
deleteButton.classList.add("close");
deleteButton.textContent = "x";
deleteButton.addEventListener("click", deleteBox);
dropZone.append(textBox);
textBox.append(inputTextBox);
textBox.append(deleteButton);
makeEditable(inputTextBox);
};
function onMouseDown(e) {
if (e.target.classList.contains("drag")) {
startX = e.pageX;
startY = e.pageY;
dragElement = e.target;
dragElement.style.cursor = "move";
dragElement.firstChild.setAttribute("contenteditable", false);
offsetX = dragElement.offsetLeft;
offsetY = dragElement.offsetTop;
}
}
function onMouseMove(e) {
if (dragElement) {
let newLeft = offsetX + e.pageX - startX;
let newTop = offsetY + e.pageY - startY;
let rightLimit = dropZoneRightLimit - dragElement.offsetWidth;
let bottomLimit = dropZoneBottomLimit - dragElement.offsetHeight;
if (newLeft > rightLimit) {
dragElement.style.left = rightLimit + "px";
} else if (newLeft < dropZoneLeftLimit) {
dragElement.style.left = dropZoneLeftLimit + "px";
} else {
dragElement.style.left = newLeft + "px";
}
if (newTop > bottomLimit) {
dragElement.style.top = bottomLimit + "px";
} else if (newTop < dropZoneTopLimit) {
dragElement.style.top = dropZoneTopLimit + "px";
} else {
dragElement.style.top = newTop + "px";
}
}
}
function onMouseUp(e) {
if (dragElement) {
makeEditable(dragElement.firstChild);
dragElement.style.cursor = "default";
dragElement = null;
}
}
function makeEditable(el) {
el.setAttribute("contenteditable", true);
el.spellcheck = false;
el.style.cursor = "text";
}
function deleteBox(e) {
e.target.parentNode.remove();
}