|
| 1 | +// Readme functionality for CodeClash Trajectory Viewer |
| 2 | + |
| 3 | +let readmeTimeout = null; |
| 4 | +let readmeTextarea = null; |
| 5 | +let readmeStatus = null; |
| 6 | + |
| 7 | +function loadReadme() { |
| 8 | + const urlParams = new URLSearchParams(window.location.search); |
| 9 | + const folder = urlParams.get("folder"); |
| 10 | + |
| 11 | + if (!folder) return; |
| 12 | + |
| 13 | + fetch("/load-readme?folder=" + encodeURIComponent(folder)) |
| 14 | + .then((response) => response.json()) |
| 15 | + .then((data) => { |
| 16 | + if (data.success) { |
| 17 | + readmeTextarea.value = data.content; |
| 18 | + readmeStatus.textContent = "Loaded"; |
| 19 | + readmeStatus.className = "readme-status"; |
| 20 | + } else { |
| 21 | + readmeStatus.textContent = "Error loading: " + data.error; |
| 22 | + readmeStatus.className = "readme-status error"; |
| 23 | + } |
| 24 | + }) |
| 25 | + .catch((error) => { |
| 26 | + console.error("Error loading readme:", error); |
| 27 | + readmeStatus.textContent = "Error loading readme"; |
| 28 | + readmeStatus.className = "readme-status error"; |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +function saveReadme() { |
| 33 | + const urlParams = new URLSearchParams(window.location.search); |
| 34 | + const folder = urlParams.get("folder"); |
| 35 | + |
| 36 | + if (!folder) return; |
| 37 | + |
| 38 | + readmeStatus.textContent = "Saving..."; |
| 39 | + readmeStatus.className = "readme-status saving"; |
| 40 | + |
| 41 | + fetch("/save-readme", { |
| 42 | + method: "POST", |
| 43 | + headers: { |
| 44 | + "Content-Type": "application/json", |
| 45 | + }, |
| 46 | + body: JSON.stringify({ |
| 47 | + selected_folder: folder, |
| 48 | + content: readmeTextarea.value, |
| 49 | + }), |
| 50 | + }) |
| 51 | + .then((response) => response.json()) |
| 52 | + .then((data) => { |
| 53 | + if (data.success) { |
| 54 | + readmeStatus.textContent = "Saved"; |
| 55 | + readmeStatus.className = "readme-status saved"; |
| 56 | + } else { |
| 57 | + readmeStatus.textContent = "Error: " + data.error; |
| 58 | + readmeStatus.className = "readme-status error"; |
| 59 | + } |
| 60 | + }) |
| 61 | + .catch((error) => { |
| 62 | + console.error("Error saving readme:", error); |
| 63 | + readmeStatus.textContent = "Error saving"; |
| 64 | + readmeStatus.className = "readme-status error"; |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +function setupReadmeAutosave() { |
| 69 | + readmeTextarea = document.getElementById("readme-textarea"); |
| 70 | + readmeStatus = document.getElementById("readme-status"); |
| 71 | + |
| 72 | + if (!readmeTextarea || !readmeStatus) return; |
| 73 | + |
| 74 | + // Load existing content |
| 75 | + loadReadme(); |
| 76 | + |
| 77 | + // Setup autosave on input |
| 78 | + readmeTextarea.addEventListener("input", function () { |
| 79 | + // Clear existing timeout |
| 80 | + if (readmeTimeout) { |
| 81 | + clearTimeout(readmeTimeout); |
| 82 | + } |
| 83 | + |
| 84 | + // Set new timeout for autosave (1 second delay) |
| 85 | + readmeTimeout = setTimeout(saveReadme, 1000); |
| 86 | + |
| 87 | + // Show typing indicator |
| 88 | + readmeStatus.textContent = "Typing..."; |
| 89 | + readmeStatus.className = "readme-status"; |
| 90 | + }); |
| 91 | +} |
0 commit comments