11/**
22 * Build styles
33 */
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+ }
612
713/**
814 * Inline Code Tool for the Editor.js
915 *
1016 * Allows to wrap inline fragment and style it somehow.
1117 */
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+
1336 /**
1437 * Class name for term-tag
1538 *
1639 * @type {string }
1740 */
18- static get CSS ( ) {
41+ static get CSS ( ) : string {
1942 return 'inline-code' ;
20- } ;
43+ }
2144
22- /**
23- */
24- constructor ( { api} ) {
45+ constructor ( { api } : InlineToolConstructorOptions ) {
2546 this . api = api ;
2647
27- /**
28- * Toolbar Button
29- *
30- * @type {HTMLElement|null }
31- */
3248 this . button = null ;
3349
34- /**
35- * Tag represented the term
36- *
37- * @type {string }
38- */
39- this . tag = 'CODE' ;
40-
41- /**
42- * CSS classes
43- */
4450 this . iconClasses = {
4551 base : this . api . styles . inlineToolButton ,
46- active : this . api . styles . inlineToolButtonActive
52+ active : this . api . styles . inlineToolButtonActive ,
4753 } ;
4854 }
4955
@@ -52,7 +58,7 @@ export default class InlineCode {
5258 *
5359 * @return {boolean }
5460 */
55- static get isInline ( ) {
61+ static get isInline ( ) : boolean {
5662 return true ;
5763 }
5864
@@ -61,7 +67,7 @@ export default class InlineCode {
6167 *
6268 * @return {HTMLElement }
6369 */
64- render ( ) {
70+ render ( ) : HTMLElement {
6571 this . button = document . createElement ( 'button' ) ;
6672 this . button . type = 'button' ;
6773 this . button . classList . add ( this . iconClasses . base ) ;
@@ -75,12 +81,12 @@ export default class InlineCode {
7581 *
7682 * @param {Range } range - selected fragment
7783 */
78- surround ( range ) {
84+ surround ( range : Range ) : void {
7985 if ( ! range ) {
8086 return ;
8187 }
8288
83- let termWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) ;
89+ let termWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) as HTMLElement ;
8490
8591 /**
8692 * If start or end of selection is in the highlighted block
@@ -97,7 +103,7 @@ export default class InlineCode {
97103 *
98104 * @param {Range } range - selected fragment
99105 */
100- wrap ( range ) {
106+ wrap ( range : Range ) : void {
101107 /**
102108 * Create a wrapper for highlighting
103109 */
@@ -125,21 +131,22 @@ export default class InlineCode {
125131 *
126132 * @param {HTMLElement } termWrapper - term wrapper tag
127133 */
128- unwrap ( termWrapper ) {
134+ unwrap ( termWrapper : HTMLElement ) : void {
129135 /**
130136 * Expand selection to all term-tag
131137 */
132138 this . api . selection . expandToTag ( termWrapper ) ;
133139
134- let sel = window . getSelection ( ) ;
135- let range = sel . getRangeAt ( 0 ) ;
140+ const sel = window . getSelection ( ) ;
141+ if ( ! sel ) return ;
136142
137- let unwrappedContent = range . extractContents ( ) ;
143+ const range = sel . getRangeAt ( 0 ) ;
144+ const unwrappedContent = range . extractContents ( ) ;
138145
139146 /**
140147 * Remove empty term-tag
141148 */
142- termWrapper . parentNode . removeChild ( termWrapper ) ;
149+ termWrapper . parentNode ? .removeChild ( termWrapper ) ;
143150
144151 /**
145152 * Insert extracted content
@@ -155,30 +162,37 @@ export default class InlineCode {
155162
156163 /**
157164 * Check and change Term's state for current selection
165+ *
166+ * @return {boolean }
158167 */
159- checkState ( ) {
168+ checkState ( ) : boolean {
160169 const termTag = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) ;
161170
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 ;
163176 }
164177
178+
165179 /**
166180 * Get Tool icon's SVG
167181 * @return {string }
168182 */
169- get toolboxIcon ( ) {
170- return IconInlineCode ;
183+ get toolboxIcon ( ) : string {
184+ return IconBrackets ;
171185 }
172186
173187 /**
174188 * Sanitizer rule
175- * @return {{span: {class: string}} }
189+ * @return {SanitizerConfig }
176190 */
177- static get sanitize ( ) {
191+ static get sanitize ( ) : SanitizerConfig {
178192 return {
179193 code : {
180- class : InlineCode . CSS
181- }
194+ class : InlineCode . CSS ,
195+ } ,
182196 } ;
183197 }
184198}
0 commit comments