File size: 8,001 Bytes
72fa03b
332b637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72fa03b
 
 
 
 
b6c59a4
 
332b637
b6c59a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72fa03b
 
 
 
 
332b637
72fa03b
 
67e26d1
 
 
 
 
 
 
 
 
 
 
 
72fa03b
 
 
 
 
 
 
67e26d1
 
 
 
 
 
 
332b637
67e26d1
 
 
72fa03b
 
67e26d1
 
 
332b637
67e26d1
 
72fa03b
 
 
 
b6c59a4
72fa03b
 
b6c59a4
72fa03b
b6c59a4
 
 
72fa03b
 
 
 
67e26d1
72fa03b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(() => {
    function connectHubFrame(shell) {
        if (window.parent === window) return null;

        const hubOrigins = ["https://huggingface.co", "https://hf.co"];
        const layout = shell.closest(".gradio-container > .main");
        let hubFrame = hubOrigins.some(origin => document.referrer.startsWith(origin + "/"));
        let resizeFrame = 0;

        function fitHubFrame() {
            const resizer = window.parentIFrame;
            if (!hubFrame || !resizer?.size) return;

            // The Hub scrolls the outer page. Measure the app itself, independently of iframe height.
            document.documentElement.style.overflowY = "visible";
            const padding = layout ? parseFloat(getComputedStyle(layout).paddingBottom) : 0;
            const contentHeight = Math.ceil(shell.getBoundingClientRect().bottom + window.scrollY + padding);
            if (contentHeight > window.innerHeight + 1) resizer.size(contentHeight);
        }

        function scheduleFrameFit() {
            if (!hubFrame || resizeFrame) return;
            resizeFrame = requestAnimationFrame(() => {
                resizeFrame = 0;
                fitHubFrame();
            });
        }

        new ResizeObserver(scheduleFrameFit).observe(shell);
        window.addEventListener("resize", scheduleFrameFit);
        window.addEventListener("pageshow", scheduleFrameFit);
        window.addEventListener("message", (event) => {
            if (event.source !== window.parent || !hubOrigins.includes(event.origin)) return;
            if (typeof event.data !== "string" || !event.data.startsWith("[iFrameSizer]")) return;
            hubFrame = true;
            scheduleFrameFit();
        });
        scheduleFrameFit();

        return (target) => {
            const resizer = window.parentIFrame;
            if (!hubFrame || !resizer?.scrollToOffset) return false;
            fitHubFrame();
            const margin = parseFloat(getComputedStyle(target).scrollMarginTop) || 0;
            const offset = Math.max(0, Math.round(target.getBoundingClientRect().top + window.scrollY - margin));
            resizer.scrollToOffset(0, offset);
            return true;
        };
    }

    function bindReviewPage(shell) {
        let waiting = false;
        let movedWhileWaiting = false;
        let seenTurn = 0;
        let frame = 0;
        const motion = window.matchMedia("(prefers-reduced-motion: reduce)");
        const sweeps = new Map();
        const revealInHub = connectHubFrame(shell);

        function syncLoadingSweeps() {
            for (const [ghost, animation] of sweeps) {
                if (motion.matches || !ghost.isConnected) {
                    animation.cancel();
                    sweeps.delete(ghost);
                }
            }
            if (motion.matches) return;
            for (const ghost of shell.querySelectorAll(".sr-ghost")) {
                if (!sweeps.has(ghost)) {
                    sweeps.set(ghost, ghost.animate(
                        [{ backgroundPosition: "100% 0" }, { backgroundPosition: "0% 0" }],
                        { duration: 1450, iterations: Infinity },
                    ));
                }
            }
        }

        function syncBadgeSpacing() {
            const badge = document.getElementById("huggingface-space-header");
            shell.classList.toggle("sr-has-space-badge", Boolean(badge?.getClientRects().length));
        }
        new MutationObserver(syncBadgeSpacing).observe(document.body, { childList: true });
        syncBadgeSpacing();
        motion.addEventListener("change", syncLoadingSweeps);

        const keepReading = () => {
            if (waiting) movedWhileWaiting = true;
        };
        const revealReply = (reply) => {
            if (!revealInHub?.(reply)) reply.scrollIntoView({ block: "start", inline: "nearest", behavior: "instant" });
        };

        function showReviewPass(article, index) {
            const total = Number(article.dataset.reviewTotal);
            if (index < 1 || index > total) return;
            article.dataset.reviewCurrent = String(index);
            for (const part of article.querySelectorAll("[data-review-part]")) {
                part.hidden = Number(part.dataset.reviewPart) !== index;
            }
            article.querySelector("[data-review-position]").textContent = index;
            article.querySelector('[data-review-step="-1"]').setAttribute("aria-disabled", String(index === 1));
            article.querySelector('[data-review-step="1"]').setAttribute("aria-disabled", String(index === total));
        }

        window.addEventListener("wheel", keepReading, { capture: true, passive: true });
        window.addEventListener("touchmove", keepReading, { capture: true, passive: true });
        window.addEventListener("keydown", (event) => {
            if (event.target instanceof Element && event.target.closest("input, textarea, select, [contenteditable]")) return;
            if (["ArrowDown", "ArrowUp", "PageDown", "PageUp", "Home", "End", " "].includes(event.key)) keepReading();
        }, true);
        shell.addEventListener("click", (event) => {
            if (!(event.target instanceof Element)) return;
            const step = event.target.closest("[data-review-step]");
            if (step) {
                const article = step.closest("[data-review-current]");
                showReviewPass(article, Number(article.dataset.reviewCurrent) + Number(step.dataset.reviewStep));
                return;
            }
            const link = event.target.closest('.sr-latest-link, .sr-pass-link, a[href="#sr-captures"]');
            if (!link) return;
            const destination = document.getElementById(link.hash.slice(1));
            if (!destination) return;
            event.preventDefault();
            keepReading();
            if (link.dataset.reviewJump) {
                showReviewPass(destination.closest("[data-review-current]"), Number(link.dataset.reviewJump));
            }
            destination.tabIndex = -1;
            destination.focus({ preventScroll: true });
            revealReply(destination);
        }, true);

        function updatePage() {
            frame = 0;
            syncLoadingSweeps();
            const button = shell.querySelector("#review-options button.label-wrap");
            if (button) {
                const open = button.classList.contains("open");
                button.setAttribute("aria-controls", "options-fields");
                button.setAttribute("aria-expanded", String(open));
                if (open) button.removeAttribute("aria-describedby");
                else button.setAttribute("aria-describedby", "sr-options-preview");
            }

            const latest = shell.querySelector("#sr-latest-reply");
            const turn = Number(latest?.dataset.turn || 0);
            const pending = shell.querySelector("#followup-status [role='status']:not(.sr-pass-ready)");
            if (pending) {
                if (!waiting) movedWhileWaiting = false;
                waiting = true;
                return;
            }

            if (waiting && !movedWhileWaiting && turn > seenTurn) {
                revealReply(latest);
            }
            waiting = false;
            seenTurn = turn;
        }

        new MutationObserver(() => {
            if (!frame) frame = requestAnimationFrame(updatePage);
        }).observe(shell, { childList: true, subtree: true, attributes: true, attributeFilter: ["class"] });
        updatePage();
    }

    const shell = document.querySelector("#shell");
    if (shell) {
        bindReviewPage(shell);
    } else {
        const mount = new MutationObserver(() => {
            const page = document.querySelector("#shell");
            if (page) {
                mount.disconnect();
                bindReviewPage(page);
            }
        });
        mount.observe(document.body, { childList: true, subtree: true });
    }
})();