From 8c1aa5c7723928283236efb358ac45f871ce219a Mon Sep 17 00:00:00 2001 From: nightwing Date: Tue, 22 Jul 2014 03:27:51 +0400 Subject: [PATCH] do not use automatic scroll into view prepares for deprecating the feature in the next version of ace --- lib/ace/autocomplete/popup.js | 3 +++ lib/ace/commands/default_commands.js | 17 ++++++++++++-- lib/ace/commands/multi_select_commands.js | 13 ++++++++++- lib/ace/editor.js | 27 +++++++++++++++++++++-- lib/ace/mouse/default_handlers.js | 15 +++++++------ lib/ace/multi_select.js | 27 ----------------------- 6 files changed, 63 insertions(+), 39 deletions(-) diff --git a/lib/ace/autocomplete/popup.js b/lib/ace/autocomplete/popup.js index f580e391..862a1c5e 100644 --- a/lib/ace/autocomplete/popup.js +++ b/lib/ace/autocomplete/popup.js @@ -209,6 +209,8 @@ var AcePopup = function(parentNode) { return this.screenWidth = 0; }; + popup.$blockScrolling = Infinity; + // public popup.isOpen = false; popup.isTopdown = false; @@ -241,6 +243,7 @@ var AcePopup = function(parentNode) { popup.on("changeSelection", function() { if (popup.isOpen) popup.setRow(popup.selection.lead.row); + popup.renderer.scrollCursorIntoView(); }); popup.hide = function() { diff --git a/lib/ace/commands/default_commands.js b/lib/ace/commands/default_commands.js index a150a76f..c5d34aae 100644 --- a/lib/ace/commands/default_commands.js +++ b/lib/ace/commands/default_commands.js @@ -205,12 +205,14 @@ exports.commands = [{ bindKey: bindKey("Shift-Up", "Shift-Up"), exec: function(editor) { editor.getSelection().selectUp(); }, multiSelectAction: "forEach", + scrollIntoView: "cursor", readOnly: true }, { name: "golineup", bindKey: bindKey("Up", "Up|Ctrl-P"), exec: function(editor, args) { editor.navigateUp(args.times); }, multiSelectAction: "forEach", + scrollIntoView: "cursor", readOnly: true }, { name: "selecttoend", @@ -395,12 +397,21 @@ exports.commands = [{ bindKey: bindKey("Ctrl-P", "Ctrl-P"), exec: function(editor) { editor.jumpToMatching(); }, multiSelectAction: "forEach", + scrollIntoView: "animate", readOnly: true }, { name: "selecttomatching", bindKey: bindKey("Ctrl-Shift-P", "Ctrl-Shift-P"), exec: function(editor) { editor.jumpToMatching(true); }, multiSelectAction: "forEach", + scrollIntoView: "animate", + readOnly: true +}, { + name: "expandToMatching", + bindKey: bindKey("Ctrl-Shift-M", "Ctrl-Shift-M"), + exec: function(editor) { editor.jumpToMatching(true, true); }, + multiSelectAction: "forEach", + scrollIntoView: "animate", readOnly: true }, { name: "passKeysToBrowser", @@ -458,11 +469,13 @@ exports.commands = [{ name: "modifyNumberUp", bindKey: bindKey("Ctrl-Shift-Up", "Alt-Shift-Up"), exec: function(editor) { editor.modifyNumber(1); }, + scrollIntoView: "cursor", multiSelectAction: "forEach" }, { name: "modifyNumberDown", bindKey: bindKey("Ctrl-Shift-Down", "Alt-Shift-Down"), exec: function(editor) { editor.modifyNumber(-1); }, + scrollIntoView: "cursor", multiSelectAction: "forEach" }, { name: "replace", @@ -629,7 +642,7 @@ exports.commands = [{ var isBackwards = editor.selection.isBackwards(); var selectionStart = isBackwards ? editor.selection.getSelectionLead() : editor.selection.getSelectionAnchor(); var selectionEnd = isBackwards ? editor.selection.getSelectionAnchor() : editor.selection.getSelectionLead(); - var firstLineEndCol = editor.session.doc.getLine(selectionStart.row).length + var firstLineEndCol = editor.session.doc.getLine(selectionStart.row).length; var selectedText = editor.session.doc.getTextRange(editor.selection.getRange()); var selectedCount = selectedText.replace(/\n\s*/, " ").length; var insertLine = editor.session.doc.getLine(selectionStart.row); @@ -640,7 +653,7 @@ exports.commands = [{ curLine = " " + curLine; } insertLine += curLine; - }; + } if (selectionEnd.row + 1 < (editor.session.doc.getLength() - 1)) { // Don't insert a newline at the end of the document diff --git a/lib/ace/commands/multi_select_commands.js b/lib/ace/commands/multi_select_commands.js index 347eb606..ba6392bc 100644 --- a/lib/ace/commands/multi_select_commands.js +++ b/lib/ace/commands/multi_select_commands.js @@ -35,41 +35,49 @@ exports.defaultCommands = [{ name: "addCursorAbove", exec: function(editor) { editor.selectMoreLines(-1); }, bindKey: {win: "Ctrl-Alt-Up", mac: "Ctrl-Alt-Up"}, + scrollIntoView: "cursor", readonly: true }, { name: "addCursorBelow", exec: function(editor) { editor.selectMoreLines(1); }, bindKey: {win: "Ctrl-Alt-Down", mac: "Ctrl-Alt-Down"}, + scrollIntoView: "cursor", readonly: true }, { name: "addCursorAboveSkipCurrent", exec: function(editor) { editor.selectMoreLines(-1, true); }, bindKey: {win: "Ctrl-Alt-Shift-Up", mac: "Ctrl-Alt-Shift-Up"}, + scrollIntoView: "cursor", readonly: true }, { name: "addCursorBelowSkipCurrent", exec: function(editor) { editor.selectMoreLines(1, true); }, bindKey: {win: "Ctrl-Alt-Shift-Down", mac: "Ctrl-Alt-Shift-Down"}, + scrollIntoView: "cursor", readonly: true }, { name: "selectMoreBefore", exec: function(editor) { editor.selectMore(-1); }, bindKey: {win: "Ctrl-Alt-Left", mac: "Ctrl-Alt-Left"}, + scrollIntoView: "cursor", readonly: true }, { name: "selectMoreAfter", exec: function(editor) { editor.selectMore(1); }, bindKey: {win: "Ctrl-Alt-Right", mac: "Ctrl-Alt-Right"}, + scrollIntoView: "cursor", readonly: true }, { name: "selectNextBefore", exec: function(editor) { editor.selectMore(-1, true); }, bindKey: {win: "Ctrl-Alt-Shift-Left", mac: "Ctrl-Alt-Shift-Left"}, + scrollIntoView: "cursor", readonly: true }, { name: "selectNextAfter", exec: function(editor) { editor.selectMore(1, true); }, bindKey: {win: "Ctrl-Alt-Shift-Right", mac: "Ctrl-Alt-Shift-Right"}, + scrollIntoView: "cursor", readonly: true }, { name: "splitIntoLines", @@ -79,11 +87,13 @@ exports.defaultCommands = [{ }, { name: "alignCursors", exec: function(editor) { editor.alignCursors(); }, - bindKey: {win: "Ctrl-Alt-A", mac: "Ctrl-Alt-A"} + bindKey: {win: "Ctrl-Alt-A", mac: "Ctrl-Alt-A"}, + scrollIntoView: "cursor" }, { name: "findAll", exec: function(editor) { editor.findAll(); }, bindKey: {win: "Ctrl-Alt-K", mac: "Ctrl-Alt-G"}, + scrollIntoView: "cursor", readonly: true }]; @@ -92,6 +102,7 @@ exports.multiSelectCommands = [{ name: "singleSelection", bindKey: "esc", exec: function(editor) { editor.exitMultiSelectMode(); }, + scrollIntoView: "cursor", readonly: true, isAvailable: function(editor) {return editor && editor.inMultiSelectMode} }]; diff --git a/lib/ace/editor.js b/lib/ace/editor.js index 59b136b1..a3fdc978 100644 --- a/lib/ace/editor.js +++ b/lib/ace/editor.js @@ -150,7 +150,8 @@ var Editor = function(renderer, session) { args: commadEvent.args, scrollTop: this.renderer.scrollTop }; - + if (this.curOp.command.name) + this.$blockScrolling++; // this.selections.push(this.selection.toJSON()); }; @@ -160,6 +161,8 @@ var Editor = function(renderer, session) { return this.curOp = null; this._signal("beforeEndOperation"); var command = this.curOp.command; + if (command.name && this.$blockScrolling) + this.$blockScrolling--; if (command && command.scrollIntoView) { switch (command.scrollIntoView) { case "center": @@ -714,6 +717,7 @@ var Editor = function(renderer, session) { this.$cursorChange(); if (!this.$blockScrolling) { + console.warn("In next version of ace automatic scrolling into view will be disabled"); this.renderer.scrollCursorIntoView(); } @@ -902,9 +906,28 @@ var Editor = function(renderer, session) { // todo this should change when paste becomes a command if (this.$readOnly) return; + var e = {text: text}; this._signal("paste", e); - this.insert(e.text, true); + text = e.text; + if (!this.inMultiSelectMode || this.inVirtualSelectionMode) { + this.insert(text); + } else { + var lines = text.split(/\r\n|\r|\n/); + var ranges = this.selection.rangeList.ranges; + + if (lines.length > ranges.length || lines.length < 2 || !lines[1]) + return this.commands.exec("insertstring", this, text); + + for (var i = ranges.length; i--;) { + var range = ranges[i]; + if (!range.isEmpty()) + this.session.remove(range); + + this.session.insert(range.start, lines[i]); + } + } + this.renderer.scrollCursorIntoView(); }; this.execCommand = function(command, args) { diff --git a/lib/ace/mouse/default_handlers.js b/lib/ace/mouse/default_handlers.js index bbd85484..a48d1097 100644 --- a/lib/ace/mouse/default_handlers.js +++ b/lib/ace/mouse/default_handlers.js @@ -70,10 +70,10 @@ function DefaultHandlers(mouseHandler) { if (button !== 0) { var selectionRange = editor.getSelectionRange(); var selectionEmpty = selectionRange.isEmpty(); - + editor.$blockScrolling++; if (selectionEmpty) editor.selection.moveToPosition(pos); - + editor.$blockScrolling--; // 2: contextmenu, 1: linux paste editor.textInput.onContextMenu(ev.domEvent); return; // stopping event here breaks contextmenu on ff mac @@ -100,7 +100,7 @@ function DefaultHandlers(mouseHandler) { pos = pos || this.editor.renderer.screenToTextCoordinates(this.x, this.y); var editor = this.editor; // allow double/triple click handlers to change selection - + editor.$blockScrolling++; if (this.mousedownEvent.getShiftKey()) editor.selection.selectToPosition(pos); else if (!waitForClickSelection) @@ -112,12 +112,13 @@ function DefaultHandlers(mouseHandler) { } editor.setStyle("ace_selecting"); this.setState("select"); + editor.$blockScrolling--; }; this.select = function() { var anchor, editor = this.editor; var cursor = editor.renderer.screenToTextCoordinates(this.x, this.y); - + editor.$blockScrolling++; if (this.$clickSelection) { var cmp = this.$clickSelection.comparePoint(cursor); @@ -133,7 +134,7 @@ function DefaultHandlers(mouseHandler) { editor.selection.setSelectionAnchor(anchor.row, anchor.column); } editor.selection.selectToPosition(cursor); - + editor.$blockScrolling--; editor.renderer.scrollCursorIntoView(); }; @@ -141,7 +142,7 @@ function DefaultHandlers(mouseHandler) { var anchor, editor = this.editor; var cursor = editor.renderer.screenToTextCoordinates(this.x, this.y); var range = editor.selection[unitName](cursor.row, cursor.column); - + editor.$blockScrolling++; if (this.$clickSelection) { var cmpStart = this.$clickSelection.comparePoint(range.start); var cmpEnd = this.$clickSelection.comparePoint(range.end); @@ -165,7 +166,7 @@ function DefaultHandlers(mouseHandler) { editor.selection.setSelectionAnchor(anchor.row, anchor.column); } editor.selection.selectToPosition(cursor); - + editor.$blockScrolling--; editor.renderer.scrollCursorIntoView(); }; diff --git a/lib/ace/multi_select.js b/lib/ace/multi_select.js index 729d5253..0635afc2 100644 --- a/lib/ace/multi_select.js +++ b/lib/ace/multi_select.js @@ -558,33 +558,6 @@ var Editor = require("./editor").Editor; } }; - // todo this should change when paste becomes a command - this.onPaste = function(text) { - if (this.$readOnly) - return; - - - var e = {text: text}; - this._signal("paste", e); - text = e.text; - if (!this.inMultiSelectMode || this.inVirtualSelectionMode) - return this.insert(text); - - var lines = text.split(/\r\n|\r|\n/); - var ranges = this.selection.rangeList.ranges; - - if (lines.length > ranges.length || lines.length < 2 || !lines[1]) - return this.commands.exec("insertstring", this, text); - - for (var i = ranges.length; i--;) { - var range = ranges[i]; - if (!range.isEmpty()) - this.session.remove(range); - - this.session.insert(range.start, lines[i]); - } - }; - /** * Finds and selects all the occurences of `needle`. * @param {String} The text to find