Skip to content

Commit 31a086d

Browse files
authored
Merge pull request editor-js#43 from idebenone/migrating_to_typescript
feat(migration): migrate codebase to TypeScript
2 parents dcd4c17 + 27e947d commit 31a086d

File tree

5 files changed

+712
-53
lines changed

5 files changed

+712
-53
lines changed

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/inline-code",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"keywords": [
55
"codex editor",
66
"inline",
@@ -16,10 +16,12 @@
1616
],
1717
"main": "./dist/inline-code.umd.js",
1818
"module": "./dist/inline-code.mjs",
19+
"types": "dist/index.d.ts",
1920
"exports": {
2021
".": {
2122
"import": "./dist/inline-code.mjs",
22-
"require": "./dist/inline-code.umd.js"
23+
"require": "./dist/inline-code.umd.js",
24+
"types": "./dist/index.d.ts"
2325
}
2426
},
2527
"scripts": {
@@ -31,10 +33,13 @@
3133
"email": "team@ifmo.su"
3234
},
3335
"devDependencies": {
36+
"@editorjs/editorjs": "^2.30.2",
37+
"typescript": "^5.5.3",
3438
"vite": "^4.5.0",
35-
"vite-plugin-css-injected-by-js": "^3.3.0"
39+
"vite-plugin-css-injected-by-js": "^3.3.0",
40+
"vite-plugin-dts": "^4.0.0-beta.1"
3641
},
3742
"dependencies": {
38-
"@codexteam/icons": "^0.0.5"
43+
"@codexteam/icons": "^0.3.2"
3944
}
4045
}

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

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
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
}

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
/* Language and Environment */
4+
"target": "es2020",
5+
/* Modules */
6+
"module": "CommonJS" /* Specify what module code is generated. */,
7+
"typeRoots": [
8+
"./node_modules/@types"
9+
] /* Specify multiple folders that act like './node_modules/@types'. */,
10+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
11+
/* Type Checking */
12+
"strict": true /* Enable all strict type-checking options. */
13+
},
14+
"include": ["src/*"]
15+
}

vite.config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
33
import * as pkg from "./package.json";
4+
import dts from 'vite-plugin-dts';
45

56
const NODE_ENV = process.argv.mode || "development";
67
const VERSION = pkg.version;
@@ -9,7 +10,7 @@ export default {
910
build: {
1011
copyPublicDir: false,
1112
lib: {
12-
entry: path.resolve(__dirname, "src", "index.js"),
13+
entry: path.resolve(__dirname, "src", "index.ts"),
1314
name: "InlineCode",
1415
fileName: "inline-code",
1516
},
@@ -19,5 +20,8 @@ export default {
1920
VERSION: JSON.stringify(VERSION),
2021
},
2122

22-
plugins: [cssInjectedByJsPlugin()],
23-
};
23+
plugins: [cssInjectedByJsPlugin(),
24+
dts({
25+
tsconfigPath: './tsconfig.json'
26+
})],
27+
}

0 commit comments

Comments
 (0)