Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking that there is indeed a selection before scrolling #257

Merged
merged 11 commits into from
Feb 20, 2024
36 changes: 22 additions & 14 deletions src/SeqViewerContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import SelectionHandler, { InputRefFunc } from "./SelectionHandler";
import CentralIndexContext from "./centralIndexContext";
import { Annotation, CutSite, Highlight, NameRange, Primer, Range, SeqType } from "./elements";
import { isEqual } from "./isEqual";
import SelectionContext, { Selection, defaultSelection } from "./selectionContext";
import SelectionContext, { ExternalSelection, Selection, defaultSelection } from "./selectionContext";

/**
* This is the width in pixels of a character that's 12px
Expand Down Expand Up @@ -46,11 +46,7 @@ interface SeqViewerContainerProps {
rotateOnScroll: boolean;
search: NameRange[];
selectAllEvent: (event: React.KeyboardEvent<HTMLElement>) => boolean;
selection?: {
clockwise?: boolean;
end: number;
start: number;
};
selection?: ExternalSelection;
seq: string;
seqType: SeqType;
showComplement: boolean;
Expand Down Expand Up @@ -91,6 +87,25 @@ class SeqViewerContainer extends React.Component<SeqViewerContainerProps, SeqVie
};
}

selectionIsProgramatic(selection: any): selection is Selection {
// If the selection was done programatically, it has not type
return !selection.type;
}

// If the selection prop updates, also scroll the linear view to the new selection
componentDidUpdate = (prevProps: SeqViewerContainerProps) => {
// Only scroll if the selection was done passed in as a prop by a user of SeqViz. Otherwise the selection was
// made by the user clicking an element or selecting a range of sequences
if (this.selectionIsProgramatic(this.props.selection)) {
if (
this.props.selection?.start !== prevProps.selection?.start &&
this.props.selection?.start !== this.props.selection?.end
) {
this.setCentralIndex("LINEAR", this.props.selection?.start || 0);
}
}
};

/** this is here because the size listener is returning a new "size" prop every time */
shouldComponentUpdate = (nextProps: SeqViewerContainerProps, nextState: any) =>
!isEqual(nextProps, this.props) || !isEqual(nextState, this.state);
Expand Down Expand Up @@ -123,14 +138,7 @@ class SeqViewerContainer extends React.Component<SeqViewerContainerProps, SeqVie
/**
* Returns the selection that was either a prop (optional) or the selection maintained in state.
*/
getSelection = (
state: Selection,
prop?: {
clockwise?: boolean;
end: number;
start: number;
}
): Selection => {
getSelection = (state: Selection, prop?: ExternalSelection): Selection => {
if (prop) {
return { ...prop, clockwise: typeof prop.clockwise === "undefined" || !!prop.clockwise, type: "" };
}
Expand Down
8 changes: 2 additions & 6 deletions src/SeqViz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "./elements";
import { isEqual } from "./isEqual";
import search from "./search";
import { Selection } from "./selectionContext";
import { ExternalSelection, Selection } from "./selectionContext";
import { complement, directionality, guessType, randomID } from "./sequence";

/** `SeqViz` props. See the README for more details. One of `seq`, `file` or `accession` is required. */
Expand Down Expand Up @@ -121,11 +121,7 @@ export interface SeqVizProps {
* Externally managed selection.
*
* If passed, SeqViz uses this prop as the selection range, rather than the internally managed selection */
selection?: {
clockwise?: boolean;
end: number;
start: number;
};
selection?: ExternalSelection;

/** a sequence to render. Can be DNA, RNA, or an amino acid sequence. Setting accession or file overrides this */
seq?: string;
Expand Down
6 changes: 6 additions & 0 deletions src/selectionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export interface Selection {
viewer?: "LINEAR" | "CIRCULAR";
}

export interface ExternalSelection {
clockwise?: boolean;
end: number;
start: number;
}

/** Initial/default selection */
export const defaultSelection: Selection = {
clockwise: true,
Expand Down
Loading