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 ;
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- */
3950 this . tag = 'CODE' ;
4051
41- /**
42- * CSS classes
43- */
4452 this . iconClasses = {
4553 base : this . api . styles . inlineToolButton ,
46- active : this . api . styles . inlineToolButtonActive
54+ active : this . api . styles . inlineToolButtonActive ,
4755 } ;
4856 }
4957
@@ -52,7 +60,7 @@ export default class InlineCode {
5260 *
5361 * @return {boolean }
5462 */
55- static get isInline ( ) {
63+ static get isInline ( ) : boolean {
5664 return true ;
5765 }
5866
@@ -61,7 +69,7 @@ export default class InlineCode {
6169 *
6270 * @return {HTMLElement }
6371 */
64- render ( ) {
72+ render ( ) : HTMLElement {
6573 this . button = document . createElement ( 'button' ) ;
6674 this . button . type = 'button' ;
6775 this . button . classList . add ( this . iconClasses . base ) ;
@@ -75,12 +83,12 @@ export default class InlineCode {
7583 *
7684 * @param {Range } range - selected fragment
7785 */
78- surround ( range ) {
86+ surround ( range : Range ) : void {
7987 if ( ! range ) {
8088 return ;
8189 }
8290
83- let termWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) ;
91+ let termWrapper = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) as HTMLElement ;
8492
8593 /**
8694 * If start or end of selection is in the highlighted block
@@ -97,7 +105,7 @@ export default class InlineCode {
97105 *
98106 * @param {Range } range - selected fragment
99107 */
100- wrap ( range ) {
108+ wrap ( range : Range ) : void {
101109 /**
102110 * Create a wrapper for highlighting
103111 */
@@ -125,21 +133,22 @@ export default class InlineCode {
125133 *
126134 * @param {HTMLElement } termWrapper - term wrapper tag
127135 */
128- unwrap ( termWrapper ) {
136+ unwrap ( termWrapper : HTMLElement ) : void {
129137 /**
130138 * Expand selection to all term-tag
131139 */
132140 this . api . selection . expandToTag ( termWrapper ) ;
133141
134- let sel = window . getSelection ( ) ;
135- let range = sel . getRangeAt ( 0 ) ;
142+ const sel = window . getSelection ( ) ;
143+ if ( ! sel ) return ;
136144
137- let unwrappedContent = range . extractContents ( ) ;
145+ const range = sel . getRangeAt ( 0 ) ;
146+ const unwrappedContent = range . extractContents ( ) ;
138147
139148 /**
140149 * Remove empty term-tag
141150 */
142- termWrapper . parentNode . removeChild ( termWrapper ) ;
151+ termWrapper . parentNode ? .removeChild ( termWrapper ) ;
143152
144153 /**
145154 * Insert extracted content
@@ -155,30 +164,37 @@ export default class InlineCode {
155164
156165 /**
157166 * Check and change Term's state for current selection
167+ *
168+ * @return {boolean }
158169 */
159- checkState ( ) {
170+ checkState ( ) : boolean {
160171 const termTag = this . api . selection . findParentTag ( this . tag , InlineCode . CSS ) ;
161172
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 ;
163178 }
164179
180+
165181 /**
166182 * Get Tool icon's SVG
167183 * @return {string }
168184 */
169- get toolboxIcon ( ) {
170- return IconInlineCode ;
185+ get toolboxIcon ( ) : string {
186+ return IconBrackets ;
171187 }
172188
173189 /**
174190 * Sanitizer rule
175- * @return {{span: {class: string}} }
191+ * @return {SanitizerConfig }
176192 */
177- static get sanitize ( ) {
193+ static get sanitize ( ) : SanitizerConfig {
178194 return {
179195 code : {
180- class : InlineCode . CSS
181- }
196+ class : InlineCode . CSS ,
197+ } ,
182198 } ;
183199 }
184200}
0 commit comments