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

MWPW-142590 - [Share] enhancement - ability to edit text above icons #2070

Merged
merged 7 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
57 changes: 39 additions & 18 deletions libs/blocks/share/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,58 @@ export async function getSVGsfromFile(path, selectors) {

function getPlatforms(el) {
const manualShares = el.querySelectorAll('a');
if (manualShares.length === 0) return null;
return [...manualShares].map((link) => {
const { href } = link;
if (manualShares.length === 0) return ['facebook', 'twitter', 'linkedin', 'pinterest', 'reddit'];
const platforms = [];
[...manualShares].forEach((share) => {
const { href } = share;
const url = new URL(href);
const parts = url.host.split('.');
return parts[parts.length - 2];
platforms.push(parts[parts.length - 2]);
const parentP = share.closest('p');
parentP?.remove();
});
return platforms;
}

export default async function decorate(block) {
const config = getConfig();
const base = config.miloLibs || config.codeRoot;
const platforms = getPlatforms(block) || [
'facebook',
'twitter',
'linkedin',
'pinterest',
'reddit',
];
block.innerHTML = '';
const platforms = getPlatforms(block);
const rows = block.querySelectorAll(':scope > div');
const childDiv = rows[0].querySelector(':scope > div');
const emptyRow = childDiv?.innerText.trim() === '';
/* eslint-disable no-confusing-arrow,no-useless-escape */
ryanmparrish marked this conversation as resolved.
Show resolved Hide resolved
const toSentenceCase = (str) => str && typeof str === 'string' ? str.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g, (c) => c.toUpperCase()) : '';
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: If we're already disabling eslint for this line, do you think we could break it out into multiple lines for better readability?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yep. Good call.
I added the /* eslint-disable-next-line no-useless-escape */ part
(I tested removing those 'useless' escapes and it didn't 'Sentence Case' right so I left that part as is)
I adjusted the arg into a few lines for better readability and fixes 'no-confusing-arrow' clause. :)
Thanks for the feedback


if (block.classList.contains('inline')) {
rows[0].innerHTML = '';
} else {
rows[0]?.classList.add('tracking-header');
// add share placeholder if empty row
if (childDiv && emptyRow) {
const heading = toSentenceCase(await replaceKey('share-this-page', config));
childDiv.append(createTag('p', null, heading));
}
}

// wrap innerHTML in <p> tag if none are present
if (childDiv && !emptyRow) {
const innerPs = childDiv.querySelectorAll(':scope > p');
if (innerPs.length === 0) {
const text = childDiv.innerText;
childDiv.innerText = '';
childDiv.append(createTag('p', null, text));
}
}

const clipboardSupport = !!navigator.clipboard;
if (clipboardSupport) platforms.push('clipboard');
const svgs = await getSVGsfromFile(
`${base}/blocks/share/share.svg`,
platforms,
);
if (!svgs) return;
/* eslint-disable no-confusing-arrow,no-useless-escape */
const toSentenceCase = (str) => str && typeof str === 'string' ? str.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g, (c) => c.toUpperCase()) : '';

const shareToText = toSentenceCase(await replaceKey('share-to', config));
const url = encodeURIComponent(window.location.href);
const title = document.title ?? url;
Expand Down Expand Up @@ -98,13 +121,11 @@ export default async function decorate(block) {
href: `https://reddit.com/submit?url=${url}&title=${title}`,
};
default:
/* c8 ignore next 1 */
return null;
}
};
if (!block.classList.contains('inline')) {
const heading = toSentenceCase(await replaceKey('share-this-page', config));
block.append(createTag('p', { class: 'tracking-header' }, heading));
}

const container = createTag('p', { class: 'icon-container' });
svgs.forEach(async (svg) => {
if (svg.name === 'clipboard') return;
Expand Down
9 changes: 8 additions & 1 deletion libs/blocks/share/share.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions test/blocks/share/mocks/body.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
</div>
</div>
</div>

<div class="share title">
<div>
<div>Custom Title</div>
</div>
</div>

<div class="section inline-no-siblings">
<div class="share inline">
<div>
Expand Down
6 changes: 6 additions & 0 deletions test/blocks/share/share.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ describe('Share', () => {
expect(re).to.exist;
expect(tw).to.not.exist;
});
it('Share w/ custom title exists', async () => {
const shareEl = document.querySelector('.share.title');
await init(shareEl);
const p = shareEl.querySelector('.tracking-header p');
expect(p).to.exist;
});
it('Inline variant (with inline siblings) creates an inline-wrapper element', async () => {
const section = document.querySelector('.section.inline-has-siblings');
const shareEls = section.querySelectorAll('.share.inline');
Expand Down
Loading