enhance(client): improve widgets component

This commit is contained in:
syuilo 2022-07-11 23:36:39 +09:00
parent a5648fb07f
commit d8264b11e2
1 changed files with 70 additions and 58 deletions

View File

@ -4,7 +4,7 @@
<header> <header>
<MkSelect v-model="widgetAdderSelected" style="margin-bottom: var(--margin)" class="mk-widget-select"> <MkSelect v-model="widgetAdderSelected" style="margin-bottom: var(--margin)" class="mk-widget-select">
<template #label>{{ $ts.selectWidget }}</template> <template #label>{{ $ts.selectWidget }}</template>
<option v-for="widget in widgetDefs" :key="widget" :value="widget">{{ $t(`_widgets.${widget}`) }}</option> <option v-for="widget in widgetDefs" :key="widget" :value="widget">{{ i18n.t(`_widgets.${widget}`) }}</option>
</MkSelect> </MkSelect>
<MkButton inline primary class="mk-widget-add" @click="addWidget"><i class="fas fa-plus"></i> {{ $ts.add }}</MkButton> <MkButton inline primary class="mk-widget-add" @click="addWidget"><i class="fas fa-plus"></i> {{ $ts.add }}</MkButton>
<MkButton inline @click="$emit('exit')">{{ $ts.close }}</MkButton> <MkButton inline @click="$emit('exit')">{{ $ts.close }}</MkButton>
@ -26,79 +26,91 @@
</template> </template>
</XDraggable> </XDraggable>
</template> </template>
<component :is="`mkw-${widget.name}`" v-for="widget in widgets" v-else :key="widget.id" class="widget" :widget="widget" @updateProps="updateWidget(widget.id, $event)"/> <component :is="`mkw-${widget.name}`" v-for="widget in widgets" v-else :key="widget.id" :ref="el => widgetRefs[widget.id] = el" class="widget" :widget="widget" @updateProps="updateWidget(widget.id, $event)" @contextmenu.stop="onContextmenu(widget, $event)"/>
</div> </div>
</template> </template>
<script lang="ts"> <script lang="ts" setup>
import { defineComponent, defineAsyncComponent, reactive, ref, computed } from 'vue'; import { defineAsyncComponent, reactive, ref, computed } from 'vue';
import { v4 as uuid } from 'uuid'; import { v4 as uuid } from 'uuid';
import MkSelect from '@/components/form/select.vue'; import MkSelect from '@/components/form/select.vue';
import MkButton from '@/components/ui/button.vue'; import MkButton from '@/components/ui/button.vue';
import { widgets as widgetDefs } from '@/widgets'; import { widgets as widgetDefs } from '@/widgets';
import * as os from '@/os';
import { i18n } from '@/i18n';
export default defineComponent({ const XDraggable = defineAsyncComponent(() => import('vuedraggable'));
components: {
XDraggable: defineAsyncComponent(() => import('vuedraggable')),
MkSelect,
MkButton,
},
props: { type Widget = {
widgets: { name: string;
type: Array, id: string;
required: true, data: Record<string, any>;
}, };
edit: {
type: Boolean,
required: true,
},
},
emits: ['updateWidgets', 'addWidget', 'removeWidget', 'updateWidget', 'exit'], const props = defineProps<{
widgets: Widget[];
edit: boolean;
}>();
setup(props, context) { const emit = defineEmits<{
const widgetRefs = reactive({}); (ev: 'updateWidgets', widgets: Widget[]): void;
const configWidget = (id: string) => { (ev: 'addWidget', widget: Widget): void;
widgetRefs[id].configure(); (ev: 'removeWidget', widget: Widget): void;
}; (ev: 'updateWidget', widget: Partial<Widget>): void;
const widgetAdderSelected = ref(null); (ev: 'exit'): void;
const addWidget = () => { }>();
if (widgetAdderSelected.value == null) return;
context.emit('addWidget', { const widgetRefs = {};
name: widgetAdderSelected.value, const configWidget = (id: string) => {
id: uuid(), widgetRefs[id].configure();
data: {}, };
}); const widgetAdderSelected = ref(null);
const addWidget = () => {
if (widgetAdderSelected.value == null) return;
widgetAdderSelected.value = null; emit('addWidget', {
}; name: widgetAdderSelected.value,
const removeWidget = (widget) => { id: uuid(),
context.emit('removeWidget', widget); data: {},
}; });
const updateWidget = (id, data) => {
context.emit('updateWidget', { id, data });
};
const widgets_ = computed({
get: () => props.widgets,
set: (value) => {
context.emit('updateWidgets', value);
},
});
return { widgetAdderSelected.value = null;
widgetRefs, };
configWidget, const removeWidget = (widget) => {
widgetAdderSelected, emit('removeWidget', widget);
widgetDefs, };
addWidget, const updateWidget = (id, data) => {
removeWidget, emit('updateWidget', { id, data });
updateWidget, };
widgets_, const widgets_ = computed({
}; get: () => props.widgets,
set: (value) => {
emit('updateWidgets', value);
}, },
}); });
function onContextmenu(widget: Widget, ev: MouseEvent) {
const isLink = (el: HTMLElement) => {
if (el.tagName === 'A') return true;
if (el.parentElement) {
return isLink(el.parentElement);
}
};
if (isLink(ev.target)) return;
if (['INPUT', 'TEXTAREA', 'IMG', 'VIDEO', 'CANVAS'].includes(ev.target.tagName) || ev.target.attributes['contenteditable']) return;
if (window.getSelection()?.toString() !== '') return;
os.contextMenu([{
type: 'label',
text: i18n.t(`_widgets.${widget.name}`),
}, {
icon: 'fas fa-cog',
text: i18n.ts.settings,
action: () => {
configWidget(widget.id);
},
}], ev);
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>