parseKeyDef.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import '../utils/dataTransfer/Clipboard.js';
  2. import { readNextDescriptor } from '../utils/keyDef/readNextDescriptor.js';
  3. /**
  4. * Parse key definitions per `keyboardMap`
  5. *
  6. * Keys can be referenced by `{key}` or `{special}` as well as physical locations per `[code]`.
  7. * Everything else will be interpreted as a typed character - e.g. `a`.
  8. * Brackets `{` and `[` can be escaped by doubling - e.g. `foo[[bar` translates to `foo[bar`.
  9. * Keeping the key pressed can be written as `{key>}`.
  10. * When keeping the key pressed you can choose how long (how many keydown and keypress) the key is pressed `{key>3}`.
  11. * You can then release the key per `{key>3/}` or keep it pressed and continue with the next key.
  12. */ function parseKeyDef(keyboardMap, text) {
  13. const defs = [];
  14. do {
  15. const { type, descriptor, consumedLength, releasePrevious, releaseSelf = true, repeat } = readNextDescriptor(text, 'keyboard');
  16. var _keyboardMap_find;
  17. const keyDef = (_keyboardMap_find = keyboardMap.find((def)=>{
  18. if (type === '[') {
  19. var _def_code;
  20. return ((_def_code = def.code) === null || _def_code === undefined ? undefined : _def_code.toLowerCase()) === descriptor.toLowerCase();
  21. } else if (type === '{') {
  22. var _def_key;
  23. return ((_def_key = def.key) === null || _def_key === undefined ? undefined : _def_key.toLowerCase()) === descriptor.toLowerCase();
  24. }
  25. return def.key === descriptor;
  26. })) !== null && _keyboardMap_find !== undefined ? _keyboardMap_find : {
  27. key: 'Unknown',
  28. code: 'Unknown',
  29. [type === '[' ? 'code' : 'key']: descriptor
  30. };
  31. defs.push({
  32. keyDef,
  33. releasePrevious,
  34. releaseSelf,
  35. repeat
  36. });
  37. text = text.slice(consumedLength);
  38. }while (text)
  39. return defs;
  40. }
  41. export { parseKeyDef };