Skip to content

Commit

Permalink
run snip in shared with reset
Browse files Browse the repository at this point in the history
  • Loading branch information
wandyezj committed Sep 19, 2024
1 parent 15fa9fe commit 8f711d7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports = async (env, options) => {
settings: "./src/settings.tsx",
blocks: "./src/blocks.tsx",
test: "./src/test.ts",
shared: "./src/shared.ts",
shared: "./src/shared.tsx",
},
output: {
// Add contenthash to cache bust on CDN
Expand Down
7 changes: 7 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function loadJs(js: string) {

async function runSnip() {
const goBack = window.location.hash === "#back";
const goBackSharedReset = window.location.hash === "#reset";

const snip = await getCurrentSnip();
console.log("snip", snip);
Expand All @@ -80,6 +81,7 @@ async function runSnip() {
const libraries = snip.files["libraries"].content;
const css = snip.files["css"].content;
let html = snip.files["html"].content;

// Add back button to top of html
if (goBack) {
// window.location.href='./edit.html';
Expand All @@ -88,6 +90,11 @@ async function runSnip() {
html = `${backButtonHtml} ${refreshButtonHtml}<br/><br/>${html}`;
}

if (goBackSharedReset) {
const backButtonHtml = `<button onclick="window.location.href='./shared.html#reset'">Reset</button>`;
html = `${backButtonHtml}<br/><br/>${html}`;
}

// compile TypeScript
const ts = snip.files["typescript"].content;
const { js, issues } = compileCode(ts);
Expand Down
5 changes: 4 additions & 1 deletion src/shared.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
</head>
<body>
<div>
<h1>Shared</h1>
<div id="container">
<h1>Startup</h1>
<p>Loading...</p>
</div>
</div>
</body>
</html>
15 changes: 12 additions & 3 deletions src/shared.ts → src/shared.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Shared runtime, will be used for snips that should run in the background.

import { setupOffice } from "./core/setupOffice";
import React from "react";
import { createRoot } from "react-dom/client";
import { App } from "./shared/App";
import { run } from "./shared/run";

// Some rules for snips to run in the background
// must
// - only allow one to run in the background
// - be embedded in the document
// - only have default library references

// Want a button to go back from currently loaded
// Want a button to load a new one.

Expand All @@ -17,8 +20,14 @@ async function setup() {
// After the first time the add in is opened it will automatically load
Office.addin.setStartupBehavior(Office.StartupBehavior.load);

// redirect to edit and allow back
window.location.href = "./run.html#back";
if (window.location.hash === "#reset") {
const container = document.getElementById("container")!;
const root = createRoot(container);
root.render(<App />);
} else {
run();
}

// back will act as reload in this case.
}

Expand Down
14 changes: 14 additions & 0 deletions src/shared/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
import { Button, FluentProvider, webLightTheme } from "@fluentui/react-components";
import { run } from "./run";

/**
* The top level application component.
*/
export function App() {
return (
<FluentProvider theme={webLightTheme}>
<Button onClick={run}>Start</Button>
</FluentProvider>
);
}
7 changes: 7 additions & 0 deletions src/shared/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Redirect to edit view, with reset that will be caught on this return.
*/
export function run() {
// redirect to edit and allow back
window.location.href = "./run.html#reset";
}

0 comments on commit 8f711d7

Please sign in to comment.