@blocklet/discuss-kit-ux 2.5.19 → 2.5.20

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.
@@ -1,7 +1,3 @@
1
- import { EditorState } from 'lexical';
2
- interface Props {
3
- value: EditorState | null;
4
- isChanged: boolean;
5
- }
6
- export default function AutoClearPlugin({ value, isChanged }: Props): null;
7
- export {};
1
+ export declare function AutoClearPlugin({ clearKey }: {
2
+ clearKey: number;
3
+ }): null;
@@ -1,26 +1,20 @@
1
1
  import { useEffect } from "react";
2
2
  import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
3
3
  import { $createParagraphNode, $getRoot, $getSelection } from "lexical";
4
- export default function AutoClearPlugin({ value, isChanged }) {
4
+ export function AutoClearPlugin({ clearKey }) {
5
5
  const [editor] = useLexicalComposerContext();
6
6
  useEffect(() => {
7
- try {
8
- if (!isChanged) return;
9
- if (value) return;
10
- if (!editor) return;
7
+ if (editor && clearKey) {
11
8
  editor.update(() => {
12
9
  const root = $getRoot();
13
- const selection = $getSelection();
14
10
  const paragraph = $createParagraphNode();
15
11
  root.clear();
16
12
  root.append(paragraph);
17
- if (selection !== null) {
13
+ if ($getSelection() !== null) {
18
14
  paragraph.select();
19
15
  }
20
16
  });
21
- } catch (e) {
22
- console.error(e);
23
17
  }
24
- }, [value, isChanged, editor]);
18
+ }, [editor, clearKey]);
25
19
  return null;
26
20
  }
@@ -6,7 +6,7 @@ import isNil from "lodash/isNil";
6
6
  import { EditorConfigProvider, useEditorConfig } from "@blocklet/editor/lib/config";
7
7
  import { isEmptyContent } from "../lexical.mjs";
8
8
  import ShortcutPlugin from "./shortcut-plugin.mjs";
9
- import AutoClearPlugin from "./auto-clear-plugin.mjs";
9
+ import { AutoClearPlugin } from "./auto-clear-plugin.mjs";
10
10
  import useMeasure from "../hooks/measure.mjs";
11
11
  import { mergeSx } from "../utils.mjs";
12
12
  import { MAX_COMMENT_LENGTH } from "../../constants.mjs";
@@ -32,8 +32,9 @@ const Input = ({
32
32
  const [error, setError] = useState("");
33
33
  const [loading, setLoading] = useState(false);
34
34
  const [ref, { width }] = useMeasure();
35
- const isChanged = useRef(false);
35
+ const [clearKey, setClearKey] = useState(0);
36
36
  const editorConfig = useEditorConfig();
37
+ const internalEditorRef = useRef(null);
37
38
  const inSmallView = width < 700;
38
39
  const draftContent = sessionStorage.getItem(sessionKey);
39
40
  const setDraftContent = (data) => {
@@ -70,12 +71,12 @@ const Input = ({
70
71
  setError("");
71
72
  const originContent = content;
72
73
  try {
73
- isChanged.current = true;
74
- const result = await send(isNil(content) ? "" : JSON.stringify(content));
74
+ const result = await send(isNil(content) ? "" : JSON.stringify(internalEditorRef?.current?.getEditorState()));
75
75
  setContent(null);
76
76
  if (draftKey) {
77
77
  setDraftContent(null);
78
78
  }
79
+ setClearKey(Date.now());
79
80
  onSuccess?.(result);
80
81
  } catch (e) {
81
82
  setContent(originContent);
@@ -83,9 +84,6 @@ const Input = ({
83
84
  console.error(e);
84
85
  } finally {
85
86
  setLoading(false);
86
- setTimeout(() => {
87
- isChanged.current = false;
88
- }, 300);
89
87
  }
90
88
  };
91
89
  const handleCmdEnterPressed = () => {
@@ -129,14 +127,19 @@ const Input = ({
129
127
  return /* @__PURE__ */ createElement(Box, { ref, ...rest, key: draftKey, sx: mergedSx }, /* @__PURE__ */ jsx(EditorConfigProvider, { value: mergedEditorConfig, children: /* @__PURE__ */ jsxs(
130
128
  BlockletEditor,
131
129
  {
132
- editorRef,
130
+ editorRef: (editor) => {
131
+ internalEditorRef.current = editor;
132
+ if (editorRef) {
133
+ editorRef.current = editor;
134
+ }
135
+ },
133
136
  placeholder,
134
137
  editorState,
135
138
  onChange: handleChange,
136
139
  autoFocus,
137
140
  children: [
138
141
  !disableCmdEnter && /* @__PURE__ */ jsx(ShortcutPlugin, { shortcut, callback: handleCmdEnterPressed }),
139
- /* @__PURE__ */ jsx(AutoClearPlugin, { value: content, isChanged: isChanged.current }),
142
+ /* @__PURE__ */ jsx(AutoClearPlugin, { clearKey }),
140
143
  children,
141
144
  renderInnerFooter?.({ submit, content, loading, error, inSmallView })
142
145
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/discuss-kit-ux",
3
- "version": "2.5.19",
3
+ "version": "2.5.20",
4
4
  "files": [
5
5
  "dist"
6
6
  ],
@@ -47,8 +47,8 @@
47
47
  "unstated-next": "^1.1.0",
48
48
  "url-join": "^4.0.1",
49
49
  "zustand": "^4.5.5",
50
- "@blocklet/editor": "2.5.19",
51
- "@blocklet/labels": "2.5.19"
50
+ "@blocklet/editor": "2.5.20",
51
+ "@blocklet/labels": "2.5.20"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "@arcblock/did-connect-react": "^3.1.5",