From 2e69992d9ba5e91cfbcc9d0328729a4946a761c1 Mon Sep 17 00:00:00 2001 From: Matt Hughes Date: Sun, 22 Dec 2013 19:24:21 -0500 Subject: [PATCH] Improved comma tag completion on international keyboard layouts Reported in issue #1475 - `String.localeCompare` can be more reliable for keys that do not relate to cursor movement - Adds a third key handler (`keypress`) that contains the character code rather than physical key - `COMMA` key constant no longer required (unless `,` char should be a constant?) --- ghost/admin/views/editor-tag-widget.js | 61 +++++++++++++++----------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/ghost/admin/views/editor-tag-widget.js b/ghost/admin/views/editor-tag-widget.js index d34e6e84eb..29c822273e 100644 --- a/ghost/admin/views/editor-tag-widget.js +++ b/ghost/admin/views/editor-tag-widget.js @@ -10,6 +10,7 @@ events: { 'keyup [data-input-behaviour="tag"]': 'handleKeyup', 'keydown [data-input-behaviour="tag"]': 'handleKeydown', + 'keypress [data-input-behaviour="tag"]': 'handleKeypress', 'click ul.suggestions li': 'handleSuggestionClick', 'click .tags .tag': 'handleTagClick', 'click .tag-label': 'mobileTags' @@ -20,7 +21,6 @@ DOWN: 40, ESC: 27, ENTER: 13, - COMMA: 188, BACKSPACE: 8 }, @@ -130,9 +130,7 @@ handleKeyup: function (e) { var $target = $(e.currentTarget), - searchTerm = $.trim($target.val()), - tag, - $selectedSuggestion; + searchTerm = $.trim($target.val()); if (e.keyCode === this.keys.UP) { e.preventDefault(); @@ -154,28 +152,6 @@ } } else if (e.keyCode === this.keys.ESC) { this.$suggestions.hide(); - } else if ((e.keyCode === this.keys.ENTER || e.keyCode === this.keys.COMMA) && searchTerm) { - // Submit tag using enter or comma key - e.preventDefault(); - - $selectedSuggestion = this.$suggestions.children(".selected"); - if (this.$suggestions.is(":visible") && $selectedSuggestion.length !== 0) { - - if ($('.tag:containsExact("' + $selectedSuggestion.data('tag-name') + '")').length === 0) { - tag = {id: $selectedSuggestion.data('tag-id'), name: $selectedSuggestion.data('tag-name')}; - this.addTag(tag); - } - } else { - if (e.keyCode === this.keys.COMMA) { - searchTerm = searchTerm.replace(/,/g, ""); - } // Remove comma from string if comma is used to submit. - if ($('.tag:containsExact("' + searchTerm + '")').length === 0) { - this.addTag({id: null, name: searchTerm}); - } - } - $target.val('').focus(); - searchTerm = ""; // Used to reset search term - this.$suggestions.hide(); } if (e.keyCode === this.keys.UP || e.keyCode === this.keys.DOWN) { @@ -202,6 +178,39 @@ } }, + handleKeypress: function (e) { + var $target = $(e.currentTarget), + searchTerm = $.trim($target.val()), + tag, + $selectedSuggestion, + isComma = ",".localeCompare(String.fromCharCode(e.keyCode || e.charCode)) === 0; + + // use localeCompare in case of international keyboard layout + if ((e.keyCode === this.keys.ENTER || isComma) && searchTerm) { + // Submit tag using enter or comma key + e.preventDefault(); + + $selectedSuggestion = this.$suggestions.children(".selected"); + if (this.$suggestions.is(":visible") && $selectedSuggestion.length !== 0) { + + if ($('.tag:containsExact("' + $selectedSuggestion.data('tag-name') + '")').length === 0) { + tag = {id: $selectedSuggestion.data('tag-id'), name: $selectedSuggestion.data('tag-name')}; + this.addTag(tag); + } + } else { + if (isComma) { + searchTerm = searchTerm.replace(/,/g, ""); + } // Remove comma from string if comma is used to submit. + if ($('.tag:containsExact("' + searchTerm + '")').length === 0) { + this.addTag({id: null, name: searchTerm}); + } + } + $target.val('').focus(); + searchTerm = ""; // Used to reset search term + this.$suggestions.hide(); + } + }, + completeCurrentTag: function () { var $target = this.$('.tag-input'), tagName = $target.val(),