Hi, I am currently trying to setup a custom Speckle Viewer and was wondering if there is a way I can hook into the SelectionExtension and receive a callback whenever an item is selected?
I know there is the viewer.on(ViewerEvent.ObjectClicked, ...., however this does not work if the element has been selected via the SelectionExtension. My current way of working is both an element could be clicked on the viewer or some external code could trigger an element selection. Both of these should, in my opinion, should trigger a callback on SelectionExtension. I noticed on the Typescript definition there seems to be support for an event emitter, however reading through the source here: speckle-server/packages/viewer/src/modules/extensions/SelectionExtension.ts at 6a51d6eaca5428648587e7b1cde7cc66f6b2fd0f · specklesystems/speckle-server · GitHub I couldn’t find any references where an event is emitted.
Any pointers/advice for spots to look are greatly appreciated!
Yup, your use case make perfect sense. We might add default events in the SelectionExtension in a future iteration, but you can already achieve the same thing with the following minimal code
export enum SelectionEvent {
Selected,
// Other types that you'd want
}
export class EventedSelectionExtension extends SelectionExtension {
protected applySelection() {
super.applySelection()
this.emit(SelectionEvent.Selected)
/** You can send data as event payload if you wish */
// this.emit(SelectionEvent.Selected, payload)
}
}
Here, we’re defining our own SelectionEvent types, then we’re extending the default SelectionExtension and making it emit the Selected event whenever something gets selected. Like you already mentioned all extensions can emit events by default ,so it’s just the matter of calling the emit function.
For the sake of completeness, here is a sandbox with a complete example containing the above code.