lib/patricia/assets/javascripts/editor.js in patricia-0.1.2 vs lib/patricia/assets/javascripts/editor.js in patricia-0.1.3

- old
+ new

@@ -80,11 +80,15 @@ }); self.offsets = []; self.currentOffset = []; self.currentOffsetIndex = 0; - self.textArea = $('#edit-modal textarea'); + if (typeof aceEditor === 'undefined') { + self.textArea = $('#editor'); + } else { + self.textArea = aceEditor; + } self.editModal = $('#edit-modal'); self.editNavigationBox = $('#edit-navigation-box'); self.editSelectedText = function() { $.ajax({ 'type': 'POST', @@ -103,13 +107,13 @@ self.offsets = 0; // Reset. self.offsets = data['offsets'] self.currentOffsetIndex = 0; self.currentOffset = self.offsets[self.currentOffsetIndex] || [0, 0]; - $('#current-offset-index').text(self.currentOffsetIndex + 1 + - ' / '); - $('#offsets-length').text(self.offsets.length); + $('#current-offset-index').text(self.currentOffsetIndex + 1 + + ' / '); + $('#offsets-length').text(self.offsets.length); if (self.offsets.length > 1) { $('#edit-navigation-box').show(); self.editNavigationBox.show(); } else { self.editNavigationBox.hide(); @@ -117,12 +121,16 @@ self.editModal.on('shown.bs.modal', function() { self.textArea.focus(); self.textArea[0].setSelectionRange(self.currentOffset[0], self.currentOffset[1]) - self.scrollToSelectedText(self.textArea, self.currentOffset[0], - self.currentOffset[1]); + try { + self.scrollToSelectedText(self.textArea, self.currentOffset[0], + self.currentOffset[1]); + } catch(e) { + // Ignore it. + } }); }, 'data-type': 'json', }); }; @@ -145,11 +153,11 @@ $('#content').on('mouseup', function(e) { if (self.getSelectedText() != '') { self.currentlyHighlightedText = self.getSelectedText(); } if (self.getSelectedText() != '') { - // if (self.currentlyHighlightedText != '') { + // if (self.currentlyHighlightedText != '') { if (self.leftEditButton.hasClass('hidden')) { self.leftEditButton.removeClass('hidden'); self.leftEditButton.show(); } else { self.leftEditButton.show(); @@ -159,20 +167,20 @@ 'top': self.mouseY - (self.leftEditButton.height() + 20), 'left': self.mouseX - (self.leftEditButton.width()), 'z-index': 9998, 'box-shadow': '0px 5px 3px rgba(0, 0, 0, 0.3), \ 0px 0px 8px rgba(102, 175, 233, 0.6)', - 'background-color': '#121212', - 'color': '#F1F1F1', - 'margin-top': '0', - 'font-weight': 'bold', - 'border': 'none', - 'border-radius': '5px', - 'border': '2px solid #F1F1F1', + 'background-color': '#121212', + 'color': '#F1F1F1', + 'margin-top': '0', + 'font-weight': 'bold', + 'border': 'none', + 'border-radius': '5px', + 'border': '2px solid #F1F1F1', }); self.leftEditButton.animate({ - 'margin-top': '-10px', + 'margin-top': '-10px', }, 230); } }); $('*').not(self.leftEditButton).on('mousedown', function(e) { @@ -197,12 +205,16 @@ } self.currentOffset = self.offsets[self.currentOffsetIndex]; self.textArea.focus(); self.textArea[0].setSelectionRange(self.currentOffset[0], self.currentOffset[1]) - self.scrollToSelectedText(self.textArea, self.currentOffset[0], - self.currentOffset[1]); + try { + self.scrollToSelectedText(self.textArea, self.currentOffset[0], + self.currentOffset[1]); + } catch(e) { + // Ignore it. + } $('#current-offset-index').text(self.currentOffsetIndex + 1 + ' / '); }); $('#next-match-button').click(function(e) { e.preventDefault(); @@ -213,12 +225,16 @@ } self.currentOffset = self.offsets[self.currentOffsetIndex]; self.textArea.focus(); self.textArea[0].setSelectionRange(self.currentOffset[0], self.currentOffset[1]) - self.scrollToSelectedText(self.textArea, self.currentOffset[0], - self.currentOffset[1]); + try { + self.scrollToSelectedText(self.textArea, self.currentOffset[0], + self.currentOffset[1]); + } catch(e) { + // Ignore it. + } $('#current-offset-index').text(self.currentOffsetIndex + 1 + ' / '); }); // 101: e - Edit selected text @@ -230,10 +246,11 @@ } }); // Saving + self.saveButton = $('#edit-save-button'); self.saveButton.on('click', function() { $.ajax({ 'type': 'POST', 'url': self.saveButton.attr('data-url'), @@ -263,7 +280,42 @@ }); self.textArea.on('keyup', function(e) { delete self.keys[e.which]; }); + + + // Add keybinding to the Ace editor, if it is available. + + if (typeof aceEditor !== 'undefined') { + // Save. + aceEditor.commands.addCommand({ + name: 'saveMarkup', + bindKey: {win: 'Ctrl-Return', mac: 'Ctrl-Return'}, + exec: function(editor) { + self.saveButton.click(); + }, + readOnly: true, + }); + + // Got to next match. + aceEditor.commands.addCommand({ + name: 'nextMarkupMatch', + bindKey: {win: 'Option-N', mac: 'Option-N'}, + exec: function(editor) { + $('#next-match-button').click(); + }, + readOnly: true, + }); + + // Got to previous match. + aceEditor.commands.addCommand({ + name: 'previousMarkupMatch', + bindKey: {win: 'Option-P', mac: 'Option-P'}, + exec: function(editor) { + $('#previous-match-button').click(); + }, + readOnly: true, + }); + } });