From dfeaeb3ea2f9e8b4357fec344aae4a902efa9f0d Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 16 Oct 2023 16:10:36 +0530 Subject: [PATCH 1/3] Pull code from remote source --- src/index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/index.js b/src/index.js index 14758a7..46e1b7c 100644 --- a/src/index.js +++ b/src/index.js @@ -109,4 +109,17 @@ Promise.all([ }); toggles.querySelector("select").removeAttribute("disabled"); + + // fetch code from URL + const params = new URLSearchParams(document.location.search) + const sourceURL = params.get('source') + try { + const response = await fetch(new URL(sourceURL)) + if (response.status === 200) { + const data = await response.text() + editor.setValue(data) + } + } catch (error) { + console.error(error) + } }); From 0ad152a4900a3161aef9d77b12fbe64c5ba016b3 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 16 Oct 2023 16:19:21 +0530 Subject: [PATCH 2/3] fix typo --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 46e1b7c..3b2b9c6 100644 --- a/src/index.js +++ b/src/index.js @@ -29,7 +29,7 @@ Promise.all([ // dang huge (> 40Mb). In the meantime the textarea that is holding the place // of the actual functional one is just going to display "Loading...". import("./createRuby").then(({ default: createRuby }) => createRuby()) -]).then(([editor, mermaid, ruby]) => { +]).then(async ([editor, mermaid, ruby]) => { // First, grab a reference to the output element so that we can update it. // Then, set it initially to the output represented by the source. const output = document.getElementById("output"); From f4efb11e18c9f80faba537cedf4590a0de1c6231 Mon Sep 17 00:00:00 2001 From: Syed Faraaz Ahmad Date: Mon, 16 Oct 2023 22:45:29 +0530 Subject: [PATCH 3/3] use promises --- src/index.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/index.js b/src/index.js index 3b2b9c6..24fd37f 100644 --- a/src/index.js +++ b/src/index.js @@ -29,7 +29,7 @@ Promise.all([ // dang huge (> 40Mb). In the meantime the textarea that is holding the place // of the actual functional one is just going to display "Loading...". import("./createRuby").then(({ default: createRuby }) => createRuby()) -]).then(async ([editor, mermaid, ruby]) => { +]).then(([editor, mermaid, ruby]) => { // First, grab a reference to the output element so that we can update it. // Then, set it initially to the output represented by the source. const output = document.getElementById("output"); @@ -113,13 +113,10 @@ Promise.all([ // fetch code from URL const params = new URLSearchParams(document.location.search) const sourceURL = params.get('source') - try { - const response = await fetch(new URL(sourceURL)) - if (response.status === 200) { - const data = await response.text() - editor.setValue(data) - } - } catch (error) { - console.error(error) + if (sourceURL) { + fetch(new URL(sourceURL)) + .then(response => response.text()) + .then(data => editor.setValue(data)) + .catch(error => console.error(error)) } });