add selection.toJSON method
This commit is contained in:
parent
fa13f377e1
commit
7798de7bd3
3 changed files with 141 additions and 56 deletions
|
|
@ -171,6 +171,30 @@ module.exports = {
|
||||||
selection.addRange(range2);
|
selection.addRange(range2);
|
||||||
editor.execCommand('insertfoo');
|
editor.execCommand('insertfoo');
|
||||||
assert.equal('l1foo\nl2foo', editor.getValue());
|
assert.equal('l1foo\nl2foo', editor.getValue());
|
||||||
|
},
|
||||||
|
|
||||||
|
"test multiselect fromJSON/toJSON": function() {
|
||||||
|
var doc = new EditSession(["l1", "l2"]);
|
||||||
|
editor = new Editor(new MockRenderer(), doc);
|
||||||
|
MultiSelect(editor);
|
||||||
|
var selection = editor.selection;
|
||||||
|
|
||||||
|
var before = selection.toJSON();
|
||||||
|
|
||||||
|
var range1 = new Range(0,2,0,2);
|
||||||
|
var range2 = new Range(1,2,1,2);
|
||||||
|
selection.fromOrientedRange(range1)
|
||||||
|
selection.addRange(range2);
|
||||||
|
|
||||||
|
var after = selection.toJSON();
|
||||||
|
|
||||||
|
selection.fromJSON(before);
|
||||||
|
assert.ok(!selection.isEqual(after));
|
||||||
|
assert.ok(selection.isEqual(before));
|
||||||
|
|
||||||
|
selection.fromJSON(after);
|
||||||
|
assert.ok(!selection.isEqual(before));
|
||||||
|
assert.ok(selection.isEqual(after));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -861,6 +861,52 @@ var Selection = function(session) {
|
||||||
return range;
|
return range;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.toJSON = function() {
|
||||||
|
if (this.rangeCount) {
|
||||||
|
var data = this.ranges.map(function(r) {
|
||||||
|
var r1 = r.clone();
|
||||||
|
r1.isBackwards = r.cursor == r.start;
|
||||||
|
return r1;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
var data = this.getRange();
|
||||||
|
data.isBackwards = this.isBackwards();
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.fromJSON = function(data) {
|
||||||
|
if (data.start == undefined) {
|
||||||
|
if (this.toSingleRange) {
|
||||||
|
this.toSingleRange(data[0]);
|
||||||
|
for (var i = data.length; i--; ) {
|
||||||
|
var r = Range.fromPoints(data[i].start, data[i].end);
|
||||||
|
if (data.isBackwards)
|
||||||
|
r.cursor = r.start;
|
||||||
|
this.addRange(r, true);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else
|
||||||
|
data = data[0];
|
||||||
|
}
|
||||||
|
if (this.toSingleRange)
|
||||||
|
this.toSingleRange(data);
|
||||||
|
this.setSelectionRange(data, data.isBackwards);
|
||||||
|
};
|
||||||
|
|
||||||
|
this.isEqual = function(data) {
|
||||||
|
if ((data.length || this.rangeCount) && data.length != this.rangeCount)
|
||||||
|
return false;
|
||||||
|
if (!data.length || !this.ranges)
|
||||||
|
return this.getRange().isEqual(data);
|
||||||
|
|
||||||
|
for (var i = this.ranges.length; i--; ) {
|
||||||
|
if (!this.ranges[i].isEqual(data[i]))
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}).call(Selection.prototype);
|
}).call(Selection.prototype);
|
||||||
|
|
||||||
exports.Selection = Selection;
|
exports.Selection = Selection;
|
||||||
|
|
|
||||||
|
|
@ -434,9 +434,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
|
|
||||||
"test (keyboard navigation) when curLine is not EOL and targetLine is all whitespace new column should be current column": function() {
|
"test (keyboard navigation) when curLine is not EOL and targetLine is all whitespace new column should be current column": function() {
|
||||||
var session = new EditSession("function (a) {\n\
|
var session = new EditSession("function (a) {\n \n}");
|
||||||
\n\
|
|
||||||
}");
|
|
||||||
var selection = session.getSelection();
|
var selection = session.getSelection();
|
||||||
|
|
||||||
selection.moveCursorTo(2, 0);
|
selection.moveCursorTo(2, 0);
|
||||||
|
|
@ -445,16 +443,33 @@ module.exports = {
|
||||||
assert.position(selection.getCursor(), 1, 0);
|
assert.position(selection.getCursor(), 1, 0);
|
||||||
},
|
},
|
||||||
|
|
||||||
"test (keyboard navigation) when curLine is EOL and targetLine is shorter dan current column, new column should be targetLine's EOL": function() {
|
"test (keyboard navigation) when curLine is EOL and targetLine is shorter than current column, new column should be targetLine's EOL": function() {
|
||||||
var session = new EditSession("function (a) {\n\
|
var session = new EditSession("function (a) {\n \n}");
|
||||||
\n\
|
|
||||||
}");
|
|
||||||
var selection = session.getSelection();
|
var selection = session.getSelection();
|
||||||
|
|
||||||
selection.moveCursorTo(0, 14);
|
selection.moveCursorTo(0, 14);
|
||||||
selection.moveCursorDown();
|
selection.moveCursorDown();
|
||||||
|
|
||||||
assert.position(selection.getCursor(), 1, 4);
|
assert.position(selection.getCursor(), 1, 4);
|
||||||
|
},
|
||||||
|
|
||||||
|
"test fromJSON/toJSON": function() {
|
||||||
|
var session = new EditSession("function (a) {\n \n}");
|
||||||
|
var selection = session.getSelection();
|
||||||
|
|
||||||
|
selection.moveCursorTo(0, 14);
|
||||||
|
selection.moveCursorDown();
|
||||||
|
assert.position(selection.getCursor(), 1, 4);
|
||||||
|
var data = selection.toJSON();
|
||||||
|
data = JSON.parse(JSON.stringify(data))
|
||||||
|
selection.moveCursorDown();
|
||||||
|
assert.position(selection.getCursor(), 2, 1);
|
||||||
|
|
||||||
|
assert.ok(!selection.isEqual(data));
|
||||||
|
|
||||||
|
selection.fromJSON(data);
|
||||||
|
assert.position(selection.getCursor(), 1, 4);
|
||||||
|
assert.ok(selection.isEqual(data));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue