Skip to content

Commit

Permalink
Refactor HTML Injection Form Component (#12)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind improvement
#### What this PR does / why we need it:
原来的注入表单组件`HtmlInjectionAdd`把新增和更改的逻辑混在一起了,
该PR删除了该组件并拆分成`HtmlInjectionForm`,`HtmlInjectionCreationModal`和`HtmlInjectionEditionModal`分别用于渲染表单,新增注入和修改注入
#### Which issue(s) this PR fixes:
Fixes #7 
#### Does this PR introduce a user-facing change?
```release-note
None
```
  • Loading branch information
CaiHaosen authored Sep 3, 2024
1 parent a62ffed commit ea5d2d7
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 193 deletions.
154 changes: 0 additions & 154 deletions ui/src/views/HtmlInjectionAdd.vue

This file was deleted.

58 changes: 58 additions & 0 deletions ui/src/views/HtmlInjectionCreationModal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<script setup lang="ts">
import HtmlInjectionForm from './HtmlInjectionForm.vue';
import {axiosInstance} from "@halo-dev/api-client";
import {VButton, VModal, VSpace} from '@halo-dev/components';
import type {HtmlInjection, HtmlInjectionSpec} from "@/types";
const props = defineProps<{
htmlInjection: HtmlInjection | null;
}>();
const emit = defineEmits(['close', 'success']);
function closeModal() {
emit('close');
}
function handleFormSubmit(formData: HtmlInjectionSpec) {
const requestData: HtmlInjection = {
apiVersion: 'theme.halo.run/v1alpha1',
kind: 'HtmlInjection',
metadata: {
name: '',
generateName: 'htmlinjection-',
},
spec: formData,
};
axiosInstance.post('/apis/theme.halo.run/v1alpha1/htmlinjections', requestData)
.then(() => {
emit('success');
closeModal();
})
.catch(error => {
console.error('Error creating html injection:', error);
});
}
</script>

<template>
<VModal
:title="'新增代码注入'"
:width="700"
@close="closeModal"
>
<HtmlInjectionForm
:htmlInjection="props.htmlInjection"
@submit="handleFormSubmit"/>
<template #footer>
<VSpace>
<!-- @vue-ignore -->
<VButton type="secondary" @click="$formkit.submit('html-injection-form')">保存</VButton>
<VButton @click="closeModal">取消</VButton>
</VSpace>
</template>
</VModal>
</template>
<style>
@import "@halo-dev/components/dist/style.css";
</style>
52 changes: 52 additions & 0 deletions ui/src/views/HtmlInjectionEditionModal.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script setup lang="ts">
import HtmlInjectionForm from './HtmlInjectionForm.vue';
import {axiosInstance} from "@halo-dev/api-client";
import {VButton, VModal, VSpace} from '@halo-dev/components';
import type {HtmlInjection, HtmlInjectionSpec} from "@/types";
const props = defineProps<{
htmlInjection: HtmlInjection | null;
}>();
const emit = defineEmits(['close', 'success']);
function closeModal() {
emit('close');
}
function handleFormSubmit(formData: HtmlInjectionSpec) {
const htmlInjection = props.htmlInjection as HtmlInjection;
const requestData: HtmlInjection = {...htmlInjection, spec: formData};
axiosInstance.put(`/apis/theme.halo.run/v1alpha1/htmlinjections/${htmlInjection.metadata?.name}`, requestData)
.then(() => {
emit('success');
closeModal();
})
.catch(error => {
console.error('Error updating html injection:', error);
});
}
</script>

<template>
<VModal
:title="'编辑代码注入'"
:width="700"
@close="closeModal"
>
<HtmlInjectionForm
:htmlInjection="props.htmlInjection"
@submit="handleFormSubmit"/>
<template #footer>
<VSpace>
<!-- @vue-ignore -->
<VButton type="secondary" @click="$formkit.submit('html-injection-form')">保存</VButton>
<VButton @click="closeModal">取消</VButton>
</VSpace>
</template>
</VModal>
</template>
<style>
@import "@halo-dev/components/dist/style.css";
</style>
88 changes: 88 additions & 0 deletions ui/src/views/HtmlInjectionForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<script setup lang="ts">
import {ref, watch} from 'vue';
import type {HtmlInjection, HtmlInjectionSpec} from "@/types";
import {cloneDeep} from "lodash-es";
const props = defineProps<{
htmlInjection: HtmlInjection | null;
}>();
const emit = defineEmits<{
(event: "submit", data: HtmlInjectionSpec): void;
}>();
// 初始化表单数据
const initialFormData = {
name: '',
description: '',
fragment: '',
injectionPoint: 'HEADER' as 'HEADER' | 'FOOTER',
pageRules: [] as string[],
enabled: false,
};
const formData = ref(cloneDeep(initialFormData));
// 更新表单数据
const updateFormData = (currentHtmlInjection: HtmlInjection | null) => {
if (currentHtmlInjection) {
formData.value = {
name: currentHtmlInjection.spec.name,
description: currentHtmlInjection.spec.description,
fragment: currentHtmlInjection.spec.fragment,
injectionPoint: currentHtmlInjection.spec.injectionPoint,
pageRules: currentHtmlInjection.spec.pageRules,
enabled: currentHtmlInjection.spec.enabled,
};
}
};
// 监听 props.htmlInjection 的变化并相应更新 formData
watch(
() => props.htmlInjection,
updateFormData,
{immediate: true}
);
// 提交表单
function onSubmit(formData: HtmlInjectionSpec) {
// 触发 submit 事件并传递表单数据
emit('submit', formData);
}
</script>

<template>
<FormKit
type="form"
id="html-injection-form"
name="html-injection-form"
v-model="formData"
:config="{ validationVisibility: 'submit'}"
:actions="false"
@submit="onSubmit"
>
<FormKit id="name" name="name" v-model="formData.name" :label="'名称'" type="text"
validation="required|length:1,100" :placeholder="'请输入名称'"/>
<FormKit id="description" name="description" v-model="formData.description" :label="'代码描述'" type="textarea"
validation="length:0,500" :placeholder="'请输入代码描述'"/>
<FormKit id="injectionPoint" name="injectionPoint" v-model="formData.injectionPoint" :label="'注入点'" type="select"
:options="[
{ value: 'HEADER', label: 'Header' },
{ value: 'FOOTER', label: 'Footer' }
]"
/>
<FormKit id="pageRules" name="pageRules" v-model="formData.pageRules" :label="'页面匹配规则'" validation="required"
type="list" item-type="string" add-label="添加">
<template #default="{ index }">
<FormKit type="text" :index="index" help="用于匹配页面路径的符合 Ant-style 的表达式,如:/archives/**"/>
</template>
</FormKit>
<FormKit id="enabled" name="enabled" v-model="formData.enabled" :label="'启用'" type="checkbox"/>
<FormKit id="fragment" name="fragment" v-model="formData.fragment" :label="'代码'" type="code" :language="'html'"
:height="'200px'"/>
</FormKit>
</template>

<style>
@import "@halo-dev/components/dist/style.css";
</style>
Loading

0 comments on commit ea5d2d7

Please sign in to comment.