-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #2491 Closes #2505 This technically contains a breaking change as icons can no longer be overwritten via DefaultThemeRenderContext, but no published theme uses this, so I've decided not to care.
- Loading branch information
Showing
10 changed files
with
136 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Component, RendererComponent } from "../components"; | ||
import { RendererEvent } from "../events"; | ||
import { writeFile } from "../../utils/fs"; | ||
import { DefaultTheme } from "../themes/default/DefaultTheme"; | ||
import { join } from "path"; | ||
import { JSX, renderElement } from "../../utils"; | ||
|
||
/** | ||
* Plugin which is responsible for creating an icons.js file that embeds the icon SVGs | ||
* within the page on page load to reduce page sizes. | ||
*/ | ||
@Component({ name: "icons" }) | ||
export class IconsPlugin extends RendererComponent { | ||
iconHtml?: string; | ||
|
||
override initialize() { | ||
this.listenTo(this.owner, { | ||
[RendererEvent.BEGIN]: this.onBeginRender, | ||
}); | ||
} | ||
|
||
private onBeginRender(_event: RendererEvent) { | ||
if (this.owner.theme instanceof DefaultTheme) { | ||
this.owner.postRenderAsyncJobs.push((event) => this.onRenderEnd(event)); | ||
} | ||
} | ||
|
||
private async onRenderEnd(event: RendererEvent) { | ||
const children: JSX.Element[] = []; | ||
const icons = (this.owner.theme as DefaultTheme).icons; | ||
|
||
for (const [name, icon] of Object.entries(icons)) { | ||
children.push(<g id={`icon-${name}`}>{icon.call(icons).children}</g>); | ||
} | ||
|
||
const svg = renderElement(<svg xmlns="http://www.w3.org/2000/svg">{children}</svg>); | ||
const js = [ | ||
"(function(svg) {", | ||
" svg.innerHTML = `" + renderElement(<>{children}</>).replaceAll("`", "\\`") + "`;", | ||
" svg.style.display = 'none';", | ||
" if (location.protocol === 'file:') {", | ||
" if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', updateUseElements);", | ||
" else updateUseElements()", | ||
" function updateUseElements() {", | ||
" document.querySelectorAll('use').forEach(el => {", | ||
" if (el.getAttribute('href').includes('#icon-')) {", | ||
" el.setAttribute('href', el.getAttribute('href').replace(/.*#/, '#'));", | ||
" }", | ||
" });", | ||
" }", | ||
" }", | ||
"})(document.body.appendChild(document.createElementNS('http://www.w3.org/2000/svg', 'svg')))", | ||
].join("\n"); | ||
|
||
const svgPath = join(event.outputDirectory, "assets/icons.svg"); | ||
const jsPath = join(event.outputDirectory, "assets/icons.js"); | ||
|
||
await Promise.all([writeFile(svgPath, svg), writeFile(jsPath, js)]); | ||
} | ||
} |
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,5 +1,6 @@ | ||
export { MarkedPlugin } from "../themes/MarkedPlugin"; | ||
export { AssetsPlugin } from "./AssetsPlugin"; | ||
export { IconsPlugin } from "./IconsPlugin"; | ||
export { JavascriptIndexPlugin } from "./JavascriptIndexPlugin"; | ||
export { NavigationPlugin } from "./NavigationPlugin"; | ||
export { SitemapPlugin } from "./SitemapPlugin"; |
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
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