Skip to content

Commit 9545dc8

Browse files
committed
Reload snippet on hash change.
Due to the way browsers work, if the user manually edits the URL to change the hash to another snippet ID and presses enter, the first time it will cause a hash change event (typically scrolls page to the matching anchor, without reloading page). Second time the user presses enter, since the hash has not changed, it will instead reload the page, and in our case reload the snippet. Therefore, before this PR, if the user manually edits the URL in browser to another snippet ID, first time it did not actually load it. After this PR, it will always try to load the snippet whose ID is in the URL box. I think this leads to a better user experience as it eliminates a possible confusing behavior. It's also more efficient than reloading entire page, since that is not necessary.
1 parent cc46515 commit 9545dc8

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

playground/playground.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,32 @@ func main() {
247247
})
248248
}()
249249
})
250+
251+
// Start watching for hashchange events, and reload snippet if it happens.
252+
dom.GetWindow().Top().AddEventListener("hashchange", false, func(event dom.Event) {
253+
event.PreventDefault()
254+
255+
if strings.HasPrefix(location.Hash, "#/") {
256+
id := location.Hash[2:]
257+
258+
req := xhr.NewRequest("GET", "http://"+snippetStoreHost+"/p/"+id)
259+
req.ResponseType = xhr.ArrayBuffer
260+
go func() {
261+
err := req.Send(nil)
262+
if err != nil || req.Status != 200 {
263+
scope.Apply(func() {
264+
scope.Set("output", []Line{Line{"type": "err", "content": `failed to load snippet "` + id + `"`}})
265+
})
266+
return
267+
}
268+
269+
data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
270+
scope.Apply(func() {
271+
scope.Set("code", string(data))
272+
})
273+
}()
274+
}
275+
})
250276
})
251277
}
252278

playground/playground.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64848,6 +64848,33 @@ $packages["main"] = (function() {
6484864848
/* */ case -1: } return; } }; $f.$blocking = true; return $f;
6484964849
}), []);
6485064850
}), funcType$1);
64851+
dom.GetWindow().Top().AddEventListener("hashchange", false, (function(event) {
64852+
var id$1, req$1;
64853+
event.PreventDefault();
64854+
if (strings.HasPrefix($internalize(location.URLUtils.Object.hash, $String), "#/")) {
64855+
id$1 = $internalize(location.URLUtils.Object.hash, $String).substring(2);
64856+
req$1 = xhr.NewRequest("GET", "http://snippets.gotools.org/p/" + id$1);
64857+
req$1.Object.responseType = $externalize("arraybuffer", $String);
64858+
$go((function($b) {
64859+
var $this = this, $args = arguments, $r, $s = 0, _r, err, data;
64860+
/* */ if($b !== $BLOCKING) { $nonblockingCall(); }; var $f = function() { s: while (true) { switch ($s) { case 0:
64861+
_r = req$1.Send($ifaceNil, $BLOCKING); /* */ $s = 1; case 1: if (_r && _r.$blocking) { _r = _r(); }
64862+
err = _r;
64863+
if (!($interfaceIsEqual(err, $ifaceNil)) || !((($parseInt(req$1.Object.status) >> 0) === 200))) {
64864+
scope.Apply((function() {
64865+
var _map, _key;
64866+
scope.Object.output = $externalize(new sliceType([(_map = new $Map(), _key = "type", _map[_key] = { k: _key, v: "err" }, _key = "content", _map[_key] = { k: _key, v: "failed to load snippet \"" + id$1 + "\"" }, _map)]), sliceType);
64867+
}));
64868+
return;
64869+
}
64870+
data = $assertType($internalize(new ($global.Uint8Array)(req$1.Object.response), $emptyInterface), sliceType$2);
64871+
scope.Apply((function() {
64872+
scope.Object.code = $externalize($bytesToString(data), $String);
64873+
}));
64874+
/* */ case -1: } return; } }; $f.$blocking = true; return $f;
64875+
}), []);
64876+
}
64877+
}));
6485164878
}));
6485264879
};
6485364880
setupEnvironment = function(scope) {

0 commit comments

Comments
 (0)