Added code storage api for storing code in the future
All checks were successful
FTP Upload on Push / deploy (push) Successful in 8s
FTP Upload on Push / release (push) Successful in 8s

This commit is contained in:
2025-12-26 18:08:52 +00:00
parent 175b6a19e0
commit 09d60b29b3
3 changed files with 208 additions and 1 deletions

View File

@@ -183,6 +183,19 @@
</svg>
</button>
</div>
<div class="header-right">
<button class="icon-btn" title="Open File" onclick="openFile()">
<svg viewBox="0 0 24 24">
<path d="M10 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z" />
</svg>
</button>
<button class="icon-btn" title="Save File (Ctrl+S)" onclick="saveFile()">
<svg viewBox="0 0 24 24">
<path
d="M17 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2V7l-4-4zm-5 16c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3zm3-10H5V5h10v4z" />
</svg>
</button>
</div>
</header>
<main>
@@ -331,11 +344,112 @@
}
}
// File System Access API support detection
let currentFileHandle = null;
async function saveFile() {
try {
// Check if File System Access API is supported
if ('showSaveFilePicker' in window) {
// Use existing file handle if available, otherwise prompt for new file
if (!currentFileHandle) {
const options = {
types: [{
description: 'Python Files',
accept: { 'text/x-python': ['.py'] },
}],
suggestedName: 'sketch.py'
};
currentFileHandle = await window.showSaveFilePicker(options);
}
const writable = await currentFileHandle.createWritable();
await writable.write(codeEditor.getValue());
await writable.close();
term.write('\r\n✓ File saved successfully\r\n');
} else {
// Fallback to download for browsers that don't support File System Access API
downloadCode();
}
} catch (err) {
if (err.name !== 'AbortError') {
console.error('Save failed:', err);
term.write('\r\n✗ Save failed: ' + err.message + '\r\n');
}
}
}
async function openFile() {
try {
if ('showOpenFilePicker' in window) {
const [fileHandle] = await window.showOpenFilePicker({
types: [{
description: 'Python Files',
accept: { 'text/x-python': ['.py'] },
}],
multiple: false
});
currentFileHandle = fileHandle;
const file = await fileHandle.getFile();
const contents = await file.text();
codeEditor.setValue(contents, -1);
term.write('\r\n✓ File opened: ' + file.name + '\r\n');
} else {
// Fallback to file input for browsers that don't support File System Access API
const input = document.createElement('input');
input.type = 'file';
input.accept = '.py';
input.onchange = async (e) => {
const file = e.target.files[0];
if (file) {
const contents = await file.text();
codeEditor.setValue(contents, -1);
term.write('\r\n✓ File opened: ' + file.name + '\r\n');
}
};
input.click();
}
} catch (err) {
if (err.name !== 'AbortError') {
console.error('Open failed:', err);
term.write('\r\n✗ Open failed: ' + err.message + '\r\n');
}
}
}
function downloadCode() {
const code = codeEditor.getValue();
const blob = new Blob([code], { type: 'text/x-python' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'sketch.py';
link.click();
URL.revokeObjectURL(url);
term.write('\r\n✓ File downloaded\r\n');
}
document.addEventListener('keydown', function (e) {
// Ctrl+Enter or Cmd+Enter to run code
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
e.preventDefault();
runCurrentCode();
}
// Ctrl+S or Cmd+S to save file
if ((e.ctrlKey || e.metaKey) && e.key === 's') {
e.preventDefault();
saveFile();
}
// Ctrl+O or Cmd+O to open file
if ((e.ctrlKey || e.metaKey) && e.key === 'o') {
e.preventDefault();
openFile();
}
});
window.addEventListener('load', () => {