-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from 10play/dynamic-css
Add `injectCSS` for Dynamic CSS
- Loading branch information
Showing
8 changed files
with
161 additions
and
50 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type BridgeExtension from '../bridges/base'; | ||
|
||
/** | ||
* Creates a new style element and appends it to the head of the document. | ||
* If the style element already exists, it will update the content of the existing element. | ||
* @param css - array of css strings | ||
* @param styleSheetTag - a unique tag to identify the style element - if not provided, a new style element will be created | ||
* @returns a string of javascript that is ready to be injected into the rich text webview | ||
*/ | ||
export const getStyleSheetCSS = (css: string, styleSheetTag: string) => { | ||
return ` | ||
cssContent = \`${css}\`; | ||
head = document.head || document.getElementsByTagName('head')[0], | ||
styleElement = head.querySelector('style[data-tag="${styleSheetTag}"]'); | ||
if (!styleElement) { | ||
// If no such element exists, create a new <style> element. | ||
styleElement = document.createElement('style'); | ||
styleElement.setAttribute('data-tag', '${styleSheetTag}'); // Assign the unique 'data-tag' attribute. | ||
styleElement.type = 'text/css'; // Specify the type attribute for clarity. | ||
head.appendChild(styleElement); // Append the newly created <style> element to the <head>. | ||
} | ||
styleElement.innerHTML = cssContent; | ||
`; | ||
}; | ||
|
||
export const getInjectedJS = (bridgeExtensions: BridgeExtension[]) => { | ||
let injectJS = ''; | ||
// For each bridge extension, we create a stylesheet with it's name as the tag | ||
const styleSheets = bridgeExtensions.map(({ extendCSS, name }) => | ||
getStyleSheetCSS(extendCSS || '', name) | ||
); | ||
injectJS += styleSheets.join(' '); | ||
return injectJS; | ||
}; |
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
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