2020-01-29 19:37:25 +00:00
|
|
|
<template>
|
2023-01-14 06:01:28 +00:00
|
|
|
<Transition
|
|
|
|
:enter-active-class="$store.state.animation ? $style.transition_fade_enterActive : ''"
|
|
|
|
:leave-active-class="$store.state.animation ? $style.transition_fade_leaveActive : ''"
|
|
|
|
:enter-from-class="$store.state.animation ? $style.transition_fade_enterFrom : ''"
|
|
|
|
:leave-to-class="$store.state.animation ? $style.transition_fade_leaveTo : ''"
|
|
|
|
mode="out-in"
|
|
|
|
>
|
2021-04-22 13:29:33 +00:00
|
|
|
<MkLoading v-if="fetching"/>
|
|
|
|
|
|
|
|
<MkError v-else-if="error" @retry="init()"/>
|
|
|
|
|
2021-11-19 10:36:12 +00:00
|
|
|
<div v-else-if="empty" key="_empty_" class="empty">
|
2021-12-23 07:10:13 +00:00
|
|
|
<slot name="empty">
|
|
|
|
<div class="_fullinfo">
|
|
|
|
<img src="https://xn--931a.moe/assets/info.jpg" class="_ghost"/>
|
2022-07-20 13:24:26 +00:00
|
|
|
<div>{{ i18n.ts.nothing }}</div>
|
2021-12-23 07:10:13 +00:00
|
|
|
</div>
|
|
|
|
</slot>
|
2020-01-29 19:37:25 +00:00
|
|
|
</div>
|
2021-04-22 13:29:33 +00:00
|
|
|
|
2022-01-09 12:35:35 +00:00
|
|
|
<div v-else ref="rootEl">
|
2023-01-14 06:01:28 +00:00
|
|
|
<div v-show="pagination.reversed && more" key="_more_" class="_margin">
|
|
|
|
<MkButton v-if="!moreFetching" v-appear="(enableInfiniteScroll && !props.disableAutoLoad) ? fetchMore : null" :class="$style.more" :disabled="moreFetching" :style="{ cursor: moreFetching ? 'wait' : 'pointer' }" primary @click="fetchMore">
|
2022-07-20 13:24:26 +00:00
|
|
|
{{ i18n.ts.loadMore }}
|
2022-05-19 11:32:55 +00:00
|
|
|
</MkButton>
|
|
|
|
<MkLoading v-else class="loading"/>
|
|
|
|
</div>
|
2023-01-13 09:25:40 +00:00
|
|
|
<slot :items="items" :fetching="fetching || moreFetching"></slot>
|
2023-01-14 06:01:28 +00:00
|
|
|
<div v-show="!pagination.reversed && more" key="_more_" class="_margin">
|
|
|
|
<MkButton v-if="!moreFetching" v-appear="(enableInfiniteScroll && !props.disableAutoLoad) ? fetchMore : null" :class="$style.more" :disabled="moreFetching" :style="{ cursor: moreFetching ? 'wait' : 'pointer' }" primary @click="fetchMore">
|
2022-07-20 13:24:26 +00:00
|
|
|
{{ i18n.ts.loadMore }}
|
2021-04-22 13:29:33 +00:00
|
|
|
</MkButton>
|
2022-01-09 12:35:35 +00:00
|
|
|
<MkLoading v-else class="loading"/>
|
2021-04-22 13:29:33 +00:00
|
|
|
</div>
|
2020-01-29 19:37:25 +00:00
|
|
|
</div>
|
2022-12-30 04:37:14 +00:00
|
|
|
</Transition>
|
2020-01-29 19:37:25 +00:00
|
|
|
</template>
|
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
<script lang="ts">
|
|
|
|
import { computed, ComputedRef, isRef, nextTick, onActivated, onBeforeUnmount, onDeactivated, onMounted, ref, watch } from 'vue';
|
2022-01-09 12:35:35 +00:00
|
|
|
import * as misskey from 'misskey-js';
|
|
|
|
import * as os from '@/os';
|
2023-01-13 09:25:40 +00:00
|
|
|
import { onScrollTop, isTopVisible, getBodyScrollHeight, getScrollContainer, onScrollBottom, scrollToBottom, scroll, isBottomVisible } from '@/scripts/scroll';
|
2022-09-06 09:21:49 +00:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-01-13 09:25:40 +00:00
|
|
|
import { defaultStore } from '@/store';
|
|
|
|
import { MisskeyEntity } from '@/types/date-separated-list';
|
2022-07-20 13:24:26 +00:00
|
|
|
import { i18n } from '@/i18n';
|
2022-01-09 12:35:35 +00:00
|
|
|
|
|
|
|
const SECOND_FETCH_LIMIT = 30;
|
2023-01-13 09:25:40 +00:00
|
|
|
const TOLERANCE = 16;
|
2022-01-09 12:35:35 +00:00
|
|
|
|
|
|
|
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 16:00:50 +00:00
|
|
|
|
|
|
|
offsetMode?: boolean;
|
2022-01-09 12:35:35 +00:00
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
pageEl?: HTMLElement;
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
<script lang="ts" setup>
|
2022-01-09 12:35:35 +00:00
|
|
|
const props = withDefaults(defineProps<{
|
|
|
|
pagination: Paging;
|
|
|
|
disableAutoLoad?: boolean;
|
|
|
|
displayLimit?: number;
|
|
|
|
}>(), {
|
2023-01-03 00:41:32 +00:00
|
|
|
displayLimit: 20,
|
2022-01-09 12:35:35 +00:00
|
|
|
});
|
|
|
|
|
2022-01-09 13:57:27 +00:00
|
|
|
const emit = defineEmits<{
|
2022-05-26 13:53:09 +00:00
|
|
|
(ev: 'queue', count: number): void;
|
2022-01-09 13:57:27 +00:00
|
|
|
}>();
|
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
let rootEl = $shallowRef<HTMLElement>();
|
|
|
|
|
|
|
|
// 遡り中かどうか
|
|
|
|
let backed = $ref(false);
|
2022-01-09 16:00:50 +00:00
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
let scrollRemove = $ref<(() => void) | null>(null);
|
|
|
|
|
|
|
|
const items = ref<MisskeyEntity[]>([]);
|
|
|
|
const queue = ref<MisskeyEntity[]>([]);
|
2022-01-09 12:35:35 +00:00
|
|
|
const offset = ref(0);
|
|
|
|
const fetching = ref(true);
|
|
|
|
const moreFetching = ref(false);
|
|
|
|
const more = ref(false);
|
|
|
|
const isBackTop = ref(false);
|
2022-01-21 07:43:56 +00:00
|
|
|
const empty = computed(() => items.value.length === 0);
|
|
|
|
const error = ref(false);
|
2023-01-13 09:25:40 +00:00
|
|
|
const {
|
2023-01-14 06:01:28 +00:00
|
|
|
enableInfiniteScroll,
|
2023-01-13 09:25:40 +00:00
|
|
|
} = defaultStore.reactiveState;
|
|
|
|
|
|
|
|
const contentEl = $computed(() => props.pagination.pageEl || rootEl);
|
|
|
|
const scrollableElement = $computed(() => getScrollContainer(contentEl));
|
|
|
|
|
|
|
|
// 先頭が表示されているかどうかを検出
|
|
|
|
// https://qiita.com/mkataigi/items/0154aefd2223ce23398e
|
|
|
|
let scrollObserver = $ref<IntersectionObserver>();
|
|
|
|
|
|
|
|
watch([() => props.pagination.reversed, $$(scrollableElement)], () => {
|
|
|
|
if (scrollObserver) scrollObserver.disconnect();
|
|
|
|
|
|
|
|
scrollObserver = new IntersectionObserver(entries => {
|
|
|
|
backed = entries[0].isIntersecting;
|
|
|
|
}, {
|
|
|
|
root: scrollableElement,
|
|
|
|
rootMargin: props.pagination.reversed ? '-100% 0px 100% 0px' : '100% 0px -100% 0px',
|
|
|
|
threshold: 0.01,
|
|
|
|
});
|
|
|
|
}, { immediate: true });
|
|
|
|
|
|
|
|
watch($$(rootEl), () => {
|
|
|
|
scrollObserver.disconnect();
|
|
|
|
nextTick(() => {
|
|
|
|
if (rootEl) scrollObserver.observe(rootEl);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
watch([$$(backed), $$(contentEl)], () => {
|
|
|
|
if (!backed) {
|
|
|
|
if (!contentEl) return;
|
|
|
|
|
|
|
|
scrollRemove = (props.pagination.reversed ? onScrollBottom : onScrollTop)(contentEl, executeQueue, TOLERANCE);
|
|
|
|
} else {
|
|
|
|
if (scrollRemove) scrollRemove();
|
|
|
|
scrollRemove = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (props.pagination.params && isRef(props.pagination.params)) {
|
|
|
|
watch(props.pagination.params, init, { deep: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
watch(queue, (a, b) => {
|
|
|
|
if (a.length === 0 && b.length === 0) return;
|
|
|
|
emit('queue', queue.value.length);
|
|
|
|
}, { deep: true });
|
2022-01-09 12:35:35 +00:00
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
async function init(): Promise<void> {
|
2022-01-09 12:35:35 +00: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];
|
2023-01-13 09:25:40 +00:00
|
|
|
if (i === 3) item._shouldInsertAd_ = true;
|
2022-01-09 12:35:35 +00:00
|
|
|
}
|
|
|
|
if (!props.pagination.noPaging && (res.length > (props.pagination.limit || 10))) {
|
|
|
|
res.pop();
|
2023-01-13 09:25:40 +00:00
|
|
|
if (props.pagination.reversed) moreFetching.value = true;
|
|
|
|
items.value = res;
|
2022-01-09 12:35:35 +00:00
|
|
|
more.value = true;
|
|
|
|
} else {
|
2023-01-13 09:25:40 +00:00
|
|
|
items.value = res;
|
2022-01-09 12:35:35 +00:00
|
|
|
more.value = false;
|
|
|
|
}
|
|
|
|
offset.value = res.length;
|
2022-01-21 07:43:56 +00:00
|
|
|
error.value = false;
|
2022-01-09 12:35:35 +00:00
|
|
|
fetching.value = false;
|
2022-05-26 13:53:09 +00:00
|
|
|
}, err => {
|
2022-01-21 07:43:56 +00:00
|
|
|
error.value = true;
|
2022-01-09 12:35:35 +00:00
|
|
|
fetching.value = false;
|
|
|
|
});
|
2023-01-13 09:25:40 +00:00
|
|
|
}
|
2022-01-09 12:35:35 +00:00
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
const reload = (): Promise<void> => {
|
2022-01-09 12:35:35 +00:00
|
|
|
items.value = [];
|
2023-01-13 09:25:40 +00:00
|
|
|
return init();
|
2022-01-09 12:35:35 +00:00
|
|
|
};
|
|
|
|
|
2022-01-09 16:00:50 +00:00
|
|
|
const fetchMore = async (): Promise<void> => {
|
2022-01-09 12:35:35 +00: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,
|
|
|
|
} : {
|
2022-07-05 08:42:54 +00:00
|
|
|
untilId: items.value[items.value.length - 1].id,
|
2022-01-09 12:35:35 +00:00
|
|
|
}),
|
|
|
|
}).then(res => {
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
const item = res[i];
|
2023-01-13 09:25:40 +00:00
|
|
|
if (i === 10) item._shouldInsertAd_ = true;
|
2022-01-09 12:35:35 +00:00
|
|
|
}
|
2023-01-13 09:25:40 +00:00
|
|
|
|
|
|
|
const reverseConcat = _res => {
|
|
|
|
const oldHeight = scrollableElement ? scrollableElement.scrollHeight : getBodyScrollHeight();
|
|
|
|
const oldScroll = scrollableElement ? scrollableElement.scrollTop : window.scrollY;
|
|
|
|
|
|
|
|
items.value = items.value.concat(_res);
|
|
|
|
|
|
|
|
return nextTick(() => {
|
|
|
|
if (scrollableElement) {
|
|
|
|
scroll(scrollableElement, { top: oldScroll + (scrollableElement.scrollHeight - oldHeight), behavior: 'instant' });
|
|
|
|
} else {
|
|
|
|
window.scroll({ top: oldScroll + (getBodyScrollHeight() - oldHeight), behavior: 'instant' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextTick();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-09 12:35:35 +00:00
|
|
|
if (res.length > SECOND_FETCH_LIMIT) {
|
|
|
|
res.pop();
|
2023-01-13 09:25:40 +00:00
|
|
|
|
|
|
|
if (props.pagination.reversed) {
|
|
|
|
reverseConcat(res).then(() => {
|
|
|
|
more.value = true;
|
|
|
|
moreFetching.value = false;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
items.value = items.value.concat(res);
|
|
|
|
more.value = true;
|
|
|
|
moreFetching.value = false;
|
|
|
|
}
|
2022-01-09 12:35:35 +00:00
|
|
|
} else {
|
2023-01-13 09:25:40 +00:00
|
|
|
if (props.pagination.reversed) {
|
|
|
|
reverseConcat(res).then(() => {
|
|
|
|
more.value = false;
|
|
|
|
moreFetching.value = false;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
items.value = items.value.concat(res);
|
|
|
|
more.value = false;
|
|
|
|
moreFetching.value = false;
|
|
|
|
}
|
2022-01-09 12:35:35 +00:00
|
|
|
}
|
|
|
|
offset.value += res.length;
|
2022-05-26 13:53:09 +00:00
|
|
|
}, err => {
|
2022-01-09 12:35:35 +00:00
|
|
|
moreFetching.value = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-09 16:00:50 +00:00
|
|
|
const fetchMoreAhead = async (): Promise<void> => {
|
2022-01-09 12:35:35 +00: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,
|
|
|
|
} : {
|
2022-07-05 08:42:54 +00:00
|
|
|
sinceId: items.value[items.value.length - 1].id,
|
2022-01-09 12:35:35 +00:00
|
|
|
}),
|
|
|
|
}).then(res => {
|
|
|
|
if (res.length > SECOND_FETCH_LIMIT) {
|
|
|
|
res.pop();
|
2023-01-13 09:25:40 +00:00
|
|
|
items.value = items.value.concat(res);
|
2022-01-09 12:35:35 +00:00
|
|
|
more.value = true;
|
|
|
|
} else {
|
2023-01-13 09:25:40 +00:00
|
|
|
items.value = items.value.concat(res);
|
2022-01-09 12:35:35 +00:00
|
|
|
more.value = false;
|
|
|
|
}
|
|
|
|
offset.value += res.length;
|
|
|
|
moreFetching.value = false;
|
2022-05-26 13:53:09 +00:00
|
|
|
}, err => {
|
2022-01-09 12:35:35 +00:00
|
|
|
moreFetching.value = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
const prepend = (item: MisskeyEntity): void => {
|
|
|
|
// 初回表示時はunshiftだけでOK
|
|
|
|
if (!rootEl) {
|
|
|
|
items.value.unshift(item);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-21 07:43:56 +00:00
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
const isTop = isBackTop.value || (props.pagination.reversed ? isBottomVisible : isTopVisible)(contentEl, TOLERANCE);
|
2022-01-09 12:35:35 +00:00
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
if (isTop) unshiftItems([item]);
|
|
|
|
else prependQueue(item);
|
|
|
|
};
|
2022-01-09 12:35:35 +00:00
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
function unshiftItems(newItems: MisskeyEntity[]) {
|
|
|
|
const length = newItems.length + items.value.length;
|
2023-01-14 06:01:28 +00:00
|
|
|
items.value = [...newItems, ...items.value].slice(0, props.displayLimit);
|
2023-01-13 09:25:40 +00:00
|
|
|
|
|
|
|
if (length >= props.displayLimit) more.value = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function executeQueue() {
|
|
|
|
if (queue.value.length === 0) return;
|
|
|
|
unshiftItems(queue.value);
|
|
|
|
queue.value = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function prependQueue(newItem: MisskeyEntity) {
|
|
|
|
queue.value.unshift(newItem);
|
|
|
|
if (queue.value.length >= props.displayLimit) {
|
|
|
|
queue.value.pop();
|
2022-01-09 12:35:35 +00:00
|
|
|
}
|
2023-01-13 09:25:40 +00:00
|
|
|
}
|
2022-01-09 12:35:35 +00:00
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
const appendItem = (item: MisskeyEntity): void => {
|
2022-01-09 12:35:35 +00:00
|
|
|
items.value.push(item);
|
|
|
|
};
|
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
const removeItem = (finder: (item: MisskeyEntity) => boolean) => {
|
2022-05-15 13:20:01 +00:00
|
|
|
const i = items.value.findIndex(finder);
|
|
|
|
items.value.splice(i, 1);
|
|
|
|
};
|
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
const updateItem = (id: MisskeyEntity['id'], replacer: (old: MisskeyEntity) => MisskeyEntity): void => {
|
2022-01-09 15:45:20 +00:00
|
|
|
const i = items.value.findIndex(item => item.id === id);
|
|
|
|
items.value[i] = replacer(items.value[i]);
|
|
|
|
};
|
|
|
|
|
2023-01-13 09:25:40 +00:00
|
|
|
const inited = init();
|
2022-01-09 12:35:35 +00:00
|
|
|
|
|
|
|
onActivated(() => {
|
|
|
|
isBackTop.value = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
onDeactivated(() => {
|
2023-01-14 06:01:28 +00:00
|
|
|
isBackTop.value = props.pagination.reversed ? window.scrollY >= (rootEl ? rootEl.scrollHeight - window.innerHeight : 0) : window.scrollY === 0;
|
2023-01-13 09:25:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function toBottom() {
|
|
|
|
scrollToBottom(contentEl);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
inited.then(() => {
|
|
|
|
if (props.pagination.reversed) {
|
|
|
|
nextTick(() => {
|
|
|
|
setTimeout(toBottom, 800);
|
|
|
|
|
|
|
|
// scrollToBottomでmoreFetchingボタンが画面外まで出るまで
|
|
|
|
// more = trueを遅らせる
|
|
|
|
setTimeout(() => {
|
|
|
|
moreFetching.value = false;
|
|
|
|
}, 2000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
scrollObserver.disconnect();
|
2022-01-09 12:35:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose({
|
2022-01-10 11:17:38 +00:00
|
|
|
items,
|
2022-04-30 12:52:07 +00:00
|
|
|
queue,
|
2022-01-21 07:43:56 +00:00
|
|
|
backed,
|
2023-01-13 09:25:40 +00:00
|
|
|
more,
|
|
|
|
inited,
|
2022-01-09 12:35:35 +00:00
|
|
|
reload,
|
|
|
|
prepend,
|
2023-01-13 09:25:40 +00:00
|
|
|
append: appendItem,
|
2022-05-15 13:20:01 +00:00
|
|
|
removeItem,
|
2022-01-09 15:45:20 +00:00
|
|
|
updateItem,
|
2020-01-29 19:37:25 +00:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-01-14 06:01:28 +00:00
|
|
|
<style lang="scss" module>
|
|
|
|
.transition_fade_enterActive,
|
|
|
|
.transition_fade_leaveActive {
|
2021-04-22 13:29:33 +00:00
|
|
|
transition: opacity 0.125s ease;
|
|
|
|
}
|
2023-01-14 06:01:28 +00:00
|
|
|
.transition_fade_enterFrom,
|
|
|
|
.transition_fade_leaveTo {
|
2021-04-22 13:29:33 +00:00
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
2023-01-14 06:01:28 +00:00
|
|
|
.more {
|
|
|
|
margin-left: auto;
|
|
|
|
margin-right: auto;
|
2020-01-29 19:37:25 +00:00
|
|
|
}
|
|
|
|
</style>
|