From 41b39765ff70a044266904f93e9ac464be47952c Mon Sep 17 00:00:00 2001 From: David Manthey Date: Thu, 6 Sep 2018 12:37:45 -0400 Subject: [PATCH] Fix an issue with key handling. Mousetrap generates a new instance of its key handler when called on an element (not retrieving an existing instance). This means that we were binding the escape key multiple times which let to problems when it was used. --- CHANGELOG.md | 3 +++ src/annotationLayer.js | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d0329bc70..9fb57acab4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +### Bug Fixes +- Fixed an issue with the annotation key handler (#923) + ## Version 0.18.0 ### Features diff --git a/src/annotationLayer.js b/src/annotationLayer.js index 11fb841cc2..e369452dbf 100644 --- a/src/annotationLayer.js +++ b/src/annotationLayer.js @@ -77,7 +77,8 @@ var annotationLayer = function (args) { m_annotations = [], m_features = [], m_labelFeature, - m_labelLayer; + m_labelLayer, + m_keyHandler; var geojsonStyleProperties = { 'closed': {dataType: 'boolean', keys: ['closed', 'close']}, @@ -519,10 +520,13 @@ var annotationLayer = function (args) { mapNode = m_this.map().node(), oldMode = m_mode; m_mode = arg; mapNode.toggleClass('annotation-input', !!(m_mode && m_mode !== m_this.modes.edit)); + if (!m_keyHandler) { + m_keyHandler = Mousetrap(mapNode[0]); + } if (m_mode) { - Mousetrap(mapNode[0]).bind('esc', function () { m_this.mode(null); }); + m_keyHandler.bind('esc', function () { m_this.mode(null); }); } else { - Mousetrap(mapNode[0]).unbind('esc'); + m_keyHandler.unbind('esc'); } if (m_this.currentAnnotation) { switch (m_this.currentAnnotation.state()) { @@ -1150,6 +1154,9 @@ var annotationLayer = function (args) { * @returns {this} The current layer. */ this._exit = function () { + if (m_keyHandler) { + m_keyHandler.reset(); + } m_this._removeLabelFeature(); // Call super class exit s_exit.call(m_this);