Skip to content

Commit

Permalink
fix: デコレーションを付けるとMkAvatarでエラーになる問題 (misskey-dev#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiyme authored Nov 1, 2024
1 parent d82c218 commit 5b26ad5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
36 changes: 15 additions & 21 deletions packages/frontend/src/pages/settings/avatar-decoration.dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,40 @@ SPDX-License-Identifier: AGPL-3.0-only

<script lang="ts" setup>
import { shallowRef, ref, computed } from 'vue';
import * as Misskey from 'misskey-js';
import MkButton from '@/components/MkButton.vue';
import MkModalWindow from '@/components/MkModalWindow.vue';
import MkSwitch from '@/components/MkSwitch.vue';
import { i18n } from '@/i18n.js';
import MkRange from '@/components/MkRange.vue';
import { signinRequired } from '@/account.js';

type AvatarDecoration = Misskey.entities.GetAvatarDecorationsResponse[number];
type AvatarDecorationWithPosition = Omit<Misskey.entities.UserDetailed['avatarDecorations'][number], 'id' | 'url'>;

const $i = signinRequired();

const props = defineProps<{
usingIndex: number | null;
decoration: {
id: string;
url: string;
name: string;
};
decoration: AvatarDecoration;
}>();

const emit = defineEmits<{
(ev: 'closed'): void;
(ev: 'attach', payload: {
angle: number;
flipH: boolean;
offsetX: number;
offsetY: number;
}): void;
(ev: 'update', payload: {
angle: number;
flipH: boolean;
offsetX: number;
offsetY: number;
}): void;
(ev: 'attach', payload: AvatarDecorationWithPosition): void;
(ev: 'update', payload: AvatarDecorationWithPosition): void;
(ev: 'detach'): void;
}>();

const dialog = shallowRef<InstanceType<typeof MkModalWindow>>();
const exceeded = computed(() => ($i.policies.avatarDecorationLimit - $i.avatarDecorations.length) <= 0);
// eslint-disable-next-line vue/no-setup-props-reactivity-loss
const angle = ref((props.usingIndex != null ? $i.avatarDecorations[props.usingIndex].angle : null) ?? 0);
// eslint-disable-next-line vue/no-setup-props-reactivity-loss
const flipH = ref((props.usingIndex != null ? $i.avatarDecorations[props.usingIndex].flipH : null) ?? false);
// eslint-disable-next-line vue/no-setup-props-reactivity-loss
const offsetX = ref((props.usingIndex != null ? $i.avatarDecorations[props.usingIndex].offsetX : null) ?? 0);
// eslint-disable-next-line vue/no-setup-props-reactivity-loss
const offsetY = ref((props.usingIndex != null ? $i.avatarDecorations[props.usingIndex].offsetY : null) ?? 0);

const decorationsForPreview = computed(() => {
Expand All @@ -108,7 +102,7 @@ const decorationsForPreview = computed(() => {
});

function cancel() {
dialog.value.close();
dialog.value?.close();
}

async function update() {
Expand All @@ -118,7 +112,7 @@ async function update() {
offsetX: offsetX.value,
offsetY: offsetY.value,
});
dialog.value.close();
dialog.value?.close();
}

async function attach() {
Expand All @@ -128,12 +122,12 @@ async function attach() {
offsetX: offsetX.value,
offsetY: offsetY.value,
});
dialog.value.close();
dialog.value?.close();
}

async function detach() {
emit('detach');
dialog.value.close();
dialog.value?.close();
}
</script>

Expand Down
42 changes: 23 additions & 19 deletions packages/frontend/src/pages/settings/avatar-decoration.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ SPDX-License-Identifier: AGPL-3.0-only
<div :class="$style.decorations">
<XDecoration
v-for="(avatarDecoration, i) in $i.avatarDecorations"
:decoration="avatarDecorations.find(d => d.id === avatarDecoration.id)"
:key="avatarDecoration.id"
:decoration="avatarDecorations.find(d => d.id === avatarDecoration.id)!"
:angle="avatarDecoration.angle"
:flipH="avatarDecoration.flipH"
:offsetX="avatarDecoration.offsetX"
:offsetY="avatarDecoration.offsetY"
:active="true"
@click="openDecoration(avatarDecoration, i)"
@click="openDecoration(avatarDecorations.find(d => d.id === avatarDecoration.id)!, i)"
/>
</div>

Expand Down Expand Up @@ -61,55 +62,58 @@ import { signinRequired } from '@/account.js';
import MkInfo from '@/components/MkInfo.vue';
import { definePageMetadata } from '@/scripts/page-metadata.js';

type AvatarDecoration = Misskey.entities.GetAvatarDecorationsResponse[number];
type AvatarDecorationWithPosition = Omit<Misskey.entities.UserDetailed['avatarDecorations'][number], 'id' | 'url'>;

const $i = signinRequired();

const loading = ref(true);
const avatarDecorations = ref<Misskey.entities.GetAvatarDecorationsResponse>([]);
const avatarDecorations = ref<AvatarDecoration[]>([]);

misskeyApi('get-avatar-decorations').then(_avatarDecorations => {
avatarDecorations.value = _avatarDecorations;
loading.value = false;
});

function openDecoration(avatarDecoration, index?: number) {
function openDecoration(avatarDecoration: AvatarDecoration, index?: number) {
const { dispose } = os.popup(defineAsyncComponent(() => import('./avatar-decoration.dialog.vue')), {
decoration: avatarDecoration,
usingIndex: index,
usingIndex: index ?? null,
}, {
'attach': async (payload) => {
'attach': async (payload: AvatarDecorationWithPosition) => {
const decoration = {
...payload,
id: avatarDecoration.id,
angle: payload.angle,
flipH: payload.flipH,
offsetX: payload.offsetX,
offsetY: payload.offsetY,
};
url: avatarDecoration.url,
} as const satisfies AvatarDecorationWithPosition & Pick<AvatarDecoration, 'id' | 'url'>;
const update = [...$i.avatarDecorations, decoration];
await os.apiWithDialog('i/update', {
avatarDecorations: update,
});
$i.avatarDecorations = update;
},
'update': async (payload) => {
'update': async (payload: AvatarDecorationWithPosition) => {
if (index == null) return;
const decoration = {
...payload,
id: avatarDecoration.id,
angle: payload.angle,
flipH: payload.flipH,
offsetX: payload.offsetX,
offsetY: payload.offsetY,
};
url: avatarDecoration.url,
} as const satisfies AvatarDecorationWithPosition & Pick<AvatarDecoration, 'id' | 'url'>;
const update = [...$i.avatarDecorations];
update[index] = decoration;
await os.apiWithDialog('i/update', {
avatarDecorations: update,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
avatarDecorations: update.map(({ url, ...d }) => d),
});
$i.avatarDecorations = update;
},
'detach': async () => {
if (index == null) return;
const update = [...$i.avatarDecorations];
update.splice(index, 1);
await os.apiWithDialog('i/update', {
avatarDecorations: update,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
avatarDecorations: update.map(({ url, ...d }) => d),
});
$i.avatarDecorations = update;
},
Expand Down

0 comments on commit 5b26ad5

Please sign in to comment.