2014-06-08 10:02:21 +04:00
|
|
|
/* global console */
|
2014-06-06 05:18:03 +04:00
|
|
|
import MarkerManager from 'ghost/mixins/marker-manager';
|
|
|
|
import PostModel from 'ghost/models/post';
|
2014-06-15 00:45:50 +04:00
|
|
|
import boundOneWay from 'ghost/utils/bound-one-way';
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// this array will hold properties we need to watch
|
|
|
|
// to know if the model has been changed (`controller.isDirty`)
|
2014-10-08 18:53:20 +04:00
|
|
|
var watchedProps = ['scratch', 'titleScratch', 'model.isDirty', 'tags.[]'];
|
2014-06-06 05:18:03 +04:00
|
|
|
|
2014-10-14 21:29:54 +04:00
|
|
|
PostModel.eachAttribute(function (name) {
|
2014-06-06 05:18:03 +04:00
|
|
|
watchedProps.push('model.' + name);
|
|
|
|
});
|
|
|
|
|
|
|
|
var EditorControllerMixin = Ember.Mixin.create(MarkerManager, {
|
2014-06-23 22:58:19 +04:00
|
|
|
needs: ['post-tags-input'],
|
|
|
|
|
2014-06-21 22:58:06 +04:00
|
|
|
init: function () {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
window.onbeforeunload = function () {
|
|
|
|
return self.get('isDirty') ? self.unloadDirtyMessage() : null;
|
|
|
|
};
|
|
|
|
},
|
2014-06-08 10:02:21 +04:00
|
|
|
/**
|
|
|
|
* By default, a post will not change its publish state.
|
|
|
|
* Only with a user-set value (via setSaveType action)
|
|
|
|
* can the post's status change.
|
|
|
|
*/
|
2014-06-15 00:45:50 +04:00
|
|
|
willPublish: boundOneWay('isPublished'),
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-10-08 18:53:20 +04:00
|
|
|
// Make sure editor starts with markdown shown
|
|
|
|
isPreview: false,
|
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// set by the editor route and `isDirty`. useful when checking
|
|
|
|
// whether the number of tags has changed for `isDirty`.
|
|
|
|
previousTagNames: null,
|
|
|
|
|
2014-10-08 18:53:20 +04:00
|
|
|
tagNames: Ember.computed('tags.@each.name', function () {
|
2014-06-06 05:18:03 +04:00
|
|
|
return this.get('tags').mapBy('name');
|
2014-07-30 05:57:19 +04:00
|
|
|
}),
|
2014-06-06 05:18:03 +04:00
|
|
|
|
|
|
|
// compares previousTagNames to tagNames
|
|
|
|
tagNamesEqual: function () {
|
|
|
|
var tagNames = this.get('tagNames'),
|
|
|
|
previousTagNames = this.get('previousTagNames'),
|
|
|
|
hashCurrent,
|
|
|
|
hashPrevious;
|
|
|
|
|
|
|
|
// beware! even if they have the same length,
|
|
|
|
// that doesn't mean they're the same.
|
|
|
|
if (tagNames.length !== previousTagNames.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// instead of comparing with slow, nested for loops,
|
|
|
|
// perform join on each array and compare the strings
|
|
|
|
hashCurrent = tagNames.join('');
|
|
|
|
hashPrevious = previousTagNames.join('');
|
|
|
|
|
|
|
|
return hashCurrent === hashPrevious;
|
|
|
|
},
|
|
|
|
|
2014-06-28 07:01:56 +04:00
|
|
|
// a hook created in editor-route-base's setupController
|
|
|
|
modelSaved: function () {
|
|
|
|
var model = this.get('model');
|
|
|
|
|
|
|
|
// safer to updateTags on save in one place
|
|
|
|
// rather than in all other places save is called
|
|
|
|
model.updateTags();
|
|
|
|
|
|
|
|
// set previousTagNames to current tagNames for isDirty check
|
|
|
|
this.set('previousTagNames', this.get('tagNames'));
|
|
|
|
|
|
|
|
// `updateTags` triggers `isDirty => true`.
|
|
|
|
// for a saved model it would otherwise be false.
|
2014-09-04 22:12:03 +04:00
|
|
|
|
|
|
|
// if the two "scratch" properties (title and content) match the model, then
|
|
|
|
// it's ok to set isDirty to false
|
|
|
|
if (this.get('titleScratch') === model.get('title') &&
|
|
|
|
this.get('scratch') === model.get('markdown')) {
|
|
|
|
|
|
|
|
this.set('isDirty', false);
|
|
|
|
}
|
2014-06-28 07:01:56 +04:00
|
|
|
},
|
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// an ugly hack, but necessary to watch all the model's properties
|
|
|
|
// and more, without having to be explicit and do it manually
|
|
|
|
isDirty: Ember.computed.apply(Ember, watchedProps.concat(function (key, value) {
|
|
|
|
if (arguments.length > 1) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var model = this.get('model'),
|
|
|
|
markdown = this.get('markdown'),
|
2014-08-07 07:39:19 +04:00
|
|
|
title = this.get('title'),
|
|
|
|
titleScratch = this.get('titleScratch'),
|
2014-06-06 05:18:03 +04:00
|
|
|
scratch = this.getMarkdown().withoutMarkers,
|
|
|
|
changedAttributes;
|
|
|
|
|
|
|
|
if (!this.tagNamesEqual()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-07 07:39:19 +04:00
|
|
|
if (titleScratch !== title) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-06 05:18:03 +04:00
|
|
|
// since `scratch` is not model property, we need to check
|
|
|
|
// it explicitly against the model's markdown attribute
|
|
|
|
if (markdown !== scratch) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// models created on the client always return `isDirty: true`,
|
|
|
|
// so we need to see which properties have actually changed.
|
|
|
|
if (model.get('isNew')) {
|
|
|
|
changedAttributes = Ember.keys(model.changedAttributes());
|
|
|
|
|
|
|
|
if (changedAttributes.length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// even though we use the `scratch` prop to show edits,
|
|
|
|
// which does *not* change the model's `isDirty` property,
|
|
|
|
// `isDirty` will tell us if the other props have changed,
|
|
|
|
// as long as the model is not new (model.isNew === false).
|
2014-10-08 18:53:20 +04:00
|
|
|
return model.get('isDirty');
|
2014-06-06 05:18:03 +04:00
|
|
|
})),
|
|
|
|
|
|
|
|
// used on window.onbeforeunload
|
|
|
|
unloadDirtyMessage: function () {
|
|
|
|
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-26 13:42:29 +04:00
|
|
|
//TODO: This has to be moved to the I18n localization file.
|
|
|
|
//This structure is supposed to be close to the i18n-localization which will be used soon.
|
|
|
|
messageMap: {
|
|
|
|
errors: {
|
|
|
|
post: {
|
|
|
|
published: {
|
2014-10-08 18:53:20 +04:00
|
|
|
published: 'Update failed.',
|
|
|
|
draft: 'Saving failed.'
|
2014-06-26 13:42:29 +04:00
|
|
|
},
|
|
|
|
draft: {
|
2014-10-08 18:53:20 +04:00
|
|
|
published: 'Publish failed.',
|
|
|
|
draft: 'Saving failed.'
|
2014-06-26 13:42:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
success: {
|
|
|
|
post: {
|
|
|
|
published: {
|
2014-10-08 18:53:20 +04:00
|
|
|
published: 'Updated.',
|
|
|
|
draft: 'Saved.'
|
2014-06-26 13:42:29 +04:00
|
|
|
},
|
|
|
|
draft: {
|
2014-10-08 18:53:20 +04:00
|
|
|
published: 'Published!',
|
|
|
|
draft: 'Saved.'
|
2014-06-26 13:42:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
showSaveNotification: function (prevStatus, status, delay) {
|
|
|
|
var message = this.messageMap.success.post[prevStatus][status];
|
|
|
|
|
2014-08-01 09:40:49 +04:00
|
|
|
this.notifications.showSuccess(message, { delayed: delay });
|
2014-06-26 13:42:29 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
showErrorNotification: function (prevStatus, status, errors, delay) {
|
|
|
|
var message = this.messageMap.errors.post[prevStatus][status];
|
|
|
|
|
|
|
|
message += '<br />' + errors[0].message;
|
|
|
|
|
2014-08-01 09:40:49 +04:00
|
|
|
this.notifications.showError(message, { delayed: delay });
|
2014-06-26 13:42:29 +04:00
|
|
|
},
|
|
|
|
|
2014-10-08 18:53:20 +04:00
|
|
|
shouldFocusTitle: Ember.computed.alias('model.isNew'),
|
2014-08-08 17:30:51 +04:00
|
|
|
|
2014-06-08 10:02:21 +04:00
|
|
|
actions: {
|
2014-10-08 18:53:20 +04:00
|
|
|
save: function (options) {
|
2014-06-08 10:02:21 +04:00
|
|
|
var status = this.get('willPublish') ? 'published' : 'draft',
|
2014-06-26 13:42:29 +04:00
|
|
|
prevStatus = this.get('status'),
|
2014-06-24 14:00:28 +04:00
|
|
|
isNew = this.get('isNew'),
|
2014-06-08 10:02:21 +04:00
|
|
|
self = this;
|
2014-10-08 18:53:20 +04:00
|
|
|
options = options || {};
|
2014-06-08 10:02:21 +04:00
|
|
|
|
2014-06-30 01:45:03 +04:00
|
|
|
self.notifications.closePassive();
|
2014-06-24 10:33:24 +04:00
|
|
|
|
2014-06-23 22:58:19 +04:00
|
|
|
// ensure an incomplete tag is finalised before save
|
|
|
|
this.get('controllers.post-tags-input').send('addNewTag');
|
|
|
|
|
2014-07-03 21:09:05 +04:00
|
|
|
// Set the properties that are indirected
|
2014-06-06 05:18:03 +04:00
|
|
|
// set markdown equal to what's in the editor, minus the image markers.
|
|
|
|
this.set('markdown', this.getMarkdown().withoutMarkers);
|
2014-06-08 10:02:21 +04:00
|
|
|
this.set('status', status);
|
2014-06-21 01:36:44 +04:00
|
|
|
|
2014-10-08 18:53:20 +04:00
|
|
|
// Set a default title
|
|
|
|
if (!this.get('titleScratch')) {
|
|
|
|
this.set('titleScratch', '(Untitled)');
|
|
|
|
}
|
|
|
|
this.set('title', this.get('titleScratch'));
|
|
|
|
|
2014-10-17 20:29:25 +04:00
|
|
|
return this.get('model').save(options).then(function (model) {
|
2014-10-08 18:53:20 +04:00
|
|
|
if (!options.silent) {
|
|
|
|
self.showSaveNotification(prevStatus, model.get('status'), isNew ? true : false);
|
|
|
|
}
|
2014-06-12 01:36:54 +04:00
|
|
|
return model;
|
2014-06-22 01:59:12 +04:00
|
|
|
}).catch(function (errors) {
|
2014-10-08 18:53:20 +04:00
|
|
|
if (!options.silent) {
|
|
|
|
self.showErrorNotification(prevStatus, self.get('status'), errors);
|
|
|
|
}
|
2014-08-15 00:39:02 +04:00
|
|
|
self.set('status', prevStatus);
|
|
|
|
|
2014-06-22 01:59:12 +04:00
|
|
|
return Ember.RSVP.reject(errors);
|
2014-06-21 01:36:44 +04:00
|
|
|
});
|
2014-06-08 10:02:21 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
setSaveType: function (newType) {
|
|
|
|
if (newType === 'publish') {
|
|
|
|
this.set('willPublish', true);
|
|
|
|
} else if (newType === 'draft') {
|
|
|
|
this.set('willPublish', false);
|
|
|
|
} else {
|
|
|
|
console.warn('Received invalid save type; ignoring.');
|
|
|
|
}
|
2014-06-06 05:18:03 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// set from a `sendAction` on the codemirror component,
|
|
|
|
// so that we get a reference for handling uploads.
|
|
|
|
setCodeMirror: function (codemirrorComponent) {
|
|
|
|
var codemirror = codemirrorComponent.get('codemirror');
|
|
|
|
|
|
|
|
this.set('codemirrorComponent', codemirrorComponent);
|
|
|
|
this.set('codemirror', codemirror);
|
|
|
|
},
|
|
|
|
|
|
|
|
// fired from the gh-markdown component when an image upload starts
|
|
|
|
disableCodeMirror: function () {
|
|
|
|
this.get('codemirrorComponent').disableCodeMirror();
|
|
|
|
},
|
|
|
|
|
|
|
|
// fired from the gh-markdown component when an image upload finishes
|
|
|
|
enableCodeMirror: function () {
|
|
|
|
this.get('codemirrorComponent').enableCodeMirror();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Match the uploaded file to a line in the editor, and update that line with a path reference
|
|
|
|
// ensuring that everything ends up in the correct place and format.
|
|
|
|
handleImgUpload: function (e, result_src) {
|
|
|
|
var editor = this.get('codemirror'),
|
|
|
|
line = this.findLine(Ember.$(e.currentTarget).attr('id')),
|
|
|
|
lineNumber = editor.getLineNumber(line),
|
|
|
|
match = line.text.match(/\([^\n]*\)?/),
|
|
|
|
replacement = '(http://)';
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
// simple case, we have the parenthesis
|
|
|
|
editor.setSelection(
|
|
|
|
{line: lineNumber, ch: match.index + 1},
|
|
|
|
{line: lineNumber, ch: match.index + match[0].length - 1}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
match = line.text.match(/\]/);
|
|
|
|
if (match) {
|
|
|
|
editor.replaceRange(
|
|
|
|
replacement,
|
|
|
|
{line: lineNumber, ch: match.index + 1},
|
|
|
|
{line: lineNumber, ch: match.index + 1}
|
|
|
|
);
|
|
|
|
editor.setSelection(
|
|
|
|
{line: lineNumber, ch: match.index + 2},
|
|
|
|
{line: lineNumber, ch: match.index + replacement.length }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
editor.replaceSelection(result_src);
|
2014-08-20 21:32:51 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
togglePreview: function (preview) {
|
|
|
|
this.set('isPreview', preview);
|
2014-10-08 18:53:20 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
autoSave: function () {
|
|
|
|
if (this.get('model.isDraft')) {
|
|
|
|
this.send('save', {silent: true, disableNProgress: true});
|
|
|
|
}
|
2014-10-18 00:57:10 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
autoSaveOnce: function () {
|
|
|
|
if (this.get('isNew')) {
|
|
|
|
this.send('autoSave');
|
|
|
|
}
|
2014-06-08 10:02:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default EditorControllerMixin;
|