2013-06-02 03:45:02 +04:00
|
|
|
// # Article Editor
|
|
|
|
|
2013-06-27 07:52:56 +04:00
|
|
|
/*global window, document, $, _, Backbone, Ghost, Showdown, CodeMirror, shortcut, Countable, JST */
|
2013-06-02 03:45:02 +04:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var PublishBar,
|
|
|
|
TagWidget,
|
|
|
|
ActionsWidget,
|
|
|
|
MarkdownShortcuts = [
|
|
|
|
{'key': 'Ctrl+B', 'style': 'bold'},
|
|
|
|
{'key': 'Meta+B', 'style': 'bold'},
|
|
|
|
{'key': 'Ctrl+I', 'style': 'italic'},
|
|
|
|
{'key': 'Meta+I', 'style': 'italic'},
|
|
|
|
{'key': 'Ctrl+Alt+U', 'style': 'strike'},
|
|
|
|
{'key': 'Ctrl+Shift+K', 'style': 'code'},
|
2013-06-11 01:52:24 +04:00
|
|
|
{'key': 'Ctrl+Alt+1', 'style': 'h1'},
|
|
|
|
{'key': 'Ctrl+Alt+2', 'style': 'h2'},
|
|
|
|
{'key': 'Ctrl+Alt+3', 'style': 'h3'},
|
|
|
|
{'key': 'Ctrl+Alt+4', 'style': 'h4'},
|
|
|
|
{'key': 'Ctrl+Alt+5', 'style': 'h5'},
|
|
|
|
{'key': 'Ctrl+Alt+6', 'style': 'h6'},
|
2013-06-02 03:45:02 +04:00
|
|
|
{'key': 'Ctrl+Shift+L', 'style': 'link'},
|
|
|
|
{'key': 'Ctrl+Shift+I', 'style': 'image'},
|
|
|
|
{'key': 'Ctrl+Q', 'style': 'blockquote'},
|
2013-07-18 17:02:54 +04:00
|
|
|
{'key': 'Ctrl+Shift+1', 'style': 'currentdate'},
|
|
|
|
{'key': 'Ctrl+U', 'style': 'uppercase'},
|
|
|
|
{'key': 'Ctrl+Shift+U', 'style': 'lowercase'},
|
|
|
|
{'key': 'Ctrl+Alt+Shift+U', 'style': 'titlecase'},
|
|
|
|
{'key': 'Ctrl+Alt+W', 'style': 'selectword'},
|
2013-07-22 16:50:50 +04:00
|
|
|
{'key': 'Ctrl+L', 'style': 'list'},
|
|
|
|
{'key': 'Ctrl+Alt+C', 'style': 'copyHTML'},
|
|
|
|
{'key': 'CMD+Alt+C', 'style': 'copyHTML'}
|
2013-06-02 03:45:02 +04:00
|
|
|
];
|
|
|
|
|
|
|
|
// The publish bar associated with a post, which has the TagWidget and
|
|
|
|
// Save button and options and such.
|
|
|
|
// ----------------------------------------
|
2013-06-04 16:38:45 +04:00
|
|
|
PublishBar = Ghost.View.extend({
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.addSubview(new TagWidget({el: this.$('#entry-categories'), model: this.model})).render();
|
|
|
|
this.addSubview(new ActionsWidget({el: this.$('#entry-actions'), model: this.model})).render();
|
2013-08-02 02:33:06 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function () { return this; }
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// The Tag UI area associated with a post
|
|
|
|
// ----------------------------------------
|
2013-06-04 16:38:45 +04:00
|
|
|
TagWidget = Ghost.View.extend({
|
2013-08-02 02:33:06 +04:00
|
|
|
render: function () { return this; }
|
2013-06-02 03:45:02 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
// The Publish, Queue, Publish Now buttons
|
|
|
|
// ----------------------------------------
|
2013-06-04 16:38:45 +04:00
|
|
|
ActionsWidget = Ghost.View.extend({
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
events: {
|
|
|
|
'click [data-set-status]': 'handleStatus',
|
|
|
|
'click .js-post-button': 'updatePost'
|
|
|
|
},
|
|
|
|
|
|
|
|
statusMap: {
|
|
|
|
'draft' : 'Save Draft',
|
|
|
|
'published': 'Update Post',
|
|
|
|
'scheduled' : 'Save Schedued Post'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
2013-06-11 01:52:24 +04:00
|
|
|
var self = this;
|
|
|
|
// Toggle publish
|
|
|
|
shortcut.add("Ctrl+Alt+P", function () {
|
|
|
|
self.toggleStatus();
|
|
|
|
});
|
|
|
|
shortcut.add("Ctrl+S", function () {
|
|
|
|
self.updatePost();
|
|
|
|
});
|
|
|
|
shortcut.add("Meta+S", function () {
|
|
|
|
self.updatePost();
|
|
|
|
});
|
2013-06-02 03:45:02 +04:00
|
|
|
this.listenTo(this.model, 'change:status', this.render);
|
|
|
|
this.model.on('change:id', function (m) {
|
|
|
|
Backbone.history.navigate('/editor/' + m.id);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-06-11 01:52:24 +04:00
|
|
|
toggleStatus: function () {
|
|
|
|
var keys = Object.keys(this.statusMap),
|
|
|
|
model = this.model,
|
|
|
|
currentIndex = keys.indexOf(model.get('status')),
|
|
|
|
newIndex;
|
|
|
|
|
|
|
|
|
|
|
|
if (keys[currentIndex + 1] === 'scheduled') { // TODO: Remove once scheduled posts work
|
|
|
|
newIndex = currentIndex + 2 > keys.length - 1 ? 0 : currentIndex + 1;
|
|
|
|
} else {
|
|
|
|
newIndex = currentIndex + 1 > keys.length - 1 ? 0 : currentIndex + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.savePost({
|
|
|
|
status: keys[newIndex]
|
|
|
|
}).then(function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: 'Your post: ' + model.get('title') + ' has been ' + keys[newIndex],
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-07-19 03:53:08 +04:00
|
|
|
}, function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: 'Your post: ' + model.get('title') + ' has not been ' + keys[newIndex],
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-06-11 01:52:24 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
handleStatus: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var status = $(e.currentTarget).attr('data-set-status'),
|
2013-08-06 12:00:12 +04:00
|
|
|
model = this.model;
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
if (status === 'publish-on') {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'alert',
|
|
|
|
message: 'Scheduled publishing not supported yet.',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-06-02 03:45:02 +04:00
|
|
|
}
|
|
|
|
if (status === 'queue') {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'alert',
|
|
|
|
message: 'Scheduled publishing not supported yet.',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-06-02 03:45:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.savePost({
|
|
|
|
status: status
|
|
|
|
}).then(function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: 'Your post: ' + model.get('title') + ' has been ' + status,
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-07-19 03:53:08 +04:00
|
|
|
}, function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: 'Your post: ' + model.get('title') + ' has not been ' + status,
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-06-02 03:45:02 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
updatePost: function (e) {
|
2013-06-11 01:52:24 +04:00
|
|
|
if (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
2013-08-06 12:00:12 +04:00
|
|
|
var model = this.model;
|
2013-06-04 16:38:45 +04:00
|
|
|
this.savePost().then(function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: 'Your post was saved as ' + model.get('status'),
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-06-04 16:38:45 +04:00
|
|
|
}, function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: model.validationError,
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-06-04 16:38:45 +04:00
|
|
|
});
|
2013-06-02 03:45:02 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
savePost: function (data) {
|
2013-07-11 19:27:19 +04:00
|
|
|
// TODO: The content_raw getter here isn't great, shouldn't rely on currentView.
|
2013-07-07 22:41:05 +04:00
|
|
|
_.each(this.model.blacklist, function (item) {
|
|
|
|
this.model.unset(item);
|
|
|
|
}, this);
|
|
|
|
|
2013-06-04 16:38:45 +04:00
|
|
|
var saved = this.model.save(_.extend({
|
2013-06-02 03:45:02 +04:00
|
|
|
title: $('#entry-title').val(),
|
2013-07-11 19:27:19 +04:00
|
|
|
content_raw: Ghost.currentView.editor.getValue()
|
2013-06-02 03:45:02 +04:00
|
|
|
}, data));
|
2013-06-04 16:38:45 +04:00
|
|
|
|
|
|
|
// TODO: Take this out if #2489 gets merged in Backbone. Or patch Backbone
|
|
|
|
// ourselves for more consistent promises.
|
|
|
|
if (saved) {
|
|
|
|
return saved;
|
|
|
|
}
|
|
|
|
return $.Deferred().reject();
|
2013-06-02 03:45:02 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
this.$('.js-post-button').text(this.statusMap[this.model.get('status')]);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// The entire /editor page's route (TODO: move all views to client side templates)
|
|
|
|
// ----------------------------------------
|
2013-06-04 16:38:45 +04:00
|
|
|
Ghost.Views.Editor = Ghost.View.extend({
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
|
|
|
|
// Add the container view for the Publish Bar
|
|
|
|
this.addSubview(new PublishBar({el: "#publish-bar", model: this.model})).render();
|
|
|
|
|
2013-07-11 19:27:19 +04:00
|
|
|
this.$('#entry-markdown').html(this.model.get('content_raw'));
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
this.initMarkdown();
|
|
|
|
this.renderPreview();
|
|
|
|
|
2013-08-01 18:28:13 +04:00
|
|
|
$('.entry-content header, .entry-preview header').on('click', function () {
|
|
|
|
$('.entry-content, .entry-preview').removeClass('active');
|
|
|
|
$(this).closest('section').addClass('active');
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.entry-title .icon-fullscreen').on('click', function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$('body').toggleClass('fullscreen');
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.options.up').on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
$(this).next("ul").fadeToggle(200);
|
|
|
|
});
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
this.$('.CodeMirror-scroll').on('scroll', this.syncScroll);
|
|
|
|
|
|
|
|
// Shadow on Markdown if scrolled
|
|
|
|
this.$('.CodeMirror-scroll').on('scroll', function (e) {
|
|
|
|
if ($('.CodeMirror-scroll').scrollTop() > 10) {
|
|
|
|
$('.entry-markdown').addClass('scrolling');
|
|
|
|
} else {
|
|
|
|
$('.entry-markdown').removeClass('scrolling');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Shadow on Preview if scrolled
|
|
|
|
this.$('.entry-preview-content').on('scroll', function (e) {
|
|
|
|
if ($('.entry-preview-content').scrollTop() > 10) {
|
|
|
|
$('.entry-preview').addClass('scrolling');
|
|
|
|
} else {
|
|
|
|
$('.entry-preview').removeClass('scrolling');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Zen writing mode shortcut
|
|
|
|
shortcut.add("Alt+Shift+Z", function () {
|
|
|
|
$('body').toggleClass('zen');
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.entry-markdown header, .entry-preview header').click(function (e) {
|
|
|
|
$('.entry-markdown, .entry-preview').removeClass('active');
|
|
|
|
$(e.target).closest('section').addClass('active');
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-07-24 14:29:20 +04:00
|
|
|
events: {
|
|
|
|
'click .markdown-help': 'showHelp'
|
|
|
|
},
|
|
|
|
|
2013-07-18 15:06:52 +04:00
|
|
|
syncScroll: _.debounce(function (e) {
|
2013-06-02 03:45:02 +04:00
|
|
|
var $codeViewport = $(e.target),
|
|
|
|
$previewViewport = $('.entry-preview-content'),
|
|
|
|
$codeContent = $('.CodeMirror-sizer'),
|
|
|
|
$previewContent = $('.rendered-markdown'),
|
|
|
|
|
|
|
|
// calc position
|
|
|
|
codeHeight = $codeContent.height() - $codeViewport.height(),
|
|
|
|
previewHeight = $previewContent.height() - $previewViewport.height(),
|
|
|
|
ratio = previewHeight / codeHeight,
|
|
|
|
previewPostition = $codeViewport.scrollTop() * ratio;
|
|
|
|
|
|
|
|
// apply new scroll
|
|
|
|
$previewViewport.scrollTop(previewPostition);
|
2013-07-18 15:06:52 +04:00
|
|
|
}, 50),
|
2013-06-02 03:45:02 +04:00
|
|
|
|
2013-07-24 14:29:20 +04:00
|
|
|
showHelp: function () {
|
|
|
|
this.addSubview(new Ghost.Views.Modal({
|
|
|
|
model: {
|
2013-07-26 18:32:44 +04:00
|
|
|
options: {
|
|
|
|
close: true,
|
|
|
|
type: "info",
|
|
|
|
style: "wide",
|
|
|
|
animation: 'fadeIn'
|
2013-07-24 14:29:20 +04:00
|
|
|
},
|
2013-07-26 18:32:44 +04:00
|
|
|
content: {
|
|
|
|
template: 'markdown',
|
|
|
|
title: 'Markdown Help'
|
|
|
|
}
|
2013-07-24 14:29:20 +04:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
// This updates the editor preview panel.
|
|
|
|
// Currently gets called on every key press.
|
|
|
|
// Also trigger word count update
|
|
|
|
renderPreview: function () {
|
|
|
|
var view = this,
|
|
|
|
preview = document.getElementsByClassName('rendered-markdown')[0];
|
|
|
|
preview.innerHTML = this.converter.makeHtml(this.editor.getValue());
|
|
|
|
Countable.once(preview, function (counter) {
|
|
|
|
view.$('.entry-word-count').text(counter.words + ' words');
|
|
|
|
view.$('.entry-character-count').text(counter.characters + ' characters');
|
|
|
|
view.$('.entry-paragraph-count').text(counter.paragraphs + ' paragraphs');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Markdown converter & markdown shortcut initialization.
|
|
|
|
initMarkdown: function () {
|
|
|
|
this.converter = new Showdown.converter({extensions: ['ghostdown']});
|
|
|
|
this.editor = CodeMirror.fromTextArea(document.getElementById('entry-markdown'), {
|
|
|
|
mode: 'markdown',
|
|
|
|
tabMode: 'indent',
|
2013-06-23 14:49:30 +04:00
|
|
|
tabindex: "2",
|
2013-06-02 03:45:02 +04:00
|
|
|
lineWrapping: true
|
|
|
|
});
|
|
|
|
|
2013-06-11 01:52:24 +04:00
|
|
|
var view = this;
|
|
|
|
|
2013-07-25 19:00:41 +04:00
|
|
|
// Inject modal for HTML to be viewed in
|
|
|
|
shortcut.add("Ctrl+Alt+C", function () {
|
|
|
|
view.showHTML();
|
|
|
|
});
|
|
|
|
shortcut.add("Ctrl+Alt+C", function () {
|
|
|
|
view.showHTML();
|
|
|
|
});
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
_.each(MarkdownShortcuts, function (combo) {
|
|
|
|
shortcut.add(combo.key, function () {
|
2013-06-11 01:52:24 +04:00
|
|
|
return view.editor.addMarkdown({style: combo.style});
|
2013-06-02 03:45:02 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.editor.on('change', function () {
|
|
|
|
view.renderPreview();
|
|
|
|
});
|
2013-07-25 19:00:41 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
showHTML: function () {
|
|
|
|
this.addSubview(new Ghost.Views.Modal({
|
|
|
|
model: {
|
2013-07-26 18:32:44 +04:00
|
|
|
options: {
|
|
|
|
close: true,
|
|
|
|
type: "info",
|
|
|
|
style: "wide",
|
|
|
|
animation: 'fadeIn'
|
2013-07-25 19:00:41 +04:00
|
|
|
},
|
2013-07-26 18:32:44 +04:00
|
|
|
content: {
|
|
|
|
template: 'copyToHTML',
|
|
|
|
title: 'Copied HTML'
|
|
|
|
}
|
2013-07-25 19:00:41 +04:00
|
|
|
}
|
|
|
|
}));
|
2013-08-02 02:33:06 +04:00
|
|
|
},
|
2013-06-27 07:52:56 +04:00
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
render: function () { return this; }
|
|
|
|
});
|
2013-06-27 07:52:56 +04:00
|
|
|
|
|
|
|
}());
|