From 27a95f634d351ad11c45ca835c9062b3851ce254 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 30 Jan 2011 00:31:38 +0400 Subject: [PATCH 01/10] add support for contextmenu --- lib/ace/css/editor.css | 2 +- lib/ace/editor.js | 14 ++++++++++---- lib/ace/keyboard/textinput.js | 20 +++++++++++++++++++- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/ace/css/editor.css b/lib/ace/css/editor.css index df204e58..af359ae9 100644 --- a/lib/ace/css/editor.css +++ b/lib/ace/css/editor.css @@ -74,7 +74,7 @@ } .ace_editor textarea { - position: "absolute"; + position: absolute; z-index: -1; opacity: 0; width: 10px; diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 5e8f2a01..7394abd1 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -281,11 +281,11 @@ var Editor =function(renderer, session) { this.onCursorChange = function(e) { this.$highlightBrackets(); - + // move text input over the cursor // this is required for iOS and IME this.renderer.moveTextAreaToCursor(this.textInput.getElement()); - + this.renderer.updateCursor(this.getCursorPosition(), this.$overwrite); if (!this.$blockScrolling && (!e || !e.blockScrolling)) { @@ -357,10 +357,16 @@ var Editor =function(renderer, session) { var pos = this.renderer.screenToTextCoordinates(pageX, pageY); pos.row = Math.max(0, Math.min(pos.row, this.session.getLength()-1)); - if (event.getButton(e) != 0) { - if (this.selection.isEmpty()) { + var button = event.getButton(e) + if (button != 0) { + var isEmpty = this.selection.isEmpty() + if (isEmpty) { this.moveCursorToPosition(pos); } + if(button == 2) { + this.textInput.onContextMenu({x: pageX, y: pageY}, isEmpty); + event.capture(this.container, function(){}, this.textInput.onContextMenuClose); + } return; } diff --git a/lib/ace/keyboard/textinput.js b/lib/ace/keyboard/textinput.js index f4ecac17..9bcecb2a 100644 --- a/lib/ace/keyboard/textinput.js +++ b/lib/ace/keyboard/textinput.js @@ -184,10 +184,28 @@ var TextInput = function(parentNode, host) { this.blur = function() { text.blur(); }; - + this.getElement = function() { return text; }; + + this.onContextMenu = function(mousePos, isEmpty){ + if(mousePos){ + text.style.cssText = 'position:fixed; z-index:1000;' + + 'left:' + (mousePos.x - 2) + 'px; top:' + (mousePos.y - 2) + 'px;' + + } + if(isEmpty) + text.value=''; + } + + this.onContextMenuClose = function(){ + setTimeout(function () { + sendText(); + }, 0); + text.style.position = ''; + text.style.zIndex = ''; + } }; exports.TextInput = TextInput; From cad1421cd922a448797176ab1e7536ef8ee1f93a Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 30 Jan 2011 00:41:45 +0400 Subject: [PATCH 02/10] moveTextAreaToCursor after updating cursor, to get right position --- lib/ace/editor.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 7394abd1..149cea24 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -280,17 +280,17 @@ var Editor =function(renderer, session) { }; this.onCursorChange = function(e) { - this.$highlightBrackets(); - - // move text input over the cursor - // this is required for iOS and IME - this.renderer.moveTextAreaToCursor(this.textInput.getElement()); - this.renderer.updateCursor(this.getCursorPosition(), this.$overwrite); if (!this.$blockScrolling && (!e || !e.blockScrolling)) { this.renderer.scrollCursorIntoView(); } + + // move text input over the cursor + // this is required for iOS and IME + this.renderer.moveTextAreaToCursor(this.textInput.getElement()); + + this.$highlightBrackets(); this.$updateHighlightActiveLine(); }; From decc10e92293492c7d1c07ff94fedae92aadbad6 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 30 Jan 2011 01:26:13 +0400 Subject: [PATCH 03/10] fix for chrome --- lib/ace/keyboard/textinput.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/ace/keyboard/textinput.js b/lib/ace/keyboard/textinput.js index 9bcecb2a..097149a7 100644 --- a/lib/ace/keyboard/textinput.js +++ b/lib/ace/keyboard/textinput.js @@ -50,6 +50,7 @@ var TextInput = function(parentNode, host) { var inCompostion = false; var copied = false; + var tempStyle = ''; function sendText(valueToSend) { if (!copied) { @@ -190,21 +191,25 @@ var TextInput = function(parentNode, host) { }; this.onContextMenu = function(mousePos, isEmpty){ - if(mousePos){ + if (mousePos) { + if(!tempStyle) + tempStyle = text.style.cssText; text.style.cssText = 'position:fixed; z-index:1000;' + 'left:' + (mousePos.x - 2) + 'px; top:' + (mousePos.y - 2) + 'px;' } - if(isEmpty) + if (isEmpty) text.value=''; } this.onContextMenuClose = function(){ setTimeout(function () { + if (tempStyle) { + text.style.cssText = tempStyle; + tempStyle = ''; + } sendText(); }, 0); - text.style.position = ''; - text.style.zIndex = ''; } }; From 31695f06c05b68a30fdb5fde49c5c8b96c8f9751 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 29 Jan 2011 19:35:36 +0800 Subject: [PATCH 04/10] fix selection handling in undo/redoChanges --- lib/ace/edit_session.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index 9e907b3d..d3ae0ee0 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -541,7 +541,7 @@ var EditSession = function(text, mode) { if (firstDelta.action == "insertText" || firstDelta.action == "insertLines") this.selection.moveCursorToPosition(firstDelta.range.start); if (firstDelta.action == "removeText" || firstDelta.action == "removeLines") - this.selection.setSelectionRange(Range.fromPoints(firstDelta.range.start, lastDelta.range.end)); + this.selection.setSelectionRange(Range.fromPoints(lastDelta.range.start, firstDelta.range.end)); }, this.redoChanges = function(deltas) { @@ -560,7 +560,7 @@ var EditSession = function(text, mode) { if (firstDelta.action == "insertText" || firstDelta.action == "insertLines") this.selection.setSelectionRange(Range.fromPoints(firstDelta.range.start, lastDelta.range.end)); if (firstDelta.action == "removeText" || firstDelta.action == "removeLines") - this.selection.moveCursorToPosition(firstDelta.range.start); + this.selection.moveCursorToPosition(lastDelta.range.start); }, this.replace = function(range, text) { From 62600917ece07819b55ad2126d240377d35b9a94 Mon Sep 17 00:00:00 2001 From: nightwing Date: Sat, 29 Jan 2011 19:41:54 +0800 Subject: [PATCH 05/10] update text layer after insertLines --- lib/ace/editor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 5e8f2a01..2e45d887 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -264,7 +264,7 @@ var Editor =function(renderer, session) { var range = delta.range; this.bgTokenizer.start(range.start.row); - if (range.start.row == range.end.row) + if (range.start.row == range.end.row && delta.action != "insertLines") var lastRow = range.end.row; else lastRow = Infinity; From 1f4c093b6e3557fa7828da37ef25e2c8e8331a5a Mon Sep 17 00:00:00 2001 From: nightwing Date: Mon, 31 Jan 2011 00:32:53 +0800 Subject: [PATCH 06/10] Clamp row before column so that selection works at document's end --- lib/ace/edit_session.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/lib/ace/edit_session.js b/lib/ace/edit_session.js index d3ae0ee0..2679f4e4 100644 --- a/lib/ace/edit_session.js +++ b/lib/ace/edit_session.js @@ -918,7 +918,7 @@ var EditSession = function(text, mode) { /** * * @returns array - * - array[0]: The documentRow aquivalent. + * - array[0]: The documentRow equivalent. * - array[1]: The screenRowOffset to the first documentRow on the screen. */ this.$screenToDocumentRow = function(row) { @@ -949,23 +949,24 @@ var EditSession = function(text, mode) { var docRow; var docColumn; var remaining = column; + var linesCount = this.getLength(); if (!this.$useWrapMode) { - docRow = row; + docRow = row >= linesCount? linesCount-1 : (row < 0 ? 0 : row); row = 0; docColumn = 0; line = this.getLine(docRow); } else { - var wrapData = this.$wrapData, linesCount = this.getLength(); + var wrapData = this.$wrapData; - var rowData = this.$screenToDocumentRow(row); - row = rowData[1]; - docRow = rowData[0]; + var docRow = 0; + while (docRow < linesCount && row >= wrapData[docRow].length + 1) { + row -= wrapData[docRow].length + 1; + docRow ++; + } if (docRow >= linesCount) { - return { - row: docRow, - column: 0 - }; + docRow = linesCount-1 + row = wrapData[docRow].length; } docColumn = wrapData[docRow][row - 1] || 0; line = this.getLine(docRow).substring(docColumn); @@ -1009,15 +1010,16 @@ var EditSession = function(text, mode) { } // Clamp docColumn. - if (docRow < linesCount && wrapData[docRow][row]) { - if (docColumn >= wrapData[docRow][row]) { + if (this.$useWrapMode) { + column = wrapData[docRow][row] + if (docColumn >= column) { // We remove one character at the end such that the docColumn // position returned is not associated to the next row on the // screen. - docColumn = wrapData[docRow][row] - 1; + docColumn = column - 1; } - } else if (this.getLine(docRow)) { - docColumn = Math.min(docColumn, this.getLine(docRow).length); + } else if (line) { + docColumn = Math.min(docColumn, line.length); } return { From d2cf4055f17ccaf063db4af950e082d8287117ca Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Mon, 31 Jan 2011 08:11:58 +0100 Subject: [PATCH 07/10] fix update issue when deleting lines --- lib/ace/editor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 2e45d887..9e2ea6db 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -264,7 +264,7 @@ var Editor =function(renderer, session) { var range = delta.range; this.bgTokenizer.start(range.start.row); - if (range.start.row == range.end.row && delta.action != "insertLines") + if (range.start.row == range.end.row && delta.action != "insertLines" && delta.action != "removeLines") var lastRow = range.end.row; else lastRow = Infinity; From 705395ea1fc810bc55ae2956975abf8115754446 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Mon, 31 Jan 2011 08:12:08 +0100 Subject: [PATCH 08/10] minor cleanups --- lib/ace/virtual_renderer.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/ace/virtual_renderer.js b/lib/ace/virtual_renderer.js index 6478b0f1..89844cd9 100644 --- a/lib/ace/virtual_renderer.js +++ b/lib/ace/virtual_renderer.js @@ -20,7 +20,7 @@ * the Initial Developer. All Rights Reserved. * * Contributor(s): - * Fabian Jakobs + * Fabian Jakobs * Irakli Gozalishvili (http://jeditoolkit.com) * Julian Viereck * @@ -96,12 +96,13 @@ var VirtualRenderer = function(container, theme) { column : 0 }; - var self = this; + var _self = this; this.$textLayer.addEventListener("changeCharaterSize", function() { - self.characterWidth = textLayer.getCharacterWidth(); - self.lineHeight = textLayer.getLineHeight(); + _self.characterWidth = textLayer.getCharacterWidth(); + _self.lineHeight = textLayer.getLineHeight(); + _self.$updatePrintMargin(); - self.$loop.schedule(self.CHANGE_FULL); + _self.$loop.schedule(_self.CHANGE_FULL); }); event.addListener(this.$gutter, "click", this.$onGutterClick.bind(this)); event.addListener(this.$gutter, "dblclick", this.$onGutterClick.bind(this)); @@ -272,6 +273,7 @@ var VirtualRenderer = function(container, theme) { if (!this.$showPrintMargin && !this.$printMarginEl) return; + if (!this.$printMarginEl) { containerEl = document.createElement("div"); containerEl.className = "ace_print_margin_layer"; From 7e6566b85b742cf206b4bc78b8ad4470f92dead4 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Mon, 31 Jan 2011 08:19:44 +0100 Subject: [PATCH 09/10] moveTextAreaToCursor after updating cursor, to get right position Conflicts: lib/ace/editor.js --- lib/ace/editor.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 9e2ea6db..f64f63db 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -280,17 +280,17 @@ var Editor =function(renderer, session) { }; this.onCursorChange = function(e) { - this.$highlightBrackets(); - - // move text input over the cursor - // this is required for iOS and IME - this.renderer.moveTextAreaToCursor(this.textInput.getElement()); - this.renderer.updateCursor(this.getCursorPosition(), this.$overwrite); if (!this.$blockScrolling && (!e || !e.blockScrolling)) { this.renderer.scrollCursorIntoView(); } + + // move text input over the cursor + // this is required for iOS and IME + this.renderer.moveTextAreaToCursor(this.textInput.getElement()); + + this.$highlightBrackets(); this.$updateHighlightActiveLine(); }; From 47371f841b7e01965b90e4f220aa9c49dbc2b18b Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Mon, 31 Jan 2011 08:21:09 +0100 Subject: [PATCH 10/10] fix css --- lib/ace/css/editor.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ace/css/editor.css b/lib/ace/css/editor.css index df204e58..af359ae9 100644 --- a/lib/ace/css/editor.css +++ b/lib/ace/css/editor.css @@ -74,7 +74,7 @@ } .ace_editor textarea { - position: "absolute"; + position: absolute; z-index: -1; opacity: 0; width: 10px;