index.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import { useRouter } from 'vue-router'
  18. import { defineComponent, onMounted, ref, toRefs } from 'vue'
  19. import { NButton, NForm, NFormItem, NSpace } from 'naive-ui'
  20. import { useI18n } from 'vue-i18n'
  21. import Card from '@/components/card'
  22. import MonacoEditor from '@/components/monaco-editor'
  23. import { useForm } from './use-form'
  24. import { useEdit } from './use-edit'
  25. import styles from '../index.module.scss'
  26. import type { Router } from 'vue-router'
  27. export default defineComponent({
  28. name: 'ResourceFileEdit',
  29. setup() {
  30. const router: Router = useRouter()
  31. const resourceViewRef = ref()
  32. const routeNameRef = ref(router.currentRoute.value.name)
  33. const idRef = ref(Number(router.currentRoute.value.params.id))
  34. const { state } = useForm()
  35. const { getResourceView, handleUpdateContent } = useEdit(state)
  36. const handleFileContent = () => {
  37. state.fileForm.content = resourceViewRef.value.content
  38. handleUpdateContent(idRef.value)
  39. }
  40. const handleReturn = () => {
  41. router.go(-1)
  42. }
  43. onMounted(() => {
  44. resourceViewRef.value = getResourceView(idRef.value)
  45. })
  46. return {
  47. idRef,
  48. routeNameRef,
  49. resourceViewRef,
  50. handleReturn,
  51. handleFileContent,
  52. ...toRefs(state)
  53. }
  54. },
  55. render() {
  56. const { t } = useI18n()
  57. return (
  58. <Card title={t('resource.file.file_details')}>
  59. <div class={styles['file-edit-content']}>
  60. <h2>
  61. <span>{this.resourceViewRef.value?.alias}</span>
  62. </h2>
  63. <NForm
  64. rules={this.rules}
  65. ref='fileFormRef'
  66. class={styles['form-content']}
  67. >
  68. <NFormItem path='content'>
  69. <div style={{ width: '90%' }}>
  70. <MonacoEditor
  71. v-model={[this.resourceViewRef.value.content, 'value']}
  72. />
  73. </div>
  74. </NFormItem>
  75. {this.routeNameRef === 'resource-file-edit' && (
  76. <NSpace>
  77. <NButton
  78. type='info'
  79. size='small'
  80. text
  81. style={{ marginRight: '15px' }}
  82. onClick={this.handleReturn}
  83. >
  84. {t('resource.file.return')}
  85. </NButton>
  86. <NButton
  87. type='info'
  88. size='small'
  89. round
  90. onClick={() => this.handleFileContent()}
  91. >
  92. {t('resource.file.save')}
  93. </NButton>
  94. </NSpace>
  95. )}
  96. </NForm>
  97. </div>
  98. </Card>
  99. )
  100. }
  101. })