Your Name 3 年前
父节点
当前提交
1a98f1a6b9

+ 10
- 3
src/components/ContextMenu.vue 查看文件

@@ -21,6 +21,7 @@ const menuRef = ref()
21 21
 const contextMenuRef = ref()
22 22
 const contextMenuOptions = [
23 23
   { id: 'newFolder', name: '新建文件夹' },
24
+  { type: 'divider', class: 'cm-option-divider' },
24 25
   { id: 'newFile', name: '新建文件' },
25 26
 ]
26 27
 const optionRef = ref()
@@ -50,9 +51,15 @@ eventBus.addEventListener('menu.contextMenu', onContextMenu)
50 51
 
51 52
 </script>
52 53
 
53
-<style lang="less" scoped>
54
-.my-context-menu {
55
-  width: 200px;
54
+<style lang="less">
55
+#my-context-menu {
56
+  min-width: 175px;
56 57
   background-color: #fff;
58
+
59
+  .cm-option-divider {
60
+    background-color: #dfe0e1;
61
+    box-sizing: content-box;
62
+    height: 1px;
63
+  }
57 64
 }
58 65
 </style>

+ 0
- 40
src/components/HelloWorld.vue 查看文件

@@ -1,40 +0,0 @@
1
-<script setup>
2
-import { ref } from 'vue'
3
-
4
-defineProps({
5
-  msg: String
6
-})
7
-
8
-const count = ref(0)
9
-</script>
10
-
11
-<template>
12
-  <h1>{{ msg }}</h1>
13
-
14
-  <p>
15
-    Recommended IDE setup:
16
-    <a href="https://code.visualstudio.com/" target="_blank">VS Code</a>
17
-    +
18
-    <a href="https://github.com/johnsoncodehk/volar" target="_blank">Volar</a>
19
-  </p>
20
-
21
-  <p>
22
-    <a href="https://vitejs.dev/guide/features.html" target="_blank">
23
-      Vite Documentation
24
-    </a>
25
-    |
26
-    <a href="https://v3.vuejs.org/" target="_blank">Vue 3 Documentation</a>
27
-  </p>
28
-
29
-  <button type="button" @click="count++">count is: {{ count }}</button>
30
-  <p>
31
-    Edit
32
-    <code>components/HelloWorld.vue</code> to test hot module replacement.
33
-  </p>
34
-</template>
35
-
36
-<style scoped>
37
-a {
38
-  color: #42b983;
39
-}
40
-</style>

+ 6
- 2
src/components/SubMenu.vue 查看文件

@@ -6,14 +6,14 @@
6 6
     >
7 7
       <template #title>        
8 8
         <div class="dir-context-menu" @contextmenu.prevent.stop="event => onContextMenu(event, menu)">
9
-          {{menu.name}}
9
+          {{ trimSuffix(menu.name) }}
10 10
         </div>
11 11
       </template>
12 12
       <template v-if="menu.children">
13 13
         <SubMenu :menus="menu.children" :sub-menu="SubMenu" />
14 14
       </template>
15 15
     </a-sub-menu>
16
-    <a-menu-item v-else :key="`/@@file@@/${menu.path}`">{{ menu.name }}</a-menu-item>
16
+    <a-menu-item v-else :key="`/@@file@@/${menu.path}`">{{ trimSuffix(menu.name) }}</a-menu-item>
17 17
   </template>
18 18
 </template>
19 19
 
@@ -33,6 +33,10 @@ const onContextMenu = (event, menu) => {
33 33
   eventBus.dispatchEvent('menu.contextMenu', { event, menu })
34 34
 }
35 35
 
36
+const trimSuffix = name => {
37
+  return name.replace(/\.html$/, '')
38
+}
39
+
36 40
 </script>
37 41
 
38 42
 

+ 20
- 32
src/layouts/components/SiderBar.vue 查看文件

@@ -10,12 +10,12 @@
10 10
     >
11 11
       <a-menu-item-group key="g1">
12 12
         <template #title>
13
-          <div>我的日记</div>
13
+          <div @contextmenu.prevent.stop="event => onContextMenu(event)">我的日记</div>
14 14
         </template>
15 15
       </a-menu-item-group>
16 16
       <SubMenu :menus="fileTree" :sub-menu="SubMenu"></SubMenu>
17 17
     </a-menu>
18
-    <ContextMenu :newFile="createNewFile" :newFolder="createNewFolder" />
18
+    <ContextMenu @newFile="createNewFile" @newFolder="createNewFolder" />
19 19
   </div>
20 20
 </template>
21 21
 
