2020-01-29 11:37:25 -08:00
|
|
|
<template>
|
2022-01-25 06:18:21 -08:00
|
|
|
<transition :name="$store.state.animation ? 'fade' : ''" mode="out-in">
|
2021-04-22 06:29:33 -07:00
|
|
|
<MkLoading v-if="fetching"/>
|
|
|
|
|
|
|
|
<MkError v-else-if="error" @retry="init()"/>
|
|
|
|
|
2021-11-19 02:36:12 -08:00
|
|
|
<div v-else-if="empty" key="_empty_" class="empty">
|
2021-12-22 23:10:13 -08:00
|
|
|
<slot name="empty">
|
|
|
|
<div class="_fullinfo">
|
|
|
|
<img src="https://xn--931a.moe/assets/info.jpg" class="_ghost"/>
|
|
|
|
<div>{{ $ts.nothing }}</div>
|
|
|
|
</div>
|
|
|
|
</slot>
|
2020-01-29 11:37:25 -08:00
|
|
|
</div>
|
2021-04-22 06:29:33 -07:00
|
|
|
|
2022-01-09 04:35:35 -08:00
|
|
|
<div v-else ref="rootEl">
|
2021-04-22 06:29:33 -07:00
|
|
|
<slot :items="items"></slot>
|
2022-01-09 04:35:35 -08:00
|
|
|
<div v-show="more" key="_more_" class="cxiknjgy _gap">
|
|
|
|
<MkButton v-if="!moreFetching" v-appear="($store.state.enableInfiniteScroll && !disableAutoLoad) ? fetchMore : null" class="button" :disabled="moreFetching" :style="{ cursor: moreFetching ? 'wait' : 'pointer' }" primary @click="fetchMore">
|
|
|
|
{{ $ts.loadMore }}
|
2021-04-22 06:29:33 -07:00
|
|
|
</MkButton>
|
2022-01-09 04:35:35 -08:00
|
|
|
<MkLoading v-else class="loading"/>
|
2021-04-22 06:29:33 -07:00
|
|
|
</div>
|
2020-01-29 11:37:25 -08:00
|
|
|
</div>
|
2021-04-22 06:29:33 -07:00
|
|
|
</transition>
|
2020-01-29 11:37:25 -08:00
|
|
|
</template>
|
|
|
|
|
2022-01-09 04:35:35 -08:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { computed, ComputedRef, isRef, markRaw, onActivated, onDeactivated, Ref, ref, watch } from 'vue';
|
|
|
|
import * as misskey from 'misskey-js';
|
|
|
|
import * as os from '@/os';
|
|
|
|
import { onScrollTop, isTopVisible, getScrollPosition, getScrollContainer } from '@/scripts/scroll';
|
|
|
|
import MkButton from '@/components/ui/button.vue';
|
|
|
|
|
|
|
|
const SECOND_FETCH_LIMIT = 30;
|
|
|
|
|
|
|
|
export type Paging<E extends keyof misskey.Endpoints = keyof misskey.Endpoints> = {
|
|
|
|
endpoint: E;
|
|
|
|
limit: number;
|
|
|
|
params?: misskey.Endpoints[E]['req'] | ComputedRef<misskey.Endpoints[E]['req']>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 検索APIのような、ページング不可なエンドポイントを利用する場合
|
|
|
|
* (そのようなAPIをこの関数で使うのは若干矛盾してるけど)
|
|
|
|
*/
|
|
|
|
noPaging?: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* items 配列の中身を逆順にする(新しい方が最後)
|
|
|
|
*/
|
|
|
|
reversed?: boolean;
|
2022-01-09 08:00:50 -08:00
|
|
|
|
|
|
|
offsetMode?: boolean;
|
2022-01-09 04:35:35 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
pagination: Paging;
|
|
|
|
disableAutoLoad?: boolean;
|
|
|
|
displayLimit?: number;
|
|
|
|
}>(), {
|
|
|
|
displayLimit: 30,
|
|
|
|
});
|
|
|
|
|
2022-01-09 05:57:27 -08:00
|
|
|
const emit = defineEmits<{
|
|
|
|
(e: 'queue', count: number): void;
|
|
|
|
}>();
|
|
|
|
|
2022-01-09 08:00:50 -08:00
|
|
|
type Item = { id: string; [another: string]: unknown; };
|
|
|
|
|
2022-01-09 04:35:35 -08:00
|
|
|
const rootEl = ref<HTMLElement>();
|
2022-01-09 08:00:50 -08:00
|
|
|
const items = ref<Item[]>([]);
|
|
|
|
const queue = ref<Item[]>([]);
|
2022-01-09 04:35:35 -08:00
|
|
|
const offset = ref(0);
|
|
|
|
const fetching = ref(true);
|
|
|
|
const moreFetching = ref(false);
|
|
|
|
const more = ref(false);
|
|
|
|
const backed = ref(false); // 遡り中か否か
|
|
|
|
const isBackTop = ref(false);
|
2022-01-20 23:43:56 -08:00
|
|
|
const empty = computed(() => items.value.length === 0);
|
|
|
|
const error = ref(false);
|
2022-01-09 04:35:35 -08:00
|
|
|
|
2022-01-09 08:00:50 -08:00
|
|
|
const init = async (): Promise<void> => {
|
2022-01-09 04:35:35 -08:00
|
|
|
queue.value = [];
|
|
|
|
fetching.value = true;
|
|
|
|
const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {};
|
|
|
|
await os.api(props.pagination.endpoint, {
|
|
|
|
...params,
|
|
|
|
limit: props.pagination.noPaging ? (props.pagination.limit || 10) : (props.pagination.limit || 10) + 1,
|
|
|
|
}).then(res => {
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
const item = res[i];
|
|
|
|
if (props.pagination.reversed) {
|
|
|
|
if (i === res.length - 2) item._shouldInsertAd_ = true;
|
|
|
|
} else {
|
|
|
|
if (i === 3) item._shouldInsertAd_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!props.pagination.noPaging && (res.length > (props.pagination.limit || 10))) {
|
|
|
|
res.pop();
|
|
|
|
items.value = props.pagination.reversed ? [...res].reverse() : res;
|
|
|
|
more.value = true;
|
|
|
|
} else {
|
|
|
|
items.value = props.pagination.reversed ? [...res].reverse() : res;
|
|
|
|
more.value = false;
|
|
|
|
}
|
|
|
|
offset.value = res.length;
|
2022-01-20 23:43:56 -08:00
|
|
|
error.value = false;
|
2022-01-09 04:35:35 -08:00
|
|
|
fetching.value = false;
|
|
|
|
}, e => {
|
2022-01-20 23:43:56 -08:00
|
|
|
error.value = true;
|
2022-01-09 04:35:35 -08:00
|
|
|
fetching.value = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-09 08:00:50 -08:00
|
|
|
const reload = (): void => {
|
2022-01-09 04:35:35 -08:00
|
|
|
items.value = [];
|
|
|
|
init();
|
|
|
|
};
|
|
|
|
|
2022-01-09 08:00:50 -08:00
|
|
|
const fetchMore = async (): Promise<void> => {
|
2022-01-09 04:35:35 -08:00
|
|
|
if (!more.value || fetching.value || moreFetching.value || items.value.length === 0) return;
|
|
|
|
moreFetching.value = true;
|
|
|
|
backed.value = true;
|
|
|
|
const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {};
|
|
|
|
await os.api(props.pagination.endpoint, {
|
|
|
|
...params,
|
|
|
|
limit: SECOND_FETCH_LIMIT + 1,
|
|
|
|
...(props.pagination.offsetMode ? {
|
|
|
|
offset: offset.value,
|
|
|
|
} : {
|
|
|
|
untilId: props.pagination.reversed ? items.value[0].id : items.value[items.value.length - 1].id,
|
|
|
|
}),
|
|
|
|
}).then(res => {
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
const item = res[i];
|
|
|
|
if (props.pagination.reversed) {
|
|
|
|
if (i === res.length - 9) item._shouldInsertAd_ = true;
|
|
|
|
} else {
|
|
|
|
if (i === 10) item._shouldInsertAd_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (res.length > SECOND_FETCH_LIMIT) {
|
|
|
|
res.pop();
|
|
|
|
items.value = props.pagination.reversed ? [...res].reverse().concat(items.value) : items.value.concat(res);
|
|
|
|
more.value = true;
|
|
|
|
} else {
|
|
|
|
items.value = props.pagination.reversed ? [...res].reverse().concat(items.value) : items.value.concat(res);
|
|
|
|
more.value = false;
|
|
|
|
}
|
|
|
|
offset.value += res.length;
|
|
|
|
moreFetching.value = false;
|
|
|
|
}, e => {
|
|
|
|
moreFetching.value = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-09 08:00:50 -08:00
|
|
|
const fetchMoreAhead = async (): Promise<void> => {
|
2022-01-09 04:35:35 -08:00
|
|
|
if (!more.value || fetching.value || moreFetching.value || items.value.length === 0) return;
|
|
|
|
moreFetching.value = true;
|
|
|
|
const params = props.pagination.params ? isRef(props.pagination.params) ? props.pagination.params.value : props.pagination.params : {};
|
|
|
|
await os.api(props.pagination.endpoint, {
|
|
|
|
...params,
|
|
|
|
limit: SECOND_FETCH_LIMIT + 1,
|
|
|
|
...(props.pagination.offsetMode ? {
|
|
|
|
offset: offset.value,
|
|
|
|
} : {
|
|
|
|
sinceId: props.pagination.reversed ? items.value[0].id : items.value[items.value.length - 1].id,
|
|
|
|
}),
|
|
|
|
}).then(res => {
|
|
|
|
if (res.length > SECOND_FETCH_LIMIT) {
|
|
|
|
res.pop();
|
|
|
|
items.value = props.pagination.reversed ? [...res].reverse().concat(items.value) : items.value.concat(res);
|
|
|
|
more.value = true;
|
|
|
|
} else {
|
|
|
|
items.value = props.pagination.reversed ? [...res].reverse().concat(items.value) : items.value.concat(res);
|
|
|
|
more.value = false;
|
|
|
|
}
|
|
|
|
offset.value += res.length;
|
|
|
|
moreFetching.value = false;
|
|
|
|
}, e => {
|
|
|
|
moreFetching.value = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-09 08:00:50 -08:00
|
|
|
const prepend = (item: Item): void => {
|
2022-01-09 04:35:35 -08:00
|
|
|
if (props.pagination.reversed) {
|
2022-01-20 23:43:56 -08:00
|
|
|
if (rootEl.value) {
|
|
|
|
const container = getScrollContainer(rootEl.value);
|
|
|
|
if (container == null) return; // TODO?
|
|
|
|
|
|
|
|
const pos = getScrollPosition(rootEl.value);
|
|
|
|
const viewHeight = container.clientHeight;
|
|
|
|
const height = container.scrollHeight;
|
|
|
|
const isBottom = (pos + viewHeight > height - 32);
|
|
|
|
if (isBottom) {
|
|
|
|
// オーバーフローしたら古いアイテムは捨てる
|
|
|
|
if (items.value.length >= props.displayLimit) {
|
|
|
|
// このやり方だとVue 3.2以降アニメーションが動かなくなる
|
|
|
|
//items.value = items.value.slice(-props.displayLimit);
|
|
|
|
while (items.value.length >= props.displayLimit) {
|
|
|
|
items.value.shift();
|
|
|
|
}
|
|
|
|
more.value = true;
|
2022-01-09 04:35:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items.value.push(item);
|
|
|
|
// TODO
|
|
|
|
} else {
|
2022-01-20 23:43:56 -08:00
|
|
|
// 初回表示時はunshiftだけでOK
|
|
|
|
if (!rootEl.value) {
|
|
|
|
items.value.unshift(item);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-09 04:35:35 -08:00
|
|
|
const isTop = isBackTop.value || (document.body.contains(rootEl.value) && isTopVisible(rootEl.value));
|
|
|
|
|
|
|
|
if (isTop) {
|
|
|
|
// Prepend the item
|
|
|
|
items.value.unshift(item);
|
|
|
|
|
|
|
|
// オーバーフローしたら古いアイテムは捨てる
|
|
|
|
if (items.value.length >= props.displayLimit) {
|
|
|
|
// このやり方だとVue 3.2以降アニメーションが動かなくなる
|
|
|
|
//this.items = items.value.slice(0, props.displayLimit);
|
|
|
|
while (items.value.length >= props.displayLimit) {
|
|
|
|
items.value.pop();
|
|
|
|
}
|
|
|
|
more.value = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
queue.value.push(item);
|
|
|
|
onScrollTop(rootEl.value, () => {
|
|
|
|
for (const item of queue.value) {
|
|
|
|
prepend(item);
|
|
|
|
}
|
|
|
|
queue.value = [];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-09 08:00:50 -08:00
|
|
|
const append = (item: Item): void => {
|
2022-01-09 04:35:35 -08:00
|
|
|
items.value.push(item);
|
|
|
|
};
|
|
|
|
|
2022-01-09 08:00:50 -08:00
|
|
|
const updateItem = (id: Item['id'], replacer: (old: Item) => Item): void => {
|
2022-01-09 07:45:20 -08:00
|
|
|
const i = items.value.findIndex(item => item.id === id);
|
|
|
|
items.value[i] = replacer(items.value[i]);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (props.pagination.params && isRef(props.pagination.params)) {
|
|
|
|
watch(props.pagination.params, init, { deep: true });
|
|
|
|
}
|
|
|
|
|
2022-01-09 04:35:35 -08:00
|
|
|
watch(queue, (a, b) => {
|
|
|
|
if (a.length === 0 && b.length === 0) return;
|
2022-01-09 05:57:27 -08:00
|
|
|
emit('queue', queue.value.length);
|
2022-01-09 04:35:35 -08:00
|
|
|
}, { deep: true });
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
onActivated(() => {
|
|
|
|
isBackTop.value = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
onDeactivated(() => {
|
|
|
|
isBackTop.value = window.scrollY === 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose({
|
2022-01-10 03:17:38 -08:00
|
|
|
items,
|
2022-04-30 05:52:07 -07:00
|
|
|
queue,
|
2022-01-20 23:43:56 -08:00
|
|
|
backed,
|
2022-01-09 04:35:35 -08:00
|
|
|
reload,
|
|
|
|
fetchMoreAhead,
|
|
|
|
prepend,
|
|
|
|
append,
|
2022-01-09 07:45:20 -08:00
|
|
|
updateItem,
|
2020-01-29 11:37:25 -08:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-04-22 06:29:33 -07:00
|
|
|
.fade-enter-active,
|
|
|
|
.fade-leave-active {
|
|
|
|
transition: opacity 0.125s ease;
|
|
|
|
}
|
|
|
|
.fade-enter-from,
|
|
|
|
.fade-leave-to {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:37:25 -08:00
|
|
|
.cxiknjgy {
|
2022-01-09 04:35:35 -08:00
|
|
|
> .button {
|
2020-02-11 09:52:37 -08:00
|
|
|
margin-left: auto;
|
|
|
|
margin-right: auto;
|
|
|
|
}
|
2020-01-29 11:37:25 -08:00
|
|
|
}
|
|
|
|
</style>
|