Replace the component bounded cachedValue by a global UnsavedEdits

This new draft saving system is currently only implemented for the
card description and comment. We need better a component
inheritance/composition model to support this for all editable fields.

Fixes #186
This commit is contained in:
Maxime Quandalle 2015-08-31 15:09:53 +02:00
parent cc88e78483
commit d644cba38f
13 changed files with 252 additions and 95 deletions

View file

@ -81,6 +81,7 @@
"Popup": true, "Popup": true,
"Sidebar": true, "Sidebar": true,
"Utils": true, "Utils": true,
"InlinedForm": true,
// XXX Temp, we should remove these // XXX Temp, we should remove these
"allowIsBoardAdmin": true, "allowIsBoardAdmin": true,

View file

@ -4,5 +4,6 @@ template(name="commentForm")
+userAvatar(userId=currentUser._id) +userAvatar(userId=currentUser._id)
form.js-new-comment-form form.js-new-comment-form
+editor(class="js-new-comment-input") +editor(class="js-new-comment-input")
| {{getUnsavedValue 'cardComment' currentCard._id}}
.add-controls .add-controls
button.primary.confirm.clear.js-add-comment(type="submit") {{_ 'comment'}} button.primary.confirm.clear.js-add-comment(type="submit") {{_ 'comment'}}

View file

