Skip to content

Commit

Permalink
fix: improve command handling and refactor repository checks in Index…
Browse files Browse the repository at this point in the history
…Status component
  • Loading branch information
purocean committed Nov 23, 2024
1 parent ec2da56 commit 6c7d3f4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
7 changes: 4 additions & 3 deletions src/renderer/components/IndexStatus.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
</script>
Expand Down
43 changes: 27 additions & 16 deletions src/renderer/plugins/view-links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,45 @@ 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
}

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
})) : []
}
})
} 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
}
})
Expand All @@ -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 () => <IndexStatus>
Expand Down

0 comments on commit 6c7d3f4

Please sign in to comment.