Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+16
-70
@@ -1,15 +1,8 @@
|
||||
import { EditorView } from "@tiptap/pm/view";
|
||||
import { t as i18nT } from "i18next";
|
||||
import { requestScopedT as i18nT } from "~/locale/request-scoped-t";
|
||||
import { toast } from "sonner";
|
||||
import { apiPath } from "~/utils/app/CanonicalUrlUtils";
|
||||
|
||||
export type ImageUploadOptions = {
|
||||
apiBaseUrl?: string;
|
||||
tenantSlug?: string | null;
|
||||
tenantId?: string | null;
|
||||
};
|
||||
|
||||
export const handleImageUpload = (file: File, view: EditorView, event: ClipboardEvent | DragEvent | Event, options: ImageUploadOptions = {}) => {
|
||||
export const handleImageUpload = (file: File, view: EditorView, event: ClipboardEvent | DragEvent | Event) => {
|
||||
// check if the file is an image
|
||||
if (!file.type.includes("image/")) {
|
||||
toast.error(i18nT("novel.editor.upload.unsupportedType"));
|
||||
@@ -18,69 +11,22 @@ export const handleImageUpload = (file: File, view: EditorView, event: Clipboard
|
||||
} else if (file.size / 1024 / 1024 > 50) {
|
||||
toast.error(i18nT("novel.editor.upload.tooLarge"));
|
||||
} else {
|
||||
// const reader = new FileReader();
|
||||
// reader.onload = (e) => {
|
||||
// const { schema } = view.state;
|
||||
// const node = schema.nodes.image.create({
|
||||
// src: e.target?.result,
|
||||
// alt: file,
|
||||
// title: file.name,
|
||||
// }); // creates the image element
|
||||
// const transaction = view.state.tr.replaceSelectionWith(node);
|
||||
// view.dispatch(transaction);
|
||||
// };
|
||||
// reader.readAsDataURL(file);
|
||||
|
||||
const tenantId = options.tenantId ?? "";
|
||||
const uploadUrl = options.apiBaseUrl && tenantId ? new URL(apiPath("ai/upload", { mode: "host-aware" }), options.apiBaseUrl).toString() : null;
|
||||
if (!uploadUrl) {
|
||||
toast.error(i18nT("novel.editor.upload.unavailable"));
|
||||
return;
|
||||
}
|
||||
|
||||
toast.promise(
|
||||
fetch(uploadUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": file?.type || "application/octet-stream",
|
||||
"x-filename": file?.name || "image.png",
|
||||
"x-tenant-id": tenantId,
|
||||
},
|
||||
body: file,
|
||||
}).then(async (res) => {
|
||||
// Successfully uploaded image
|
||||
if (res.status === 200) {
|
||||
const { url } = await res.json();
|
||||
// preload the image
|
||||
let image = new Image();
|
||||
image.src = url;
|
||||
image.onload = () => {
|
||||
insertImage(url);
|
||||
};
|
||||
|
||||
// No blob store configured
|
||||
} else if (res.status === 401) {
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
insertImage(e.target?.result as string);
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
throw new Error(i18nT("novel.editor.upload.localFallback"));
|
||||
|
||||
// Unknown error
|
||||
} else {
|
||||
throw new Error(i18nT("novel.editor.upload.error"));
|
||||
}
|
||||
}),
|
||||
{
|
||||
loading: i18nT("novel.editor.upload.loading"),
|
||||
success: i18nT("novel.editor.upload.success"),
|
||||
error: (e) => e.message,
|
||||
const reader = new FileReader();
|
||||
reader.onload = (readerEvent) => {
|
||||
const dataUrl = readerEvent.target?.result;
|
||||
if (typeof dataUrl === "string") {
|
||||
insertImage(dataUrl);
|
||||
return;
|
||||
}
|
||||
);
|
||||
toast.error(i18nT("novel.editor.upload.error"));
|
||||
};
|
||||
reader.onerror = () => {
|
||||
toast.error(i18nT("novel.editor.upload.error"));
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
|
||||
const insertImage = (url: string) => {
|
||||
function insertImage(url: string) {
|
||||
// for paste events
|
||||
if (event instanceof ClipboardEvent) {
|
||||
return view.dispatch(
|
||||
@@ -120,5 +66,5 @@ export const handleImageUpload = (file: File, view: EditorView, event: Clipboard
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user