Skip to content

Commit efe3a06

Browse files
committed
feat(SettingType): keybind
1 parent 0c0b3cb commit efe3a06

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

src/utils/settings/types/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ export { default as Select } from './select.js';
88
export { default as Slider } from './slider.js';
99
export { default as Text } from './text.js';
1010
export { default as MapType } from './map.js';
11-
12-
// Must always be last
13-
export { default as AdvancedMap } from './map2.js';
11+
export { default as Keybind } from './keybind.js';
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)