支持为资源设置元数据(metadata.annotations)
你当前使用的版本
2.0.0
描述一下此特性
期望为资源提供元数据的设置功能,类似于 1.x 文章的 metas 字段,此功能可以非常方便的为某一个特定的资源扩展字段。但需要考虑以下问题:
- 是否需要支持主题或者插件提供表单定义,因为大部分情况下,主题需要得到这些元数据去完成特定的功能。
- key 是否有命名要求。
- 哪些资源需要这个功能,比如文章、自定义页面、分类。
/area core /area console /kind feature
附加信息
No response
/milestone 2.1.x
关于如何通过主题进行 annotations 表单的定义,我的想法:
- 在
theme.yaml中提供customAnnotations的配置,用于配置各个资源的扩展表单(annotations) - Console 端拿到这个配置进行表单渲染,将表单的 value 值绑定到
metadata.annotations。
比如:
apiVersion: theme.halo.run/v1alpha1
kind: Theme
metadata:
name: theme-earth
spec:
displayName: Earth
author:
name: halo-dev
website: https://halo.run
description: Default theme for Halo 2.0
logo: https://halo.run/logo
website: https://github.com/halo-dev/theme-earth
repo: https://github.com/halo-dev/theme-earth.git
settingName: "theme-earth-setting"
configMapName: "theme-earth-configMap"
customAnnotations:
post:
- $formkit: text
name: download_url
label: 下载链接
- $formkit: select
name: version
label: 支持的版本
options:
- value: "1.5"
label: "1.5"
- value: "2.0"
label: "2.0"
menuItem:
- $formkit: text
name: icon
label: 图标
version: 1.0.0
require: 2.0.0
效果:
其他非主题定义的表单项,会以 Key Value 的形式单独提供表单项,且可以由使用者自行定义 Key。
/cc @halo-dev/sig-halo any ideas?
/cc @halo-dev/sig-halo any ideas?
我认为可行
在尝试实现的时候遇到一个问题,annotations 的类型是一个键值都为 String 的对象,这就意味着如果使用 FormKit 的 Schema 来构建表单的话,一些复杂类型的表单元素(比如 repeater)就无法使用。不能够像 ConfigMap 一样将整个 JSON 对象转为字符串 JSON。如果一定要支持保存复杂类型的表单元素,只能对单个对象元素的值进行处理,但后端似乎也无法序列化。
增加 AnnotationSetting 自定义模型用于保存模型 annotations 的 form schema,可以在使用时通过 targetRef 查询到一些列 fromSchemas 进行特定资源扩展设置的表单渲染。
apiVersion: v1alpha1
kind: AnnotationSetting
metadata:
name: a-name
labels:
# plugin.halo.run/plugin-name: fake-plugin
theme.halo.run/theme-name: fake-theme
halo.run/target-ref: posts.content.halo.run
spec:
targetRef:
group: content.halo.run
kind: Post
formSchema:
- $formkit: text
name: fancy.halo.run/download_url
label: 下载链接
- $formkit: select
name: fancy.halo.run/version
label: 支持的版本
options:
- value: "1.5"
label: "1.5"
- value: "2.0"
label: "2.0"
如果为主题创建的用于文章使用的 annotations setting,文章使用时最终通过以上 setting 渲染的表单填写值后存储在 annotations 示例如下:
apiVersion: content.halo.run/v1alpha1
kind: Post
metadata:
annotations:
fancy.halo.run/download_url: https://example.com/fake
fancy.halo.run/version: "[\"1.5\", \"2.0\"]"
creationTimestamp: '2022-12-15T09:45:36.089125Z'
name: fake-post
spec:
slug: hello-halo
title: Hello Halo
visible: PUBLIC
/cc @halo-dev/sig-halo