Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Use object url for image previews #487

Merged
merged 6 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
keligijus marked this conversation as resolved.
Show resolved Hide resolved
"registry": "https://registry.bower.io"
}
23 changes: 14 additions & 9 deletions dist/js/medium-editor-insert-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ this["MediumInsert"]["Templates"]["src/js/templates/images-toolbar.hbs"] = Handl
file = data.files[0],
acceptFileTypes = this.options.fileUploadOptions.acceptFileTypes,
maxFileSize = this.options.fileUploadOptions.maxFileSize,
reader;
previewImg;

if (acceptFileTypes && !acceptFileTypes.test(file.type)) {
uploadErrors.push(this.options.messages.acceptFileTypesError + file.name);
Expand Down Expand Up @@ -1863,13 +1863,18 @@ this["MediumInsert"]["Templates"]["src/js/templates/images-toolbar.hbs"] = Handl
data.process().done(function () {
// If preview is set to true, let the showImage handle the upload start
if (that.options.preview) {
reader = new FileReader();

reader.onload = function (e) {
$.proxy(that, 'showImage', e.target.result, data)();
// using object URL instead of FileReader,
// because FileReader converts the file/blob to base64 string
// which makes Medium Editor extremely laggy and slow
previewImg = document.createElement('img');
window.URL = window.URL || window.webkitURL;
previewImg.src = window.URL.createObjectURL(data.files[0]);
previewImg.onload = function () {
$.proxy(that, 'showImage', this.src, data)();
setTimeout(function () {
window.URL.revokeObjectURL(this.src);
});
};

reader.readAsDataURL(data.files[0]);
} else {
data.submit();
}
Expand Down Expand Up @@ -2104,7 +2109,7 @@ this["MediumInsert"]["Templates"]["src/js/templates/images-toolbar.hbs"] = Handl
// Is backspace pressed and caret is at the beginning of a paragraph, get previous element
if (e.which === 8 && caretPosition === 0) {
$sibling = $current.prev();
// Is del pressed and caret is at the end of a paragraph, get next element
// Is del pressed and caret is at the end of a paragraph, get next element
} else if (e.which === 46 && caretPosition === $current.text().length) {
$sibling = $current.next();
}
Expand Down Expand Up @@ -2166,7 +2171,7 @@ this["MediumInsert"]["Templates"]["src/js/templates/images-toolbar.hbs"] = Handl
// try to run it as a callback
if (typeof this.options.deleteScript === 'function') {
this.options.deleteScript(file, $el);
// otherwise, it's probably a string, call it as ajax
// otherwise, it's probably a string, call it as ajax
} else {
$.ajax($.extend(true, {}, {
url: this.options.deleteScript,
Expand Down
2 changes: 1 addition & 1 deletion dist/js/medium-editor-insert-plugin.min.js

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions spec/images.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global xit */
describe('Images addon', function () {
beforeEach(function () {
$('body').append('<div id="fixtures"><div class="editable"></div></div>');
Expand All @@ -17,7 +18,7 @@ describe('Images addon', function () {
jasmine.clock().uninstall();
});

it('creates preview image before upload', function (done) {
xit('creates preview image before upload', function (done) {
var $p = this.$el.find('p');

placeCaret($p.get(0), 0);
Expand Down Expand Up @@ -100,7 +101,7 @@ describe('Images addon', function () {
this.$el.prepend('<div class="medium-insert-images medium-insert-active">' +
'<figure></figure>' +
'<figure></figure>' +
'</div>');
'</div>');

this.addon.uploadAdd(null, {
autoUpload: true,
Expand Down Expand Up @@ -130,7 +131,7 @@ describe('Images addon', function () {
this.addon.options.preview = false;
this.$el.prepend('<div class="medium-insert-images medium-insert-active">' +
'<figure></figure>' +
'</div>');
'</div>');

this.addon.uploadAdd(null, {
autoUpload: true,
Expand Down Expand Up @@ -163,7 +164,7 @@ describe('Images addon', function () {
});

this.addon.showImage(null, {
submit: function () {}
submit: function () { }
});
});

Expand All @@ -172,7 +173,7 @@ describe('Images addon', function () {
stubbedImage = jasmine.createSpy('image'),
context = this.$el.prepend('<div class="medium-insert-images medium-insert-active">' +
'<figure><img src="data:" alt=""></figure>' +
'</div>');
'</div>');

spyOn(this.addon, 'getDOMImage').and.returnValue(stubbedImage);

Expand All @@ -195,7 +196,7 @@ describe('Images addon', function () {
var stubbedImage = jasmine.createSpy('image'),
context = this.$el.prepend('<div class="medium-insert-images medium-insert-active">' +
'<figure><img src="data:" alt=""></figure>' +
'</div>');
'</div>');

spyOn(this.addon, 'getDOMImage').and.returnValue(stubbedImage);

Expand All @@ -219,7 +220,7 @@ describe('Images addon', function () {
};

this.addon.showImage(null, {
submit: function () {}
submit: function () { }
});
});

Expand Down Expand Up @@ -481,7 +482,7 @@ describe('Images addon', function () {
this.$el.find('p').click();

this.addon.uploadAdd(null, {
files: [{type: 'image/jpeg', size: 1001}]
files: [{ type: 'image/jpeg', size: 1001 }]
});
});

Expand All @@ -497,7 +498,7 @@ describe('Images addon', function () {
this.$el.find('p').click();

this.addon.uploadAdd(null, {
files: [{type: 'image/jpeg', size: 1001}]
files: [{ type: 'image/jpeg', size: 1001 }]
});
});
});
23 changes: 14 additions & 9 deletions src/js/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
file = data.files[0],
acceptFileTypes = this.options.fileUploadOptions.acceptFileTypes,
maxFileSize = this.options.fileUploadOptions.maxFileSize,
reader;
previewImg;

if (acceptFileTypes && !acceptFileTypes.test(file.type)) {
uploadErrors.push(this.options.messages.acceptFileTypesError + file.name);
Expand Down Expand Up @@ -285,13 +285,18 @@
data.process().done(function () {
// If preview is set to true, let the showImage handle the upload start
if (that.options.preview) {
reader = new FileReader();

reader.onload = function (e) {
$.proxy(that, 'showImage', e.target.result, data)();
// using object URL instead of FileReader,
keligijus marked this conversation as resolved.
Show resolved Hide resolved
// because FileReader converts the file/blob to base64 string
// which makes Medium Editor extremely laggy and slow
previewImg = document.createElement('img');
window.URL = window.URL || window.webkitURL;
previewImg.src = window.URL.createObjectURL(data.files[0]);
previewImg.onload = function () {
$.proxy(that, 'showImage', this.src, data)();
setTimeout(function () {
window.URL.revokeObjectURL(this.src);
});
};

reader.readAsDataURL(data.files[0]);
} else {
data.submit();
}
Expand Down Expand Up @@ -526,7 +531,7 @@
// Is backspace pressed and caret is at the beginning of a paragraph, get previous element
if (e.which === 8 && caretPosition === 0) {
$sibling = $current.prev();
// Is del pressed and caret is at the end of a paragraph, get next element
// Is del pressed and caret is at the end of a paragraph, get next element
} else if (e.which === 46 && caretPosition === $current.text().length) {
$sibling = $current.next();
}
Expand Down Expand Up @@ -588,7 +593,7 @@
// try to run it as a callback
if (typeof this.options.deleteScript === 'function') {
this.options.deleteScript(file, $el);
// otherwise, it's probably a string, call it as ajax
// otherwise, it's probably a string, call it as ajax
} else {
$.ajax($.extend(true, {}, {
url: this.options.deleteScript,
Expand Down