TemplateInput.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <el-popover
  3. :visible="visible"
  4. width="500"
  5. :teleported="true"
  6. placement="bottom-end"
  7. ref="tempaleInput"
  8. popper-class="fm-template-popover"
  9. >
  10. <div class="code-line">{{prefix}}</div>
  11. <code-editor mode="xml" v-model="value" height="150px" :key="key"></code-editor>
  12. <div class="code-line">{{suffix}}</div>
  13. <div style="text-align: right; margin: 0">
  14. <el-button size="small" bg text @click="visible = false">{{$i18n.locale == 'zh-cn' ? '取 消' : 'Cancel'}}</el-button>
  15. <el-button size="small" type="primary" @click="handleConfirm">
  16. {{$i18n.locale == 'zh-cn' ? '确 定' : 'Confirm'}}
  17. </el-button>
  18. </div>
  19. <template #reference>
  20. <el-button ref="buttonRef" @click="handleEdit" size="small" :style="$attrs.style">{{$i18n.locale == 'zh-cn' ? '点击编辑模板' : 'Setting'}}</el-button>
  21. </template>
  22. </el-popover>
  23. </template>
  24. <script>
  25. import CodeEditor from '../CodeEditor/index.vue'
  26. export default {
  27. components: {
  28. CodeEditor
  29. },
  30. props: {
  31. modelValue: {
  32. type: String
  33. },
  34. prefix: {
  35. type: String,
  36. default: ''
  37. },
  38. suffix: {
  39. type: String,
  40. default: ''
  41. }
  42. },
  43. emits: ['update:modelValue'],
  44. data () {
  45. return {
  46. visible: false,
  47. value: '',
  48. key: Math.random().toString(36).slice(-8),
  49. }
  50. },
  51. mounted () {
  52. document.addEventListener('click', this.onClickOutside)
  53. },
  54. beforeUnmount () {
  55. document.removeEventListener('click', this.onClickOutside)
  56. },
  57. methods: {
  58. handleEdit () {
  59. if (this.visible) {
  60. this.visible = false
  61. } else {
  62. this.value = this.modelValue
  63. this.visible = true
  64. this.key = Math.random().toString(36).slice(-8)
  65. }
  66. },
  67. handleConfirm () {
  68. this.visible = false
  69. this.$emit('update:modelValue', this.value)
  70. },
  71. onClickOutside(event) {
  72. const popoverElement = this.$refs.tempaleInput?.$refs?.tooltipRef?.$refs?.popperRef?.contentRef
  73. const buttonElement = this.$refs.buttonRef.$el
  74. if (popoverElement && !popoverElement.contains(event.target) &&
  75. (!buttonElement || !buttonElement.contains(event.target))
  76. ) {
  77. this.visible = false
  78. }
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss">
  84. .fm-template-popover{
  85. .code-line{
  86. font-size: 14px;
  87. color: var(--el-color-primary);
  88. font-weight: 500;
  89. line-height: 30px;
  90. }
  91. }
  92. </style>