mirror of https://github.com/usememos/memos
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import convertResourceToDataURL from "./convertResourceToDataURL";
|
|
|
|
const getFontsStyleElement = async (element: HTMLElement) => {
|
|
const styleSheets = element.ownerDocument.styleSheets;
|
|
const fontFamilyStyles: CSSStyleDeclaration[] = [];
|
|
|
|
for (const sheet of styleSheets) {
|
|
for (const rule of sheet.cssRules) {
|
|
if (rule.constructor.name === "CSSFontFaceRule") {
|
|
fontFamilyStyles.push((rule as CSSFontFaceRule).style);
|
|
}
|
|
}
|
|
}
|
|
|
|
const styleElement = document.createElement("style");
|
|
|
|
for (const f of fontFamilyStyles) {
|
|
const fontFamily = f.getPropertyValue("font-family");
|
|
const fontWeight = f.getPropertyValue("font-weight");
|
|
const src = f.getPropertyValue("src");
|
|
const resourceUrls = src.split(",").map((s) => {
|
|
return s.replace(/url\("?(.+?)"?\)/, "$1");
|
|
});
|
|
const base64Urls: string[] = [];
|
|
|
|
for (const url of resourceUrls) {
|
|
try {
|
|
const base64Url = await convertResourceToDataURL(url);
|
|
base64Urls.push(`url("${base64Url}")`);
|
|
} catch (error) {
|
|
// do nth
|
|
}
|
|
}
|
|
|
|
styleElement.innerHTML += `
|
|
@font-face {
|
|
font-family: "${fontFamily}";
|
|
src: ${base64Urls.join(",")};
|
|
font-weight: ${fontWeight};
|
|
}`;
|
|
}
|
|
|
|
return styleElement;
|
|
};
|
|
|
|
export default getFontsStyleElement;
|