fixing the screenshot issue

This commit is contained in:
Amine Ben hammou
2018-02-22 17:27:49 +01:00
parent 17e13c0956
commit ab2b380f2d
6 changed files with 1105 additions and 163 deletions

View File

@@ -3,5 +3,7 @@
.mailpoet_template_iframe .mailpoet_template_iframe
position: absolute position: absolute
z-index: -9999 z-index: -9999
width: $newsletter-width top: 0
max-width: $newsletter-width left: 0
width: $newsletter-width + 20
max-width: $newsletter-width + 20

View File

@@ -0,0 +1,78 @@
import _ from 'underscore';
import MailPoet from 'mailpoet';
import html2canvas from 'html2canvas';
/**
* Generates a thumbnail from a DOM element.
*
* @param {DOMElement} element
* @return {Promise<String>} DataURL of the generated image.
*/
export const fromDom = element =>
html2canvas(element, {
allowTaint: true,
useCORS: true,
foreignObjectRendering: true,
logging: false,
}).then(canvas => canvas.toDataURL('image/jpeg'));
/**
* Generates a thumbnail from an URL.
*
* @param {String} url
* @return {Promise<String>} DataURL of the generated image.
*/
export const fromUrl = url =>
new Promise((resolve, reject) => {
const iframe = document.createElement('iframe');
const protocol = location.href.startsWith('https://') ? 'https' : 'http';
iframe.src = protocol + url.replace(/^https?/, '');
iframe.style.opacity = 0;
iframe.onload = () => {
fromDom(iframe.contentDocument.documentElement)
.then((image) => {
document.body.removeChild(iframe);
resolve(image);
})
.catch(() => {
document.body.removeChild(iframe);
reject(MailPoet.I18n.t('errorWhileTakingScreenshot'));
});
};
const onError = () => {
document.body.removeChild(iframe);
reject(MailPoet.I18n.t('errorWhileTakingScreenshot'));
};
iframe.onerror = onError;
iframe.onError = onError;
iframe.className = 'mailpoet_template_iframe';
try {
document.body.appendChild(iframe);
} catch (err) {
onError();
}
});
/**
* Generates a thumbnail from a newsletter's data.
*
* @param {Object} data
* @return {Promise<String>} DataURL of the generated image.
*/
export const fromNewsletter = data =>
new Promise((resolve, reject) => {
const json = data;
if (!_.isUndefined(json.body)) {
json.body = JSON.stringify(json.body);
}
MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'newsletters',
action: 'showPreview',
data: json,
}).done(response => fromUrl(response.meta.preview_url)
.then(resolve)
.catch(reject)
).fail(response => reject(response.errors));
});

View File

