diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index 8000c9a..b860089 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -8,6 +8,7 @@ import { $editor } from "../../stores/editor.ts"; import useComponentVisible from "../../useComponentVisible.ts"; import cs from "./Header.module.css"; +import { getQueryParam } from "../../fsInitializer.ts"; export const Header = () => { const { isOpen } = useStore($menu); @@ -31,41 +32,45 @@ export const Header = () => { - {showFork && ( - - )} - {(showSave) && ( - - )} - {showUpdate && ( - + {getQueryParam("embed") !== "1" && ( + <> + {showFork && ( + + )} + {showSave && ( + + )} + {showUpdate && ( + + )} +
+ {gistLoading && } +
+ )} -
- {gistLoading && } -
@@ -74,32 +79,36 @@ export const Header = () => {
- {currentUser.id ? ( -
setIsComponentVisible(true)} - ref={ref} - > - {currentUser.username} -
- -
-
- ) : ( - + {getQueryParam("embed") === "1" && ( + <> + {currentUser.id ? ( +
setIsComponentVisible(true)} + ref={ref} + > + {currentUser.username} +
+ +
+
+ ) : ( + + )} + )}
diff --git a/src/components/Output/SettingsTab.tsx b/src/components/Output/SettingsTab.tsx index a0493b0..904ed4d 100644 --- a/src/components/Output/SettingsTab.tsx +++ b/src/components/Output/SettingsTab.tsx @@ -15,7 +15,7 @@ function findDirectory(dir: DirectoryContents, s: string) { let current = dir; for (const part of parts) { - if (!("contents" in current[part])) { + if (!(current[part] && "contents" in current[part])) { return undefined; } current = (current[part] as MarshaledDirectory).contents; diff --git a/src/fsInitializer.ts b/src/fsInitializer.ts index 9489333..0a962a3 100644 --- a/src/fsInitializer.ts +++ b/src/fsInitializer.ts @@ -1,11 +1,28 @@ import importFromGist from "./gist.ts"; +export const getQueryParam = (key: string) => { + const url = new URL(window.location.href); + const params = new URLSearchParams(url.search); + return params.get(key); +}; + const defaultGem = "octokit"; -const mainRbHeader = (gem: string | null) => `# This is a Ruby WASI playground +const description = `# This is a Ruby WASI playground # You can run any Ruby code here and see the result -# You can also install gems using a Gemfile and the "Bundle install" button. -${gem ? `\nrequire "${gem}"\n` : ""}`; +# You can also install gems using a Gemfile and the "Bundle install" button.`; + +const mainRbHeader = (gem: string | null) => { + const strings: string[] = []; + if (getQueryParam("embed") !== "1") { + strings.push(description); + } + if (gem) { + strings.push(`require "${gem.split("@")[0]}"\n`); + } + + return strings.join("\n"); +}; const mainRb = `${mainRbHeader(defaultGem)} # RunRuby.dev comes with a WASI-compatible Faraday adapter @@ -21,26 +38,20 @@ user = client.user("matz") pp({login: user.login, name: user.name, company: user.company}) `; -export const getQueryParam = (key: string) => { - const url = new URL(window.location.href); - const params = new URLSearchParams(url.search); - return params.get(key); -}; - const gemfileContents = (gem: string) => ( `source "https://rubygems.org" -gem "${gem}" +gem "${gem.split("@")[0]}", "${gem.split("@")[1] || ">= 0"}" ` ); const buildStarter = ({ gem, main }: { gem: string | null, main: string }) => { const result = [ - { filename: "main.rb", content: main }, + { filename: "main.rb", content: main } ]; if (gem) { - result.push({ filename: "Gemfile", content: gemfileContents(gem) }) + result.push({ filename: "Gemfile", content: gemfileContents(gem) }); } return result; @@ -62,6 +73,10 @@ export const composeInitialFS = async () => { return buildMainRB(); }; +export const urlSafeAtob = (str: string) => { + return decodeURIComponent(atob(str.replace(/-/g, "+").replace(/_/g, "/"))); +}; + const buildMainRB = () => { const initialGem = getQueryParam("gem"); const initialCode = getQueryParam("code"); @@ -70,7 +85,7 @@ const buildMainRB = () => { return { files: buildStarter({ gem: defaultGem, main: mainRb }) }; } - const code = initialCode ? decodeURIComponent(atob(initialCode)) : ""; + const code = initialCode ? urlSafeAtob(initialCode) : ""; const main = mainRbHeader(initialGem) + "\n" + code; return { files: buildStarter({ gem: initialGem, main }) }; diff --git a/src/stores/theme.ts b/src/stores/theme.ts index 5290db5..9cd81a3 100644 --- a/src/stores/theme.ts +++ b/src/stores/theme.ts @@ -7,7 +7,6 @@ const THEME_KEY = "theme"; export const $theme = atom("system"); export const setTheme = action($theme, "set", (store, theme: Theme) => { - document.documentElement.setAttribute(THEME_KEY, theme); store.set(theme); localStorage.setItem(THEME_KEY, theme);