summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/code_navigation/utils/index.js
blob: 6c078891ed42a33bd9ec3d6443f1160d8013f6a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
export const cachedData = new Map();

export const getCurrentHoverElement = () => cachedData.get('current');
export const setCurrentHoverElement = (el) => cachedData.set('current', el);

export const addInteractionClass = (path, d) => {
  const lineNumber = d.start_line + 1;
  const lines = document
    .querySelector(`[data-path="${path}"]`)
    .querySelectorAll(`.blob-content #LC${lineNumber}, .line_content:not(.old) #LC${lineNumber}`);
  if (!lines?.length) return;

  lines.forEach((line) => {
    let charCount = 0;
    const el = [...line.childNodes].find(({ textContent }) => {
      if (charCount === d.start_char) return true;
      charCount += textContent.length;
      return false;
    });

    if (el) {
      el.setAttribute('data-char-index', d.start_char);
      el.setAttribute('data-line-index', d.start_line);
      el.classList.add('cursor-pointer', 'code-navigation', 'js-code-navigation');
      el.closest('.line').classList.add('code-navigation-line');
    }
  });
};