Repository navigation
Expand file tree
/
Copy pathtext.js
More file actions
171 lines (149 loc) · 3.26 KB
/
Copy pathtext.js
File metadata and controls
171 lines (149 loc) · 3.26 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Base Text block for the CodeX Editor.
* Represents simple paragraph
*
* @author CodeX Team (team@ifmo.su)
* @copyright CodeX Team 2017
* @license The MIT License (MIT)
* @version 2.0.1
*/
/**
* @typedef {Object} TextData
* @description Tool's input and output data format
* @property {String} text — Paragraph's content. Can include HTML tags: <a><b><i>
*/
class Text {
/**
* Should this tools be displayed at the Editor's Toolbox
* @returns {boolean}
* @public
*/
static get displayInToolbox() {
return false;
}
/**
* Class for the Toolbox icon
* @returns {string}
* @public
*/
static get iconClassName() {
return 'cdx-text-icon';
}
/**
* Render plugin`s main Element and fill it with saved data
* @param {TextData} savedData — previously saved data
*/
constructor(savedData = {}) {
this._CSS = {
wrapper: 'ce-text'
};
this._data = {};
this._element = this.drawView();
this.data = savedData;
}
/**
* Create Tool's view
* @return {HTMLElement}
* @private
*/
drawView() {
let div = document.createElement('DIV');
div.classList.add(this._CSS.wrapper);
div.contentEditable = true;
return div;
}
/**
* Return Tool's view
* @returns {HTMLDivElement}
* @public
*/
render() {
return this._element;
}
/**
* Method that specified how to merge two Text blocks.
* Called by CodeX Editor by backspace at the beginning of the Block
* @param {TextData} data
* @public
*/
merge(data) {
let newData = {
text : this.data.text + data.text
};
this.data = newData;
}
/**
* Validate Text block data:
* - check for emptiness
*
* @param {TextData} savedData — data received after saving
* @returns {boolean} false if saved data is not correct, otherwise true
* @public
*/
validate(savedData) {
if (savedData.text.trim() === '') {
return false;
}
return true;
}
/**
* Extract Tool's data from the view
* @param {HTMLDivElement} toolsContent - Text tools rendered view
* @returns {TextData} - saved data
* @public
*/
save(toolsContent) {
/**
* @todo sanitize data
*/
return {
text: toolsContent.innerHTML
};
}
/**
* Get current Tools`s data
* @returns {TextData} Current data
* @private
*/
get data() {
let text = this._element.innerHTML;
this._data.text = text;
return this._data;
}
/**
* Store data in plugin:
* - at the this._data property
* - at the HTML
*
* @param {TextData} data — data to set
* @private
*/
set data(data) {
this._data = data || {};
this._element.innerHTML = this._data.text || '';
}
/**
* Handle pasted DIV and P tags.
*
* @private
* @param {HTMLElement} content - pasted element
* @returns {{text: string}}
*/
static onPasteHandler(content) {
return {
text: content.innerHTML
};
}
/**
* Used by Codex Editor paste handling API.
* Provides configuration to handle DIV and P tags.
*
* @returns {{handler: (function(HTMLElement): {text: string}), tags: string[]}}
*/
static get onPaste() {
return {
handler: Text.onPasteHandler,
tags: ['P', 'DIV']
};
}
}