-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
39 lines (34 loc) · 1.09 KB
/
script.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
document.addEventListener("DOMContentLoaded", (event) => {
const notepad = document.getElementById("notepad");
// Load saved content from localStorage
if (localStorage.getItem("notepadContent")) {
notepad.innerHTML = localStorage.getItem("notepadContent");
}
// Save content to localStorage on input
notepad.addEventListener("input", () => {
localStorage.setItem("notepadContent", notepad.innerHTML);
});
});
// Function to format the selected text
function formatText(command) {
document.execCommand(command, false, null);
}
// Function to insert a checkbox at the caret position
function insertCheckbox() {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.style.marginRight = "5px";
insertNodeAtCaret(checkbox);
}
// Helper function to insert a node at the caret position
function insertNodeAtCaret(node) {
let sel = window.getSelection();
if (sel.rangeCount) {
let range = sel.getRangeAt(0);
range.deleteContents();
range.insertNode(node);
range.collapse(false);
sel.removeAllRanges();
sel.addRange(range);
}
}