Commit e0a168ac authored by Nick Stenning's avatar Nick Stenning

Merge pull request #2884 from hypothesis/canceling-edits-loses-changes

Fix canceling edits reverting to the first-loaded version of an annotation
parents dbc860f9 3d9d84ff
...@@ -384,6 +384,7 @@ function AnnotationController( ...@@ -384,6 +384,7 @@ function AnnotationController(
function onAnnotationUpdated(event, updatedDomainModel) { function onAnnotationUpdated(event, updatedDomainModel) {
if (updatedDomainModel.id === domainModel.id) { if (updatedDomainModel.id === domainModel.id) {
domainModel = updatedDomainModel;
updateView(updatedDomainModel); updateView(updatedDomainModel);
} }
} }
...@@ -699,9 +700,9 @@ function AnnotationController( ...@@ -699,9 +700,9 @@ function AnnotationController(
} }
onFulfilled = function() { onFulfilled = function() {
drafts.remove(domainModel);
$rootScope.$emit('annotationUpdated', updatedModel); $rootScope.$emit('annotationUpdated', updatedModel);
view(); view();
drafts.remove(domainModel);
}; };
onRejected = function(reason) { onRejected = function(reason) {
flash.error( flash.error(
......
...@@ -1925,7 +1925,7 @@ describe('annotation', function() { ...@@ -1925,7 +1925,7 @@ describe('annotation', function() {
var controller = createAnnotationDirective({ var controller = createAnnotationDirective({
annotation: { annotation: {
id: 'test-annotation-id', id: 'test-annotation-id',
user: 'acct:bill@localhost' user: 'acct:bill@localhost',
} }
}).controller; }).controller;
controller.edit(); controller.edit();
...@@ -1934,5 +1934,33 @@ describe('annotation', function() { ...@@ -1934,5 +1934,33 @@ describe('annotation', function() {
controller.revert(); controller.revert();
assert.equal(controller.form.text, void 0); assert.equal(controller.form.text, void 0);
}); });
it('reverts to the most recently saved version when canceling changes',
function () {
var controller = createAnnotationDirective({
annotation: {
user: 'acct:bill@localhost',
$create: function () {
this.id = 'new-annotation-id';
return Promise.resolve();
},
$update: function () {
return Promise.resolve(this);
},
},
}).controller;
controller.edit();
controller.form.text = 'New annotation text';
return controller.save().then(function () {
controller.edit();
controller.form.text = 'Updated annotation text';
return controller.save();
}).then(function () {
controller.edit();
controller.revert();
assert.equal(controller.form.text, 'Updated annotation text');
});
});
}); });
}); });
...@@ -17,10 +17,13 @@ ...@@ -17,10 +17,13 @@
function DraftStore() { function DraftStore() {
this._drafts = []; this._drafts = [];
// returns true if 'draft' is a draft for a given /**
// annotation. Annotations are matched by ID * Returns true if 'draft' is a draft for a given
// and annotation instance (for unsaved annotations * annotation.
// which have no ID) *
* Annotations are matched by ID and annotation instance (for unsaved
* annotations which have no ID)
*/
function match(draft, model) { function match(draft, model) {
return draft.model === model || return draft.model === model ||
(draft.model.id && model.id === draft.model.id); (draft.model.id && model.id === draft.model.id);
...@@ -49,11 +52,12 @@ function DraftStore() { ...@@ -49,11 +52,12 @@ function DraftStore() {
/** Retrieve the draft changes for an annotation. */ /** Retrieve the draft changes for an annotation. */
this.get = function get(model) { this.get = function get(model) {
for (var i = 0; i < this._drafts.length; i++) { for (var i = 0; i < this._drafts.length; i++) {
if (match(this._drafts[i], model)) { var draft = this._drafts[i];
if (match(draft, model)) {
return { return {
isPrivate: this._drafts[i].isPrivate, isPrivate: draft.isPrivate,
tags: this._drafts[i].tags, tags: draft.tags,
text: this._drafts[i].text, text: draft.text,
}; };
} }
} }
...@@ -81,6 +85,7 @@ function DraftStore() { ...@@ -81,6 +85,7 @@ function DraftStore() {
}); });
}; };
/** Remove all drafts. */
this.discard = function discard() { this.discard = function discard() {
this._drafts = []; this._drafts = [];
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment