Skip to content

Commit

Permalink
Merge pull request #29 from widmogrod/feature/selection-test
Browse files Browse the repository at this point in the history
Refactoring of selection + test
  • Loading branch information
widmogrod authored Sep 17, 2017
2 parents 5809103 + 07cc8a5 commit 7ee3860
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 252 deletions.
75 changes: 44 additions & 31 deletions build/text/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/text/utils.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 44 additions & 31 deletions dist/crdt.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,45 +627,58 @@ function selectionUpdate(selection, op) {
return selection;
}
if (op instanceof insert_1.Insert) {
if (op.at < selection.at) {
return selection.moveRightBy(op.length);
// Don't move cursor when insert is done at the same position
if (selection.isCursor() && op.at === selection.at) {
return selection;
}
else if (op.at === selection.at) {
return selection.isCursor()
? selection
: selection.moveRightBy(op.length);
// is after selection:
// sssss
// iii
// iiiiii
if (op.at >= selection.endsAt) {
return selection;
}
else if (selection.isInside(op.at)) {
return selection.expandBy(op.length);
// is before selection or on the same position:
// sssss
// iii
// iiii
// iiiiii
if (op.at <= selection.at) {
return selection.moveRightBy(op.length);
}
return selection;
// is inside selection:
// sssss
// i
// iiii
return selection.expandBy(op.length);
}
if (op instanceof delete_1.Delete) {
if (op.at < selection.at) {
if (selection.isInside(op.endsAt)) {
return selection
.moveRightBy(op.at - selection.at)
.expandBy(selection.at - op.endsAt);
}
else if (op.endsAt < selection.at) {
return selection
.moveRightBy(-op.length);
}
else {
return selection
.moveRightBy(op.at - selection.at)
.expandBy(-selection.length);
}
// is before selection:
// ssssss
// ddd
if (op.endsAt < selection.at) {
return selection.moveRightBy(-op.length);
}
else if (op.at === selection.at) {
return selection
.expandBy(selection.at - op.endsAt);
// is after selection:
// ssssss
// ddddd
if (op.at > selection.endsAt) {
return selection;
}
else if (selection.isInside(op.at)) {
return selection
.expandBy(op.at - selection.endsAt);
// starts inside selection block:
// ssssss
// dddddddddd
// ddd
// ddd
// ddddddddd
if (op.at >= selection.at) {
return selection.expandBy(-Math.min(selection.endsAt - op.at, op.length));
}
return selection;
// ends inside selection:
// ssssss
// dddd
// dddddddd
return selection.expandBy(selection.at - op.endsAt).moveRightBy(op.at - selection.at);
}
return selection;
}
Expand Down
Loading

0 comments on commit 7ee3860

Please sign in to comment.