Skip to content

Commit 8d4f966

Browse files
committed
feat(migration): migrate codebase to TypeScript
closes #42
1 parent dcd4c17 commit 8d4f966

File tree

5 files changed

+715
-51
lines changed

5 files changed

+715
-51
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": "[email protected]"
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 & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,57 @@
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
}

tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
/* Language and Environment */
4+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
5+
/* Modules */
6+
"module": "CommonJS" /* Specify what module code is generated. */,
7+
"typeRoots": [
8+
"./node_modules/@types",
9+
"./types"
10+
] /* Specify multiple folders that act like './node_modules/@types'. */,
11+
/* Interop Constraints */
12+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
13+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
14+
/* Type Checking */
15+
"strict": true /* Enable all strict type-checking options. */
16+
},
17+
"include": ["src/*", "src/types/**/*"]
18+
}

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)