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

Fix doc icons not loading when switching pages without refreshing #170

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 0 additions & 26 deletions docs/src/.vitepress/theme/FluentIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,6 @@
import { computed } from "vue";

const props = defineProps(["name"]);

const link = computed(() => {
const sizeIndex = props.name.search(/\d+/g);
if (sizeIndex === -1) {
// error case, fallback
console.error("Invalid icon name: " + props.name);
return props.name + ".svg";
}
const modifierIndex = props.name.substring(sizeIndex).search(/(Regular|Filled)/g);
if (modifierIndex === -1) {
// error case, fallback
console.error("Invalid icon name: " + props.name);
return props.name + ".svg";
}
const noSpacePascal = props.name.substring(0, sizeIndex);
const size = props.name.substring(sizeIndex, sizeIndex+modifierIndex);
const modifier = props.name.substring(sizeIndex+modifierIndex);
console.log(noSpacePascal, size, modifier);

const spacedPascal = noSpacePascal.replace(/([A-Z])/g, " $1").trim();
const snake = spacedPascal.replace(/ /g, "_").toLowerCase();
const urlEncoded = encodeURIComponent(spacedPascal);

const file = `ic_fluent_${snake}_${size}_${modifier.toLowerCase()}`;
return `https://raw.githubusercontent.com/microsoft/fluentui-system-icons/main/assets/${urlEncoded}/SVG/${file}.svg`;
});
</script>

<template>
Expand Down
22 changes: 19 additions & 3 deletions docs/src/public/transform-fluent-icon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
(function () {
function load() {
var transformingHref = undefined;
function transformIcons() {
if (window.location.href === transformingHref) {
return;
}
transformingHref = window.location.href;
var elements = document.querySelectorAll("i.fluent-icon");
elements.forEach(function (e) {
var icon = e.getAttribute("data-icon");
Expand Down Expand Up @@ -30,9 +35,20 @@
});
});
}
function initLoadIcons() {
transformIcons();
if (window.MutationObserver) {
var observer = new MutationObserver(transformIcons);
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
});
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", load);
document.addEventListener("DOMContentLoaded", initLoadIcons);
} else {
load();
initLoadIcons();
}
})();