Skip to content

Commit c205b16

Browse files
committed
feat: implement entry provider
1 parent 3fbfee5 commit c205b16

File tree

9 files changed

+120
-26
lines changed

9 files changed

+120
-26
lines changed

packages/app/src/components/IsAppDrawerMini.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const links = [
1414
{
1515
to: '/app-pages/directory',
1616
icon: 'mdi:folder',
17-
label: 'Explorer'
17+
label: 'fileExplorer'
1818
},
1919
{
2020
to: '/workspace-selector',

packages/app/src/composables/defineEntryMiddleware.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import type { DriveEntry } from "./useDrive";
2-
import type { RouteRecordRaw, Router } from 'vue-router'
2+
import type { RouteLocationRaw, Router } from 'vue-router'
33

44
export interface EntryMiddlewareContext {
55
entry: DriveEntry
6-
route: RouteRecordRaw
7-
router: Router
86
}
97

108
export interface EntryMiddleware {
11-
(context: EntryMiddlewareContext): Promise<any>
9+
(context: EntryMiddlewareContext): Promise<RouteLocationRaw | null> | RouteLocationRaw | null
1210
}
1311

1412
export function defineEntryMiddleware(middleware: EntryMiddleware) {

packages/app/src/composables/useDrive.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface DriveEntry {
55
}
66

77
export interface Drive {
8+
get: (path: string) => Promise<DriveEntry | null>
89
list: (path: string) => Promise<DriveEntry[]>
910
read: (path: string) => Promise<string>
1011
write: (path: string, content: any) => Promise<void>

packages/app/src/composables/useDriveFileSystemApi.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import type { Drive, DriveEntry } from "./useDrive";
22

33
export function useDriveFileSystemApi(handle: FileSystemDirectoryHandle): Drive {
44

5+
const dirname = (path: string) => {
6+
const args = path.split('/').slice(0, -1).join('/')
7+
8+
return args === '' ? '/' : args
9+
}
10+
511
const list: Drive['list'] = async (path) => {
612

713
const result = [] as DriveEntry[]
@@ -35,6 +41,24 @@ export function useDriveFileSystemApi(handle: FileSystemDirectoryHandle): Drive
3541
return result
3642
}
3743

44+
const get: Drive['get'] = async (path) => {
45+
46+
if (path === '/') {
47+
return {
48+
path: '/',
49+
type: 'directory'
50+
}
51+
}
52+
53+
const directory = dirname(path)
54+
55+
const allParent = await list(dirname(path))
56+
57+
const entry = allParent.find(e => e.path === path.split('/').pop())
58+
59+
return entry || null
60+
}
61+
3862
const read: Drive['read'] = async (path) => {
3963
throw new Error('Not implemented')
4064
}
@@ -43,5 +67,5 @@ export function useDriveFileSystemApi(handle: FileSystemDirectoryHandle): Drive
4367
throw new Error('Not implemented')
4468
}
4569

46-
return { list, read, write }
70+
return { list, get, read, write }
4771
}

packages/app/src/pages/AppPage/AppPage.vue

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
<script lang="ts" setup>
22
33
import type { AppPage } from '@/composables/defineAppPage'
4-
import Directory from './AppPageDirectory.vue';
4+
import AppPageDirectory from './AppPageDirectory.vue';
5+
import AppPageFile from './AppPageFile.vue';
56
67
const name = defineProp('name', {
78
type: String,
89
required: true
910
})
1011
11-
const pages = shallowRef<AppPage[]>([])
12-
13-
const directoryPage = defineAppPage({
14-
name: 'directory',
15-
component: Directory,
16-
})
17-
18-
pages.value.push(directoryPage)
12+
const pages = shallowRef<AppPage[]>([
13+
defineAppPage({ name: 'directory', component: AppPageDirectory }),
14+
defineAppPage({ name: 'file', component: AppPageFile }),
15+
])
1916
2017
const current = computed(() => pages.value.find(p => p.name === name.value))
2118
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script lang="ts" setup>
2+
const route = useRoute()
3+
4+
const path = computed(() => {
5+
return route.query.path as string
6+
})
7+
</script>
8+
9+
<template>
10+
<div>
11+
File {{ path }}
12+
</div>
13+
</template>
Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,78 @@
11
<script lang="ts" setup>
2-
3-
import type { AppPage } from '@/composables/defineAppPage'
42
import type { EntryMiddleware } from '@/composables/defineEntryMiddleware'
3+
import type { RouteLocationRaw } from 'vue-router';
4+
5+
// general
6+
7+
const router = useRouter()
8+
const { drive } = useDrive()
59
10+
// middlewares
611
const middlewares = ref<EntryMiddleware[]>([])
712
13+
const main = defineEntryMiddleware((ctx) => {
14+
if (ctx.entry.type !== 'directory') {
15+
return {
16+
path: '/app-pages/file',
17+
query: {
18+
path: ctx.entry.path
19+
}
20+
}
21+
}
22+
23+
return {
24+
path: '/app-pages/directory',
25+
query: {
26+
path: ctx.entry.path
27+
}
28+
}
29+
})
30+
31+
middlewares.value.push(main)
32+
33+
// load
34+
35+
const path = defineProp<string | string[]>('path', {
36+
type: [String, Array],
37+
default: '/'
38+
})
39+
40+
async function load(){
41+
42+
const args = Array.isArray(path.value) ? path.value : [path.value]
43+
44+
const filename = `/${args.join('/')}`
45+
46+
const entry = await drive.value.get(filename)
47+
48+
if (!entry) {
49+
alert('Entry not found')
50+
return
51+
}
52+
53+
let result: RouteLocationRaw | undefined
54+
55+
for await (const middleware of middlewares.value) {
56+
const middlewareResult = await middleware({ entry })
57+
58+
if (middlewareResult) {
59+
result = middlewareResult
60+
}
61+
}
62+
63+
if (!result) {
64+
alert('Route not found')
65+
return
66+
}
67+
68+
router.replace(result)
69+
}
70+
71+
onMounted(load)
72+
73+
874
</script>
975

1076
<template>
11-
<div>file provider</div>
77+
<div />
1278
</template>

packages/app/src/pages/EntryProvider/middlewares/directory.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/app/src/pages/WorkspaceSelector/WorkspaceSelector.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ async function select(workspace: Workspace){
4343
4444
setDrive(workspaceDrive)
4545
46-
router.push('/app-pages/directory')
46+
setTimeout(() => {
47+
router.push('/entries')
48+
}, 100)
4749
}
4850
4951
</script>

0 commit comments

Comments
 (0)