Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8c6f7ca

Browse files
committedNov 18, 2024
feat(migration): migrate project to typescript
1 parent e63e91a commit 8c6f7ca

File tree

6 files changed

+2137
-233
lines changed

6 files changed

+2137
-233
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ npm-debug.log
33
.idea/
44
.DS_Store
55
dist
6+
.yarn/

‎package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@editorjs/warning",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"keywords": [
55
"codex editor",
66
"warning",
@@ -16,10 +16,12 @@
1616
],
1717
"main": "./dist/warning.umd.js",
1818
"module": "./dist/warning.mjs",
19+
"types": "./dist/index.d.ts",
1920
"exports": {
2021
".": {
2122
"import": "./dist/warning.mjs",
22-
"require": "./dist/warning.umd.js"
23+
"require": "./dist/warning.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+
"typescript": "^5.6.3",
3437
"vite": "^4.5.0",
35-
"vite-plugin-css-injected-by-js": "^3.3.0"
38+
"vite-plugin-css-injected-by-js": "^3.3.0",
39+
"vite-plugin-dts": "^4.3.0"
3640
},
3741
"dependencies": {
38-
"@codexteam/icons": "^0.0.4"
42+
"@codexteam/icons": "^0.3.2",
43+
"@editorjs/editorjs": "^2.30.7"
3944
}
4045
}

‎src/index.js ‎src/index.ts

+78-29
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/**
2+
* import types
3+
*/
4+
import type {
5+
API,
6+
ToolboxConfig,
7+
BlockTool,
8+
ToolConfig,
9+
BlockToolData,
10+
} from '@editorjs/editorjs';
11+
112
/**
213
* Import Tool's icon
314
*/
@@ -8,38 +19,70 @@ import { IconWarning } from '@codexteam/icons';
819
*/
920
import './index.css';
1021

