forked from OtterMind/Chat2DB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseFocusData.ts
More file actions
49 lines (45 loc) · 1.56 KB
/
Copy pathuseFocusData.ts
File metadata and controls
49 lines (45 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { useEffect } from 'react';
import { useCommonStore } from '@/store/common';
import { tableCopy, copy } from '@/utils'
import { setFocusedContent } from '@/store/common/copyFocusedContent';
// 如果用户点击的不是可复制的元素,就清空选中的内容
function useCopyFocusData() {
const { focusedContent } = useCommonStore((state) => {
return {
focusedContent: state.focusedContent
}
});
// 注册快捷键监听cmd+c或ctrl+c复制focusedContent
useEffect(() => {
const handleCopy = (e: KeyboardEvent) => {
if (e.key === 'c' && (e.metaKey || e.ctrlKey)) {
if (!focusedContent) return
// 如果是数据是数组,就调用tableCopy
if (Array.isArray(focusedContent)) {
tableCopy(focusedContent as any)
return
}
copy(focusedContent as any);
}
};
document.addEventListener('keydown', handleCopy);
return () => {
document.removeEventListener('keydown', handleCopy);
};
}, [focusedContent]);
useEffect(() => {
const handleClick = (event) => {
const targetElement = event.target as Element;
if (!targetElement.closest('[data-chat2db-general-can-copy-element]')) {
setFocusedContent(null)
}
};
document.addEventListener('click', handleClick);
document.addEventListener('contextmenu', handleClick);
return () => {
document.removeEventListener('click', handleClick);
document.removeEventListener('contextmenu', handleClick);
};
}, [focusedContent]);
}
export default useCopyFocusData;