Skip to content
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

More flexible submenus? #98

Open
krassowski opened this issue Jul 30, 2020 · 0 comments
Open

More flexible submenus? #98

krassowski opened this issue Jul 30, 2020 · 0 comments

Comments

@krassowski
Copy link
Member

Currently, the menu widgets are either:

  • commands,
  • separators, or
  • submenus

however, the developer API is not as convenient to use for submenus as it is for commands. For example, one can create a callback for a command to be asked about its visibility (isVisible) or state (isEnabled), but this does not seem to be possible for submenus:

get isVisible(): boolean {
if (this.type === 'command') {
return this._commands.isVisible(this.command, this.args);
}
if (this.type === 'submenu') {
return this.submenu !== null;
}
return true;
}

Lack of callbacks also means that the we do not have an easy way to:

  • dynamically change the name of the submenu
  • dynamically generate its content

This led me to use some quirky workarounds when implementing spellcheck suggestions.

One lazy way of getting what I want would be (pseudo-code follows):

type ItemType = 'command' | 'submenu' | 'separator' | 'subcommand';    // ← added

IItemOptions: {
    type?: ItemType;
    command?: string;
    args?: ReadonlyJSONObject;
    submenu?: Menu | null;
    subcommand?: string;   // ← added
}

this.app.commands.addCommand(
    'spellchecker:suggestions',
   {
        label: () => anySuggestions() ? 'Adjust to' : 'No suggestions',
        isEnabled: () => anySuggestions(),
        /* provides the menu items options; command defaults to subcommand */
        execute: () => getSuggestions().map(suggestion => {args: {name: suggestion}}) as IItemOptions[],
   }
)

this.app.commands.addCommand(
    'spellchecker:use-suggestion',
   {
        label: (args) => args.name as string,
        execute: (args) => replaceToken(args.name as string),
   }
)

this.app.contextMenu.addItem({
    selector: '.cm-spell-error',
    command: 'spellchecker:suggestions',
    subcommand: 'spellchecker:use-suggestion',
    type: 'subcommand'
});

Not sure how good it would be; the trick with re-using execute seems elegant but sadly the type checking would not be of much use here... Unless we would have additional addSubCommand method with items instead of execute.

Also, if I am the only one struggling with this it might not be worth introducing extra complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant