StoneCandy's picture
v0.99.2: VERSION, online notice, version labels, extra tip
314cd24 verified
Raw History Blame Contribute Delete
16.8 kB
/* ๋ชฉ์ฐจ ์ž๋™ ์ƒ์„ฑ + ๋ณธ๋ฌธ/๋ชฉ์ฐจ ์Šคํฌ๋กค ์—ฐ๋™ + ๊ธฐ๋Šฅ ์†Œ๊ฐœ ๋žœ๋ค ํ‘œ์‹œ */
(function () {
"use strict";
var RESYNC_MS = 3000; // ๋ชฉ์ฐจ๋ฅผ ๋งŒ์ง€๊ณ  ์ด ์‹œ๊ฐ„ ์ง€๋‚˜๋ฉด ๋ณธ๋ฌธ ์œ„์น˜๋กœ ๋ณต๊ท€ (๋งˆ์šฐ์Šค๊ฐ€ ๋ชฉ์ฐจ ์œ„๋ฉด ์นด์šดํŠธ ์•ˆ ํ•จ)
var SPY_OFFSET = 100; // ํ™”๋ฉด ์œ„์—์„œ ์ด ๋†’์ด ์•ˆ์— ๋“ค์–ด์˜จ ์ œ๋ชฉ์„ "์ง€๊ธˆ ์ฝ๋Š” ๊ณณ"์œผ๋กœ ๋ด„
var JUMP_MARGIN = 24; // ๋ชฉ์ฐจ ํด๋ฆญ ์‹œ ์ œ๋ชฉ์„ ํ™”๋ฉด ์œ„์—์„œ ์ด๋งŒํผ ๋„์›Œ ๋ฉˆ์ถค
var manual = document.getElementById("manual");
var tocBox = document.getElementById("toc-scroll");
var tocList = document.getElementById("toc-list");
var heads = Array.prototype.slice.call(manual.querySelectorAll("h2[id], h3[id]"));
var linkById = {};
// ---------- ๋ชฉ์ฐจ ์ƒ์„ฑ ----------
function roman(n) {
var r = "", v = [10, 9, 5, 4, 1], c = ["X", "IX", "V", "IV", "I"];
for (var i = 0; i < v.length; i++) while (n >= v[i]) { r += c[i]; n -= v[i]; }
return r;
}
var subCount = 0;
heads.forEach(function (h) {
var li = document.createElement("li");
var a = document.createElement("a");
a.href = "#" + h.id;
if (h.dataset.num) { // ๋Œ€์ œ๋ชฉ์€ ์žฅ ๋ฒˆํ˜ธ๋ฅผ ์•ž์— ๋ถ™์ž„
var num = document.createElement("span");
num.className = "toc-num";
num.textContent = h.dataset.num;
a.appendChild(num);
}
if (h.tagName === "H2") subCount = 0;
if (h.tagName === "H3" && h.closest(".chapter")) { // ์†Œ์ œ๋ชฉ์€ ๋ณธ๋ฌธ์ฒ˜๋Ÿผ ์žฅ๋งˆ๋‹ค I. II. ...
var sub = document.createElement("span");
sub.className = "toc-sub";
sub.textContent = roman(++subCount) + ".";
a.appendChild(sub);
}
var label = document.createElement("span");
label.textContent = h.getAttribute("data-toc") || h.textContent;
a.appendChild(label);
a.className = "lv-" + h.tagName.toLowerCase();
if (h.closest(".draft")) a.classList.add("is-draft");
a.addEventListener("click", function (e) {
e.preventDefault();
goTo(h.id, false);
});
li.appendChild(a);
tocList.appendChild(li);
linkById[h.id] = a;
});
// ---------- ๊ฐ•์กฐ ๋ฐฐ๊ฒฝ(๋„ค๋ชจ ๋ ˆ์ด์–ด) ----------
var marker = document.createElement("div");
marker.className = "toc-marker";
tocBox.insertBefore(marker, tocList);
function moveMarker(id) {
var a = linkById[id];
if (!a) return;
var top = tocList.offsetTop + a.offsetTop; // a์˜ ๊ธฐ์ค€์€ ul(position: relative)
marker.style.transform = "translateY(" + top + "px)";
marker.style.height = a.offsetHeight + "px";
}
window.addEventListener("resize", function () { if (activeId) moveMarker(activeId); });
// ---------- ์ƒํƒœ ----------
var activeId = null;
var lockedId = null; // ๋ชฉ์ฐจ ํด๋ฆญ์œผ๋กœ ์ด๋™ํ•œ ์„น์…˜. ์‚ฌ์šฉ์ž๊ฐ€ ๋ณธ๋ฌธ์„ ์ง์ ‘ ์›€์ง์ด๊ธฐ ์ „๊นŒ์ง€ ๊ณ ์ •
var detached = false; // ์‚ฌ์šฉ์ž๊ฐ€ ๋ชฉ์ฐจ๋ฅผ ์ง์ ‘ ์Šคํฌ๋กคํ•ด์„œ ๋ณธ๋ฌธ๊ณผ ์–ด๊ธ‹๋‚œ ์ƒํƒœ
var hovering = false; // ๋งˆ์šฐ์Šค๊ฐ€ ๋ชฉ์ฐจ ์œ„์— ์žˆ์Œ
var timer = null;
function setActive(id, syncToc) {
if (id === activeId) return;
if (activeId && linkById[activeId]) linkById[activeId].classList.remove("active");
activeId = id;
if (linkById[id]) linkById[id].classList.add("active");
moveMarker(id);
if (syncToc && !detached) scrollTocTo(id, true);
}
// ๋ชฉ์ฐจ ์นธ ์•ˆ์—์„œ ํ•ด๋‹น ํ•ญ๋ชฉ์„ ๊ฐ€์šด๋ฐ๋กœ (๋ชฉ์ฐจ ์นธ๋งŒ ์›€์ง์ž„, ๋ณธ๋ฌธ์€ ์•ˆ ์›€์ง์ž„)
function scrollTocTo(id, smooth) {
var a = linkById[id];
if (!a) return;
var boxTop = tocBox.getBoundingClientRect().top;
var aTop = a.getBoundingClientRect().top;
var target = tocBox.scrollTop + (aTop - boxTop) - (tocBox.clientHeight - a.offsetHeight) / 2;
tocBox.scrollTo({ top: Math.max(0, target), behavior: smooth ? "smooth" : "auto" });
}
// ์ง€๊ธˆ ๋ณธ๋ฌธ์—์„œ ์ฝ๊ณ  ์žˆ๋Š” ์ œ๋ชฉ
function currentHead() {
var doc = document.documentElement;
if (window.innerHeight + window.scrollY >= doc.scrollHeight - 2) return heads[heads.length - 1];
var cur = heads[0];
for (var i = 0; i < heads.length; i++) {
// ๋Œ€์ œ๋ชฉ์€ ์žฅ ๋งจ ์œ„(์žฅ ๋ฒˆํ˜ธ ํฌํ•จ)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํŒ๋‹จ
var ref = (heads[i].tagName === "H2" && heads[i].closest(".chapter")) || heads[i];
if (ref.getBoundingClientRect().top - SPY_OFFSET <= 0) cur = heads[i];
else break;
}
return cur;
}
// ---------- ๋ณธ๋ฌธ ์Šคํฌ๋กค โ†’ ๋ชฉ์ฐจ ๋”ฐ๋ผ์˜ด ----------
var ticking = false;
window.addEventListener("scroll", function () {
if (ticking) return;
ticking = true;
requestAnimationFrame(function () {
ticking = false;
if (lockedId) return;
var h = currentHead();
if (h) setActive(h.id, true);
});
}, { passive: true });
// ์‚ฌ์šฉ์ž๊ฐ€ ๋ณธ๋ฌธ์„ ์ง์ ‘ ์›€์ง์ด๋ฉด ํด๋ฆญ ๊ณ ์ • ํ•ด์ œ
function releaseLock(e) {
if (e && e.target && tocBox.contains(e.target)) return;
if (!lockedId) return;
lockedId = null;
}
window.addEventListener("wheel", releaseLock, { passive: true });
window.addEventListener("touchstart", releaseLock, { passive: true });
window.addEventListener("mousedown", releaseLock);
window.addEventListener("keydown", releaseLock);
// ---------- ๋ชฉ์ฐจ ์ง์ ‘ ์Šคํฌ๋กค โ†’ ๋ณธ๋ฌธ์€ ๊ทธ๋Œ€๋กœ, 3์ดˆ ๋’ค ๋ณต๊ท€ ----------
// wheel/touch/์Šคํฌ๋กค๋ฐ” ๋“œ๋ž˜๊ทธ๋Š” ์‚ฌ์šฉ์ž๋งŒ ์ผ์œผํ‚ค๋ฏ€๋กœ, ํ”„๋กœ๊ทธ๋žจ์ด ๋ชฉ์ฐจ๋ฅผ ์›€์ง์ธ ๊ฒƒ๊ณผ ์„ž์ด์ง€ ์•Š์Œ
function userTouchedToc() {
detached = true;
restartTimer();
}
function restartTimer() {
clearTimeout(timer);
timer = null;
if (detached && !hovering) timer = setTimeout(reattach, RESYNC_MS);
}
function reattach() {
timer = null;
detached = false;
if (activeId) scrollTocTo(activeId, true);
}
tocBox.addEventListener("wheel", userTouchedToc, { passive: true });
tocBox.addEventListener("touchmove", userTouchedToc, { passive: true });
tocBox.addEventListener("mousedown", function (e) {
if (e.target === tocBox) userTouchedToc(); // ์Šคํฌ๋กค๋ฐ” ์žก๊ณ  ๋“œ๋ž˜๊ทธ
});
tocBox.addEventListener("mouseenter", function () {
hovering = true;
clearTimeout(timer); // ๋งˆ์šฐ์Šค๊ฐ€ ๋ชฉ์ฐจ ์œ„์— ์žˆ๋Š” ๋™์•ˆ์€ ์นด์šดํŠธ ์•ˆ ํ•จ
timer = null;
});
tocBox.addEventListener("mouseleave", function () {
hovering = false;
restartTimer(); // ๋ฒ—์–ด๋‚œ ์ˆœ๊ฐ„๋ถ€ํ„ฐ 3์ดˆ
});
// ---------- ๋ชฉ์ฐจ ํด๋ฆญ / ๊ธฐ๋Šฅ ์†Œ๊ฐœ ๋ฒ„ํŠผ โ†’ ๋ณธ๋ฌธ ์ด๋™ ----------
function goTo(id, syncToc) {
var h = document.getElementById(id);
if (!h) return;
// ์ œ๋ชฉ์ด ์•„๋‹Œ ์š”์†Œ(์˜ˆ: ์‚ฌ์šฉํŒ ํ•ญ๋ชฉ)๋กœ ๊ฐˆ ๋•Œ๋Š” ๊ทธ ์•ž์˜ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ œ๋ชฉ์„ ๋ชฉ์ฐจ์— ํ‘œ์‹œ
var tocId = id;
if (!linkById[id]) {
tocId = heads[0].id;
heads.forEach(function (hd) {
if (hd.compareDocumentPosition(h) & Node.DOCUMENT_POSITION_FOLLOWING) tocId = hd.id;
});
}
lockedId = tocId;
detached = false;
clearTimeout(timer);
timer = null;
setActive(tocId, false);
if (syncToc) scrollTocTo(tocId, true);
// ๋Œ€์ œ๋ชฉ์œผ๋กœ ๊ฐˆ ๋•Œ๋Š” ์žฅ ๋ฒˆํ˜ธ๊นŒ์ง€ ๋ณด์ด๋„๋ก ์žฅ ๋งจ ์œ„๋กœ
var anchor = (h.tagName === "H2" && h.closest(".chapter")) || h;
var y = anchor.getBoundingClientRect().top + window.scrollY - (anchor === h ? JUMP_MARGIN : 0);
window.scrollTo({ top: Math.max(0, y), behavior: "smooth" });
try { history.replaceState(null, "", "#" + id); } catch (err) { /* file:// ์—์„œ ๋ง‰ํžˆ๋ฉด ๋ฌด์‹œ */ }
}
window.manualGoTo = goTo;
// ---------- ์ฒ˜์Œ ์—ด ๋•Œ ----------
var first = location.hash && document.getElementById(location.hash.slice(1));
var startHead = first && heads.indexOf(first) >= 0 ? first : currentHead();
if (startHead) {
setActive(startHead.id, false);
scrollTocTo(startHead.id, false);
}
// ์ฒ˜์Œ ์œ„์น˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜ ์—†์ด ์žก๊ณ , ๊ทธ ๋‹ค์Œ๋ถ€ํ„ฐ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ
requestAnimationFrame(function () {
requestAnimationFrame(function () { marker.classList.add("animate"); });
});
// ---------- ๊ธฐ๋Šฅ ์†Œ๊ฐœ(๊ด‘๊ณ ) ์„น์…˜ ----------
var promo = document.getElementById("promo");
var items = window.MANUAL_PROMOS || [];
var lastIndex = -1;
function pickIndex() {
if (items.length <= 1) return 0;
var i;
do { i = Math.floor(Math.random() * items.length); } while (i === lastIndex);
return i;
}
function renderPromo() {
if (!items.length) { promo.hidden = true; return; }
lastIndex = pickIndex();
var it = items[lastIndex];
promo.innerHTML = "";
var head = document.createElement("div");
head.className = "side-title";
head.textContent = "๊ธฐ๋Šฅ ์†Œ๊ฐœ";
promo.appendChild(head);
var media = document.createElement("div");
media.className = "promo-media";
var el;
if (it.type === "video") {
el = document.createElement("video");
el.src = it.src;
el.autoplay = true;
el.muted = true;
el.loop = true;
el.playsInline = true;
if (it.poster) el.poster = it.poster;
} else {
el = document.createElement("img");
el.src = it.src;
el.alt = it.title || "";
}
media.appendChild(el);
if (it.target) media.addEventListener("click", function () { goTo(it.target, true); });
promo.appendChild(media);
var body = document.createElement("div");
body.className = "promo-body";
if (it.title) {
var t = document.createElement("p");
t.className = "promo-title";
t.textContent = it.title;
body.appendChild(t);
}
if (it.desc) {
var d = document.createElement("p");
d.className = "promo-desc";
d.textContent = it.desc;
body.appendChild(d);
}
var actions = document.createElement("div");
actions.className = "promo-actions";
if (it.target) {
var go = document.createElement("button");
go.className = "primary";
go.textContent = "๋งค๋‰ด์–ผ์—์„œ ๋ณด๊ธฐ";
go.addEventListener("click", function () { goTo(it.target, true); });
actions.appendChild(go);
}
if (items.length > 1) {
var next = document.createElement("button");
next.textContent = "๋‹ค๋ฅธ ๊ธฐ๋Šฅ";
next.addEventListener("click", renderPromo);
actions.appendChild(next);
}
body.appendChild(actions);
promo.appendChild(body);
}
// ๊ธฐ๋Šฅ ์†Œ๊ฐœ๋Š” ๊บผ ๋‘๊ณ , ๋ชฉ์ฐจ ์•„๋ž˜๋ฅผ ์–‡์€ ๋งˆ๊ฐ ์ค„๋กœ ์“ด๋‹ค. ๋‹ค์‹œ ์“ฐ๋ ค๋ฉด ์•„๋ž˜ ๋Œ€์‹  renderPromo()๋ฅผ ๋ถ€๋ฅธ๋‹ค.
function renderSideFoot() {
promo.hidden = false;
promo.innerHTML = "";
promo.className = "promo side-foot";
promo.setAttribute("aria-label", "StoneCandy Translator");
var bar = document.createElement("div");
bar.className = "side-foot-bar";
var name = document.createElement("span");
name.className = "side-foot-name";
name.textContent = "StoneCandy Translator";
var ver = document.createElement("span");
ver.className = "side-foot-ver";
ver.textContent = "Ver. 0.99.2";
bar.appendChild(name);
bar.appendChild(ver);
promo.appendChild(bar); // ๋ธŒ๋žœ๋“œ ์ค„: ๋ชฉ์ฐจ์™€ ๋ผ์ด์„ ์Šค ์นด๋“œ ์‚ฌ์ด ๊ตฌ๋ถ„์„  ์—ญํ• 
// ๊ด‘๊ณ  ์˜์ƒ ์ž๋ฆฌ๋Š” ๋ผ์ด์„ ์Šค ์นด๋“œ(index.html์˜ .side-license)๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค. ์˜์ƒ ํŒŒ์ผ์€ doc/video์— ๊ทธ๋Œ€๋กœ ๋‘”๋‹ค.
}
// [์ค€๋น„์ค‘] ์นธ (์˜ˆ์ „ ์ž๋ฆฌ ํ‘œ์‹œ). ์ง€๊ธˆ์€ ์•ˆ ์”€.
function renderPromoPlaceholder() {
promo.hidden = false;
promo.innerHTML = "";
var head = document.createElement("div");
head.className = "side-title";
head.textContent = "๊ธฐ๋Šฅ ์†Œ๊ฐœ";
promo.appendChild(head);
var body = document.createElement("div");
body.className = "promo-body promo-placeholder";
var d = document.createElement("p");
d.className = "promo-desc";
d.textContent = "[์ค€๋น„์ค‘]";
body.appendChild(d);
promo.appendChild(body);
}
// ๋ธŒ๋žœ๋“œ ์ค„(StoneCandy Translator ยท Ver.)์€ ๋ผ์ด์„ ์Šค ์นด๋“œ ๋จธ๋ฆฌ ์ค„(index.html .lic-head)๋กœ ๋Œ€์‹ ํ•œ๋‹ค. ๋˜๋Œ๋ฆฌ๋ ค๋ฉด ์•„๋ž˜ ๋Œ€์‹  renderSideFoot()๋ฅผ ๋ถ€๋ฅธ๋‹ค.
promo.hidden = true;
// ---------- ๊ทธ๋ฆผ ํด๋ฆญ ํ™•๋Œ€ (๋‹ค์‹œ ํด๋ฆญํ•˜๊ฑฐ๋‚˜ Esc๋กœ ๋‹ซ๊ธฐ) ----------
var box = document.createElement("div");
box.className = "lightbox";
box.hidden = true;
var big = document.createElement("img");
big.alt = "";
box.appendChild(big);
document.body.appendChild(box);
function openBox(src, alt) {
big.src = src;
big.alt = alt || "";
box.hidden = false;
void box.offsetWidth; // ํ™”๋ฉด์— ๋ถ™์ธ ๋’ค ๋ฐ”๋กœ ํŽ˜์ด๋“œ์ธ ์‹œ์ž‘
box.classList.add("open");
document.documentElement.classList.add("no-scroll");
}
function closeBox() {
box.classList.remove("open");
document.documentElement.classList.remove("no-scroll");
setTimeout(function () { if (!box.classList.contains("open")) box.hidden = true; }, 200);
}
manual.addEventListener("click", function (e) {
var img = e.target.closest(".shot img, .compare img");
if (img) openBox(img.currentSrc || img.src, img.alt);
var hero = e.target.closest("img.hero-art[data-full]"); // ์ฒซ ํ™”๋ฉด ๊ทธ๋ฆผ: ํฐ ๊ทธ๋ฆผ์œผ๋กœ ํ™•๋Œ€
if (hero) openBox(hero.getAttribute("data-full"), hero.alt);
var link = e.target.closest("a.popup-img"); // '์˜ˆ์‹œ' ๊ฐ™์€ ๊ธ€์ž ๋งํฌ๋„ ๊ฐ™์€ ํ™•๋Œ€ ์ฐฝ์œผ๋กœ
if (link) {
e.preventDefault();
openBox(link.href, link.getAttribute("data-alt") || link.textContent);
}
});
box.addEventListener("click", closeBox);
document.addEventListener("keydown", function (e) {
if (e.key === "Escape" && !box.hidden) closeBox();
});
})();
// ---------- Model Test: ๋ฐ๋ชจ(Gradio)๋ฅผ ํŒ์—… iframe์œผ๋กœ ----------
// ๋ฒ„ํŠผ์˜ data-local / data-web ์ฃผ์†Œ๋ฅผ iframe์— ๋„์šด๋‹ค. Space์— ์˜ฌ๋ฆฌ๋ฉด index.html์˜ data-web๋งŒ ์ฑ„์šด๋‹ค.
(function () {
var frameBox = null, frame, titleEl;
function build() {
frameBox = document.createElement("div");
frameBox.className = "frame-pop";
frameBox.hidden = true;
frameBox.innerHTML =
'<div class="frame-pop-panel" role="dialog">' +
'<div class="frame-pop-bar"><b></b>' +
'<a class="frame-pop-new" target="_blank" rel="noopener">์ƒˆ ์ฐฝ์œผ๋กœ</a>' +
'<button type="button" class="frame-pop-close" aria-label="๋‹ซ๊ธฐ">&times;</button></div>' +
'<iframe title="Model Test" allow="clipboard-read; clipboard-write"></iframe>' +
'</div>';
document.body.appendChild(frameBox);
frame = frameBox.querySelector("iframe");
titleEl = frameBox.querySelector(".frame-pop-bar b");
frameBox.addEventListener("click", function (e) {
if (e.target === frameBox || e.target.closest(".frame-pop-close")) close();
});
}
function open(url, title) {
if (!frameBox) build();
titleEl.textContent = title || "";
frameBox.querySelector(".frame-pop-new").href = url;
if (frame.getAttribute("src") !== url) frame.src = url; // ํ•œ ๋ฒˆ ๋„์šด ๋ฐ๋ชจ๋Š” ๋‹ซ์•„๋„ ๊ทธ๋Œ€๋กœ ๋‘”๋‹ค
frameBox.hidden = false;
document.documentElement.classList.add("no-scroll");
}
function close() {
frameBox.hidden = true;
document.documentElement.classList.remove("no-scroll");
}
// ๋งค๋‰ด์–ผ์„ ๋กœ์ปฌ(localhostยท127.0.0.1ยทํŒŒ์ผ)์—์„œ ์—ด์—ˆ์œผ๋ฉด ๋กœ์ปฌ ๋ฐ๋ชจ, ์›น(Hugging Face)์ด๋ฉด Space ์ฃผ์†Œ
function isLocalPage() {
var h = location.hostname;
return location.protocol === "file:" || h === "localhost" || h === "127.0.0.1" || h === "[::1]" || h === "";
}
function demoUrl(a) {
var local = a.getAttribute("data-local"), web = a.getAttribute("data-web");
return (isLocalPage() ? local : web) || a.href;
}
document.querySelectorAll("a.popup-frame").forEach(function (a) { a.href = demoUrl(a); }); // Ctrl+ํด๋ฆญ ์ƒˆ ํƒญ๋„ ๊ฐ™์€ ์ฃผ์†Œ๋กœ
document.addEventListener("click", function (e) {
var a = e.target.closest("a.popup-frame");
if (!a || e.ctrlKey || e.metaKey || e.shiftKey || e.button !== 0) return; // Ctrl+ํด๋ฆญ์€ ๊ทธ๋ƒฅ ์ƒˆ ํƒญ
e.preventDefault();
open(demoUrl(a), a.getAttribute("data-title"));
});
document.addEventListener("keydown", function (e) {
if (e.key === "Escape" && frameBox && !frameBox.hidden) close();
});
})();
// ๋ชฉ์ฐจ ยท Models used ์•ž 2ร—2 ํƒ€์ผ: ์นธ 5px ยท ํ‹ˆ 1px์„ ์‹ค์ œ ํ™”๋ฉด ํ”ฝ์…€ ์ •์ˆ˜๋กœ ๋งž์ถ˜๋‹ค.
// ํ™”๋ฉด ๋ฐฐ์œจ(125 % ๋“ฑ)์ด๋‚˜ ํ™•๋Œ€ยท์ถ•์†Œ ๋•Œ ์นธ ํฌ๊ธฐ๊ฐ€ ์ •์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฉด ์œ„์•„๋ž˜ ํ‹ˆ์ด 1px/2px๋กœ ๋‹ฌ๋ผ ๋ณด์ด๊ธฐ ๋•Œ๋ฌธ.
(function () {
function snapTiles() {
var d = window.devicePixelRatio || 1;
var s = document.documentElement.style;
s.setProperty('--tile', Math.max(3, Math.round(5 * d)) / d + 'px');
s.setProperty('--tile-gap', Math.max(1, Math.round(d)) / d + 'px');
}
snapTiles();
window.addEventListener('resize', snapTiles);
})();