hippofish/packages/frontend/src/ui/universal.widgets.vue

82 lines
2.5 KiB
Vue
Raw Normal View History

<!--
SPDX-FileCopyrightText: syuilo and misskey-project
SPDX-License-Identifier: AGPL-3.0-only
-->
<template>
<div>
<XWidgets :edit="editMode" :widgets="widgets" @addWidget="addWidget" @removeWidget="removeWidget" @updateWidget="updateWidget" @updateWidgets="updateWidgets" @exit="editMode = false"/>
2023-09-30 21:53:52 +02:00
<button v-if="editMode" class="_textButton" style="font-size: 0.9em;" @click="editMode = false"><i class="ph-check ph-bold ph-lg"></i> {{ i18n.ts.editWidgetsExit }}</button>
2024-02-05 12:39:15 +01:00
<button v-else class="_textButton" data-cy-widget-edit :class="$style.edit" style="font-size: 0.9em;" @click="editMode = true"><i class="ph-pencil-simple ph-bold ph-lg"></i> {{ i18n.ts.editWidgets }}</button>
</div>
</template>
<script lang="ts">
import { computed, ref } from 'vue';
const editMode = ref(false);
</script>
<script lang="ts" setup>
import XWidgets from '@/components/MkWidgets.vue';
2023-09-19 09:37:43 +02:00
import { i18n } from '@/i18n.js';
import { defaultStore } from '@/store.js';
const props = withDefaults(defineProps<{
// null = 全てのウィジェットを表示
// left = place: leftだけを表示
// right = rightとnullを表示
place?: 'left' | null | 'right';
}>(), {
place: null,
});
const widgets = computed(() => {
if (props.place === null) return defaultStore.reactiveState.widgets.value;
if (props.place === 'left') return defaultStore.reactiveState.widgets.value.filter(w => w.place === 'left');
return defaultStore.reactiveState.widgets.value.filter(w => w.place !== 'left');
});
function addWidget(widget) {
defaultStore.set('widgets', [{
...widget,
place: props.place,
}, ...defaultStore.state.widgets]);
}
function removeWidget(widget) {
defaultStore.set('widgets', defaultStore.state.widgets.filter(w => w.id !== widget.id));
}
function updateWidget({ id, data }) {
defaultStore.set('widgets', defaultStore.state.widgets.map(w => w.id === id ? {
...w,
data,
place: props.place,
} : w));
}
function updateWidgets(thisWidgets) {
if (props.place === null) {
defaultStore.set('widgets', thisWidgets);
return;
}
if (props.place === 'left') {
defaultStore.set('widgets', [
...thisWidgets.map(w => ({ ...w, place: 'left' })),
...defaultStore.state.widgets.filter(w => w.place !== 'left' && !thisWidgets.some(t => w.id === t.id)),
]);
return;
}
defaultStore.set('widgets', [
...defaultStore.state.widgets.filter(w => w.place === 'left' && !thisWidgets.some(t => w.id === t.id)),
...thisWidgets.map(w => ({ ...w, place: 'right' })),
]);
}
</script>
2023-01-10 03:15:29 +01:00
<style lang="scss" module>
.edit {
width: 100%;
}
</style>