egirlskey/packages/frontend/src/widgets/WidgetSlideshow.vue

162 lines
3.2 KiB
Vue
Raw Normal View History

<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->
2020-12-05 13:29:55 +00:00
<template>
<div data-cy-mkw-slideshow class="kvausudm _panel mkw-slideshow" :style="{ height: widgetProps.height + 'px' }">
2020-12-05 13:29:55 +00:00
<div @click="choose">
<p v-if="widgetProps.folderId == null">
2022-07-20 13:24:26 +00:00
{{ i18n.ts.folder }}
2020-12-05 13:29:55 +00:00
</p>
2023-04-01 05:01:57 +00:00
<p v-if="widgetProps.folderId != null && images.length === 0 && !fetching">{{ i18n.t('no-image') }}</p>
2020-12-05 13:29:55 +00:00
<div ref="slideA" class="slide a"></div>
<div ref="slideB" class="slide b"></div>
</div>
</div>
</template>
<script lang="ts" setup>
import { onMounted, ref, shallowRef } from 'vue';
2023-09-29 02:22:59 +00:00
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget.js';
2023-09-19 07:37:43 +00:00
import { GetFormResultType } from '@/scripts/form.js';
import * as os from '@/os.js';
import { useInterval } from '@/scripts/use-interval.js';
import { i18n } from '@/i18n.js';
2020-12-05 13:29:55 +00:00
const name = 'slideshow';
2020-12-05 13:29:55 +00:00
const widgetPropsDef = {
height: {
type: 'number' as const,
default: 300,
2020-12-05 13:29:55 +00:00
},
folderId: {
type: 'string' as const,
default: null,
hidden: true,
2020-12-05 13:29:55 +00:00
},
};
type WidgetProps = GetFormResultType<typeof widgetPropsDef>;
2023-05-19 07:20:53 +00:00
const props = defineProps<WidgetComponentProps<WidgetProps>>();
const emit = defineEmits<WidgetComponentEmits<WidgetProps>>();
const { widgetProps, configure, save } = useWidgetPropsManager(name,
widgetPropsDef,
props,
emit,
);
const images = ref([]);
const fetching = ref(true);
const slideA = shallowRef<HTMLElement>();
const slideB = shallowRef<HTMLElement>();
const change = () => {
if (images.value.length === 0) return;
const index = Math.floor(Math.random() * images.value.length);
const img = `url(${ images.value[index].url })`;
slideB.value.style.backgroundImage = img;
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;
slideB.value.classList.remove('anime');
}, 1000);
};
const fetch = () => {
fetching.value = true;
os.api('drive/files', {
folderId: widgetProps.folderId,
type: 'image/*',
limit: 100,
}).then(res => {
images.value = res;
fetching.value = false;
slideA.value.style.backgroundImage = '';
slideB.value.style.backgroundImage = '';
change();
});
};
const choose = () => {
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>
2020-12-05 13:29:55 +00:00
.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>