@@ -9,7 +9,7 @@ define([
'jquery', 'jquery',
'blob', 'blob',
'file-saver', 'file-saver',
'html2canvas', 'common/thumbnail.jsx',
'underscore', 'underscore',
'jquery' 'jquery'
], function ( ], function (
@@ -22,7 +22,7 @@ define([
jQuery, jQuery,
Blob, Blob,
FileSaver, FileSaver,
html2canvas, Thumbnail,
_, _,
$ $
) { ) {
@@ -71,36 +71,11 @@ define([
}); });
}; };
Module.getThumbnail = function (element, options) {
var promise = html2canvas(element, options || {});
return promise.then(function (oldCanvas) {
// Temporary workaround for html2canvas-alpha2.
// Removes 1px left transparent border from resulting canvas.
var newCanvas = document.createElement('canvas');
var newContext = newCanvas.getContext('2d');
var leftBorderWidth = 1;
newCanvas.width = oldCanvas.width;
newCanvas.height = oldCanvas.height;
newContext.drawImage(
oldCanvas,
leftBorderWidth, 0, oldCanvas.width - leftBorderWidth, oldCanvas.height,
0, 0, oldCanvas.width, oldCanvas.height
);
return newCanvas;
});
};
Module.saveTemplate = function (options) { Module.saveTemplate = function (options) {
var promise = jQuery.Deferred(); return Thumbnail.fromNewsletter(App.toJSON())
.then(function (thumbnail) {
promise.then(function (thumbnail) {
var data = _.extend(options || {}, { var data = _.extend(options || {}, {
thumbnail: thumbnail.toDataURL('image/jpeg'), thumbnail: thumbnail,
body: JSON.stringify(App.getBody()), body: JSON.stringify(App.getBody()),
categories: JSON.stringify([ categories: JSON.stringify([
'saved', 'saved',
@@ -115,22 +90,13 @@ define([
data: data data: data
}); });
}); });
Module.getThumbnail(
jQuery('#mailpoet_editor_content > .mailpoet_block').get(0)
).then(function (thumbnail) {
promise.resolve(thumbnail);
});
return promise;
}; };
Module.exportTemplate = function (options) { Module.exportTemplate = function (options) {
return Module.getThumbnail( return Thumbnail.fromNewsletter(App.toJSON())
jQuery('#mailpoet_editor_content > .mailpoet_block').get(0) .then(function (thumbnail) {
).then(function (thumbnail) {
var data = _.extend(options || {}, { var data = _.extend(options || {}, {
thumbnail: thumbnail.toDataURL('image/jpeg'), thumbnail: thumbnail,
body: App.getBody(), body: App.getBody(),
categories: JSON.stringify(['saved', App.getNewsletter().get('type')]) categories: JSON.stringify(['saved', App.getNewsletter().get('type')])
}); });
@@ -220,7 +186,7 @@ define([
Module.saveTemplate({ Module.saveTemplate({
name: templateName, name: templateName,
description: templateDescription description: templateDescription
}).done(function () { }).then(function () {
MailPoet.Notice.success( MailPoet.Notice.success(
MailPoet.I18n.t('templateSaved'), MailPoet.I18n.t('templateSaved'),
{ {
@@ -231,7 +197,7 @@ define([
MailPoet.trackEvent('Editor > Template saved', { MailPoet.trackEvent('Editor > Template saved', {
'MailPoet Free version': window.mailpoet_version 'MailPoet Free version': window.mailpoet_version
}); });
}).fail(function () { }).catch(function () {
MailPoet.Notice.error( MailPoet.Notice.error(
MailPoet.I18n.t('templateSaveFailed'), MailPoet.I18n.t('templateSaveFailed'),
{ {

View File

@@ -11,7 +11,7 @@ define(
'newsletters/breadcrumb.jsx', 'newsletters/breadcrumb.jsx',
'help-tooltip.jsx', 'help-tooltip.jsx',
'jquery', 'jquery',
'html2canvas', 'common/thumbnail.jsx',
], ],
( (
React, React,
@@ -25,7 +25,7 @@ define(
Breadcrumb, Breadcrumb,
HelpTooltip, HelpTooltip,
jQuery, jQuery,
html2canvas Thumbnail
) => { ) => {
const NewsletterSend = React.createClass({ const NewsletterSend = React.createClass({
contextTypes: { contextTypes: {
@@ -91,41 +91,23 @@ define(
}); });
}, },
saveTemplate: function (response, done) { saveTemplate: function (response, done) {
const iframe = document.createElement('iframe'); Thumbnail.fromUrl(response.meta.preview_url)
const protocol = location.href.startsWith('https://') ? 'https' : 'http'; .then(function (thumbnail) {
iframe.src = protocol + response.meta.preview_url.replace(/^https?/, ''); MailPoet.Ajax.post({
iframe.onload = () => { api_version: window.mailpoet_api_version,
html2canvas(iframe.contentDocument.documentElement).then((thumbnail) => { endpoint: 'newsletterTemplates',
document.body.removeChild(iframe); action: 'save',
MailPoet.Ajax.post({ data: {
api_version: window.mailpoet_api_version, newsletter_id: response.data.id,
endpoint: 'newsletterTemplates', name: response.data.subject,
action: 'save', description: response.data.preheader,
data: { thumbnail: thumbnail,
newsletter_id: response.data.id, body: JSON.stringify(response.data.body),
name: response.data.subject, categories: '["recent"]',
description: response.data.preheader, },
thumbnail: thumbnail.toDataURL('image/jpeg'), }).then(done).fail(this.showError);
body: JSON.stringify(response.data.body), })
categories: '["recent"]', .catch(err => this.showError({ errors: [err] }));
},
}).then(done).fail(this.showError);
});
};
const onError = () => {
document.body.removeChild(iframe);
MailPoet.Notice.error([MailPoet.I18n.t('errorWhileTakingScreenshot')], { scroll: true });
done();
};
iframe.onerror = onError;
iframe.onError = onError;
// just to hide the iframe
iframe.className ='mailpoet_template_iframe';
try {
document.body.appendChild(iframe);
} catch (err) {
onError();
}
}, },
handleSend: function (e) { handleSend: function (e) {
e.preventDefault(); e.preventDefault();

1072
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -18,7 +18,7 @@
"file-saver": "^1.3.3", "file-saver": "^1.3.3",
"handlebars": "4.0.11", "handlebars": "4.0.11",
"history": "1.13.1", "history": "1.13.1",
"html2canvas": "0.5.0-alpha2", "html2canvas": "^1.0.0-alpha.10",
"interact.js": "~1.2.8", "interact.js": "~1.2.8",
"jquery": "2.1.4", "jquery": "2.1.4",
"moment": "^2.19.3", "moment": "^2.19.3",