diff --git a/README.md b/README.md index 363bfaf..56fe937 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,17 @@ Use Alt + click 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 Alt to Control, Shift, Meta or AltGraph in the settings. +You can replace the key modifier for mouse click from Alt to Control, Shift, Meta or AltGraph in the settings*. To jump back to the variable/function usage, use Alt + o. 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 Alt, Control, Shift and Meta are supported. + ## Prerequisites * JupyterLab diff --git a/package.json b/package.json index db6a188..ca16d86 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/editors/codemirror/extension.ts b/src/editors/codemirror/extension.ts index aafd337..3433c1c 100644 --- a/src/editors/codemirror/extension.ts +++ b/src/editors/codemirror/extension.ts @@ -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; @@ -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'];