Skip to content

Commit

Permalink
Merge pull request #4048 from traPtitech/SSlime/fix-message-search
Browse files Browse the repository at this point in the history
検索モーダルを空にしたときの挙動を修正
  • Loading branch information
mehm8128 authored Aug 24, 2023
2 parents e5b8de9 + 1ac077e commit 35b2133
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/components/Main/CommandPalette/SearchResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</template>

<script lang="ts" setup>
import { computed, onMounted, ref, watch } from 'vue'
import { computed, onMounted, onBeforeUnmount, ref, watch } from 'vue'
import type { MessageId } from '/@/types/entity-ids'
import { useCommandPalette } from '/@/store/app/commandPalette'
import type { PopupSelectorItem } from '/@/components/UI/PopupSelector.vue'
Expand Down Expand Up @@ -78,7 +78,8 @@ const {
resetPaging,
pageCount,
currentSortKey,
query
query,
executed
} = useSearchMessages()
watch(
Expand All @@ -94,8 +95,17 @@ watch(
)
onMounted(() => {
resetPaging()
executeSearchForCurrentPage(query.value)
// 初回マウント時に取得する
if (!executed.value) {
executeSearchForCurrentPage(query.value)
}
})
onBeforeUnmount(() => {
// 検索クエリを空にして Enter を押したときにリセットされるようにする
if (query.value === '') {
resetPaging()
noRestore()
}
})
const resultListEle = ref<HTMLElement | null>(null)
Expand All @@ -116,7 +126,7 @@ const jumpToPage = (page: number) => {
}
}
const { didRender } = useKeepScrollPosition(
const { didRender, noRestore } = useKeepScrollPosition(
resultListEle,
computed(() => searchResult.value.map(message => message.id))
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Ref } from 'vue'
import { onBeforeUnmount } from 'vue'
import { onBeforeUnmount, ref } from 'vue'
import useOnAllRendered from './useOnAllRendered'
import { useCommandPalette } from '/@/store/app/commandPalette'
import type { MessageId } from '/@/types/entity-ids'
Expand All @@ -10,6 +10,10 @@ const useKeepScrollPosition = (
) => {
const { currentScrollTop } = useCommandPalette()
const { didRender, onAllRendered } = useOnAllRendered(list)
const isNoRestore = ref(false)
const noRestore = () => {
isNoRestore.value = true
}

// 開くときにマークダウンが描画しおえたらスクロール位置を適用
onAllRendered(() => {
Expand All @@ -21,11 +25,15 @@ const useKeepScrollPosition = (
// 閉じるときにスクロール位置を保持
onBeforeUnmount(() => {
if (ele.value) {
if (isNoRestore.value) {
currentScrollTop.value = 0
return
}
currentScrollTop.value = ele.value.scrollTop
}
})

return { didRender }
return { didRender, noRestore }
}

export default useKeepScrollPosition

0 comments on commit 35b2133

Please sign in to comment.