@li-canghai/dsh-paper 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/lib/client-parts/01-pure-helpers.js +164 -9
- package/lib/client-parts/02-author-year.js +95 -0
- package/lib/client.js +840 -28
- package/lib/metadata-proxy.ps1 +35 -35
- package/lib/page-image-helper.py +18 -4
- package/lib/pdf-page.js +87 -9
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -575,14 +575,26 @@ function createLibraryModel() {
|
|
|
575
575
|
return { tex, tag: typeof block.math?.tag === 'string' ? block.math.tag : '' }
|
|
576
576
|
}
|
|
577
577
|
|
|
578
|
-
/**
|
|
578
|
+
/**
|
|
579
|
+
* 行内片段的稳定 key:块 id + 下标就够定位,同一块的两段各自的片段也不会撞。
|
|
580
|
+
*
|
|
581
|
+
* 🔴 **必须把 raw / items 原样带过来**:这两个字段由 {@link inlineRefs} 给多标号
|
|
582
|
+
* (`[41, 44, 13, 16]`)产出好后,这一层是通往悬停预览的**唯一通道**。
|
|
583
|
+
* 早先只复制 key/type/value/text,它们在这里被静默丢掉 —— 下游 {@link paintCiteSpans}
|
|
584
|
+
* 于是退回用 value 拼 `[41,44,13,16]`(无空格)去正文里找,而页面上真实的字带空格,
|
|
585
|
+
* 结果单编号 `[23]` 能悬停、多标号一律没反应。丢字段不报错,所以肉眼查不出来。
|
|
586
|
+
*/
|
|
579
587
|
function inlinePart(part, index, at) {
|
|
580
|
-
|
|
588
|
+
const shape = {
|
|
581
589
|
key: `${index}-${at}-${part.type}-${part.value ?? ''}`,
|
|
582
590
|
type: part.type,
|
|
583
591
|
value: part.value ?? '',
|
|
584
592
|
text: part.text ?? '',
|
|
585
593
|
}
|
|
594
|
+
// 单编号不带这两项时**不补空值**:既有断言按字段全等,多一个 raw: undefined 也会红。
|
|
595
|
+
if (part.raw !== undefined) shape.raw = part.raw
|
|
596
|
+
if (part.items !== undefined) shape.items = part.items
|
|
597
|
+
return shape
|
|
586
598
|
}
|
|
587
599
|
|
|
588
600
|
// 阅读器布局(D46 + D75):右栏未展开 —— 或读不到展开状态 —— 恒定单栏,这时宽度再大也不并排;
|
|
@@ -759,6 +771,60 @@ function createLibraryModel() {
|
|
|
759
771
|
* @returns {Map<string, object[]>} blockId → 该块的划线,按面板顺序。
|
|
760
772
|
*/
|
|
761
773
|
|
|
774
|
+
/**
|
|
775
|
+
* 将 PDF 原生字形宽度和后继词的物理空隙分别映射到 CSS。
|
|
776
|
+
* 不能把 (字框 + gap) 一起分摊给字形:这会把本词最后一个字母推向下一个词,
|
|
777
|
+
* 而浏览器的选区、插入光标又按实际字形推进量走,最终表现为蓝色选区水平偏移。
|
|
778
|
+
*
|
|
779
|
+
* 有尾空格时,letter-spacing 也会作用于这个空格,所以 word-spacing 必须减掉它。
|
|
780
|
+
* 返回的长度遵循:字形 = wordWidth;字形 + 尾空格 = wordWidth + gap。
|
|
781
|
+
*/
|
|
782
|
+
function pdfWordMetrics(text, wordWidth, gap, measure) {
|
|
783
|
+
const raw = String(text ?? '')
|
|
784
|
+
const glyphs = raw.replace(/\s+$/, '')
|
|
785
|
+
const count = Array.from(glyphs).length
|
|
786
|
+
if (!(wordWidth > 0) || count === 0 || typeof measure !== 'function') return null
|
|
787
|
+
const glyphWidth = measure(glyphs)
|
|
788
|
+
const trailingWidth = measure(raw) - glyphWidth
|
|
789
|
+
if (!Number.isFinite(glyphWidth) || !Number.isFinite(trailingWidth)) return null
|
|
790
|
+
const letterSpacing = (wordWidth - glyphWidth) / count
|
|
791
|
+
return {
|
|
792
|
+
letterSpacing,
|
|
793
|
+
wordSpacing: glyphs.length !== raw.length ? gap - trailingWidth - letterSpacing : 0,
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* 将 PDFium 原生字符的每个起点转换成浏览器可渲染的字符推进宽度。
|
|
799
|
+
* 单词字体、字重、连字均不可假定等于浏览器 serif,所以不能再让
|
|
800
|
+
* 所有字母平分整词宽差。非法、倒序、叠字或不匹配的字符回退整词布局。
|
|
801
|
+
*/
|
|
802
|
+
function pdfGlyphAdvances(word, scale) {
|
|
803
|
+
const raw = String(word?.text ?? '')
|
|
804
|
+
const core = raw.replace(/\s+$/, '')
|
|
805
|
+
const trailing = raw.slice(core.length)
|
|
806
|
+
const chars = word?.chars
|
|
807
|
+
const box = word?.bbox
|
|
808
|
+
if (!Array.isArray(chars) || chars.length === 0 || !Array.isArray(box) || !(scale > 0)
|
|
809
|
+
|| (trailing !== '' && trailing !== ' ')
|
|
810
|
+
|| !chars.every(char => typeof char?.text === 'string'
|
|
811
|
+
&& Array.from(char.text).length === 1 && Array.isArray(char.bbox)
|
|
812
|
+
&& char.bbox.length === 4 && char.bbox.every(Number.isFinite))
|
|
813
|
+
|| chars.map(char => char.text).join('') !== core) return null
|
|
814
|
+
const left = box[0], right = box[2]
|
|
815
|
+
if (!Number.isFinite(left) || !Number.isFinite(right) || !(right > left)
|
|
816
|
+
|| Math.abs(chars[0].bbox[0] - left) > 0.1) return null
|
|
817
|
+
const spans = []
|
|
818
|
+
for (let i = 0; i < chars.length; i++) {
|
|
819
|
+
const start = chars[i].bbox[0]
|
|
820
|
+
const end = i + 1 < chars.length ? chars[i + 1].bbox[0] : right
|
|
821
|
+
if (start < left - 0.1 || end > right + 0.1 || end - start < 0.01) return null
|
|
822
|
+
spans.push({ text: chars[i].text, advance: (end - start) * scale })
|
|
823
|
+
}
|
|
824
|
+
if (trailing) spans.push({ text: ' ', advance: Math.max(0, Number(word.gap) || 0) * scale })
|
|
825
|
+
return spans
|
|
826
|
+
}
|
|
827
|
+
|
|
762
828
|
function selectionCount(text) {
|
|
763
829
|
const source = String(text ?? '')
|
|
764
830
|
const characters = (source.match(/\p{Script=Han}/gu) ?? []).length
|
|
@@ -2002,11 +2068,54 @@ function createLibraryModel() {
|
|
|
2002
2068
|
// 引用编号挂 data-ref,公式编号挂 data-eq。只认"本篇里真的存在的编号":引用编号来自参考
|
|
2003
2069
|
// 文献块开头的 [n](条目正文里的年份括号不算),公式编号来自 math 块的 tag。认不出、或编号
|
|
2004
2070
|
// 不在集合里,一律原样留在纯文本里 —— 不猜、不改写正文。
|
|
2005
|
-
|
|
2071
|
+
// 🔴 一次引用多篇(`[42, 43]`、`[40–41]`)在真实论文里很常见(实测 ResNet 一篇就有
|
|
2072
|
+
// `[42, 43]` 3 次、`[40, 41]` 2 次)。旧版正则只认 `[41]` 这一种形态,多标号会整段落回
|
|
2073
|
+
// 纯文本 —— 那是缺口,不是刻意省略。现在把**连编号带分隔符**的整段一起捕获,再由
|
|
2074
|
+
// {@link refItemsOf} 摊成单个编号。
|
|
2075
|
+
//
|
|
2076
|
+
// 仍然要求方括号内**以数字开头**:`[.5, .95]` 这类置信区间因此天然不匹配(实测同一篇里
|
|
2077
|
+
// 出现 7 次),不需要额外的黑白名单。
|
|
2078
|
+
// 🔴 编号位数不能写死 3 位:实测一篇综述有 1411 条参考文献,正文里出现 `[841, 1141]`、
|
|
2079
|
+
// `[1211, 1372]` 这类四位数编号(`\d{1,3}` 会把它们整段挡掉,界面上就是"悬停没反应")。
|
|
2080
|
+
// 上限给到 5 位足以覆盖任何真实文献表,同时仍挡掉更长的方括号噪声。
|
|
2081
|
+
const REF_NUMBER = /\[(\d{1,5}(?:\s*[,,、–—-]\s*\d{1,5})*)\]/g
|
|
2006
2082
|
// 公式号不加单词边界:词边界只对拉丁字母成立,中文的"式 (4)"加了就永远匹配不上。
|
|
2007
2083
|
const EQUATION_NUMBER = /(?:\bEquations?\b|\bEqs?\.|式)\s*\(?\s*(\d{1,3})\)?/g
|
|
2008
2084
|
/** 引用条目开头的方括号编号;条目正文里的年份括号不会被当成编号。 */
|
|
2009
|
-
const REFERENCE_LEAD = /^\[\s*(\d{1,
|
|
2085
|
+
const REFERENCE_LEAD = /^\[\s*(\d{1,5})\s*\]/
|
|
2086
|
+
/** 连字符区间的最大展开跨度;超过它的一律不当作引用区间。 */
|
|
2087
|
+
const REF_RANGE_MAX = 200
|
|
2088
|
+
|
|
2089
|
+
/**
|
|
2090
|
+
* 把一个引用标记的内部文本摊成单个编号字符串。
|
|
2091
|
+
*
|
|
2092
|
+
* 逗号/顿号分隔 = 并列,原样列出;连字符/短破折号/长破折号分隔 = 区间,展开。
|
|
2093
|
+
* 任何一个成分认不出来(例如 `4a`、小数、空段)**整段作废**返回空数组:
|
|
2094
|
+
* 只认得出的那几个编号不算数 —— 显示半截引用比不显示更坏,读者会以为那就是全部。
|
|
2095
|
+
*
|
|
2096
|
+
* 区间跨度过大也整段作废:那不是引用区间,是解析事故。
|
|
2097
|
+
*
|
|
2098
|
+
* @param {unknown} inner 方括号内部的文本(不含两侧方括号)。
|
|
2099
|
+
* @returns {string[]} 单个编号,按出现顺序;空数组表示"这不是引用标记"。
|
|
2100
|
+
*/
|
|
2101
|
+
function refItemsOf(inner) {
|
|
2102
|
+
if (typeof inner !== 'string' || inner === '') return []
|
|
2103
|
+
const items = []
|
|
2104
|
+
for (const chunk of inner.split(/[,,、]/)) {
|
|
2105
|
+
const range = /^\s*(\d{1,5})\s*[–—-]\s*(\d{1,5})\s*$/.exec(chunk)
|
|
2106
|
+
if (range !== null) {
|
|
2107
|
+
const start = Number(range[1])
|
|
2108
|
+
const end = Number(range[2])
|
|
2109
|
+
if (end < start || end - start > REF_RANGE_MAX) return []
|
|
2110
|
+
for (let value = start; value <= end; value += 1) items.push(String(value))
|
|
2111
|
+
continue
|
|
2112
|
+
}
|
|
2113
|
+
const single = /^\s*(\d{1,5})\s*$/.exec(chunk)
|
|
2114
|
+
if (single === null) return []
|
|
2115
|
+
items.push(single[1])
|
|
2116
|
+
}
|
|
2117
|
+
return items
|
|
2118
|
+
}
|
|
2010
2119
|
|
|
2011
2120
|
/**
|
|
2012
2121
|
* 把段落切成"文字段 + 标记"的有序片段。标记是结构化对象({"type":"ref"|"eq", value}),
|
|
@@ -2015,9 +2124,15 @@ function createLibraryModel() {
|
|
|
2015
2124
|
* 两个模式都带 g,lastIndex 在换模式时不会被重置,所以每次 exec 前按 cursor 重新定位;
|
|
2016
2125
|
* 空匹配(两个正则都不可能产生)原地踏步时按一个字符推进,保证一定会终止。
|
|
2017
2126
|
*
|
|
2127
|
+
* `ref` 片段一次引用多篇时带 {@link items}:正文**原样**保留(`[42, 43]` 不拆成两段),
|
|
2128
|
+
* 悬停预览再逐条展开。单编号时不带 `items` —— 与旧行为逐字段相同,既有断言不受影响。
|
|
2129
|
+
*
|
|
2130
|
+
* 多标号里只要有一个编号不在本篇集合内,整段按纯文本处理(同 {@link refItemsOf} 的口径):
|
|
2131
|
+
* 显示半截引用会让读者以为那就是全部。
|
|
2132
|
+
*
|
|
2018
2133
|
* @param {unknown} text 段落文本。
|
|
2019
2134
|
* @param {unknown} known {refs: Set, eqs: Set},本篇真实存在的编号。
|
|
2020
|
-
* @returns {Array<{type: string, value: string} | {type: 'text', text: string}>}
|
|
2135
|
+
* @returns {Array<{type: string, value: string, items?: string[]} | {type: 'text', text: string}>}
|
|
2021
2136
|
*/
|
|
2022
2137
|
function inlineRefs(text, known) {
|
|
2023
2138
|
if (typeof text !== 'string' || text === '') return []
|
|
@@ -2039,9 +2154,21 @@ function createLibraryModel() {
|
|
|
2039
2154
|
if (hit === null) break
|
|
2040
2155
|
const match = hit === 'ref' ? ref : eq
|
|
2041
2156
|
plain += text.slice(cursor, match.index)
|
|
2042
|
-
if (
|
|
2157
|
+
if (hit === 'ref') {
|
|
2158
|
+
const items = refItemsOf(match[1])
|
|
2159
|
+
if (items.length > 0 && items.every(number => refs.has(number))) {
|
|
2160
|
+
flush()
|
|
2161
|
+
// 单编号不带 raw/items:与旧的 {type, value} 逐字段相同,既有断言照旧通过。
|
|
2162
|
+
// 多标号必须带 raw —— 渲染时按它原样输出(`[42,43]` 不能变成 `[42, 43]`)。
|
|
2163
|
+
parts.push(items.length === 1
|
|
2164
|
+
? { type: 'ref', value: items[0] }
|
|
2165
|
+
: { type: 'ref', value: items.join(','), items, raw: match[0] })
|
|
2166
|
+
} else {
|
|
2167
|
+
plain += match[0]
|
|
2168
|
+
}
|
|
2169
|
+
} else if (eqs.has(match[1])) {
|
|
2043
2170
|
flush()
|
|
2044
|
-
parts.push({ type:
|
|
2171
|
+
parts.push({ type: 'eq', value: match[1] })
|
|
2045
2172
|
} else {
|
|
2046
2173
|
plain += match[0]
|
|
2047
2174
|
}
|
|
@@ -2060,6 +2187,27 @@ function createLibraryModel() {
|
|
|
2060
2187
|
return lead === null ? '' : lead[1]
|
|
2061
2188
|
}
|
|
2062
2189
|
|
|
2190
|
+
/**
|
|
2191
|
+
* 编号 → 该条参考文献的**原文**(去掉开头的 `[n]` 与两侧空白)。
|
|
2192
|
+
*
|
|
2193
|
+
* 悬停预览要给人看的是条目文字本身,不是块 id —— 所以与 {@link referenceIndex} 分开:
|
|
2194
|
+
* 那个给"跳到哪一段"用,这个给"显示什么"用。两者同一口径地只认参考文献块**开头**的编号。
|
|
2195
|
+
*
|
|
2196
|
+
* @param {unknown} blocks 阅读器的块列表。
|
|
2197
|
+
* @returns {Map<string, {blockId: string, text: string}>}
|
|
2198
|
+
*/
|
|
2199
|
+
function referenceTexts(blocks) {
|
|
2200
|
+
const texts = new Map()
|
|
2201
|
+
for (const block of Array.isArray(blocks) ? blocks : []) {
|
|
2202
|
+
if (block === null || typeof block !== 'object' || typeof block.id !== 'string') continue
|
|
2203
|
+
const number = refNumbersOf(block)
|
|
2204
|
+
if (number === '' || texts.has(number)) continue
|
|
2205
|
+
const source = typeof block.sourceText === 'string' ? block.sourceText : ''
|
|
2206
|
+
texts.set(number, { blockId: block.id, text: source.replace(REFERENCE_LEAD, '').trim() })
|
|
2207
|
+
}
|
|
2208
|
+
return texts
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2063
2211
|
/** 全文真实的编号集合。非数组入参按没有编号处理,不抛错。 */
|
|
2064
2212
|
function numbersOf(values, pick) {
|
|
2065
2213
|
const numbers = new Set()
|
|
@@ -2071,11 +2219,18 @@ function createLibraryModel() {
|
|
|
2071
2219
|
return numbers
|
|
2072
2220
|
}
|
|
2073
2221
|
|
|
2074
|
-
/**
|
|
2222
|
+
/**
|
|
2223
|
+
* 本篇真实存在的编号:refs 来自参考文献块,eqs 来自 math 块的 tag。
|
|
2224
|
+
*
|
|
2225
|
+
* 编号制整篇一条都没识别出来时**才**建作者-年份索引:那套索引要解析每一条参考文献的
|
|
2226
|
+
* 姓氏与年份,对编号制论文是纯浪费(实测 4128 个 ref_text 块,建一次不便宜)。
|
|
2227
|
+
*/
|
|
2075
2228
|
function readerNumbers(blocks) {
|
|
2229
|
+
const refs = numbersOf(blocks, refNumbersOf)
|
|
2076
2230
|
return {
|
|
2077
|
-
refs
|
|
2231
|
+
refs,
|
|
2078
2232
|
eqs: numbersOf(blocks, block => (typeof block?.math?.tag === 'string' ? block.math.tag : '')),
|
|
2233
|
+
authorYears: refs.size > 0 ? new Map() : authorYearIndex(blocks),
|
|
2079
2234
|
}
|
|
2080
2235
|
}
|
|
2081
2236
|
/**
|
|
@@ -2754,6 +2909,101 @@ function createLibraryModel() {
|
|
|
2754
2909
|
return { ratio: Math.min(1, page / last), block }
|
|
2755
2910
|
}
|
|
2756
2911
|
|
|
2912
|
+
// ---- 作者-年份制引用(T25b,纯函数) ----
|
|
2913
|
+
// 很多论文的参考文献**不带编号**(实测抽查 4 篇里有 2 篇),正文改用 `(Zhang et al., 2020)`。
|
|
2914
|
+
// 这条链路独立于编号制:编号制未命中时才启用,两套规则不互相干扰。
|
|
2915
|
+
//
|
|
2916
|
+
// 🔴 与编号制最大的差别是**没有唯一键**。"姓氏 + 年份"在 AI 领域经常撞车(同一作者同年
|
|
2917
|
+
// 多篇),所以索引是一对多:命中几条就全列,由读者自己判断。宁可显示两条让人选,也不要
|
|
2918
|
+
// 挑一条赌对 —— 猜错时读者无从知道错。
|
|
2919
|
+
/** `(Surname et al., 2020)` / `(Surname & Other, 2020)` / `(Surname, 2020)`。 */
|
|
2920
|
+
const AUTHOR_YEAR =
|
|
2921
|
+
/\(\s*([\p{Lu}][\p{L}'-]+)(?:\s+et\s+al\.?|\s*(?:&|and)\s*[\p{Lu}][\p{L}'-]+)?\s*,\s*(\d{4})[a-z]?\s*\)/gu
|
|
2922
|
+
// 🔴 三个正则都**必须带 u flag**:`\p{Lu}` / `\p{L}` 是 Unicode 属性转义,没有 u 时
|
|
2923
|
+
// 会被当成字面量 p / L / u,正则永远匹配不上而**不报错** —— 表现为索引恒为空、悬停全无反应。
|
|
2924
|
+
// 这条踩过一次(首版漏了 u,4 条作者-年份用例全挂)。
|
|
2925
|
+
/** 参考文献条目开头的姓:surname, Initial. */
|
|
2926
|
+
const REFERENCE_SURNAME = /^\s*(?:\[\s*\d{1,3}\s*\]\s*)?([\p{Lu}][\p{L}'-]+)\s*,/u
|
|
2927
|
+
/** 条目里出现的第一个四位数年份。 */
|
|
2928
|
+
const REFERENCE_YEAR = /\b(\d{4})\b/
|
|
2929
|
+
|
|
2930
|
+
/** 匹配键:姓氏折叠成小写 + 年份。作者大小写与变音符号不在这一层处理。 */
|
|
2931
|
+
function authorYearKey(surname, year) {
|
|
2932
|
+
return typeof surname === 'string' && surname !== '' && /^\d{4}$/.test(String(year))
|
|
2933
|
+
? surname.toLowerCase() + '|' + year
|
|
2934
|
+
: ''
|
|
2935
|
+
}
|
|
2936
|
+
|
|
2937
|
+
/**
|
|
2938
|
+
* 从参考文献块建「姓氏|年份 → 块」的一对多索引。
|
|
2939
|
+
*
|
|
2940
|
+
* 姓氏取条目开头(可带 `[n]` 前缀)的第一个大写词,年份取条目里第一个四位数 ——
|
|
2941
|
+
* 学术期刊格式千差万别,这是**启发式**而不是解析器。认不出来的条目直接跳过:
|
|
2942
|
+
* 索引里少一条只是那条引用悬停没反应,不会错配到别的条目上。
|
|
2943
|
+
*
|
|
2944
|
+
* @param {unknown} blocks 阅读器的块列表。
|
|
2945
|
+
* @returns {Map<string, Array<{blockId: string, text: string}>>}
|
|
2946
|
+
*/
|
|
2947
|
+
function authorYearIndex(blocks) {
|
|
2948
|
+
const index = new Map()
|
|
2949
|
+
for (const block of Array.isArray(blocks) ? blocks : []) {
|
|
2950
|
+
if (block === null || typeof block !== 'object' || typeof block.id !== 'string') continue
|
|
2951
|
+
if (readerKindOf(block) !== 'references') continue
|
|
2952
|
+
const source = typeof block.sourceText === 'string' ? block.sourceText : ''
|
|
2953
|
+
const surname = REFERENCE_SURNAME.exec(source)
|
|
2954
|
+
const year = REFERENCE_YEAR.exec(source)
|
|
2955
|
+
if (surname === null || year === null) continue
|
|
2956
|
+
const key = authorYearKey(surname[1], year[1])
|
|
2957
|
+
if (key === '') continue
|
|
2958
|
+
const entry = { blockId: block.id, text: source.replace(REFERENCE_LEAD, '').trim() }
|
|
2959
|
+
const list = index.get(key)
|
|
2960
|
+
if (list === undefined) index.set(key, [entry])
|
|
2961
|
+
else if (!list.some(item => item.blockId === block.id)) list.push(entry)
|
|
2962
|
+
}
|
|
2963
|
+
return index
|
|
2964
|
+
}
|
|
2965
|
+
|
|
2966
|
+
/**
|
|
2967
|
+
* 把段落切成「文字段 + 作者-年份标记」。
|
|
2968
|
+
*
|
|
2969
|
+
* 只产出**索引里真有**的标记:查不到的括号原样留在文本里。这一条与编号制同一口径 ——
|
|
2970
|
+
* 点了没反应/悬停了没内容,比没有标记更坏。
|
|
2971
|
+
*
|
|
2972
|
+
* 标记带 `ambiguous`:命中多条时为真,渲染层据此提示"共 N 条可能匹配"。
|
|
2973
|
+
*
|
|
2974
|
+
* @param {unknown} text 段落文本。
|
|
2975
|
+
* @param {unknown} index {@link authorYearIndex} 的产物。
|
|
2976
|
+
* @returns {Array<{type: string, value: string, surname: string, year: string, ambiguous?: boolean, count?: number} | {type: 'text', text: string}>}
|
|
2977
|
+
*/
|
|
2978
|
+
function inlineAuthors(text, index) {
|
|
2979
|
+
if (typeof text !== 'string' || text === '' || !(index instanceof Map) || index.size === 0) return []
|
|
2980
|
+
const parts = []
|
|
2981
|
+
let plain = ''
|
|
2982
|
+
const flush = () => {
|
|
2983
|
+
if (plain !== '') parts.push({ type: 'text', text: plain })
|
|
2984
|
+
plain = ''
|
|
2985
|
+
}
|
|
2986
|
+
let cursor = 0
|
|
2987
|
+
AUTHOR_YEAR.lastIndex = 0
|
|
2988
|
+
for (let match = AUTHOR_YEAR.exec(text); match !== null; match = AUTHOR_YEAR.exec(text)) {
|
|
2989
|
+
const key = authorYearKey(match[1], match[2])
|
|
2990
|
+
const hits = key === '' ? undefined : index.get(key)
|
|
2991
|
+
if (hits === undefined || hits.length === 0) continue
|
|
2992
|
+
plain += text.slice(cursor, match.index)
|
|
2993
|
+
flush()
|
|
2994
|
+
parts.push({
|
|
2995
|
+
type: 'author',
|
|
2996
|
+
value: match[0],
|
|
2997
|
+
surname: match[1],
|
|
2998
|
+
year: match[2],
|
|
2999
|
+
...(hits.length > 1 ? { ambiguous: true, count: hits.length } : {}),
|
|
3000
|
+
})
|
|
3001
|
+
cursor = match.index + match[0].length
|
|
3002
|
+
}
|
|
3003
|
+
plain += text.slice(cursor)
|
|
3004
|
+
flush()
|
|
3005
|
+
return parts
|
|
3006
|
+
}
|
|
2757
3007
|
// END CLIENT PARTS
|
|
2758
3008
|
// 量容器的实际宽度(resize 时重量):ResizeObserver 不在就退回 window resize,清理函数
|
|
2759
3009
|
// 一律走同一个 disconnect 形状。回调 ref 而不是 ref 对象 —— 节点挂上/换掉时都要重挂观察者。
|
|
@@ -3060,8 +3310,29 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
3060
3310
|
.paper-pdf-line{position:absolute;display:block;white-space:nowrap;pointer-events:auto}
|
|
3061
3311
|
/* The word box is the PDF word rect including its trailing space; the invisible glyphs are
|
|
3062
3312
|
stretched across it, so the browser paints the selection over the gap as well. */
|
|
3063
|
-
.paper-pdf-word{display:inline-block;vertical-align:top;white-space:pre;font-family:serif;color:transparent;pointer-events:auto;cursor:text;user-select:text;overflow:hidden}
|
|
3064
|
-
|
|
3313
|
+
.paper-pdf-word{display:inline-block;vertical-align:top;white-space:pre;font-family:serif;font-weight:400;font-style:normal;font-kerning:none;color:transparent;pointer-events:auto;cursor:text;user-select:text;overflow:hidden}
|
|
3314
|
+
/* 物理行仍负责行间的鼠标命中,但不把其整片行盒涂成蓝色;只给实际选中文字上色。 */
|
|
3315
|
+
.paper-pdf-line::selection{background:transparent;color:transparent}
|
|
3316
|
+
.paper-pdf-char{white-space:pre}
|
|
3317
|
+
.paper-pdf-word::selection{background:rgba(66,133,244,.22);color:transparent}
|
|
3318
|
+
.paper-pdf-char::selection{background:rgba(66,133,244,.22);color:transparent}
|
|
3319
|
+
.paper-cite{background:transparent;border-radius:2px;cursor:help;transition:background-color var(--paper-t-feedback)}
|
|
3320
|
+
.paper-cite:hover,.paper-cite:focus-visible{background:color-mix(in srgb,var(--paper-accent,#3b82f6) 16%,transparent);outline:none}
|
|
3321
|
+
/* 🔴 PDF 原页这侧**不套 span**:文字层是「绝对定位行盒 + 定宽词盒 + 逐字符盒」,把字节点从
|
|
3322
|
+
这些盒子里 extractContents 出来再塞进一个新 span,等于让它们退回普通行内流 —— 位置全歪,
|
|
3323
|
+
背景框压在相邻文字上(实测截图:蓝块明显大于 [23, 9, 37, 13] 那几个字,还跑到左边距上)。
|
|
3324
|
+
这里改为直接给既有的字符 / 词元素打标记,DOM 结构一个节点都不动。
|
|
3325
|
+
背景因此落在字形自己的内容盒上,刚好是字的大小。 */
|
|
3326
|
+
.paper-pdf-text [data-cite]{cursor:help}
|
|
3327
|
+
/* 🔴 **同一引用标记要整组一起亮**:文字层逐字符渲染,每个 .paper-pdf-char 都是独立元素,
|
|
3328
|
+
CSS 的 :hover 只命中鼠标正下方那一个 —— 实测「[41, 44, 13, 16] 里只有鼠标指着的那个字变色」。
|
|
3329
|
+
所以这里**不用 :hover**:高亮由 onCiteOver/onCiteOut 按组号写 is-cite-active,
|
|
3330
|
+
一次点亮同一处标记的全部字符。混用两者会让"鼠标下的那个"和"整组"两套样式打架。 */
|
|
3331
|
+
.paper-pdf-text [data-cite].is-cite-active{background:color-mix(in srgb,var(--paper-accent,#3b82f6) 16%,transparent)}
|
|
3332
|
+
.paper-cite-pop{position:fixed;z-index:var(--paper-layer-float,40);box-sizing:border-box;width:min(420px,calc(100vw - 16px));max-height:280px;overflow:auto;display:flex;flex-direction:column;gap:0;padding:8px 10px;border:1px solid var(--paper-border);border-radius:8px;background:var(--paper-bg,#fff);color:var(--paper-text);box-shadow:0 8px 24px rgba(0,0,0,.16);font-size:12px;line-height:1.55}
|
|
3333
|
+
.paper-cite-entry{padding:6px 0;overflow-wrap:anywhere;user-select:text}
|
|
3334
|
+
.paper-cite-entry + .paper-cite-entry{border-top:1px solid var(--paper-border)}
|
|
3335
|
+
.paper-cite-note{margin-top:4px;padding-top:6px;border-top:1px solid var(--paper-border);color:var(--paper-muted);font-size:11px}
|
|
3065
3336
|
.paper-mark-draft{position:fixed;z-index:var(--paper-layer-float,40);display:flex;flex-direction:column;gap:0;width:min(400px,calc(100vw - 16px));max-height:70vh;min-height:0;overflow:auto;padding:0;border:1px solid var(--paper-border);border-radius:10px;background:var(--paper-bg,#fff);color:var(--paper-text);box-shadow:0 8px 28px rgba(0,0,0,.18);font-size:12px;line-height:1.5}
|
|
3066
3337
|
.paper-selection-toolbar{display:flex;align-items:center;flex:0 0 auto;gap:2px;padding:5px;overflow-x:auto;white-space:nowrap}
|
|
3067
3338
|
.paper-selection-toolbar .paper-btn{flex:0 0 auto;border:0;border-radius:5px;padding:6px;background:transparent;font-size:12px}
|
|
@@ -3648,6 +3919,292 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
3648
3919
|
* @param {object} row 这一行对应的渲染行(拿 payload)。
|
|
3649
3920
|
* @param {{pathImages: object, documentId: string, onJump: Function}} options
|
|
3650
3921
|
*/
|
|
3922
|
+
/**
|
|
3923
|
+
* 给正文里已渲染的引用标记包一层可悬停的 span(T25b)。
|
|
3924
|
+
*
|
|
3925
|
+
* 🔴 **为什么要走 DOM 而不是渲染**:正文整块交给宿主的 MarkdownText,拿不到里面的节点;
|
|
3926
|
+
* 而绕开 MarkdownText 自己渲染会破坏「正文统一由渲染器排版」这条既有约束
|
|
3927
|
+
* (reader-render 的用例名就叫「阅读器正文渲染成 MarkdownText,而不是自己画的」)。
|
|
3928
|
+
* 所以与划线同一范式({@link paintMarkHighlights}):**渲染不动,只在已挂载的 DOM 上
|
|
3929
|
+
* 按文本找位置**。字形/排版全归渲染器,这里只加属性与外层包装。
|
|
3930
|
+
*
|
|
3931
|
+
* 与划线的差别:划线用 CSS.highlights 画横线(不需要改 DOM 结构),这里必须**包元素**
|
|
3932
|
+
* 才能拿到 hover 事件。包的是标记自己那几个字,前后文本节点原样保留、顺序不变。
|
|
3933
|
+
*
|
|
3934
|
+
* 找不到 / 已经包过的块跳过;重复调用是幂等的(先按 data-cite 还原再重建)。
|
|
3935
|
+
*
|
|
3936
|
+
* @param {object|null} scope 正文容器。
|
|
3937
|
+
* @param {Map<string, object[]>} grouped 块 id → 该块的标记片段。
|
|
3938
|
+
* @returns {number} 实际包上的标记数(测试断言用)。
|
|
3939
|
+
*/
|
|
3940
|
+
/**
|
|
3941
|
+
* 块 id → 该块的引用标记片段。渲染层与 DOM 装饰层共用这一份判定。
|
|
3942
|
+
*
|
|
3943
|
+
* 编号制整篇一条都没认出来时才走作者-年份制(与 readerNumbers 的 authorYears 同源),
|
|
3944
|
+
* 两套规则不叠加:同一段既按编号切又按作者切,会切出互相重叠的标记。
|
|
3945
|
+
*
|
|
3946
|
+
* @param {unknown} blocks /reader 的块列表。
|
|
3947
|
+
* @param {unknown} numbers {@link readerNumbers} 的产物。
|
|
3948
|
+
* @returns {Map<string, object[]>}
|
|
3949
|
+
*/
|
|
3950
|
+
/**
|
|
3951
|
+
* 按「姓氏|年份」从一对多索引里取出候选条目。
|
|
3952
|
+
*
|
|
3953
|
+
* 键的构造与 {@link authorYearIndex} 一致;这里只做查表,不重新解析。
|
|
3954
|
+
* 取不到时返回空数组 —— 调用方据此不弹窗(宁可不显示,也不显示错的)。
|
|
3955
|
+
*
|
|
3956
|
+
* @param {unknown} index {@link authorYearIndex} 的产物。
|
|
3957
|
+
* @param {unknown} surname 标记上的姓氏。
|
|
3958
|
+
* @param {unknown} year 标记上的年份。
|
|
3959
|
+
* @returns {Array<{blockId: string, text: string}>}
|
|
3960
|
+
*/
|
|
3961
|
+
/**
|
|
3962
|
+
* 引用浮层的位置。
|
|
3963
|
+
*
|
|
3964
|
+
* 锚点是标记自己的 bounding rect:优先放在**下方** 6px;下方空间不够(浮层估计高 160px)
|
|
3965
|
+
* 就翻到上方。左右按视口夹住,宽度由 CSS 上限(420px)保证,不让它越出屏幕。
|
|
3966
|
+
*
|
|
3967
|
+
* 锚点拿不到(测试替身、节点已脱离)时返回 hidden —— 位置宁可不显示,也不摆在 (0,0)。
|
|
3968
|
+
*
|
|
3969
|
+
* @param {{anchor?: {left: number, top: number, bottom: number}|null}} hover 悬停状态。
|
|
3970
|
+
* @returns {object} React style。
|
|
3971
|
+
*/
|
|
3972
|
+
function citePopStyle(hover) {
|
|
3973
|
+
const anchor = hover?.anchor ?? null
|
|
3974
|
+
if (anchor === null || !Number.isFinite(anchor.left) || !Number.isFinite(anchor.top)) {
|
|
3975
|
+
return { visibility: 'hidden', left: 0, top: 0 }
|
|
3976
|
+
}
|
|
3977
|
+
const height = 160
|
|
3978
|
+
const width = 420
|
|
3979
|
+
const gap = 6
|
|
3980
|
+
const viewportWidth = typeof window !== 'undefined' && Number(window.innerWidth) > 0 ? Number(window.innerWidth) : 1280
|
|
3981
|
+
const viewportHeight = typeof window !== 'undefined' && Number(window.innerHeight) > 0 ? Number(window.innerHeight) : 800
|
|
3982
|
+
const below = anchor.bottom + gap
|
|
3983
|
+
const top = below + height > viewportHeight && anchor.top - height - gap > 0
|
|
3984
|
+
? anchor.top - height - gap
|
|
3985
|
+
: below
|
|
3986
|
+
const left = Math.max(8, Math.min(anchor.left, viewportWidth - width - 8))
|
|
3987
|
+
return { left, top }
|
|
3988
|
+
}
|
|
3989
|
+
|
|
3990
|
+
function ambiguousLookup(index, surname, year) {
|
|
3991
|
+
if (!(index instanceof Map)) return []
|
|
3992
|
+
const key = authorYearKey(surname, year)
|
|
3993
|
+
if (key === '') return []
|
|
3994
|
+
const hits = index.get(key)
|
|
3995
|
+
return Array.isArray(hits) ? hits : []
|
|
3996
|
+
}
|
|
3997
|
+
|
|
3998
|
+
/**
|
|
3999
|
+
* 块 id → 该块要包成可悬停元素的标记片段。
|
|
4000
|
+
*
|
|
4001
|
+
* 🔴 **与渲染层同一口径**:`readerRows` 已经按 `sourceText` 算好了 `row.inline.parts`,
|
|
4002
|
+
* 这里直接复用,不再自己调一遍 `inlineRefs` —— 两处各算一次迟早会分叉(第一版就是这样:
|
|
4003
|
+
* 这边用 `displayInlineHtml` 之后的串、那边用原始串,含上标的段落两边对不上)。
|
|
4004
|
+
*
|
|
4005
|
+
* 没有 `row.inline.parts` 时(非 render 档)才退回自行计算,保证函数单独可测。
|
|
4006
|
+
*
|
|
4007
|
+
* @param {Map<string, object>} rowsById readerRows 产物按 id 建的索引(可为空)。
|
|
4008
|
+
* @param {unknown} blocks 块列表。
|
|
4009
|
+
* @param {unknown} numbers {@link readerNumbers} 的产物。
|
|
4010
|
+
* @returns {Map<string, object[]>}
|
|
4011
|
+
*/
|
|
4012
|
+
function citePartsOf(rowsById, blocks, numbers) {
|
|
4013
|
+
const grouped = new Map()
|
|
4014
|
+
if (!Array.isArray(blocks)) return grouped
|
|
4015
|
+
const authorYears = numbers?.authorYears instanceof Map ? numbers.authorYears : new Map()
|
|
4016
|
+
for (const block of blocks) {
|
|
4017
|
+
if (block === null || typeof block !== 'object' || typeof block.id !== 'string') continue
|
|
4018
|
+
const row = rowsById instanceof Map ? rowsById.get(block.id) : undefined
|
|
4019
|
+
const known = Array.isArray(row?.inline?.parts) ? row.inline.parts : null
|
|
4020
|
+
let tags
|
|
4021
|
+
if (known !== null) {
|
|
4022
|
+
tags = known.filter(part => part.type !== 'text')
|
|
4023
|
+
} else {
|
|
4024
|
+
const payload = readerPayloadOf(block, String(block.sourceText ?? ''), '')
|
|
4025
|
+
if (payload.markdown !== true || readerKindOf(block) === 'references') continue
|
|
4026
|
+
const sourceText = displayInlineHtml(String(block.sourceText ?? ''))
|
|
4027
|
+
const byNumber = inlineRefs(sourceText, numbers)
|
|
4028
|
+
const parts = byNumber.length > 0 ? byNumber : inlineAuthors(sourceText, authorYears)
|
|
4029
|
+
tags = parts.filter(part => part.type !== 'text')
|
|
4030
|
+
}
|
|
4031
|
+
if (tags.length > 0) grouped.set(block.id, tags)
|
|
4032
|
+
}
|
|
4033
|
+
return grouped
|
|
4034
|
+
}
|
|
4035
|
+
|
|
4036
|
+
/**
|
|
4037
|
+
* 收集一个宿主节点里的可见文本节点,并拼出「整串 + 每段起点」。
|
|
4038
|
+
*
|
|
4039
|
+
* 🔴 **必须拼接而不是逐节点找**:PDF 文字层是逐字符渲染的(每个 `.paper-pdf-char`
|
|
4040
|
+
* 只装一个字符),`indexOf('[41]')` 在单字符节点里永远不命中 —— 这正是原版在原页
|
|
4041
|
+
* 那侧全无反应的原因。拼接后整串里才有完整标记,再按偏移映射回具体节点。
|
|
4042
|
+
*
|
|
4043
|
+
* 只收父级仍是本宿主的文本节点;`<style>` 之类不参与。
|
|
4044
|
+
*/
|
|
4045
|
+
function citeTextRuns(node, doc = typeof document === 'undefined' ? null : document) {
|
|
4046
|
+
if (doc === null || typeof doc.createTreeWalker !== 'function') return { runs: [], joined: '' }
|
|
4047
|
+
const walker = doc.createTreeWalker(node, 4 /* NodeFilter.SHOW_TEXT */)
|
|
4048
|
+
const runs = []
|
|
4049
|
+
let joined = ''
|
|
4050
|
+
for (let current = walker.nextNode(); current !== null; current = walker.nextNode()) {
|
|
4051
|
+
const value = String(current.nodeValue ?? '')
|
|
4052
|
+
if (value === '') continue
|
|
4053
|
+
runs.push({ node: current, start: joined.length, length: value.length })
|
|
4054
|
+
joined += value
|
|
4055
|
+
}
|
|
4056
|
+
return { runs, joined }
|
|
4057
|
+
}
|
|
4058
|
+
|
|
4059
|
+
/**
|
|
4060
|
+
* 把整串里的 [from, to) 拆成「一段或连续几段」,供 Range 逐段落点。
|
|
4061
|
+
*
|
|
4062
|
+
* 纯字符节点时一个标记会横跨好几个文本节点;跨节点的 Range 无法 `surroundContents`
|
|
4063
|
+
* (会抛错),所以这里返回逐段的落点,由调用方按段合并处理。
|
|
4064
|
+
*/
|
|
4065
|
+
function citeSlice(runs, from, to) {
|
|
4066
|
+
const pieces = []
|
|
4067
|
+
for (const run of runs) {
|
|
4068
|
+
const start = Math.max(from, run.start)
|
|
4069
|
+
const end = Math.min(to, run.start + run.length)
|
|
4070
|
+
if (end <= start) continue
|
|
4071
|
+
pieces.push({ node: run.node, start: start - run.start, end: end - run.start })
|
|
4072
|
+
}
|
|
4073
|
+
return pieces
|
|
4074
|
+
}
|
|
4075
|
+
|
|
4076
|
+
/** 正则元字符转义;只给 {@link citeLocate} 用。空格不在其中,容错那一步要靠它保留。 */
|
|
4077
|
+
function citeEscape(text) {
|
|
4078
|
+
return String(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
4079
|
+
}
|
|
4080
|
+
|
|
4081
|
+
/**
|
|
4082
|
+
* 在拼接好的整串里找出一个引用标记出现的**全部**位置。
|
|
4083
|
+
*
|
|
4084
|
+
* 先按原样精确找;一处都没有时才按「空白容错」再找一次。两条都需要:
|
|
4085
|
+
* PDF 文字层把每个词单独成一个 span,词间空白由 pdf-page 按版面重算,于是同一处标记
|
|
4086
|
+
* 在 DOM 里的空白数量(甚至有没有空白)都可能与解析正文那一份不同 —— 正文栏走精确匹配,
|
|
4087
|
+
* 容错只在 PDF 那侧兜底。
|
|
4088
|
+
*
|
|
4089
|
+
* 🔴 **容错不能优先**:让 `\s*` 先上场时,`[4]` 会命中到 `[44]` 这类串的内部。
|
|
4090
|
+
* 精确命中不为空就说明正文自洽,这时容错只会引入误判。
|
|
4091
|
+
*
|
|
4092
|
+
* @returns {Array<{at: number, length: number}>} 命中位置与**实际**长度(容错命中会比 raw 长)。
|
|
4093
|
+
*/
|
|
4094
|
+
function citeLocate(joined, raw) {
|
|
4095
|
+
const exact = []
|
|
4096
|
+
let at = joined.indexOf(raw)
|
|
4097
|
+
while (at >= 0) {
|
|
4098
|
+
exact.push({ at, length: raw.length })
|
|
4099
|
+
at = joined.indexOf(raw, at + raw.length)
|
|
4100
|
+
}
|
|
4101
|
+
if (exact.length > 0) return exact
|
|
4102
|
+
const pattern = new RegExp(citeEscape(raw).replace(/\s+/g, '\\s*'), 'g')
|
|
4103
|
+
const loose = []
|
|
4104
|
+
for (const match of joined.matchAll(pattern)) loose.push({ at: match.index, length: match[0].length })
|
|
4105
|
+
return loose
|
|
4106
|
+
}
|
|
4107
|
+
|
|
4108
|
+
let citeSpanCounter = 0
|
|
4109
|
+
/**
|
|
4110
|
+
* 给已挂载 DOM 里的引用标记包一层可悬停 span。
|
|
4111
|
+
*
|
|
4112
|
+
* @param {object|null} scope 容器。
|
|
4113
|
+
* @param {Map<string, object[]>} grouped 块 id → 该块的标记片段。
|
|
4114
|
+
* @returns {number} 实际包上的标记数。
|
|
4115
|
+
*/
|
|
4116
|
+
function paintCiteSpans(scope, grouped, doc = typeof document === 'undefined' ? null : document) {
|
|
4117
|
+
if (scope === null || typeof scope !== 'object' || typeof scope.querySelectorAll !== 'function') return 0
|
|
4118
|
+
if (!(grouped instanceof Map) || grouped.size === 0) return 0
|
|
4119
|
+
if (doc === null || doc === undefined || typeof doc.createTreeWalker !== 'function') return 0
|
|
4120
|
+
let painted = 0
|
|
4121
|
+
for (const node of scope.querySelectorAll('[data-block-id]')) {
|
|
4122
|
+
const blockId = typeof node?.getAttribute === 'function' ? node.getAttribute('data-block-id') : null
|
|
4123
|
+
const parts = blockId === null ? undefined : grouped.get(blockId)
|
|
4124
|
+
if (!Array.isArray(parts) || parts.length === 0) continue
|
|
4125
|
+
if (typeof node.querySelector === 'function' && node.querySelector('.paper-cite') !== null) continue
|
|
4126
|
+
const { runs, joined } = citeTextRuns(node, doc)
|
|
4127
|
+
if (runs.length === 0) continue
|
|
4128
|
+
// 从后往前包:包前面的标记会切分后面的文本节点,倒序处理可让后面的偏移一直有效。
|
|
4129
|
+
const hits = []
|
|
4130
|
+
for (const part of parts) {
|
|
4131
|
+
const raw = part.raw !== undefined
|
|
4132
|
+
? String(part.raw)
|
|
4133
|
+
: part.type === 'ref' ? `[${part.value}]` : String(part.value ?? '')
|
|
4134
|
+
if (raw === '') continue
|
|
4135
|
+
// 位置由 citeLocate 给:精确优先、空白容错兜底(PDF 文字层的空白与解析正文不同源)。
|
|
4136
|
+
for (const found of citeLocate(joined, raw)) hits.push({ part, raw, at: found.at, length: found.length })
|
|
4137
|
+
}
|
|
4138
|
+
hits.sort((a, b) => b.at - a.at)
|
|
4139
|
+
const spanId = () => String((citeSpanCounter += 1))
|
|
4140
|
+
for (const hit of hits) {
|
|
4141
|
+
const pieces = citeSlice(runs, hit.at, hit.at + hit.length)
|
|
4142
|
+
if (pieces.length === 0) continue
|
|
4143
|
+
const first = pieces[0]
|
|
4144
|
+
if (first.node.parentNode === null || first.node.parentNode === undefined) continue
|
|
4145
|
+
// 🔴 **PDF 原页走另一条路**:那里不套 span,只给已有元素打标记。
|
|
4146
|
+
// 文字层是「绝对定位行盒 → 定宽词盒 → 逐字符盒」,把字节点 extractContents 出来
|
|
4147
|
+
// 塞进新 span 会让它们退回普通行内流:位置全歪、背景框压在相邻文字上(实测截图)。
|
|
4148
|
+
// 打标记不改 DOM 结构,背景就落在字形自己的盒子上,大小刚好。
|
|
4149
|
+
if (typeof node.querySelectorAll === 'function' && node.closest?.('.paper-pdf-text') != null) {
|
|
4150
|
+
const markId = spanId()
|
|
4151
|
+
const marked = []
|
|
4152
|
+
for (const piece of pieces) {
|
|
4153
|
+
// 逐字符渲染时落点是 .paper-pdf-char,整词时是 .paper-pdf-word。
|
|
4154
|
+
// 跨字符的那一段要把沿途**每个**字符都标上 —— 只标首尾会漏掉中间几个字,
|
|
4155
|
+
// 表现就是「[41, 44, 13, 16] 里只有一两个字能悬停」。
|
|
4156
|
+
const hostElement = piece.node.parentNode
|
|
4157
|
+
if (hostElement === null || hostElement === undefined) continue
|
|
4158
|
+
if (!marked.includes(hostElement)) marked.push(hostElement)
|
|
4159
|
+
}
|
|
4160
|
+
for (const element of marked) {
|
|
4161
|
+
if (element === node) continue
|
|
4162
|
+
element.setAttribute('data-cite', hit.part.type)
|
|
4163
|
+
element.setAttribute('data-cite-key', String(hit.part.value ?? ''))
|
|
4164
|
+
if (hit.part.items !== undefined) element.setAttribute('data-cite-items', hit.part.items.join(','))
|
|
4165
|
+
if (hit.part.surname !== undefined) element.setAttribute('data-cite-surname', String(hit.part.surname))
|
|
4166
|
+
if (hit.part.year !== undefined) element.setAttribute('data-cite-year', String(hit.part.year))
|
|
4167
|
+
if (hit.part.ambiguous === true) element.setAttribute('data-cite-ambiguous', String(hit.part.count ?? 2))
|
|
4168
|
+
element.setAttribute('data-cite-span', markId)
|
|
4169
|
+
}
|
|
4170
|
+
if (marked.length > 0) painted += 1
|
|
4171
|
+
continue
|
|
4172
|
+
}
|
|
4173
|
+
const span = doc.createElement('span')
|
|
4174
|
+
span.className = 'paper-cite'
|
|
4175
|
+
span.setAttribute('data-cite', hit.part.type)
|
|
4176
|
+
span.setAttribute('data-cite-key', String(hit.part.value ?? ''))
|
|
4177
|
+
if (hit.part.items !== undefined) span.setAttribute('data-cite-items', hit.part.items.join(','))
|
|
4178
|
+
if (hit.part.surname !== undefined) span.setAttribute('data-cite-surname', String(hit.part.surname))
|
|
4179
|
+
if (hit.part.year !== undefined) span.setAttribute('data-cite-year', String(hit.part.year))
|
|
4180
|
+
if (hit.part.ambiguous === true) span.setAttribute('data-cite-ambiguous', String(hit.part.count ?? 2))
|
|
4181
|
+
span.setAttribute('data-cite-span', spanId())
|
|
4182
|
+
try {
|
|
4183
|
+
if (pieces.length === 1) {
|
|
4184
|
+
// 最常见:标记在一个文本节点里(正文栏的段落)。
|
|
4185
|
+
const range = doc.createRange()
|
|
4186
|
+
range.setStart(first.node, first.start)
|
|
4187
|
+
range.setEnd(first.node, first.end)
|
|
4188
|
+
range.surroundContents(span)
|
|
4189
|
+
} else {
|
|
4190
|
+
// 逐字符渲染:把标记覆盖到的那些节点整体搬进 span。
|
|
4191
|
+
// 🔴 用 extractContents 而不是 surroundContents —— 后者跨节点必抛。
|
|
4192
|
+
const range = doc.createRange()
|
|
4193
|
+
range.setStart(first.node, first.start)
|
|
4194
|
+
const last = pieces[pieces.length - 1]
|
|
4195
|
+
range.setEnd(last.node, last.end)
|
|
4196
|
+
span.appendChild(range.extractContents())
|
|
4197
|
+
range.insertNode(span)
|
|
4198
|
+
}
|
|
4199
|
+
painted += 1
|
|
4200
|
+
} catch {
|
|
4201
|
+
// 跨元素边界等失败的情况跳过:宁可这一处没有悬停,也不破坏正文。
|
|
4202
|
+
}
|
|
4203
|
+
}
|
|
4204
|
+
}
|
|
4205
|
+
return painted
|
|
4206
|
+
}
|
|
4207
|
+
|
|
3651
4208
|
function readerBlockNode(line, row, options) {
|
|
3652
4209
|
const payload = row?.payload ?? null
|
|
3653
4210
|
const tag = line.column === "src" ? "p" : line.tag
|
|
@@ -3788,6 +4345,17 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
3788
4345
|
const matchingMarks = React.useRef(new Set())
|
|
3789
4346
|
const [matchingIds, setMatchingIds] = React.useState([])
|
|
3790
4347
|
const markDraftEpoch = React.useRef(0)
|
|
4348
|
+
// ---- 引用悬停预览(T25b)----
|
|
4349
|
+
// cite = { key, type, anchor: {left, top, bottom}, entries: [{blockId, text}], ambiguous }
|
|
4350
|
+
// 只存**已经解析好**的内容:悬停时查表,不在这里做任何匹配计算。
|
|
4351
|
+
const [citeHover, setCiteHover] = React.useState(null)
|
|
4352
|
+
const citeTimer = React.useRef(null)
|
|
4353
|
+
/** 当前亮着的整组元素。见 showCite 的说明:PDF 逐字符渲染,高亮不能靠 CSS :hover。
|
|
4354
|
+
* 用 ref 而不是 state:它只管 DOM 类的加减,本身不参与渲染。 */
|
|
4355
|
+
const citeGroupOfMarked = React.useRef([])
|
|
4356
|
+
/** 悬停处理函数经它转存给 PDF 页面树 —— 那棵树在本组件渲染期构建,早于下面两个
|
|
4357
|
+
* const 的初始化位置(见 PDF 块上的注释)。初始值是空操作,避免 ref 未装时报错。 */
|
|
4358
|
+
const citeHandlersRef = React.useRef({ over: () => {}, out: () => {} })
|
|
3791
4359
|
// ---- 选区即时释义(D160) ----
|
|
3792
4360
|
// 条上那一行译文的状态:null / pending / ready / failed / no-text。每一项都带 key,
|
|
3793
4361
|
// 换过选区之后晚到的回复据此丢弃(否则 A 的译文会覆盖到 B 那一行上)。
|
|
@@ -5379,7 +5947,7 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
5379
5947
|
// ---- PDF page and native selection layer ----
|
|
5380
5948
|
const firstPage = React.useMemo(() => (readerBlocks.find(row => typeof row.page === 'number')?.page) ?? 1, [readerBlocks])
|
|
5381
5949
|
const currentPage = readerPage === null ? firstPage : readerPage
|
|
5382
|
-
const activePdf = pdfData?.documentId === readerId && pdfData.page === currentPage ? pdfData : null
|
|
5950
|
+
const activePdf = pdfData?.documentId === readerId && pdfData.page === currentPage && pdfData.cropped === pdfCrop ? pdfData : null
|
|
5383
5951
|
async function loadPdfPage(documentId = readerId, page = currentPage, force = false) {
|
|
5384
5952
|
if (!documentId) return
|
|
5385
5953
|
if (force) setPdfImageAttempt(value => value + 1)
|
|
@@ -5462,6 +6030,13 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
5462
6030
|
style: { left: bounds[0] * activePdf.width / 1000, top: bounds[1] * activePdf.height / 1000,
|
|
5463
6031
|
width: (bounds[2] - bounds[0]) * activePdf.width / 1000, height: (bounds[3] - bounds[1]) * activePdf.height / 1000 },
|
|
5464
6032
|
onMouseUp: event => { event.stopPropagation?.(); captureSelection(event, true) },
|
|
6033
|
+
// 引用悬停(T25b):原页这侧的引用标记同样是 paintCiteSpans 事后包出来的
|
|
6034
|
+
// (文字层的字符 span 就是 DOM 文字,包法与正文栏一致)。
|
|
6035
|
+
// 🔴 走 ref 转存而不是直接引用那两个箭头函数:PDF 页面树的 useMemo 在本组件的
|
|
6036
|
+
// 渲染期就执行,那时后面的 const 还处于 TDZ —— 直接引用会抛
|
|
6037
|
+
// "Cannot access 'onCiteOver' before initialization"(本机实测踩过)。
|
|
6038
|
+
onMouseOver: event => citeHandlersRef.current.over(event),
|
|
6039
|
+
onMouseOut: event => citeHandlersRef.current.out(event),
|
|
5465
6040
|
}, rows.map((row, rowIndex) => {
|
|
5466
6041
|
const size = Math.max(1, (row.bottom - row.top) * activePdf.height / 1000)
|
|
5467
6042
|
const rowWidth = (row.words.at(-1).bbox[2] - row.words[0].bbox[0]) * activePdf.width / 1000
|
|
@@ -5493,32 +6068,43 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
5493
6068
|
// 也是词与词之间那段**要被子选区覆盖**的宽度来源。
|
|
5494
6069
|
const core = word.text
|
|
5495
6070
|
const box = width + gap
|
|
5496
|
-
|
|
5497
|
-
//
|
|
5498
|
-
//
|
|
5499
|
-
//
|
|
5500
|
-
//
|
|
5501
|
-
//
|
|
5502
|
-
// 空格自己本来就占着那一格,字距只补字形与词框之间的差额。
|
|
5503
|
-
const glyphs = core.replace(/\s+$/, '')
|
|
6071
|
+
const nativeChars = pdfGlyphAdvances(word, activePdf.width / 1000)
|
|
6072
|
+
// 词盒仍取字框 + 词间隙,但浏览器选择范围必须有两个独立终点:
|
|
6073
|
+
// 字形的右沿对齐 PDF bbox[2],尾空格才延伸到下一个词 bbox[0]。
|
|
6074
|
+
// 原实现把整盒宽度摊给字形(只用 letter-spacing),本词最后几个字会越过 PDF 字框;
|
|
6075
|
+
// CSS 再以 overflow:hidden 截断选区,视觉上就像蓝色区域和文字错位。
|
|
6076
|
+
// 🔴 不用 scaleX:字符光标及 Range 的字形位置必须继续由浏览器正常排版。
|
|
5504
6077
|
const stretch = node => {
|
|
5505
|
-
if (!node || !(
|
|
6078
|
+
if (!node || !(width > 0)) return
|
|
5506
6079
|
const context = pdfMeasureRef.current ??= document.createElement('canvas').getContext?.('2d')
|
|
5507
6080
|
if (!context) return
|
|
5508
6081
|
context.font = size + 'px serif'
|
|
5509
|
-
const
|
|
5510
|
-
|
|
5511
|
-
|
|
5512
|
-
|
|
5513
|
-
|
|
6082
|
+
const metrics = pdfWordMetrics(core, width, gap, text => context.measureText(text).width)
|
|
6083
|
+
if (!metrics) return
|
|
6084
|
+
node.style.letterSpacing = metrics.letterSpacing + 'px'
|
|
6085
|
+
node.style.wordSpacing = metrics.wordSpacing + 'px'
|
|
6086
|
+
}
|
|
6087
|
+
// 逐字对齐时,每个独立文本 span 只包含一个 PDF 字符。
|
|
6088
|
+
// CSS 的末字 letter-spacing 会自然画满字符与下一个字符之间的空当;
|
|
6089
|
+
// 选区由连续的原生文本节点组成,不会在两个字符之间留下蓝色断缝。
|
|
6090
|
+
const stretchChar = (node, advance) => {
|
|
6091
|
+
if (!node) return
|
|
6092
|
+
const context = pdfMeasureRef.current ??= document.createElement('canvas').getContext?.('2d')
|
|
6093
|
+
if (!context) return
|
|
6094
|
+
context.font = size + 'px serif'
|
|
6095
|
+
node.style.letterSpacing = (advance - context.measureText(node.textContent).width) + 'px'
|
|
5514
6096
|
}
|
|
5515
6097
|
return h('span', {
|
|
5516
6098
|
className: 'paper-pdf-word', key: index,
|
|
5517
|
-
ref: stretch,
|
|
6099
|
+
ref: nativeChars ? undefined : stretch,
|
|
5518
6100
|
style: { width: box, height: band, lineHeight: size + 'px', fontSize: size,
|
|
6101
|
+
letterSpacing: 'normal', wordSpacing: 'normal',
|
|
5519
6102
|
boxSizing: 'border-box', paddingTop: before,
|
|
5520
6103
|
marginLeft: index > 0 ? Math.min(0, word.bbox[0] - row.words[index - 1].bbox[2]) * activePdf.width / 1000 : 0 },
|
|
5521
|
-
},
|
|
6104
|
+
}, nativeChars ? nativeChars.map((char, i) => h('span', {
|
|
6105
|
+
className: 'paper-pdf-char', key: i,
|
|
6106
|
+
ref: node => stretchChar(node, char.advance),
|
|
6107
|
+
}, char.text)) : core)
|
|
5522
6108
|
}))
|
|
5523
6109
|
}))
|
|
5524
6110
|
})) : null), [activePdf, readerId, currentPage, pdfZoom, pdfImageAttempt, pdfCrop])
|
|
@@ -5659,6 +6245,198 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
5659
6245
|
)
|
|
5660
6246
|
return undefined
|
|
5661
6247
|
}, [highlightSupport, view, reader, marks, paneView, activePdf])
|
|
6248
|
+
// ---- 引用悬停预览(T25b)----
|
|
6249
|
+
// 与划线同一范式:渲染树不动,effect 里把已挂载的 DOM 包上 span(见 paintCiteSpans)。
|
|
6250
|
+
// 依赖与划线那条一致 —— 正文 DOM 会在切换档位时重建,span 必须跟着重包。
|
|
6251
|
+
// 解析后的查找表:编号 → 原文、姓氏|年份 → 原文。只在 blocks 变化时重建一次,
|
|
6252
|
+
// 悬停时只做 Map 查询 —— 每次鼠标移动都重扫几千条参考文献是不可接受的。
|
|
6253
|
+
const citeLookup = React.useMemo(() => ({
|
|
6254
|
+
numbers: referenceTexts(reader?.blocks),
|
|
6255
|
+
authors: authorYearIndex(reader?.blocks),
|
|
6256
|
+
}), [reader])
|
|
6257
|
+
// 🔴 渲染行按 id 建索引**只在换篇时做一次**。早先这段在 effect 体内,每次依赖变化
|
|
6258
|
+
// 都要重建 400+ 项的 Map —— 翻页、缩放、切档位都跑一遍,纯属浪费。
|
|
6259
|
+
const citeRowsById = React.useMemo(() => {
|
|
6260
|
+
const map = new Map()
|
|
6261
|
+
for (const row of readerBlocks) {
|
|
6262
|
+
if (typeof row?.id === 'string') map.set(row.id, row)
|
|
6263
|
+
}
|
|
6264
|
+
return map
|
|
6265
|
+
}, [readerBlocks])
|
|
6266
|
+
// 最后一次涂过的「容器 + 内容签名」。DOM 没被重建、块内容也没变就不必重扫。
|
|
6267
|
+
const citePaintedRef = React.useRef({ layout: '', pdf: '' })
|
|
6268
|
+
React.useEffect(() => {
|
|
6269
|
+
if (view !== 'reader') {
|
|
6270
|
+
citePaintedRef.current = { layout: '', pdf: '' }
|
|
6271
|
+
return undefined
|
|
6272
|
+
}
|
|
6273
|
+
// 🔴 **两个容器都要涂**:正文栏挂在 layoutElRef,PDF 原页是另一个子树(pdfFrameRef)。
|
|
6274
|
+
// 只涂前者时原页那侧静默没有任何反应 —— 界面上看不出错,只是"悬停不管用"。
|
|
6275
|
+
// PDF 这侧的块文本以文字层 group.text 为准(与划线那条同一口径),否则段落正文与
|
|
6276
|
+
// 页面上真实的字对不上,找位置会失败。
|
|
6277
|
+
const pdfMode = isPdfPane(paneView) && activePdf !== null
|
|
6278
|
+
const blocks = pdfMode
|
|
6279
|
+
? (reader?.blocks ?? []).map(block => {
|
|
6280
|
+
const group = activePdf.groups.find(item => item.id === block.id)
|
|
6281
|
+
return group ? { ...block, sourceText: group.text } : block
|
|
6282
|
+
})
|
|
6283
|
+
: reader?.blocks
|
|
6284
|
+
const parts = citePartsOf(citeRowsById, blocks, readerNumbers(blocks ?? []))
|
|
6285
|
+
// 内容签名:**块 id + 该块标记原文**逐个拼起来。只看数量是不够的 —— 换一篇论文
|
|
6286
|
+
// 完全可能块数与标记数都不变而内容全变,那时按数量判会漏涂。
|
|
6287
|
+
// 拼接是 O(标记数),比重新扫几千个文本节点便宜两个数量级。
|
|
6288
|
+
let signature = String((blocks ?? []).length)
|
|
6289
|
+
for (const [blockId, tags] of parts) {
|
|
6290
|
+
signature += '|' + blockId
|
|
6291
|
+
for (const tag of tags) signature += ',' + (tag.raw ?? tag.value ?? '')
|
|
6292
|
+
}
|
|
6293
|
+
// 正文栏的 key 带上档位:切原文 / 译文 / 双语时渲染的是不同的行集合
|
|
6294
|
+
// (visibleRows 按 paneView 取),React 会重建这批段落节点。
|
|
6295
|
+
const layoutKey = `${paneView}:${layout}:${signature}`
|
|
6296
|
+
const painted = citePaintedRef.current
|
|
6297
|
+
// 🔴 签名只是**省钱的门槛,不是正确性的依据**:要不要重涂最终看 DOM 里标记还在不在。
|
|
6298
|
+
// 靠内容哈希判就得穷举「所有会让 React 重建这批节点的输入」—— 这一点漏过两次
|
|
6299
|
+
// (PDF 侧漏 pdfZoom、正文栏漏 paneView),每次都表现为"某个操作之后悬停整片失灵"。
|
|
6300
|
+
// 直接探测不必穷举:少列哪个输入都不会坏,代价只是一次 querySelectorAll。
|
|
6301
|
+
// 探测比的是**应有标记数**,不是"有没有标记":切档位后可能残留别的块的标记,
|
|
6302
|
+
// 只判存在会漏掉"这一块的标记没了"。
|
|
6303
|
+
const stillMarked = () => {
|
|
6304
|
+
const scope = layoutElRef.current
|
|
6305
|
+
if (scope === null || typeof scope?.querySelectorAll !== 'function') return false
|
|
6306
|
+
const found = scope.querySelectorAll('.paper-cite').length
|
|
6307
|
+
let expected = 0
|
|
6308
|
+
for (const tags of parts.values()) expected += tags.length
|
|
6309
|
+
return found >= expected
|
|
6310
|
+
}
|
|
6311
|
+
if (painted.layout !== layoutKey || !stillMarked()) {
|
|
6312
|
+
paintCiteSpans(layoutElRef.current ?? (typeof document !== 'undefined' ? document : null), parts)
|
|
6313
|
+
painted.layout = layoutKey
|
|
6314
|
+
}
|
|
6315
|
+
// 🔴 PDF 侧另有一份签名:**任何让页面树 useMemo 重算的输入**都会重建整棵子树并
|
|
6316
|
+
// 丢掉 data-cite-span,那时块内容可能一模一样,仍然必须重涂。
|
|
6317
|
+
// 页面树的依赖是 [activePdf, readerId, currentPage, pdfZoom, pdfImageAttempt, pdfCrop]
|
|
6318
|
+
// —— 少放一个就会出现「缩放之后悬停失灵」,而界面上完全不报错。
|
|
6319
|
+
const pdfKey = pdfMode
|
|
6320
|
+
? `${activePdf.page}:${pdfCrop ? 1 : 0}:${currentPage}:${pdfZoom}:${pdfImageAttempt}:${signature}`
|
|
6321
|
+
: ''
|
|
6322
|
+
const pdfStillMarked = () => {
|
|
6323
|
+
const scope = pdfFrameRef.current
|
|
6324
|
+
if (scope === null || typeof scope?.querySelectorAll !== 'function') return false
|
|
6325
|
+
return scope.querySelectorAll('[data-cite-span]').length > 0
|
|
6326
|
+
}
|
|
6327
|
+
if (pdfKey !== '' && (painted.pdf !== pdfKey || !pdfStillMarked())) {
|
|
6328
|
+
paintCiteSpans(pdfFrameRef.current, parts)
|
|
6329
|
+
painted.pdf = pdfKey
|
|
6330
|
+
} else if (pdfKey === '') {
|
|
6331
|
+
painted.pdf = ''
|
|
6332
|
+
}
|
|
6333
|
+
return undefined
|
|
6334
|
+
// readerBlocks 在依赖里:渲染行变了(换篇、重解析)标记位置也跟着变。
|
|
6335
|
+
// currentPage 在依赖里:PDF 原页翻页是**整棵子树重建**,不重涂新页就没有悬停。
|
|
6336
|
+
}, [view, reader, readerBlocks, paneView, activePdf, currentPage, pdfZoom, pdfCrop, pdfImageAttempt])
|
|
6337
|
+
React.useEffect(() => () => { if (citeTimer.current) clearTimeout(citeTimer.current) }, [])
|
|
6338
|
+
/**
|
|
6339
|
+
* 指针落进/离开一个引用标记。
|
|
6340
|
+
*
|
|
6341
|
+
* 悬停进入有约 140ms 延迟:正文里引用很密,鼠标划过时立刻弹窗会一路闪。
|
|
6342
|
+
* 离开延迟 100ms 且**可被取消** —— 指针从标记移到浮层上时不该关掉它。
|
|
6343
|
+
*/
|
|
6344
|
+
const citeUnder = event => {
|
|
6345
|
+
let node = event?.target ?? null
|
|
6346
|
+
// 🔴 判据用 `data-cite-span`:**两侧的标记元素都带它**(正文栏的 .paper-cite 与
|
|
6347
|
+
// PDF 的字形元素),而组号悬停时本来就要读,一次 getAttribute 拿到两样东西。
|
|
6348
|
+
// 这个函数在 PDF 上每次 mousemove 都跑,且要爬 3~4 层(字符 → 词 → 行 → 块)。
|
|
6349
|
+
while (node && typeof node.getAttribute === 'function') {
|
|
6350
|
+
if (node.getAttribute('data-cite-span') !== null) return node
|
|
6351
|
+
node = node.parentNode
|
|
6352
|
+
}
|
|
6353
|
+
return null
|
|
6354
|
+
}
|
|
6355
|
+
/**
|
|
6356
|
+
* 同一引用标记的整组一起高亮。
|
|
6357
|
+
*
|
|
6358
|
+
* 🔴 PDF 文字层逐字符渲染,`[41, 44, 13, 16]` 会拆成十几个独立元素。CSS 的 `:hover`
|
|
6359
|
+
* 只命中鼠标正下方那一个 —— 实测"只有指着的那一个字变色"。所以高亮状态由这里按组号写,
|
|
6360
|
+
* 而不是交给 CSS 伪类。组号是 paintCiteSpans 打在**同一处标记**全部元素上的
|
|
6361
|
+
* `data-cite-span`;清掉旧的再加新的,保证同一时刻只有一组亮着。
|
|
6362
|
+
*/
|
|
6363
|
+
const citeGroup = node => {
|
|
6364
|
+
const id = node.getAttribute('data-cite-span')
|
|
6365
|
+
const root = node.ownerDocument ?? (typeof document === 'undefined' ? null : document)
|
|
6366
|
+
if (id === null || id === '' || root === null) return []
|
|
6367
|
+
const scope = pdfFrameRef.current ?? root
|
|
6368
|
+
if (typeof scope.querySelectorAll !== 'function') return []
|
|
6369
|
+
return [...scope.querySelectorAll('[data-cite-span]')].filter(item => item.getAttribute('data-cite-span') === id)
|
|
6370
|
+
}
|
|
6371
|
+
const clearCiteGroup = () => {
|
|
6372
|
+
for (const node of citeGroupOfMarked.current) node.classList?.remove('is-cite-active')
|
|
6373
|
+
citeGroupOfMarked.current = []
|
|
6374
|
+
}
|
|
6375
|
+
const showCite = node => {
|
|
6376
|
+
clearCiteGroup()
|
|
6377
|
+
for (const item of citeGroup(node)) {
|
|
6378
|
+
item.classList?.add('is-cite-active')
|
|
6379
|
+
citeGroupOfMarked.current.push(item)
|
|
6380
|
+
}
|
|
6381
|
+
const key = node.getAttribute('data-cite-key') ?? ''
|
|
6382
|
+
const type = node.getAttribute('data-cite') ?? 'ref'
|
|
6383
|
+
let entries = []
|
|
6384
|
+
let ambiguous = 0
|
|
6385
|
+
if (type === 'ref') {
|
|
6386
|
+
// 多标号:data-cite-items 是逗号分隔的全部编号,逐条查。
|
|
6387
|
+
const items = String(node.getAttribute('data-cite-items') ?? (key === '' ? '' : key)).split(',')
|
|
6388
|
+
for (const number of items) {
|
|
6389
|
+
const hit = citeLookup.numbers.get(number)
|
|
6390
|
+
if (hit !== undefined) entries.push(hit)
|
|
6391
|
+
}
|
|
6392
|
+
} else {
|
|
6393
|
+
entries = ambiguousLookup(
|
|
6394
|
+
citeLookup.authors,
|
|
6395
|
+
node.getAttribute('data-cite-surname') ?? '',
|
|
6396
|
+
node.getAttribute('data-cite-year') ?? '',
|
|
6397
|
+
)
|
|
6398
|
+
}
|
|
6399
|
+
ambiguous = Number(node.getAttribute('data-cite-ambiguous') ?? 0)
|
|
6400
|
+
if (entries.length === 0) return
|
|
6401
|
+
const box = typeof node.getBoundingClientRect === 'function' ? node.getBoundingClientRect() : null
|
|
6402
|
+
setCiteHover({
|
|
6403
|
+
key: `${type}:${key}`,
|
|
6404
|
+
type,
|
|
6405
|
+
entries,
|
|
6406
|
+
ambiguous: Number.isFinite(ambiguous) ? ambiguous : 0,
|
|
6407
|
+
anchor: box === null ? null : { left: box.left, top: box.top, bottom: box.bottom },
|
|
6408
|
+
})
|
|
6409
|
+
}
|
|
6410
|
+
const onCiteOver = event => {
|
|
6411
|
+
const node = citeUnder(event)
|
|
6412
|
+
if (node === null) return
|
|
6413
|
+
if (citeTimer.current) { clearTimeout(citeTimer.current); citeTimer.current = null }
|
|
6414
|
+
citeTimer.current = setTimeout(() => { citeTimer.current = null; showCite(node) }, 140)
|
|
6415
|
+
}
|
|
6416
|
+
const onCiteOut = event => {
|
|
6417
|
+
const node = citeUnder(event)
|
|
6418
|
+
const next = event?.relatedTarget ?? null
|
|
6419
|
+
// 移到浮层里(或浮层内部的另一个节点)时保持显示。
|
|
6420
|
+
let inside = next
|
|
6421
|
+
while (inside && typeof inside.getAttribute === 'function') {
|
|
6422
|
+
if (inside.getAttribute('data-cite-pop') !== null) return
|
|
6423
|
+
inside = inside.parentNode
|
|
6424
|
+
}
|
|
6425
|
+
// 同一组内换字(PDF 逐字符,指针从 4 移到 1 就是跨了元素):不算离开。
|
|
6426
|
+
if (node !== null && next !== null) {
|
|
6427
|
+
const id = node.getAttribute('data-cite-span')
|
|
6428
|
+
let probe = next
|
|
6429
|
+
while (probe && typeof probe.getAttribute === 'function') {
|
|
6430
|
+
if (probe.getAttribute('data-cite-span') === id) return
|
|
6431
|
+
probe = probe.parentNode
|
|
6432
|
+
}
|
|
6433
|
+
}
|
|
6434
|
+
if (citeTimer.current) { clearTimeout(citeTimer.current); citeTimer.current = null }
|
|
6435
|
+
clearCiteGroup()
|
|
6436
|
+
setCiteHover(null)
|
|
6437
|
+
}
|
|
6438
|
+
// 每轮渲染后把最新的处理函数交给 PDF 页面树(它是渲染期构建的,拿不到这两个 const)。
|
|
6439
|
+
citeHandlersRef.current = { over: onCiteOver, out: onCiteOut }
|
|
5662
6440
|
const editClickedMark = event => {
|
|
5663
6441
|
const selection = window.getSelection?.()
|
|
5664
6442
|
if (selection && !selection.isCollapsed && String(selection).trim()) return
|
|
@@ -6099,6 +6877,10 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
6099
6877
|
ref: layoutRef,
|
|
6100
6878
|
className: 'paper-reader-layout',
|
|
6101
6879
|
onClick: editClickedMark,
|
|
6880
|
+
// 引用悬停(T25b):事件委托挂在整个正文容器上,标记 span 是事后由
|
|
6881
|
+
// paintCiteSpans 包出来的 —— 逐个挂监听会在每次重涂后全部失效。
|
|
6882
|
+
onMouseOver: onCiteOver,
|
|
6883
|
+
onMouseOut: onCiteOut,
|
|
6102
6884
|
// 阅读位置靠这条(D163):正文自己的滚动容器才知道"视口上沿那一块"是谁。
|
|
6103
6885
|
onScroll: isPdfPane(paneView) ? undefined : onReaderScroll,
|
|
6104
6886
|
onMouseUp: isPdfPane(paneView) ? event => {
|
|
@@ -6157,6 +6939,23 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
6157
6939
|
}, '让 AI 点评')))
|
|
6158
6940
|
return [node, ...inlineNotes]
|
|
6159
6941
|
}))]),
|
|
6942
|
+
// ---- 引用悬停浮层(T25b)----
|
|
6943
|
+
// 定位与 .paper-mark-draft 同一套(fixed + 状态里的坐标),但内容只读:
|
|
6944
|
+
// 一段一条参考文献原文,不提供任何会改动数据的按钮。
|
|
6945
|
+
citeHover !== null && h('div', {
|
|
6946
|
+
className: 'paper-cite-pop',
|
|
6947
|
+
'data-cite-pop': citeHover.key,
|
|
6948
|
+
role: 'tooltip',
|
|
6949
|
+
'aria-label': '参考文献',
|
|
6950
|
+
style: citePopStyle(citeHover),
|
|
6951
|
+
onMouseEnter: () => { if (citeTimer.current) { clearTimeout(citeTimer.current); citeTimer.current = null } },
|
|
6952
|
+
onMouseLeave: () => setCiteHover(null),
|
|
6953
|
+
},
|
|
6954
|
+
citeHover.entries.map((entry, at) => h('div', {
|
|
6955
|
+
className: 'paper-cite-entry',
|
|
6956
|
+
key: `${entry.blockId}-${at}`,
|
|
6957
|
+
}, entry.text)),
|
|
6958
|
+
citeHover.ambiguous > 1 && h('div', { className: 'paper-cite-note' }, `共 ${citeHover.ambiguous} 条可能匹配`)),
|
|
6160
6959
|
pendingMark !== null && h('div', {
|
|
6161
6960
|
className: 'paper-mark-draft',
|
|
6162
6961
|
ref: markPopoverRef,
|
|
@@ -9484,6 +10283,8 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
9484
10283
|
exports.glossRow = glossRow
|
|
9485
10284
|
exports.glossSegmentOf = glossSegmentOf
|
|
9486
10285
|
exports.formulaSelectionText = formulaSelectionText
|
|
10286
|
+
exports.pdfGlyphAdvances = pdfGlyphAdvances
|
|
10287
|
+
exports.pdfWordMetrics = pdfWordMetrics
|
|
9487
10288
|
exports.selectionTextInBlock = selectionTextInBlock
|
|
9488
10289
|
// 跨块选区(D46):按块收集、分段视图、多段原话。纯函数,测试直接喂假 DOM 与草稿。
|
|
9489
10290
|
exports.selectionSegmentInNode = selectionSegmentInNode
|
|
@@ -9553,6 +10354,17 @@ h4.paper-reader-block{font-size:13px;font-weight:650}
|
|
|
9553
10354
|
// 引用索引与「这一段引了谁」(P6):纯函数,测试直接断言。
|
|
9554
10355
|
exports.referenceIndex = referenceIndex
|
|
9555
10356
|
exports.citedNumbersOf = citedNumbersOf
|
|
10357
|
+
// 悬停预览(T25b):编号 → 条目原文,以及作者-年份制那条独立链路。
|
|
10358
|
+
exports.referenceTexts = referenceTexts
|
|
10359
|
+
exports.refItemsOf = refItemsOf
|
|
10360
|
+
// 引用装饰层的两个纯函数:跨节点定位的判据都在这里,测试直接断言。
|
|
10361
|
+
exports.citeTextRuns = citeTextRuns
|
|
10362
|
+
exports.citeSlice = citeSlice
|
|
10363
|
+
exports.citeLocate = citeLocate
|
|
10364
|
+
exports.citePartsOf = citePartsOf
|
|
10365
|
+
exports.paintCiteSpans = paintCiteSpans
|
|
10366
|
+
exports.authorYearIndex = authorYearIndex
|
|
10367
|
+
exports.inlineAuthors = inlineAuthors
|
|
9556
10368
|
// 正文渲染的档位与本地化文案:视图区按引用传给 MarkdownText,不能在渲染里新建对象。
|
|
9557
10369
|
exports.MARKDOWN_VARIANT = MARKDOWN_VARIANT
|
|
9558
10370
|
exports.MARKDOWN_LABELS = MARKDOWN_LABELS
|