@ -1,47 +1,83 @@
var commentFormIsOpen = new ReactiveVar(false); let commentFormIsOpen = new ReactiveVar(false);
Template.commentForm.helpers({ BlazeComponent.extendComponent({
commentFormIsOpen: function() { template() {
return 'commentForm';
},
onDestroyed() {
commentFormIsOpen.set(false);
},
commentFormIsOpen() {
return commentFormIsOpen.get(); return commentFormIsOpen.get();
} },
});
Template.commentForm.events({ getInput() {
return this.$('.js-new-comment-input');
},
events() {
return [{
'click .js-new-comment:not(.focus)': function() { 'click .js-new-comment:not(.focus)': function() {
commentFormIsOpen.set(true); commentFormIsOpen.set(true);
}, },
'submit .js-new-comment-form': function(evt, tpl) { 'submit .js-new-comment-form': function(evt) {
var input = tpl.$('.js-new-comment-input'); let input = this.getInput();
if ($.trim(input.val())) { if ($.trim(input.val())) {
CardComments.insert({ CardComments.insert({
boardId: this.boardId, boardId: this.boardId,
cardId: this._id, cardId: this._id,
text: input.val() text: input.val()
}); });
input.val(''); resetCommentInput(input);
input.blur();
commentFormIsOpen.set(false);
Tracker.flush(); Tracker.flush();
autosize.update(input); autosize.update(input);
} }
evt.preventDefault(); evt.preventDefault();
}, },
// Pressing Ctrl+Enter should submit the form // Pressing Ctrl+Enter should submit the form
'keydown form textarea': function(evt, tpl) { 'keydown form textarea': function(evt) {
if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) { if (evt.keyCode === 13 && (evt.metaKey || evt.ctrlKey)) {
tpl.find('button[type=submit]').click(); this.find('button[type=submit]').click();
} }
} }
}); }];
}
}).register('commentForm');
Template.commentForm.onDestroyed(function() { // XXX This should be a static method of the `commentForm` component
function resetCommentInput(input) {
input.val('');
input.blur();
commentFormIsOpen.set(false); commentFormIsOpen.set(false);
}
// XXX This should handled a `onUpdated` callback of the `commentForm` component
// but since this callback doesn't exists, and `onRendered` is not called if the
// data is not destroyed and recreated, we simulate the desired callback using
// Tracker.autorun to register the component dependencies, and re-run when these
// dependencies are invalidated. A better component API would remove this hack.
Tracker.autorun(() => {
Session.get('currentCard');
Tracker.afterFlush(() => {
autosize.update($('.js-new-comment-input'));
}); });
})
EscapeActions.register('inlinedForm', EscapeActions.register('inlinedForm',
function() { function() {
commentFormIsOpen.set(false); const draftKey = {
$('.js-new-comment-input').blur(); fieldName: 'cardComment',
docId: Session.get('currentCard')
};
let commentInput = $('.js-new-comment-input');
if ($.trim(commentInput.val())) {
UnsavedEdits.set(draftKey, commentInput.val());
} else {
UnsavedEdits.reset(draftKey);
}
resetCommentInput(commentInput);
}, },
function() { return commentFormIsOpen.get(); }, { function() { return commentFormIsOpen.get(); }, {
noClickEscapeOn: '.js-new-comment' noClickEscapeOn: '.js-new-comment'

View file

@ -34,11 +34,11 @@ template(name="cardDetails")
//- XXX We should use "editable" to avoid repetiting ourselves //- XXX We should use "editable" to avoid repetiting ourselves
if currentUser.isBoardMember if currentUser.isBoardMember
h3.card-details-item-title Description h3.card-details-item-title Description
+inlinedForm(classNames="card-description js-card-description") +inlinedCardDescription(classNames="card-description js-card-description")
+editor(autofocus=true) +editor(autofocus=true)
= description | {{getUnsavedValue 'cardDescription' _id description}}
.edit-controls.clearfix .edit-controls.clearfix
button.primary(type="submit") {{_ 'edit'}} button.primary(type="submit") {{_ 'save'}}
a.fa.fa-times-thin.js-close-inlined-form a.fa.fa-times-thin.js-close-inlined-form
else else
a.js-open-inlined-form a.js-open-inlined-form
@ -47,6 +47,12 @@ template(name="cardDetails")
= description = description
else else
| {{_ 'edit'}} | {{_ 'edit'}}
if (hasUnsavedValue 'cardDescription' _id)
p.quiet
| You have an unsaved description.
a.js-open-inlined-form View it
= ' - '
a.js-close-inlined-form Discard
else if description else if description
h3.card-details-item-title Description h3.card-details-item-title Description
+viewer +viewer

View file

@ -1,7 +1,3 @@
// XXX Obviously this shouldn't be a global, but this is currently the only way
// to share a variable between two files.
BlazeComponent.extendComponent({ BlazeComponent.extendComponent({
template: function() { template: function() {
return 'cardDetails'; return 'cardDetails';
@ -80,6 +76,37 @@ BlazeComponent.extendComponent({
} }
}).register('cardDetails'); }).register('cardDetails');
// We extends the normal InlinedForm component to support UnsavedEdits draft
// feature.
(class extends InlinedForm {
_getUnsavedEditKey() {
return {
fieldName: 'cardDescription',
docId: Session.get('currentCard'),
}
}
close(isReset = false) {
if (this.isOpen.get() && ! isReset) {
UnsavedEdits.set(this._getUnsavedEditKey(), this.getValue());
}
super();
}
reset() {
UnsavedEdits.reset(this._getUnsavedEditKey());
this.close(true);
}
events() {
const parentEvents = InlinedForm.prototype.events()[0];
return [{
...parentEvents,
'click .js-close-inlined-form': this.reset,
}];
}
}).register('inlinedCardDescription');
Template.cardDetailsActionsPopup.events({ Template.cardDetailsActionsPopup.events({
'click .js-members': Popup.open('cardMembers'), 'click .js-members': Popup.open('cardMembers'),
'click .js-labels': Popup.open('cardLabels'), 'click .js-labels': Popup.open('cardLabels'),

View file

@ -1,22 +0,0 @@
var emptyValue = '';
Mixins.CachedValue = BlazeComponent.extendComponent({
onCreated: function() {
this._cachedValue = emptyValue;
},
setCache: function(value) {
this._cachedValue = value;
},
getCache: function(defaultValue) {
if (this._cachedValue === emptyValue)
return defaultValue || '';
else
return this._cachedValue;
},
resetCache: function() {
this.setCache('');
}
});

View file

@ -11,7 +11,7 @@ template(name="listHeader")
template(name="editListTitleForm") template(name="editListTitleForm")
.list-composer .list-composer
input.full-line(type="text" value=title autofocus) input.full-line(type="text" value="{{../trySomething}}" autofocus)
.edit-controls.clearfix .edit-controls.clearfix
button.primary.confirm(type="submit") {{_ 'save'}} button.primary.confirm(type="submit") {{_ 'save'}}
a.fa.fa-times-thin.js-close-inlined-form a.fa.fa-times-thin.js-close-inlined-form

View file

@ -37,7 +37,7 @@ FlowRouter.route('/b/:id/:slug', {
FlowRouter.route('/b/:boardId/:slug/:cardId', { FlowRouter.route('/b/:boardId/:slug/:cardId', {
name: 'card', name: 'card',
action: function(params) { action: function(params) {
EscapeActions.executeUpTo('popup-close'); EscapeActions.executeUpTo('inlinedForm');
Session.set('currentBoard', params.boardId); Session.set('currentBoard', params.boardId);
Session.set('currentCard', params.cardId); Session.set('currentCard', params.cardId);

View file

@ -17,10 +17,10 @@ EscapeActions = {
'inlinedForm', 'inlinedForm',
'detailsPane', 'detailsPane',
'multiselection', 'multiselection',
'sidebarView' 'sidebarView',
], ],
register: function(label, action, condition = () => true, options = {}) { register(label, action, condition = () => true, options = {}) {
const priority = this.hierarchy.indexOf(label); const priority = this.hierarchy.indexOf(label);
if (priority === -1) { if (priority === -1) {
throw Error('You must define the label in the EscapeActions hierarchy'); throw Error('You must define the label in the EscapeActions hierarchy');
@ -33,35 +33,35 @@ EscapeActions = {
let noClickEscapeOn = options.noClickEscapeOn; let noClickEscapeOn = options.noClickEscapeOn;
this._actions[priority] = { this._actions = _.sortBy([...this._actions, {
priority, priority,
condition, condition,
action, action,
noClickEscapeOn, noClickEscapeOn,
enabledOnClick enabledOnClick,
}; }], (action) => action.priority);
}, },
executeLowest: function() { executeLowest() {
return this._execute({ return this._execute({
multipleAction: false multipleAction: false
}); });
}, },
executeAll: function() { executeAll() {
return this._execute({ return this._execute({
multipleActions: true multipleActions: true
}); });
}, },
executeUpTo: function(maxLabel) { executeUpTo(maxLabel) {
return this._execute({ return this._execute({
maxLabel: maxLabel, maxLabel: maxLabel,
multipleActions: true multipleActions: true
}); });
}, },
clickExecute: function(target, maxLabel) { clickExecute(target, maxLabel) {
if (this._nextclickPrevented) { if (this._nextclickPrevented) {
this._nextclickPrevented = false; this._nextclickPrevented = false;
} else { } else {
@ -74,18 +74,18 @@ EscapeActions = {
} }
}, },
preventNextClick: function() { preventNextClick() {
this._nextclickPrevented = true; this._nextclickPrevented = true;
}, },
_stopClick: function(action, clickTarget) { _stopClick(action, clickTarget) {
if (! _.isString(action.noClickEscapeOn)) if (! _.isString(action.noClickEscapeOn))
return false; return false;
else else
return $(clickTarget).closest(action.noClickEscapeOn).length > 0; return $(clickTarget).closest(action.noClickEscapeOn).length > 0;
}, },
_execute: function(options) { _execute(options) {
const maxLabel = options.maxLabel; const maxLabel = options.maxLabel;
const multipleActions = options.multipleActions; const multipleActions = options.multipleActions;
const isClick = !! options.isClick; const isClick = !! options.isClick;
@ -99,8 +99,7 @@ EscapeActions = {
else else
maxPriority = this.hierarchy.indexOf(maxLabel); maxPriority = this.hierarchy.indexOf(maxLabel);
for (let i = 0; i < this._actions.length; i++) { for (let currentAction of this._actions) {
let currentAction = this._actions[i];
if (currentAction.priority > maxPriority) if (currentAction.priority > maxPriority)
return executedAtLeastOne; return executedAtLeastOne;

View file

@ -13,19 +13,13 @@
// // the content when the form is close (optional) // // the content when the form is close (optional)
// We can only have one inlined form element opened at a time // We can only have one inlined form element opened at a time
// XXX Could we avoid using a global here ? This is used in Mousetrap currentlyOpenedForm = new ReactiveVar(null);
// keyboard.js
var currentlyOpenedForm = new ReactiveVar(null);
BlazeComponent.extendComponent({ InlinedForm = BlazeComponent.extendComponent({
template: function() { template: function() {
return 'inlinedForm'; return 'inlinedForm';
}, },
mixins: function() {
return [Mixins.CachedValue];
},
onCreated: function() { onCreated: function() {
this.isOpen = new ReactiveVar(false); this.isOpen = new ReactiveVar(false);
}, },
@ -42,7 +36,6 @@ BlazeComponent.extendComponent({
}, },
close: function() { close: function() {
this.saveValue();
this.isOpen.set(false); this.isOpen.set(false);
currentlyOpenedForm.set(null); currentlyOpenedForm.set(null);
}, },
@ -52,10 +45,6 @@ BlazeComponent.extendComponent({
return this.isOpen.get() && input && input.value; return this.isOpen.get() && input && input.value;
}, },
saveValue: function() {
this.callFirstWith(this, 'setCache', this.getValue());
},
events: function() { events: function() {
return [{ return [{
'click .js-close-inlined-form': this.close, 'click .js-close-inlined-form': this.close,
@ -73,7 +62,6 @@ BlazeComponent.extendComponent({
if (this.currentData().autoclose !== false) { if (this.currentData().autoclose !== false) {
Tracker.afterFlush(() => { Tracker.afterFlush(() => {
this.close(); this.close();
this.callFirstWith(this, 'resetCache');
}); });
} }
} }

View file

@ -0,0 +1,82 @@
Meteor.subscribe('unsaved-edits');
// `UnsavedEdits` is a global key-value store used to save drafts of user
// inputs. We used to have the notion of a `cachedValue` that was local to a
// component but the global store has multiple advantages:
// 1. When the component is unmounted (ie, destroyed) the draft isn't lost
// 2. The drafts are synced across multiple computers
// 3. The drafts are synced across multiple browser tabs
// XXX This currently doesn't work in purely offline mode since the sync is
// handled with the DDP connection to the server. To solve this, we could use
// something like GroundDB that syncs using localstorage.
//
// The key is a dictionary composed of two fields:
// * a `fieldName` which identifies the particular field. Since this is a global
// identifier a good practice would be to compose it with the collection name
// and the document field, eg. `boardTitle`, `cardDescription`.
// * a `docId` which identifies the appropriate document. In general we use
// MongoDB `_id` field.
//
// The value is a string containing the draft.
UnsavedEdits = {
// XXX Wanted to have the collection has an instance variable, but
// unfortunately the collection isn't defined yet at this point. We need ES6
// modules to solve the file order issue!
//
// _collection: UnsavedEditCollection,
get({ fieldName, docId }, defaultTo = '') {
let unsavedValue = this._getCollectionDocument(fieldName, docId);
if (unsavedValue) {
return unsavedValue.value
} else {
return defaultTo;
}
},
has({ fieldName, docId }) {
return Boolean(this.get({fieldName, docId}));
},
set({ fieldName, docId }, value) {
let currentDoc = this._getCollectionDocument(fieldName, docId);
if (currentDoc) {
UnsavedEditCollection.update(currentDoc._id, {
$set: {
value: value
}
});
} else {
UnsavedEditCollection.insert({
fieldName,
docId,
value,
});
}
},
reset({ fieldName, docId }) {
let currentDoc = this._getCollectionDocument(fieldName, docId);
if (currentDoc) {
UnsavedEditCollection.remove(currentDoc._id);
}
},
_getCollectionDocument(fieldName, docId) {
return UnsavedEditCollection.findOne({fieldName, docId});
}
}
Blaze.registerHelper('getUnsavedValue', (fieldName, docId, defaultTo) => {
// Workaround some blaze feature that ass a list of keywords arguments as the
// last parameter (even if the caller didn't specify any).
if (! _.isString(defaultTo)) {
defaultTo = '';
}
return UnsavedEdits.get({ fieldName, docId }, defaultTo);
});
Blaze.registerHelper('hasUnsavedValue', (fieldName, docId) => {
return UnsavedEdits.has({ fieldName, docId });
});

View file

@ -0,0 +1,34 @@
// This collection shouldn't be manipulated directly by instead throw the
// `UnsavedEdits` API on the client.
UnsavedEditCollection = new Mongo.Collection('unsaved-edits');
UnsavedEditCollection.attachSchema(new SimpleSchema({
fieldName: {
type: String
},
docId: {
type: String
},
value: {
type: String
},
userId: {
type: String
},
}));
if (Meteor.isServer) {
function isAuthor(userId, doc, fieldNames = []) {
return userId === doc.userId && fieldNames.indexOf('userId') === -1;
}
UnsavedEditCollection.allow({
insert: isAuthor,
update: isAuthor,
remove: isAuthor,
fetch: ['userId']
});
}
UnsavedEditCollection.before.insert(function(userId, doc) {
doc.userId = userId;
});

View file

@ -0,0 +1,5 @@
Meteor.publish('unsaved-edits', function() {
return UnsavedEditCollection.find({
userId: this.userId
});
});