2020-01-29 20:37:25 +01:00
|
|
|
<template>
|
2023-01-18 23:54:59 +01:00
|
|
|
<transition :name="$store.state.animation ? 'fade' : ''" mode="out-in">
|
2023-04-08 02:01:42 +02:00
|
|
|
<MkLoading v-if="fetching" />
|
2023-01-18 23:54:59 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
<MkError v-else-if="error" @retry="init()" />
|
2023-01-18 23:54:59 +01:00
|
|
|
|
|
|
|
<div v-else-if="empty" key="_empty_" class="empty">
|
|
|
|
<slot name="empty">
|
|
|
|
<div class="_fullinfo">
|
2023-04-08 02:01:42 +02:00
|
|
|
<img
|
|
|
|
src="/static-assets/badges/info.png"
|
|
|
|
class="_ghost"
|
|
|
|
alt="Error"
|
|
|
|
/>
|
2023-01-18 23:54:59 +01:00
|
|
|
<div>{{ i18n.ts.nothing }}</div>
|
|
|
|
</div>
|
|
|
|
</slot>
|
|
|
|
</div>
|
2021-04-22 15:29:33 +02:00
|
|
|
|
2023-01-18 23:54:59 +01:00
|
|
|
<div v-else ref="rootEl">
|
2023-04-08 02:01:42 +02:00
|
|
|
<div
|
|
|
|
v-show="pagination.reversed && more"
|
|
|
|
key="_more_"
|
|
|
|
class="cxiknjgy _gap"
|
|
|
|
>
|
|
|
|
<MkButton
|
|
|
|
v-if="!moreFetching"
|
|
|
|
class="button"
|
|
|
|
:disabled="moreFetching"
|
|
|
|
:style="{ cursor: moreFetching ? 'wait' : 'pointer' }"
|
|
|
|
primary
|
|
|
|
@click="fetchMoreAhead"
|
|
|
|
>
|
2023-01-18 23:54:59 +01:00
|
|
|
{{ i18n.ts.loadMore }}
|
|
|
|
</MkButton>
|
2023-04-08 02:01:42 +02:00
|
|
|
<MkLoading v-else class="loading" />
|
2023-01-18 23:54:59 +01:00
|
|
|
</div>
|
|
|
|
<slot :items="items"></slot>
|
2023-04-08 02:01:42 +02:00
|
|
|
<div
|
|
|
|
v-show="!pagination.reversed && 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"
|
|
|
|
>
|
2023-01-18 23:54:59 +01:00
|
|
|
{{ i18n.ts.loadMore }}
|
|
|
|
</MkButton>
|
2023-04-08 02:01:42 +02:00
|
|
|
<MkLoading v-else class="loading" />
|
2021-12-23 08:10:13 +01:00
|
|
|
</div>
|
2021-04-22 15:29:33 +02:00
|
|
|
</div>
|
2023-01-18 23:54:59 +01:00
|
|
|
</transition>
|
2023-04-08 02:01:42 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
|
|
|
import {
|
|
|
|
computed,
|
|
|
|
ComputedRef,
|
|
|
|
isRef,
|
|
|
|
markRaw,
|
|
|
|
onActivated,
|
|
|
|
onDeactivated,
|
|
|
|
Ref,
|
|
|
|
ref,
|
|
|
|
watch,
|
|
|
|
} from "vue";
|
|
|
|
import * as misskey from "calckey-js";
|
|
|
|
import * as os from "@/os";
|
|
|
|
import {
|
|
|
|
onScrollTop,
|
|
|
|
isTopVisible,
|
|
|
|
getScrollPosition,
|
|
|
|
getScrollContainer,
|
|
|
|
} from "@/scripts/scroll";
|
|
|
|
import MkButton from "@/components/MkButton.vue";
|
|
|
|
import { i18n } from "@/i18n";
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
offsetMode?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
const SECOND_FETCH_LIMIT = 30;
|
|
|
|
|
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
2023-01-20 01:33:14 +01:00
|
|
|
pagination: Paging;
|
|
|
|
disableAutoLoad?: boolean;
|
|
|
|
displayLimit?: number;
|
2023-04-08 02:01:42 +02:00
|
|
|
}>(),
|
|
|
|
{
|
2023-01-20 01:33:14 +01:00
|
|
|
displayLimit: 30,
|
2023-04-08 02:01:42 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(ev: "queue", count: number): void;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
type Item = { id: string; [another: string]: unknown };
|
|
|
|
|
|
|
|
const rootEl = ref<HTMLElement>();
|
|
|
|
const items = ref<Item[]>([]);
|
|
|
|
const queue = ref<Item[]>([]);
|
|
|
|
const offset = ref(0);
|
|
|
|
const fetching = ref(true);
|
|
|
|
const moreFetching = ref(false);
|
|
|
|
const more = ref(false);
|
|
|
|
const backed = ref(false); // 遡り中か否か
|
|
|
|
const isBackTop = ref(false);
|
|
|
|
const empty = computed(() => items.value.length === 0);
|
|
|
|
const error = ref(false);
|
|
|
|
|
|
|
|
const init = async (): Promise<void> => {
|
|
|
|
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, {
|
2023-01-20 01:33:14 +01:00
|
|
|
...params,
|
2023-04-08 02:01:42 +02:00
|
|
|
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;
|
2023-01-20 01:33:14 +01:00
|
|
|
} else {
|
2023-04-08 02:01:42 +02:00
|
|
|
items.value = props.pagination.reversed
|
|
|
|
? [...res].reverse()
|
|
|
|
: res;
|
|
|
|
more.value = false;
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
offset.value = res.length;
|
|
|
|
error.value = false;
|
|
|
|
fetching.value = false;
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
error.value = true;
|
|
|
|
fetching.value = false;
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const reload = (): void => {
|
|
|
|
items.value = [];
|
|
|
|
init();
|
|
|
|
};
|
|
|
|
|
|
|
|
const refresh = async (): void => {
|
|
|
|
const params = props.pagination.params
|
|
|
|
? isRef(props.pagination.params)
|
|
|
|
? props.pagination.params.value
|
|
|
|
: props.pagination.params
|
|
|
|
: {};
|
|
|
|
await os
|
|
|
|
.api(props.pagination.endpoint, {
|
2023-01-20 01:33:14 +01:00
|
|
|
...params,
|
|
|
|
limit: items.value.length + 1,
|
|
|
|
offset: 0,
|
2023-04-08 02:01:42 +02:00
|
|
|
})
|
|
|
|
.then(
|
|
|
|
(res) => {
|
|
|
|
let ids = items.value.reduce((a, b) => {
|
|
|
|
a[b.id] = true;
|
|
|
|
return a;
|
|
|
|
}, {} as { [id: string]: boolean });
|
|
|
|
|
|
|
|
for (let i = 0; i < res.length; i++) {
|
|
|
|
const item = res[i];
|
|
|
|
if (!updateItem(item.id, (old) => item)) {
|
|
|
|
append(item);
|
|
|
|
}
|
|
|
|
delete ids[item.id];
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
2022-12-29 11:00:30 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
for (const id in ids) {
|
|
|
|
removeItem((i) => i.id === id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
error.value = true;
|
|
|
|
fetching.value = false;
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchMore = async (): Promise<void> => {
|
|
|
|
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, {
|
2023-01-20 01:33:14 +01:00
|
|
|
...params,
|
|
|
|
limit: SECOND_FETCH_LIMIT + 1,
|
2023-04-08 02:01:42 +02:00
|
|
|
...(props.pagination.offsetMode
|
|
|
|
? {
|
|
|
|
offset: offset.value,
|
|
|
|
}
|
|
|
|
: props.pagination.reversed
|
|
|
|
? {
|
|
|
|
sinceId: items.value[0].id,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
untilId: 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;
|
2023-01-20 01:33:14 +01:00
|
|
|
} else {
|
2023-04-08 02:01:42 +02:00
|
|
|
items.value = props.pagination.reversed
|
|
|
|
? [...res].reverse().concat(items.value)
|
|
|
|
: items.value.concat(res);
|
|
|
|
more.value = false;
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
offset.value += res.length;
|
|
|
|
moreFetching.value = false;
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
moreFetching.value = false;
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchMoreAhead = async (): Promise<void> => {
|
|
|
|
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, {
|
2023-01-20 01:33:14 +01:00
|
|
|
...params,
|
|
|
|
limit: SECOND_FETCH_LIMIT + 1,
|
2023-04-08 02:01:42 +02:00
|
|
|
...(props.pagination.offsetMode
|
|
|
|
? {
|
|
|
|
offset: offset.value,
|
|
|
|
}
|
|
|
|
: props.pagination.reversed
|
|
|
|
? {
|
|
|
|
untilId: items.value[0].id,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
sinceId: 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;
|
2023-01-20 01:33:14 +01:00
|
|
|
} else {
|
2023-04-08 02:01:42 +02:00
|
|
|
items.value = props.pagination.reversed
|
|
|
|
? [...res].reverse().concat(items.value)
|
|
|
|
: items.value.concat(res);
|
|
|
|
more.value = false;
|
2022-01-09 13:35:35 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
offset.value += res.length;
|
|
|
|
moreFetching.value = false;
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
moreFetching.value = false;
|
2022-01-09 13:35:35 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const prepend = (item: Item): void => {
|
|
|
|
if (props.pagination.reversed) {
|
|
|
|
if (rootEl.value) {
|
|
|
|
const container = getScrollContainer(rootEl.value);
|
|
|
|
if (container == null) {
|
|
|
|
// TODO?
|
2023-01-20 01:33:14 +01:00
|
|
|
} else {
|
2023-04-08 02:01:42 +02:00
|
|
|
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;
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
}
|
2023-01-20 01:31:33 +01:00
|
|
|
}
|
2022-01-09 13:35:35 +01:00
|
|
|
}
|
2023-01-20 01:33:14 +01:00
|
|
|
items.value.push(item);
|
2023-04-08 02:01:42 +02:00
|
|
|
// TODO
|
|
|
|
} else {
|
|
|
|
// 初回表示時はunshiftだけでOK
|
|
|
|
if (!rootEl.value) {
|
|
|
|
items.value.unshift(item);
|
|
|
|
return;
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
|
|
|
|
2023-04-08 02:01:42 +02: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 queueItem of queue.value) {
|
|
|
|
prepend(queueItem);
|
|
|
|
}
|
|
|
|
queue.value = [];
|
|
|
|
});
|
2023-01-20 01:33:14 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
}
|
|
|
|
};
|
2022-01-09 13:35:35 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const append = (item: Item): void => {
|
|
|
|
items.value.push(item);
|
|
|
|
};
|
2022-01-09 13:35:35 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const removeItem = (finder: (item: Item) => boolean): boolean => {
|
|
|
|
const i = items.value.findIndex(finder);
|
|
|
|
if (i === -1) {
|
|
|
|
return false;
|
2022-12-29 11:00:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
items.value.splice(i, 1);
|
|
|
|
return true;
|
|
|
|
};
|
2023-01-20 01:33:14 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const updateItem = (id: Item["id"], replacer: (old: Item) => Item): boolean => {
|
|
|
|
const i = items.value.findIndex((item) => item.id === id);
|
|
|
|
if (i === -1) {
|
|
|
|
return false;
|
2022-12-29 11:00:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
items.value[i] = replacer(items.value[i]);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 }
|
|
|
|
);
|
|
|
|
|
|
|
|
init();
|
|
|
|
|
|
|
|
onActivated(() => {
|
|
|
|
isBackTop.value = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
onDeactivated(() => {
|
|
|
|
isBackTop.value = window.scrollY === 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
items,
|
|
|
|
queue,
|
|
|
|
backed,
|
|
|
|
reload,
|
|
|
|
refresh,
|
|
|
|
prepend,
|
|
|
|
append,
|
|
|
|
removeItem,
|
|
|
|
updateItem,
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.fade-enter-active,
|
|
|
|
.fade-leave-active {
|
|
|
|
transition: opacity 0.125s ease;
|
|
|
|
}
|
|
|
|
.fade-enter-from,
|
|
|
|
.fade-leave-to {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.cxiknjgy {
|
|
|
|
> .button {
|
|
|
|
margin-left: auto;
|
|
|
|
margin-right: auto;
|
2020-02-11 18:52:37 +01:00
|
|
|
}
|
2023-04-08 02:01:42 +02:00
|
|
|
}
|
|
|
|
</style>
|