MCPcopy
hub / github.com/jagenjo/webglstudio.js / applyTextInput

Function applyTextInput

editor/js/extra/codemirror/codemirror.js:1103–1146  ·  view source on GitHub ↗
(cm, inserted, deleted, sel, origin)

Source from the content-addressed store, hash-verified

1101 var lastCopied = null;
1102
1103 function applyTextInput(cm, inserted, deleted, sel, origin) {
1104 var doc = cm.doc;
1105 cm.display.shift = false;
1106 if (!sel) sel = doc.sel;
1107
1108 var paste = cm.state.pasteIncoming || origin == "paste";
1109 var textLines = doc.splitLines(inserted), multiPaste = null;
1110 // When pasing N lines into N selections, insert one line per selection
1111 if (paste && sel.ranges.length > 1) {
1112 if (lastCopied && lastCopied.join("\n") == inserted) {
1113 if (sel.ranges.length % lastCopied.length == 0) {
1114 multiPaste = [];
1115 for (var i = 0; i < lastCopied.length; i++)
1116 multiPaste.push(doc.splitLines(lastCopied[i]));
1117 }
1118 } else if (textLines.length == sel.ranges.length) {
1119 multiPaste = map(textLines, function(l) { return [l]; });
1120 }
1121 }
1122
1123 // Normal behavior is to insert the new text into every selection
1124 for (var i = sel.ranges.length - 1; i >= 0; i--) {
1125 var range = sel.ranges[i];
1126 var from = range.from(), to = range.to();
1127 if (range.empty()) {
1128 if (deleted && deleted > 0) // Handle deletion
1129 from = Pos(from.line, from.ch - deleted);
1130 else if (cm.state.overwrite && !paste) // Handle overwrite
1131 to = Pos(to.line, Math.min(getLine(doc, to.line).text.length, to.ch + lst(textLines).length));
1132 }
1133 var updateInput = cm.curOp.updateInput;
1134 var changeEvent = {from: from, to: to, text: multiPaste ? multiPaste[i % multiPaste.length] : textLines,
1135 origin: origin || (paste ? "paste" : cm.state.cutIncoming ? "cut" : "+input")};
1136 makeChange(cm.doc, changeEvent);
1137 signalLater(cm, "inputRead", cm, changeEvent);
1138 }
1139 if (inserted && !paste)
1140 triggerElectric(cm, inserted);
1141
1142 ensureCursorVisible(cm);
1143 cm.curOp.updateInput = updateInput;
1144 cm.curOp.typing = true;
1145 cm.state.pasteIncoming = cm.state.cutIncoming = false;
1146 }
1147
1148 function handlePaste(e, cm) {
1149 var pasted = e.clipboardData && e.clipboardData.getData("text/plain");

Callers 2

handlePasteFunction · 0.85
codemirror.jsFile · 0.85

Calls 7

mapFunction · 0.85
getLineFunction · 0.85
lstFunction · 0.85
makeChangeFunction · 0.85
signalLaterFunction · 0.85
triggerElectricFunction · 0.85
ensureCursorVisibleFunction · 0.85

Tested by

no test coverage detected