Skip to content

api.utils

Utility API methods to control wK.

Example:

window.webknossos.apiReady(3).then(api => {
  api.utils.sleep(...);
  api.utils.showToast(...);
  ...
}
Methods

registerKeyHandler

registerKeyHandler(
  key: string,
  handler: KeyboardNoLoopHandler | (() => void),
): UnregisterHandler

Sets a custom handler function for a keyboard shortcut.

Parameters:

  • key: The key combo to bind, using the @rwh/keystrokes syntax (e.g. "a", "shift + a", "control > y, r"). See the keystrokes docs/InputKeyboard usages in this codebase for more syntax examples.
  • handler: Either a plain function, which is invoked once when the key combo is pressed (there is no release callback in that case), or a { onPressed, onReleased? } object if you also need to react when the key combo is released.

Returns:

An object with an unregister() method that removes the handler again.

Example:

const handler = api.utils.registerKeyHandler("g", () => {
  console.log("g was pressed");
});
// later
handler.unregister();

registerOverwrite

registerOverwrite(
  actionName: string,
  overwriteFunction: (store: S, next: (action: A) => void, originalAction: A) => A | Promise<A>,
): () => void

Overwrite existing wK actions. wK uses Redux actions to trigger any changes to the application state.

Parameters:

  • actionName: The name of the action you wish to override:
  • CREATE_NODE
  • DELETE_NODE
  • SET_ACTIVE_NODE
  • SET_NODE_RADIUS
  • CREATE_BRANCHPOINT
  • DELETE_BRANCHPOINT
  • CREATE_TREE
  • DELETE_TREE
  • SET_ACTIVE_TREE
  • SET_ACTIVE_GROUP
  • SET_TREE_NAME
  • MERGE_TREES
  • SELECT_NEXT_TREE
  • SHUFFLE_TREE_COLOR
  • SHUFFLE_ALL_TREE_COLORS
  • CREATE_COMMENT
  • DELETE_COMMENT
  • overwriteFunction: Your new implementation for the method in question. Receives the central wK store, a callback to fire the next/original action and the original action.

Returns:

A function used to unregister the overwriteFunction

Example:

api.utils.registerOverwrite("MERGE_TREES", (store, next, originalAction) => {
  // ... do stuff before the original function...
  next(originalAction);
  // ... do something after the original function ...
});

showToast

showToast(
  type: ToastStyle,
  message: string,
  timeout?: number,
): ((args: any[]) => any) | null | undefined

Show a toast to the user. Returns a function which can be used to remove the toast again.

Parameters:

  • type: Can be one of the following: "info", "warning", "success" or "error"
  • message: The message string you want to show
  • timeout: Time period in milliseconds after which the toast will be hidden. Time is measured as soon as the user moves the mouse. A value of 0 means that the toast will only hide by clicking on it's X button.

Example:

// Show a toast for 5 seconds
const removeToast = api.utils.showToast("info", "You just got toasted", 5000);
// ... optionally:
// removeToast();

sleep

sleep(milliseconds: number): Promise<void>

Wait for some milliseconds before continuing the control flow.

Example:

// Wait for 5 seconds
await api.utils.sleep(5000);