forked from jaandrle/CordovaTips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_SimpleEditor.html
69 lines (66 loc) · 2.74 KB
/
class_SimpleEditor.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple editor</title>
</head>
<body>
<p>
<button type="button" onclick="_editor.format('removeFormat')">Normal</button>
<button type="button" onclick="_editor.format('bold')">Bold</button>
<button type="button" onclick="_editor.format('italic')">Title</button>
<button type="button" onclick="_editor.format('email')">Email</button>
<button type="button" onclick="alert(_editor.getContent())">View output</button>
</p>
<iframe id="editor" frameborder="0" rows="7"></iframe>
<script>
var _editor= class_SimpleEditor({editor_element: document.getElementById("editor"), default_value: '<p class="a">Default</p>'});
function class_SimpleEditor(def){
let {editor_element, default_value}= def;
let _this= {};
var editor;
editor_element.contentWindow.location.reload();
editor_element.onload= function(){
editor= editor_element.contentDocument;
editor.body.innerHTML= default_value;
editor.designMode = "on";
};
function validateEmail(email) {
let re= /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
return re.test(email);
}
_this.format= function(action){
editor_element.contentWindow.focus();
let selected_value= editor.getSelection().toString();
let selected_value_correction= null;
//let warning_text= "Error!";
if(action==="email"){
action= "createLink";
if(editor.getSelection().anchorNode.parentNode.nodeName!=="A"){
if(!validateEmail(selected_value)){
selected_value= prompt("Value for email:", selected_value);
if(!validateEmail(selected_value)){
action= false;
//warning_text+= " Wrong email!"
} else {
selected_value_correction= "mailto:"+selected_value;
}
} else {
selected_value_correction= "mailto:"+selected_value;
}
} else {
action= "unlink";
}
}
if(action) editor.execCommand(action, false, selected_value_correction);
//else alert(warning_text);
}
_this.getContent= function(){
return editor.body.innerHTML.replace(/(<[^\/<> ]+) [^>]+(>|$)/g, "$1>");
//? jsete BRka?
}
return Object.freeze(_this);
}
</script>
</body>
</html>