1
+ /**
2
+ * import types
3
+ */
4
+ import type {
5
+ API ,
6
+ ToolboxConfig ,
7
+ BlockTool ,
8
+ ToolConfig ,
9
+ BlockToolData ,
10
+ } from '@editorjs/editorjs' ;
11
+
1
12
/**
2
13
* Import Tool's icon
3
14
*/
@@ -8,38 +19,70 @@ import { IconWarning } from '@codexteam/icons';
8
19
*/
9
20
import './index.css' ;
10
21
22
+ interface WarningCSS {
23
+ baseClass : string ;
24
+ wrapper : string ;
25
+ title : string ;
26
+ input : string ;
27
+ message : string ;
28
+ }
29
+
11
30
/**
12
- * @class Warning
13
- * @classdesc Warning Tool for Editor.js
14
- * @property {WarningData } data - Warning Tool`s input and output data
15
- * @property {object } api - Editor.js API instance
16
- *
17
31
* @typedef {object } WarningData
18
32
* @description Warning Tool`s input and output data
19
33
* @property {string } title - warning`s title
20
34
* @property {string } message - warning`s message
21
- *
35
+ */
36
+ export interface WarningData extends BlockToolData {
37
+ title : string ;
38
+ message : string ;
39
+ }
40
+
41
+ /**
22
42
* @typedef {object } WarningConfig
23
43
* @description Warning Tool`s initial configuration
24
44
* @property {string } titlePlaceholder - placeholder to show in warning`s title input
25
45
* @property {string } messagePlaceholder - placeholder to show in warning`s message input
26
46
*/
27
- export default class Warning {
47
+ export interface WarningConfig extends ToolConfig {
48
+ titlePlaceholder ?: string ;
49
+ messagePlaceholder ?: string ;
50
+ }
51
+
52
+ interface WarningConstructorArgs {
53
+ data : WarningData ;
54
+ config ?: WarningConfig ;
55
+ api : API ;
56
+ readOnly : boolean ;
57
+ }
58
+
59
+ /**
60
+ * @class Warning
61
+ * @classdesc Warning Tool for Editor.js
62
+ * @property {WarningData } data - Warning Tool`s input and output data
63
+ * @property {API } api - Editor.js API instance
64
+ */
65
+ export default class Warning implements BlockTool {
66
+ private api : API ;
67
+ private readOnly : boolean ;
68
+ private data : WarningData ;
69
+ titlePlaceholder : string ;
70
+ messagePlaceholder : string ;
28
71
29
72
/**
30
73
* Notify core that read-only mode is supported
31
74
*/
32
- static get isReadOnlySupported ( ) {
75
+ static get isReadOnlySupported ( ) : boolean {
33
76
return true ;
34
77
}
35
78
36
79
/**
37
80
* Get Toolbox settings
38
81
*
39
82
* @public
40
- * @returns {string }
83
+ * @returns {ToolboxConfig }
41
84
*/
42
- static get toolbox ( ) {
85
+ static get toolbox ( ) : ToolboxConfig {
43
86
return {
44
87
icon : IconWarning ,
45
88
title : 'Warning' ,
@@ -52,7 +95,7 @@ export default class Warning {
52
95
* @public
53
96
* @returns {boolean }
54
97
*/
55
- static get enableLineBreaks ( ) {
98
+ static get enableLineBreaks ( ) : boolean {
56
99
return true ;
57
100
}
58
101
@@ -62,7 +105,7 @@ export default class Warning {
62
105
* @public
63
106
* @returns {string }
64
107
*/
65
- static get DEFAULT_TITLE_PLACEHOLDER ( ) {
108
+ static get DEFAULT_TITLE_PLACEHOLDER ( ) : string {
66
109
return 'Title' ;
67
110
}
68
111
@@ -72,16 +115,16 @@ export default class Warning {
72
115
* @public
73
116
* @returns {string }
74
117
*/
75
- static get DEFAULT_MESSAGE_PLACEHOLDER ( ) {
118
+ static get DEFAULT_MESSAGE_PLACEHOLDER ( ) : string {
76
119
return 'Message' ;
77
120
}
78
121
79
122
/**
80
123
* Warning Tool`s styles
81
124
*
82
- * @returns {object }
125
+ * @returns {WarningCSS }
83
126
*/
84
- get CSS ( ) {
127
+ get CSS ( ) : WarningCSS {
85
128
return {
86
129
baseClass : this . api . styles . block ,
87
130
wrapper : 'cdx-warning' ,
@@ -94,17 +137,20 @@ export default class Warning {
94
137
/**
95
138
* Render plugin`s main Element and fill it with saved data
96
139
*
97
- * @param {WarningData } data — previously saved data
98
- * @param {WarningConfig } config — user config for Tool
99
- * @param {object } api - Editor.js API
100
- * @param {boolean } readOnly - read-only mode flag
140
+ * @param {object } params — constructor params
141
+ * @param {WarningData } params.data — previously saved data
142
+ * @param {WarningConfig } params.config — user config for Tool
143
+ * @param {API } params.api - Editor.js API
144
+ * @param {boolean } params.readOnly - read-only mode flag
101
145
*/
102
- constructor ( { data, config, api, readOnly } ) {
146
+ constructor ( { data, config, api, readOnly } : WarningConstructorArgs ) {
103
147
this . api = api ;
104
148
this . readOnly = readOnly ;
105
149
106
- this . titlePlaceholder = config . titlePlaceholder || Warning . DEFAULT_TITLE_PLACEHOLDER ;
107
- this . messagePlaceholder = config . messagePlaceholder || Warning . DEFAULT_MESSAGE_PLACEHOLDER ;
150
+ this . titlePlaceholder =
151
+ config ?. titlePlaceholder || Warning . DEFAULT_TITLE_PLACEHOLDER ;
152
+ this . messagePlaceholder =
153
+ config ?. messagePlaceholder || Warning . DEFAULT_MESSAGE_PLACEHOLDER ;
108
154
109
155
this . data = {
110
156
title : data . title || '' ,
@@ -117,7 +163,7 @@ export default class Warning {
117
163
*
118
164
* @returns {Element }
119
165
*/
120
- render ( ) {
166
+ render ( ) : HTMLElement {
121
167
const container = this . _make ( 'div' , [ this . CSS . baseClass , this . CSS . wrapper ] ) ;
122
168
const title = this . _make ( 'div' , [ this . CSS . input , this . CSS . title ] , {
123
169
contentEditable : ! this . readOnly ,
@@ -143,13 +189,13 @@ export default class Warning {
143
189
* @param {HTMLDivElement } warningElement - element to save
144
190
* @returns {WarningData }
145
191
*/
146
- save ( warningElement ) {
192
+ save ( warningElement : HTMLDivElement ) : WarningData {
147
193
const title = warningElement . querySelector ( `.${ this . CSS . title } ` ) ;
148
194
const message = warningElement . querySelector ( `.${ this . CSS . message } ` ) ;
149
195
150
196
return Object . assign ( this . data , {
151
- title : title . innerHTML ,
152
- message : message . innerHTML ,
197
+ title : title ? .innerHTML ?? '' ,
198
+ message : message ? .innerHTML ?? '' ,
153
199
} ) ;
154
200
}
155
201
@@ -161,7 +207,11 @@ export default class Warning {
161
207
* @param {object } attributes - any attributes
162
208
* @returns {Element }
163
209
*/
164
- _make ( tagName , classNames = null , attributes = { } ) {
210
+ private _make < K extends keyof HTMLElementTagNameMap > (
211
+ tagName : K ,
212
+ classNames : string | string [ ] | null = null ,
213
+ attributes : { [ key : string ] : any } = { }
214
+ ) : HTMLElementTagNameMap [ K ] {
165
215
const el = document . createElement ( tagName ) ;
166
216
167
217
if ( Array . isArray ( classNames ) ) {
@@ -171,7 +221,7 @@ export default class Warning {
171
221
}
172
222
173
223
for ( const attrName in attributes ) {
174
- el [ attrName ] = attributes [ attrName ] ;
224
+ ( el as any ) [ attrName ] = attributes [ attrName ] ;
175
225
}
176
226
177
227
return el ;
@@ -180,7 +230,6 @@ export default class Warning {
180
230
/**
181
231
* Sanitizer config for Warning Tool saved data
182
232
*
183
- * @returns {object }
184
233
*/
185
234
static get sanitize ( ) {
186
235
return {
0 commit comments