Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
🐛 fix autosave+transition on title blur with empty title (#767)
Browse files Browse the repository at this point in the history
refs TryGhost/Ghost#8525
- fix `saveTitle` action
	- don't abort title save when we have an empty title
	- force a "dirty" state so that the save actually happens
- add acceptance test for title blur behaviour
- extract multiple instances `"(Untitled)"` into a const
  • Loading branch information
kevinansfield authored and aileen committed Jul 10, 2017
1 parent 8b95438 commit a9c8d72
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/mixins/editor-base-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {testing} = Ember;
// to know if the model has been changed (`controller.hasDirtyAttributes`)
const watchedProps = ['model.scratch', 'model.titleScratch', 'model.hasDirtyAttributes', 'model.tags.[]'];

const DEFAULT_TITLE = '(Untitled)';
const TITLE_DEBOUNCE = testing ? 10 : 700;

PostModel.eachAttribute(function (name) {
Expand Down Expand Up @@ -148,7 +149,7 @@ export default Mixin.create({

// Set a default title
if (!this.get('model.titleScratch').trim()) {
this.set('model.titleScratch', '(Untitled)');
this.set('model.titleScratch', DEFAULT_TITLE);
}

this.set('model.title', this.get('model.titleScratch'));
Expand Down Expand Up @@ -492,9 +493,9 @@ export default Mixin.create({

model.set('titleScratch', newTitle);

// if model is not new and title is not '(Untitled)', or model is new and
// if model is not new and title is not DEFAULT_TITLE, or model is new and
// has a title, don't generate a slug
if ((!model.get('isNew') || model.get('title')) && newTitle !== '(Untitled)') {
if ((!model.get('isNew') || model.get('title')) && newTitle !== DEFAULT_TITLE) {
return;
}

Expand All @@ -508,7 +509,7 @@ export default Mixin.create({
let title = this.get('model.titleScratch');

// Only set an "untitled" slug once per post
if (title === '(Untitled)' && this.get('model.slug')) {
if (title === DEFAULT_TITLE && this.get('model.slug')) {
return;
}

Expand Down Expand Up @@ -606,10 +607,13 @@ export default Mixin.create({
let currentTitle = this.get('model.title');
let newTitle = this.get('model.titleScratch').trim();

if (newTitle === currentTitle) {
if (currentTitle && newTitle && newTitle === currentTitle) {
return;
}

// this is necessary to force a save when the title is blank
this.set('hasDirtyAttributes', true);

if (this.get('model.isDraft')) {
this.send('save', {
silent: true,
Expand Down
4 changes: 4 additions & 0 deletions mirage/config/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export default function mockPosts(server) {
server.post('/posts', function ({posts}) {
let attrs = this.normalizedRequestAttrs();

// mirage expects `author` to be a reference but we only have an ID
attrs.authorId = attrs.author;
delete attrs.author;

if (isBlank(attrs.slug) && !isBlank(attrs.title)) {
attrs.slug = dasherize(attrs.title);
}
Expand Down
28 changes: 28 additions & 0 deletions tests/acceptance/editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,5 +520,33 @@ describe('Acceptance: Editor', function() {
expect(find('select[name="post-setting-author"]').val()).to.equal('2');
expect(server.db.posts[0].authorId).to.equal(author.id);
});

it('autosaves when title loses focus', async function () {
let role = server.create('role', {name: 'Administrator'});
server.create('user', {name: 'Admin', roles: [role]});

await visit('/editor');

// NOTE: there were checks here for the title element having focus
// but they were very temperamental whilst running tests in the
// browser so they've been left out for now

expect(
currentURL(),
'url on initial visit'
).to.equal('/editor');

await triggerEvent(testSelector('editor-title-input'), 'blur');

expect(
find(testSelector('editor-title-input')).val(),
'title value after autosave'
).to.equal('(Untitled)');

expect(
currentURL(),
'url after autosave'
).to.equal('/editor/1');
});
});
});

0 comments on commit a9c8d72

Please sign in to comment.