feat: add shortcut support for role editing form

Signed-off-by: Ryan Wang <i@ryanc.cc>
pull/591/head
Ryan Wang 2022-07-27 15:05:54 +08:00
parent 7094f3ebaa
commit bb25524c0a
2 changed files with 46 additions and 11 deletions

View File

@ -1,7 +1,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { VButton, VModal, VTabItem, VTabs } from "@halo-dev/components"; import { VButton, VModal, VSpace, VTabItem, VTabs } from "@halo-dev/components";
import type { PropType } from "vue"; import type { PropType } from "vue";
import { ref, watch } from "vue"; import { computed, ref, watch } from "vue";
import { rbacAnnotations } from "@/constants/annotations"; import { rbacAnnotations } from "@/constants/annotations";
import type { Role } from "@halo-dev/api-client"; import type { Role } from "@halo-dev/api-client";
import { import {
@ -9,6 +9,8 @@ import {
useRoleTemplateSelection, useRoleTemplateSelection,
} from "@/modules/system/roles/composables/use-role"; } from "@/modules/system/roles/composables/use-role";
import cloneDeep from "lodash.clonedeep"; import cloneDeep from "lodash.clonedeep";
import { submitForm } from "@formkit/core";
import { useMagicKeys } from "@vueuse/core";
const props = defineProps({ const props = defineProps({
visible: { visible: {
@ -25,8 +27,14 @@ const emit = defineEmits(["update:visible", "close"]);
const { roleTemplateGroups, handleRoleTemplateSelect, selectedRoleTemplates } = const { roleTemplateGroups, handleRoleTemplateSelect, selectedRoleTemplates } =
useRoleTemplateSelection(); useRoleTemplateSelection();
const { formState, initialFormState, saving, handleCreateOrUpdate } =
useRoleForm(); const {
formState,
isUpdateMode,
initialFormState,
saving,
handleCreateOrUpdate,
} = useRoleForm();
watch( watch(
() => selectedRoleTemplates.value, () => selectedRoleTemplates.value,
@ -39,15 +47,37 @@ watch(
); );
watch(props, (newVal) => { watch(props, (newVal) => {
const { Command_Enter } = useMagicKeys();
let keyboardWatcher;
if (newVal.visible) {
keyboardWatcher = watch(Command_Enter, (v) => {
if (v) {
submitForm("role-form");
}
});
} else {
keyboardWatcher?.unwatch();
}
if (newVal.visible && props.role) { if (newVal.visible && props.role) {
formState.value = cloneDeep(props.role); formState.value = cloneDeep(props.role);
const dependencies =
props.role.metadata.annotations?.[rbacAnnotations.DEPENDENCIES];
if (dependencies) {
selectedRoleTemplates.value = new Set(JSON.parse(dependencies));
}
return; return;
} }
formState.value = cloneDeep(initialFormState); formState.value = cloneDeep(initialFormState);
selectedRoleTemplates.value.clear();
}); });
const tabActiveId = ref("general"); const tabActiveId = ref("general");
const editingModalTitle = computed(() => {
return isUpdateMode.value ? "编辑角色" : "创建角色";
});
const handleCreateOrUpdateRole = async () => { const handleCreateOrUpdateRole = async () => {
try { try {
await handleCreateOrUpdate(); await handleCreateOrUpdate();
@ -68,7 +98,7 @@ const handleVisibleChange = (visible: boolean) => {
<VModal <VModal
:visible="visible" :visible="visible"
:width="700" :width="700"
title="创建角色" :title="editingModalTitle"
@update:visible="handleVisibleChange" @update:visible="handleVisibleChange"
> >
<VTabs v-model:active-id="tabActiveId" type="outline"> <VTabs v-model:active-id="tabActiveId" type="outline">
@ -157,12 +187,16 @@ const handleVisibleChange = (visible: boolean) => {
</VTabItem> </VTabItem>
</VTabs> </VTabs>
<template #footer> <template #footer>
<VSpace>
<VButton <VButton
:loading="saving" :loading="saving"
type="secondary" type="secondary"
@click="$formkit.submit('role-form')" @click="$formkit.submit('role-form')"
>创建 >
提交 +
</VButton> </VButton>
<VButton @click="handleVisibleChange(false)"> Esc</VButton>
</VSpace>
</template> </template>
</VModal> </VModal>
</template> </template>

View File

@ -53,6 +53,7 @@ export function useRoleForm() {
formState, formState,
initialFormState, initialFormState,
saving, saving,
isUpdateMode,
handleCreateOrUpdate, handleCreateOrUpdate,
}; };
} }