File size: 11,316 Bytes
04b8892
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// astroparse — annotated reading view: main text + marginalia

const MODES = window.ASTROPARSE_MODES;
const modeById = Object.fromEntries(MODES.map((m) => [m.id, m]));

/* ---------- citation popover ---------- */

function ScoreBar({ label, value, max }) {
  const pct = Math.max(0, Math.min(1, value / max));
  return (
    <div className="ap-score">
      <span className="ap-score-label">{label}</span>
      <span className="ap-score-track"><span className="ap-score-fill" style={{ width: (pct * 100).toFixed(0) + '%' }}></span></span>
      <span className="ap-score-val">{value}</span>
    </div>
  );
}

function CitePopover({ lit, pos, showScores, onClose }) {
  const ref = React.useRef(null);
  React.useLayoutEffect(() => {
    const el = ref.current; if (!el) return;
    const r = el.getBoundingClientRect();
    let x = pos.x, y = pos.y + 10;
    if (x + r.width > window.innerWidth - 16) x = window.innerWidth - r.width - 16;
    if (y + r.height > window.innerHeight - 16) y = pos.y - r.height - 10;
    el.style.left = Math.max(8, x) + 'px';
    el.style.top = Math.max(8, y) + 'px';
    el.style.opacity = 1;
  }, [lit, pos]);
  React.useEffect(() => {
    const onKey = (e) => { if (e.key === 'Escape') onClose(); };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [onClose]);
  return (
    <div>
      <div className="ap-pop-backdrop" onClick={onClose}></div>
      <div className="ap-pop" ref={ref} style={{ opacity: 0 }}>
        <div className="ap-pop-rank">retrieved &middot; rank {lit.scores.rank}</div>
        <div className="ap-pop-title">{lit.title}</div>
        <div className="ap-pop-authors">{lit.authors} ({lit.year}) &middot; {lit.journal}</div>
        <div className="ap-pop-abstract">{lit.abstract}</div>
        {showScores && (
          <div className="ap-pop-scores">
            <ScoreBar label="specter" value={lit.scores.specter} max={1} />
            <ScoreBar label="bm25" value={lit.scores.bm25} max={20} />
          </div>
        )}
        <div className="ap-pop-links">
          <a href={'https://ui.adsabs.harvard.edu/abs/' + lit.bibcode + '/abstract'} target="_blank" rel="noopener">ADS &#8599;</a>
          <a href={'https://arxiv.org/abs/' + lit.arxiv} target="_blank" rel="noopener">arXiv &#8599;</a>
        </div>
        <button className="ap-pop-close" onClick={onClose} title="close">&times;</button>
      </div>
    </div>
  );
}

/* ---------- margin note ---------- */

function MarginNote({ para, mode, entry, generating, hovered, onHover, onSetMode, onRegenerate, onCite, top, noteRef }) {
  const m = modeById[mode];
  const lit = (window.ASTROPARSE_DATA.litByPara[para.id] || []).map((id) => window.ASTROPARSE_DATA.litPapers[id]).filter(Boolean);
  return (
    <aside
      ref={noteRef}
      className={'ap-note' + (hovered ? ' is-hovered' : '')}
      style={{ top: top, '--mode-c': 'var(--mc-' + mode + ')' }}
      onMouseEnter={() => onHover(para.id)}
      onMouseLeave={() => onHover(null)}
      data-comment-anchor={'note-' + para.id}
    >
      <div className="ap-note-head">
        <span className="ap-note-mode">{m.label}</span>
        {entry && entry.source === 'llm' && !generating && <span className="ap-note-gen" title="generated by the annotator">&#10022;</span>}
        {entry && !generating && (
          <button className="ap-note-regen" title="regenerate this note" onClick={() => onRegenerate(para.id, mode)}>&#8635;</button>
        )}
      </div>
      {generating ? (
        <div className="ap-note-loading">consulting the literature<span className="ap-ellipsis"></span></div>
      ) : entry && entry.error ? (
        <div className="ap-note-error">
          {entry.message ? entry.message + ' \u2014 ' : 'the annotator did not answer. '}
          <button className="ap-textbtn" onClick={() => onRegenerate(para.id, mode)}>try again</button>
        </div>
      ) : entry ? (
        <div className="ap-note-text">{entry.text}</div>
      ) : (
        <div className="ap-note-loading">awaiting annotation&hellip;</div>
      )}
      {lit.length > 0 && (
        <div className="ap-note-cites">
          {lit.map((l, i) => (
            <button key={l.id} className="ap-cite" onClick={(e) => onCite(l, e)}>
              <span className="ap-cite-n">{i + 1}</span> {l.short}
            </button>
          ))}
        </div>
      )}
      <div className="ap-note-modes" role="group" aria-label="commentary mode">
        {MODES.map((mm) => (
          <button
            key={mm.id}
            className={'ap-mode-dot' + (mm.id === mode ? ' is-active' : '')}
            style={{ '--mode-c': 'var(--mc-' + mm.id + ')' }}
            title={mm.label}
            onClick={() => onSetMode(para.id, mm.id)}
          >{mm.glyph}</button>
        ))}
      </div>
    </aside>
  );
}

/* ---------- reader ---------- */

function Reader({ paper, commentary, noteModes, generating, onSetMode, onRegenerate, tweaks, toolbar }) {
  const containerRef = React.useRef(null);
  const marginRef = React.useRef(null);
  const paraRefs = React.useRef({});
  const noteRefs = React.useRef({});
  const [tops, setTops] = React.useState({});
  const [spacers, setSpacers] = React.useState({});
  const spacersRef = React.useRef({});
  const [marginH, setMarginH] = React.useState(0);
  const [hoverId, setHoverId] = React.useState(null);
  const [pop, setPop] = React.useState(null); // {lit, pos}

  const paras = paper.paragraphs;

  const recompute = React.useCallback(() => {
    const c = containerRef.current; if (!c) return;
    const cTop = c.getBoundingClientRect().top;
    const narrow = window.innerWidth <= 900;
    const applied = spacersRef.current;
    const NOTE_GAP = 26;

    // reconstruct each paragraph's natural top (as if no spacers were applied)
    const naturalTop = {};
    let cum = 0;
    paras.forEach((p) => {
      const pe = paraRefs.current[p.id]; if (!pe) return;
      naturalTop[p.id] = pe.getBoundingClientRect().top - cTop - cum;
      cum += applied[p.id] || 0;
    });

    // pin every note to its paragraph's top; pad the text column below any
    // paragraph shorter than its note so the next pair stays level too
    const nextTops = {}; const nextSpacers = {};
    let cum2 = 0; let lastBottom = 0;
    paras.forEach((p, i) => {
      const ne = noteRefs.current[p.id];
      if (naturalTop[p.id] == null || !ne) return;
      const top = naturalTop[p.id] + cum2;
      nextTops[p.id] = top;
      lastBottom = top + ne.offsetHeight + NOTE_GAP;
      const nxt = paras[i + 1];
      if (!narrow && nxt && naturalTop[nxt.id] != null) {
        const delta = Math.max(0, Math.ceil(lastBottom - (naturalTop[nxt.id] + cum2)));
        if (delta > 0) nextSpacers[p.id] = delta;
        cum2 += delta;
      }
    });

    setTops((old) => {
      const same = paras.every((p) => Math.abs((old[p.id] || 0) - (nextTops[p.id] || 0)) < 1);
      return same ? old : nextTops;
    });
    const sameSp = paras.every((p) => Math.abs((applied[p.id] || 0) - (nextSpacers[p.id] || 0)) < 1);
    if (!sameSp) { spacersRef.current = nextSpacers; setSpacers(nextSpacers); }
    setMarginH(lastBottom);
  }, [paras]);

  React.useLayoutEffect(() => { recompute(); });
  React.useEffect(() => {
    if (document.fonts && document.fonts.ready) document.fonts.ready.then(recompute);
    window.addEventListener('resize', recompute);
    return () => window.removeEventListener('resize', recompute);
  }, [recompute]);

  const showCite = (lit, e) => {
    const r = e.currentTarget.getBoundingClientRect();
    setPop({ lit, pos: { x: r.left, y: r.bottom } });
  };

  // group paragraphs by section for headings
  const rows = [];
  let lastSection = null;
  paras.forEach((p) => {
    if (p.section !== lastSection) { rows.push({ type: 'heading', section: p.section, key: 'h-' + p.id }); lastSection = p.section; }
    rows.push({ type: 'para', para: p, key: p.id });
  });

  return (
    <div className="ap-screen ap-reader" data-screen-label="Reader">
      {toolbar}
      <div className={'ap-folio' + (tweaks.dropCaps ? ' has-dropcaps' : '') + (tweaks.justify ? ' is-justified' : '')}>
        <header className="ap-paper-head">
          <div className="ap-paper-title">{paper.title}</div>
          <div className="ap-paper-authors">{paper.authors}</div>
          <div className="ap-paper-meta">arXiv:{paper.arxivId} &middot; {paper.pages} pp &middot; {paper.venue}</div>
        </header>
        <div className="ap-legend">
          {MODES.map((m) => (
            <button
              key={m.id} className="ap-legend-item" style={{ '--mode-c': 'var(--mc-' + m.id + ')' }}
              title={'set every note to ' + m.label}
              onClick={() => paras.forEach((p) => onSetMode(p.id, m.id))}
            >
              <span className="ap-legend-dot"></span>{m.label}
            </button>
          ))}
        </div>
        <div className={'ap-body' + (tweaks.notesSide === 'left' ? ' notes-left' : '')} ref={containerRef}>
          <article className="ap-text">
            {rows.map((row) =>
              row.type === 'heading' ? (
                <h2 className="ap-section" key={row.key}>{row.section}</h2>
              ) : (
                <p
                  key={row.key}
                  ref={(el) => (paraRefs.current[row.para.id] = el)}
                  className={'ap-para' + (row.para.firstOfSection ? ' is-first' : '') + (hoverId === row.para.id ? ' is-hovered' : '')}
                  style={{
                    '--mode-c': 'var(--mc-' + (noteModes[row.para.id] || 'literature') + ')',
                    marginBottom: spacers[row.para.id] ? 'calc(1.15em + ' + spacers[row.para.id] + 'px)' : undefined,
                  }}
                  onMouseEnter={() => setHoverId(row.para.id)}
                  onMouseLeave={() => setHoverId(null)}
                  data-comment-anchor={'para-' + row.para.id}
                >
                  <span className="ap-pilcrow">&para;</span>{row.para.text}
                </p>
              )
            )}
          </article>
          <div className="ap-margin" ref={marginRef} style={{ minHeight: marginH }}>
            {paras.map((p) => {
              const mode = noteModes[p.id] || 'literature';
              const key = p.id + ':' + mode;
              return (
                <MarginNote
                  key={p.id}
                  para={p}
                  mode={mode}
                  entry={commentary[key]}
                  generating={generating[key]}
                  hovered={hoverId === p.id}
                  onHover={setHoverId}
                  onSetMode={onSetMode}
                  onRegenerate={onRegenerate}
                  onCite={showCite}
                  top={tops[p.id] || 0}
                  noteRef={(el) => (noteRefs.current[p.id] = el)}
                />
              );
            })}
          </div>
        </div>
        <footer className="ap-folio-foot">
          annotations are machine-generated against retrieved literature &mdash; verify before citing
        </footer>
      </div>
      {pop && <CitePopover lit={pop.lit} pos={pop.pos} showScores={tweaks.showScores} onClose={() => setPop(null)} />}
    </div>
  );
}

Object.assign(window, { Reader });