forked from codex-team/editor.js
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode.js
More file actions
71 lines (56 loc) · 1.31 KB
/
code.js
File metadata and controls
71 lines (56 loc) · 1.31 KB
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
/**
* Code Plugin\
* Creates code tag and adds content to this tag
*/
var code = (function (code_plugin) {
var baseClass = 'ce-code';
/**
* Make initial header block
* @param {object} JSON with block data
* @return {Element} element to append
*/
var make_ = function (data) {
var tag = codex.editor.draw.node('TEXTAREA', [ baseClass ], {});
if (data && data.text) {
tag.value = data.text;
}
return tag;
};
/**
* Escapes HTML chars
*
* @param {string} input
* @return {string} — escaped string
*/
var escapeHTML_ = function (input) {
var div = document.createElement('DIV'),
text = document.createTextNode(input);
div.appendChild(text);
return div.innerHTML;
};
/**
* Method to render HTML block from JSON
*/
code_plugin.render = function (data) {
return make_(data);
};
/**
* Method to extract JSON data from HTML block
*/
code_plugin.save = function (blockContent) {
var escaped = escapeHTML_(blockContent.value),
data = {
text : escaped
};
return data;
};
code_plugin.validate = function (data) {
if (data.text.trim() == '')
return;
return true;
};
code_plugin.destroy = function () {
code = null;
};
return code_plugin;
})({});