Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor sourceSelector #215

Merged
merged 18 commits into from
Sep 18, 2024
Merged

refactor sourceSelector #215

merged 18 commits into from
Sep 18, 2024

Conversation

trieloff
Copy link
Contributor

@trieloff trieloff commented Jun 26, 2024

I'm rethinking how the sourceSelector should work, and started by writing down the spec

If you want to try this, open dev tools on a random page, paste this

(function() {
  function walk(element, checkFn) {
    if (!element || element === document.body || element === document.documentElement) {
      return undefined;
    }
    const checkValue = checkFn(element);
    return checkValue || walk(element.parentElement, checkFn);
  }

  function isFakeDialog(element) {
    if (element.tagName === 'DIALOG') return true;
    if (element.getAttribute('role') === 'dialog') return true;
    if (element.getAttribute('role') === 'alertdialog') return true;
    if (element.getAttribute('aria-modal') === 'true') return true;
    const computedStyle = window.getComputedStyle(element);
    return (computedStyle && computedStyle.position === 'fixed' && computedStyle.zIndex > 100);
  }

  function isFakeButton(element) {
    if (element.tagName === 'BUTTON') return true;
    if (element.tagName === 'INPUT' && element.getAttribute('type') === 'button') return true;
    if (element.tagName === 'A') {
      const classes = Array.from(element.classList);
      return classes.some((className) => className.match(/button|cta/));
    }
    return element.getAttribute('role') === 'button';
  }

  function getSourceContext(element) {
    if (element.closest('form')) return 'form';
    if (element.closest('.block')) return `.${element.closest('.block').getAttribute('data-block-name')}`;
    if (walk(element, isFakeDialog)) return 'dialog';
    if (element.closest('nav')) return 'nav';
    if (element.closest('header')) return 'header';
    if (element.closest('footer')) return 'footer';
    if (element.closest('aside')) return 'aside';
    return (walk(element, (e) => e.id && `#${e.id}`));
  }

  function getSourceElement(element) {
    if (element.closest('form') && Array.from(element.closest('form').elements).includes(element)) return element.tagName.toLowerCase() + (element.tagName === 'INPUT' ? `[type='${element.getAttribute('type')}']` : '');
    if (walk(element, isFakeButton)) return 'button';
    return element.tagName.toLowerCase().match(/^(a|img|video)$/) && element.tagName.toLowerCase();
  }

  function getSourceIdentifier(element) {
    if (element.id) return `#${element.id}`;
    if (element.getAttribute('data-block-name')) return `.${element.getAttribute('data-block-name')}`;
    return (element.classList.length > 0 && `.${element.classList[0]}`);
  }

  const sourceSelector = (element) => {
    try {
      if (!element || element === document.body || element === document.documentElement) {
        return undefined;
      }
      if (element.getAttribute('data-rum-source')) {
        return element.getAttribute('data-rum-source');
      }
      const context = getSourceContext(element.parentElement) || '';
      const elementName = getSourceElement(element) || '';
      const identifier = getSourceIdentifier(element) || '';
      return `${context} ${elementName}${identifier}`.trim() || `"${element.textContent.substring(0, 10)}"`;
    } catch (error) {
      return null;
    }
  };

  let lastElement = null;

  document.addEventListener('mousemove', (event) => {
    const currentElement = document.elementFromPoint(event.clientX, event.clientY);

    if (currentElement !== lastElement) {
      const source = sourceSelector(currentElement);
      console.log('Source:', source, currentElement);

      // Flash effect
      if (currentElement) {
        currentElement.style.transition = 'background-color 0.5s ease-in-out';
        currentElement.style.backgroundColor = 'yellow';
        setTimeout(() => {
          currentElement.style.backgroundColor = '';
        }, 500);
      }

      lastElement = currentElement;
    }
  });
})();

move the mouse pointer around on the website and compare the output to your expectations

Copy link

codecov bot commented Jun 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (944d40d) to head (dca6c1c).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #215   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          524       551   +27     
=========================================
+ Hits           524       551   +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +50 to +51
- `img` for images
- `video` for videos
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something that we don't have right now, but it would be useful.

Copy link
Collaborator

@ramboz ramboz Jun 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add object to support generic embeds, and possibly iframe/audio


`context` is
- `form` for form elements
- `dialog` for dialog elements, or parent containers that are fixed positioned and have a positive high z-index
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

README.md Show resolved Hide resolved
Copy link

github-actions bot commented Jul 3, 2024

This PR will trigger a minor release when merged.

@trieloff trieloff marked this pull request as ready for review July 5, 2024 10:37
@trieloff trieloff changed the title WIP: refactor sourceSelector refactor sourceSelector Jul 17, 2024
@trieloff trieloff requested a review from kptdobe August 15, 2024 12:18
modules/dom.js Outdated Show resolved Hide resolved
trieloff and others added 2 commits September 18, 2024 11:19
Co-authored-by: Alexandre Capt <acapt@adobe.com>
@trieloff trieloff merged commit 8c474d2 into main Sep 18, 2024
5 checks passed
@trieloff trieloff deleted the duck-dialog branch September 18, 2024 10:03
@adobe-bot
Copy link
Collaborator

🎉 This issue has been resolved in version 2.21.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants