Permalink
Browse files

Multi character input.

  • Loading branch information...
1 parent fabcaf3 commit 492efc5f6f05e055578baa1a8ce00c6b2af19827 @luqui committed Jun 15, 2011
Showing with 41 additions and 11 deletions.
  1. +28 −6 demo/structural.html
  2. +13 −5 structural.js
View
34 demo/structural.html
@@ -40,44 +40,66 @@
var container = elt('div');
$(document.body).append(container);
+var buffer = '';
+
var zipper = new Zipper([], Exp_eplus_box.make());
var update = function(z) {
if (!z) return;
zipper = z;
container.empty();
- container.append(elt('pre', {}, render_zipper(z)));
+ container.append(elt('pre', {}, render_zipper_with(z, function(t) {
+ return elt('span', {'class': 'selected'}, t, text_node(buffer))
+ })));
};
update(zipper);
$(document.body).keydown(function(e) {
var head = typeof(zipper.expr) === 'string' ? new EClass({}) : zipper.expr.head;
+
+ var navigate = function(dir) {
+ buffer = '';
+ update(head[dir].call(head, zipper));
+ };
+
if (37 == e.which) { // left
- update(head.nav_left(zipper));
+ navigate('nav_left');
}
else if (38 == e.which) { // up
- update(head.nav_up(zipper));
+ navigate('nav_up');
}
else if (39 == e.which) { // right
- update(head.nav_right(zipper));
+ navigate('nav_right');
}
else if (40 == e.which) { // down
- update(head.nav_down(zipper));
+ navigate('nav_down');
+ }
+ else if (8 == e.which) { // backspace
+ buffer = buffer.slice(0, buffer.length-1);
+ update(zipper);
+ return false; // prevent browser from handling it
}
else {
//console.log(e.which, e.charCode);
}
});
+
$(document.body).keypress(function(e) {
if (typeof(zipper.expr) !== 'string') {
var ch = String.fromCharCode(e.charCode);
- var tokresult = zipper.expr.head.parse(zipper.expr)(ch);
+ buffer += ch;
+ var tokresult = zipper.expr.head.parse(zipper.expr)(buffer);
if (tokresult) {
+ buffer = tokresult[1];
update(new Zipper(zipper.contexts, tokresult[0]));
}
+ else {
+ update(zipper);
+ }
}
+ return false;
});
});
View
18 structural.js
@@ -87,7 +87,7 @@ var regexp_tokenizer = function(tokens) {
// don't match the whole string in case we are in the middle of typing a token
// we use \0 to mean "eof" so this will pass.
- if (bestFunc && (bestMatch[0].length < str.length || true)) {
+ if (bestFunc && bestMatch[0].length < str.length) {
return [bestFunc(bestMatch), str.slice(bestMatch[0].length)];
}
else {
@@ -137,6 +137,7 @@ var trace = function() {
};
// End CodeCatalog Snippet
+// CodeCatalog Snippet http://www.codecatalog.net/389/2/
var splice_replace = function(e, replacements, xs) {
var r = [];
foreach(xs, function(x) {
@@ -149,6 +150,8 @@ var splice_replace = function(e, replacements, xs) {
});
return r;
};
+// End CodeCatalog Snippet
+
var Expr = object({
init: function(head, args) {
@@ -429,9 +432,14 @@ var render_context = function(contexts, hole) {
return r;
};
-var render_zipper = function(zipper) {
+var render_zipper_with = function(zipper, f) {
return render_context(
- zipper.contexts,
- elt('span', {'class': 'selected'},
- render_expr_tree(zipper.expr)));
+ zipper.contexts,
+ f(render_expr_tree(zipper.expr)));
+};
+
+var render_zipper = function(zipper) {
+ return render_zipper_with(zipper, function(t) {
+ return elt('span', {'class': 'selected'}, t);
+ });
};

0 comments on commit 492efc5

Please sign in to comment.