parseKeyDef.js 1.9 KB

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