diff --git a/src/renderer/components/IndexStatus.vue b/src/renderer/components/IndexStatus.vue index e20914af3..c6358d9b0 100644 --- a/src/renderer/components/IndexStatus.vue +++ b/src/renderer/components/IndexStatus.vue @@ -35,13 +35,14 @@ function handleCommand (e: MouseEvent) { return } - const command = (e.target as HTMLElement).dataset.command + const target = e.target as HTMLElement + const command = target.dataset.command || target.parentElement?.dataset.command if (command === 'switch-repository') { - setCurrentRepo(currentFile.value.repo) e.preventDefault() + setCurrentRepo(currentFile.value.repo) } else if (command === 'enable-indexing') { - toggleRepoIndexing(currentRepo.value.name, true) e.preventDefault() + toggleRepoIndexing(currentRepo.value.name, true) } } diff --git a/src/renderer/plugins/view-links.tsx b/src/renderer/plugins/view-links.tsx index 25ee6b306..ab4c8b2db 100644 --- a/src/renderer/plugins/view-links.tsx +++ b/src/renderer/plugins/view-links.tsx @@ -19,18 +19,18 @@ export default { const list = ctx.lib.vue.ref<(ListItem[] | null)>(null) const { $t } = ctx.i18n.useI18n() - const buildList = (type: TabItemValue) => { - const currentRepoName = ctx.store.state.currentRepo?.name - const currentFilePath = ctx.store.state.currentFile?.path - const currentFileName = ctx.store.state.currentFile?.name + const currentFileRepo = ctx.store.state.currentFile?.repo + const currentFilePath = ctx.store.state.currentFile?.path + const currentFileName = ctx.store.state.currentFile?.name + const buildList = (type: TabItemValue) => { list.value = null title.value = currentFileName ? { links: $t.value('view-links.links-in', currentFileName), 'back-links': $t.value('view-links.back-links-for', currentFileName), }[type] : '' - if (!ctx.store.state.currentRepoIndexStatus?.status.ready || currentRepoName !== ctx.store.state.currentRepoIndexStatus?.repo || !currentFilePath) { + if (!ctx.store.state.currentRepoIndexStatus?.status.ready || currentFileRepo !== ctx.store.state.currentRepoIndexStatus?.repo || !currentFilePath) { list.value = [] return } @@ -38,10 +38,10 @@ export default { const dm = ctx.indexer.getDocumentsManager() if (type === 'links') { - dm.findByRepoAndPath(currentRepoName, currentFilePath).then(doc => { - if (currentTab.value === 'links' && currentFilePath === ctx.store.state.currentFile?.path) { + dm.findByRepoAndPath(currentFileRepo, currentFilePath).then(doc => { + if (currentTab.value === 'links') { list.value = doc ? doc.links.filter(link => !!link.internal).map(link => ({ - doc: { type: 'file', repo: currentRepoName, path: link.internal!, name: ctx.utils.path.basename(link.internal!) }, + doc: { type: 'file', repo: currentFileRepo, path: link.internal!, name: ctx.utils.path.basename(link.internal!) }, position: link.position })) : [] } @@ -49,15 +49,15 @@ export default { } else if (type === 'back-links') { const data: ListItem[] = [] - dm.getTable().where({ repo: currentRepoName }).each(doc => { + dm.getTable().where({ repo: currentFileRepo }).each(doc => { doc.links.forEach(link => { if (link.internal === currentFilePath) { const position: PositionState = { line: link.blockMap[0] + 1 } - data.push({ doc: { type: 'file', repo: currentRepoName, path: doc.path, name: ctx.utils.path.basename(doc.path) }, position }) + data.push({ doc: { type: 'file', repo: currentFileRepo, path: doc.path, name: ctx.utils.path.basename(doc.path) }, position }) } }) }).then(() => { - if (currentTab.value === 'back-links' && currentFilePath === ctx.store.state.currentFile?.path) { + if (currentTab.value === 'back-links') { list.value = data } }) @@ -68,17 +68,28 @@ export default { ctx.ui.useFixedFloat().hide() } + const handleKeyDown = (e: KeyboardEvent) => { + if (e.key === 'Escape') { + close() + } + } + ctx.lib.vue.watch(() => currentTab.value, buildList) ctx.lib.vue.watch(() => ctx.store.state.currentRepoIndexStatus?.status.ready, () => buildList(currentTab.value)) + ctx.lib.vue.watch(() => [ctx.store.state.currentFile?.repo, ctx.store.state.currentRepo?.name], (val) => { + if (val[0] !== currentFileRepo || val[1] !== currentFileRepo) { + close() + } + }, { flush: 'post' }) ctx.lib.vue.onMounted(() => { buildList(currentTab.value) - ctx.registerHook('GLOBAL_KEYDOWN', e => { - if (e.key === 'Escape') { - close() - } - }) + ctx.registerHook('GLOBAL_KEYDOWN', handleKeyDown) + }) + + ctx.lib.vue.onBeforeUnmount(() => { + ctx.removeHook('GLOBAL_KEYDOWN', handleKeyDown) }) return () =>