Skip to content

Commit

Permalink
deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Sep 17, 2023
1 parent d3a6825 commit 2079014
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 47 deletions.
49 changes: 26 additions & 23 deletions script.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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(
{},
"",
Expand All @@ -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", {
Expand All @@ -71,5 +74,5 @@ const loadAlgorithm = async (initialLoad = false) => {
}
};

algorithmSelect.addEventListener("change", loadAlgorithm);
loadAlgorithm(true);
window.algorithmSelect.addEventListener("change", loadAlgorithm);
loadAlgorithm();
2 changes: 1 addition & 1 deletion src/livecodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
49 changes: 26 additions & 23 deletions src/script.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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(
{},
"",
Expand All @@ -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", {
Expand All @@ -71,5 +74,5 @@ const loadAlgorithm = async (initialLoad = false) => {
}
};

algorithmSelect.addEventListener("change", loadAlgorithm);
loadAlgorithm(true);
window.algorithmSelect.addEventListener("change", loadAlgorithm);
loadAlgorithm();

0 comments on commit 2079014

Please sign in to comment.