(fileName, folder, sourceId = '')
| 896 | } |
| 897 | |
| 898 | export function saveFile(fileName, folder, sourceId = '') { |
| 899 | const editor = window.currentEditor; |
| 900 | if (!editor) { |
| 901 | console.error("Editor not found!"); |
| 902 | return; |
| 903 | } |
| 904 | const saveBtn = document.getElementById("saveBtn"); |
| 905 | const setSavingState = (busy) => { |
| 906 | if (!saveBtn) return; |
| 907 | if (busy) { |
| 908 | if (!saveBtn.dataset.originalLabel) { |
| 909 | saveBtn.dataset.originalLabel = saveBtn.innerHTML; |
| 910 | } |
| 911 | saveBtn.innerHTML = |
| 912 | '<span class="material-icons spinning" style="font-size:16px; vertical-align:middle; margin-right:6px;">autorenew</span>Saving...'; |
| 913 | saveBtn.disabled = true; |
| 914 | return; |
| 915 | } |
| 916 | if (saveBtn.dataset.originalLabel) { |
| 917 | saveBtn.innerHTML = saveBtn.dataset.originalLabel; |
| 918 | delete saveBtn.dataset.originalLabel; |
| 919 | } |
| 920 | saveBtn.disabled = false; |
| 921 | }; |
| 922 | if (saveBtn && saveBtn.dataset.busy === "1") return; |
| 923 | if (saveBtn) saveBtn.dataset.busy = "1"; |
| 924 | setSavingState(true); |
| 925 | |
| 926 | const folderUsed = folder || window.currentFolder || "root"; |
| 927 | const sid = normalizeSourceId(sourceId) |
| 928 | || normalizeSourceId(window.currentEditorSourceId) |
| 929 | || normalizeSourceId(resolveActiveSourceId()); |
| 930 | const fileDataObj = { |
| 931 | fileName: fileName, |
| 932 | content: editor.getValue(), |
| 933 | folder: folderUsed |
| 934 | }; |
| 935 | if (sid) { |
| 936 | fileDataObj.sourceId = sid; |
| 937 | } |
| 938 | fetch("/api/file/saveFile.php", { |
| 939 | method: "POST", |
| 940 | credentials: "include", |
| 941 | headers: { |
| 942 | "Content-Type": "application/json", |
| 943 | "X-CSRF-Token": window.csrfToken |
| 944 | }, |
| 945 | body: JSON.stringify(fileDataObj) |
| 946 | }) |
| 947 | .then(response => response.json()) |
| 948 | .then(result => { |
| 949 | showToast(result.success || result.error); |
| 950 | document.getElementById("editorContainer")?.remove(); |
| 951 | loadFileList(folderUsed); |
| 952 | }) |
| 953 | .catch(error => console.error("Error saving file:", error)) |
| 954 | .finally(() => { |
| 955 | if (saveBtn) delete saveBtn.dataset.busy; |
no test coverage detected