From d60931bae53433e861978423baa52564a8c18b79 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Mon, 26 Dec 2022 22:08:32 +0800 Subject: [PATCH] feat: add AnnotationsForm Component to edit extension annotations (#770) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind feature #### What this PR does / why we need it: 提供一个 Annotations 编辑组件,支持由主题或者插件提供表单定义,也支持使用者自定义 key-value,用于扩展资源字段。 #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/2858 #### Screenshots: image #### Special notes for your reviewer: 测试方式: 1. 将一下内容放到任意一个主题下,后缀为 `yaml`,文件名随意。 ```yaml spec: targetRef: group: content.halo.run kind: Post formSchema: - $formkit: "text" name: "download" label: "下载地址" - $formkit: "text" name: "version" label: "版本" apiVersion: v1alpha1 kind: AnnotationSetting metadata: generateName: annotation- ``` 3. 后端需要使用 https://github.com/halo-dev/halo/pull/3028 4. 测试为文章设置 Annotations 和自定义的 Annotations。 5. 检查是否可以设置正常。 #### Does this PR introduce a user-facing change? ```release-note 文章设置支持设置元数据。 ``` --- package.json | 3 +- pnpm-lock.yaml | 34 ++- src/components/form/AnnotationsForm.vue | 231 ++++++++++++++++++ .../posts/components/PostSettingModal.vue | 39 +++ src/utils/api-client.ts | 6 + 5 files changed, 308 insertions(+), 5 deletions(-) create mode 100644 src/components/form/AnnotationsForm.vue diff --git a/package.json b/package.json index 629ede92d..659965c70 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,9 @@ "@formkit/inputs": "^1.0.0-beta.12", "@formkit/themes": "^1.0.0-beta.12", "@formkit/utils": "^1.0.0-beta.12", + "@formkit/validation": "1.0.0-beta.12", "@formkit/vue": "^1.0.0-beta.12", - "@halo-dev/api-client": "^0.0.62", + "@halo-dev/api-client": "0.0.64", "@halo-dev/components": "workspace:*", "@halo-dev/console-shared": "workspace:*", "@halo-dev/richtext-editor": "^0.0.0-alpha.17", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ebee7f8b1..69c907844 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,8 +11,9 @@ importers: '@formkit/inputs': ^1.0.0-beta.12 '@formkit/themes': ^1.0.0-beta.12 '@formkit/utils': ^1.0.0-beta.12 + '@formkit/validation': 1.0.0-beta.12 '@formkit/vue': ^1.0.0-beta.12 - '@halo-dev/api-client': ^0.0.62 + '@halo-dev/api-client': 0.0.64 '@halo-dev/components': workspace:* '@halo-dev/console-shared': workspace:* '@halo-dev/richtext-editor': ^0.0.0-alpha.17 @@ -105,8 +106,9 @@ importers: '@formkit/inputs': 1.0.0-beta.12-e579559 '@formkit/themes': 1.0.0-beta.12-e579559_tailwindcss@3.2.4 '@formkit/utils': 1.0.0-beta.12-e579559 + '@formkit/validation': 1.0.0-beta.12 '@formkit/vue': 1.0.0-beta.12-e579559_ior6jr3fpijijuwpr34w2i25va - '@halo-dev/api-client': 0.0.62 + '@halo-dev/api-client': 0.0.64 '@halo-dev/components': link:packages/components '@halo-dev/console-shared': link:packages/shared '@halo-dev/richtext-editor': 0.0.0-alpha.17_vue@3.2.45 @@ -1874,6 +1876,12 @@ packages: '@floating-ui/core': 0.3.1 dev: false + /@formkit/core/1.0.0-beta.12: + resolution: {integrity: sha512-/Pod7k4N58eDOG+0LE0ccV7BOYD5sEA4RqaRNJn9m6Jg9nF/JATmSNL46kVCH3mV6sSoV045L3Zir8//rya0Lw==} + dependencies: + '@formkit/utils': 1.0.0-beta.12 + dev: false + /@formkit/core/1.0.0-beta.12-e579559: resolution: {integrity: sha512-y90ubMcFr6WtAjZqUOLgA3p4jm024f6R7iDRVKHsmdwSKm1/GZl4D+Yo2k8DL40xM7NbZhIvK40MiEC0pNZ3ig==} dependencies: @@ -1901,6 +1909,13 @@ packages: '@formkit/core': 1.0.0-beta.12-e579559 dev: false + /@formkit/observer/1.0.0-beta.12: + resolution: {integrity: sha512-kXVWUkjbNsymwv50QuI5thjlCknGy8azPlzepTDj3fsuZANuHG8qaj3W5h9mY6cCgvil4d4WeBz5f54iJsaEXA==} + dependencies: + '@formkit/core': 1.0.0-beta.12 + '@formkit/utils': 1.0.0-beta.12 + dev: false + /@formkit/observer/1.0.0-beta.12-e579559: resolution: {integrity: sha512-6Nki4VmUN3OyU70N6AIttvn/ScZXq5mwv11BIYLgfsfiMI5zoiflmNHrD7yf4KTUVvz16ThJB5nRGIXlnUnH3Q==} dependencies: @@ -1934,10 +1949,21 @@ packages: tailwindcss: 3.2.4_postcss@8.4.19 dev: false + /@formkit/utils/1.0.0-beta.12: + resolution: {integrity: sha512-L2P221teC+58Y1A4TZpJsLt/Y+WErLxbBt4hH6XhokssVX7mYR56YWienY9StN5jmiGVWtcK1+riXCb0n1aMOA==} + dev: false + /@formkit/utils/1.0.0-beta.12-e579559: resolution: {integrity: sha512-4yTz4IRzPX3G08aAO+ZfEuBYfy6OSgk3csx3TBhOcL6NzyIbJkTc2ZsxaUYXgY6FOVclazLGMKM5+jBitMOL5Q==} dev: false + /@formkit/validation/1.0.0-beta.12: + resolution: {integrity: sha512-ZSD5llsfSqXfIYMtSeeDwx7h421JCKobi00RxpGJkuHgpnAi3oxTJB7J6u+3pMrO9BqcidGtk5G/rk9F4fCNEw==} + dependencies: + '@formkit/core': 1.0.0-beta.12 + '@formkit/observer': 1.0.0-beta.12 + dev: false + /@formkit/validation/1.0.0-beta.12-e579559: resolution: {integrity: sha512-f/S0LefikeBZRxKkjx1fD7RODXMHglg6+B1NUYEKsEKc3ySjH/V9/XroaPszXfkupAnVBSTjZXG7T9hmn39VVA==} dependencies: @@ -1966,8 +1992,8 @@ packages: - windicss dev: false - /@halo-dev/api-client/0.0.62: - resolution: {integrity: sha512-LVoAH4/+8iHxqHf7X6Ax3wy+IRSB2Tm9IC3zhfGdnbrdgyn/NnnpRIhTCKo6cK0Sr2j/3W4Pvmc0xCWNVzNAyw==} + /@halo-dev/api-client/0.0.64: + resolution: {integrity: sha512-zzhTdRi4p7nRsWG7u85YCkoHZLHsbpuWeBCwLl5FSXxY8ruKuiOo0FypkS7Ll7505XASk4MJL6rsyeX+l7rLmQ==} dev: false /@halo-dev/richtext-editor/0.0.0-alpha.17_vue@3.2.45: diff --git a/src/components/form/AnnotationsForm.vue b/src/components/form/AnnotationsForm.vue new file mode 100644 index 000000000..2d640f3fd --- /dev/null +++ b/src/components/form/AnnotationsForm.vue @@ -0,0 +1,231 @@ + + + diff --git a/src/modules/contents/posts/components/PostSettingModal.vue b/src/modules/contents/posts/components/PostSettingModal.vue index a5184cfe9..d785d2e7b 100644 --- a/src/modules/contents/posts/components/PostSettingModal.vue +++ b/src/modules/contents/posts/components/PostSettingModal.vue @@ -8,6 +8,7 @@ import { useThemeCustomTemplates } from "@/modules/interface/themes/composables/ import { postLabels } from "@/constants/labels"; import { randomUUID } from "@/utils/id"; import { toDatetimeLocal, toISOString } from "@/utils/date"; +import AnnotationsForm from "@/components/form/AnnotationsForm.vue"; import { submitForm } from "@formkit/core"; const initialFormState: Post = { @@ -107,6 +108,21 @@ const handlePublishClick = () => { }; const handleSave = async () => { + annotationsFormRef.value?.handleSubmit(); + await nextTick(); + + const { customAnnotations, annotations, customFormInvalid, specFormInvalid } = + annotationsFormRef.value || {}; + + if (customFormInvalid || specFormInvalid) { + return; + } + + formState.value.metadata.annotations = { + ...annotations, + ...customAnnotations, + }; + if (props.onlyEmit) { emit("saved", formState.value); return; @@ -204,6 +220,8 @@ const publishTime = computed(() => { const onPublishTimeChange = (value: string) => { formState.value.spec.publishTime = value ? toISOString(value) : undefined; }; + +const annotationsFormRef = ref>();