Skip to content

Commit

Permalink
Merge pull request #22808 from jczekalski/migrate-copy-selection-helper
Browse files Browse the repository at this point in the history
Migrate CopySelectionHelper.js from class component to hook
  • Loading branch information
Gonals authored Jul 18, 2023
2 parents ffe659e + 12d752b commit 7fba181
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 48 deletions.
46 changes: 0 additions & 46 deletions src/components/CopySelectionHelper.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/hooks/useCopySelectionHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}, []);
}
5 changes: 3 additions & 2 deletions src/pages/home/report/ReportActionsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import Timing from '../../../libs/actions/Timing';
import CONST from '../../../CONST';
import compose from '../../../libs/compose';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import useCopySelectionHelper from '../../../hooks/useCopySelectionHelper';
import useReportScrollManager from '../../../hooks/useReportScrollManager';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import Performance from '../../../libs/Performance';
import {withNetwork} from '../../../components/OnyxProvider';
import FloatingMessageCounter from './FloatingMessageCounter';
import networkPropTypes from '../../../components/networkPropTypes';
import ReportActionsList from './ReportActionsList';
import CopySelectionHelper from '../../../components/CopySelectionHelper';
import * as ReportActionsUtils from '../../../libs/ReportActionsUtils';
import * as ReportUtils from '../../../libs/ReportUtils';
import reportPropTypes from '../../reportPropTypes';
Expand Down Expand Up @@ -61,6 +61,8 @@ const defaultProps = {
function ReportActionsView(props) {
const context = useContext(ReportScreenContext);

useCopySelectionHelper();

const reportScrollManager = useReportScrollManager();

const didLayout = useRef(false);
Expand Down Expand Up @@ -340,7 +342,6 @@ function ReportActionsView(props) {
ref={context.reactionListRef}
report={props.report}
/>
<CopySelectionHelper />
</>
);
}
Expand Down

0 comments on commit 7fba181

Please sign in to comment.