feat: revamped spotlight (#3171)

Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
Andrew Bastin
2023-07-11 23:02:33 +05:30
committed by GitHub
parent c3531c9d8b
commit 5230d2d3b8
36 changed files with 3941 additions and 1043 deletions

View File

@@ -2,8 +2,10 @@
* For example, sending a request.
*/
import { onBeforeUnmount, onMounted } from "vue"
import { Ref, onBeforeUnmount, onMounted, watch } from "vue"
import { BehaviorSubject } from "rxjs"
import { HoppRESTDocument } from "./rest/document"
import { HoppGQLRequest } from "@hoppscotch/data"
export type HoppAction =
| "request.send-cancel" // Send/Cancel a Hoppscotch Request
@@ -22,8 +24,6 @@ export type HoppAction =
| "modals.search.toggle" // Shows the search modal
| "modals.support.toggle" // Shows the support modal
| "modals.share.toggle" // Shows the share modal
| "modals.my.environment.edit" // Edit current personal environment
| "modals.team.environment.edit" // Edit current team environment
| "navigation.jump.rest" // Jump to REST page
| "navigation.jump.graphql" // Jump to GraphQL page
| "navigation.jump.realtime" // Jump to realtime page
@@ -38,6 +38,9 @@ export type HoppAction =
| "response.file.download" // Download response as file
| "response.copy" // Copy response to clipboard
| "modals.login.toggle" // Login to Hoppscotch
| "history.clear" // Clear REST History
| "user.login" // Login to Hoppscotch
| "user.logout" // Log out of Hoppscotch
/**
* Defines the arguments, if present for a given type that is required to be passed on
@@ -50,7 +53,7 @@ export type HoppAction =
* NOTE: We can't enforce type checks to make sure the key is Action, you
* will know if you got something wrong if there is a type error in this file
*/
type HoppActionArgs = {
type HoppActionArgsMap = {
"modals.my.environment.edit": {
envName: string
variableName: string
@@ -59,12 +62,18 @@ type HoppActionArgs = {
envName: string
variableName: string
}
"rest.request.open": {
doc: HoppRESTDocument
}
"gql.request.open": {
request: HoppGQLRequest
}
}
/**
* HoppActions which require arguments for their invocation
*/
type HoppActionWithArgs = keyof HoppActionArgs
export type HoppActionWithArgs = keyof HoppActionArgsMap
/**
* HoppActions which do not require arguments for their invocation
@@ -74,27 +83,27 @@ export type HoppActionWithNoArgs = Exclude<HoppAction, HoppActionWithArgs>
/**
* Resolves the argument type for a given HoppAction
*/
type ArgOfHoppAction<A extends HoppAction> = A extends HoppActionWithArgs
? HoppActionArgs[A]
: undefined
type ArgOfHoppAction<A extends HoppAction | HoppActionWithArgs> =
A extends HoppActionWithArgs ? HoppActionArgsMap[A] : undefined
/**
* Resolves the action function for a given HoppAction, used by action handler function defs
*/
type ActionFunc<A extends HoppAction> = A extends HoppActionWithArgs
? (arg: ArgOfHoppAction<A>) => void
: () => void
type ActionFunc<A extends HoppAction | HoppActionWithArgs> =
A extends HoppActionWithArgs ? (arg: ArgOfHoppAction<A>) => void : () => void
type BoundActionList = {
// eslint-disable-next-line no-unused-vars
[A in HoppAction]?: Array<ActionFunc<A>>
[A in HoppAction | HoppActionWithArgs]?: Array<ActionFunc<A>>
}
const boundActions: BoundActionList = {}
export const activeActions$ = new BehaviorSubject<HoppAction[]>([])
export const activeActions$ = new BehaviorSubject<
(HoppAction | HoppActionWithArgs)[]
>([])
export function bindAction<A extends HoppAction>(
export function bindAction<A extends HoppAction | HoppActionWithArgs>(
action: A,
handler: ActionFunc<A>
) {
@@ -110,7 +119,7 @@ export function bindAction<A extends HoppAction>(
type InvokeActionFunc = {
(action: HoppActionWithNoArgs, args?: undefined): void
<A extends HoppActionWithArgs>(action: A, args: ArgOfHoppAction<A>): void
<A extends HoppActionWithArgs>(action: A, args: HoppActionArgsMap[A]): void
}
/**
@@ -119,14 +128,16 @@ type InvokeActionFunc = {
* @param action The action to fire
* @param args The argument passed to the action handler. Optional if action has no args required
*/
export const invokeAction: InvokeActionFunc = <A extends HoppAction>(
export const invokeAction: InvokeActionFunc = <
A extends HoppAction | HoppActionWithArgs
>(
action: A,
args: ArgOfHoppAction<A>
) => {
boundActions[action]?.forEach((handler) => handler(args!))
boundActions[action]?.forEach((handler) => handler(args! as any))
}
export function unbindAction<A extends HoppAction>(
export function unbindAction<A extends HoppAction | HoppActionWithArgs>(
action: A,
handler: ActionFunc<A>
) {
@@ -142,15 +153,57 @@ export function unbindAction<A extends HoppAction>(
activeActions$.next(Object.keys(boundActions) as HoppAction[])
}
export function defineActionHandler<A extends HoppAction>(
/**
* A composable function that defines a component can handle a given
* HoppAction. The handler will be bound when the component is mounted
* and unbound when the component is unmounted.
* @param action The action to be bound
* @param handler The function to be called when the action is invoked
* @param isActive A ref that indicates whether the action is active
*/
export function defineActionHandler<A extends HoppAction | HoppActionWithArgs>(
action: A,
handler: ActionFunc<A>
handler: ActionFunc<A>,
isActive: Ref<boolean> | undefined = undefined
) {
let mounted = false
let bound = false
onMounted(() => {
bindAction(action, handler)
mounted = true
// Only bind if isActive is undefined or true
if (isActive === undefined || isActive.value === true) {
bound = true
bindAction(action, handler)
}
})
onBeforeUnmount(() => {
mounted = false
bound = false
unbindAction(action, handler)
})
if (isActive) {
watch(
isActive,
(active) => {
if (mounted) {
if (active) {
if (!bound) {
bound = true
bindAction(action, handler)
}
} else if (bound) {
bound = false
unbindAction(action, handler)
}
}
},
{ immediate: true }
)
}
}

View File

@@ -48,8 +48,8 @@ export const bindings: {
"alt-p": "request.method.post",
"alt-u": "request.method.put",
"alt-x": "request.method.delete",
"ctrl-k": "flyouts.keybinds.toggle",
"/": "modals.search.toggle",
"ctrl-k": "modals.search.toggle",
"ctrl-/": "flyouts.keybinds.toggle",
"?": "modals.support.toggle",
"ctrl-m": "modals.share.toggle",
"alt-r": "navigation.jump.rest",

View File

@@ -1,55 +0,0 @@
import { ref } from "vue"
const NAVIGATION_KEYS = ["ArrowDown", "ArrowUp", "Enter"]
export function useArrowKeysNavigation(searchItems: any, options: any = {}) {
function handleArrowKeysNavigation(
event: any,
itemIndex: any,
preventPropagation: boolean
) {
if (!NAVIGATION_KEYS.includes(event.key)) return
if (preventPropagation) event.stopImmediatePropagation()
const itemsLength = searchItems.value.length
const lastItemIndex = itemsLength - 1
const itemIndexValue = itemIndex.value
const action = searchItems.value[itemIndexValue]?.action
if (action && event.key === "Enter" && options.onEnter) {
options.onEnter(action)
return
}
if (itemsLength && event.key === "ArrowDown") {
itemIndex.value = itemIndexValue < lastItemIndex ? itemIndexValue + 1 : 0
} else if (itemIndexValue === 0) itemIndex.value = lastItemIndex
else if (itemsLength && event.key === "ArrowUp")
itemIndex.value = itemIndexValue - 1
}
const preventPropagation = options && options.stopPropagation
const selectedEntry = ref(0)
const onKeyUp = (event: any) => {
handleArrowKeysNavigation(event, selectedEntry, preventPropagation)
}
function bindArrowKeysListeners() {
window.addEventListener("keydown", onKeyUp, { capture: preventPropagation })
}
function unbindArrowKeysListeners() {
window.removeEventListener("keydown", onKeyUp, {
capture: preventPropagation,
})
}
return {
bindArrowKeysListeners,
unbindArrowKeysListeners,
selectedEntry,
}
}

View File

@@ -1,315 +1,146 @@
import IconLifeBuoy from "~icons/lucide/life-buoy"
import IconZap from "~icons/lucide/zap"
import IconArrowRight from "~icons/lucide/arrow-right"
import IconGift from "~icons/lucide/gift"
import IconMonitor from "~icons/lucide/monitor"
import IconSun from "~icons/lucide/sun"
import IconCloud from "~icons/lucide/cloud"
import IconMoon from "~icons/lucide/moon"
import { getPlatformAlternateKey, getPlatformSpecialKey } from "./platformutils"
export default [
{
section: "shortcut.general.title",
shortcuts: [
{
keys: ["?"],
label: "shortcut.general.help_menu",
},
{
keys: ["/"],
label: "shortcut.general.command_menu",
},
{
keys: [getPlatformSpecialKey(), "K"],
label: "shortcut.general.show_all",
},
{
keys: ["ESC"],
label: "shortcut.general.close_current_menu",
},
],
},
{
section: "shortcut.request.title",
shortcuts: [
{
keys: [getPlatformSpecialKey(), "↩"],
label: "shortcut.request.send_request",
},
{
keys: [getPlatformSpecialKey(), "S"],
label: "shortcut.request.save_to_collections",
},
{
keys: [getPlatformSpecialKey(), "U"],
label: "shortcut.request.copy_request_link",
},
{
keys: [getPlatformSpecialKey(), "I"],
label: "shortcut.request.reset_request",
},
{
keys: [getPlatformAlternateKey(), "↑"],
label: "shortcut.request.next_method",
},
{
keys: [getPlatformAlternateKey(), "↓"],
label: "shortcut.request.previous_method",
},
{
keys: [getPlatformAlternateKey(), "G"],
label: "shortcut.request.get_method",
},
{
keys: [getPlatformAlternateKey(), "H"],
label: "shortcut.request.head_method",
},
{
keys: [getPlatformAlternateKey(), "P"],
label: "shortcut.request.post_method",
},
{
keys: [getPlatformAlternateKey(), "U"],
label: "shortcut.request.put_method",
},
{
keys: [getPlatformAlternateKey(), "X"],
label: "shortcut.request.delete_method",
},
],
},
{
section: "shortcut.response.title",
shortcuts: [
{
keys: [getPlatformSpecialKey(), "J"],
label: "shortcut.response.download",
},
{
keys: [getPlatformSpecialKey(), "."],
label: "shortcut.response.copy",
},
],
},
{
section: "shortcut.navigation.title",
shortcuts: [
{
keys: [getPlatformSpecialKey(), "←"],
label: "shortcut.navigation.back",
},
{
keys: [getPlatformSpecialKey(), "→"],
label: "shortcut.navigation.forward",
},
{
keys: [getPlatformAlternateKey(), "R"],
label: "shortcut.navigation.rest",
},
{
keys: [getPlatformAlternateKey(), "Q"],
label: "shortcut.navigation.graphql",
},
{
keys: [getPlatformAlternateKey(), "W"],
label: "shortcut.navigation.realtime",
},
{
keys: [getPlatformAlternateKey(), "S"],
label: "shortcut.navigation.settings",
},
{
keys: [getPlatformAlternateKey(), "M"],
label: "shortcut.navigation.profile",
},
],
},
{
section: "shortcut.miscellaneous.title",
shortcuts: [
{
keys: [getPlatformSpecialKey(), "M"],
label: "shortcut.miscellaneous.invite",
},
],
},
]
export type ShortcutDef = {
label: string
keys: string[]
section: string
}
export const spotlight = [
{
section: "app.spotlight",
shortcuts: [
{
keys: ["?"],
label: "shortcut.general.help_menu",
action: "modals.support.toggle",
icon: IconLifeBuoy,
},
{
keys: [getPlatformSpecialKey(), "K"],
label: "shortcut.general.show_all",
action: "flyouts.keybinds.toggle",
icon: IconZap,
},
],
},
{
section: "shortcut.navigation.title",
shortcuts: [
{
keys: [getPlatformAlternateKey(), "R"],
label: "shortcut.navigation.rest",
action: "navigation.jump.rest",
icon: IconArrowRight,
},
{
keys: [getPlatformAlternateKey(), "Q"],
label: "shortcut.navigation.graphql",
action: "navigation.jump.graphql",
icon: IconArrowRight,
},
{
keys: [getPlatformAlternateKey(), "W"],
label: "shortcut.navigation.realtime",
action: "navigation.jump.realtime",
icon: IconArrowRight,
},
{
keys: [getPlatformAlternateKey(), "S"],
label: "shortcut.navigation.settings",
action: "navigation.jump.settings",
icon: IconArrowRight,
},
{
keys: [getPlatformAlternateKey(), "M"],
label: "shortcut.navigation.profile",
action: "navigation.jump.profile",
icon: IconArrowRight,
},
],
},
{
section: "shortcut.miscellaneous.title",
shortcuts: [
{
keys: [getPlatformSpecialKey(), "M"],
label: "shortcut.miscellaneous.invite",
action: "modals.share.toggle",
icon: IconGift,
},
],
},
]
export function getShortcuts(t: (x: string) => string): ShortcutDef[] {
// General
return [
{
label: t("shortcut.general.help_menu"),
keys: ["?"],
section: t("shortcut.general.title"),
},
{
label: t("shortcut.general.command_menu"),
keys: ["/"],
section: t("shortcut.general.title"),
},
{
label: t("shortcut.general.show_all"),
keys: [getPlatformSpecialKey(), "K"],
section: t("shortcut.general.title"),
},
{
label: t("shortcut.general.close_current_menu"),
keys: ["ESC"],
section: t("shortcut.general.title"),
},
export const fuse = [
{
keys: ["?"],
label: "shortcut.general.help_menu",
action: "modals.support.toggle",
icon: IconLifeBuoy,
tags: [
"help",
"support",
"menu",
"discord",
"twitter",
"documentation",
"troubleshooting",
"chat",
"community",
"feedback",
"report",
"bug",
"issue",
"ticket",
],
},
{
keys: [getPlatformSpecialKey(), "K"],
label: "shortcut.general.show_all",
action: "flyouts.keybinds.toggle",
icon: IconZap,
tags: ["keyboard", "shortcuts"],
},
{
keys: [getPlatformAlternateKey(), "R"],
label: "shortcut.navigation.rest",
action: "navigation.jump.rest",
icon: IconArrowRight,
tags: ["rest", "jump", "page", "navigation", "go"],
},
{
keys: [getPlatformAlternateKey(), "Q"],
label: "shortcut.navigation.graphql",
action: "navigation.jump.graphql",
icon: IconArrowRight,
tags: ["graphql", "jump", "page", "navigation", "go"],
},
{
keys: [getPlatformAlternateKey(), "W"],
label: "shortcut.navigation.realtime",
action: "navigation.jump.realtime",
icon: IconArrowRight,
tags: [
"realtime",
"jump",
"page",
"navigation",
"websocket",
"socket",
"mqtt",
"sse",
"go",
],
},
{
keys: [getPlatformAlternateKey(), "S"],
label: "shortcut.navigation.settings",
action: "navigation.jump.settings",
icon: IconArrowRight,
tags: ["settings", "jump", "page", "navigation", "account", "theme", "go"],
},
{
keys: [getPlatformAlternateKey(), "M"],
label: "shortcut.navigation.profile",
action: "navigation.jump.profile",
icon: IconArrowRight,
tags: ["profile", "jump", "page", "navigation", "account", "theme", "go"],
},
{
keys: [getPlatformSpecialKey(), "M"],
label: "shortcut.miscellaneous.invite",
action: "modals.share.toggle",
icon: IconGift,
tags: ["invite", "share", "app", "friends", "people", "social"],
},
{
keys: [getPlatformAlternateKey(), "0"],
label: "shortcut.theme.system",
action: "settings.theme.system",
icon: IconMonitor,
tags: ["theme", "system"],
},
{
keys: [getPlatformAlternateKey(), "1"],
label: "shortcut.theme.light",
action: "settings.theme.light",
icon: IconSun,
tags: ["theme", "light"],
},
{
keys: [getPlatformAlternateKey(), "2"],
label: "shortcut.theme.dark",
action: "settings.theme.dark",
icon: IconCloud,
tags: ["theme", "dark"],
},
{
keys: [getPlatformAlternateKey(), "3"],
label: "shortcut.theme.black",
action: "settings.theme.black",
icon: IconMoon,
tags: ["theme", "black"],
},
]
// Request
{
label: t("shortcut.request.send_request"),
keys: [getPlatformSpecialKey(), "↩"],
section: t("shortcut.request.title"),
},
{
keys: [getPlatformSpecialKey(), "S"],
label: t("shortcut.request.save_to_collections"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformSpecialKey(), "U"],
label: t("shortcut.request.copy_request_link"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformSpecialKey(), "I"],
label: t("shortcut.request.reset_request"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformAlternateKey(), "↑"],
label: t("shortcut.request.next_method"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformAlternateKey(), "↓"],
label: t("shortcut.request.previous_method"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformAlternateKey(), "G"],
label: t("shortcut.request.get_method"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformAlternateKey(), "H"],
label: t("shortcut.request.head_method"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformAlternateKey(), "P"],
label: t("shortcut.request.post_method"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformAlternateKey(), "U"],
label: t("shortcut.request.put_method"),
section: t("shortcut.request.title"),
},
{
keys: [getPlatformAlternateKey(), "X"],
label: t("shortcut.request.delete_method"),
section: t("shortcut.request.title"),
},
// Response
{
keys: [getPlatformSpecialKey(), "J"],
label: t("shortcut.response.download"),
section: t("shortcut.response.title"),
},
{
keys: [getPlatformSpecialKey(), "."],
label: t("shortcut.response.copy"),
section: t("shortcut.response.title"),
},
// Navigation
{
keys: [getPlatformSpecialKey(), "←"],
label: t("shortcut.navigation.back"),
section: t("shortcut.navigation.title"),
},
{
keys: [getPlatformSpecialKey(), ""],
label: t("shortcut.navigation.forward"),
section: t("shortcut.navigation.title"),
},
{
keys: [getPlatformAlternateKey(), "R"],
label: t("shortcut.navigation.rest"),
section: t("shortcut.navigation.title"),
},
{
keys: [getPlatformAlternateKey(), "Q"],
label: t("shortcut.navigation.graphql"),
section: t("shortcut.navigation.title"),
},
{
keys: [getPlatformAlternateKey(), "W"],
label: t("shortcut.navigation.realtime"),
section: t("shortcut.navigation.title"),
},
{
keys: [getPlatformAlternateKey(), "S"],
label: t("shortcut.navigation.settings"),
section: t("shortcut.navigation.title"),
},
{
keys: [getPlatformAlternateKey(), "M"],
label: t("shortcut.navigation.profile"),
section: t("shortcut.navigation.title"),
},
// Miscellaneous
{
keys: [getPlatformSpecialKey(), "M"],
label: t("shortcut.miscellaneous.invite"),
section: t("shortcut.miscellaneous.title"),
},
]
}