diff --git a/config/assets.yml b/config/assets.yml index f615fadbe..9e451f769 100644 --- a/config/assets.yml +++ b/config/assets.yml @@ -17,6 +17,7 @@ javascripts: - public/javascripts/vendor/fileuploader.js - public/javascripts/vendor/Mustache.js - public/javascripts/jquery.autocomplete-custom.js + - public/javascripts/keycodes.js - public/javascripts/diaspora.js - public/javascripts/widgets/i18n.js - public/javascripts/widgets/alert.js diff --git a/public/javascripts/keycodes.js b/public/javascripts/keycodes.js new file mode 100644 index 000000000..6138b08ac --- /dev/null +++ b/public/javascripts/keycodes.js @@ -0,0 +1,115 @@ +var KEYCODES = { +BACKSPACE : 8, +TAB : 9, +ENTER : 13, +RETURN : 13, +SHIFT : 16, +CTRL : 17, +ALT : 18, +PAUSE : 19, +BREAK : 19, +CAPSLOCK : 20, +ESCAPE : 27, +SPACEBAR : 32, +PAGEUP : 33, +PAGEDOWN : 34, +END : 35, +HOME : 36, +LEFT : 37, +UP : 38, +RIGHT : 39, +DOWN : 40, +INSERT : 45, +DEL : 46, +DELETE : 46, +0 : 48, +1 : 49, +2 : 50, +3 : 51, +4 : 52, +5 : 53, +6 : 54, +7 : 55, +8 : 56, +9 : 57, +A : 65, +B : 66, +C : 67, +D : 68, +E : 69, +F : 70, +G : 71, +H : 72, +I : 73, +J : 74, +K : 75, +L : 76, +M : 77, +N : 78, +O : 79, +P : 80, +Q : 81, +R : 82, +S : 83, +T : 84, +U : 85, +V : 86, +W : 87, +X : 88, +Y : 89, +Z : 90, +LEFTWINDOW : 91, +RIGHTWINDOW : 92, +SELECT : 93, +NUMPAD0 : 96, +NUMPAD1 : 97, +NUMPAD2 : 98, +NUMPAD3 : 99, +NUMPAD4 : 100, +NUMPAD5 : 101, +NUMPAD6 : 102, +NUMPAD7 : 103, +NUMPAD8 : 104, +NUMPAD9 : 105, +MULTIPLY : 106, +ADD : 107, +SUBTRACT : 109, +DECIMALPOINT : 110, +DIVIDE : 111, +F1 : 112, +F2 : 113, +F3 : 114, +F4 : 115, +F5 : 116, +F6 : 117, +F7 : 118, +F8 : 119, +F9 : 120, +F10 : 121, +F11 : 122, +F12 : 123, +NUMLOCK : 144, +SCROLLLOCK : 145, +SEMICOLON : 186, +EQUALSIGN : 187, +COMMA : 188, +DASH : 189, +PERIOD : 190, +FORWARDSLASH : 191, +ACCENTGRAVE : 192, +OPENBRACKET : 219, +BACKSLASH : 220, +CLOSEBRACKET : 221, +SINGLEQUOTE : 222, +isInsertion : function(keyCode){ + if(keyCode <= 46 && keyCode != this.RETURN && keyCode != this.SPACEBAR){ + return false; + }else if(keyCode > 90 && keyCode < 96){ + return false; + }else if(keyCode >= 112 && keyCode <= 145){ + return false; + }else { + return true; + } +}, +} diff --git a/public/javascripts/publisher.js b/public/javascripts/publisher.js index 3f914e62c..1d4c5f927 100644 --- a/public/javascripts/publisher.js +++ b/public/javascripts/publisher.js @@ -2,18 +2,7 @@ * licensed under the Affero General Public License version 3 or later. See * the COPYRIGHT file. */ - var KEY = { - UP: 38, - DOWN: 40, - DEL: 46, - TAB: 9, - RETURN: 13, - ESC: 27, - COMMA: 188, - PAGEUP: 33, - PAGEDOWN: 34, - BACKSPACE: 8 - }; + //TODO: make this a widget var Publisher = { close: function(){ @@ -112,9 +101,9 @@ var Publisher = { return resultString; }, - insertionAt : function(insertionEndIndex, insertionStartIndex, keyCode){ - this.incrementMentionLocations(insertionStartIndex, insertionEndIndex - insertionStartIndex); - var mentionIndex = this.mentionAt(insertionEndIndex); + insertionAt : function(insertionStartIndex, keyCode){ + this.incrementMentionLocations(insertionStartIndex, 1); + var mentionIndex = this.mentionAt(insertionStartIndex + 1); var mention = this.mentions[mentionIndex]; if(mention){ @@ -125,7 +114,7 @@ var Publisher = { deletionAt : function(visibleCursorIndex, keyCode){ var effectiveCursorIndex; - if(keyCode == KEY.DEL){ + if(keyCode == KEYCODES.DEL){ effectiveCursorIndex = visibleCursorIndex; }else{ effectiveCursorIndex = visibleCursorIndex - 1; @@ -185,31 +174,21 @@ var Publisher = { }, keyUpHandler : function(event){ - var input = Publisher.input(); - var cursorIndexAtKeydown = Publisher.cursorIndexAtKeydown; - Publisher.cursorIndexAtKeydown = -1; - if(input.val() == Publisher.oldInputContent || event.keyCode == KEY.RETURN || event.keyCode == KEY.DEL || event.keyCode == KEY.BACKSPACE){ - Publisher.autocompletion.repopulateHiddenInput(); - return; - }else { - Publisher.oldInputContent = input.val(); - var visibleCursorIndex = input[0].selectionStart; - Publisher.autocompletion.mentionList.insertionAt(visibleCursorIndex, cursorIndexAtKeydown, event.keyCode); - Publisher.autocompletion.repopulateHiddenInput(); - } + Publisher.autocompletion.repopulateHiddenInput(); }, keyDownHandler : function(event){ var input = Publisher.input(); var visibleCursorIndex = input[0].selectionStart; - if(Publisher.cursorIndexAtKeydown == -1){ - Publisher.cursorIndexAtKeydown = visibleCursorIndex; - } + //if(Publisher.cursorIndexAtKeydown == -1){ + // Publisher.cursorIndexAtKeydown = visibleCursorIndex; + //} - if((event.keyCode == KEY.DEL && visibleCursorIndex < input.val().length) || (event.keyCode == KEY.BACKSPACE && visibleCursorIndex > 0)){ + if((event.keyCode == KEYCODES.DEL && visibleCursorIndex < input.val().length) || (event.keyCode == KEYCODES.BACKSPACE && visibleCursorIndex > 0)){ Publisher.autocompletion.mentionList.deletionAt(visibleCursorIndex, event.keyCode); + }else if(KEYCODES.isInsertion(event.keyCode) && event.keyCode != KEYCODES.RETURN ){ + Publisher.autocompletion.mentionList.insertionAt(visibleCursorIndex, event.keyCode); } - Publisher.autocompletion.repopulateHiddenInput(); }, addMentionToInput: function(input, cursorIndex, formatted){ diff --git a/spec/javascripts/publisher-spec.js b/spec/javascripts/publisher-spec.js index 8b44ae771..5001ac9ea 100644 --- a/spec/javascripts/publisher-spec.js +++ b/spec/javascripts/publisher-spec.js @@ -139,19 +139,19 @@ describe("Publisher", function() { expect(list.mentionAt(8)).toBeFalsy(); }); }); - describe("keypressAt", function(){ + describe("insertionAt", function(){ it("does nothing if there is no visible mention at that index", function(){ - list.keypressAt(8); + list.insertionAt(8); expect(visibleInput.val()).toBe(visibleVal); expect(hiddenInput.val()).toBe(hiddenVal); }); it("deletes the mention from the hidden field if there is a mention", function(){ - list.keypressAt(3); + list.insertionAt(3); expect(visibleInput.val()).toBe(visibleVal); expect(list.generateHiddenInput(visibleInput.val())).toBe(visibleVal); }); it("deletes the mention from the list", function(){ - list.keypressAt(3); + list.insertionAt(3); expect(list.mentionAt(3)).toBeFalsy(); }); it("calls updateMentionLocations", function(){ @@ -160,19 +160,19 @@ describe("Publisher", function() { mentionString : "@{SomeoneElse; other@pod.org}" }; list.push(mentionTwo); - spyOn(list, 'updateMentionLocations'); - list.keypressAt(3, 60); - expect(list.updateMentionLocations).toHaveBeenCalled(); + spyOn(list, 'incrementMentionLocations'); + list.insertionAt(3,4, 60); + expect(list.incrementMentionLocations).toHaveBeenCalled(); }); }); - describe("updateMentionLocations", function(){ - it("updates the offsets of the remaining mentions in the list", function(){ + describe("incrementMentionLocations", function(){ + it("increments the offsets of the remaining mentions in the list", function(){ mentionTwo = { visibleStart : 8, visibleEnd : 15, mentionString : "@{SomeoneElse; other@pod.org}" }; list.push(mentionTwo); - list.updateMentionLocations(7, 60); + list.incrementMentionLocations(7, 1); expect(mentionTwo.visibleStart).toBe(9); expect(mentionTwo.visibleEnd).toBe(16); }); @@ -189,7 +189,7 @@ describe("Publisher", function() { spec.loadFixture('aspects_index'); func = Publisher.autocompletion.addMentionToInput; input = Publisher.input(); - Publisher.autocompletion.mentionList = []; + Publisher.autocompletion.mentionList.mentions = []; replaceWith = "Replace with this."; }); it("replaces everything up to the cursor if the cursor is a word after that @", function(){