Your Name 3 years ago
parent
commit
1a98f1a6b9

+ 10
- 3
src/components/ContextMenu.vue View File

21
 const contextMenuRef = ref()
21
 const contextMenuRef = ref()
22
 const contextMenuOptions = [
22
 const contextMenuOptions = [
23
   { id: 'newFolder', name: '新建文件夹' },
23
   { id: 'newFolder', name: '新建文件夹' },
24
+  { type: 'divider', class: 'cm-option-divider' },
24
   { id: 'newFile', name: '新建文件' },
25
   { id: 'newFile', name: '新建文件' },
25
 ]
26
 ]
26
 const optionRef = ref()
27
 const optionRef = ref()
50
 
51
 
51
 </script>
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
   background-color: #fff;
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
 </style>
65
 </style>

+ 0
- 40
src/components/HelloWorld.vue View File

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 View File

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

+ 20
- 32
src/layouts/components/SiderBar.vue View File

10
     >
10
     >
11
       <a-menu-item-group key="g1">
11
       <a-menu-item-group key="g1">
12
         <template #title>
12
         <template #title>
13
-          <div>我的日记</div>
13
+          <div @contextmenu.prevent.stop="event => onContextMenu(event)">我的日记</div>
14
         </template>
14
         </template>
15
       </a-menu-item-group>
15
       </a-menu-item-group>
16
       <SubMenu :menus="fileTree" :sub-menu="SubMenu"></SubMenu>
16
       <SubMenu :menus="fileTree" :sub-menu="SubMenu"></SubMenu>
17
     </a-menu>
17
     </a-menu>
18
-    <ContextMenu :newFile="createNewFile" :newFolder="createNewFolder" />
18
+    <ContextMenu @newFile="createNewFile" @newFolder="createNewFolder" />
19
   </div>
19
   </div>
20
 </template>
20
 </template>
21
 
21
 
36
 
36
 
37
 const { user } = useModel('user')
37
 const { user } = useModel('user')
38
 const { curMenu, menus, getMenus } = useModel('menu')
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
 const repo = 'my-note'
42
 const repo = 'my-note'
43
 const rootPath = ''
43
 const rootPath = ''
53
 }
53
 }
54
 
54
 
55
 const handleOpenChange = (keys) => {
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
   const path = menuId.replace('/@@dir@@/', '')
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
   const newFilePath = parentPath ? `${parentPath}/${newFileName}.html` : `${newFileName}.html`
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
   const newFilePath = parentPath ? `${parentPath}/${newFolderName}` : newFolderName
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
 </script>
85
 </script>

+ 1
- 1
src/service/gitee.js View File

93
 // 新增目录, 其实就是新增一个 内容是 # 的 .gitignore 文件
93
 // 新增目录, 其实就是新增一个 内容是 # 的 .gitignore 文件
94
 export const newFolder = (user, path, repo = 'my-note') => {
94
 export const newFolder = (user, path, repo = 'my-note') => {
95
   const filePath = `${path}/.gitignore`
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 View File

2
 import { message } from 'ant-design-vue';
2
 import { message } from 'ant-design-vue';
3
 import * as gitee from '@/service/gitee'
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
 export default function useNote() {
11
 export default function useNote() {
6
   const fileArr = reactive([])
12
   const fileArr = reactive([])
7
   const fileTree = reactive([])
13
   const fileTree = reactive([])
17
         fileArr.push(...files)
23
         fileArr.push(...files)
18
         fileTree.push(...files)
24
         fileTree.push(...files)
19
       } else {
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
         dir.children = files
27
         dir.children = files
22
         fileArr.push(...files)
28
         fileArr.push(...files)
23
       }
29
       }
28
     return gitee.getFile(user, path, repo).then(res => note.value = res)
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
   const updateNote = (content, user) => {
43
   const updateNote = (content, user) => {
42
       gitee.getFile(user, filePath, 'my-note').then(res => note.value = res)
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
   return {
60
   return {
47
     fileArr,
61
     fileArr,
48
     fileTree,
62
     fileTree,
49
     note,
63
     note,
50
     getFiles,
64
     getFiles,
65
+    newFolder,
51
     getNote,
66
     getNote,
67
+    newNote,
52
     updateNote,
68
     updateNote,
53
   }
69
   }
54
 }
70
 }