Skip to content

Commit

Permalink
Close #822 support custom keyboard actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Jan 5, 2023
1 parent e06b426 commit 11bccff
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
23 changes: 21 additions & 2 deletions docs/guide/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,25 @@ keyboard: {
}
```

Enable and configure keyboard navigation in fullscreen. It is a map defining key code->action. Set to `false` to disable.
Enable and configure keyboard navigation in fullscreen. It is a map defining key code->action. Set to `false` to disable. (all the available actions are listed above)

(all the available actions are listed above)
Since 5.0.2 you can configure an arbitrary callback to any key.

```js
keyboard: {
'h': (viewer) => {
if (viewer.panel.isVisible('help')) {
viewer.panel.hide();
} else {
viewer.panel.show({
id: 'help',
content: 'Help content',
});
}
},
},
```

::: tip More keyboard controls
To enable the keyboard outside fullscreen view, call `startKeyboardControl()` after init. To make more complex interactions, listen to the `key-press` event.
:::
13 changes: 13 additions & 0 deletions examples/equirectangular.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@
'caption',
'fullscreen',
],
keyboard: {
...PhotoSphereViewer.DEFAULTS.keyboard,
'h': (viewer) => {
if (viewer.panel.isVisible('help')) {
viewer.panel.hide();
} else {
viewer.panel.show({
id: 'help',
content: 'Help content',
});
}
},
},
});
</script>
</body>
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export type ViewerConfig = {
loadError: string;
[K: string]: string;
};
keyboard?: boolean | Record<string, ACTIONS>;
keyboard?: boolean | Record<string, ACTIONS | ((viewer: Viewer) => void)>;
};

export type DeprecatedViewerConfig =
Expand Down Expand Up @@ -408,7 +408,7 @@ export type ParsedViewerConfig = Omit<
fisheye?: number;
requestHeaders?: (url: string) => Record<string, string>;
navbar?: Array<string | NavbarCustomButton>;
keyboard?: Record<string, ACTIONS>;
keyboard?: Record<string, ACTIONS | ((viewer: Viewer) => void)>;
};

/**
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/services/EventsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,16 @@ export class EventsHandler extends AbstractService {
return;
}

if (!this.state.keyboardEnabled) {
if (!this.state.keyboardEnabled || !this.config.keyboard) {
return;
}

const action = this.config.keyboard[e.key];
if (action && !this.keyHandler.pending) {

if (typeof action === 'function') {
action(this.viewer);
}
else if (action && !this.keyHandler.pending) {
if (action !== ACTIONS.ZOOM_IN && action !== ACTIONS.ZOOM_OUT) {
this.viewer.stopAll();
}
Expand Down

0 comments on commit 11bccff

Please sign in to comment.