Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
3c1a416a
Unverified
Commit
3c1a416a
authored
Aug 09, 2022
by
XQ
Committed by
GitHub
Aug 09, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update `i18nStore` (#141)
parent
972a49d6
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
97 deletions
+55
-97
getFontsStyleElement.ts
web/src/labs/html2image/getFontsStyleElement.ts
+0
-46
createI18nStore.tsx
web/src/labs/i18n/createI18nStore.tsx
+52
-0
i18nStore.ts
web/src/labs/i18n/i18nStore.ts
+1
-50
useI18n.ts
web/src/labs/i18n/useI18n.ts
+2
-1
No files found.
web/src/labs/html2image/getFontsStyleElement.ts
deleted
100644 → 0
View file @
972a49d6
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
;
web/src/labs/i18n/createI18nStore.tsx
0 → 100644
View file @
3c1a416a
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
;
web/src/labs/i18n/i18nStore.ts
View file @
3c1a416a
type
I18nState
=
Readonly
<
{
import
createI18nStore
from
"./createI18nStore"
;
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
,
};
};
const
defaultI18nState
=
{
const
defaultI18nState
=
{
locale
:
"en"
,
locale
:
"en"
,
...
...
web/src/labs/i18n/useI18n.ts
View file @
3c1a416a
...
@@ -25,7 +25,8 @@ const useI18n = () => {
...
@@ -25,7 +25,8 @@ const useI18n = () => {
const
translate
=
(
key
:
string
)
=>
{
const
translate
=
(
key
:
string
)
=>
{
try
{
try
{
return
resources
[
locale
][
key
]
as
string
;
const
value
=
resources
[
locale
][
key
]
as
string
;
return
value
;
}
catch
(
error
)
{
}
catch
(
error
)
{
return
key
;
return
key
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment