-
Notifications
You must be signed in to change notification settings - Fork 168
Smarter ContextMenu
items: pass contextmenu
event to ContextMenu
items
#355
base: master
Are you sure you want to change the base?
Conversation
I've been wondering if we should add something like Another alternative would be to have a static property somewhere which is set to the node for the duration of the command execution. The relevant command could then look at that node as needed. |
I like the idea of having this static method on currentCommandTarget(): HTMLElement | null {
return document.body.querySelector('[data-phosphorCommandTarget]');
} |
Another options would be to add a third optional arg to the execute(id: string, args: ReadonlyJSONObject = JSONExt.emptyObject, target?: HTMLElement): Promise<any> And that would element would be set to the |
https://developer.mozilla.org/en-US/docs/Web/API/Window/event If only Firefox supported this, we'd be in the clear. In Chrome and Safari and IE, you can access app.commands.addCommand(command, {
label: 'Single-Document Mode',
isToggled: () => app.shell.mode === 'single-document',
execute: () => {
console.log('target is', window.event && window.event.target);
const args =
app.shell.mode === 'multiple-document'
? { mode: 'single-document' }
: { mode: 'multiple-document' };
return app.commands.execute(CommandIDs.setMode, args);
}
}); |
That would be nice. However that's giving you the actual label |
Here's another wrinkle: not just There's some relevant examples of |
I like the idea of stashing the target node in a static property, since that will make it easy to access the target within various functions (eg I think these two things could be combined into a single nice design via some code in |
Okay, I've written/tested a new version of the PR. The commands.addCommand(CommandIDs.showInFileBrowser, {
label: () => `Show in File Browser`,
isEnabled,
execute: (args, target) => {
// give 1st precedence to explicitly passed path
let path = args['path'];
if (!path && target) {
// give 2nd precedence to path on the target node, if present
path = target.dataset['path'];
}
if (!path) {
// fall back to path of focused widget
path = docManager.contextForWidget(shell.currentWidget).path;
}
if (!path) {
return;
}
...
// do stuff with path
...
}
}); There's some more work to be done to make |
uses latest version of PR phosphorjs/phosphor#355
What if the command registry has an attached property so that the API surface area of the command functions doesn't change. Suppose you could do: commands.addCommand(CommandIDs.showInFileBrowser, {
label: () => `Show in File Browser`,
isEnabled,
execute: (args) => {
const target = commands.targetProperty.get(args);
// etc.
}
}); |
I can definitely make target into a command registry property. It'll be a little tricky to still support the fact that different items in the same menu can have different targets (depending on their I am a little confused by something in your example code, though @afshin. Could you please clarify what you mean by |
@telamonian I would say hold off on doing that to see what @sccolbert thinks. But the |
Huh. I don't think I've ever come across a pattern quite like that of |
c1d517b
to
c5b6e38
Compare
Okay, here's the 3rd stab at this PR. This version is written along the lines of the discussion I had with @sccolbert at the Jupytercon sprint.
|
ContextMenu
items: pass contextmenu
event to ContextMenu
items
Adds
passDataset
property to contextmenu items:The behavior of the flag is implemented via a few lines added to the
matchItems
function:This PR doesn't change any existing behavior, it just adds some optional new behavior. See #354 for use cases/discussion.