Skip to content

Commit 46fd5a9

Browse files
authored
Merge pull request #73 from idebenone/migration
Migrate to TypeScript
2 parents f281996 + 8d1bcbe commit 46fd5a9

File tree

7 files changed

+1451
-141
lines changed

7 files changed

+1451
-141
lines changed

.eslintrc

+6-81
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,8 @@
11
{
2-
/** Enable ES6 features */
3-
"parserOptions": {
4-
"ecmaVersion": 6,
5-
"sourceType": "module",
6-
"ecmaFeatures": {
7-
"classes": true
8-
}
9-
},
10-
"rules": {
11-
12-
"arrow-spacing": [2, {
13-
"before": true,
14-
"after": true
15-
}],
16-
17-
/** Variables */
18-
"no-catch-shadow": 2,
19-
"no-delete-var": 2,
20-
"no-label-var": 2,
21-
"no-shadow-restricted-names": 2,
22-
"no-shadow": 2,
23-
"no-undef-init": 2,
24-
"no-undef": 2,
25-
"no-unused-vars": 1,
26-
27-
/** Style */
28-
"array-bracket-spacing": [2, "never", {
29-
"singleValue": true,
30-
"objectsInArrays": true,
31-
"arraysInArrays": true
32-
}],
33-
"quotes": [1, "single", "avoid-escape"],
34-
"eqeqeq": 0,
35-
"brace-style": [2, "1tbs"],
36-
37-
"comma-spacing": [2, {
38-
"before": false,
39-
"after": true
40-
}],
41-
"comma-style": [2, "last"],
42-
"eol-last": 0,
43-
"no-nested-ternary": 1,
44-
"no-trailing-spaces": 2,
45-
"no-mixed-spaces-and-tabs": 2,
46-
"padded-blocks": [2, "never"],
47-
"space-before-blocks": 1,
48-
"space-before-function-paren": [1, {
49-
"anonymous": "always",
50-
"named": "never"
51-
}],
52-
"spaced-comment": [2, "always", {
53-
"exceptions": ["-", "+"],
54-
"markers": ["=", "!"]
55-
}],
56-
"semi": [2, "always"],
57-
"indent": [2, 2, {
58-
"SwitchCase": 1,
59-
"VariableDeclarator": 2
60-
}],
61-
"camelcase": [2, {
62-
"properties": "always"
63-
}],
64-
"newline-after-var": [1, "always"],
65-
"require-jsdoc": ["error", {
66-
"require": {
67-
"FunctionDeclaration": true,
68-
"MethodDefinition": true,
69-
"ClassDeclaration": true,
70-
"ArrowFunctionExpression": false
71-
}
72-
}]
73-
74-
},
75-
"globals": {
76-
"document": true,
77-
"module": true,
78-
"require": true,
79-
"window": true,
80-
"console": true,
81-
"define": true
82-
}
2+
"globals": {},
3+
"parserOptions": {
4+
"sourceType": "module",
5+
"ecmaVersion": 2020
6+
},
7+
"parser": "@typescript-eslint/parser"
838
}

package.json

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/code",
3-
"version": "2.9.0",
3+
"version": "2.9.1",
44
"keywords": [
55
"codex editor",
66
"code",
@@ -15,25 +15,36 @@
1515
],
1616
"main": "./dist/code.umd.js",
1717
"module": "./dist/code.mjs",
18+
"types": "dist/index.d.ts",
1819
"exports": {
1920
".": {
2021
"import": "./dist/code.mjs",
21-
"require": "./dist/code.umd.js"
22+
"require": "./dist/code.umd.js",
23+
"types": "./dist/index.d.ts"
2224
}
2325
},
2426
"scripts": {
2527
"dev": "vite",
26-
"build": "vite build"
28+
"build": "vite build",
29+
"lint": "eslint src/**.ts --ext .ts",
30+
"lint:errors": "eslint src/**.ts --quiet",
31+
"lint:fix": "eslint src/**.ts --fix"
2732
},
2833
"author": {
2934
"name": "CodeX Team",
3035
"email": "[email protected]"
3136
},
3237
"devDependencies": {
38+
"@editorjs/editorjs": "^2.30.2",
39+
"@typescript-eslint/eslint-plugin": "^7.16.1",
40+
"@typescript-eslint/parser": "^7.16.1",
41+
"eslint": "^8.57.0",
42+
"typescript": "^5.5.3",
3343
"vite": "^4.5.0",
34-
"vite-plugin-css-injected-by-js": "^3.3.0"
44+
"vite-plugin-css-injected-by-js": "^3.3.0",
45+
"vite-plugin-dts": "^3.9.1"
3546
},
3647
"dependencies": {
37-
"@codexteam/icons": "^0.0.5"
48+
"@codexteam/icons": "^0.3.2"
3849
}
3950
}

src/index.js renamed to src/index.ts

+88-35
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,82 @@
1-
/**
2-
* Build styles
3-
*/
41
import './index.css';
52
import { getLineStartPosition } from './utils/string';
63
import { 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;

src/utils/string.js renamed to src/utils/string.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @param {number} position - search starting position
1111
* @returns {number}
1212
*/
13-
export function getLineStartPosition(string, position) {
13+
export function getLineStartPosition(string: string, position: number): number {
1414
const charLength = 1;
1515
let char = '';
1616

0 commit comments

Comments
 (0)