forked from ehsan/editing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.html
63 lines (59 loc) · 1.8 KB
/
editor.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
<!doctype html>
<script src=implementation.js></script>
<style>
p, ol, ul, blockquote {
margin-top: 0;
margin-bottom: 0;
}
[contenteditable] { white-space: pre-wrap }
</style>
<title>Specification-based rich text editor</title>
<button onclick='myExecCommand("bold")'><b>B</b></button>
<button onclick='myExecCommand("italic")'><i>I</i></button>
<button onclick='myExecCommand("underline")'><u>U</u></button>
<button onclick='myExecCommand("strikethrough")'><s>S</s></button>
<button onclick='myExecCommand("subscript")'>X<sub>S</sub></button>
<button onclick='myExecCommand("superscript")'>X<sup>S</sup></button>
<div
contenteditable
style="border: inset 2px #ccc;min-height:20em"
>Hello</div>
<script>
document.querySelector("[contenteditable]").onkeypress = function(e) {
if (e.altKey || e.ctrlKey || e.charCode == 0) {
return true;
}
if (e.keyCode == 13 && !e.shiftKey) {
myExecCommand("insertparagraph");
} else if (e.keyCode == 13) {
myExecCommand("insertlinebreak");
} else {
myExecCommand("inserttext", false, String.fromCharCode(e.charCode));
}
return false;
}
document.onkeydown = function(e) {
if (e.keyCode == 8 || e.keyCode == 46) {
// Delete, backspace
return false;
}
// Bold/italic/underline.
if (e.ctrlKey
&& ["b", "u", "i"].indexOf(String.fromCharCode(e.keyCode).toLowerCase()) != -1) {
return false;
}
}
document.onkeyup = function(e) {
if (e.keyCode == 8) {
myExecCommand("delete");
} else if (e.keyCode == 46) {
myExecCommand("forwarddelete");
} else if (e.ctrlKey && String.fromCharCode(e.keyCode).toLowerCase() == "b") {
myExecCommand("bold");
} else if (e.ctrlKey && String.fromCharCode(e.keyCode).toLowerCase() == "i") {
myExecCommand("italic");
} else if (e.ctrlKey && String.fromCharCode(e.keyCode).toLowerCase() == "u") {
myExecCommand("underline");
}
}
</script>