-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: n-icons now replace text without removing event handlers (#800)
* Find and replace is working with plugin * n-icons is almost working with plugin * Updated height of SVG n-icon and added more notes * Uninstalled old package * Updated JS logic with better comments * Removed old example * Set homepage back to how it should be
- Loading branch information
1 parent
224c6dd
commit 546550f
Showing
6 changed files
with
227 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,39 @@ | ||
import * as findAndReplaceDOMText from 'findandreplacedomtext'; // https://github.com/padolsey/findAndReplaceDOMText | ||
|
||
const nIcons = () => { | ||
// Function to replace text with our icon | ||
const replaceTextWithIcon = (textNode, phraseToReplace) => { | ||
if (!textNode.includes(phraseToReplace)) return textNode; // If textNode doesn't include our matched text, then return | ||
// Convert a html string into an actual HTML node | ||
const convertHtmlStringToNode = htmlString => | ||
document.createRange().createContextualFragment(htmlString).firstChild; | ||
|
||
// Otherwise... | ||
const textWithoutN = phraseToReplace.substring(1); // Chop the first character from our string (the 'n') as it will be replaced with an icon | ||
const convertStringToNode = string => { | ||
const textWithoutN = string.substring(1); // Chop the first character from our string (the 'n') as it will be replaced with an icon | ||
// HTML/SVG of icon we will use | ||
const svgIcon = ` | ||
<svg class="wmnds-n-icon__svg"> | ||
<title>N-Network icon</title> | ||
<desc>A hexagon with the letter 'n' inside of it.</desc> | ||
<use xlink:href="#wmnds-general-n-ticket" href="#wmnds-general-n-ticket"></use> | ||
</svg>`; | ||
<svg class="wmnds-n-icon__svg"> | ||
<title>N-Network icon</title> | ||
<desc>A hexagon with the letter 'n' inside of it.</desc> | ||
<use xlink:href="#wmnds-general-n-ticket" href="#wmnds-general-n-ticket"></use> | ||
</svg>`; | ||
// Then replace our found text with 'n' icon and text without 'n' | ||
const modifiedTextNode = textNode.replace( | ||
new RegExp(phraseToReplace, 'ig'), | ||
`<span class="wmnds-n-icon">${svgIcon}${textWithoutN}</span>` | ||
); | ||
const htmlString = `<span class="wmnds-n-icon">${svgIcon}${textWithoutN}</span>`; | ||
|
||
return modifiedTextNode; // Return modified textNode back | ||
return convertHtmlStringToNode(htmlString); | ||
}; | ||
|
||
function replacePhraseInHTML(htmlEle, phrase) { | ||
const phraseToReplace = phrase; | ||
const insideHTMLTags = new RegExp('(<.+?>|&\\w+;)'); // Get items within HTML </> tags | ||
const textNodes = htmlEle.split(insideHTMLTags).filter(Boolean); // Split on the HTML tags to avoid manipulating attributes etc. | ||
|
||
// Loop through textNodes and any that match 'insideHTMLTags', return straight away | ||
// Otherwise, that textNode is ready to be searched for our textToChange | ||
const updatedDOM = textNodes | ||
.map(DOMText => | ||
insideHTMLTags.test(DOMText) ? DOMText : replaceTextWithIcon(DOMText, phraseToReplace) | ||
) | ||
.join(''); | ||
const replaceNText = () => { | ||
const wordsToReplace = /(nNetwork|nTicket|nTrain|nBus)/; // Pipe in any words that we should replace the 'n' with the SVG 'n' icon (not case sensitive) | ||
const bodyEle = document.querySelector('body'); | ||
|
||
return updatedDOM; | ||
} | ||
// Read https://github.com/padolsey/findAndReplaceDOMText on how this works | ||
findAndReplaceDOMText(bodyEle, { | ||
preset: 'prose', | ||
find: new RegExp(wordsToReplace, 'gi'), | ||
replace: (_portion, match) => convertStringToNode(match[0]) | ||
}); | ||
}; | ||
|
||
// START HERE | ||
const bodyElement = document.querySelector('body'); | ||
const phrasesToReplace = ['nNetwork', 'nTicket', 'nTrain', 'nBus']; // These are the phrases we want to replace the first char with the 'n' icon SVG (they are not case sensitive - we match on upper and lowercase with regex 'i' on line 17) | ||
|
||
// Loop through each phrase and update the DOM body innerHTML with the replaced phrase | ||
phrasesToReplace.forEach(phraseToReplace => { | ||
bodyElement.innerHTML = replacePhraseInHTML(bodyElement.innerHTML, phraseToReplace); | ||
}); | ||
replaceNText(); | ||
}; | ||
|
||
export default nIcons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters