Skip to content

Plugin API

Topher Anselmo edited this page Oct 16, 2023 · 8 revisions

tmcp.addItem(itemOptions)

Adds a static search result to TMCP. The interface for the itemOptions object is as follows:

{
  // What shows up in the search results
  text: string;

  // What happens when the user hits enter on this result
  action?: () => any;

  // When true, will prevent the window from hiding while
  // the user is viewing this item's display
  keepOpen?: boolean;

  // Absolute path to an icon to display next to the item
  // (If omitted, the plugin icon will be used)
  icon?: string;

  // When provided, will show addditional information in
  // a panel on the right side of the window
  display?: {
    type: 'markdown' | 'html';
    content: string;
  };
}

tmcp.addSetting(settingOptions)

Adds an item to the settings section for this plugin. Can be used to add user toggleable settings to your plugin. The interface for the settingOptions object is as follows:

{
  // The label that appears above this setting
  text: string;

  // Optional small text that appears below the setting
  help?: string;

  // The type of setting to use
  // 'boolean' - Renders a checkbox, and has a boolean value
  // 'select' - Renders a select drop down box, and has a string value
  // 'number' - Renders a number input, and has an integer value
  // 'string' - Renders a text input, and has a string value
  type: 'boolean' | 'select' | 'number' | 'string';

  // The default value for this setting (depending on the type)
  value: any;

  // When the type is `select`, these are the options that will show
  // in the drop down. The default value should be one of these.
  options?: string[];

  // When the type is `number`, these are the minimum and maximum values
  min?: number;
  max?: number;
}

This returns a function that can be used to get or update the value the setting. Here's an example:

const shouldDoThing = tmcp.addSetting({
  text: 'Do the thing?',
  type: 'boolean',
  value: false
});

// Somewhere else...

if (shouldDoThing()) {
  // Do the thing!
}

shouldDoThing(true); // Set the value to true

tmcp.addSettingButton(buttonSettingOptions)

Adds a button to the settings section for this plugin. Can be used to add user triggered action to your plugin. The interface for the buttonSettingOptions is as follows:

{
  // The text to display on the button
  text: string;

  // Optional small text that appears below the button
  help?: string;

  // Callback to execute when the button is clicked
  action: () => void;
}

tmcp.onResult(handler)

Sets the result handler for this plugin. This allows you to filter search results from your plugin before they get added to the results list for the user. The handler argument should be a function that accepts one argument, and return a boolean value. If true is returned, the item will appear in the result, and it will be hidden if false is returned. The argument passed to the handler function will be the item as defined by a previous addItem call.

Example:

tmcp.onResult(result => {
  if (result.text === 'This item should never appear') {
    return false
  } else {
    return true;
  }
});

tmcp.onShow(handler)

Sets the show handler for this plugin. This allows you to execute a callback function (handler) whenever the window is shown. The handler argument should be a function that has no arguments.

Example:

tmcp.onShow(() => {
  console.log('The window has just appeared');
});

tmcp.onQuery(handler)

Sets the query handler for this plugin. Allows you to dynamically react to the user typing their query and add temporary results. The handler argument should be a callback that accepts two arguments. The first argument (query) is a string representing what the user currently has typed. The second argument (addItem) is a function that can add a dynamic result.

Example:

tmcp.onQuery((query, addItem) => {
  if (query.includes('hello')) {
    addItem({
      text: 'Hello World',
      action: () => console.log('Hello World!')
    });
  }
});

tmcp.onDisplay(handler)

Sets the display handler for this plugin. Allows you to react to a result from your plugin being displayed. The handler argument should be a function that takes no arguments.

Example:

tmcp.onDisplay(() =>{
  console.log('Something from my plugin is being shown');
});

__dirname

This global variable is a string that contains the absolute path to the folder containing the script file it's being called from.

__filename

This global variable is a string that contains the absolute path to the script file it's being called from.

Clone this wiki locally