11import * as React from 'react' ;
2- import { NodeHtmlMarkdown } from 'node-html-markdown' ;
32
43import { WE_EDITOR_ID } from './common/const' ;
54import usePressKey from './hook/usePressKey' ;
65
76import { WeToolbar } from './WeToolbar' ;
7+ import { htmlToMarkdown , markdownToHTML } from './module/markdown' ;
88
99export 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+ */
1937export 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