-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (73 loc) · 1.96 KB
/
script.js
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
// http://dev.opera.com/articles/view/rich-html-editing-in-the-browser-part-2/
// http://dev.w3.org/html5/spec/Overview.html#editing-apis
//DONE:
// evaluation The Humane Environment-style
//TODO:
// Start with lambda calculus?
// Add nesting blocks on ({[ and such
// Use dl for binding names to expressions
// GUID for identifier declarations
// href and class? for references to enable refactoring inline expression
$(document).ready(function() {
c = $("#content");
c.bind('keydown', 'ctrl+space', auto_eval);
});
function auto_eval(e) {
var rng = window.getSelection().getRangeAt(0);
var cmd = commands[rng.toString()];
if (!!cmd) {
rng.deleteContents();
alert(cmd());
rng.insertNode(cmd());
} else {
var val = eval(rng.toString());
if (!!val)
replace(rng, window.document.createTextNode(val));
}
return false;
}
var commands = {
"class" : create_class_node
}
function create_class_node() {
var div = document.createElement('div');
div.innerHTML = '<h2>classname</h2><dl><dt>methodName</dt><dd>methodBody</dd></dl>';
return div;
}
//
// Random utils picked up around the net...
//
function isTextNode(node) {
return node.nodeType == 3;
}
function rightPart(node, ix) {
return node.splitText(ix);
}
function leftPart(node, ix) {
node.splitText(ix);
return node;
}
// overwrites the current selection with a node
function replace(rng, node) {
rng.deleteContents();
if (isTextNode(rng.startContainer)) {
var refNode = rightPart(rng.startContainer, rng.startOffset);
refNode.parentNode.insertBefore(node, refNode);
} else {
if (rng.startOffset == rng.startContainer.childNodes.length) {
refNode.parentNode.appendChild(node);
} else {
var refNode = rng.startContainer.childNodes[rng.startOffset];
refNode.parentNode.insertBefore(node, refNode);
}
}
}
if (!Array.prototype.map) {
Array.prototype.map = function(fun) {
var collect = [];
for ( var ix = 0; ix < this.length; ix++) {
collect[ix] = fun(this[ix]);
}
return collect;
}
}