2022-01-21 22:25:47 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2021-10-05 23:32:42 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2021-10-11 21:30:58 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2021-10-05 22:44:27 +03:00
|
|
|
import {isEmpty} from '@ember/utils';
|
|
|
|
import {isThemeValidationError} from 'ghost-admin/services/ajax';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-10-05 23:32:42 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2021-10-05 22:44:27 +03:00
|
|
|
|
|
|
|
export default class ThemeManagementService extends Service {
|
2021-10-05 23:32:42 +03:00
|
|
|
@service ajax;
|
|
|
|
@service customThemeSettings;
|
2021-10-05 22:44:27 +03:00
|
|
|
@service limit;
|
|
|
|
@service modals;
|
2021-10-05 23:32:42 +03:00
|
|
|
@service settings;
|
2021-10-13 18:06:49 +03:00
|
|
|
@service store;
|
2021-10-28 15:59:41 +03:00
|
|
|
@service frontend;
|
2021-10-05 23:32:42 +03:00
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
|
|
|
|
2021-10-11 21:30:58 +03:00
|
|
|
@tracked isUploading;
|
2021-10-13 18:06:49 +03:00
|
|
|
@tracked previewType = 'homepage';
|
2021-10-05 23:32:42 +03:00
|
|
|
@tracked previewHtml;
|
2021-10-05 22:44:27 +03:00
|
|
|
|
2021-10-13 18:06:49 +03:00
|
|
|
allPosts = this.store.peekAll('post');
|
|
|
|
|
|
|
|
availablePreviewTypes = [{
|
|
|
|
name: 'homepage',
|
|
|
|
label: 'Homepage'
|
|
|
|
}, {
|
|
|
|
name: 'post',
|
|
|
|
label: 'Post'
|
2022-02-10 13:41:36 +03:00
|
|
|
}];
|
2021-10-13 18:06:49 +03:00
|
|
|
|
|
|
|
get latestPublishedPost() {
|
2021-10-14 12:31:30 +03:00
|
|
|
return this.allPosts.toArray().filterBy('status', 'published').sort((a, b) => {
|
|
|
|
return b.publishedAtUTC.valueOf() - a.publishedAtUTC.valueOf();
|
|
|
|
}).lastObject;
|
2021-10-13 18:06:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setPreviewType(type) {
|
|
|
|
if (type !== this.previewType) {
|
|
|
|
this.previewType = type;
|
|
|
|
this.updatePreviewHtmlTask.perform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 21:30:58 +03:00
|
|
|
@action
|
2021-10-19 21:43:36 +03:00
|
|
|
async upload(options = {}) {
|
2021-10-11 21:30:58 +03:00
|
|
|
try {
|
|
|
|
// Sending a bad string to make sure it fails (empty string isn't valid)
|
|
|
|
await this.limit.limiter.errorIfWouldGoOverLimit('customThemes', {value: '.'});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.errorType === 'HostLimitError') {
|
2021-11-01 21:35:21 +03:00
|
|
|
return this.modals.open('modals/limits/custom-theme', {
|
2021-10-11 21:30:58 +03:00
|
|
|
message: error.message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2021-10-19 21:43:36 +03:00
|
|
|
return this.modals.open('modals/design/upload-theme', options);
|
2021-10-11 21:30:58 +03:00
|
|
|
}
|
|
|
|
|
2021-10-05 22:44:27 +03:00
|
|
|
@task
|
2021-10-18 14:32:31 +03:00
|
|
|
*activateTask(theme, options = {}) {
|
2021-10-05 22:44:27 +03:00
|
|
|
let resultModal = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const isOverLimit = yield this.limit.checkWouldGoOverLimit('customThemes', {value: theme.name});
|
|
|
|
|
|
|
|
if (isOverLimit) {
|
|
|
|
try {
|
|
|
|
yield this.limit.limiter.errorIfWouldGoOverLimit('customThemes', {value: theme.name});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.errorType !== 'HostLimitError') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
resultModal = this.modals.open('modals/limits/custom-theme', {
|
|
|
|
message: error.message
|
|
|
|
});
|
|
|
|
|
|
|
|
yield resultModal;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const activatedTheme = yield theme.activate();
|
|
|
|
|
2021-11-05 18:10:24 +03:00
|
|
|
yield this.customThemeSettings.reload();
|
2021-10-05 23:32:42 +03:00
|
|
|
|
2021-11-08 12:47:04 +03:00
|
|
|
// must come after settings reload has finished otherwise we'll preview previous theme settings
|
|
|
|
this.updatePreviewHtmlTask.perform();
|
|
|
|
|
2021-10-12 18:47:33 +03:00
|
|
|
if (!options.skipErrors) {
|
|
|
|
const {warnings, errors} = activatedTheme;
|
2021-10-05 22:44:27 +03:00
|
|
|
|
2021-10-12 18:47:33 +03:00
|
|
|
if (!isEmpty(warnings) || !isEmpty(errors)) {
|
|
|
|
resultModal = this.modals.open('modals/design/theme-errors', {
|
|
|
|
title: 'Activation successful',
|
|
|
|
canActivate: true,
|
|
|
|
warnings,
|
|
|
|
errors
|
|
|
|
});
|
2021-10-05 22:44:27 +03:00
|
|
|
|
2021-10-12 18:47:33 +03:00
|
|
|
yield resultModal;
|
|
|
|
}
|
2021-10-05 22:44:27 +03:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-10-12 18:47:33 +03:00
|
|
|
if (!options.skipErrors) {
|
|
|
|
if (isThemeValidationError(error)) {
|
|
|
|
let errors = error.payload.errors[0].details.errors;
|
|
|
|
let fatalErrors = [];
|
|
|
|
let normalErrors = [];
|
|
|
|
|
|
|
|
// to have a proper grouping of fatal errors and none fatal, we need to check
|
|
|
|
// our errors for the fatal property
|
|
|
|
if (errors.length > 0) {
|
|
|
|
for (let i = 0; i < errors.length; i += 1) {
|
|
|
|
if (errors[i].fatal) {
|
|
|
|
fatalErrors.push(errors[i]);
|
|
|
|
} else {
|
|
|
|
normalErrors.push(errors[i]);
|
|
|
|
}
|
2021-10-05 22:44:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 18:47:33 +03:00
|
|
|
resultModal = this.modals.open('modals/design/theme-errors', {
|
|
|
|
title: 'Activation failed',
|
|
|
|
canActivate: false,
|
|
|
|
errors: normalErrors,
|
|
|
|
fatalErrors
|
|
|
|
});
|
2021-10-05 22:44:27 +03:00
|
|
|
|
2021-10-12 18:47:33 +03:00
|
|
|
yield resultModal;
|
|
|
|
}
|
2021-10-05 22:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
// finally is always called even if the task is cancelled which gives
|
|
|
|
// consumers the ability to cancel the task to clear any opened modals
|
|
|
|
resultModal?.close();
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 23:32:42 +03:00
|
|
|
|
|
|
|
@task
|
|
|
|
*updatePreviewHtmlTask() {
|
|
|
|
// skip during testing because we don't have mocks for the front-end
|
|
|
|
if (config.environment === 'test') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:59:41 +03:00
|
|
|
let frontendUrl = '/';
|
2021-10-13 18:06:49 +03:00
|
|
|
|
|
|
|
if (this.previewType === 'post') {
|
|
|
|
// in case we haven't loaded any posts so far
|
|
|
|
if (!this.latestPublishedPost) {
|
2021-10-14 12:31:30 +03:00
|
|
|
yield this.store.query('post', {filter: 'status:published', order: 'published_at DESC', limit: 1});
|
2021-10-13 18:06:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
frontendUrl = this.latestPublishedPost.url;
|
|
|
|
}
|
|
|
|
|
2021-10-28 15:59:41 +03:00
|
|
|
const previewResponse = yield this.frontend.fetch(frontendUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'text/html;charset=utf-8',
|
|
|
|
'x-ghost-preview': this.previewData,
|
|
|
|
Accept: 'text/plain'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const previewContents = yield previewResponse.text();
|
2021-10-05 23:32:42 +03:00
|
|
|
|
|
|
|
// inject extra CSS to disable navigation and prevent clicks
|
|
|
|
const injectedCss = `html { pointer-events: none; }`;
|
|
|
|
|
|
|
|
const domParser = new DOMParser();
|
|
|
|
const htmlDoc = domParser.parseFromString(previewContents, 'text/html');
|
|
|
|
|
|
|
|
const stylesheet = htmlDoc.querySelector('style');
|
|
|
|
const originalCSS = stylesheet.innerHTML;
|
|
|
|
stylesheet.innerHTML = `${originalCSS}\n\n${injectedCss}`;
|
|
|
|
|
|
|
|
// replace the iframe contents with the doctored preview html
|
2021-10-28 16:53:40 +03:00
|
|
|
const doctype = new XMLSerializer().serializeToString(htmlDoc.doctype);
|
|
|
|
this.previewHtml = doctype + htmlDoc.documentElement.outerHTML;
|
2021-10-05 23:32:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get previewData() {
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
params.append('c', this.settings.accentColor || '#ffffff');
|
|
|
|
params.append('d', this.settings.description);
|
|
|
|
params.append('icon', this.settings.icon);
|
|
|
|
params.append('logo', this.settings.logo);
|
|
|
|
params.append('cover', this.settings.coverImage);
|
2021-10-05 23:32:42 +03:00
|
|
|
|
|
|
|
params.append('custom', JSON.stringify(this.customThemeSettings.keyValueObject));
|
|
|
|
|
|
|
|
return params.toString();
|
|
|
|
}
|
2021-10-05 22:44:27 +03:00
|
|
|
}
|