flamegraphKeyboardNavigation.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {FlamegraphFrame} from '../flamegraphFrame';
  2. export type DirectionX = 'left' | 'right';
  3. export type DirectionY = 'up' | 'down';
  4. export type Direction = DirectionY | DirectionX;
  5. /**
  6. * selectNearestFrame walks a FlamegraphFrame tree the direction specified and returns the nearest
  7. * FlamegraphFrame relative to the starting node.
  8. */
  9. export function selectNearestFrame(frame: FlamegraphFrame, direction: Direction) {
  10. if (direction === 'up') {
  11. const parent = frame.parent;
  12. // sentry root is a virtual root that should not be selectable
  13. if (parent?.frame.isRoot) {
  14. return frame;
  15. }
  16. // if there is an immediate parent, goto parent
  17. if (parent) {
  18. return parent;
  19. }
  20. // if there is no parent, we attempt to move left to the next stack
  21. const leftSibling = getSibling(frame, 'left');
  22. if (leftSibling) {
  23. return leftSibling;
  24. }
  25. return frame;
  26. }
  27. if (direction === 'down') {
  28. // if there is an immediate child, goto child
  29. const child = frame.children?.[0];
  30. if (child) {
  31. return child;
  32. }
  33. // if there is no child, we attempt to move right to the next stack
  34. const sibling = scanForNearestSibling(frame, 'right');
  35. if (sibling) {
  36. return sibling;
  37. }
  38. return frame;
  39. }
  40. // scan for the nearest sibling in either the left or right direction
  41. // this function will walk us up the tree to a new branch
  42. const sibling = scanForNearestSibling(frame, direction);
  43. // if we find an adjacent sibling we attempt to walk down to a depth that
  44. // matches our targetDepth
  45. if (sibling) {
  46. const targetDepth = frame.depth;
  47. const nodeWithDepth = scanForNearestFrameWithDepth(sibling, targetDepth, direction);
  48. if (nodeWithDepth) {
  49. return nodeWithDepth;
  50. }
  51. }
  52. return frame;
  53. }
  54. /**
  55. * scanForNearestSibling will walk up a branch looking for an adjacent sibling
  56. */
  57. function scanForNearestSibling(frame: FlamegraphFrame, directionX: DirectionX) {
  58. let node = frame;
  59. while (node) {
  60. const sibling = getSibling(node, directionX);
  61. if (sibling) {
  62. return sibling;
  63. }
  64. if (!node.parent) {
  65. break;
  66. }
  67. node = node.parent;
  68. }
  69. return frame;
  70. }
  71. /**
  72. * scanForNearestFrameWithDepth will walk down a FlamegraphFrame looking for a target depth.
  73. * it will follow either the furthest left or right branch based on direction.
  74. * if target depth is not found, we return the frame with the max depth along that branch.
  75. */
  76. function scanForNearestFrameWithDepth(
  77. frame: FlamegraphFrame,
  78. depth: number,
  79. directionX: DirectionX
  80. ) {
  81. let node = frame;
  82. let nextNode = node.children[directionX === 'right' ? 0 : node.children.length - 1];
  83. while (node && nextNode) {
  84. if (node.depth === depth) {
  85. return node;
  86. }
  87. nextNode = node.children[directionX === 'right' ? 0 : node.children.length - 1];
  88. if (nextNode) {
  89. node = nextNode;
  90. }
  91. }
  92. return node;
  93. }
  94. function getSibling(frame: FlamegraphFrame, directionX: DirectionX) {
  95. const parent = frame.parent;
  96. if (!parent) {
  97. return null;
  98. }
  99. // get the index of the current frame relative to its siblings
  100. const indexOfFrame = parent.children.indexOf(frame);
  101. // this should never happen, it would make our data structure invalid
  102. if (indexOfFrame === -1) {
  103. throw Error('frame.parent.children does not include current frame');
  104. }
  105. if (directionX === 'right') {
  106. const hasRightSiblings = indexOfFrame < parent.children.length - 1;
  107. if (hasRightSiblings) {
  108. return parent.children[indexOfFrame + 1];
  109. }
  110. return null;
  111. }
  112. const hasLeftSiblings = indexOfFrame > 0;
  113. if (hasLeftSiblings) {
  114. return parent.children[indexOfFrame - 1];
  115. }
  116. return null;
  117. }
  118. // loosely based spreadsheet navigation
  119. const keyDirectionMap: Record<string, Direction> = {
  120. ArrowUp: 'up',
  121. ArrowDown: 'down',
  122. ArrowLeft: 'left',
  123. ArrowRight: 'right',
  124. Tab: 'down',
  125. 'Shift+Tab': 'up',
  126. } as const;
  127. export function handleFlamegraphKeyboardNavigation(
  128. evt: KeyboardEvent,
  129. currentFrame: FlamegraphFrame | undefined,
  130. inverted: boolean = false
  131. ) {
  132. if (!currentFrame) {
  133. return null;
  134. }
  135. const key = evt.shiftKey ? `Shift+${evt.key}` : evt.key;
  136. let direction = keyDirectionMap[key];
  137. // if the key is not mapped, don't handle it
  138. if (!direction) {
  139. return null;
  140. }
  141. if (inverted && (direction === 'up' || direction === 'down')) {
  142. direction = direction === 'up' ? 'down' : 'up';
  143. }
  144. const nextSelection = selectNearestFrame(currentFrame, direction);
  145. if (nextSelection === currentFrame) {
  146. return null;
  147. }
  148. evt.preventDefault();
  149. return nextSelection;
  150. }