Skip to content

Instantly share code, notes, and snippets.

@davidshumway
Last active August 29, 2015 14:21
Show Gist options
  • Save davidshumway/9827eac41f19b44bd5de to your computer and use it in GitHub Desktop.
Save davidshumway/9827eac41f19b44bd5de to your computer and use it in GitHub Desktop.
iD.ui.PresetList = function(context) {
...
function keydown() {
// hack to let delete shortcut work when search is autofocused
if (search.property('value').length === 0 &&
(d3.event.keyCode === d3.keybinding.keyCodes['⌫'] ||
d3.event.keyCode === d3.keybinding.keyCodes['⌦'])) {
d3.event.preventDefault();
d3.event.stopPropagation();
iD.operations.Delete([id], context)();
} else if (search.property('value').length === 0 && // How to go about adding the equivalent of this?
(d3.event.ctrlKey || d3.event.metaKey) &&
d3.event.keyCode === d3.keybinding.keyCodes.B) {
d3.event.preventDefault();
d3.event.stopPropagation();
} else if (search.property('value').length === 0 &&
(d3.event.ctrlKey || d3.event.metaKey) &&
d3.event.keyCode === d3.keybinding.keyCodes.z) {
d3.event.preventDefault();
d3.event.stopPropagation();
context.undo();
} else if (!d3.event.ctrlKey && !d3.event.metaKey) {
d3.select(this).on('keydown', null);
}
}
...
}
@davidshumway
Copy link
Author

The following works to bypass autofocus. It adds the keybinding to both the document and the autofocused input field. However, only when the autofocused field (document.getElementsByClassName('preset-search-input')[0]) exists on the page. The input added to the page after first instance of "Select feature type" menu appears, which is after drawing a new entity on the map.

var kb =
    d3.
    keybinding('osm_browser_tool_tags').
    on(
        iD.ui.cmd('⌘' + hk),
        bToolChangeTags
    )
// Exists.
d3.select(document).call(kb);

// Does not exist yet. Add an eventListener?
d3.select(document.getElementsByClassName('preset-search-input')[0]).call(kb);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment