dsh-pdf-workbench 0.0.0-stage → 1.3.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.
Files changed (278) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +192 -3
  3. package/assets/screenshot-bilingual.jpg +0 -0
  4. package/assets/screenshot-hero.jpg +0 -0
  5. package/assets/screenshot-overlay.jpg +0 -0
  6. package/assets/screenshot-qa.jpg +0 -0
  7. package/assets/screenshot-reader.jpg +0 -0
  8. package/assets/screenshot-shelf.jpg +0 -0
  9. package/client.js +14126 -0
  10. package/cordis.patch.yml +13 -0
  11. package/docs/DETAIL.md +69 -0
  12. package/docs/READER-REWRITE.md +95 -0
  13. package/docs/RELEASE.md +110 -0
  14. package/docs/ROADMAP.md +172 -0
  15. package/docs/SIDE-PREVIEW.md +271 -0
  16. package/docs/TESTING.md +109 -0
  17. package/docs/TRANSLATION.md +167 -0
  18. package/icon.svg +6 -0
  19. package/index.js +210 -0
  20. package/locale/en.json +7 -0
  21. package/locale/zh.json +7 -0
  22. package/package.json +64 -6
  23. package/samples/manifest.json +52 -0
  24. package/samples/test/serre_c01_p2-3.pdf +0 -0
  25. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c00_/345/211/215/347/275/256_/345/260/201/351/235/242/347/233/256/345/275/225.pdf +0 -0
  26. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c01_/347/254/2540/347/253/240_/345/257/274/350/257/273.pdf +0 -0
  27. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c02_/347/254/2541/347/253/240_/345/217/244/351/242/230_/345/255/231/345/255/220/347/256/227/347/273/217/345/210/260/347/247/246/344/271/235/351/237/266.pdf +0 -0
  28. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c03_/347/254/2542/347/253/240_/346/225/264/346/225/260/347/211/210/346/234/254.pdf +0 -0
  29. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c04_/347/254/2543/347/253/240_/347/216/257/350/256/272/347/211/210/346/234/254.pdf +0 -0
  30. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c05_/347/254/2544/347/253/240_/345/244/232/351/241/271/345/274/217/347/211/210/346/234/254.pdf +0 -0
  31. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c06_/347/254/2545/347/253/240_/347/276/244/350/256/272/347/211/210/346/234/254.pdf +0 -0
  32. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c07_/347/254/2546/347/253/240_/347/273/237/344/270/200/347/273/223/346/236/204.pdf +0 -0
  33. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c08_/347/254/2547/347/253/240_/345/233/236/346/234/233.pdf +0 -0
  34. package/samples//345/210/206/347/211/207//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206//344/270/255/345/233/275/345/211/251/344/275/231/345/256/232/347/220/206_c90_/351/231/204/345/275/225_/346/225/231/346/235/220/351/241/265/347/240/201/347/254/246/345/217/267/350/241/250/350/207/252/346/265/213.pdf +0 -0
  35. package/screenshots.json +8 -0
  36. package/src/client/viewer.js +80 -0
  37. package/src/host/annotations.js +145 -0
  38. package/src/host/artifacts.js +317 -0
  39. package/src/host/blocks.js +44 -0
  40. package/src/host/config.js +112 -0
  41. package/src/host/digest.js +156 -0
  42. package/src/host/environment.js +42 -0
  43. package/src/host/http.js +124 -0
  44. package/src/host/index-source.js +488 -0
  45. package/src/host/libraries.js +221 -0
  46. package/src/host/links.js +201 -0
  47. package/src/host/pdf-source.js +122 -0
  48. package/src/host/pending.js +166 -0
  49. package/src/host/pressure.js +137 -0
  50. package/src/host/purge.js +100 -0
  51. package/src/host/render.js +43 -0
  52. package/src/host/routes.js +1778 -0
  53. package/src/host/shelf-groups.js +109 -0
  54. package/src/host/state.js +128 -0
  55. package/src/host/toolkit.js +54 -0
  56. package/src/host/translate-blocks.js +132 -0
  57. package/src/host/translate-job.js +316 -0
  58. package/src/host/translate.js +94 -0
  59. package/src/host/update.js +134 -0
  60. package/tools/AGENTS.md +33 -0
  61. package/tools/README.md +138 -0
  62. package/tools/auto_chapters.py +122 -0
  63. package/tools/dsfile.mjs +198 -0
  64. package/tools/export_chapter.py +108 -0
  65. package/tools/export_page.py +91 -0
  66. package/tools/md2overlay.py +200 -0
  67. package/tools/md2tex.py +422 -0
  68. package/tools/pdf_blocks.py +110 -0
  69. package/tools/pdf_fontsize.py +20 -0
  70. package/tools/pdf_merge.py +15 -0
  71. package/tools/pdf_pagesizes.py +8 -0
  72. package/tools/pdf_raster.py +21 -0
  73. package/tools/pdfkit.py +896 -0
  74. package/tools/realign_overlay.py +248 -0
  75. package/tools/rebuild_overlay.py +55 -0
  76. package/tools/rename_chapters.py +93 -0
  77. package/tools//350/247/204/350/214/203/00_/345/205/261/345/220/214/347/272/246/345/256/232.md +142 -0
  78. package/tools//350/247/204/350/214/203/01_/346/226/207/346/234/254/346/217/220/345/217/226.md +204 -0
  79. package/tools//350/247/204/350/214/203/02_/347/255/224/347/226/221.md +61 -0
  80. package/tools//350/247/204/350/214/203/03_/344/271/240/351/242/230/350/247/243/347/255/224.md +77 -0
  81. package/tools//350/247/204/350/214/203/api/346/217/220/347/244/272/350/257/215_/346/217/220/345/217/226.md +11 -0
  82. package/vendor/cmaps/78-EUC-H.bcmap +0 -0
  83. package/vendor/cmaps/78-EUC-V.bcmap +0 -0
  84. package/vendor/cmaps/78-H.bcmap +0 -0
  85. package/vendor/cmaps/78-RKSJ-H.bcmap +0 -0
  86. package/vendor/cmaps/78-RKSJ-V.bcmap +0 -0
  87. package/vendor/cmaps/78-V.bcmap +0 -0
  88. package/vendor/cmaps/78ms-RKSJ-H.bcmap +0 -0
  89. package/vendor/cmaps/78ms-RKSJ-V.bcmap +0 -0
  90. package/vendor/cmaps/83pv-RKSJ-H.bcmap +0 -0
  91. package/vendor/cmaps/90ms-RKSJ-H.bcmap +0 -0
  92. package/vendor/cmaps/90ms-RKSJ-V.bcmap +0 -0
  93. package/vendor/cmaps/90msp-RKSJ-H.bcmap +0 -0
  94. package/vendor/cmaps/90msp-RKSJ-V.bcmap +0 -0
  95. package/vendor/cmaps/90pv-RKSJ-H.bcmap +0 -0
  96. package/vendor/cmaps/90pv-RKSJ-V.bcmap +0 -0
  97. package/vendor/cmaps/Add-H.bcmap +0 -0
  98. package/vendor/cmaps/Add-RKSJ-H.bcmap +0 -0
  99. package/vendor/cmaps/Add-RKSJ-V.bcmap +0 -0
  100. package/vendor/cmaps/Add-V.bcmap +0 -0
  101. package/vendor/cmaps/Adobe-CNS1-0.bcmap +0 -0
  102. package/vendor/cmaps/Adobe-CNS1-1.bcmap +0 -0
  103. package/vendor/cmaps/Adobe-CNS1-2.bcmap +0 -0
  104. package/vendor/cmaps/Adobe-CNS1-3.bcmap +0 -0
  105. package/vendor/cmaps/Adobe-CNS1-4.bcmap +0 -0
  106. package/vendor/cmaps/Adobe-CNS1-5.bcmap +0 -0
  107. package/vendor/cmaps/Adobe-CNS1-6.bcmap +0 -0
  108. package/vendor/cmaps/Adobe-CNS1-UCS2.bcmap +0 -0
  109. package/vendor/cmaps/Adobe-GB1-0.bcmap +0 -0
  110. package/vendor/cmaps/Adobe-GB1-1.bcmap +0 -0
  111. package/vendor/cmaps/Adobe-GB1-2.bcmap +0 -0
  112. package/vendor/cmaps/Adobe-GB1-3.bcmap +0 -0
  113. package/vendor/cmaps/Adobe-GB1-4.bcmap +0 -0
  114. package/vendor/cmaps/Adobe-GB1-5.bcmap +0 -0
  115. package/vendor/cmaps/Adobe-GB1-UCS2.bcmap +0 -0
  116. package/vendor/cmaps/Adobe-Japan1-0.bcmap +0 -0
  117. package/vendor/cmaps/Adobe-Japan1-1.bcmap +0 -0
  118. package/vendor/cmaps/Adobe-Japan1-2.bcmap +0 -0
  119. package/vendor/cmaps/Adobe-Japan1-3.bcmap +0 -0
  120. package/vendor/cmaps/Adobe-Japan1-4.bcmap +0 -0
  121. package/vendor/cmaps/Adobe-Japan1-5.bcmap +0 -0
  122. package/vendor/cmaps/Adobe-Japan1-6.bcmap +0 -0
  123. package/vendor/cmaps/Adobe-Japan1-UCS2.bcmap +0 -0
  124. package/vendor/cmaps/Adobe-Korea1-0.bcmap +0 -0
  125. package/vendor/cmaps/Adobe-Korea1-1.bcmap +0 -0
  126. package/vendor/cmaps/Adobe-Korea1-2.bcmap +0 -0
  127. package/vendor/cmaps/Adobe-Korea1-UCS2.bcmap +0 -0
  128. package/vendor/cmaps/B5-H.bcmap +0 -0
  129. package/vendor/cmaps/B5-V.bcmap +0 -0
  130. package/vendor/cmaps/B5pc-H.bcmap +0 -0
  131. package/vendor/cmaps/B5pc-V.bcmap +0 -0
  132. package/vendor/cmaps/CNS-EUC-H.bcmap +0 -0
  133. package/vendor/cmaps/CNS-EUC-V.bcmap +0 -0
  134. package/vendor/cmaps/CNS1-H.bcmap +0 -0
  135. package/vendor/cmaps/CNS1-V.bcmap +0 -0
  136. package/vendor/cmaps/CNS2-H.bcmap +0 -0
  137. package/vendor/cmaps/CNS2-V.bcmap +3 -0
  138. package/vendor/cmaps/ETHK-B5-H.bcmap +0 -0
  139. package/vendor/cmaps/ETHK-B5-V.bcmap +0 -0
  140. package/vendor/cmaps/ETen-B5-H.bcmap +0 -0
  141. package/vendor/cmaps/ETen-B5-V.bcmap +0 -0
  142. package/vendor/cmaps/ETenms-B5-H.bcmap +3 -0
  143. package/vendor/cmaps/ETenms-B5-V.bcmap +0 -0
  144. package/vendor/cmaps/EUC-H.bcmap +0 -0
  145. package/vendor/cmaps/EUC-V.bcmap +0 -0
  146. package/vendor/cmaps/Ext-H.bcmap +0 -0
  147. package/vendor/cmaps/Ext-RKSJ-H.bcmap +0 -0
  148. package/vendor/cmaps/Ext-RKSJ-V.bcmap +0 -0
  149. package/vendor/cmaps/Ext-V.bcmap +0 -0
  150. package/vendor/cmaps/GB-EUC-H.bcmap +0 -0
  151. package/vendor/cmaps/GB-EUC-V.bcmap +0 -0
  152. package/vendor/cmaps/GB-H.bcmap +4 -0
  153. package/vendor/cmaps/GB-V.bcmap +0 -0
  154. package/vendor/cmaps/GBK-EUC-H.bcmap +0 -0
  155. package/vendor/cmaps/GBK-EUC-V.bcmap +0 -0
  156. package/vendor/cmaps/GBK2K-H.bcmap +0 -0
  157. package/vendor/cmaps/GBK2K-V.bcmap +0 -0
  158. package/vendor/cmaps/GBKp-EUC-H.bcmap +0 -0
  159. package/vendor/cmaps/GBKp-EUC-V.bcmap +0 -0
  160. package/vendor/cmaps/GBT-EUC-H.bcmap +0 -0
  161. package/vendor/cmaps/GBT-EUC-V.bcmap +0 -0
  162. package/vendor/cmaps/GBT-H.bcmap +0 -0
  163. package/vendor/cmaps/GBT-V.bcmap +0 -0
  164. package/vendor/cmaps/GBTpc-EUC-H.bcmap +0 -0
  165. package/vendor/cmaps/GBTpc-EUC-V.bcmap +0 -0
  166. package/vendor/cmaps/GBpc-EUC-H.bcmap +0 -0
  167. package/vendor/cmaps/GBpc-EUC-V.bcmap +0 -0
  168. package/vendor/cmaps/H.bcmap +0 -0
  169. package/vendor/cmaps/HKdla-B5-H.bcmap +0 -0
  170. package/vendor/cmaps/HKdla-B5-V.bcmap +0 -0
  171. package/vendor/cmaps/HKdlb-B5-H.bcmap +0 -0
  172. package/vendor/cmaps/HKdlb-B5-V.bcmap +0 -0
  173. package/vendor/cmaps/HKgccs-B5-H.bcmap +0 -0
  174. package/vendor/cmaps/HKgccs-B5-V.bcmap +0 -0
  175. package/vendor/cmaps/HKm314-B5-H.bcmap +0 -0
  176. package/vendor/cmaps/HKm314-B5-V.bcmap +0 -0
  177. package/vendor/cmaps/HKm471-B5-H.bcmap +0 -0
  178. package/vendor/cmaps/HKm471-B5-V.bcmap +0 -0
  179. package/vendor/cmaps/HKscs-B5-H.bcmap +0 -0
  180. package/vendor/cmaps/HKscs-B5-V.bcmap +0 -0
  181. package/vendor/cmaps/Hankaku.bcmap +0 -0
  182. package/vendor/cmaps/Hiragana.bcmap +0 -0
  183. package/vendor/cmaps/KSC-EUC-H.bcmap +0 -0
  184. package/vendor/cmaps/KSC-EUC-V.bcmap +0 -0
  185. package/vendor/cmaps/KSC-H.bcmap +0 -0
  186. package/vendor/cmaps/KSC-Johab-H.bcmap +0 -0
  187. package/vendor/cmaps/KSC-Johab-V.bcmap +0 -0
  188. package/vendor/cmaps/KSC-V.bcmap +0 -0
  189. package/vendor/cmaps/KSCms-UHC-H.bcmap +0 -0
  190. package/vendor/cmaps/KSCms-UHC-HW-H.bcmap +0 -0
  191. package/vendor/cmaps/KSCms-UHC-HW-V.bcmap +0 -0
  192. package/vendor/cmaps/KSCms-UHC-V.bcmap +0 -0
  193. package/vendor/cmaps/KSCpc-EUC-H.bcmap +0 -0
  194. package/vendor/cmaps/KSCpc-EUC-V.bcmap +0 -0
  195. package/vendor/cmaps/Katakana.bcmap +0 -0
  196. package/vendor/cmaps/LICENSE.txt +36 -0
  197. package/vendor/cmaps/NWP-H.bcmap +0 -0
  198. package/vendor/cmaps/NWP-V.bcmap +0 -0
  199. package/vendor/cmaps/RKSJ-H.bcmap +0 -0
  200. package/vendor/cmaps/RKSJ-V.bcmap +0 -0
  201. package/vendor/cmaps/Roman.bcmap +0 -0
  202. package/vendor/cmaps/UniCNS-UCS2-H.bcmap +0 -0
  203. package/vendor/cmaps/UniCNS-UCS2-V.bcmap +0 -0
  204. package/vendor/cmaps/UniCNS-UTF16-H.bcmap +0 -0
  205. package/vendor/cmaps/UniCNS-UTF16-V.bcmap +0 -0
  206. package/vendor/cmaps/UniCNS-UTF32-H.bcmap +0 -0
  207. package/vendor/cmaps/UniCNS-UTF32-V.bcmap +0 -0
  208. package/vendor/cmaps/UniCNS-UTF8-H.bcmap +0 -0
  209. package/vendor/cmaps/UniCNS-UTF8-V.bcmap +0 -0
  210. package/vendor/cmaps/UniGB-UCS2-H.bcmap +0 -0
  211. package/vendor/cmaps/UniGB-UCS2-V.bcmap +0 -0
  212. package/vendor/cmaps/UniGB-UTF16-H.bcmap +0 -0
  213. package/vendor/cmaps/UniGB-UTF16-V.bcmap +0 -0
  214. package/vendor/cmaps/UniGB-UTF32-H.bcmap +0 -0
  215. package/vendor/cmaps/UniGB-UTF32-V.bcmap +0 -0
  216. package/vendor/cmaps/UniGB-UTF8-H.bcmap +0 -0
  217. package/vendor/cmaps/UniGB-UTF8-V.bcmap +0 -0
  218. package/vendor/cmaps/UniJIS-UCS2-H.bcmap +0 -0
  219. package/vendor/cmaps/UniJIS-UCS2-HW-H.bcmap +0 -0
  220. package/vendor/cmaps/UniJIS-UCS2-HW-V.bcmap +0 -0
  221. package/vendor/cmaps/UniJIS-UCS2-V.bcmap +0 -0
  222. package/vendor/cmaps/UniJIS-UTF16-H.bcmap +0 -0
  223. package/vendor/cmaps/UniJIS-UTF16-V.bcmap +0 -0
  224. package/vendor/cmaps/UniJIS-UTF32-H.bcmap +0 -0
  225. package/vendor/cmaps/UniJIS-UTF32-V.bcmap +0 -0
  226. package/vendor/cmaps/UniJIS-UTF8-H.bcmap +0 -0
  227. package/vendor/cmaps/UniJIS-UTF8-V.bcmap +0 -0
  228. package/vendor/cmaps/UniJIS2004-UTF16-H.bcmap +0 -0
  229. package/vendor/cmaps/UniJIS2004-UTF16-V.bcmap +0 -0
  230. package/vendor/cmaps/UniJIS2004-UTF32-H.bcmap +0 -0
  231. package/vendor/cmaps/UniJIS2004-UTF32-V.bcmap +0 -0
  232. package/vendor/cmaps/UniJIS2004-UTF8-H.bcmap +0 -0
  233. package/vendor/cmaps/UniJIS2004-UTF8-V.bcmap +0 -0
  234. package/vendor/cmaps/UniJISPro-UCS2-HW-V.bcmap +0 -0
  235. package/vendor/cmaps/UniJISPro-UCS2-V.bcmap +0 -0
  236. package/vendor/cmaps/UniJISPro-UTF8-V.bcmap +0 -0
  237. package/vendor/cmaps/UniJISX0213-UTF32-H.bcmap +0 -0
  238. package/vendor/cmaps/UniJISX0213-UTF32-V.bcmap +0 -0
  239. package/vendor/cmaps/UniJISX02132004-UTF32-H.bcmap +0 -0
  240. package/vendor/cmaps/UniJISX02132004-UTF32-V.bcmap +0 -0
  241. package/vendor/cmaps/UniKS-UCS2-H.bcmap +0 -0
  242. package/vendor/cmaps/UniKS-UCS2-V.bcmap +0 -0
  243. package/vendor/cmaps/UniKS-UTF16-H.bcmap +0 -0
  244. package/vendor/cmaps/UniKS-UTF16-V.bcmap +0 -0
  245. package/vendor/cmaps/UniKS-UTF32-H.bcmap +0 -0
  246. package/vendor/cmaps/UniKS-UTF32-V.bcmap +0 -0
  247. package/vendor/cmaps/UniKS-UTF8-H.bcmap +0 -0
  248. package/vendor/cmaps/UniKS-UTF8-V.bcmap +0 -0
  249. package/vendor/cmaps/V.bcmap +0 -0
  250. package/vendor/cmaps/WP-Symbol.bcmap +0 -0
  251. package/vendor/fonts/excalifont/Excalifont-Regular-349fac6ca4700ffec595a7150a0d1e1d.woff2 +0 -0
  252. package/vendor/fonts/excalifont/Excalifont-Regular-3f2c5db56cc93c5a6873b1361d730c16.woff2 +0 -0
  253. package/vendor/fonts/excalifont/Excalifont-Regular-41b173a47b57366892116a575a43e2b6.woff2 +0 -0
  254. package/vendor/fonts/excalifont/Excalifont-Regular-623ccf21b21ef6b3a0d87738f77eb071.woff2 +0 -0
  255. package/vendor/fonts/excalifont/Excalifont-Regular-a88b72a24fb54c9f94e3b5fdaa7481c9.woff2 +0 -0
  256. package/vendor/fonts/excalifont/Excalifont-Regular-b9dcf9d2e50a1eaf42fc664b50a3fd0d.woff2 +0 -0
  257. package/vendor/fonts/excalifont/Excalifont-Regular-be310b9bcd4f1a43f571c46df7809174.woff2 +0 -0
  258. package/vendor/fonts/excalifont/OFL.txt +98 -0
  259. package/vendor/fonts/zhhand/OFL.txt +99 -0
  260. package/vendor/fonts/zhhand/Xiaolai-Regular.ttf +0 -0
  261. package/vendor/pdf.min.mjs +29 -0
  262. package/vendor/pdf.worker.min.mjs +29 -0
  263. package/vendor/pdfjs-LICENSE.txt +177 -0
  264. package/vendor/rough/LICENSE +21 -0
  265. package/vendor/rough/rough.mjs +1 -0
  266. package/vendor/wasm/LICENSE_JBIG2 +196 -0
  267. package/vendor/wasm/LICENSE_OPENJPEG +39 -0
  268. package/vendor/wasm/LICENSE_PDFJS_JBIG2 +13 -0
  269. package/vendor/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
  270. package/vendor/wasm/LICENSE_PDFJS_QCMS +19 -0
  271. package/vendor/wasm/LICENSE_QCMS +21 -0
  272. package/vendor/wasm/jbig2.wasm +0 -0
  273. package/vendor/wasm/jbig2_nowasm_fallback.js +15 -0
  274. package/vendor/wasm/openjpeg.wasm +0 -0
  275. package/vendor/wasm/openjpeg_nowasm_fallback.js +17 -0
  276. package/vendor/wasm/qcms_bg.wasm +0 -0
  277. package/vendor/wasm/quickjs-eval.wasm +0 -0
  278. package//345/256/211/350/243/205/344/270/216/345/210/206/345/217/221.md +54 -0
