49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import type { CommandOption } from "@/context/command"
|
|
import { batch } from "solid-js"
|
|
|
|
export const focusTerminalById = (id: string) => {
|
|
const wrapper = document.getElementById(`terminal-wrapper-${id}`)
|
|
const terminal = wrapper?.querySelector('[data-component="terminal"]')
|
|
if (!(terminal instanceof HTMLElement)) return false
|
|
|
|
const textarea = terminal.querySelector("textarea")
|
|
if (textarea instanceof HTMLTextAreaElement) {
|
|
textarea.focus()
|
|
return true
|
|
}
|
|
|
|
terminal.focus()
|
|
terminal.dispatchEvent(
|
|
typeof PointerEvent === "function"
|
|
? new PointerEvent("pointerdown", { bubbles: true, cancelable: true })
|
|
: new MouseEvent("pointerdown", { bubbles: true, cancelable: true }),
|
|
)
|
|
return true
|
|
}
|
|
|
|
export const createOpenReviewFile = (input: {
|
|
showAllFiles: () => void
|
|
tabForPath: (path: string) => string
|
|
openTab: (tab: string) => void
|
|
loadFile: (path: string) => void
|
|
}) => {
|
|
return (path: string) => {
|
|
batch(() => {
|
|
input.showAllFiles()
|
|
input.openTab(input.tabForPath(path))
|
|
input.loadFile(path)
|
|
})
|
|
}
|
|
}
|
|
|
|
export const combineCommandSections = (sections: readonly (readonly CommandOption[])[]) => {
|
|
return sections.flatMap((section) => section)
|
|
}
|
|
|
|
export const getTabReorderIndex = (tabs: readonly string[], from: string, to: string) => {
|
|
const fromIndex = tabs.indexOf(from)
|
|
const toIndex = tabs.indexOf(to)
|
|
if (fromIndex === -1 || toIndex === -1 || fromIndex === toIndex) return undefined
|
|
return toIndex
|
|
}
|