fix ctrl-shift-mousedown

This commit is contained in:
nightwing 2014-05-21 20:07:07 +04:00
commit 9ab12a6ed3
6 changed files with 99 additions and 44 deletions

View file

@ -306,13 +306,7 @@ var Autocomplete = function() {
}; };
this.cancelContextMenu = function() { this.cancelContextMenu = function() {
var stop = function(e) { this.editor.$mouseHandler.cancelContextMenu();
this.editor.off("nativecontextmenu", stop);
if (e && e.domEvent)
event.stopEvent(e.domEvent);
}.bind(this);
setTimeout(stop, 10);
this.editor.on("nativecontextmenu", stop);
}; };
}).call(Autocomplete.prototype); }).call(Autocomplete.prototype);

View file

@ -85,7 +85,7 @@ exports.preventDefault = function(e) {
exports.getButton = function(e) { exports.getButton = function(e) {
if (e.type == "dblclick") if (e.type == "dblclick")
return 0; return 0;
if (e.type == "contextmenu" || (e.ctrlKey && useragent.isMac)) if (e.type == "contextmenu" || (useragent.isMac && (e.ctrlKey && !e.altKey && !e.shiftKey)))
return 2; return 2;
// DOM Event // DOM Event
@ -183,7 +183,7 @@ exports.addMultiMouseDownListener = function(el, timeouts, eventHandler, callbac
if (!timer || isNewClick) if (!timer || isNewClick)
clicks = 1; clicks = 1;
if (timer) if (timer)
clearTimeout(timer) clearTimeout(timer);
timer = setTimeout(function() {timer = null}, timeouts[clicks - 1] || 600); timer = setTimeout(function() {timer = null}, timeouts[clicks - 1] || 600);
if (clicks == 1) { if (clicks == 1) {

View file

@ -157,6 +157,17 @@ var MouseHandler = function(editor) {
var timerId = setInterval(onCaptureInterval, 20); var timerId = setInterval(onCaptureInterval, 20);
}; };
this.releaseMouse = null; this.releaseMouse = null;
this.cancelContextMenu = function() {
var stop = function(e) {
if (e && e.domEvent && e.domEvent.type != "contextmenu")
return;
this.editor.off("nativecontextmenu", stop);
if (e && e.domEvent)
event.stopEvent(e.domEvent);
}.bind(this);
setTimeout(stop, 10);
this.editor.on("nativecontextmenu", stop);
};
}).call(MouseHandler.prototype); }).call(MouseHandler.prototype);
config.defineOptions(MouseHandler.prototype, "mouseHandler", { config.defineOptions(MouseHandler.prototype, "mouseHandler", {

View file

@ -31,6 +31,7 @@
define(function(require, exports, module) { define(function(require, exports, module) {
var event = require("../lib/event"); var event = require("../lib/event");
var useragent = require("../lib/useragent");
// mouse // mouse
function isSamePoint(p1, p2) { function isSamePoint(p1, p2) {
@ -41,9 +42,13 @@ function onMouseDown(e) {
var ev = e.domEvent; var ev = e.domEvent;
var alt = ev.altKey; var alt = ev.altKey;
var shift = ev.shiftKey; var shift = ev.shiftKey;
var ctrl = e.getAccelKey(); var ctrl = ev.ctrlKey;
var accel = e.getAccelKey();
var button = e.getButton(); var button = e.getButton();
if (ctrl && useragent.isMac)
button = ev.button;
if (e.editor.inMultiSelectMode && button == 2) { if (e.editor.inMultiSelectMode && button == 2) {
e.editor.textInput.onContextMenu(e.domEvent); e.editor.textInput.onContextMenu(e.domEvent);
return; return;
@ -55,6 +60,9 @@ function onMouseDown(e) {
return; return;
} }
if (button !== 0)
return;
var editor = e.editor; var editor = e.editor;
var selection = editor.selection; var selection = editor.selection;
var isMultiSelect = editor.inMultiSelectMode; var isMultiSelect = editor.inMultiSelectMode;
@ -91,9 +99,27 @@ function onMouseDown(e) {
var screenAnchor = editor.renderer.pixelToScreenCoordinates(mouseX, mouseY); var screenAnchor = editor.renderer.pixelToScreenCoordinates(mouseX, mouseY);
var screenCursor = screenAnchor; var screenCursor = screenAnchor;
var selectionMode;
if (editor.$mouseHandler.$enableJumpToDef) {
if (ctrl && alt || accel && alt)
selectionMode = "add";
else if (alt)
selectionMode = "block";
} else {
if (accel && !alt) {
selectionMode = "add";
if (!isMultiSelect && shift)
return;
} else if (alt) {
selectionMode = "block";
}
}
if (selectionMode && useragent.isMac && ev.ctrlKey) {
editor.$mouseHandler.cancelContextMenu();
}
if (ctrl && !alt && !shift && button === 0) { if (selectionMode == "add") {
if (!isMultiSelect && inSelection) if (!isMultiSelect && inSelection)
return; // dragging return; // dragging
@ -104,23 +130,34 @@ function onMouseDown(e) {
var oldRange = selection.rangeList.rangeAtPoint(pos); var oldRange = selection.rangeList.rangeAtPoint(pos);
editor.$blockScrolling++; editor.$blockScrolling++;
editor.inVirtualSelectionMode = true;
if (shift) {
oldRange = null;
range = selection.ranges[0];
editor.removeSelectionMarker(range);
}
editor.once("mouseup", function() { editor.once("mouseup", function() {
var tmpSel = selection.toOrientedRange(); var tmpSel = selection.toOrientedRange();
if (oldRange && tmpSel.isEmpty() && isSamePoint(oldRange.cursor, tmpSel.cursor)) if (oldRange && tmpSel.isEmpty() && isSamePoint(oldRange.cursor, tmpSel.cursor))
selection.substractPoint(tmpSel.cursor); selection.substractPoint(tmpSel.cursor);
else { else {
if (range) { if (shift) {
selection.substractPoint(range.cursor);
} else if (range) {
editor.removeSelectionMarker(range); editor.removeSelectionMarker(range);
selection.addRange(range); selection.addRange(range);
} }
selection.addRange(tmpSel); selection.addRange(tmpSel);
} }
editor.$blockScrolling--; editor.$blockScrolling--;
editor.inVirtualSelectionMode = false;
}); });
} else if (alt && button === 0) { } else if (selectionMode == "block") {
e.stop(); e.stop();
if (isMultiSelect && !ctrl) if (isMultiSelect && !ctrl)

View file

@ -76,7 +76,7 @@ var EditSession = require("./edit_session").EditSession;
if (!range) if (!range)
return; return;
if (!this.inMultiSelectMode && this.rangeCount == 0) { if (!this.inMultiSelectMode && this.rangeCount === 0) {
var oldRange = this.toOrientedRange(); var oldRange = this.toOrientedRange();
this.rangeList.add(oldRange); this.rangeList.add(oldRange);
this.rangeList.add(range); this.rangeList.add(range);
@ -168,7 +168,7 @@ var EditSession = require("./edit_session").EditSession;
this._signal("removeRange", {ranges: removed}); this._signal("removeRange", {ranges: removed});
if (this.rangeCount == 0 && this.inMultiSelectMode) { if (this.rangeCount === 0 && this.inMultiSelectMode) {
this.inMultiSelectMode = false; this.inMultiSelectMode = false;
this._signal("singleSelect"); this._signal("singleSelect");
this.session.$undoSelect = true; this.session.$undoSelect = true;
@ -544,6 +544,19 @@ var Editor = require("./editor").Editor;
return text; return text;
}; };
this.$checkMultiselectChange = function(e, anchor) {
if (this.inMultiSelectMode && !this.inVirtualSelectionMode) {
var range = this.multiSelect.ranges[0];
if (this.multiSelect.isEmpty() && anchor == this.multiSelect.anchor)
return;
var pos = anchor == this.multiSelect.anchor
? range.cursor == range.start ? range.end : range.start
: range.cursor;
if (!isSamePoint(pos, anchor))
this.multiSelect.toSingleRange(this.multiSelect.toOrientedRange());
}
};
// todo this should change when paste becomes a command // todo this should change when paste becomes a command
this.onPaste = function(text) { this.onPaste = function(text) {
if (this.$readOnly) if (this.$readOnly)
@ -742,8 +755,15 @@ var Editor = require("./editor").Editor;
var session = this.session; var session = this.session;
var sel = session.multiSelect; var sel = session.multiSelect;
var ranges = sel.ranges; var ranges = sel.ranges;
// filter out ranges on same row
var row = -1;
var sameRowRanges = ranges.filter(function(r) {
if (r.cursor.row == row)
return true;
row = r.cursor.row;
});
if (!ranges.length) { if (!ranges.length || sameRowRanges.length == ranges.length - 1) {
var range = this.selection.getRange(); var range = this.selection.getRange();
var fr = range.start.row, lr = range.end.row; var fr = range.start.row, lr = range.end.row;
var guessRange = fr == lr; var guessRange = fr == lr;
@ -769,14 +789,9 @@ var Editor = require("./editor").Editor;
} }
this.selection.setRange(range); this.selection.setRange(range);
} else { } else {
// filter out ranges on same row sameRowRanges.forEach(function(r) {
var row = -1; sel.substractPoint(r.cursor);
var sameRowRanges = ranges.filter(function(r) {
if (r.cursor.row == row)
return true;
row = r.cursor.row;
}); });
sel.$onRemoveRange(sameRowRanges);
var maxCol = 0; var maxCol = 0;
var minSpace = Infinity; var minSpace = Infinity;
@ -851,19 +866,19 @@ var Editor = require("./editor").Editor;
function alignLeft(m) { function alignLeft(m) {
return !m[2] ? m[0] : spaces(startW) + m[2] return !m[2] ? m[0] : spaces(startW) + m[2]
+ spaces(textW - m[2].length + endW) + spaces(textW - m[2].length + endW)
+ m[4].replace(/^([=:])\s+/, "$1 ") + m[4].replace(/^([=:])\s+/, "$1 ");
} }
function alignRight(m) { function alignRight(m) {
return !m[2] ? m[0] : spaces(startW + textW - m[2].length) + m[2] return !m[2] ? m[0] : spaces(startW + textW - m[2].length) + m[2]
+ spaces(endW, " ") + spaces(endW, " ")
+ m[4].replace(/^([=:])\s+/, "$1 ") + m[4].replace(/^([=:])\s+/, "$1 ");
} }
function unAlign(m) { function unAlign(m) {
return !m[2] ? m[0] : spaces(startW) + m[2] return !m[2] ? m[0] : spaces(startW) + m[2]
+ spaces(endW) + spaces(endW)
+ m[4].replace(/^([=:])\s+/, "$1 ") + m[4].replace(/^([=:])\s+/, "$1 ");
} }
} };
}).call(Editor.prototype); }).call(Editor.prototype);
@ -884,16 +899,20 @@ exports.onSessionChange = function(e) {
var oldSession = e.oldSession; var oldSession = e.oldSession;
if (oldSession) { if (oldSession) {
oldSession.multiSelect.removeEventListener("addRange", this.$onAddRange); oldSession.multiSelect.off("addRange", this.$onAddRange);
oldSession.multiSelect.removeEventListener("removeRange", this.$onRemoveRange); oldSession.multiSelect.off("removeRange", this.$onRemoveRange);
oldSession.multiSelect.removeEventListener("multiSelect", this.$onMultiSelect); oldSession.multiSelect.off("multiSelect", this.$onMultiSelect);
oldSession.multiSelect.removeEventListener("singleSelect", this.$onSingleSelect); oldSession.multiSelect.off("singleSelect", this.$onSingleSelect);
oldSession.multiSelect.lead.off("change", this.$checkMultiselectChange);
oldSession.multiSelect.anchor.off("change", this.$checkMultiselectChange);
} }
session.multiSelect.on("addRange", this.$onAddRange); session.multiSelect.on("addRange", this.$onAddRange);
session.multiSelect.on("removeRange", this.$onRemoveRange); session.multiSelect.on("removeRange", this.$onRemoveRange);
session.multiSelect.on("multiSelect", this.$onMultiSelect); session.multiSelect.on("multiSelect", this.$onMultiSelect);
session.multiSelect.on("singleSelect", this.$onSingleSelect); session.multiSelect.on("singleSelect", this.$onSingleSelect);
session.multiSelect.lead.on("change", this.$checkMultiselectChange);
session.multiSelect.anchor.on("change", this.$checkMultiselectChange);
// this.$onSelectionChange = this.onSelectionChange.bind(this); // this.$onSelectionChange = this.onSelectionChange.bind(this);
@ -916,6 +935,7 @@ function MultiSelect(editor) {
editor.$onMultiSelect = editor.$onMultiSelect.bind(editor); editor.$onMultiSelect = editor.$onMultiSelect.bind(editor);
editor.$onSingleSelect = editor.$onSingleSelect.bind(editor); editor.$onSingleSelect = editor.$onSingleSelect.bind(editor);
editor.$multiselectOnSessionChange = exports.onSessionChange.bind(editor); editor.$multiselectOnSessionChange = exports.onSessionChange.bind(editor);
editor.$checkMultiselectChange = editor.$checkMultiselectChange.bind(editor);
editor.$multiselectOnSessionChange(editor); editor.$multiselectOnSessionChange(editor);
editor.on("changeSession", editor.$multiselectOnSessionChange); editor.on("changeSession", editor.$multiselectOnSessionChange);
@ -969,7 +989,7 @@ require("./config").defineOptions(Editor.prototype, "editor", {
}, },
value: true value: true
} }
}) });

View file

@ -37,7 +37,6 @@ var EventEmitter = require("./lib/event_emitter").EventEmitter;
var Range = require("./range").Range; var Range = require("./range").Range;
/** /**
*
* Contains the cursor position and the text selection of an edit session. * Contains the cursor position and the text selection of an edit session.
* *
* The row/columns used in the selection are in document coordinates representing ths coordinates as thez appear in the document before applying soft wrap and folding. * The row/columns used in the selection are in document coordinates representing ths coordinates as thez appear in the document before applying soft wrap and folding.
@ -49,22 +48,16 @@ var Range = require("./range").Range;
* Emitted when the cursor position changes. * Emitted when the cursor position changes.
* @event changeCursor * @event changeCursor
* *
*
*
**/ **/
/** /**
* Emitted when the cursor selection changes. * Emitted when the cursor selection changes.
*
* @event changeSelection * @event changeSelection
*
*
*
**/ **/
/** /**
* Creates a new `Selection` object. * Creates a new `Selection` object.
* @param {EditSession} session The session to use * @param {EditSession} session The session to use
* *
*
*
* @constructor * @constructor
**/ **/
var Selection = function(session) { var Selection = function(session) {