Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Keep selections active for a time

  • Loading branch information...
commit edb2d83b0bf9f43bf70f4c42744326a0b23c69fb 1 parent a43ce7c
@rhburrows authored
Showing with 43 additions and 7 deletions.
  1. +15 −4 src/extras/selection.js
  2. +28 −3 test/extras/selectionTest.js
View
19 src/extras/selection.js
@@ -37,15 +37,15 @@
this.display.faces = [];
$(this).unbind('s2e:movePoint s2e:contentsUpdate', setSelectionFace);
} else {
- this.selectionBegan = this.pointPosition();
this.selectionActive = true;
+ this.selectionBegan = this.pointPosition();
$(this).bind('s2e:movePoint s2e:contentsUpdate', setSelectionFace);
}
};
$.fn.s2e.Editor.prototype.selectionStart = function() {
- if (!this.selectionActive) {
+ if (this.selectionBegan == null) {
return null;
}
@@ -57,7 +57,7 @@
};
$.fn.s2e.Editor.prototype.selectionEnd = function() {
- if (!this.selectionActive) {
+ if (this.selectionBegan == null) {
return null;
}
@@ -69,7 +69,7 @@
};
$.fn.s2e.Editor.prototype.selectedText = function() {
- if (!this.selectionActive) {
+ if (this.selectionBegan == null) {
return null;
}
@@ -80,6 +80,10 @@
}
};
+ $.fn.s2e.Editor.prototype.clearSelection = function() {
+ this.selectionBegan = null;
+ };
+
function isSelected(editor, position) {
return position > editor.selectionStart() &&
position < editor.selectionEnd();
@@ -87,6 +91,13 @@
$.fn.s2e.Editor.addInit(function(){
this.selectionActive = false;
+
+ var e = this;
+ $(this).bind('s2e:movePoint s2e:contentsUpdate', function(){
+ if (!e.selectionActive) {
+ e.selectionBegan = null;
+ }
+ });
});
})(jQuery);
View
31 test/extras/selectionTest.js
@@ -36,8 +36,12 @@ test("selectionStart", function(){
"It still works if the point moves before the starting point");
editor.toggleSelection();
+ equals(editor.selectionStart(), 0,
+ "Its not cleared when the selection is toggled again");
+
+ editor.pointForward();
equals(editor.selectionStart(), null,
- "Its cleared when the selection is toggled again");
+ "Its cleared when the cursor is moved");
});
test("selectionEnd", function(){
@@ -60,8 +64,12 @@ test("selectionEnd", function(){
"Its the later of the two points on the selection");
editor.toggleSelection();
+ equals(editor.selectionEnd(), 2,
+ "Its stays the same when the selection is toggled");
+
+ editor.pointForward();
equals(editor.selectionEnd(), null,
- "Its cleared when the selection is toggled again");
+ "Its cleared when the cursor is moved");
});
test("selectedText", function(){
@@ -81,6 +89,23 @@ test("selectedText", function(){
equals(editor.selectedText(), "Te", "It returns the contained string");
editor.toggleSelection();
+ equals(editor.selectedText(), "Te", "It stays the same when toggled");
+
+ editor.pointForward();
equals(editor.selectedText(), null,
- "Its cleared when the selection is toggled again");
+ "Its cleared when the cursor is moved");
+});
+
+test("clearSelection", function(){
+ editor.insertString("Testing selection");
+ editor.movePointTo(2);
+ editor.toggleSelection();
+ editor.movePoint(5);
+
+ equals(editor.selectedText(), "sting", "Make sure the selection still works");
+
+ editor.clearSelection();
+ equals(editor.selectedText(), null, "It makes the selection null");
+ equals(editor.selectionStart(), null, "It clears the start point");
+ equals(editor.selectionEnd(), null, "It clears the end");
});
Please sign in to comment.
Something went wrong with that request. Please try again.