Feat: Add capture option to keybindings#788
Open
i-aki-y wants to merge 3 commits into
Open
Conversation
It is necessary! This reverts commit ab5f4bc.
capture option to keybindings**capture option to keybindings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This pull request introduces a
captureoption for keybindings, allowing them to be processed during the capturing phase of the event propagation. This change aims to resolve complex keyboard shortcut conflicts between Lumino applications and other components that have their own keymap systems, such as CodeMirror.This PR tries to address #761 and the original JupyterLab issue jupyterlab/jupyterlab#15897.
By handling certain keybindings in the capturing phase, we can intercept events before they reach components like CodeMirror, preventing them from being consumed and allowing Lumino's shortcut system to have precedence when needed.
I hope this PR can help clarify these issues and serve as a starting point for further discussion and improvements.
Code Changes
packages/commands/src/index.tscaptureproperty toIKeyBindingOptionsandIKeyBinding: This boolean flag allows developers to specify that a keybinding should be evaluated during the event capturing phase. The default remainsfalsefor backward compatibility.processKeydownEventto be phase-aware: The method now checksevent.eventPhaseto filter keybindings based on theircaptureflag, ensuring that capturing bindings are only matched in the capturing phase and non-capturing bindings in the bubbling phase._allowReplayInBubblingflag: A new internal flag,_allowReplayInBubbling, has been added to manage complex interactions between phases. When a keystroke sequence in the capturing phase results in a mismatch, this flag enables the suppressed events to be replayed and re-evaluated in the bubbling phase. This ensures that the event can still be processed by bubbling-phase listeners (e.g., in CodeMirror or Lumino itself), enabling fallback behaviors as demonstrated in the new test cases.packages/application/src/index.tsthis._bubblingKeydown: The application now always attaches both capturing and bubbling phase listeners todocument.bubblingKeydownoption inIStartOptionsis preserved for API compatibility but is no longer used.packages/commands/tests/src/index.spec.tscapturefeature behaves as expected in realistic integration contexts. A brief summary of the tested cases is included below.Use Cases & Scenarios Tested
To illustrate the capabilities and limitations of this feature, I have defined several key interaction scenarios between Lumino bindings and a mocked external handler (simulating CodeMirror). While this mock is not a full replacement for end-to-end testing in JupyterLab, it provides valuable insight into the expected behavior.
Case 1: Precedence of Capturing Bindings
['Ctrl K', 'Ctrl L']) takes precedence over a single-stroke capturing binding (['Ctrl K']), which in turn executes after a timeout. Both correctly mask bubbling and external bindings.Case 2: Masking by External Handlers
Case 3: Distinct Chord Resolution
Ctrl K, Ctrl Mvs.Ctrl K, Ctrl L), allowing an external handler to process its chord even if a Lumino capturing binding shares the same prefix.Case 4: Chord Interruption and Timeout Execution
['Ctrl K']) is correctly executed after the timeout.Case 5 & 6: Greedy vs. Non-Greedy Capturing Bindings
Case 7 & 8: Sequence Integrity
A Note on Testing
The
externalHandlerintroduced in the unit tests simulates only a limited subset of CodeMirror's behavior to demonstrate the intended interactions. It is recommended that further integration tests be added on the JupyterLab side to validate the behavior with a real CodeMirror instance.Potential Issue for Consideration
The current implementation of
processKeydownEventrelies onevent.eventPhase. Ifevent.target === event.currentTarget, the phase will beAT_TARGET(2), which our logic treats as the bubbling phase. In Lumino's application context, the listener is ondocument, so this is unlikely to be an issue. However, many of our existing unit tests dispatch events directly on the target element, creating this condition. If this PR is merged, a review of existing tests may be necessary to ensure they correctly simulate event propagation.