Welcome to mirror list, hosted at ThFree Co, Russian Federation.

utils.js « source_viewer « components « vue_shared « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 596829b51a4a3c8b813d014a6343ef555d82f935 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const BLAME_INFO_CLASSLIST = ['gl-border-t', 'gl-border-gray-500', 'gl-pt-3!'];
const PADDING_BOTTOM_LARGE = 'gl-pb-6!';
const PADDING_BOTTOM_SMALL = 'gl-pb-3!';
const VIEWER_SELECTOR = '.file-holder .blob-viewer';

const findLineNumberElement = (lineNumber) => document.getElementById(`L${lineNumber}`);

const findLineContentElement = (lineNumber) => document.getElementById(`LC${lineNumber}`);

export const calculateBlameOffset = (lineNumber) => {
  if (lineNumber === 1) return '0px';
  const blobViewerOffset = document.querySelector(VIEWER_SELECTOR)?.getBoundingClientRect().top;
  const lineContentOffset = findLineContentElement(lineNumber)?.getBoundingClientRect().top;
  return `${lineContentOffset - blobViewerOffset}px`;
};

export const shouldRender = (data, index) => {
  const prevBlame = data[index - 1];
  const currBlame = data[index];
  const identicalSha = currBlame.commit.sha === prevBlame?.commit?.sha;
  const lineNumberSmaller = currBlame.lineno < prevBlame?.lineno;

  return !identicalSha || lineNumberSmaller;
};

export const toggleBlameClasses = (blameData, isVisible) => {
  /**
   * Adds/removes classes to line number/content elements to match the line with the blame info
   * */
  const method = isVisible ? 'add' : 'remove';
  blameData.forEach(({ lineno, span }, index) => {
    if (!shouldRender(blameData, index)) return;

    const lineNumberEl = findLineNumberElement(lineno)?.parentElement;
    const lineContentEl = findLineContentElement(lineno);
    const lineNumberSpanEl = findLineNumberElement(lineno + span - 1)?.parentElement;
    const lineContentSpanEl = findLineContentElement(lineno + span - 1);

    lineNumberEl?.classList[method](...BLAME_INFO_CLASSLIST);
    lineContentEl?.classList[method](...BLAME_INFO_CLASSLIST);

    if (span === 1) {
      lineNumberSpanEl?.classList[method](PADDING_BOTTOM_LARGE);
      lineContentSpanEl?.classList[method](PADDING_BOTTOM_LARGE);
    } else {
      lineNumberSpanEl?.classList[method](PADDING_BOTTOM_SMALL);
      lineContentSpanEl?.classList[method](PADDING_BOTTOM_SMALL);
    }
  });
};