JsonPreview.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <el-container class="_fc-json-preview">
  3. <el-header height="40px" class="_fc-l-tabs">
  4. <div class="_fc-l-tab"
  5. :class="{active: active==='rule'}"
  6. @click="active='rule'"> {{ t('designer.json') }}
  7. </div>
  8. <div class="_fc-l-tab"
  9. :class="{active: active==='options'}"
  10. @click="active='options'"> {{ t('designer.form') }}
  11. </div>
  12. </el-header>
  13. <el-main style="padding: 8px;">
  14. <StructEditor ref="editor" v-model="value" @blur="handleBlur" @focus="handleFocus" format
  15. style="height:100%;"></StructEditor>
  16. </el-main>
  17. </el-container>
  18. </template>
  19. <script>
  20. import {defineComponent} from 'vue';
  21. import StructEditor from './StructEditor.vue';
  22. import {designerForm} from '../utils/form';
  23. export default defineComponent({
  24. name: 'JsonPreview',
  25. components: {StructEditor},
  26. inject: ['designer'],
  27. data() {
  28. return {
  29. active: 'rule',
  30. value: this.designer.getRule(),
  31. oldValue: '',
  32. }
  33. },
  34. watch: {
  35. active() {
  36. this.updateValue();
  37. }
  38. },
  39. computed: {
  40. change() {
  41. if (this.active === 'rule') {
  42. return this.designer.children;
  43. } else {
  44. return this.designer.formOptions;
  45. }
  46. },
  47. t() {
  48. return this.designer.t;
  49. },
  50. },
  51. methods: {
  52. updateValue() {
  53. if (this.active === 'rule') {
  54. this.value = this.designer.getRule();
  55. } else {
  56. this.value = this.designer.getOptions();
  57. }
  58. },
  59. handleFocus() {
  60. this.oldValue = designerForm.toJson(this.value);
  61. },
  62. handleBlur() {
  63. if (this.$refs.editor.save() && designerForm.toJson(this.value) !== this.oldValue) {
  64. if (this.active === 'rule') {
  65. this.designer.setRule(this.value || []);
  66. } else {
  67. this.designer.setOptions(this.value || {});
  68. }
  69. }
  70. }
  71. },
  72. mounted() {
  73. this.$watch(() => this.change, () => {
  74. this.updateValue();
  75. }, {deep: true});
  76. }
  77. });
  78. </script>
  79. <style>
  80. ._fc-json-preview {
  81. display: flex;
  82. width: 100%;
  83. color: #262626;
  84. }
  85. ._fc-json-preview .CodeMirror {
  86. height: 100%;
  87. font-size: 12px;
  88. }
  89. </style>