2022-04-20 20:41:29 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-05-04 12:30:37 +03:00
|
|
|
import EmailFailedError from 'ghost-admin/errors/email-failed-error';
|
2022-05-12 01:46:01 +03:00
|
|
|
import PreviewModal from './modals/preview';
|
2022-04-27 20:20:46 +03:00
|
|
|
import PublishFlowModal from './modals/publish-flow';
|
2022-04-22 19:56:05 +03:00
|
|
|
import PublishOptionsResource from 'ghost-admin/helpers/publish-options';
|
2022-05-05 17:59:34 +03:00
|
|
|
import UpdateFlowModal from './modals/update-flow';
|
2022-05-10 12:04:14 +03:00
|
|
|
import envConfig from 'ghost-admin/config/environment';
|
2022-05-11 19:51:53 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-05-10 13:22:36 +03:00
|
|
|
import {capitalize} from '@ember/string';
|
2022-04-20 20:41:29 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-05-10 12:04:14 +03:00
|
|
|
import {task, taskGroup, timeout} from 'ember-concurrency';
|
2022-05-13 19:43:14 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2022-04-22 19:56:05 +03:00
|
|
|
import {use} from 'ember-could-get-used-to-this';
|
2022-04-20 20:41:29 +03:00
|
|
|
|
2022-05-10 12:04:14 +03:00
|
|
|
const SHOW_SAVE_STATUS_DURATION = 3000;
|
2022-05-12 17:28:35 +03:00
|
|
|
export const CONFIRM_EMAIL_POLL_LENGTH = 1000;
|
|
|
|
export const CONFIRM_EMAIL_MAX_POLL_LENGTH = 15 * 1000;
|
2022-05-04 12:30:37 +03:00
|
|
|
|
2022-04-22 19:56:05 +03:00
|
|
|
// This component exists for the duration of the editor screen being open.
|
2022-05-05 17:59:34 +03:00
|
|
|
// It's used to store the selected publish options, control the publishing flow
|
|
|
|
// modal display, and provide an editor-specific save behaviour wrapper around
|
|
|
|
// PublishOptions saving.
|
2022-04-22 19:56:05 +03:00
|
|
|
export default class PublishManagement extends Component {
|
2022-04-20 20:41:29 +03:00
|
|
|
@service modals;
|
2022-05-10 13:22:36 +03:00
|
|
|
@service notifications;
|
2022-04-20 20:41:29 +03:00
|
|
|
|
2022-04-22 19:56:05 +03:00
|
|
|
// ensure we get a new PublishOptions instance when @post is replaced
|
|
|
|
@use publishOptions = new PublishOptionsResource(() => [this.args.post]);
|
|
|
|
|
2022-05-13 19:43:14 +03:00
|
|
|
@tracked previewTab = 'browser';
|
|
|
|
|
2022-04-20 20:41:29 +03:00
|
|
|
publishFlowModal = null;
|
2022-05-05 17:59:34 +03:00
|
|
|
updateFlowModal = null;
|
2022-04-20 20:41:29 +03:00
|
|
|
|
|
|
|
willDestroy() {
|
|
|
|
super.willDestroy(...arguments);
|
|
|
|
this.publishFlowModal?.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-05-19 14:02:10 +03:00
|
|
|
async openPublishFlow(event, {skipAnimation} = {}) {
|
2022-04-22 19:56:05 +03:00
|
|
|
event?.preventDefault();
|
|
|
|
|
2022-05-05 17:59:34 +03:00
|
|
|
this.updateFlowModal?.close();
|
|
|
|
|
2022-05-10 21:24:45 +03:00
|
|
|
const isValid = await this._validatePost();
|
|
|
|
|
2022-06-01 00:59:12 +03:00
|
|
|
if (isValid && !this.publishFlowModal || this.publishFlowModal?.isClosing) {
|
2022-04-27 20:20:46 +03:00
|
|
|
this.publishOptions.resetPastScheduledAt();
|
|
|
|
|
2022-04-22 19:56:05 +03:00
|
|
|
this.publishFlowModal = this.modals.open(PublishFlowModal, {
|
2022-05-04 12:30:37 +03:00
|
|
|
publishOptions: this.publishOptions,
|
2022-05-12 01:46:01 +03:00
|
|
|
saveTask: this.publishTask,
|
2022-05-19 14:02:10 +03:00
|
|
|
togglePreviewPublish: this.togglePreviewPublish,
|
|
|
|
skipAnimation
|
2022-04-22 19:56:05 +03:00
|
|
|
});
|
2022-05-18 12:49:08 +03:00
|
|
|
|
|
|
|
const result = await this.publishFlowModal;
|
|
|
|
|
|
|
|
if (result?.afterTask && this[result?.afterTask]) {
|
|
|
|
await timeout(160); // wait for modal animation to finish
|
|
|
|
this[result.afterTask].perform();
|
|
|
|
}
|
2022-04-22 19:56:05 +03:00
|
|
|
}
|
2022-04-20 20:41:29 +03:00
|
|
|
}
|
2022-05-04 12:30:37 +03:00
|
|
|
|
2022-05-05 17:59:34 +03:00
|
|
|
@action
|
2022-05-10 13:22:36 +03:00
|
|
|
async openUpdateFlow(event) {
|
2022-05-05 17:59:34 +03:00
|
|
|
event?.preventDefault();
|
|
|
|
|
|
|
|
this.publishFlowModal?.close();
|
|
|
|
|
2022-05-10 21:24:45 +03:00
|
|
|
const isValid = await this._validatePost();
|
|
|
|
|
|
|
|
if (isValid && !this.updateFlowModal || this.updateFlowModal.isClosing) {
|
2022-05-05 17:59:34 +03:00
|
|
|
this.updateFlowModal = this.modals.open(UpdateFlowModal, {
|
|
|
|
publishOptions: this.publishOptions,
|
2022-05-10 13:22:36 +03:00
|
|
|
saveTask: this.publishTask
|
2022-05-05 17:59:34 +03:00
|
|
|
});
|
2022-05-10 13:22:36 +03:00
|
|
|
|
|
|
|
const result = await this.updateFlowModal;
|
|
|
|
|
|
|
|
if (result?.afterTask && this[result?.afterTask]) {
|
2022-05-10 13:51:55 +03:00
|
|
|
await timeout(160); // wait for modal animation to finish
|
2022-05-10 13:22:36 +03:00
|
|
|
this[result.afterTask].perform();
|
|
|
|
}
|
2022-05-05 17:59:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:46:01 +03:00
|
|
|
@action
|
2022-05-19 14:02:10 +03:00
|
|
|
openPreview(event, {skipAnimation} = {}) {
|
2022-05-12 01:46:01 +03:00
|
|
|
event?.preventDefault();
|
|
|
|
|
|
|
|
if (!this.previewModal || this.previewModal.isClosing) {
|
|
|
|
// open publish flow modal underneath to offer quick switching
|
|
|
|
// without restarting the flow or causing flicker
|
|
|
|
|
|
|
|
this.previewModal = this.modals.open(PreviewModal, {
|
2022-05-16 16:15:02 +03:00
|
|
|
publishOptions: this.publishOptions,
|
2022-05-12 01:46:01 +03:00
|
|
|
hasDirtyAttributes: this.args.hasUnsavedChanges,
|
|
|
|
saveTask: this.saveTask,
|
2023-01-09 16:48:30 +03:00
|
|
|
savePostTask: this.args.savePostTask,
|
2022-05-13 19:43:14 +03:00
|
|
|
togglePreviewPublish: this.togglePreviewPublish,
|
|
|
|
currentTab: this.previewTab,
|
2022-05-19 14:02:10 +03:00
|
|
|
changeTab: this.changePreviewTab,
|
|
|
|
skipAnimation
|
2022-05-12 01:46:01 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 11:39:28 +03:00
|
|
|
// triggered by ctrl/cmd+p
|
|
|
|
@action
|
|
|
|
togglePreview(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
|
|
|
|
if (!this.previewModal || this.previewModal.isClosing) {
|
|
|
|
if (this.publishFlowModal && !this.publishFlowModal.isClosing) {
|
|
|
|
this.togglePreviewPublish();
|
|
|
|
} else {
|
|
|
|
this.openPreview();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.previewModal.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 19:43:14 +03:00
|
|
|
@action
|
|
|
|
changePreviewTab(tab) {
|
|
|
|
this.previewTab = tab;
|
|
|
|
}
|
|
|
|
|
2022-05-12 01:46:01 +03:00
|
|
|
@action
|
|
|
|
async togglePreviewPublish(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
|
|
|
|
if (this.previewModal && !this.previewModal.isClosing) {
|
2022-05-19 14:02:10 +03:00
|
|
|
this.openPublishFlow(event, {skipAnimation: true});
|
2022-05-12 01:46:01 +03:00
|
|
|
await timeout(160);
|
|
|
|
this.previewModal.close();
|
|
|
|
} else if (this.publishFlowModal && !this.publishFlowModal.isClosing) {
|
2022-05-19 14:02:10 +03:00
|
|
|
this.openPreview(event, {skipAnimation: true});
|
2022-05-12 01:46:01 +03:00
|
|
|
await timeout(160);
|
|
|
|
this.publishFlowModal.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 21:24:45 +03:00
|
|
|
async _validatePost() {
|
|
|
|
this.notifications.closeAlerts('post.save');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.publishOptions.post.validate();
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
if (e === undefined && this.publishOptions.post.errors.length !== 0) {
|
|
|
|
// validation error
|
|
|
|
const validationError = this.publishOptions.post.errors.messages[0];
|
|
|
|
const errorMessage = `Validation failed: ${validationError}`;
|
|
|
|
|
|
|
|
this.notifications.showAlert(errorMessage, {type: 'error', key: 'post.save'});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.notifications.showAPIError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:30:37 +03:00
|
|
|
@task
|
2022-05-10 12:04:14 +03:00
|
|
|
*publishTask({taskName = 'saveTask'} = {}) {
|
2023-03-09 14:32:22 +03:00
|
|
|
const willEmailImmediately = this.publishOptions.willEmailImmediately;
|
2022-05-05 17:59:34 +03:00
|
|
|
|
2022-05-04 12:30:37 +03:00
|
|
|
// clean up blank editor cards
|
|
|
|
// apply cloned mobiledoc
|
|
|
|
// apply scratch values
|
|
|
|
// generate slug if needed (should never happen - publish flow can't be opened on new posts)
|
2022-05-10 12:04:14 +03:00
|
|
|
yield this.args.beforePublish();
|
2022-05-04 12:30:37 +03:00
|
|
|
|
|
|
|
// apply publish options (with undo on failure)
|
|
|
|
// save with the required query params for emailing
|
2022-05-05 17:59:34 +03:00
|
|
|
const result = yield this.publishOptions[taskName].perform();
|
2022-05-04 12:30:37 +03:00
|
|
|
|
|
|
|
// perform any post-save cleanup for the editor
|
2022-05-10 12:04:14 +03:00
|
|
|
yield this.args.afterPublish(result);
|
2022-05-04 12:30:37 +03:00
|
|
|
|
|
|
|
// if emailed, wait until it has been submitted so we can show a failure message if needed
|
2023-03-09 14:32:22 +03:00
|
|
|
if (willEmailImmediately && this.publishOptions.post.email) {
|
2022-05-04 12:30:37 +03:00
|
|
|
yield this.confirmEmailTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:04:14 +03:00
|
|
|
// used by the non-publish "Save" button shown for scheduled/published posts
|
|
|
|
@task({group: 'saveButtonTaskGroup'})
|
|
|
|
*saveTask() {
|
|
|
|
yield this.args.saveTask.perform();
|
|
|
|
this.saveButtonTimeoutTask.perform();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@task({group: 'saveButtonTaskGroup'})
|
|
|
|
*saveButtonTimeoutTask() {
|
|
|
|
yield timeout(envConfig.environment === 'test' ? 1 : SHOW_SAVE_STATUS_DURATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
@taskGroup saveButtonTaskGroup;
|
|
|
|
|
2022-05-04 12:30:37 +03:00
|
|
|
@task
|
|
|
|
*confirmEmailTask() {
|
|
|
|
const post = this.publishOptions.post;
|
|
|
|
|
|
|
|
let pollTimeout = 0;
|
|
|
|
if (post.email && post.email.status !== 'submitted') {
|
|
|
|
while (pollTimeout < CONFIRM_EMAIL_MAX_POLL_LENGTH) {
|
|
|
|
yield timeout(CONFIRM_EMAIL_POLL_LENGTH);
|
|
|
|
pollTimeout += CONFIRM_EMAIL_POLL_LENGTH;
|
|
|
|
|
|
|
|
yield post.reload();
|
|
|
|
|
2023-03-09 14:32:22 +03:00
|
|
|
if (!post.isSent && !post.isPublished) {
|
|
|
|
// A post that is not published doesn't try to send or retry an email
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:30:37 +03:00
|
|
|
if (post.email.status === 'submitted') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (post.email.status === 'failed') {
|
|
|
|
throw new EmailFailedError(post.email.error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-05-05 17:59:34 +03:00
|
|
|
|
|
|
|
@task
|
|
|
|
*revertToDraftTask() {
|
2022-05-10 13:22:36 +03:00
|
|
|
try {
|
|
|
|
yield this.publishTask.perform({taskName: 'revertToDraftTask'});
|
|
|
|
|
|
|
|
const postType = capitalize(this.args.post.displayName);
|
|
|
|
this.notifications.showNotification(`${postType} successfully reverted to a draft.`, {type: 'success'});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
2022-05-10 13:33:17 +03:00
|
|
|
this.notifications.showAPIError(e);
|
2022-05-10 13:22:36 +03:00
|
|
|
}
|
2022-05-05 17:59:34 +03:00
|
|
|
}
|
2022-04-20 20:41:29 +03:00
|
|
|
}
|