2022-01-25 16:13:18 +03:00
|
|
|
import Component from '@glimmer/component';
|
2019-11-05 08:21:29 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2019-11-05 08:21:29 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-12-10 14:38:38 +03:00
|
|
|
import {timeout} from 'ember-concurrency';
|
2022-01-25 16:13:18 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2019-11-05 08:21:29 +03:00
|
|
|
|
2019-11-13 19:38:32 +03:00
|
|
|
const INJECTED_CSS = `
|
|
|
|
html::-webkit-scrollbar {
|
|
|
|
display: none;
|
|
|
|
width: 0;
|
|
|
|
background: transparent
|
|
|
|
}
|
|
|
|
html {
|
|
|
|
scrollbar-width: none;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2022-01-25 16:13:18 +03:00
|
|
|
export default class EmailPreviewModal extends Component {
|
|
|
|
@service ajax;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service settings;
|
2022-04-28 13:01:25 +03:00
|
|
|
@service store;
|
2019-11-07 13:11:53 +03:00
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
|
|
|
|
2022-03-14 13:52:04 +03:00
|
|
|
static modalOptions = {
|
|
|
|
className: 'fullscreen-modal-full-overlay fullscreen-modal-email-preview'
|
|
|
|
};
|
|
|
|
|
2022-01-25 16:13:18 +03:00
|
|
|
@tracked tab = 'desktop';
|
|
|
|
@tracked subject = null;
|
2022-04-28 13:01:25 +03:00
|
|
|
@tracked newsletter = null;
|
2019-11-07 13:11:53 +03:00
|
|
|
|
2022-01-25 16:13:18 +03:00
|
|
|
// cached to avoid re-fetching when changing tabs
|
|
|
|
html = null;
|
2019-11-07 13:11:53 +03:00
|
|
|
|
2022-01-25 16:13:18 +03:00
|
|
|
@action
|
|
|
|
changeTab(tab) {
|
|
|
|
this.tab = tab;
|
|
|
|
}
|
2019-11-05 08:21:29 +03:00
|
|
|
|
2022-01-25 16:13:18 +03:00
|
|
|
@action
|
|
|
|
async renderEmailPreview(iframe) {
|
2019-11-13 19:38:32 +03:00
|
|
|
await this._fetchEmailData();
|
2020-12-10 14:38:38 +03:00
|
|
|
// avoid timing issues when _fetchEmailData didn't perform any async ops
|
|
|
|
await timeout(100);
|
2019-11-05 08:21:29 +03:00
|
|
|
|
2019-11-13 19:38:32 +03:00
|
|
|
if (iframe) {
|
|
|
|
iframe.contentWindow.document.open();
|
|
|
|
iframe.contentWindow.document.write(this.html);
|
|
|
|
iframe.contentWindow.document.close();
|
|
|
|
}
|
2022-01-25 16:13:18 +03:00
|
|
|
}
|
2019-11-07 13:11:53 +03:00
|
|
|
|
2019-11-13 19:38:32 +03:00
|
|
|
async _fetchEmailData() {
|
|
|
|
let {html, subject} = this;
|
2019-11-07 13:11:53 +03:00
|
|
|
|
2022-04-28 13:01:25 +03:00
|
|
|
// Fetch newsletter
|
2022-05-10 13:21:05 +03:00
|
|
|
if (!this.newsletter && this.args.data.newsletter) {
|
|
|
|
this.newsletter = this.args.data.newsletter;
|
2022-04-28 13:01:25 +03:00
|
|
|
}
|
2022-11-03 14:14:36 +03:00
|
|
|
|
2022-04-28 13:01:25 +03:00
|
|
|
if (!this.newsletter) {
|
|
|
|
const newsletters = (await this.store.query('newsletter', {filter: 'status:active', limit: 1})).toArray();
|
|
|
|
const defaultNewsletter = newsletters[0];
|
2022-11-03 14:14:36 +03:00
|
|
|
this.newsletter = defaultNewsletter;
|
2022-04-28 13:01:25 +03:00
|
|
|
}
|
|
|
|
|
2019-11-13 19:38:32 +03:00
|
|
|
if (html && subject) {
|
2022-01-25 16:13:18 +03:00
|
|
|
return;
|
2019-11-13 19:38:32 +03:00
|
|
|
}
|
2019-11-07 13:11:53 +03:00
|
|
|
|
2022-01-25 16:13:18 +03:00
|
|
|
// data is an email object
|
|
|
|
if (this.args.data.html && this.args.data.subject) {
|
|
|
|
html = this.args.data.html;
|
|
|
|
subject = this.args.data.subject;
|
|
|
|
// data is an object with an email property
|
|
|
|
} else if (this.args.data.email) {
|
|
|
|
html = this.args.data.email.html;
|
|
|
|
subject = this.args.data.email.subject;
|
|
|
|
// data is a post? try fetching email preview
|
2019-11-13 19:38:32 +03:00
|
|
|
} else {
|
2022-04-25 17:38:56 +03:00
|
|
|
let url = this.ghostPaths.url.api('/email_previews/posts', this.args.data.id);
|
2019-11-13 19:38:32 +03:00
|
|
|
let response = await this.ajax.request(url);
|
|
|
|
let [emailPreview] = response.email_previews;
|
|
|
|
html = emailPreview.html;
|
|
|
|
subject = emailPreview.subject;
|
2019-11-05 08:21:29 +03:00
|
|
|
}
|
2019-11-13 19:38:32 +03:00
|
|
|
|
|
|
|
// 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}`;
|
2021-10-28 16:53:40 +03:00
|
|
|
|
|
|
|
const doctype = new XMLSerializer().serializeToString(htmlDoc.doctype);
|
|
|
|
html = doctype + htmlDoc.documentElement.outerHTML;
|
2019-11-13 19:38:32 +03:00
|
|
|
|
2022-01-25 16:13:18 +03:00
|
|
|
this.html = html;
|
|
|
|
this.subject = subject;
|
2019-11-13 19:38:32 +03:00
|
|
|
}
|
2022-01-25 16:13:18 +03:00
|
|
|
}
|