-
Notifications
You must be signed in to change notification settings - Fork 82
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 #1992 from ThemeFuse/22690-Custom-SVG-For-Icon
Added custom icons in editor
- Loading branch information
Showing
8 changed files
with
283 additions
and
7 deletions.
There are no files selected for viewing
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,62 @@ | ||
import { deleteIcon as apiDeleteIcon, getCustomIcons } from "../api"; | ||
import { CustomIcon, IconData } from "../types/Icon"; | ||
import { createUpload } from "../utils/createUpload"; | ||
import { t } from "../utils/i18n"; | ||
import { uploadIcon } from "./utils"; | ||
|
||
export const customIcon: CustomIcon = { | ||
async get(res, rej) { | ||
try { | ||
const icons = await getCustomIcons(); | ||
|
||
const normalizedIcons = icons.map(({ uid }) => ({ | ||
name: uid | ||
})); | ||
|
||
res(normalizedIcons); | ||
} catch (e) { | ||
rej(t("Failed to load icons")); | ||
} | ||
}, | ||
|
||
async add(res, rej, { acceptedExtensions = [".svg", ".ico"] }) { | ||
try { | ||
const icons = await createUpload({ | ||
accept: acceptedExtensions?.join(","), | ||
multiple: true | ||
}); | ||
|
||
const uploadedIcons = await Promise.all( | ||
icons.map( | ||
(icon) => | ||
new Promise<IconData>((resolve) => { | ||
uploadIcon( | ||
icon, | ||
({ uid }) => { | ||
resolve({ | ||
name: uid | ||
}); | ||
}, | ||
() => { | ||
rej(t("Failed to upload icon")); | ||
} | ||
); | ||
}) | ||
) | ||
); | ||
|
||
res(uploadedIcons); | ||
} catch (e) { | ||
rej(t("Icon upload cancelled")); | ||
} | ||
}, | ||
|
||
async delete(res, rej, { uid }) { | ||
try { | ||
await apiDeleteIcon(uid); | ||
res(uid); | ||
} catch (e) { | ||
rej(t("Failed to delete icon")); | ||
} | ||
} | ||
}; |
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,11 @@ | ||
import { uploadIcon as apiUploadIcon } from "../api"; | ||
import { UploadIconData } from "../types/Icon"; | ||
import { handleFileUpload } from "../utils/uploadFile"; | ||
|
||
export const uploadIcon: UploadIconData = (icon, onUpload, onError) => { | ||
handleFileUpload(icon, { | ||
upload: apiUploadIcon, | ||
onUpload, | ||
onError | ||
}); | ||
}; |
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,46 @@ | ||
import { throwOnNullish } from "@/utils/throwOnNullish"; | ||
import { Obj, pipe, Str } from "@brizy/readers"; | ||
import { mPipe } from "fp-utilities"; | ||
import { Response } from "./Response"; | ||
|
||
export interface IconUploadData { | ||
uid: string; | ||
} | ||
|
||
export interface IconData { | ||
name: string; | ||
} | ||
|
||
export type UploadIconData = ( | ||
icon: File, | ||
onUpload: (uploadData: IconUploadData) => void, | ||
onError: VoidFunction | ||
) => void; | ||
|
||
export interface AddIconExtra { | ||
acceptedExtensions?: Array<string>; | ||
} | ||
|
||
export interface DeleteIconExtra { | ||
uid: string; | ||
} | ||
|
||
export interface CustomIcon { | ||
get: (res: Response<IconData[]>, rej: Response<string>) => void; | ||
add: ( | ||
res: Response<IconData[]>, | ||
rej: Response<string>, | ||
extra: AddIconExtra | ||
) => void; | ||
delete: ( | ||
res: Response<string>, | ||
rej: Response<string>, | ||
extra: DeleteIconExtra | ||
) => void; | ||
} | ||
|
||
export const readIconUrl = (url: string) => | ||
pipe( | ||
mPipe(Obj.readKey("customIcon"), Obj.read, Obj.readKey(url), Str.read), | ||
throwOnNullish(`Invalid API: ${url}`) | ||
); |
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,10 @@ | ||
export async function getBase64( | ||
file: File | ||
): Promise<string | ArrayBuffer | null> { | ||
return new Promise((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.readAsDataURL(file); | ||
reader.onload = () => resolve(reader.result); | ||
reader.onerror = (error) => reject(error); | ||
}); | ||
} |
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,34 @@ | ||
import { UploadData } from "../types/File"; | ||
import { getBase64 } from "./base64"; | ||
import { t } from "./i18n"; | ||
|
||
interface Options<T> { | ||
upload: (attachment: string, filename: string) => Promise<T>; | ||
onUpload: (uploadData: T) => void; | ||
onError: (e: unknown) => void; | ||
componentId?: string; | ||
} | ||
|
||
export const handleFileUpload = <T = UploadData>( | ||
File: File, | ||
{ upload, onUpload, onError }: Options<T> | ||
): void => { | ||
Promise.resolve(File) | ||
.then(getBase64) | ||
.then((base64) => { | ||
if (typeof base64 !== "string") { | ||
throw { | ||
status: 406, | ||
message: t("Extension is not accepted") | ||
}; | ||
} | ||
|
||
const attachment = base64.replace(/.+;base64,/, ""); | ||
|
||
return upload(attachment, File.name).then((res) => { | ||
return res; | ||
}); | ||
}) | ||
.then(onUpload) | ||
.catch(onError); | ||
}; |