mirror of https://github.com/usememos/memos
chore: update `i18nStore` (#141)
parent
972a49d6aa
commit
3c1a416afc
@ -1,46 +0,0 @@
|
|||||||
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;
|
|
@ -0,0 +1,52 @@
|
|||||||
|
type I18nState = Readonly<{
|
||||||
|
locale: string;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
type Listener = (ns: I18nState, ps?: I18nState) => void;
|
||||||
|
|
||||||
|
const createI18nStore = (preloadedState: I18nState) => {
|
||||||
|
const listeners: Listener[] = [];
|
||||||
|
let currentState = preloadedState;
|
||||||
|
|
||||||
|
const getState = () => {
|
||||||
|
return currentState;
|
||||||
|
};
|
||||||
|
|
||||||
|
const setState = (state: Partial<I18nState>) => {
|
||||||
|
const nextState = {
|
||||||
|
...currentState,
|
||||||
|
...state,
|
||||||
|
};
|
||||||
|
const prevState = currentState;
|
||||||
|
currentState = nextState;
|
||||||
|
|
||||||
|
for (const cb of listeners) {
|
||||||
|
cb(currentState, prevState);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const subscribe = (listener: Listener) => {
|
||||||
|
let isSubscribed = true;
|
||||||
|
listeners.push(listener);
|
||||||
|
|
||||||
|
const unsubscribe = () => {
|
||||||
|
if (!isSubscribed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const index = listeners.indexOf(listener);
|
||||||
|
listeners.splice(index, 1);
|
||||||
|
isSubscribed = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
return unsubscribe;
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
getState,
|
||||||
|
setState,
|
||||||
|
subscribe,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default createI18nStore;
|
Loading…
Reference in New Issue