Skip to content

Adding dynamic command seems to not be searchable until after command pallete is opened #11

@rouilj

Description

@rouilj

Hello:

I am creating a command to copy the current URL to the clipboard. Since the user may not have given
me permission, or the clipboard API may not be available, I run some code to determine that the prerequisites
are available and add it dynamically.

To do this, I define a commands array and follow along with the documentation:

commands = [ {...}, {...},
      {
      name: "Add new entry here",
      handler: () => (add_new_entry(commands)),
    },
 ];

function add_new_entry (c) {
    c.push( {
        name: "New Command Here",
        handler: () => (alert("I'm a new command!!!") ),
          )
}

const c = new CommandPal({
  hotkey: "ctrl+space",
  commands: commands,
});

function add_copy_url_to_clipboard (c) {
    if ( ! navigator.clipboard ) return;
    navigator.permissions.query({name: "clipboard-write"}).then(
        function (result) {
            if (result.state === "granted") {
                c.push( {
                    name: "Copy URL to Clipboard",
                    handler: () => (navigator.clipboard.writeText(
                        window.location.href)), }
                      )
            }
        }
    )
}

add_copy_url_to_clipboard(commands)

c.start();

If I activate command-pal, I can see the "Copy URL to Clipboard" entry listed, but searching for URL doesn't
find it. If I exit (esc key) from command-pal and re-activate it, search for URL works.

Even on the first activation, I can choose the "Copy URL to Clipboard" item by arrowing down to it and it works as expected.

It looks like the dynamic entry is unsearchable only if it is the first invocation after the page loads.

If I choose the "Add new entry here" item (by search or arrow keys), I can see the new entry when I
re-activate the command-pal and I can search for "New command here" successfully.

What's weird is even if I change the code to read:

add_copy_url_to_clipboard(commands)

const c = new CommandPal({
  hotkey: "ctrl+space",
  commands: commands,
});

c.start();

searching still fails until the second restart. printing commands to the console shows what I expected.

I'm at a loss to explain this.

With command-pal deactivated, if I call add_new_entry(commands) in the console then activate command-pal,
the new item is not there. If I close and re-open command-pal the item is there and is searchable.

Am I doing something wrong?

Console is not reporting any errors.

Browser: Version 109.0.5414.120 (Official Build) (64-bit) on windows 10.

Ideas welcome.

-- rouilj

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions