-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Simplify application context menu cache, rename to be clearer. #5932
Conversation
@ian-r-rose - it seems that composedPath is now pretty well supported. What do you think about using it? https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath |
I haven't really looked into it. Does it return all the DOM nodes in the hierarchy, or just ones for which an event handler is registered? |
(I also don't view this PR as critical, just wanted to sneak it in since it's API breaking) |
From the examples, it appears to return all elements on which listeners could be invoked, i.e., all, not just ones that have listeners actually listening. |
Sure, I'll try that. |
diff --git a/packages/application/src/frontend.ts b/packages/application/src/frontend.ts
index c9bd3196c..637bab0f4 100644
--- a/packages/application/src/frontend.ts
+++ b/packages/application/src/frontend.ts
@@ -115,17 +115,7 @@ export abstract class JupyterFrontEnd<
) {
return undefined;
}
- // This one-liner doesn't work, but should at some point in the future
- // `return this._contextMenuEvent.composedPath() as HTMLElement[];`
- // cf. (https://developer.mozilla.org/en-US/docs/Web/API/Event)
- let node = this._contextMenuEvent.target as HTMLElement;
- do {
- if (test(node)) {
- return node;
- }
- node = node.parentNode as HTMLElement;
- } while (node.parentNode && node !== node.parentNode);
- return undefined;
+ return this._contextMenuEvent.composedPath().find(test);
}
/** |
Or maybe even simpler: diff --git a/packages/application/src/frontend.ts b/packages/application/src/frontend.ts
index c9bd3196c..dda4d7aea 100644
--- a/packages/application/src/frontend.ts
+++ b/packages/application/src/frontend.ts
@@ -109,22 +109,9 @@ export abstract class JupyterFrontEnd<
contextMenuHitTest(
test: (node: HTMLElement) => boolean
): HTMLElement | undefined {
- if (
- !this._contextMenuEvent ||
- !(this._contextMenuEvent.target instanceof HTMLElement)
- ) {
- return undefined;
+ if (this._contextMenuEvent) {
+ return this._contextMenuEvent.composedPath().find(test);
}
- // This one-liner doesn't work, but should at some point in the future
- // `return this._contextMenuEvent.composedPath() as HTMLElement[];`
- // cf. (https://developer.mozilla.org/en-US/docs/Web/API/Event)
- let node = this._contextMenuEvent.target as HTMLElement;
- do {
- if (test(node)) {
- return node;
- }
- node = node.parentNode as HTMLElement;
- } while (node.parentNode && node !== node.parentNode);
return undefined;
} |
Or perhaps we want to filter event targets to be just html elements? |
Yeah, I think we should filter. |
Okay. I'm afk for a bit, but I'll check here when I'm back. |
Okay, updated. |
Nice! Does it actually work too? (I didn't test it...) |
Hmm, it seems to have some problems. |
|
97231c2
to
a0a0686
Compare
Okay, that's fine. Your version was pretty nice too. I liked the way you simplified it down to a do-while. |
I updated the comment to be more accurate in the context of this new code. |
Sounds good, thanks @jasongrout |
This function is still a bit of a hack, but a useful one at times. This tries to simplify it and make it a bit clearer.