@groupher/rich-editor 0.0.9 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rich-editor.es.js +5602 -5593
- package/dist/rich-editor.umd.js +26 -26
- package/package.json +2 -2
- package/src/RichEditor.tsx +43 -23
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@groupher/rich-editor",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.11",
|
|
5
5
|
"main": "dist/rich-editor.umd.js",
|
|
6
6
|
"module": "dist/rich-editor.es.js",
|
|
7
7
|
"type": "module",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"clean": "rm -rf dist",
|
|
12
12
|
"typecheck": "tsc -p tsconfig.types.json",
|
|
13
13
|
"build:vite": "vite build",
|
|
14
|
-
"
|
|
14
|
+
"release": "npm publish --access public",
|
|
15
15
|
"preview": "vite preview"
|
|
16
16
|
},
|
|
17
17
|
"overrides": {
|
package/src/RichEditor.tsx
CHANGED
|
@@ -15,8 +15,6 @@ import { MarkToolbarButton } from "@/components/ui/mark-toolbar-button";
|
|
|
15
15
|
import { I18nProvider, type TLocale, useI18n } from "@/i18n";
|
|
16
16
|
import { MentionProvider, type TMentionOption } from "@/mention-context";
|
|
17
17
|
|
|
18
|
-
const storageKey = "groupher-rich-editor-value";
|
|
19
|
-
|
|
20
18
|
const defaultValue: Value = [
|
|
21
19
|
{
|
|
22
20
|
type: "h1",
|
|
@@ -67,46 +65,58 @@ const defaultValue: Value = [
|
|
|
67
65
|
},
|
|
68
66
|
];
|
|
69
67
|
|
|
70
|
-
type TRichEditorProps = {
|
|
68
|
+
export type TRichEditorProps = {
|
|
69
|
+
value?: Value;
|
|
70
|
+
defaultValue?: Value;
|
|
71
|
+
onChange?: (value: Value) => void;
|
|
71
72
|
locale?: TLocale;
|
|
72
73
|
mentionOptions?: TMentionOption[];
|
|
73
74
|
onMentionSearch?: (query: string) => void;
|
|
74
75
|
};
|
|
75
76
|
|
|
76
|
-
|
|
77
|
+
type TRichEditorInnerProps = {
|
|
78
|
+
value?: Value;
|
|
79
|
+
defaultValue: Value;
|
|
80
|
+
onChange?: (value: Value) => void;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
function RichEditorInner({
|
|
84
|
+
value: controlledValue,
|
|
85
|
+
defaultValue,
|
|
86
|
+
onChange,
|
|
87
|
+
}: TRichEditorInnerProps) {
|
|
77
88
|
const i18n = useI18n();
|
|
89
|
+
const isControlled = controlledValue !== undefined;
|
|
78
90
|
const [value, setValue] = React.useState<Value>(() => {
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
const savedValue = localStorage.getItem(storageKey);
|
|
82
|
-
|
|
83
|
-
if (!savedValue) return defaultValue;
|
|
84
|
-
|
|
85
|
-
try {
|
|
86
|
-
return JSON.parse(savedValue) as Value;
|
|
87
|
-
} catch {
|
|
88
|
-
return defaultValue;
|
|
89
|
-
}
|
|
91
|
+
if (isControlled) return controlledValue;
|
|
92
|
+
return defaultValue;
|
|
90
93
|
});
|
|
94
|
+
const currentValue = isControlled ? controlledValue : value;
|
|
91
95
|
const [jsonInput, setJsonInput] = React.useState("");
|
|
92
96
|
const [jsonError, setJsonError] = React.useState("");
|
|
93
|
-
const [readOnlyValue, setReadOnlyValue] = React.useState<Value>(
|
|
97
|
+
const [readOnlyValue, setReadOnlyValue] = React.useState<Value>(currentValue);
|
|
94
98
|
|
|
95
99
|
const editor = usePlateEditor({
|
|
96
100
|
plugins: EditorKit,
|
|
97
|
-
value,
|
|
101
|
+
value: currentValue,
|
|
98
102
|
});
|
|
99
103
|
const readOnlyEditor = usePlateEditor({
|
|
100
104
|
plugins: EditorKit,
|
|
101
105
|
value: readOnlyValue,
|
|
102
106
|
});
|
|
103
107
|
|
|
108
|
+
React.useEffect(() => {
|
|
109
|
+
if (!isControlled) return;
|
|
110
|
+
|
|
111
|
+
editor.tf.setValue(controlledValue);
|
|
112
|
+
}, [controlledValue, editor, isControlled]);
|
|
113
|
+
|
|
104
114
|
const handleExport = React.useCallback(() => {
|
|
105
|
-
const nextJson = JSON.stringify(
|
|
115
|
+
const nextJson = JSON.stringify(currentValue, null, 2);
|
|
106
116
|
setJsonInput(nextJson);
|
|
107
|
-
setReadOnlyValue(
|
|
117
|
+
setReadOnlyValue(currentValue);
|
|
108
118
|
setJsonError("");
|
|
109
|
-
}, [
|
|
119
|
+
}, [currentValue]);
|
|
110
120
|
|
|
111
121
|
const handleRenderReadonly = React.useCallback(() => {
|
|
112
122
|
try {
|
|
@@ -127,8 +137,11 @@ function RichEditorInner() {
|
|
|
127
137
|
<Plate
|
|
128
138
|
editor={editor}
|
|
129
139
|
onChange={({ value }) => {
|
|
130
|
-
|
|
131
|
-
|
|
140
|
+
if (!isControlled) {
|
|
141
|
+
setValue(value);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
onChange?.(value);
|
|
132
145
|
}}
|
|
133
146
|
>
|
|
134
147
|
<FloatingToolbar>
|
|
@@ -197,6 +210,9 @@ function RichEditorInner() {
|
|
|
197
210
|
}
|
|
198
211
|
|
|
199
212
|
export default function RichEditor({
|
|
213
|
+
value,
|
|
214
|
+
defaultValue: defaultValueProp = defaultValue,
|
|
215
|
+
onChange,
|
|
200
216
|
locale,
|
|
201
217
|
mentionOptions,
|
|
202
218
|
onMentionSearch,
|
|
@@ -207,7 +223,11 @@ export default function RichEditor({
|
|
|
207
223
|
mentionOptions={mentionOptions}
|
|
208
224
|
onMentionSearch={onMentionSearch}
|
|
209
225
|
>
|
|
210
|
-
<RichEditorInner
|
|
226
|
+
<RichEditorInner
|
|
227
|
+
value={value}
|
|
228
|
+
defaultValue={defaultValueProp}
|
|
229
|
+
onChange={onChange}
|
|
230
|
+
/>
|
|
211
231
|
</MentionProvider>
|
|
212
232
|
</I18nProvider>
|
|
213
233
|
);
|