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;
(ev: 'removeWidget', widget: Widget): void;
(ev: 'updateWidget', widget: Partial<Widget>): void;
(ev: 'exit'): void;
}>();
const widgetRefs = {};
const configWidget = (id: string) => {
widgetRefs[id].configure(); widgetRefs[id].configure();
}; };
const widgetAdderSelected = ref(null); const widgetAdderSelected = ref(null);
const addWidget = () => { const addWidget = () => {
if (widgetAdderSelected.value == null) return; if (widgetAdderSelected.value == null) return;
context.emit('addWidget', { emit('addWidget', {
name: widgetAdderSelected.value, name: widgetAdderSelected.value,
id: uuid(), id: uuid(),
data: {}, data: {},
}); });
widgetAdderSelected.value = null; widgetAdderSelected.value = null;
}; };
const removeWidget = (widget) => { const removeWidget = (widget) => {
context.emit('removeWidget', widget); emit('removeWidget', widget);
}; };
const updateWidget = (id, data) => { const updateWidget = (id, data) => {
context.emit('updateWidget', { id, data }); emit('updateWidget', { id, data });
}; };
const widgets_ = computed({ const widgets_ = computed({
get: () => props.widgets, get: () => props.widgets,
set: (value) => { set: (value) => {
context.emit('updateWidgets', value); emit('updateWidgets', value);
},
});
return {
widgetRefs,
configWidget,
widgetAdderSelected,
widgetDefs,
addWidget,
removeWidget,
updateWidget,
widgets_,
};
}, },
}); });
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>