Skip to content

Commit 9a7c474

Browse files
committed
Allow dragging out of a CodeMirror instance
Closes #163
1 parent 8dee1d4 commit 9a7c474

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

lib/codemirror.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var CodeMirror = (function() {
2828
'<div style="position: relative">' + // Moved around its parent to cover visible view
2929
'<div class="CodeMirror-gutter"><div class="CodeMirror-gutter-text"></div></div>' +
3030
// Provides positioning relative to (visible) text origin
31-
'<div class="CodeMirror-lines"><div style="position: relative">' +
31+
'<div class="CodeMirror-lines"><div style="position: relative" draggable="true">' +
3232
'<pre class="CodeMirror-cursor">&#160;</pre>' + // Absolutely positioned blinky cursor
3333
'<div></div>' + // This DIV contains the actual code
3434
'</div></div></div></div></div>';
@@ -80,6 +80,7 @@ var CodeMirror = (function() {
8080

8181
// Register our event handlers.
8282
connect(scroller, "mousedown", operation(onMouseDown));
83+
connect(lineSpace, "dragstart", onDragStart);
8384
// Gecko browsers fire contextmenu *after* opening the menu, at
8485
// which point we can't mess with it anymore. Context menu is
8586
// handled in onMouseDown for Gecko.
@@ -262,18 +263,24 @@ var CodeMirror = (function() {
262263
if (!start) {if (e_target(e) == scroller) e_preventDefault(e); return;}
263264

264265
if (!focused) onFocus();
265-
e_preventDefault(e);
266266

267267
var now = +new Date;
268268
if (lastDoubleClick > now - 400) {
269+
e_preventDefault(e);
269270
return selectLine(start.line);
270271
} else if (lastClick > now - 400) {
271272
lastDoubleClick = now;
273+
e_preventDefault(e);
272274
return selectWordAt(start);
273275
} else { lastClick = now; }
274276

275-
setCursor(start.line, start.ch, true);
276277
var last = start, going;
278+
if (!posLess(start, sel.from) && !posLess(sel.to, start)) {
279+
// Let the drag handler handle this.
280+
return;
281+
}
282+
e_preventDefault(e);
283+
setCursor(start.line, start.ch, true);
277284

278285
function extend(e) {
279286
var cur = posFromMouse(e, true);
@@ -327,6 +334,13 @@ var CodeMirror = (function() {
327334
catch(e){}
328335
}
329336
}
337+
function onDragStart(e) {
338+
var txt = getSelection();
339+
// This will reset escapeElement
340+
htmlEscape(txt);
341+
e.dataTransfer.setDragImage(escapeElement, 0, 0);
342+
e.dataTransfer.setData("Text", txt);
343+
}
330344
function onKeyDown(e) {
331345
if (!focused) onFocus();
332346

0 commit comments

Comments
 (0)