Skip to content

Commit 1e8fdc9

Browse files
committed
added ability to upgrade state in UrlHashBinding.updateFromUrlHash for backwards compatibilty
1 parent 3428902 commit 1e8fdc9

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/ui/default_viewer_setup.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,22 @@ export function setupDefaultViewer() {
161161
hashBinding.parseError;
162162
}),
163163
);
164-
hashBinding.updateFromUrlHash();
164+
hashBinding.updateFromUrlHash((state) => {
165+
// convert graphene state timestamp to layer timestamp
166+
const fixTimestamp = (layer: any) => {
167+
if (layer.source?.state?.timestamp) {
168+
layer.timestamp = layer.source.state.timestamp;
169+
layer.source.state.timestamp = undefined;
170+
}
171+
};
172+
if (state.layers) {
173+
const layers = Array.isArray(state.layers)
174+
? state.layers
175+
: Object.values(state.layers);
176+
layers.map(fixTimestamp);
177+
}
178+
return state;
179+
});
165180
viewer.registerDisposer(bindTitle(viewer.title));
166181

167182
bindDefaultCopyHandler(viewer);

src/ui/url_hash_binding.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class UrlHashBinding extends RefCounted {
113113
* Sets the current state to match the URL hash. If it is desired to initialize the state based
114114
* on the URL hash, then this should be called immediately after construction.
115115
*/
116-
updateFromUrlHash() {
116+
updateFromUrlHash(upgradeState: (a: any) => any = (x) => x) {
117117
try {
118118
let s = location.href.replace(/^[^#]+/, "");
119119
if (s === "" || s === "#" || s === "#!") {
@@ -160,7 +160,7 @@ export class UrlHashBinding extends RefCounted {
160160
this.root.reset();
161161
const state = urlSafeParse(s);
162162
verifyObject(state);
163-
this.root.restoreState(state);
163+
this.root.restoreState(upgradeState(state));
164164
} else {
165165
throw new Error(
166166
`URL hash is expected to be of the form "#!{...}" or "#!+{...}".`,

0 commit comments

Comments
 (0)