22+
interface WarningCSS {
23+
baseClass: string;
24+
wrapper: string;
25+
title: string;
26+
input: string;
27+
message: string;
28+
}
29+
1130
/**
12-
* @class Warning
13-
* @classdesc Warning Tool for Editor.js
14-
* @property {WarningData} data - Warning Tool`s input and output data
15-
* @property {object} api - Editor.js API instance
16-
*
1731
* @typedef {object} WarningData
1832
* @description Warning Tool`s input and output data
1933
* @property {string} title - warning`s title
2034
* @property {string} message - warning`s message
21-
*
35+
*/
36+
export interface WarningData extends BlockToolData {
37+
title: string;
38+
message: string;
39+
}
40+
41+
/**
2242
* @typedef {object} WarningConfig
2343
* @description Warning Tool`s initial configuration
2444
* @property {string} titlePlaceholder - placeholder to show in warning`s title input
2545
* @property {string} messagePlaceholder - placeholder to show in warning`s message input
2646
*/
27-
export default class Warning {
47+
export interface WarningConfig extends ToolConfig {
48+
titlePlaceholder?: string;
49+
messagePlaceholder?: string;
50+
}
51+
52+
interface WarningConstructorArgs {
53+
data: WarningData;
54+
config?: WarningConfig;
55+
api: API;
56+
readOnly: boolean;
57+
}
58+
59+
/**
60+
* @class Warning
61+
* @classdesc Warning Tool for Editor.js
62+
* @property {WarningData} data - Warning Tool`s input and output data
63+
* @property {API} api - Editor.js API instance
64+
*/
65+
export default class Warning implements BlockTool {
66+
private api: API;
67+
private readOnly: boolean;
68+
private data: WarningData;
69+
titlePlaceholder: string;
70+
messagePlaceholder: string;
2871

2972
/**
3073
* Notify core that read-only mode is supported
3174
*/
32-
static get isReadOnlySupported() {
75+
static get isReadOnlySupported(): boolean {
3376
return true;
3477
}
3578

3679
/**
3780
* Get Toolbox settings
3881
*
3982
* @public
40-
* @returns {string}
83+
* @returns {ToolboxConfig}
4184
*/
42-
static get toolbox() {
85+
static get toolbox(): ToolboxConfig {
4386
return {
4487
icon: IconWarning,
4588
title: 'Warning',
@@ -52,7 +95,7 @@ export default class Warning {
5295
* @public
5396
* @returns {boolean}
5497
*/
55-
static get enableLineBreaks() {
98+
static get enableLineBreaks(): boolean {
5699
return true;
57100
}
58101

@@ -62,7 +105,7 @@ export default class Warning {
62105
* @public
63106
* @returns {string}
64107
*/
65-
static get DEFAULT_TITLE_PLACEHOLDER() {
108+
static get DEFAULT_TITLE_PLACEHOLDER(): string {
66109
return 'Title';
67110
}
68111

@@ -72,16 +115,16 @@ export default class Warning {
72115
* @public
73116
* @returns {string}
74117
*/
75-
static get DEFAULT_MESSAGE_PLACEHOLDER() {
118+
static get DEFAULT_MESSAGE_PLACEHOLDER(): string {
76119
return 'Message';
77120
}
78121

79122
/**
80123
* Warning Tool`s styles
81124
*
82-
* @returns {object}
125+
* @returns {WarningCSS}
83126
*/
84-
get CSS() {
127+
get CSS(): WarningCSS {
85128
return {
86129
baseClass: this.api.styles.block,
87130
wrapper: 'cdx-warning',
@@ -94,17 +137,20 @@ export default class Warning {
94137
/**
95138
* Render plugin`s main Element and fill it with saved data
96139
*
97-
* @param {WarningData} data — previously saved data
98-
* @param {WarningConfig} config — user config for Tool
99-
* @param {object} api - Editor.js API
100-
* @param {boolean} readOnly - read-only mode flag
140+
* @param {object} params — constructor params
141+
* @param {WarningData} params.data — previously saved data
142+
* @param {WarningConfig} params.config — user config for Tool
143+
* @param {API} params.api - Editor.js API
144+
* @param {boolean} params.readOnly - read-only mode flag
101145
*/
102-
constructor({ data, config, api, readOnly }) {
146+
constructor({ data, config, api, readOnly }: WarningConstructorArgs) {
103147
this.api = api;
104148
this.readOnly = readOnly;
105149

106-
this.titlePlaceholder = config.titlePlaceholder || Warning.DEFAULT_TITLE_PLACEHOLDER;
107-
this.messagePlaceholder = config.messagePlaceholder || Warning.DEFAULT_MESSAGE_PLACEHOLDER;
150+
this.titlePlaceholder =
151+
config?.titlePlaceholder || Warning.DEFAULT_TITLE_PLACEHOLDER;
152+
this.messagePlaceholder =
153+
config?.messagePlaceholder || Warning.DEFAULT_MESSAGE_PLACEHOLDER;
108154

109155
this.data = {
110156
title: data.title || '',
@@ -117,7 +163,7 @@ export default class Warning {
117163
*
118164
* @returns {Element}
119165
*/
120-
render() {
166+
render(): HTMLElement {
121167
const container = this._make('div', [this.CSS.baseClass, this.CSS.wrapper]);
122168
const title = this._make('div', [this.CSS.input, this.CSS.title], {
123169
contentEditable: !this.readOnly,
@@ -143,13 +189,13 @@ export default class Warning {
143189
* @param {HTMLDivElement} warningElement - element to save
144190
* @returns {WarningData}
145191
*/
146-
save(warningElement) {
192+
save(warningElement: HTMLDivElement): WarningData {
147193
const title = warningElement.querySelector(`.${this.CSS.title}`);
148194
const message = warningElement.querySelector(`.${this.CSS.message}`);
149195

150196
return Object.assign(this.data, {
151-
title: title.innerHTML,
152-
message: message.innerHTML,
197+
title: title?.innerHTML ?? '',
198+
message: message?.innerHTML ?? '',
153199
});
154200
}
155201

@@ -161,7 +207,11 @@ export default class Warning {
161207
* @param {object} attributes - any attributes
162208
* @returns {Element}
163209
*/
164-
_make(tagName, classNames = null, attributes = {}) {
210+
private _make<K extends keyof HTMLElementTagNameMap>(
211+
tagName: K,
212+
classNames: string | string[] | null = null,
213+
attributes: { [key: string]: any } = {}
214+
): HTMLElementTagNameMap[K] {
165215
const el = document.createElement(tagName);
166216

167217
if (Array.isArray(classNames)) {
@@ -171,7 +221,7 @@ export default class Warning {
171221
}
172222

173223
for (const attrName in attributes) {
174-
el[attrName] = attributes[attrName];
224+
(el as any)[attrName] = attributes[attrName];
175225
}
176226

177227
return el;
@@ -180,7 +230,6 @@ export default class Warning {
180230
/**
181231
* Sanitizer config for Warning Tool saved data
182232
*
183-
* @returns {object}
184233
*/
185234
static get sanitize() {
186235
return {

‎tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2016",
4+
"module": "ESNext",
5+
"moduleResolution": "Node",
6+
"lib": ["dom", "ESNext"],
7+
"esModuleInterop": true,
8+
"forceConsistentCasingInFileNames": true,
9+
"strict": true,
10+
"skipLibCheck": true,
11+
"outDir": "./dist",
12+
"declaration": true,
13+
"declarationDir": "./dist/types",
14+
"isolatedModules": true,
15+
},
16+
"include": ["src"],
17+
"exclude": ["node_modules"]
18+
}

‎vite.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "path";
22
import cssInjectedByJsPlugin from "vite-plugin-css-injected-by-js";
3+
import dts from "vite-plugin-dts";
34
import * as pkg from "./package.json";
45

56
const NODE_ENV = process.argv.mode || "development";
@@ -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: "Warning",
1415
fileName: "warning",
1516
},
@@ -19,5 +20,5 @@ export default {
1920
VERSION: JSON.stringify(VERSION),
2021
},
2122

22-
plugins: [cssInjectedByJsPlugin()],
23+
plugins: [cssInjectedByJsPlugin(), dts()],
2324
};

‎yarn.lock

+2,028-198
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.