-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
101 lines (80 loc) · 3.01 KB
/
index.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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Text editor</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript">
function view_text () {
// Find html elements.
var textArea = document.getElementById('my_text');
var div = document.getElementById('view_text');
// Put the text in a variable so we can manipulate it.
var text = textArea.value;
// Make sure html and php tags are unusable by disabling < and >.
text = text.replace(/\</gi, "<");
text = text.replace(/\>/gi, ">");
// Exchange newlines for <br />
text = text.replace(/\n/gi, "<br />");
// Basic BBCodes.
text = text.replace(/\[b\]/gi, "<b>");
text = text.replace(/\[\/b\]/gi, "</b>");
text = text.replace(/\[i\]/gi, "<i>");
text = text.replace(/\[\/i\]/gi, "</i>");
text = text.replace(/\[u\]/gi, "<u>");
text = text.replace(/\[\/u\]/gi, "</u>");
// Print the text in the div made for it.
div.innerHTML = text;
}
function mod_selection (val1,val2) {
// Get the text area
var textArea = document.getElementById('my_text');
// IE specific code.
if( -1 != navigator.userAgent.indexOf ("MSIE") ) {
var range = document.selection.createRange();
var stored_range = range.duplicate();
if(stored_range.length > 0) {
stored_range.moveToElementText(textArea);
stored_range.setEndPoint('EndToEnd', range);
textArea.selectionstart = stored_range.text.length - range.text.length;
textArea.selectionend = textArea.selectionstart + range.text.length;
}
}
var begin = 0;
var selection = 0;
var end = 0;
// Do we even have a selection?
if (typeof(textArea.selectionstart) != "undefined") {
// Split the text in three pieces - the selection, and what comes before and after.
begin = textArea.value.substr(0, textArea.selectionstart);
selection = textArea.value.substr(textArea.selectionstart, textArea.selectionend - textArea.selectionstart);
end = textArea.value.substr(textArea.selectionend);
}
else { // Just change the middle third
// Split the text in thirds
var third = Math.floor(textArea.value.length / 3);
begin = textArea.value.substr(0, third);
selection = textArea.value.substr(third, third * 2);
end = textArea.value.substr(third * 2);
}
// Insert the tags between the three pieces of text.
textArea.value = begin + val1 + selection + val2 + end;
}
</script>
</head>
<body>
<!-- Knapper -->
<input type="button" value="B" onclick="mod_selection('[b]','[/b]')" />
<input type="button" value="I" onclick="mod_selection('[i]','[/i]')" />
<input type="button" value="U" onclick="mod_selection('[u]','[/u]')" />
<br />
<!-- Text area -->
<textarea class="text_edit" id="my_text"></textarea>
<br />
<!-- Submit button -->
<input type="button" value="Show text" onclick="view_text()" />
<!-- Empty div to put the text in -->
<div id="view_text">
</div>
</body>
</html>