Skip to content

Commit

Permalink
Track last copied text, in order to find selection boundaries on paste
Browse files Browse the repository at this point in the history
Issue #2697
  • Loading branch information
marijnh committed Jul 15, 2014
1 parent 13acf66 commit f4ae5b4
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,11 @@
cm.display.poll.set(20, p);
}

// This will be set to an array of strings when copying, so that,
// when pasting, we know what kind of selections the copied text
// was made out of.
var lastCopied = null;

// Read input from the textarea, and update the document to match.
// When something is selected, it is present in the textarea, and
// selected (unless it is huge, in which case a placeholder is
Expand Down Expand Up @@ -2409,7 +2414,13 @@
var inserted = text.slice(same), textLines = splitLines(inserted);

// When pasing N lines into N selections, insert one line per selection
var multiPaste = cm.state.pasteIncoming && textLines.length > 1 && doc.sel.ranges.length == textLines.length;
var multiPaste = null;
if (cm.state.pasteIncoming && doc.sel.ranges.length > 1) {
if (lastCopied && lastCopied.join("\n") == inserted)
multiPaste = lastCopied.length == doc.sel.ranges.length && map(lastCopied, splitLines);
else if (textLines.length == doc.sel.ranges.length)
multiPaste = map(textLines, function(l) { return [l]; });
}

// Normal behavior is to insert the new text into every selection
for (var i = doc.sel.ranges.length - 1; i >= 0; i--) {
Expand All @@ -2422,7 +2433,7 @@
else if (cm.state.overwrite && range.empty() && !cm.state.pasteIncoming)
to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length));
var updateInput = cm.curOp.updateInput;
var changeEvent = {from: from, to: to, text: multiPaste ? [textLines[i]] : textLines,
var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i] : textLines,
origin: cm.state.pasteIncoming ? "paste" : cm.state.cutIncoming ? "cut" : "+input"};
makeChange(cm.doc, changeEvent);
signalLater(cm, "inputRead", cm, changeEvent);
Expand Down Expand Up @@ -2589,27 +2600,29 @@

function prepareCopyCut(e) {
if (cm.somethingSelected()) {
lastCopied = cm.getSelections();
if (d.inaccurateSelection) {
d.prevInput = "";
d.inaccurateSelection = false;
d.input.value = cm.getSelection();
d.input.value = lastCopied.join("\n");
selectInput(d.input);
}
} else {
var text = "", ranges = [];
var text = [], ranges = [];
for (var i = 0; i < cm.doc.sel.ranges.length; i++) {
var line = cm.doc.sel.ranges[i].head.line;
var lineRange = {anchor: Pos(line, 0), head: Pos(line + 1, 0)};
ranges.push(lineRange);
text += cm.getRange(lineRange.anchor, lineRange.head);
text.push(cm.getRange(lineRange.anchor, lineRange.head));
}
if (e.type == "cut") {
cm.setSelections(ranges, null, sel_dontScroll);
} else {
d.prevInput = "";
d.input.value = text;
d.input.value = text.join("\n");
selectInput(d.input);
}
lastCopied = text;
}
if (e.type == "cut") cm.state.cutIncoming = true;
}
Expand Down

0 comments on commit f4ae5b4

Please sign in to comment.