Store/retrieve unsaved drafts by range when available

Previously, drafts were only based on line number and not patch range.
This adds range to the key in localstorage so that multiple drafts can
be stored for a line range. It also prevents the wrong draft from
surfacing in the incorrect context.

This change is also important for the multi-thread changes that are
currently in development as well, because it is possible to have
separate threads for these ranges, each of which can have its own draft
saved in storage.

Bug: Issue 3548
Change-Id: Id4c1beb5d73a47a1f98b0b169091905c80f8c64a
This commit is contained in:
Becky Siegel
2017-01-31 16:57:14 -08:00
parent 069b8e65b7
commit 64ce59f48a
3 changed files with 36 additions and 1 deletions

View File

@@ -188,6 +188,7 @@
patchNum: this.patchNum,
path: this.comment.path,
line: this.comment.line,
range: this.comment.range,
});
},
@@ -300,6 +301,7 @@
patchNum: this.patchNum,
path: this.comment.path,
line: this.comment.line,
range: this.comment.range,
};
if ((!this._messageText || !this._messageText.length) && oldValue) {
@@ -434,6 +436,7 @@
patchNum: patchNum,
path: comment.path,
line: comment.line,
range: comment.range,
});
if (draft) {

View File

@@ -57,8 +57,15 @@
},
_getDraftKey: function(location) {
return ['draft', location.changeNum, location.patchNum, location.path,
var range = location.range ? location.range.start_line + '-' +
location.range.start_character + '-' + location.range.end_character +
'-' + location.range.end_line : null;
var key = ['draft', location.changeNum, location.patchNum, location.path,
location.line || ''].join(':');
if (range) {
key = key + ':' + range;
}
return key;
},
_cleanupDrafts: function() {

View File

@@ -79,6 +79,7 @@ limitations under the License.
cleanupStorage();
});
test('automatically removes old drafts', function() {
var changeNum = 1234;
var patchNum = 5;
@@ -90,6 +91,7 @@ limitations under the License.
path: path,
line: line,
};
var key = element._getDraftKey(location);
// Make sure that the call to cleanup doesn't get throttled.
@@ -113,5 +115,28 @@ limitations under the License.
cleanupSpy.restore();
cleanupStorage();
});
test('_getDraftKey', function() {
var changeNum = 1234;
var patchNum = 5;
var path = 'my_source_file.js';
var line = 123;
var location = {
changeNum: changeNum,
patchNum: patchNum,
path: path,
line: line,
};
var expectedResult = 'draft:1234:5:my_source_file.js:123';
assert.equal(element._getDraftKey(location), expectedResult);
location.range = {
start_character: 1,
start_line: 1,
end_character: 1,
end_line: 2,
};
expectedResult = 'draft:1234:5:my_source_file.js:123:1-1-1-2';
assert.equal(element._getDraftKey(location), expectedResult);
});
});
</script>