Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Added "View email" button to view already-sent email
Browse files Browse the repository at this point in the history
no issue

- refactored `renderEmailPreview` into separate fetch and render functions
- grab email info from an existing email resource if it exists on the post rather than fetching a preview
- added "View email" button the the email-sent state of the newsletter settings PSM section
  • Loading branch information
kevinansfield committed Nov 13, 2019
1 parent 567ec91 commit 2fd85c8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 52 deletions.
9 changes: 5 additions & 4 deletions app/components/gh-post-settings-menu/email.js
@@ -1,5 +1,6 @@
import Component from '@ember/component';
import validator from 'validator';
import {action} from '@ember/object';
import {alias, oneWay, or} from '@ember/object/computed';
import {computed} from '@ember/object';
import {inject as service} from '@ember/service';
Expand Down Expand Up @@ -52,15 +53,15 @@ export default Component.extend({
});
},

toggleEmailPreview() {
this.toggleEmailPreviewModal();
},

discardEnter() {
return false;
}
},

toggleEmailPreview: action(function () {
this.toggleEmailPreviewModal();
}),

sendTestEmail: task(function* () {
try {
const resourceId = this.post.id;
Expand Down
97 changes: 51 additions & 46 deletions app/components/modal-post-email-preview.js
Expand Up @@ -3,13 +3,27 @@ import {action} from '@ember/object';
import {alias} from '@ember/object/computed';
import {inject as service} from '@ember/service';

const INJECTED_CSS = `
html::-webkit-scrollbar {
display: none;
width: 0;
background: transparent
}
html {
scrollbar-width: none;
}
body {
pointer-events: none !important;
}
`;

export default ModalComponent.extend({
ghostPaths: service(),
ajax: service(),

type: 'desktop',
previewHtml: '',
previewEmailSubject: null,
html: '',
subject: '',

post: alias('model'),

Expand All @@ -20,53 +34,44 @@ export default ModalComponent.extend({
},

renderEmailPreview: action(async function renderEmailPreview() {
try {
const resourceId = this.post.id;
const url = this.get('ghostPaths.url').api('/email_preview/posts', resourceId);
let htmlData = this.get('previewHtml');
let emailSubject = this.get('previewEmailSubject');

if (!htmlData) {
const response = await this.ajax.request(url);
let [emailPreview] = response.email_previews;
htmlData = emailPreview.html;
emailSubject = emailPreview.subject;
}
await this._fetchEmailData();

let domParser = new DOMParser();
let htmlDoc = domParser.parseFromString(htmlData, 'text/html');
let iframe = this.element.querySelector('iframe');
if (iframe) {
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(this.html);
iframe.contentWindow.document.close();
}
}),

let stylesheet = htmlDoc.querySelector('style');
let originalCss = stylesheet.innerHTML;
let extraCss = `
html::-webkit-scrollbar {
display: none;
width: 0;
background: transparent
}
html {
scrollbar-width: none;
}
body {
pointer-events: none !important;
}
`;
stylesheet.innerHTML = `${originalCss}\n\n${extraCss}`;
async _fetchEmailData() {
let {html, subject} = this;

let iframe = this.element.querySelector('iframe');
if (iframe) {
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(htmlDoc.documentElement.innerHTML);
iframe.contentWindow.document.close();
}
if (html && subject) {
return {html, subject};
}

this.set('previewHtml', htmlData);
this.set('previewEmailSubject', emailSubject);
} catch (error) {
// re-throw if we don't have a validation error
if (error) {
throw error;
}
if (this.post.email) {
// use sent email
html = this.post.email.html;
subject = this.post.email.subject;
} else {
// fetch email preview
let url = this.get('ghostPaths.url').api('/email_preview/posts', this.post.id);
let response = await this.ajax.request(url);
let [emailPreview] = response.email_previews;
html = emailPreview.html;
subject = emailPreview.subject;
}
})

// inject extra CSS into the html for disabling links and scrollbars etc
let domParser = new DOMParser();
let htmlDoc = domParser.parseFromString(html, 'text/html');
let stylesheet = htmlDoc.querySelector('style');
let originalCss = stylesheet.innerHTML;
stylesheet.innerHTML = `${originalCss}\n\n${INJECTED_CSS}`;
html = htmlDoc.documentElement.innerHTML;

this.setProperties({html, subject});
}
});
4 changes: 2 additions & 2 deletions app/templates/components/gh-post-settings-menu/email.hbs
Expand Up @@ -11,6 +11,7 @@
<p>Status: {{this.post.email.status}}</p>
<p>Sent on: {{gh-format-post-time this.post.email.createdAtUTC}}</p>
<p>Sent to {{pluralize this.post.email.emailCount "member"}}</p>
<p><button {{on "click" this.toggleEmailPreview}}>View email</button></p>
{{else}}
{{!-- Mail not sent yet --}}
{{#if mailgunError}}
Expand Down Expand Up @@ -40,8 +41,7 @@
<div class="form-group">
<div class="flex">
<label class="nowrap flex-auto">Test email</label>
<button type="button" class="gh-btn gh-btn-link settings-menu-email-button" onclick={{action "toggleEmailPreview"}}
data-test-button="toggle-email-preview">
<button type="button" class="gh-btn gh-btn-link settings-menu-email-button" {{on "click" this.toggleEmailPreview}} data-test-button="toggle-email-preview">
<span class="blue">
Preview in browser
</span>
Expand Down

0 comments on commit 2fd85c8

Please sign in to comment.