2023-10-30 01:12:20 +01:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div ref="rootEl">
|
2023-10-30 07:16:59 +01:00
|
|
|
<div v-if="isPullStart" :class="$style.frame" :style="`--frame-min-height: ${pullDistance / (PULL_BRAKE_BASE + (pullDistance / PULL_BRAKE_FACTOR))}px;`">
|
2023-10-30 01:12:20 +01:00
|
|
|
<div :class="$style.frameContent">
|
|
|
|
<MkLoading v-if="isRefreshing" :class="$style.loader" :em="true"/>
|
2023-11-03 23:20:53 +01:00
|
|
|
<i v-else class="ph-arrow-line-down ph-bold ph-lg" :class="[$style.icon, { [$style.refresh]: isPullEnd }]"></i>
|
2023-10-30 01:12:20 +01:00
|
|
|
<div :class="$style.text">
|
|
|
|
<template v-if="isPullEnd">{{ i18n.ts.releaseToRefresh }}</template>
|
|
|
|
<template v-else-if="isRefreshing">{{ i18n.ts.refreshing }}</template>
|
|
|
|
<template v-else>{{ i18n.ts.pullDownToRefresh }}</template>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div :class="{ [$style.slotClip]: isPullStart }">
|
|
|
|
<slot/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-12-21 03:36:45 +01:00
|
|
|
import { onMounted, onUnmounted, ref, shallowRef } from 'vue';
|
2023-10-30 01:12:20 +01:00
|
|
|
import { i18n } from '@/i18n.js';
|
2023-11-04 00:56:48 +01:00
|
|
|
import { getScrollContainer } from '@/scripts/scroll.js';
|
2024-02-07 12:02:29 +01:00
|
|
|
import { isHorizontalSwipeSwiping } from '@/scripts/touch.js';
|
2023-10-30 01:12:20 +01:00
|
|
|
|
|
|
|
const SCROLL_STOP = 10;
|
|
|
|
const MAX_PULL_DISTANCE = Infinity;
|
2023-10-30 07:16:59 +01:00
|
|
|
const FIRE_THRESHOLD = 230;
|
2023-10-30 01:12:20 +01:00
|
|
|
const RELEASE_TRANSITION_DURATION = 200;
|
2023-11-04 05:58:41 +01:00
|
|
|
const PULL_BRAKE_BASE = 1.5;
|
|
|
|
const PULL_BRAKE_FACTOR = 170;
|
2023-10-30 01:12:20 +01:00
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
const isPullStart = ref(false);
|
|
|
|
const isPullEnd = ref(false);
|
|
|
|
const isRefreshing = ref(false);
|
|
|
|
const pullDistance = ref(0);
|
2023-10-30 01:12:20 +01:00
|
|
|
|
|
|
|
let supportPointerDesktop = false;
|
|
|
|
let startScreenY: number | null = null;
|
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
const rootEl = shallowRef<HTMLDivElement>();
|
2023-10-30 01:12:20 +01:00
|
|
|
let scrollEl: HTMLElement | null = null;
|
|
|
|
|
|
|
|
let disabled = false;
|
|
|
|
|
2023-11-02 10:07:42 +01:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
refresher: () => Promise<void>;
|
|
|
|
}>(), {
|
|
|
|
refresher: () => Promise.resolve(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2023-10-30 01:12:20 +01:00
|
|
|
(ev: 'refresh'): void;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
function getScreenY(event) {
|
|
|
|
if (supportPointerDesktop) {
|
|
|
|
return event.screenY;
|
|
|
|
}
|
|
|
|
return event.touches[0].screenY;
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveStart(event) {
|
2023-12-07 06:42:09 +01:00
|
|
|
if (!isPullStart.value && !isRefreshing.value && !disabled) {
|
|
|
|
isPullStart.value = true;
|
2023-10-30 01:12:20 +01:00
|
|
|
startScreenY = getScreenY(event);
|
2023-12-07 06:42:09 +01:00
|
|
|
pullDistance.value = 0;
|
2023-10-30 01:12:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveBySystem(to: number): Promise<void> {
|
|
|
|
return new Promise(r => {
|
2023-12-07 06:42:09 +01:00
|
|
|
const startHeight = pullDistance.value;
|
|
|
|
const overHeight = pullDistance.value - to;
|
2023-10-30 01:12:20 +01:00
|
|
|
if (overHeight < 1) {
|
|
|
|
r();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const startTime = Date.now();
|
|
|
|
let intervalId = setInterval(() => {
|
|
|
|
const time = Date.now() - startTime;
|
|
|
|
if (time > RELEASE_TRANSITION_DURATION) {
|
2023-12-07 06:42:09 +01:00
|
|
|
pullDistance.value = to;
|
2023-10-30 01:12:20 +01:00
|
|
|
clearInterval(intervalId);
|
|
|
|
r();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const nextHeight = startHeight - (overHeight / RELEASE_TRANSITION_DURATION) * time;
|
2023-12-07 06:42:09 +01:00
|
|
|
if (pullDistance.value < nextHeight) return;
|
|
|
|
pullDistance.value = nextHeight;
|
2023-10-30 01:12:20 +01:00
|
|
|
}, 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fixOverContent() {
|
2023-12-07 06:42:09 +01:00
|
|
|
if (pullDistance.value > FIRE_THRESHOLD) {
|
2023-10-30 01:12:20 +01:00
|
|
|
await moveBySystem(FIRE_THRESHOLD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function closeContent() {
|
2023-12-07 06:42:09 +01:00
|
|
|
if (pullDistance.value > 0) {
|
2023-10-30 01:12:20 +01:00
|
|
|
await moveBySystem(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveEnd() {
|
2023-12-07 06:42:09 +01:00
|
|
|
if (isPullStart.value && !isRefreshing.value) {
|
2023-10-30 01:12:20 +01:00
|
|
|
startScreenY = null;
|
2023-12-07 06:42:09 +01:00
|
|
|
if (isPullEnd.value) {
|
|
|
|
isPullEnd.value = false;
|
|
|
|
isRefreshing.value = true;
|
2023-11-02 10:07:42 +01:00
|
|
|
fixOverContent().then(() => {
|
|
|
|
emit('refresh');
|
|
|
|
props.refresher().then(() => {
|
|
|
|
refreshFinished();
|
|
|
|
});
|
|
|
|
});
|
2023-10-30 01:12:20 +01:00
|
|
|
} else {
|
2023-12-07 06:42:09 +01:00
|
|
|
closeContent().then(() => isPullStart.value = false);
|
2023-10-30 01:12:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-04 00:56:48 +01:00
|
|
|
function moving(event: TouchEvent | PointerEvent) {
|
2023-12-07 06:42:09 +01:00
|
|
|
if (!isPullStart.value || isRefreshing.value || disabled) return;
|
2023-10-30 01:12:20 +01:00
|
|
|
|
2024-02-07 12:02:29 +01:00
|
|
|
if ((scrollEl?.scrollTop ?? 0) > (supportPointerDesktop ? SCROLL_STOP : SCROLL_STOP + pullDistance.value) || isHorizontalSwipeSwiping.value) {
|
2023-12-07 06:42:09 +01:00
|
|
|
pullDistance.value = 0;
|
|
|
|
isPullEnd.value = false;
|
2023-10-30 01:12:20 +01:00
|
|
|
moveEnd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startScreenY === null) {
|
|
|
|
startScreenY = getScreenY(event);
|
|
|
|
}
|
|
|
|
const moveScreenY = getScreenY(event);
|
|
|
|
|
|
|
|
const moveHeight = moveScreenY - startScreenY!;
|
2023-12-07 06:42:09 +01:00
|
|
|
pullDistance.value = Math.min(Math.max(moveHeight, 0), MAX_PULL_DISTANCE);
|
2023-10-30 01:12:20 +01:00
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
if (pullDistance.value > 0) {
|
2023-11-04 00:56:48 +01:00
|
|
|
if (event.cancelable) event.preventDefault();
|
|
|
|
}
|
|
|
|
|
2024-02-07 12:02:29 +01:00
|
|
|
if (pullDistance.value > SCROLL_STOP) {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
isPullEnd.value = pullDistance.value >= FIRE_THRESHOLD;
|
2023-10-30 01:12:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* emit(refresh)が完了したことを知らせる関数
|
|
|
|
*
|
|
|
|
* タイムアウトがないのでこれを最終的に実行しないと出たままになる
|
|
|
|
*/
|
|
|
|
function refreshFinished() {
|
|
|
|
closeContent().then(() => {
|
2023-12-07 06:42:09 +01:00
|
|
|
isPullStart.value = false;
|
|
|
|
isRefreshing.value = false;
|
2023-10-30 01:12:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setDisabled(value) {
|
|
|
|
disabled = value;
|
|
|
|
}
|
|
|
|
|
2023-11-04 00:56:48 +01:00
|
|
|
function onScrollContainerScroll() {
|
|
|
|
const scrollPos = scrollEl!.scrollTop;
|
2023-10-30 01:12:20 +01:00
|
|
|
|
2023-11-04 00:56:48 +01:00
|
|
|
// When at the top of the page, disable vertical overscroll so passive touch listeners can take over.
|
|
|
|
if (scrollPos === 0) {
|
|
|
|
scrollEl!.style.touchAction = 'pan-x pan-down pinch-zoom';
|
|
|
|
registerEventListenersForReadyToPull();
|
2023-10-30 01:12:20 +01:00
|
|
|
} else {
|
2023-11-04 00:56:48 +01:00
|
|
|
scrollEl!.style.touchAction = 'auto';
|
|
|
|
unregisterEventListenersForReadyToPull();
|
2023-10-30 01:12:20 +01:00
|
|
|
}
|
2023-11-04 00:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function registerEventListenersForReadyToPull() {
|
2023-12-07 06:42:09 +01:00
|
|
|
if (rootEl.value == null) return;
|
|
|
|
rootEl.value.addEventListener('touchstart', moveStart, { passive: true });
|
|
|
|
rootEl.value.addEventListener('touchmove', moving, { passive: false }); // passive: falseにしないとpreventDefaultが使えない
|
2023-11-04 00:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function unregisterEventListenersForReadyToPull() {
|
2023-12-07 06:42:09 +01:00
|
|
|
if (rootEl.value == null) return;
|
|
|
|
rootEl.value.removeEventListener('touchstart', moveStart);
|
|
|
|
rootEl.value.removeEventListener('touchmove', moving);
|
2023-11-04 00:56:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
2023-12-07 06:42:09 +01:00
|
|
|
if (rootEl.value == null) return;
|
2023-11-04 00:56:48 +01:00
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
scrollEl = getScrollContainer(rootEl.value);
|
2023-11-04 00:56:48 +01:00
|
|
|
if (scrollEl == null) return;
|
|
|
|
|
|
|
|
scrollEl.addEventListener('scroll', onScrollContainerScroll, { passive: true });
|
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
rootEl.value.addEventListener('touchend', moveEnd, { passive: true });
|
2023-11-04 00:56:48 +01:00
|
|
|
|
|
|
|
registerEventListenersForReadyToPull();
|
2023-10-30 01:12:20 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
2023-11-04 00:56:48 +01:00
|
|
|
if (scrollEl) scrollEl.removeEventListener('scroll', onScrollContainerScroll);
|
|
|
|
|
|
|
|
unregisterEventListenersForReadyToPull();
|
2023-10-30 01:12:20 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
setDisabled,
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" module>
|
|
|
|
.frame {
|
|
|
|
position: relative;
|
|
|
|
overflow: clip;
|
|
|
|
|
|
|
|
width: 100%;
|
|
|
|
min-height: var(--frame-min-height, 0px);
|
|
|
|
|
|
|
|
mask-image: linear-gradient(90deg, #000 0%, #000 80%, transparent);
|
|
|
|
-webkit-mask-image: -webkit-linear-gradient(90deg, #000 0%, #000 80%, transparent);
|
|
|
|
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.frameContent {
|
|
|
|
position: absolute;
|
|
|
|
bottom: 0;
|
|
|
|
width: 100%;
|
|
|
|
margin: 5px 0;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
font-size: 14px;
|
|
|
|
|
|
|
|
> .icon, > .loader {
|
|
|
|
margin: 6px 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
> .icon {
|
|
|
|
transition: transform .25s;
|
|
|
|
|
|
|
|
&.refresh {
|
|
|
|
transform: rotate(180deg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .text {
|
|
|
|
margin: 5px 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.slotClip {
|
|
|
|
overflow-y: clip;
|
|
|
|
}
|
|
|
|
</style>
|