Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22402720
colors-builder.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None
colors-builder.ts
View Options
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import
{
build
}
from
"esbuild"
;
import
tempfile
from
"tempfile"
;
import
{
promises
as
fs
}
from
"fs"
;
import
{
resolve
}
from
"path"
;
import
{
colord
}
from
"colord"
;
import
{
formatWithPrettier
}
from
"../utils/prettier.js"
;
import
{
Builder
}
from
"./builder.js"
;
import
{
exec
}
from
"../utils/exec.js"
;
export
const
COLORS_TS_PATH
=
"src/back/colors.ts"
;
// keeping it in src/back instead of just src to make typescript hints work there
export
const
COLORS_CSS_PATH
=
"src/colors.css"
;
export
const
COLORS_HTML_PATH
=
"src/colors.html"
;
export
class
ColorsBuilder
extends
Builder
{
getName
()
:
string
{
return
"colors"
;
}
ownsFile
(
file_path
:
string
)
{
return
file_path
==
COLORS_TS_PATH
;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async
dispose
()
:
Promise
<
void
>
{}
async
_build
()
{
const
outfile
=
tempfile
({
extension
:
"mjs"
});
await
build
({
entryPoints
:
[
COLORS_TS_PATH
],
bundle
:
true
,
format
:
"esm"
,
outfile
,
});
await
fs
.
appendFile
(
outfile
,
"console.log(JSON.stringify(colors))"
);
const
output
=
await
exec
(
"node"
,
[
outfile
]);
const
colors
=
JSON
.
parse
(
output
.
stdout
)
as
Record
<
string
,
Record
<
string
,
string
>
>
;
let
css_colors
=
""
;
let
html_colors
=
""
;
for
(
const
group_name
in
colors
)
{
const
sorted_colors
=
Object
.
entries
(
colors
[
group_name
]).
sort
(
([
key1
],
[
key2
])
=>
(
key1
>
key2
?
1
:
-
1
)
);
html_colors
+=
`<h2>
${
group_name
}
</h2><div class="group"><div class="base">`
;
let
last_color_name_base
=
""
;
for
(
const
[
color_name
,
color
]
of
sorted_colors
)
{
const
color_name_elements
=
color_name
.
split
(
"-"
);
const
color_name_base
=
color_name_elements
.
filter
((
s
)
=>
!
/^(light|dark)[0-9]$/
.
test
(
s
))
.
join
(
"-"
);
let
index
=
0
;
const
modifier
=
color_name_elements
.
at
(
-
1
);
let
multiplier
=
1
;
let
steps
=
0
;
if
(
modifier
)
{
if
(
modifier
.
startsWith
(
"dark"
))
{
multiplier
=
-
1
;
}
}
if
(
modifier
&&
modifier
.
match
(
/[0-9]$/
)
&&
modifier
.
match
(
/^(dark|light)[0-9]/
)
)
{
steps
=
parseInt
(
modifier
.
at
(
-
1
)
!
);
}
index
=
multiplier
*
steps
;
const
is_new_base
=
color_name_base
!=
last_color_name_base
;
css_colors
+=
`--color-
${
group_name
}
-
${
color_name
}
:
${
color
}
;`
;
if
(
is_new_base
)
{
html_colors
+=
`</div><h3>
${
color_name_base
}
</h3><div class="base" base="
${
color_name_base
}
">`
;
}
if
(
color
!=
"#000000"
&&
color
!=
"#ffffff"
)
{
html_colors
+=
/* HTML */
`<div
class="color-box
${
colord
(
color
).
isDark
()
?
"is-dark"
:
""
}
${
index
==
0
?
"is-main"
:
""
}
"
style="background-color:
${
color
}
; order:
${
index
}
"
data-color-name="
${
color_name
}
"
data-color-group="
${
group_name
}
"
></div>`
;
}
last_color_name_base
=
color_name_base
;
}
html_colors
+=
"</div></div>"
;
// close group and base
}
const
css
=
await
formatWithPrettier
(
`/* DO NOT EDIT! This file is automatically generated by sealgen */
:root {
${
css_colors
}
}`
,
"css"
);
const
html
=
await
formatWithPrettier
(
/* HTML */
`<!DOCTYPE html>
<html>
<head>
<style>
* {
font-family: sans-serif;
}
.base {
display: flex;
}
.color-box {
width: 100px;
height: 100px;
transition: all 50ms;
transition-timing-function: ease-in-out;
cursor: pointer;
&:hover {
transform: scale(1.1);
}
&.is-main {
height: 116px;
width: 116px;
margin-top: -8px;
box-shadow: 0px 0px 6px 2px white;
z-index: 1;
}
}
.color-box:after {
content: attr(data-color-name);
display: block;
color: black;
font-size: 14px;
width: 100%;
height: 100%;
padding: 10px;
opacity: 0.3;
}
.color-box.is-dark:after {
color: white;
}
@keyframes float-up {
from {
transform: translateY(0);
opacity: 1;
}
to {
transform: translateY(-20px);
opacity: 0;
}
}
.toast {
background-color: black;
position: absolute;
color: white;
border-radius: 8px;
padding: 8px;
animation: float-up 800ms;
animation-timing-function: ease-out;
z-index: 2;
}
</style>
</head>
<body>
${
html_colors
}
</body>
<script>
document.addEventListener("click", (event) => {
navigator.clipboard.writeText(
\`var(--color-\${event.target.getAttribute(
"data-color-group"
)}-\${event.target.getAttribute(
"data-color-name"
)})\`
);
const toast = document.createElement("div");
document.body.appendChild(toast);
toast.classList.add("toast");
toast.textContent = "Copied!";
toast.style.setProperty(
"top",
event.clientY +
document.scrollingElement.scrollTop -
40 +
"px"
);
toast.style.setProperty(
"left",
event.clientX + "px"
);
toast.addEventListener("animationend", () => {
toast.remove();
});
});
</script>
</html>`
,
"html"
);
await
Promise
.
all
([
fs
.
writeFile
(
resolve
(
this
.
project_dir
,
COLORS_CSS_PATH
),
css
),
fs
.
writeFile
(
resolve
(
this
.
project_dir
,
COLORS_HTML_PATH
),
html
),
]);
}
}
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Nov 27, 18:39 (1 d, 1 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1218627
Default Alt Text
colors-builder.ts (5 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline