HotkeyStroke.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import loggerFactory from '~/utils/logger';
  2. const logger = loggerFactory('growi:cli:HotkeyStroke');
  3. export default class HotkeyStroke {
  4. constructor(stroke) {
  5. this.stroke = stroke;
  6. this.activeIndices = [];
  7. }
  8. get firstKey() {
  9. return this.stroke[0];
  10. }
  11. /**
  12. * Evaluate whether the specified key completes stroke or not
  13. * @param {string} key
  14. * @return T/F whether the specified key completes stroke or not
  15. */
  16. evaluate(key) {
  17. if (key === this.firstKey) {
  18. // add a new active index
  19. this.activeIndices.push(0);
  20. }
  21. let isCompleted = false;
  22. this.activeIndices = this.activeIndices
  23. .map((index) => {
  24. // return null when key does not match
  25. if (key !== this.stroke[index]) {
  26. return null;
  27. }
  28. const nextIndex = index + 1;
  29. if (this.stroke.length <= nextIndex) {
  30. isCompleted = true;
  31. return null;
  32. }
  33. return nextIndex;
  34. })
  35. // exclude null
  36. .filter(index => index != null);
  37. // reset if completed
  38. if (isCompleted) {
  39. this.activeIndices = [];
  40. }
  41. logger.debug('activeIndices for [', this.stroke, '] => [', this.activeIndices, ']');
  42. return isCompleted;
  43. }
  44. }