-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f5ae1f
commit de8d8b5
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |