Giter Site home page Giter Site logo

pfcbenjamin / command-pal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from benwinding/command-pal

0.0 1.0 0.0 458 KB

The hackable command palette for the web, inspired by Visual Studio Code.

Home Page: https://benwinding.github.io/command-pal/docs

License: MIT License

HTML 66.31% JavaScript 8.53% CSS 2.34% Svelte 22.82%

command-pal's Introduction

command-pal

โŒจ The hackable command palette for the web, inspired by Visual Studio Code.

NPM Version License Downloads/week Github Issues

screen cap

Configure custom commands with custom keyboard shortcuts!

Demos

Benefit's of Command Palettes

  • Ease of use
    • Simply 1 keyboard shortcut to remember
    • Fuzzy search allows you to find commands easily
  • Speed
    • Keyboard makes it fast to access any command/function
    • Fuzzy search allows for quick ordering of commands
    • Efficient to find a commands that you used once a long time ago
  • Discoverability
    • You can scroll down the entire list of commands
    • Find commands by simply searching the Command palette
    • Tips and functions can be given to you as you type

Features

  • JS framework agnostic (can be attached to any site)
  • Keyboard first control (shortcuts configurable)
  • Custom commands
  • Dynamically Add/Remove commands
  • Nested commands
  • Fuzzy text matching (fuse.js)
  • Themeable (theme-light.css and theme-dark.css included)
  • Mobile friendly (button in bottom-left)

screen cap

Why?

Command Palettes have alwyas impressed me with how easy they are to use and develop for. I rarely see them on the web platform, so I thought I'd give it a shot.

Get Started

Install

Either install from npm

yarn add command-pal

Or use the script tag

<script src="https://cdn.jsdelivr.net/npm/command-pal"></script>

Usage - Simple

const c = new CommandPal({
  hotkey: "ctrl+space",
  commands: [
    {
      name: "Send Message",
      shortcut: "ctrl+m",
      handler: () => alert("Send Message"),
    },
    {
      name: "Search Contacts",
      handler: () => alert("Searching contacts..."),
    },
    {
      name: "Goto Profile",
      shortcut: "ctrl+4",
      handler: () => window.location.hash = "profile",
    },
  ],
});
c.start();

Usage - Avanced

const c = new CommandPal({
  hotkey: "ctrl+space",
  commands: [
    {
      name: "Change Language",
      children: [
        {
          name: "English",
          handler: () => alert("Changing to English")
        },
        {
          name: "Spanglish",
          handler: () => alert("Changing to Spanglish")
        }
      ]
    },
    {
      name: "Goto About",
      handler: () => window.location.hash = "about",
    },
  ],
});
c.start();

API

CommandPal instance

const c = new CommandPal({
  hotkey: "ctrl+space",  // Launcher shortcut
  hotkeysGlobal: true,       // Makes shortcut keys work in any <textarea>, <input> or <select>
  commands: [
    // Commands go here
  ]
});
// Start the instance
c.start()
// Destroy the instance
c.destroy()

Subscribe to events

There's a few events that can be subscribed to during command-pal's execution.

// When a command is executed
c.subscribe("exec", (e) => { console.log("exec", { e }); });
// On TextChanged
c.subscribe("textChanged", (e) => { console.log("textChanged", { e }); });
// When a command palette is opened
c.subscribe("opened", (e) => { console.log("opened", { e }); });
// When a command palette is closed
c.subscribe("closed", (e) => { console.log("closed", { e }); });

Command Item

{
  // Required name of command (displayed)
  name: "Open Messages",
  // Required name of command (displayed)
  description: "View all messages in inbox",
  // Shortcut of command
  shortcut: "ctrl+3",
  // Callback function of the command to execute
  handler: (e) => {
    // DO SOMETHING
  }
  // Child commands which can be executed
  children: [...]
},

Command Item Child

Note: Child commands cannot have shortcuts.

{
  // Required name of command (displayed)
  name: "Open Messages",
  // Required name of command (displayed)
  description: "View all messages in inbox",
  // Callback function of the command to execute
  handler: (e) => {
    // DO SOMETHING
  }
},

Add/Remove Command's At Runtime

The command list is an observed array, which means you can modify it even after it's instantiated. The following snippet shows how commands can be dynamically added during runtime.

const commands = [
  {
    name: "Add Command to List",
    handler: () => {
      commands.push({
        name: 'New Command',
        handler: () => {
          // Do something
        },
      });
    },
  },
];
const c = new CommandPal({
  hotkey: "ctrl+space",
  commands: commands,
});
c.start();

Local Development

To develop on command-pal simply clone, install and run

npm run dev

Then the following link:

Have a go, PR's and issues always welcome.

Prior Art

Many applications have implemented this before, here's a few. My favourite implementation is the VScode, which is the main influence for this project.

Editors

Misc

command-pal's People

Contributors

benwinding avatar dependabot[bot] avatar marlamin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.