Fix type errors in src/components/launch-bar

This commit is contained in:
timongh 2023-02-09 19:26:42 +08:00
parent 495b52a095
commit a666e2af7a
2 changed files with 24 additions and 20 deletions

View File

@ -22,7 +22,7 @@
:name="action.name" :name="action.name"
></component> ></component>
<div v-else class="suggest-item-name"> <div v-else class="suggest-item-name">
{{ action.title || action.name }} {{ action['title'] || action.name }}
</div> </div>
<div v-if="action.description" class="suggest-item-description"> <div v-if="action.description" class="suggest-item-description">
{{ action.description }} {{ action.description }}
@ -55,18 +55,23 @@ export default defineComponent({
required: true, required: true,
}, },
}, },
emits: ['action', 'delete-item', 'previous-item', 'next-item'], emits: {
action: null as (e: KeyboardEvent | MouseEvent) => true,
'delete-item': null as (e: KeyboardEvent | MouseEvent) => true,
'previous-item': null as (e: KeyboardEvent) => true,
'next-item': null as (e: KeyboardEvent) => true,
},
methods: { methods: {
performAction(event: KeyboardEvent | MouseEvent) { performAction(event: Event) {
this.action.action() this.action.action()
this.$emit('action', event) this.$emit('action', event as KeyboardEvent | MouseEvent)
}, },
performDelete(event: KeyboardEvent | MouseEvent) { performDelete(event: Event) {
if (!this.action.deleteAction) { if (!this.action.deleteAction) {
return return
} }
this.action.deleteAction() this.action.deleteAction()
this.$emit('delete-item', event) this.$emit('delete-item', event as KeyboardEvent | MouseEvent)
}, },
}, },
}) })

View File

@ -33,7 +33,7 @@
@delete-item="onDeleteItem($event, index)" @delete-item="onDeleteItem($event, index)"
@action=" @action="
index === actions.length - 1 && onClearHistory() index === actions.length - 1 && onClearHistory()
onAction(a) onAction()
" "
/> />
</div> </div>
@ -55,7 +55,7 @@
@previous-item="previousItem($event, index)" @previous-item="previousItem($event, index)"
@next-item="nextItem($event, index)" @next-item="nextItem($event, index)"
@delete-item="onDeleteItem($event, index)" @delete-item="onDeleteItem($event, index)"
@action="onAction(a)" @action="onAction"
/> />
</div> </div>
</div> </div>
@ -115,15 +115,14 @@ async function getOnlineActions(this: InstanceType<typeof ThisComponent>) {
this.actions = fuseResult.map(it => it.item).slice(0, 12) this.actions = fuseResult.map(it => it.item).slice(0, 12)
this.noActions = this.actions.length === 0 this.noActions = this.actions.length === 0
} }
async function getActions() { async function getActions(this: InstanceType<typeof ThisComponent>) {
this.noActions = false this.noActions = false
if (this.isHistory) { if (this.isHistory) {
this.actions = generateKeys(historyProvider, await historyProvider.getActions(this.keyword)) this.actions = generateKeys(historyProvider, await historyProvider.getActions(this.keyword))
return return
} }
const actions: LaunchBarAction[] = [] this.actions = []
this.actions = actions this.getOnlineActions().then()
this.getOnlineActions()
} }
const [recommended] = registerAndGetData('launchBar.recommended', { const [recommended] = registerAndGetData('launchBar.recommended', {
@ -164,7 +163,7 @@ const ThisComponent = defineComponent({
}, },
}, },
async mounted() { async mounted() {
this.getActions() this.getActions().then()
if (!matchUrlPattern(/^https?:\/\/search\.bilibili\.com/)) { if (!matchUrlPattern(/^https?:\/\/search\.bilibili\.com/)) {
return return
} }
@ -190,12 +189,12 @@ const ThisComponent = defineComponent({
this.$emit('close') this.$emit('close')
this.getActions() this.getActions()
}, },
async handleEnter(e: KeyboardEvent) { async handleEnter(e: KeyboardEvent | MouseEvent) {
if (e.isComposing) { if ('isComposing' in e && e.isComposing) {
return return
} }
if (this.actions.length > 0 && !this.isHistory) { if (this.actions.length > 0 && !this.isHistory) {
const [first] = this.actions as LaunchBarAction[] const [first] = this.actions
if (first.explicitSelect === false) { if (first.explicitSelect === false) {
first.action() first.action()
return return
@ -213,17 +212,17 @@ const ThisComponent = defineComponent({
if (e.isComposing) { if (e.isComposing) {
return return
} }
this.list.querySelector('.suggest-item:last-child').focus() ;(this.list.querySelector('.suggest-item:last-child') as HTMLElement).focus()
e.preventDefault() e.preventDefault()
}, },
handleDown(e: KeyboardEvent) { handleDown(e: KeyboardEvent) {
if (e.isComposing) { if (e.isComposing) {
return return
} }
this.list.querySelector('.suggest-item').focus() ;(this.list.querySelector('.suggest-item') as HTMLElement).focus()
e.preventDefault() e.preventDefault()
}, },
previousItem(e: KeyboardEvent, index: number) { previousItem(e: KeyboardEvent | MouseEvent, index: number) {
if (index === 0) { if (index === 0) {
this.focus() this.focus()
} else { } else {
@ -239,7 +238,7 @@ const ThisComponent = defineComponent({
} }
}, },
search, search,
onDeleteItem(e: Event, index: number) { onDeleteItem(e: KeyboardEvent | MouseEvent, index: number) {
this.previousItem(e, index) this.previousItem(e, index)
this.getActions() this.getActions()
}, },