Skip to content

Instantly share code, notes, and snippets.

@adamayres
Last active September 5, 2016 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamayres/3ad2833339079611d18b to your computer and use it in GitHub Desktop.
Save adamayres/3ad2833339079611d18b to your computer and use it in GitHub Desktop.
tinymce-fix-control-drop-chrome
(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