-
Notifications
You must be signed in to change notification settings - Fork 1
/
hammer.html
148 lines (129 loc) · 4.35 KB
/
hammer.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Collaborative editing with CodeMirror</title>
<link rel="stylesheet" href="codemirror/lib/codemirror.css" />
<script src="codemirror/lib/codemirror.js"></script>
<script src="https://cdn.firebase.com/v0/firebase.js"></script>
<script src="firepad.js"></script>
<style>
.CodeMirror pre {
font-family: Helvetica, sans-serif;
}
.cmrt-b { font-weight: bold; }
.cmrt-i { font-style: italic; }
.cmrt-u { text-decoration: underline; }
.cmrt-h1 { font-size: 200%; }
.cmrt-h2 { font-size: 150%; }
.cmrt-h3 { font-size: 125%; }
</style>
</head>
<body>
<h1>Firepad</h1>
<div id="editor-wrapper">
<a id="hammer-btn" class="btn" href="#" title="Hammer Time">Hammer Time!</a>
<span id="hammeringText" style="display: none;">Hammering!</span>
<script>
//Firebase.enableLogging(true);
var firepadRef = new Firebase('https://firepad.firebaseio-demo.com/hammer');
var editorWrapper = document.getElementById('editor-wrapper');
var cm = window.cm = CodeMirror(editorWrapper, {
lineNumbers: true,
lineWrapping: true,
mode: 'markdown'
});
var firepad = new Firepad(firepadRef, cm);
var richText = window.location.href.indexOf('#rich') >= 0;
var nextHammerOp;
var opNum = 0;
function doHammerOp() {
var lastLine = cm.lineCount() - 1;
var end = cm.indexFromPos({line: lastLine, ch: cm.getLine(lastLine).length});
var index = Math.floor(Math.random() * (end+1));
var pos = cm.posFromIndex(index);
var length = Math.floor(Math.random() * 7);
var length2 = Math.floor(Math.random() * 7);
cm.setCursor(cm.posFromIndex(index));
var op = randInt(0, 100);
if (op <= 40) { // insert
var string = randString(length, true);
if (richText) {
rtcm.insertText(index, string, randAttributes());
} else {
cm.replaceRange(string, pos);
}
cm.setCursor(cm.posFromIndex(index + length));
} else if (op <= 70) {
if (richText) {
rtcm.updateTextAttributes(index, Math.min(index + length, end), randAttributes(true));
} else {
var from = pos, to = cm.posFromIndex(index + length2);
var old = cm.getRange(from, to).replace(/\n/g, '\\n');
var string = randString(length, true);
cm.replaceRange(string, from, to);
}
cm.setCursor(cm.posFromIndex(index + length));
} else if (op <= 100) { // delete
var from = pos, to = cm.posFromIndex(index + length);
cm.replaceRange("", from, to);
}
nextHammerOp = setTimeout(doHammerOp, Math.random() * 500);
}
function hammer() {
if (nextHammerOp) {
clearTimeout(nextHammerOp);
nextHammerOp = null;
document.getElementById('hammeringText').style.cssText = "display: none;";
} else {
doHammerOp();
document.getElementById('hammeringText').style.cssText = "display: inline;";
}
}
function preventDefault (e) {
if (e.preventDefault) { e.preventDefault(); }
}
function stopPropagation (e) {
if (e.stopPropagation) { e.stopPropagation(); }
}
function stopEvent (e) {
preventDefault(e);
stopPropagation(e);
}
var hammerBtn = document.getElementById('hammer-btn');
hammerBtn.onclick = function(e) { hammer(); stopEvent(e); };
function randString(length, includeNewlines) {
var str = ""
for(var i = 0; i < length; i++) {
if (includeNewlines && Math.random()*10 < 1)
str += '\n';
else
str += String.fromCharCode(65 + Math.floor(Math.random() * 26));
}
return str;
}
function randAttributes(isRetain) {
var attrs = { };
if (Math.random() > 0.9) {
attrs['b'] = isRetain ? randBool() : true;
}
if (Math.random() > 0.9) {
attrs['u'] = isRetain ? randBool() : true;
}
if (Math.random() > 0.9) {
attrs['i'] = isRetain ? randBool() : true;
}
if (Math.random() > 0.9) {
attrs['h'] = isRetain ? (randInt(0, 3) || false) : randInt(1, 3);
}
return attrs;
}
function randBool() {
return Math.random() > 0.5;
}
function randInt(min, max) {
return min + Math.ceil(Math.random() * (max - min));
}
</script>
</body>
</html>