Skip to content

Commit 9d0d22f

Browse files
committed
feat(nav): upgrade sidebar, navScreenMenu, localNav components with pinia
1 parent d5b6576 commit 9d0d22f

File tree

16 files changed

+152
-204
lines changed

16 files changed

+152
-204
lines changed

examples/App.vue

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import {
66
watchEffect,
77
provide,
88
onUnmounted,
9+
watch,
910
} from "vue";
1011
import { useSidebar } from "@examples/composables/sidebar";
1112
import { useRoute } from "@examples/composables/router";
13+
import { useSidebarStore } from "./store/modules/sidebar";
1214
1315
export default {
1416
name: "App",
@@ -24,24 +26,24 @@ export default {
2426
2527
setup() {
2628
const router = useRoute();
27-
// 解构
28-
const {
29-
isOpen: isSidebarOpen,
30-
open: openSidebar,
31-
close: closeSidebar,
32-
} = useSidebar(router.value);
29+
const sidebarStore = useSidebarStore();
30+
sidebarStore.setCurrentRoute(router.value);
31+
32+
watch(router, (newValue, oldValue) => {
33+
sidebarStore.setCurrentRoute(newValue);
34+
});
3335
3436
// https://stackoverflow.com/questions/53126710/how-to-get-current-name-of-route-in-vue
3537
let triggerElement: HTMLButtonElement | undefined;
3638
watchEffect(() => {
37-
triggerElement = isSidebarOpen.value
39+
triggerElement = sidebarStore.getSidebarOpen
3840
? (document.activeElement as HTMLButtonElement)
3941
: undefined;
4042
});
4143
4244
const onEsacpe = (e: KeyboardEvent) => {
43-
if (e.key === "Escape" && isSidebarOpen.value) {
44-
closeSidebar();
45+
if (e.key === "Escape" && sidebarStore.getSidebarOpen) {
46+
sidebarStore.setSidebarClose();
4547
triggerElement?.focus();
4648
}
4749
};
@@ -54,7 +56,7 @@ export default {
5456
window.removeEventListener("keyup", onEsacpe);
5557
});
5658
57-
provide("close-sidebar", closeSidebar);
59+
provide("close-sidebar", sidebarStore.setSidebarClose);
5860
5961
const isComponent = computed(
6062
() =>
@@ -76,22 +78,27 @@ export default {
7678
isRepl,
7779
isChangelog,
7880
isHome,
79-
// Sidebar
80-
isSidebarOpen,
81-
openSidebar,
82-
closeSidebar,
81+
82+
sidebarStore,
83+
openSidebar: sidebarStore.setSidebarOpen,
84+
closeSidebar: sidebarStore.setSidebarClose,
8385
};
8486
},
8587
};
8688
</script>
8789
<template>
8890
<div id="app" class="VPApp" :class="{ 'is-component': isComponent }">
8991
<!-- <VPSkipLink /> -->
90-
<VCVBackdrop class="backdrop" :show="isSidebarOpen" @click="closeSidebar" />
92+
<VCVBackdrop
93+
class="backdrop"
94+
:show="sidebarStore.getSidebarOpen"
95+
@click.native="closeSidebar"
96+
/>
9197
<Banner />
9298
<VPNav />
93-
<VPLocalNav :open="isSidebarOpen" @open-menu="openSidebar" />
94-
<VPSidebar :open="isSidebarOpen">
99+
<!-- 左侧导航隐藏后显示 -->
100+
<VPLocalNav :open="sidebarStore.getSidebarOpen" @open-menu="openSidebar" />
101+
<VPSidebar :open="sidebarStore.getSidebarOpen">
95102
<template #top> </template>
96103
<template #bottom> </template>
97104
</VPSidebar>

examples/docs/guides/code-logic-intro.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
2-
# 0x00 前言
1+
# 源码概述
32

43
书接上文,本文将从源码功能方面讲解下 `vue-code-view` 组件核心逻辑,您可以了解以下内容:
54

65
- 动态组件的使用。
76
- `codeMirror`插件的使用。
87
- 单文件组件(SFC,single-file component) Parser。
98

10-
# 0x01 CodeEditor组件
9+
## CodeEditor组件
1110

1211
项目使用功能丰富的`codeMirror`实现在线代码展示编辑功能。
1312

@@ -150,7 +149,7 @@ this.codeEditor.setValue(this.value);
150149
this.codeEditor.on("change", (item) => { this.codeHandler(item.getValue()); });
151150
```
152151

153-
# 0x02 SFC Parser
152+
## SFC Parser
154153

155154
组件的功能场景是用于简单示例代码运行展示,将源码视为 单文件组件(SFC,single-file component)的简单实例。
156155

@@ -183,15 +182,15 @@ export interface SFCBlock {
183182
`SFCDescriptor` 包含 `template``script``styles``customBlocks` 四个部分,将用于示例组件的动态构建。 其中 `styles`是数组,可以包含多个代码块并解析; `template``script` 若存在多个代码块只能解析最后一个。
184183
`customBlocks`是没在`template`的HTML代码,处理逻辑暂未包含此内容。
185184

186-
# 0x03 组件动态样式
185+
## 组件动态样式
187186

188187
文件`src\utils\style-loader\addStylesClient.js` 移植 `vue-style-loader` 源码 [addStylesClient](https://github.com/vuejs/vue-style-loader/blob/master/lib/addStylesClient.js) 方法,用于在页面DOM中动态创建组件样式。
189188

190189
![image.png](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/b1c7fda5256549929e49f5be63326ffd~tplv-k3u1fbpfcp-watermark.image?)
191190

192191
根据 `SFCDescriptor` 中的 `styles`和组件编号,在DOM中添加对应样式内容,若新增删除 `<style>`,页面DOM中对应创建或移除该样式内容。若更新 `<style>`内容,DOM节点只更新对应块的内容,优化页面性能。
193192

194-
# 0x04 CodeViewer 组件
193+
## CodeViewer 组件
195194

196195
使用 `JSX` 语法实现组件核心代码。
197196

@@ -378,6 +377,6 @@ computed: {
378377

379378
---
380379

381-
# 完结
380+
## 完结
382381

383-
此组件编写是个人对于 [📚Element 2 源码学习系列](https://juejin.cn/column/6961321064110489631 "https://juejin.cn/column/6961321064110489631") 学习实践的总结,希望会对您有所帮助!
382+
此组件编写是个人对于 [📚Element 2 源码学习系列](https://juejin.cn/column/6961321064110489631) 学习实践的总结,希望会对您有所帮助!

examples/docs/guides/overview.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
</a>
55
</p>
66

7-
<h1 align="center">Vue Code View(VCV)</h1>
8-
9-
![Build Status](https://app.travis-ci.com/andurils/vue-code-view.svg?branch=dev)
10-
![GitHub license](https://img.shields.io/github/license/andurils/vue-code-view)
11-
![npm](https://img.shields.io/npm/v/vue-code-view)
12-
![npm bundle size](https://img.shields.io/bundlephobia/min/vue-code-view)
13-
![npm](https://img.shields.io/npm/dt/vue-code-view?label=npm%20downloads)
7+
<h1 align="center">Vue Code View(VCV)</h1>
148

159
一个基于 `vue 2.x`的轻量代码交互组件,在网页中可以编辑、运行并实时预览代码效果展示。
1610

examples/docs/guides/project-intro.md

Lines changed: 9 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
# 0x00 前言
1+
# 项目概述
32

43
本文将从结构、功能等方面讲解下项目 `vue-code-view` 的搭建过程,您可以了解以下内容:
54

@@ -9,138 +8,9 @@
98
- 项目NPM包发布。
109
- 项目组件的自定义 `Markdown` 解析 `loader`
1110

12-
本文算是 [📚Element 2 源码学习系列](https://juejin.cn/column/6961321064110489631) 的拓展篇内容,通过之前的文章了解了开源组件库的结构原理后,自己也搭建个组件项目怎么办?接下来就是实践的过程,算是"**知行合一**"了吧! 耐心读完,相信会对您有所帮助!
13-
14-
---
15-
16-
# 0x01 项目概述
17-
18-
## 创建项目
19-
20-
本项目使用 `Vue CLI 4.x` 进行项目创建。
21-
22-
```bash
23-
// 安装环境
24-
npm install -g @vue/cli
25-
// 创建项目
26-
vue create vue-code-view
27-
```
28-
29-
在终端中输入命令后,按照以下步骤操作创建项目:
30-
31-
1. 选择手动选择功能 `Manually select features`
32-
2. 选中 `Babel`, `Router`, `CSS Pre-processors`,`Linter / Formatter`等功能 。
33-
3. 选择 `vue` 版本 `2.X`
34-
4. 选择路由是否使用`history`模式,默认 `yes`
35-
5. 选择CSS 预处理器`Sass/SCSS(with node-sass)`
36-
6. 选择代码风格、格式校验 `linter / formatter`配置`ESLint + Prettier`
37-
7. 选择校验时机保存时检测`Lint on save`
38-
8. 选择 `Babel`, `ESLint`等配置文件存放在专用配置文件中 `In dedicated config files`
39-
40-
## 配置多环境变量
41-
42-
在项目根目录中新建 `.env`, `.env.deploy`,`.env.production`等文件。
43-
44-
`VUE_APP` 开头的变量会被 `webpack.DefinePlugin` 静态嵌入到客户端侧的包中,代码中可以通过 `process.env.VUE_APP[xxx]` 访问。
45-
46-
> `NODE_ENV``BASE_URL` 是两个特殊变量,在代码中始终可用。
47-
48-
**.env**\
49-
`vue-cli-service serve` 默认的本地开发环境配置
50-
51-
```bash
52-
NODE_ENV = development
53-
VUE_APP_PUBLIC_PATH = /
54-
```
55-
56-
**.env.production**\
57-
`vue-cli-service build` 默认的环境配置(正式服务器)
58-
59-
```bash
60-
NODE_ENV = production
61-
VUE_APP_PUBLIC_PATH = /
62-
VUE_APP_ENV = pub
63-
```
64-
65-
**.env.deploy**\
66-
用于 github pages 构建部署的环境配置。`VUE_APP_PUBLIC_PATH` 设置 `/vue-code-view` 用于虚拟目录。
67-
68-
```bash
69-
NODE_ENV = production
70-
VUE_APP_PUBLIC_PATH = /vue-code-view
71-
VUE_APP_ENV = deploy
72-
```
11+
## 项目构建
7312

74-
## 目录结构调整
75-
76-
默认的 `src` 目录下存放项目源码及需要引用的资源文件。根目录下新建 `examples` 文件夹用于项目示例网站,将`src` 目录下文件移至 `examples` 文件 。`src` 目录存放项目组件源码。
77-
78-
调整后根目录文件结构如下:
79-
80-
```bash
81-
├── examples // 项目示例网站
82-
| ├── App.vue
83-
| ├── assets
84-
| ├── components
85-
| ├── main.js
86-
| ├── router
87-
| └── views
88-
├── src // 项目组件源码
89-
| ├── fonts
90-
| ├── index.js
91-
| ├── locale
92-
| ├── mixins
93-
| ├── src
94-
| ├── styles
95-
| └── utils
96-
├── public
97-
├── package.json
98-
```
99-
100-
## 配置基础 vue.config.js
101-
102-
项目默认入口`./src/main.js`,配置如下:
103-
104-
```js
105-
{
106-
entry: {
107-
app: [
108-
'./src/main.js'
109-
]
110-
}
111-
}
112-
```
113-
114-
在根目录下创建 `vue.config.js` 修改默认配置。
115-
116-
```js
117-
const path = require("path");
118-
const resolve = (dir) => path.join(__dirname, dir);
119-
120-
module.exports = {
121-
configureWebpack: (config) => {
122-
// 项目入口
123-
config.entry.app = "./examples/main.js";
124-
},
125-
chainWebpack: (config) => {
126-
// 添加别名
127-
config.resolve.alias
128-
.set("vue$", "vue/dist/vue.esm.js")
129-
.set("@", resolve("examples"))
130-
.set("@assets", resolve("examples/assets"))
131-
.set("@src", resolve("src"))
132-
.set("@views", resolve("examples/views"))
133-
.set("@router", resolve("examples/router"))
134-
.set("@store", resolve("examples/store"));
135-
},
136-
};
137-
```
138-
139-
运行 `npm run serve` , 项目网站正常运行。
140-
141-
# 0x02 项目构建
142-
143-
## npm scripts 配置
13+
### npm scripts 配置
14414

14515
调整 `package.json` 里的 `scripts` 配置脚本,并添加 `--mode xxx` 来选择不同环境配置。
14616

@@ -155,7 +25,7 @@ module.exports = {
15525
}
15626
```
15727

158-
## 组件构建
28+
### 组件构建
15929

16030
组件库构建通过指定入口文件`src/index.js`、设定参数选项。
16131

@@ -176,7 +46,7 @@ module.exports = {
17646
- `lib/vue-code-viewer.umd.min.js`:压缩后的 UMD 构建版本。
17747
- `lib/vue-code-viewer.css`:提取出来的 CSS 文件。
17848

179-
## 组件NPM包发布
49+
### 组件NPM包发布
18050

18151
配置 `package.json` 文件中属性值用于npm 发布。
18252

@@ -248,7 +118,7 @@ deploy/
248118

249119
![image.png](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c96d56ee6144485f93913b688f664e68~tplv-k3u1fbpfcp-watermark.image?)
250120

251-
## 项目示例网站构建
121+
### 项目示例网站构建
252122

253123
更新 `vue.config.js`,运行 `npm run deploy:build` 构建项目示例网站输出至 `deploy` 目录下。
254124

@@ -270,7 +140,7 @@ module.exports = {
270140

271141
```
272142

273-
## 持续集成
143+
### 持续集成
274144

275145
使用 `Travis CI`的持续集成服务自动构建项目示例网站并部署至 `gh-pages` 分支 。
276146

@@ -305,7 +175,7 @@ after_script:
305175

306176
![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/dbfeedcb19854b06acb0af1ec87e415e~tplv-k3u1fbpfcp-watermark.image?)
307177

308-
## 开启构建压缩
178+
### 开启构建压缩
309179

310180
安装相关插件。
311181

@@ -370,7 +240,7 @@ module.exports = {
370240

371241
```
372242

373-
## 组件说明文档
243+
### 组件说明文档
374244

375245
参考`element 2`的实现,自定义 `build/md-loder``Markdown` 文件进行解析渲染,将 `examples\docs\zh-CN\example.md` 编译为 HTML。已在前文 [04.封装组件封装、编写说明文档](https://juejin.cn/post/6953614014546968589#heading-5) 中详细说明,不再过多赘述。
376246

@@ -410,5 +280,3 @@ const routes = [
410280
},
411281
];
412282
```
413-
414-
---

examples/docs/tutorials/todo.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# tutorial
2+
3+
内容建设中...

examples/layouts/content/VPContent.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export default defineComponent({
7171

7272
<!-- <VPNotFound v-if="route.path === 'VPNotFound'" /> -->
7373
<!-- <VPContentPage v-else-if="!!frontmatter.page"> -->
74+
<!-- 文档展示 存在侧边/底部 -->
7475
<VPContentDoc v-if="!!hasSidebar" :class="{ 'has-sidebar': !!hasSidebar }">
7576
<template #content-top><slot name="content-top" /></template>
7677
<template #content-bottom><slot name="content-bottom" /></template>

0 commit comments

Comments
 (0)