|
| 1 | +import { translateText } from '../../translate.js'; |
| 2 | +import Text from './text.js'; |
| 3 | + |
| 4 | +const baseLabels = { |
| 5 | + ' ': 'Space', |
| 6 | +}; |
| 7 | + |
| 8 | +export default class extends Text { |
| 9 | + constructor(name = 'keybind') { |
| 10 | + super(name); |
| 11 | + } |
| 12 | + |
| 13 | + element(value, update, { |
| 14 | + data = {}, |
| 15 | + }) { |
| 16 | + const labels = { |
| 17 | + ...baseLabels, |
| 18 | + ...data.labels, |
| 19 | + }; |
| 20 | + function getLabel(key) { |
| 21 | + return translateText(labels[key] || key); |
| 22 | + } |
| 23 | + let val = value !== 'Escape' ? value : ''; |
| 24 | + let temp = getLabel(val); |
| 25 | + const ret = $('<div class="keybind-wrapper">'); |
| 26 | + const input = $('<input type="text">') |
| 27 | + .val(temp) |
| 28 | + .on('focus', () => { |
| 29 | + input.val('').prop('placeholder', 'Press Any Key...'); |
| 30 | + ret.addClass('editing'); |
| 31 | + }) |
| 32 | + .on('keydown', (event) => { |
| 33 | + const { key } = event; |
| 34 | + if (key !== 'Escape' && key !== val) { |
| 35 | + event.preventDefault(); |
| 36 | + update(key); |
| 37 | + temp = getLabel(key); |
| 38 | + val = key; |
| 39 | + } |
| 40 | + input.blur(); |
| 41 | + }) |
| 42 | + .on('blur', () => { |
| 43 | + input.val(temp).prop('placeholder', 'Click to bind'); |
| 44 | + ret.removeClass('editing'); |
| 45 | + }) |
| 46 | + .prop('placeholder', 'Click to bind'); |
| 47 | + const button = $('<button class="glyphicon glyphicon-remove-sign">') |
| 48 | + .on('click', () => { |
| 49 | + if (temp === '') return; |
| 50 | + temp = ''; |
| 51 | + input.val(temp); |
| 52 | + update(undefined); |
| 53 | + }); |
| 54 | + ret.append(input, button); |
| 55 | + return ret; |
| 56 | + } |
| 57 | + |
| 58 | + styles() { |
| 59 | + return [ |
| 60 | + '.keybind-wrapper { position: relative; }', |
| 61 | + '.keybind-wrapper input { text-align: center; caret-color: transparent; }', |
| 62 | + '.keybind-wrapper input:focus { border-color: transparent; outline: red double 1px; }', |
| 63 | + '.keybind-wrapper button { position: absolute; top: 4px; right: 0px; color: red; background-color: transparent; border: none; display: none; }', |
| 64 | + '.keybind-wrapper:not(.editing):hover button { display: inline-block; }', |
| 65 | + ]; |
| 66 | + } |
| 67 | +} |
0 commit comments