summaryrefslogtreecommitdiff
path: root/web/index.html
blob: d176c2aa06114070faace572f43afff9ecc95265 (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!doctype html>
<html lang="zh-Hans">
<head>
<meta charset="utf-8">
<title>lilchinese</title>
<style>
  :root { --pad-y: 6vh; --pad-x: 5vw; }
  html, body {
    margin: 0; padding: 0; height: 100%; overflow: hidden;
    background: #fff; color: #000; cursor: none;
    transition: background-color 90ms linear, color 90ms linear;
    -webkit-user-select: none; user-select: none;
    font-family: "Noto Sans CJK SC Black", "Noto Sans SC Black", "Source Han Sans SC",
                 "Noto Sans CJK JP Black", "PingFang SC", sans-serif;
  }
  #wrap {
    position: fixed; inset: 0; box-sizing: border-box;
    padding: var(--pad-y) var(--pad-x);
    display: flex; align-items: center; justify-content: center;
  }
  #stack { display: flex; flex-direction: column; align-items: center; max-width: 100%; }
  #zh {
    font-weight: 900;
    line-height: 1.04;
    letter-spacing: .02em;
    text-align: center;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    text-wrap: balance;
    max-width: 100%;
    will-change: font-size;
  }
  #en {
    font-family: "Noto Sans", "Inter", "DejaVu Sans", sans-serif;
    font-weight: 500;
    line-height: 1.18;
    text-align: center;
    white-space: pre-wrap;
    overflow-wrap: break-word;
    max-width: 100%;
    margin-top: .28em;
    opacity: .88;
    display: none;
  }
  body.en #en { display: block; }
  /* sentence alternates black-on-white and white-on-black; Backspace = red retake */
  body.invert { background: #000; color: #fff; }
  body.cut { background: #d10d0d; color: #fff; }
  /* Enter: the sentence was kept and written to the session log */
  body.keep { color: #0a7d3a; }
  body.invert.keep { color: #4ade80; }
  @keyframes pop {
    from { opacity: .5; transform: scale(.955); }
    to   { opacity: 1;  transform: scale(1); }
  }
  .pop { animation: pop 90ms ease-out; }
  /* intro page: red, the photo, and the title */
  #intro {
    position: fixed; inset: 0; z-index: 10;
    background: #c8102e; color: #fff;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 3.2vh; padding: 6vh 6vw; box-sizing: border-box;
    text-align: center;
  }
  #intro[hidden] { display: none; }
  #intro img {
    max-width: min(74vw, 1150px);
    max-height: 44vh;
    box-shadow: 0 0 0 5px rgba(0,0,0,.28), 0 18px 50px rgba(0,0,0,.35);
    filter: grayscale(1) contrast(1.06);
  }
  #intro h1 {
    margin: 0;
    font-size: clamp(44px, 11vh, 190px);
    font-weight: 900;
    line-height: 1;
    letter-spacing: .05em;
    text-indent: .05em;
  }
  #intro h2 {
    margin: 0;
    font-family: "Noto Sans", "DejaVu Sans", sans-serif;
    font-size: clamp(16px, 3.4vh, 56px);
    font-weight: 800;
    letter-spacing: .34em;
    text-indent: .34em;
  }
  #intro .hint {
    position: absolute; bottom: 4.5vh;
    font: 600 clamp(12px, 1.7vh, 22px)/1 "Noto Sans", "DejaVu Sans", sans-serif;
    letter-spacing: .1em; opacity: .9;
  }
  #intro .hint .key {
    display: inline-block; padding: .12em .45em; margin-right: .5em;
    border: 2px solid currentColor; border-radius: .28em; font-weight: 700;
  }
  #d {
    position: fixed; right: 12px; bottom: 10px; display: none;
    font: 12px/1.4 ui-monospace, monospace; color: #bbb; letter-spacing: 0;
  }
</style>
</head>
<body>
<div id="intro">
  <img id="intro-img" src="/intro.jpg" alt="" onerror="this.remove()">
  <h1 id="intro-zh">批斗大会</h1>
  <h2 id="intro-en">STRUGGLE SESSION</h2>
  <div class="hint"><span class="key">&#9166;</span> Press enter to start</div>
