New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow native context menu to be shown for notebook cell in Chrome. #4720
Allow native context menu to be shown for notebook cell in Chrome. #4720
Conversation
The code looks good to me. |
I am not sure I understand all the edge cases, but does this achieve the same effect? /**
* Handle `contextmenu` event.
*/
private _evtContextMenuCapture(event: PointerEvent): void {
if (event.shiftKey) {
return;
}
// `event.target` sometimes gives an orphaned node in Firefox 57, which
// can have `null` anywhere in its parent tree. If we fail to find a
// cell using `event.target`, try again using a target reconstructed from
// the position of the click event.
let target = event.target as HTMLElement;
let index = this._findCell(target);
if (index === -1) {
target = document.elementFromPoint(event.clientX, event.clientY) as HTMLElement;
index = this._findCell(target);
}
let widget = this.widgets[index];
if (widget && widget.editorWidget.node.contains(target)) {
// Prevent CodeMirror from focusing the editor.
// TODO: find an editor-agnostic solution.
event.preventDefault();
}
} |
Because if we can save on doing some DOM traversal and bail early, it seems both clearer and more performant. |
Yes, that would work as well, and I almost did that. I went for the version that I submitted because I wanted to do have as tight of a box around |
@afshin, can you review and merge when you're happy? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cheers!
👍
Fixes #4718.
This is a consequence of the saga of #3554. To refresh people's memory: CodeMirror has some special context menu logic that differs depending on whether Firefox or Chrome is being used. That logic was causing some extremely subtle focusing problems for notebook cells that ate up several developer-days.
We ultimately fixed it with a somewhat-hackish solution of calling
preventDefault
on context menu events for notebook cells. This lets CodeMirror know that is should not handle the event itself. However, we inadvertently prevented native context menus for notebook cells (and only in Chrome).This fix continues in the somewhat-hackish code path by allowing the context menu to continue un-cancelled if the shift key is pressed.