Skip to content

Commit

Permalink
Improve the link sharing widget in the workbench
Browse files Browse the repository at this point in the history
(we also now support hash based links in the workbench)
  • Loading branch information
sgenoud committed Jan 10, 2024
1 parent 7ad25f5 commit 653ad93
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 209 deletions.
212 changes: 44 additions & 168 deletions packages/studio/src/LinkWidget.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import React, { useRef, useState, useEffect, useCallback } from "react";


import styled from "styled-components";
import { useParams } from "react-router-dom";
import axios from "axios";
import { ellipsis } from "polished";

import builderAPI from "./utils/builderAPI";
import loadCode from "./utils/loadCode"
import loadCode from "./utils/loadCode";
import saveShape from "./utils/saveShape";

import { Button } from "./components/Button.jsx";
import StandardUI from "./components/StandardUI.jsx";
import downloadCode from './utils/downloadCode.js'
import { LinkEditor } from "./components/LinkEditor.jsx";
import downloadCode from "./utils/downloadCode.js";

const CenterInfo = styled.div`
background-color: var(--bg-color);
Expand Down Expand Up @@ -69,30 +67,30 @@ export default function LinkWidget() {
})
.catch((e) => {
console.error(e);
setError({ type: 'url' });
setError({ type: "url" });
});
}, [shapeURL]);

useEffect(() => {
const loadCodeFromParam = async () => {
const hash = window.location.hash.substring(1);
const hashParams = new URLSearchParams(hash);
if (!hashParams.has('code')) {
setError({ type: 'code' })
if (!hashParams.has("code")) {
setError({ type: "code" });
return;
}
try {
const rawCode = hashParams.get('code');
setCode(await loadCode(rawCode))
const rawCode = hashParams.get("code");
setCode(await loadCode(rawCode));
setRawCode(rawCode);
readyToBuild.current = true;
} catch(e) {
setError({ type: 'code' })
} catch (e) {
setError({ type: "code" });
}
}
};

if (shapeURL) return;
loadCodeFromParam()
loadCodeFromParam();
}, [shapeURL]);

useEffect(() => {
Expand All @@ -103,7 +101,7 @@ export default function LinkWidget() {
.then((defaultParams) => {
setDefaultParams(defaultParams);
})
.catch(() => setError({ type: 'url' }));
.catch(() => setError({ type: "url" }));

return () => {
paramsToCompute.current = null;
Expand Down Expand Up @@ -146,56 +144,56 @@ export default function LinkWidget() {
return (
<CenterInfo>
<h4>Error</h4>
{ error.type === 'url' &&
{error.type === "url" && (
<>
<p>
We could not find a shape to render{" "}
<a href={decodeURIComponent(shapeURL)}>here</a>.
</p>
<p>Are you sure that the link is pointing to a raw javascript file?</p>
<p>
Are you sure that the link is pointing to a raw javascript file?
</p>
</>
}
{ error.type === 'code' &&
)}
{error.type === "code" && (
<>
<p>
<code>#code</code> parameter is missing or could not be rendered.
</p>
</>
}
)}
</CenterInfo>
);

const downloadPrompt = (e) => {
e.preventDefault()
const shapeName = computedShapes?.length === 1 ? `${computedShapes[0].name}` : null
return downloadCode(code, shapeName)
}
e.preventDefault();
const shapeName =
computedShapes?.length === 1 ? `${computedShapes[0].name}` : null;
return downloadCode(code, shapeName);
};

const url = new URL(window.location.href);
url.pathname = "/workbench";
url.hash = ''
url.hash = "";
url.search = "";
if (shapeURL) {
url.searchParams.set("from-url", shapeURL);
} else {
url.searchParams.set("code", rawCode)
const hashParams = new URLSearchParams();
hashParams.set("code", rawCode);
url.hash = hashParams.toString();
}
const workbenchUrl = url.toString();

const searchParams = new URLSearchParams(window.location.search);


const DownloadLink = () => {
if (shapeURL) {
return <a href={shapeURL} target="_blank" rel="noopener noreferrer">
{" "}
source{" "}
</a>;
} else {
return <FooterButton onClick={downloadPrompt}>
{" "}
source{" "}
</FooterButton>
}
let downloadURL = shapeURL;
if (!downloadURL) {
downloadURL = URL.createObjectURL(
new Blob([code], {
type: "application/javascript",
})
);
}

return (
Expand All @@ -217,49 +215,25 @@ export default function LinkWidget() {
canSave={geometryHasBeenComputed}
/>
<AdditionalInfo>
<a
href="https://replicad.xyz"
target="_blank"
rel="noopener noreferrer"
>
<a href="https://replicad.xyz" target="_blank">
{" "}
replicad
</a>{" "}
|
<a href={workbenchUrl} target="_blank" rel="noopener noreferrer">
<a href={workbenchUrl} target="_blank">
{" "}
edit{" "}
</a>
|
<DownloadLink />
<a href={downloadURL} target="_blank">
{" "}
source{" "}
</a>
</AdditionalInfo>
</>
);
}

const Subtitle = styled.h4`
margin-bottom: 0.5em;
margin-top: 2em;
`;

const Inline = styled.div`
display: flex;
align-items: center;
& > :not(:last-child) {
margin-right: 1em;
}
max-width: 100%;
`;

const Input = styled.input`
width: 100%;
max-width: 800px;
`;

const Url = styled.a`
${ellipsis("100%")};
`;

const Content = styled.div`
display: flex;
position: relative;
Expand All @@ -268,110 +242,12 @@ const Content = styled.div`
max-width: 600px;
`;

