fix(剪贴板): 改进剪贴板操作的错误处理
在复制文本和图片到剪贴板时,捕获并返回更详细的错误信息 同时更新GlobalSnackbar组件,优化类型导入和消息显示
This commit is contained in:
@@ -35,7 +35,14 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
import React, { JSX, useState, createContext, useContext, type ReactNode } from 'react';
|
||||
import {
|
||||
JSX,
|
||||
useState,
|
||||
createContext,
|
||||
useContext,
|
||||
type ReactNode,
|
||||
type SyntheticEvent,
|
||||
} from 'react';
|
||||
import { Snackbar, Alert, type SxProps, type Theme, alpha, Portal } from '@mui/material';
|
||||
|
||||
/**
|
||||
@@ -181,7 +188,9 @@ export function GlobalSnackbar({
|
||||
>
|
||||
{message}
|
||||
</Alert>
|
||||
) : undefined}
|
||||
) : (
|
||||
<div>{message}</div>
|
||||
)}
|
||||
</Snackbar>
|
||||
</Portal>
|
||||
);
|
||||
@@ -210,7 +219,7 @@ export function useSnackbarState(initialOptions?: SnackbarOptions): UseSnackbarS
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const handleClose = (_event?: React.SyntheticEvent | Event, reason?: string) => {
|
||||
const handleClose = (_event?: SyntheticEvent | Event, reason?: string) => {
|
||||
if (reason === 'clickaway') return;
|
||||
closeMessage();
|
||||
};
|
||||
|
||||
+4
-4
@@ -10,8 +10,8 @@ export const copyTextToClipboard = async (text: string): Promise<boolean> => {
|
||||
.then(() => {
|
||||
resolve(true);
|
||||
})
|
||||
.catch(() => {
|
||||
reject(false);
|
||||
.catch((error) => {
|
||||
reject(new Error(`复制文本到剪贴板失败,错误: ${error?.message || 'Unknown error'}`));
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -32,8 +32,8 @@ export const copyImageToClipboard = async (blob: Blob): Promise<boolean> => {
|
||||
.then(() => {
|
||||
resolve(true);
|
||||
})
|
||||
.catch(() => {
|
||||
reject(false);
|
||||
.catch((error) => {
|
||||
reject(new Error(`复制图片到剪贴板失败,错误: ${error?.message || 'Unknown error'}`));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user