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 = 'CODE' ;
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
- this . tag = 'CODE' ;
40
-
41
- /**
42
- * CSS classes
43
- */
44
50
this . iconClasses = {
45
51
base : this . api . styles . inlineToolButton ,
46
- active : this . api . styles . inlineToolButtonActive
52
+ active : this . api . styles . inlineToolButtonActive ,
47
53
} ;
48
54
}
49
55
@@ -52,7 +58,7 @@ export default class InlineCode {
52
58
*
53
59
* @return {boolean }
54
60
*/
55
- static get isInline ( ) {
61
+ static get isInline ( ) : boolean {
56
62
return true ;
57
63
}
58
64
@@ -61,7 +67,7 @@ export default class InlineCode {
61
67
*
62
68
* @return {HTMLElement }
63
69
*/
64
- render ( ) {
70
+ render ( ) : HTMLElement {
65
71
this . button = document . createElement ( 'button' ) ;
66
72
this . button . type = 'button' ;
67
73
this . button . classList . add ( this . iconClasses . base ) ;
@@ -75,12 +81,12 @@ export default class InlineCode {
75
81
*
76
82
* @param {Range } range - selected fragment
77
83
*/
78
- surround ( range ) {
84
+ surround ( range : Range ) : void {
79
85
if ( ! range ) {
80
86
return ;
81
87
}
82
88
83
- let termWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) ;
89
+ let termWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) as HTMLElement ;
84
90
85
91
/**
86
92
* If start or end of selection is in the highlighted block
@@ -97,7 +103,7 @@ export default class InlineCode {
97
103
*
98
104
* @param {Range } range - selected fragment
99
105
*/
100
- wrap ( range ) {
106
+ wrap ( range : Range ) : void {
101
107
/**
102
108
* Create a wrapper for highlighting
103
109
*/
@@ -125,21 +131,22 @@ export default class InlineCode {
125
131
*
126
132
* @param {HTMLElement } termWrapper - term wrapper tag
127
133
*/
128
- unwrap ( termWrapper ) {
134
+ unwrap ( termWrapper : HTMLElement ) : void {
129
135
/**
130
136
* Expand selection to all term-tag
131
137
*/
132
138
this . api . selection . expandToTag ( termWrapper ) ;
133
139
134
- let sel = window . getSelection ( ) ;
135
- let range = sel . getRangeAt ( 0 ) ;
140
+ const sel = window . getSelection ( ) ;
141
+ if ( ! sel ) return ;
136
142
137
- let unwrappedContent = range . extractContents ( ) ;
143
+ const range = sel . getRangeAt ( 0 ) ;
144
+ const unwrappedContent = range . extractContents ( ) ;
138
145
139
146
/**
140
147
* Remove empty term-tag
141
148
*/
142
- termWrapper . parentNode . removeChild ( termWrapper ) ;
149
+ termWrapper . parentNode ? .removeChild ( termWrapper ) ;
143
150
144
151
/**
145
152
* Insert extracted content
@@ -155,30 +162,37 @@ export default class InlineCode {
155
162
156
163
/**
157
164
* Check and change Term's state for current selection
165
+ *
166
+ * @return {boolean }
158
167
*/
159
- checkState ( ) {
168
+ checkState ( ) : boolean {
160
169
const termTag = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) ;
161
170
162
- this . button . classList . toggle ( this . iconClasses . active , ! ! termTag ) ;
171
+ if ( this . button ) {
172
+ this . button . classList . toggle ( this . iconClasses . active , ! ! termTag ) ;
173
+ }
174
+
175
+ return ! ! termTag ;
163
176
}
164
177
178
+
165
179
/**
166
180
* Get Tool icon's SVG
167
181
* @return {string }
168
182
*/
169
- get toolboxIcon ( ) {
170
- return IconInlineCode ;
183
+ get toolboxIcon ( ) : string {
184
+ return IconBrackets ;
171
185
}
172
186
173
187
/**
174
188
* Sanitizer rule
175
- * @return {{span: {class: string}} }
189
+ * @return {SanitizerConfig }
176
190
*/
177
- static get sanitize ( ) {
191
+ static get sanitize ( ) : SanitizerConfig {
178
192
return {
179
193
code : {
180
- class : InlineCode . CSS
181
- }
194
+ class : InlineCode . CSS ,
195
+ } ,
182
196
} ;
183
197
}
184
198
}
0 commit comments