mirror of
https://github.com/hex248/fonts.git
synced 2026-02-07 18:23:06 +00:00
202 lines
6.8 KiB
HTML
202 lines
6.8 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>fonts.ob248.com</title>
|
|
<link rel="stylesheet" href="/index.css" />
|
|
<style id="font-import">/* FONT_IMPORT */</style>
|
|
</head>
|
|
<body class="page">
|
|
<header class="decal-grid decal-grid--header">
|
|
fonts.ob248.com
|
|
<input
|
|
id="font-search"
|
|
type="search"
|
|
placeholder="SEARCH"
|
|
class="search__input"
|
|
autofocus
|
|
/>
|
|
</header>
|
|
|
|
<main class="page__main">
|
|
<section class="catalog">
|
|
<!-- FONT_CARDS -->
|
|
</section>
|
|
|
|
<footer class="decal-grid" style="margin-top: 2rem; border-top: 1.5px solid var(--border-color); padding-top: 1rem;">
|
|
<div class="decal-text-block">
|
|
OLIVER BRYAN
|
|
</div>
|
|
<div class="decal-text-block" style="margin-left: auto; text-align: right;">
|
|
<a href="https://ob248.com" target="_blank" style="color: var(--fg-color);">ob248.com</a>
|
|
</div>
|
|
</footer>
|
|
</main>
|
|
|
|
<script>
|
|
const searchInput = document.querySelector("#font-search");
|
|
const fontCards = document.querySelectorAll("[data-font-card]");
|
|
|
|
const buildImportSnippet = (importUrl) => {
|
|
if (!importUrl) {
|
|
return "";
|
|
}
|
|
return `@import url("${window.location.origin}${importUrl}");`;
|
|
};
|
|
|
|
const copyText = async (text) => {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
return true;
|
|
} catch (error) {
|
|
const textarea = document.createElement("textarea");
|
|
textarea.value = text;
|
|
textarea.setAttribute("readonly", "true");
|
|
textarea.style.position = "absolute";
|
|
textarea.style.left = "-9999px";
|
|
document.body.appendChild(textarea);
|
|
textarea.select();
|
|
document.execCommand("copy");
|
|
document.body.removeChild(textarea);
|
|
return true;
|
|
}
|
|
};
|
|
|
|
const parseWeights = (value) => {
|
|
if (!value) {
|
|
return [];
|
|
}
|
|
return value
|
|
.split(",")
|
|
.map((entry) => Number.parseInt(entry, 10))
|
|
.filter((weight) => Number.isFinite(weight))
|
|
.sort((a, b) => a - b);
|
|
};
|
|
|
|
fontCards.forEach((card) => {
|
|
const importUrl = card.getAttribute("data-import-url");
|
|
const importSnippet = buildImportSnippet(importUrl);
|
|
const importOutput = card.querySelector("[data-import]");
|
|
const copyButton = card.querySelector("[data-copy]");
|
|
const weightSelect = card.querySelector("[data-weight-select]");
|
|
const weightRange = card.querySelector("[data-weight-range]");
|
|
const demoText = card.querySelector("[data-demo]");
|
|
const weightValue = card.querySelector("[data-weight-value]");
|
|
const weights = parseWeights(card.getAttribute("data-weights"));
|
|
const defaultWeight = Number.parseInt(
|
|
card.getAttribute("data-default-weight") ?? "",
|
|
10,
|
|
);
|
|
const weightMin = Number.parseInt(
|
|
card.getAttribute("data-weight-min") ?? "",
|
|
10,
|
|
);
|
|
const weightMax = Number.parseInt(
|
|
card.getAttribute("data-weight-max") ?? "",
|
|
10,
|
|
);
|
|
const weightStep = Number.parseInt(
|
|
card.getAttribute("data-weight-step") ?? "10",
|
|
10,
|
|
);
|
|
|
|
if (importOutput) {
|
|
importOutput.textContent = importSnippet;
|
|
}
|
|
|
|
if (!weightRange && !weightSelect) {
|
|
if (demoText && Number.isFinite(defaultWeight)) {
|
|
demoText.style.fontWeight = String(defaultWeight);
|
|
}
|
|
} else if (weightRange && Number.isFinite(weightMin) && Number.isFinite(weightMax)) {
|
|
const step = Number.isFinite(weightStep) && weightStep > 0 ? weightStep : 10;
|
|
const midpoint = Math.round((weightMin + weightMax) / 2);
|
|
const initialWeight = Number.isFinite(defaultWeight)
|
|
? defaultWeight
|
|
: midpoint;
|
|
const normalizedWeight = Math.min(
|
|
weightMax,
|
|
Math.max(
|
|
weightMin,
|
|
Math.round((initialWeight - weightMin) / step) * step + weightMin,
|
|
),
|
|
);
|
|
weightRange.min = String(weightMin);
|
|
weightRange.max = String(weightMax);
|
|
weightRange.step = String(step);
|
|
weightRange.value = String(normalizedWeight);
|
|
if (weightValue) {
|
|
weightValue.textContent = String(normalizedWeight);
|
|
}
|
|
if (demoText) {
|
|
demoText.style.fontWeight = String(normalizedWeight);
|
|
}
|
|
weightRange.addEventListener("input", (event) => {
|
|
const value = Number.parseInt(event.target.value, 10);
|
|
if (!Number.isFinite(value)) {
|
|
return;
|
|
}
|
|
if (weightValue) {
|
|
weightValue.textContent = String(value);
|
|
}
|
|
if (demoText) {
|
|
demoText.style.fontWeight = String(value);
|
|
}
|
|
});
|
|
} else if (weightSelect && weights.length > 0) {
|
|
weightSelect.innerHTML = "";
|
|
weights.forEach((weight) => {
|
|
const option = document.createElement("option");
|
|
option.value = String(weight);
|
|
option.textContent = String(weight);
|
|
weightSelect.appendChild(option);
|
|
});
|
|
const initialWeight = Number.isFinite(defaultWeight)
|
|
? defaultWeight
|
|
: weights[Math.floor((weights.length - 1) / 2)];
|
|
weightSelect.value = String(initialWeight);
|
|
if (demoText) {
|
|
demoText.style.fontWeight = String(initialWeight);
|
|
}
|
|
weightSelect.addEventListener("change", (event) => {
|
|
const value = Number.parseInt(event.target.value, 10);
|
|
if (!Number.isFinite(value)) {
|
|
return;
|
|
}
|
|
if (demoText) {
|
|
demoText.style.fontWeight = String(value);
|
|
}
|
|
});
|
|
}
|
|
|
|
if (copyButton) {
|
|
copyButton.addEventListener("click", async () => {
|
|
if (!importSnippet) {
|
|
return;
|
|
}
|
|
const copied = await copyText(importSnippet);
|
|
if (copied) {
|
|
copyButton.textContent = "Copied";
|
|
setTimeout(() => {
|
|
copyButton.textContent = "Copy";
|
|
}, 1500);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
if (searchInput) {
|
|
searchInput.addEventListener("input", (event) => {
|
|
const value = event.target.value.toLowerCase();
|
|
fontCards.forEach((card) => {
|
|
const name = card.getAttribute("data-font-name") ?? "";
|
|
const matches = name.includes(value);
|
|
card.classList.toggle("hidden", !matches);
|
|
});
|
|
});
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|