const Options = styled.div`
display: flex;
flex-wrap: wrap;
gap: 2em;
& label {
margin-left: 0.3em;
}
`;
const FooterButton = styled.button`
cursor: pointer;
border: none;
font-weight: 300;
color: var(--color-primary);
background-color: transparent;
`;

export function MakeLink() {
const [inputVal, setInputVal] = useState("");
const [disableAutoPosition, setDisableAutoPosition] = useState(false);
const [disableDamping, setDisableDamping] = useState(false);
const [hideGrid, setHideGrid] = useState(false);
const [expandParametersPanel, setExpandParametersPanel] = useState(false);

let link = null;
if (inputVal) {
const url = new URL(window.location.href);
url.pathname = `/share/${encodeURIComponent(inputVal)}`;
disableAutoPosition &&
url.searchParams.set("disable-auto-position", "true");
disableDamping && url.searchParams.set("disable-damping", "true");
hideGrid && url.searchParams.set("hide-grid", "true");
expandParametersPanel && url.searchParams.set("params", "true");
link = url.toString();
}

return (
<CenterInfo>
<Content>
<h4>Create a link to a replicad file</h4>
<div>
<label htmlFor="url-input">
Enter the link to a <strong>raw</strong> javascript file.
</label>
<Input
id="url-input"
value={inputVal}
onChange={(e) => setInputVal(e.target.value)}
/>
</div>

<Subtitle>Options</Subtitle>
<Options>
<span>
<input
id="disable-auto-position"
type="checkbox"
checked={!disableAutoPosition}
onChange={(e) => setDisableAutoPosition(!e.target.checked)}
/>
<label htmlFor="disable-auto-position">Auto position</label>
</span>
<span>
<input
id="disable-damping"
type="checkbox"
checked={!disableDamping}
onChange={(e) => setDisableDamping(!e.target.checked)}
/>
<label htmlFor="disable-damping">Damping</label>
</span>
<span>
<input
id="hide-grid"
type="checkbox"
checked={!hideGrid}
onChange={(e) => setHideGrid(!e.target.checked)}
/>
<label htmlFor="hide-grid">Grid</label>
</span>
<span>
<input
id="expand-params"
type="checkbox"
checked={expandParametersPanel}
onChange={(e) => setExpandParametersPanel(e.target.checked)}
/>
<label htmlFor="expand-params">Parameters panel open</label>
</span>
</Options>

{link && (
<>
<Subtitle>Your link</Subtitle>
<Inline>
<Url href={link} rel="noopener noreferrer" target="_blank">
{link}
</Url>
<Button onClick={() => navigator.clipboard.writeText(link)} solid>
Copy
</Button>
</Inline>
</>
)}
<LinkEditor />
</Content>
</CenterInfo>
);
Expand Down
Loading

0 comments on commit 653ad93

Please sign in to comment.