Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak copyWithEmptySelection feature #3452

Merged
merged 2 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/ace/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2012, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */

define(function(require, exports, module) {
"use strict";

module.exports = { lineMode: false };

});
8 changes: 4 additions & 4 deletions lib/ace/commands/default_commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,13 @@ exports.commands = [{
{
name: "cut",
exec: function(editor) {
var range = editor.getSelectionRange();
var cutLine = editor.$copyWithEmptySelection && editor.selection.isEmpty();
var range = cutLine ? editor.selection.getLineRange() : editor.selection.getRange();
editor._emit("cut", range);

if (!editor.selection.isEmpty()) {
if (!range.isEmpty())
editor.session.remove(range);
editor.clearSelection();
}
editor.clearSelection();
},
scrollIntoView: "cursor",
multiSelectAction: "forEach"
Expand Down
43 changes: 37 additions & 6 deletions lib/ace/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ var defaultCommands = require("./commands/default_commands").commands;
var config = require("./config");
var TokenIterator = require("./token_iterator").TokenIterator;

var clipboard = require("./clipboard");

/**
* The main entry point into the Ace functionality.
*
Expand Down Expand Up @@ -889,12 +891,25 @@ Editor.$uid = 0;
/**
* Returns the string of text currently highlighted.
* @returns {String}
* @deprecated Use getSelectedText instead.
**/
this.getCopyText = function() {
var text = this.getSelectedText();
this._signal("copy", text);
return text;
var nl = this.session.doc.getNewLineCharacter();
var copyLine= false;
if (!text && this.$copyWithEmptySelection) {
copyLine = true;
var ranges = this.selection.getAllRanges();
for (var i = 0; i < ranges.length; i++) {
var range = ranges[i];
if (i && ranges[i - 1].start.row == range.start.row)
continue;
text += this.session.getLine(range.start.row) + nl;
}
}
var e = {text: text};
this._signal("copy", e);
clipboard.lineMode = copyLine ? e.text : "";
return e.text;
};

/**
Expand Down Expand Up @@ -934,8 +949,18 @@ Editor.$uid = 0;
e = {text: e};
this._signal("paste", e);
var text = e.text;

var lineMode = text == clipboard.lineMode;
var session = this.session;
if (!this.inMultiSelectMode || this.inVirtualSelectionMode) {
this.insert(text);
if (lineMode)
session.insert({ row: this.selection.lead.row, column: 0 }, text);
else
this.insert(text);
} else if (lineMode) {
this.selection.rangeList.ranges.forEach(function(range) {
session.insert({ row: range.start.row, column: 0 }, text);
});
} else {
var lines = text.split(/\r\n|\r|\n/);
var ranges = this.selection.rangeList.ranges;
Expand All @@ -946,9 +971,9 @@ Editor.$uid = 0;
for (var i = ranges.length; i--;) {
var range = ranges[i];
if (!range.isEmpty())
this.session.remove(range);
session.remove(range);

this.session.insert(range.start, lines[i]);
session.insert(range.start, lines[i]);
}
}
};
Expand Down Expand Up @@ -2642,6 +2667,12 @@ config.defineOptions(Editor.prototype, "editor", {
},
initialValue: false
},
copyWithEmptySelection: {
set: function(value) {
this.textInput.setCopyWithEmptySelection(value);
},
initialValue: false
},
cursorStyle: {
set: function(val) { this.$resetCursorStyle(); },
values: ["ace", "slim", "smooth", "wide"],
Expand Down
6 changes: 6 additions & 0 deletions lib/ace/keyboard/textinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var TextInput = function(parentNode, host) {
var inComposition = false;
var tempStyle = '';
var isSelectionEmpty = true;
var copyWithEmptySelection = false;

// FOCUS
// ie9 throws error if document.activeElement is accessed too soon
Expand Down Expand Up @@ -106,6 +107,7 @@ var TextInput = function(parentNode, host) {
});

function resetSelection(isEmpty) {
isEmpty = copyWithEmptySelection ? false : isEmpty;
if (inComposition)
return;

Expand Down Expand Up @@ -410,6 +412,10 @@ var TextInput = function(parentNode, host) {
text.readOnly = readOnly;
};

this.setCopyWithEmptySelection = function(value) {
copyWithEmptySelection = value;
};

this.onContextMenu = function(e) {
afterContextMenu = true;
resetSelection(host.selection.isEmpty());
Expand Down