This repository has been archived by the owner on Oct 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Dungeon-MASSters/editor
Editor
- Loading branch information
Showing
7 changed files
with
655 additions
and
89 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function DataURIToBlob(dataURI: string) { | ||
const splitDataURI = dataURI.split(","); | ||
const byteString = | ||
splitDataURI[0].indexOf("base64") >= 0 | ||
? atob(splitDataURI[1]) | ||
: decodeURI(splitDataURI[1]); | ||
const mimeString = splitDataURI[0].split(":")[1].split(";")[0]; | ||
|
||
const ia = new Uint8Array(byteString.length); | ||
for (let i = 0; i < byteString.length; i++) | ||
ia[i] = byteString.charCodeAt(i); | ||
|
||
return new Blob([ia], { type: mimeString }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { pb } from "@/lib/pb-client"; | ||
import { IconDownload, IconLoader3 } from "@tabler/icons-react"; | ||
import { useQuery } from "react-query"; | ||
|
||
export function EditedCoversPage() { | ||
const { data, isLoading } = useQuery(["get-gallery"], () => | ||
pb.collection("edited_covers").getFullList() | ||
); | ||
|
||
return ( | ||
<div> | ||
<h1 className="text-xl font-bold">Созданные обложки</h1> | ||
{isLoading ? ( | ||
<div className="flex gap-1 text-primary"> | ||
<IconLoader3 className="animate-spin" /> | ||
<span>загрузка...</span> | ||
</div> | ||
) : data && data.length > 0 ? ( | ||
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-2"> | ||
{data?.map((cover) => ( | ||
<ImageCard | ||
key={cover.id} | ||
fileURL={pb.files.getUrl(cover, cover.result)} | ||
/> | ||
))} | ||
</div> | ||
) : ( | ||
<div>Вы ещё не сохранили ни одной обложки :(</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
function ImageCard({ fileURL }: { fileURL: string }) { | ||
return ( | ||
<div className="h-52 relative"> | ||
<img | ||
className=" w-full h-full object-cover rounded-lg" | ||
src={fileURL} | ||
/> | ||
|
||
<a | ||
href={fileURL} | ||
className="absolute bottom-0 flex gap-1 p-2 text-primary-foreground bg-primary rounded-bl-lg rounded-tr-lg hover:text-secondary-foreground hover:bg-secondary" | ||
download | ||
> | ||
<IconDownload /> | ||
<span>Скачать</span> | ||
</a> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,130 @@ | ||
export function EditorPage() { | ||
import { FullscreenLoader } from "@/components/loaders"; | ||
import { pb } from "@/lib/pb-client"; | ||
import { useUser } from "@/lib/use-user"; | ||
import { IconBug } from "@tabler/icons-react"; | ||
import { useState } from "react"; | ||
import FilerobotImageEditor, { | ||
TABS, | ||
TOOLS | ||
} from "react-filerobot-image-editor"; | ||
import { useLocation } from "wouter"; | ||
|
||
export function EditorPage({ fileUrlB64 }: { fileUrlB64: string }) { | ||
const [_, navigate] = useLocation(); | ||
const user = useUser(false); | ||
const [isSaving, setIsSaving] = useState(false); | ||
const [isError, setIsError] = useState(false); | ||
|
||
const fileUrl = atob(fileUrlB64); | ||
|
||
if (user.isLoading || user.data === undefined) { | ||
return <FullscreenLoader title={"Загрузка..."} />; | ||
} | ||
|
||
if (isSaving) { | ||
return <FullscreenLoader title="Сохранение..." />; | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className="mx-auto w-fit h-[500px]"> | ||
<h1 className="font-bold text-xl">Создать обложку</h1> | ||
|
||
{isError && ( | ||
<div className="flex gap-1 text-red-500"> | ||
<IconBug /> | ||
<span>Не получилось сохранить изображение</span> | ||
</div> | ||
)} | ||
|
||
<FilerobotImageEditor | ||
source={fileUrl} | ||
savingPixelRatio={4} | ||
previewPixelRatio={window.devicePixelRatio} | ||
onBeforeSave={() => { | ||
return false; | ||
}} | ||
onSave={(editedImageObject, _) => { | ||
setIsSaving(true); | ||
|
||
editedImageObject.imageCanvas?.toBlob((blob) => { | ||
if (blob === null) { | ||
setIsError(true); | ||
return; | ||
} | ||
|
||
const formData = new FormData(); | ||
formData.append("created_by", user.data.record.id); | ||
formData.append( | ||
"result", | ||
blob, | ||
editedImageObject.fullName ?? "cover.png" | ||
); | ||
|
||
pb.collection("edited_covers") | ||
.create(formData) | ||
.then(() => { | ||
console.log("saved"); | ||
navigate("/gallery"); | ||
}) | ||
.catch(() => setIsError(true)); | ||
}); | ||
}} | ||
onClose={() => {}} | ||
annotationsCommon={{ | ||
fill: "#ffffff" | ||
}} | ||
Text={{ | ||
text: "Обложкер", | ||
fontSize: 32, | ||
fontStyle: "bold", | ||
align: "center" | ||
}} | ||
Rotate={{ angle: 90, componentType: "slider" }} | ||
Crop={{ | ||
presetsItems: [ | ||
{ | ||
titleKey: "classicTv", | ||
descriptionKey: "4:3", | ||
ratio: 4 / 3 | ||
// icon: CropClassicTv, // optional, CropClassicTv is a React Function component. Possible (React Function component, string or HTML Element) | ||
}, | ||
{ | ||
titleKey: "cinemascope", | ||
descriptionKey: "21:9", | ||
ratio: 21 / 9 | ||
// icon: CropCinemaScope, // optional, CropCinemaScope is a React Function component. Possible (React Function component, string or HTML Element) | ||
} | ||
], | ||
presetsFolders: [ | ||
{ | ||
titleKey: "socialMedia", // will be translated into Social Media as backend contains this translation key | ||
// icon: Social, // optional, Social is a React Function component. Possible (React Function component, string or HTML Element) | ||
groups: [ | ||
{ | ||
titleKey: "facebook", | ||
items: [ | ||
{ | ||
titleKey: "profile", | ||
width: 180, | ||
height: 180, | ||
descriptionKey: "fbProfileSize" | ||
}, | ||
{ | ||
titleKey: "coverPhoto", | ||
width: 820, | ||
height: 312, | ||
descriptionKey: "fbCoverPhotoSize" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
}} | ||
tabsIds={[TABS.ADJUST, TABS.ANNOTATE]} // or {['Adjust', 'Annotate', 'Watermark']} | ||
defaultTabId={TABS.ANNOTATE} // or 'Annotate' | ||
defaultToolId={TOOLS.TEXT} // or 'Text' | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters