Your Name пре 3 година
родитељ
комит
406948234b

+ 2
- 1
src/components/ContextMenu.vue Прегледај датотеку

@@ -28,8 +28,9 @@ const optionRef = ref()
28 28
 const contextMenuOptions = computed(() => {
29 29
   const folderOpts = [
30 30
     { id: 'newFolder', name: '新建文件夹' },
31
-    { type: 'divider', class: 'cm-option-divider' },
32 31
     { id: 'newFile', name: '新建文件' },
32
+    // { type: 'divider', class: 'cm-option-divider' },
33
+    // { id: 'deleteFile', name: '删除...' },
33 34
   ]
34 35
 
35 36
   const fileOpts = [

+ 1
- 2
src/pages/index.vue Прегледај датотеку

@@ -8,7 +8,6 @@
8 8
 </template>
9 9
 
10 10
 <script setup>
11
-import { Base64 } from 'js-base64'
12 11
 import { ref, shallowRef, watch, onMounted } from 'vue'
13 12
 import Editor from '@/components/Editor.vue';
14 13
 import { useModel } from '@zjxpcyc/vue-tiny-store'
@@ -26,7 +25,7 @@ const handleSave = () => {
26 25
 
27 26
 watch(note, (newVal) => {
28 27
   if (newVal && newVal.content) {
29
-    valueHtml.value = Base64.decode(newVal.content)
28
+    valueHtml.value = newVal.content
30 29
   } else {
31 30
     valueHtml.value = ''
32 31
   }

+ 33
- 0
src/plugins/core/index.js Прегледај датотеку

@@ -0,0 +1,33 @@
1
+
2
+// from redux code
3
+function compose(...funcs) {
4
+  if (funcs.length === 0) {
5
+      return arg => arg
6
+  }
7
+
8
+  if (funcs.length === 1) {
9
+      return funcs[0]
10
+  }
11
+
12
+  return funcs.reduce((a, b) => (...args) => a(b(...args)))
13
+}
14
+
15
+function createPlugin() {
16
+  const hooks = {};
17
+
18
+  const apply = (type, func) => {
19
+    let listeners = hooks[type] || []
20
+    listeners = listeners.filter(x => x !== func).concat(func)
21
+    hooks[type] = listeners
22
+  }
23
+
24
+  const invoke = (type, ...args) => {
25
+    const listeners = hooks[type] || []
26
+    return compose(...listeners)(...args)
27
+  }
28
+
29
+  return { apply, invoke }
30
+}
31
+
32
+const plugins = createPlugin()
33
+export default plugins

+ 4
- 32
src/plugins/index.js Прегледај датотеку

@@ -1,34 +1,6 @@
1
-import createEvent from '@zjxpcyc/js_event'
2
-const eventBus = createEvent();
1
+import plugins from "./core/index";
2
+import onNote from "./plugins/onNote";
3 3
 
4
-// from redux code
5
-function compose(...funcs) {
6
-  if (funcs.length === 0) {
7
-      return arg => arg
8
-  }
4
+onNote(plugins)
9 5
 
10
-  if (funcs.length === 1) {
11
-      return funcs[0]
12
-  }
13
-
14
-  return funcs.reduce((a, b) => (...args) => a(b(...args)))
15
-}
16
-
17
-function createPlugin(hookBus) {
18
-  const hooks = {};
19
-
20
-  const apply = (type, func) => {
21
-    let listeners = hooks[type] || []
22
-    listeners = listeners.filter(x => x !== func).concat(func)
23
-    hooks[type] = apply
24
-  }
25
-
26
-  const invoke = (type, ...args) => {
27
-    const listeners = hooks[type] || []
28
-    return compose(...listeners)(...args)
29
-  }
30
-
31
-  return { apply, invoke }
32
-}
33
-
34
-const plugins = createPlugin(eventBus)
6
+export default plugins

+ 23
- 13
src/plugins/plugins/onNote.js Прегледај датотеку

@@ -1,18 +1,28 @@
1 1
 import CryptoJS from 'crypto-js'
2
+import { Base64 } from 'js-base64'
2 3
 import * as cache from '@/utils/cache'
3
-import plugins from '../index'
4 4
 
5
+export default (plugins) => {
5 6
 
6
-plugins.apply('readNote', (rawNote) => {
7
-  const secretKey = cache.get('encrypt_secret')
8
-  if (!secretKey) return rawNote;
7
+  plugins.apply('readNote', (rawNote) => {
8
+    debugger
9
+    const base64Data = Base64.decode(rawNote)
10
+    const secretKey = cache.get('encrypt_secret')
11
+    if (!secretKey) return base64Data;
12
+  
13
+    const decrypted = CryptoJS.AES.decrypt(base64Data, secretKey)
14
+    return decrypted.toString(CryptoJS.enc.Utf8);
15
+  })
16
+  
17
+  plugins.apply('writeNote', (rawNote) => {
18
+    const secretKey = cache.get('encrypt_secret')
19
+    if (!secretKey) return Base64.encode(rawNote);
9 20
 
10
-  return CryptoJS.AES.decrypt(rawNote, secretKey).toString(CryptoJS.enc.Utf8)
11
-})
12
-
13
-plugins.apply('writeNote', (rawNote) => {
14
-  const secretKey = cache.get('encrypt_secret')
15
-  if (!secretKey) return rawNote;
16
-
17
-  return CryptoJS.AES.encrypt(rawNote, secretKey).toString()
18
-})
21
+    const encrypted = CryptoJS.AES.encrypt(rawNote, secretKey, {
22
+      mode: CryptoJS.mode.CBC,
23
+      padding: CryptoJS.pad.Pkcs7,
24
+    })  
25
+    return Base64.encode(encrypted.toString())
26
+  })
27
+  
28
+}

+ 5
- 6
src/service/gitee.js Прегледај датотеку

@@ -1,6 +1,5 @@
1 1
 
2
-import { Base64 } from 'js-base64'
3
-import request, { BASE_URL } from '@/utils/request'
2
+import request from '@/utils/request'
4 3
 import { getToken, setToken } from '@/utils/token'
5 4
 
6 5
 // 获取当前用户信息
@@ -96,7 +95,7 @@ export const newFile = (user, content, path, repo = 'my-note') => {
96 95
   const { access_token } = getToken() || {}
97 96
   const data = {
98 97
     access_token,
99
-    content: Base64.encode(content),
98
+    content,
100 99
     message: new Date().toJSON(),
101 100
   }
102 101
   const options = {
@@ -112,12 +111,12 @@ export const newFolder = (user, path, repo = 'my-note') => {
112 111
 }
113 112
 
114 113
 // 更新文件
115
-export const updateFile = (user, fileInfo) => {
114
+export const updateFile = (user, fileInfo, repo = 'my-note') => {
116 115
   const { access_token } = getToken() || {}
117 116
   const { path, sha, content } = fileInfo
118 117
   const data = {
119 118
     access_token,
120
-    content: Base64.encode(content),
119
+    content,
121 120
     sha,
122 121
     message: new Date().toJSON(),
123 122
   }
@@ -125,7 +124,7 @@ export const updateFile = (user, fileInfo) => {
125 124
     loading: 'loading.note',
126 125
   }
127 126
 
128
-  return request.put(path, data, options)
127
+  return request.put(`/api/v5/repos/${user.login}/${repo}/contents/${path}`, data, options)
129 128
 }
130 129
 
131 130
 export const deleteFile = (user, fileInfo, repo = 'my-note') => {

+ 27
- 7
src/store/model/note.js Прегледај датотеку

@@ -1,6 +1,7 @@
1 1
 import { reactive, ref, computed } from 'vue'
2 2
 import { message } from 'ant-design-vue';
3 3
 import * as gitee from '@/service/gitee'
4
+import plugins from '@/plugins'
4 5
 
5 6
 const getParentPath = path => {
6 7
   const paths = path.split('/')
@@ -8,6 +9,9 @@ const getParentPath = path => {
8 9
   return paths.join('/')       
9 10
 }
10 11
 
12
+const decodeData = raw => plugins.invoke('readNote', raw)
13
+const encodeData = raw => plugins.invoke('writeNote', raw)
14
+
11 15
 export default function useNote() {
12 16
   const fileArr = reactive([])
13 17
   // const fileTree = reactive([])
@@ -34,23 +38,38 @@ export default function useNote() {
34 38
   })
35 39
 
36 40
   const getNote = (user, path = '', repo = 'my-note') => {
37
-    return gitee.getFile(user, path, repo).then(res => note.value = res)
41
+    return gitee.getFile(user, path, repo).then(res => {
42
+      const content = res.content ? decodeData(res.content) : undefined
43
+
44
+      note.value = {
45
+        ...res,
46
+        content
47
+      }
48
+    })
38 49
   }
39 50
 
40 51
   const newNote = (user, path, repo = 'my-note') => {
41
-    gitee.newFile(user, '又是一篇崭新的日记...', path, repo).then(() => {
52
+    const content = encodeData('又是一篇崭新的日记...')
53
+    gitee.newFile(user, content, path, repo).then(() => {
42 54
       getFiles(user, getParentPath(path), repo)
43 55
     })
44 56
   }
45 57
 
46
-  const updateNote = (content, user) => {
47
-    const filePath = note.value.url.replace(BASE_URL, '')
48
-    const fileInfo = { filePath, sha: note.value.sha, content }
58
+  const updateNote = (content, user, repo = 'my-note') => {
59
+    const path = note.value.path
60
+    const fileInfo = { path, sha: note.value.sha, content: encodeData(content) }
49 61
 
50
-    return gitee.updateFile(user, content, fileInfo).then(() => {
62
+    return gitee.updateFile(user, fileInfo, repo).then(() => {
51 63
       message.success('保存成功');
52 64
       // 获取最新的数据, 目的是更新 sha 值
53
-      gitee.getFile(user, filePath, 'my-note').then(res => note.value = res)
65
+      gitee.getFile(user, path, repo).then(res => {
66
+        const content = res.content ? decodeData(res.content) : undefined
67
+  
68
+        note.value = {
69
+          ...res,
70
+          content
71
+        }
72
+      })
54 73
     })
55 74
   }
56 75
 
@@ -71,6 +90,7 @@ export default function useNote() {
71 90
     }
72 91
 
73 92
     if (!file) return;
93
+
74 94
     gitee.deleteFile(user, { path, sha: file.sha }, repo).then(() => {
75 95
       fileArr.splice(pos, 1)
76 96
     })