Skip to content

Commit

Permalink
feat(devtools): add postList & open in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Feb 12, 2024
1 parent d2e4ca0 commit 719049b
Show file tree
Hide file tree
Showing 35 changed files with 370 additions and 254 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"demo:yun": "pnpm -C demo/yun run dev",
"dev:lib": "pnpm -C packages/valaxy run dev",
"dev": "pnpm -r --filter=./packages/** --parallel run dev",
"devtools": "pnpm -C packages/devtools run dev",
"docs": "pnpm -C docs run dev",
"docs:dev": "pnpm -C docs run dev",
"docs:build": "pnpm -C docs run build",
Expand Down
13 changes: 13 additions & 0 deletions packages/devtools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# @valaxyjs/devtools

## Dev

```bash
# in packages/devtools
pnpm dev
```

```bash
# in root
pnpm devtools
```
5 changes: 5 additions & 0 deletions packages/devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"build:node": "unbuild",
"dev": "npm run stub && npm run dev:client",
"dev:client": "vite build src/client --watch",
"dev:src": "vite dev src/client",
"stub": "unbuild --stub",
"prepublishOnly": "npm run build",
"release": "bumpp && npm publish"
Expand All @@ -32,8 +33,12 @@
"sirv": "^2.0.4"
},
"devDependencies": {
"@iconify-json/ri": "^1.1.19",
"@types/splitpanes": "^2.2.6",
"splitpanes": "^3.1.5",
"typescript": "^5.3.3",
"unbuild": "^2.0.0",
"unplugin-vue-router": "^0.7.0",
"vite": "^5.1.1"
}
}
19 changes: 1 addition & 18 deletions packages/devtools/src/client/App.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
<script setup lang="ts">
import { onMounted } from 'vue'
import { isStaticMode } from './utils'
onMounted(() => {
if (isStaticMode)
document.title = 'Vite Inspect (Production)'
})
</script>

<template>
<main grid="~ rows-[min-content_1fr]" size="h-screen w-screen" text="gray-700 dark:gray-200">
<Suspense>
<RouterView />
<template #fallback>
Loading...
</template>
</Suspense>
</main>
<RouterView />
</template>
15 changes: 15 additions & 0 deletions packages/devtools/src/client/components/PageFrontmatter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script lang="ts" setup>
defineProps<{
frontmatter?: object
}>()
</script>

<template>
<div>
<ul v-if="frontmatter">
<li v-for="(value, key) in frontmatter" :key="key">
<strong>{{ key }}</strong>: {{ value }}
</li>
</ul>
</div>
</template>
51 changes: 51 additions & 0 deletions packages/devtools/src/client/components/VDPostList.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import type { Router } from 'vue-router/auto'
import { getGlobalValaxyProperty, getWindowProperty, openInEditor } from '../utils'
import { frontmatter } from '../composables/app'
const activePath = ref('')
const __VUE_DEVTOOLS_ROUTER__ = getWindowProperty('__VUE_DEVTOOLS_ROUTER__') as Router
__VUE_DEVTOOLS_ROUTER__.beforeEach((to, _from, next) => {
activePath.value = to.path
frontmatter.value = getWindowProperty('$frontmatter')
next()
})
const postList = ref()
onMounted(() => {
postList.value = getGlobalValaxyProperty('postList').value
})
function onClickPost(post: any) {
__VUE_DEVTOOLS_ROUTER__.push(post.path)
}
function launchEditor() {
openInEditor({
file: getWindowProperty('$pageData').path,
})
}
</script>

<template>
<ul class="h-full" overflow="auto" pl="12" pr="4" py="4">
{{ activePath }}
<li v-for="post in postList" :key="post.path" class="list-decimal">
<div flex>
<span
class="inline-flex flex-grow cursor-pointer underline hover:text-blue-500"
:class="{ 'text-blue-500': activePath === post.path }"
@click="onClickPost(post)"
>
{{ post.title }}
</span>
<button class="ml-2 text-xs" @click="launchEditor()">
<div i-vscode-icons:file-type-vscode />
</button>
</div>
</li>
</ul>
</template>
3 changes: 3 additions & 0 deletions packages/devtools/src/client/composables/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ref } from 'vue'

export const frontmatter = ref<object>({})
2 changes: 1 addition & 1 deletion packages/devtools/src/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>