</div>
<div id="wrap"><div id="stack"><div id="zh"></div><div id="en"></div></div></div>
<div id="d"></div>
<script>
(function () {
  const wrap = document.getElementById('wrap');
  const stack = document.getElementById('stack');
  const zh = document.getElementById('zh');
  const en = document.getElementById('en');
  const d = document.getElementById('d');
  const body = document.body;
  const q = new URLSearchParams(location.search);
  const debug = q.has('debug');
  const wantEN = q.has('en');
  if (debug) d.style.display = 'block';
  if (wantEN) document.body.classList.add('en');

  let curZh = '', curEn = '';
  let padW = 0, padH = 0;
  const EN_RATIO = 0.30;
  const EN_MAX = 120;   // keep the English line from crowding out the Chinese

  function measurePad() {
    const cs = getComputedStyle(wrap);
    padW = parseFloat(cs.paddingLeft) + parseFloat(cs.paddingRight);
    padH = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
  }

  function setSizes(s) {
    zh.style.fontSize = s + 'px';
    en.style.fontSize = Math.max(11, Math.round(s * EN_RATIO)) + 'px';
  }

  // Largest Chinese font size at which Chinese + English still fit the screen.
  function fit() {
    const boxW = wrap.clientWidth - padW;
    const boxH = wrap.clientHeight - padH;
    if (boxW <= 0 || boxH <= 0) return;
    // Reserve the English line even before its translation lands, so the Chinese
    // text does not jump in size when the translation appears.
    const reserve = wantEN && !curEn;
    if (reserve) en.textContent = '\u00a0';
    let lo = 8, hi = Math.max(boxW, boxH) * 1.7;
    for (let i = 0; i < 18; i++) {
      const mid = (lo + hi) / 2;
      setSizes(mid);
      const r = stack.getBoundingClientRect();
      if (r.width <= boxW && r.height <= boxH) lo = mid; else hi = mid;
    }
    setSizes(Math.floor(lo));
    if (reserve) en.textContent = '';
  }

  function keep() {
    body.classList.add('keep');          // instant feedback, server confirms it
    fetch('/keep', { method: 'POST', keepalive: true }).catch(function () {});
  }

  function cut() {
    body.classList.add('cut');           // instant feedback, server confirms it
    curZh = ''; curEn = '';
    zh.textContent = ''; en.textContent = '';
    fetch('/cut', { method: 'POST', keepalive: true }).catch(function () {});
  }

  function render(text, translation, cap) {
    if (!text) return;
    const newPhrase = text !== curZh;
    const growing = !newPhrase ? false
                  : (curZh.length > 0 && text.startsWith(curZh));

    if (newPhrase) {
      curZh = text;
      zh.textContent = text;
      if (!growing) {
        // New phrase: slam it in. Refinements just update in place (no flicker).
        zh.classList.remove('pop');
        void zh.offsetWidth;
        zh.classList.add('pop');
      }
    }
    let tr = translation || '';
    if (tr.length > EN_MAX) tr = tr.slice(0, EN_MAX - 1).trimEnd() + '\u2026';
    if (tr !== curEn) {
      curEn = tr;
      en.textContent = tr;
      if (tr && newPhrase) {
        en.classList.remove('pop');
        void en.offsetWidth;
        en.classList.add('pop');
      }
    }
    fit();
    if (debug) {
      const lat = cap ? (Date.now() - cap) : 0;
      d.textContent = text.length + '\u5b57  ' + (cap ? lat + 'ms' : '') +
                      (tr ? '  en:' + tr.length : '');
    }
  }

  addEventListener('resize', function () { measurePad(); fit(); });
  addEventListener('keydown', function (e) {
    if (introVisible) {
      if (e.key === 'Enter' || e.key === ' ') {
        e.preventDefault();
        startTalk();
      }
      return;
    }
    if (e.key === 'Enter') {
      e.preventDefault();
      keep();
      return;
    }
    if (e.key === 'Backspace' || e.key === 'Delete' || e.key === 'c') {
      // "that was wrong, say it again": red screen, then straight back to work
      e.preventDefault();
      cut();
      return;
    }
    if (e.key === 'f' || e.key === 'F') {
      if (document.fullscreenElement) document.exitFullscreen();
      else document.documentElement.requestFullscreen();
    }
  });

  measurePad();

  const introEl = document.getElementById('intro');
  let introVisible = !introEl.hidden;

  function showIntro(cfg) {
    document.getElementById('intro-zh').textContent = cfg.introZH || '';
    document.getElementById('intro-en').textContent = cfg.introEN || '';
    const img = document.getElementById('intro-img'); // may already be gone (no picture)
    if (!img) return;
    if (cfg.introImg) img.src = cfg.introImg; else img.remove();
    introEl.hidden = false;
    introVisible = true;
  }

  function hideIntro() {
    introEl.hidden = true;
    introVisible = false;
  }

  function startTalk() {
    hideIntro();
    fetch('/start', { method: 'POST', keepalive: true }).catch(function () {});
  }

  if (q.has('text') || q.has('cut')) hideIntro();
  if (q.has('invert')) body.classList.add('invert');
  if (q.has('keep')) body.classList.add('keep');
  if (q.has('cut')) { body.classList.add('cut'); return; }

  if (q.has('text')) {              // design preview: index.html?en=1&text=你好&t=hello
    render(q.get('text'), q.get('t') || '', 0);
    return;
  }

  fetch('/config').then(function (r) { return r.json(); }).then(function (cfg) {
    if (cfg.translate) body.classList.add('en');
    if (cfg.debug) d.style.display = 'block';
    if (cfg.intro && !cfg.started) showIntro(cfg); else hideIntro();
  }).catch(function () {});

  const es = new EventSource('/events');
  es.onmessage = function (e) {
    try {
      const s = JSON.parse(e.data);
      body.classList.toggle('invert', !!s.invert);
      body.classList.toggle('cut', !!s.cut);
      body.classList.toggle('keep', !!s.keep);
      if (s.start && introVisible) hideIntro();
      if (s.cut) { curZh = ''; curEn = ''; zh.textContent = ''; en.textContent = ''; return; }
      if (s.keep && s.text === curZh) return;  // only the colour changed
      render(s.text, s.en, s.cap);
    } catch (_) {}
  };
  es.onerror = function () { /* EventSource reconnects on its own; keep the phrase up */ };
})();
</script>
</body>
</html>