Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ability to upgrade state in UrlHashBinding.updateFromUrlHash fo…
Browse files Browse the repository at this point in the history
…r backwards compatibilty
chrisj committed Dec 5, 2024
1 parent 3428902 commit 1e8fdc9
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/ui/default_viewer_setup.ts
Original file line number Diff line number Diff line change
@@ -161,7 +161,22 @@ export function setupDefaultViewer() {
hashBinding.parseError;
}),
);
hashBinding.updateFromUrlHash();
hashBinding.updateFromUrlHash((state) => {
// convert graphene state timestamp to layer timestamp
const fixTimestamp = (layer: any) => {
if (layer.source?.state?.timestamp) {
layer.timestamp = layer.source.state.timestamp;
layer.source.state.timestamp = undefined;
}
};
if (state.layers) {
const layers = Array.isArray(state.layers)
? state.layers
: Object.values(state.layers);
layers.map(fixTimestamp);
}
return state;
});
viewer.registerDisposer(bindTitle(viewer.title));

bindDefaultCopyHandler(viewer);
4 changes: 2 additions & 2 deletions src/ui/url_hash_binding.ts
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ export class UrlHashBinding extends RefCounted {
* Sets the current state to match the URL hash. If it is desired to initialize the state based
* on the URL hash, then this should be called immediately after construction.
*/
updateFromUrlHash() {
updateFromUrlHash(upgradeState: (a: any) => any = (x) => x) {
try {
let s = location.href.replace(/^[^#]+/, "");
if (s === "" || s === "#" || s === "#!") {
@@ -160,7 +160,7 @@ export class UrlHashBinding extends RefCounted {
this.root.reset();
const state = urlSafeParse(s);
verifyObject(state);
this.root.restoreState(state);
this.root.restoreState(upgradeState(state));
} else {
throw new Error(
`URL hash is expected to be of the form "#!{...}" or "#!+{...}".`,

0 comments on commit 1e8fdc9

Please sign in to comment.