Browse files

Selecting text works

It doesn't show up in the UI though and there's no use for it so I'm
using a pretty liberal definition of 'works'
  • Loading branch information...
1 parent 6c1b33b commit 9f736609f01b07ef60662848469ab5f34326399e @rhburrows committed Jul 4, 2011
Showing with 172 additions and 19 deletions.
  1. +7 −7 TODO.org
  2. +3 −6 src/core.js
  3. +1 −1 src/display.js
  4. +12 −1 src/editor.js
  5. +53 −0 src/extras/selection.js
  6. +1 −1 src/input.js
  7. +1 −1 test/displayTest.js
  8. +1 −1 test/editorTest.js
  9. +86 −0 test/extras/selectionTest.js
  10. +6 −0 test/index.html
  11. +1 −1 test/inputTest.js
View
14 TODO.org
@@ -1,17 +1,17 @@
This is a list of things todo to finish s2e for release
=======================================================
-* Selection
+* Selection <-- extras
* Line Wrapping (again)
-* Mouse Selection
-* Mouse Scrolling
-* Copy & Paste
+* Mouse Selection <-- (in selection)
+* Mouse Scrolling <-- extras?
+* Copy & Paste <-- extras
* Cursor Styling
* Background Styling
* Scrollbar Styling
-* syntax highlighting mode <-- contrib
-* html syntax highlighting <-- contrib
-* javascript syntax highlighting <-- contrib
+* syntax highlighting mode <-- extras
+* html syntax highlighting <-- extras
+* javascript syntax highlighting <-- extras
* Flush out the README
* Document the public API
* Write documentation generating tasks
View
9 src/core.js
@@ -3,9 +3,6 @@
var config = {
initialText : "",
keybindings : {},
- Display : null,
- InputManager : null,
- Editor : null,
keybindings : {}
};
@@ -23,9 +20,9 @@
var width = textarea.width();
var height = textarea.height();
- var editor = new $.fn.s2e.config.Editor(options.initialText);
- var display = new $.fn.s2e.config.Display(editor, width, height);
- var inputManager = new $.fn.s2e.config.InputManager();
+ var editor = new $.fn.s2e.Editor(options.initialText);
+ var display = new $.fn.s2e.Display(editor, width, height);
+ var inputManager = new $.fn.s2e.InputManager();
textarea.css({
position: 'absolute',
View
2 src/display.js
@@ -290,5 +290,5 @@
}
};
- $.fn.s2e.config.Display = Display;
+ $.fn.s2e.Display = Display;
})(jQuery);
View
13 src/editor.js
@@ -1,5 +1,7 @@
(function($){
+ var extraInitializers = [];
+
function Editor(initialText) {
this.buffer = new Array(50);
this.size = 50;
@@ -8,8 +10,17 @@
this.insertString(initialText);
movePoint(this, -initialText.length);
+
+ var edit = this;
+ $.each(extraInitializers, function(_, i){
+ i.call(edit);
+ });
}
+ Editor.addInit = function(init) {
+ extraInitializers.push(init);
+ };
+
function pointForward(e) {
if (e.postsize > 0) {
e.buffer[e.presize] = e.charAtPoint();
@@ -256,5 +267,5 @@
}
};
- $.fn.s2e.config.Editor = Editor;
+ $.fn.s2e.Editor = Editor;
})(jQuery);
View
53 src/extras/selection.js
@@ -0,0 +1,53 @@
+(function($){
+
+ $.fn.s2e.Editor.prototype.toggleSelection = function() {
+ if (this.selectionActive) {
+ this.selectionActive = false;
+ } else {
+ this.selectionBegan = this.pointPosition();
+ this.selectionActive = true;
+ }
+ };
+
+ $.fn.s2e.Editor.prototype.selectionStart = function() {
+ if (!this.selectionActive) {
+ return null;
+ }
+
+ if (this.selectionBegan < this.pointPosition()) {
+ return this.selectionBegan;
+ } else {
+ return this.pointPosition();
+ }
+ };
+
+ $.fn.s2e.Editor.prototype.selectionEnd = function() {
+ if (!this.selectionActive) {
+ return null;
+ }
+
+ if (this.selectionBegan > this.pointPosition()) {
+ return this.selectionBegan;
+ } else {
+ return this.pointPosition();
+ }
+ };
+
+ $.fn.s2e.Editor.prototype.selectedText = function() {
+ if (!this.selectionActive) {
+ return null;
+ }
+
+ var contents = this.contents();
+ if (this.selectionBegan < this.pointPosition()) {
+ return contents.slice(this.selectionBegan, this.pointPosition());
+ } else {
+ return contents.slice(this.pointPosition(), this.selectionBegan);
+ }
+ };
+
+ $.fn.s2e.Editor.addInit(function(){
+ this.selectionActive = false;
+ });
+
+})(jQuery);
View
2 src/input.js
@@ -141,5 +141,5 @@
}
};
- $.fn.s2e.config.InputManager = InputManager;
+ $.fn.s2e.InputManager = InputManager;
})(jQuery);
View
2 test/displayTest.js
@@ -1,6 +1,6 @@
module("display", {
setup: function() {
- display = new $.fn.s2e.config.Display(100, 100);
+ display = new $.fn.s2e.Display(100, 100);
// Stupid setup. Need to clean this up
// Make sure no lines wrap
View
2 test/editorTest.js
@@ -1,6 +1,6 @@
module("editor", {
setup: function() {
- editor = new $.fn.s2e.config.Editor("");
+ editor = new $.fn.s2e.Editor("");
}
});
View
86 test/extras/selectionTest.js
@@ -0,0 +1,86 @@
+module("extras/selection", {
+ setup: function() {
+ editor = $('#editor').s2e().get(0);
+ }
+});
+
+var editor;
+
+test("toggleSelection", function(){
+ equals(editor.selectionActive, false, "It starts inactive");
+
+ editor.toggleSelection();
+ equals(editor.selectionActive, true, "Toggling makes it active");
+
+ editor.toggleSelection();
+ equals(editor.selectionActive, false, "Toggling again makes it inactive");
+});
+
+test("selectionStart", function(){
+ equals(editor.selectionStart(), null,
+ "Its null when the selection isn't active");
+
+ editor.insertString("Testing selection");
+ editor.movePointTo(2);
+ editor.toggleSelection();
+
+ equals(editor.selectionStart(), 2,
+ "When the selection is active it marks where the selection began");
+
+ editor.movePoint(5);
+ equals(editor.selectionStart(), 2,
+ "If the point moves it is the earlier point");
+
+ editor.movePoint(-7);
+ equals(editor.selectionStart(), 0,
+ "It still works if the point moves before the starting point");
+
+ editor.toggleSelection();
+ equals(editor.selectionStart(), null,
+ "Its cleared when the selection is toggled again");
+});
+
+test("selectionEnd", function(){
+ equals(editor.selectionEnd(), null,
+ "Its null when the selection isn't active");
+
+ editor.insertString("Testing selection");
+ editor.movePointTo(2);
+ editor.toggleSelection();
+
+ equals(editor.selectionEnd(), 2,
+ "When the selection is active it marks where the selection ends");
+
+ editor.movePoint(5);
+ equals(editor.selectionEnd(), 7,
+ "If the point is moved the end matches the end of the selection");
+
+ editor.movePoint(-7);
+ equals(editor.selectionEnd(), 2,
+ "Its the later of the two points on the selection");
+
+ editor.toggleSelection();
+ equals(editor.selectionEnd(), null,
+ "Its cleared when the selection is toggled again");
+});
+
+test("selectedText", function(){
+ equals(editor.selectedText(), null,
+ "Its null when the selection isn't active");
+
+ editor.insertString("Testing selection");
+ editor.movePointTo(2);
+ editor.toggleSelection();
+
+ equals(editor.selectedText(), "", "Its begins as an empty string");
+
+ editor.movePoint(5);
+ equals(editor.selectedText(), "sting", "It returns the contained string");
+
+ editor.movePoint(-7);
+ equals(editor.selectedText(), "Te", "It returns the contained string");
+
+ editor.toggleSelection();
+ equals(editor.selectedText(), null,
+ "Its cleared when the selection is toggled again");
+});
View
6 test/index.html
@@ -14,11 +14,17 @@
<script type="text/javascript" src="../src/input.js"></script>
<script type="text/javascript" src="../src/defaults.js"></script>
+ <!-- extra modules -->
+ <script type="text/javascript" src="../src/extras/selection.js"></script>
+
<!-- Test Files -->
<script type="text/javascript" src="coreTest.js"></script>
<script type="text/javascript" src="editorTest.js"></script>
<script type="text/javascript" src="displayTest.js"></script>
<script type="text/javascript" src="inputTest.js"></script>
+
+ <!-- extra module tests -->
+ <script type="text/javascript" src="extras/selectionTest.js"></script>
</head>
<body>
<h1 id="qunit-header">QUnit Test Suite</h1>
View
2 test/inputTest.js
@@ -1,6 +1,6 @@
module("input", {
setup: function() {
- inputManager = new $.fn.s2e.config.InputManager();
+ inputManager = new $.fn.s2e.InputManager();
inputHandler = inputManager.handler();
}
});

0 comments on commit 9f73660

Please sign in to comment.