Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
168 lines (138 loc) · 4.78 KB
/
Copy pathscript.js
File metadata and controls
168 lines (138 loc) · 4.78 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const addBtn = document.getElementById("add-note");
const input = document.getElementById("note-input");
const list = document.getElementById("notes-list");
let notes = [];
input.addEventListener("focus", function () {
if (input.value.trim() === "") {
input.value = "☐ ";
}
});
input.addEventListener("keydown", function(e) {
if (e.key === "Enter") {
e.preventDefault();
const start = input.selectionStart;
const end = input.selectionEnd;
const currentValue = input.value;
const checkbox = "☐ ";
const newValue =
currentValue.substring(0, start) + "\n" + checkbox + currentValue.substring(end);
input.value = newValue;
input.selectionStart = input.selectionEnd = start + checkbox.length + 1;
}
});
addBtn.addEventListener("click", () => {
const noteText = input.value.trim();
if (noteText !== "") {
notes.push(noteText);
input.value = "";
renderNotes();
}
});
function renderNotes() {
list.innerHTML = "";
notes.forEach((note, index) => {
const div = document.createElement("div");
div.className = "note";
div.setAttribute("draggable", "true");
div.setAttribute("data-index", index);
const lines = note.split('\n').map(line => {
const trimmed = line.trim();
if (trimmed.startsWith("☑")) {
return `<label><input type="checkbox" checked> ${trimmed.substring(1).trim()}</label>`;
} else if (trimmed.startsWith("☐")) {
return `<label><input type="checkbox"> ${trimmed.substring(1).trim()}</label>`;
} else {
return `<label><input type="checkbox"> ${trimmed}</label>`;
}
}).join('<br>');
div.innerHTML = `
<div class="note-text">${lines}</div>
<textarea class="edit-area" style="display:none;">${note}</textarea>
<div style="margin-top: 10px;">
<button class="edit-btn" onclick="editNote(${index}, this)">Edit</button>
<button class="save-btn" style="display:none;" onclick="saveNote(${index}, this)">Save</button>
<button class="delete-btn" onclick="deleteNote(${index})">✕</button>
</div>
`;
const checkboxes = div.querySelectorAll("input[type='checkbox']");
checkboxes.forEach((checkbox, i) => {
checkbox.addEventListener("change", () => {
const lines = notes[index].split('\n');
if (checkbox.checked) {
lines[i] = lines[i].replace(/^☐/, "☑");
} else {
lines[i] = lines[i].replace(/^☑/, "☐");
}
notes[index] = lines.join('\n');
renderNotes();
});
});
const editTextarea = div.querySelector(".edit-area");
editTextarea.addEventListener("keydown", function(e) {
if (e.key === "Enter") {
e.preventDefault();
const start = this.selectionStart;
const end = this.selectionEnd;
const currentValue = this.value;
const checkbox = "☐ ";
const newValue =
currentValue.substring(0, start) + "\n" + checkbox + currentValue.substring(end);
this.value = newValue;
this.selectionStart = this.selectionEnd = start + checkbox.length + 1;
}
});
div.addEventListener("dragstart", handleDragStart);
div.addEventListener("dragover", handleDragOver);
div.addEventListener("drop", handleDrop);
list.appendChild(div);
});
}
function editNote(index, button) {
const noteDiv = button.closest(".note");
const p = noteDiv.querySelector(".note-text");
const textarea = noteDiv.querySelector(".edit-area");
const saveBtn = noteDiv.querySelector(".save-btn");
p.style.display = "none";
textarea.style.display = "block";
button.style.display = "none";
saveBtn.style.display = "inline";
}
function saveNote(index, button) {
const noteDiv = button.closest(".note");
const p = noteDiv.querySelector(".note-text");
const textarea = noteDiv.querySelector(".edit-area");
const editBtn = noteDiv.querySelector(".edit-btn");
const newText = textarea.value.trim();
if (newText !== "") {
notes[index] = newText;
}
p.style.display = "block";
textarea.style.display = "none";
button.style.display = "none";
editBtn.style.display = "inline";
renderNotes();
}
function deleteNote(i) {
notes.splice(i, 1);
renderNotes();
}
let draggedIndex = null;
function handleDragStart(e) {
draggedIndex = +e.target.getAttribute("data-index");
e.dataTransfer.effectAllowed = "move";
}
function handleDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = "move";
}
function handleDrop(e) {
e.preventDefault();
const droppedIndex = +e.target.closest(".note").getAttribute("data-index");
if (draggedIndex !== null && draggedIndex !== droppedIndex) {
const draggedNote = notes[draggedIndex];
notes.splice(draggedIndex, 1);
notes.splice(droppedIndex, 0, draggedNote);
renderNotes();
}
draggedIndex = null;
}