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

99 lines
2.7 KiB
Vue
Raw Normal View History

<template>
2023-01-10 03:15:29 +01:00
<div :class="$style.root" :style="{ paddingTop: marginTop }">
2023-05-19 13:52:15 +02:00
<XWidgets :class="$style.widgets" :edit="editMode" :widgets="widgets" @addWidget="addWidget" @removeWidget="removeWidget" @updateWidget="updateWidget" @updateWidgets="updateWidgets" @exit="editMode = false"/>
<button v-if="editMode" class="_textButton" style="font-size: 0.9em;" @click="editMode = false"><i class="ti ti-check"></i> {{ i18n.ts.editWidgetsExit }}</button>
2023-05-09 00:49:53 +02:00
<button v-else class="_textButton" data-cy-widget-edit :class="$style.edit" style="font-size: 0.9em;" @click="editMode = true"><i class="ti ti-pencil"></i> {{ i18n.ts.editWidgets }}</button>
</div>
</template>
<script lang="ts">
let editMode = $ref(false);
</script>
<script lang="ts" setup>
import { onMounted } from 'vue';
import XWidgets from '@/components/MkWidgets.vue';
import { i18n } from '@/i18n';
import { defaultStore } from '@/store';
const props = withDefaults(defineProps<{
// null = 全てのウィジェットを表示
// left = place: leftだけを表示
// right = rightとnullを表示
place?: 'left' | null | 'right';
2023-01-10 03:15:29 +01:00
marginTop?: string;
}>(), {
place: null,
2023-01-10 03:15:29 +01:00
marginTop: '0',
});
const emit = defineEmits<{
(ev: 'mounted', el?: Element): void;
}>();
let rootEl = $shallowRef<HTMLDivElement>();
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');
});
onMounted(() => {
emit('mounted', rootEl);
});
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>
.root {
position: sticky;
height: min-content;
box-sizing: border-box;
2023-01-10 03:15:29 +01:00
}
2023-01-10 03:15:29 +01:00
.widgets {
width: 300px;
}
.edit {
width: 100%;
}
</style>