3328350766
6 天以前 761eb03d6b3bebd0b197179564c84d89d3d12a0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * 选中多个组件进行组合
 */
import { mapMutations, mapState } from 'vuex'
let isMac = false
if (window && window.navigator && window.navigator.userAgent) {
  isMac = window.navigator.userAgent.indexOf('Mac') > -1
}
export default {
  computed: {
    ...mapState({
      activeCodes: (state) => state.bigScreen.activeCodes,
      activeChart: (state) => state.bigScreen.activeItemConfig
    })
  },
  mounted () {
    // 监听shift键的按下和抬起
    document.addEventListener('keydown', this.keydown)
    document.addEventListener('keyup', this.keyup)
  },
  beforeDestroy () {
    document.removeEventListener('keydown', this.keydown)
    document.removeEventListener('keyup', this.keyup)
  },
  methods: {
    ...mapMutations('bigScreen', {
      changeCtrlOrCommandDown: 'changeCtrlOrCommandDown',
      changeActivePos: 'changeActivePos',
      deleteItem: 'delItem',
      undoTimeLine: 'undoTimeLine',
      copyCharts: 'copyCharts',
      pasteCharts: 'pasteCharts'
    }),
    keydown (event) {
      // 获取当前获得焦点的元素
      const activeElement = document.activeElement
      // 判断当前获得焦点的元素是否是一个输入元素
      const isInputFocused = activeElement instanceof HTMLInputElement || activeElement instanceof HTMLTextAreaElement
      if (!isInputFocused) {
        // 当前页面没有输入聚焦
        if (event.keyCode === 37) {
          // 关闭默认事件
          event.preventDefault()
          // 左箭头键被按下
          this.changeActivePos({ diffX: -1, diffY: 0 })
        } else if (event.keyCode === 38) {
          // 关闭默认事件
          event.preventDefault()
          // 上箭头键被按下
          this.changeActivePos({ diffX: 0, diffY: -1 })
        } else if (event.keyCode === 39) {
          // 关闭默认事件
          event.preventDefault()
          // 右箭头键被按下
          this.changeActivePos({ diffX: 1, diffY: 0 })
        } else if (event.keyCode === 40) {
          // 关闭默认事件
          event.preventDefault()
          // 下箭头键被按下
          this.changeActivePos({ diffX: 0, diffY: 1 })
        }
      }
 
      // ctrl/command + s保存
      if ((event.ctrlKey || event.metaKey) && event.keyCode === 83) {
        // 关闭默认事件
        event.preventDefault()
        this.$refs.PageTopSetting.save('saveLoading')
      }
      // ctrl/command + z撤销
      if ((event.ctrlKey || event.metaKey) && !event.shiftKey && event.keyCode === 90) {
        event.preventDefault()
        this.undoTimeLine(true)
      }
 
      // ctrl/command + shift + z 反撤销
      if ((event.ctrlKey || event.metaKey) && event.shiftKey && event.keyCode === 90) {
        event.preventDefault()
        this.undoTimeLine(false)
      }
      if (isMac) {
        // 是否按下了command键
        if (event.metaKey) {
          this.changeCtrlOrCommandDown(true)
        }
      } else {
        // 是否按下了ctrl键
        if (event.ctrlKey) {
          this.changeCtrlOrCommandDown(true)
        }
      }
    },
    keyup (event) {
      // 判断mac系统还是windows系统
      if (isMac) {
        // 是否按下了command键
        if (!event.metaKey) {
          this.changeCtrlOrCommandDown(false)
        }
      } else {
        // 是否按下了ctrl键
        if (!event.ctrlKey) {
          this.changeCtrlOrCommandDown(false)
        }
      }
    },
    designKeydown (event) {
      // 删除键被按下且鼠标没有在输入框中
      if (
        (event.keyCode === 8 || event.keyCode === 46) &&
        !event.target.classList.contains('el-input__inner')
      ) {
        // 关闭默认事件
        event.preventDefault()
        // 如果没有选中任何组件的话则不弹出删除提示框
        if (!this.activeCodes.length) {
          return
        }
        this.$confirm('确定删除该组件吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          customClass: 'bs-el-message-box'
        }).then(() => {
          // 批量删除
          if (Array.isArray(this.activeCodes) && this.activeCodes.length > 0) {
            this.deleteItem(this.activeCodes)
          } else {
            // 单个删除
            this.deleteItem(this.activeChart)
          }
        }).catch(() => {})
      }
      // ctrl/command + c复制
      if ((event.ctrlKey || event.metaKey) && event.keyCode === 67) {
        this.copyCharts()
      }
 
      if ((event.ctrlKey || event.metaKey) && event.keyCode === 86) {
        // 粘贴
        this.pasteCharts()
      }
    }
  }
}