| 1 | import type { MaybeRefOrGetter } from 'reactive-vscode' |
| 2 | import type { TextDocument } from 'vscode' |
| 3 | import { slash } from '@antfu/utils' |
| 4 | import { computed, toValue } from 'reactive-vscode' |
| 5 | import { activeProject, projects } from '../projects' |
| 6 | |
| 7 | export function getProjectFromDoc(doc: TextDocument | undefined) { |
| 8 | if (!doc) |
| 9 | return null |
| 10 | const path = slash(doc.uri.fsPath) |
| 11 | const md = activeProject.value?.data.markdownFiles[path] |
| 12 | if (md) |
| 13 | return { project: activeProject.value!, md } |
| 14 | for (const project of projects.values()) { |
| 15 | const md = project.data.markdownFiles[path] |
| 16 | if (md) |
| 17 | return { project, md } |
| 18 | } |
| 19 | return null |
| 20 | } |
| 21 | |
| 22 | export function useProjectFromDoc(doc: MaybeRefOrGetter<TextDocument | undefined>) { |
| 23 | return computed(() => getProjectFromDoc(toValue(doc))) |
| 24 | } |
| 25 |