2020-12-05 13:29:55 +00:00
|
|
|
<template>
|
2022-05-28 05:28:12 +00:00
|
|
|
<div class="kvausudm _panel mkw-slideshow" :style="{ height: widgetProps.height + 'px' }">
|
2020-12-05 13:29:55 +00:00
|
|
|
<div @click="choose">
|
2022-01-08 11:30:01 +00:00
|
|
|
<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>
|
2022-01-08 11:30:01 +00:00
|
|
|
<p v-if="widgetProps.folderId != null && images.length === 0 && !fetching">{{ $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>
|
|
|
|
|
2022-01-08 11:30:01 +00:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { nextTick, onMounted, onUnmounted, reactive, ref } from 'vue';
|
|
|
|
import { useWidgetPropsManager, Widget, WidgetComponentEmits, WidgetComponentExpose, WidgetComponentProps } from './widget';
|
2022-06-25 18:12:58 +00:00
|
|
|
import { GetFormResultType } from '@/scripts/form';
|
2021-11-11 17:02:25 +00:00
|
|
|
import * as os from '@/os';
|
2022-06-25 18:12:58 +00:00
|
|
|
import { useInterval } from '@/scripts/use-interval';
|
2022-07-20 13:24:26 +00:00
|
|
|
import { i18n } from '@/i18n';
|
2020-12-05 13:29:55 +00:00
|
|
|
|
2022-01-08 11:30:01 +00:00
|
|
|
const name = 'slideshow';
|
2020-12-05 13:29:55 +00:00
|
|
|
|
2022-01-08 11:30:01 +00:00
|
|
|
const widgetPropsDef = {
|
|
|
|
height: {
|
|
|
|
type: 'number' as const,
|
|
|
|
default: 300,
|
2020-12-05 13:29:55 +00:00
|
|
|
},
|
2022-01-08 11:30:01 +00:00
|
|
|
folderId: {
|
|
|
|
type: 'string' as const,
|
|
|
|
default: null,
|
|
|
|
hidden: true,
|
2020-12-05 13:29:55 +00:00
|
|
|
},
|
2022-01-08 11:30:01 +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>; }>();
|
2022-05-25 07:43:12 +00:00
|
|
|
const emit = defineEmits<{ (ev: 'updateProps', props: WidgetProps); }>();
|
2022-01-08 11:30:01 +00:00
|
|
|
|
|
|
|
const { widgetProps, configure, save } = useWidgetPropsManager(name,
|
|
|
|
widgetPropsDef,
|
|
|
|
props,
|
|
|
|
emit,
|
|
|
|
);
|
|
|
|
|
|
|
|
const images = ref([]);
|
|
|
|
const fetching = ref(true);
|
|
|
|
const slideA = ref<HTMLElement>();
|
|
|
|
const slideB = ref<HTMLElement>();
|
|
|
|
|
|
|
|
const change = () => {
|
2022-05-25 07:43:12 +00:00
|
|
|
if (images.value.length === 0) return;
|
2022-01-08 11:30:01 +00:00
|
|
|
|
|
|
|
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(() => {
|
2022-01-08 11:30:01 +00:00
|
|
|
// 既にこのウィジェットが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/*',
|
2022-06-25 18:12:58 +00:00
|
|
|
limit: 100,
|
2022-01-08 11:30:01 +00:00
|
|
|
}).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
|
|
|
}
|
2022-01-08 11:30:01 +00:00
|
|
|
widgetProps.folderId = folder.id;
|
|
|
|
save();
|
|
|
|
fetch();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-25 18:12:58 +00:00
|
|
|
useInterval(change, 10000, {
|
|
|
|
immediate: false,
|
|
|
|
afterMounted: true,
|
|
|
|
});
|
|
|
|
|
2022-01-08 11:30:01 +00:00
|
|
|
onMounted(() => {
|
|
|
|
if (widgetProps.folderId != null) {
|
|
|
|
fetch();
|
2020-12-05 13:29:55 +00:00
|
|
|
}
|
2022-01-08 11:30:01 +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>
|