1
1
/**
2
2
* Build styles
3
3
*/
4
- import './index.css' ;
5
- import { IconInlineCode } from '@codexteam/icons'
4
+ import './index.css' ;
5
+ import { IconBrackets } from '@codexteam/icons' ;
6
+ import { API , InlineTool , InlineToolConstructorOptions , SanitizerConfig } from "@editorjs/editorjs" ;
7
+
8
+ interface IconClasses {
9
+ base : string ;
10
+ active : string ;
11
+ }
6
12
7
13
/**
8
14
* Inline Code Tool for the Editor.js
9
15
*
10
16
* Allows to wrap inline fragment and style it somehow.
11
17
*/
12
- export default class InlineCode {
18
+ export default class InlineCode implements InlineTool {
19
+ /**
20
+ * Editor.js API
21
+ */
22
+ private api : API ;
23
+ /**
24
+ * Button element for the toolbar
25
+ */
26
+ private button : HTMLButtonElement | null ;
27
+ /**
28
+ * Tag representing the term
29
+ */
30
+ private tag : string ;
31
+ /**
32
+ * CSS classes for the icon
33
+ */
34
+ private iconClasses : IconClasses ;
35
+
13
36
/**
14
37
* Class name for term-tag
15
38
*
16
39
* @type {string }
17
40
*/
18
- static get CSS ( ) {
41
+ static get CSS ( ) : string {
19
42
return 'inline-code' ;
20
- } ;
43
+ }
21
44
22
- /**
23
- */
24
- constructor ( { api} ) {
45
+ constructor ( { api } : InlineToolConstructorOptions ) {
25
46
this . api = api ;
26
47
27
- /**
28
- * Toolbar Button
29
- *
30
- * @type {HTMLElement|null }
31
- */
32
48
this . button = null ;
33
49
34
- /**
35
- * Tag represented the term
36
- *
37
- * @type {string }
38
- */
39
50
this . tag = 'CODE' ;
40
51
41
- /**
42
- * CSS classes
43
- */
44
52
this . iconClasses = {
45
53
base : this . api . styles . inlineToolButton ,
46
- active : this . api . styles . inlineToolButtonActive
54
+ active : this . api . styles . inlineToolButtonActive ,
47
55
} ;
48
56
}
49
57
@@ -52,7 +60,7 @@ export default class InlineCode {
52
60
*
53
61
* @return {boolean }
54
62
*/
55
- static get isInline ( ) {
63
+ static get isInline ( ) : boolean {
56
64
return true ;
57
65
}
58
66
@@ -61,7 +69,7 @@ export default class InlineCode {
61
69
*
62
70
* @return {HTMLElement }
63
71
*/
64
- render ( ) {
72
+ render ( ) : HTMLElement {
65
73
this . button = document . createElement ( 'button' ) ;
66
74
this . button . type = 'button' ;
67
75
this . button . classList . add ( this . iconClasses . base ) ;
@@ -75,12 +83,12 @@ export default class InlineCode {
75
83
*
76
84
* @param {Range } range - selected fragment
77
85
*/
78
- surround ( range ) {
86
+ surround ( range : Range ) : void {
79
87
if ( ! range ) {
80
88
return ;
81
89
}
82
90
83
- let termWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) ;
91
+ let termWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) as HTMLElement ;
84
92
85
93
/**
86
94
* If start or end of selection is in the highlighted block
@@ -97,7 +105,7 @@ export default class InlineCode {
97
105
*
98
106
* @param {Range } range - selected fragment
99
107
*/
100
- wrap ( range ) {
108
+ wrap ( range : Range ) : void {
101
109
/**
102
110
* Create a wrapper for highlighting
103
111
*/
@@ -125,21 +133,22 @@ export default class InlineCode {
125
133
*
126
134
* @param {HTMLElement } termWrapper - term wrapper tag
127
135
*/
128
- unwrap ( termWrapper ) {
136
+ unwrap ( termWrapper : HTMLElement ) : void {
129
137
/**
130
138
* Expand selection to all term-tag
131
139
*/
132
140
this . api . selection . expandToTag ( termWrapper ) ;
133
141
134
- let sel = window . getSelection ( ) ;
135
- let range = sel . getRangeAt ( 0 ) ;
142
+ const sel = window . getSelection ( ) ;
143
+ if ( ! sel ) return ;
136
144
137
- let unwrappedContent = range . extractContents ( ) ;
145
+ const range = sel . getRangeAt ( 0 ) ;
146
+ const unwrappedContent = range . extractContents ( ) ;
138
147
139
148
/**
140
149
* Remove empty term-tag
141
150
*/
142
- termWrapper . parentNode . removeChild ( termWrapper ) ;
151
+ termWrapper . parentNode ? .removeChild ( termWrapper ) ;
143
152
144
153
/**
145
154
* Insert extracted content
@@ -155,30 +164,37 @@ export default class InlineCode {
155
164
156
165
/**
157
166
* Check and change Term's state for current selection
167
+ *
168
+ * @return {boolean }
158
169
*/
159
- checkState ( ) {
170
+ checkState ( ) : boolean {
160
171
const termTag = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) ;
161
172
162
- this . button . classList . toggle ( this . iconClasses . active , ! ! termTag ) ;
173
+ if ( this . button ) {
174
+ this . button . classList . toggle ( this . iconClasses . active , ! ! termTag ) ;
175
+ }
176
+
177
+ return ! ! termTag ;
163
178
}
164
179
180
+
165
181
/**
166
182
* Get Tool icon's SVG
167
183
* @return {string }
168
184
*/
169
- get toolboxIcon ( ) {
170
- return IconInlineCode ;
185
+ get toolboxIcon ( ) : string {
186
+ return IconBrackets ;
171
187
}
172
188
173
189
/**
174
190
* Sanitizer rule
175
- * @return {{span: {class: string}} }
191
+ * @return {SanitizerConfig }
176
192
*/
177
- static get sanitize ( ) {
193
+ static get sanitize ( ) : SanitizerConfig {
178
194
return {
179
195
code : {
180
- class : InlineCode . CSS
181
- }
196
+ class : InlineCode . CSS ,
197
+ } ,
182
198
} ;
183
199
}
184
200
}
0 commit comments