@@ -36,8 +36,8 @@ const props = defineProps({
36 36
 
37 37
 const { user } = useModel('user')
38 38
 const { curMenu, menus, getMenus } = useModel('menu')
39
-const { fileArr, fileTree, getNote, getFiles } = useModel('note')
40
-const { newFile, newFolder } = useModel('repo')
39
+const { fileArr, fileTree, getNote, newNote, getFiles, newFolder } = useModel('note')
40
+const { newFile } = useModel('repo')
41 41
 
42 42
 const repo = 'my-note'
43 43
 const rootPath = ''
@@ -53,45 +53,33 @@ const handleMenu = (e) => {
53 53
 }
54 54
 
55 55
 const handleOpenChange = (keys) => {
56
-  const [ menuId ] = keys
57
-  if (!menuId) return;
56
+  const menuId = keys.slice().pop()
57
+  if (!menuId) return true;
58 58
 
59 59
   const path = menuId.replace('/@@dir@@/', '')
60
-  getFiles(user, path, repo)
60
+  const menu = fileArr.filter(x => x.path === path && x.type === 'dir')[0]
61
+  if (!menu.children) {
62
+    getFiles(user, path, repo)
63
+  }
61 64
 }
62 65
 
63 66
 // 创建新文件
64
-const createNewFile = (parentMenu, newFileName) => {
65
-  const parentPath = parentMenu.path
67
+const createNewFile = ({ menu: parentMenu, name: newFileName }) => {
68
+  const parentPath = parentMenu ? parentMenu.path : ''
66 69
   const newFilePath = parentPath ? `${parentPath}/${newFileName}.html` : `${newFileName}.html`
67 70
 
68
-  newFile('又是一篇崭新的日记...', newFilePath, user).then(() => {
69
-    getMenus(user, parentPath).then(res => {
70
-      const newDIR = (res || []).filter(x => x.path.indexOf('.gitignore') === -1)
71
-      if (curMenu.value) {
72
-        curMenu.value.children = newDIR
73
-      } else {
74
-        menus.splice().push(...newDIR || [])
75
-      }
76
-    })
77
-  })
71
+  newNote(user, newFilePath, repo)
78 72
 }
79 73
 
80 74
 // 创建新目录
81
-const createNewFolder = (parentMenu, newFolderName) => {
82
-  const parentPath = parentMenu.path
75
+const createNewFolder = ({ menu: parentMenu, name: newFolderName }) => {
76
+  const parentPath = parentMenu ? parentMenu.path : ''
83 77
   const newFilePath = parentPath ? `${parentPath}/${newFolderName}` : newFolderName
84
-
85
-  newFolder(newFilePath, user).then(() => {
86
-    getMenus(user, parentPath).then(res => {
87
-      const newDIR = (res || []).filter(x => x.path.indexOf('.gitignore') === -1)
88
-      if (curMenu.value) {
89
-        curMenu.value.children = newDIR
90
-      } else {
91
-        menus.push(...newDIR || [])
92
-      }
93
-    })
94
-  })
78
+  newFolder(user, newFilePath, repo)
95 79
 }
96 80
 
81
+
82
+const onContextMenu = (event) => {
83
+  eventBus.dispatchEvent('menu.contextMenu', { event })
84
+}
97 85
 </script>

+ 1
- 1
src/service/gitee.js 查看文件

@@ -93,7 +93,7 @@ export const newFile = (user, content, path, repo = 'my-note') => {
93 93
 // 新增目录, 其实就是新增一个 内容是 # 的 .gitignore 文件
94 94
 export const newFolder = (user, path, repo = 'my-note') => {
95 95
   const filePath = `${path}/.gitignore`
96
-  return newFile('#', filePath, user, repo)
96
+  return newFile(user, '#', filePath, repo)
97 97
 }
98 98
 
99 99
 // 更新文件

+ 19
- 3
src/store/model/note.js 查看文件

@@ -2,6 +2,12 @@ import { reactive, ref } from 'vue'
2 2
 import { message } from 'ant-design-vue';
3 3
 import * as gitee from '@/service/gitee'
4 4
 
5
+const getParentPath = path => {
6
+  const paths = path.split('/')
7
+  paths.pop()
8
+  return paths.join('/')       
9
+}
10
+
5 11
 export default function useNote() {
6 12
   const fileArr = reactive([])
7 13
   const fileTree = reactive([])
@@ -17,7 +23,7 @@ export default function useNote() {
17 23
         fileArr.push(...files)
18 24
         fileTree.push(...files)
19 25
       } else {
20
-        const dir = fileArr.filter(x => x.type === 'dir' && x.name === path)[0]
26
+        const dir = fileArr.filter(x => x.type === 'dir' && x.path === path)[0]
21 27
         dir.children = files
22 28
         fileArr.push(...files)
23 29
       }
@@ -28,8 +34,10 @@ export default function useNote() {
28 34
     return gitee.getFile(user, path, repo).then(res => note.value = res)
29 35
   }
30 36
 
31
-  const newNote = () => {
32
-    
37
+  const newNote = (user, path, repo = 'my-note') => {
38
+    gitee.newFile(user, '又是一篇崭新的日记...', path, repo).then(() => {
39
+      getFiles(user, getParentPath(path), repo)
40
+    })
33 41
   }
34 42
 
35 43
   const updateNote = (content, user) => {
@@ -42,13 +50,21 @@ export default function useNote() {
42 50
       gitee.getFile(user, filePath, 'my-note').then(res => note.value = res)
43 51
     })
44 52
   }
53
+
54
+  const newFolder = (user, path, repo = 'my-note') => {
55
+    gitee.newFolder(user, path, repo).then(() => {
56
+      getFiles(user, getParentPath(path), repo)
57
+    })
58
+  }
45 59
   
46 60
   return {
47 61
     fileArr,
48 62
     fileTree,
49 63
     note,
50 64
     getFiles,
65
+    newFolder,
51 66
     getNote,
67
+    newNote,
52 68
     updateNote,
53 69
   }
54 70
 }