magnetar/fe_calckey/frontend/client/src/widgets/rss.vue

115 lines
2.9 KiB
Vue
Raw Normal View History

2023-07-07 19:22:30 +00:00
<template>
<MkContainer :show-header="widgetProps.showHeader" class="mkw-rss">
<template #header><i class="ph-rss ph-bold ph-lg"></i>RSS</template>
<template #func
><button class="_button" @click="configure">
<i class="ph-gear-six ph-bold ph-lg"></i></button
></template>
2023-07-07 19:22:30 +00:00
<div class="ekmkgxbj">
<MkLoading v-if="fetching" />
<div v-else class="feed">
<a
v-for="item in items"
class="item"
:href="item.link"
rel="nofollow noopener"
target="_blank"
:title="item.title"
>{{ item.title }}</a
>
</div>
</div>
</MkContainer>
2023-07-07 19:22:30 +00:00
</template>
<script lang="ts" setup>
import { onMounted, onUnmounted, ref, watch } from "vue";
import {
useWidgetPropsManager,
Widget,
WidgetComponentEmits,
WidgetComponentExpose,
WidgetComponentProps,
2023-07-07 19:22:30 +00:00
} from "./widget";
import { GetFormResultType } from "@/scripts/form";
import * as os from "@/os";
import MkContainer from "@/components/MkContainer.vue";
import { useInterval } from "@/scripts/use-interval";
const name = "rss";
const widgetPropsDef = {
url: {
type: "string" as const,
default: "http://feeds.afpbb.com/rss/afpbb/afpbbnews",
},
showHeader: {
type: "boolean" as const,
default: true,
},
2023-07-07 19:22:30 +00:00
};
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
// 現時点ではvueの制限によりimportしたtypeをジェネリックに渡せない
//const props = defineProps<WidgetComponentProps<WidgetProps>>();
//const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
const props = defineProps<{ widget?: Widget<WidgetProps> }>();
const emit = defineEmits<{ (ev: "updateProps", props: WidgetProps) }>();
const { widgetProps, configure } = useWidgetPropsManager(
name,
widgetPropsDef,
props,
2024-04-08 01:10:23 +00:00
emit,
2023-07-07 19:22:30 +00:00
);
const items = ref([]);
const fetching = ref(true);
const tick = () => {
fetch(`/api/fetch-rss?url=${widgetProps.url}`, {}).then((res) => {
res.json().then((feed) => {
items.value = feed.items;
fetching.value = false;
});
});
2023-07-07 19:22:30 +00:00
};
watch(() => widgetProps.url, tick);
useInterval(tick, 60000, {
immediate: true,
afterMounted: true,
2023-07-07 19:22:30 +00:00
});
defineExpose<WidgetComponentExpose>({
name,
configure,
id: props.widget ? props.widget.id : null,
2023-07-07 19:22:30 +00:00
});
</script>
<style lang="scss" scoped>
.ekmkgxbj {
> .feed {
padding: 0;
font-size: 0.9em;
2023-07-07 19:22:30 +00:00
> .item {
display: block;
padding: 8px 16px;
color: var(--fg);
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
2023-07-07 19:22:30 +00:00
&:nth-child(even) {
background: rgba(#000, 0.05);
}
}
}
2023-07-07 19:22:30 +00:00
}
</style>