-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a button that copies shareable URL into the clipboard (#44)
This provides a simple way to share state between two instances of the tool. It captures most of the current state we care about persisting, with the exception of selection because that is timing/order sensitive and may need some related cleanup before we use it as a shareable state. The approach defines a dataclass for serializing/deserializing the state from the URL's hash. I don't love it because it requires some boilerplate in the class itself and related book-keeping elsewhere (in `state.tsx`) but I think it's probably good enough for now. Open to better ideas though! I encoded the state using `URLSearchParams` to sanitize the strings for usage in a URL. Alternatively, we could use a base64 encoding of the JSON string which could also simplify the (de)serialization code, though will likely be longer than the current encoding. Closes #18 --------- Co-authored-by: Ashley Anderson <[email protected]>
- Loading branch information
1 parent
1154fa4
commit 865a508
Showing
3 changed files
with
120 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Vector3 } from "three"; | ||
|
||
export const DEFAULT_ZARR_URL = new URL( | ||
"https://sci-imaging-vis-public-demo-data.s3.us-west-2.amazonaws.com" + | ||
"/points-web-viewer/sparse-zarr-v2/ZSNS001_tracks_bundle.zarr", | ||
); | ||
|
||
const HASH_KEY = "viewerState"; | ||
|
||
// Clears the hash from the window's URL without triggering a hashchange | ||
// event or an update to history. | ||
export function clearUrlHash() { | ||
// Use this instead of setting window.location.hash to avoid triggering | ||
// a hashchange event (which would reset the state again). | ||
window.history.replaceState(null, "", `${window.location.pathname}${window.location.search}`); | ||
} | ||
|
||
// Encapsulates all the persistent state in the viewer (e.g. that can be serialized and shared). | ||
export class ViewerState { | ||
dataUrl: URL; | ||
curTime: number; | ||
cameraPosition: Vector3; | ||
cameraTarget: Vector3; | ||
|
||
constructor( | ||
dataUrl: URL = DEFAULT_ZARR_URL, | ||
curTime: number = 0, | ||
// Default position and target from interacting with ZSNS001. | ||
cameraPosition: Vector3 = new Vector3(500, 500, -1250), | ||
cameraTarget: Vector3 = new Vector3(500, 500, 250), | ||
) { | ||
this.dataUrl = dataUrl; | ||
this.curTime = curTime; | ||
this.cameraPosition = cameraPosition; | ||
this.cameraTarget = cameraTarget; | ||
} | ||
|
||
toUrlHash(): string { | ||
// Use SearchParams to sanitize serialized string values for URL. | ||
const searchParams = new URLSearchParams(); | ||
searchParams.append(HASH_KEY, JSON.stringify(this)); | ||
return searchParams.toString(); | ||
} | ||
|
||
static fromUrlHash(urlHash: string): ViewerState { | ||
console.debug("getting state from hash: %s", urlHash); | ||
// Remove the # from the hash to get the fragment. | ||
const searchParams = new URLSearchParams(urlHash.slice(1)); | ||
if (searchParams.has(HASH_KEY)) { | ||
return JSON.parse(searchParams.get(HASH_KEY)!); | ||
} | ||
if (urlHash.length > 0) { | ||
console.error("failed to find state key in hash: %s", urlHash); | ||
} | ||
return new ViewerState(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters