mirror of
https://github.com/ant-design/ant-design.git
synced 2025-06-17 23:50:52 +08:00

* feat: 用原生方法重写copy * feat: 优化copy方法,并修改test * feat: 优化copy方法,并修改test * feat: 修改copy逻辑,并恢复使用到的单测 * 调整格式。恢复不需要改动的格式 * 控制台提示使用util封装的 --------- Co-authored-by: 刘欢 <lh01217311@antgroup.com>
29 lines
711 B
TypeScript
29 lines
711 B
TypeScript
import warning from './warning';
|
|
|
|
function copy(text: string, config?: { format?: 'text/plain' | 'text/html' }) {
|
|
const format = config?.format;
|
|
|
|
if (typeof text !== 'string') {
|
|
warning(false, 'The clipboard content must be of string type', '');
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
if (format === 'text/html') {
|
|
const item = new ClipboardItem({
|
|
[format]: new Blob([text], { type: format }),
|
|
'text/plain': new Blob([text], { type: 'text/plain' }),
|
|
});
|
|
navigator.clipboard.write([item]);
|
|
} else {
|
|
navigator.clipboard.writeText(text);
|
|
}
|
|
|
|
return true;
|
|
} catch (err) {
|
|
warning(false, 'Clipboard API failed:', String(err));
|
|
}
|
|
}
|
|
|
|
export default copy;
|