index.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (C) 2012-2023 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 '@formkit/dev'
  6. import type {
  7. FormAppSpecificTheme,
  8. FormFieldTypeImportModules,
  9. } from '@shared/types/form'
  10. import type {
  11. ImportGlobEagerOutput,
  12. ImportGlobEagerDefault,
  13. } from '@shared/types/utils'
  14. import createFieldPlugin from './core/createFieldPlugin'
  15. import createValidationPlugin from './core/createValidationPlugin'
  16. import createI18nPlugin from './core/createI18nPlugin'
  17. import createTailwindClasses from './core/createTailwindClasses'
  18. export const getFormPlugins = (
  19. modules: ImportGlobEagerOutput<FormKitPlugin>,
  20. ): FormKitPlugin[] => {
  21. const plugins: Array<FormKitPlugin> = []
  22. Object.values(modules).forEach(
  23. (module: ImportGlobEagerDefault<FormKitPlugin>) => {
  24. const plugin = module.default
  25. plugins.push(plugin)
  26. },
  27. )
  28. return plugins
  29. }
  30. const pluginModules: ImportGlobEagerOutput<FormKitPlugin> = import.meta.glob(
  31. './plugins/global/*.ts',
  32. { eager: true },
  33. )
  34. const plugins = getFormPlugins(pluginModules)
  35. export const buildFormKitPluginConfig = (
  36. config?: FormKitConfig,
  37. appSpecificFieldModules: ImportGlobEagerOutput<FormFieldTypeImportModules> = {},
  38. appSpecificPlugins: FormKitPlugin[] = [],
  39. appSpecificTheme: FormAppSpecificTheme = {},
  40. ) => {
  41. return {
  42. plugins: [
  43. createFieldPlugin(appSpecificFieldModules),
  44. bindingsPlugin,
  45. createI18nPlugin(),
  46. createValidationPlugin(),
  47. ...plugins,
  48. ...appSpecificPlugins,
  49. ],
  50. locale: 'staticLocale',
  51. config: {
  52. classes: createTailwindClasses(appSpecificTheme),
  53. ...config,
  54. },
  55. }
  56. }
  57. export default function initializeForm(
  58. app: App,
  59. appSpecificConfig?: FormKitConfig,
  60. appSpecificFieldModules: ImportGlobEagerOutput<FormFieldTypeImportModules> = {},
  61. appSpecificPlugins: FormKitPlugin[] = [],
  62. appSpecificTheme: FormAppSpecificTheme = {},
  63. ) {
  64. app.use(
  65. formPlugin,
  66. buildFormKitPluginConfig(
  67. appSpecificConfig,
  68. appSpecificFieldModules,
  69. appSpecificPlugins,
  70. appSpecificTheme,
  71. ),
  72. )
  73. }