Skip to content

Commit 5c7362b

Browse files
committed
fix: safari code mirror drack creates ghost image
1 parent 6729ed6 commit 5c7362b

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/editor/Editor.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,31 @@ define(function (require, exports, module) {
375375
readOnly: isReadOnly
376376
});
377377

378+
// Override default drag image in Safari (and harmless in others):
379+
// Safari shows a text image by default when dragging from CodeMirror,
380+
// which can be visually distracting. Use a 1x1 transparent image instead.
381+
// Note: The CodeMirror "wrapper" element (returned by getWrapperElement()) is NOT the same
382+
// as the "container" we pass to new CodeMirror(container, ...). CodeMirror creates its own
383+
// wrapper inside that container. We attach the drag listener to the wrapper so it captures
384+
// drags originating from the editor surface reliably across browsers.
385+
try {
386+
const wrapperEl = self._codeMirror.getWrapperElement();
387+
if (wrapperEl) {
388+
// Create once per editor instance
389+
const transparentImg = new Image();
390+
transparentImg.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
391+
function handleCMDragStart(e) {
392+
if (e && e.dataTransfer && typeof e.dataTransfer.setDragImage === "function") {
393+
e.dataTransfer.setDragImage(transparentImg, 0, 0);
394+
}
395+
}
396+
wrapperEl.addEventListener("dragstart", handleCMDragStart);
397+
// No explicit removal necessary since the wrapper element is removed on editor.destroy().
398+
}
399+
} catch (err) {
400+
// Fail silently; drag image override is non-critical.
401+
}
402+
378403
// Can't get CodeMirror's focused state without searching for
379404
// CodeMirror-focused. Instead, track focus via onFocus and onBlur
380405
// options and track state with this._focused
@@ -953,18 +978,18 @@ define(function (require, exports, module) {
953978
};
954979

955980
/**
956-
* Takes an anchor/head pair and returns a start/end pair where the start is guaranteed to be <= end,
981+
* Takes an anchor/head pair and returns a start/end pair where the start is guaranteed to be <= end,
957982
* and a "reversed" flag indicating if the head is before the anchor.
958983
* @private
959984
* @typedef {Object} Position
960985
* @property {number} line - Line number
961986
* @property {number} ch - Character position
962-
*
987+
*
963988
* @typedef {Object} NormalizedRange
964989
* @property {Position} start - Start position
965990
* @property {Position} end - End position
966991
* @property {boolean} reversed - Whether the range is reversed
967-
*
992+
*
968993
* @param {Position} anchorPos - The anchor position
969994
* @param {Position} headPos - The head position
970995
* @return {NormalizedRange} The normalized range with start <= end

0 commit comments

Comments
 (0)