<body data-vite-inspect-mode="DEV">
<div id="app"></div>
<div id="app" class="h-full"></div>
<script>
(function () {
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
Expand Down
9 changes: 6 additions & 3 deletions packages/devtools/src/client/main.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// register vue composition api globally
import { createApp } from 'vue'
import { createRouter, createWebHashHistory } from 'vue-router'
import { routes } from 'vue-router/auto/routes'
import { createRouter, createWebHashHistory } from 'vue-router/auto'
import App from './App.vue'

import '@unocss/reset/tailwind.css'
import 'uno.css'

import './styles/index.css'
import 'splitpanes/dist/splitpanes.css'

const app = createApp(App)

const router = createRouter({
history: createWebHashHistory(),
routes,
})

app.use(router)
app.mount('#app')
5 changes: 5 additions & 0 deletions packages/devtools/src/client/pages/about.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
About
</div>
</template>
5 changes: 5 additions & 0 deletions packages/devtools/src/client/pages/categories.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
Categories
</div>
</template>
39 changes: 36 additions & 3 deletions packages/devtools/src/client/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
<script lang="ts" setup>
import { Pane, Splitpanes } from 'splitpanes'
import { onMounted } from 'vue'
import { getWindowProperty, isStaticMode } from '../utils'
import type { BlogWindow } from '../types'
import { frontmatter } from '../composables/app'
onMounted(() => {
if (isStaticMode)
document.title = 'Valaxy DevTools (Production)'
const $frontmatter = getWindowProperty('$frontmatter') as BlogWindow['$frontmatter']
if ($frontmatter)
frontmatter.value = $frontmatter
})
</script>

<template>
<div>
Index
</div>
<main class="h-full" flex="~ col" text="gray-700 dark:gray-200">
<div class="w-full border-b shadow flex justify-end">
<a href="https://valaxy.site" target="_blank" class="bg-gray-100 inline-flex justify-center items-center w-8 h-8">
<div i-ri-book-line />
</a>
</div>

<div style="height: calc(100% - 32px)" overflow="auto">
<Splitpanes class="h-full">
<Pane>
<VDPostList />
</Pane>
<Pane>
<PageFrontmatter :frontmatter="frontmatter" />
</Pane>
</Splitpanes>
</div>
</main>
</template>
5 changes: 5 additions & 0 deletions packages/devtools/src/client/pages/tags.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
Tags
</div>
</template>
4 changes: 4 additions & 0 deletions packages/devtools/src/client/styles/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
html,
body {
height: 100%;
}
2 changes: 2 additions & 0 deletions packages/devtools/src/client/typed-routes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import type {

declare module 'vue-router/auto/routes' {
export interface RouteNamedMap {
'/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>,
'/about': RouteRecordInfo<'/about', '/about', Record<never, never>, Record<never, never>>,
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/devtools/src/client/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface BlogWindow {
$frontmatter: any
}
18 changes: 18 additions & 0 deletions packages/devtools/src/client/utils/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// import from @vue/devtools-api not work
import { getAppWindow } from './get'

const target = getAppWindow()

export interface OpenInEditorOptions {
file?: string
line?: number
column?: number
}

export function openInEditor(options: OpenInEditorOptions = {}) {
const { file, line = 0, column = 0 } = options
if (file) {
const baseUrl = window.location.origin
target?.__VUE_INSPECTOR__.openInEditor(baseUrl, file, line, column)
}
}
19 changes: 19 additions & 0 deletions packages/devtools/src/client/utils/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function getAppWindow() {
return window.parent.parent as unknown as {
__VUE_INSPECTOR__: {
openInEditor: (baseUrl: string, file: string, line: number, column: number) => void
}
}
}

/**
* window.parent.parent is the window object of the main app
*/
export function getWindowProperty(property: string) {
return (window.parent.parent as any)[property]
}

export function getGlobalValaxyProperty(property: string) {
const $valaxy = (window.parent.parent as any).$valaxy
return $valaxy[property]
}
3 changes: 3 additions & 0 deletions packages/devtools/src/client/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './api'
export * from './get'

export const isStaticMode = document.body.getAttribute('data-valaxy-devtools-mode') === 'BUILD'
23 changes: 10 additions & 13 deletions packages/devtools/src/client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { join, resolve } from 'node:path'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Router from 'unplugin-vue-router/vite'
import Components from 'unplugin-vue-components/vite'
import VueRouter from 'unplugin-vue-router/vite'
import VueComponents from 'unplugin-vue-components/vite'
import Unocss from 'unocss/vite'
import { unoConfig } from '../../../../uno.config'

export default defineConfig({
base: './',
Expand Down Expand Up @@ -48,22 +49,18 @@ export default defineConfig({
},
},

Vue({
script: {
defineModel: true,
},
}),

Router({
routesFolder: ['pages'],
VueRouter({
routesFolder: join(__dirname, 'pages'),
dts: join(__dirname, 'typed-routes.d.ts'),
}),

Components({
Vue({
include: [/\.vue$/, /\.md$/],
}),
VueComponents({
dirs: ['components'],
dts: join(__dirname, 'components.d.ts'),
}),
Unocss(),
Unocss(unoConfig),
],

optimizeDeps: {
Expand Down
3 changes: 0 additions & 3 deletions packages/devtools/uno.config.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/valaxy/client/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ components.d.ts

# unplugin-vue-router
typed-router.d.ts

.env.local
4 changes: 4 additions & 0 deletions packages/valaxy/client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import '/@valaxyjs/styles'
import 'uno.css'

import setupMain from './setup/main'
import { initDevToolsClientLogic } from './utils/dev'

const valaxyConfig = initValaxyConfig()

Expand Down Expand Up @@ -58,6 +59,9 @@ const routesWithLayout = setupLayouts(import.meta.env.DEV
: filterDraft(routes),
)

if (import.meta.env.DEV)
initDevToolsClientLogic()

// https://github.com/antfu/vite-ssg
export const createApp = ViteSSG(
App,
Expand Down
2 changes: 1 addition & 1 deletion packages/valaxy/client/modules/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function addValaxyTabAndCommand() {
// iframe view
view: {
type: 'iframe',
src: 'https://valaxy.site',
src: '/__valaxy_devtools__/',
},
// category: 'pinned',
category: 'app',
Expand Down
3 changes: 3 additions & 0 deletions packages/valaxy/client/modules/valaxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ function handleHMR(router: Router): void {
if (import.meta.hot) {
import.meta.hot.on('valaxy:pageData', (payload: PageDataPayload) => {
if (shouldHotReload(payload)) {
// @ts-expect-error $pageData
window.$pageData = payload.pageData

// console.log(payload.pageData.headers)
Object.assign(router.currentRoute.value.meta, payload.pageData)
}
Expand Down

0 comments on commit 719049b

Please sign in to comment.