新增 文件夹列表展开收起动画效果

This commit is contained in:
yuantao
2025-10-23 17:48:46 +08:00
parent 0c4e92a4b8
commit ff4adcd949

View File

@@ -1,430 +1,458 @@
<template> <template>
<ion-page> <ion-page>
<div class="container"> <div class="container">
<ion-content class="content"> <ion-content class="content">
<Header <Header
:title="headerTitle" :title="headerTitle"
:onAction="handleHeaderAction" :onAction="handleHeaderAction"
actionIcon="create" actionIcon="create"
leftType="settings" leftType="settings"
:onLeftAction="handleSettingsPress" :onLeftAction="handleSettingsPress"
:onFolderToggle="handleFolderToggle" :onFolderToggle="handleFolderToggle"
:isFolderExpanded="isFolderExpanded" :isFolderExpanded="isFolderExpanded"
:onTitlePress="handleFolderToggle" :onTitlePress="handleFolderToggle"
slot="fixed" /> slot="fixed" />
<!-- 悬浮文件夹列表 - 使用绝对定位实现 --> <!-- 悬浮文件夹列表 - 使用绝对定位实现 -->
<div v-if="isFolderExpanded" class="folder-list" slot="fixed"> <transition name="folder-slide">
<FolderManage <div v-if="isFolderExpanded" class="folder-list" slot="fixed">
:allCount="allNotesCount" <FolderManage
:starredCount="starredNotesCount" :allCount="allNotesCount"
:trashCount="trashNotesCount" :starredCount="starredNotesCount"
:archiveCount="0" :trashCount="trashNotesCount"
:selectedFolder="currentFolder" :archiveCount="0"
:onAllClick="handleAllNotesClick" :selectedFolder="currentFolder"
:onStarredClick="handleStarredNotesClick" :onAllClick="handleAllNotesClick"
:onTrashClick="handleTrashNotesClick" /> :onStarredClick="handleStarredNotesClick"
</div> :onTrashClick="handleTrashNotesClick" />
<!-- 点击外部区域收起文件夹列表的覆盖层 --> </div>
<div v-if="isFolderExpanded" @click="() => setIsFolderExpanded(false)" class="folder-overlay" slot="fixed"></div> </transition>
<div class="search-container"> <!-- 点击外部区域收起文件夹列表的覆盖层 -->
<SearchBar v-model="searchQuery" @search="handleSearch" @clear="handleClearSearch" @focus="handleSearchFocus" @blur="handleSearchBlur" /> <div v-if="isFolderExpanded" @click="() => setIsFolderExpanded(false)" class="folder-overlay" slot="fixed"></div>
</div> <div class="search-container">
<SearchBar v-model="searchQuery" @search="handleSearch" @clear="handleClearSearch" @focus="handleSearchFocus" @blur="handleSearchBlur" />
<div class="notes-container"> </div>
<transition-group name="note-list" tag="div" class="notes-list">
<div v-for="note in filteredAndSortedNotes" :key="note.id" class="note-item"> <div class="notes-container">
<NoteItem <transition-group name="note-list" tag="div" class="notes-list">
:ref=" <div v-for="note in filteredAndSortedNotes" :key="note.id" class="note-item">
el => { <NoteItem
if (el) noteItemRefs[note.id] = el :ref="
} el => {
" if (el) noteItemRefs[note.id] = el
:content="note.content" }
:date="formatDate(note.updatedAt)" "
:isStarred="note.isStarred" :content="note.content"
:isTop="note.isTop || false" :date="formatDate(note.updatedAt)"
:hasImage="note.hasImage || false" :isStarred="note.isStarred"
:onPress="() => handleNotePress(note.id)" :isTop="note.isTop || false"
:onStarToggle="() => handleStarToggle(note.id)" :hasImage="note.hasImage || false"
:onTopToggle="() => handleTopToggle(note.id)" :onPress="() => handleNotePress(note.id)"
:onDelete="() => confirmDeleteNote(note.id)" /> :onStarToggle="() => handleStarToggle(note.id)"
</div> :onTopToggle="() => handleTopToggle(note.id)"
</transition-group> :onDelete="() => confirmDeleteNote(note.id)" />
</div> </div>
</ion-content> </transition-group>
</div> </div>
</ion-page> </ion-content>
</template> </div>
</ion-page>
<script setup> </template>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router' <script setup>
import { useAppStore } from '../stores/useAppStore' import { ref, computed, onMounted } from 'vue'
import NoteItem from '../components/NoteItem.vue' import { useRouter } from 'vue-router'
import Header from '../components/Header.vue' import { useAppStore } from '../stores/useAppStore'
import FolderManage from '../components/FolderManage.vue' import NoteItem from '../components/NoteItem.vue'
import SearchBar from '../components/Search.vue' import Header from '../components/Header.vue'
import { formatNoteListDate } from '../utils/dateUtils' import FolderManage from '../components/FolderManage.vue'
import { IonContent, IonPage } from '@ionic/vue' import SearchBar from '../components/Search.vue'
import { formatNoteListDate } from '../utils/dateUtils'
const store = useAppStore() import { IonContent, IonPage } from '@ionic/vue'
const router = useRouter()
const noteItemRefs = ref({}) const store = useAppStore()
const router = useRouter()
// 页面挂载时加载初始数据 const noteItemRefs = ref({})
onMounted(() => {
// 检查URL参数是否包含mock数据加载指令用于开发和演示 // 页面挂载时加载初始数据
const urlParams = new URLSearchParams(window.location.search) onMounted(() => {
if (urlParams.get('mock') === 'true') { // 检查URL参数是否包含mock数据加载指令用于开发和演示
// 加载预设的模拟数据 const urlParams = new URLSearchParams(window.location.search)
store.loadMockData() if (urlParams.get('mock') === 'true') {
} else { // 加载预设的模拟数据
// 从Storage加载用户数据 store.loadMockData()
store.loadData() } else {
} // 从Storage加载用户数据
}) store.loadData()
}
const searchQuery = ref('') })
const sortBy = ref('date') // 排序方式:'date'(按日期)、'title'(按标题)、'starred'(按星标)
const isFolderExpanded = ref(false) // 文件夹列表是否展开 const searchQuery = ref('')
const currentFolder = ref('all') // 当前选中的文件夹,默认是"全部便签" const sortBy = ref('date') // 排序方式:'date'(按日期)、'title'(按标题)、'starred'(按星标)
const noteToDelete = ref(null) const isFolderExpanded = ref(false) // 文件夹列表是否展开
const currentFolder = ref('all') // 当前选中的文件夹,默认是"全部便签"
// 计算加星便签数量(未删除的) const noteToDelete = ref(null)
const starredNotesCount = computed(() => {
return store.notes.filter(note => note.isStarred && !note.isDeleted).length // 计算加星便签数量(未删除的)
}) const starredNotesCount = computed(() => {
return store.notes.filter(note => note.isStarred && !note.isDeleted).length
// 计算回收站便签数量 })
const trashNotesCount = computed(() => {
return store.notes.filter(note => note.isDeleted).length // 计算回收站便签数量
}) const trashNotesCount = computed(() => {
return store.notes.filter(note => note.isDeleted).length
// 根据当前文件夹过滤便签 })
const filteredNotes = computed(() => {
// 预处理搜索查询,提高性能 // 根据当前文件夹过滤便签
const lowerCaseQuery = searchQuery.value?.toLowerCase().trim() || '' const filteredNotes = computed(() => {
// 预处理搜索查询,提高性能
return store.notes.filter(note => { const lowerCaseQuery = searchQuery.value?.toLowerCase().trim() || ''
// 先检查搜索条件
const matchesSearch = return store.notes.filter(note => {
!lowerCaseQuery || (note.title && typeof note.title === 'string' && note.title.toLowerCase().includes(lowerCaseQuery)) || (note.content && typeof note.content === 'string' && note.content.toLowerCase().includes(lowerCaseQuery)) // 先检查搜索条件
const matchesSearch =
if (!matchesSearch) return false !lowerCaseQuery || (note.title && typeof note.title === 'string' && note.title.toLowerCase().includes(lowerCaseQuery)) || (note.content && typeof note.content === 'string' && note.content.toLowerCase().includes(lowerCaseQuery))
// 再检查文件夹条件 if (!matchesSearch) return false
switch (currentFolder.value) {
case 'all': // 再检查文件夹条件
// 全部便签中不显示已删除的便签 switch (currentFolder.value) {
return !note.isDeleted case 'all':
case 'starred': // 全部便签中不显示已删除的便签
// 加星便签中只显示未删除的加星便签 return !note.isDeleted
return note.isStarred && !note.isDeleted case 'starred':
case 'trash': // 加星便签中只显示未删除的加星便签
// 回收站中只显示已删除的便签 return note.isStarred && !note.isDeleted
return note.isDeleted case 'trash':
default: // 回收站中只显示已删除的便签
// 自定义文件夹中不显示已删除的便签 return note.isDeleted
return note.folderId === currentFolder.value && !note.isDeleted default:
} // 自定义文件夹中不显示已删除的便签
}) return note.folderId === currentFolder.value && !note.isDeleted
}) }
})
// 过滤并排序便签列表 })
// 首先按置顶状态排序,置顶的便签排在前面
// 然后根据sortBy的值进行二次排序 // 过滤并排序便签列表
const filteredAndSortedNotes = computed(() => { // 首先按置顶状态排序,置顶的便签排在前面
return [...filteredNotes.value].sort((a, b) => { // 然后根据sortBy的值进行二次排序
// 置顶的便签排在前面 const filteredAndSortedNotes = computed(() => {
if (a.isTop && !b.isTop) return -1 return [...filteredNotes.value].sort((a, b) => {
if (!a.isTop && b.isTop) return 1 // 置顶的便签排在前面
if (a.isTop && !b.isTop) return -1
// 根据排序方式排序 if (!a.isTop && b.isTop) return 1
switch (sortBy.value) {
case 'title': // 根据排序方式排序
// 按标题字母顺序排序 switch (sortBy.value) {
return a.title.localeCompare(b.title) case 'title':
case 'starred': // 按标题字母顺序排序
// 按星标状态排序,加星的便签排在前面 return a.title.localeCompare(b.title)
return (b.isStarred ? 1 : 0) - (a.isStarred ? 1 : 0) case 'starred':
case 'date': // 按星标状态排序,加星的便签排在前面
default: return (b.isStarred ? 1 : 0) - (a.isStarred ? 1 : 0)
// 按更新时间倒序排列(最新的在前) case 'date':
return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime() default:
} // 按更新时间倒序排列(最新的在前)
}) return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
}) }
})
// 计算头部标题 })
const headerTitle = computed(() => {
switch (currentFolder.value) { // 计算头部标题
case 'all': const headerTitle = computed(() => {
return '全部便签' switch (currentFolder.value) {
case 'starred': case 'all':
return '加星便签' return '全部便签'
case 'trash': case 'starred':
return '回收站' return '加星便签'
default: case 'trash':
return '全部便签' return '回收站'
} default:
}) return '全部便签'
}
// 计算全部便签数量(未删除的) })
const allNotesCount = computed(() => {
return store.notes.filter(note => !note.isDeleted).length // 计算全部便签数量(未删除的)
}) const allNotesCount = computed(() => {
return store.notes.filter(note => !note.isDeleted).length
const handleNotePress = noteId => { })
// 检查是否有便签条处于展开状态
let hasSlidedNote = false const handleNotePress = noteId => {
Object.values(noteItemRefs.value).forEach(noteItem => { // 检查是否有便签条处于展开状态
// 注意isSlided是ref值需要通过.value访问 let hasSlidedNote = false
if (noteItem && noteItem.getSlideState()) { Object.values(noteItemRefs.value).forEach(noteItem => {
hasSlidedNote = true // 注意isSlided是ref值需要通过.value访问
noteItem.resetSlideState() if (noteItem && noteItem.getSlideState()) {
} hasSlidedNote = true
}) noteItem.resetSlideState()
}
// 如果有便签条处于展开状态,不跳转到编辑页面 })
if (hasSlidedNote) {
return // 如果有便签条处于展开状态,不跳转到编辑页面
} if (hasSlidedNote) {
return
// 使用vue-router导航到编辑页面 }
router.push(`/editor/${noteId}`)
} // 使用vue-router导航到编辑页面
router.push(`/editor/${noteId}`)
const handleAddNote = () => { }
// 使用vue-router导航到新建便签页面
router.push('/editor') const handleAddNote = () => {
} // 使用vue-router导航到新建便签页面
router.push('/editor')
// 处理Header组件的操作按钮点击事件 }
const handleHeaderAction = actionType => {
if (actionType === 'create') { // 处理Header组件的操作按钮点击事件
handleAddNote() const handleHeaderAction = actionType => {
} if (actionType === 'create') {
} handleAddNote()
}
const handleStarToggle = async noteId => { }
const note = store.notes.find(n => n.id === noteId)
if (note) { const handleStarToggle = async noteId => {
try { const note = store.notes.find(n => n.id === noteId)
await store.updateNote(noteId, { isStarred: !note.isStarred }) if (note) {
console.log(`便签 ${noteId} 星标状态已更新`) try {
} catch (error) { await store.updateNote(noteId, { isStarred: !note.isStarred })
console.error('更新便签星标状态失败:', error) console.log(`便签 ${noteId} 星标状态已更新`)
} } catch (error) {
} console.error('更新便签星标状态失败:', error)
} }
}
const handleTopToggle = async noteId => { }
const note = store.notes.find(n => n.id === noteId)
if (note) { const handleTopToggle = async noteId => {
try { const note = store.notes.find(n => n.id === noteId)
await store.updateNote(noteId, { isTop: !note.isTop }) if (note) {
console.log(`便签 ${noteId} 置顶状态已更新`) try {
} catch (error) { await store.updateNote(noteId, { isTop: !note.isTop })
console.error('更新便签置顶状态失败:', error) console.log(`便签 ${noteId} 置顶状态已更新`)
} } catch (error) {
} console.error('更新便签置顶状态失败:', error)
} }
}
const confirmDeleteNote = async noteId => { }
noteToDelete.value = noteId
if (noteToDelete.value) { const confirmDeleteNote = async noteId => {
try { noteToDelete.value = noteId
// 检查当前是否在回收站中 if (noteToDelete.value) {
if (currentFolder.value === 'trash') { try {
// 在回收站中删除便签,彻底删除 // 检查当前是否在回收站中
await store.permanentlyDeleteNote(noteToDelete.value) if (currentFolder.value === 'trash') {
console.log(`便签 ${noteToDelete.value} 已彻底删除`) // 在回收站中删除便签,彻底删除
} else { await store.permanentlyDeleteNote(noteToDelete.value)
// 不在回收站中,将便签移至回收站 console.log(`便签 ${noteToDelete.value} 已彻底删除`)
await store.moveToTrash(noteToDelete.value) } else {
console.log(`便签 ${noteToDelete.value} 已移至回收站`) // 不在回收站中,将便签移至回收站
} await store.moveToTrash(noteToDelete.value)
noteToDelete.value = null console.log(`便签 ${noteToDelete.value} 已移至回收站`)
} catch (error) { }
console.error('删除便签失败:', error) noteToDelete.value = null
} } catch (error) {
} console.error('删除便签失败:', error)
} }
}
// 处理排序方式切换 }
// 循环切换排序选项:按日期 -> 按标题 -> 按星标 -> 按日期...
const handleSort = () => { // 处理排序方式切换
const sortOptions = ['date', 'title', 'starred'] // 循环切换排序选项:按日期 -> 按标题 -> 按星标 -> 按日期...
const currentIndex = sortOptions.indexOf(sortBy.value) const handleSort = () => {
const nextIndex = (currentIndex + 1) % sortOptions.length const sortOptions = ['date', 'title', 'starred']
sortBy.value = sortOptions[nextIndex] const currentIndex = sortOptions.indexOf(sortBy.value)
console.log('当前排序方式:', sortOptions[nextIndex]) const nextIndex = (currentIndex + 1) % sortOptions.length
} sortBy.value = sortOptions[nextIndex]
console.log('当前排序方式:', sortOptions[nextIndex])
const handleAllNotesClick = () => { }
setCurrentFolder('all')
setIsFolderExpanded(false) const handleAllNotesClick = () => {
} setCurrentFolder('all')
setIsFolderExpanded(false)
const handleStarredNotesClick = () => { }
setCurrentFolder('starred')
setIsFolderExpanded(false) const handleStarredNotesClick = () => {
} setCurrentFolder('starred')
setIsFolderExpanded(false)
const handleTrashNotesClick = () => { }
setCurrentFolder('trash')
setIsFolderExpanded(false) const handleTrashNotesClick = () => {
} setCurrentFolder('trash')
setIsFolderExpanded(false)
const handleFolderPress = () => { }
// 使用vue-router导航到文件夹页面
router.push('/folders') const handleFolderPress = () => {
} // 使用vue-router导航到文件夹页面
router.push('/folders')
const handleSettingsPress = () => { }
// 使用vue-router导航到设置页面
router.push('/settings') const handleSettingsPress = () => {
} // 使用vue-router导航到设置页面
router.push('/settings')
const handleFolderToggle = () => { }
// 在实际应用中,这里会触发文件夹列表的展开/收起
isFolderExpanded.value = !isFolderExpanded.value const handleFolderToggle = () => {
} // 在实际应用中,这里会触发文件夹列表的展开/收起
isFolderExpanded.value = !isFolderExpanded.value
const handleSearch = query => { }
// 搜索功能已在computed属性filteredAndSortedNotes中实现
console.log('搜索:', query) const handleSearch = query => {
// 搜索功能已在computed属性filteredAndSortedNotes中实现
// 可以在这里添加搜索统计或其它功能 console.log('搜索:', query)
if (query && query.length > 0) {
console.log(`找到 ${filteredAndSortedNotes.value.length} 个匹配的便签`) // 可以在这里添加搜索统计或其它功能
} if (query && query.length > 0) {
} console.log(`找到 ${filteredAndSortedNotes.value.length} 个匹配的便签`)
}
const handleClearSearch = () => { }
// 清除搜索已在v-model中处理
console.log('搜索已清除') const handleClearSearch = () => {
// 清除搜索已在v-model中处理
// 清除搜索后可以重置一些状态 console.log('搜索已清除')
setSearchQuery('')
} // 清除搜索后可以重置一些状态
setSearchQuery('')
const handleSearchFocus = () => { }
console.log('搜索栏获得焦点')
// 可以在这里添加获得焦点时的特殊处理 const handleSearchFocus = () => {
} console.log('搜索栏获得焦点')
// 可以在这里添加获得焦点时的特殊处理
const handleSearchBlur = () => { }
console.log('搜索栏失去焦点')
// 可以在这里添加失去焦点时的特殊处理 const handleSearchBlur = () => {
} console.log('搜索栏失去焦点')
// 可以在这里添加失去焦点时的特殊处理
// 防抖函数,用于避免频繁触发搜索 }
// 通过延迟执行函数,只在最后一次调用后执行
const debounceSearch = (func, delay) => { // 防抖函数,用于避免频繁触发搜索
let timeoutId // 通过延迟执行函数,只在最后一次调用后执行
return function (...args) { const debounceSearch = (func, delay) => {
clearTimeout(timeoutId) let timeoutId
timeoutId = setTimeout(() => func.apply(this, args), delay) return function (...args) {
} clearTimeout(timeoutId)
} timeoutId = setTimeout(() => func.apply(this, args), delay)
}
// 防抖搜索处理函数延迟300ms执行搜索 }
const debouncedHandleSearch = debounceSearch(query => {
handleSearch(query) // 防抖搜索处理函数延迟300ms执行搜索
}, 300) const debouncedHandleSearch = debounceSearch(query => {
handleSearch(query)
// 改进的日期格式化函数 }, 300)
const formatDate = dateString => {
return formatNoteListDate(dateString) // 改进的日期格式化函数
} const formatDate = dateString => {
return formatNoteListDate(dateString)
const setCurrentFolder = folder => { }
currentFolder.value = folder
} const setCurrentFolder = folder => {
currentFolder.value = folder
const setIsFolderExpanded = expanded => { }
isFolderExpanded.value = expanded
} const setIsFolderExpanded = expanded => {
isFolderExpanded.value = expanded
const setSearchQuery = query => { }
searchQuery.value = query
} const setSearchQuery = query => {
searchQuery.value = query
const notes = computed(() => store.notes) }
</script>
<style lang="less" scoped> const notes = computed(() => store.notes)
.container { </script>
width: 100vw; <style lang="less" scoped>
height: 100vh; .container {
background: url(/assets/icons/drawable-xxhdpi/note_background.png); width: 100vw;
background-size: cover; height: 100vh;
} background: url(/assets/icons/drawable-xxhdpi/note_background.png);
background-size: cover;
.folder-list { }
position: absolute;
top: 3.125rem; .folder-list {
left: 10%; position: absolute;
right: 10%; top: 3.125rem;
z-index: 1000; left: 10%;
background-color: var(--background-card); right: 10%;
border-radius: 0.5rem; z-index: 1000;
box-shadow: 0 0.125rem 0.25rem var(--shadow); background-color: var(--background-card);
border: 1px solid #f0ece7; border-radius: 0.5rem;
overflow: hidden; box-shadow: 0 0.125rem 0.25rem var(--shadow);
} border: 1px solid #f0ece7;
overflow: hidden;
.folder-overlay { }
position: absolute;
top: 0; .folder-overlay {
left: 0; position: absolute;
right: 0; top: 0;
bottom: 0; left: 0;
background-color: transparent; right: 0;
z-index: 99; bottom: 0;
} background-color: transparent;
.content { z-index: 99;
--background: transparent; }
--padding-top: 4.5rem; .content {
--padding-bottom: 2rem; --background: transparent;
} --padding-top: 4.5rem;
--padding-bottom: 2rem;
.search-container { }
padding: 0.8rem 0.5rem;
} .search-container {
padding: 0.8rem 0.5rem;
.notes-container { }
flex: 1;
position: relative; .notes-container {
} flex: 1;
position: relative;
.notes-list { }
position: relative;
} .notes-list {
position: relative;
.note-item { }
margin: 0.6rem 0;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94); .note-item {
} margin: 0.6rem 0;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
/* 便签列表动画 */ }
.note-list-enter-active,
.note-list-leave-active { /* 便签列表动画 */
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94); .note-list-enter-active,
} .note-list-leave-active {
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
.note-list-leave-to { }
opacity: 0;
transform: translateX(-30px); .note-list-leave-to {
} opacity: 0;
transform: translateX(-30px);
.note-list-move { }
transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
} .note-list-move {
transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
.note-list-leave-active { }
position: absolute;
width: calc(100% - 1rem); .note-list-leave-active {
} position: absolute;
</style> width: calc(100% - 1rem);
}
/* 文件夹列表动画 */
.folder-slide-enter-active,
.folder-slide-leave-active {
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.folder-slide-enter-from {
opacity: 0;
transform: scale(0.8) translateY(-20px);
}
.folder-slide-enter-to {
opacity: 1;
transform: scale(1) translateY(0);
}
.folder-slide-leave-from {
opacity: 1;
transform: scale(1) translateY(0);
}
.folder-slide-leave-to {
opacity: 0;
transform: scale(0.8) translateY(-20px);
}
</style>