chore: types for $

This commit is contained in:
EnixCoda 2021-02-01 18:03:15 +08:00
parent eb6343a1ff
commit e066675817
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
4 changed files with 23 additions and 25 deletions

View file

@ -31,7 +31,7 @@ export function isInCodePage() {
}
export function getIssueTitle() {
const title: string | undefined = $('.gh-header-title')?.textContent
const title = $('.gh-header-title')?.textContent
return title?.trim().replace(/\n/g, '')
}
@ -40,7 +40,7 @@ export function getCurrentBranch(passive = false) {
'.repository-content #branch-select-menu summary',
'.repository-content .branch-select-menu summary',
].join()
const branchButtonElement: HTMLElement = $(selectedBranchButtonSelector)
const branchButtonElement = $(selectedBranchButtonSelector)
if (branchButtonElement) {
const branchNameSpanElement = branchButtonElement.querySelector('span')
if (branchNameSpanElement) {

View file

@ -13,12 +13,11 @@ export function isInCodePage() {
export function getCurrentBranch() {
const branchListSelector = '.reference'
const branchButtonElement: HTMLElement = $(branchListSelector)
const branchNameElement = branchButtonElement.querySelector('.text > strong')
const branchButtonElement = $(branchListSelector)
const branchNameElement = branchButtonElement?.querySelector('.text > strong')
if (branchNameElement) {
return branchNameElement.textContent;
return branchNameElement.textContent
}
raiseError(new Error('cannot get current branch'))
}

View file

@ -15,14 +15,11 @@ export function isInCodePage() {
}
export function getCurrentBranch() {
const selectedBranchButtonSelector = '#git-project-branch'
const branchButtonElement: HTMLElement = $(selectedBranchButtonSelector)
if (branchButtonElement) {
const branchNameSpanElement = branchButtonElement.querySelector('.text')
if (branchNameSpanElement) {
const partialBranchNameFromInnerText = branchNameSpanElement.textContent
if (!partialBranchNameFromInnerText?.includes('…')) return partialBranchNameFromInnerText
}
const selectedBranchButtonSelector = '#git-project-branch .text'
const element = $(selectedBranchButtonSelector)
if (element) {
const partialBranchNameFromInnerText = element.textContent
if (!partialBranchNameFromInnerText?.includes('…')) return partialBranchNameFromInnerText
}
raiseError(new Error('cannot get current branch'))

View file

@ -26,20 +26,22 @@ export function setBodyIndent(shouldShowGitako: boolean) {
}
}
export function $<EE extends Element, E extends (element: EE) => any, O extends () => any>(
export function $(selector: string): HTMLElement | null
export function $<T1>(selector: string, existCallback: (element: HTMLElement) => T1): T1
export function $<T1, T2>(
selector: string,
existCallback?: E,
otherwise?: O,
): E extends never
? O extends never
? Element | null
: ReturnType<O> | null
: O extends never
? ReturnType<E> | null
: ReturnType<O> | ReturnType<E> {
existCallback: (element: HTMLElement) => T1,
otherwise: () => T2,
): T1 | T2
export function $<T2>(
selector: string,
existCallback: undefined | null,
otherwise: () => T2,
): HTMLElement | null | T2
export function $(selector: string, existCallback?: any, otherwise?: any) {
const element = document.querySelector(selector)
if (element) {
return existCallback ? existCallback(element as EE) : element
return existCallback ? existCallback(element) : element
}
return otherwise ? otherwise() : null
}