mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 04:48:53 +00:00
refactor: workspace manager (#5060)
This commit is contained in:
@@ -4,11 +4,9 @@ import {
|
||||
TRACE_ID_BYTES,
|
||||
traceReporter,
|
||||
} from '@affine/graphql';
|
||||
import { refreshRootMetadataAtom } from '@affine/workspace/atom';
|
||||
import { getCurrentStore } from '@toeverything/infra/atom';
|
||||
import { CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY } from '@affine/workspace';
|
||||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
|
||||
import { signIn, signOut } from 'next-auth/react';
|
||||
import { startTransition } from 'react';
|
||||
|
||||
type TraceParams = {
|
||||
startTime: string;
|
||||
@@ -91,10 +89,9 @@ export const signOutCloud: typeof signOut = async options => {
|
||||
})
|
||||
.then(result => {
|
||||
if (result) {
|
||||
startTransition(() => {
|
||||
localStorage.removeItem('last_workspace_id');
|
||||
getCurrentStore().set(refreshRootMetadataAtom);
|
||||
});
|
||||
new BroadcastChannel(
|
||||
CLOUD_WORKSPACE_CHANGED_BROADCAST_CHANNEL_KEY
|
||||
).postMessage(1);
|
||||
}
|
||||
return onResolveHandleTrace(result, traceParams);
|
||||
})
|
||||
|
||||
42
packages/frontend/core/src/utils/reduce-image.ts
Normal file
42
packages/frontend/core/src/utils/reduce-image.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import reduce from 'image-blob-reduce';
|
||||
|
||||
// validate and reduce image size and return as file
|
||||
export const validateAndReduceImage = async (file: File): Promise<File> => {
|
||||
// Declare a new async function that wraps the decode logic
|
||||
const decodeAndReduceImage = async (): Promise<Blob> => {
|
||||
const img = new Image();
|
||||
const url = URL.createObjectURL(file);
|
||||
img.src = url;
|
||||
|
||||
await img.decode().catch(() => {
|
||||
URL.revokeObjectURL(url);
|
||||
throw new Error('Image could not be decoded');
|
||||
});
|
||||
|
||||
img.onload = img.onerror = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
const sizeInMB = file.size / (1024 * 1024);
|
||||
if (sizeInMB > 10 || img.width > 4000 || img.height > 4000) {
|
||||
// Compress the file to less than 10MB
|
||||
const compressedImg = await reduce().toBlob(file, {
|
||||
max: 4000,
|
||||
unsharpAmount: 80,
|
||||
unsharpRadius: 0.6,
|
||||
unsharpThreshold: 2,
|
||||
});
|
||||
return compressedImg;
|
||||
}
|
||||
|
||||
return file;
|
||||
};
|
||||
|
||||
try {
|
||||
const reducedBlob = await decodeAndReduceImage();
|
||||
|
||||
return new File([reducedBlob], file.name, { type: file.type });
|
||||
} catch (error) {
|
||||
throw new Error('Image could not be reduce :' + error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user