|
1 | 1 | <template> |
2 | 2 | <XMask :show="show" @close="close"> |
3 | 3 | <QuickOpen |
4 | | - @choose-file="chooseFile" |
| 4 | + ref="quickOpen" |
| 5 | + @choose-item="chooseItem" |
5 | 6 | @close="close" |
6 | | - :filter-item="filterItem" |
7 | | - :only-current-repo="onlyCurrentRepo" /> |
| 7 | + :filter-item="filterItem" /> |
8 | 8 | </XMask> |
9 | 9 | </template> |
10 | 10 |
|
11 | 11 | <script lang="ts"> |
12 | | -import { computed, defineComponent, onMounted, onUnmounted, ref, shallowRef } from 'vue' |
| 12 | +import { computed, defineComponent, nextTick, onMounted, onUnmounted, ref, shallowRef } from 'vue' |
13 | 13 | import { registerAction, removeAction } from '@fe/core/action' |
14 | 14 | import { CtrlCmd } from '@fe/core/keybinding' |
15 | 15 | import { switchDoc } from '@fe/services/document' |
16 | 16 | import { t } from '@fe/services/i18n' |
17 | | -import type { Doc, BaseDoc } from '@fe/types' |
| 17 | +import type { BaseDoc, Components, Doc } from '@fe/types' |
| 18 | +import { isMarkdownFile } from '@share/misc' |
| 19 | +import store from '@fe/support/store' |
18 | 20 | import XMask from './Mask.vue' |
19 | 21 | import QuickOpen from './QuickOpen.vue' |
20 | | -import { isMarkdownFile } from '@share/misc' |
21 | 22 |
|
22 | 23 | export default defineComponent({ |
23 | 24 | name: 'x-filter', |
24 | 25 | components: { QuickOpen, XMask }, |
25 | 26 | setup () { |
26 | | - const callback = ref<((doc: Doc | null) => void) | null>(null) |
27 | | - const onlyCurrentRepo = ref(false) |
28 | | - const filterItem = shallowRef<(item: BaseDoc) => boolean>() |
| 27 | + const callback = ref<((item: Components.QuickOpen.DataItem | null) => void) | null>(null) |
| 28 | + const filterItem = shallowRef<(item: Components.QuickOpen.DataItem) => boolean>() |
| 29 | + const quickOpen = ref<InstanceType<typeof QuickOpen> | null>(null) |
29 | 30 |
|
30 | | - function showQuickOpen () { |
31 | | - onlyCurrentRepo.value = false |
32 | | - callback.value = (f: Doc | null) => { |
33 | | - if (f) { |
34 | | - switchDoc(f) |
| 31 | + function bindAction ( |
| 32 | + cb: (item: Components.QuickOpen.DataItem | null) => void, |
| 33 | + filter: (item: Components.QuickOpen.DataItem) => boolean |
| 34 | + ) { |
| 35 | + filterItem.value = filter |
| 36 | + callback.value = (item: Components.QuickOpen.DataItem | null) => { |
| 37 | + if (item?.type === 'tag') { |
| 38 | + quickOpen.value?.switchTab('file') |
| 39 | + quickOpen.value?.updateSearchText(item.payload + ' ') |
| 40 | + } else { |
| 41 | + cb(item) |
| 42 | + callback.value = null |
| 43 | + filterItem.value = undefined |
35 | 44 | } |
36 | | -
|
37 | | - callback.value = null |
38 | | - filterItem.value = undefined |
39 | 45 | } |
40 | 46 | } |
41 | 47 |
|
42 | | - function chooseFile (file: Doc | null) { |
| 48 | + function showQuickOpen (options?: { query?: string, tab?: Components.QuickOpen.TabKey }) { |
| 49 | + bindAction( |
| 50 | + (f: Components.QuickOpen.DataItem | null) => { |
| 51 | + if (f?.type === 'file') { |
| 52 | + switchDoc(f.payload as Doc) |
| 53 | + } |
| 54 | + }, |
| 55 | + () => true |
| 56 | + ) |
| 57 | +
|
| 58 | + nextTick(() => { |
| 59 | + if (options?.tab) { |
| 60 | + quickOpen.value?.switchTab(options.tab) |
| 61 | + } |
| 62 | +
|
| 63 | + if (typeof options?.query === 'string') { |
| 64 | + quickOpen.value?.updateSearchText(options.query) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +
|
| 69 | + function chooseItem (item: Components.QuickOpen.DataItem | null) { |
43 | 70 | if (callback.value) { |
44 | | - callback.value(file) |
| 71 | + callback.value(item) |
45 | 72 | } |
46 | 73 | } |
47 | 74 |
|
48 | 75 | function chooseDocument (filter = (item: BaseDoc) => isMarkdownFile(item.path)) { |
49 | | - return new Promise<Doc | null>(resolve => { |
50 | | - callback.value = (f: Doc | null) => { |
51 | | - resolve(f) |
52 | | - callback.value = null |
53 | | - filterItem.value = undefined |
54 | | - } |
| 76 | + return new Promise<BaseDoc | null>(resolve => { |
| 77 | + bindAction( |
| 78 | + (item: Components.QuickOpen.DataItem | null) => { |
| 79 | + resolve((item && item.type === 'file') ? item.payload : null) |
| 80 | + }, |
| 81 | + (item: Components.QuickOpen.DataItem) => { |
| 82 | + // if item is a tag, always show it |
| 83 | + if (item.type === 'tag') { |
| 84 | + return true |
| 85 | + } |
55 | 86 |
|
56 | | - onlyCurrentRepo.value = true |
57 | | - filterItem.value = filter |
| 87 | + return item.type === 'file' && |
| 88 | + item.payload.repo === store.state.currentRepo?.name && // only current repo |
| 89 | + filter(item.payload as BaseDoc) |
| 90 | + }) |
58 | 91 | }) |
59 | 92 | } |
60 | 93 |
|
@@ -88,9 +121,9 @@ export default defineComponent({ |
88 | 121 | show, |
89 | 122 | close, |
90 | 123 | callback, |
91 | | - chooseFile, |
92 | | - onlyCurrentRepo, |
| 124 | + chooseItem, |
93 | 125 | filterItem, |
| 126 | + quickOpen, |
94 | 127 | } |
95 | 128 | }, |
96 | 129 | }) |
|
0 commit comments