大空间消毒机
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
3.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <template>
  2. <div :class="keyboardClass"></div>
  3. </template>
  4. <script>
  5. import Keyboard from 'simple-keyboard'
  6. import 'simple-keyboard/build/css/index.css'
  7. import layout from 'simple-keyboard-layouts/build/layouts/chinese' // 中文输入法
  8. export default {
  9. name: 'SimpleKeyboard',
  10. props: {
  11. keyboardClass: {
  12. default: 'simple-keyboard',
  13. type: String,
  14. },
  15. input: {
  16. default: '',
  17. },
  18. maxLength: { default: '' },
  19. },
  20. data: () => ({
  21. keyboard: null,
  22. displayDefault: {
  23. '{bksp}': 'backspace',
  24. '{lock}': 'caps',
  25. '{enter}': '> enter',
  26. '{tab}': 'tab',
  27. '{shift}': 'shift',
  28. '{change}': '英文',
  29. '{space}': ' ',
  30. '{clear}': '清空',
  31. '{close}': '关闭',
  32. },
  33. }),
  34. mounted() {
  35. this.keyboard = new Keyboard(this.keyboardClass, {
  36. onChange: this.onChange,
  37. onKeyPress: this.onKeyPress,
  38. layoutCandidates: layout.layoutCandidates,
  39. layout: {
  40. // 默认布局
  41. default: [
  42. '` 1 2 3 4 5 6 7 8 9 0 - = {bksp}',
  43. '{tab} q w e r t y u i o p [ ] \\',
  44. "{lock} a s d f g h j k l ; '",
  45. '{shift} z x c v b n m , . /',
  46. '{clear}',
  47. ],
  48. // shift布局
  49. shift: [
  50. '~ ! @ # $ % ^ & * ( ) _ + {bksp}',
  51. '{tab} Q W E R T Y U I O P { } |',
  52. '{lock} A S D F G H J K L : "',
  53. '{shift} Z X C V B N M &lt; &gt; ?',
  54. '{clear}',
  55. ],
  56. },
  57. // 按钮展示文字
  58. display: this.displayDefault,
  59. // 按钮样式
  60. buttonTheme: [
  61. {
  62. class: 'hg-red close',
  63. buttons: '{close}',
  64. },
  65. {
  66. class: 'change',
  67. buttons: '{change}',
  68. },
  69. ],
  70. // 输入限制长度
  71. maxLength: this.maxLength,
  72. })
  73. },
  74. methods: {
  75. onChange(input) {
  76. this.keyboard.setInput(input)
  77. this.$emit('onChange', input)
  78. },
  79. // 点击键盘
  80. onKeyPress(button, $event) {
  81. const self = this
  82. // 点击关闭
  83. if (button === '{close}') {
  84. let keyboard = $event.path[3]
  85. keyboard.style.visibility = 'hidden'
  86. return false
  87. } else if (button === '{change}') {
  88. // 切换中英文输入法
  89. if (this.keyboard.options.layoutCandidates !== null) {
  90. self.$set(this.displayDefault, '{change}', '中文')
  91. // 切换至英文
  92. this.keyboard.setOptions({
  93. layoutCandidates: null,
  94. display: this.displayDefault,
  95. })
  96. } else {
  97. // 切换至中文
  98. self.$set(this.displayDefault, '{change}', '英文')
  99. this.keyboard.setOptions({
  100. layoutCandidates: layout.layoutCandidates,
  101. display: this.displayDefault,
  102. })
  103. }
  104. } else if (button === '{clear}') {
  105. this.keyboard.clearInput()
  106. } else {
  107. let value =
  108. $event.target.offsetParent.parentElement.children[0].children[0].value
  109. // 输入框有默认值时,覆写
  110. if (value) {
  111. this.keyboard.setInput(value)
  112. }
  113. this.$emit('onKeyPress', button)
  114. }
  115. if (button === '{shift}' || button === '{lock}') this.handleShift()
  116. },
  117. // 切换shift/默认布局
  118. handleShift() {
  119. let currentLayout = this.keyboard.options.layoutName
  120. let shiftToggle = currentLayout === 'default' ? 'shift' : 'default'
  121. this.keyboard.setOptions({
  122. layoutName: shiftToggle,
  123. })
  124. },
  125. },
  126. watch: {
  127. input(input) {
  128. this.keyboard.setInput(input)
  129. },
  130. },
  131. }
  132. </script>
  133. <style lang="scss">
  134. @deep: ~'>>>';
  135. .hg-theme-default {
  136. width: 100%;
  137. .hg-buton {
  138. &.hg-red {
  139. background: #db3e5d;
  140. color: white;
  141. &.close {
  142. max-width: 200px;
  143. }
  144. }
  145. &.change {
  146. max-width: 200px;
  147. }
  148. }
  149. }
  150. </style>