Skip to content

Commit

Permalink
export JSON button
Browse files Browse the repository at this point in the history
  • Loading branch information
theogutt committed Aug 12, 2024
1 parent aea304e commit 3b75246
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "../../../common.css";
import "./info-view.css";
import { useInstrumentContext } from "../../../Contexts/InstrumentContext";
import { useRaysContext } from "../../../Contexts/RaysContext";
import ExportJSONButton from "./export-json-button/ExportJSONButton";

const InfoView = () => {
const { instrument, setInstrument } = useInstrumentContext();
Expand Down Expand Up @@ -53,6 +54,7 @@ const InfoView = () => {
{rays.vmin}
</p>
) : null}
<ExportJSONButton />
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";
import "../../../../common.css";
import "./export-json-button.css";
import { useInstrumentContext } from "../../../../Contexts/InstrumentContext";

const ExportJSONButton = () => {
const { instrument, setInstrument } = useInstrumentContext();

const handleButtonClick = () => {
const comps = instrument.components;
let element = document.createElement("a");
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," +
encodeURIComponent(JSON.stringify(comps, null, 2))
);
element.setAttribute("download", "components.json");
element.style.display = "none";
document.body.appendChild(element);

element.click();

document.body.removeChild(element);
};

return (
<div id="export-json-button" className="">
<button onClick={handleButtonClick}>Export JSON</button>
</div>
);
};

export default ExportJSONButton;

0 comments on commit 3b75246

Please sign in to comment.