@open-document/core 0.15.1 → 0.17.0
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/dist/{build-CBMroMqd.js → build-BzNZnIhF.js} +1 -1
- package/dist/{check-C2kIZr3f.js → check-C7kxlAFW.js} +11 -4
- package/dist/cli/bin.js +24 -11
- package/dist/{context-DGp1jwN5.js → context-CFPnmNCR.js} +1 -1
- package/dist/{dev-DmzGncKf.js → dev-BUo7OESU.js} +1 -1
- package/dist/{diff-CMI_PVzz.js → diff-B950T0O6.js} +2 -2
- package/dist/{export-BQMUBoBB.js → export-CHN1CiGv.js} +3 -3
- package/dist/{flow-CS5apebG.d.ts → flow-DlV3t5ML.d.ts} +24 -4
- package/dist/{import-CiSRqZyo.js → import-CLSnIxvN.js} +11 -7
- package/dist/index.d.ts +26 -2
- package/dist/index.js +55 -1
- package/dist/ops/index.d.ts +200 -4
- package/dist/ops/index.js +3 -3
- package/dist/{ops-DQbZmYRF.js → ops-BgbM5Irm.js} +364 -9
- package/dist/{preview-CERhZ_FX.js → preview-BVwVqS-I.js} +1 -1
- package/dist/proofread-BpR_3GZz.js +35 -0
- package/dist/{session-DdaKKd7_.js → session-DmbXf3u7.js} +5179 -106
- package/dist/{templates-CBeC-v7e.js → templates-BsWxs7oo.js} +3 -3
- package/dist/translations-CW5OVM0L.js +42 -0
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/skills/doc-authoring/references/pagination.md +1 -1
- package/skills/fill-form/SKILL.md +42 -0
- package/skills/fit-pages/SKILL.md +48 -0
- package/skills/import-document/SKILL.md +58 -0
- package/skills/proofread/SKILL.md +44 -0
- package/skills/refresh-report/SKILL.md +48 -0
- package/skills/sync-translation/SKILL.md +55 -0
- package/src/agent/prompt.ts +1 -1
- package/src/agent/types.ts +9 -2
- package/src/app/components/agent/agent-panel.tsx +3 -2
- package/src/app/components/checkbox.tsx +68 -0
- package/src/app/components/doc-sidebar.tsx +5 -3
- package/src/app/components/flow-page.tsx +7 -2
- package/src/app/components/host-bridge.tsx +49 -0
- package/src/app/components/panel/edit-save-card.tsx +23 -1
- package/src/app/lib/agent-bridge.ts +37 -4
- package/src/app/lib/diagnostics.ts +46 -5
- package/src/app/lib/export-docx.ts +5 -2
- package/src/app/lib/export-dom.ts +13 -0
- package/src/app/lib/export-html.ts +6 -1
- package/src/app/lib/export-image.ts +9 -4
- package/src/app/lib/export-pdf.ts +17 -4
- package/src/app/lib/flow-measure.ts +9 -5
- package/src/app/lib/flow.ts +3 -2
- package/src/app/lib/host.ts +71 -0
- package/src/app/lib/i18n-ja/browser.ts +1 -0
- package/src/app/lib/i18n-ko/browser.ts +1 -0
- package/src/app/lib/i18n-zh-cn/browser.ts +1 -0
- package/src/app/lib/i18n-zh-tw/browser.ts +1 -0
- package/src/app/lib/page-budget.ts +81 -0
- package/src/app/lib/proofread-script.ts +61 -0
- package/src/app/lib/proofread.ts +399 -0
- package/src/app/lib/recent-docs.ts +27 -0
- package/src/app/lib/sdk.ts +44 -2
- package/src/app/lib/text-runs.ts +83 -0
- package/src/app/lib/use-doc-pages.ts +10 -3
- package/src/app/main.tsx +2 -0
- package/src/app/routes/doc-preview.tsx +1 -1
- package/src/app/routes/doc.tsx +26 -2
- package/src/app/routes/home.tsx +39 -5
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simplified → Traditional pairs where each side is used by one script only.
|
|
3
|
+
* A character either script writes (后, 里, 干, 只, 台, 面, 系, 着, 于, 价…)
|
|
4
|
+
* never goes in here: flagging it would be wrong in one of them. Variant
|
|
5
|
+
* shapes that Hong Kong or Taiwan also print (决/決, 别/別, 没/沒) are left
|
|
6
|
+
* out for the same reason.
|
|
7
|
+
*/
|
|
8
|
+
const PAIRS = `
|
|
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
|
+
function build(): { toTraditional: Map<string, string>; toSimplified: Map<string, string> } {
|
|
50
|
+
const toTraditional = new Map<string, string>();
|
|
51
|
+
const toSimplified = new Map<string, string>();
|
|
52
|
+
for (const pair of PAIRS.split(/\s+/)) {
|
|
53
|
+
const [simplified, traditional] = Array.from(pair);
|
|
54
|
+
if (!simplified || !traditional) continue;
|
|
55
|
+
toTraditional.set(simplified, traditional);
|
|
56
|
+
toSimplified.set(traditional, simplified);
|
|
57
|
+
}
|
|
58
|
+
return { toTraditional, toSimplified };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const { toTraditional: SIMPLIFIED_ONLY, toSimplified: TRADITIONAL_ONLY } = build();
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { SIMPLIFIED_ONLY, TRADITIONAL_ONLY } from './proofread-script';
|
|
2
|
+
|
|
3
|
+
export type ProofreadRule =
|
|
4
|
+
| 'fullwidth-alnum'
|
|
5
|
+
| 'punctuation-width'
|
|
6
|
+
| 'script-mix'
|
|
7
|
+
| 'repeated-word'
|
|
8
|
+
| 'term-casing';
|
|
9
|
+
|
|
10
|
+
export type ProofreadFinding = {
|
|
11
|
+
/** 1-based sheet the text is on. */
|
|
12
|
+
page: number;
|
|
13
|
+
rule: ProofreadRule;
|
|
14
|
+
severity: 'warn';
|
|
15
|
+
message: string;
|
|
16
|
+
/** The words around the problem, as printed. */
|
|
17
|
+
snippet: string;
|
|
18
|
+
/** `line:column` in the document source, from the inspector's `data-od-loc`. */
|
|
19
|
+
loc?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* One stretch of printed text inside a single block, in reading order. Code
|
|
24
|
+
* and block boundaries end a run, so two runs never read as adjacent text.
|
|
25
|
+
*/
|
|
26
|
+
export type TextRun = {
|
|
27
|
+
text: string;
|
|
28
|
+
page: number;
|
|
29
|
+
loc?: string;
|
|
30
|
+
/** A `lang` set inside the sheet; the document's language otherwise. */
|
|
31
|
+
lang?: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type ProofreadOptions = {
|
|
35
|
+
/** Findings kept per rule per page, so one bad page can't drown the report. */
|
|
36
|
+
perPageLimit?: number;
|
|
37
|
+
/** Findings kept per rule across the document. */
|
|
38
|
+
perRuleLimit?: number;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type ProofreadResult = { findings: ProofreadFinding[]; omitted: number };
|
|
42
|
+
|
|
43
|
+
const DEFAULTS = { perPageLimit: 3, perRuleLimit: 20 } as const;
|
|
44
|
+
const CONTEXT = 16;
|
|
45
|
+
const MASK = '';
|
|
46
|
+
|
|
47
|
+
const CJK_RE =
|
|
48
|
+
/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}々〆〇ー -〿-/:-@[-`{-・]/u;
|
|
49
|
+
const LATIN_RE = /[\p{Script=Latin}0-9]/u;
|
|
50
|
+
const DIGIT_RE = /[0-9]/;
|
|
51
|
+
|
|
52
|
+
const isCjk = (ch: string | undefined) => ch !== undefined && CJK_RE.test(ch);
|
|
53
|
+
const isLatin = (ch: string | undefined) => ch !== undefined && LATIN_RE.test(ch);
|
|
54
|
+
const isDigit = (ch: string | undefined) => ch !== undefined && DIGIT_RE.test(ch);
|
|
55
|
+
|
|
56
|
+
function isCjkLang(lang: string): boolean {
|
|
57
|
+
return /^(zh|ja)(-|$)/i.test(lang);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function chineseScript(lang: string): 'hant' | 'hans' | null {
|
|
61
|
+
if (/^zh-(hant|tw|hk|mo)(-|$)/i.test(lang)) return 'hant';
|
|
62
|
+
if (/^zh-(hans|cn|sg|my)(-|$)/i.test(lang)) return 'hans';
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const MASKS = [
|
|
67
|
+
/(?:[a-z][a-z\d+.-]*:\/\/|www\.)[^\s\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana} -〿-"'<>]+/giu,
|
|
68
|
+
/[\w.+-]+@[\w-]+(?:\.[\w-]+)+/g,
|
|
69
|
+
/(?<![\p{Script=Latin}\p{N}])[:;=][-^']?[()DPpO3|\\/\][*]+(?![\p{Script=Latin}\p{N}])/gu,
|
|
70
|
+
// Kaomoji: a bracketed face is drawn with these, never written as prose.
|
|
71
|
+
/[((][^()()\s\p{Script=Han}\p{Script=Latin}]{0,10}[´`^∀ω・_;*°゚▽◕‿≧≦⊙•ー^﹏∇ε><Д][^()()\s\p{Script=Han}\p{Script=Latin}]{0,10}[))]/gu,
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
/** Same length as the text, with URLs, addresses and emoticons blanked out. */
|
|
75
|
+
function masked(text: string): string {
|
|
76
|
+
let out = text;
|
|
77
|
+
for (const re of MASKS) {
|
|
78
|
+
out = out.replace(re, (m) => MASK.repeat(m.length));
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function snippetAt(text: string, start: number, end: number): string {
|
|
84
|
+
const from = Math.max(0, start - CONTEXT);
|
|
85
|
+
const to = Math.min(text.length, end + CONTEXT);
|
|
86
|
+
const body = text.slice(from, to).replace(/\s+/g, ' ').trim();
|
|
87
|
+
return `${from > 0 ? '…' : ''}${body}${to < text.length ? '…' : ''}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function times(n: number): string {
|
|
91
|
+
return n > 1 ? ` (${n} in this block)` : '';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type Hit = { start: number; end: number };
|
|
95
|
+
|
|
96
|
+
function finding(run: TextRun, rule: ProofreadRule, message: string, hit: Hit): ProofreadFinding {
|
|
97
|
+
return {
|
|
98
|
+
page: run.page,
|
|
99
|
+
rule,
|
|
100
|
+
severity: 'warn',
|
|
101
|
+
message,
|
|
102
|
+
snippet: snippetAt(run.text, hit.start, hit.end),
|
|
103
|
+
...(run.loc ? { loc: run.loc } : {}),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const FULLWIDTH_ALNUM_RE = /[A-Za-z0-9]+/g;
|
|
108
|
+
|
|
109
|
+
function toHalfWidth(s: string): string {
|
|
110
|
+
return s.replace(/[A-Za-z0-9]/g, (c) => String.fromCharCode(c.charCodeAt(0) - 0xfee0));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function fullwidthAlnum(run: TextRun, text: string): ProofreadFinding[] {
|
|
114
|
+
const hits = Array.from(text.matchAll(FULLWIDTH_ALNUM_RE));
|
|
115
|
+
const first = hits[0];
|
|
116
|
+
if (!first) return [];
|
|
117
|
+
const word = first[0];
|
|
118
|
+
return [
|
|
119
|
+
finding(
|
|
120
|
+
run,
|
|
121
|
+
'fullwidth-alnum',
|
|
122
|
+
`Full-width "${word}" — write "${toHalfWidth(word)}"${times(hits.length)}.`,
|
|
123
|
+
{ start: first.index, end: first.index + word.length },
|
|
124
|
+
),
|
|
125
|
+
];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const TO_FULL: Record<string, string> = {
|
|
129
|
+
',': ',',
|
|
130
|
+
';': ';',
|
|
131
|
+
':': ':',
|
|
132
|
+
'?': '?',
|
|
133
|
+
'!': '!',
|
|
134
|
+
'(': '(',
|
|
135
|
+
')': ')',
|
|
136
|
+
};
|
|
137
|
+
const TO_HALF: Record<string, string> = Object.fromEntries(
|
|
138
|
+
Object.entries(TO_FULL).map(([half, full]) => [full, half]),
|
|
139
|
+
);
|
|
140
|
+
TO_HALF['。'] = '.';
|
|
141
|
+
|
|
142
|
+
function halfWidthInCjk(text: string): Hit[] {
|
|
143
|
+
const hits: Hit[] = [];
|
|
144
|
+
for (let i = 0; i < text.length; i++) {
|
|
145
|
+
const ch = text[i] as string;
|
|
146
|
+
if (!(ch in TO_FULL)) continue;
|
|
147
|
+
const prev = text[i - 1];
|
|
148
|
+
const next = text[i + 1];
|
|
149
|
+
let wrong: boolean;
|
|
150
|
+
// A bracket belongs to what it holds: `中文 (English)` is fine, `(中文)` is not.
|
|
151
|
+
if (ch === '(') wrong = isCjk(next);
|
|
152
|
+
else if (ch === ')') wrong = isCjk(prev);
|
|
153
|
+
else if ((ch === ',' || ch === ':') && isDigit(prev) && isDigit(next)) wrong = false;
|
|
154
|
+
else wrong = isCjk(prev) || isCjk(next);
|
|
155
|
+
if (wrong) hits.push({ start: i, end: i + 1 });
|
|
156
|
+
}
|
|
157
|
+
return hits;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function neighbour(text: string, from: number, step: 1 | -1): string | undefined {
|
|
161
|
+
let i = from + step;
|
|
162
|
+
while (text[i] === ' ') i += step;
|
|
163
|
+
return text[i];
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function fullWidthInLatin(text: string): Hit[] {
|
|
167
|
+
const hits: Hit[] = [];
|
|
168
|
+
for (let i = 0; i < text.length; i++) {
|
|
169
|
+
const ch = text[i] as string;
|
|
170
|
+
// The opening bracket speaks for the pair; `中文(Chinese)means` closes
|
|
171
|
+
// a bracket opened after CJK text.
|
|
172
|
+
if (!(ch in TO_HALF) || ch === ')') continue;
|
|
173
|
+
if (isLatin(neighbour(text, i, -1)) && isLatin(neighbour(text, i, 1))) {
|
|
174
|
+
hits.push({ start: i, end: i + 1 });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return hits;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function punctuationWidth(run: TextRun, text: string, lang: string): ProofreadFinding[] {
|
|
181
|
+
const cjk = isCjkLang(lang);
|
|
182
|
+
const hits = cjk ? halfWidthInCjk(text) : fullWidthInLatin(text);
|
|
183
|
+
const first = hits[0];
|
|
184
|
+
if (!first) return [];
|
|
185
|
+
const ch = text[first.start] as string;
|
|
186
|
+
const message = cjk
|
|
187
|
+
? `Half-width "${ch}" next to CJK text — use "${TO_FULL[ch]}"${times(hits.length)}.`
|
|
188
|
+
: `Full-width "${ch}" between Latin text — use "${TO_HALF[ch]}"${times(hits.length)}.`;
|
|
189
|
+
return [finding(run, 'punctuation-width', message, first)];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const SIMPLIFIED_NAMES = /简体中文|中文简体|简体|简中/g;
|
|
193
|
+
const TRADITIONAL_NAMES = /日本語|繁體中文|中文繁體|正體中文|繁體|正體|繁中/g;
|
|
194
|
+
|
|
195
|
+
function scriptMix(run: TextRun, source: string, lang: string): ProofreadFinding[] {
|
|
196
|
+
const script = chineseScript(lang);
|
|
197
|
+
if (!script) return [];
|
|
198
|
+
const table = script === 'hant' ? SIMPLIFIED_ONLY : TRADITIONAL_ONLY;
|
|
199
|
+
// A language picker writes each script's name in that script.
|
|
200
|
+
const own = script === 'hant' ? SIMPLIFIED_NAMES : TRADITIONAL_NAMES;
|
|
201
|
+
const text = source.replace(own, (m) => MASK.repeat(m.length));
|
|
202
|
+
const found = new Map<string, string>();
|
|
203
|
+
let start = -1;
|
|
204
|
+
for (let i = 0; i < text.length; i++) {
|
|
205
|
+
const ch = text[i] as string;
|
|
206
|
+
const other = table.get(ch);
|
|
207
|
+
if (!other) continue;
|
|
208
|
+
if (start < 0) start = i;
|
|
209
|
+
found.set(ch, other);
|
|
210
|
+
}
|
|
211
|
+
if (start < 0) return [];
|
|
212
|
+
const pairs = Array.from(found, ([from, to]) => `${from}→${to}`).join(', ');
|
|
213
|
+
const message =
|
|
214
|
+
script === 'hant'
|
|
215
|
+
? `Simplified characters in a Traditional Chinese document: ${pairs}.`
|
|
216
|
+
: `Traditional characters in a Simplified Chinese document: ${pairs}.`;
|
|
217
|
+
return [finding(run, 'script-mix', message, { start, end: start + 1 })];
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Doubled on purpose often enough that flagging them would be noise. */
|
|
221
|
+
const LEGIT_DOUBLES = new Set([
|
|
222
|
+
'that',
|
|
223
|
+
'had',
|
|
224
|
+
'is',
|
|
225
|
+
'was',
|
|
226
|
+
'bye',
|
|
227
|
+
'ha',
|
|
228
|
+
'no',
|
|
229
|
+
'so',
|
|
230
|
+
'very',
|
|
231
|
+
'la',
|
|
232
|
+
'knock',
|
|
233
|
+
'can',
|
|
234
|
+
'do',
|
|
235
|
+
'yes',
|
|
236
|
+
'oh',
|
|
237
|
+
'ho',
|
|
238
|
+
'hey',
|
|
239
|
+
'now',
|
|
240
|
+
'well',
|
|
241
|
+
'go',
|
|
242
|
+
'tut',
|
|
243
|
+
'wait',
|
|
244
|
+
]);
|
|
245
|
+
|
|
246
|
+
const LATIN_DOUBLE_RE = /(?<![\p{L}\p{N}'’-])(\p{Script=Latin}+)\s+(\1)(?![\p{L}\p{N}'’-])/giu;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Doubled function characters that are typing slips, each with the contexts
|
|
250
|
+
* where the pair is a real word: 的的確確, 是是非非, 在在皆是, 小時了了.
|
|
251
|
+
*/
|
|
252
|
+
const CJK_DOUBLES: Record<string, { before?: string; after?: string }> = {
|
|
253
|
+
的: { after: '確确' },
|
|
254
|
+
是: { after: '非' },
|
|
255
|
+
在: { after: '皆都顯显說说表證证' },
|
|
256
|
+
了: { before: '時时小', after: '分無无可一心這这此卻却結结' },
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
function repeatedWord(run: TextRun, text: string): ProofreadFinding[] {
|
|
260
|
+
const out: ProofreadFinding[] = [];
|
|
261
|
+
for (const m of text.matchAll(LATIN_DOUBLE_RE)) {
|
|
262
|
+
const [whole, word = '', second = ''] = m;
|
|
263
|
+
if (LEGIT_DOUBLES.has(word.toLowerCase())) continue;
|
|
264
|
+
// `Bora Bora`, `Walla Walla`: a capital on the second copy is a name.
|
|
265
|
+
if (second[0] !== second[0]?.toLowerCase()) continue;
|
|
266
|
+
out.push(
|
|
267
|
+
finding(run, 'repeated-word', `"${word} ${second}" — the word is doubled.`, {
|
|
268
|
+
start: m.index,
|
|
269
|
+
end: m.index + whole.length,
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
for (let i = 0; i + 1 < text.length; i++) {
|
|
274
|
+
const ch = text[i] as string;
|
|
275
|
+
const rule = CJK_DOUBLES[ch];
|
|
276
|
+
if (!rule || text[i + 1] !== ch) continue;
|
|
277
|
+
const prev = text[i - 1] ?? '';
|
|
278
|
+
const next = text[i + 2] ?? '';
|
|
279
|
+
if (prev === ch || next === ch) continue;
|
|
280
|
+
if (rule.before && prev && rule.before.includes(prev)) continue;
|
|
281
|
+
if (rule.after && next && rule.after.includes(next)) continue;
|
|
282
|
+
out.push(
|
|
283
|
+
finding(run, 'repeated-word', `"${ch}${ch}" — the character is doubled.`, {
|
|
284
|
+
start: i,
|
|
285
|
+
end: i + 2,
|
|
286
|
+
}),
|
|
287
|
+
);
|
|
288
|
+
i++;
|
|
289
|
+
}
|
|
290
|
+
return out;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const WORD_RE =
|
|
294
|
+
/(?<![\p{Script=Latin}\p{N}])\p{Script=Latin}[\p{Script=Latin}\p{N}]+(?![\p{Script=Latin}\p{N}])/gu;
|
|
295
|
+
|
|
296
|
+
type Occurrence = { run: TextRun; start: number; end: number; form: string };
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Compares how one term is capitalised across the whole document. A capital
|
|
300
|
+
* only on the first letter is a sentence start and ALL CAPS is a heading
|
|
301
|
+
* style, so neither counts as a different spelling.
|
|
302
|
+
*/
|
|
303
|
+
function termCasing(runs: Array<{ run: TextRun; text: string }>): ProofreadFinding[] {
|
|
304
|
+
const terms = new Map<string, Map<string, Occurrence[]>>();
|
|
305
|
+
for (const { run, text } of runs) {
|
|
306
|
+
for (const m of text.matchAll(WORD_RE)) {
|
|
307
|
+
const form = m[0];
|
|
308
|
+
if (form === form.toUpperCase()) continue;
|
|
309
|
+
const variant = (form[0] as string).toLowerCase() + form.slice(1);
|
|
310
|
+
const key = form.toLowerCase();
|
|
311
|
+
let variants = terms.get(key);
|
|
312
|
+
if (!variants) {
|
|
313
|
+
variants = new Map();
|
|
314
|
+
terms.set(key, variants);
|
|
315
|
+
}
|
|
316
|
+
const list = variants.get(variant) ?? [];
|
|
317
|
+
list.push({ run, start: m.index, end: m.index + form.length, form });
|
|
318
|
+
variants.set(variant, list);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const out: ProofreadFinding[] = [];
|
|
323
|
+
for (const variants of terms.values()) {
|
|
324
|
+
if (variants.size < 2) continue;
|
|
325
|
+
// Most used first; on a tie the spelling met first is the one kept.
|
|
326
|
+
const ranked = Array.from(variants.values()).sort((a, b) => b.length - a.length);
|
|
327
|
+
const minority = ranked[ranked.length - 1] as Occurrence[];
|
|
328
|
+
const first = minority[0] as Occurrence;
|
|
329
|
+
const listed = ranked.map((list) => `"${commonForm(list)}" ×${list.length}`).join(', ');
|
|
330
|
+
out.push(
|
|
331
|
+
finding(
|
|
332
|
+
first.run,
|
|
333
|
+
'term-casing',
|
|
334
|
+
`One term, several spellings: ${listed} — pick one.`,
|
|
335
|
+
first,
|
|
336
|
+
),
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
return out;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function commonForm(list: Occurrence[]): string {
|
|
343
|
+
const counts = new Map<string, number>();
|
|
344
|
+
for (const { form } of list) counts.set(form, (counts.get(form) ?? 0) + 1);
|
|
345
|
+
return Array.from(counts).sort((a, b) => b[1] - a[1])[0]?.[0] ?? '';
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Reads the printed text of a document and reports consistency slips a
|
|
350
|
+
* proofreader would mark: full-width letters, punctuation of the wrong width
|
|
351
|
+
* for the language, Simplified characters in a Traditional document (or the
|
|
352
|
+
* reverse), a doubled word, a term capitalised two ways.
|
|
353
|
+
*
|
|
354
|
+
* Advice only: every finding is a warning.
|
|
355
|
+
*/
|
|
356
|
+
export function proofreadRuns(
|
|
357
|
+
runs: TextRun[],
|
|
358
|
+
docLang: string,
|
|
359
|
+
opts: ProofreadOptions = {},
|
|
360
|
+
): ProofreadResult {
|
|
361
|
+
const perPageLimit = opts.perPageLimit ?? DEFAULTS.perPageLimit;
|
|
362
|
+
const perRuleLimit = opts.perRuleLimit ?? DEFAULTS.perRuleLimit;
|
|
363
|
+
|
|
364
|
+
const prepared = runs.map((run) => ({ run, text: masked(run.text) }));
|
|
365
|
+
const raw: ProofreadFinding[] = [];
|
|
366
|
+
for (const { run, text } of prepared) {
|
|
367
|
+
const lang = run.lang || docLang;
|
|
368
|
+
raw.push(
|
|
369
|
+
...fullwidthAlnum(run, text),
|
|
370
|
+
...punctuationWidth(run, text, lang),
|
|
371
|
+
...scriptMix(run, text, lang),
|
|
372
|
+
...repeatedWord(run, text),
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
raw.push(...termCasing(prepared));
|
|
376
|
+
|
|
377
|
+
const seen = new Set<string>();
|
|
378
|
+
const perPage = new Map<string, number>();
|
|
379
|
+
const perRule = new Map<string, number>();
|
|
380
|
+
const findings: ProofreadFinding[] = [];
|
|
381
|
+
let omitted = 0;
|
|
382
|
+
for (const f of raw.sort((a, b) => a.page - b.page)) {
|
|
383
|
+
// A running header prints the same words from one source line on every sheet.
|
|
384
|
+
const key = `${f.rule}\0${f.loc ?? ''}\0${f.message}\0${f.snippet}`;
|
|
385
|
+
if (seen.has(key)) continue;
|
|
386
|
+
seen.add(key);
|
|
387
|
+
const pageKey = `${f.rule}\0${f.page}`;
|
|
388
|
+
const onPage = perPage.get(pageKey) ?? 0;
|
|
389
|
+
const inRule = perRule.get(f.rule) ?? 0;
|
|
390
|
+
if (onPage >= perPageLimit || inRule >= perRuleLimit) {
|
|
391
|
+
omitted++;
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
perPage.set(pageKey, onPage + 1);
|
|
395
|
+
perRule.set(f.rule, inRule + 1);
|
|
396
|
+
findings.push(f);
|
|
397
|
+
}
|
|
398
|
+
return { findings, omitted };
|
|
399
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const STORAGE = 'open-doc:recent';
|
|
2
|
+
const KEEP = 8;
|
|
3
|
+
|
|
4
|
+
export function pushRecent(list: readonly string[], docId: string): string[] {
|
|
5
|
+
return [docId, ...list.filter((id) => id !== docId)].slice(0, KEEP);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Ids no longer in the workspace — deleted, or renamed by the agent — are dropped. */
|
|
9
|
+
export function recentAmong(list: readonly string[], known: readonly string[], limit: number) {
|
|
10
|
+
const present = new Set(known);
|
|
11
|
+
return list.filter((id) => present.has(id)).slice(0, limit);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function readRecentDocs(): string[] {
|
|
15
|
+
try {
|
|
16
|
+
const parsed: unknown = JSON.parse(localStorage.getItem(STORAGE) ?? '[]');
|
|
17
|
+
return Array.isArray(parsed) ? parsed.filter((id): id is string => typeof id === 'string') : [];
|
|
18
|
+
} catch {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function markDocOpened(docId: string): void {
|
|
24
|
+
try {
|
|
25
|
+
localStorage.setItem(STORAGE, JSON.stringify(pushRecent(readRecentDocs(), docId)));
|
|
26
|
+
} catch {}
|
|
27
|
+
}
|
package/src/app/lib/sdk.ts
CHANGED
|
@@ -47,12 +47,41 @@ export type PageGeometry = {
|
|
|
47
47
|
css: string;
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
+
/** The same paper turned a quarter: a landscape section in a portrait document, or back. */
|
|
51
|
+
export function turnGeometry(geometry: PageGeometry): PageGeometry {
|
|
52
|
+
const [across, down] = geometry.css.split(/\s+/);
|
|
53
|
+
return { width: geometry.height, height: geometry.width, css: `${down} ${across}` };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A flow section's sheet: the document's, turned when the section sets the other orientation. */
|
|
57
|
+
export function sectionGeometry(
|
|
58
|
+
geometry: PageGeometry,
|
|
59
|
+
orientation: Orientation | undefined,
|
|
60
|
+
): PageGeometry {
|
|
61
|
+
if (!orientation) return geometry;
|
|
62
|
+
const landscape = geometry.width > geometry.height;
|
|
63
|
+
return (orientation === 'landscape') === landscape ? geometry : turnGeometry(geometry);
|
|
64
|
+
}
|
|
65
|
+
|
|
50
66
|
export type DocPage = ComponentType;
|
|
51
67
|
|
|
52
68
|
/**
|
|
53
69
|
* A run of continuous content the framework paginates itself. Build one with
|
|
54
70
|
* `flow(...)` and put it in the page array alongside fixed pages.
|
|
55
71
|
*/
|
|
72
|
+
export type FlowPadding = number | { top: number; right: number; bottom: number; left: number };
|
|
73
|
+
|
|
74
|
+
/** A flow section's padding, side by side. */
|
|
75
|
+
export function paddingSides(
|
|
76
|
+
padding: FlowPadding | undefined,
|
|
77
|
+
fallback: number,
|
|
78
|
+
): { top: number; right: number; bottom: number; left: number } {
|
|
79
|
+
const value = padding ?? fallback;
|
|
80
|
+
return typeof value === 'number'
|
|
81
|
+
? { top: value, right: value, bottom: value, left: value }
|
|
82
|
+
: value;
|
|
83
|
+
}
|
|
84
|
+
|
|
56
85
|
export type FlowSection = {
|
|
57
86
|
readonly __odFlow: true;
|
|
58
87
|
blocks: ReactNode[];
|
|
@@ -60,8 +89,17 @@ export type FlowSection = {
|
|
|
60
89
|
header?: ComponentType;
|
|
61
90
|
/** Rendered on every page the section expands into. */
|
|
62
91
|
footer?: ComponentType;
|
|
63
|
-
/**
|
|
64
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Page padding override in px — one number for all four sides, or each side
|
|
94
|
+
* on its own (a Word page's top and bottom margins rarely match its sides).
|
|
95
|
+
* Defaults to the design's `margin`.
|
|
96
|
+
*/
|
|
97
|
+
padding?: FlowPadding;
|
|
98
|
+
/**
|
|
99
|
+
* Turns this section's sheets — a wide table's landscape pages inside a
|
|
100
|
+
* portrait document. Defaults to the document's orientation.
|
|
101
|
+
*/
|
|
102
|
+
orientation?: Orientation;
|
|
65
103
|
/** Number of columns for body content inside each page of this flow section. */
|
|
66
104
|
columns?: 1 | 2 | 3;
|
|
67
105
|
/** Gap in px between columns when `columns > 1`; defaults to 24. */
|
|
@@ -107,6 +145,10 @@ export type DocMeta = {
|
|
|
107
145
|
recordLabel?: string;
|
|
108
146
|
/** Diagonal watermark text stamped across every sheet (e.g. 'CONFIDENTIAL', 'DRAFT'). */
|
|
109
147
|
watermark?: string;
|
|
148
|
+
/** Id of the document this one translates. `open-doc translations` reports when it falls behind. */
|
|
149
|
+
translationOf?: string;
|
|
150
|
+
/** The source's commit this translation matches. Written by `open-doc translations --mark`. */
|
|
151
|
+
translatedAt?: string;
|
|
110
152
|
};
|
|
111
153
|
|
|
112
154
|
/**
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { locOf, pageNumberOf } from './diagnostics';
|
|
2
|
+
import { PAGE_ATTR } from './outline';
|
|
3
|
+
import type { TextRun } from './proofread';
|
|
4
|
+
|
|
5
|
+
/** Text a proofreader leaves alone: code, and furigana that sits above a word rather than in it. */
|
|
6
|
+
const SKIP = new Set([
|
|
7
|
+
'code',
|
|
8
|
+
'pre',
|
|
9
|
+
'kbd',
|
|
10
|
+
'samp',
|
|
11
|
+
'var',
|
|
12
|
+
'script',
|
|
13
|
+
'style',
|
|
14
|
+
'noscript',
|
|
15
|
+
'template',
|
|
16
|
+
'textarea',
|
|
17
|
+
'rt',
|
|
18
|
+
'rp',
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
function isInline(el: Element): boolean {
|
|
22
|
+
if (el instanceof SVGElement) return el.tagName === 'tspan' || el.tagName === 'a';
|
|
23
|
+
const display = getComputedStyle(el).display;
|
|
24
|
+
return display === 'inline' || display === 'contents';
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Splits each rendered sheet into runs of text that read as one line of
|
|
29
|
+
* prose: text inside one block, joined across inline marks, cut at every
|
|
30
|
+
* block, line break of a block, and piece of code.
|
|
31
|
+
*/
|
|
32
|
+
export function collectTextRuns(root: ParentNode): TextRun[] {
|
|
33
|
+
const runs: TextRun[] = [];
|
|
34
|
+
const frames = Array.from(root.querySelectorAll<HTMLElement>(`[${PAGE_ATTR}]`));
|
|
35
|
+
|
|
36
|
+
frames.forEach((frame, index) => {
|
|
37
|
+
const page = pageNumberOf(frame, index);
|
|
38
|
+
let text = '';
|
|
39
|
+
let block: Element = frame;
|
|
40
|
+
|
|
41
|
+
const flush = () => {
|
|
42
|
+
if (text.trim()) {
|
|
43
|
+
const holder = block.closest('[lang]');
|
|
44
|
+
const lang = holder && frame.contains(holder) ? holder.getAttribute('lang') : null;
|
|
45
|
+
const loc = locOf(block);
|
|
46
|
+
runs.push({ text, page, ...(loc ? { loc } : {}), ...(lang ? { lang } : {}) });
|
|
47
|
+
}
|
|
48
|
+
text = '';
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const walk = (node: Node, container: Element) => {
|
|
52
|
+
for (const child of Array.from(node.childNodes)) {
|
|
53
|
+
if (child.nodeType === Node.TEXT_NODE) {
|
|
54
|
+
if (!text) block = container;
|
|
55
|
+
text += (child as Text).data;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (!(child instanceof Element)) continue;
|
|
59
|
+
if (SKIP.has(child.tagName.toLowerCase()) || child.hasAttribute('data-od-watermark')) {
|
|
60
|
+
flush();
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (child.tagName === 'BR') {
|
|
64
|
+
text += '\n';
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (!(child instanceof SVGElement) && getComputedStyle(child).display === 'none') continue;
|
|
68
|
+
if (isInline(child)) {
|
|
69
|
+
walk(child, container);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
flush();
|
|
73
|
+
walk(child, child);
|
|
74
|
+
flush();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
walk(frame, frame);
|
|
79
|
+
flush();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return runs;
|
|
83
|
+
}
|