Skip to content

Commit

Permalink
refactor: Extract shared copy sequence logic to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
theosanderson committed Dec 12, 2024
1 parent c1b055d commit 338d4f5
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions gensplore-component/src/components/GensploreView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,8 @@ function GensploreView({ genbankString, searchInput, setSearchInput, showLogo })
const handleKeyDown = (e) => {
// ctrl-C
if ((e.ctrlKey || e.metaKey) && e.keyCode === 67) {
const selStart = Math.min(whereMouseWentDown, whereMouseWentUp);
const selEnd = Math.max(whereMouseWentDown, whereMouseWentUp);
//console.log(selStart,selEnd);
let selectedText = genbankData.parsedSequence.sequence.substring(
selStart,
selEnd
);
if (selectedText) {
if (e.shiftKey) {
selectedText = getReverseComplement(selectedText);
}
console.log(selectedText);
navigator.clipboard.writeText(selectedText);
toast.success(
`Copied ${e.shiftKey ? "reverse complement" : ""} to clipboard`
);
if (whereMouseWentDown !== null && whereMouseWentUp !== null) {
copySelectedSequence(e.shiftKey);
e.preventDefault();
}
}
Expand Down Expand Up @@ -375,22 +361,28 @@ if (hit1 === -1) {
setContextMenu({ x: null, y: null });
};

const handleCopySelection = () => {
const copySelectedSequence = (asReverseComplement = false) => {
if (!whereMouseWentDown || !whereMouseWentUp) return;

const selStart = Math.min(whereMouseWentDown, whereMouseWentUp);
const selEnd = Math.max(whereMouseWentDown, whereMouseWentUp);
const selectedText = genbankData.parsedSequence.sequence.substring(selStart, selEnd);
let selectedText = genbankData.parsedSequence.sequence.substring(selStart, selEnd);

if (asReverseComplement) {
selectedText = getReverseComplement(selectedText);
}

navigator.clipboard.writeText(selectedText);
toast.success('Copied to clipboard');
toast.success(`Copied ${asReverseComplement ? 'reverse complement ' : ''}to clipboard`);
};

const handleCopySelection = () => {
copySelectedSequence(false);
handleCloseContextMenu();
};

const handleCopyRC = () => {
const selStart = Math.min(whereMouseWentDown, whereMouseWentUp);
const selEnd = Math.max(whereMouseWentDown, whereMouseWentUp);
const selectedText = genbankData.parsedSequence.sequence.substring(selStart, selEnd);
const rc = getReverseComplement(selectedText);
navigator.clipboard.writeText(rc);
toast.success('Copied reverse complement to clipboard');
copySelectedSequence(true);
handleCloseContextMenu();
};

Expand Down

0 comments on commit 338d4f5

Please sign in to comment.