index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { App } from 'vue'
  3. import { plugin as formPlugin, bindings as bindingsPlugin } from '@formkit/vue'
  4. import type { FormKitConfig, FormKitPlugin } from '@formkit/core'
  5. import { createThemePlugin } from '@formkit/themes'
  6. import '@formkit/dev'
  7. import type {
  8. FormAppSpecificTheme,
  9. FormDecoratorIcons,
  10. FormFieldTypeImportModules,
  11. } from '#shared/types/form.ts'
  12. import type {
  13. ImportGlobEagerOutput,
  14. ImportGlobEagerDefault,
  15. } from '#shared/types/utils.ts'
  16. import createFieldPlugin from './core/createFieldPlugin.ts'
  17. import createValidationPlugin from './core/createValidationPlugin.ts'
  18. import createI18nPlugin from './core/createI18nPlugin.ts'
  19. import createTailwindClasses from './core/createTailwindClasses.ts'
  20. import createCustomIcons from './core/createCustomIcons.ts'
  21. export const getFormPlugins = (
  22. modules: ImportGlobEagerOutput<FormKitPlugin>,
  23. ): FormKitPlugin[] => {
  24. const plugins: Array<FormKitPlugin> = []
  25. Object.values(modules).forEach(
  26. (module: ImportGlobEagerDefault<FormKitPlugin>) => {
  27. const plugin = module.default
  28. plugins.push(plugin)
  29. },
  30. )
  31. return plugins
  32. }
  33. const pluginModules: ImportGlobEagerOutput<FormKitPlugin> = import.meta.glob(
  34. './plugins/global/*.ts',
  35. { eager: true },
  36. )
  37. const plugins = getFormPlugins(pluginModules)
  38. const checkIcon =
  39. '<svg width="16" height="16" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="fill-current"><path d="M20.8087 5.58815L11.3542 18.5881C11.1771 18.8316 10.8998 18.9824 10.5992 18.9985C10.2986 19.0147 10.0067 18.8946 9.80449 18.6715L3.25903 11.4493L4.74097 10.1062L10.4603 16.4169L19.1913 4.4118L20.8087 5.58815Z" /></svg>'
  40. export const buildFormKitPluginConfig = (
  41. config?: FormKitConfig,
  42. appSpecificFieldModules: ImportGlobEagerOutput<FormFieldTypeImportModules> = {},
  43. appSpecificPlugins: FormKitPlugin[] = [],
  44. appSpecificTheme: FormAppSpecificTheme = {},
  45. appSpecificDecoratorIcons: FormDecoratorIcons = {},
  46. ) => {
  47. const customIcons = createCustomIcons()
  48. return {
  49. plugins: [
  50. createFieldPlugin(appSpecificFieldModules),
  51. bindingsPlugin,
  52. createI18nPlugin(),
  53. createValidationPlugin(),
  54. createThemePlugin(
  55. undefined,
  56. {
  57. checkboxDecorator:
  58. appSpecificDecoratorIcons.checkboxDecorator || checkIcon,
  59. radioDecorator: appSpecificDecoratorIcons.radioDecorator || checkIcon,
  60. ...customIcons,
  61. },
  62. undefined,
  63. () => undefined,
  64. ),
  65. ...plugins,
  66. ...appSpecificPlugins,
  67. ],
  68. locale: 'staticLocale',
  69. config: {
  70. classes: createTailwindClasses(appSpecificTheme),
  71. ...config,
  72. },
  73. props: {
  74. dirtyBehavior: 'compare',
  75. },
  76. }
  77. }
  78. export default function initializeForm(
  79. app: App,
  80. appSpecificConfig?: FormKitConfig,
  81. appSpecificFieldModules: ImportGlobEagerOutput<FormFieldTypeImportModules> = {},
  82. appSpecificPlugins: FormKitPlugin[] = [],
  83. appSpecificTheme: FormAppSpecificTheme = {},
  84. appSpecificDecoratorIcons: FormDecoratorIcons = {},
  85. ) {
  86. app.use(
  87. formPlugin,
  88. buildFormKitPluginConfig(
  89. appSpecificConfig,
  90. appSpecificFieldModules,
  91. appSpecificPlugins,
  92. appSpecificTheme,
  93. appSpecificDecoratorIcons,
  94. ),
  95. )
  96. }