Skip to content

Commit da5f7c8

Browse files
committed
Add: WeEditor's JSdoc
1 parent bfdd552 commit da5f7c8

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

.eslintrc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"@typescript-eslint/no-use-before-define": "off",
2020
"import/prefer-default-export": "off",
2121
"react/jsx-props-no-spreading": "off",
22-
"no-param-reassign": ["error", { "props": false }]
22+
"no-param-reassign": ["error", { "props": false }],
23+
"no-console": "off",
24+
"no-alert": "off"
2325
}
2426
}

src/WeEditor.tsx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import * as React from 'react';
2-
import { NodeHtmlMarkdown } from 'node-html-markdown';
32

43
import { WE_EDITOR_ID } from './common/const';
54
import usePressKey from './hook/usePressKey';
65

76
import { WeToolbar } from './WeToolbar';
7+
import { htmlToMarkdown, markdownToHTML } from './module/markdown';
88

99
export interface WeEditorRef {
1010
getHTML: () => string;
@@ -16,24 +16,46 @@ export interface WeEditorProps extends React.HTMLAttributes<HTMLDivElement> {
1616
initialMarkdown?: string;
1717
}
1818

19+
/**
20+
* `WeEditor` is lightweight, tag-less and simple editor.
21+
*
22+
*
23+
* ```typescript
24+
* import { WeEditor, WeEditorRef } from "we-editor";
25+
* function ReactFunctionComponent() {
26+
* const editorRef = useRef<WeEditorRef>();
27+
*
28+
* const getHTML = () =>
29+
* editorRef.current?.getHTML();
30+
* const getMarkdown = () =>
31+
* editorRef.current?.getMarkdown();
32+
*
33+
* return <WeEditor ref={editorRef} />;
34+
* }
35+
* ```
36+
*/
1937
export const WeEditor = React.forwardRef<WeEditorRef, WeEditorProps>(
2038
({ initialHTML, initialMarkdown, ...divProps }, forwardedRef) => {
2139
const divRef = React.useRef<HTMLDivElement>(null);
2240

2341
React.useEffect(() => {
42+
if (initialHTML && initialMarkdown) {
43+
console.warn('we-editool: html state will override because markdown state imported together');
44+
}
45+
2446
if (divRef.current && initialHTML) {
2547
divRef.current.innerHTML = initialHTML;
2648
}
2749

2850
if (divRef.current && initialMarkdown) {
29-
divRef.current.innerHTML = initialMarkdown;
51+
divRef.current.innerHTML = markdownToHTML(initialMarkdown);
3052
}
3153
// eslint-disable-next-line react-hooks/exhaustive-deps
3254
}, []);
3355

3456
React.useImperativeHandle(forwardedRef, () => ({
3557
getHTML: () => divRef.current?.innerHTML ?? '',
36-
getMarkdown: () => NodeHtmlMarkdown.translate(divRef.current?.innerHTML ?? ''),
58+
getMarkdown: () => htmlToMarkdown(divRef.current?.innerHTML ?? ''),
3759
}));
3860

3961
usePressKey(divRef);

0 commit comments

Comments
 (0)