JsonDrawer.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div>
  3. <el-drawer v-bind="$attrs" v-on="$listeners" @opened="onOpen" @close="onClose">
  4. <div class="action-bar" :style="{'text-align': 'left'}">
  5. <span class="bar-btn" @click="refresh">
  6. <i class="el-icon-refresh" />
  7. 刷新
  8. </span>
  9. <span ref="copyBtn" class="bar-btn copy-json-btn">
  10. <i class="el-icon-document-copy" />
  11. 复制JSON
  12. </span>
  13. <span class="bar-btn" @click="exportJsonFile">
  14. <i class="el-icon-download" />
  15. 导出JSON文件
  16. </span>
  17. <span class="bar-btn delete-btn" @click="$emit('update:visible', false)">
  18. <i class="el-icon-circle-close" />
  19. 关闭
  20. </span>
  21. </div>
  22. <div id="editorJson" class="json-editor" />
  23. </el-drawer>
  24. </div>
  25. </template>
  26. <script>
  27. import { beautifierConf } from '@/utils'
  28. import ClipboardJS from 'clipboard'
  29. import { saveAs } from 'file-saver'
  30. import loadMonaco from '@/utils/loadMonaco'
  31. import loadBeautifier from '@/utils/loadBeautifier'
  32. let beautifier
  33. let monaco
  34. export default {
  35. components: {},
  36. props: {
  37. jsonStr: {
  38. type: String,
  39. required: true
  40. }
  41. },
  42. data() {
  43. return {}
  44. },
  45. computed: {},
  46. watch: {},
  47. created() {},
  48. mounted() {
  49. window.addEventListener('keydown', this.preventDefaultSave)
  50. const clipboard = new ClipboardJS('.copy-json-btn', {
  51. text: trigger => {
  52. this.$notify({
  53. title: '成功',
  54. message: '代码已复制到剪切板,可粘贴。',
  55. type: 'success'
  56. })
  57. return this.beautifierJson
  58. }
  59. })
  60. clipboard.on('error', e => {
  61. this.$message.error('代码复制失败')
  62. })
  63. },
  64. beforeDestroy() {
  65. window.removeEventListener('keydown', this.preventDefaultSave)
  66. },
  67. methods: {
  68. preventDefaultSave(e) {
  69. if (e.key === 's' && (e.metaKey || e.ctrlKey)) {
  70. e.preventDefault()
  71. }
  72. },
  73. onOpen() {
  74. loadBeautifier(btf => {
  75. beautifier = btf
  76. this.beautifierJson = beautifier.js(this.jsonStr, beautifierConf.js)
  77. loadMonaco(val => {
  78. monaco = val
  79. this.setEditorValue('editorJson', this.beautifierJson)
  80. })
  81. })
  82. },
  83. onClose() {},
  84. setEditorValue(id, codeStr) {
  85. if (this.jsonEditor) {
  86. this.jsonEditor.setValue(codeStr)
  87. } else {
  88. this.jsonEditor = monaco.editor.create(document.getElementById(id), {
  89. value: codeStr,
  90. theme: 'vs-dark',
  91. language: 'json',
  92. automaticLayout: true
  93. })
  94. // ctrl + s 刷新
  95. this.jsonEditor.onKeyDown(e => {
  96. if (e.keyCode === 49 && (e.metaKey || e.ctrlKey)) {
  97. this.refresh()
  98. }
  99. })
  100. }
  101. },
  102. exportJsonFile() {
  103. this.$prompt('文件名:', '导出文件', {
  104. inputValue: `${+new Date()}.json`,
  105. closeOnClickModal: false,
  106. inputPlaceholder: '请输入文件名'
  107. }).then(({ value }) => {
  108. if (!value) value = `${+new Date()}.json`
  109. const codeStr = this.jsonEditor.getValue()
  110. const blob = new Blob([codeStr], { type: 'text/plain;charset=utf-8' })
  111. saveAs(blob, value)
  112. })
  113. },
  114. refresh() {
  115. try {
  116. this.$emit('refresh', JSON.parse(this.jsonEditor.getValue()))
  117. } catch (error) {
  118. this.$notify({
  119. title: '错误',
  120. message: 'JSON格式错误,请检查',
  121. type: 'error'
  122. })
  123. }
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. @import '@/styles/mixin.scss';
  130. :deep(.el-drawer__header) {
  131. display: none;
  132. }
  133. @include action-bar;
  134. .json-editor{
  135. height: calc(100vh - 33px);
  136. }
  137. </style>