Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 100 additions & 86 deletions assets/client/services/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@ const liveComponentsColors = [
];
const streamItemColors = ['#8bca8480'];

const isElementVisible = (element) => {
if (!element) return false;

if (element.checkVisibility) {
return element.checkVisibility();
}

const style = window.getComputedStyle(element);
return (
style.display !== 'none' &&
style.visibility !== 'hidden' &&
style.opacity !== '0'
);
};
function getHighlightRect(element) {
if (!element) return null;

const rect = element.getBoundingClientRect();
if (rect.width !== 0 || rect.height !== 0) return rect;

const childRects = [...element.children]
.map((child) => getHighlightRect(child))
.filter(Boolean);
Comment thread
GuzekAlan marked this conversation as resolved.

if (childRects.length === 0) return null;

const top = Math.min(...childRects.map((r) => r.top));
const left = Math.min(...childRects.map((r) => r.left));
const right = Math.max(...childRects.map((r) => r.right));
const bottom = Math.max(...childRects.map((r) => r.bottom));

return {
top,
left,
right,
bottom,
width: right - left,
height: bottom - top,
};
}

function getHighlightColors(type) {
switch (type) {
Expand All @@ -39,8 +51,7 @@ function getHighlightColors(type) {
}
}

function createHighlightElement(activeElement, detail, id) {
const rect = activeElement.getBoundingClientRect();
function createHighlightElement(rect, detail, id) {
const highlight = document.createElement('div');

highlight.id = id;
Expand All @@ -50,8 +61,8 @@ function createHighlightElement(activeElement, detail, id) {
highlight.style.position = 'absolute';
highlight.style.top = `${rect.top + window.scrollY}px`;
highlight.style.left = `${rect.left + window.scrollX}px`;
highlight.style.width = `${activeElement.offsetWidth}px`;
highlight.style.height = `${activeElement.offsetHeight}px`;
highlight.style.width = `${rect.width}px`;
highlight.style.height = `${rect.height}px`;
highlight.style.backgroundColor = getHighlightColors(detail.type)[0];
highlight.style.zIndex = '10000';
highlight.style.pointerEvents = 'none';
Expand All @@ -60,12 +71,7 @@ function createHighlightElement(activeElement, detail, id) {
}

function removeHighlightElement(shadowRoot) {
const highlightElement = shadowRoot.querySelector(`#${highlightElementID}`);

if (highlightElement) {
highlightElement.remove();
}

shadowRoot.querySelector(`#${highlightElementID}`)?.remove();
dispatchCustomEvent('lvdbg:remove-tooltip');
}

Expand All @@ -88,82 +94,90 @@ function handleHighlight({ detail }, shadowRoot) {
`[${detail.attr}="${detail.val}"]`
);

if (isElementVisible(activeElement)) {
highlightElement = createHighlightElement(
activeElement,
detail,
highlightElementID
);
if (!activeElement) return;
Comment thread
GuzekAlan marked this conversation as resolved.

shadowRoot.appendChild(highlightElement);
showTooltip(detail);
}
const rect = getHighlightRect(activeElement);

if (!rect) return;

highlightElement = createHighlightElement(rect, detail, highlightElementID);

shadowRoot.appendChild(highlightElement);
showTooltip(detail);
}

function handleHighlightResize(shadowRoot) {
const highlight = shadowRoot.querySelector(`#${highlightElementID}`);
if (highlight) {
const activeElement = document.querySelector(
`[${highlight.dataset.attr}="${highlight.dataset.val}"]`
);
const rect = activeElement.getBoundingClientRect();

highlight.style.top = `${rect.top + window.scrollY}px`;
highlight.style.left = `${rect.left + window.scrollX}px`;
highlight.style.width = `${activeElement.offsetWidth}px`;
highlight.style.height = `${activeElement.offsetHeight}px`;
if (!highlight) return;

const activeElement = document.querySelector(
`[${highlight.dataset.attr}="${highlight.dataset.val}"]`
);

if (!activeElement) {
removeHighlightElement(shadowRoot);
return;
}

const rect = getHighlightRect(activeElement);

if (!rect) {
removeHighlightElement(shadowRoot);
return;
}

highlight.style.top = `${rect.top + window.scrollY}px`;
highlight.style.left = `${rect.left + window.scrollX}px`;
highlight.style.width = `${rect.width}px`;
highlight.style.height = `${rect.height}px`;
}

function handlePulse({ detail }, shadowRoot) {
const activeElement = document.querySelector(
`[${detail.attr}="${detail.val}"]`
);

if (isElementVisible(activeElement)) {
const highlightPulse = createHighlightElement(
activeElement,
detail,
highlightPulseElementID
);

shadowRoot.appendChild(highlightPulse);

const w = highlightPulse.offsetWidth;
const h = highlightPulse.offsetHeight;

const colors = getHighlightColors(detail.type);

highlightPulse.animate(
[
{
width: `${w}px`,
height: `${h}px`,
transform: 'translate(0, 0)',
backgroundColor: colors[1],
},
{
width: `${w + 20}px`,
height: `${h + 20}px`,
transform: 'translate(-10px, -10px)',
backgroundColor: colors[2],
},
{
width: `${w + 40}px`,
height: `${h + 40}px`,
transform: 'translate(-20px, -20px)',
backgroundColor: colors[3],
},
],
if (!activeElement) return;

const rect = getHighlightRect(activeElement);

if (!rect) return;

const highlightPulse = createHighlightElement(
rect,
detail,
highlightPulseElementID
);
shadowRoot.appendChild(highlightPulse);

const w = highlightPulse.offsetWidth;
const h = highlightPulse.offsetHeight;

const colors = getHighlightColors(detail.type);

highlightPulse.animate(
[
{
duration: 500,
iterations: 1,
delay: 200,
}
).onfinish = () => {
highlightPulse.remove();
};
}
width: `${w}px`,
height: `${h}px`,
transform: 'translate(0, 0)',
backgroundColor: colors[1],
},
{
width: `${w + 20}px`,
height: `${h + 20}px`,
transform: 'translate(-10px, -10px)',
backgroundColor: colors[2],
},
{
width: `${w + 40}px`,
height: `${h + 40}px`,
transform: 'translate(-20px, -20px)',
backgroundColor: colors[3],
},
],
{ duration: 500, iterations: 1, delay: 200 }
).onfinish = () => highlightPulse.remove();
}

function showTooltip(detail) {
Expand Down
Loading
Loading