Skip to content

Commit

Permalink
Add documentation about how to instantiate a selection callback
Browse files Browse the repository at this point in the history
  • Loading branch information
nlarusstone committed Jun 30, 2023
1 parent e755c38 commit 5d7e783
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/selections.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,35 @@ const ligandLoci = Structure.toStructureElementLoci(ligandData as any);
plugin.managers.camera.focusLoci(ligandLoci);
plugin.managers.interactivity.lociSelects.select({ loci: ligandLoci });
```

## Selection callbacks
If you want to subscribe to selection events (e.g. to change external state in your application based on a user selection), you can use: `plugin.behaviors.interaction.click.subscribe`

Here's an example of passing in a React "set" function to update selected residue positions.
```typescript
import {
Structure,
StructureProperties,
} from "molstar/lib/mol-model/structure"
// setSelected is assumed to be a "set" function returned by useState
// (selected: any[]) => void
plugin.behaviors.interaction.click.subscribe(
(event: InteractivityManager.ClickEvent) => {
const selections = Array.from(
plugin.managers.structure.selection.entries.values()
);
// This bit can be customized to record any piece information you want
const localSelected: any[] = [];
for (const { structure } of selections) {
if (!structure) continue;
Structure.eachAtomicHierarchyElement(structure, {
residue: (loc) => {
const position = StructureProperties.residue.label_seq_id(loc);
localSelected.push({ position });
},
});
}
setSelected(localSelected);
}
)
```

0 comments on commit 5d7e783

Please sign in to comment.