Permalink
Browse files

Make the face setting stuff a little better

Setting faces now works without having to clear out the internal face
variable manually. It is also much more flexible.
  • Loading branch information...
1 parent 5d76068 commit 12bf681f2aa0866c72175501e77a77c5435b4dae @rhburrows committed Jul 14, 2011
Showing with 122 additions and 16 deletions.
  1. +53 −6 src/display.js
  2. +13 −8 src/extras/selection.js
  3. +55 −1 test/displayTest.js
  4. +1 −1 test/editorTest.js
View
59 src/display.js
@@ -211,17 +211,62 @@
},
faceForPosition : function(position) {
- for (var i=0; i<this.faces.length; i++) {
- var faceApplication = this.faces[i];
- if (faceApplication[0] <= position && position <= faceApplication[1]) {
- return faceApplication[2];
+ for (var i = 0; i < this.faces.length; i++) {
+ var face = this.faces[i];
+ if (face[0] <= position && position <= face[1]) {
+ return face[2];
}
}
return this.defaultFace;
},
setFace : function(from, to, face) {
- this.faces.push([from, to, face]);
+ var updatedFaces = [];
+ for (var i = 0; i < this.faces.length; i++) {
+ var currentFace = this.faces[i];
+ if (from <= currentFace[0]) {
+ updatedFaces[updatedFaces.length] = [from, to, face];
+
+ if (to < currentFace[1]) {
+ updatedFaces[updatedFaces.length] = [to + 1, currentFace[1], currentFace[2]];
+ }
+ } else if (from < currentFace[1]) {
+ updatedFaces[updatedFaces.length] = [currentFace[0], from - 1, currentFace[2]];
+ updatedFaces[updatedFaces.length] = [from, to, face];
+
+ if (to < currentFace[1]) {
+ updatedFaces[updatedFaces.length] = [to + 1, currentFace[1], currentFace[2]];
+ }
+ } else {
+ updatedFaces[updatedFaces.length] = currentFace;
+ }
+ }
+
+ if (updatedFaces.length == 0) {
+ updatedFaces[0] = [from, to, face];
+ }
+ this.faces = updatedFaces;
+ },
+
+ clearFace : function(from, to) {
+ var updatedFaces = [];
+ for (var i = 0; i < this.faces.length; i++) {
+ var face = this.faces[i];
+ if (from < face[0]) {
+ if (to < face[1]) {
+ updatedFaces[updatedFaces.length] = [to, face[1], face[2]];
+ } else if (to < face[0]) {
+ updatedFaces[updatedFaces.length] = face;
+ }
+ } else if (from < face[1]) {
+ updatedFaces[updatedFaces.length] = [face[0], from - 1, face[2]];
+
+ if (to < face[1]) {
+ updatedFaces[updatedFaces.length] = [to + 1, face[1], face[2]];
+ }
+ }
+ }
+ this.faces = updatedFaces;
},
insertAfter : function(e) {
@@ -249,6 +294,8 @@
this.color = options['color'] || 'black';
}
+ Display.Face = Face;
+
Face.prototype = {
fontString : function() {
return this.style + ' ' +
@@ -298,4 +345,4 @@
];
$.fn.s2e.Display = Display;
-})(jQuery);
+})(jQuery);
View
21 src/extras/selection.js
@@ -21,11 +21,16 @@
}
};
+ var lastSelectStart, lastSelectEnd;
+
function setSelectionFace(editor) {
- editor.display.faces = [];
- editor.display.setFace(editor.selectionStart(),
- editor.selectionEnd()-1,
- selectedFace);
+ if (lastSelectStart && lastSelectEnd) {
+ editor.display.clearFace(lastSelectStart, lastSelectEnd);
+ }
+
+ lastSelectStart = editor.selectionStart();
+ lastSelectEnd = editor.selectionEnd() - 1;
+ editor.display.setFace(lastSelectStart, lastSelectEnd, selectedFace);
}
$.fn.s2e.Editor.prototype.toggleSelection = function() {
@@ -76,6 +81,9 @@
};
$.fn.s2e.Editor.prototype.clearSelection = function() {
+ if (this.display) {
+ this.display.clearFace(this.selectionStart(), this.selectionEnd()-1);
+ }
this.selectionBegan = null;
};
@@ -90,10 +98,7 @@
var e = this;
$(e).bind('s2e:movePoint s2e:contentsUpdate', function(){
if (!e.selectionActive) {
- if (e.display) {
- e.display.faces = [];
- }
- e.selectionBegan = null;
+ e.clearSelection();
} else {
setSelectionFace(e);
}
View
56 test/displayTest.js
@@ -2,7 +2,7 @@ module("display", {
setup: function() {
display = new $.fn.s2e.Display(100, 100);
- // Stupid setup. Need to clean this up
+ // stupid setup. Need to clean this up
// Make sure no lines wrap
display.lineLengths = [10, 10, 10, 10, 10, 10];
}
@@ -81,3 +81,57 @@ test('s2e:mouseup event', function(){
$(display.canvas).trigger(mousemove);
});
+
+test('faces', function(){
+ var face = new $.fn.s2e.Display.Face({});
+ var defaultFace = new $.fn.s2e.Display.Face({});
+ display.defaultFace = defaultFace;
+
+ display.setFace(5, 10, face);
+
+ equals(display.faceForPosition(0), defaultFace,
+ "It returns the default if the face hasn't been set");
+ equals(display.faceForPosition(4), defaultFace,
+ "Outside the boundaries don't count as the set face");
+ equals(display.faceForPosition(5), face,
+ "It counts as the face at the first boundary");
+ equals(display.faceForPosition(7), face,
+ "It counts as the face in the middle");
+ equals(display.faceForPosition(10), face,
+ "It counts as the face at the second boundary");
+
+ var newFace = new $.fn.s2e.Display.Face({});
+ display.setFace(6, 8, newFace);
+ equals(display.faceForPosition(5), face,
+ "It doesn't override the old set face outside the boundaries");
+ equals(display.faceForPosition(6), newFace,
+ "It counts as the face at the first boundary");
+ equals(display.faceForPosition(7), newFace,
+ "It counts as the face in the middle");
+ equals(display.faceForPosition(8), newFace,
+ "It counts as the face at the second boundary");
+ equals(display.faceForPosition(15), defaultFace,
+ "It's still the default after the set faces");
+});
+
+test('clearFace', function(){
+ var face = new $.fn.s2e.Display.Face({});
+ var defaultFace = new $.fn.s2e.Display.Face({});
+ display.defaultFace = defaultFace;
+
+ display.setFace(5, 10, face);
+
+ equals(display.faceForPosition(7), face,
+ "It set the face properly");
+
+ display.clearFace(7, 9);
+
+ equals(display.faceForPosition(6), face,
+ "It doesn't clear before the starting point");
+ equals(display.faceForPosition(7), defaultFace,
+ "It sets from the starting point back to deafult");
+ equals(display.faceForPosition(9), defaultFace,
+ "It sets at the ending point back to deafult");
+ equals(display.faceForPosition(10), face,
+ "It doesn't clear after the ending point");
+});
View
2 test/editorTest.js
@@ -1,6 +1,6 @@
module("editor", {
setup: function() {
- display = {};
+ display = { clearFace : function(){} };
editor = new $.fn.s2e.Editor("", display);
}
});

0 comments on commit 12bf681

Please sign in to comment.