Skip to content

Commit 1cc6aee

Browse files
committed
feat: 提供 ts typings
1 parent df54ee2 commit 1cc6aee

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/config/
22
/scripts/
33
*.test.*
4-
/main.js
4+
/main.js
5+
*.d.ts

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ export default App;
5151

5252
- [x] 自动注入 style 文件
5353
- [x] 提供对外的 props 配置
54+
- [x] 提供 ts types
5455
- [ ] 压缩代码
55-
- [ ] 提供 ts types
56+
- [ ] MathJax 更新优化

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"module": "lib/Lib.js",
99
"homepage": "https://mdnice.com",
1010
"license": "GPL-3.0",
11+
"typings": "./lib/index.d.ts",
1112
"repository": {
1213
"type": "git",
1314
"url": "https://github.com/zhning12/markdown-nice"

src/App.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class App extends Component {
4343
}
4444

4545
setEditorContent = () => {
46-
const {text} = this.props;
47-
if (text) {
48-
this.props.content.setContent(text);
46+
const {defaultText} = this.props;
47+
if (defaultText) {
48+
this.props.content.setContent(defaultText);
4949
}
5050
};
5151

@@ -130,13 +130,13 @@ class App extends Component {
130130

131131
return (
132132
<appContext.Consumer>
133-
{({previewType, title, onTitleChange}) => (
133+
{({previewType, defaultTitle, onTitleChange}) => (
134134
<div className="App">
135-
<Navbar title={title} onTitleChange={onTitleChange} />
135+
<Navbar title={defaultTitle} onTitleChange={onTitleChange} />
136136
<div className="text-container">
137137
<div className="text-box" onMouseOver={(e) => this.setCurrentIndex(1, e)}>
138138
<CodeMirror
139-
value={this.props.text || this.props.content.content}
139+
value={this.props.content.content}
140140
options={{
141141
theme: "md-mirror",
142142
keyMap: "sublime",

src/Lib.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,18 @@ class Lib extends Component {
6969
}
7070

7171
render() {
72-
const {previewType, title, onTitleChange, text, onTextChange} = this.props;
72+
const {previewType, defaultTitle, onTitleChange, defaultText, onTextChange} = this.props;
7373
const appCtx = {
7474
previewType,
75-
title,
75+
defaultTitle,
7676
onTitleChange,
7777
};
7878

7979
return (
80-
<Provider
81-
content={content}
82-
title={title}
83-
userInfo={userInfo}
84-
navbar={navbar}
85-
dialog={dialog}
86-
imageHosting={imageHosting}
87-
>
80+
<Provider content={content} userInfo={userInfo} navbar={navbar} dialog={dialog} imageHosting={imageHosting}>
8881
{isPC() ? (
8982
<appContext.Provider value={appCtx}>
90-
<App text={text} onTextChange={onTextChange} />
83+
<App defaultText={defaultText} onTextChange={onTextChange} />
9184
</appContext.Provider>
9285
) : (
9386
<Result
@@ -115,17 +108,17 @@ const style = {
115108
};
116109

117110
Lib.defaultProps = {
118-
title: "Markdown Nice",
111+
defaultTitle: "Markdown Nice",
119112
previewType: "mobile",
120113
onTitleChange: () => {},
121-
text: "",
114+
defaultText: "",
122115
onTextChange: () => {},
123116
};
124117
Lib.propTypes = {
125-
title: PropTypes.node,
118+
defaultTitle: PropTypes.node,
126119
previewType: PropTypes.oneOf(["mobile", "pc"]),
127120
onTitleChange: PropTypes.func,
128-
text: PropTypes.string,
121+
defaultText: PropTypes.string,
129122
onTextChange: PropTypes.func,
130123
};
131124

src/index.d.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as React from "react";
2+
3+
declare const PreviewTypes: ["pc", "mobile"];
4+
5+
export declare type PreviewType = (typeof PreviewTypes)[number];
6+
7+
export interface MarkdownNiceProps {
8+
/**
9+
* 默认标题
10+
*/
11+
defaultTitle?: React.ReactNode;
12+
/**
13+
* 右侧预览类型 pc 或者 mobile
14+
*/
15+
previewType?: PreviewType;
16+
/**
17+
* 标题变化监听函数
18+
*/
19+
onTitleChange?: (title: React.ReactNode) => void;
20+
/**
21+
* 默认编辑器内容
22+
*/
23+
defaultText?: String;
24+
/**
25+
* 编辑器内容监听函数
26+
*/
27+
onTextChange?: (text: String) => void;
28+
}
29+
30+
declare class MarkdownNice extends React.Component<MarkdownNiceProps, any> {}
31+
32+
export default MarkdownNice;

0 commit comments

Comments
 (0)