4949 placeholder =" 输入笔记标题..."
5050 class =" w-full px-4 py-3 text-lg border border-gray-300 dark:border-gray-600 rounded-xl focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-gray-100 placeholder-gray-400"
5151 />
52- <button @click =" showSelector = true " class =" btn btn-lg btn-primary ml-2 rounded-lg" >
52+ <button @click =" handleShowSelector " class =" btn btn-lg btn-primary ml-2 rounded-lg" >
5353 + 关联
5454 </button >
5555 </div >
208208import { ref , watch } from ' vue'
209209import noteService from ' ../api/noteService'
210210import FileFolderSelector from ' ./FileFolderSelector.vue'
211+ import { useToast } from ' @/composables/useToast'
212+ import { getFileIcon , getFileTypeColor } from ' @/utils/file'
213+ import { formatSize } from ' @/utils/format'
211214
212215const props = defineProps ({
213216 note: {
@@ -224,35 +227,7 @@ const attachedFiles = ref([])
224227const attachedFolders = ref ([])
225228const showSelector = ref (false )
226229
227- // 文件相关辅助函数
228- const getFileIcon = (mimeType ) => {
229- if (! mimeType) return ' 📁'
230- if (mimeType .startsWith (' image/' )) return ' 🖼️'
231- if (mimeType .startsWith (' video/' )) return ' 🎥'
232- if (mimeType === ' application/pdf' ) return ' 📄'
233- if (mimeType .startsWith (' audio/' )) return ' 🎵'
234- if (mimeType .includes (' document' ) || mimeType .includes (' word' )) return ' 📝'
235- if (mimeType .includes (' sheet' ) || mimeType .includes (' excel' )) return ' 📊'
236- if (mimeType .includes (' presentation' ) || mimeType .includes (' powerpoint' )) return ' 📋'
237- return ' 📁'
238- }
239-
240- const getFileTypeColor = (mimeType ) => {
241- if (! mimeType) return ' bg-gray-100 text-gray-600'
242- if (mimeType .startsWith (' image/' )) return ' bg-green-100 text-green-600'
243- if (mimeType .startsWith (' video/' )) return ' bg-blue-100 text-blue-600'
244- if (mimeType === ' application/pdf' ) return ' bg-red-100 text-red-600'
245- if (mimeType .startsWith (' audio/' )) return ' bg-purple-100 text-purple-600'
246- return ' bg-gray-100 text-gray-600'
247- }
248-
249- const formatSize = (bytes ) => {
250- if (bytes === 0 ) return ' 0 Bytes'
251- const k = 1024
252- const sizes = [' Bytes' , ' KB' , ' MB' , ' GB' , ' TB' ]
253- const i = Math .floor (Math .log (bytes) / Math .log (k))
254- return parseFloat ((bytes / Math .pow (k, i)).toFixed (2 )) + ' ' + sizes[i]
255- }
230+ const { showToast } = useToast ()
256231
257232// 监听属性变化
258233watch (
@@ -285,11 +260,19 @@ const handleSubmit = () => {
285260 })
286261}
287262
263+ const handleShowSelector = () => {
264+ if (! props .note || ! props .note .id ) {
265+ showToast (' 请先保存笔记,再关联文件和文件夹' , ' warning' )
266+ return
267+ }
268+ showSelector .value = true
269+ }
270+
288271const handleAttachItems = async ({ files: fileIds, folders: folderIds }) => {
289272 showSelector .value = false
290273
291274 if (! props .note || ! props .note .id ) {
292- alert (' 请先保存笔记,然后再关联文件和文件夹 ' )
275+ showToast (' 请先保存笔记,再关联文件和文件夹 ' , ' warning ' )
293276 return
294277 }
295278
@@ -311,7 +294,7 @@ const handleAttachItems = async ({ files: fileIds, folders: folderIds }) => {
311294 attachedFolders .value = response .folders || []
312295 } catch (error) {
313296 console .error (' Failed to attach items' , error)
314- alert (' 关联失败' )
297+ showToast (' 关联失败' , ' error ' )
315298 }
316299}
317300
@@ -325,7 +308,7 @@ const handleDetachFile = async (fileId) => {
325308 attachedFiles .value = attachedFiles .value .filter ((file ) => file .id !== fileId)
326309 } catch (error) {
327310 console .error (' Failed to detach file' , error)
328- alert (' 移除文件关联失败' )
311+ showToast (' 移除文件关联失败' , ' error ' )
329312 }
330313}
331314
@@ -339,7 +322,7 @@ const handleDetachFolder = async (folderId) => {
339322 attachedFolders .value = attachedFolders .value .filter ((folder ) => folder .id !== folderId)
340323 } catch (error) {
341324 console .error (' Failed to detach folder' , error)
342- alert (' 移除文件夹关联失败' )
325+ showToast (' 移除文件夹关联失败' , ' error ' )
343326 }
344327}
345328 </script >
0 commit comments