ConfigEdit.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <script setup lang="ts">
  2. import FooterToolBar from '@/components/FooterToolbar/FooterToolBar.vue'
  3. import gettext from '@/gettext'
  4. import {useRoute} from 'vue-router'
  5. import {computed, ref} from 'vue'
  6. import config from '@/api/config'
  7. import {message} from 'ant-design-vue'
  8. import CodeEditor from '@/components/CodeEditor/CodeEditor.vue'
  9. import ngx from '@/api/ngx'
  10. import InspectConfig from '@/views/config/InspectConfig.vue'
  11. const {$gettext, interpolate} = gettext
  12. const route = useRoute()
  13. const inspect_config = ref()
  14. const name = computed(() => {
  15. const n = route.params.name
  16. if (typeof n === 'string') {
  17. return n
  18. }
  19. return n?.join('/')
  20. })
  21. const configText = ref('')
  22. function init() {
  23. if (name.value) {
  24. config.get(name.value).then(r => {
  25. configText.value = r.config
  26. }).catch(r => {
  27. message.error(r.message ?? $gettext('Server error'))
  28. })
  29. } else {
  30. configText.value = ''
  31. }
  32. }
  33. init()
  34. function save() {
  35. config.save(name.value, {content: configText.value}).then(r => {
  36. configText.value = r.config
  37. message.success($gettext('Saved successfully'))
  38. }).catch(r => {
  39. message.error(interpolate($gettext('Save error %{msg}'), {msg: r.message ?? ''}))
  40. }).finally(() => {
  41. inspect_config.value.test()
  42. })
  43. }
  44. function format_code() {
  45. ngx.format_code(configText.value).then(r => {
  46. configText.value = r.content
  47. message.success($gettext('Format successfully'))
  48. }).catch(r => {
  49. message.error(interpolate($gettext('Format error %{msg}'), {msg: r.message ?? ''}))
  50. })
  51. }
  52. </script>
  53. <template>
  54. <inspect-config ref="inspect_config"/>
  55. <a-card :title="$gettext('Edit Configuration')">
  56. <code-editor v-model:content="configText"/>
  57. <footer-tool-bar>
  58. <a-space>
  59. <a-button @click="$router.go(-1)">
  60. <translate>Back</translate>
  61. </a-button>
  62. <a-button @click="format_code">
  63. <translate>Format Code</translate>
  64. </a-button>
  65. <a-button type="primary" @click="save">
  66. <translate>Save</translate>
  67. </a-button>
  68. </a-space>
  69. </footer-tool-bar>
  70. </a-card>
  71. </template>
  72. <style lang="less" scoped>
  73. </style>