-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad.js
More file actions
106 lines (95 loc) · 3.86 KB
/
notepad.js
File metadata and controls
106 lines (95 loc) · 3.86 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
document.addEventListener("DOMContentLoaded", function () {
// Recupero degli elementi del DOM
const textArea = document.getElementById("notepad");
const newFileBtn = document.getElementById("newFile");
const openFileBtn = document.getElementById("openFile");
const saveFileBtn = document.getElementById("saveFile");
const saveAsFileBtn = document.getElementById("saveAsFile");
const fontFamilySelect = document.getElementById("fontFamily");
const fontSizeSelect = document.getElementById("fontSize");
const fontColorInput = document.getElementById("fontColor");
const hiddenFileInput = document.getElementById("hiddenFileInput");
// Variabile per tenere traccia del nome del file corrente
let currentFileName = null;
// ==========================================================
// Funzione: Aggiornamento Dinamico dello Stile del Testo
// ==========================================================
function updateTextStyle() {
textArea.style.fontFamily = fontFamilySelect.value;
textArea.style.fontSize = fontSizeSelect.value;
textArea.style.color = fontColorInput.value;
}
// Eventi per la toolbar
fontFamilySelect.addEventListener("change", updateTextStyle);
fontSizeSelect.addEventListener("change", updateTextStyle);
fontColorInput.addEventListener("change", updateTextStyle);
// Inizializza lo stile
updateTextStyle();
// ==========================================================
// Funzioni di Gestione File
// ==========================================================
// New Note: Pulisce l'editor dopo una conferma e resetta currentFileName.
newFileBtn.addEventListener("click", function () {
if (confirm("Are you sure you want to create a new note? Unsaved changes will be lost.")) {
textArea.value = "";
currentFileName = null;
}
});
// Open Note: Utilizzo dell'input file nascosto.
openFileBtn.addEventListener("click", function () {
hiddenFileInput.value = ""; // Ripulire la selezione precedente
hiddenFileInput.click();
});
// Gestione del file input: Legge il file e carica il contenuto.
hiddenFileInput.addEventListener("change", function (event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (evt) {
textArea.value = evt.target.result;
currentFileName = file.name;
};
reader.onerror = function () {
alert("Error reading file.");
};
reader.readAsText(file);
});
// Funzione helper: Forza il download del contenuto nel file.
function downloadTextFile(fileName) {
const content = textArea.value;
const blob = new Blob([content], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
downloadLink.href = url;
downloadLink.download = fileName;
document.body.appendChild(downloadLink);
downloadLink.click();
setTimeout(function () {
URL.revokeObjectURL(url);
document.body.removeChild(downloadLink);
}, 100);
}
// Save Note: Se currentFileName è definito, salva; altrimenti usa Save As.
saveFileBtn.addEventListener("click", function () {
if (currentFileName) {
downloadTextFile(currentFileName);
} else {
saveAsFunction();
}
});
// Save As: Chiede un nome file all'utente e salva il file.
saveAsFileBtn.addEventListener("click", function () {
saveAsFunction();
});
function saveAsFunction() {
const defaultName = currentFileName ? currentFileName : "note.txt";
let fileName = prompt("Enter the file name to save (include extension, e.g., note.txt):", defaultName);
if (fileName && fileName.trim() !== "") {
currentFileName = fileName.trim();
downloadTextFile(currentFileName);
} else {
alert("Invalid file name. Please try again.");
}
}
console.log("Notepad‑JS initialized successfully.");
});