1 |
wakaba |
1.1 |
window.onload = function () { |
2 |
|
|
createToolbar (); |
3 |
|
|
}; // window.onload |
4 |
|
|
|
5 |
|
|
function getElementsByClassName (c) { |
6 |
|
|
if (document.getElementsByClassName) { |
7 |
|
|
return document.getElementsByClassName (c); |
8 |
|
|
} else { |
9 |
|
|
|
10 |
|
|
} |
11 |
|
|
} // getElementsByClassName |
12 |
|
|
|
13 |
|
|
function getAncestorElement (n, t) { |
14 |
|
|
while (n != null) { |
15 |
|
|
if (n.nodeName == t) { |
16 |
|
|
return n; |
17 |
|
|
} else { |
18 |
|
|
n = n.parentNode; |
19 |
|
|
} |
20 |
|
|
} |
21 |
|
|
return null; |
22 |
|
|
} // getAncestorElement |
23 |
|
|
|
24 |
|
|
function getNextAnchorNumber (s) { |
25 |
|
|
var lastId = 0; |
26 |
|
|
s.replace (/\[([0-9]+)\]/g, function (l, n) { |
27 |
|
|
var v = parseInt (n); |
28 |
|
|
if (v > lastId) { |
29 |
|
|
lastId = v; |
30 |
|
|
} |
31 |
|
|
}); |
32 |
|
|
return lastId + 1; |
33 |
|
|
} // getNextAnchorNumber |
34 |
|
|
|
35 |
|
|
function createToolbar () { |
36 |
|
|
var containers = getElementsByClassName ('text-toolbar'); |
37 |
|
|
for (var i = 0; i < containers.length; i++) { |
38 |
|
|
var container = containers[i]; |
39 |
|
|
|
40 |
|
|
var button = document.createElement ('button'); |
41 |
|
|
button.type = 'button'; |
42 |
|
|
button.innerHTML = '[#]'; |
43 |
|
|
button.onclick = function () { |
44 |
|
|
var form = getAncestorElement (container, 'FORM'); |
45 |
|
|
var ta = form.elements.text; |
46 |
|
|
var st = ta.scrollTop; |
47 |
|
|
var ss = ta.selectionStart; |
48 |
|
|
var se = (ss != ta.selectionEnd); |
49 |
|
|
var added = '[' + getNextAnchorNumber (ta.value) + '] '; |
50 |
|
|
/* |
51 |
|
|
if (ss > 0 && ta.value.substring (ss - 1, ss) != "\n") { |
52 |
|
|
added = "\n" + added; |
53 |
|
|
} |
54 |
|
|
*/ |
55 |
|
|
ta.value = ta.value.substring (0, ta.selectionStart) |
56 |
|
|
+ added + ta.value.substring (ta.selectionEnd); |
57 |
|
|
if (se) { |
58 |
|
|
ta.setSelectionRange (ss, ss + added.length); |
59 |
|
|
} else { |
60 |
|
|
ta.setSelectionRange (ss + added.length, ss + added.length); |
61 |
|
|
} |
62 |
|
|
ta.scrollTop = st; |
63 |
|
|
ta.focus (); |
64 |
|
|
}; // button.onclick |
65 |
|
|
container.appendChild (button); |
66 |
|
|
} |
67 |
|
|
} // createToolbar |