editor-svg 1.0.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.
Files changed (472) hide show
  1. package/.editorconfig +9 -0
  2. package/.eslintrc +46 -0
  3. package/.prettierrc +8 -0
  4. package/AGENTS.md +186 -0
  5. package/CHANGELOG.md +2543 -0
  6. package/CLAUDE.md +110 -0
  7. package/LICENSE +21 -0
  8. package/README.md +110 -0
  9. package/cypress/e2e/control/checkbox.cy.ts +46 -0
  10. package/cypress/e2e/control/select.cy.ts +56 -0
  11. package/cypress/e2e/control/text.cy.ts +43 -0
  12. package/cypress/e2e/editor.cy.ts +67 -0
  13. package/cypress/e2e/menus/block.cy.ts +38 -0
  14. package/cypress/e2e/menus/checkbox.cy.ts +33 -0
  15. package/cypress/e2e/menus/codeblock.cy.ts +34 -0
  16. package/cypress/e2e/menus/date.cy.ts +28 -0
  17. package/cypress/e2e/menus/format.cy.ts +40 -0
  18. package/cypress/e2e/menus/hyperlink.cy.ts +39 -0
  19. package/cypress/e2e/menus/image.cy.ts +25 -0
  20. package/cypress/e2e/menus/latex.cy.ts +34 -0
  21. package/cypress/e2e/menus/pagebreak.cy.ts +21 -0
  22. package/cypress/e2e/menus/painter.cy.ts +53 -0
  23. package/cypress/e2e/menus/print.cy.ts +25 -0
  24. package/cypress/e2e/menus/row.cy.ts +103 -0
  25. package/cypress/e2e/menus/search.cy.ts +112 -0
  26. package/cypress/e2e/menus/separator.cy.ts +32 -0
  27. package/cypress/e2e/menus/table.cy.ts +25 -0
  28. package/cypress/e2e/menus/text.cy.ts +304 -0
  29. package/cypress/e2e/menus/title.cy.ts +43 -0
  30. package/cypress/e2e/menus/undoRedo.cy.ts +49 -0
  31. package/cypress/e2e/menus/watermark.cy.ts +64 -0
  32. package/cypress/fixtures/example.json +3 -0
  33. package/cypress/fixtures/test.png +0 -0
  34. package/cypress/global.d.ts +13 -0
  35. package/cypress/support/commands.ts +5 -0
  36. package/cypress/support/e2e.ts +1 -0
  37. package/cypress/tsconfig.json +21 -0
  38. package/cypress.config.ts +10 -0
  39. package/docs/.vitepress/config.ts +191 -0
  40. package/docs/.vitepress/theme/components/DeepWikiBadge.vue +21 -0
  41. package/docs/.vitepress/theme/components/ZreadBadge.vue +21 -0
  42. package/docs/.vitepress/theme/index.ts +27 -0
  43. package/docs/en/guide/api-common.md +49 -0
  44. package/docs/en/guide/api-instance.md +34 -0
  45. package/docs/en/guide/command-execute.md +1167 -0
  46. package/docs/en/guide/command-get.md +355 -0
  47. package/docs/en/guide/contextmenu-custom.md +44 -0
  48. package/docs/en/guide/contextmenu-internal.md +61 -0
  49. package/docs/en/guide/eventbus.md +260 -0
  50. package/docs/en/guide/i18n.md +112 -0
  51. package/docs/en/guide/listener.md +126 -0
  52. package/docs/en/guide/option.md +214 -0
  53. package/docs/en/guide/override.md +57 -0
  54. package/docs/en/guide/plugin-custom.md +25 -0
  55. package/docs/en/guide/plugin-internal.md +125 -0
  56. package/docs/en/guide/schema.md +237 -0
  57. package/docs/en/guide/shortcut-custom.md +22 -0
  58. package/docs/en/guide/shortcut-internal.md +189 -0
  59. package/docs/en/guide/start.md +97 -0
  60. package/docs/en/index.md +43 -0
  61. package/docs/guide/api-common.md +49 -0
  62. package/docs/guide/api-instance.md +34 -0
  63. package/docs/guide/command-execute.md +1157 -0
  64. package/docs/guide/command-get.md +353 -0
  65. package/docs/guide/contextmenu-custom.md +44 -0
  66. package/docs/guide/contextmenu-internal.md +61 -0
  67. package/docs/guide/eventbus.md +260 -0
  68. package/docs/guide/i18n.md +111 -0
  69. package/docs/guide/listener.md +126 -0
  70. package/docs/guide/option.md +214 -0
  71. package/docs/guide/override.md +57 -0
  72. package/docs/guide/plugin-custom.md +25 -0
  73. package/docs/guide/plugin-internal.md +125 -0
  74. package/docs/guide/schema.md +237 -0
  75. package/docs/guide/shortcut-custom.md +22 -0
  76. package/docs/guide/shortcut-internal.md +189 -0
  77. package/docs/guide/start.md +97 -0
  78. package/docs/index.md +43 -0
  79. package/docs/public/favicon.png +0 -0
  80. package/favicon.png +0 -0
  81. package/index.html +435 -0
  82. package/package.json +55 -0
  83. package/pnpm-lock.yaml +4113 -0
  84. package/scripts/release.js +41 -0
  85. package/scripts/verifyCommit.js +19 -0
  86. package/src/assets/images/alignment.svg +1 -0
  87. package/src/assets/images/arrow-left.svg +1 -0
  88. package/src/assets/images/arrow-right.svg +1 -0
  89. package/src/assets/images/block.svg +1 -0
  90. package/src/assets/images/bold.svg +1 -0
  91. package/src/assets/images/catalog.svg +1 -0
  92. package/src/assets/images/center.svg +1 -0
  93. package/src/assets/images/checkbox.svg +1 -0
  94. package/src/assets/images/close.svg +1 -0
  95. package/src/assets/images/codeblock.svg +1 -0
  96. package/src/assets/images/color.svg +1 -0
  97. package/src/assets/images/control.svg +1 -0
  98. package/src/assets/images/date.svg +1 -0
  99. package/src/assets/images/exit-fullscreen.svg +1 -0
  100. package/src/assets/images/format.svg +1 -0
  101. package/src/assets/images/highlight.svg +1 -0
  102. package/src/assets/images/hyperlink.svg +1 -0
  103. package/src/assets/images/image.svg +1 -0
  104. package/src/assets/images/italic.svg +1 -0
  105. package/src/assets/images/justify.svg +7 -0
  106. package/src/assets/images/latex.svg +1 -0
  107. package/src/assets/images/left.svg +1 -0
  108. package/src/assets/images/line-dash-dot-dot.svg +1 -0
  109. package/src/assets/images/line-dash-dot.svg +1 -0
  110. package/src/assets/images/line-dash-large-gap.svg +1 -0
  111. package/src/assets/images/line-dash-small-gap.svg +1 -0
  112. package/src/assets/images/line-dot.svg +1 -0
  113. package/src/assets/images/line-double.svg +1 -0
  114. package/src/assets/images/line-single.svg +1 -0
  115. package/src/assets/images/line-wavy.svg +1 -0
  116. package/src/assets/images/list.svg +1 -0
  117. package/src/assets/images/option.svg +1 -0
  118. package/src/assets/images/page-break.svg +1 -0
  119. package/src/assets/images/page-mode.svg +1 -0
  120. package/src/assets/images/page-scale-add.svg +1 -0
  121. package/src/assets/images/page-scale-minus.svg +1 -0
  122. package/src/assets/images/painter.svg +1 -0
  123. package/src/assets/images/paper-direction.svg +1 -0
  124. package/src/assets/images/paper-margin.svg +1 -0
  125. package/src/assets/images/paper-size.svg +1 -0
  126. package/src/assets/images/print.svg +1 -0
  127. package/src/assets/images/radio.svg +4 -0
  128. package/src/assets/images/redo.svg +1 -0
  129. package/src/assets/images/request-fullscreen.svg +1 -0
  130. package/src/assets/images/right.svg +1 -0
  131. package/src/assets/images/row-margin.svg +1 -0
  132. package/src/assets/images/search.svg +1 -0
  133. package/src/assets/images/separator.svg +1 -0
  134. package/src/assets/images/signature-undo.svg +1 -0
  135. package/src/assets/images/signature.svg +1 -0
  136. package/src/assets/images/size-add.svg +1 -0
  137. package/src/assets/images/size-minus.svg +1 -0
  138. package/src/assets/images/strikeout.svg +1 -0
  139. package/src/assets/images/subscript.svg +1 -0
  140. package/src/assets/images/superscript.svg +1 -0
  141. package/src/assets/images/table.svg +1 -0
  142. package/src/assets/images/title.svg +1 -0
  143. package/src/assets/images/trash.svg +1 -0
  144. package/src/assets/images/underline.svg +1 -0
  145. package/src/assets/images/undo.svg +1 -0
  146. package/src/assets/images/watermark.svg +1 -0
  147. package/src/assets/images/word-tool.svg +1 -0
  148. package/src/assets/snapshots/main_v0.2.1.png +0 -0
  149. package/src/assets/snapshots/main_v0.2.2.png +0 -0
  150. package/src/assets/snapshots/main_v0.3.0.png +0 -0
  151. package/src/assets/snapshots/main_v0.3.1.png +0 -0
  152. package/src/assets/snapshots/main_v0.5.0.png +0 -0
  153. package/src/assets/snapshots/main_v0.5.1.png +0 -0
  154. package/src/assets/snapshots/main_v0.6.0.png +0 -0
  155. package/src/assets/snapshots/main_v0.6.1.png +0 -0
  156. package/src/assets/snapshots/main_v0.7.0.png +0 -0
  157. package/src/assets/snapshots/main_v0.7.1.png +0 -0
  158. package/src/assets/snapshots/main_v0.7.2.png +0 -0
  159. package/src/assets/snapshots/main_v0.7.3.png +0 -0
  160. package/src/assets/snapshots/main_v0.7.4.png +0 -0
  161. package/src/assets/snapshots/main_v0.7.6.png +0 -0
  162. package/src/assets/snapshots/main_v0.7.7.png +0 -0
  163. package/src/assets/snapshots/main_v0.8.0.png +0 -0
  164. package/src/assets/snapshots/main_v0.8.5.png +0 -0
  165. package/src/assets/snapshots/main_v0.8.6.png +0 -0
  166. package/src/assets/snapshots/main_v0.8.7.png +0 -0
  167. package/src/assets/snapshots/main_v0.8.8.png +0 -0
  168. package/src/assets/snapshots/main_v0.9.0.png +0 -0
  169. package/src/assets/snapshots/main_v0.9.1.png +0 -0
  170. package/src/assets/snapshots/main_v0.9.2.png +0 -0
  171. package/src/assets/snapshots/main_v0.9.23.png +0 -0
  172. package/src/assets/snapshots/main_v0.9.28.png +0 -0
  173. package/src/assets/snapshots/main_v0.9.29.png +0 -0
  174. package/src/assets/snapshots/main_v0.9.3.png +0 -0
  175. package/src/assets/snapshots/main_v0.9.30.png +0 -0
  176. package/src/assets/snapshots/main_v0.9.32.png +0 -0
  177. package/src/assets/snapshots/main_v0.9.35.png +0 -0
  178. package/src/assets/snapshots/main_v0.9.4.png +0 -0
  179. package/src/assets/snapshots/main_v0.9.5.png +0 -0
  180. package/src/assets/snapshots/main_v0.9.6.png +0 -0
  181. package/src/assets/snapshots/main_v0.9.8.png +0 -0
  182. package/src/components/dialog/Dialog.ts +171 -0
  183. package/src/components/dialog/dialog.css +131 -0
  184. package/src/components/signature/Signature.ts +340 -0
  185. package/src/components/signature/signature.css +132 -0
  186. package/src/editor/assets/css/block/block.css +21 -0
  187. package/src/editor/assets/css/contextmenu/contextmenu.css +196 -0
  188. package/src/editor/assets/css/control/calculator.css +85 -0
  189. package/src/editor/assets/css/control/select.css +44 -0
  190. package/src/editor/assets/css/date/datePicker.css +233 -0
  191. package/src/editor/assets/css/hyperlink/hyperlink.css +26 -0
  192. package/src/editor/assets/css/index.css +78 -0
  193. package/src/editor/assets/css/previewer/previewer.css +122 -0
  194. package/src/editor/assets/css/resizer/resizer.css +74 -0
  195. package/src/editor/assets/css/table/table.css +155 -0
  196. package/src/editor/assets/css/zone/zone.css +61 -0
  197. package/src/editor/assets/images/close.svg +1 -0
  198. package/src/editor/assets/images/delete-col.svg +1 -0
  199. package/src/editor/assets/images/delete-row-col.svg +1 -0
  200. package/src/editor/assets/images/delete-row.svg +1 -0
  201. package/src/editor/assets/images/delete-table.svg +1 -0
  202. package/src/editor/assets/images/image-change.svg +1 -0
  203. package/src/editor/assets/images/image-download.svg +1 -0
  204. package/src/editor/assets/images/image-next.svg +1 -0
  205. package/src/editor/assets/images/image-pre.svg +1 -0
  206. package/src/editor/assets/images/image.svg +1 -0
  207. package/src/editor/assets/images/insert-bottom-row.svg +1 -0
  208. package/src/editor/assets/images/insert-left-col.svg +1 -0
  209. package/src/editor/assets/images/insert-right-col.svg +1 -0
  210. package/src/editor/assets/images/insert-row-col.svg +1 -0
  211. package/src/editor/assets/images/insert-top-row.svg +1 -0
  212. package/src/editor/assets/images/merge-cancel-cell.svg +1 -0
  213. package/src/editor/assets/images/merge-cell.svg +1 -0
  214. package/src/editor/assets/images/original-size.svg +1 -0
  215. package/src/editor/assets/images/print.svg +1 -0
  216. package/src/editor/assets/images/rotate.svg +1 -0
  217. package/src/editor/assets/images/submenu-dropdown.svg +1 -0
  218. package/src/editor/assets/images/table-border-all.svg +1 -0
  219. package/src/editor/assets/images/table-border-dash.svg +1 -0
  220. package/src/editor/assets/images/table-border-empty.svg +1 -0
  221. package/src/editor/assets/images/table-border-external.svg +1 -0
  222. package/src/editor/assets/images/table-border-internal.svg +1 -0
  223. package/src/editor/assets/images/table-border-td-back.svg +1 -0
  224. package/src/editor/assets/images/table-border-td-bottom.svg +1 -0
  225. package/src/editor/assets/images/table-border-td-forward.svg +1 -0
  226. package/src/editor/assets/images/table-border-td-left.svg +1 -0
  227. package/src/editor/assets/images/table-border-td-right.svg +1 -0
  228. package/src/editor/assets/images/table-border-td-top.svg +1 -0
  229. package/src/editor/assets/images/table-border-td.svg +1 -0
  230. package/src/editor/assets/images/vertical-align-bottom.svg +1 -0
  231. package/src/editor/assets/images/vertical-align-middle.svg +1 -0
  232. package/src/editor/assets/images/vertical-align-top.svg +1 -0
  233. package/src/editor/assets/images/vertical-align.svg +1 -0
  234. package/src/editor/assets/images/zoom-in.svg +1 -0
  235. package/src/editor/assets/images/zoom-out.svg +1 -0
  236. package/src/editor/core/actuator/Actuator.ts +21 -0
  237. package/src/editor/core/actuator/handlers/positionContextChange.ts +13 -0
  238. package/src/editor/core/command/Command.ts +312 -0
  239. package/src/editor/core/command/CommandAdapt.ts +2733 -0
  240. package/src/editor/core/contextmenu/ContextMenu.ts +363 -0
  241. package/src/editor/core/contextmenu/menus/controlMenus.ts +25 -0
  242. package/src/editor/core/contextmenu/menus/globalMenus.ts +66 -0
  243. package/src/editor/core/contextmenu/menus/hyperlinkMenus.ts +58 -0
  244. package/src/editor/core/contextmenu/menus/imageMenus.ts +134 -0
  245. package/src/editor/core/contextmenu/menus/tableMenus.ts +331 -0
  246. package/src/editor/core/cursor/Cursor.ts +248 -0
  247. package/src/editor/core/cursor/CursorAgent.ts +75 -0
  248. package/src/editor/core/draw/Draw.ts +2934 -0
  249. package/src/editor/core/draw/control/Control.ts +1767 -0
  250. package/src/editor/core/draw/control/checkbox/CheckboxControl.ts +154 -0
  251. package/src/editor/core/draw/control/date/DateControl.ts +363 -0
  252. package/src/editor/core/draw/control/interactive/ControlSearch.ts +214 -0
  253. package/src/editor/core/draw/control/number/Calculator.ts +183 -0
  254. package/src/editor/core/draw/control/number/NumberControl.ts +183 -0
  255. package/src/editor/core/draw/control/radio/RadioControl.ts +68 -0
  256. package/src/editor/core/draw/control/richtext/Border.ts +52 -0
  257. package/src/editor/core/draw/control/select/SelectControl.ts +567 -0
  258. package/src/editor/core/draw/control/text/TextControl.ts +280 -0
  259. package/src/editor/core/draw/frame/Background.ts +117 -0
  260. package/src/editor/core/draw/frame/Badge.ts +88 -0
  261. package/src/editor/core/draw/frame/Footer.ts +155 -0
  262. package/src/editor/core/draw/frame/Header.ts +158 -0
  263. package/src/editor/core/draw/frame/LineNumber.ts +43 -0
  264. package/src/editor/core/draw/frame/Margin.ts +53 -0
  265. package/src/editor/core/draw/frame/PageBorder.ts +47 -0
  266. package/src/editor/core/draw/frame/PageNumber.ts +88 -0
  267. package/src/editor/core/draw/frame/Placeholder.ts +114 -0
  268. package/src/editor/core/draw/frame/Watermark.ts +188 -0
  269. package/src/editor/core/draw/graffiti/Graffiti.ts +125 -0
  270. package/src/editor/core/draw/interactive/Area.ts +312 -0
  271. package/src/editor/core/draw/interactive/Group.ts +198 -0
  272. package/src/editor/core/draw/interactive/Search.ts +527 -0
  273. package/src/editor/core/draw/particle/CheckboxParticle.ts +111 -0
  274. package/src/editor/core/draw/particle/HyperlinkParticle.ts +86 -0
  275. package/src/editor/core/draw/particle/ImageParticle.ts +280 -0
  276. package/src/editor/core/draw/particle/LabelParticle.ts +79 -0
  277. package/src/editor/core/draw/particle/LineBreakParticle.ts +55 -0
  278. package/src/editor/core/draw/particle/ListParticle.ts +255 -0
  279. package/src/editor/core/draw/particle/PageBreakParticle.ts +54 -0
  280. package/src/editor/core/draw/particle/RadioParticle.ts +99 -0
  281. package/src/editor/core/draw/particle/SeparatorParticle.ts +37 -0
  282. package/src/editor/core/draw/particle/SubscriptParticle.ts +23 -0
  283. package/src/editor/core/draw/particle/SuperscriptParticle.ts +23 -0
  284. package/src/editor/core/draw/particle/TextParticle.ts +174 -0
  285. package/src/editor/core/draw/particle/WhiteSpaceParticle.ts +32 -0
  286. package/src/editor/core/draw/particle/block/BlockParticle.ts +76 -0
  287. package/src/editor/core/draw/particle/block/modules/BaseBlock.ts +280 -0
  288. package/src/editor/core/draw/particle/block/modules/IFrameBlock.ts +47 -0
  289. package/src/editor/core/draw/particle/block/modules/VideoBlock.ts +61 -0
  290. package/src/editor/core/draw/particle/date/DateParticle.ts +111 -0
  291. package/src/editor/core/draw/particle/date/DatePicker.ts +577 -0
  292. package/src/editor/core/draw/particle/latex/LaTexParticle.ts +43 -0
  293. package/src/editor/core/draw/particle/latex/utils/LaTexUtils.ts +1196 -0
  294. package/src/editor/core/draw/particle/latex/utils/hershey.ts +1632 -0
  295. package/src/editor/core/draw/particle/latex/utils/symbols.ts +318 -0
  296. package/src/editor/core/draw/particle/previewer/Previewer.ts +582 -0
  297. package/src/editor/core/draw/particle/table/TableOperate.ts +988 -0
  298. package/src/editor/core/draw/particle/table/TableParticle.ts +558 -0
  299. package/src/editor/core/draw/particle/table/TableTool.ts +551 -0
  300. package/src/editor/core/draw/richtext/AbstractRichText.ts +59 -0
  301. package/src/editor/core/draw/richtext/Highlight.ts +24 -0
  302. package/src/editor/core/draw/richtext/Strikeout.ts +28 -0
  303. package/src/editor/core/draw/richtext/Underline.ts +106 -0
  304. package/src/editor/core/event/CanvasEvent.ts +215 -0
  305. package/src/editor/core/event/GlobalEvent.ts +173 -0
  306. package/src/editor/core/event/eventbus/EventBus.ts +50 -0
  307. package/src/editor/core/event/handlers/click.ts +234 -0
  308. package/src/editor/core/event/handlers/composition.ts +45 -0
  309. package/src/editor/core/event/handlers/copy.ts +72 -0
  310. package/src/editor/core/event/handlers/cut.ts +47 -0
  311. package/src/editor/core/event/handlers/drag.ts +66 -0
  312. package/src/editor/core/event/handlers/drop.ts +28 -0
  313. package/src/editor/core/event/handlers/input.ts +129 -0
  314. package/src/editor/core/event/handlers/keydown/backspace.ts +161 -0
  315. package/src/editor/core/event/handlers/keydown/delete.ts +119 -0
  316. package/src/editor/core/event/handlers/keydown/end.ts +69 -0
  317. package/src/editor/core/event/handlers/keydown/enter.ts +126 -0
  318. package/src/editor/core/event/handlers/keydown/home.ts +69 -0
  319. package/src/editor/core/event/handlers/keydown/index.ts +85 -0
  320. package/src/editor/core/event/handlers/keydown/left.ts +162 -0
  321. package/src/editor/core/event/handlers/keydown/right.ts +178 -0
  322. package/src/editor/core/event/handlers/keydown/tab.ts +41 -0
  323. package/src/editor/core/event/handlers/keydown/updown.ts +342 -0
  324. package/src/editor/core/event/handlers/mousedown.ts +262 -0
  325. package/src/editor/core/event/handlers/mouseleave.ts +14 -0
  326. package/src/editor/core/event/handlers/mousemove.ts +133 -0
  327. package/src/editor/core/event/handlers/mouseup.ts +341 -0
  328. package/src/editor/core/event/handlers/paste.ts +231 -0
  329. package/src/editor/core/history/HistoryManager.ts +61 -0
  330. package/src/editor/core/i18n/I18n.ts +51 -0
  331. package/src/editor/core/i18n/lang/en.json +92 -0
  332. package/src/editor/core/i18n/lang/zh-CN.json +92 -0
  333. package/src/editor/core/listener/Listener.ts +41 -0
  334. package/src/editor/core/observer/ImageObserver.ts +19 -0
  335. package/src/editor/core/observer/MouseObserver.ts +56 -0
  336. package/src/editor/core/observer/ScrollObserver.ts +88 -0
  337. package/src/editor/core/observer/SelectionObserver.ts +143 -0
  338. package/src/editor/core/override/Override.ts +14 -0
  339. package/src/editor/core/plugin/Plugin.ts +17 -0
  340. package/src/editor/core/position/Position.ts +870 -0
  341. package/src/editor/core/range/RangeManager.ts +723 -0
  342. package/src/editor/core/register/Register.ts +28 -0
  343. package/src/editor/core/shortcut/Shortcut.ts +80 -0
  344. package/src/editor/core/shortcut/keys/listKeys.ts +22 -0
  345. package/src/editor/core/shortcut/keys/richtextKeys.ts +102 -0
  346. package/src/editor/core/shortcut/keys/titleKeys.ts +62 -0
  347. package/src/editor/core/worker/WorkerManager.ts +96 -0
  348. package/src/editor/core/worker/works/catalog.ts +189 -0
  349. package/src/editor/core/worker/works/group.ts +34 -0
  350. package/src/editor/core/worker/works/value.ts +32 -0
  351. package/src/editor/core/worker/works/wordCount.ts +132 -0
  352. package/src/editor/core/zone/Zone.ts +183 -0
  353. package/src/editor/core/zone/ZoneTip.ts +108 -0
  354. package/src/editor/dataset/constant/Background.ts +10 -0
  355. package/src/editor/dataset/constant/Badge.ts +6 -0
  356. package/src/editor/dataset/constant/Checkbox.ts +12 -0
  357. package/src/editor/dataset/constant/Common.ts +44 -0
  358. package/src/editor/dataset/constant/ContextMenu.ts +61 -0
  359. package/src/editor/dataset/constant/Control.ts +14 -0
  360. package/src/editor/dataset/constant/Cursor.ts +11 -0
  361. package/src/editor/dataset/constant/Editor.ts +19 -0
  362. package/src/editor/dataset/constant/Element.ts +173 -0
  363. package/src/editor/dataset/constant/Footer.ts +10 -0
  364. package/src/editor/dataset/constant/Graffiti.ts +6 -0
  365. package/src/editor/dataset/constant/Group.ts +10 -0
  366. package/src/editor/dataset/constant/Header.ts +10 -0
  367. package/src/editor/dataset/constant/ImgCaption.ts +8 -0
  368. package/src/editor/dataset/constant/Label.ts +8 -0
  369. package/src/editor/dataset/constant/LineBreak.ts +7 -0
  370. package/src/editor/dataset/constant/LineNumber.ts +11 -0
  371. package/src/editor/dataset/constant/List.ts +26 -0
  372. package/src/editor/dataset/constant/PageBorder.ts +8 -0
  373. package/src/editor/dataset/constant/PageBreak.ts +7 -0
  374. package/src/editor/dataset/constant/PageNumber.ts +22 -0
  375. package/src/editor/dataset/constant/Placeholder.ts +9 -0
  376. package/src/editor/dataset/constant/Radio.ts +12 -0
  377. package/src/editor/dataset/constant/Regular.ts +23 -0
  378. package/src/editor/dataset/constant/Separator.ts +6 -0
  379. package/src/editor/dataset/constant/Shortcut.ts +3 -0
  380. package/src/editor/dataset/constant/Table.ts +9 -0
  381. package/src/editor/dataset/constant/Title.ts +38 -0
  382. package/src/editor/dataset/constant/Watermark.ts +17 -0
  383. package/src/editor/dataset/constant/WhiteSpace.ts +7 -0
  384. package/src/editor/dataset/constant/Zone.ts +5 -0
  385. package/src/editor/dataset/enum/Area.ts +5 -0
  386. package/src/editor/dataset/enum/Background.ts +11 -0
  387. package/src/editor/dataset/enum/Block.ts +4 -0
  388. package/src/editor/dataset/enum/Common.ts +30 -0
  389. package/src/editor/dataset/enum/Control.ts +39 -0
  390. package/src/editor/dataset/enum/Editor.ts +51 -0
  391. package/src/editor/dataset/enum/Element.ts +21 -0
  392. package/src/editor/dataset/enum/ElementStyle.ts +12 -0
  393. package/src/editor/dataset/enum/Event.ts +5 -0
  394. package/src/editor/dataset/enum/KeyMap.ts +85 -0
  395. package/src/editor/dataset/enum/LineNumber.ts +4 -0
  396. package/src/editor/dataset/enum/List.ts +23 -0
  397. package/src/editor/dataset/enum/Observer.ts +6 -0
  398. package/src/editor/dataset/enum/Row.ts +7 -0
  399. package/src/editor/dataset/enum/Text.ts +13 -0
  400. package/src/editor/dataset/enum/Title.ts +8 -0
  401. package/src/editor/dataset/enum/VerticalAlign.ts +5 -0
  402. package/src/editor/dataset/enum/Watermark.ts +4 -0
  403. package/src/editor/dataset/enum/table/Table.ts +19 -0
  404. package/src/editor/dataset/enum/table/TableTool.ts +4 -0
  405. package/src/editor/index.ts +241 -0
  406. package/src/editor/interface/Area.ts +68 -0
  407. package/src/editor/interface/Background.ts +9 -0
  408. package/src/editor/interface/Badge.ts +17 -0
  409. package/src/editor/interface/Block.ts +18 -0
  410. package/src/editor/interface/Catalog.ts +11 -0
  411. package/src/editor/interface/Checkbox.ts +17 -0
  412. package/src/editor/interface/Command.ts +3 -0
  413. package/src/editor/interface/Common.ts +43 -0
  414. package/src/editor/interface/Control.ts +250 -0
  415. package/src/editor/interface/Cursor.ts +7 -0
  416. package/src/editor/interface/Draw.ts +86 -0
  417. package/src/editor/interface/Editor.ts +172 -0
  418. package/src/editor/interface/Element.ts +273 -0
  419. package/src/editor/interface/Event.ts +27 -0
  420. package/src/editor/interface/EventBus.ts +46 -0
  421. package/src/editor/interface/Footer.ts +9 -0
  422. package/src/editor/interface/Graffiti.ts +15 -0
  423. package/src/editor/interface/Group.ts +8 -0
  424. package/src/editor/interface/Header.ts +9 -0
  425. package/src/editor/interface/Label.ts +8 -0
  426. package/src/editor/interface/LineBreak.ts +5 -0
  427. package/src/editor/interface/LineNumber.ts +10 -0
  428. package/src/editor/interface/Listener.ts +88 -0
  429. package/src/editor/interface/Margin.ts +1 -0
  430. package/src/editor/interface/PageBorder.ts +8 -0
  431. package/src/editor/interface/PageBreak.ts +5 -0
  432. package/src/editor/interface/PageNumber.ts +16 -0
  433. package/src/editor/interface/Placeholder.ts +7 -0
  434. package/src/editor/interface/Plugin.ts +8 -0
  435. package/src/editor/interface/Position.ts +113 -0
  436. package/src/editor/interface/Previewer.ts +15 -0
  437. package/src/editor/interface/Radio.ts +17 -0
  438. package/src/editor/interface/Range.ts +61 -0
  439. package/src/editor/interface/Row.ts +25 -0
  440. package/src/editor/interface/Search.ts +36 -0
  441. package/src/editor/interface/Separator.ts +4 -0
  442. package/src/editor/interface/Text.ts +15 -0
  443. package/src/editor/interface/Title.ts +32 -0
  444. package/src/editor/interface/Watermark.ts +16 -0
  445. package/src/editor/interface/WhiteSpace.ts +5 -0
  446. package/src/editor/interface/Zone.ts +3 -0
  447. package/src/editor/interface/contextmenu/ContextMenu.ts +76 -0
  448. package/src/editor/interface/i18n/I18n.ts +7 -0
  449. package/src/editor/interface/shortcut/Shortcut.ts +14 -0
  450. package/src/editor/interface/table/Colgroup.ts +4 -0
  451. package/src/editor/interface/table/Table.ts +9 -0
  452. package/src/editor/interface/table/Td.ts +36 -0
  453. package/src/editor/interface/table/Tr.ts +11 -0
  454. package/src/editor/types/index.d.ts +5 -0
  455. package/src/editor/utils/clipboard.ts +94 -0
  456. package/src/editor/utils/element.ts +1877 -0
  457. package/src/editor/utils/hotkey.ts +5 -0
  458. package/src/editor/utils/index.ts +445 -0
  459. package/src/editor/utils/option.ts +253 -0
  460. package/src/editor/utils/paragraph.ts +28 -0
  461. package/src/editor/utils/print.ts +99 -0
  462. package/src/editor/utils/ua.ts +13 -0
  463. package/src/main.ts +2053 -0
  464. package/src/mock.ts +611 -0
  465. package/src/plugins/copy/index.ts +30 -0
  466. package/src/plugins/markdown/index.ts +118 -0
  467. package/src/style.css +1079 -0
  468. package/src/utils/index.ts +45 -0
  469. package/src/utils/prism.ts +89 -0
  470. package/src/vite-env.d.ts +1 -0
  471. package/tsconfig.json +25 -0
  472. package/vite.config.ts +46 -0
