UNPKG

svelte-command-palette

Version:

#### Increase your productivity exponentially. 🚀🚀

80 lines (79 loc) • 2.49 kB
import Fuse from 'fuse.js'; import { paletteStore } from '../store/PaletteStore'; import { get } from 'svelte/store'; const noop = () => true; const defineActions = (actions = []) => { return actions.map(({ actionId = Math.random(), canActionRun = noop, title = '', subTitle = '', onRun = noop, description, keywords = [], shortcut = '' }) => { return { actionId, canActionRun, title, subTitle, onRun, description, keywords, shortcut }; }); }; const formatResults = (results) => results.map(({ item }) => item); const updatePaletteStoreAfterActionExec = (actionId) => { paletteStore.update((n) => { return { ...n, isVisible: false, textInput: '', activeCommandId: null, selectedCommandId: null, results: [], calledActions: [...n.calledActions, actionId] }; }); }; const runAction = ({ action }) => { const { onRun, canActionRun = noop, actionId = '' } = action || {}; const storeProps = get(paletteStore); const { storeMethods } = storeProps; if (canActionRun({ action, storeProps, storeMethods }) && onRun) { updatePaletteStoreAfterActionExec(actionId); onRun?.({ action, storeProps, storeMethods }); return true; } paletteStore.update((n) => { return { ...n, isVisible: false }; }); return false; }; const createFuse = (actions) => new Fuse(actions, { keys: [ { name: 'title', weight: 1 }, { name: 'subtitle', weight: 0.7 }, { name: 'description', weight: 0.6 }, { name: 'keywords', weight: 0.5 } ] }); const getNonEmptyArray = (...args) => { return args.find((array = []) => array.length > 0) || []; }; const camelCaseToDash = (str) => str.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase(); const toCssString = (props = {}) => props ? // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore Object.keys(props).reduce((str, key) => `${str}; ${camelCaseToDash(key)}: ${props[key]}`, '') : ''; export { noop, defineActions, formatResults, runAction, createFuse, updatePaletteStoreAfterActionExec, getNonEmptyArray, camelCaseToDash, toCssString };