Skip to content

Commit

Permalink
feat: upload images from fs not URLs (#335)
Browse files Browse the repository at this point in the history
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
Co-authored-by: sidemt <25644062+sidemt@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 20, 2023
1 parent 2008e53 commit 866851e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 22 deletions.
12 changes: 11 additions & 1 deletion apps/backend/config/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ module.exports = [
"strapi::poweredBy",
"strapi::logger",
"strapi::query",
"strapi::body",
"strapi::session",
"strapi::favicon",
"strapi::public",
{
name: "strapi::body",
config: {
formLimit: "256mb",
jsonLimit: "256mb",
textLimit: "256mb",
formidable: {
maxFileSize: 250 * 1024 * 1024,
},
},
},
];
9 changes: 9 additions & 0 deletions apps/backend/config/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = () => ({
upload: {
config: {
provider: "local",
// 250MB
sizeLimit: 250 * 1024 * 1024,
},
},
});
1 change: 1 addition & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@strapi/plugin-i18n": "4.15.0",
"@strapi/plugin-users-permissions": "4.15.4",
"@strapi/provider-email-nodemailer": "^4.12.4",
"@strapi/provider-upload-local": "^4.15.4",
"@strapi/strapi": "4.15.4",
"nanoid": "^3.3.6",
"pg": "^8.11.3",
Expand Down
76 changes: 55 additions & 21 deletions apps/frontend/src/components/tiptap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,12 @@ import Placeholder from "@tiptap/extension-placeholder";
import Link from "@tiptap/extension-link";
import { EditorContent, useEditor } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
import { useCallback } from "react";
import Youtube from "@tiptap/extension-youtube";
import { Markdown } from "tiptap-markdown";
import Code from "@tiptap/extension-code";
import CharacterCount from "@tiptap/extension-character-count";

function ToolBar({ editor }) {
const addImage = useCallback(() => {
const url = window.prompt("URL");

if (url) {
editor.chain().focus().setImage({ src: url, alt: "" }).run();
}
}, [editor]);

function ToolBar({ editor, user }) {
const addYoutubeEmbed = () => {
const url = window.prompt("URL");

Expand All @@ -61,6 +52,35 @@ function ToolBar({ editor }) {
}
};

const handleImageSubmit = async (event) => {
event.preventDefault();

const apiBase = process.env.NEXT_PUBLIC_STRAPI_BACKEND_URL;

const formData = new FormData();
const image = document.getElementById("feature-image").files;

// Handle the case where the user opts not to submit an image.
if (!image) return;
formData.append("files", image[0]);

const res = await fetch(new URL("api/upload", apiBase), {
method: "post",
headers: {
Accept: "application/json",
Authorization: `Bearer ${user.jwt}`,
},
body: formData,
});

const data = await res.json();

editor.commands.setImage({
src: new URL(data[0].url, apiBase),
alt: "image",
});
};

return (
<Box
display="flex"
Expand Down Expand Up @@ -194,15 +214,26 @@ function ToolBar({ editor }) {
leftIcon={<FontAwesomeIcon icon={faListOl} />}
/>
<div className="vl"></div>
<Button
variant="ghost"
iconSpacing={0}
p={2}
title="add an image"
aria-label="add an image"
leftIcon={<FontAwesomeIcon icon={faImage} />}
onClick={() => addImage()}
/>
<label htmlFor="feature-image" className="custom-file-upload">
<Button
type="button"
variant="ghost"
iconSpacing={0}
p={2}
title="Add an image"
aria-label="Add an image"
leftIcon={<FontAwesomeIcon icon={faImage} />}
onClick={() => document.getElementById("feature-image").click()}
/>
</label>
<form id="choose-image-form" onChange={handleImageSubmit}>
<input
type="file"
id="feature-image"
accept="image/*"
style={{ display: "none" }}
/>{" "}
</form>
<Menu>
<MenuButton
as={Button}
Expand Down Expand Up @@ -230,7 +261,7 @@ function ToolBar({ editor }) {
);
}

const Tiptap = ({ handleContentChange, content }) => {
const Tiptap = ({ handleContentChange, user, content }) => {
const editor = useEditor({
extensions: [
StarterKit.configure({
Expand All @@ -249,6 +280,9 @@ const Tiptap = ({ handleContentChange, content }) => {
}),
Image.configure({
inline: true,
HTMLAttributes: {
class: "add-image-form",
},
}),
Youtube.configure({
width: 480,
Expand Down Expand Up @@ -283,7 +317,7 @@ const Tiptap = ({ handleContentChange, content }) => {

return (
<>
<ToolBar editor={editor} />
<ToolBar editor={editor} user={user} />
<Prose>
<EditorContent editor={editor} />
</Prose>
Expand Down
5 changes: 5 additions & 0 deletions apps/frontend/src/styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@
height: 2rem;
margin: 0 0.5rem;
}

.add-image-form {
width: 40%;
height: 40%;
}
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 866851e

Please sign in to comment.