Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
chore: update `i18nStore` (#141)
XQ3c1a416afcb9
diff --git a/web/src/labs/html2image/getFontsStyleElement.ts b/web/src/labs/html2image/getFontsStyleElement.tsdeleted file mode 100644index fef390ff5..000000000--- a/web/src/labs/html2image/getFontsStyleElement.ts+++ /dev/null@@ -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;diff --git a/web/src/labs/i18n/createI18nStore.tsx b/web/src/labs/i18n/createI18nStore.tsxnew file mode 100644index 000000000..10a0c56ef--- /dev/null+++ b/web/src/labs/i18n/createI18nStore.tsx@@ -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;diff --git a/web/src/labs/i18n/i18nStore.ts b/web/src/labs/i18n/i18nStore.tsindex 05ebb1f35..a3a86ae49 100644--- a/web/src/labs/i18n/i18nStore.ts+++ b/web/src/labs/i18n/i18nStore.ts@@ -1,53 +1,4 @@-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,- };-};+import createI18nStore from "./createI18nStore";const defaultI18nState = {locale: "en",diff --git a/web/src/labs/i18n/useI18n.ts b/web/src/labs/i18n/useI18n.tsindex 760b7b2a8..0334b578d 100644--- a/web/src/labs/i18n/useI18n.ts+++ b/web/src/labs/i18n/useI18n.ts@@ -25,7 +25,8 @@ const useI18n = () => {const translate = (key: string) => {try {- return resources[locale][key] as string;+ const value = resources[locale][key] as string;+ return value;} catch (error) {return key;}