@bbki.ng/site 2.0.10 → 2.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/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
export const useClipboardContent = () => {
|
|
4
|
+
const [clipboardContent, setClipboardContent] = React.useState<string | null>(null);
|
|
5
|
+
|
|
6
|
+
React.useEffect(() => {
|
|
7
|
+
const handlePaste = (event: ClipboardEvent) => {
|
|
8
|
+
if (event.clipboardData) {
|
|
9
|
+
setClipboardContent(event.clipboardData.getData("text/plain"));
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
document.addEventListener("paste", handlePaste);
|
|
14
|
+
|
|
15
|
+
return () => {
|
|
16
|
+
document.removeEventListener("paste", handlePaste);
|
|
17
|
+
};
|
|
18
|
+
}, []);
|
|
19
|
+
|
|
20
|
+
return clipboardContent;
|
|
21
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {useClipboardContent} from "@/hooks/use_clipboard_content";
|
|
2
|
+
import {useSWRConfig} from "swr";
|
|
3
|
+
import {useContext, useEffect} from "react";
|
|
4
|
+
import {GlobalLoadingContext} from "@/context/global_loading_state_provider";
|
|
5
|
+
import {usePost} from "@/hooks/use_post";
|
|
6
|
+
import {API} from "@/constants/routes";
|
|
7
|
+
|
|
8
|
+
export const useClipboardToPost = () => {
|
|
9
|
+
const clipboardContent = useClipboardContent();
|
|
10
|
+
|
|
11
|
+
const { mutate } = useSWRConfig();
|
|
12
|
+
|
|
13
|
+
const { setIsLoading } = useContext(GlobalLoadingContext);
|
|
14
|
+
|
|
15
|
+
const post = usePost();
|
|
16
|
+
|
|
17
|
+
const firstLine = clipboardContent ? clipboardContent.split("\n")[0] : "";
|
|
18
|
+
const title = firstLine ? firstLine.trim() : "";
|
|
19
|
+
|
|
20
|
+
const restContent = clipboardContent ? clipboardContent.slice(title.length).trim() : "";
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (!restContent || !title) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
setIsLoading(true);
|
|
29
|
+
|
|
30
|
+
post(title, restContent)
|
|
31
|
+
.then((r) => {
|
|
32
|
+
mutate(API.POSTS).then();
|
|
33
|
+
})
|
|
34
|
+
.finally(() => {
|
|
35
|
+
setIsLoading(false);
|
|
36
|
+
});
|
|
37
|
+
}, [title, restContent]);
|
|
38
|
+
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
@@ -7,6 +7,7 @@ import { useAuthed } from "@/hooks/use_authed";
|
|
|
7
7
|
import { useFile2Post } from "@/hooks/use_file_to_post";
|
|
8
8
|
import { DelayFadeIn } from "@/components/DelayFadeIn/DelayFadeIn";
|
|
9
9
|
import { useSafeArticleLoading } from "@/hooks/use_safe_loading";
|
|
10
|
+
import {useClipboardToPost} from "@/hooks/use_clipboard_to_post";
|
|
10
11
|
|
|
11
12
|
type TxtProps = {
|
|
12
13
|
title?: string;
|
|
@@ -40,6 +41,8 @@ export default (props: TxtProps) => {
|
|
|
40
41
|
const reader = useFile2Post();
|
|
41
42
|
const isKing = useAuthed();
|
|
42
43
|
|
|
44
|
+
useClipboardToPost();
|
|
45
|
+
|
|
43
46
|
return (
|
|
44
47
|
<DropZone onDrop={reader} disabled={!isKing}>
|
|
45
48
|
<Posts {...props} />
|