Track HiveGrid Cloud planning reference

This commit is contained in:
david committed 2026-06-26 20:48:01 +02:00
1 parent b3d5ea6764
commit e341c5938a
5784 files changed
+1104929 -3

No files matched your search

@@ -0,0 +1,124 @@
import { EditorView } from "@tiptap/pm/view";
import { t as i18nT } from "i18next";
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 = {}) => {
// check if the file is an image
if (!file.type.includes("image/")) {
toast.error(i18nT("novel.editor.upload.unsupportedType"));
// check if the file size is less than 50MB
} 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 insertImage = (url: string) => {
// for paste events
if (event instanceof ClipboardEvent) {
return view.dispatch(
view.state.tr.replaceSelectionWith(
view.state.schema.nodes.image.create({
src: url,
alt: file.name,
title: file.name,
})
)
);
// for drag and drop events
} else if (event instanceof DragEvent) {
const { schema } = view.state;
const coordinates = view.posAtCoords({
left: event.clientX,
top: event.clientY,
});
const node = schema.nodes.image.create({
src: url,
alt: file.name,
title: file.name,
}); // creates the image element
const transaction = view.state.tr.insert(coordinates?.pos || 0, node); // places it in the correct position
return view.dispatch(transaction);
// for input upload events
} else if (event instanceof Event) {
return view.dispatch(
view.state.tr.replaceSelectionWith(
view.state.schema.nodes.image.create({
src: url,
alt: file.name,
title: file.name,
})
)
);
}
};
};