/
structural.html
88 lines (77 loc) · 2.2 KB
/
structural.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<html>
<head>
<script src="jquery.min.js"></script>
<script src="../structural.js"></script>
<style>
.eatom { color: red }
.selected { background: #fdd }
</style>
<script>
$(function() {
var container = elt('div');
$(document.body).append(container);
var update = function(z) {
container.empty();
container.append(elt('pre', {}, render_zipper(z)));
};
var parser = {
tokenizer: map_tokenizer(flatten, multi_tokenizer(regexp_tokenizer({
'\\d+': function(m) { return [Exp_eatom.make(m[0])] },
'\\s+': function(m) { return [] },
'\\+': function() { return [Exp_plus_token.make()] }
}))),
parser: rewrite_parser({
'eatom': function(eatom) { return Exp_eplus_single.make(eatom) },
'eplus plus_token eatom': function(eplus, plus_token, eatom) {
return Exp_eplus.make(eplus, plus_token, eatom);
}
})
};
var zipper = new Zipper([], Exp_unassembled(parser).make());
update(zipper);
var unfocus = function() {
if (typeof(zipper.expr) !== 'string' && zipper.expr.head.unfocus) {
zipper = zipper.expr.head.unfocus(zipper)
}
};
$(document.body).keydown(function(e) {
if (37 == e.which) { // left
unfocus();
update(zipper = zipper.left());
}
else if (38 == e.which) { // up
unfocus();
update(zipper = zipper.up());
}
else if (39 == e.which) { // right
unfocus();
update(zipper = zipper.right());
}
else if (40 == e.which) { // down
unfocus();
update(zipper = zipper.down(0));
}
else {
console.log(e.which, e.charCode);
}
});
$(document.body).keypress(function(e) {
if (typeof(zipper.expr) !== 'string' && zipper.expr.head.keypress) {
update(zipper = zipper.expr.head.keypress(String.fromCharCode(e.charCode), zipper));
}
else {
var cmd = String.fromCharCode(e.charCode);
if (cmd == 'a') {
update(zipper = new Zipper(zipper.contexts, Exp_unassembled(parser).make(zipper.expr)));
}
if (cmd == 'c') {
update(zipper = new Zipper(zipper.contexts, Exp_unassembled(parser).make()));
}
}
});
});
</script>
</head>
<body>
</body>
</html>