Skip to content

Commit

Permalink
Uploading web app source files
Browse files Browse the repository at this point in the history
  • Loading branch information
matteotardito committed Mar 6, 2024
1 parent 7f5ae1f commit de8d8b5
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const UI = class {

static textarea = document.querySelector("textarea");

};



const DB = class {

static open = () => {
return new Promise((resolve, reject) => {
const request = window.indexedDB.open('appDB', 1);
request.onupgradeneeded = event => {
const db = event.target.result;
db.createObjectStore('texts', { keyPath: 'id' });
};
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
};

static saveText = async text => {
try {
const db = await DB.open();
const transaction = db.transaction(['texts'], 'readwrite');
const store = transaction.objectStore('texts');
const data = { id: 1, text: text };
store.put(data);
} catch(error) {
console.error('Errore durante il salvataggio nel database:', error);
}
};

static loadText = async () => {
try {
const db = await DB.open();
const transaction = db.transaction(['texts'], 'readonly');
const store = transaction.objectStore('texts');
const request = store.get(1);
request.onsuccess = event => {
const data = event.target.result;
if (data) UI.textarea.value = data.text;
};
} catch(error) {
console.error('Errore durante il salvataggio nel database:', error);
}
};

};



const App = class {

static timer;

static textAutosave = () => {
clearTimeout(App.timer);
DB.saveText(UI.textarea.value);
};

static resetAutosaveTimer = () => {
clearTimeout(App.timer);
App.timer = setTimeout(App.textAutosave, 1000);
};

static init = () => {
if (!localStorage.getItem('testPopup')) {
if (confirm("This app is in ALPHA state and should be used for testing purposes only."))
localStorage.setItem('testPopup','ok');
else
window.location.href = "https://app.tardito.it/";
}
DB.loadText();
UI.textarea.addEventListener('input', App.resetAutosaveTimer);
window.addEventListener('unload', () => {
DB.saveText(UI.textarea.value);
});
};

}



window.addEventListener("load",App.init);
7 changes: 7 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>App Test</title>
<textarea></textarea>
<script src="app.js"></script>
26 changes: 26 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
body {
background: #eee;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: 0;
}

textarea {
all: unset;
color: #111;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
font-family: monospace;
font-size: 1rem;
padding: 1rem 2ch;
background: white;
word-wrap: break-word;
max-width: 64ch;
margin: auto;
}

0 comments on commit de8d8b5

Please sign in to comment.