()
| 783 | |
| 784 | // Save the current file |
| 785 | function saveCurrentFile() { |
| 786 | if (!currentFile) { |
| 787 | alert('No file is currently open.'); |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | const content = editor.value; |
| 792 | |
| 793 | // Use the full path stored in currentFile |
| 794 | fetch(`/api/files/${encodeURIComponent(currentFile)}`, { |
| 795 | method: 'PUT', |
| 796 | headers: { |
| 797 | 'Content-Type': 'application/json' |
| 798 | }, |
| 799 | body: JSON.stringify({ content }) |
| 800 | }) |
| 801 | .then(response => response.json()) |
| 802 | .then(data => { |
| 803 | if (data.status === 'success') { |
| 804 | // Show success message |
| 805 | const saveBtn = document.getElementById('btn-save'); |
| 806 | const originalText = saveBtn.innerHTML; |
| 807 | |
| 808 | saveBtn.innerHTML = ` |
| 809 | <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor"> |
| 810 | <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /> |
| 811 | </svg> |
| 812 | Saved! |
| 813 | `; |
| 814 | |
| 815 | setTimeout(() => { |
| 816 | saveBtn.innerHTML = originalText; |
| 817 | }, 2000); |
| 818 | |
| 819 | // Update preview |
| 820 | updatePreview(); |
| 821 | } else { |
| 822 | console.error('Error saving file:', data.message); |
| 823 | alert(`Error saving file: ${data.message}`); |
| 824 | } |
| 825 | }) |
| 826 | .catch(error => { |
| 827 | console.error('Error saving file:', error); |
| 828 | alert('Error saving file. Please try again.'); |
| 829 | }); |
| 830 | } |
| 831 | |
| 832 | // Schedule auto-save |
| 833 | function scheduleAutoSave() { |
no test coverage detected