-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathuseCopySelectionHelper.js
34 lines (31 loc) · 1.09 KB
/
useCopySelectionHelper.js
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
import {useEffect} from 'react';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import CONST from '../CONST';
import KeyboardShortcut from '../libs/KeyboardShortcut';
import Clipboard from '../libs/Clipboard';
import SelectionScraper from '../libs/SelectionScraper';
function copySelectionToClipboard() {
const selection = SelectionScraper.getCurrentSelection();
if (!selection) {
return;
}
const parser = new ExpensiMark();
if (!Clipboard.canSetHtml()) {
Clipboard.setString(parser.htmlToMarkdown(selection));
return;
}
Clipboard.setHtml(selection, parser.htmlToText(selection));
}
export default function useCopySelectionHelper() {
useEffect(() => {
const copyShortcutConfig = CONST.KEYBOARD_SHORTCUTS.COPY;
const unsubscribeCopyShortcut = KeyboardShortcut.subscribe(
copyShortcutConfig.shortcutKey,
copySelectionToClipboard,
copyShortcutConfig.descriptionKey,
copyShortcutConfig.modifiers,
false,
);
return unsubscribeCopyShortcut;
}, []);
}