From ddbf50345d8cac44cd8020857d49aa7b978e0520 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Tue, 4 Dec 2012 16:26:00 -0500 Subject: [PATCH] Reintroduce keybindings for deleting features --- js/id/id.js | 6 +++--- js/id/renderer/map.js | 29 +++++++++++++++++++++-------- js/lib/d3.keybinding.js | 14 ++++++-------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/js/id/id.js b/js/id/id.js index fdd9969cf..328ba85cb 100644 --- a/js/id/id.js +++ b/js/id/id.js @@ -28,9 +28,7 @@ window.iD = function(container) { var buttons = bar.selectAll('button.add-button') .data([iD.modes.AddPlace, iD.modes.AddRoad, iD.modes.AddArea]) .enter().append('button') - .attr('class', function(d) { - return 'add-button ' + d.id; - }) + .attr('class', 'add-button') .text(function (mode) { return mode.title; }) .on('click', function (mode) { controller.enter(mode); }); @@ -155,6 +153,8 @@ window.iD = function(container) { if (mods === '⌘') history.undo(); }); d3.select(document).call(keybinding); + map.keybinding(keybinding); + var hash = iD.Hash().map(map); if (!hash.hadHash) { diff --git a/js/id/renderer/map.js b/js/id/renderer/map.js index a5112703b..a8e9cf614 100644 --- a/js/id/renderer/map.js +++ b/js/id/renderer/map.js @@ -5,6 +5,7 @@ iD.Map = function() { inspector = iD.Inspector(), selection = null, translateStart, + keybinding, apiTilesLoaded = {}, projection = d3.geo.mercator(), zoom = d3.behavior.zoom() @@ -334,6 +335,18 @@ iD.Map = function() { function nameHoverOut() { d3.select('.messages').text(''); } + function selectClick() { + var entity = d3.select(d3.event.target).data(); + if (entity) entity = entity[0]; + if (!entity || selection === entity.id || (entity.tags && entity.tags.elastic)) return; + if (entity.type === 'way') d3.select(d3.event.target).call(waydragbehavior); + map.selectEntity(entity); + keybinding.on('⌫.deletefeature', function(e) { + removeEntity(entity); + e.preventDefault(); + }); + } + function deselectClick() { var hadSelection = !!selection; if (hadSelection) { @@ -345,17 +358,10 @@ iD.Map = function() { redraw(); hideInspector(); } + keybinding.on('⌫.deletefeature', null); selection = null; } - function selectClick() { - var entity = d3.select(d3.event.target).data(); - if (entity) entity = entity[0]; - if (!entity || selection === entity.id || (entity.tags && entity.tags.elastic)) return; - if (entity.type === 'way') d3.select(d3.event.target).call(waydragbehavior); - map.selectEntity(entity); - } - function removeEntity(entity) { // Remove this node from any ways that is a member of history.graph().parents(entity.id) @@ -364,6 +370,7 @@ iD.Map = function() { parent.nodes = _.without(parent.nodes, entity.id); history.perform(iD.actions.removeWayNode(parent, entity)); }); + deselectClick(); history.perform(iD.actions.remove(entity)); } @@ -496,6 +503,12 @@ iD.Map = function() { return map; }; + map.keybinding = function (_) { + if (!arguments.length) return keybinding; + keybinding = _; + return map; + }; + map.selectEntity = function(entity) { selection = entity.id; d3.select('.inspector-wrap') diff --git a/js/lib/d3.keybinding.js b/js/lib/d3.keybinding.js index f5958a2c7..12f7a33fd 100644 --- a/js/lib/d3.keybinding.js +++ b/js/lib/d3.keybinding.js @@ -1,4 +1,6 @@ d3.keybinding = function() { + // via https://github.com/keithamus/jwerty/ + // and https://github.com/madrobby/keymaster var _keys = { // MOD aka toggleable keys mods: { @@ -11,7 +13,6 @@ d3.keybinding = function() { // META, on Mac: ⌘ (CMD), on Windows (Win), on Linux (Super) '⌘': 91 }, - // Normal keys keys: { // Backspace key, on Mac: ⌫ (Backspace) @@ -40,7 +41,6 @@ d3.keybinding = function() { ins: 45, insert: 45, // Delete key, on Mac: ⌫ (Delete) del: 46, 'delete': 46, - // Left Arrow Key, or ← '←': 37, left: 37, 'arrow-left': 37, // Up Arrow Key, or ↑ @@ -49,7 +49,6 @@ d3.keybinding = function() { '→': 39, right: 39, 'arrow-right': 39, // Up Arrow Key, or ↓ '↓': 40, down: 40, 'arrow-down': 40, - // odities, printing characters that come out wrong: // Num-Multiply, or * '*': 106, star: 106, asterisk: 106, multiply: 106, @@ -96,13 +95,12 @@ d3.keybinding = function() { var pairs = d3.entries(_keys.keys), key_shortcuts = pairs.map(function(d) { return d.key; - }); - var mods = d3.entries(_keys.mods), + }), + mods = d3.entries(_keys.mods), mod_shortcuts = mods.map(function(d) { return d.key; - }); - - var event = d3.dispatch.apply(d3, key_shortcuts), + }), + event = d3.dispatch.apply(d3, key_shortcuts), modifiers = []; function keydown() {