package/index.js ADDED
@@ -0,0 +1,210 @@
1
+ /**
2
+ * Host half of dsh-pdf-workbench.
3
+ *
4
+ * Owns the catalog (index file or file-name derivation), the persisted
5
+ * workbench position, the pending-intent registry for created-but-unused
6
+ * sessions, and the Workspace registered for the library directory — sessions
7
+ * created from the workbench must live in that Workspace so the right-side
8
+ * document preview can authorize their files.
9
+ *
10
+ * A workbench session is named only when its first turn starts: until then it
11
+ * is an ordinary untitled blank that no other surface can mistake for a
12
+ * classified session. The browser half never renames on creation for that
13
+ * reason.
14
+ *
15
+ * @module dsh-pdf-workbench
16
+ */
17
+ import { basename, dirname, join } from 'node:path'
18
+ import { fileURLToPath } from 'node:url'
19
+ import { resolveConfig } from './src/host/config.js'
20
+ import { collectTranscript, saveDigest, summarize } from './src/host/digest.js'
21
+ import { LibrariesStore } from './src/host/libraries.js'
22
+ import { LinksStore } from './src/host/links.js'
23
+ import { PendingStore } from './src/host/pending.js'
24
+ import { PressureStore } from './src/host/pressure.js'
25
+ import { ShelfGroups } from './src/host/shelf-groups.js'
26
+ import { PREFIX, createHandler } from './src/host/routes.js'
27
+ import { installToolkit } from './src/host/toolkit.js'
28
+ import { purgeArchivedBlanks } from './src/host/purge.js'
29
+
30
+ /** Cordis plugin name. */
31
+ export const name = 'pdfdesk'
32
+
33
+ /** The browser carrier is the only hard dependency. */
34
+ export const inject = ['webServer']
35
+
36
+ /**
37
+ * Mount the `/pdfdesk/*` routes and name pending sessions on their first turn.
38
+ * @param {import('@deepseek-ai/cordis').Context} ctx - host context.
39
+ * @param {unknown} [rawConfig] - the Loader row's `config`.
40
+ * @returns {void}
41
+ */
42
+ export function apply(ctx, rawConfig) {
43
+ const config = resolveConfig(rawConfig)
44
+ const pending = new PendingStore(config.pendingPath)
45
+ const links = new LinksStore(config.linksPath)
46
+ const pressure = new PressureStore(config.pressurePath)
47
+ const shelves = new ShelfGroups(config.shelvesPath)
48
+ const libraries = new LibrariesStore(config.librariesPath, {
49
+ libraryDir: config.libraryDir,
50
+ fragmentDir: config.fragmentDir,
51
+ artifactDir: config.artifactDir,
52
+ coverDir: config.coverDir,
53
+ indexPath: config.indexPath,
54
+ name: basename(config.libraryDir),
55
+ })
56
+
57
+ /**
58
+ * Read one session's context pressure, live when it is running and from the
59
+ * last recorded snapshot when it is not.
60
+ * @param {string} sessionId - the session.
61
+ * @returns {Promise<{ tokens: number, window: number, ratio: number, source: 'live' | 'stored' } | null>} the pressure.
62
+ */
63
+ async function contextPressure(sessionId) {
64
+ const meter = ctx.get('tokenMeter')
65
+ if (meter !== undefined) {
66
+ const sessions = ctx.get('sessions')
67
+ const session = sessions === undefined ? undefined : sessions.get(sessionId)
68
+ if (session !== undefined) {
69
+ try {
70
+ const reading = meter.contextPressure(session)
71
+ const tokens = Number(reading?.pressureTokens)
72
+ const window = Number(reading?.contextWindow)
73
+ if (Number.isFinite(tokens) && tokens >= 0 && Number.isFinite(window) && window > 0) {
74
+ void pressure.put(sessionId, { tokens, window }).catch(() => {})
75
+ return { tokens, window, ratio: tokens / window, source: 'live' }
76
+ }
77
+ } catch {
78
+ // Fall through to the stored snapshot.
79
+ }
80
+ }
81
+ }
82
+ const stored = await pressure.get(sessionId)
83
+ return stored === null ? null : { tokens: stored.tokens, window: stored.window, ratio: stored.tokens / stored.window, source: 'stored' }
84
+ }
85
+
86
+ /**
87
+ * Summarize one chapter's conversations into an outline file.
88
+ * @param {{ book: object, chapter: object, sessionIds: string[], mode: string }} request - what to digest.
89
+ * @returns {Promise<{ text: string, path: string, sessions: number, dropped: number }>} the outline.
90
+ */
91
+ async function digestChapter(request) {
92
+ const query = ctx.get('sessionQuery')
93
+ const model = ctx.get('agentDefaultModel')
94
+ const llm = ctx.get('llm')
95
+ if (query === undefined || model === undefined || llm === undefined) {
96
+ throw new Error('模型或会话服务不可用')
97
+ }
98
+ const selection = model.currentSelection()
99
+ if (selection === null || selection === undefined || typeof selection.provider !== 'string' || typeof selection.model !== 'string') {
100
+ throw new Error('没有可用的默认模型')
101
+ }
102
+ const collected = await collectTranscript(request.sessionIds, id => query.readSession(id))
103
+ if (collected.text.trim() === '') throw new Error('这些会话还没有可总结的内容')
104
+ const text = await summarize(selection, options => llm.stream(options), {
105
+ bookTitle: request.book.title,
106
+ code: request.chapter.code,
107
+ chapterTitle: request.chapter.title,
108
+ mode: request.mode === 'all' ? '' : request.mode,
109
+ transcript: collected.text,
110
+ })
111
+ if (text === '') throw new Error('模型没有返回内容')
112
+ const library = await libraries.active()
113
+ const path = await saveDigest(library, request.book.id, request.chapter.code, request.mode, text)
114
+ return { text, path, sessions: collected.used.length, dropped: collected.dropped }
115
+ }
116
+
117
+ const handler = createHandler(config, {
118
+ md2texScript: join(dirname(fileURLToPath(import.meta.url)), 'tools', 'md2tex.py'),
119
+ pending,
120
+ links,
121
+ libraries,
122
+ shelves,
123
+ contextPressure,
124
+ digest: digestChapter,
125
+ installToolkit: libraryDir => installToolkit(config.toolsDir, libraryDir),
126
+ /**
127
+ * The Workspaces a user can choose for conversations. The browser never
128
+ * creates one silently; it shows these and a create action.
129
+ * @returns {Promise<Array<{ id: string, title: string, path: string }>>} the list.
130
+ */
131
+ async listWorkspaces() {
132
+ const registry = ctx.get('workspaceRegistry')
133
+ if (registry === undefined) return []
134
+ try {
135
+ return registry.list().map(workspace => ({
136
+ id: String(workspace.id),
137
+ title: typeof workspace.title === 'string' ? workspace.title : '',
138
+ path: typeof workspace.path === 'string' ? workspace.path : '',
139
+ }))
140
+ } catch {
141
+ return []
142
+ }
143
+ },
144
+ /**
145
+ * Resolve the Workspace that owns the library directory, so the browser
146
+ * creates sessions inside it instead of into an ungrouped cwd.
147
+ * @returns {Promise<{ workspaceId: string | null, cwd: string }>} the create target.
148
+ */
149
+ async resolveTarget() {
150
+ const registry = ctx.get('workspaceRegistry')
151
+ const library = await libraries.active()
152
+ const libraryDir = library === null ? config.libraryDir : library.libraryDir
153
+ if (registry === undefined) return { workspaceId: null, cwd: libraryDir }
154
+ try {
155
+ const workspace = await registry.resolveByPath(libraryDir)
156
+ if (workspace === undefined || workspace === null) return { workspaceId: null, cwd: libraryDir }
157
+ return {
158
+ workspaceId: String(workspace.id),
159
+ cwd: typeof workspace.path === 'string' && workspace.path !== '' ? workspace.path : libraryDir,
160
+ }
161
+ } catch {
162
+ return { workspaceId: null, cwd: libraryDir }
163
+ }
164
+ },
165
+ /**
166
+ * Remove the empty session records the workbench itself left behind.
167
+ * @returns {Promise<{ purged: number }>} how many were removed.
168
+ */
169
+ async purge() {
170
+ return await purgeArchivedBlanks(ctx, config, pending, links, pressure)
171
+ },
172
+ })
173
+
174
+ ctx.effect(() => ctx.webServer.register({ kind: 'prefix', path: PREFIX, handler }), 'pdfdesk: routes')
175
+
176
+ // The first turn names a created session; every finished turn records how full
177
+ // its context is, so "New" can decide between continuing and starting over.
178
+ ctx.effect(() => ctx.on('session/event', (session, event) => {
179
+ if (event === null || typeof event !== 'object') return
180
+ if (event.type === 'turn/end') {
181
+ const meter = ctx.get('tokenMeter')
182
+ if (meter === undefined) return
183
+ try {
184
+ const reading = meter.contextPressure(session)
185
+ const tokens = Number(reading?.pressureTokens)
186
+ const window = Number(reading?.contextWindow)
187
+ if (Number.isFinite(tokens) && tokens >= 0 && Number.isFinite(window) && window > 0) {
188
+ void pressure.put(session.id, { tokens, window }).catch(() => {})
189
+ }
190
+ } catch {
191
+ // A session without a measurable request simply has no snapshot yet.
192
+ }
193
+ return
194
+ }
195
+ if (event.type !== 'turn/start') return
196
+ const titles = ctx.get('sessionTitle')
197
+ if (titles === undefined) return
198
+ void pending.get(session.id).then(async entry => {
199
+ if (entry === null) return
200
+ try {
201
+ titles.rename(session, entry.title)
202
+ } catch (error) {
203
+ // Keep the intent: the workbench renames it on its next visit.
204
+ ctx.logger?.warn?.('pdfdesk: failed to name session %s: %s', session.id, String(error))
205
+ return
206
+ }
207
+ await pending.remove(session.id)
208
+ }).catch(() => {})
209
+ }), 'pdfdesk: pending naming and context pressure')
210
+ }
package/locale/en.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "PDF reading · ask as you read",
4
+ "description": "A PDF reading workbench: libraries and shelves, chapter-by-chapter reading, ask-as-you-read conversations (extract / Q&A / exercises), ink and box annotations, bilingual view, and a built-in pdf.js reader, plus shelf groups, a recycle bin, and whole-book PDFs.",
5
+ "detail": "docs/DETAIL.md"
6
+ }
7
+ }
package/locale/zh.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "meta": {
3
+ "title": "PDF 阅读 · 随时问答",
4
+ "description": "面向 PDF 阅读的工作台:书库与书架、按章阅读、随时问答(提取 / 答疑 / 习题)、笔迹与框选标注、双语对照、内置 pdf.js 阅读器,并支持书架分组、回收站与整本 PDF。",
5
+ "detail": "docs/DETAIL.md"
6
+ }
7
+ }
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
- {
2
- "name": "dsh-pdf-workbench",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "dsh-pdf-workbench",
3
+ "version": "1.3.1",
4
+ "private": false,
5
+ "type": "module",
6
+ "description": "PDF reading workbench for DSH: books and chapters, ask-as-you-read conversations, annotations, and a built-in pdf.js reader.",
7
+ "keywords": [
8
+ "dsh-plugin",
9
+ "pdf",
10
+ "reader",
11
+ "annotation",
12
+ "math",
13
+ "latex"
14
+ ],
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/minggemofashi/dsh-pdf-workbench.git"
18
+ },
19
+ "homepage": "https://github.com/minggemofashi/dsh-pdf-workbench",
20
+ "icon": "icon.svg",
21
+ "exports": {
22
+ ".": "./index.js",
23
+ "./client": "./client.js",
24
+ "./cordis.patch.yml": "./cordis.patch.yml",
25
+ "./locale/*.json": "./locale/*.json",
26
+ "./docs/*": "./docs/*",
27
+ "./docs/DETAIL.md": "./docs/DETAIL.md",
28
+ "./icon": "./icon.svg"
29
+ },
30
+ "files": [
31
+ "index.js",
32
+ "client.js",
33
+ "src",
34
+ "vendor",
35
+ "tools",
36
+ "samples",
37
+ "assets",
38
+ "cordis.patch.yml",
39
+ "locale/*.json",
40
+ "docs",
41
+ "screenshots.json",
42
+ "icon.svg",
43
+ "README.md",
44
+ "LICENSE",
45
+ "安装与分发.md"
46
+ ],
47
+ "engines": {
48
+ "dsh": ">=0.2.2-rc.5"
49
+ },
50
+ "dsh": {
51
+ "bundle": {
52
+ "patch": "./cordis.patch.yml"
53
+ },
54
+ "client": {
55
+ "platform": "web",
56
+ "immediately": true,
57
+ "inject": [
58
+ "@deepseek-ai/dsh-client-ui-layout",
59
+ "@deepseek-ai/dsh-client-ui-sidebar"
60
+ ]
61
+ }
62
+ },
63
+ "license": "MIT"
64
+ }
@@ -0,0 +1,52 @@
1
+ {
2
+ "version": 1,
3
+ "book": {
4
+ "id": "中国剩余定理",
5
+ "title": "中国剩余定理",
6
+ "subtitle": "从《孙子算经》到环、多项式与群",
7
+ "chapters": 10,
8
+ "bytes": 829691
9
+ },
10
+ "files": [
11
+ {
12
+ "name": "中国剩余定理_c00_前置_封面目录.pdf",
13
+ "bytes": 27554
14
+ },
15
+ {
16
+ "name": "中国剩余定理_c01_第0章_导读.pdf",
17
+ "bytes": 61585
18
+ },
19
+ {
20
+ "name": "中国剩余定理_c02_第1章_古题_孙子算经到秦九韶.pdf",
21
+ "bytes": 101415
22
+ },
23
+ {
24
+ "name": "中国剩余定理_c03_第2章_整数版本.pdf",
25
+ "bytes": 86675
26
+ },
27
+ {
28
+ "name": "中国剩余定理_c04_第3章_环论版本.pdf",
29
+ "bytes": 89052
30
+ },
31
+ {
32
+ "name": "中国剩余定理_c05_第4章_多项式版本.pdf",
33
+ "bytes": 93162
34
+ },
35
+ {
36
+ "name": "中国剩余定理_c06_第5章_群论版本.pdf",
37
+ "bytes": 97547
38
+ },
39
+ {
40
+ "name": "中国剩余定理_c07_第6章_统一结构.pdf",
41
+ "bytes": 130235
42
+ },
43
+ {
44
+ "name": "中国剩余定理_c08_第7章_回望.pdf",
45
+ "bytes": 48444
46
+ },
47
+ {
48
+ "name": "中国剩余定理_c90_附录_教材页码符号表自测.pdf",
49
+ "bytes": 94022
50
+ }
51
+ ]
52
+ }
Binary file
@@ -0,0 +1,8 @@
1
+ [
2
+ "assets/screenshot-hero.jpg",
3
+ "assets/screenshot-shelf.jpg",
4
+ "assets/screenshot-reader.jpg",
5
+ "assets/screenshot-bilingual.jpg",
6
+ "assets/screenshot-overlay.jpg",
7
+ "assets/screenshot-qa.jpg"
8
+ ]
@@ -0,0 +1,80 @@
1
+ /**
2
+ * PdfViewer — the unified reader face of the rewrite.
3
+ *
4
+ * One component owns: zoom, scroll container, page painting per layer, and the
5
+ * mode layout (single / continuous / dual). Modes only choose which layers and
6
+ * columns are rendered; painting and geometry stay identical, which is what the
7
+ * old per-mode canvases kept getting wrong.
8
+ *
9
+ * This module is the extracted target: `client.js` will mount it once per
10
+ * reader and per side preview, then the legacy paint paths can be deleted.
11
+ *
12
+ * Layers:
13
+ * { kind: 'source', doc }
14
+ * { kind: 'translation', doc }
15
+ * { kind: 'masks', boxes } // reveal boxes over `translation`
16
+ * { kind: 'blocks', blocks } // debug block overlay
17
+ *
18
+ * Geometry rules (from docs/READER-REWRITE.md):
19
+ * - centered content uses `safe center`, so a zoomed page can still scroll to
20
+ * its left edge;
21
+ * - the overlay is ONE page-sized translation image clipped by a `clipPath` of
22
+ * rectangle subpaths per mask (v0.49.2). A per-mask canvas drifted because a
23
+ * zoomed page box is wider than the stage; `maskPageStyle` below is the old
24
+ * per-canvas formula, kept only for reference;
25
+ * - repainting is driven by (doc, page, scale, version), not by DOM mounting.
26
+ */
27
+
28
+ /** CSS injected once by the viewer's owner. */
29
+ export const VIEWER_STYLE = `
30
+ .mdv-root { position: relative; width: 100%; height: 100%; display: flex; flex-direction: column; min-height: 0; }
31
+ .mdv-stage { flex: 1 1 auto; min-height: 0; overflow: auto; }
32
+ .mdv-columns { display: flex; gap: 12px; width: 100%; height: 100%; }
33
+ .mdv-column { flex: 1 1 0; min-width: 0; overflow: auto; }
34
+ .mdv-page { position: relative; margin: 0 auto 18px; width: fit-content; }
35
+ .mdv-canvas { display: block; }
36
+ .mdv-layer { position: absolute; inset: 0; pointer-events: none; }
37
+ .mdv-mask { position: absolute; overflow: hidden; border: 1px dashed rgba(40, 140, 235, .9); box-sizing: border-box; }
38
+ .mdv-mask-page { position: absolute; max-width: none; }
39
+ .mdv-block { position: absolute; border: 1px dashed rgba(235, 70, 70, .85); box-sizing: border-box; }
40
+ .mdv-block[data-kind="heading"] { border-color: rgba(60, 130, 235, .95); }
41
+ .mdv-block[data-kind="formula"] { border-color: rgba(40, 160, 90, .95); }
42
+ `
43
+
44
+ /**
45
+ * Paint one page into a canvas at a given scale.
46
+ * @param {object} options - doc, pageNumber, scale, canvas, signal.
47
+ * @returns {Promise<void>} resolves when the page finished painting.
48
+ */
49
+ export async function paintPage(options) {
50
+ const { doc, pageNumber, scale, canvas, signal } = options
51
+ const page = await doc.getPage(pageNumber)
52
+ if (signal !== undefined && signal.aborted) return
53
+ const viewport = page.getViewport({ scale })
54
+ let dpr = Math.min(2, Math.max(1, typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1))
55
+ if (viewport.width * dpr > 8000 || viewport.height * dpr > 8000) dpr = 1
56
+ canvas.width = Math.max(1, Math.floor(viewport.width * dpr))
57
+ canvas.height = Math.max(1, Math.floor(viewport.height * dpr))
58
+ canvas.style.width = `${Math.floor(viewport.width)}px`
59
+ canvas.style.height = `${Math.floor(viewport.height)}px`
60
+ await page.render({
61
+ canvasContext: canvas.getContext('2d'),
62
+ viewport,
63
+ transform: dpr !== 1 ? [dpr, 0, 0, dpr, 0, 0] : undefined,
64
+ }).promise
65
+ }
66
+
67
+ /**
68
+ * Compute the geometry a per-mask translated canvas used to need.
69
+ * @deprecated Superseded by the single-image `clipPath` overlay (v0.49.2).
70
+ * @param {{ x: number, y: number, w: number, h: number }} box - normalized box.
71
+ * @returns {{ left: string, top: string, width: string, height: string }} CSS percentages.
72
+ */
73
+ export function maskPageStyle(box) {
74
+ return {
75
+ left: `${-(box.x / box.w) * 100}%`,
76
+ top: `${-(box.y / box.h) * 100}%`,
77
+ width: `${100 / box.w}%`,
78
+ height: `${100 / box.h}%`,
79
+ }
80
+ }
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Durable mirror of the reader's annotations and overlay boxes.
3
+ *
4
+ * The browser's localStorage is the live store (both the reader and the right
5
+ * column edit it directly), but it disappears with site data, a new browser or
6
+ * a new machine. Every edit is therefore mirrored to
7
+ * `<库根>\.pdfdesk\annotations\<书>\<章>.json`, which travels with the library
8
+ * and survives plugin updates — the file format carries its own version so a
9
+ * later release can migrate old files instead of dropping them.
10
+ */
11
+ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
12
+ import { dirname, join } from 'node:path'
13
+
14
+ /** On-disk schema version; bump together with a migration in `readAnnotations`. */
15
+ export const ANNOTATION_FORMAT = 1
16
+
17
+ /**
18
+ * Reject a value that cannot be one path segment.
19
+ * @param {unknown} value - book id or chapter code from a request body.
20
+ * @returns {string} the value, or '' when it is not a plain segment.
21
+ */
22
+ export function pathSegment(value) {
23
+ if (typeof value !== 'string') return ''
24
+ const trimmed = value.trim()
25
+ if (trimmed === '' || trimmed === '.' || trimmed === '..') return ''
26
+ if (/[\\/:*?"<>|]/.test(trimmed)) return ''
27
+ return trimmed
28
+ }
29
+
30
+ /**
31
+ * The mirror file of one chapter.
32
+ * @param {string} libraryDir - library root.
33
+ * @param {string} book - book id.
34
+ * @param {string} code - chapter code.
35
+ * @returns {string} absolute path.
36
+ */
37
+ export function annotationsFile(libraryDir, book, code) {
38
+ return join(libraryDir, '.pdfdesk', 'annotations', book, `${code}.json`)
39
+ }
40
+
41
+ /**
42
+ * Normalize one page entry from a request body or from an older file.
43
+ * @param {unknown} entry - `{ masks, annots, updatedAt }`.
44
+ * @returns {{ masks: object[], annots: object[], updatedAt: number }} the entry.
45
+ */
46
+ function pageEntry(entry) {
47
+ const source = entry !== null && typeof entry === 'object' ? entry : {}
48
+ const masks = Array.isArray(source.masks) ? source.masks.filter(item => item !== null && typeof item === 'object') : []
49
+ const annots = Array.isArray(source.annots) ? source.annots.filter(item => item !== null && typeof item === 'object') : []
50
+ const stamp = Number(source.updatedAt)
51
+ return { masks, annots, updatedAt: Number.isFinite(stamp) && stamp > 0 ? stamp : Date.now() }
52
+ }
53
+
54
+ /**
55
+ * Read one chapter's mirror.
56
+ * @param {string} libraryDir - library root.
57
+ * @param {string} book - book id.
58
+ * @param {string} code - chapter code.
59
+ * @returns {Promise<{ format: number, pages: Record<string, object>, updatedAt: number }>} the mirror; empty when absent.
60
+ */
61
+ export async function readAnnotations(libraryDir, book, code) {
62
+ let parsed = null
63
+ try {
64
+ parsed = JSON.parse(await readFile(annotationsFile(libraryDir, book, code), 'utf8'))
65
+ } catch {
66
+ return { format: ANNOTATION_FORMAT, pages: {}, updatedAt: 0 }
67
+ }
68
+ const pages = {}
69
+ const raw = parsed !== null && typeof parsed === 'object' && parsed.pages !== null && typeof parsed.pages === 'object'
70
+ ? parsed.pages
71
+ : {}
72
+ for (const [key, value] of Object.entries(raw)) {
73
+ const page = Number.parseInt(key, 10)
74
+ if (!Number.isFinite(page) || page < 1) continue
75
+ pages[String(page)] = pageEntry(value)
76
+ }
77
+ // Format 0 is what an early build wrote (no `format` field): the page shape is
78
+ // the same, so it is adopted as version 1 rather than discarded.
79
+ const format = Number.isFinite(Number(parsed?.format)) && Number(parsed?.format) > 0 ? Number(parsed.format) : 1
80
+ const stamp = Number(parsed?.updatedAt)
81
+ return { format, pages, updatedAt: Number.isFinite(stamp) ? stamp : 0 }
82
+ }
83
+
84
+ /**
85
+ * Write the mirror atomically (temp file + rename), so a crash never leaves a
86
+ * half file that the next read would treat as empty.
87
+ * @param {string} libraryDir - library root.
88
+ * @param {string} book - book id.
89
+ * @param {string} code - chapter code.
90
+ * @param {{ pages: Record<string, object>, updatedAt: number }} record - pages to keep.
91
+ * @returns {Promise<number>} the stored `updatedAt`.
92
+ */
93
+ async function persist(libraryDir, book, code, record) {
94
+ const target = annotationsFile(libraryDir, book, code)
95
+ await mkdir(dirname(target), { recursive: true })
96
+ const temporary = `${target}.tmp`
97
+ await writeFile(temporary, JSON.stringify({ format: ANNOTATION_FORMAT, book, code, ...record }, null, 1), 'utf8')
98
+ await rename(temporary, target)
99
+ return record.updatedAt
100
+ }
101
+
102
+ /**
103
+ * Merge one page into the mirror. The page is replaced wholesale, because the
104
+ * editor's page state is the authority for its own page.
105
+ * @param {string} libraryDir - library root.
106
+ * @param {string} book - book id.
107
+ * @param {string} code - chapter code.
108
+ * @param {number} page - 1-based page number.
109
+ * @param {{ masks?: unknown, annots?: unknown, updatedAt?: unknown }} body - the page payload.
110
+ * @returns {Promise<{ updatedAt: number }>} the mirror's new timestamp.
111
+ */
112
+ export async function writeAnnotations(libraryDir, book, code, page, body) {
113
+ const record = await readAnnotations(libraryDir, book, code)
114
+ record.pages[String(page)] = pageEntry({ ...body, updatedAt: body.updatedAt ?? Date.now() })
115
+ record.updatedAt = Date.now()
116
+ await persist(libraryDir, book, code, record)
117
+ return { updatedAt: record.updatedAt }
118
+ }
119
+
120
+ /**
121
+ * Seed pages the mirror has never seen. Existing pages are left alone, so a
122
+ * browser that still holds the notes can hand them over without a chance of
123
+ * overwriting something newer on disk.
124
+ * @param {string} libraryDir - library root.
125
+ * @param {string} book - book id.
126
+ * @param {string} code - chapter code.
127
+ * @param {Array<{ page: unknown, masks?: unknown, annots?: unknown }>} pages - candidate pages.
128
+ * @returns {Promise<{ added: number }>} how many pages were new.
129
+ */
130
+ export async function seedAnnotations(libraryDir, book, code, pages) {
131
+ const record = await readAnnotations(libraryDir, book, code)
132
+ let added = 0
133
+ for (const item of Array.isArray(pages) ? pages : []) {
134
+ const page = Number.parseInt(String(item?.page ?? ''), 10)
135
+ if (!Number.isFinite(page) || page < 1) continue
136
+ if (record.pages[String(page)] !== undefined) continue
137
+ record.pages[String(page)] = pageEntry(item)
138
+ added += 1
139
+ }
140
+ if (added > 0) {
141
+ record.updatedAt = Date.now()
142
+ await persist(libraryDir, book, code, record)
143
+ }
144
+ return { added }
145
+ }