2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
2024-02-13 16:59:27 +01:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
<template>
|
|
|
|
<div class="zmdxowus">
|
2021-11-19 11:36:12 +01:00
|
|
|
<p v-if="choices.length < 2" class="caution">
|
2023-09-30 21:53:52 +02:00
|
|
|
<i class="ph-warning ph-bold ph-lg"></i>{{ i18n.ts._poll.noOnlyOneChoice }}
|
2020-01-29 20:37:25 +01:00
|
|
|
</p>
|
2022-01-25 19:26:12 +01:00
|
|
|
<ul>
|
2020-01-29 20:37:25 +01:00
|
|
|
<li v-for="(choice, i) in choices" :key="i">
|
2024-01-20 00:11:59 +01:00
|
|
|
<MkInput class="input" small :modelValue="choice" :placeholder="i18n.tsx._poll.choiceN({ n: i + 1 })" @update:modelValue="onInput(i, $event)">
|
2020-10-17 13:12:00 +02:00
|
|
|
</MkInput>
|
2021-11-19 11:36:12 +01:00
|
|
|
<button class="_button" @click="remove(i)">
|
2023-09-30 21:53:52 +02:00
|
|
|
<i class="ph-x ph-bold ph-lg"></i>
|
2020-01-29 20:37:25 +01:00
|
|
|
</button>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2022-07-20 15:24:26 +02:00
|
|
|
<MkButton v-if="choices.length < 10" class="add" @click="add">{{ i18n.ts.add }}</MkButton>
|
|
|
|
<MkButton v-else class="add" disabled>{{ i18n.ts._poll.noMore }}</MkButton>
|
|
|
|
<MkSwitch v-model="multiple">{{ i18n.ts._poll.canMultipleVote }}</MkSwitch>
|
2020-01-29 20:37:25 +01:00
|
|
|
<section>
|
|
|
|
<div>
|
2022-06-28 08:59:49 +02:00
|
|
|
<MkSelect v-model="expiration" small>
|
2022-07-20 15:24:26 +02:00
|
|
|
<template #label>{{ i18n.ts._poll.expiration }}</template>
|
|
|
|
<option value="infinite">{{ i18n.ts._poll.infinite }}</option>
|
|
|
|
<option value="at">{{ i18n.ts._poll.at }}</option>
|
|
|
|
<option value="after">{{ i18n.ts._poll.after }}</option>
|
2020-10-17 13:12:00 +02:00
|
|
|
</MkSelect>
|
2020-01-29 20:37:25 +01:00
|
|
|
<section v-if="expiration === 'at'">
|
2022-06-28 08:59:49 +02:00
|
|
|
<MkInput v-model="atDate" small type="date" class="input">
|
2022-07-20 15:24:26 +02:00
|
|
|
<template #label>{{ i18n.ts._poll.deadlineDate }}</template>
|
2020-10-17 13:12:00 +02:00
|
|
|
</MkInput>
|
2022-06-28 08:59:49 +02:00
|
|
|
<MkInput v-model="atTime" small type="time" class="input">
|
2022-07-20 15:24:26 +02:00
|
|
|
<template #label>{{ i18n.ts._poll.deadlineTime }}</template>
|
2020-10-17 13:12:00 +02:00
|
|
|
</MkInput>
|
2020-01-29 20:37:25 +01:00
|
|
|
</section>
|
2022-01-25 19:26:12 +01:00
|
|
|
<section v-else-if="expiration === 'after'">
|
2022-06-28 08:59:49 +02:00
|
|
|
<MkInput v-model="after" small type="number" class="input">
|
2022-07-20 15:24:26 +02:00
|
|
|
<template #label>{{ i18n.ts._poll.duration }}</template>
|
2020-10-17 13:12:00 +02:00
|
|
|
</MkInput>
|
2022-06-28 08:59:49 +02:00
|
|
|
<MkSelect v-model="unit" small>
|
2022-07-20 15:24:26 +02:00
|
|
|
<option value="second">{{ i18n.ts._time.second }}</option>
|
|
|
|
<option value="minute">{{ i18n.ts._time.minute }}</option>
|
|
|
|
<option value="hour">{{ i18n.ts._time.hour }}</option>
|
|
|
|
<option value="day">{{ i18n.ts._time.day }}</option>
|
2020-10-17 13:12:00 +02:00
|
|
|
</MkSelect>
|
2020-01-29 20:37:25 +01:00
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-01-25 19:26:12 +01:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { ref, watch } from 'vue';
|
2023-01-07 07:09:46 +01:00
|
|
|
import MkInput from './MkInput.vue';
|
|
|
|
import MkSelect from './MkSelect.vue';
|
2023-01-07 06:59:54 +01:00
|
|
|
import MkSwitch from './MkSwitch.vue';
|
2022-09-06 11:21:49 +02:00
|
|
|
import MkButton from './MkButton.vue';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { formatDateTimeString } from '@/scripts/format-time-string.js';
|
|
|
|
import { addTime } from '@/scripts/time.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
export type PollEditorModelValue = {
|
|
|
|
expiresAt: number | null;
|
|
|
|
expiredAfter: number | null;
|
|
|
|
choices: string[];
|
|
|
|
multiple: boolean;
|
|
|
|
};
|
|
|
|
|
2022-01-25 19:26:12 +01:00
|
|
|
const props = defineProps<{
|
2024-01-30 11:53:53 +01:00
|
|
|
modelValue: PollEditorModelValue;
|
2022-01-25 19:26:12 +01:00
|
|
|
}>();
|
|
|
|
const emit = defineEmits<{
|
2024-01-30 11:53:53 +01:00
|
|
|
(ev: 'update:modelValue', v: PollEditorModelValue): void;
|
2022-01-25 19:26:12 +01:00
|
|
|
}>();
|
|
|
|
|
|
|
|
const choices = ref(props.modelValue.choices);
|
|
|
|
const multiple = ref(props.modelValue.multiple);
|
|
|
|
const expiration = ref('infinite');
|
|
|
|
const atDate = ref(formatDateTimeString(addTime(new Date(), 1, 'day'), 'yyyy-MM-dd'));
|
|
|
|
const atTime = ref('00:00');
|
|
|
|
const after = ref(0);
|
|
|
|
const unit = ref('second');
|
|
|
|
|
|
|
|
if (props.modelValue.expiresAt) {
|
|
|
|
expiration.value = 'at';
|
2024-01-30 11:53:53 +01:00
|
|
|
const expiresAt = new Date(props.modelValue.expiresAt);
|
|
|
|
atDate.value = formatDateTimeString(expiresAt, 'yyyy-MM-dd');
|
|
|
|
atTime.value = formatDateTimeString(expiresAt, 'HH:mm');
|
2022-01-25 19:26:12 +01:00
|
|
|
} else if (typeof props.modelValue.expiredAfter === 'number') {
|
|
|
|
expiration.value = 'after';
|
|
|
|
after.value = props.modelValue.expiredAfter / 1000;
|
|
|
|
} else {
|
|
|
|
expiration.value = 'infinite';
|
|
|
|
}
|
|
|
|
|
|
|
|
function onInput(i, value) {
|
|
|
|
choices.value[i] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function add() {
|
|
|
|
choices.value.push('');
|
|
|
|
// TODO
|
|
|
|
// nextTick(() => {
|
|
|
|
// (this.$refs.choices as any).childNodes[this.choices.length - 1].childNodes[0].focus();
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove(i) {
|
2022-05-26 15:53:09 +02:00
|
|
|
choices.value = choices.value.filter((_, _i) => _i !== i);
|
2022-01-25 19:26:12 +01:00
|
|
|
}
|
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
function get(): PollEditorModelValue {
|
2022-01-25 19:26:12 +01:00
|
|
|
const calcAt = () => {
|
|
|
|
return new Date(`${atDate.value} ${atTime.value}`).getTime();
|
|
|
|
};
|
|
|
|
|
|
|
|
const calcAfter = () => {
|
2024-01-30 11:53:53 +01:00
|
|
|
let base = parseInt(after.value.toString());
|
2022-01-25 19:26:12 +01:00
|
|
|
switch (unit.value) {
|
2024-01-30 11:53:53 +01:00
|
|
|
// @ts-expect-error fallthrough
|
2022-01-25 19:26:12 +01:00
|
|
|
case 'day': base *= 24;
|
2024-01-30 11:53:53 +01:00
|
|
|
// @ts-expect-error fallthrough
|
2022-01-25 19:26:12 +01:00
|
|
|
case 'hour': base *= 60;
|
2024-01-30 11:53:53 +01:00
|
|
|
// @ts-expect-error fallthrough
|
2022-01-25 19:26:12 +01:00
|
|
|
case 'minute': base *= 60;
|
2024-01-30 11:53:53 +01:00
|
|
|
// eslint-disable-next-line no-fallthrough
|
2022-01-25 19:26:12 +01:00
|
|
|
case 'second': return base *= 1000;
|
|
|
|
default: return null;
|
2020-10-18 08:52:34 +02:00
|
|
|
}
|
2022-01-25 19:26:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
choices: choices.value,
|
|
|
|
multiple: multiple.value,
|
2024-01-30 11:53:53 +01:00
|
|
|
expiresAt: expiration.value === 'at' ? calcAt() : null,
|
|
|
|
expiredAfter: expiration.value === 'after' ? calcAfter() : null,
|
2022-01-25 19:26:12 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
watch([choices, multiple, expiration, atDate, atTime, after, unit], () => emit('update:modelValue', get()), {
|
|
|
|
deep: true,
|
2020-01-29 20:37:25 +01:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-12-27 10:29:39 +01:00
|
|
|
<style lang="scss" scoped>
|
2020-01-29 20:37:25 +01:00
|
|
|
.zmdxowus {
|
2022-01-25 19:31:10 +01:00
|
|
|
padding: 8px 16px;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
> .caution {
|
|
|
|
margin: 0 0 8px 0;
|
|
|
|
font-size: 0.8em;
|
|
|
|
color: #f00;
|
|
|
|
|
2021-04-20 16:22:59 +02:00
|
|
|
> i {
|
2020-01-29 20:37:25 +01:00
|
|
|
margin-right: 4px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> ul {
|
|
|
|
display: block;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
list-style: none;
|
|
|
|
|
|
|
|
> li {
|
|
|
|
display: flex;
|
|
|
|
margin: 8px 0;
|
|
|
|
padding: 0;
|
|
|
|
width: 100%;
|
|
|
|
|
|
|
|
> .input {
|
|
|
|
flex: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
> button {
|
|
|
|
width: 32px;
|
|
|
|
padding: 4px 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .add {
|
2022-01-25 19:26:12 +01:00
|
|
|
margin: 8px 0;
|
2020-01-29 20:37:25 +01:00
|
|
|
z-index: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
> section {
|
2021-12-03 05:55:30 +01:00
|
|
|
margin: 16px 0 0 0;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
> div {
|
|
|
|
margin: 0 8px;
|
2022-01-25 19:26:12 +01:00
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
gap: 12px;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
&:last-child {
|
|
|
|
flex: 1 0 auto;
|
|
|
|
|
2022-01-25 19:26:12 +01:00
|
|
|
> div {
|
|
|
|
flex-grow: 1;
|
|
|
|
}
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
> section {
|
2022-01-25 19:26:12 +01:00
|
|
|
// MAGIC: Prevent div above from growing unless wrapped to its own line
|
|
|
|
flex-grow: 9999;
|
|
|
|
align-items: end;
|
2020-01-29 20:37:25 +01:00
|
|
|
display: flex;
|
2022-01-25 19:26:12 +01:00
|
|
|
gap: 4px;
|
2020-01-29 20:37:25 +01:00
|
|
|
|
|
|
|
> .input {
|
2022-01-25 19:26:12 +01:00
|
|
|
flex: 1 1 auto;
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|