diff --git a/src/app/bookmarks/bookmarks.tsx b/src/app/bookmarks/bookmarks.tsx index 9e675ca61..31e31953d 100644 --- a/src/app/bookmarks/bookmarks.tsx +++ b/src/app/bookmarks/bookmarks.tsx @@ -22,6 +22,59 @@ type BookmarkProps = { bookmark: BookmarkType; }; +class BookmarkKeybindings extends React.Component<{}, {}> { + @boundMethod + componentDidMount(): void { + let keybindManager = GlobalModel.keybindManager; + let bookmarksModel = GlobalModel.bookmarksModel; + keybindManager.registerKeybinding("mainview", "bookmarks", "generic:cancel", (waveEvent) => { + bookmarksModel.handleUserClose(); + return true; + }); + keybindManager.registerKeybinding("mainview", "bookmarks", "generic:deleteItem", (waveEvent) => { + bookmarksModel.handleUserDelete(); + return true; + }); + keybindManager.registerKeybinding("mainview", "bookmarks", "generic:selectAbove", (waveEvent) => { + bookmarksModel.handleUserNavigate(-1); + return true; + }); + keybindManager.registerKeybinding("mainview", "bookmarks", "generic:selectBelow", (waveEvent) => { + bookmarksModel.handleUserNavigate(1); + return true; + }); + keybindManager.registerKeybinding("mainview", "bookmarks", "generic:selectPageAbove", (waveEvent) => { + bookmarksModel.handleUserNavigate(-10); + return true; + }); + keybindManager.registerKeybinding("mainview", "bookmarks", "generic:selectPageBelow", (waveEvent) => { + bookmarksModel.handleUserNavigate(10); + return true; + }); + keybindManager.registerKeybinding("mainview", "bookmarks", "generic:confirm", (waveEvent) => { + bookmarksModel.handleUserConfirm(); + return true; + }); + keybindManager.registerKeybinding("mainview", "bookmarks", "bookmarks:edit", (waveEvent) => { + bookmarksModel.handleUserEdit(); + return true; + }); + keybindManager.registerKeybinding("mainview", "bookmarks", "bookmarks:copy", (waveEvent) => { + bookmarksModel.handleUserCopy(); + return true; + }); + } + + @boundMethod + componentWillUnmount() { + GlobalModel.keybindManager.unregisterDomain("bookmarks"); + } + + render() { + return null; + } +} + @mobxReact.observer class Bookmark extends React.Component { @boundMethod @@ -194,6 +247,9 @@ class BookmarksView extends React.Component<{}, {}> { let bookmark: BookmarkType = null; return ( + + +
diff --git a/src/models/bookmarks.ts b/src/models/bookmarks.ts index a5e58f55b..a4b06d23c 100644 --- a/src/models/bookmarks.ts +++ b/src/models/bookmarks.ts @@ -205,80 +205,76 @@ class BookmarksModel { })(); } - handleDocKeyDown(e: any): void { - const waveEvent = adaptFromReactOrNativeKeyEvent(e); - if (checkKeyPressed(waveEvent, "Escape")) { - e.preventDefault(); - if (this.editingBookmark.get() != null) { - this.cancelEdit(); - return; - } - this.closeView(); + handleUserClose() { + if (this.editingBookmark.get() != null) { + this.cancelEdit(); return; } + this.closeView(); + } + + handleUserDelete() { if (this.editingBookmark.get() != null) { return; } - if (checkKeyPressed(waveEvent, "Backspace") || checkKeyPressed(waveEvent, "Delete")) { - if (this.activeBookmark.get() == null) { - return; - } - e.preventDefault(); - this.handleDeleteBookmark(this.activeBookmark.get()); + if (this.activeBookmark.get() == null) { return; } + this.handleDeleteBookmark(this.activeBookmark.get()); + } - if ( - checkKeyPressed(waveEvent, "ArrowUp") || - checkKeyPressed(waveEvent, "ArrowDown") || - checkKeyPressed(waveEvent, "PageUp") || - checkKeyPressed(waveEvent, "PageDown") - ) { - e.preventDefault(); - if (this.bookmarks.length == 0) { - return; - } - let newPos = 0; // if active is null, then newPos will be 0 (select the first) - if (this.activeBookmark.get() != null) { - let amtMap = { ArrowUp: -1, ArrowDown: 1, PageUp: -10, PageDown: 10 }; - let amt = amtMap[e.code]; - let curIdx = this.getBookmarkPos(this.activeBookmark.get()); - newPos = curIdx + amt; - if (newPos < 0) { - newPos = 0; - } - if (newPos >= this.bookmarks.length) { - newPos = this.bookmarks.length - 1; - } - } - let bm = this.bookmarks[newPos]; - mobx.action(() => { - this.activeBookmark.set(bm.bookmarkid); - })(); + handleUserNavigate(amt: number) { + if (this.editingBookmark.get() != null) { return; } - if (checkKeyPressed(waveEvent, "Enter")) { - if (this.activeBookmark.get() == null) { - return; - } - this.useBookmark(this.activeBookmark.get()); + if (this.bookmarks.length == 0) { return; } - if (checkKeyPressed(waveEvent, "e")) { - if (this.activeBookmark.get() == null) { - return; + let newPos = 0; // if active is null, then newPos will be 0 (select the first) + if (this.activeBookmark.get() != null) { + let curIdx = this.getBookmarkPos(this.activeBookmark.get()); + newPos = curIdx + amt; + if (newPos < 0) { + newPos = 0; } - e.preventDefault(); - this.handleEditBookmark(this.activeBookmark.get()); + if (newPos >= this.bookmarks.length) { + newPos = this.bookmarks.length - 1; + } + } + let bm = this.bookmarks[newPos]; + mobx.action(() => { + this.activeBookmark.set(bm.bookmarkid); + })(); + } + + handleUserConfirm() { + if (this.editingBookmark.get() != null) { return; } - if (checkKeyPressed(waveEvent, "c")) { - if (this.activeBookmark.get() == null) { - return; - } - e.preventDefault(); - this.handleCopyBookmark(this.activeBookmark.get()); + if (this.activeBookmark.get() == null) { + return; } + this.useBookmark(this.activeBookmark.get()); + } + + handleUserEdit() { + if (this.editingBookmark.get() != null) { + return; + } + if (this.activeBookmark.get() == null) { + return; + } + this.handleEditBookmark(this.activeBookmark.get()); + } + + handleUserCopy() { + if (this.editingBookmark.get() != null) { + return; + } + if (this.activeBookmark.get() == null) { + return; + } + this.handleCopyBookmark(this.activeBookmark.get()); } } diff --git a/src/models/model.ts b/src/models/model.ts index 8d35ef550..52bc48c43 100644 --- a/src/models/model.ts +++ b/src/models/model.ts @@ -491,9 +491,7 @@ class Model { this.modalsModel.popModal(); return; } - if (this.activeMainView.get() == "bookmarks") { - this.bookmarksModel.handleDocKeyDown(e); - } + this.keybindManager.processKeyEvent(e, waveEvent); if (this.activeMainView.get() == "history") { this.historyViewModel.handleDocKeyDown(e); } @@ -535,7 +533,6 @@ class Model { } } } - this.keybindManager.processKeyEvent(e, waveEvent); } deleteActiveLine(): boolean { diff --git a/src/util/keyutil.ts b/src/util/keyutil.ts index 4da4fecf1..f4c50cc4f 100644 --- a/src/util/keyutil.ts +++ b/src/util/keyutil.ts @@ -38,7 +38,7 @@ type Keybind = { commandStr: string; }; -const KeybindLevels = ["system", "modal", "app", "pane", "plugin"]; +const KeybindLevels = ["system", "modal", "app", "mainview", "pane", "plugin"]; class KeybindManager { domainCallbacks: Map; @@ -159,8 +159,6 @@ class KeybindManager { let curDomainCallback = this.domainCallbacks.get(curKeybind.domain); if (curDomainCallback != null) { shouldReturn = curDomainCallback(event); - } else { - console.log("domain callback for ", curKeybind.domain, " is null. This should never happen"); } } if (shouldRunCommand) {