@xiaobuyu/nodesign 0.0.1 → 0.0.2

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.
Files changed (282) hide show
  1. package/README.md +156 -106
  2. package/bin/nodesign.js +0 -0
  3. package/package.json +1 -1
  4. package/server/api/README.md +45 -45
  5. package/server/api/_guard.js +75 -75
  6. package/server/api/admin.js +187 -187
  7. package/server/api/assets/docx-page.js +103 -103
  8. package/server/api/assets/helpers.js +22 -22
  9. package/server/api/assets/notes.js +144 -144
  10. package/server/api/assets.js +896 -896
  11. package/server/api/board.js +57 -57
  12. package/server/api/browse.js +138 -138
  13. package/server/api/canvas.js +444 -444
  14. package/server/api/chatai.js +324 -324
  15. package/server/api/exports/build-standalone.js +980 -980
  16. package/server/api/exports/cards.js +118 -118
  17. package/server/api/exports/docx-pdf.js +46 -46
  18. package/server/api/exports/export-page.js +142 -142
  19. package/server/api/exports/handoff.js +147 -147
  20. package/server/api/exports.js +690 -690
  21. package/server/api/instruction.js +75 -75
  22. package/server/api/me.js +116 -116
  23. package/server/api/memory.js +185 -185
  24. package/server/api/pending-changes.js +366 -366
  25. package/server/api/plugins.js +164 -164
  26. package/server/api/projects.js +220 -220
  27. package/server/api/publish.js +64 -64
  28. package/server/api/recent.js +73 -73
  29. package/server/api/sessions.js +589 -589
  30. package/server/api/skills.js +77 -77
  31. package/server/api/standalone-fit.js +168 -168
  32. package/server/api/turn-compose.js +180 -180
  33. package/server/api/turn-inflight.js +44 -44
  34. package/server/auth/README.md +17 -17
  35. package/server/auth/middleware.js +138 -138
  36. package/server/auth/origin-guard.js +71 -71
  37. package/server/auth/session.js +108 -108
  38. package/server/auth/tier.js +113 -113
  39. package/server/auth/users-store.js +348 -348
  40. package/server/edit/README.md +29 -29
  41. package/server/engine/README.md +113 -113
  42. package/server/engine/agent/agent-shared.js +550 -550
  43. package/server/engine/agent/auto-mode-default-hard-deny.txt +4 -4
  44. package/server/engine/agent/auto-mode-rules.js +78 -78
  45. package/server/engine/agent/context.js +418 -418
  46. package/server/engine/agent/events.js +464 -464
  47. package/server/engine/agent/hooks/canvas-validate.js +199 -199
  48. package/server/engine/agent/hooks/failure.js +120 -120
  49. package/server/engine/agent/hooks/file-events.js +75 -75
  50. package/server/engine/agent/hooks/lifecycle.js +233 -233
  51. package/server/engine/agent/hooks/post-canvas-focus.js +80 -80
  52. package/server/engine/agent/hooks/post-guidance.js +206 -206
  53. package/server/engine/agent/hooks/post-subagent-report.js +104 -104
  54. package/server/engine/agent/hooks/post-trim.js +58 -58
  55. package/server/engine/agent/hooks/pre-board-neighborhood.js +34 -34
  56. package/server/engine/agent/hooks/pre-defaults.js +84 -84
  57. package/server/engine/agent/hooks/pre-injectors.js +277 -277
  58. package/server/engine/agent/hooks/pre-performance-log-guard.js +78 -78
  59. package/server/engine/agent/hooks/pre-starter-files.js +61 -61
  60. package/server/engine/agent/hooks/pre-workspace-scope-guard.js +78 -78
  61. package/server/engine/agent/hooks/site-validate.js +94 -94
  62. package/server/engine/agent/hooks/tool-prompts.js +31 -31
  63. package/server/engine/agent/hooks/turn-state-memory.js +56 -56
  64. package/server/engine/agent/hooks/user-prompt-submit.js +215 -215
  65. package/server/engine/agent/hooks.js +354 -354
  66. package/server/engine/agent/init-contract.js +69 -69
  67. package/server/engine/agent/isolation.js +159 -159
  68. package/server/engine/agent/model-table.js +292 -292
  69. package/server/engine/agent/plugin-loader.js +266 -266
  70. package/server/engine/agent/prompts/nodesign-prelude.md +336 -336
  71. package/server/engine/agent/prompts/tools/ask-user-question-protocol.md +71 -71
  72. package/server/engine/agent/prompts/tools/direct-edit-protocol.md +98 -98
  73. package/server/engine/agent/prompts/tools/generate-image-cookbook.gemini-gateway.md +583 -583
  74. package/server/engine/agent/prompts/tools/generate-image-cookbook.md +483 -483
  75. package/server/engine/agent/prompts/tools/hybrid-reference.md +100 -100
  76. package/server/engine/agent/prompts/tools/paint-still-cookbook.md +270 -270
  77. package/server/engine/agent/prompts/tools/roll-film-cookbook.md +70 -70
  78. package/server/engine/agent/prompts/tools/site-reference.md +106 -106
  79. package/server/engine/agent/prompts/tools/tweaks-syntax.md +125 -125
  80. package/server/engine/agent/prompts/tools/vision-checker-dispatch.md +48 -48
  81. package/server/engine/agent/session-loop.js +1024 -1024
  82. package/server/engine/agent/session-model.js +164 -164
  83. package/server/engine/agent/skill.js +204 -204
  84. package/server/engine/agent/task-events.js +78 -78
  85. package/server/engine/agents/ds-extractor.md +132 -132
  86. package/server/engine/agents/explorer.md +190 -190
  87. package/server/engine/agents/index.js +230 -230
  88. package/server/engine/agents/schemas/design-system.json +156 -156
  89. package/server/engine/agents/schemas/tweak-schema.json +113 -113
  90. package/server/engine/agents/tweak-proposer.md +127 -127
  91. package/server/engine/agents/vision-checker.md +254 -254
  92. package/server/engine/browse/capture.js +422 -422
  93. package/server/engine/browse/card.js +137 -137
  94. package/server/engine/browse/handover.js +59 -59
  95. package/server/engine/browse/page-digest.js +119 -119
  96. package/server/engine/browse/refs.js +196 -196
  97. package/server/engine/browse/registry.js +307 -307
  98. package/server/engine/browse/screencast.js +170 -170
  99. package/server/engine/browse/state.js +121 -121
  100. package/server/engine/chatai/chat-log.js +79 -79
  101. package/server/engine/chatai/index.js +193 -193
  102. package/server/engine/chatai/openai-compat.js +152 -152
  103. package/server/engine/chatai/orchestrate.js +309 -309
  104. package/server/engine/chatai/perform.js +40 -40
  105. package/server/engine/chatai/summarize.js +79 -79
  106. package/server/engine/mcp/capability-gate.js +52 -52
  107. package/server/engine/mcp/index.js +339 -339
  108. package/server/engine/mcp/param-sanitizer.js +142 -142
  109. package/server/engine/mcp/tools/arrange-on-board.js +124 -124
  110. package/server/engine/mcp/tools/artifact-session.js +278 -278
  111. package/server/engine/mcp/tools/browse-computer.js +421 -421
  112. package/server/engine/mcp/tools/browse-find-batch.js +183 -183
  113. package/server/engine/mcp/tools/browse-screenshot.js +157 -157
  114. package/server/engine/mcp/tools/browse.js +480 -480
  115. package/server/engine/mcp/tools/build-docx.js +101 -101
  116. package/server/engine/mcp/tools/clear-pending-changes.js +99 -99
  117. package/server/engine/mcp/tools/create-on-board.js +155 -155
  118. package/server/engine/mcp/tools/crystallize-skill.js +168 -168
  119. package/server/engine/mcp/tools/explain-style.js +237 -237
  120. package/server/engine/mcp/tools/export-handoff.js +133 -133
  121. package/server/engine/mcp/tools/expose-tweaks.js +149 -149
  122. package/server/engine/mcp/tools/generate-image.js +842 -842
  123. package/server/engine/mcp/tools/get-computed-styles.js +164 -164
  124. package/server/engine/mcp/tools/get-pending-changes.js +205 -205
  125. package/server/engine/mcp/tools/h3box-ssh.js +84 -84
  126. package/server/engine/mcp/tools/helpers/acquire-page.js +82 -82
  127. package/server/engine/mcp/tools/helpers/motion-lab.js +554 -554
  128. package/server/engine/mcp/tools/helpers/motion-scroll.js +112 -112
  129. package/server/engine/mcp/tools/helpers/perception-page.js +215 -215
  130. package/server/engine/mcp/tools/helpers/reference-download.js +103 -103
  131. package/server/engine/mcp/tools/helpers/rembg-bridge.py +80 -80
  132. package/server/engine/mcp/tools/helpers/shot-pipeline.js +280 -280
  133. package/server/engine/mcp/tools/highlight.js +68 -68
  134. package/server/engine/mcp/tools/list-pages.js +222 -222
  135. package/server/engine/mcp/tools/lookup-tags.js +59 -59
  136. package/server/engine/mcp/tools/navigate-to-page.js +60 -60
  137. package/server/engine/mcp/tools/organize-board.js +64 -64
  138. package/server/engine/mcp/tools/paint-still.js +434 -434
  139. package/server/engine/mcp/tools/pin-to-board.js +130 -130
  140. package/server/engine/mcp/tools/preview-deck.js +130 -130
  141. package/server/engine/mcp/tools/profile-scroll.js +305 -305
  142. package/server/engine/mcp/tools/publish-site.js +102 -102
  143. package/server/engine/mcp/tools/query-elements.js +195 -195
  144. package/server/engine/mcp/tools/read-board.js +103 -103
  145. package/server/engine/mcp/tools/read-document.js +93 -93
  146. package/server/engine/mcp/tools/read-page.js +264 -264
  147. package/server/engine/mcp/tools/read-tavern-json.js +143 -143
  148. package/server/engine/mcp/tools/record-decision.js +140 -140
  149. package/server/engine/mcp/tools/relate-on-board.js +110 -110
  150. package/server/engine/mcp/tools/report-issue.js +115 -115
  151. package/server/engine/mcp/tools/roll-film.js +266 -266
  152. package/server/engine/mcp/tools/screenshot-docx.js +138 -138
  153. package/server/engine/mcp/tools/screenshot-url.js +195 -195
  154. package/server/engine/mcp/tools/screenshot.js +583 -583
  155. package/server/engine/mcp/tools/tier-gate.js +96 -96
  156. package/server/engine/mcp/tools/trace-motion.js +215 -215
  157. package/server/engine/mcp/tools/web-search.js +548 -548
  158. package/server/engine/motion/inventory.js +349 -349
  159. package/server/engine/perception/session.js +235 -235
  160. package/server/engine/plugins/nodesign/.claude-plugin/plugin.json +5 -5
  161. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/SKILL.md +192 -192
  162. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/canvas.template.html +667 -667
  163. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/hybrid-grid.md +22 -22
  164. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/image-led-cover.md +25 -25
  165. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/portrait.md +23 -23
  166. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/quote-backdrop.md +21 -21
  167. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/section-divider.md +23 -23
  168. package/server/engine/plugins/nodesign/skills/deskskill-engine-mini/patterns/text-led.md +21 -21
  169. package/server/engine/plugins/nodesign/skills/docx-craft/SKILL.md +162 -162
  170. package/server/engine/plugins/nodesign/skills/docx-craft/references/token-schema.md +407 -407
  171. package/server/engine/plugins/nodesign/skills/docx-craft//346/226/207/346/241/243.template.json +80 -80
  172. package/server/engine/plugins/nodesign/skills/rp-craft/SKILL.md +228 -228
  173. package/server/engine/plugins/nodesign/skills/rp-craft/patterns//346/274/224/345/207/272/351/241/265-/346/234/200/345/260/217/345/256/236/347/216/260.html +78 -78
  174. package/server/engine/plugins/nodesign/skills/rp-craft//346/274/224/345/207/272.template.js +268 -268
  175. package/server/engine/plugins/nodesign/skills/site-craft/SKILL.md +323 -323
  176. package/server/engine/plugins/nodesign/skills/site-craft/patterns/build-lane.md +134 -134
  177. package/server/engine/plugins/nodesign/skills/site-craft/patterns/cutout-collage.md +61 -61
  178. package/server/engine/plugins/nodesign/skills/site-craft/patterns/mock-app.md +163 -163
  179. package/server/engine/plugins/nodesign/skills/site-craft/patterns/page-transitions.md +86 -86
  180. package/server/engine/runs/active-runs.js +698 -698
  181. package/server/engine/runs/live-turn.js +252 -252
  182. package/server/engine/runs/store.js +364 -364
  183. package/server/engine/runs/turn-relay.js +217 -217
  184. package/server/engine/runtime/workspace.js +164 -164
  185. package/server/lib/artifact-file-path.js +80 -80
  186. package/server/lib/artifact-target.js +284 -284
  187. package/server/lib/asset-refs.js +239 -239
  188. package/server/lib/async-queue.js +104 -104
  189. package/server/lib/auto-relations.js +130 -130
  190. package/server/lib/binding-types.js +60 -60
  191. package/server/lib/board-kind-sizes.js +62 -62
  192. package/server/lib/board-relations.js +133 -133
  193. package/server/lib/browse-proxy.js +180 -180
  194. package/server/lib/canvas-id.js +36 -36
  195. package/server/lib/cover.js +227 -227
  196. package/server/lib/danbooru-tags.js +291 -291
  197. package/server/lib/doc-extract.js +156 -156
  198. package/server/lib/docx/build-from-source.js +260 -260
  199. package/server/lib/docx/build.js +490 -490
  200. package/server/lib/docx/dump-styles.js +287 -287
  201. package/server/lib/docx/fonts/nodesign-cjk.conf +164 -164
  202. package/server/lib/docx/merge-runs.js +129 -129
  203. package/server/lib/docx/numbering.js +218 -218
  204. package/server/lib/docx/order.js +152 -152
  205. package/server/lib/docx/rawzip.js +172 -172
  206. package/server/lib/docx/render.js +92 -92
  207. package/server/lib/docx/text-lint.js +225 -225
  208. package/server/lib/docx/tokens.js +335 -335
  209. package/server/lib/docx/units.js +29 -29
  210. package/server/lib/docx/xml.js +291 -291
  211. package/server/lib/docx-pages.js +187 -187
  212. package/server/lib/export-collect.js +289 -289
  213. package/server/lib/export-package.js +210 -210
  214. package/server/lib/html-srcset.js +145 -145
  215. package/server/lib/image-variant.js +460 -460
  216. package/server/lib/ingress/forward-openai-chat.js +305 -305
  217. package/server/lib/ingress/openai-chat.js +491 -491
  218. package/server/lib/ingress/session-notice.js +66 -66
  219. package/server/lib/ingress/session-routes.js +68 -68
  220. package/server/lib/ingress/slot-probe.js +130 -130
  221. package/server/lib/ingress/upstream-billing.js +52 -52
  222. package/server/lib/ingress/upstream-fail-streak.js +72 -72
  223. package/server/lib/ingress/upstream-truncation.js +48 -48
  224. package/server/lib/issues-store.js +182 -182
  225. package/server/lib/kinds/deck.js +72 -72
  226. package/server/lib/kinds/docx.js +283 -283
  227. package/server/lib/kinds/file-kinds.js +113 -113
  228. package/server/lib/kinds/index.js +202 -202
  229. package/server/lib/kinds/site.js +161 -161
  230. package/server/lib/model-ingress.js +543 -543
  231. package/server/lib/moderation.js +255 -255
  232. package/server/lib/notice-store.js +103 -103
  233. package/server/lib/plugin-install.js +104 -104
  234. package/server/lib/plugin-validator.js +721 -721
  235. package/server/lib/publish-store.js +115 -115
  236. package/server/lib/quick-summary.js +52 -52
  237. package/server/lib/quota.js +270 -270
  238. package/server/lib/rate-window.js +29 -29
  239. package/server/lib/reference-assets.js +92 -92
  240. package/server/lib/region-shot.js +135 -135
  241. package/server/lib/safe-path.js +73 -73
  242. package/server/lib/sdk-session.js +33 -33
  243. package/server/lib/showcase-store.js +92 -92
  244. package/server/lib/site-publish.js +465 -465
  245. package/server/lib/ssrf-guard.js +432 -432
  246. package/server/lib/task-scan.js +158 -158
  247. package/server/lib/tavern-json.js +154 -154
  248. package/server/lib/video-variant.js +237 -237
  249. package/server/lib/workspace-path.js +33 -33
  250. package/server/ops/fix-sdk-musl.mjs +40 -40
  251. package/server/ops/install-macos-fonts.sh +114 -114
  252. package/server/ops/macos-fonts.conf +336 -336
  253. package/server/ops/sandbox-shim/bwrap +57 -57
  254. package/server/projects/assets-summary.js +119 -119
  255. package/server/projects/auto-name.js +53 -53
  256. package/server/projects/board-store.js +640 -640
  257. package/server/projects/move-entry.js +103 -103
  258. package/server/projects/store.js +270 -270
  259. package/server/projects/ui-config.js +54 -54
  260. package/server/projects/workspace-templates.js +53 -53
  261. package/server/projects/workspace.js +1025 -1025
  262. package/server/runtime/local-config.js +177 -177
  263. package/server/runtime/profile.js +72 -72
  264. package/server/services/rembg-service.py +334 -334
  265. package/server/shared/README.md +25 -25
  266. package/server/shared/deck.js +54 -54
  267. package/server/shared/time.js +49 -49
  268. package/server/style-pipeline/README.md +53 -53
  269. package/server/ws/broker.js +36 -36
  270. package/server/ws/browse-channel.js +177 -177
  271. package/server/ws/index.js +386 -386
  272. package/web/README.md +89 -89
  273. package/web/dist/assets/BrowserWindow-BYCgizHw.js +11 -0
  274. package/web/dist/assets/{DeckWindow-4G0xX7P3.js → DeckWindow-pNydEM4L.js} +1 -1
  275. package/web/dist/assets/{DocxWindow-BXQsoPkh.js → DocxWindow-PPgzh3Gq.js} +1 -1
  276. package/web/dist/assets/{SiteWindow-D6eS-2gM.js → SiteWindow-DM0LQPCO.js} +1 -1
  277. package/web/dist/assets/{index-Cbl3ATvp.js → index-BHTBJlpY.js} +32 -32
  278. package/web/dist/assets/{pending-edit-apply-DTjo2geW.js → pending-edit-apply-C0I13CbD.js} +1 -1
  279. package/web/dist/assets/{rotate-cw-LMT3x1Zj.js → rotate-cw-B1UQ8Hld.js} +1 -1
  280. package/web/dist/assets/{square-dashed-mouse-pointer-CBoAkfhi.js → square-dashed-mouse-pointer-8lrgqmfv.js} +1 -1
  281. package/web/dist/index.html +17 -17
  282. package/web/dist/assets/BrowserWindow-Dc7J7CYc.js +0 -11
