dmitri.shuralyov.com/app/changes/frontend

Handle escape keydown even if non-body selected.

This way, pressing escape works even if you have a link selected (which
is often the case after clicking on an anchor). We avoid processing
unwanted keydown events because they'll be default prevented.

Add "keydown" event listener to body rather than document, because it's
sufficient and for consistency.
dmitshur committed 6 years ago commit 5640e7c55b341c0556e7904d351ab82d58727531
Showing partial commit. Full Commit
Collapse all
frontend/main.go
@@ -48,10 +48,12 @@ func main() {
		panic(fmt.Errorf("internal error: unexpected document.ReadyState value: %v", readyState))
	}
}

func setup(f *frontend) {
	setupScroll()

	if !state.DisableReactions {
		reactionsService := ChangeReactions{Change: f.cs}
		reactionsmenu.Setup(state.RepoSpec, reactionsService, state.CurrentUser)
	}
}
frontend/scroll.go
@@ -8,11 +8,11 @@ import (
	"github.com/gopherjs/gopherjs/js"
	"github.com/shurcooL/go/gopherjs_http/jsutil"
	"honnef.co/go/js/dom"
)

func init() {
func setupScroll() {
	js.Global.Set("AnchorScroll", jsutil.Wrap(AnchorScroll))

	processHashSet := func() {
		// Scroll to hash target.
		targetID := strings.TrimPrefix(dom.GetWindow().Location().Hash, "#")
@@ -36,18 +36,14 @@ func init() {
		processHashSet()

		event.PreventDefault()
	})

	document.AddEventListener("keydown", false, func(event dom.Event) {
	document.Body().AddEventListener("keydown", false, func(event dom.Event) {
		if event.DefaultPrevented() {
			return
		}
		// Ignore when some element other than body has focus (it means the user is typing elsewhere).
		if !event.Target().IsEqualNode(document.Body()) {
			return
		}

		switch ke := event.(*dom.KeyboardEvent); {
		// Escape.
		case ke.KeyCode == 27 && !ke.Repeat && !ke.CtrlKey && !ke.AltKey && !ke.MetaKey && !ke.ShiftKey:
			if strings.TrimPrefix(dom.GetWindow().Location().Hash, "#") == "" {