Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F7112537
html.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
html.ts
View Options
import
{
FlatTemplatable
,
Templatable
,
tempstream
}
from
"tempstream"
;
import
{
Readable
}
from
"stream"
;
import
{
BaseContext
}
from
"koa"
;
import
{
default
as
default_navbar
}
from
"./routes/common/navbar.js"
;
import
{
toKebabCase
}
from
"js-convert-case"
;
export
const
defaultHead
=
(
ctx
:
BaseContext
,
title
:
string
,
options
:
HTMLOptions
)
=>
/* HTML */
`<title>
${
title
}
·
${
ctx
.
$app
.
manifest
.
name
}
</title>
<meta name="viewport" content="width=device-width" />
<script async src="/dist/bundle.js"></script>
<link
href="/dist/main.css
${
options
.
autoRefreshCSS
?
`?
${
Math
.
random
()
}${
Math
.
random
()
}
`
:
""
}
"
rel="stylesheet"
type="text/css"
/>
${
options
.
morphing
?
`<meta name="turbo-refresh-method" content="morph" />`
:
""
}
${
options
.
preserveScroll
?
`<meta name="turbo-refresh-scroll" content="preserve">`
:
""
}
`
;
export
type
HTMLOptions
=
{
preserveScroll
?:
boolean
;
morphing
?:
boolean
;
navbar
?:
(
ctx
:
BaseContext
)
=>
FlatTemplatable
;
autoRefreshCSS
?:
boolean
;
};
export
default
function
html
(
ctx
:
BaseContext
,
title
:
string
,
body
:
Templatable
,
htmlOptions
:
HTMLOptions
=
{},
makeHead
:
(
ctx
:
BaseContext
,
title
:
string
,
options
:
HTMLOptions
)
=>
Templatable
=
defaultHead
)
:
Readable
{
ctx
.
set
(
"content-type"
,
"text/html;charset=utf-8"
);
return
tempstream
/* HTML */
` <!DOCTYPE html>
<html lang="pl" class="title--
${
toKebabCase
(
title
)
}
">
<head>
${
makeHead
(
ctx
,
title
,
htmlOptions
)
}
</head>
<body>
${
(
htmlOptions
.
navbar
||
default_navbar
)(
ctx
)
}
${
body
}
${
htmlOptions
.
autoRefreshCSS
?
/* HTML */
`<script>
function make_new_link() {
const new_link = document.createElement("link");
new_link.rel = "stylesheet";
new_link.href = \`/dist/main.css?\${Math.random()}+\${Math.random()}\`;
new_link.type = "text/css";
return new_link;
}
function getStyles() {
return Array.from(
document.querySelectorAll("head link")
).filter(
(e) => new URL(e.href).pathname == "/dist/main.css"
);
}
function cleanup_css() {
console.log("clearing styles");
getStyles()
.slice(0, -1)
.forEach((style) => {
style.parentElement.removeChild(style);
});
}
document.documentElement.addEventListener(
"turbo:morph",
cleanup_css
);
const sleep = (time) =>
new Promise((resolve) => {
setTimeout(resolve, time);
});
let last_known_start_timestamp = 0;
async function wait_for_app_restart() {
while (true) {
const { started_at, status } = await fetch(
"/status.json"
)
.then((r) => r.json())
.catch(() => ({
started_at: last_known_start_timestamp,
}));
if (started_at !== last_known_start_timestamp) {
last_known_start_timestamp = started_at;
return;
}
await sleep(100);
}
}
(async function () {
const { started_at, status } = await fetch(
"/status.json"
).then((r) => r.json());
last_known_start_timestamp = started_at;
const { port, watch } = await fetch(
"/dist/notifier.json"
).then((r) => r.json());
if (!watch) {
console.warning(
"Not running auto refresh on watch because the build process is not running in watch mode"
);
return;
}
const socket = new WebSocket(\`ws://localhost:\${port}\`);
socket.onmessage = async (message) => {
if (message.data === "css") {
const new_link = make_new_link();
new_link.onload = cleanup_css;
document
.querySelector("head")
.appendChild(new_link);
}
if (message.data === "ts") {
document.documentElement.classList.add(
"restarting"
);
await wait_for_app_restart();
document.documentElement.dispatchEvent(
new Event("ts-rebuilt")
);
document.documentElement.classList.remove(
"restarting"
);
}
};
})();
</script>`
:
""
}
</body>
</html>`
;
}
File Metadata
Details
Attached
Mime Type
text/html
Expires
Thu, Jul 3, 19:22 (6 h, 47 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
799706
Default Alt Text
html.ts (4 KB)
Attached To
Mode
rPLAY Sealious playground
Attached
Detach File
Event Timeline
Log In to Comment