Skip to content

Commit

Permalink
[MWPW-141785] Implement custom link actions (#2324)
Browse files Browse the repository at this point in the history
Co-authored-by: Okan Sahin <[email protected]>
  • Loading branch information
narcis-radu and mokimo authored Jun 4, 2024
1 parent a60b132 commit 1362ce1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
19 changes: 9 additions & 10 deletions libs/blocks/card/cardUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,18 @@ export const addFooter = (links, container, merch) => {
const linksArr = Array.from(links);
const linksLeng = linksArr.length;
const hrTag = merch ? '<hr>' : '';
let footer = `<div class="consonant-CardFooter">${hrTag}<div class="consonant-CardFooter-row" data-cells="1">`;
footer = linksArr.reduce(
(combined, link, index) => (
`${combined}<div class="consonant-CardFooter-cell consonant-CardFooter-cell--${(linksLeng === 2 && index === 0) ? 'left' : 'right'}">${link.outerHTML}</div>`),
footer,
);
footer += '</div></div>';

container.insertAdjacentHTML('beforeend', footer);
links.forEach((link) => {
const footer = createTag('div', { class: 'consonant-CardFooter' }, hrTag);
const row = createTag('div', { class: 'consonant-CardFooter-row', 'data-cells': '1' });
linksArr.forEach((link, index) => {
const { parentElement } = link;
if (parentElement && document.body.contains(parentElement)) parentElement.remove();
const holder = createTag('div', { class: `consonant-CardFooter-cell consonant-CardFooter-cell--${(linksLeng === 2 && index === 0) ? 'left' : 'right'}` });
holder.append(link);
row.append(holder);
});

footer.append(row);
container.insertAdjacentElement('beforeend', footer);
};

export const addWrapper = (el, section, cardType) => {
Expand Down
9 changes: 9 additions & 0 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,15 @@ export function decorateLinks(el) {
rdx.push(a);
}
}
// Custom action links
const loginEvent = '#_evt-login';
if (a.href.includes(loginEvent)) {
a.href = a.href.replace(loginEvent, '');
a.addEventListener('click', (e) => {
e.preventDefault();
window.adobeIMS?.signIn();
});
}
return rdx;
}, []);
}
Expand Down
2 changes: 2 additions & 0 deletions test/utils/mocks/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<p><a class="disable-autoblock" href="https://www.instagram.com/#_dnb">Disable Autoblock</a></p>
<!-- Auto Block -->
<p><a class="autoblock" href="https://twitter.com/Adobe#_dnb">Auto Block</a></p>
<!-- Custom actions -->
<p><a class="login-action" href="https://www.adobe.com/#_evt-login"></a></p>
</div>
<div class="quote borders contained hide-block">
<div>
Expand Down
9 changes: 9 additions & 0 deletions test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ describe('Utils', () => {
});
});

describe('Custom Link Actions', () => {
it('Implements a login action', async () => {
await waitForElement('.login-action');
const login = document.querySelector('.login-action');
utils.decorateLinks(login);
expect(login.href).to.equal('https://www.adobe.com/');
});
});

describe('Fragments', () => {
it('fully unwraps a fragment', () => {
const fragments = document.querySelectorAll('.link-block.fragment');
Expand Down

0 comments on commit 1362ce1

Please sign in to comment.