|
1 | 1 | import Store from "./store.js"; |
| 2 | +import importFromGist from "./gist.js"; |
2 | 3 |
|
3 | 4 | const DEFAULT_SOURCE = `# Welcome to Ruby Next playground! |
4 | 5 | # Here you can write Ruby code and see how it will be transformed by Ruby Next. |
@@ -158,10 +159,43 @@ export default class App { |
158 | 159 | }); |
159 | 160 | } |
160 | 161 |
|
| 162 | + const importDialog = document.getElementById("importDialog"); |
| 163 | + |
| 164 | + if (importDialog) { |
| 165 | + this.el |
| 166 | + .querySelector('[target="import-btn"]') |
| 167 | + .addEventListener("click", () => importDialog.show()); |
| 168 | + |
| 169 | + importDialog.addEventListener("submit", (e) => { |
| 170 | + e.preventDefault(); |
| 171 | + |
| 172 | + const input = e.target.querySelector('[name="gistId"]'); |
| 173 | + const url = input.value; |
| 174 | + |
| 175 | + importDialog.hide(); |
| 176 | + |
| 177 | + this.importGist(url); |
| 178 | + }); |
| 179 | + |
| 180 | + importDialog.addEventListener("click", (e) => { |
| 181 | + if (e.target.tagName !== "A" || !e.target.dataset.url) return; |
| 182 | + |
| 183 | + e.preventDefault(); |
| 184 | + |
| 185 | + const url = e.target.dataset.url; |
| 186 | + |
| 187 | + importDialog.hide(); |
| 188 | + |
| 189 | + this.importGist(url); |
| 190 | + }); |
| 191 | + } |
| 192 | + |
161 | 193 | this.setCurrentVMVersion(); |
162 | 194 |
|
163 | 195 | if (theme === "dark") |
164 | 196 | document.documentElement.classList.add("sl-theme-dark"); |
| 197 | + |
| 198 | + this.loadExampleFromUrl(); |
165 | 199 | } |
166 | 200 |
|
167 | 201 | transpile(code, opts = {}) { |
@@ -272,11 +306,35 @@ export default class App { |
272 | 306 | content.innerHTML = examples |
273 | 307 | .map( |
274 | 308 | (key) => |
275 | | - `<a class="text-blue-600 dark:text-blue-200 hover:text-blue-500 dark:text-blue-100 cursor-pointer py-2 inline-block" href="#" data-key="${key}">${key}</a>` |
| 309 | + `<a class="text-blue-600 dark:text-blue-200 hover:text-blue-500 dark:hover:text-blue-100 cursor-pointer py-2 inline-block" href="#" data-key="${key}">${key}</a>` |
276 | 310 | ) |
277 | 311 | .join(""); |
278 | 312 | } |
279 | 313 |
|
280 | 314 | dialog.show(); |
281 | 315 | } |
| 316 | + |
| 317 | + async importGist(url) { |
| 318 | + try { |
| 319 | + const { id, code, config } = await importFromGist(url); |
| 320 | + |
| 321 | + this.codeEditor.setValue(code); |
| 322 | + this.configEditor.setValue(config); |
| 323 | + |
| 324 | + // Set URL fragment to include gist ID |
| 325 | + window.location.hash = `gist:${id}`; |
| 326 | + } catch (e) { |
| 327 | + alert(e.message); |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + async loadExampleFromUrl() { |
| 332 | + if (!window.location.hash) return; |
| 333 | + |
| 334 | + const [type, id] = window.location.hash.slice(1).split(":"); |
| 335 | + |
| 336 | + if (type !== "gist") return; |
| 337 | + |
| 338 | + await this.importGist(`https://gist.github.com/${id}`); |
| 339 | + } |
282 | 340 | } |
0 commit comments