notesy::commands

A plugin's commands: in the palette, the keybindings and, when it likes, the editor's / menu. Its script adds them as it runs, and takes them away when they stop making sense: "Sign in" while signed out, "Sign out" after. Takes the commands permission.

rustuse notesy::{accounts, commands};

pub fn ready() {
    if accounts::signed_in("github") { sign_out_command(); } else { sign_in_command(); }
}

fn sign_in_command() {
    commands::add("sign-in", #{ title: "Sign in to GitHub", shortcut: "Ctrl+Alt+L" }, |command| {
        command.remove();
        accounts::sign_in("github", |result| {
            match result {
                Ok(_) => sign_out_command(),
                Err(_) => sign_in_command(),
            }
        });
    });
}

fn sign_out_command() {
    commands::add("sign-out", "Sign out of GitHub", |command| {
        command.remove();
        accounts::sign_out("github");
        sign_in_command();
    });
}

#commands::add(key, options, run)

Adds a command, or changes the one it added with that key, and returns it. run(command) runs it, from the palette, its shortcut, the / menu, a status item, a sidebar item or a view; command is the command itself, so it can take itself away.

key names the command for good: lowercase letters, digits, -, _ and ., starting with a letter. notesy keeps the shortcut the user gave it by its key, so keep it the same from one version to the next.

options is its title, or an object:

Option What it is
title What the palette calls it.
shortcut Its shortcut until the user picks another, like Ctrl+Alt+L. A key that's bound already stays with what has it, and this one starts without.
category The group it's in, like Insert; the plugin's name unless set.
slash A line for it in the editor's / menu, saying what it does.
icon Its icon in the / menu, by name.
keywords More words the / menu finds it by.
snippet What it types, for a command that only types something: notesy types it at the caret itself, at once, with {date} and {time} filled in and the caret at its first $0 ("> [!tip] $0"). Its run isn't called: give it |command| (). Takes the editor permission.
block true: its snippet is a block, so typed mid-line it starts a line of its own.

A shortcut notesy can't read leaves the command without one, and the plugin's log says why.

Picked in the / menu, a command's /query comes out first, then it runs where that was. What its script types straight after (editor::insert, replace or insert_snippet, notesy::editor) undoes in one step with it, as notesy's own do.

#A command

Method What it does
command.id() Its full id, plugin.<plugin id>.<key>: what command fields in views, status items and section items take, and what the command.ran event says.
command.remove() Takes it out of the palette, the keybindings and the / menu. Adding it again with its key brings it back.

#commands::run(id)

Runs a command by its full id: notesy's own (toggle_sidebar, new_note …; the Keybindings page shows each one's), or one of its own. notesy's that change the note in front, the editor's own (delete_line, bold, undo …) and the ones that only type something, take the editor permission too. Another plugin's command is never run; what isn't allowed is refused, and its log says why.

Whatever a script added goes when it stops: when it's turned off, reloaded, or fails too often.

Every page