[MD Bookmarks] Handle Cut/Copy/Paste natively.
This CL changes the Cut/Copy/Paste shortcuts to be triggered through the
web events so that the browser menu's edit buttons will work in the
bookmark manager.
Bug: 807104
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I57dfa94825b450de6d69f45f9a36085bd43775c3
Reviewed-on: https://chromium-review.googlesource.com/892549
Reviewed-by: Tim Sergeant <[email protected]>
Reviewed-by: Patti <[email protected]>
Commit-Queue: calamity <[email protected]>
Cr-Commit-Position: refs/heads/master@{#533519}
diff --git a/chrome/browser/resources/md_bookmarks/command_manager.js b/chrome/browser/resources/md_bookmarks/command_manager.js
index 668a44975..900da62 100644
--- a/chrome/browser/resources/md_bookmarks/command_manager.js
+++ b/chrome/browser/resources/md_bookmarks/command_manager.js
@@ -58,21 +58,6 @@
});
this.updateFromStore();
- /** @private {function(!Event)} */
- this.boundOnOpenCommandMenu_ = this.onOpenCommandMenu_.bind(this);
- document.addEventListener(
- 'open-command-menu', this.boundOnOpenCommandMenu_);
-
- /** @private {function()} */
- this.boundOnCommandUndo_ = () => {
- this.handle(Command.UNDO, new Set());
- };
- document.addEventListener('command-undo', this.boundOnCommandUndo_);
-
- /** @private {function(!Event)} */
- this.boundOnKeydown_ = this.onKeydown_.bind(this);
- document.addEventListener('keydown', this.boundOnKeydown_);
-
/** @private {!Map<Command, cr.ui.KeyboardShortcutList>} */
this.shortcuts_ = new Map();
@@ -92,14 +77,40 @@
this.addShortcut_(Command.CUT, 'Ctrl|x', 'Meta|x');
this.addShortcut_(Command.COPY, 'Ctrl|c', 'Meta|c');
this.addShortcut_(Command.PASTE, 'Ctrl|v', 'Meta|v');
+
+ /** @private {!Map<string, Function>} */
+ this.boundListeners_ = new Map();
+
+ const addDocumentListener = (eventName, handler) => {
+ assert(!this.boundListeners_.has(eventName));
+ const boundListener = handler.bind(this);
+ this.boundListeners_.set(eventName, boundListener);
+ document.addEventListener(eventName, boundListener);
+ };
+ addDocumentListener('open-command-menu', this.onOpenCommandMenu_);
+ addDocumentListener('keydown', this.onKeydown_);
+
+ const addDocumentListenerForCommand = (eventName, command) => {
+ addDocumentListener(eventName, (e) => {
+ if (e.path[0].tagName == 'INPUT')
+ return;
+
+ const items = this.getState().selection.items;
+ if (this.canExecute(command, items))
+ this.handle(command, items);
+ });
+ };
+ addDocumentListenerForCommand('command-undo', Command.UNDO);
+ addDocumentListenerForCommand('cut', Command.CUT);
+ addDocumentListenerForCommand('copy', Command.COPY);
+ addDocumentListenerForCommand('paste', Command.PASTE);
},
detached: function() {
CommandManager.instance_ = null;
- document.removeEventListener(
- 'open-command-menu', this.boundOnOpenCommandMenu_);
- document.removeEventListener('command-undo', this.boundOnCommandUndo_);
- document.removeEventListener('keydown', this.boundOnKeydown_);
+ this.boundListeners_.forEach(
+ (handler, eventName) =>
+ document.removeEventListener(eventName, handler));
},
/**