Last active
September 5, 2016 06:14
-
-
Save adamayres/3ad2833339079611d18b to your computer and use it in GitHub Desktop.
tinymce-fix-control-drop-chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
function overrideDragDrop(editor) { | |
var dragStartRng; | |
var selection = editor.selection; | |
editor.on('dragstart', function(e) { | |
dragStartRng = selection.getRng(); | |
if (editor.selection.isCollapsed() && e.target.tagName == 'IMG') { | |
selection.select(e.target); | |
} | |
}); | |
editor.on('drop', function(e) { | |
var internalContent = e.dataTransfer.getData('URL'), | |
urlPrefix = 'data:text/mce-internal,', | |
doc = tinymce.activeEditor.getDoc(); | |
if (!internalContent || internalContent.indexOf(urlPrefix) == -1 || !doc.caretRangeFromPoint) { | |
return; | |
} | |
internalContent = unescape(internalContent.substr(urlPrefix.length)); | |
if (doc.caretRangeFromPoint && (e.altKey || e.ctrlKey)) { | |
e.preventDefault(); | |
// Safari has a weird issue where drag/dropping images sometimes | |
// produces a green plus icon. When this happens the caretRangeFromPoint | |
// will return "null" even though the x, y coordinate is correct. | |
// But if we detach the insert from the drop event we will get a proper range | |
window.setTimeout(function() { | |
var pointRng = doc.caretRangeFromPoint(e.x, e.y); | |
if (dragStartRng) { | |
selection.setRng(dragStartRng); | |
dragStartRng = null; | |
} | |
selection.setRng(pointRng); | |
editor.insertContent(internalContent); | |
}, 0); | |
} | |
}, true); | |
} | |
var counter = 0; | |
var interval = setInterval(function () { | |
if (window.tinymce && tinymce.editors.length) { | |
for (var i = 0; i < tinymce.editors.length; i++) { | |
clearInterval(interval); | |
overrideDragDrop(tinymce.editors[i]); | |
} | |
} else if (counter++ > 50) { | |
clearInterval(interval); | |
} | |
}, 100); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment