DEV: Remove old _firefoxPastingHack

This commit is contained in:
Penar Musaraj 2019-01-04 11:14:35 -05:00
parent 858a456aaf
commit f400830575
1 changed files with 0 additions and 100 deletions

View File

@ -760,106 +760,6 @@ export default Ember.Component.extend({
$("#mobile-uploader").click();
});
}
this._firefoxPastingHack();
},
// Believe it or not pasting an image in Firefox doesn't work without this code
_firefoxPastingHack() {
const uaMatch = navigator.userAgent.match(/Firefox\/(\d+)\.\d/);
if (uaMatch) {
let uaVersion = parseInt(uaMatch[1]);
if (uaVersion < 24 || 50 <= uaVersion) {
// The hack is no longer required in FF 50 and later.
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=906420
return;
}
this.$().append(
Ember.$(
"<div id='contenteditable' contenteditable='true' style='height: 0; width: 0; overflow: hidden'></div>"
)
);
this.$("textarea").off("keydown.contenteditable");
this.$("textarea").on("keydown.contenteditable", event => {
// Catch Ctrl+v / Cmd+v and hijack focus to a contenteditable div. We can't
// use the onpaste event because for some reason the paste isn't resumed
// after we switch focus, probably because it is being executed too late.
if ((event.ctrlKey || event.metaKey) && event.keyCode === 86) {
// Save the current textarea selection.
const textarea = this.$("textarea")[0];
const selectionStart = textarea.selectionStart;
const selectionEnd = textarea.selectionEnd;
// Focus the contenteditable div.
const contentEditableDiv = this.$("#contenteditable");
contentEditableDiv.focus();
// The paste doesn't finish immediately and we don't have any onpaste
// event, so wait for 100ms which _should_ be enough time.
Ember.run.later(() => {
const pastedImg = contentEditableDiv.find("img");
if (pastedImg.length === 1) {
pastedImg.remove();
}
// For restoring the selection.
textarea.focus();
const textareaContent = $(textarea).val(),
startContent = textareaContent.substring(0, selectionStart),
endContent = textareaContent.substring(selectionEnd);
const restoreSelection = function(pastedText) {
$(textarea).val(startContent + pastedText + endContent);
textarea.selectionStart = selectionStart + pastedText.length;
textarea.selectionEnd = textarea.selectionStart;
};
if (contentEditableDiv.html().length > 0) {
// If the image wasn't the only pasted content we just give up and
// fall back to the original pasted text.
contentEditableDiv.find("br").replaceWith("\n");
restoreSelection(contentEditableDiv.text());
} else {
// Depending on how the image is pasted in, we may get either a
// normal URL or a data URI. If we get a data URI we can convert it
// to a Blob and upload that, but if it is a regular URL that
// operation is prevented for security purposes. When we get a regular
// URL let's just create an <img> tag for the image.
const imageSrc = pastedImg.attr("src");
if (imageSrc.match(/^data:image/)) {
// Restore the cursor position, and remove any selected text.
restoreSelection("");
// Create a Blob to upload.
const image = new Image();
image.onload = () => {
// Create a new canvas.
const canvas = document.createElementNS(
"http://www.w3.org/1999/xhtml",
"canvas"
);
canvas.height = image.height;
canvas.width = image.width;
const ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0);
canvas.toBlob(blob =>
this.$().fileupload("add", { files: blob })
);
};
image.src = imageSrc;
} else {
restoreSelection("<img src='" + imageSrc + "'>");
}
}
contentEditableDiv.html("");
}, 100);
}
});
}
},
@on("willDestroyElement")