Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/components/Main/NavigationBar/ChannelList/ChannelTree.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<template>
<div
ref="treeElement"
:class="$style.inner"
:style="
visibleAnimation
? undefined
? {}
: {
paddingTop: `${virtualPadding.start}px`,
paddingBottom: `${virtualPadding.end}px`
Expand Down Expand Up @@ -39,7 +40,7 @@
:show-shortened-path="showShortenedPath && item.depth === 0"
:show-star="showStar"
:show-notified="showNotified"
@click-hash="() => handleToggle(item.key)"
@click-hash="handleToggle(item.key)"
/>
</div>

Expand All @@ -60,7 +61,7 @@
</template>

<script lang="ts" setup>
import { computed, toRef } from 'vue'
import { computed, toRef, useTemplateRef } from 'vue'

import ChannelElement from './ChannelElement.vue'
import ChannelTreeAnimation from './ChannelTreeAnimation.vue'
Expand Down Expand Up @@ -88,6 +89,8 @@ const virtualizer = useChannelTreeVirtualizer({
scrollMargin: computed(() => props.scrollMargin)
})

const treeElement = useTemplateRef<HTMLElement>('treeElement')

const {
displayedRows,
visibleAnimation,
Expand All @@ -99,6 +102,7 @@ const {
virtualizer,
flatItems,
expandedNodeKeys,
treeElement,
scrollElement: toRef(props, 'scrollElement'),
scrollMargin: computed(() => props.scrollMargin),
toggle
Expand Down Expand Up @@ -127,6 +131,7 @@ const virtualPadding = computed(() => {
<style lang="scss" module>
.inner {
position: relative;
min-height: var(--tree-min-height, 0px);
overflow-anchor: none;
}

Expand All @@ -135,8 +140,7 @@ const virtualPadding = computed(() => {

&[data-is-animating] {
position: absolute;
top: 0;
transform: translateY(var(--row-y));
top: var(--row-y);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
type ChannelTreeAnimationMeasurement,
type VisibleChannelTreeAnimation
} from './channelTreeAnimation'
import { measureChannelTreeHeight } from './channelTreeMeasurement'

const props = withDefaults(
defineProps<{
Expand Down Expand Up @@ -122,7 +123,7 @@ useRafFn(async () => {

const element = treeElement.value
if (element) {
emit('resize', props.animation.id, element.getBoundingClientRect().height)
emit('resize', props.animation.id, measureChannelTreeHeight(element))
}
})

Expand Down Expand Up @@ -155,7 +156,8 @@ watch(
const measurements = [...elements].flatMap(([key, element]) => {
if (previousElements.get(key) === element) return []

const size = Math.round(element.getBoundingClientRect().height)
const size = measureChannelTreeHeight(element)

return size > 0 ? [{ key, size }] : []
})

Expand All @@ -170,11 +172,13 @@ useResizeObserver(
observedElements,
entries => {
for (const entry of entries) {
const height =
entry.borderBoxSize[0]?.blockSize ?? entry.contentRect.height

const key = (entry.target as HTMLElement).dataset['rowKey']
if (key) pendingMeasurements.set(key, Math.round(height))
if (key) {
pendingMeasurements.set(
key,
measureChannelTreeHeight(entry.target, entry)
)
}
}
},
{ box: 'border-box' }
Expand Down Expand Up @@ -210,9 +214,8 @@ useResizeObserver(
.row {
overflow: hidden;
position: absolute;
top: 0;
top: var(--row-y);
width: 100%;
transform: translateY(var(--row-y));
}

.element {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export const createChannelTreeAnimation = ({
postRows.filter(
({ key, vItem }) =>
!key.startsWith(descendantKeyPrefix) &&
vItem.start - scrollMargin >= origin + finalTreeHeight
vItem.start - scrollMargin >= origin
),
expandedNodeKeys,
origin + finalTreeHeight,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const measureChannelTreeHeight = (
element: Element,
entry?: ResizeObserverEntry
) =>
entry?.borderBoxSize?.[0]?.blockSize ??
element.getBoundingClientRect().height / (element.currentCSSZoom ?? 1)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getVisibleChannelTreeAnimation,
resizeChannelTreeAnimation
} from '../channelTreeAnimation'
import { measureChannelTreeHeight } from '../channelTreeMeasurement'
import type { FlatChannelItem } from './useChannelFlatList'

const TREE_ANIMATION_FALLBACK_MS = CHANNEL_TREE_ANIMATION_DURATION_MS + 50
Expand All @@ -24,6 +25,7 @@ interface ChannelTreeAnimationOptions<
virtualizer: Ref<Virtualizer<TScrollElement, TItemElement>>
flatItems: ComputedRef<FlatChannelItem[]>
expandedNodeKeys: Ref<Set<string>>
treeElement: Readonly<Ref<HTMLElement | null>>
scrollElement: Readonly<Ref<TScrollElement | null>>
scrollMargin: ComputedRef<number>
toggle: (itemKey: string) => void
Expand All @@ -36,6 +38,7 @@ const useChannelTreeAnimation = <
virtualizer,
flatItems,
expandedNodeKeys,
treeElement,
scrollElement,
scrollMargin,
toggle
Expand Down Expand Up @@ -90,12 +93,30 @@ const useChannelTreeAnimation = <
)
})

const finishTreeAnimation = (animationId: number) => {
if (activeAnimation.value?.id === animationId) {
activeAnimation.value = null
resolveTreeAnimation?.()
resolveTreeAnimation = undefined
const holdTreeHeight = () => {
const element = treeElement.value

if (element) {
// 行の入れ替え中にスクロール範囲が縮むとブラウザーが位置を補正しようとする
element.style.setProperty(
'--tree-min-height',
`${measureChannelTreeHeight(element)}px`
)
}

return () => element?.style.removeProperty('--tree-min-height')
}

const finishTreeAnimation = async (animationId: number) => {
if (activeAnimation.value?.id !== animationId) return

const releaseHeight = holdTreeHeight()
const resolve = resolveTreeAnimation
resolveTreeAnimation = undefined
activeAnimation.value = null

await nextTick(releaseHeight)
resolve?.()
}

const updateTreeAnimationHeight = (animationId: number, height: number) => {
Expand Down Expand Up @@ -171,47 +192,58 @@ const useChannelTreeAnimation = <
const descendantKeyPrefix = `${itemKey}/`
const origin = parent.end - scrollMargin.value

toggle(itemKey)
await nextTick()

const postTotalSize = virtualizer.value.getTotalSize()
const postRows = snapshotRows()
for (const row of virtualItemRows.value) retainedKeys.add(row.key)
const totalSizeDelta = postTotalSize - preTotalSize
const releaseHeight = holdTreeHeight()
let finished: Promise<void>

if (totalSizeDelta === 0) {
activeAnimation.value = null
return
try {
toggle(itemKey)
await nextTick()

const postTotalSize = virtualizer.value.getTotalSize()
const postRows = snapshotRows()
for (const row of virtualItemRows.value) retainedKeys.add(row.key)

const totalSizeDelta = postTotalSize - preTotalSize
if (totalSizeDelta === 0) return

const animationId = ++treeAnimationId
currentHeight.value =
direction === 'expand' ? 0 : Math.abs(totalSizeDelta)

activeAnimation.value = createChannelTreeAnimation({
id: animationId,
direction,
origin,
scrollMargin: scrollMargin.value,
preRows,
postRows,
retainedKeys,
preExpandedNodeKeys,
expandedNodeKeys: expandedNodeKeys.value,
postTotalSize,
totalSizeDelta,
descendantKeyPrefix
})

finished = new Promise<void>(resolve => {
resolveTreeAnimation = resolve
setTimeout(
() => void finishTreeAnimation(animationId),
TREE_ANIMATION_FALLBACK_MS
)
})
} finally {
await nextTick(releaseHeight)
}

const animationId = ++treeAnimationId
currentHeight.value = direction === 'expand' ? 0 : Math.abs(totalSizeDelta)
activeAnimation.value = createChannelTreeAnimation({
id: animationId,
direction,
origin,
scrollMargin: scrollMargin.value,
preRows,
postRows,
retainedKeys,
preExpandedNodeKeys,
expandedNodeKeys: expandedNodeKeys.value,
postTotalSize,
totalSizeDelta,
descendantKeyPrefix
})
await new Promise<void>(resolve => {
resolveTreeAnimation = resolve
setTimeout(
() => finishTreeAnimation(animationId),
TREE_ANIMATION_FALLBACK_MS
)
})
await finished
}

let toggleQueue = Promise.resolve()
const handleToggle = (itemKey: string) => {
toggleQueue = toggleQueue.then(() => toggleWithAnimation(itemKey))

return toggleQueue
}

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { computed, ref, watch } from 'vue'
import type { ComputedRef, Ref } from 'vue'

import { useVirtualizer } from '@tanstack/vue-virtual'
import { type VirtualizerOptions, useVirtualizer } from '@tanstack/vue-virtual'
import { useRafFn } from '@vueuse/core'

import { useNavigationVisibility } from '../../composables/useNavigationVisibility'
import { measureChannelTreeHeight } from '../channelTreeMeasurement'
import { createScrollCorrectionHandlers } from '../virtualizerScrollCorrection'
import type { FlatChannelItem } from './useChannelFlatList'
import useTouchScrollInteraction from './useTouchScrollInteraction'
Expand All @@ -17,6 +18,27 @@ interface ChannelTreeVirtualizerOptions {
scrollMargin: ComputedRef<number>
}

const measureRow: VirtualizerOptions<
HTMLElement,
HTMLElement
>['measureElement'] = (element, entry, instance) => {
if (!entry || instance.options.useCachedMeasurements) {
const index = instance.indexFromElement(element)
const cachedSize = instance.itemSizeCache.get(
instance.options.getItemKey(index)
)

if (instance.options.useCachedMeasurements) {
return cachedSize ?? instance.options.estimateSize(index)
}

if (cachedSize !== undefined) return cachedSize
}

// 通常の表示と開閉アニメーションで行の位置を揃えるために端数も保持
return measureChannelTreeHeight(element, entry)
}

const useChannelTreeVirtualizer = ({
flatItems,
scrollElement,
Expand Down Expand Up @@ -50,6 +72,7 @@ const useChannelTreeVirtualizer = ({
getScrollElement: () => scrollElement.value,
initialOffset: () => scrollElement.value?.scrollTop ?? 0,
estimateSize: () => 36,
measureElement: measureRow,
useCachedMeasurements: useCachedMeasurements.value,
useAnimationFrameWithResizeObserver: true,
overscan: 5,
Expand Down
38 changes: 27 additions & 11 deletions src/components/UI/MarkdownPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
<script lang="ts" setup>
import type { MarkdownRenderResult } from '@traptitech/traq-markdown-it'

import { computed, nextTick, ref, watchEffect } from 'vue'
import { computed, nextTick, ref, watch } from 'vue'

import useMarkdownInternalHandler from '/@/composables/markdown/useMarkdownInternalHandler'
import { render, renderInline } from '/@/lib/markdown/markdown'
import {
render,
renderInline,
renderInlineIfReady
} from '/@/lib/markdown/markdown'

const props = withDefaults(
defineProps<{
Expand All @@ -43,15 +47,27 @@ const emit = defineEmits<{
}>()

const rendered = ref<MarkdownRenderResult>()
watchEffect(async () => {
if (props.inline) {
rendered.value = await renderInline(props.content)
} else {
rendered.value = await render(props.content)
}
await nextTick()
emit('render')
})
watch(
() => [props.inline, props.content] as const,
async ([inline, content], _previous, onCleanup) => {
let cancelled = false
onCleanup(() => {
cancelled = true
})

const result = inline
? (renderInlineIfReady(content) ?? (await renderInline(content)))
: await render(content)

if (cancelled) return

rendered.value = result
await nextTick()
if (!cancelled) emit('render')
},
{ immediate: true }
)

const renderedContent = computed(() => rendered.value?.renderedText)

const { onClick } = useMarkdownInternalHandler(() =>
Expand Down
Loading
Loading