2022-06-21 17:19:12 +05:30
|
|
|
import { wrapNodes, isTextNode } from './dom_utils';
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
export const cachedData = new Map();
|
|
|
|
|
|
|
|
export const getCurrentHoverElement = () => cachedData.get('current');
|
2021-03-08 18:12:59 +05:30
|
|
|
export const setCurrentHoverElement = (el) => cachedData.set('current', el);
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
export const addInteractionClass = ({ path, d, wrapTextNodes }) => {
|
2020-04-22 19:07:51 +05:30
|
|
|
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;
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
lines.forEach((line) => {
|
2020-04-22 19:07:51 +05:30
|
|
|
let charCount = 0;
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
if (wrapTextNodes) {
|
|
|
|
line.childNodes.forEach((elm) => {
|
|
|
|
if (isTextNode(elm)) {
|
|
|
|
// Highlight.js does not wrap all text nodes by default
|
|
|
|
// We need all text nodes to be wrapped in order to append code nav attributes
|
|
|
|
elm.replaceWith(...wrapNodes(elm.textContent));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
const el = [...line.childNodes].find(({ textContent }) => {
|
|
|
|
if (charCount === d.start_char) return true;
|
|
|
|
charCount += textContent.length;
|
|
|
|
return false;
|
|
|
|
});
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
if (el && !isTextNode(el)) {
|
2022-07-23 23:45:48 +05:30
|
|
|
el.dataset.charIndex = d.start_char;
|
|
|
|
el.dataset.lineIndex = d.start_line;
|
2020-04-22 19:07:51 +05:30
|
|
|
el.classList.add('cursor-pointer', 'code-navigation', 'js-code-navigation');
|
2020-07-28 23:09:34 +05:30
|
|
|
el.closest('.line').classList.add('code-navigation-line');
|
2020-04-22 19:07:51 +05:30
|
|
|
}
|
|
|
|
});
|
2020-03-13 15:44:24 +05:30
|
|
|
};
|