Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support pasting image in the textfield #228

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/components/dashboard/UploadDropzone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import LoadingSpinner from "../ui/spinner";
import { toast } from "../ui/use-toast";
import BookmarkAlreadyExistsToast from "../utils/BookmarkAlreadyExistsToast";

function useUploadAsset() {
export function useUploadAsset() {
const { mutateAsync: createBookmark } = useCreateBookmarkWithPostHook({
onSuccess: (resp) => {
if (resp.alreadyExists) {
Expand Down
30 changes: 29 additions & 1 deletion apps/web/components/dashboard/bookmarks/EditorCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { z } from "zod";

import { useCreateBookmarkWithPostHook } from "@hoarder/shared-react/hooks/bookmarks";

import { useUploadAsset } from "../UploadDropzone";

function useFocusOnKeyPress(inputRef: React.RefObject<HTMLTextAreaElement>) {
useEffect(() => {
function handleKeyPress(e: KeyboardEvent) {
Expand Down Expand Up @@ -74,6 +76,8 @@ export default function EditorCard({ className }: { className?: string }) {
},
});

const uploadAsset = useUploadAsset();

function tryToImportUrls(text: string): void {
const lines = text.split("\n");
const urls: URL[] = [];
Expand Down Expand Up @@ -118,6 +122,23 @@ export default function EditorCard({ className }: { className?: string }) {
list: undefined,
});

const handlePaste = async (
event: React.ClipboardEvent<HTMLTextAreaElement>,
) => {
if (event?.clipboardData?.items) {
await Promise.all(
Array.from(event.clipboardData.items)
.filter((item) => item?.type?.startsWith("image"))
.map((item) => {
const blob = item.getAsFile();
if (blob) {
return uploadAsset(blob);
}
}),
);
}
};

return (
<Form {...form}>
<form
Expand All @@ -144,7 +165,7 @@ export default function EditorCard({ className }: { className?: string }) {
disabled={isPending}
className="h-full w-full resize-none border-none text-lg focus-visible:ring-0"
placeholder={
"Paste a link, write a note or drag and drop an image in here ..."
"Paste a link or an image, write a note or drag and drop an image in here ..."
}
onKeyDown={(e) => {
if (demoMode) {
Expand All @@ -154,6 +175,13 @@ export default function EditorCard({ className }: { className?: string }) {
form.handleSubmit(onSubmit, onError)();
}
}}
onPaste={(e) => {
e.preventDefault();
if (demoMode) {
return;
}
handlePaste(e);
}}
{...textFieldProps}
/>
</FormControl>
Expand Down
Loading