2018-01-17 16:27:37 +03:00
|
|
|
import 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';
|
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';
|
2019-01-10 18:00:07 +03:00
|
|
|
import {alias, mapBy} from '@ember/object/computed';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as controller} from '@ember/controller';
|
2019-11-04 12:44:18 +03:00
|
|
|
import {get} from '@ember/object';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {htmlSafe} from '@ember/string';
|
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {isArray as isEmberArray} from '@ember/array';
|
2016-09-26 19:59:04 +03:00
|
|
|
import {isInvalidError} from 'ember-ajax/errors';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-06-13 18:04:09 +03:00
|
|
|
import {task, taskGroup, timeout} from 'ember-concurrency';
|
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: {
|
|
|
|
published: 'Updated.',
|
|
|
|
draft: 'Saved.',
|
|
|
|
scheduled: 'Scheduled.'
|
|
|
|
},
|
|
|
|
draft: {
|
|
|
|
published: 'Published!',
|
|
|
|
draft: 'Saved.',
|
|
|
|
scheduled: 'Scheduled.'
|
|
|
|
},
|
|
|
|
scheduled: {
|
|
|
|
scheduled: 'Updated.',
|
|
|
|
draft: 'Unscheduled.',
|
|
|
|
published: 'Published!'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
application: controller(),
|
2018-01-18 18:36:01 +03:00
|
|
|
feature: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
notifications: service(),
|
2018-01-17 16:27:37 +03:00
|
|
|
router: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
slugGenerator: service(),
|
2018-02-07 12:42:46 +03:00
|
|
|
session: service(),
|
2017-10-30 12:38:01 +03:00
|
|
|
ui: service(),
|
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
|
|
|
|
2018-05-15 15:47:50 +03:00
|
|
|
infoMessage: null,
|
2018-01-17 16:27:37 +03:00
|
|
|
leaveEditorTransition: null,
|
|
|
|
shouldFocusEditor: false,
|
|
|
|
showDeletePostModal: false,
|
|
|
|
showLeaveEditorModal: false,
|
|
|
|
showReAuthenticateModal: false,
|
2019-11-05 08:21:29 +03:00
|
|
|
showEmailPreviewModal: false,
|
2018-01-18 18:36:01 +03:00
|
|
|
|
|
|
|
// koenig related properties
|
2018-07-20 17:53:21 +03:00
|
|
|
wordcount: null,
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
/* private properties ----------------------------------------------------*/
|
|
|
|
|
|
|
|
// set by setPost and _postSaved, used in hasDirtyAttributes
|
|
|
|
_previousTagNames: null,
|
|
|
|
|
|
|
|
/* computed properties ---------------------------------------------------*/
|
|
|
|
|
|
|
|
post: alias('model'),
|
|
|
|
|
|
|
|
// store the desired post status locally without updating the model,
|
|
|
|
// the model will only be updated when a save occurs
|
|
|
|
willPublish: boundOneWay('post.isPublished'),
|
|
|
|
willSchedule: boundOneWay('post.isScheduled'),
|
|
|
|
|
|
|
|
// updateSlug and save should always be enqueued so that we don't run into
|
|
|
|
// 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
|
|
|
|
saveTasks: taskGroup().enqueue(),
|
|
|
|
|
|
|
|
_tagNames: mapBy('post.tags', 'name'),
|
|
|
|
|
2018-05-03 19:52:39 +03:00
|
|
|
hasDirtyAttributes: computed(...watchedProps, {
|
2018-01-17 16:27:37 +03:00
|
|
|
get() {
|
|
|
|
return this._hasDirtyAttributes();
|
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
return value;
|
|
|
|
}
|
2018-05-03 19:52:39 +03:00
|
|
|
}),
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-11-04 12:13:18 +03:00
|
|
|
deliveredAction: computed('actionsList', function () {
|
|
|
|
return this.actionsList && this.actionsList.findBy('event', 'delivered');
|
|
|
|
}),
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
_autosaveRunning: computed('_autosave.isRunning', '_timedSave.isRunning', function () {
|
|
|
|
let autosave = this.get('_autosave.isRunning');
|
|
|
|
let timedsave = this.get('_timedSave.isRunning');
|
|
|
|
|
|
|
|
return autosave || timedsave;
|
|
|
|
}),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2018-01-11 01:57:43 +03:00
|
|
|
_canAutosave: computed('post.isDraft', function () {
|
2018-05-03 19:52:39 +03:00
|
|
|
return config.environment !== 'test' && this.get('post.isDraft');
|
2017-04-11 16:39:45 +03:00
|
|
|
}),
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
/* actions ---------------------------------------------------------------*/
|
2017-09-28 15:53:22 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
actions: {
|
|
|
|
updateScratch(mobiledoc) {
|
|
|
|
this.set('post.scratch', mobiledoc);
|
2017-07-22 02:11:24 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// save 3 seconds after last edit
|
2019-03-06 16:53:54 +03:00
|
|
|
this._autosave.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
// force save at 60 seconds
|
2019-03-06 16:53:54 +03:00
|
|
|
this._timedSave.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
},
|
|
|
|
updateTitleScratch(title) {
|
|
|
|
this.set('post.titleScratch', title);
|
|
|
|
},
|
2017-09-28 15:53:22 +03:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
// updates local willPublish/Schedule values, does not get applied to
|
|
|
|
// the post's `status` value until a save is triggered
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
save(options) {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.save.perform(options);
|
2018-01-17 16:27:37 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// used to prevent unexpected background saves. Triggered when opening
|
|
|
|
// publish menu, starting a manual save, and when leaving the editor
|
|
|
|
cancelAutosave() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this._autosave.cancelAll();
|
|
|
|
this._timedSave.cancelAll();
|
2018-01-17 16:27:37 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleLeaveEditorModal(transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let leaveTransition = this.leaveEditorTransition;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
// "cancel" was clicked in the "are you sure?" modal so we just
|
|
|
|
// reset the saved transition and remove the modal
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!transition && this.showLeaveEditorModal) {
|
2018-01-17 16:27:37 +03:00
|
|
|
this.set('leaveEditorTransition', null);
|
|
|
|
this.set('showLeaveEditorModal', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveEditorTransition', transition);
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.get('saveTasks.isRunning')) {
|
|
|
|
return this.get('saveTasks.last').then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// if an autosave is scheduled, cancel it, save then transition
|
2019-03-06 16:53:54 +03:00
|
|
|
if (this._autosaveRunning) {
|
2018-01-17 16:27:37 +03:00
|
|
|
this.send('cancelAutosave');
|
2019-03-06 16:53:54 +03:00
|
|
|
this.autosave.cancelAll();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.autosave.perform().then(() => {
|
2018-01-17 16:27:37 +03:00
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showLeaveEditorModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// called by the "are you sure?" modal
|
|
|
|
leaveEditor() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let transition = this.leaveEditorTransition;
|
2018-01-17 16:27:37 +03:00
|
|
|
|
|
|
|
if (!transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
2018-01-17 16:27:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform cleanup and reset manually, ensures the transition will succeed
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
return transition.retry();
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleDeletePostModal() {
|
|
|
|
if (!this.get('post.isNew')) {
|
|
|
|
this.toggleProperty('showDeletePostModal');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-05 08:21:29 +03:00
|
|
|
toggleEmailPreviewModal() {
|
|
|
|
this.toggleProperty('showEmailPreviewModal');
|
|
|
|
},
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
toggleReAuthenticateModal() {
|
|
|
|
this.toggleProperty('showReAuthenticateModal');
|
2018-01-18 18:36:01 +03:00
|
|
|
},
|
|
|
|
|
2018-06-14 18:55:02 +03:00
|
|
|
setKoenigEditor(koenig) {
|
|
|
|
this._koenig = koenig;
|
|
|
|
|
|
|
|
// 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
|
|
|
},
|
|
|
|
|
|
|
|
updateWordCount(counts) {
|
|
|
|
this.set('wordCount', counts);
|
2017-04-11 16:39:45 +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
|
|
|
|
autosave: task(function* () {
|
|
|
|
if (!this.get('save.isRunning')) {
|
2019-03-06 16:53:54 +03:00
|
|
|
return yield this.save.perform({
|
2015-05-10 01:23:14 +03:00
|
|
|
silent: true,
|
|
|
|
backgroundSave: true
|
2017-04-11 16:39:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}).drop(),
|
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
|
2017-07-22 17:25:00 +03:00
|
|
|
save: task(function* (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';
|
|
|
|
} else {
|
|
|
|
status = 'draft';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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')) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.saveTitle.cancelAll();
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
yield this.generateSlug.perform();
|
2017-04-11 16:39:45 +03:00
|
|
|
}
|
|
|
|
|
2017-07-22 17:25:00 +03:00
|
|
|
try {
|
2019-08-26 17:08:49 +03:00
|
|
|
let post = yield this._savePost.perform(options);
|
|
|
|
|
|
|
|
post.set('statusScratch', null);
|
2017-07-22 17:25:00 +03:00
|
|
|
|
|
|
|
if (!options.silent) {
|
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) {
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-11 01:57:43 +03:00
|
|
|
this.set('post.status', prevStatus);
|
2017-04-11 16:39:45 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}).group('saveTasks'),
|
2017-04-11 16:39:45 +03:00
|
|
|
|
2017-06-13 18:04:09 +03:00
|
|
|
/*
|
|
|
|
* triggered by a user manually changing slug
|
|
|
|
*/
|
|
|
|
updateSlug: task(function* (_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;
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:08:49 +03:00
|
|
|
return yield this._savePost.perform();
|
2017-06-13 18:04:09 +03:00
|
|
|
}).group('saveTasks'),
|
|
|
|
|
2017-11-02 12:47:52 +03:00
|
|
|
// used in the PSM so that saves are sequential and don't trigger collision
|
|
|
|
// detection errors
|
|
|
|
savePost: task(function* () {
|
|
|
|
try {
|
2019-08-26 17:08:49 +03:00
|
|
|
return yield this._savePost.perform();
|
2017-11-02 12:47:52 +03:00
|
|
|
} catch (error) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}).group('saveTasks'),
|
|
|
|
|
2019-08-26 17:08:49 +03:00
|
|
|
// convenience method for saving the post and performing post-save cleanup
|
|
|
|
_savePost: task(function* (options) {
|
|
|
|
let {post} = this;
|
|
|
|
|
|
|
|
yield post.save(options);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}),
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
saveTitle: task(function* () {
|
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
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
if (currentTitle && newTitle && newTitle === currentTitle) {
|
|
|
|
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) {
|
2019-03-06 16:53:54 +03:00
|
|
|
yield this.generateSlug.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get('post.isDraft')) {
|
2019-03-06 16:53:54 +03:00
|
|
|
yield this.autosave.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();
|
2015-01-08 21:23:48 +03:00
|
|
|
}),
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
generateSlug: task(function* () {
|
|
|
|
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 {
|
2019-03-06 16:53:54 +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
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
}).enqueue(),
|
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-04 12:13:18 +03:00
|
|
|
// load supplementel data such as the actions list in the background
|
|
|
|
backgroundLoader: task(function* () {
|
2019-11-07 07:12:48 +03:00
|
|
|
if (this.feature.members) {
|
|
|
|
let actions = yield this.store.query('action', {
|
|
|
|
filter: `resource_type:post+resource_id:${this.post.id}+event:delivered`,
|
|
|
|
limit: 'all'
|
|
|
|
});
|
|
|
|
this.set('actionsList', actions);
|
|
|
|
|
|
|
|
let membersResponse = yield this.store.query('member', {limit: 1});
|
|
|
|
this.set('memberCount', get(membersResponse, 'meta.pagination.total'));
|
|
|
|
}
|
2019-11-04 12:13:18 +03:00
|
|
|
}).restartable(),
|
|
|
|
|
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) {
|
2018-01-25 16:14:50 +03:00
|
|
|
// set autofocus as change signal to the persistent editor on new->edit
|
|
|
|
this.set('shouldFocusEditor', 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);
|
2019-11-04 12:13:18 +03:00
|
|
|
this.backgroundLoader.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2018-01-25 16:14:50 +03:00
|
|
|
// autofocus the editor if we have a new post
|
|
|
|
this.set('shouldFocusEditor', post.get('isNew'));
|
|
|
|
|
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'
|
|
|
|
+ '==============================';
|
|
|
|
}
|
|
|
|
};
|
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,
|
|
|
|
// editor.edit->edit, or editor->any. Triggers `toggleLeaveEditorModal` action
|
|
|
|
// which will either finish autosave then retry transition or abort and show
|
|
|
|
// the "are you sure?" modal
|
|
|
|
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
|
|
|
|
// has already been called as in the `leaveEditor` action
|
|
|
|
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
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
let fromNewToEdit = this.get('router.currentRouteName') === 'editor.new'
|
|
|
|
&& transition.targetName === 'editor.edit'
|
|
|
|
&& transition.intent.contexts
|
|
|
|
&& transition.intent.contexts[0]
|
|
|
|
&& transition.intent.contexts[0].id === post.get('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
|
|
|
|
if (!fromNewToEdit && !deletedWithoutChanges && hasDirtyAttributes) {
|
|
|
|
transition.abort();
|
|
|
|
this.send('toggleLeaveEditorModal', transition);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
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 = [];
|
2014-08-07 07:39:19 +04:00
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
this.set('post', null);
|
|
|
|
this.set('hasDirtyAttributes', false);
|
|
|
|
this.set('shouldFocusEditor', false);
|
|
|
|
this.set('leaveEditorTransition', null);
|
2018-05-15 15:47:50 +03:00
|
|
|
this.set('infoMessage', null);
|
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;
|
|
|
|
},
|
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
|
|
|
|
_autosave: 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')) {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.autosave.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
yield timeout(AUTOSAVE_TIMEOUT);
|
2019-03-06 16:53:54 +03:00
|
|
|
this.autosave.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
}).restartable(),
|
|
|
|
|
|
|
|
// save at 60 seconds even if the user doesn't stop typing
|
|
|
|
_timedSave: 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);
|
2019-03-06 16:53:54 +03:00
|
|
|
this.autosave.perform();
|
2018-01-17 16:27:37 +03:00
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
/* 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')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// post.tags is an array so hasDirtyAttributes doesn't pick up
|
|
|
|
// changes unless the array ref is changed
|
|
|
|
let currentTags = this.getWithDefault('_tagNames', []).join('');
|
|
|
|
let previousTags = this.getWithDefault('_previousTagNames', []).join('');
|
|
|
|
if (currentTags !== previousTags) {
|
|
|
|
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) {
|
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) {
|
|
|
|
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());
|
|
|
|
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
|
|
|
|
return post.get('hasDirtyAttributes');
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
2018-01-17 16:27:37 +03:00
|
|
|
_showSaveNotification(prevStatus, status, delay) {
|
|
|
|
let message = messageMap.success.post[prevStatus][status];
|
2019-03-06 16:53:54 +03:00
|
|
|
let notifications = this.notifications;
|
2016-01-12 23:29:57 +03:00
|
|
|
let type, path;
|
2014-06-26 13:42:29 +04:00
|
|
|
|
2014-12-15 13:11:29 +03:00
|
|
|
if (status === 'published') {
|
2018-01-17 16:27:37 +03:00
|
|
|
type = this.get('post.page') ? 'Page' : 'Post';
|
2018-10-16 18:54:15 +03:00
|
|
|
path = this.get('post.url');
|
2016-01-12 23:29:57 +03:00
|
|
|
} else {
|
|
|
|
type = 'Preview';
|
2018-01-11 01:57:43 +03:00
|
|
|
path = this.get('post.previewUrl');
|
2014-12-15 13:11:29 +03:00
|
|
|
}
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2016-01-12 23:29:57 +03:00
|
|
|
message += ` <a href="${path}" target="_blank">View ${type}</a>`;
|
|
|
|
|
2015-06-19 00:56:18 +03:00
|
|
|
notifications.showNotification(message.htmlSafe(), {delayed: delay});
|
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]';
|
|
|
|
}
|
|
|
|
|
2016-07-19 02:23:43 +03:00
|
|
|
if (error && isString(error)) {
|
|
|
|
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
|
|
|
}
|
2018-01-17 16:27:37 +03:00
|
|
|
|
2014-06-08 10:02:21 +04:00
|
|
|
});
|