ant-design/components/_util/copy.ts
EmilyyyLiu a339f81c83
feat: V6 abandons copy-to-clipboard dependency (#53427)
* feat: 用原生方法重写copy

* feat: 优化copy方法,并修改test

* feat: 优化copy方法,并修改test

* feat: 修改copy逻辑,并恢复使用到的单测

* 调整格式。恢复不需要改动的格式

* 控制台提示使用util封装的

---------

Co-authored-by: 刘欢 <lh01217311@antgroup.com>
2025-04-09 16:24:47 +08:00

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;