misskey/packages/frontend/src/components/MkReactionsViewer.reaction.vue

212 lines
4.8 KiB
Vue
Raw Normal View History

<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->
<template>
2020-02-21 21:43:46 +00:00
<button
2023-01-08 05:28:14 +00:00
ref="buttonEl"
2021-12-27 13:59:14 +00:00
v-ripple="canToggle"
2023-01-14 08:23:49 +00:00
class="_button"
:class="[$style.root, { [$style.reacted]: note.myReaction == reaction, [$style.canToggle]: canToggle, [$style.small]: defaultStore.state.reactionsDisplaySize === 'small', [$style.large]: defaultStore.state.reactionsDisplaySize === 'large' }]"
@click="toggleReaction()"
>
<MkReactionIcon :class="defaultStore.state.limitWidthOfReaction ? $style.limitWidth : ''" :reaction="reaction" :emojiUrl="note.reactionEmojis[reaction.substring(1, reaction.length - 1)]"/>
2023-01-14 08:23:49 +00:00
<span :class="$style.count">{{ count }}</span>
2020-02-21 21:43:46 +00:00
</button>
</template>
2022-06-30 14:51:18 +00:00
<script lang="ts" setup>
import { computed, inject, onMounted, shallowRef, watch } from 'vue';
import * as Misskey from 'misskey-js';
import XDetails from '@/components/MkReactionsViewer.details.vue';
2023-01-08 05:22:04 +00:00
import MkReactionIcon from '@/components/MkReactionIcon.vue';
2023-09-19 07:37:43 +00:00
import * as os from '@/os.js';
import { misskeyApi, misskeyApiGet } from '@/scripts/misskey-api.js';
2023-09-19 07:37:43 +00:00
import { useTooltip } from '@/scripts/use-tooltip.js';
import { $i } from '@/account.js';
2023-01-08 07:17:42 +00:00
import MkReactionEffect from '@/components/MkReactionEffect.vue';
2023-09-19 07:37:43 +00:00
import { claimAchievement } from '@/scripts/achievements.js';
import { defaultStore } from '@/store.js';
import { i18n } from '@/i18n.js';
import * as sound from '@/scripts/sound.js';
2022-06-30 14:51:18 +00:00
const props = defineProps<{
reaction: string;
count: number;
isInitial: boolean;
note: Misskey.entities.Note;
2022-06-30 14:51:18 +00:00
}>();
const mock = inject<boolean>('mock', false);
const emit = defineEmits<{
(ev: 'reactionToggled', emoji: string, newCount: number): void;
}>();
2023-01-08 05:28:14 +00:00
const buttonEl = shallowRef<HTMLElement>();
2022-06-30 14:51:18 +00:00
const canToggle = computed(() => !props.reaction.match(/@\w/) && $i);
async function toggleReaction() {
2022-06-30 14:51:18 +00:00
if (!canToggle.value) return;
2023-05-19 00:48:48 +00:00
// TODO: その絵文字を使う権限があるかどうか確認
2022-06-30 14:51:18 +00:00
const oldReaction = props.note.myReaction;
if (oldReaction) {
const confirm = await os.confirm({
type: 'warning',
text: oldReaction !== props.reaction ? i18n.ts.changeReactionConfirm : i18n.ts.cancelReactionConfirm,
});
if (confirm.canceled) return;
if (oldReaction !== props.reaction) {
sound.play('reaction');
}
if (mock) {
emit('reactionToggled', props.reaction, (props.count - 1));
return;
}
misskeyApi('notes/reactions/delete', {
2022-06-30 14:51:18 +00:00
noteId: props.note.id,
}).then(() => {
if (oldReaction !== props.reaction) {
misskeyApi('notes/reactions/create', {
noteId: props.note.id,
2022-06-30 14:51:18 +00:00
reaction: props.reaction,
});
}
});
2022-06-30 14:51:18 +00:00
} else {
sound.play('reaction');
if (mock) {
emit('reactionToggled', props.reaction, (props.count + 1));
return;
}
misskeyApi('notes/reactions/create', {
2022-06-30 14:51:18 +00:00
noteId: props.note.id,
reaction: props.reaction,
});
if (props.note.text && props.note.text.length > 100 && (Date.now() - new Date(props.note.createdAt).getTime() < 1000 * 3)) {
claimAchievement('reactWithoutRead');
}
2022-06-30 14:51:18 +00:00
}
}
function anime() {
2022-06-30 14:51:18 +00:00
if (document.hidden) return;
if (!defaultStore.state.animation) return;
2022-06-30 14:51:18 +00:00
2023-01-08 05:28:14 +00:00
const rect = buttonEl.value.getBoundingClientRect();
const x = rect.left + 16;
const y = rect.top + (buttonEl.value.offsetHeight / 2);
2023-01-08 07:17:42 +00:00
os.popup(MkReactionEffect, { reaction: props.reaction, x, y }, {}, 'end');
}
2022-06-30 14:51:18 +00:00
watch(() => props.count, (newCount, oldCount) => {
if (oldCount < newCount) anime();
});
2022-06-30 14:51:18 +00:00
onMounted(() => {
if (!props.isInitial) anime();
});
if (!mock) {
useTooltip(buttonEl, async (showing) => {
const reactions = await misskeyApiGet('notes/reactions', {
noteId: props.note.id,
type: props.reaction,
limit: 10,
_cacheKey_: props.count,
});
const users = reactions.map(x => x.user);
os.popup(XDetails, {
showing,
reaction: props.reaction,
users,
count: props.count,
targetElement: buttonEl.value,
}, {}, 'closed');
}, 100);
}
</script>
2023-01-14 08:23:49 +00:00
<style lang="scss" module>
.root {
display: inline-flex;
height: 42px;
margin: 2px;
padding: 0 6px;
font-size: 1.5em;
border-radius: 6px;
align-items: center;
justify-content: center;
2019-09-02 21:20:04 +00:00
2020-04-15 15:47:17 +00:00
&.canToggle {
background: var(--buttonBg);
2020-04-15 15:47:17 +00:00
&:hover {
background: rgba(0, 0, 0, 0.1);
}
}
2020-04-15 15:47:17 +00:00
&:not(.canToggle) {
cursor: default;
}
&.small {
height: 32px;
font-size: 1em;
border-radius: 4px;
> .count {
font-size: 0.9em;
line-height: 32px;
}
}
&.large {
height: 52px;
font-size: 2em;
border-radius: 8px;
> .count {
font-size: 0.6em;
line-height: 52px;
}
}
&.reacted, &.reacted:hover {
2023-09-18 04:25:49 +00:00
background: var(--accentedBg);
color: var(--accent);
box-shadow: 0 0 0 1px var(--accent) inset;
2020-04-17 11:30:12 +00:00
> .count {
color: var(--accent);
}
> .icon {
filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.5));
}
}
2023-01-14 08:23:49 +00:00
}
.limitWidth {
2023-12-27 06:15:08 +00:00
max-width: 70px;
object-fit: contain;
2023-09-21 01:29:40 +00:00
}
2023-01-14 08:23:49 +00:00
.count {
font-size: 0.7em;
line-height: 42px;
2023-01-14 08:23:49 +00:00
margin: 0 0 0 4px;
}
</style>