From 207901428de716994a168d89ee2f5b35a5bf8e54 Mon Sep 17 00:00:00 2001 From: Hatem Hosny Date: Sun, 17 Sep 2023 19:21:45 +0300 Subject: [PATCH] deploy --- script.js | 49 ++++++++++++++++++++++++---------------------- src/livecodes.json | 2 +- src/script.js | 49 ++++++++++++++++++++++++---------------------- 3 files changed, 53 insertions(+), 47 deletions(-) diff --git a/script.js b/script.js index b4372e8..69acffb 100644 --- a/script.js +++ b/script.js @@ -1,7 +1,6 @@ import { createPlayground } from "https://unpkg.com/livecodes@0.2.0/livecodes.js"; let playground; -const algorithmSelect = document.getElementById("algorithm-select"); const getContent = async (url) => { const pathname = new URL(url).pathname.slice(1); @@ -30,10 +29,10 @@ const getContent = async (url) => { return { script, test, name }; }; -const loadAlgorithm = async (initialLoad = false) => { - const algorithm = algorithmSelect.value; +const loadAlgorithm = async () => { + const algorithm = window.algorithmSelect.value; const algorithmUrl = `${window.lang.repo}/blob/master/${algorithm}.${window.lang.ext}`; - if (!initialLoad) { + if (playground) { history.pushState( {}, "", @@ -43,23 +42,27 @@ const loadAlgorithm = async (initialLoad = false) => { const { script, test, name } = await getContent(algorithmUrl); const config = { - title: name, - languages: [window.lang.name], - script: { - language: window.lang.name, - content: script, - }, - tests: { - language: window.lang.name, - content: test || "", - }, - tools: { - enabled: ['console', 'tests', ...(window.lang.name === 'typescript' ? ['compiled'] : [])], - active: "tests", - status: "full", - }, - autotest: true, - }; + title: name, + languages: [window.lang.name], + script: { + language: window.lang.name, + content: script, + }, + tests: { + language: window.lang.name, + content: test || "", + }, + tools: { + enabled: [ + "console", + "tests", + ...(window.lang.name === "typescript" ? ["compiled"] : []), + ], + active: "tests", + status: "full", + }, + autotest: true, + }; if (!playground) { playground = await createPlayground("#container", { @@ -71,5 +74,5 @@ const loadAlgorithm = async (initialLoad = false) => { } }; -algorithmSelect.addEventListener("change", loadAlgorithm); -loadAlgorithm(true); +window.algorithmSelect.addEventListener("change", loadAlgorithm); +loadAlgorithm(); diff --git a/src/livecodes.json b/src/livecodes.json index 0b2d08c..64d5060 100644 --- a/src/livecodes.json +++ b/src/livecodes.json @@ -13,7 +13,7 @@ }, "script": { "language": "javascript", - "content": "import { createPlayground } from \"https://unpkg.com/livecodes@0.2.0/livecodes.js\";\r\n\r\nlet playground;\r\nconst algorithmSelect = document.getElementById(\"algorithm-select\");\r\n\r\nconst getContent = async (url) => {\r\n const pathname = new URL(url).pathname.slice(1);\r\n const [user, repo, _type, branch, ...path] = pathname.split(\"/\");\r\n const [name, extension] = path[path.length - 1].split(\".\");\r\n const testPath = `${path\r\n .slice(0, -1)\r\n .join(\"/\")}/test/${name}.test.${extension}`;\r\n const scriptUrl = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${path.join(\r\n \"/\",\r\n )}`;\r\n const testUrl = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${testPath}`;\r\n const [script, test] = await Promise.all([\r\n fetch(scriptUrl).then(\r\n async (res) => `// from ${url}\\n\\n` + (await res.text()),\r\n ),\r\n fetch(testUrl).then(async (res) =>\r\n res.ok\r\n ? (await res.text()).replace(\r\n /import (.*?) from ('|\").*?('|\")/g,\r\n \"import $1 from './script'\",\r\n )\r\n : null,\r\n ),\r\n ]);\r\n return { script, test, name };\r\n};\r\n\r\nconst loadAlgorithm = async (initialLoad = false) => {\r\n const algorithm = algorithmSelect.value;\r\n const algorithmUrl = `${window.lang.repo}/blob/master/${algorithm}.${window.lang.ext}`;\r\n if (!initialLoad) {\r\n history.pushState(\r\n {},\r\n \"\",\r\n `${location.origin}${location.pathname}?lang=${window.lang.ext}&algorithm=${algorithm}`,\r\n );\r\n }\r\n const { script, test, name } = await getContent(algorithmUrl);\r\n\r\n const config = {\r\n title: name,\r\n languages: [window.lang.name],\r\n script: {\r\n language: window.lang.name,\r\n content: script,\r\n },\r\n tests: {\r\n language: window.lang.name,\r\n content: test || \"\",\r\n },\r\n tools: {\r\n enabled: ['console', 'tests', ...(window.lang.name === 'typescript' ? ['compiled'] : [])],\r\n active: \"tests\",\r\n status: \"full\",\r\n },\r\n autotest: true,\r\n };\r\n\r\n if (!playground) {\r\n playground = await createPlayground(\"#container\", {\r\n appUrl: \"https://v14.livecodes.io/\",\r\n config,\r\n });\r\n } else {\r\n playground.setConfig(config);\r\n }\r\n};\r\n\r\nalgorithmSelect.addEventListener(\"change\", loadAlgorithm);\r\nloadAlgorithm(true);\r\n" + "content": "import { createPlayground } from \"https://unpkg.com/livecodes@0.2.0/livecodes.js\";\r\n\r\nlet playground;\r\n\r\nconst getContent = async (url) => {\r\n const pathname = new URL(url).pathname.slice(1);\r\n const [user, repo, _type, branch, ...path] = pathname.split(\"/\");\r\n const [name, extension] = path[path.length - 1].split(\".\");\r\n const testPath = `${path\r\n .slice(0, -1)\r\n .join(\"/\")}/test/${name}.test.${extension}`;\r\n const scriptUrl = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${path.join(\r\n \"/\",\r\n )}`;\r\n const testUrl = `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${testPath}`;\r\n const [script, test] = await Promise.all([\r\n fetch(scriptUrl).then(\r\n async (res) => `// from ${url}\\n\\n` + (await res.text()),\r\n ),\r\n fetch(testUrl).then(async (res) =>\r\n res.ok\r\n ? (await res.text()).replace(\r\n /import (.*?) from ('|\").*?('|\")/g,\r\n \"import $1 from './script'\",\r\n )\r\n : null,\r\n ),\r\n ]);\r\n return { script, test, name };\r\n};\r\n\r\nconst loadAlgorithm = async () => {\r\n const algorithm = window.algorithmSelect.value;\r\n const algorithmUrl = `${window.lang.repo}/blob/master/${algorithm}.${window.lang.ext}`;\r\n if (playground) {\r\n history.pushState(\r\n {},\r\n \"\",\r\n `${location.origin}${location.pathname}?lang=${window.lang.ext}&algorithm=${algorithm}`,\r\n );\r\n }\r\n const { script, test, name } = await getContent(algorithmUrl);\r\n\r\n const config = {\r\n title: name,\r\n languages: [window.lang.name],\r\n script: {\r\n language: window.lang.name,\r\n content: script,\r\n },\r\n tests: {\r\n language: window.lang.name,\r\n content: test || \"\",\r\n },\r\n tools: {\r\n enabled: [\r\n \"console\",\r\n \"tests\",\r\n ...(window.lang.name === \"typescript\" ? [\"compiled\"] : []),\r\n ],\r\n active: \"tests\",\r\n status: \"full\",\r\n },\r\n autotest: true,\r\n };\r\n\r\n if (!playground) {\r\n playground = await createPlayground(\"#container\", {\r\n appUrl: \"https://v14.livecodes.io/\",\r\n config,\r\n });\r\n } else {\r\n playground.setConfig(config);\r\n }\r\n};\r\n\r\nwindow.algorithmSelect.addEventListener(\"change\", loadAlgorithm);\r\nloadAlgorithm();\r\n" }, "stylesheets": [], "scripts": [], diff --git a/src/script.js b/src/script.js index b4372e8..69acffb 100644 --- a/src/script.js +++ b/src/script.js @@ -1,7 +1,6 @@ import { createPlayground } from "https://unpkg.com/livecodes@0.2.0/livecodes.js"; let playground; -const algorithmSelect = document.getElementById("algorithm-select"); const getContent = async (url) => { const pathname = new URL(url).pathname.slice(1); @@ -30,10 +29,10 @@ const getContent = async (url) => { return { script, test, name }; }; -const loadAlgorithm = async (initialLoad = false) => { - const algorithm = algorithmSelect.value; +const loadAlgorithm = async () => { + const algorithm = window.algorithmSelect.value; const algorithmUrl = `${window.lang.repo}/blob/master/${algorithm}.${window.lang.ext}`; - if (!initialLoad) { + if (playground) { history.pushState( {}, "", @@ -43,23 +42,27 @@ const loadAlgorithm = async (initialLoad = false) => { const { script, test, name } = await getContent(algorithmUrl); const config = { - title: name, - languages: [window.lang.name], - script: { - language: window.lang.name, - content: script, - }, - tests: { - language: window.lang.name, - content: test || "", - }, - tools: { - enabled: ['console', 'tests', ...(window.lang.name === 'typescript' ? ['compiled'] : [])], - active: "tests", - status: "full", - }, - autotest: true, - }; + title: name, + languages: [window.lang.name], + script: { + language: window.lang.name, + content: script, + }, + tests: { + language: window.lang.name, + content: test || "", + }, + tools: { + enabled: [ + "console", + "tests", + ...(window.lang.name === "typescript" ? ["compiled"] : []), + ], + active: "tests", + status: "full", + }, + autotest: true, + }; if (!playground) { playground = await createPlayground("#container", { @@ -71,5 +74,5 @@ const loadAlgorithm = async (initialLoad = false) => { } }; -algorithmSelect.addEventListener("change", loadAlgorithm); -loadAlgorithm(true); +window.algorithmSelect.addEventListener("change", loadAlgorithm); +loadAlgorithm();