1
- /**
2
- * Build styles
3
- */
4
1
import './index.css' ;
5
2
import { getLineStartPosition } from './utils/string' ;
6
3
import { IconBrackets } from '@codexteam/icons' ;
7
-
4
+ import { API , BlockTool , BlockToolConstructorOptions , PasteEvent , SanitizerConfig } from '@editorjs/editorjs' ;
8
5
9
6
/**
10
7
* CodeTool for Editor.js
11
8
*
12
- * @author CodeX ([email protected] )
13
- * @copyright CodeX 2018
14
- * @license MIT
15
9
* @version 2.0.0
10
+ * @license MIT
16
11
*/
17
12
18
- /* global PasteEvent */
13
+ /**
14
+ * CodeTool generates data in this format
15
+ */
16
+ export interface CodeData {
17
+ code : string ;
18
+ }
19
+
20
+ /**
21
+ * CodeTool's config from User
22
+ */
23
+ export interface CodeConfig {
24
+ placeholder : string
25
+ }
26
+
27
+ interface CodeToolCSS {
28
+ /** Block Styling from Editor.js API */
29
+ baseClass : string ;
30
+ /** Input Styling from Editor.js API */
31
+ input : string ;
32
+ /** Wrapper styling */
33
+ wrapper : string ;
34
+ /** Textarea styling */
35
+ textarea : string ;
36
+ }
37
+
38
+ interface CodeToolNodes {
39
+ /** Main container or Wrapper for CodeTool */
40
+ holder : HTMLDivElement | null ;
41
+ /** Textarea where user inputs their code */
42
+ textarea : HTMLTextAreaElement | null ;
43
+ }
19
44
20
45
/**
21
46
* Code Tool for the Editor.js allows to include code examples in your articles.
22
47
*/
23
- export default class CodeTool {
48
+ export default class CodeTool implements BlockTool {
49
+ /**
50
+ * Editor.js API
51
+ */
52
+ private api : API ;
53
+ /**
54
+ * Read-only mode flag
55
+ */
56
+ private readOnly : boolean ;
57
+ /**
58
+ * CodeTool's placeholder
59
+ */
60
+ private placeholder : string ;
61
+ /**
62
+ * CodeTool's CSS
63
+ */
64
+ private CSS : CodeToolCSS ;
65
+ /**
66
+ * CodeTool nodes
67
+ */
68
+ private nodes : CodeToolNodes ;
69
+ /**
70
+ * CodeTool's data
71
+ */
72
+ private _data ! : CodeData ;
73
+
24
74
/**
25
75
* Notify core that read-only mode is supported
26
76
*
27
77
* @returns {boolean }
28
78
*/
29
- static get isReadOnlySupported ( ) {
79
+ static get isReadOnlySupported ( ) : boolean {
30
80
return true ;
31
81
}
32
82
@@ -36,7 +86,7 @@ export default class CodeTool {
36
86
* @returns {boolean }
37
87
* @public
38
88
*/
39
- static get enableLineBreaks ( ) {
89
+ static get enableLineBreaks ( ) : boolean {
40
90
return true ;
41
91
}
42
92
@@ -54,7 +104,7 @@ export default class CodeTool {
54
104
* @param {object } options.api - Editor.js API
55
105
* @param {boolean } options.readOnly - read only mode flag
56
106
*/
57
- constructor ( { data, config, api, readOnly } ) {
107
+ constructor ( { data, config, api, readOnly } : BlockToolConstructorOptions ) {
58
108
this . api = api ;
59
109
this . readOnly = readOnly ;
60
110
@@ -82,12 +132,12 @@ export default class CodeTool {
82
132
/**
83
133
* Create Tool's view
84
134
*
85
- * @returns {HTMLElement }
135
+ * @returns {HTMLDivElement }
86
136
* @private
87
137
*/
88
- drawView ( ) {
89
- const wrapper = document . createElement ( 'div' ) ,
90
- textarea = document . createElement ( 'textarea' ) ;
138
+ private drawView ( ) : HTMLDivElement {
139
+ const wrapper = document . createElement ( 'div' ) as HTMLDivElement ;
140
+ const textarea = document . createElement ( 'textarea' ) ;
91
141
92
142
wrapper . classList . add ( this . CSS . baseClass , this . CSS . wrapper ) ;
93
143
textarea . classList . add ( this . CSS . textarea , this . CSS . input ) ;
@@ -123,8 +173,8 @@ export default class CodeTool {
123
173
* @returns {HTMLDivElement } this.nodes.holder - Code's wrapper
124
174
* @public
125
175
*/
126
- render ( ) {
127
- return this . nodes . holder ;
176
+ public render ( ) : HTMLDivElement {
177
+ return this . nodes . holder ! ;
128
178
}
129
179
130
180
/**
@@ -134,9 +184,9 @@ export default class CodeTool {
134
184
* @returns {CodeData } - saved plugin code
135
185
* @public
136
186
*/
137
- save ( codeWrapper ) {
187
+ public save ( codeWrapper : HTMLDivElement ) : CodeData {
138
188
return {
139
- code : codeWrapper . querySelector ( 'textarea' ) . value ,
189
+ code : codeWrapper . querySelector ( 'textarea' ) ! . value ,
140
190
} ;
141
191
}
142
192
@@ -145,20 +195,23 @@ export default class CodeTool {
145
195
*
146
196
* @param {PasteEvent } event - event with pasted content
147
197
*/
148
- onPaste ( event ) {
149
- const content = event . detail . data ;
150
-
151
- this . data = {
152
- code : content . textContent ,
153
- } ;
198
+ public onPaste ( event : PasteEvent ) : void {
199
+ const detail = event . detail ;
200
+
201
+ if ( 'data' in detail ) {
202
+ const content = detail . data as string ;
203
+ this . data = {
204
+ code : content || '' ,
205
+ } ;
206
+ }
154
207
}
155
208
156
209
/**
157
210
* Returns Tool`s data from private property
158
211
*
159
212
* @returns {CodeData }
160
213
*/
161
- get data ( ) {
214
+ public get data ( ) : CodeData {
162
215
return this . _data ;
163
216
}
164
217
@@ -167,7 +220,7 @@ export default class CodeTool {
167
220
*
168
221
* @param {CodeData } data - saved tool data
169
222
*/
170
- set data ( data ) {
223
+ public set data ( data : CodeData ) {
171
224
this . _data = data ;
172
225
173
226
if ( this . nodes . textarea ) {
@@ -182,7 +235,7 @@ export default class CodeTool {
182
235
*
183
236
* @returns {{icon: string, title: string} }
184
237
*/
185
- static get toolbox ( ) {
238
+ static get toolbox ( ) : { icon : string ; title : string } {
186
239
return {
187
240
icon : IconBrackets ,
188
241
title : 'Code' ,
@@ -195,7 +248,7 @@ export default class CodeTool {
195
248
* @public
196
249
* @returns {string }
197
250
*/
198
- static get DEFAULT_PLACEHOLDER ( ) {
251
+ static get DEFAULT_PLACEHOLDER ( ) : string {
199
252
return 'Enter a code' ;
200
253
}
201
254
@@ -206,9 +259,9 @@ export default class CodeTool {
206
259
* @static
207
260
* @returns {{tags: string[]} }
208
261
*/
209
- static get pasteConfig ( ) {
262
+ static get pasteConfig ( ) : { tags : string [ ] } {
210
263
return {
211
- tags : [ 'pre' ] ,
264
+ tags : [ 'pre' ] ,
212
265
} ;
213
266
}
214
267
@@ -217,7 +270,7 @@ export default class CodeTool {
217
270
*
218
271
* @returns {{code: boolean} }
219
272
*/
220
- static get sanitize ( ) {
273
+ static get sanitize ( ) : SanitizerConfig {
221
274
return {
222
275
code : true , // Allow HTML tags
223
276
} ;
@@ -230,7 +283,7 @@ export default class CodeTool {
230
283
* @param {KeyboardEvent } event - keydown
231
284
* @returns {void }
232
285
*/
233
- tabHandler ( event ) {
286
+ private tabHandler ( event : KeyboardEvent ) : void {
234
287
/**
235
288
* Prevent editor.js tab handler
236
289
*/
@@ -241,7 +294,7 @@ export default class CodeTool {
241
294
*/
242
295
event . preventDefault ( ) ;
243
296
244
- const textarea = event . target ;
297
+ const textarea = event . target as HTMLTextAreaElement ;
245
298
const isShiftPressed = event . shiftKey ;
246
299
const caretPosition = textarea . selectionStart ;
247
300
const value = textarea . value ;
0 commit comments