refactor: fix type errors of MkCropperDialog

This commit is contained in:
Lhcfl 2024-04-11 00:14:36 +08:00
parent 18ba024cbb
commit f275fc9cdf
3 changed files with 40 additions and 29 deletions

View file

@ -68,40 +68,48 @@ let cropper: Cropper | null = null;
const loading = ref(true); const loading = ref(true);
const ok = async () => { const ok = async () => {
const promise = new Promise<entities.DriveFile>(async (res) => { async function UploadCroppedImg(): Promise<entities.DriveFile> {
const croppedCanvas = await cropper?.getCropperSelection()?.$toCanvas(); const croppedCanvas = await cropper?.getCropperSelection()?.$toCanvas();
croppedCanvas.toBlob((blob) => {
const formData = new FormData();
formData.append("file", blob);
if (defaultStore.state.uploadFolder) {
formData.append("folderId", defaultStore.state.uploadFolder);
}
fetch(apiUrl + "/drive/files/create", { const blob = await new Promise<Blob | null>((resolve) =>
method: "POST", croppedCanvas!.toBlob((blob) => resolve(blob)),
body: formData, );
headers: {
authorization: `Bearer ${me.token}`, // MDN says `null` may be passed if the image cannot be created for any reason.
}, // But I don't think this is reachable for normal case.
}) if (blob == null) {
.then((response) => response.json()) throw "Cropping image failed.";
.then((f) => { }
res(f);
}); const formData = new FormData();
formData.append("file", blob);
if (defaultStore.state.uploadFolder) {
formData.append("folderId", defaultStore.state.uploadFolder);
}
const response = await fetch(`${apiUrl}/drive/files/create`, {
method: "POST",
body: formData,
headers: {
authorization: `Bearer ${me!.token}`,
},
}); });
}); return await response.json();
}
const promise = UploadCroppedImg();
os.promiseDialog(promise); os.promiseDialog(promise);
const f = await promise; const f = await promise;
emit("ok", f); emit("ok", f);
dialogEl.value.close(); dialogEl.value!.close();
}; };
const cancel = () => { const cancel = () => {
emit("cancel"); emit("cancel");
dialogEl.value.close(); dialogEl.value!.close();
}; };
const onImageLoad = () => { const onImageLoad = () => {
@ -114,7 +122,7 @@ const onImageLoad = () => {
}; };
onMounted(() => { onMounted(() => {
cropper = new Cropper(imgEl.value, {}); cropper = new Cropper(imgEl.value!, {});
const computedStyle = getComputedStyle(document.documentElement); const computedStyle = getComputedStyle(document.documentElement);
@ -127,13 +135,13 @@ onMounted(() => {
selection.outlined = true; selection.outlined = true;
window.setTimeout(() => { window.setTimeout(() => {
cropper.getCropperImage()!.$center("contain"); cropper!.getCropperImage()!.$center("contain");
selection.$center(); selection.$center();
}, 100); }, 100);
// 調 // 調
window.setTimeout(() => { window.setTimeout(() => {
cropper.getCropperImage()!.$center("contain"); cropper!.getCropperImage()!.$center("contain");
selection.$center(); selection.$center();
}, 500); }, 500);
}); });

View file

@ -54,7 +54,10 @@
</button> </button>
</div> </div>
<div class="body"> <div class="body">
<slot></slot> <slot
:width="width"
:height="height"
></slot>
</div> </div>
</div> </div>
</FocusTrap> </FocusTrap>

View file

@ -125,12 +125,12 @@ export const apiWithDialog = ((
return promise; return promise;
}) as typeof api; }) as typeof api;
export function promiseDialog<T extends Promise<any>>( export function promiseDialog<T>(
promise: T, promise: Promise<T>,
onSuccess?: ((res: any) => void) | null, onSuccess?: ((res: T) => void) | null,
onFailure?: ((err: Error) => void) | null, onFailure?: ((err: Error) => void) | null,
text?: string, text?: string,
): T { ): Promise<T> {
const showing = ref(true); const showing = ref(true);
const success = ref(false); const success = ref(false);