@@ -0,0 +1,158 @@
1
+ import { maxHeightRadioMapping } from '../../../dataset/constant/Common'
2
+ import { EditorZone } from '../../../dataset/enum/Editor'
3
+ import { DeepRequired } from '../../../interface/Common'
4
+ import { IEditorOption } from '../../../interface/Editor'
5
+ import { IElement, IElementPosition } from '../../../interface/Element'
6
+ import { IRow } from '../../../interface/Row'
7
+ import { pickSurroundElementList } from '../../../utils/element'
8
+ import { Position } from '../../position/Position'
9
+ import { Zone } from '../../zone/Zone'
10
+ import { Draw } from '../Draw'
11
+
12
+ export class Header {
13
+ private draw: Draw
14
+ private position: Position
15
+ private zone: Zone
16
+ private options: DeepRequired<IEditorOption>
17
+
18
+ private elementList: IElement[]
19
+ private rowList: IRow[]
20
+ private positionList: IElementPosition[]
21
+
22
+ constructor(draw: Draw, data?: IElement[]) {
23
+ this.draw = draw
24
+ this.position = draw.getPosition()
25
+ this.zone = draw.getZone()
26
+ this.options = draw.getOptions()
27
+
28
+ this.elementList = data || []
29
+ this.rowList = []
30
+ this.positionList = []
31
+ }
32
+
33
+ public getRowList(): IRow[] {
34
+ return this.rowList
35
+ }
36
+
37
+ public setElementList(elementList: IElement[]) {
38
+ this.elementList = elementList
39
+ }
40
+
41
+ public getElementList(): IElement[] {
42
+ return this.elementList
43
+ }
44
+
45
+ public getPositionList(): IElementPosition[] {
46
+ return this.positionList
47
+ }
48
+
49
+ public compute() {
50
+ this.recovery()
51
+ this._computeRowList()
52
+ this._computePositionList()
53
+ }
54
+
55
+ public recovery() {
56
+ this.rowList = []
57
+ this.positionList = []
58
+ }
59
+
60
+ private _computeRowList() {
61
+ const innerWidth = this.draw.getInnerWidth()
62
+ const margins = this.draw.getMargins()
63
+ const surroundElementList = pickSurroundElementList(this.elementList)
64
+ this.rowList = this.draw.computeRowList({
65
+ startX: margins[3],
66
+ startY: this.getHeaderTop(),
67
+ innerWidth,
68
+ elementList: this.elementList,
69
+ surroundElementList
70
+ })
71
+ }
72
+
73
+ private _computePositionList() {
74
+ const headerTop = this.getHeaderTop()
75
+ const innerWidth = this.draw.getInnerWidth()
76
+ const margins = this.draw.getMargins()
77
+ const startX = margins[3]
78
+ const startY = headerTop
79
+ this.position.computePageRowPosition({
80
+ positionList: this.positionList,
81
+ rowList: this.rowList,
82
+ pageNo: 0,
83
+ startRowIndex: 0,
84
+ startIndex: 0,
85
+ startX,
86
+ startY,
87
+ innerWidth,
88
+ zone: EditorZone.HEADER
89
+ })
90
+ }
91
+
92
+ public getHeaderTop(): number {
93
+ const {
94
+ header: { top, disabled },
95
+ scale
96
+ } = this.options
97
+ if (disabled) return 0
98
+ return Math.floor(top * scale)
99
+ }
100
+
101
+ public getMaxHeight(): number {
102
+ const {
103
+ header: { maxHeightRadio }
104
+ } = this.options
105
+ const height = this.draw.getHeight()
106
+ return Math.floor(height * maxHeightRadioMapping[maxHeightRadio])
107
+ }
108
+
109
+ public getHeight(): number {
110
+ if (this.options.header.disabled) return 0
111
+ const maxHeight = this.getMaxHeight()
112
+ const rowHeight = this.getRowHeight()
113
+ return rowHeight > maxHeight ? maxHeight : rowHeight
114
+ }
115
+
116
+ public getRowHeight(): number {
117
+ return this.rowList.reduce((pre, cur) => pre + cur.height, 0)
118
+ }
119
+
120
+ public getExtraHeight(): number {
121
+ // 页眉上边距 + 实际高 - 页面上边距
122
+ const margins = this.draw.getMargins()
123
+ const headerHeight = this.getHeight()
124
+ const headerTop = this.getHeaderTop()
125
+ const extraHeight = headerTop + headerHeight - margins[0]
126
+ return extraHeight <= 0 ? 0 : extraHeight
127
+ }
128
+
129
+ public render(ctx: CanvasRenderingContext2D, pageNo: number) {
130
+ ctx.save()
131
+ ctx.globalAlpha = this.zone.isHeaderActive()
132
+ ? 1
133
+ : this.options.header.inactiveAlpha
134
+ const innerWidth = this.draw.getInnerWidth()
135
+ const maxHeight = this.getMaxHeight()
136
+ // 超出最大高度不渲染
137
+ const rowList: IRow[] = []
138
+ let curRowHeight = 0
139
+ for (let r = 0; r < this.rowList.length; r++) {
140
+ const row = this.rowList[r]
141
+ if (curRowHeight + row.height > maxHeight) {
142
+ break
143
+ }
144
+ rowList.push(row)
145
+ curRowHeight += row.height
146
+ }
147
+ this.draw.drawRow(ctx, {
148
+ elementList: this.elementList,
149
+ positionList: this.positionList,
150
+ rowList,
151
+ pageNo,
152
+ startIndex: 0,
153
+ innerWidth,
154
+ zone: EditorZone.HEADER
155
+ })
156
+ ctx.restore()
157
+ }
158
+ }
@@ -0,0 +1,43 @@
1
+ import { LineNumberType } from '../../../dataset/enum/LineNumber'
2
+ import { DeepRequired } from '../../../interface/Common'
3
+ import { IEditorOption } from '../../../interface/Editor'
4
+ import { Draw } from '../Draw'
5
+
6
+ export class LineNumber {
7
+ private draw: Draw
8
+ private options: DeepRequired<IEditorOption>
9
+
10
+ constructor(draw: Draw) {
11
+ this.draw = draw
12
+ this.options = draw.getOptions()
13
+ }
14
+
15
+ public render(ctx: CanvasRenderingContext2D, pageNo: number) {
16
+ const {
17
+ scale,
18
+ lineNumber: { color, size, font, right, type }
19
+ } = this.options
20
+ const textParticle = this.draw.getTextParticle()
21
+ const margins = this.draw.getMargins()
22
+ const positionList = this.draw.getPosition().getOriginalMainPositionList()
23
+ const pageRowList = this.draw.getPageRowList()
24
+ const rowList = pageRowList[pageNo]
25
+ ctx.save()
26
+ ctx.fillStyle = color
27
+ ctx.font = `${size * scale}px ${font}`
28
+ for (let i = 0; i < rowList.length; i++) {
29
+ const row = rowList[i]
30
+ const {
31
+ coordinate: { leftBottom }
32
+ } = positionList[row.startIndex]
33
+ const seq = type === LineNumberType.PAGE ? i + 1 : row.rowIndex + 1
34
+ const textMetrics = textParticle.measureText(ctx, {
35
+ value: `${seq}`
36
+ })
37
+ const x = margins[3] - (textMetrics.width + right) * scale
38
+ const y = leftBottom[1] - textMetrics.actualBoundingBoxAscent * scale
39
+ ctx.fillText(`${seq}`, x, y)
40
+ }
41
+ ctx.restore()
42
+ }
43
+ }
@@ -0,0 +1,53 @@
1
+ import { PageMode } from '../../../dataset/enum/Editor'
2
+ import { IEditorOption } from '../../../interface/Editor'
3
+ import { Draw } from '../Draw'
4
+
5
+ export class Margin {
6
+ private draw: Draw
7
+ private options: Required<IEditorOption>
8
+
9
+ constructor(draw: Draw) {
10
+ this.draw = draw
11
+ this.options = draw.getOptions()
12
+ }
13
+
14
+ public render(ctx: CanvasRenderingContext2D, pageNo: number) {
15
+ const { marginIndicatorColor, pageMode } = this.options
16
+ const width = this.draw.getWidth()
17
+ const height =
18
+ pageMode === PageMode.CONTINUITY
19
+ ? this.draw.getCanvasHeight(pageNo) / this.draw.getPagePixelRatio()
20
+ : this.draw.getHeight()
21
+ const margins = this.draw.getMargins()
22
+ const marginIndicatorSize = this.draw.getMarginIndicatorSize()
23
+ ctx.save()
24
+ ctx.translate(0.5, 0.5)
25
+ ctx.strokeStyle = marginIndicatorColor
26
+ ctx.beginPath()
27
+ const leftTopPoint: [number, number] = [margins[3], margins[0]]
28
+ const rightTopPoint: [number, number] = [width - margins[1], margins[0]]
29
+ const leftBottomPoint: [number, number] = [margins[3], height - margins[2]]
30
+ const rightBottomPoint: [number, number] = [
31
+ width - margins[1],
32
+ height - margins[2]
33
+ ]
34
+ // 上左
35
+ ctx.moveTo(leftTopPoint[0] - marginIndicatorSize, leftTopPoint[1])
36
+ ctx.lineTo(...leftTopPoint)
37
+ ctx.lineTo(leftTopPoint[0], leftTopPoint[1] - marginIndicatorSize)
38
+ // 上右
39
+ ctx.moveTo(rightTopPoint[0] + marginIndicatorSize, rightTopPoint[1])
40
+ ctx.lineTo(...rightTopPoint)
41
+ ctx.lineTo(rightTopPoint[0], rightTopPoint[1] - marginIndicatorSize)
42
+ // 下左
43
+ ctx.moveTo(leftBottomPoint[0] - marginIndicatorSize, leftBottomPoint[1])
44
+ ctx.lineTo(...leftBottomPoint)
45
+ ctx.lineTo(leftBottomPoint[0], leftBottomPoint[1] + marginIndicatorSize)
46
+ // 下右
47
+ ctx.moveTo(rightBottomPoint[0] + marginIndicatorSize, rightBottomPoint[1])
48
+ ctx.lineTo(...rightBottomPoint)
49
+ ctx.lineTo(rightBottomPoint[0], rightBottomPoint[1] + marginIndicatorSize)
50
+ ctx.stroke()
51
+ ctx.restore()
52
+ }
53
+ }
@@ -0,0 +1,47 @@
1
+ import { DeepRequired } from '../../../interface/Common'
2
+ import { IEditorOption } from '../../../interface/Editor'
3
+ import { Draw } from '../Draw'
4
+ import { Footer } from './Footer'
5
+ import { Header } from './Header'
6
+
7
+ export class PageBorder {
8
+ private draw: Draw
9
+ private header: Header
10
+ private footer: Footer
11
+ private options: DeepRequired<IEditorOption>
12
+
13
+ constructor(draw: Draw) {
14
+ this.draw = draw
15
+ this.header = draw.getHeader()
16
+ this.footer = draw.getFooter()
17
+ this.options = draw.getOptions()
18
+ }
19
+
20
+ public render(ctx: CanvasRenderingContext2D) {
21
+ const {
22
+ scale,
23
+ pageBorder: { color, lineWidth, padding }
24
+ } = this.options
25
+ ctx.save()
26
+ ctx.translate(0.5, 0.5)
27
+ ctx.strokeStyle = color
28
+ ctx.lineWidth = lineWidth * scale
29
+ const margins = this.draw.getMargins()
30
+ // x:左边距 - 左距离正文距离
31
+ const x = margins[3] - padding[3] * scale
32
+ // y:页眉上边距 + 页眉高度 - 上距离正文距离
33
+ const y = margins[0] + this.header.getExtraHeight() - padding[0] * scale
34
+ // width:页面宽度 + 左右距离正文距离
35
+ const width = this.draw.getInnerWidth() + (padding[1] + padding[3]) * scale
36
+ // height:页面高度 - 正文起始位置 - 页脚高度 - 下边距 - 下距离正文距离
37
+ const height =
38
+ this.draw.getHeight() -
39
+ y -
40
+ this.footer.getExtraHeight() -
41
+ margins[2] +
42
+ padding[2] * scale
43
+ ctx.rect(x, y, width, height)
44
+ ctx.stroke()
45
+ ctx.restore()
46
+ }
47
+ }
@@ -0,0 +1,88 @@
1
+ import { FORMAT_PLACEHOLDER } from '../../../dataset/constant/PageNumber'
2
+ import { NumberType } from '../../../dataset/enum/Common'
3
+ import { RowFlex } from '../../../dataset/enum/Row'
4
+ import { DeepRequired } from '../../../interface/Common'
5
+ import { IEditorOption } from '../../../interface/Editor'
6
+ import { convertNumberToChinese } from '../../../utils'
7
+ import { Draw } from '../Draw'
8
+
9
+ export class PageNumber {
10
+ private draw: Draw
11
+ private options: DeepRequired<IEditorOption>
12
+
13
+ constructor(draw: Draw) {
14
+ this.draw = draw
15
+ this.options = draw.getOptions()
16
+ }
17
+
18
+ static formatNumberPlaceholder(
19
+ text: string,
20
+ pageNo: number,
21
+ replaceReg: RegExp,
22
+ numberType: NumberType
23
+ ) {
24
+ const pageNoText =
25
+ numberType === NumberType.CHINESE
26
+ ? convertNumberToChinese(pageNo)
27
+ : `${pageNo}`
28
+ return text.replace(replaceReg, pageNoText)
29
+ }
30
+
31
+ public render(ctx: CanvasRenderingContext2D, pageNo: number) {
32
+ const {
33
+ scale,
34
+ pageNumber: {
35
+ size,
36
+ font,
37
+ color,
38
+ rowFlex,
39
+ numberType,
40
+ format,
41
+ startPageNo,
42
+ fromPageNo
43
+ }
44
+ } = this.options
45
+ if (pageNo < fromPageNo) return
46
+ // 处理页码格式
47
+ let text = format
48
+ const pageNoReg = new RegExp(FORMAT_PLACEHOLDER.PAGE_NO)
49
+ if (pageNoReg.test(text)) {
50
+ text = PageNumber.formatNumberPlaceholder(
51
+ text,
52
+ pageNo + startPageNo - fromPageNo,
53
+ pageNoReg,
54
+ numberType
55
+ )
56
+ }
57
+ const pageCountReg = new RegExp(FORMAT_PLACEHOLDER.PAGE_COUNT)
58
+ if (pageCountReg.test(text)) {
59
+ text = PageNumber.formatNumberPlaceholder(
60
+ text,
61
+ this.draw.getPageCount() - fromPageNo,
62
+ pageCountReg,
63
+ numberType
64
+ )
65
+ }
66
+ const width = this.draw.getWidth()
67
+ // 计算y位置
68
+ const height = this.draw.getHeight()
69
+ const pageNumberBottom = this.draw.getPageNumberBottom()
70
+ const y = height - pageNumberBottom
71
+ ctx.save()
72
+ ctx.fillStyle = color
73
+ ctx.font = `${size * scale}px ${font}`
74
+ // 计算x位置-居左、居中、居右
75
+ let x = 0
76
+ const margins = this.draw.getMargins()
77
+ const { width: textWidth } = ctx.measureText(text)
78
+ if (rowFlex === RowFlex.CENTER) {
79
+ x = (width - textWidth) / 2
80
+ } else if (rowFlex === RowFlex.RIGHT) {
81
+ x = width - textWidth - margins[1]
82
+ } else {
83
+ x = margins[3]
84
+ }
85
+ ctx.fillText(text, x, y)
86
+ ctx.restore()
87
+ }
88
+ }
@@ -0,0 +1,114 @@
1
+ import { IEditorOption, IElement } from '../../..'
2
+ import { DeepRequired } from '../../../interface/Common'
3
+ import { IElementPosition } from '../../../interface/Element'
4
+ import { IPlaceholder } from '../../../interface/Placeholder'
5
+ import { IRow } from '../../../interface/Row'
6
+ import { formatElementList } from '../../../utils/element'
7
+ import { Position } from '../../position/Position'
8
+ import { Draw } from '../Draw'
9
+ import { LineBreakParticle } from '../particle/LineBreakParticle'
10
+
11
+ export interface IPlaceholderRenderOption {
12
+ placeholder: Required<IPlaceholder>
13
+ startY?: number
14
+ }
15
+
16
+ export class Placeholder {
17
+ private draw: Draw
18
+ private position: Position
19
+ private options: DeepRequired<IEditorOption>
20
+
21
+ private elementList: IElement[]
22
+ private rowList: IRow[]
23
+ private positionList: IElementPosition[]
24
+
25
+ constructor(draw: Draw) {
26
+ this.draw = draw
27
+ this.position = draw.getPosition()
28
+ this.options = <DeepRequired<IEditorOption>>draw.getOptions()
29
+
30
+ this.elementList = []
31
+ this.rowList = []
32
+ this.positionList = []
33
+ }
34
+
35
+ private _recovery() {
36
+ this.elementList = []
37
+ this.rowList = []
38
+ this.positionList = []
39
+ }
40
+
41
+ public _compute(options?: IPlaceholderRenderOption) {
42
+ this._computeRowList()
43
+ this._computePositionList(options)
44
+ }
45
+
46
+ private _computeRowList() {
47
+ const innerWidth = this.draw.getInnerWidth()
48
+ this.rowList = this.draw.computeRowList({
49
+ innerWidth,
50
+ elementList: this.elementList
51
+ })
52
+ }
53
+
54
+ private _computePositionList(options?: IPlaceholderRenderOption) {
55
+ const { lineBreak, scale } = this.options
56
+ const headerExtraHeight = this.draw.getHeader().getExtraHeight()
57
+ const innerWidth = this.draw.getInnerWidth()
58
+ const margins = this.draw.getMargins()
59
+ let startX = margins[3]
60
+ // 换行符绘制开启时,移动起始位置
61
+ if (!lineBreak.disabled) {
62
+ startX += (LineBreakParticle.WIDTH + LineBreakParticle.GAP) * scale
63
+ }
64
+ const startY = options?.startY || margins[0] + headerExtraHeight
65
+ this.position.computePageRowPosition({
66
+ positionList: this.positionList,
67
+ rowList: this.rowList,
68
+ pageNo: 0,
69
+ startRowIndex: 0,
70
+ startIndex: 0,
71
+ startX,
72
+ startY,
73
+ innerWidth
74
+ })
75
+ }
76
+
77
+ public render(
78
+ ctx: CanvasRenderingContext2D,
79
+ options?: IPlaceholderRenderOption
80
+ ) {
81
+ const { placeholder = this.options.placeholder } = options || {}
82
+ const { data, font, size, color, opacity } = placeholder
83
+ this._recovery()
84
+ // 构建元素列表并格式化
85
+ this.elementList = [
86
+ {
87
+ value: data,
88
+ font,
89
+ size,
90
+ color
91
+ }
92
+ ]
93
+ formatElementList(this.elementList, {
94
+ editorOptions: this.options,
95
+ isForceCompensation: true
96
+ })
97
+ // 计算
98
+ this._compute(options)
99
+ const innerWidth = this.draw.getInnerWidth()
100
+ // 绘制
101
+ ctx.save()
102
+ ctx.globalAlpha = opacity
103
+ this.draw.drawRow(ctx, {
104
+ elementList: this.elementList,
105
+ positionList: this.positionList,
106
+ rowList: this.rowList,
107
+ pageNo: 0,
108
+ startIndex: 0,
109
+ innerWidth,
110
+ isDrawLineBreak: false
111
+ })
112
+ ctx.restore()
113
+ }
114
+ }
@@ -0,0 +1,188 @@
1
+ import { IEditorOption } from '../../..'
2
+ import { FORMAT_PLACEHOLDER } from '../../../dataset/constant/PageNumber'
3
+ import { WatermarkType } from '../../../dataset/enum/Watermark'
4
+ import { DeepRequired } from '../../../interface/Common'
5
+ import { Draw } from '../Draw'
6
+ import { PageNumber } from './PageNumber'
7
+
8
+ export class Watermark {
9
+ private draw: Draw
10
+ private options: DeepRequired<IEditorOption>
11
+ private imageCache: Map<string, HTMLImageElement>
12
+
13
+ constructor(draw: Draw) {
14
+ this.draw = draw
15
+ this.options = <DeepRequired<IEditorOption>>draw.getOptions()
16
+ this.imageCache = new Map()
17
+ }
18
+
19
+ public renderText(ctx: CanvasRenderingContext2D, pageNo: number) {
20
+ const {
21
+ watermark: { data, opacity, font, size, color, repeat, gap, numberType },
22
+ scale
23
+ } = this.options
24
+ const width = this.draw.getWidth()
25
+ const height = this.draw.getHeight()
26
+ // 开始绘制
27
+ ctx.save()
28
+ ctx.globalAlpha = opacity
29
+ ctx.font = `${size * scale}px ${font}`
30
+ // 格式化文本
31
+ let text = data
32
+ const pageNoReg = new RegExp(FORMAT_PLACEHOLDER.PAGE_NO)
33
+ if (pageNoReg.test(text)) {
34
+ text = PageNumber.formatNumberPlaceholder(
35
+ text,
36
+ pageNo + 1,
37
+ pageNoReg,
38
+ numberType
39
+ )
40
+ }
41
+ const pageCountReg = new RegExp(FORMAT_PLACEHOLDER.PAGE_COUNT)
42
+ if (pageCountReg.test(text)) {
43
+ text = PageNumber.formatNumberPlaceholder(
44
+ text,
45
+ this.draw.getPageCount(),
46
+ pageCountReg,
47
+ numberType
48
+ )
49
+ }
50
+ // 测量长度并绘制
51
+ const measureText = ctx.measureText(text)
52
+ if (repeat) {
53
+ const dpr = this.draw.getPagePixelRatio()
54
+ const temporaryCanvas = document.createElement('canvas')
55
+ const temporaryCtx = temporaryCanvas.getContext('2d')!
56
+ // 勾股定理计算旋转后的宽高对角线尺寸 a^2 + b^2 = c^2
57
+ const textWidth = measureText.width
58
+ const textHeight =
59
+ measureText.actualBoundingBoxAscent +
60
+ measureText.actualBoundingBoxDescent
61
+ const diagonalLength = Math.sqrt(
62
+ Math.pow(textWidth, 2) + Math.pow(textHeight, 2)
63
+ )
64
+ // 加上 gap 间距
65
+ const patternWidth = diagonalLength + 2 * gap[0] * scale
66
+ const patternHeight = diagonalLength + 2 * gap[1] * scale
67
+ // 宽高设置
68
+ temporaryCanvas.width = patternWidth
69
+ temporaryCanvas.height = patternHeight
70
+ temporaryCanvas.style.width = `${patternWidth * dpr}px`
71
+ temporaryCanvas.style.height = `${patternHeight * dpr}px`
72
+ // 旋转45度
73
+ temporaryCtx.translate(patternWidth / 2, patternHeight / 2)
74
+ temporaryCtx.rotate((-45 * Math.PI) / 180)
75
+ temporaryCtx.translate(-patternWidth / 2, -patternHeight / 2)
76
+ // 绘制文本
77
+ temporaryCtx.font = `${size * scale}px ${font}`
78
+ temporaryCtx.fillStyle = color
79
+ temporaryCtx.fillText(
80
+ text,
81
+ (patternWidth - textWidth) / 2,
82
+ (patternHeight - textHeight) / 2 + measureText.actualBoundingBoxAscent
83
+ )
84
+ // 创建平铺模式
85
+ const pattern = ctx.createPattern(temporaryCanvas, 'repeat')
86
+ if (pattern) {
87
+ ctx.fillStyle = pattern
88
+ ctx.fillRect(0, 0, width, height)
89
+ }
90
+ } else {
91
+ const x = width / 2
92
+ const y = height / 2
93
+ ctx.fillStyle = color
94
+ ctx.translate(x, y)
95
+ ctx.rotate((-45 * Math.PI) / 180)
96
+ ctx.fillText(
97
+ text,
98
+ -measureText.width / 2,
99
+ measureText.actualBoundingBoxAscent - (size * scale) / 2
100
+ )
101
+ }
102
+ ctx.restore()
103
+ }
104
+
105
+ public renderImage(ctx: CanvasRenderingContext2D) {
106
+ const {
107
+ watermark: { width, height, data, opacity, repeat, gap },
108
+ scale
109
+ } = this.options
110
+ if (!this.imageCache.has(data)) {
111
+ const img = new Image()
112
+ img.setAttribute('crossOrigin', 'Anonymous')
113
+ img.src = data
114
+ img.onload = () => {
115
+ this.imageCache.set(data, img)
116
+ // 避免层级上浮,触发编辑器二次渲染
117
+ this.draw.render({
118
+ isCompute: false,
119
+ isSubmitHistory: false
120
+ })
121
+ }
122
+ return
123
+ }
124
+ const docWidth = this.draw.getWidth()
125
+ const docHeight = this.draw.getHeight()
126
+ const imageWidth = width * scale
127
+ const imageHeight = height * scale
128
+ // 开始绘制
129
+ ctx.save()
130
+ ctx.globalAlpha = opacity
131
+ if (repeat) {
132
+ const dpr = this.draw.getPagePixelRatio()
133
+ const temporaryCanvas = document.createElement('canvas')
134
+ const temporaryCtx = temporaryCanvas.getContext('2d')!
135
+ // 勾股定理计算旋转后的宽高对角线尺寸 a^2 + b^2 = c^2
136
+ const diagonalLength = Math.sqrt(
137
+ Math.pow(imageWidth, 2) + Math.pow(imageHeight, 2)
138
+ )
139
+ // 加上 gap 间距
140
+ const patternWidth = diagonalLength + 2 * gap[0] * scale
141
+ const patternHeight = diagonalLength + 2 * gap[1] * scale
142
+ // 宽高设置
143
+ temporaryCanvas.width = patternWidth
144
+ temporaryCanvas.height = patternHeight
145
+ temporaryCanvas.style.width = `${patternWidth * dpr}px`
146
+ temporaryCanvas.style.height = `${patternHeight * dpr}px`
147
+ // 旋转45度
148
+ temporaryCtx.translate(patternWidth / 2, patternHeight / 2)
149
+ temporaryCtx.rotate((-45 * Math.PI) / 180)
150
+ temporaryCtx.translate(-patternWidth / 2, -patternHeight / 2)
151
+ // 绘制图片
152
+ temporaryCtx.drawImage(
153
+ this.imageCache.get(data)!,
154
+ (patternWidth - imageWidth) / 2,
155
+ (patternHeight - imageHeight) / 2,
156
+ imageWidth,
157
+ imageHeight
158
+ )
159
+ // 创建平铺模式
160
+ const pattern = ctx.createPattern(temporaryCanvas, 'repeat')
161
+ if (pattern) {
162
+ ctx.fillStyle = pattern
163
+ ctx.fillRect(0, 0, docWidth, docHeight)
164
+ }
165
+ } else {
166
+ const x = docWidth / 2
167
+ const y = docHeight / 2
168
+ ctx.translate(x, y)
169
+ ctx.rotate((-45 * Math.PI) / 180)
170
+ ctx.drawImage(
171
+ this.imageCache.get(data)!,
172
+ -imageWidth / 2,
173
+ -imageHeight / 2,
174
+ imageWidth,
175
+ imageHeight
176
+ )
177
+ }
178
+ ctx.restore()
179
+ }
180
+
181
+ public render(ctx: CanvasRenderingContext2D, pageNo: number) {
182
+ if (this.options.watermark.type === WatermarkType.IMAGE) {
183
+ this.renderImage(ctx)
184
+ } else {
185
+ this.renderText(ctx, pageNo)
186
+ }
187
+ }
188
+ }