ConfigEdit.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <a-card title="配置文件实时编辑">
  3. <a-textarea v-model="configText" :rows="36" @keydown.tab.prevent="pressTab"/>
  4. <footer-tool-bar>
  5. <a-button type="primary" @click="save">保存</a-button>
  6. </footer-tool-bar>
  7. </a-card>
  8. </template>
  9. <script>
  10. import FooterToolBar from "@/components/FooterToolbar/FooterToolBar"
  11. export default {
  12. name: "DomainEdit",
  13. components: {FooterToolBar},
  14. data() {
  15. return {
  16. name: this.$route.params.name,
  17. configText: ""
  18. }
  19. },
  20. watch: {
  21. $route() {
  22. this.config = {}
  23. this.configText = ""
  24. },
  25. config: {
  26. handler() {
  27. this.unparse()
  28. },
  29. deep: true
  30. }
  31. },
  32. created() {
  33. if (this.name) {
  34. this.$api.config.get(this.name).then(r => {
  35. this.configText = r.config
  36. }).catch(r => {
  37. console.log(r)
  38. this.$message.error("服务器错误")
  39. })
  40. } else {
  41. this.configText = ""
  42. }
  43. },
  44. methods: {
  45. save() {
  46. this.$api.config.save(this.name ? this.name : this.config.name, {content: this.configText}).then(r => {
  47. this.configText = r.config
  48. this.$message.success("保存成功")
  49. }).catch(r => {
  50. console.log(r)
  51. this.$message.error("保存错误")
  52. })
  53. },
  54. pressTab(event) {
  55. let target = event.target
  56. let value = target.value
  57. let start = target.selectionStart;
  58. let end = target.selectionEnd;
  59. if (event) {
  60. value = value.substring(0, start) + '\t' + value.substring(end);
  61. event.target.value = value;
  62. setTimeout(() => target.selectionStart = target.selectionEnd = start + 1, 0);
  63. }
  64. }
  65. }
  66. }
  67. </script>
  68. <style lang="less" scoped>
  69. .ant-card {
  70. margin: 10px;
  71. @media (max-width: 512px) {
  72. margin: 10px 0;
  73. }
  74. }
  75. </style>