2023-07-27 14:31:52 +09:00
|
|
|
|
<!--
|
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
-->
|
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
|
<template>
|
2023-01-14 15:01:28 +09:00
|
|
|
|
<Transition
|
2023-05-19 13:58:09 +09:00
|
|
|
|
:enterActiveClass="defaultStore.state.animation ? $style.transition_fade_enterActive : ''"
|
|
|
|
|
:leaveActiveClass="defaultStore.state.animation ? $style.transition_fade_leaveActive : ''"
|
|
|
|
|
:enterFromClass="defaultStore.state.animation ? $style.transition_fade_enterFrom : ''"
|
|
|
|
|
:leaveToClass="defaultStore.state.animation ? $style.transition_fade_leaveTo : ''"
|
2023-01-14 15:01:28 +09:00
|
|
|
|
mode="out-in"
|
|
|
|
|
>
|
2021-04-22 22:29:33 +09:00
|
|
|
|
<MkLoading v-if="fetching"/>
|
|
|
|
|
|
|
|
|
|
<MkError v-else-if="error" @retry="init()"/>
|
|
|
|
|
|
2021-11-19 19:36:12 +09:00
|
|
|
|
<div v-else-if="empty" key="_empty_" class="empty">
|
2021-12-23 16:10:13 +09:00
|
|
|
|
<slot name="empty">
|
|
|
|
|
<div class="_fullinfo">
|
2023-06-09 14:00:53 +09:00
|
|
|
|
<img :src="infoImageUrl" class="_ghost"/>
|
2022-07-20 22:24:26 +09:00
|
|
|
|
<div>{{ i18n.ts.nothing }}</div>
|
2021-12-23 16:10:13 +09:00
|
|
|
|
</div>
|
|
|
|
|
</slot>
|
2020-01-30 04:37:25 +09:00
|
|
|
|
</div>
|
2021-04-22 22:29:33 +09:00
|
|
|
|
|
2022-01-09 21:35:35 +09:00
|
|
|
|
<div v-else ref="rootEl">
|
2023-01-14 15:01:28 +09:00
|
|
|
|
<div v-show="pagination.reversed && more" key="_more_" class="_margin">
|
2023-07-05 00:59:37 +09:00
|
|
|
|
<MkButton v-if="!moreFetching" v-appear="(enableInfiniteScroll && !props.disableAutoLoad) ? appearFetchMoreAhead : null" :class="$style.more" :disabled="moreFetching" :style="{ cursor: moreFetching ? 'wait' : 'pointer' }" primary rounded @click="fetchMoreAhead">
|
2022-07-20 22:24:26 +09:00
|
|
|
|
{{ i18n.ts.loadMore }}
|
2022-05-19 20:32:55 +09:00
|
|
|
|
</MkButton>
|
|
|
|
|
<MkLoading v-else class="loading"/>
|
|
|
|
|
</div>
|
2023-07-05 00:59:37 +09:00
|
|
|
|
<slot :items="Array.from(items.values())" :fetching="fetching || moreFetching"></slot>
|
2023-01-14 15:01:28 +09:00
|
|
|
|
<div v-show="!pagination.reversed && more" key="_more_" class="_margin">
|
2023-07-05 00:59:37 +09:00
|
|
|
|
<MkButton v-if="!moreFetching" v-appear="(enableInfiniteScroll && !props.disableAutoLoad) ? appearFetchMore : null" :class="$style.more" :disabled="moreFetching" :style="{ cursor: moreFetching ? 'wait' : 'pointer' }" primary rounded @click="fetchMore">
|
2022-07-20 22:24:26 +09:00
|
|
|
|
{{ i18n.ts.loadMore }}
|
2021-04-22 22:29:33 +09:00
|
|
|
|
</MkButton>
|
2022-01-09 21:35:35 +09:00
|
|
|
|
<MkLoading v-else class="loading"/>
|
2021-04-22 22:29:33 +09:00
|
|
|
|
</div>
|
2020-01-30 04:37:25 +09:00
|
|
|
|
</div>
|
2022-12-30 13:37:14 +09:00
|
|
|
|
</Transition>
|
2020-01-30 04:37:25 +09:00
|
|
|
|
</template>
|
|
|
|
|
|
2023-01-13 18:25:40 +09:00
|
|
|
|
<script lang="ts">
|
2023-12-07 14:42:09 +09:00
|
|
|
|
import { computed, ComputedRef, isRef, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onDeactivated, ref, shallowRef, watch } from 'vue';
|
2023-09-04 13:33:38 +09:00
|
|
|
|
import * as Misskey from 'misskey-js';
|
2023-09-19 16:37:43 +09:00
|
|
|
|
import * as os from '@/os.js';
|
2024-01-04 18:32:46 +09:00
|
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-09-19 16:37:43 +09:00
|
|
|
|
import { onScrollTop, isTopVisible, getBodyScrollHeight, getScrollContainer, onScrollBottom, scrollToBottom, scroll, isBottomVisible } from '@/scripts/scroll.js';
|
|
|
|
|
import { useDocumentVisibility } from '@/scripts/use-document-visibility.js';
|
|
|
|
|
import { defaultStore } from '@/store.js';
|
2023-12-24 16:16:58 +09:00
|
|
|
|
import { MisskeyEntity } from '@/types/date-separated-list.js';
|
2023-09-19 16:37:43 +09:00
|
|
|
|
import { i18n } from '@/i18n.js';
|
2022-01-09 21:35:35 +09:00
|
|
|
|
|
|
|
|
|
const SECOND_FETCH_LIMIT = 30;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
const TOLERANCE = 16;
|
2023-07-05 00:59:37 +09:00
|
|
|
|
const APPEAR_MINIMUM_INTERVAL = 600;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
|
2023-09-04 13:33:38 +09:00
|
|
|
|
export type Paging<E extends keyof Misskey.Endpoints = keyof Misskey.Endpoints> = {
|
2022-01-09 21:35:35 +09:00
|
|
|
|
endpoint: E;
|
|
|
|
|
limit: number;
|
2023-09-04 13:33:38 +09:00
|
|
|
|
params?: Misskey.Endpoints[E]['req'] | ComputedRef<Misskey.Endpoints[E]['req']>;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 検索APIのような、ページング不可なエンドポイントを利用する場合
|
|
|
|
|
* (そのようなAPIをこの関数で使うのは若干矛盾してるけど)
|
|
|
|
|
*/
|
|
|
|
|
noPaging?: boolean;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* items 配列の中身を逆順にする(新しい方が最後)
|
|
|
|
|
*/
|
|
|
|
|
reversed?: boolean;
|
2022-01-10 01:00:50 +09:00
|
|
|
|
|
|
|
|
|
offsetMode?: boolean;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
|
2023-01-13 18:25:40 +09:00
|
|
|
|
pageEl?: HTMLElement;
|
|
|
|
|
};
|
2023-07-05 00:59:37 +09:00
|
|
|
|
|
|
|
|
|
type MisskeyEntityMap = Map<string, MisskeyEntity>;
|
|
|
|
|
|
|
|
|
|
function arrayToEntries(entities: MisskeyEntity[]): [string, MisskeyEntity][] {
|
|
|
|
|
return entities.map(en => [en.id, en]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function concatMapWithArray(map: MisskeyEntityMap, entities: MisskeyEntity[]): MisskeyEntityMap {
|
|
|
|
|
return new Map([...map, ...arrayToEntries(entities)]);
|
|
|
|
|
}
|
2023-10-29 16:09:20 +09:00
|
|
|
|
|
2023-01-13 18:25:40 +09:00
|
|
|
|
</script>
|
|
|
|
|
<script lang="ts" setup>
|
2023-09-19 16:37:43 +09:00
|
|
|
|
import { infoImageUrl } from '@/instance.js';
|
2023-11-15 11:24:54 +09:00
|
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-06-09 14:00:53 +09:00
|
|
|
|
|
2022-01-09 21:35:35 +09:00
|
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
|
pagination: Paging;
|
|
|
|
|
disableAutoLoad?: boolean;
|
|
|
|
|
displayLimit?: number;
|
|
|
|
|
}>(), {
|
2023-01-03 09:41:32 +09:00
|
|
|
|
displayLimit: 20,
|
2022-01-09 21:35:35 +09:00
|
|
|
|
});
|
|
|
|
|
|
2022-01-09 22:57:27 +09:00
|
|
|
|
const emit = defineEmits<{
|
2022-05-26 22:53:09 +09:00
|
|
|
|
(ev: 'queue', count: number): void;
|
2023-10-30 09:12:20 +09:00
|
|
|
|
(ev: 'status', error: boolean): void;
|
2022-01-09 22:57:27 +09:00
|
|
|
|
}>();
|
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
const rootEl = shallowRef<HTMLElement>();
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
|
|
|
|
// 遡り中かどうか
|
2023-12-07 14:42:09 +09:00
|
|
|
|
const backed = ref(false);
|
2022-01-10 01:00:50 +09:00
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
const scrollRemove = ref<(() => void) | null>(null);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
2023-07-05 00:59:37 +09:00
|
|
|
|
/**
|
|
|
|
|
* 表示するアイテムのソース
|
|
|
|
|
* 最新が0番目
|
|
|
|
|
*/
|
|
|
|
|
const items = ref<MisskeyEntityMap>(new Map());
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* タブが非アクティブなどの場合に更新を貯めておく
|
|
|
|
|
* 最新が0番目
|
|
|
|
|
*/
|
|
|
|
|
const queue = ref<MisskeyEntityMap>(new Map());
|
|
|
|
|
|
2022-01-09 21:35:35 +09:00
|
|
|
|
const offset = ref(0);
|
2023-07-05 00:59:37 +09:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初期化中かどうか(trueならMkLoadingで全て隠す)
|
|
|
|
|
*/
|
2022-01-09 21:35:35 +09:00
|
|
|
|
const fetching = ref(true);
|
2023-07-05 00:59:37 +09:00
|
|
|
|
|
2022-01-09 21:35:35 +09:00
|
|
|
|
const moreFetching = ref(false);
|
|
|
|
|
const more = ref(false);
|
2023-07-05 00:59:37 +09:00
|
|
|
|
const preventAppearFetchMore = ref(false);
|
|
|
|
|
const preventAppearFetchMoreTimer = ref<number | null>(null);
|
2022-01-09 21:35:35 +09:00
|
|
|
|
const isBackTop = ref(false);
|
2023-07-05 00:59:37 +09:00
|
|
|
|
const empty = computed(() => items.value.size === 0);
|
2022-01-21 16:43:56 +09:00
|
|
|
|
const error = ref(false);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
const {
|
2023-01-14 15:01:28 +09:00
|
|
|
|
enableInfiniteScroll,
|
2023-01-13 18:25:40 +09:00
|
|
|
|
} = defaultStore.reactiveState;
|
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
const contentEl = computed(() => props.pagination.pageEl ?? rootEl.value);
|
|
|
|
|
const scrollableElement = computed(() => contentEl.value ? getScrollContainer(contentEl.value) : document.body);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
2023-02-25 08:18:12 +09:00
|
|
|
|
const visibility = useDocumentVisibility();
|
|
|
|
|
|
|
|
|
|
let isPausingUpdate = false;
|
|
|
|
|
let timerForSetPause: number | null = null;
|
|
|
|
|
const BACKGROUND_PAUSE_WAIT_SEC = 10;
|
|
|
|
|
|
2023-01-13 18:25:40 +09:00
|
|
|
|
// 先頭が表示されているかどうかを検出
|
|
|
|
|
// https://qiita.com/mkataigi/items/0154aefd2223ce23398e
|
2023-12-07 14:42:09 +09:00
|
|
|
|
const scrollObserver = ref<IntersectionObserver>();
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
watch([() => props.pagination.reversed, scrollableElement], () => {
|
|
|
|
|
if (scrollObserver.value) scrollObserver.value.disconnect();
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
scrollObserver.value = new IntersectionObserver(entries => {
|
|
|
|
|
backed.value = entries[0].isIntersecting;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
}, {
|
2023-12-07 14:42:09 +09:00
|
|
|
|
root: scrollableElement.value,
|
2023-01-13 18:25:40 +09:00
|
|
|
|
rootMargin: props.pagination.reversed ? '-100% 0px 100% 0px' : '100% 0px -100% 0px',
|
|
|
|
|
threshold: 0.01,
|
|
|
|
|
});
|
|
|
|
|
}, { immediate: true });
|
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
watch(rootEl, () => {
|
|
|
|
|
scrollObserver.value?.disconnect();
|
2023-01-13 18:25:40 +09:00
|
|
|
|
nextTick(() => {
|
2023-12-07 14:42:09 +09:00
|
|
|
|
if (rootEl.value) scrollObserver.value?.observe(rootEl.value);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
watch([backed, contentEl], () => {
|
|
|
|
|
if (!backed.value) {
|
|
|
|
|
if (!contentEl.value) return;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
scrollRemove.value = (props.pagination.reversed ? onScrollBottom : onScrollTop)(contentEl.value, executeQueue, TOLERANCE);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
} else {
|
2023-12-07 14:42:09 +09:00
|
|
|
|
if (scrollRemove.value) scrollRemove.value();
|
|
|
|
|
scrollRemove.value = null;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-11-15 11:24:54 +09:00
|
|
|
|
// パラメータに何らかの変更があった際、再読込したい(チャンネル等のIDが変わったなど)
|
2023-11-28 20:43:25 +09:00
|
|
|
|
watch(() => [props.pagination.endpoint, props.pagination.params], init, { deep: true });
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
|
|
|
|
watch(queue, (a, b) => {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
if (a.size === 0 && b.size === 0) return;
|
|
|
|
|
emit('queue', queue.value.size);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
}, { deep: true });
|
2022-01-09 21:35:35 +09:00
|
|
|
|
|
2023-10-30 09:12:20 +09:00
|
|
|
|
watch(error, (n, o) => {
|
|
|
|
|
if (n === o) return;
|
|
|
|
|
emit('status', n);
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-13 18:25:40 +09:00
|
|
|
|
async function init(): Promise<void> {
|
2023-07-06 15:43:05 +09:00
|
|
|
|
items.value = new Map();
|
2023-07-05 00:59:37 +09:00
|
|
|
|
queue.value = new Map();
|
2022-01-09 21:35:35 +09:00
|
|
|
|
fetching.value = true;
|
|
|
|
|
const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {};
|
2024-01-04 18:32:46 +09:00
|
|
|
|
await misskeyApi(props.pagination.endpoint, {
|
2022-01-09 21:35:35 +09:00
|
|
|
|
...params,
|
2023-04-03 11:50:17 +09:00
|
|
|
|
limit: props.pagination.limit ?? 10,
|
2023-12-02 18:25:07 +09:00
|
|
|
|
allowPartial: true,
|
2022-01-09 21:35:35 +09:00
|
|
|
|
}).then(res => {
|
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
|
const item = res[i];
|
2023-01-13 18:25:40 +09:00
|
|
|
|
if (i === 3) item._shouldInsertAd_ = true;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
}
|
2023-04-03 11:50:17 +09:00
|
|
|
|
|
|
|
|
|
if (res.length === 0 || props.pagination.noPaging) {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
concatItems(res);
|
2023-04-03 11:50:17 +09:00
|
|
|
|
more.value = false;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
} else {
|
2023-04-03 11:50:17 +09:00
|
|
|
|
if (props.pagination.reversed) moreFetching.value = true;
|
2023-07-05 00:59:37 +09:00
|
|
|
|
concatItems(res);
|
2023-04-03 11:50:17 +09:00
|
|
|
|
more.value = true;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
}
|
2023-04-03 11:50:17 +09:00
|
|
|
|
|
2022-01-09 21:35:35 +09:00
|
|
|
|
offset.value = res.length;
|
2022-01-21 16:43:56 +09:00
|
|
|
|
error.value = false;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
fetching.value = false;
|
2022-05-26 22:53:09 +09:00
|
|
|
|
}, err => {
|
2022-01-21 16:43:56 +09:00
|
|
|
|
error.value = true;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
fetching.value = false;
|
|
|
|
|
});
|
2023-01-13 18:25:40 +09:00
|
|
|
|
}
|
2022-01-09 21:35:35 +09:00
|
|
|
|
|
2023-01-13 18:25:40 +09:00
|
|
|
|
const reload = (): Promise<void> => {
|
|
|
|
|
return init();
|
2022-01-09 21:35:35 +09:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-10 01:00:50 +09:00
|
|
|
|
const fetchMore = async (): Promise<void> => {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
if (!more.value || fetching.value || moreFetching.value || items.value.size === 0) return;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
moreFetching.value = true;
|
|
|
|
|
const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {};
|
2024-01-04 18:32:46 +09:00
|
|
|
|
await misskeyApi(props.pagination.endpoint, {
|
2022-01-09 21:35:35 +09:00
|
|
|
|
...params,
|
2023-04-03 11:50:17 +09:00
|
|
|
|
limit: SECOND_FETCH_LIMIT,
|
2022-01-09 21:35:35 +09:00
|
|
|
|
...(props.pagination.offsetMode ? {
|
|
|
|
|
offset: offset.value,
|
|
|
|
|
} : {
|
2023-07-14 10:45:01 +09:00
|
|
|
|
untilId: Array.from(items.value.keys()).at(-1),
|
2022-01-09 21:35:35 +09:00
|
|
|
|
}),
|
|
|
|
|
}).then(res => {
|
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
|
const item = res[i];
|
2023-01-13 18:25:40 +09:00
|
|
|
|
if (i === 10) item._shouldInsertAd_ = true;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
}
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
|
|
|
|
const reverseConcat = _res => {
|
2023-12-07 14:42:09 +09:00
|
|
|
|
const oldHeight = scrollableElement.value ? scrollableElement.value.scrollHeight : getBodyScrollHeight();
|
|
|
|
|
const oldScroll = scrollableElement.value ? scrollableElement.value.scrollTop : window.scrollY;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
2023-07-05 00:59:37 +09:00
|
|
|
|
items.value = concatMapWithArray(items.value, _res);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
|
|
|
|
return nextTick(() => {
|
2023-12-07 14:42:09 +09:00
|
|
|
|
if (scrollableElement.value) {
|
|
|
|
|
scroll(scrollableElement.value, { top: oldScroll + (scrollableElement.value.scrollHeight - oldHeight), behavior: 'instant' });
|
2023-01-13 18:25:40 +09:00
|
|
|
|
} else {
|
|
|
|
|
window.scroll({ top: oldScroll + (getBodyScrollHeight() - oldHeight), behavior: 'instant' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nextTick();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-03 11:50:17 +09:00
|
|
|
|
if (res.length === 0) {
|
2023-01-13 18:25:40 +09:00
|
|
|
|
if (props.pagination.reversed) {
|
|
|
|
|
reverseConcat(res).then(() => {
|
2023-04-03 11:50:17 +09:00
|
|
|
|
more.value = false;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
moreFetching.value = false;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
items.value = concatMapWithArray(items.value, res);
|
2023-04-03 11:50:17 +09:00
|
|
|
|
more.value = false;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
moreFetching.value = false;
|
|
|
|
|
}
|
2022-01-09 21:35:35 +09:00
|
|
|
|
} else {
|
2023-01-13 18:25:40 +09:00
|
|
|
|
if (props.pagination.reversed) {
|
|
|
|
|
reverseConcat(res).then(() => {
|
2023-04-03 11:50:17 +09:00
|
|
|
|
more.value = true;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
moreFetching.value = false;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
items.value = concatMapWithArray(items.value, res);
|
2023-04-03 11:50:17 +09:00
|
|
|
|
more.value = true;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
moreFetching.value = false;
|
|
|
|
|
}
|
2022-01-09 21:35:35 +09:00
|
|
|
|
}
|
|
|
|
|
offset.value += res.length;
|
2022-05-26 22:53:09 +09:00
|
|
|
|
}, err => {
|
2022-01-09 21:35:35 +09:00
|
|
|
|
moreFetching.value = false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-10 01:00:50 +09:00
|
|
|
|
const fetchMoreAhead = async (): Promise<void> => {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
if (!more.value || fetching.value || moreFetching.value || items.value.size === 0) return;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
moreFetching.value = true;
|
|
|
|
|
const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {};
|
2024-01-04 18:32:46 +09:00
|
|
|
|
await misskeyApi(props.pagination.endpoint, {
|
2022-01-09 21:35:35 +09:00
|
|
|
|
...params,
|
2023-04-03 11:50:17 +09:00
|
|
|
|
limit: SECOND_FETCH_LIMIT,
|
2022-01-09 21:35:35 +09:00
|
|
|
|
...(props.pagination.offsetMode ? {
|
|
|
|
|
offset: offset.value,
|
|
|
|
|
} : {
|
2023-07-14 10:45:01 +09:00
|
|
|
|
sinceId: Array.from(items.value.keys()).at(-1),
|
2022-01-09 21:35:35 +09:00
|
|
|
|
}),
|
|
|
|
|
}).then(res => {
|
2023-04-03 11:50:17 +09:00
|
|
|
|
if (res.length === 0) {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
items.value = concatMapWithArray(items.value, res);
|
2023-04-03 11:50:17 +09:00
|
|
|
|
more.value = false;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
} else {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
items.value = concatMapWithArray(items.value, res);
|
2023-04-03 11:50:17 +09:00
|
|
|
|
more.value = true;
|
2022-01-09 21:35:35 +09:00
|
|
|
|
}
|
|
|
|
|
offset.value += res.length;
|
|
|
|
|
moreFetching.value = false;
|
2022-05-26 22:53:09 +09:00
|
|
|
|
}, err => {
|
2022-01-09 21:35:35 +09:00
|
|
|
|
moreFetching.value = false;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-05 00:59:37 +09:00
|
|
|
|
/**
|
|
|
|
|
* Appear(IntersectionObserver)によってfetchMoreが呼ばれる場合、
|
|
|
|
|
* APPEAR_MINIMUM_INTERVALミリ秒以内に2回fetchMoreが呼ばれるのを防ぐ
|
|
|
|
|
*/
|
|
|
|
|
const fetchMoreApperTimeoutFn = (): void => {
|
|
|
|
|
preventAppearFetchMore.value = false;
|
|
|
|
|
preventAppearFetchMoreTimer.value = null;
|
|
|
|
|
};
|
|
|
|
|
const fetchMoreAppearTimeout = (): void => {
|
|
|
|
|
preventAppearFetchMore.value = true;
|
|
|
|
|
preventAppearFetchMoreTimer.value = window.setTimeout(fetchMoreApperTimeoutFn, APPEAR_MINIMUM_INTERVAL);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const appearFetchMore = async (): Promise<void> => {
|
|
|
|
|
if (preventAppearFetchMore.value) return;
|
|
|
|
|
await fetchMore();
|
|
|
|
|
fetchMoreAppearTimeout();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const appearFetchMoreAhead = async (): Promise<void> => {
|
|
|
|
|
if (preventAppearFetchMore.value) return;
|
|
|
|
|
await fetchMoreAhead();
|
|
|
|
|
fetchMoreAppearTimeout();
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-07 14:42:09 +09:00
|
|
|
|
const isTop = (): boolean => isBackTop.value || (props.pagination.reversed ? isBottomVisible : isTopVisible)(contentEl.value!, TOLERANCE);
|
2023-02-25 08:18:12 +09:00
|
|
|
|
|
|
|
|
|
watch(visibility, () => {
|
|
|
|
|
if (visibility.value === 'hidden') {
|
|
|
|
|
timerForSetPause = window.setTimeout(() => {
|
|
|
|
|
isPausingUpdate = true;
|
|
|
|
|
timerForSetPause = null;
|
|
|
|
|
},
|
|
|
|
|
BACKGROUND_PAUSE_WAIT_SEC * 1000);
|
|
|
|
|
} else { // 'visible'
|
|
|
|
|
if (timerForSetPause) {
|
|
|
|
|
clearTimeout(timerForSetPause);
|
|
|
|
|
timerForSetPause = null;
|
|
|
|
|
} else {
|
|
|
|
|
isPausingUpdate = false;
|
|
|
|
|
if (isTop()) {
|
|
|
|
|
executeQueue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-07-05 00:59:37 +09:00
|
|
|
|
/**
|
|
|
|
|
* 最新のものとして1つだけアイテムを追加する
|
|
|
|
|
* ストリーミングから降ってきたアイテムはこれで追加する
|
|
|
|
|
* @param item アイテム
|
|
|
|
|
*/
|
2023-01-13 18:25:40 +09:00
|
|
|
|
const prepend = (item: MisskeyEntity): void => {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
if (items.value.size === 0) {
|
|
|
|
|
items.value.set(item.id, item);
|
|
|
|
|
fetching.value = false;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-01-21 16:43:56 +09:00
|
|
|
|
|
2023-02-25 08:18:12 +09:00
|
|
|
|
if (isTop() && !isPausingUpdate) unshiftItems([item]);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
else prependQueue(item);
|
|
|
|
|
};
|
2022-01-09 21:35:35 +09:00
|
|
|
|
|
2023-07-05 00:59:37 +09:00
|
|
|
|
/**
|
|
|
|
|
* 新着アイテムをitemsの先頭に追加し、displayLimitを適用する
|
|
|
|
|
* @param newItems 新しいアイテムの配列
|
|
|
|
|
*/
|
2023-01-13 18:25:40 +09:00
|
|
|
|
function unshiftItems(newItems: MisskeyEntity[]) {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
const length = newItems.length + items.value.size;
|
|
|
|
|
items.value = new Map([...arrayToEntries(newItems), ...items.value].slice(0, props.displayLimit));
|
|
|
|
|
|
|
|
|
|
if (length >= props.displayLimit) more.value = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 古いアイテムをitemsの末尾に追加し、displayLimitを適用する
|
|
|
|
|
* @param oldItems 古いアイテムの配列
|
|
|
|
|
*/
|
|
|
|
|
function concatItems(oldItems: MisskeyEntity[]) {
|
|
|
|
|
const length = oldItems.length + items.value.size;
|
|
|
|
|
items.value = new Map([...items.value, ...arrayToEntries(oldItems)].slice(0, props.displayLimit));
|
2023-01-13 18:25:40 +09:00
|
|
|
|
|
|
|
|
|
if (length >= props.displayLimit) more.value = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function executeQueue() {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
unshiftItems(Array.from(queue.value.values()));
|
|
|
|
|
queue.value = new Map();
|
2023-01-13 18:25:40 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prependQueue(newItem: MisskeyEntity) {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
queue.value = new Map([[newItem.id, newItem], ...queue.value].slice(0, props.displayLimit) as [string, MisskeyEntity][]);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
}
|
2022-01-09 21:35:35 +09:00
|
|
|
|
|
2023-07-05 00:59:37 +09:00
|
|
|
|
/*
|
|
|
|
|
* アイテムを末尾に追加する(使うの?)
|
|
|
|
|
*/
|
2023-01-13 18:25:40 +09:00
|
|
|
|
const appendItem = (item: MisskeyEntity): void => {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
items.value.set(item.id, item);
|
2022-01-09 21:35:35 +09:00
|
|
|
|
};
|
|
|
|
|
|
2023-07-05 00:59:37 +09:00
|
|
|
|
const removeItem = (id: string) => {
|
|
|
|
|
items.value.delete(id);
|
|
|
|
|
queue.value.delete(id);
|
2022-05-15 22:20:01 +09:00
|
|
|
|
};
|
|
|
|
|
|
2023-01-13 18:25:40 +09:00
|
|
|
|
const updateItem = (id: MisskeyEntity['id'], replacer: (old: MisskeyEntity) => MisskeyEntity): void => {
|
2023-07-05 00:59:37 +09:00
|
|
|
|
const item = items.value.get(id);
|
|
|
|
|
if (item) items.value.set(id, replacer(item));
|
|
|
|
|
|
|
|
|
|
const queueItem = queue.value.get(id);
|
|
|
|
|
if (queueItem) queue.value.set(id, replacer(queueItem));
|
2022-01-10 00:45:20 +09:00
|
|
|
|
};
|
|
|
|
|
|
2022-01-09 21:35:35 +09:00
|
|
|
|
onActivated(() => {
|
|
|
|
|
isBackTop.value = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onDeactivated(() => {
|
2023-12-07 14:42:09 +09:00
|
|
|
|
isBackTop.value = props.pagination.reversed ? window.scrollY >= (rootEl.value ? rootEl.value.scrollHeight - window.innerHeight : 0) : window.scrollY === 0;
|
2023-01-13 18:25:40 +09:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function toBottom() {
|
2023-12-07 14:42:09 +09:00
|
|
|
|
scrollToBottom(contentEl.value!);
|
2023-01-13 18:25:40 +09:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 11:24:54 +09:00
|
|
|
|
onBeforeMount(() => {
|
|
|
|
|
init().then(() => {
|
2023-01-13 18:25:40 +09:00
|
|
|
|
if (props.pagination.reversed) {
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
setTimeout(toBottom, 800);
|
|
|
|
|
|
|
|
|
|
// scrollToBottomでmoreFetchingボタンが画面外まで出るまで
|
|
|
|
|
// more = trueを遅らせる
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
moreFetching.value = false;
|
|
|
|
|
}, 2000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
2023-02-25 08:18:12 +09:00
|
|
|
|
if (timerForSetPause) {
|
|
|
|
|
clearTimeout(timerForSetPause);
|
|
|
|
|
timerForSetPause = null;
|
|
|
|
|
}
|
2023-07-05 00:59:37 +09:00
|
|
|
|
if (preventAppearFetchMoreTimer.value) {
|
|
|
|
|
clearTimeout(preventAppearFetchMoreTimer.value);
|
|
|
|
|
preventAppearFetchMoreTimer.value = null;
|
|
|
|
|
}
|
2023-12-07 14:42:09 +09:00
|
|
|
|
scrollObserver.value?.disconnect();
|
2022-01-09 21:35:35 +09:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
defineExpose({
|
2022-01-10 20:17:38 +09:00
|
|
|
|
items,
|
2022-04-30 21:52:07 +09:00
|
|
|
|
queue,
|
2023-12-07 14:42:09 +09:00
|
|
|
|
backed: backed.value,
|
2023-01-13 18:25:40 +09:00
|
|
|
|
more,
|
2022-01-09 21:35:35 +09:00
|
|
|
|
reload,
|
|
|
|
|
prepend,
|
2023-01-13 18:25:40 +09:00
|
|
|
|
append: appendItem,
|
2022-05-15 22:20:01 +09:00
|
|
|
|
removeItem,
|
2022-01-10 00:45:20 +09:00
|
|
|
|
updateItem,
|
2020-01-30 04:37:25 +09:00
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
2023-01-14 15:01:28 +09:00
|
|
|
|
<style lang="scss" module>
|
|
|
|
|
.transition_fade_enterActive,
|
|
|
|
|
.transition_fade_leaveActive {
|
2021-04-22 22:29:33 +09:00
|
|
|
|
transition: opacity 0.125s ease;
|
|
|
|
|
}
|
2023-01-14 15:01:28 +09:00
|
|
|
|
.transition_fade_enterFrom,
|
|
|
|
|
.transition_fade_leaveTo {
|
2021-04-22 22:29:33 +09:00
|
|
|
|
opacity: 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-14 15:01:28 +09:00
|
|
|
|
.more {
|
|
|
|
|
margin-left: auto;
|
|
|
|
|
margin-right: auto;
|
2020-01-30 04:37:25 +09:00
|
|
|
|
}
|
|
|
|
|
</style>
|