An experimental web framework where PostgreSQL does everything: routing, HTML, file storage, jobs and sessions.
fix_editor.sql · 7.9 KB · 193 lines
| 1 | -- Fix the code editor JavaScript |
| 2 | UPDATE code_files SET content = '<!DOCTYPE html> |
| 3 | <html> |
| 4 | <head> |
| 5 | <title>PostBlob Code Editor</title> |
| 6 | <style> |
| 7 | * { margin: 0; padding: 0; box-sizing: border-box; } |
| 8 | body { font-family: monospace; background: #1e1e1e; color: #d4d4d4; } |
| 9 | .container { display: flex; height: 100vh; } |
| 10 | .sidebar { width: 250px; background: #252526; border-right: 1px solid #3e3e42; overflow-y: auto; } |
| 11 | .editor-pane { flex: 1; display: flex; flex-direction: column; } |
| 12 | .toolbar { background: #2d2d30; padding: 10px; border-bottom: 1px solid #3e3e42; } |
| 13 | .editor { flex: 1; padding: 20px; overflow: auto; } |
| 14 | textarea { width: 100%; height: 400px; background: #1e1e1e; color: #d4d4d4; border: 1px solid #3e3e42; padding: 10px; font-family: monospace; font-size: 14px; } |
| 15 | .output { background: #1e1e1e; border-top: 1px solid #3e3e42; padding: 20px; height: 200px; overflow-y: auto; } |
| 16 | .file-item { padding: 8px 15px; cursor: pointer; border-bottom: 1px solid #3e3e42; } |
| 17 | .file-item:hover { background: #2d2d30; } |
| 18 | .file-item.active { background: #094771; } |
| 19 | button { background: #007acc; color: white; border: none; padding: 8px 16px; margin-right: 10px; cursor: pointer; } |
| 20 | button:hover { background: #005a9e; } |
| 21 | .status { padding: 5px 10px; background: #007acc; color: white; display: inline-block; } |
| 22 | pre { white-space: pre-wrap; word-wrap: break-word; } |
| 23 | </style> |
| 24 | </head> |
| 25 | <body> |
| 26 | <div class="container"> |
| 27 | <div class="sidebar"> |
| 28 | <h3 style="padding: 15px; border-bottom: 1px solid #3e3e42;">Code Files</h3> |
| 29 | <div id="fileList"></div> |
| 30 | </div> |
| 31 | <div class="editor-pane"> |
| 32 | <div class="toolbar"> |
| 33 | <button onclick="saveCode()">Save</button> |
| 34 | <button onclick="executeCode()">Execute</button> |
| 35 | <button onclick="hotReload()">Hot Reload</button> |
| 36 | <button onclick="newFile()">New File</button> |
| 37 | <span class="status" id="status">Ready</span> |
| 38 | </div> |
| 39 | <div class="editor"> |
| 40 | <input type="text" id="filePath" placeholder="File path..." style="width: 100%; padding: 5px; margin-bottom: 10px; background: #1e1e1e; color: #d4d4d4; border: 1px solid #3e3e42;"> |
| 41 | <textarea id="codeEditor" placeholder="-- Write your SQL or PL/pgSQL code here..."></textarea> |
| 42 | </div> |
| 43 | <div class="output"> |
| 44 | <h4 style="margin-bottom: 10px;">Output:</h4> |
| 45 | <pre id="output"></pre> |
| 46 | </div> |
| 47 | </div> |
| 48 | </div> |
| 49 | |
| 50 | <script> |
| 51 | let currentFile = null; |
| 52 | |
| 53 | async function loadFiles() { |
| 54 | try { |
| 55 | const response = await fetch("/code/list"); |
| 56 | const files = await response.json(); |
| 57 | const fileList = document.getElementById("fileList"); |
| 58 | |
| 59 | if (!files || files.error) { |
| 60 | fileList.innerHTML = "<div>No files found</div>"; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | fileList.innerHTML = files.map(f => { |
| 65 | return `<div class="file-item" data-path="${f.path}">${f.path}<br><small>${f.size} bytes | v${f.version}</small></div>`; |
| 66 | }).join(""); |
| 67 | |
| 68 | // Add click handlers |
| 69 | document.querySelectorAll(".file-item").forEach(item => { |
| 70 | item.addEventListener("click", function() { |
| 71 | loadFile(this.getAttribute("data-path"), this); |
| 72 | }); |
| 73 | }); |
| 74 | } catch (e) { |
| 75 | console.error("Error loading files:", e); |
| 76 | document.getElementById("output").textContent = "Error loading files: " + e.message; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | async function loadFile(path, element) { |
| 81 | try { |
| 82 | currentFile = path; |
| 83 | document.getElementById("filePath").value = path; |
| 84 | |
| 85 | const response = await fetch(`/code/file?path=${encodeURIComponent(path)}`); |
| 86 | const file = await response.json(); |
| 87 | |
| 88 | if (file.error) { |
| 89 | document.getElementById("output").textContent = "Error: " + file.error; |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | document.getElementById("codeEditor").value = file.content || ""; |
| 94 | setStatus(`Loaded ${path}`); |
| 95 | |
| 96 | // Update active state |
| 97 | document.querySelectorAll(".file-item").forEach(el => el.classList.remove("active")); |
| 98 | if (element) element.classList.add("active"); |
| 99 | |
| 100 | } catch (e) { |
| 101 | console.error("Error loading file:", e); |
| 102 | document.getElementById("output").textContent = "Error loading file: " + e.message; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | async function saveCode() { |
| 107 | try { |
| 108 | const path = document.getElementById("filePath").value; |
| 109 | const content = document.getElementById("codeEditor").value; |
| 110 | |
| 111 | if (!path) { |
| 112 | alert("Please enter a file path"); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | const response = await fetch("/code/save", { |
| 117 | method: "POST", |
| 118 | headers: {"Content-Type": "application/json"}, |
| 119 | body: JSON.stringify({path, content}) |
| 120 | }); |
| 121 | |
| 122 | const result = await response.json(); |
| 123 | document.getElementById("output").textContent = JSON.stringify(result, null, 2); |
| 124 | setStatus(result.hot_reloaded ? "Saved & Reloaded!" : "Saved!"); |
| 125 | loadFiles(); |
| 126 | } catch (e) { |
| 127 | console.error("Error saving:", e); |
| 128 | document.getElementById("output").textContent = "Error saving: " + e.message; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | async function executeCode() { |
| 133 | try { |
| 134 | const code = document.getElementById("codeEditor").value; |
| 135 | |
| 136 | if (!code) { |
| 137 | alert("No code to execute"); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | const response = await fetch("/code/execute", { |
| 142 | method: "POST", |
| 143 | headers: {"Content-Type": "application/json"}, |
| 144 | body: JSON.stringify({code, language: "sql"}) |
| 145 | }); |
| 146 | |
| 147 | const result = await response.json(); |
| 148 | document.getElementById("output").textContent = JSON.stringify(result, null, 2); |
| 149 | setStatus(result.success ? "Executed!" : "Error!"); |
| 150 | } catch (e) { |
| 151 | console.error("Error executing:", e); |
| 152 | document.getElementById("output").textContent = "Error executing: " + e.message; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | async function hotReload() { |
| 157 | try { |
| 158 | const response = await fetch("/code/reload", {method: "POST"}); |
| 159 | const result = await response.json(); |
| 160 | document.getElementById("output").textContent = JSON.stringify(result, null, 2); |
| 161 | setStatus("Hot Reloaded!"); |
| 162 | } catch (e) { |
| 163 | console.error("Error reloading:", e); |
| 164 | document.getElementById("output").textContent = "Error reloading: " + e.message; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | function newFile() { |
| 169 | document.getElementById("filePath").value = "functions/new_function.sql"; |
| 170 | document.getElementById("codeEditor").value = `-- New function |
| 171 | CREATE OR REPLACE FUNCTION new_function() |
| 172 | RETURNS text AS $$ |
| 173 | BEGIN |
| 174 | RETURN ''Hello from new function!''; |
| 175 | END; |
| 176 | $$ LANGUAGE plpgsql;`; |
| 177 | currentFile = null; |
| 178 | setStatus("New file"); |
| 179 | } |
| 180 | |
| 181 | function setStatus(text) { |
| 182 | document.getElementById("status").textContent = text; |
| 183 | } |
| 184 | |
| 185 | // Load files on startup |
| 186 | window.addEventListener("load", loadFiles); |
| 187 | </script> |
| 188 | </body> |
| 189 | </html>' |
| 190 | WHERE path = 'ui/code_editor.html'; |
| 191 | |
| 192 | -- Return success |
| 193 | SELECT 'Editor fixed!' as result; |