calckey/packages/client/src/widgets/slideshow.vue

178 lines
3.4 KiB
Vue
Raw Normal View History

2020-12-05 13:29:55 +00:00
<template>
2023-04-08 00:01:42 +00:00
<div
class="kvausudm _panel mkw-slideshow"
:style="{ height: widgetProps.height + 'px' }"
>
<div @click="choose">
<p v-if="widgetProps.folderId == null">
{{ i18n.ts.folder }}
</p>
<p
v-if="
widgetProps.folderId != null &&
images.length === 0 &&
!fetching
"
>
{{ i18n.t("no-image") }}
</p>
<div ref="slideA" class="slide a"></div>
<div ref="slideB" class="slide b"></div>
</div>
2020-12-05 13:29:55 +00:00
</div>
</template>
<script lang="ts" setup>
2023-04-08 00:01:42 +00:00
import { nextTick, onMounted, onUnmounted, reactive, ref } from "vue";
import {
useWidgetPropsManager,
Widget,
WidgetComponentEmits,
WidgetComponentExpose,
WidgetComponentProps,
} from "./widget";
import { GetFormResultType } from "@/scripts/form";
import * as os from "@/os";
import { useInterval } from "@/scripts/use-interval";
import { i18n } from "@/i18n";
const name = "slideshow";
2020-12-05 13:29:55 +00:00
const widgetPropsDef = {
height: {
2023-04-08 00:01:42 +00:00
type: "number" as const,
default: 300,
2020-12-05 13:29:55 +00:00
},
folderId: {
2023-04-08 00:01:42 +00:00
type: "string" as const,
default: null,
hidden: true,
2020-12-05 13:29:55 +00:00
},
};
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
// 現時点ではvueの制限によりimportしたtypeをジェネリックに渡せない
//const props = defineProps<WidgetComponentProps<WidgetProps>>();
//const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
2023-04-08 00:01:42 +00:00
const props = defineProps<{ widget?: Widget<WidgetProps> }>();
const emit = defineEmits<{ (ev: "updateProps", props: WidgetProps) }>();
2023-04-08 00:01:42 +00:00
const { widgetProps, configure, save } = useWidgetPropsManager(
name,
widgetPropsDef,
props,
2023-04-08 00:01:42 +00:00
emit
);
const images = ref([]);
const fetching = ref(true);
const slideA = ref<HTMLElement>();
const slideB = ref<HTMLElement>();
const change = () => {
if (images.value.length === 0) return;
const index = Math.floor(Math.random() * images.value.length);
2023-04-08 00:01:42 +00:00
const img = `url(${images.value[index].url})`;
slideB.value.style.backgroundImage = img;
2023-04-08 00:01:42 +00:00
slideB.value.classList.add("anime");
2022-01-16 01:14:14 +00:00
window.setTimeout(() => {
// 既にこのウィジェットがunmountされていたら要素がない
if (slideA.value == null) return;
slideA.value.style.backgroundImage = img;
2023-04-08 00:01:42 +00:00
slideB.value.classList.remove("anime");
}, 1000);
};
const fetch = () => {
fetching.value = true;
2023-04-08 00:01:42 +00:00
os.api("drive/files", {
folderId: widgetProps.folderId,
2023-04-08 00:01:42 +00:00
type: "image/*",
limit: 100,
2023-04-08 00:01:42 +00:00
}).then((res) => {
images.value = res;
fetching.value = false;
2023-04-08 00:01:42 +00:00
slideA.value.style.backgroundImage = "";
slideB.value.style.backgroundImage = "";
change();
});
};
const choose = () => {
2023-04-08 00:01:42 +00:00
os.selectDriveFolder(false).then((folder) => {
if (folder == null) {
return;
2020-12-05 13:29:55 +00:00
}
widgetProps.folderId = folder.id;
save();
fetch();
});
};
useInterval(change, 10000, {
immediate: false,
afterMounted: true,
});
onMounted(() => {
if (widgetProps.folderId != null) {
fetch();
2020-12-05 13:29:55 +00:00
}
});
defineExpose<WidgetComponentExpose>({
name,
configure,
id: props.widget ? props.widget.id : null,
2020-12-05 13:29:55 +00:00
});
</script>
<style lang="scss" scoped>
.kvausudm {
position: relative;
> div {
width: 100%;
height: 100%;
cursor: pointer;
> p {
display: block;
margin: 1em;
text-align: center;
color: #888;
}
> * {
pointer-events: none;
}
> .slide {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
&.b {
opacity: 0;
}
&.anime {
transition: opacity 1s;
opacity: 1;
}
}
}
}
</style>