Skip to content

Commit

Permalink
Implement modifier key polyfill for Safari #3
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Mar 16, 2019
1 parent 274fe72 commit fac3f7b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ Use <kbd>Alt</kbd> + <kbd>click</kbd> to jump to a definition using your mouse,

![Go to definition](https://raw.githubusercontent.com/krassowski/jupyterlab-go-to-definition/master/examples/demo.gif)

You can replace the key modifier for mouse click from <kbd>Alt</kbd> to <kbd>Control</kbd>, <kbd>Shift</kbd>, <kbd>Meta</kbd> or <kbd>AltGraph</kbd> in the settings.
You can replace the key modifier for mouse click from <kbd>Alt</kbd> to <kbd>Control</kbd>, <kbd>Shift</kbd>, <kbd>Meta</kbd> or <kbd>AltGraph</kbd> in the settings*.

To jump back to the variable/function usage, use <kbd>Alt</kbd> + <kbd>o</kbd>.

The plugin is language-agnostic, though optimized for Python. Initial support for R was recently implemented.
Support for other languages is possible (PRs welcome).

*) For full list of physical keys mapped to the modifiers (which depend on your Operating System), please see [the MDN documentation](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState).

Safari users: Safari does not implement `MouseEvent.getModifierState` (see [#3](https://github.com/krassowski/jupyterlab-go-to-definition/issues/3)), thus only <kbd>Alt</kbd>, <kbd>Control</kbd>, <kbd>Shift</kbd> and <kbd>Meta</kbd> are supported.

## Prerequisites

* JupyterLab
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@krassowski/jupyterlab_go_to_definition",
"version": "0.1.4",
"version": "0.1.5",
"description": "Jump to definition of a variable or function in JupyterLab",
"keywords": [
"jupyter",
Expand Down
27 changes: 26 additions & 1 deletion src/editors/codemirror/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import { CodeMirrorTokensProvider } from "./tokens";
const HANDLERS_ON = '_go_to_are_handlers_on';


function getModifierState(event: MouseEvent, modifierKey: string): boolean {
// Note: Safari does not support getModifierState on MouseEvent, see:
// https://github.com/krassowski/jupyterlab-go-to-definition/issues/3
// thus AltGraph and others are not supported on Safari
// Full list of modifier keys and mappings to physical keys on different OSes:
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState

if (event.getModifierState !== undefined) {
return event.getModifierState(modifierKey)
}

switch (modifierKey) {
case 'Shift':
return event.shiftKey;
case 'Alt':
return event.altKey;
case 'Control':
return event.ctrlKey;
case 'Meta':
return event.metaKey;
}
}



export class CodeMirrorExtension extends CodeMirrorTokensProvider implements IEditorExtension {

jumper: CodeJumper;
Expand Down Expand Up @@ -44,7 +69,7 @@ export class CodeMirrorExtension extends CodeMirrorTokensProvider implements IEd
//codemirror_editor.addKeydownHandler()
let target = event.target as HTMLElement;
const {button} = event;
if (button === 0 && event.getModifierState(CodeMirrorExtension.modifierKey as string)) {
if (button === 0 && getModifierState(event, CodeMirrorExtension.modifierKey as string)) {

const classes = ['cm-variable', 'cm-property'];

Expand Down

0 comments on commit fac3f7b

Please sign in to comment.