2022-01-21 22:25:47 +03:00
|
|
|
import Controller, {inject as controller} from '@ember/controller';
|
2017-05-29 21:50:03 +03:00
|
|
|
import PostModel from 'ghost-admin/models/post';
|
|
|
|
import boundOneWay from 'ghost-admin/utils/bound-one-way';
|
2022-02-09 12:50:46 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
2018-05-03 19:52:39 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2017-06-13 18:04:09 +03:00
|
|
|
import isNumber from 'ghost-admin/utils/isNumber';
|
2020-06-30 13:15:15 +03:00
|
|
|
import moment from 'moment';
|
2022-01-21 22:25:47 +03:00
|
|
|
import {action, computed, get} from '@ember/object';
|
2019-01-10 18:00:07 +03:00
|
|
|
import {alias, mapBy} from '@ember/object/computed';
|
2021-01-25 12:23:03 +03:00
|
|
|
import {capitalize} from '@ember/string';
|
2022-02-09 12:50:46 +03:00
|
|
|
import {dropTask, enqueueTask, restartableTask, task, taskGroup, timeout} from 'ember-concurrency';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
2022-01-21 22:25:47 +03:00
|
|
|
import {isHostLimitError, isServerUnreachableError, isVersionMismatchError} from 'ghost-admin/services/ajax';
|
2016-09-26 19:59:04 +03:00
|
|
|
import {isInvalidError} from 'ember-ajax/errors';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2017-07-10 14:33:05 +03:00
|
|
|
const DEFAULT_TITLE = '(Untitled)';
|
2017-03-28 22:41:25 +03:00
|
|
|
|
2017-07-10 18:09:50 +03:00
|
|
|
// time in ms to save after last content edit
|
|
|
|
const AUTOSAVE_TIMEOUT = 3000;
|
|
|
|
// time in ms to force a save if the user is continuously typing
|
|
|
|
const TIMEDSAVE_TIMEOUT = 60000;
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// this array will hold properties we need to watch for this.hasDirtyAttributes
|
2018-05-03 19:52:39 +03:00
|
|
|
let watchedProps = [
|
|
|
|
'post.scratch',
|
|
|
|
'post.titleScratch',
|
|
|
|
'post.hasDirtyAttributes',
|
|
|
|
'post.tags.[]',
|
|
|
|
'post.isError'
|
|
|
|
];
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
// add all post model attrs to the watchedProps array, easier to do it this way
|
|
|
|
// than remember to update every time we add a new attr
|
2014-10-14 21:29:54 +04:00
|
|
|
PostModel.eachAttribute(function (name) {
|
2018-01-11 01:57:43 +03:00
|
|
|
watchedProps.push(`post.${name}`);
|
2014-06-06 05:18:03 +04:00
|
|
|
});
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
const messageMap = {
|
|
|
|
errors: {
|
|
|
|
post: {
|
|
|
|
published: {
|
|
|
|
published: 'Update failed',
|
|
|
|
draft: 'Saving failed',
|
|
|
|
scheduled: 'Scheduling failed'
|
|
|
|
},
|
|
|
|
draft: {
|
|
|
|
published: 'Publish failed',
|
|
|
|
draft: 'Saving failed',
|
|
|
|
scheduled: 'Scheduling failed'
|
|
|
|
},
|
|
|
|
scheduled: {
|
2019-03-12 20:49:15 +03:00
|
|
|
scheduled: 'Update failed',
|
2018-01-17 16:27:37 +03:00
|
|
|
draft: 'Unscheduling failed',
|
|
|
|
published: 'Publish failed'
|
|
|
|
}
|
2014-12-30 05:11:24 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
},
|
2018-01-11 01:57:43 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
success: {
|
|
|
|
post: {
|
|
|
|
published: {
|
2020-02-27 12:19:29 +03:00
|
|
|
published: 'Updated',
|
|
|
|
draft: 'Saved',
|
2021-08-13 17:17:47 +03:00
|
|
|
scheduled: 'Scheduled',
|
|
|
|
sent: 'Sent'
|
2018-01-17 16:27:37 +03:00
|
|
|
},
|
|
|
|
draft: {
|
2020-02-27 12:19:29 +03:00
|
|
|
published: 'Published',
|
|
|
|
draft: 'Saved',
|
2021-08-13 17:17:47 +03:00
|
|
|
scheduled: 'Scheduled',
|
|
|
|
sent: 'Sent'
|
2018-01-17 16:27:37 +03:00
|
|
|
},
|
|
|
|
scheduled: {
|
2020-02-27 12:19:29 +03:00
|
|
|
scheduled: 'Updated',
|
|
|
|
draft: 'Unscheduled',
|
2021-08-13 17:17:47 +03:00
|
|
|
published: 'Published',
|
|
|
|
sent: 'Sent'
|
|
|
|
},
|
|
|
|
sent: {
|
2021-10-05 12:52:44 +03:00
|
|
|
sent: 'Updated'
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@classic
|
|
|
|
export default class EditorController extends Controller {
|
|
|
|
@controller application;
|
|
|
|
|
|
|
|
@service config;
|
|
|
|
@service feature;
|
|
|
|
@service membersCountCache;
|
|
|
|
@service modals;
|
|
|
|
@service notifications;
|
|
|
|
@service router;
|
|
|
|
@service slugGenerator;
|
|
|
|
@service session;
|
|
|
|
@service settings;
|
|
|
|
@service ui;
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
/* public properties -----------------------------------------------------*/
|
2017-04-19 16:20:42 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
shouldFocusTitle = false;
|
|
|
|
showReAuthenticateModal = false;
|
|
|
|
showPostPreviewModal = false;
|
|
|
|
showUpgradeModal = false;
|
|
|
|
showDeleteSnippetModal = false;
|
|
|
|
showSettingsMenu = false;
|
|
|
|
hostLimitError = null;
|
|
|
|
|
2018-01-18 18:36:01 +03:00
|
|
|
// koenig related properties
|
2022-02-09 12:50:46 +03:00
|
|
|
wordcount = null;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
/* private properties ----------------------------------------------------*/
|
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
_leaveConfirmed = false;
|
|
|
|
_previousTagNames = null; // set by setPost and _postSaved, used in hasDirtyAttributes
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
/* computed properties ---------------------------------------------------*/
|
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@alias('model')
|
2022-02-10 13:41:36 +03:00
|
|
|
post;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
// store the desired post status locally without updating the model,
|
|
|
|
// the model will only be updated when a save occurs
|
2022-02-09 12:50:46 +03:00
|
|
|
@boundOneWay('post.isPublished')
|
2022-02-10 13:41:36 +03:00
|
|
|
willPublish;
|
2022-02-09 12:50:46 +03:00
|
|
|
|
|
|
|
@boundOneWay('post.isScheduled')
|
2022-02-10 13:41:36 +03:00
|
|
|
willSchedule;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2021-06-21 17:05:29 +03:00
|
|
|
// updateSlugTask and saveTask should always be enqueued so that we don't run into
|
2018-01-17 16:27:37 +03:00
|
|
|
// problems with concurrency, for example when Cmd-S is pressed whilst the
|
|
|
|
// cursor is in the slug field - that would previously trigger a simultaneous
|
|
|
|
// slug update and save resulting in ember data errors and inconsistent save
|
|
|
|
// results
|
2022-02-09 12:50:46 +03:00
|
|
|
@(taskGroup().enqueue())
|
2022-02-10 13:41:36 +03:00
|
|
|
saveTasks;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@mapBy('post.tags', 'name')
|
2022-02-10 13:41:36 +03:00
|
|
|
_tagNames;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@computed(...watchedProps)
|
|
|
|
get hasDirtyAttributes() {
|
|
|
|
return this._hasDirtyAttributes();
|
|
|
|
}
|
|
|
|
|
|
|
|
set hasDirtyAttributes(value) {
|
|
|
|
// eslint-disable-next-line no-setter-return
|
|
|
|
return value;
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@computed
|
|
|
|
get _snippets() {
|
2020-10-15 20:03:18 +03:00
|
|
|
return this.store.peekAll('snippet');
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2020-10-15 20:03:18 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@computed('_snippets.@each.{name,isNew}')
|
|
|
|
get snippets() {
|
2021-05-26 22:29:41 +03:00
|
|
|
return this._snippets
|
|
|
|
.reject(snippet => snippet.get('isNew'))
|
|
|
|
.sort((a, b) => a.name.localeCompare(b.name));
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2020-10-15 20:03:18 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@computed('session.user.{isAdmin,isEditor}')
|
|
|
|
get canManageSnippets() {
|
2020-10-27 17:42:36 +03:00
|
|
|
let {user} = this.session;
|
2021-07-12 15:55:56 +03:00
|
|
|
if (user.get('isAdmin') || user.get('isEditor')) {
|
2020-10-27 17:42:36 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2020-10-27 17:42:36 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@computed('_autosaveTask.isRunning', '_timedSaveTask.isRunning')
|
|
|
|
get _autosaveRunning() {
|
2021-06-21 17:05:29 +03:00
|
|
|
let autosave = this.get('_autosaveTask.isRunning');
|
|
|
|
let timedsave = this.get('_timedSaveTask.isRunning');
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
return autosave || timedsave;
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@computed('post.isDraft')
|
|
|
|
get _canAutosave() {
|
2018-05-03 19:52:39 +03:00
|
|
|
return config.environment !== 'test' && this.get('post.isDraft');
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
updateScratch(mobiledoc) {
|
|
|
|
this.set('post.scratch', mobiledoc);
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
// save 3 seconds after last edit
|
|
|
|
this._autosaveTask.perform();
|
|
|
|
// force save at 60 seconds
|
|
|
|
this._timedSaveTask.perform();
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
updateTitleScratch(title) {
|
|
|
|
this.set('post.titleScratch', title);
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
// updates local willPublish/Schedule values, does not get applied to
|
|
|
|
// the post's `status` value until a save is triggered
|
|
|
|
@action
|
|
|
|
setSaveType(newType) {
|
|
|
|
if (newType === 'publish') {
|
|
|
|
this.set('willPublish', true);
|
|
|
|
this.set('willSchedule', false);
|
|
|
|
} else if (newType === 'draft') {
|
|
|
|
this.set('willPublish', false);
|
|
|
|
this.set('willSchedule', false);
|
|
|
|
} else if (newType === 'schedule') {
|
|
|
|
this.set('willSchedule', true);
|
|
|
|
this.set('willPublish', false);
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
save(options) {
|
|
|
|
return this.saveTask.perform(options);
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
// used to prevent unexpected background saves. Triggered when opening
|
|
|
|
// publish menu, starting a manual save, and when leaving the editor
|
|
|
|
@action
|
|
|
|
cancelAutosave() {
|
|
|
|
this._autosaveTask.cancelAll();
|
|
|
|
this._timedSaveTask.cancelAll();
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
// called by the "are you sure?" modal
|
|
|
|
@action
|
|
|
|
leaveEditor() {
|
|
|
|
let transition = this.leaveEditorTransition;
|
2019-11-05 08:21:29 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
if (!transition) {
|
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
|
|
|
return;
|
|
|
|
}
|
2022-01-16 16:48:58 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
// perform cleanup and reset manually, ensures the transition will succeed
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
return transition.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
openDeletePostModal() {
|
|
|
|
if (!this.get('post.isNew')) {
|
|
|
|
this.modals.open('modals/delete-post', {
|
|
|
|
post: this.post
|
2021-09-08 16:29:33 +03:00
|
|
|
}, {
|
2022-02-09 12:50:46 +03:00
|
|
|
className: 'fullscreen-modal fullscreen-modal-action fullscreen-modal-wide'
|
2021-09-08 16:29:33 +03:00
|
|
|
});
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
|
|
|
}
|
2021-01-22 12:24:33 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
openPostPreview(keyboardEvent) {
|
|
|
|
keyboardEvent?.preventDefault();
|
|
|
|
|
|
|
|
if (this.post.isDraft) {
|
|
|
|
this.send('openPostPreviewModal');
|
|
|
|
} else {
|
|
|
|
window.open(this.post.previewUrl, '_blank', 'noopener');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
openPostPreviewModal() {
|
|
|
|
this.modals.open('modals/post-preview', {
|
|
|
|
post: this.post,
|
|
|
|
saveTask: this.saveTask,
|
|
|
|
hasDirtyAttributes: this.hasDirtyAttributes,
|
|
|
|
// TODO: update to call action method directly when switching to class syntax
|
|
|
|
setEditorSaveType: this.actions.setSaveType.bind(this),
|
|
|
|
memberCount: this.memberCount
|
|
|
|
}, {
|
|
|
|
className: 'fullscreen-modal fullscreen-modal-full-overlay fullscreen-modal-email-preview'
|
|
|
|
});
|
|
|
|
}
|
2021-07-14 14:27:51 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
toggleReAuthenticateModal() {
|
|
|
|
if (this.showReAuthenticateModal) {
|
|
|
|
// closing, re-attempt save if needed
|
|
|
|
if (this._reauthSave) {
|
|
|
|
this.saveTask.perform(this._reauthSaveOptions);
|
2021-07-14 14:27:51 +03:00
|
|
|
}
|
2018-01-18 18:36:01 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
this._reauthSave = false;
|
|
|
|
this._reauthSaveOptions = null;
|
|
|
|
}
|
|
|
|
this.toggleProperty('showReAuthenticateModal');
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
openUpgradeModal() {
|
|
|
|
this.set('showUpgradeModal', true);
|
|
|
|
}
|
2019-12-17 16:57:42 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
closeUpgradeModal() {
|
|
|
|
this.set('showUpgradeModal', false);
|
|
|
|
}
|
2019-12-17 16:57:42 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
setKoenigEditor(koenig) {
|
|
|
|
this._koenig = koenig;
|
2018-06-14 18:55:02 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
// remove any empty cards when displaying a draft post
|
|
|
|
// - empty cards may be left in draft posts due to autosave occuring
|
|
|
|
// whilst an empty card is present then the user closing the browser
|
|
|
|
// or refreshing the page
|
|
|
|
if (this.post.isDraft) {
|
|
|
|
this._koenig.cleanup();
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 17:53:21 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
updateWordCount(counts) {
|
|
|
|
this.set('wordCount', counts);
|
|
|
|
}
|
2021-06-10 22:42:32 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
setFeatureImage(url) {
|
|
|
|
this.post.set('featureImage', url);
|
2021-06-16 19:56:25 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
if (this.post.isDraft) {
|
|
|
|
this.autosaveTask.perform();
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 22:42:32 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
clearFeatureImage() {
|
|
|
|
this.post.set('featureImage', null);
|
|
|
|
this.post.set('featureImageAlt', null);
|
|
|
|
this.post.set('featureImageCaption', null);
|
2021-06-16 19:56:25 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
if (this.post.isDraft) {
|
|
|
|
this.autosaveTask.perform();
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 22:42:32 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
setFeatureImageAlt(text) {
|
|
|
|
this.post.set('featureImageAlt', text);
|
2021-06-16 19:56:25 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
if (this.post.isDraft) {
|
|
|
|
this.autosaveTask.perform();
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 22:42:32 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
setFeatureImageCaption(html) {
|
|
|
|
this.post.set('featureImageCaption', html);
|
2021-06-16 19:56:25 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
if (this.post.isDraft) {
|
|
|
|
this.autosaveTask.perform();
|
2020-10-27 17:42:36 +03:00
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2020-10-15 20:03:18 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
toggleSettingsMenu() {
|
2021-06-17 21:00:59 +03:00
|
|
|
this.set('showSettingsMenu', !this.showSettingsMenu);
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2021-06-17 21:00:59 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
closeSettingsMenu() {
|
2021-06-17 21:00:59 +03:00
|
|
|
this.set('showSettingsMenu', false);
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2021-06-17 21:00:59 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
saveSnippet(snippet) {
|
2020-10-27 17:42:36 +03:00
|
|
|
let snippetRecord = this.store.createRecord('snippet', snippet);
|
|
|
|
return snippetRecord.save().then(() => {
|
|
|
|
this.notifications.closeAlerts('snippet.save');
|
|
|
|
this.notifications.showNotification(
|
|
|
|
`Snippet saved as "${snippet.name}"`,
|
|
|
|
{type: 'success'}
|
|
|
|
);
|
2021-08-23 17:59:53 +03:00
|
|
|
return snippetRecord;
|
|
|
|
}).catch((error) => {
|
|
|
|
if (!snippetRecord.errors.isEmpty) {
|
|
|
|
this.notifications.showAlert(
|
|
|
|
`Snippet save failed: ${snippetRecord.errors.messages.join('. ')}`,
|
|
|
|
{type: 'error', key: 'snippet.save'}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
snippetRecord.rollbackAttributes();
|
|
|
|
throw error;
|
|
|
|
});
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2021-08-23 17:59:53 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
toggleUpdateSnippetModal(snippetRecord, updatedProperties = {}) {
|
2021-08-23 19:12:26 +03:00
|
|
|
if (snippetRecord) {
|
|
|
|
this.set('snippetToUpdate', {snippetRecord, updatedProperties});
|
|
|
|
} else {
|
|
|
|
this.set('snippetToUpdate', null);
|
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2021-08-23 19:12:26 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
updateSnippet() {
|
2021-08-23 19:12:26 +03:00
|
|
|
if (!this.snippetToUpdate) {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
const {snippetRecord, updatedProperties: {mobiledoc}} = this.snippetToUpdate;
|
2021-08-23 17:59:53 +03:00
|
|
|
snippetRecord.set('mobiledoc', mobiledoc);
|
|
|
|
|
|
|
|
return snippetRecord.save().then(() => {
|
2021-08-23 19:12:26 +03:00
|
|
|
this.set('snippetToUpdate', null);
|
2021-08-23 17:59:53 +03:00
|
|
|
this.notifications.closeAlerts('snippet.save');
|
|
|
|
this.notifications.showNotification(
|
|
|
|
`Snippet "${snippetRecord.name}" updated`,
|
|
|
|
{type: 'success'}
|
|
|
|
);
|
2020-10-27 17:42:36 +03:00
|
|
|
return snippetRecord;
|
|
|
|
}).catch((error) => {
|
|
|
|
if (!snippetRecord.errors.isEmpty) {
|
|
|
|
this.notifications.showAlert(
|
|
|
|
`Snippet save failed: ${snippetRecord.errors.messages.join('. ')}`,
|
|
|
|
{type: 'error', key: 'snippet.save'}
|
2020-10-15 20:03:18 +03:00
|
|
|
);
|
2020-10-27 17:42:36 +03:00
|
|
|
}
|
|
|
|
snippetRecord.rollbackAttributes();
|
|
|
|
throw error;
|
|
|
|
});
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2020-10-26 20:09:09 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
toggleDeleteSnippetModal(snippet) {
|
2020-10-27 17:42:36 +03:00
|
|
|
this.set('snippetToDelete', snippet);
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2020-10-26 20:09:09 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@action
|
|
|
|
deleteSnippet(snippet) {
|
2020-10-27 17:42:36 +03:00
|
|
|
return snippet.destroyRecord();
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
/* Public tasks ----------------------------------------------------------*/
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
// separate task for autosave so that it doesn't override a manual save
|
2022-02-09 12:50:46 +03:00
|
|
|
@dropTask *autosaveTask() {
|
2021-06-21 17:05:29 +03:00
|
|
|
if (!this.get('saveTask.isRunning')) {
|
|
|
|
return yield this.saveTask.perform({
|
2015-05-10 01:23:14 +03:00
|
|
|
silent: true,
|
|
|
|
backgroundSave: true
|
2017-04-11 16:39:45 +03:00
|
|
|
});
|
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2015-03-17 21:03:41 +03:00
|
|
|
|
2017-04-19 19:09:31 +03:00
|
|
|
// save tasks cancels autosave before running, although this cancels the
|
|
|
|
// _xSave tasks that will also cancel the autosave task
|
2022-02-09 12:50:46 +03:00
|
|
|
@task({group: 'saveTasks'})
|
|
|
|
*saveTask(options = {}) {
|
2018-01-11 01:57:43 +03:00
|
|
|
let prevStatus = this.get('post.status');
|
|
|
|
let isNew = this.get('post.isNew');
|
2017-07-22 17:25:00 +03:00
|
|
|
let status;
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2017-07-22 17:25:00 +03:00
|
|
|
this.send('cancelAutosave');
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (options.backgroundSave && !this.hasDirtyAttributes) {
|
2017-07-22 17:25:00 +03:00
|
|
|
return;
|
2017-04-19 19:09:31 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
if (options.backgroundSave) {
|
|
|
|
// do not allow a post's status to be set to published by a background save
|
|
|
|
status = 'draft';
|
|
|
|
} else {
|
|
|
|
if (this.get('post.pastScheduledTime')) {
|
2019-03-06 16:53:54 +03:00
|
|
|
status = (!this.willSchedule && !this.willPublish) ? 'draft' : 'published';
|
2017-04-11 16:39:45 +03:00
|
|
|
} else {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.willPublish && !this.get('post.isScheduled')) {
|
2017-04-11 16:39:45 +03:00
|
|
|
status = 'published';
|
2019-03-06 16:53:54 +03:00
|
|
|
} else if (this.willSchedule && !this.get('post.isPublished')) {
|
2017-04-11 16:39:45 +03:00
|
|
|
status = 'scheduled';
|
2021-08-31 19:06:36 +03:00
|
|
|
} else if (this.get('post.isSent')) {
|
|
|
|
status = 'sent';
|
2017-04-11 16:39:45 +03:00
|
|
|
} else {
|
|
|
|
status = 'draft';
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 20:33:35 +03:00
|
|
|
|
2020-11-06 21:54:27 +03:00
|
|
|
// let the adapter know it should use the `?email_recipient_filter` QP when saving
|
2019-11-14 20:33:35 +03:00
|
|
|
let isPublishing = status === 'published' && !this.post.isPublished;
|
|
|
|
let isScheduling = status === 'scheduled' && !this.post.isScheduled;
|
|
|
|
if (options.sendEmailWhenPublished && (isPublishing || isScheduling)) {
|
2020-11-06 21:54:27 +03:00
|
|
|
options.adapterOptions = Object.assign({}, options.adapterOptions, {sendEmailWhenPublished: options.sendEmailWhenPublished});
|
2019-11-14 20:33:35 +03:00
|
|
|
}
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
|
2018-06-14 18:55:02 +03:00
|
|
|
// ensure we remove any blank cards when performing a full save
|
|
|
|
if (!options.backgroundSave) {
|
|
|
|
if (this._koenig) {
|
|
|
|
this._koenig.cleanup();
|
|
|
|
this.set('hasDirtyAttributes', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
// Set the properties that are indirected
|
2018-04-19 19:28:31 +03:00
|
|
|
// set mobiledoc equal to what's in the editor but create a copy so that
|
|
|
|
// nested objects/arrays don't keep references which can mean that both
|
|
|
|
// scratch and mobiledoc get updated simultaneously
|
2019-01-30 17:44:53 +03:00
|
|
|
this.set('post.mobiledoc', JSON.parse(JSON.stringify(this.post.scratch || null)));
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('post.status', status);
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
// Set a default title
|
2018-01-11 01:57:43 +03:00
|
|
|
if (!this.get('post.titleScratch').trim()) {
|
|
|
|
this.set('post.titleScratch', DEFAULT_TITLE);
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('post.title', this.get('post.titleScratch'));
|
|
|
|
this.set('post.customExcerpt', this.get('post.customExcerptScratch'));
|
|
|
|
this.set('post.footerInjection', this.get('post.footerExcerptScratch'));
|
|
|
|
this.set('post.headerInjection', this.get('post.headerExcerptScratch'));
|
|
|
|
this.set('post.metaTitle', this.get('post.metaTitleScratch'));
|
|
|
|
this.set('post.metaDescription', this.get('post.metaDescriptionScratch'));
|
|
|
|
this.set('post.ogTitle', this.get('post.ogTitleScratch'));
|
|
|
|
this.set('post.ogDescription', this.get('post.ogDescriptionScratch'));
|
|
|
|
this.set('post.twitterTitle', this.get('post.twitterTitleScratch'));
|
|
|
|
this.set('post.twitterDescription', this.get('post.twitterDescriptionScratch'));
|
2019-11-04 09:15:13 +03:00
|
|
|
this.set('post.emailSubject', this.get('post.emailSubjectScratch'));
|
2018-01-11 01:57:43 +03:00
|
|
|
|
|
|
|
if (!this.get('post.slug')) {
|
2021-06-14 17:35:15 +03:00
|
|
|
this.saveTitleTask.cancelAll();
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2021-06-21 17:05:29 +03:00
|
|
|
yield this.generateSlugTask.perform();
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
|
2017-07-22 17:25:00 +03:00
|
|
|
try {
|
2021-06-21 17:05:29 +03:00
|
|
|
let post = yield this._savePostTask.perform(options);
|
2019-08-26 17:08:49 +03:00
|
|
|
|
|
|
|
post.set('statusScratch', null);
|
2017-07-22 17:25:00 +03:00
|
|
|
|
|
|
|
if (!options.silent) {
|
2021-02-01 20:59:18 +03:00
|
|
|
this.set('showPostPreviewModal', false);
|
2018-01-17 16:27:37 +03:00
|
|
|
this._showSaveNotification(prevStatus, post.get('status'), isNew ? true : false);
|
2017-07-22 17:25:00 +03:00
|
|
|
}
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2017-07-22 17:25:00 +03:00
|
|
|
// redirect to edit route if saving a new record
|
2018-01-11 01:57:43 +03:00
|
|
|
if (isNew && post.get('id')) {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this.leaveEditorTransition) {
|
2018-01-11 01:57:43 +03:00
|
|
|
this.replaceRoute('editor.edit', post);
|
2017-07-10 18:09:50 +03:00
|
|
|
}
|
2017-07-22 17:25:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-10 18:09:50 +03:00
|
|
|
|
2018-01-11 01:57:43 +03:00
|
|
|
return post;
|
2017-07-22 17:25:00 +03:00
|
|
|
} catch (error) {
|
2021-07-14 14:27:51 +03:00
|
|
|
if (this.showReAuthenticateModal) {
|
|
|
|
this._reauthSave = true;
|
|
|
|
this._reauthSaveOptions = options;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-28 17:28:50 +03:00
|
|
|
this.set('post.status', prevStatus);
|
|
|
|
|
2021-06-28 17:52:36 +03:00
|
|
|
if (error === undefined && this.post.errors.length === 0) {
|
|
|
|
// "handled" error from _saveTask
|
2021-06-28 16:59:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-17 16:57:42 +03:00
|
|
|
// trigger upgrade modal if forbidden(403) error
|
|
|
|
if (isHostLimitError(error)) {
|
|
|
|
this.post.rollbackAttributes();
|
|
|
|
this.set('hostLimitError', error.payload.errors[0]);
|
|
|
|
this.set('showUpgradeModal', true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
// re-throw if we have a general server error
|
|
|
|
if (error && !isInvalidError(error)) {
|
|
|
|
this.send('error', error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.silent) {
|
2018-01-11 01:57:43 +03:00
|
|
|
let errorOrMessages = error || this.get('post.errors.messages');
|
2018-01-17 16:27:37 +03:00
|
|
|
this._showErrorAlert(prevStatus, this.get('post.status'), errorOrMessages);
|
2017-04-11 16:39:45 +03:00
|
|
|
// simulate a validation error for upstream tasks
|
|
|
|
throw undefined;
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.post;
|
2017-07-22 17:25:00 +03:00
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2017-06-13 18:04:09 +03:00
|
|
|
/*
|
|
|
|
* triggered by a user manually changing slug
|
|
|
|
*/
|
2022-02-09 12:50:46 +03:00
|
|
|
@task({group: 'saveTasks'})
|
|
|
|
*updateSlugTask(_newSlug) {
|
2018-01-11 01:57:43 +03:00
|
|
|
let slug = this.get('post.slug');
|
2017-06-13 18:04:09 +03:00
|
|
|
let newSlug, serverSlug;
|
|
|
|
|
|
|
|
newSlug = _newSlug || slug;
|
|
|
|
newSlug = newSlug && newSlug.trim();
|
|
|
|
|
|
|
|
// Ignore unchanged slugs or candidate slugs that are empty
|
|
|
|
if (!newSlug || slug === newSlug) {
|
|
|
|
// reset the input to its previous state
|
|
|
|
this.set('slugValue', slug);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
serverSlug = yield this.slugGenerator.generateSlug('post', newSlug);
|
2017-06-13 18:04:09 +03:00
|
|
|
|
|
|
|
// If after getting the sanitized and unique slug back from the API
|
|
|
|
// we end up with a slug that matches the existing slug, abort the change
|
|
|
|
if (serverSlug === slug) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because the server transforms the candidate slug by stripping
|
|
|
|
// certain characters and appending a number onto the end of slugs
|
|
|
|
// to enforce uniqueness, there are cases where we can get back a
|
|
|
|
// candidate slug that is a duplicate of the original except for
|
|
|
|
// the trailing incrementor (e.g., this-is-a-slug and this-is-a-slug-2)
|
|
|
|
|
|
|
|
// get the last token out of the slug candidate and see if it's a number
|
|
|
|
let slugTokens = serverSlug.split('-');
|
|
|
|
let check = Number(slugTokens.pop());
|
|
|
|
|
|
|
|
// if the candidate slug is the same as the existing slug except
|
|
|
|
// for the incrementor then the existing slug should be used
|
|
|
|
if (isNumber(check) && check > 0) {
|
|
|
|
if (slug === slugTokens.join('-') && serverSlug !== newSlug) {
|
|
|
|
this.set('slugValue', slug);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('post.slug', serverSlug);
|
2017-06-13 18:04:09 +03:00
|
|
|
|
2018-01-11 01:57:43 +03:00
|
|
|
// If this is a new post. Don't save the post. Defer the save
|
2017-06-13 18:04:09 +03:00
|
|
|
// to the user pressing the save button
|
2018-01-11 01:57:43 +03:00
|
|
|
if (this.get('post.isNew')) {
|
2017-06-13 18:04:09 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:05:29 +03:00
|
|
|
return yield this._savePostTask.perform();
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2017-06-13 18:04:09 +03:00
|
|
|
|
2017-11-02 12:47:52 +03:00
|
|
|
// used in the PSM so that saves are sequential and don't trigger collision
|
|
|
|
// detection errors
|
2022-02-09 12:50:46 +03:00
|
|
|
@task({group: 'saveTasks'})
|
|
|
|
*savePostTask() {
|
2017-11-02 12:47:52 +03:00
|
|
|
try {
|
2021-06-21 17:05:29 +03:00
|
|
|
return yield this._savePostTask.perform();
|
2017-11-02 12:47:52 +03:00
|
|
|
} catch (error) {
|
2021-05-12 17:47:37 +03:00
|
|
|
if (error === undefined) {
|
|
|
|
// validation error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-02 12:47:52 +03:00
|
|
|
if (error) {
|
2018-01-11 01:57:43 +03:00
|
|
|
let status = this.get('post.status');
|
2018-01-17 16:27:37 +03:00
|
|
|
this._showErrorAlert(status, status, error);
|
2017-11-02 12:47:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2017-11-02 12:47:52 +03:00
|
|
|
|
2019-08-26 17:08:49 +03:00
|
|
|
// convenience method for saving the post and performing post-save cleanup
|
2022-02-09 12:50:46 +03:00
|
|
|
@task *_savePostTask(options = {}) {
|
2019-08-26 17:08:49 +03:00
|
|
|
let {post} = this;
|
|
|
|
|
2021-10-15 17:42:32 +03:00
|
|
|
const previousEmailOnlyValue = this.post.emailOnly;
|
|
|
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(options, 'emailOnly')) {
|
|
|
|
this.post.set('emailOnly', options.emailOnly);
|
|
|
|
}
|
|
|
|
|
2021-06-30 16:51:40 +03:00
|
|
|
try {
|
|
|
|
yield post.save(options);
|
|
|
|
} catch (error) {
|
2021-10-15 17:42:32 +03:00
|
|
|
this.post.set('emailOnly', previousEmailOnlyValue);
|
2021-06-30 16:51:40 +03:00
|
|
|
if (isServerUnreachableError(error)) {
|
|
|
|
const [prevStatus, newStatus] = this.post.changedAttributes().status || [this.post.status, this.post.status];
|
|
|
|
this._showErrorAlert(prevStatus, newStatus, error);
|
|
|
|
|
|
|
|
// simulate a validation error so we don't end up on a 500 screen
|
|
|
|
throw undefined;
|
2021-06-21 20:13:37 +03:00
|
|
|
}
|
2021-06-30 16:51:40 +03:00
|
|
|
|
|
|
|
throw error;
|
2021-06-21 20:13:37 +03:00
|
|
|
}
|
2019-08-26 17:08:49 +03:00
|
|
|
|
2021-06-30 16:51:40 +03:00
|
|
|
this.notifications.closeAlerts('post.save');
|
|
|
|
|
2019-08-26 17:08:49 +03:00
|
|
|
// remove any unsaved tags
|
|
|
|
// NOTE: `updateTags` changes `hasDirtyAttributes => true`.
|
|
|
|
// For a saved post it would otherwise be false.
|
|
|
|
post.updateTags();
|
|
|
|
this._previousTagNames = this._tagNames;
|
|
|
|
|
2019-10-09 12:53:57 +03:00
|
|
|
// update the scratch property if it's `null` and we get a blank mobiledoc
|
|
|
|
// back from the API - prevents "unsaved changes" modal on new+blank posts
|
|
|
|
if (!post.scratch) {
|
|
|
|
post.set('scratch', JSON.parse(JSON.stringify(post.get('mobiledoc'))));
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:08:49 +03:00
|
|
|
// if the two "scratch" properties (title and content) match the post,
|
|
|
|
// then it's ok to set hasDirtyAttributes to false
|
|
|
|
// TODO: why is this necessary?
|
|
|
|
let titlesMatch = post.get('titleScratch') === post.get('title');
|
|
|
|
let bodiesMatch = JSON.stringify(post.get('scratch')) === JSON.stringify(post.get('mobiledoc'));
|
|
|
|
|
|
|
|
if (titlesMatch && bodiesMatch) {
|
|
|
|
this.set('hasDirtyAttributes', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return post;
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2019-08-26 17:08:49 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@task *saveTitleTask() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let post = this.post;
|
2018-01-17 16:27:37 +03:00
|
|
|
let currentTitle = post.get('title');
|
|
|
|
let newTitle = post.get('titleScratch').trim();
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2021-06-14 17:35:15 +03:00
|
|
|
if ((currentTitle && newTitle && newTitle === currentTitle) || (!currentTitle && !newTitle)) {
|
2018-01-17 16:27:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// this is necessary to force a save when the title is blank
|
|
|
|
this.set('hasDirtyAttributes', true);
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// generate a slug if a post is new and doesn't have a title yet or
|
|
|
|
// if the title is still '(Untitled)'
|
|
|
|
if ((post.get('isNew') && !currentTitle) || currentTitle === DEFAULT_TITLE) {
|
2021-06-21 17:05:29 +03:00
|
|
|
yield this.generateSlugTask.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('post.isDraft')) {
|
2021-06-21 17:05:29 +03:00
|
|
|
yield this.autosaveTask.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
2019-01-21 14:44:30 +03:00
|
|
|
|
2019-05-20 18:16:19 +03:00
|
|
|
this.ui.updateDocumentTitle();
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2015-01-08 21:23:48 +03:00
|
|
|
|
2022-02-09 12:50:46 +03:00
|
|
|
@enqueueTask *generateSlugTask() {
|
2018-01-17 16:27:37 +03:00
|
|
|
let title = this.get('post.titleScratch');
|
|
|
|
|
|
|
|
// Only set an "untitled" slug once per post
|
|
|
|
if (title === DEFAULT_TITLE && this.get('post.slug')) {
|
|
|
|
return;
|
|
|
|
}
|
Scheduler UI
refs TryGhost/Ghost#6413 and TryGhost/Ghost#6870
needs TryGhost/Ghost#6861
- **Post Settings Menu (PSM)**:'Publish Date' input accepts a date from now, min. 2 minutes to allow scheduler processing on the server. Also, there will always be some delay between typing the date and clicking on the 'Schedule Post' button. If the user types a future date for an already published post, the date will be reseted and he sees the message, that the post needs to be unpublished first. Once, the date is accepted, the label will change to 'Scheduled Date'.
- adds a CP 'timeScheduled' to post model, which will return `true` if the publish time is currently in the future.
- **Changes to the button flow in editor**:
- if the the CP `timeScheduled` returns true, a different drop-down-menu will be shown: 'Schedule Post' replaces 'Publish Now' and 'Unschedule' replaces 'Unpublish'.
- Covering the _edge cases_, especially when a scheduled post is about to be published, while the user is in the editor.
- First, a new CP `scheduleCountdown` will return the remaining time, when the estimated publish time is 15 minutes from now. A notification with this live-ticker is shown next to the save button. Once, we reach a 2 minutes limit, another CP `statusFreeze` will return true and causes the save button to only show `Unschedule` in a red state, until we reach the publish time
- Once the publish time is reached, a CP `scheduledWillPublish` causes the buttons and the existing code to pretend we're already dealing with a publish post. At the moment, there's no way to make a background-fetch of the now serverside-scheduled post model from the server, so Ember doesn't know about the changed state at that time.
- Changes in the editor, which are done during this 'status freeze'-process will be saved back correctly, once the user hits 'Update Post' after the buttons changed back. A click on 'Unpublish' will change the status back to a draft.
- The user will get a regular 'toaster' notification that the post has been published.
- adds CP `isScheduled` for scheduled posts
- adds CP `offset` to component `gh-posts-list-item` and helper `gh-format-time-scheduled` to show schedule date in content overview.
- sets timeout in `gh-spin-button` to 10ms for `Ember.testing`
- changes error message in `gh-editor-base-controller` to be in one line, seperated with a `:`
TODOs:
- [x] new sort order for posts (1. scheduled, 2. draft, 3. published) (refs TryGhost/Ghost#6932)
- [ ] Move posts sorting from posts controller to model and refactor to use `Ember.comparable` mixin
- [x] Flows for draft -> scheduled -> published like described in TryGhost/Ghost#6870 incl. edge cases and button behaviour
- [x] Tests
- [x] new PSM behaviour for time/date in future
- [x] display publishedAt date with timezone offset on posts overview
2016-02-02 10:04:40 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
try {
|
2021-06-21 17:18:11 +03:00
|
|
|
let slug = yield this.slugGenerator.generateSlug('post', title);
|
Scheduler UI
refs TryGhost/Ghost#6413 and TryGhost/Ghost#6870
needs TryGhost/Ghost#6861
- **Post Settings Menu (PSM)**:'Publish Date' input accepts a date from now, min. 2 minutes to allow scheduler processing on the server. Also, there will always be some delay between typing the date and clicking on the 'Schedule Post' button. If the user types a future date for an already published post, the date will be reseted and he sees the message, that the post needs to be unpublished first. Once, the date is accepted, the label will change to 'Scheduled Date'.
- adds a CP 'timeScheduled' to post model, which will return `true` if the publish time is currently in the future.
- **Changes to the button flow in editor**:
- if the the CP `timeScheduled` returns true, a different drop-down-menu will be shown: 'Schedule Post' replaces 'Publish Now' and 'Unschedule' replaces 'Unpublish'.
- Covering the _edge cases_, especially when a scheduled post is about to be published, while the user is in the editor.
- First, a new CP `scheduleCountdown` will return the remaining time, when the estimated publish time is 15 minutes from now. A notification with this live-ticker is shown next to the save button. Once, we reach a 2 minutes limit, another CP `statusFreeze` will return true and causes the save button to only show `Unschedule` in a red state, until we reach the publish time
- Once the publish time is reached, a CP `scheduledWillPublish` causes the buttons and the existing code to pretend we're already dealing with a publish post. At the moment, there's no way to make a background-fetch of the now serverside-scheduled post model from the server, so Ember doesn't know about the changed state at that time.
- Changes in the editor, which are done during this 'status freeze'-process will be saved back correctly, once the user hits 'Update Post' after the buttons changed back. A click on 'Unpublish' will change the status back to a draft.
- The user will get a regular 'toaster' notification that the post has been published.
- adds CP `isScheduled` for scheduled posts
- adds CP `offset` to component `gh-posts-list-item` and helper `gh-format-time-scheduled` to show schedule date in content overview.
- sets timeout in `gh-spin-button` to 10ms for `Ember.testing`
- changes error message in `gh-editor-base-controller` to be in one line, seperated with a `:`
TODOs:
- [x] new sort order for posts (1. scheduled, 2. draft, 3. published) (refs TryGhost/Ghost#6932)
- [ ] Move posts sorting from posts controller to model and refactor to use `Ember.comparable` mixin
- [x] Flows for draft -> scheduled -> published like described in TryGhost/Ghost#6870 incl. edge cases and button behaviour
- [x] Tests
- [x] new PSM behaviour for time/date in future
- [x] display publishedAt date with timezone offset on posts overview
2016-02-02 10:04:40 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
if (!isBlank(slug)) {
|
|
|
|
this.set('post.slug', slug);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Nothing to do (would be nice to log this somewhere though),
|
|
|
|
// but a rejected promise needs to be handled here so that a resolved
|
|
|
|
// promise is returned.
|
|
|
|
if (isVersionMismatchError(error)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error);
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
Scheduler UI
refs TryGhost/Ghost#6413 and TryGhost/Ghost#6870
needs TryGhost/Ghost#6861
- **Post Settings Menu (PSM)**:'Publish Date' input accepts a date from now, min. 2 minutes to allow scheduler processing on the server. Also, there will always be some delay between typing the date and clicking on the 'Schedule Post' button. If the user types a future date for an already published post, the date will be reseted and he sees the message, that the post needs to be unpublished first. Once, the date is accepted, the label will change to 'Scheduled Date'.
- adds a CP 'timeScheduled' to post model, which will return `true` if the publish time is currently in the future.
- **Changes to the button flow in editor**:
- if the the CP `timeScheduled` returns true, a different drop-down-menu will be shown: 'Schedule Post' replaces 'Publish Now' and 'Unschedule' replaces 'Unpublish'.
- Covering the _edge cases_, especially when a scheduled post is about to be published, while the user is in the editor.
- First, a new CP `scheduleCountdown` will return the remaining time, when the estimated publish time is 15 minutes from now. A notification with this live-ticker is shown next to the save button. Once, we reach a 2 minutes limit, another CP `statusFreeze` will return true and causes the save button to only show `Unschedule` in a red state, until we reach the publish time
- Once the publish time is reached, a CP `scheduledWillPublish` causes the buttons and the existing code to pretend we're already dealing with a publish post. At the moment, there's no way to make a background-fetch of the now serverside-scheduled post model from the server, so Ember doesn't know about the changed state at that time.
- Changes in the editor, which are done during this 'status freeze'-process will be saved back correctly, once the user hits 'Update Post' after the buttons changed back. A click on 'Unpublish' will change the status back to a draft.
- The user will get a regular 'toaster' notification that the post has been published.
- adds CP `isScheduled` for scheduled posts
- adds CP `offset` to component `gh-posts-list-item` and helper `gh-format-time-scheduled` to show schedule date in content overview.
- sets timeout in `gh-spin-button` to 10ms for `Ember.testing`
- changes error message in `gh-editor-base-controller` to be in one line, seperated with a `:`
TODOs:
- [x] new sort order for posts (1. scheduled, 2. draft, 3. published) (refs TryGhost/Ghost#6932)
- [ ] Move posts sorting from posts controller to model and refactor to use `Ember.comparable` mixin
- [x] Flows for draft -> scheduled -> published like described in TryGhost/Ghost#6870 incl. edge cases and button behaviour
- [x] Tests
- [x] new PSM behaviour for time/date in future
- [x] display publishedAt date with timezone offset on posts overview
2016-02-02 10:04:40 +03:00
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
Scheduler UI
refs TryGhost/Ghost#6413 and TryGhost/Ghost#6870
needs TryGhost/Ghost#6861
- **Post Settings Menu (PSM)**:'Publish Date' input accepts a date from now, min. 2 minutes to allow scheduler processing on the server. Also, there will always be some delay between typing the date and clicking on the 'Schedule Post' button. If the user types a future date for an already published post, the date will be reseted and he sees the message, that the post needs to be unpublished first. Once, the date is accepted, the label will change to 'Scheduled Date'.
- adds a CP 'timeScheduled' to post model, which will return `true` if the publish time is currently in the future.
- **Changes to the button flow in editor**:
- if the the CP `timeScheduled` returns true, a different drop-down-menu will be shown: 'Schedule Post' replaces 'Publish Now' and 'Unschedule' replaces 'Unpublish'.
- Covering the _edge cases_, especially when a scheduled post is about to be published, while the user is in the editor.
- First, a new CP `scheduleCountdown` will return the remaining time, when the estimated publish time is 15 minutes from now. A notification with this live-ticker is shown next to the save button. Once, we reach a 2 minutes limit, another CP `statusFreeze` will return true and causes the save button to only show `Unschedule` in a red state, until we reach the publish time
- Once the publish time is reached, a CP `scheduledWillPublish` causes the buttons and the existing code to pretend we're already dealing with a publish post. At the moment, there's no way to make a background-fetch of the now serverside-scheduled post model from the server, so Ember doesn't know about the changed state at that time.
- Changes in the editor, which are done during this 'status freeze'-process will be saved back correctly, once the user hits 'Update Post' after the buttons changed back. A click on 'Unpublish' will change the status back to a draft.
- The user will get a regular 'toaster' notification that the post has been published.
- adds CP `isScheduled` for scheduled posts
- adds CP `offset` to component `gh-posts-list-item` and helper `gh-format-time-scheduled` to show schedule date in content overview.
- sets timeout in `gh-spin-button` to 10ms for `Ember.testing`
- changes error message in `gh-editor-base-controller` to be in one line, seperated with a `:`
TODOs:
- [x] new sort order for posts (1. scheduled, 2. draft, 3. published) (refs TryGhost/Ghost#6932)
- [ ] Move posts sorting from posts controller to model and refactor to use `Ember.comparable` mixin
- [x] Flows for draft -> scheduled -> published like described in TryGhost/Ghost#6870 incl. edge cases and button behaviour
- [x] Tests
- [x] new PSM behaviour for time/date in future
- [x] display publishedAt date with timezone offset on posts overview
2016-02-02 10:04:40 +03:00
|
|
|
|
2019-11-13 13:17:00 +03:00
|
|
|
// load supplementel data such as the members count in the background
|
2022-02-09 12:50:46 +03:00
|
|
|
@restartableTask *backgroundLoaderTask() {
|
2020-02-24 10:52:39 +03:00
|
|
|
try {
|
2021-01-28 18:25:21 +03:00
|
|
|
let membersResponse = yield this.store.query('member', {limit: 1, filter: 'subscribed:true'});
|
|
|
|
this.set('memberCount', get(membersResponse, 'meta.pagination.total'));
|
2020-02-24 10:52:39 +03:00
|
|
|
} catch (error) {
|
|
|
|
this.set('memberCount', 0);
|
2019-11-07 07:12:48 +03:00
|
|
|
}
|
2020-10-27 17:42:36 +03:00
|
|
|
|
|
|
|
yield this.store.query('snippet', {limit: 'all'});
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2019-11-04 12:13:18 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
/* Public methods --------------------------------------------------------*/
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// called by the new/edit routes to change the post model
|
|
|
|
setPost(post) {
|
2018-01-18 18:36:01 +03:00
|
|
|
// don't do anything else if we're setting the same post
|
2019-03-06 16:53:54 +03:00
|
|
|
if (post === this.post) {
|
2021-06-14 17:35:15 +03:00
|
|
|
this.set('shouldFocusTitle', post.get('isNew'));
|
2018-01-17 16:27:37 +03:00
|
|
|
return;
|
2014-06-06 05:18:03 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// reset everything ready for a new post
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
this.set('post', post);
|
2021-06-21 17:05:29 +03:00
|
|
|
this.backgroundLoaderTask.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2021-06-14 17:35:15 +03:00
|
|
|
// autofocus the title if we have a new post
|
|
|
|
this.set('shouldFocusTitle', post.get('isNew'));
|
2018-01-25 16:14:50 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// need to set scratch values because they won't be present on first
|
|
|
|
// edit of the post
|
|
|
|
// TODO: can these be `boundOneWay` on the model as per the other attrs?
|
|
|
|
post.set('titleScratch', post.get('title'));
|
|
|
|
post.set('scratch', post.get('mobiledoc'));
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this._previousTagNames = this._tagNames;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
// triggered any time the admin tab is closed, we need to use a native
|
|
|
|
// dialog here instead of our custom modal
|
|
|
|
window.onbeforeunload = () => {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.hasDirtyAttributes) {
|
2018-01-17 16:27:37 +03:00
|
|
|
return '==============================\n\n'
|
|
|
|
+ 'Hey there! It looks like you\'re in the middle of writing'
|
|
|
|
+ ' something and you haven\'t saved all of your content.'
|
|
|
|
+ '\n\nSave before you go!\n\n'
|
|
|
|
+ '==============================';
|
|
|
|
}
|
|
|
|
};
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// called by editor route's willTransition hook, fires for editor.new->edit,
|
2022-01-12 13:16:39 +03:00
|
|
|
// editor.edit->edit, or editor->any. Will either finish autosave then retry
|
|
|
|
// transition or abort and show the "are you sure want to leave?" modal
|
|
|
|
async willTransition(transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let post = this.post;
|
2014-06-28 07:01:56 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// exit early and allow transition if we have no post, occurs if reset
|
2022-01-12 13:16:39 +03:00
|
|
|
// has already been called
|
2018-01-17 16:27:37 +03:00
|
|
|
if (!post) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-28 07:01:56 +04:00
|
|
|
|
2018-06-14 18:55:02 +03:00
|
|
|
// clean up blank cards when leaving the editor if we have a draft post
|
|
|
|
// - blank cards could be left around due to autosave triggering whilst
|
|
|
|
// a blank card is present then the user attempting to leave
|
|
|
|
// - will mark the post as dirty so it gets saved when transitioning
|
|
|
|
if (this._koenig && post.isDraft) {
|
|
|
|
this._koenig.cleanup();
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let hasDirtyAttributes = this.hasDirtyAttributes;
|
2018-01-17 16:27:37 +03:00
|
|
|
let state = post.getProperties('isDeleted', 'isSaving', 'hasDirtyAttributes', 'isNew');
|
2014-06-28 07:01:56 +04:00
|
|
|
|
2022-01-12 13:16:39 +03:00
|
|
|
let fromNewToEdit = this.router.currentRouteName === 'editor.new'
|
2018-01-17 16:27:37 +03:00
|
|
|
&& transition.targetName === 'editor.edit'
|
|
|
|
&& transition.intent.contexts
|
|
|
|
&& transition.intent.contexts[0]
|
2022-01-12 13:16:39 +03:00
|
|
|
&& transition.intent.contexts[0].id === post.id;
|
2014-09-04 22:12:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
let deletedWithoutChanges = state.isDeleted
|
|
|
|
&& (state.isSaving || !state.hasDirtyAttributes);
|
|
|
|
|
|
|
|
// controller is dirty and we aren't in a new->edit or delete->index
|
|
|
|
// transition so show our "are you sure you want to leave?" modal
|
2022-01-12 13:16:39 +03:00
|
|
|
if (!this._leaveConfirmed && !fromNewToEdit && !deletedWithoutChanges && hasDirtyAttributes) {
|
2018-01-17 16:27:37 +03:00
|
|
|
transition.abort();
|
2022-01-12 13:16:39 +03:00
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.saveTasks.isRunning) {
|
|
|
|
await this.saveTasks.last;
|
|
|
|
return transition.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if an autosave is scheduled, cancel it, save then transition
|
|
|
|
if (this._autosaveRunning) {
|
|
|
|
this.send('cancelAutosave');
|
|
|
|
this.autosaveTask.cancelAll();
|
|
|
|
|
|
|
|
await this.autosaveTask.perform();
|
|
|
|
return transition.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
if (this.post) {
|
|
|
|
Object.assign(this._leaveModalReason, {status: this.post.status});
|
|
|
|
}
|
|
|
|
console.log('showing leave editor modal', this._leaveModalReason); // eslint-disable-line
|
|
|
|
|
|
|
|
const reallyLeave = await this.modals.open('modals/editor/confirm-leave');
|
|
|
|
|
|
|
|
if (reallyLeave !== true) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
this._leaveConfirmed = true;
|
|
|
|
transition.retry();
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// the transition is now certain to complete so cleanup and reset if
|
|
|
|
// we're exiting the editor. new->edit keeps everything around and
|
|
|
|
// edit->edit will call reset in the setPost method if necessary
|
|
|
|
if (!fromNewToEdit && transition.targetName !== 'editor.edit') {
|
|
|
|
this.reset();
|
2014-09-04 22:12:03 +04:00
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2014-06-28 07:01:56 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// called when the editor route is left or the post model is swapped
|
|
|
|
reset() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let post = this.post;
|
2016-02-24 07:39:54 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// make sure the save tasks aren't still running in the background
|
|
|
|
// after leaving the edit route
|
|
|
|
this.send('cancelAutosave');
|
2016-02-24 07:39:54 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
if (post) {
|
|
|
|
// clear post of any unsaved, client-generated tags
|
|
|
|
post.updateTags();
|
2015-06-03 05:56:42 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// remove new+unsaved records from the store and rollback any unsaved changes
|
|
|
|
if (post.get('isNew')) {
|
|
|
|
post.deleteRecord();
|
|
|
|
} else {
|
|
|
|
post.rollbackAttributes();
|
2015-06-03 05:56:42 +03:00
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
this._previousTagNames = [];
|
2022-01-12 13:16:39 +03:00
|
|
|
this._leaveConfirmed = false;
|
2014-08-07 07:39:19 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
this.set('post', null);
|
|
|
|
this.set('hasDirtyAttributes', false);
|
2021-06-14 17:35:15 +03:00
|
|
|
this.set('shouldFocusTitle', false);
|
2021-02-01 20:59:18 +03:00
|
|
|
this.set('showPostPreviewModal', false);
|
2021-06-17 21:00:59 +03:00
|
|
|
this.set('showSettingsMenu', false);
|
2018-07-20 17:53:21 +03:00
|
|
|
this.set('wordCount', null);
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// remove the onbeforeunload handler as it's only relevant whilst on
|
|
|
|
// the editor route
|
|
|
|
window.onbeforeunload = null;
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2014-11-29 22:42:57 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
/* Private tasks ---------------------------------------------------------*/
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// save 3 seconds after the last edit
|
2022-02-09 12:50:46 +03:00
|
|
|
@(task(function* () {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this._canAutosave) {
|
2018-01-17 16:27:37 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// force an instant save on first body edit for new posts
|
|
|
|
if (this.get('post.isNew')) {
|
2021-06-21 17:05:29 +03:00
|
|
|
return this.autosaveTask.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
yield timeout(AUTOSAVE_TIMEOUT);
|
2021-06-21 17:05:29 +03:00
|
|
|
this.autosaveTask.perform();
|
2022-02-09 12:50:46 +03:00
|
|
|
}).restartable())
|
2022-02-10 13:41:36 +03:00
|
|
|
_autosaveTask;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
// save at 60 seconds even if the user doesn't stop typing
|
2022-02-09 12:50:46 +03:00
|
|
|
@(task(function* () {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this._canAutosave) {
|
2018-01-17 16:27:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-03 19:52:39 +03:00
|
|
|
while (config.environment !== 'test' && true) {
|
2018-01-17 16:27:37 +03:00
|
|
|
yield timeout(TIMEDSAVE_TIMEOUT);
|
2021-06-21 17:05:29 +03:00
|
|
|
this.autosaveTask.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}).drop())
|
2022-02-10 13:41:36 +03:00
|
|
|
_timedSaveTask;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
/* Private methods -------------------------------------------------------*/
|
|
|
|
|
|
|
|
_hasDirtyAttributes() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let post = this.post;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
if (!post) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the Adapter failed to save the post isError will be true
|
|
|
|
// and we should consider the post still dirty.
|
|
|
|
if (post.get('isError')) {
|
2019-12-17 16:57:42 +03:00
|
|
|
this._leaveModalReason = {reason: 'isError', context: post.errors.messages};
|
2018-01-17 16:27:37 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// post.tags is an array so hasDirtyAttributes doesn't pick up
|
|
|
|
// changes unless the array ref is changed
|
2019-12-17 12:57:37 +03:00
|
|
|
let currentTags = (this._tagNames || []).join(', ');
|
|
|
|
let previousTags = (this._previousTagNames || []).join(', ');
|
2018-01-17 16:27:37 +03:00
|
|
|
if (currentTags !== previousTags) {
|
2019-12-17 12:57:37 +03:00
|
|
|
this._leaveModalReason = {reason: 'tags are different', context: {currentTags, previousTags}};
|
2018-01-17 16:27:37 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// titleScratch isn't an attr so needs a manual dirty check
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this.titleScratch !== this.title) {
|
2019-12-17 12:57:37 +03:00
|
|
|
this._leaveModalReason = {reason: 'title is different', context: {current: this.title, scratch: this.titleScratch}};
|
2018-01-17 16:27:37 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scratch isn't an attr so needs a manual dirty check
|
2018-01-18 18:36:01 +03:00
|
|
|
let mobiledoc = post.get('mobiledoc');
|
|
|
|
let scratch = post.get('scratch');
|
|
|
|
// additional guard in case we are trying to compare null with undefined
|
|
|
|
if (scratch || mobiledoc) {
|
|
|
|
let mobiledocJSON = JSON.stringify(mobiledoc);
|
|
|
|
let scratchJSON = JSON.stringify(scratch);
|
|
|
|
|
|
|
|
if (scratchJSON !== mobiledocJSON) {
|
2019-12-17 12:57:37 +03:00
|
|
|
this._leaveModalReason = {reason: 'mobiledoc is different', context: {current: mobiledocJSON, scratch: scratchJSON}};
|
2018-01-18 18:36:01 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// new+unsaved posts always return `hasDirtyAttributes: true`
|
|
|
|
// so we need a manual check to see if any
|
|
|
|
if (post.get('isNew')) {
|
|
|
|
let changedAttributes = Object.keys(post.changedAttributes());
|
2019-12-17 12:57:37 +03:00
|
|
|
|
|
|
|
if (changedAttributes.length) {
|
|
|
|
this._leaveModalReason = {reason: 'post.changedAttributes.length > 0', context: post.changedAttributes()};
|
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
return changedAttributes.length ? true : false;
|
2014-06-06 05:18:03 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// we've covered all the non-tracked cases we care about so fall
|
|
|
|
// back on Ember Data's default dirty attribute checks
|
2019-12-17 12:57:37 +03:00
|
|
|
let {hasDirtyAttributes} = post;
|
|
|
|
|
|
|
|
if (hasDirtyAttributes) {
|
|
|
|
this._leaveModalReason = {reason: 'post.hasDirtyAttributes === true', context: post.changedAttributes()};
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasDirtyAttributes;
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2020-06-30 13:15:15 +03:00
|
|
|
_showSaveNotification(prevStatus, status, delayed) {
|
|
|
|
// scheduled messaging is completely custom
|
|
|
|
if (status === 'scheduled') {
|
|
|
|
return this._showScheduledNotification(delayed);
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let notifications = this.notifications;
|
2020-06-30 13:15:15 +03:00
|
|
|
let message = messageMap.success.post[prevStatus][status];
|
2020-02-27 12:19:29 +03:00
|
|
|
let actions, type, path;
|
2014-06-26 13:42:29 +04:00
|
|
|
|
2020-02-27 12:19:29 +03:00
|
|
|
if (status === 'published' || status === 'scheduled') {
|
2021-01-25 12:23:03 +03:00
|
|
|
type = capitalize(this.get('post.displayName'));
|
2018-10-16 18:54:15 +03:00
|
|
|
path = this.get('post.url');
|
2020-02-27 12:19:29 +03:00
|
|
|
actions = `<a href="${path}" target="_blank">View ${type}</a>`;
|
2014-12-15 13:11:29 +03:00
|
|
|
}
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2021-07-16 10:50:28 +03:00
|
|
|
notifications.showNotification(message, {type: 'success', actions: (actions && htmlSafe(actions)), delayed});
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2020-06-30 13:15:15 +03:00
|
|
|
|
2021-06-11 13:44:42 +03:00
|
|
|
async _showScheduledNotification(delayed) {
|
2020-06-30 13:15:15 +03:00
|
|
|
let {
|
|
|
|
publishedAtUTC,
|
2020-11-06 21:54:27 +03:00
|
|
|
emailRecipientFilter,
|
2021-12-07 12:37:49 +03:00
|
|
|
previewUrl,
|
|
|
|
emailOnly
|
2020-06-30 13:15:15 +03:00
|
|
|
} = this.post;
|
|
|
|
let publishedAtBlogTZ = moment.tz(publishedAtUTC, this.settings.get('timezone'));
|
|
|
|
|
|
|
|
let title = 'Scheduled';
|
2021-12-07 12:37:49 +03:00
|
|
|
let description = emailOnly ? ['Will be sent'] : ['Will be published'];
|
2020-06-30 13:15:15 +03:00
|
|
|
|
2020-11-06 21:54:27 +03:00
|
|
|
if (emailRecipientFilter && emailRecipientFilter !== 'none') {
|
2021-06-11 13:44:42 +03:00
|
|
|
const recipientCount = await this.membersCountCache.countString(`subscribed:true+(${emailRecipientFilter})`);
|
2021-12-07 12:37:49 +03:00
|
|
|
description.push(`${!emailOnly ? 'and delivered ' : ''}to <span><strong>${recipientCount}</strong></span>`);
|
2020-06-30 13:15:15 +03:00
|
|
|
}
|
|
|
|
|
2020-08-04 10:34:24 +03:00
|
|
|
description.push(`on <span><strong>${publishedAtBlogTZ.format('MMM Do')}</strong></span>`);
|
2020-06-30 13:15:15 +03:00
|
|
|
description.push(`at <span><strong>${publishedAtBlogTZ.format('HH:mm')}</strong>`);
|
|
|
|
if (publishedAtBlogTZ.utcOffset() === 0) {
|
|
|
|
description.push('(UTC)</span>');
|
|
|
|
} else {
|
|
|
|
description.push(`(UTC${publishedAtBlogTZ.format('Z').replace(/([+-])0/, '$1').replace(/:00/, '')})</span>`);
|
|
|
|
}
|
|
|
|
|
2021-07-15 17:27:29 +03:00
|
|
|
description = htmlSafe(description.join(' '));
|
2020-06-30 13:15:15 +03:00
|
|
|
|
2021-07-15 17:27:29 +03:00
|
|
|
let actions = htmlSafe(`<a href="${previewUrl}" target="_blank">View Preview</a>`);
|
2020-06-30 13:15:15 +03:00
|
|
|
|
|
|
|
return this.notifications.showNotification(title, {description, actions, type: 'success', delayed});
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|
2014-06-26 13:42:29 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
_showErrorAlert(prevStatus, status, error, delay) {
|
|
|
|
let message = messageMap.errors.post[prevStatus][status];
|
2019-03-06 16:53:54 +03:00
|
|
|
let notifications = this.notifications;
|
2016-07-19 02:23:43 +03:00
|
|
|
let errorMessage;
|
2015-08-29 22:02:06 +03:00
|
|
|
|
|
|
|
function isString(str) {
|
|
|
|
return toString.call(str) === '[object String]';
|
|
|
|
}
|
|
|
|
|
2021-06-21 20:13:37 +03:00
|
|
|
if (isServerUnreachableError(error)) {
|
2021-07-05 19:15:01 +03:00
|
|
|
errorMessage = 'Unable to connect, please check your connection and try again';
|
2021-06-21 20:13:37 +03:00
|
|
|
} else if (error && isString(error)) {
|
2016-07-19 02:23:43 +03:00
|
|
|
errorMessage = error;
|
2016-07-22 12:14:32 +03:00
|
|
|
} else if (error && isEmberArray(error)) {
|
|
|
|
// This is here because validation errors are returned as an array
|
|
|
|
// TODO: remove this once validations are fixed
|
|
|
|
errorMessage = error[0];
|
2017-11-04 01:59:39 +03:00
|
|
|
} else if (error && error.payload && error.payload.errors && error.payload.errors[0].message) {
|
2019-03-12 20:49:15 +03:00
|
|
|
return this.notifications.showAPIError(error, {key: 'post.save'});
|
2015-08-29 22:02:06 +03:00
|
|
|
} else {
|
2016-07-19 02:23:43 +03:00
|
|
|
errorMessage = 'Unknown Error';
|
2015-08-29 22:02:06 +03:00
|
|
|
}
|
2014-06-26 13:42:29 +04:00
|
|
|
|
2016-07-19 02:23:43 +03:00
|
|
|
message += `: ${errorMessage}`;
|
2016-06-11 19:52:36 +03:00
|
|
|
message = htmlSafe(message);
|
2014-06-26 13:42:29 +04:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
notifications.showAlert(message, {type: 'error', delayed: delay, key: 'post.save'});
|
2014-06-08 10:02:21 +04:00
|
|
|
}
|
2022-02-09 12:50:46 +03:00
|
|
|
}
|