Skip to content

Commit 2a0aae0

Browse files
Merge pull request #12833 from CesiumGS/gist-urls
Add support for loading GH Gist urls
2 parents 8126080 + 642bbc3 commit 2a0aae0

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

packages/sandcastle/src/App.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,18 @@ function App() {
251251
const [galleryLoaded, setGalleryLoaded] = useState(false);
252252

253253
const [consoleMessages, setConsoleMessages] = useState<ConsoleMessage[]>([]);
254-
function appendConsole(type: ConsoleMessageType, message: string) {
255-
setConsoleMessages((prevConsoleMessages) => [
256-
...prevConsoleMessages,
257-
{ type, message, id: crypto.randomUUID() },
258-
]);
259-
if (!consoleExpanded && type !== "log") {
260-
rightSideRef.current?.toggleExpanded();
261-
}
262-
}
254+
const appendConsole = useCallback(
255+
function appendConsole(type: ConsoleMessageType, message: string) {
256+
setConsoleMessages((prevConsoleMessages) => [
257+
...prevConsoleMessages,
258+
{ type, message, id: crypto.randomUUID() },
259+
]);
260+
if (!consoleExpanded && type !== "log") {
261+
rightSideRef.current?.toggleExpanded();
262+
}
263+
},
264+
[consoleExpanded],
265+
);
263266

264267
function resetConsole() {
265268
// the console should only be cleared by the Bucket when the viewer page
@@ -373,6 +376,26 @@ function App() {
373376
html: data.html,
374377
});
375378
setReadyForViewer(true);
379+
} else if (searchParams.has("gist")) {
380+
// This is currently for legacy support only so old links on GH or the forums don't break
381+
fetch(`https://api.github.com/gists/${searchParams.get("gist")}`)
382+
.then((data) => data.json())
383+
.then(function (data) {
384+
const files = data.files;
385+
const code = files["Cesium-Sandcastle.js"].content;
386+
const html =
387+
files["Cesium-Sandcastle.html"]?.content ?? defaultHtmlCode;
388+
dispatch({ type: "setAndRun", code: code, html: html });
389+
setTitle("Gist Import");
390+
setReadyForViewer(true);
391+
})
392+
.catch(function (error) {
393+
appendConsole(
394+
"error",
395+
`Unable to GET gist from GitHub API. This could be due to too many requests from your IP or an incorrect id. Try again in an hour or copy and paste the code from the gist: https://gist.github.com/${searchParams.get("gist")}`,
396+
);
397+
console.log(error);
398+
});
376399
} else if (searchParams.has("src")) {
377400
const legacyId = searchParams.get("src");
378401
if (!legacyId) {
@@ -400,7 +423,7 @@ function App() {
400423
}
401424
}
402425
},
403-
[galleryLoaded, legacyIdMap, loadGalleryItem],
426+
[galleryLoaded, legacyIdMap, loadGalleryItem, appendConsole],
404427
);
405428

406429
useEffect(() => loadFromUrl(), [galleryLoaded, galleryItems, loadFromUrl]);

0 commit comments

Comments
 (0)