1- /**
2- * Build styles
3- */
41import './index.css' ;
52import { getLineStartPosition } from './utils/string' ;
63import { IconBrackets } from '@codexteam/icons' ;
7-
4+ import { API , BlockTool , BlockToolConstructorOptions , PasteEvent , SanitizerConfig } from '@editorjs/editorjs' ;
85
96/**
107 * CodeTool for Editor.js
118 *
12- * @author CodeX ([email protected] ) 13- * @copyright CodeX 2018
14- * @license MIT
159 * @version 2.0.0
10+ * @license MIT
1611 */
1712
18- /* global PasteEvent */
13+ /**
14+ * CodeTool generates data in this format
15+ */
16+ export interface CodeData {
17+ code : string ;
18+ }
19+
20+ /**
21+ * CodeTool's config from User
22+ */
23+ export interface CodeConfig {
24+ placeholder : string
25+ }
26+
27+ interface CodeToolCSS {
28+ /** Block Styling from Editor.js API */
29+ baseClass : string ;
30+ /** Input Styling from Editor.js API */
31+ input : string ;
32+ /** Wrapper styling */
33+ wrapper : string ;
34+ /** Textarea styling */
35+ textarea : string ;
36+ }
37+
38+ interface CodeToolNodes {
39+ /** Main container or Wrapper for CodeTool */
40+ holder : HTMLDivElement | null ;
41+ /** Textarea where user inputs their code */
42+ textarea : HTMLTextAreaElement | null ;
43+ }
1944
2045/**
2146 * Code Tool for the Editor.js allows to include code examples in your articles.
2247 */
23- export default class CodeTool {
48+ export default class CodeTool implements BlockTool {
49+ /**
50+ * Editor.js API
51+ */
52+ private api : API ;
53+ /**
54+ * Read-only mode flag
55+ */
56+ private readOnly : boolean ;
57+ /**
58+ * CodeTool's placeholder
59+ */
60+ private placeholder : string ;
61+ /**
62+ * CodeTool's CSS
63+ */
64+ private CSS : CodeToolCSS ;
65+ /**
66+ * CodeTool nodes
67+ */
68+ private nodes : CodeToolNodes ;
69+ /**
70+ * CodeTool's data
71+ */
72+ private _data ! : CodeData ;
73+
2474 /**
2575 * Notify core that read-only mode is supported
2676 *
2777 * @returns {boolean }
2878 */
29- static get isReadOnlySupported ( ) {
79+ static get isReadOnlySupported ( ) : boolean {
3080 return true ;
3181 }
3282
@@ -36,7 +86,7 @@ export default class CodeTool {
3686 * @returns {boolean }
3787 * @public
3888 */
39- static get enableLineBreaks ( ) {
89+ static get enableLineBreaks ( ) : boolean {
4090 return true ;
4191 }
4292
@@ -54,7 +104,7 @@ export default class CodeTool {
54104 * @param {object } options.api - Editor.js API
55105 * @param {boolean } options.readOnly - read only mode flag
56106 */
57- constructor ( { data, config, api, readOnly } ) {
107+ constructor ( { data, config, api, readOnly } : BlockToolConstructorOptions ) {
58108 this . api = api ;
59109 this . readOnly = readOnly ;
60110
@@ -82,12 +132,12 @@ export default class CodeTool {
82132 /**
83133 * Create Tool's view
84134 *
85- * @returns {HTMLElement }
135+ * @returns {HTMLDivElement }
86136 * @private
87137 */
88- drawView ( ) {
89- const wrapper = document . createElement ( 'div' ) ,
90- textarea = document . createElement ( 'textarea' ) ;
138+ private drawView ( ) : HTMLDivElement {
139+ const wrapper = document . createElement ( 'div' ) as HTMLDivElement ;
140+ const textarea = document . createElement ( 'textarea' ) ;
91141
92142 wrapper . classList . add ( this . CSS . baseClass , this . CSS . wrapper ) ;
93143 textarea . classList . add ( this . CSS . textarea , this . CSS . input ) ;
@@ -123,8 +173,8 @@ export default class CodeTool {
123173 * @returns {HTMLDivElement } this.nodes.holder - Code's wrapper
124174 * @public
125175 */
126- render ( ) {
127- return this . nodes . holder ;
176+ public render ( ) : HTMLDivElement {
177+ return this . nodes . holder ! ;
128178 }
129179
130180 /**
@@ -134,9 +184,9 @@ export default class CodeTool {
134184 * @returns {CodeData } - saved plugin code
135185 * @public
136186 */
137- save ( codeWrapper ) {
187+ public save ( codeWrapper : HTMLDivElement ) : CodeData {
138188 return {
139- code : codeWrapper . querySelector ( 'textarea' ) . value ,
189+ code : codeWrapper . querySelector ( 'textarea' ) ! . value ,
140190 } ;
141191 }
142192
@@ -145,20 +195,23 @@ export default class CodeTool {
145195 *
146196 * @param {PasteEvent } event - event with pasted content
147197 */
148- onPaste ( event ) {
149- const content = event . detail . data ;
150-
151- this . data = {
152- code : content . textContent ,
153- } ;
198+ public onPaste ( event : PasteEvent ) : void {
199+ const detail = event . detail ;
200+
201+ if ( 'data' in detail ) {
202+ const content = detail . data as string ;
203+ this . data = {
204+ code : content || '' ,
205+ } ;
206+ }
154207 }
155208
156209 /**
157210 * Returns Tool`s data from private property
158211 *
159212 * @returns {CodeData }
160213 */
161- get data ( ) {
214+ public get data ( ) : CodeData {
162215 return this . _data ;
163216 }
164217
@@ -167,7 +220,7 @@ export default class CodeTool {
167220 *
168221 * @param {CodeData } data - saved tool data
169222 */
170- set data ( data ) {
223+ public set data ( data : CodeData ) {
171224 this . _data = data ;
172225
173226 if ( this . nodes . textarea ) {
@@ -182,7 +235,7 @@ export default class CodeTool {
182235 *
183236 * @returns {{icon: string, title: string} }
184237 */
185- static get toolbox ( ) {
238+ static get toolbox ( ) : { icon : string ; title : string } {
186239 return {
187240 icon : IconBrackets ,
188241 title : 'Code' ,
@@ -195,7 +248,7 @@ export default class CodeTool {
195248 * @public
196249 * @returns {string }
197250 */
198- static get DEFAULT_PLACEHOLDER ( ) {
251+ static get DEFAULT_PLACEHOLDER ( ) : string {
199252 return 'Enter a code' ;
200253 }
201254
@@ -206,9 +259,9 @@ export default class CodeTool {
206259 * @static
207260 * @returns {{tags: string[]} }
208261 */
209- static get pasteConfig ( ) {
262+ static get pasteConfig ( ) : { tags : string [ ] } {
210263 return {
211- tags : [ 'pre' ] ,
264+ tags : [ 'pre' ] ,
212265 } ;
213266 }
214267
@@ -217,7 +270,7 @@ export default class CodeTool {
217270 *
218271 * @returns {{code: boolean} }
219272 */
220- static get sanitize ( ) {
273+ static get sanitize ( ) : SanitizerConfig {
221274 return {
222275 code : true , // Allow HTML tags
223276 } ;
@@ -230,7 +283,7 @@ export default class CodeTool {
230283 * @param {KeyboardEvent } event - keydown
231284 * @returns {void }
232285 */
233- tabHandler ( event ) {
286+ private tabHandler ( event : KeyboardEvent ) : void {
234287 /**
235288 * Prevent editor.js tab handler
236289 */
@@ -241,7 +294,7 @@ export default class CodeTool {
241294 */
242295 event . preventDefault ( ) ;
243296
244- const textarea = event . target ;
297+ const textarea = event . target as HTMLTextAreaElement ;
245298 const isShiftPressed = event . shiftKey ;
246299 const caretPosition = textarea . selectionStart ;
247300 const value = textarea . value ;
0 commit comments