@@ -1,335 +1,335 @@
1
- /**
2
- * tokens.js — docx 形态的「真相源」:token JSON schema + 三个内置词典条目。
3
- *
4
- * 原则(已拍板):
5
- * - token JSON 是权威,styles.xml/docDefaults 是构建产物;
6
- * - 风格名开放(agent 可以起新名字调 token),schema 闭合(字段全集在
7
- * 这里,build 层只认识这些字段,不接受任意 XML 注入——外来文档的
8
- * 未建模部分走 extraXml/rawStyles 透传,不走 token 字段扩张)。
9
- *
10
- * ── schema v1 ────────────────────────────────────────────────
11
- * {
12
- * v: 1,
13
- * provenance: 'nodesign' | 'imported', // imported = 外来 docx dump 出来的
14
- * page: {
15
- * size: 'A4'|'Letter'|{wTwip,hTwip}, landscape?: bool,
16
- * marginsTwip: {top,bottom,left,right,header,footer,gutter},
17
- * docGrid?: {type:'lines'|'linesAndChars', linePitchTwip, charSpace?}|null,
18
- * },
19
- * lang: {latin:'en-US', eastAsia:'zh-CN'},
20
- * fonts: { // 具名字体槽;style.run.font 用名字引用
21
- * body:{eastAsia,ascii,hAnsi?,cs?}, heading:{...}, quote:{...}, mono:{...}, ...自定义槽
22
- * },
23
- * base: { // docDefaults
24
- * sizePt: number|字号名, color:'RRGGBB',
25
- * font:'body'|{...}, // 引用字体槽
26
- * spacing?: {line,lineRule,beforePt,afterPt},
27
- * cjk: {kinsoku,autoSpaceDE,autoSpaceDN,adjustRightInd,snapToGrid,overflowPunct,wordWrap},
28
- * },
29
- * styles: { [styleId]: Style },
30
- * numbering?: { [numName]: {levels:[{fmt,text,indentTwip,hangingTwip,alignedAt?}]} },
31
- * rawStyles?: [xmlString], // dump 时装不进模型的整条 w:style 原文
32
- * }
33
- *
34
- * Style = {
35
- * type:'paragraph'|'character'|'table',
36
- * name: string, // Word UI 名('heading 1' 等内置名有魔法含义)
37
- * basedOn?, next?, link?, qFormat?: bool, uiPriority?: number,
38
- * run?: {
39
- * font?:'body'|{eastAsia?,ascii?,hAnsi?,cs?}, sizePt?: number|字号名,
40
- * bold?, italic?, color?, underline?: true|'single'|'double'|...,
41
- * strike?, caps?, smallCaps?, vertAlign?:'superscript'|'subscript',
42
- * em?:'dot'|'comma'|'circle'|'underDot', // 着重号
43
- * kernPt?: number, spacingTwip?: number, highlight?: string,
44
- * },
45
- * para?: {
46
- * align?:'left'|'center'|'right'|'both'|'distribute',
47
- * outlineLevel?: 0-8,
48
- * indent?: {firstLineChars?, firstLineTwip?, hangingChars?, hangingTwip?,
49
- * leftChars?, leftTwip?, rightChars?, rightTwip?},
50
- * spacing?: {beforePt?, afterPt?, beforeLines?, afterLines?,
51
- * line?: number, lineRule?:'multiple'|'exact'|'atLeast'},
52
- * // lineRule=multiple 时 line 是倍数(1.5);exact/atLeast 时是磅
53
- * keepNext?, keepLines?, pageBreakBefore?, widowControl?, contextualSpacing?,
54
- * borders?: {top?,bottom?,left?,right?: {style,sizePt8?,color,spacePt?}},
55
- * shading?: 'RRGGBB',
56
- * tabs?: [{pos:twip, val:'left'|'center'|'right'|'decimal', leader?}],
57
- * cjk?: 覆盖 base.cjk 的同名开关,
58
- * },
59
- * extraXml?: {pPr?: string, rPr?: string}, // dump 透传,build 时原样并入
60
- * }
61
- */
62
-
63
- import { ZIHAO } from './units.js';
64
- import { validateNumbering } from './numbering.js';
65
-
66
- /** 词典条目一:办公标准(素颜 Word)——中文 Word 默认观感 */
67
- export function presetOffice() {
68
- return {
69
- v: 1,
70
- provenance: 'nodesign',
71
- page: {
72
- size: 'A4',
73
- marginsTwip: { top: 1440, bottom: 1440, left: 1800, right: 1800, header: 851, footer: 992, gutter: 0 },
74
- },
75
- lang: { latin: 'en-US', eastAsia: 'zh-CN' },
76
- fonts: {
77
- body: { eastAsia: '宋体', ascii: 'Calibri' },
78
- heading: { eastAsia: '黑体', ascii: 'Arial' },
79
- quote: { eastAsia: '楷体', ascii: 'Calibri' },
80
- mono: { eastAsia: '宋体', ascii: 'Courier New' },
81
- },
82
- base: {
83
- font: 'body', sizePt: '小四', color: '000000',
84
- spacing: { line: 1.3, lineRule: 'multiple' },
85
- cjk: { kinsoku: true, autoSpaceDE: true, autoSpaceDN: true, adjustRightInd: true, snapToGrid: true },
86
- },
87
- styles: {
88
- Normal: { type: 'paragraph', name: 'Normal', qFormat: true, para: { indent: { firstLineChars: 200 } } },
89
- Title: {
90
- type: 'paragraph', name: 'Title', basedOn: 'Normal', next: 'Normal', qFormat: true,
91
- run: { font: 'heading', sizePt: '二号', bold: true },
92
- para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { beforePt: 12, afterPt: 18 }, keepNext: true, outlineLevel: 0 },
93
- },
94
- Heading1: {
95
- type: 'paragraph', name: 'heading 1', basedOn: 'Normal', next: 'Normal', qFormat: true,
96
- run: { font: 'heading', sizePt: '三号', bold: true },
97
- para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 13, afterPt: 13 }, keepNext: true, outlineLevel: 0 },
98
- },
99
- Heading2: {
100
- type: 'paragraph', name: 'heading 2', basedOn: 'Normal', next: 'Normal', qFormat: true,
101
- run: { font: 'heading', sizePt: '四号', bold: true },
102
- para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 10, afterPt: 10 }, keepNext: true, outlineLevel: 1 },
103
- },
104
- Heading3: {
105
- type: 'paragraph', name: 'heading 3', basedOn: 'Normal', next: 'Normal', qFormat: true,
106
- run: { font: 'heading', sizePt: '小四', bold: true },
107
- para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 8, afterPt: 8 }, keepNext: true, outlineLevel: 2 },
108
- },
109
- Quote: {
110
- type: 'paragraph', name: 'Quote', basedOn: 'Normal', qFormat: true,
111
- run: { font: 'quote', color: '404040' },
112
- para: { indent: { leftChars: 200, firstLineChars: 0 } },
113
- },
114
- Header: { type: 'paragraph', name: 'header', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { sizePt: '小五' } },
115
- Footer: { type: 'paragraph', name: 'footer', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { sizePt: '小五' } },
116
- },
117
- };
118
- }
119
-
120
- /** 词典条目二:公文体(GB/T 9704-2012 版式硬指标) */
121
- export function presetGongwen() {
122
- return {
123
- v: 1,
124
- provenance: 'nodesign',
125
- page: {
126
- size: 'A4',
127
- // GB/T 9704: 上37 下35 左28 右26 (mm)
128
- marginsTwip: { top: 2098, bottom: 1984, left: 1588, right: 1474, header: 851, footer: 992, gutter: 0 },
129
- docGrid: { type: 'lines', linePitchTwip: 560 }, // 固定 28pt 行距的格
130
- },
131
- lang: { latin: 'zh-CN', eastAsia: 'zh-CN' },
132
- fonts: {
133
- body: { eastAsia: '仿宋_GB2312', ascii: 'Times New Roman' },
134
- biaosong: { eastAsia: '方正小标宋简体', ascii: 'Times New Roman' },
135
- hei: { eastAsia: '黑体', ascii: 'Times New Roman' },
136
- kai: { eastAsia: '楷体_GB2312', ascii: 'Times New Roman' },
137
- song: { eastAsia: '宋体', ascii: 'Times New Roman' },
138
- },
139
- base: {
140
- font: 'body', sizePt: '三号', color: '000000',
141
- spacing: { line: 28, lineRule: 'exact' },
142
- cjk: { kinsoku: true, autoSpaceDE: false, autoSpaceDN: false, adjustRightInd: true, snapToGrid: true },
143
- },
144
- styles: {
145
- Normal: { type: 'paragraph', name: 'Normal', qFormat: true, para: { indent: { firstLineChars: 200 } } },
146
- // 发文机关标志(红头)
147
- Hongtou: {
148
- type: 'paragraph', name: '红头', basedOn: 'Normal', next: 'Normal',
149
- run: { font: 'biaosong', sizePt: 32, color: 'FF0000', bold: false },
150
- para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { line: 40, lineRule: 'exact' } },
151
- },
152
- Fawenzihao: {
153
- type: 'paragraph', name: '发文字号', basedOn: 'Normal', next: 'Normal',
154
- para: { align: 'center', indent: { firstLineChars: 0 } },
155
- },
156
- GwTitle: {
157
- type: 'paragraph', name: '公文标题', basedOn: 'Normal', next: 'Normal', qFormat: true,
158
- // 标题前后各空一行(28pt)。用 pt 不用 beforeLines:lab/02 已证 LO 对
159
- // 字符/行单位的兼容不可信,*Lines 同族,公文版式是硬指标不赌。
160
- run: { font: 'biaosong', sizePt: '二号' },
161
- para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { beforePt: 28, afterPt: 28 }, keepNext: true, outlineLevel: 0 },
162
- },
163
- GwH1: { // 一、 黑体
164
- type: 'paragraph', name: '公文一级', basedOn: 'Normal', next: 'Normal', qFormat: true,
165
- run: { font: 'hei' }, para: { keepNext: true, outlineLevel: 0 },
166
- },
167
- GwH2: { // (一) 楷体
168
- type: 'paragraph', name: '公文二级', basedOn: 'Normal', next: 'Normal', qFormat: true,
169
- run: { font: 'kai' }, para: { keepNext: true, outlineLevel: 1 },
170
- },
171
- GwH3: { // 1. 仿宋加粗
172
- type: 'paragraph', name: '公文三级', basedOn: 'Normal', next: 'Normal', qFormat: true,
173
- run: { bold: true }, para: { keepNext: true, outlineLevel: 2 },
174
- },
175
- Chaosong: {
176
- type: 'paragraph', name: '抄送', basedOn: 'Normal',
177
- run: { sizePt: '四号', font: 'body' }, para: { indent: { firstLineChars: 0 } },
178
- },
179
- Footer: { type: 'paragraph', name: 'footer', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { font: 'song', sizePt: '四号' } },
180
- Header: { type: 'paragraph', name: 'header', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { font: 'song', sizePt: '小五' } },
181
- },
182
- };
183
- }
184
-
185
- /** 词典条目三:学术论文体(中文期刊/学位论文常规) */
186
- export function presetAcademic() {
187
- return {
188
- v: 1,
189
- provenance: 'nodesign',
190
- page: {
191
- size: 'A4',
192
- marginsTwip: { top: 1700, bottom: 1700, left: 1800, right: 1800, header: 851, footer: 992, gutter: 0 },
193
- },
194
- lang: { latin: 'en-US', eastAsia: 'zh-CN' },
195
- fonts: {
196
- body: { eastAsia: '宋体', ascii: 'Times New Roman' },
197
- heading: { eastAsia: '黑体', ascii: 'Times New Roman' },
198
- quote: { eastAsia: '楷体', ascii: 'Times New Roman' },
199
- caption: { eastAsia: '宋体', ascii: 'Times New Roman' },
200
- mono: { eastAsia: '宋体', ascii: 'Courier New' },
201
- },
202
- base: {
203
- font: 'body', sizePt: '小四', color: '000000',
204
- spacing: { line: 1.5, lineRule: 'multiple' },
205
- cjk: { kinsoku: true, autoSpaceDE: true, autoSpaceDN: true, adjustRightInd: true, snapToGrid: true },
206
- },
207
- styles: {
208
- Normal: { type: 'paragraph', name: 'Normal', qFormat: true, para: { indent: { firstLineChars: 200 } } },
209
- Title: {
210
- type: 'paragraph', name: 'Title', basedOn: 'Normal', next: 'Normal', qFormat: true,
211
- run: { font: 'heading', sizePt: '三号', bold: true },
212
- para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { beforePt: 17, afterPt: 17 }, keepNext: true, outlineLevel: 0 },
213
- },
214
- Abstract: {
215
- type: 'paragraph', name: '摘要', basedOn: 'Normal',
216
- run: { sizePt: '五号', font: 'quote' },
217
- para: { indent: { firstLineChars: 200 } },
218
- },
219
- Heading1: {
220
- type: 'paragraph', name: 'heading 1', basedOn: 'Normal', next: 'Normal', qFormat: true,
221
- run: { font: 'heading', sizePt: '四号', bold: true },
222
- para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 13, afterPt: 13 }, keepNext: true, outlineLevel: 0 },
223
- },
224
- Heading2: {
225
- type: 'paragraph', name: 'heading 2', basedOn: 'Normal', next: 'Normal', qFormat: true,
226
- run: { font: 'heading', sizePt: '小四', bold: true },
227
- para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 13, afterPt: 6 }, keepNext: true, outlineLevel: 1 },
228
- },
229
- Caption: {
230
- type: 'paragraph', name: 'caption', basedOn: 'Normal', qFormat: true,
231
- run: { font: 'caption', sizePt: '五号', bold: true },
232
- para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { beforePt: 6, afterPt: 6 } },
233
- },
234
- Quote: {
235
- type: 'paragraph', name: 'Quote', basedOn: 'Normal', qFormat: true,
236
- run: { font: 'quote' },
237
- para: { indent: { leftChars: 200, firstLineChars: 0 } },
238
- },
239
- Header: { type: 'paragraph', name: 'header', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { sizePt: '小五' } },
240
- Footer: { type: 'paragraph', name: 'footer', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { sizePt: '小五' } },
241
- },
242
- };
243
- }
244
-
245
- export const PRESETS = {
246
- 办公标准: presetOffice,
247
- 公文: presetGongwen,
248
- 学术论文: presetAcademic,
249
- };
250
-
251
- /** token 校验:字段闭合 —— 未知字段直接报错,防 schema 野蛮生长 */
252
- export const STYLE_KEYS = new Set(['type', 'name', 'basedOn', 'next', 'link', 'qFormat', 'uiPriority', 'run', 'para', 'extraXml']);
253
- export const RUN_KEYS = new Set(['font', 'sizePt', 'bold', 'italic', 'color', 'underline', 'strike', 'caps',
254
- 'smallCaps', 'vertAlign', 'em', 'kernPt', 'spacingTwip', 'highlight']);
255
- export const PARA_KEYS = new Set(['align', 'outlineLevel', 'indent', 'spacing', 'keepNext', 'keepLines',
256
- 'pageBreakBefore', 'widowControl', 'contextualSpacing', 'borders', 'shading', 'tabs', 'cjk', 'list']);
257
-
258
- /**
259
- * 行距规则的合法值。
260
- *
261
- * ⚠️ 单独校验它是因为**写错这个字段不会静默失效,会静默生效成荒谬值**:
262
- * `lineRule` 只有 'multiple' 时 `line` 才是倍数,其余按磅算。写 `'auto'`
263
- * (CSS/OOXML 里都存在这个词,很容易顺手打出来)会落进磅值分支,
264
- * `line: 360` 本意「1.5 倍」就变成每行 360 磅 = 5 英寸,正文整个被挤出页面。
265
- * 只查键名的闭合 schema 挡不住这种,得查值。
266
- */
267
- const LINE_RULES = new Set(['multiple', 'exact', 'atLeast']);
268
-
269
- /** 字号:数字直接用,字符串必须是字号名 */
270
- function badSize(v) {
271
- return typeof v === 'string' && !(v in ZIHAO);
272
- }
273
-
274
- /**
275
- * indent 里 firstLine* 和 hanging* 互斥。OOXML 的 w:ind 里它们是**同一个属性的
276
- * 正负两面**(悬挂 = 负的首行缩进),同时给的话哪个生效随渲染器不同——LO 实测
277
- * firstLine 覆盖 hanging,ECMA-376 又说 hanging 优先。写 `firstLineChars: 0` +
278
- * `hangingChars` 的人是想"清掉继承的首行缩进再悬挂",但悬挂本身就含这层意思,
279
- * 多写那个 0 反而把悬挂静默打掉(agent 实踩:两轮 build 白跑,2026-08-19 上报)。
280
- * 跟 lineRule:'auto' 同族:写了不报错但生效成别的东西,必须在校验层拦。
281
- */
282
- export function indentConflict(ind) {
283
- if (!ind || typeof ind !== 'object') return null;
284
- const fl = ind.firstLineChars ?? ind.firstLineTwip;
285
- const hg = ind.hangingChars ?? ind.hangingTwip;
286
- if (fl == null || hg == null) return null;
287
- return 'indent 的 firstLine* 和 hanging* 互斥(同一个属性的正负两面,同时给会静默覆盖悬挂)。'
288
- + '要悬挂缩进只写 hanging*——悬挂天然含「首行回到左缘」,不用再补 firstLineChars: 0';
289
- }
290
-
291
- export function validateTokens(tok) {
292
- const errs = [];
293
- if (tok.v !== 1) errs.push('v must be 1');
294
- const checkSpacing = (where, sp) => {
295
- if (!sp) return;
296
- if (sp.lineRule != null && !LINE_RULES.has(sp.lineRule)) {
297
- errs.push(`${where}.spacing.lineRule: '${sp.lineRule}' 不是合法值,只有 ${[...LINE_RULES].join(' / ')}`
298
- + "(multiple 时 line 是倍数如 1.5;exact/atLeast 时 line 是磅)");
299
- }
300
- if (sp.line != null && sp.lineRule === 'multiple' && sp.line > 10) {
301
- errs.push(`${where}.spacing: lineRule=multiple 时 line 是**倍数**,${sp.line} 倍行距几乎肯定是把磅值写这儿了`);
302
- }
303
- };
304
- for (const [id, st] of Object.entries(tok.styles ?? {})) {
305
- for (const k of Object.keys(st)) if (!STYLE_KEYS.has(k)) errs.push(`styles.${id}: unknown key ${k}`);
306
- for (const k of Object.keys(st.run ?? {})) if (!RUN_KEYS.has(k)) errs.push(`styles.${id}.run: unknown key ${k}`);
307
- for (const k of Object.keys(st.para ?? {})) if (!PARA_KEYS.has(k)) errs.push(`styles.${id}.para: unknown key ${k}`);
308
- if (st.run?.font && typeof st.run.font === 'string' && !tok.fonts?.[st.run.font]) {
309
- errs.push(`styles.${id}.run.font: no such font slot '${st.run.font}'`);
310
- }
311
- if (badSize(st.run?.sizePt)) errs.push(`styles.${id}.run.sizePt: unknown 字号 ${st.run.sizePt}`);
312
- if (st.basedOn && !tok.styles[st.basedOn]) errs.push(`styles.${id}.basedOn: no such style '${st.basedOn}'`);
313
- checkSpacing(`styles.${id}.para`, st.para?.spacing);
314
- const indErr = indentConflict(st.para?.indent);
315
- if (indErr) errs.push(`styles.${id}.para.indent: ${indErr}`);
316
- }
317
- if (badSize(tok.base?.sizePt)) errs.push(`base.sizePt: unknown 字号 ${tok.base.sizePt}`);
318
- checkSpacing('base', tok.base?.spacing);
319
- // 自动编号(2026-08-18):定义本身的校验在 numbering.js,这里再钉一条**引用完整性**
320
- // —— 引用一个不存在的编号名以前会产出悬空 numId(Word 打开可能直接报文档损坏),
321
- // 这正是"闭合 schema 只查键名不查值"那类漏的延续。
322
- errs.push(...validateNumbering(tok.numbering));
323
- const numNames = new Set(Object.keys(tok.numbering ?? {}));
324
- const checkList = (where, list) => {
325
- if (!list) return;
326
- const name = typeof list === 'string' ? list : list.name;
327
- if (!name) errs.push(`${where}.list: 要写 { name: '编号名', ilvl?: N } 或直接写编号名`);
328
- else if (!numNames.has(name)) {
329
- errs.push(`${where}.list: 没有名为 '${name}' 的编号定义`
330
- + (numNames.size ? `,现有:${[...numNames].join(' / ')}` : '(tokens.numbering 是空的)'));
331
- }
332
- };
333
- for (const [id, st] of Object.entries(tok.styles ?? {})) checkList(`styles.${id}.para`, st.para?.list);
334
- return errs;
335
- }
1
+ /**
2
+ * tokens.js — docx 形态的「真相源」:token JSON schema + 三个内置词典条目。
3
+ *
4
+ * 原则(已拍板):
5
+ * - token JSON 是权威,styles.xml/docDefaults 是构建产物;
6
+ * - 风格名开放(agent 可以起新名字调 token),schema 闭合(字段全集在
7
+ * 这里,build 层只认识这些字段,不接受任意 XML 注入——外来文档的
8
+ * 未建模部分走 extraXml/rawStyles 透传,不走 token 字段扩张)。
9
+ *
10
+ * ── schema v1 ────────────────────────────────────────────────
11
+ * {
12
+ * v: 1,
13
+ * provenance: 'nodesign' | 'imported', // imported = 外来 docx dump 出来的
14
+ * page: {
15
+ * size: 'A4'|'Letter'|{wTwip,hTwip}, landscape?: bool,
16
+ * marginsTwip: {top,bottom,left,right,header,footer,gutter},
17
+ * docGrid?: {type:'lines'|'linesAndChars', linePitchTwip, charSpace?}|null,
18
+ * },
19
+ * lang: {latin:'en-US', eastAsia:'zh-CN'},
20
+ * fonts: { // 具名字体槽;style.run.font 用名字引用
21
+ * body:{eastAsia,ascii,hAnsi?,cs?}, heading:{...}, quote:{...}, mono:{...}, ...自定义槽
22
+ * },
23
+ * base: { // docDefaults
24
+ * sizePt: number|字号名, color:'RRGGBB',
25
+ * font:'body'|{...}, // 引用字体槽
26
+ * spacing?: {line,lineRule,beforePt,afterPt},
27
+ * cjk: {kinsoku,autoSpaceDE,autoSpaceDN,adjustRightInd,snapToGrid,overflowPunct,wordWrap},
28
+ * },
29
+ * styles: { [styleId]: Style },
30
+ * numbering?: { [numName]: {levels:[{fmt,text,indentTwip,hangingTwip,alignedAt?}]} },
31
+ * rawStyles?: [xmlString], // dump 时装不进模型的整条 w:style 原文
32
+ * }
33
+ *
34
+ * Style = {
35
+ * type:'paragraph'|'character'|'table',
36
+ * name: string, // Word UI 名('heading 1' 等内置名有魔法含义)
37
+ * basedOn?, next?, link?, qFormat?: bool, uiPriority?: number,
38
+ * run?: {
39
+ * font?:'body'|{eastAsia?,ascii?,hAnsi?,cs?}, sizePt?: number|字号名,
40
+ * bold?, italic?, color?, underline?: true|'single'|'double'|...,
41
+ * strike?, caps?, smallCaps?, vertAlign?:'superscript'|'subscript',
42
+ * em?:'dot'|'comma'|'circle'|'underDot', // 着重号
43
+ * kernPt?: number, spacingTwip?: number, highlight?: string,
44
+ * },
45
+ * para?: {
46
+ * align?:'left'|'center'|'right'|'both'|'distribute',
47
+ * outlineLevel?: 0-8,
48
+ * indent?: {firstLineChars?, firstLineTwip?, hangingChars?, hangingTwip?,
49
+ * leftChars?, leftTwip?, rightChars?, rightTwip?},
50
+ * spacing?: {beforePt?, afterPt?, beforeLines?, afterLines?,
51
+ * line?: number, lineRule?:'multiple'|'exact'|'atLeast'},
52
+ * // lineRule=multiple 时 line 是倍数(1.5);exact/atLeast 时是磅
53
+ * keepNext?, keepLines?, pageBreakBefore?, widowControl?, contextualSpacing?,
54
+ * borders?: {top?,bottom?,left?,right?: {style,sizePt8?,color,spacePt?}},
55
+ * shading?: 'RRGGBB',
56
+ * tabs?: [{pos:twip, val:'left'|'center'|'right'|'decimal', leader?}],
57
+ * cjk?: 覆盖 base.cjk 的同名开关,
58
+ * },
59
+ * extraXml?: {pPr?: string, rPr?: string}, // dump 透传,build 时原样并入
60
+ * }
61
+ */
62
+
63
+ import { ZIHAO } from './units.js';
64
+ import { validateNumbering } from './numbering.js';
65
+
66
+ /** 词典条目一:办公标准(素颜 Word)——中文 Word 默认观感 */
67
+ export function presetOffice() {
68
+ return {
69
+ v: 1,
70
+ provenance: 'nodesign',
71
+ page: {
72
+ size: 'A4',
73
+ marginsTwip: { top: 1440, bottom: 1440, left: 1800, right: 1800, header: 851, footer: 992, gutter: 0 },
74
+ },
75
+ lang: { latin: 'en-US', eastAsia: 'zh-CN' },
76
+ fonts: {
77
+ body: { eastAsia: '宋体', ascii: 'Calibri' },
78
+ heading: { eastAsia: '黑体', ascii: 'Arial' },
79
+ quote: { eastAsia: '楷体', ascii: 'Calibri' },
80
+ mono: { eastAsia: '宋体', ascii: 'Courier New' },
81
+ },
82
+ base: {
83
+ font: 'body', sizePt: '小四', color: '000000',
84
+ spacing: { line: 1.3, lineRule: 'multiple' },
85
+ cjk: { kinsoku: true, autoSpaceDE: true, autoSpaceDN: true, adjustRightInd: true, snapToGrid: true },
86
+ },
87
+ styles: {
88
+ Normal: { type: 'paragraph', name: 'Normal', qFormat: true, para: { indent: { firstLineChars: 200 } } },
89
+ Title: {
90
+ type: 'paragraph', name: 'Title', basedOn: 'Normal', next: 'Normal', qFormat: true,
91
+ run: { font: 'heading', sizePt: '二号', bold: true },
92
+ para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { beforePt: 12, afterPt: 18 }, keepNext: true, outlineLevel: 0 },
93
+ },
94
+ Heading1: {
95
+ type: 'paragraph', name: 'heading 1', basedOn: 'Normal', next: 'Normal', qFormat: true,
96
+ run: { font: 'heading', sizePt: '三号', bold: true },
97
+ para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 13, afterPt: 13 }, keepNext: true, outlineLevel: 0 },
98
+ },
99
+ Heading2: {
100
+ type: 'paragraph', name: 'heading 2', basedOn: 'Normal', next: 'Normal', qFormat: true,
101
+ run: { font: 'heading', sizePt: '四号', bold: true },
102
+ para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 10, afterPt: 10 }, keepNext: true, outlineLevel: 1 },
103
+ },
104
+ Heading3: {
105
+ type: 'paragraph', name: 'heading 3', basedOn: 'Normal', next: 'Normal', qFormat: true,
106
+ run: { font: 'heading', sizePt: '小四', bold: true },
107
+ para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 8, afterPt: 8 }, keepNext: true, outlineLevel: 2 },
108
+ },
109
+ Quote: {
110
+ type: 'paragraph', name: 'Quote', basedOn: 'Normal', qFormat: true,
111
+ run: { font: 'quote', color: '404040' },
112
+ para: { indent: { leftChars: 200, firstLineChars: 0 } },
113
+ },
114
+ Header: { type: 'paragraph', name: 'header', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { sizePt: '小五' } },
115
+ Footer: { type: 'paragraph', name: 'footer', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { sizePt: '小五' } },
116
+ },
117
+ };
118
+ }
119
+
120
+ /** 词典条目二:公文体(GB/T 9704-2012 版式硬指标) */
121
+ export function presetGongwen() {
122
+ return {
123
+ v: 1,
124
+ provenance: 'nodesign',
125
+ page: {
126
+ size: 'A4',
127
+ // GB/T 9704: 上37 下35 左28 右26 (mm)
128
+ marginsTwip: { top: 2098, bottom: 1984, left: 1588, right: 1474, header: 851, footer: 992, gutter: 0 },
129
+ docGrid: { type: 'lines', linePitchTwip: 560 }, // 固定 28pt 行距的格
130
+ },
131
+ lang: { latin: 'zh-CN', eastAsia: 'zh-CN' },
132
+ fonts: {
133
+ body: { eastAsia: '仿宋_GB2312', ascii: 'Times New Roman' },
134
+ biaosong: { eastAsia: '方正小标宋简体', ascii: 'Times New Roman' },
135
+ hei: { eastAsia: '黑体', ascii: 'Times New Roman' },
136
+ kai: { eastAsia: '楷体_GB2312', ascii: 'Times New Roman' },
137
+ song: { eastAsia: '宋体', ascii: 'Times New Roman' },
138
+ },
139
+ base: {
140
+ font: 'body', sizePt: '三号', color: '000000',
141
+ spacing: { line: 28, lineRule: 'exact' },
142
+ cjk: { kinsoku: true, autoSpaceDE: false, autoSpaceDN: false, adjustRightInd: true, snapToGrid: true },
143
+ },
144
+ styles: {
145
+ Normal: { type: 'paragraph', name: 'Normal', qFormat: true, para: { indent: { firstLineChars: 200 } } },
146
+ // 发文机关标志(红头)
147
+ Hongtou: {
148
+ type: 'paragraph', name: '红头', basedOn: 'Normal', next: 'Normal',
149
+ run: { font: 'biaosong', sizePt: 32, color: 'FF0000', bold: false },
150
+ para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { line: 40, lineRule: 'exact' } },
151
+ },
152
+ Fawenzihao: {
153
+ type: 'paragraph', name: '发文字号', basedOn: 'Normal', next: 'Normal',
154
+ para: { align: 'center', indent: { firstLineChars: 0 } },
155
+ },
156
+ GwTitle: {
157
+ type: 'paragraph', name: '公文标题', basedOn: 'Normal', next: 'Normal', qFormat: true,
158
+ // 标题前后各空一行(28pt)。用 pt 不用 beforeLines:lab/02 已证 LO 对
159
+ // 字符/行单位的兼容不可信,*Lines 同族,公文版式是硬指标不赌。
160
+ run: { font: 'biaosong', sizePt: '二号' },
161
+ para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { beforePt: 28, afterPt: 28 }, keepNext: true, outlineLevel: 0 },
162
+ },
163
+ GwH1: { // 一、 黑体
164
+ type: 'paragraph', name: '公文一级', basedOn: 'Normal', next: 'Normal', qFormat: true,
165
+ run: { font: 'hei' }, para: { keepNext: true, outlineLevel: 0 },
166
+ },
167
+ GwH2: { // (一) 楷体
168
+ type: 'paragraph', name: '公文二级', basedOn: 'Normal', next: 'Normal', qFormat: true,
169
+ run: { font: 'kai' }, para: { keepNext: true, outlineLevel: 1 },
170
+ },
171
+ GwH3: { // 1. 仿宋加粗
172
+ type: 'paragraph', name: '公文三级', basedOn: 'Normal', next: 'Normal', qFormat: true,
173
+ run: { bold: true }, para: { keepNext: true, outlineLevel: 2 },
174
+ },
175
+ Chaosong: {
176
+ type: 'paragraph', name: '抄送', basedOn: 'Normal',
177
+ run: { sizePt: '四号', font: 'body' }, para: { indent: { firstLineChars: 0 } },
178
+ },
179
+ Footer: { type: 'paragraph', name: 'footer', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { font: 'song', sizePt: '四号' } },
180
+ Header: { type: 'paragraph', name: 'header', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { font: 'song', sizePt: '小五' } },
181
+ },
182
+ };
183
+ }
184
+
185
+ /** 词典条目三:学术论文体(中文期刊/学位论文常规) */
186
+ export function presetAcademic() {
187
+ return {
188
+ v: 1,
189
+ provenance: 'nodesign',
190
+ page: {
191
+ size: 'A4',
192
+ marginsTwip: { top: 1700, bottom: 1700, left: 1800, right: 1800, header: 851, footer: 992, gutter: 0 },
193
+ },
194
+ lang: { latin: 'en-US', eastAsia: 'zh-CN' },
195
+ fonts: {
196
+ body: { eastAsia: '宋体', ascii: 'Times New Roman' },
197
+ heading: { eastAsia: '黑体', ascii: 'Times New Roman' },
198
+ quote: { eastAsia: '楷体', ascii: 'Times New Roman' },
199
+ caption: { eastAsia: '宋体', ascii: 'Times New Roman' },
200
+ mono: { eastAsia: '宋体', ascii: 'Courier New' },
201
+ },
202
+ base: {
203
+ font: 'body', sizePt: '小四', color: '000000',
204
+ spacing: { line: 1.5, lineRule: 'multiple' },
205
+ cjk: { kinsoku: true, autoSpaceDE: true, autoSpaceDN: true, adjustRightInd: true, snapToGrid: true },
206
+ },
207
+ styles: {
208
+ Normal: { type: 'paragraph', name: 'Normal', qFormat: true, para: { indent: { firstLineChars: 200 } } },
209
+ Title: {
210
+ type: 'paragraph', name: 'Title', basedOn: 'Normal', next: 'Normal', qFormat: true,
211
+ run: { font: 'heading', sizePt: '三号', bold: true },
212
+ para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { beforePt: 17, afterPt: 17 }, keepNext: true, outlineLevel: 0 },
213
+ },
214
+ Abstract: {
215
+ type: 'paragraph', name: '摘要', basedOn: 'Normal',
216
+ run: { sizePt: '五号', font: 'quote' },
217
+ para: { indent: { firstLineChars: 200 } },
218
+ },
219
+ Heading1: {
220
+ type: 'paragraph', name: 'heading 1', basedOn: 'Normal', next: 'Normal', qFormat: true,
221
+ run: { font: 'heading', sizePt: '四号', bold: true },
222
+ para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 13, afterPt: 13 }, keepNext: true, outlineLevel: 0 },
223
+ },
224
+ Heading2: {
225
+ type: 'paragraph', name: 'heading 2', basedOn: 'Normal', next: 'Normal', qFormat: true,
226
+ run: { font: 'heading', sizePt: '小四', bold: true },
227
+ para: { indent: { firstLineChars: 0 }, spacing: { beforePt: 13, afterPt: 6 }, keepNext: true, outlineLevel: 1 },
228
+ },
229
+ Caption: {
230
+ type: 'paragraph', name: 'caption', basedOn: 'Normal', qFormat: true,
231
+ run: { font: 'caption', sizePt: '五号', bold: true },
232
+ para: { align: 'center', indent: { firstLineChars: 0 }, spacing: { beforePt: 6, afterPt: 6 } },
233
+ },
234
+ Quote: {
235
+ type: 'paragraph', name: 'Quote', basedOn: 'Normal', qFormat: true,
236
+ run: { font: 'quote' },
237
+ para: { indent: { leftChars: 200, firstLineChars: 0 } },
238
+ },
239
+ Header: { type: 'paragraph', name: 'header', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { sizePt: '小五' } },
240
+ Footer: { type: 'paragraph', name: 'footer', basedOn: 'Normal', para: { indent: { firstLineChars: 0 }, align: 'center' }, run: { sizePt: '小五' } },
241
+ },
242
+ };
243
+ }
244
+
245
+ export const PRESETS = {
246
+ 办公标准: presetOffice,
247
+ 公文: presetGongwen,
248
+ 学术论文: presetAcademic,
249
+ };
250
+
251
+ /** token 校验:字段闭合 —— 未知字段直接报错,防 schema 野蛮生长 */
252
+ export const STYLE_KEYS = new Set(['type', 'name', 'basedOn', 'next', 'link', 'qFormat', 'uiPriority', 'run', 'para', 'extraXml']);
253
+ export const RUN_KEYS = new Set(['font', 'sizePt', 'bold', 'italic', 'color', 'underline', 'strike', 'caps',
254
+ 'smallCaps', 'vertAlign', 'em', 'kernPt', 'spacingTwip', 'highlight']);
255
+ export const PARA_KEYS = new Set(['align', 'outlineLevel', 'indent', 'spacing', 'keepNext', 'keepLines',
256
+ 'pageBreakBefore', 'widowControl', 'contextualSpacing', 'borders', 'shading', 'tabs', 'cjk', 'list']);
257
+
258
+ /**
259
+ * 行距规则的合法值。
260
+ *
261
+ * ⚠️ 单独校验它是因为**写错这个字段不会静默失效,会静默生效成荒谬值**:
262
+ * `lineRule` 只有 'multiple' 时 `line` 才是倍数,其余按磅算。写 `'auto'`
263
+ * (CSS/OOXML 里都存在这个词,很容易顺手打出来)会落进磅值分支,
264
+ * `line: 360` 本意「1.5 倍」就变成每行 360 磅 = 5 英寸,正文整个被挤出页面。
265
+ * 只查键名的闭合 schema 挡不住这种,得查值。
266
+ */
267
+ const LINE_RULES = new Set(['multiple', 'exact', 'atLeast']);
268
+
269
+ /** 字号:数字直接用,字符串必须是字号名 */
270
+ function badSize(v) {
271
+ return typeof v === 'string' && !(v in ZIHAO);
272
+ }
273
+
274
+ /**
275
+ * indent 里 firstLine* 和 hanging* 互斥。OOXML 的 w:ind 里它们是**同一个属性的
276
+ * 正负两面**(悬挂 = 负的首行缩进),同时给的话哪个生效随渲染器不同——LO 实测
277
+ * firstLine 覆盖 hanging,ECMA-376 又说 hanging 优先。写 `firstLineChars: 0` +
278
+ * `hangingChars` 的人是想"清掉继承的首行缩进再悬挂",但悬挂本身就含这层意思,
279
+ * 多写那个 0 反而把悬挂静默打掉(agent 实踩:两轮 build 白跑,2026-08-19 上报)。
280
+ * 跟 lineRule:'auto' 同族:写了不报错但生效成别的东西,必须在校验层拦。
281
+ */
282
+ export function indentConflict(ind) {
283
+ if (!ind || typeof ind !== 'object') return null;
284
+ const fl = ind.firstLineChars ?? ind.firstLineTwip;
285
+ const hg = ind.hangingChars ?? ind.hangingTwip;
286
+ if (fl == null || hg == null) return null;
287
+ return 'indent 的 firstLine* 和 hanging* 互斥(同一个属性的正负两面,同时给会静默覆盖悬挂)。'
288
+ + '要悬挂缩进只写 hanging*——悬挂天然含「首行回到左缘」,不用再补 firstLineChars: 0';
289
+ }
290
+
291
+ export function validateTokens(tok) {
292
+ const errs = [];
293
+ if (tok.v !== 1) errs.push('v must be 1');
294
+ const checkSpacing = (where, sp) => {
295
+ if (!sp) return;
296
+ if (sp.lineRule != null && !LINE_RULES.has(sp.lineRule)) {
297
+ errs.push(`${where}.spacing.lineRule: '${sp.lineRule}' 不是合法值,只有 ${[...LINE_RULES].join(' / ')}`
298
+ + "(multiple 时 line 是倍数如 1.5;exact/atLeast 时 line 是磅)");
299
+ }
300
+ if (sp.line != null && sp.lineRule === 'multiple' && sp.line > 10) {
301
+ errs.push(`${where}.spacing: lineRule=multiple 时 line 是**倍数**,${sp.line} 倍行距几乎肯定是把磅值写这儿了`);
302
+ }
303
+ };
304
+ for (const [id, st] of Object.entries(tok.styles ?? {})) {
305
+ for (const k of Object.keys(st)) if (!STYLE_KEYS.has(k)) errs.push(`styles.${id}: unknown key ${k}`);
306
+ for (const k of Object.keys(st.run ?? {})) if (!RUN_KEYS.has(k)) errs.push(`styles.${id}.run: unknown key ${k}`);
307
+ for (const k of Object.keys(st.para ?? {})) if (!PARA_KEYS.has(k)) errs.push(`styles.${id}.para: unknown key ${k}`);
308
+ if (st.run?.font && typeof st.run.font === 'string' && !tok.fonts?.[st.run.font]) {
309
+ errs.push(`styles.${id}.run.font: no such font slot '${st.run.font}'`);
310
+ }
311
+ if (badSize(st.run?.sizePt)) errs.push(`styles.${id}.run.sizePt: unknown 字号 ${st.run.sizePt}`);
312
+ if (st.basedOn && !tok.styles[st.basedOn]) errs.push(`styles.${id}.basedOn: no such style '${st.basedOn}'`);
313
+ checkSpacing(`styles.${id}.para`, st.para?.spacing);
314
+ const indErr = indentConflict(st.para?.indent);
315
+ if (indErr) errs.push(`styles.${id}.para.indent: ${indErr}`);
316
+ }
317
+ if (badSize(tok.base?.sizePt)) errs.push(`base.sizePt: unknown 字号 ${tok.base.sizePt}`);
318
+ checkSpacing('base', tok.base?.spacing);
319
+ // 自动编号(2026-08-18):定义本身的校验在 numbering.js,这里再钉一条**引用完整性**
320
+ // —— 引用一个不存在的编号名以前会产出悬空 numId(Word 打开可能直接报文档损坏),
321
+ // 这正是"闭合 schema 只查键名不查值"那类漏的延续。
322
+ errs.push(...validateNumbering(tok.numbering));
323
+ const numNames = new Set(Object.keys(tok.numbering ?? {}));
324
+ const checkList = (where, list) => {
325
+ if (!list) return;
326
+ const name = typeof list === 'string' ? list : list.name;
327
+ if (!name) errs.push(`${where}.list: 要写 { name: '编号名', ilvl?: N } 或直接写编号名`);
328
+ else if (!numNames.has(name)) {
329
+ errs.push(`${where}.list: 没有名为 '${name}' 的编号定义`
330
+ + (numNames.size ? `,现有:${[...numNames].join(' / ')}` : '(tokens.numbering 是空的)'));
331
+ }
332
+ };
333
+ for (const [id, st] of Object.entries(tok.styles ?? {})) checkList(`styles.${id}.para`, st.para?.list);
334
+ return errs;
335
+ }