Skip to content

Commit 491a9f9

Browse files
committed
Refactor: seperate function
1 parent 5e5852b commit 491a9f9

28 files changed

+1066
-959
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"react/require-default-props": "off",
1919
"@typescript-eslint/no-use-before-define": "off",
2020
"import/prefer-default-export": "off",
21-
"react/jsx-props-no-spreading": "off"
21+
"react/jsx-props-no-spreading": "off",
22+
"no-param-reassign": ["error", { "props": false }]
2223
}
2324
}

package-lock.json

Lines changed: 417 additions & 315 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "we-editor",
2+
"name": "we-editool",
33
"version": "1.0.0",
4-
"description": "light-weight, tagless and simple text editor!",
4+
"description": "light-weight, tagless and simple text edit tool!",
55
"main": "dist/index.js",
66
"module": "lib/index.js",
77
"repository": {
@@ -26,10 +26,9 @@
2626
"prettier": "prettier --write --config ./.prettierrc './src/**/*.{ts,tsx}'"
2727
},
2828
"devDependencies": {
29-
"@rollup/plugin-commonjs": "^22.0.0",
30-
"@rollup/plugin-node-resolve": "^13.3.0",
3129
"@testing-library/react": "13.2.0",
3230
"@types/jest": "27.5.1",
31+
"@types/marked": "^4.0.3",
3332
"@types/react": "18.0.9",
3433
"@typescript-eslint/eslint-plugin": "5.25.0",
3534
"@typescript-eslint/parser": "5.25.0",
@@ -48,15 +47,18 @@
4847
"react-dom": "18.1.0",
4948
"rollup": "2.74.1",
5049
"rollup-plugin-analyzer": "4.0.0",
50+
"rollup-plugin-node-externals": "^4.0.0",
5151
"rollup-plugin-peer-deps-external": "^2.2.4",
5252
"rollup-plugin-typescript2": "0.31.2",
5353
"ts-jest": "28.0.3",
5454
"typescript": "4.6.4"
5555
},
56-
"peerDependencies": {
56+
"dependencies": {
5757
"@emotion/react": "11.9.0",
5858
"@emotion/styled": "11.8.1",
59-
"react": "18.1.0",
59+
"marked": "4.0.16",
60+
"node-html-markdown": "1.2.0",
61+
"react": "^18.1.0",
6062
"react-icons": "4.3.1"
6163
}
6264
}

rollup.config.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import typescript from 'rollup-plugin-typescript2';
2-
import analyze from 'rollup-plugin-analyzer';
3-
import { nodeResolve } from '@rollup/plugin-node-resolve';
4-
import commonjs from '@rollup/plugin-commonjs';
52
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
3+
import externals from 'rollup-plugin-node-externals';
4+
import analyze from 'rollup-plugin-analyzer';
65
import packageJSON from './package.json';
76

87
export default {
@@ -19,13 +18,12 @@ export default {
1918
],
2019
plugins: [
2120
typescript(),
21+
peerDepsExternal(),
22+
externals(),
2223
analyze({
2324
// TODO: Add size_limit for CI
2425
onAnalysis: ({ bundleSize }) => {},
2526
summaryOnly: true,
2627
}),
27-
nodeResolve(),
28-
commonjs(),
29-
peerDepsExternal(),
3028
],
3129
};

src/Toolbar/ToolButton.tsx

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/Toolbar/Toolbar.tsx

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/Toolbar/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Toolbar/useToolbar.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/WeEditor.tsx

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
11
import * as React from 'react';
2+
import { NodeHtmlMarkdown } from 'node-html-markdown';
3+
24
import { WE_EDITOR_ID } from './common/const';
35
import usePressKey from './hook/usePressKey';
4-
import Toolbar from './Toolbar/Toolbar';
56

6-
const WeEditor = React.forwardRef<WeEditorRef, WeEditorProps>(
7-
({ htmlState, setHTMLState, ...divProps }, forwardedRef) => {
8-
const divRef = React.useRef<HTMLDivElement | null>(null);
7+
import { WeToolbar } from './WeToolbar';
8+
9+
export interface WeEditorRef {
10+
getHTML: () => string;
11+
getMarkdown: () => string;
12+
}
13+
14+
export interface WeEditorProps extends React.HTMLAttributes<HTMLDivElement> {
15+
initialHTML?: string;
16+
initialMarkdown?: string;
17+
}
18+
19+
export const WeEditor = React.forwardRef<WeEditorRef, WeEditorProps>(
20+
({ initialHTML, initialMarkdown, ...divProps }, forwardedRef) => {
21+
const divRef = React.useRef<HTMLDivElement>(null);
22+
23+
React.useEffect(() => {
24+
if (divRef.current && initialHTML) {
25+
divRef.current.innerHTML = initialHTML;
26+
}
27+
28+
if (divRef.current && initialMarkdown) {
29+
divRef.current.innerHTML = initialMarkdown;
30+
}
31+
// eslint-disable-next-line react-hooks/exhaustive-deps
32+
}, []);
933

1034
React.useImperativeHandle(forwardedRef, () => ({
11-
getHTMLState: () => divRef.current?.innerHTML,
35+
getHTML: () => divRef.current?.innerHTML ?? '',
36+
getMarkdown: () => NodeHtmlMarkdown.translate(divRef.current?.innerHTML ?? ''),
1237
}));
1338

1439
usePressKey(divRef);
1540

16-
const { onInput } = useOnInputCallback({ setHTMLState });
17-
1841
return (
1942
<>
20-
<div {...divProps} contentEditable id={WE_EDITOR_ID} ref={divRef} onInput={onInput} />
21-
<Toolbar divRef={divRef} />
43+
<div {...divProps} contentEditable id={WE_EDITOR_ID} ref={divRef} />
44+
<WeToolbar editorRef={divRef} />
2245
</>
2346
);
2447
}
2548
);
26-
27-
export default WeEditor;
28-
29-
export interface WeEditorRef {
30-
getHTMLState: () => string | undefined;
31-
}
32-
33-
export interface WeEditorProps extends React.HTMLAttributes<HTMLDivElement> {
34-
htmlState?: string;
35-
setHTMLState?: React.Dispatch<React.SetStateAction<string>>;
36-
}
37-
38-
function useOnInputCallback({ setHTMLState }: UseOnInputProps) {
39-
const onInput = React.useCallback(
40-
(event: React.FormEvent<HTMLDivElement>) => {
41-
if (setHTMLState) {
42-
setHTMLState(event.currentTarget.innerHTML);
43-
}
44-
},
45-
[setHTMLState]
46-
);
47-
48-
return { onInput };
49-
}
50-
51-
interface UseOnInputProps {
52-
setHTMLState?: React.Dispatch<React.SetStateAction<string>>;
53-
}

0 commit comments

Comments
 (0)