enhance: RSSウィジェット / RSSティッカーウィジェットをいい感じにする (#9469)
* ✌️ * use useInterval * ✌️ * rawItems.value.length !== 0 * fix * https://github.com/misskey-dev/misskey/pull/9469#discussion_r1061763613
This commit is contained in:
parent
f51220a5bf
commit
b1a75177a0
3 changed files with 159 additions and 97 deletions
|
@ -3,7 +3,7 @@ import { onMounted, onUnmounted } from 'vue';
|
||||||
export function useInterval(fn: () => void, interval: number, options: {
|
export function useInterval(fn: () => void, interval: number, options: {
|
||||||
immediate: boolean;
|
immediate: boolean;
|
||||||
afterMounted: boolean;
|
afterMounted: boolean;
|
||||||
}): void {
|
}): (() => void) | undefined {
|
||||||
if (Number.isNaN(interval)) return;
|
if (Number.isNaN(interval)) return;
|
||||||
|
|
||||||
let intervalId: number | null = null;
|
let intervalId: number | null = null;
|
||||||
|
@ -18,7 +18,14 @@ export function useInterval(fn: () => void, interval: number, options: {
|
||||||
intervalId = window.setInterval(fn, interval);
|
intervalId = window.setInterval(fn, interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
onUnmounted(() => {
|
const clear = () => {
|
||||||
if (intervalId) window.clearInterval(intervalId);
|
if (intervalId) window.clearInterval(intervalId);
|
||||||
|
intervalId = null;
|
||||||
|
};
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
clear();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return clear;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,13 +3,15 @@
|
||||||
<template #header><i class="ti ti-rss"></i>RSS</template>
|
<template #header><i class="ti ti-rss"></i>RSS</template>
|
||||||
<template #func><button class="_button" @click="configure"><i class="ti ti-settings"></i></button></template>
|
<template #func><button class="_button" @click="configure"><i class="ti ti-settings"></i></button></template>
|
||||||
|
|
||||||
<div class="ekmkgxbk">
|
<div :class="$style.feed">
|
||||||
<MkLoading v-if="fetching"/>
|
<div v-if="fetching" :class="$style.loading">
|
||||||
<div v-else class="feed">
|
<MkEllipsis/>
|
||||||
<Transition name="change" mode="default">
|
</div>
|
||||||
|
<div v-else>
|
||||||
|
<Transition :name="$style.change" mode="default" appear>
|
||||||
<MarqueeText :key="key" :duration="widgetProps.duration" :reverse="widgetProps.reverse">
|
<MarqueeText :key="key" :duration="widgetProps.duration" :reverse="widgetProps.reverse">
|
||||||
<span v-for="item in items" class="item">
|
<span v-for="item in items" :class="$style.item" :key="item.link">
|
||||||
<a class="link" :href="item.link" rel="nofollow noopener" target="_blank" :title="item.title">{{ item.title }}</a><span class="divider"></span>
|
<a :class="$style.link" :href="item.link" rel="nofollow noopener" target="_blank" :title="item.title">{{ item.title }}</a><span :class="$style.divider"></span>
|
||||||
</span>
|
</span>
|
||||||
</MarqueeText>
|
</MarqueeText>
|
||||||
</Transition>
|
</Transition>
|
||||||
|
@ -19,14 +21,14 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { onMounted, onUnmounted, ref, watch } from 'vue';
|
import { ref, watch, computed } from 'vue';
|
||||||
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
|
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
|
||||||
import MarqueeText from '@/components/MkMarquee.vue';
|
import MarqueeText from '@/components/MkMarquee.vue';
|
||||||
import { GetFormResultType } from '@/scripts/form';
|
import { GetFormResultType } from '@/scripts/form';
|
||||||
import * as os from '@/os';
|
|
||||||
import MkContainer from '@/components/MkContainer.vue';
|
import MkContainer from '@/components/MkContainer.vue';
|
||||||
import { useInterval } from '@/scripts/use-interval';
|
|
||||||
import { shuffle } from '@/scripts/shuffle';
|
import { shuffle } from '@/scripts/shuffle';
|
||||||
|
import { url as base } from '@/config';
|
||||||
|
import { useInterval } from '@/scripts/use-interval';
|
||||||
|
|
||||||
const name = 'rssTicker';
|
const name = 'rssTicker';
|
||||||
|
|
||||||
|
@ -43,6 +45,10 @@ const widgetPropsDef = {
|
||||||
type: 'number' as const,
|
type: 'number' as const,
|
||||||
default: 60,
|
default: 60,
|
||||||
},
|
},
|
||||||
|
maxEntries: {
|
||||||
|
type: 'number' as const,
|
||||||
|
default: 15,
|
||||||
|
},
|
||||||
duration: {
|
duration: {
|
||||||
type: 'range' as const,
|
type: 'range' as const,
|
||||||
default: 70,
|
default: 70,
|
||||||
|
@ -78,29 +84,49 @@ const { widgetProps, configure } = useWidgetPropsManager(name,
|
||||||
emit,
|
emit,
|
||||||
);
|
);
|
||||||
|
|
||||||
const items = ref([]);
|
const rawItems = ref([]);
|
||||||
|
const items = computed(() => {
|
||||||
|
const newItems = rawItems.value.slice(0, widgetProps.maxEntries)
|
||||||
|
if (widgetProps.shuffle) {
|
||||||
|
shuffle(newItems);
|
||||||
|
}
|
||||||
|
return newItems;
|
||||||
|
});
|
||||||
const fetching = ref(true);
|
const fetching = ref(true);
|
||||||
|
const fetchEndpoint = computed(() => {
|
||||||
|
const url = new URL('/api/fetch-rss', base);
|
||||||
|
url.searchParams.set('url', widgetProps.url);
|
||||||
|
return url;
|
||||||
|
});
|
||||||
|
let intervalClear = $ref<(() => void) | undefined>();
|
||||||
|
|
||||||
let key = $ref(0);
|
let key = $ref(0);
|
||||||
|
|
||||||
const tick = () => {
|
const tick = () => {
|
||||||
window.fetch(`/api/fetch-rss?url=${widgetProps.url}`, {}).then(res => {
|
if (document.visibilityState === 'hidden' && rawItems.value.length !== 0) return;
|
||||||
res.json().then(feed => {
|
|
||||||
|
window.fetch(fetchEndpoint.value, {})
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(feed => {
|
||||||
if (widgetProps.shuffle) {
|
if (widgetProps.shuffle) {
|
||||||
shuffle(feed.items);
|
shuffle(feed.items);
|
||||||
}
|
}
|
||||||
items.value = feed.items;
|
rawItems.value = feed.items;
|
||||||
fetching.value = false;
|
fetching.value = false;
|
||||||
key++;
|
key++;
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(() => widgetProps.url, tick);
|
watch(() => fetchEndpoint, tick);
|
||||||
|
watch(() => widgetProps.refreshIntervalSec, () => {
|
||||||
useInterval(tick, Math.max(10000, widgetProps.refreshIntervalSec * 1000), {
|
if (intervalClear) {
|
||||||
|
intervalClear();
|
||||||
|
}
|
||||||
|
intervalClear = useInterval(tick, Math.max(10000, widgetProps.refreshIntervalSec * 1000), {
|
||||||
immediate: true,
|
immediate: true,
|
||||||
afterMounted: true,
|
afterMounted: true,
|
||||||
});
|
});
|
||||||
|
}, { immediate: true });
|
||||||
|
|
||||||
defineExpose<WidgetComponentExpose>({
|
defineExpose<WidgetComponentExpose>({
|
||||||
name,
|
name,
|
||||||
|
@ -109,44 +135,49 @@ defineExpose<WidgetComponentExpose>({
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" module>
|
||||||
.change-enter-active, .change-leave-active {
|
.change {
|
||||||
|
&:global(-enter-active),
|
||||||
|
&:global(-leave-active) {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 0;
|
top: 0;
|
||||||
transition: all 1s ease;
|
transition: all 1s ease;
|
||||||
}
|
}
|
||||||
.change-enter-from {
|
&:global(-enter-from) {
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
transform: translateY(-100%);
|
transform: translateY(-100%);
|
||||||
}
|
}
|
||||||
.change-leave-to {
|
&:global(-leave-to) {
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
transform: translateY(100%);
|
transform: translateY(100%);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.ekmkgxbk {
|
.feed {
|
||||||
> .feed {
|
|
||||||
--height: 42px;
|
--height: 42px;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
line-height: var(--height);
|
line-height: var(--height);
|
||||||
height: var(--height);
|
height: var(--height);
|
||||||
contain: strict;
|
contain: strict;
|
||||||
|
}
|
||||||
|
|
||||||
::v-deep(.item) {
|
.loading {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.item {
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
vertical-align: bottom;
|
vertical-align: bottom;
|
||||||
color: var(--fg);
|
color: var(--fg);
|
||||||
|
}
|
||||||
|
|
||||||
> .divider {
|
.divider {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
width: 0.5px;
|
width: 0.5px;
|
||||||
height: 16px;
|
height: 16px;
|
||||||
margin: 0 1em;
|
margin: 0 1em;
|
||||||
background: var(--divider);
|
background: var(--divider);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
@ -5,19 +5,24 @@
|
||||||
|
|
||||||
<div class="ekmkgxbj">
|
<div class="ekmkgxbj">
|
||||||
<MkLoading v-if="fetching"/>
|
<MkLoading v-if="fetching"/>
|
||||||
<div v-else class="feed">
|
<div class="_fullinfo" v-else-if="(!items || items.length === 0) && widgetProps.showHeader">
|
||||||
<a v-for="item in items" class="item" :href="item.link" rel="nofollow noopener" target="_blank" :title="item.title">{{ item.title }}</a>
|
<img src="https://xn--931a.moe/assets/info.jpg" class="_ghost"/>
|
||||||
|
<div>{{ i18n.ts.nothing }}</div>
|
||||||
|
</div>
|
||||||
|
<div v-else :class="$style.feed">
|
||||||
|
<a v-for="item in items" :class="$style.item" :href="item.link" :key="item.link" rel="nofollow noopener" target="_blank" :title="item.title">{{ item.title }}</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</MkContainer>
|
</MkContainer>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { onMounted, onUnmounted, ref, watch } from 'vue';
|
import { ref, watch, computed } from 'vue';
|
||||||
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
|
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
|
||||||
import { GetFormResultType } from '@/scripts/form';
|
import { GetFormResultType } from '@/scripts/form';
|
||||||
import * as os from '@/os';
|
|
||||||
import MkContainer from '@/components/MkContainer.vue';
|
import MkContainer from '@/components/MkContainer.vue';
|
||||||
|
import { url as base } from '@/config';
|
||||||
|
import { i18n } from '@/i18n';
|
||||||
import { useInterval } from '@/scripts/use-interval';
|
import { useInterval } from '@/scripts/use-interval';
|
||||||
|
|
||||||
const name = 'rss';
|
const name = 'rss';
|
||||||
|
@ -27,6 +32,14 @@ const widgetPropsDef = {
|
||||||
type: 'string' as const,
|
type: 'string' as const,
|
||||||
default: 'http://feeds.afpbb.com/rss/afpbb/afpbbnews',
|
default: 'http://feeds.afpbb.com/rss/afpbb/afpbbnews',
|
||||||
},
|
},
|
||||||
|
refreshIntervalSec: {
|
||||||
|
type: 'number' as const,
|
||||||
|
default: 60,
|
||||||
|
},
|
||||||
|
maxEntries: {
|
||||||
|
type: 'number' as const,
|
||||||
|
default: 15,
|
||||||
|
},
|
||||||
showHeader: {
|
showHeader: {
|
||||||
type: 'boolean' as const,
|
type: 'boolean' as const,
|
||||||
default: true,
|
default: true,
|
||||||
|
@ -47,24 +60,37 @@ const { widgetProps, configure } = useWidgetPropsManager(name,
|
||||||
emit,
|
emit,
|
||||||
);
|
);
|
||||||
|
|
||||||
const items = ref([]);
|
const rawItems = ref([]);
|
||||||
|
const items = computed(() => rawItems.value.slice(0, widgetProps.maxEntries));
|
||||||
const fetching = ref(true);
|
const fetching = ref(true);
|
||||||
|
const fetchEndpoint = computed(() => {
|
||||||
|
const url = new URL('/api/fetch-rss', base);
|
||||||
|
url.searchParams.set('url', widgetProps.url);
|
||||||
|
return url;
|
||||||
|
});
|
||||||
|
let intervalClear = $ref<(() => void) | undefined>();
|
||||||
|
|
||||||
const tick = () => {
|
const tick = () => {
|
||||||
window.fetch(`/api/fetch-rss?url=${widgetProps.url}`, {}).then(res => {
|
if (document.visibilityState === 'hidden' && rawItems.value.length !== 0) return;
|
||||||
res.json().then(feed => {
|
|
||||||
items.value = feed.items;
|
window.fetch(fetchEndpoint.value, {})
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(feed => {
|
||||||
|
rawItems.value = feed.items ?? [];
|
||||||
fetching.value = false;
|
fetching.value = false;
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
watch(() => widgetProps.url, tick);
|
watch(() => fetchEndpoint, tick);
|
||||||
|
watch(() => widgetProps.refreshIntervalSec, () => {
|
||||||
useInterval(tick, 60000, {
|
if (intervalClear) {
|
||||||
|
intervalClear();
|
||||||
|
}
|
||||||
|
intervalClear = useInterval(tick, Math.max(10000, widgetProps.refreshIntervalSec * 1000), {
|
||||||
immediate: true,
|
immediate: true,
|
||||||
afterMounted: true,
|
afterMounted: true,
|
||||||
});
|
});
|
||||||
|
}, { immediate: true });
|
||||||
|
|
||||||
defineExpose<WidgetComponentExpose>({
|
defineExpose<WidgetComponentExpose>({
|
||||||
name,
|
name,
|
||||||
|
@ -73,13 +99,13 @@ defineExpose<WidgetComponentExpose>({
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" module>
|
||||||
.ekmkgxbj {
|
.feed {
|
||||||
> .feed {
|
|
||||||
padding: 0;
|
padding: 0;
|
||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
|
||||||
> .item {
|
.item {
|
||||||
display: block;
|
display: block;
|
||||||
padding: 8px 16px;
|
padding: 8px 16px;
|
||||||
color: var(--fg);
|
color: var(--fg);
|
||||||
|
@ -90,7 +116,5 @@ defineExpose<WidgetComponentExpose>({
|
||||||
&:nth-child(even) {
|
&:nth-child(even) {
|
||||||
background: rgba(#000, 0.05);
|
background: rgba(#000, 0.05);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
Loading…
Reference in a new issue