Skip to content

Commit

Permalink
Add option to control when URL hash is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
wch committed May 7, 2024
1 parent ae298ae commit 3c1f83e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
9 changes: 8 additions & 1 deletion site_template/editor/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand All @@ -18,6 +18,13 @@
allowCodeUrl: true,
allowGistUrl: true,
allowExampleUrl: true,
// This option causes shinylive to update the URL hash when the user
// clicks on the re-run button in the editor. It is false by default.
// It should be set to true only when the editor and viewer are used
// in a full-window configuration. If you are using the editor and
// viewer embedded in a larger page, it does not make sense to set
// this to true.
updateUrlHashOnRerun: true,
};

const appRoot = document.getElementById("root");
Expand Down
24 changes: 18 additions & 6 deletions site_template/examples/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand All @@ -15,11 +15,23 @@
// reasons, if you enable any of these, then this site should be hosted on
// a separate domain or subdomain from other content. Otherwise the
// running of arbitrary code could be used, for example, to steal cookies.
runApp(appRoot, "examples-editor-terminal-viewer", {
allowCodeUrl: true,
allowGistUrl: true,
allowExampleUrl: true,
}, "{{APP_ENGINE}}");
runApp(
appRoot,
"examples-editor-terminal-viewer",
{
allowCodeUrl: true,
allowGistUrl: true,
allowExampleUrl: true,
// This option causes shinylive to update the URL hash when the user
// clicks on the re-run button in the editor. It is false by default.
// It should be set to true only when the editor and viewer are used
// in a full-window configuration. If you are using the editor and
// viewer embedded in a larger page, it does not make sense to set
// this to true.
updateUrlHashOnRerun: true,
},
"{{APP_ENGINE}}",
);
</script>
<link rel="stylesheet" href="../shinylive/style-resets.css" />
<link rel="stylesheet" href="../shinylive/shinylive.css" />
Expand Down
9 changes: 9 additions & 0 deletions src/Components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ type AppOptions = {

// In Viewer-only mode, should the header bar be shown?
showHeaderBar?: boolean;

// When the app is re-run from the Editor, should the URL hash be updated with
// the encoded version of the app?
updateUrlHashOnRerun?: boolean;
};

export type ProxyHandle = PyodideProxyHandle | WebRProxyHandle;
Expand Down Expand Up @@ -353,6 +357,7 @@ export function App({
file.name === "app.R" ||
file.name === "server.R",
)}
updateUrlHashOnRerun={appOptions.updateUrlHashOnRerun}
appEngine={appEngine}
/>
</React.Suspense>
Expand Down Expand Up @@ -400,6 +405,7 @@ export function App({
file.name === "app.R" ||
file.name === "server.R",
)}
updateUrlHashOnRerun={appOptions.updateUrlHashOnRerun}
appEngine={appEngine}
/>
</React.Suspense>
Expand Down Expand Up @@ -433,6 +439,7 @@ export function App({
terminalMethods={terminalMethods}
utilityMethods={utilityMethods}
runOnLoad={false}
updateUrlHashOnRerun={appOptions.updateUrlHashOnRerun}
appEngine={appEngine}
/>
</React.Suspense>
Expand All @@ -458,6 +465,7 @@ export function App({
lineNumbers={false}
showHeaderBar={false}
floatingButtons={true}
updateUrlHashOnRerun={appOptions.updateUrlHashOnRerun}
appEngine={appEngine}
/>
</React.Suspense>
Expand Down Expand Up @@ -495,6 +503,7 @@ export function App({
terminalMethods={terminalMethods}
utilityMethods={utilityMethods}
viewerMethods={viewerMethods}
updateUrlHashOnRerun={appOptions.updateUrlHashOnRerun}
appEngine={appEngine}
/>
</React.Suspense>
Expand Down
8 changes: 6 additions & 2 deletions src/Components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default function Editor({
lineNumbers = true,
showHeaderBar = true,
floatingButtons = false,
updateUrlHashOnRerun = false,
appEngine,
}: {
currentFilesFromApp: FileContent[];
Expand All @@ -97,6 +98,7 @@ export default function Editor({
lineNumbers?: boolean;
showHeaderBar?: boolean;
floatingButtons?: boolean;
updateUrlHashOnRerun?: boolean;
appEngine: AppEngine;
}) {
// In the future, instead of directly instantiating the PyrightClient, it
Expand Down Expand Up @@ -188,8 +190,10 @@ export default function Editor({
syncActiveFileState();
const fileContents = editorFilesToFileContents(files);

// eslint-disable-next-line @typescript-eslint/no-floating-promises
updateBrowserUrlHash(fileContents);
if (updateUrlHashOnRerun) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
updateBrowserUrlHash(fileContents);
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
Expand Down

0 comments on commit 3c1f83e

Please sign in to comment.