-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhello-etherpad.html
69 lines (58 loc) · 1.88 KB
/
hello-etherpad.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
<!DOCTYPE HTML>
<html>
<head>
<link href="ace/style.css" rel="stylesheet" type="text/css">
<title>Hello World</title>
</head>
<body>
<div id="header">
<div id="htext">
Editing <b>hello</b>
<button class="format-btn" data-attr="bold">Bold</button>
<button class="format-btn" data-attr="italic">Italic</button>
</div>
</div>
<div id="editor"></div>
<script src="/lib/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="/lib/ace/mode-latex.js" type="text/javascript" charset="utf-8"></script>
<script src="/channel/bcsocket.js"></script>
<script src="/share/AttributePool.js"></script>
<script src="/share/Changeset.js"></script>
<script src="/share/share.uncompressed.js"></script>
<script src="/share/ace.js"></script>
<script>
var editor = ace.edit("editor");
editor.getSession().setMode(new (require("ace/mode/latex").Mode));
ed = editor.getSession();
sharejs.open('hello', 'etherpad', function(error, doc) {
if (doc.created) {
doc.insert(0, "Hello \\latex world\nLine 2");
doc.setAttributes(0, 5, [["bold", true]]);
}
getOffsetPosition = function(range) {
var i, line, lines, offset, _i, _len;
lines = ed.getLines(0, range.row);
offset = 0;
for (i = _i = 0, _len = lines.length; _i < _len; i = ++_i) {
line = lines[i];
offset += i < range.row ? line.length : range.column;
}
return offset + range.row;
};
var buttons = document.getElementsByClassName("format-btn");
for(var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function (e) {
range = editor.getSelectionRange();
if (range.isEmpty())
return;
offset = getOffsetPosition(range.start);
offsetEnd = getOffsetPosition(range.end);
var btn = e.target;
doc.setAttributes(offset, offsetEnd-offset, [[btn.dataset.attr, true]]);
};
}
doc.attach_ace(editor);
});
</script>
</body>
</html>