@antv/infographic 0.1.1 → 0.1.3

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 (556) hide show
  1. package/README.md +26 -2
  2. package/README.zh-CN.md +26 -3
  3. package/dist/infographic.min.js +484 -79
  4. package/dist/infographic.min.js.map +1 -1
  5. package/esm/constants/components.d.ts +1 -0
  6. package/esm/constants/components.js +1 -0
  7. package/esm/constants/element.d.ts +17 -6
  8. package/esm/constants/index.d.ts +2 -0
  9. package/esm/constants/index.js +2 -0
  10. package/esm/constants/shape.d.ts +16 -0
  11. package/esm/designs/components/Btn.js +2 -2
  12. package/esm/designs/components/BtnsGroup.js +1 -1
  13. package/esm/designs/components/Illus.js +2 -2
  14. package/esm/designs/components/ItemDesc.js +1 -2
  15. package/esm/designs/components/ItemIcon.js +1 -1
  16. package/esm/designs/components/ItemLabel.js +1 -2
  17. package/esm/designs/components/ItemValue.js +1 -2
  18. package/esm/designs/components/ItemsGroup.js +1 -1
  19. package/esm/designs/components/Title.js +2 -5
  20. package/esm/designs/items/CandyCardLite.js +1 -1
  21. package/esm/designs/items/CircularProgress.js +2 -1
  22. package/esm/designs/items/HorizontalIconArrow.js +1 -2
  23. package/esm/designs/items/HorizontalIconLine.js +1 -1
  24. package/esm/designs/items/IconBadge.js +1 -1
  25. package/esm/designs/items/LinedText.d.ts +12 -0
  26. package/esm/designs/items/LinedText.js +57 -0
  27. package/esm/designs/items/PlainText.d.ts +1 -0
  28. package/esm/designs/items/PlainText.js +16 -3
  29. package/esm/designs/items/QuarterCircular.js +1 -31
  30. package/esm/designs/items/index.d.ts +1 -0
  31. package/esm/designs/items/index.js +1 -0
  32. package/esm/designs/layouts/Align.js +31 -45
  33. package/esm/designs/layouts/Flex.js +33 -29
  34. package/esm/designs/structures/chart-bar.d.ts +15 -0
  35. package/esm/designs/structures/chart-bar.js +99 -0
  36. package/esm/designs/structures/chart-line.d.ts +12 -0
  37. package/esm/designs/structures/chart-line.js +164 -0
  38. package/esm/designs/structures/chart-pie.d.ts +9 -0
  39. package/esm/designs/structures/chart-pie.js +127 -0
  40. package/esm/designs/structures/compare-binary-horizontal/dividers/pros-cons-arrow.js +1 -1
  41. package/esm/designs/structures/hierarchy-mindmap.d.ts +24 -0
  42. package/esm/designs/structures/hierarchy-mindmap.js +199 -0
  43. package/esm/designs/structures/index.d.ts +4 -0
  44. package/esm/designs/structures/index.js +4 -0
  45. package/esm/designs/structures/list-sector.js +1 -1
  46. package/esm/designs/structures/sequence-circular.js +1 -1
  47. package/esm/designs/structures/sequence-cylinders-3d.js +4 -4
  48. package/esm/designs/structures/sequence-roadmap-vertical.js +1 -1
  49. package/esm/designs/structures/sequence-zigzag-pucks-3d.js +5 -5
  50. package/esm/editor/commands/Batch.d.ts +11 -0
  51. package/esm/editor/commands/Batch.js +21 -0
  52. package/esm/editor/commands/UpdateElement.d.ts +16 -0
  53. package/esm/editor/commands/UpdateElement.js +83 -0
  54. package/esm/editor/commands/UpdateOptions.d.ts +14 -0
  55. package/esm/editor/commands/UpdateOptions.js +25 -0
  56. package/esm/editor/commands/UpdateText.d.ts +16 -0
  57. package/esm/editor/commands/UpdateText.js +40 -0
  58. package/esm/editor/commands/index.d.ts +4 -0
  59. package/esm/editor/commands/index.js +4 -0
  60. package/esm/editor/editor.d.ts +16 -0
  61. package/esm/editor/editor.js +50 -0
  62. package/esm/editor/index.d.ts +4 -0
  63. package/esm/editor/index.js +3 -0
  64. package/esm/editor/interactions/base.d.ts +12 -0
  65. package/esm/editor/interactions/base.js +5 -0
  66. package/esm/editor/interactions/brush-select.d.ts +23 -0
  67. package/esm/editor/interactions/brush-select.js +167 -0
  68. package/esm/editor/interactions/click-select.d.ts +12 -0
  69. package/esm/editor/interactions/click-select.js +67 -0
  70. package/esm/editor/interactions/dblclick-edit-text.d.ts +10 -0
  71. package/esm/editor/interactions/dblclick-edit-text.js +204 -0
  72. package/esm/editor/interactions/drag-element.d.ts +41 -0
  73. package/esm/editor/interactions/drag-element.js +391 -0
  74. package/esm/editor/interactions/hotkey-history.d.ts +10 -0
  75. package/esm/editor/interactions/hotkey-history.js +27 -0
  76. package/esm/editor/interactions/index.d.ts +8 -0
  77. package/esm/editor/interactions/index.js +8 -0
  78. package/esm/editor/interactions/select-highlight.d.ts +16 -0
  79. package/esm/editor/interactions/select-highlight.js +99 -0
  80. package/esm/editor/interactions/zoom-wheel.d.ts +8 -0
  81. package/esm/editor/interactions/zoom-wheel.js +46 -0
  82. package/esm/editor/managers/command.d.ts +19 -0
  83. package/esm/editor/managers/command.js +63 -0
  84. package/esm/editor/managers/index.d.ts +4 -0
  85. package/esm/editor/managers/index.js +4 -0
  86. package/esm/editor/managers/interaction.d.ts +33 -0
  87. package/esm/editor/managers/interaction.js +196 -0
  88. package/esm/editor/managers/plugin.d.ts +14 -0
  89. package/esm/editor/managers/plugin.js +45 -0
  90. package/esm/editor/managers/state.d.ts +22 -0
  91. package/esm/editor/managers/state.js +141 -0
  92. package/esm/editor/plugins/base.d.ts +12 -0
  93. package/esm/editor/plugins/base.js +5 -0
  94. package/esm/editor/plugins/edit-bar/components/button.d.ts +12 -0
  95. package/esm/editor/plugins/edit-bar/components/button.js +53 -0
  96. package/esm/editor/plugins/edit-bar/components/color-picker.d.ts +10 -0
  97. package/esm/editor/plugins/edit-bar/components/color-picker.js +351 -0
  98. package/esm/editor/plugins/edit-bar/components/icons.d.ts +26 -0
  99. package/esm/editor/plugins/edit-bar/components/icons.js +30 -0
  100. package/esm/editor/plugins/edit-bar/components/index.d.ts +5 -0
  101. package/esm/editor/plugins/edit-bar/components/index.js +5 -0
  102. package/esm/editor/plugins/edit-bar/components/popover.d.ts +23 -0
  103. package/esm/editor/plugins/edit-bar/components/popover.js +378 -0
  104. package/esm/editor/plugins/edit-bar/components/select.d.ts +22 -0
  105. package/esm/editor/plugins/edit-bar/components/select.js +201 -0
  106. package/esm/editor/plugins/edit-bar/edit-bar.d.ts +32 -0
  107. package/esm/editor/plugins/edit-bar/edit-bar.js +211 -0
  108. package/esm/editor/plugins/edit-bar/edit-items/align-elements.d.ts +2 -0
  109. package/esm/editor/plugins/edit-bar/edit-items/align-elements.js +312 -0
  110. package/esm/editor/plugins/edit-bar/edit-items/font-align.d.ts +3 -0
  111. package/esm/editor/plugins/edit-bar/edit-items/font-align.js +81 -0
  112. package/esm/editor/plugins/edit-bar/edit-items/font-color.d.ts +3 -0
  113. package/esm/editor/plugins/edit-bar/edit-items/font-color.js +77 -0
  114. package/esm/editor/plugins/edit-bar/edit-items/font-family.d.ts +3 -0
  115. package/esm/editor/plugins/edit-bar/edit-items/font-family.js +115 -0
  116. package/esm/editor/plugins/edit-bar/edit-items/font-size.d.ts +3 -0
  117. package/esm/editor/plugins/edit-bar/edit-items/font-size.js +88 -0
  118. package/esm/editor/plugins/edit-bar/edit-items/icon-color.d.ts +3 -0
  119. package/esm/editor/plugins/edit-bar/edit-items/icon-color.js +77 -0
  120. package/esm/editor/plugins/edit-bar/edit-items/index.d.ts +7 -0
  121. package/esm/editor/plugins/edit-bar/edit-items/index.js +6 -0
  122. package/esm/editor/plugins/edit-bar/edit-items/types.d.ts +3 -0
  123. package/esm/editor/plugins/edit-bar/edit-items/types.js +1 -0
  124. package/esm/editor/plugins/edit-bar/index.d.ts +4 -0
  125. package/esm/editor/plugins/edit-bar/index.js +2 -0
  126. package/esm/editor/plugins/edit-bar/utils.d.ts +4 -0
  127. package/esm/editor/plugins/edit-bar/utils.js +14 -0
  128. package/esm/editor/plugins/index.d.ts +3 -0
  129. package/esm/editor/plugins/index.js +3 -0
  130. package/esm/editor/plugins/resize-element.d.ts +45 -0
  131. package/esm/editor/plugins/resize-element.js +393 -0
  132. package/esm/editor/types/command.d.ts +28 -0
  133. package/esm/editor/types/command.js +1 -0
  134. package/esm/editor/types/editor.d.ts +4 -0
  135. package/esm/editor/types/editor.js +1 -0
  136. package/esm/editor/types/index.d.ts +7 -0
  137. package/esm/editor/types/index.js +1 -0
  138. package/esm/editor/types/interaction.d.ts +44 -0
  139. package/esm/editor/types/interaction.js +1 -0
  140. package/esm/editor/types/plugin.d.ts +30 -0
  141. package/esm/editor/types/plugin.js +1 -0
  142. package/esm/editor/types/selection.d.ts +2 -0
  143. package/esm/editor/types/selection.js +1 -0
  144. package/esm/editor/types/shape.d.ts +16 -0
  145. package/esm/editor/types/shape.js +1 -0
  146. package/esm/editor/types/state.d.ts +57 -0
  147. package/esm/editor/types/state.js +1 -0
  148. package/esm/editor/utils/click-handler.d.ts +28 -0
  149. package/esm/editor/utils/click-handler.js +97 -0
  150. package/esm/editor/utils/coordinate.d.ts +6 -0
  151. package/esm/editor/utils/coordinate.js +31 -0
  152. package/esm/editor/utils/data.d.ts +10 -0
  153. package/esm/editor/utils/data.js +18 -0
  154. package/esm/editor/utils/element.d.ts +2 -0
  155. package/esm/editor/utils/element.js +9 -0
  156. package/esm/editor/utils/event.d.ts +3 -0
  157. package/esm/editor/utils/event.js +63 -0
  158. package/esm/editor/utils/extension.d.ts +13 -0
  159. package/esm/editor/utils/extension.js +39 -0
  160. package/esm/editor/utils/hotkey.d.ts +20 -0
  161. package/esm/editor/utils/hotkey.js +109 -0
  162. package/esm/editor/utils/index.d.ts +7 -0
  163. package/esm/editor/utils/index.js +7 -0
  164. package/esm/exporter/svg.js +7 -5
  165. package/esm/index.d.ts +5 -2
  166. package/esm/index.js +2 -0
  167. package/esm/jsx/components/Polygon.js +2 -1
  168. package/esm/jsx/components/Text.js +66 -68
  169. package/esm/options/types.d.ts +15 -2
  170. package/esm/renderer/composites/background.js +2 -2
  171. package/esm/renderer/composites/button.js +4 -3
  172. package/esm/renderer/composites/icon.d.ts +1 -1
  173. package/esm/renderer/composites/icon.js +2 -0
  174. package/esm/renderer/composites/text.js +2 -1
  175. package/esm/renderer/renderer.js +3 -2
  176. package/esm/renderer/stylize/rough.js +3 -7
  177. package/esm/renderer/types/index.d.ts +0 -1
  178. package/esm/resource/utils/index.d.ts +0 -1
  179. package/esm/resource/utils/index.js +0 -1
  180. package/esm/resource/utils/ref.js +1 -1
  181. package/esm/runtime/Infographic.d.ts +8 -2
  182. package/esm/runtime/Infographic.js +31 -2
  183. package/esm/runtime/options.d.ts +2 -0
  184. package/esm/runtime/options.js +24 -0
  185. package/esm/runtime/utils.d.ts +2 -0
  186. package/esm/runtime/utils.js +18 -0
  187. package/esm/templates/built-in.js +112 -1
  188. package/esm/templates/hierarchy-mindmap.d.ts +2 -0
  189. package/esm/templates/hierarchy-mindmap.js +61 -0
  190. package/esm/types/attrs.d.ts +15 -13
  191. package/esm/types/data.d.ts +2 -0
  192. package/esm/types/element.d.ts +1 -1
  193. package/esm/types/event.d.ts +6 -0
  194. package/esm/types/event.js +1 -0
  195. package/esm/types/index.d.ts +2 -0
  196. package/esm/{renderer/types → types}/text.d.ts +0 -1
  197. package/esm/types/text.js +1 -0
  198. package/esm/utils/attrs.d.ts +1 -0
  199. package/esm/utils/attrs.js +9 -0
  200. package/esm/utils/element.d.ts +4 -0
  201. package/esm/utils/element.js +13 -0
  202. package/esm/utils/icon.d.ts +5 -3
  203. package/esm/utils/icon.js +38 -19
  204. package/esm/utils/index.d.ts +5 -0
  205. package/esm/utils/index.js +5 -0
  206. package/esm/utils/measure-text.d.ts +5 -0
  207. package/esm/utils/measure-text.js +22 -0
  208. package/esm/utils/recognizer.d.ts +15 -0
  209. package/esm/utils/recognizer.js +61 -1
  210. package/esm/utils/style.d.ts +1 -0
  211. package/esm/utils/style.js +8 -0
  212. package/esm/utils/text.d.ts +7 -0
  213. package/esm/utils/text.js +110 -35
  214. package/lib/constants/components.d.ts +1 -0
  215. package/lib/constants/components.js +4 -0
  216. package/lib/constants/element.d.ts +17 -6
  217. package/lib/constants/index.d.ts +2 -0
  218. package/lib/constants/index.js +2 -0
  219. package/lib/constants/shape.d.ts +16 -0
  220. package/lib/designs/components/Btn.js +2 -2
  221. package/lib/designs/components/BtnsGroup.js +1 -1
  222. package/lib/designs/components/Illus.js +2 -2
  223. package/lib/designs/components/ItemDesc.js +1 -2
  224. package/lib/designs/components/ItemIcon.js +1 -1
  225. package/lib/designs/components/ItemLabel.js +1 -2
  226. package/lib/designs/components/ItemValue.js +1 -2
  227. package/lib/designs/components/ItemsGroup.js +1 -1
  228. package/lib/designs/components/Title.js +2 -5
  229. package/lib/designs/items/CandyCardLite.js +1 -1
  230. package/lib/designs/items/CircularProgress.js +2 -1
  231. package/lib/designs/items/HorizontalIconArrow.js +1 -2
  232. package/lib/designs/items/HorizontalIconLine.js +1 -1
  233. package/lib/designs/items/IconBadge.js +1 -1
  234. package/lib/designs/items/LinedText.d.ts +12 -0
  235. package/lib/designs/items/LinedText.js +61 -0
  236. package/lib/designs/items/PlainText.d.ts +1 -0
  237. package/lib/designs/items/PlainText.js +16 -3
  238. package/lib/designs/items/QuarterCircular.js +1 -31
  239. package/lib/designs/items/index.d.ts +1 -0
  240. package/lib/designs/items/index.js +1 -0
  241. package/lib/designs/layouts/Align.js +31 -45
  242. package/lib/designs/layouts/Flex.js +33 -29
  243. package/lib/designs/structures/chart-bar.d.ts +15 -0
  244. package/lib/designs/structures/chart-bar.js +103 -0
  245. package/lib/designs/structures/chart-line.d.ts +12 -0
  246. package/lib/designs/structures/chart-line.js +168 -0
  247. package/lib/designs/structures/chart-pie.d.ts +9 -0
  248. package/lib/designs/structures/chart-pie.js +131 -0
  249. package/lib/designs/structures/compare-binary-horizontal/dividers/pros-cons-arrow.js +1 -1
  250. package/lib/designs/structures/hierarchy-mindmap.d.ts +24 -0
  251. package/lib/designs/structures/hierarchy-mindmap.js +203 -0
  252. package/lib/designs/structures/index.d.ts +4 -0
  253. package/lib/designs/structures/index.js +4 -0
  254. package/lib/designs/structures/list-sector.js +1 -1
  255. package/lib/designs/structures/sequence-circular.js +1 -1
  256. package/lib/designs/structures/sequence-cylinders-3d.js +4 -4
  257. package/lib/designs/structures/sequence-roadmap-vertical.js +1 -1
  258. package/lib/designs/structures/sequence-zigzag-pucks-3d.js +5 -5
  259. package/lib/editor/commands/Batch.d.ts +11 -0
  260. package/lib/editor/commands/Batch.js +25 -0
  261. package/lib/editor/commands/UpdateElement.d.ts +16 -0
  262. package/lib/editor/commands/UpdateElement.js +87 -0
  263. package/lib/editor/commands/UpdateOptions.d.ts +14 -0
  264. package/lib/editor/commands/UpdateOptions.js +29 -0
  265. package/lib/editor/commands/UpdateText.d.ts +16 -0
  266. package/lib/editor/commands/UpdateText.js +44 -0
  267. package/lib/editor/commands/index.d.ts +4 -0
  268. package/lib/editor/commands/index.js +20 -0
  269. package/lib/editor/editor.d.ts +16 -0
  270. package/lib/editor/editor.js +54 -0
  271. package/lib/editor/index.d.ts +4 -0
  272. package/lib/editor/index.js +21 -0
  273. package/lib/editor/interactions/base.d.ts +12 -0
  274. package/lib/editor/interactions/base.js +9 -0
  275. package/lib/editor/interactions/brush-select.d.ts +23 -0
  276. package/lib/editor/interactions/brush-select.js +171 -0
  277. package/lib/editor/interactions/click-select.d.ts +12 -0
  278. package/lib/editor/interactions/click-select.js +71 -0
  279. package/lib/editor/interactions/dblclick-edit-text.d.ts +10 -0
  280. package/lib/editor/interactions/dblclick-edit-text.js +208 -0
  281. package/lib/editor/interactions/drag-element.d.ts +41 -0
  282. package/lib/editor/interactions/drag-element.js +395 -0
  283. package/lib/editor/interactions/hotkey-history.d.ts +10 -0
  284. package/lib/editor/interactions/hotkey-history.js +31 -0
  285. package/lib/editor/interactions/index.d.ts +8 -0
  286. package/lib/editor/interactions/index.js +19 -0
  287. package/lib/editor/interactions/select-highlight.d.ts +16 -0
  288. package/lib/editor/interactions/select-highlight.js +103 -0
  289. package/lib/editor/interactions/zoom-wheel.d.ts +8 -0
  290. package/lib/editor/interactions/zoom-wheel.js +50 -0
  291. package/lib/editor/managers/command.d.ts +19 -0
  292. package/lib/editor/managers/command.js +67 -0
  293. package/lib/editor/managers/index.d.ts +4 -0
  294. package/lib/editor/managers/index.js +20 -0
  295. package/lib/editor/managers/interaction.d.ts +33 -0
  296. package/lib/editor/managers/interaction.js +200 -0
  297. package/lib/editor/managers/plugin.d.ts +14 -0
  298. package/lib/editor/managers/plugin.js +49 -0
  299. package/lib/editor/managers/state.d.ts +22 -0
  300. package/lib/editor/managers/state.js +145 -0
  301. package/lib/editor/plugins/base.d.ts +12 -0
  302. package/lib/editor/plugins/base.js +9 -0
  303. package/lib/editor/plugins/edit-bar/components/button.d.ts +12 -0
  304. package/lib/editor/plugins/edit-bar/components/button.js +57 -0
  305. package/lib/editor/plugins/edit-bar/components/color-picker.d.ts +10 -0
  306. package/lib/editor/plugins/edit-bar/components/color-picker.js +354 -0
  307. package/lib/editor/plugins/edit-bar/components/icons.d.ts +26 -0
  308. package/lib/editor/plugins/edit-bar/components/icons.js +33 -0
  309. package/lib/editor/plugins/edit-bar/components/index.d.ts +5 -0
  310. package/lib/editor/plugins/edit-bar/components/index.js +21 -0
  311. package/lib/editor/plugins/edit-bar/components/popover.d.ts +23 -0
  312. package/lib/editor/plugins/edit-bar/components/popover.js +381 -0
  313. package/lib/editor/plugins/edit-bar/components/select.d.ts +22 -0
  314. package/lib/editor/plugins/edit-bar/components/select.js +204 -0
  315. package/lib/editor/plugins/edit-bar/edit-bar.d.ts +32 -0
  316. package/lib/editor/plugins/edit-bar/edit-bar.js +215 -0
  317. package/lib/editor/plugins/edit-bar/edit-items/align-elements.d.ts +2 -0
  318. package/lib/editor/plugins/edit-bar/edit-items/align-elements.js +316 -0
  319. package/lib/editor/plugins/edit-bar/edit-items/font-align.d.ts +3 -0
  320. package/lib/editor/plugins/edit-bar/edit-items/font-align.js +85 -0
  321. package/lib/editor/plugins/edit-bar/edit-items/font-color.d.ts +3 -0
  322. package/lib/editor/plugins/edit-bar/edit-items/font-color.js +81 -0
  323. package/lib/editor/plugins/edit-bar/edit-items/font-family.d.ts +3 -0
  324. package/lib/editor/plugins/edit-bar/edit-items/font-family.js +119 -0
  325. package/lib/editor/plugins/edit-bar/edit-items/font-size.d.ts +3 -0
  326. package/lib/editor/plugins/edit-bar/edit-items/font-size.js +92 -0
  327. package/lib/editor/plugins/edit-bar/edit-items/icon-color.d.ts +3 -0
  328. package/lib/editor/plugins/edit-bar/edit-items/icon-color.js +81 -0
  329. package/lib/editor/plugins/edit-bar/edit-items/index.d.ts +7 -0
  330. package/lib/editor/plugins/edit-bar/edit-items/index.js +22 -0
  331. package/lib/editor/plugins/edit-bar/edit-items/types.d.ts +3 -0
  332. package/lib/editor/plugins/edit-bar/edit-items/types.js +2 -0
  333. package/lib/editor/plugins/edit-bar/index.d.ts +4 -0
  334. package/lib/editor/plugins/edit-bar/index.js +9 -0
  335. package/lib/editor/plugins/edit-bar/utils.d.ts +4 -0
  336. package/lib/editor/plugins/edit-bar/utils.js +17 -0
  337. package/lib/editor/plugins/index.d.ts +3 -0
  338. package/lib/editor/plugins/index.js +22 -0
  339. package/lib/editor/plugins/resize-element.d.ts +45 -0
  340. package/lib/editor/plugins/resize-element.js +397 -0
  341. package/lib/editor/types/command.d.ts +28 -0
  342. package/lib/editor/types/command.js +2 -0
  343. package/lib/editor/types/editor.d.ts +4 -0
  344. package/lib/editor/types/editor.js +2 -0
  345. package/lib/editor/types/index.d.ts +7 -0
  346. package/lib/editor/types/index.js +2 -0
  347. package/lib/editor/types/interaction.d.ts +44 -0
  348. package/lib/editor/types/interaction.js +2 -0
  349. package/lib/editor/types/plugin.d.ts +30 -0
  350. package/lib/editor/types/plugin.js +2 -0
  351. package/lib/editor/types/selection.d.ts +2 -0
  352. package/lib/editor/types/selection.js +2 -0
  353. package/lib/editor/types/shape.d.ts +16 -0
  354. package/lib/editor/types/shape.js +2 -0
  355. package/lib/editor/types/state.d.ts +57 -0
  356. package/lib/editor/types/state.js +2 -0
  357. package/lib/editor/utils/click-handler.d.ts +28 -0
  358. package/lib/editor/utils/click-handler.js +101 -0
  359. package/lib/editor/utils/coordinate.d.ts +6 -0
  360. package/lib/editor/utils/coordinate.js +38 -0
  361. package/lib/editor/utils/data.d.ts +10 -0
  362. package/lib/editor/utils/data.js +22 -0
  363. package/lib/editor/utils/element.d.ts +2 -0
  364. package/lib/editor/utils/element.js +12 -0
  365. package/lib/editor/utils/event.d.ts +3 -0
  366. package/lib/editor/utils/event.js +67 -0
  367. package/lib/editor/utils/extension.d.ts +13 -0
  368. package/lib/editor/utils/extension.js +43 -0
  369. package/lib/editor/utils/hotkey.d.ts +20 -0
  370. package/lib/editor/utils/hotkey.js +113 -0
  371. package/lib/editor/utils/index.d.ts +7 -0
  372. package/lib/editor/utils/index.js +23 -0
  373. package/lib/exporter/svg.js +6 -4
  374. package/lib/index.d.ts +5 -2
  375. package/lib/index.js +14 -1
  376. package/lib/jsx/components/Polygon.js +2 -1
  377. package/lib/jsx/components/Text.js +66 -68
  378. package/lib/options/types.d.ts +15 -2
  379. package/lib/renderer/composites/background.js +1 -1
  380. package/lib/renderer/composites/button.js +3 -2
  381. package/lib/renderer/composites/icon.d.ts +1 -1
  382. package/lib/renderer/composites/icon.js +2 -0
  383. package/lib/renderer/composites/text.js +2 -1
  384. package/lib/renderer/renderer.js +2 -1
  385. package/lib/renderer/stylize/rough.js +3 -7
  386. package/lib/renderer/types/index.d.ts +0 -1
  387. package/lib/resource/utils/index.d.ts +0 -1
  388. package/lib/resource/utils/index.js +0 -1
  389. package/lib/resource/utils/ref.js +2 -2
  390. package/lib/runtime/Infographic.d.ts +8 -2
  391. package/lib/runtime/Infographic.js +34 -2
  392. package/lib/runtime/options.d.ts +2 -0
  393. package/lib/runtime/options.js +27 -0
  394. package/lib/runtime/utils.d.ts +2 -0
  395. package/lib/runtime/utils.js +21 -0
  396. package/lib/templates/built-in.js +112 -1
  397. package/lib/templates/hierarchy-mindmap.d.ts +2 -0
  398. package/lib/templates/hierarchy-mindmap.js +64 -0
  399. package/lib/types/attrs.d.ts +15 -13
  400. package/lib/types/data.d.ts +2 -0
  401. package/lib/types/element.d.ts +1 -1
  402. package/lib/types/event.d.ts +6 -0
  403. package/lib/types/event.js +2 -0
  404. package/lib/types/index.d.ts +2 -0
  405. package/lib/{renderer/types → types}/text.d.ts +0 -1
  406. package/lib/types/text.js +2 -0
  407. package/lib/utils/attrs.d.ts +1 -0
  408. package/lib/utils/attrs.js +12 -0
  409. package/lib/utils/element.d.ts +4 -0
  410. package/lib/utils/element.js +18 -0
  411. package/lib/utils/icon.d.ts +5 -3
  412. package/lib/utils/icon.js +42 -21
  413. package/lib/utils/index.d.ts +5 -0
  414. package/lib/utils/index.js +5 -0
  415. package/lib/utils/measure-text.d.ts +5 -0
  416. package/lib/utils/measure-text.js +28 -0
  417. package/lib/utils/recognizer.d.ts +15 -0
  418. package/lib/utils/recognizer.js +72 -2
  419. package/lib/utils/style.d.ts +1 -0
  420. package/lib/utils/style.js +11 -0
  421. package/lib/utils/text.d.ts +7 -0
  422. package/lib/utils/text.js +113 -32
  423. package/package.json +7 -3
  424. package/src/constants/components.ts +1 -0
  425. package/src/constants/element.ts +17 -5
  426. package/src/constants/index.ts +2 -0
  427. package/src/constants/shape.ts +16 -0
  428. package/src/designs/components/Btn.tsx +3 -2
  429. package/src/designs/components/BtnsGroup.tsx +8 -1
  430. package/src/designs/components/Illus.tsx +3 -2
  431. package/src/designs/components/ItemDesc.tsx +2 -2
  432. package/src/designs/components/ItemIcon.tsx +3 -2
  433. package/src/designs/components/ItemLabel.tsx +2 -2
  434. package/src/designs/components/ItemValue.tsx +2 -2
  435. package/src/designs/components/ItemsGroup.tsx +2 -1
  436. package/src/designs/components/Title.tsx +8 -5
  437. package/src/designs/items/CandyCardLite.tsx +3 -3
  438. package/src/designs/items/CircularProgress.tsx +19 -8
  439. package/src/designs/items/HorizontalIconArrow.tsx +4 -5
  440. package/src/designs/items/HorizontalIconLine.tsx +7 -10
  441. package/src/designs/items/IconBadge.tsx +3 -3
  442. package/src/designs/items/LinedText.tsx +131 -0
  443. package/src/designs/items/PlainText.tsx +25 -3
  444. package/src/designs/items/QuarterCircular.tsx +2 -31
  445. package/src/designs/items/index.ts +1 -0
  446. package/src/designs/layouts/Align.tsx +31 -48
  447. package/src/designs/layouts/Flex.tsx +33 -31
  448. package/src/designs/structures/chart-bar.tsx +289 -0
  449. package/src/designs/structures/chart-line.tsx +415 -0
  450. package/src/designs/structures/chart-pie.tsx +298 -0
  451. package/src/designs/structures/compare-binary-horizontal/dividers/pros-cons-arrow.tsx +2 -2
  452. package/src/designs/structures/hierarchy-mindmap.tsx +394 -0
  453. package/src/designs/structures/index.ts +4 -0
  454. package/src/designs/structures/list-sector.tsx +2 -1
  455. package/src/designs/structures/sequence-ascending-steps.tsx +0 -1
  456. package/src/designs/structures/sequence-circular.tsx +1 -1
  457. package/src/designs/structures/sequence-cylinders-3d.tsx +5 -5
  458. package/src/designs/structures/sequence-roadmap-vertical.tsx +2 -1
  459. package/src/designs/structures/sequence-zigzag-pucks-3d.tsx +18 -25
  460. package/src/editor/commands/Batch.ts +24 -0
  461. package/src/editor/commands/UpdateElement.ts +115 -0
  462. package/src/editor/commands/UpdateOptions.ts +31 -0
  463. package/src/editor/commands/UpdateText.ts +54 -0
  464. package/src/editor/commands/index.ts +4 -0
  465. package/src/editor/editor.ts +78 -0
  466. package/src/editor/index.ts +4 -0
  467. package/src/editor/interactions/base.ts +25 -0
  468. package/src/editor/interactions/brush-select.ts +204 -0
  469. package/src/editor/interactions/click-select.ts +72 -0
  470. package/src/editor/interactions/dblclick-edit-text.ts +260 -0
  471. package/src/editor/interactions/drag-element.ts +516 -0
  472. package/src/editor/interactions/hotkey-history.ts +34 -0
  473. package/src/editor/interactions/index.ts +8 -0
  474. package/src/editor/interactions/select-highlight.ts +140 -0
  475. package/src/editor/interactions/zoom-wheel.ts +52 -0
  476. package/src/editor/managers/command.ts +83 -0
  477. package/src/editor/managers/index.ts +4 -0
  478. package/src/editor/managers/interaction.ts +244 -0
  479. package/src/editor/managers/plugin.ts +66 -0
  480. package/src/editor/managers/state.ts +186 -0
  481. package/src/editor/plugins/base.ts +25 -0
  482. package/src/editor/plugins/edit-bar/components/button.ts +77 -0
  483. package/src/editor/plugins/edit-bar/components/color-picker.ts +397 -0
  484. package/src/editor/plugins/edit-bar/components/icons.ts +86 -0
  485. package/src/editor/plugins/edit-bar/components/index.ts +5 -0
  486. package/src/editor/plugins/edit-bar/components/popover.ts +446 -0
  487. package/src/editor/plugins/edit-bar/components/select.ts +247 -0
  488. package/src/editor/plugins/edit-bar/edit-bar.ts +292 -0
  489. package/src/editor/plugins/edit-bar/edit-items/align-elements.ts +447 -0
  490. package/src/editor/plugins/edit-bar/edit-items/font-align.ts +129 -0
  491. package/src/editor/plugins/edit-bar/edit-items/font-color.ts +101 -0
  492. package/src/editor/plugins/edit-bar/edit-items/font-family.ts +147 -0
  493. package/src/editor/plugins/edit-bar/edit-items/font-size.ts +116 -0
  494. package/src/editor/plugins/edit-bar/edit-items/icon-color.ts +101 -0
  495. package/src/editor/plugins/edit-bar/edit-items/index.ts +7 -0
  496. package/src/editor/plugins/edit-bar/edit-items/types.ts +9 -0
  497. package/src/editor/plugins/edit-bar/index.ts +18 -0
  498. package/src/editor/plugins/edit-bar/utils.ts +16 -0
  499. package/src/editor/plugins/index.ts +3 -0
  500. package/src/editor/plugins/resize-element.ts +518 -0
  501. package/src/editor/types/command.ts +32 -0
  502. package/src/editor/types/editor.ts +4 -0
  503. package/src/editor/types/index.ts +7 -0
  504. package/src/editor/types/interaction.ts +56 -0
  505. package/src/editor/types/plugin.ts +34 -0
  506. package/src/editor/types/selection.ts +3 -0
  507. package/src/editor/types/shape.ts +28 -0
  508. package/src/editor/types/state.ts +51 -0
  509. package/src/editor/utils/click-handler.ts +114 -0
  510. package/src/editor/utils/coordinate.ts +54 -0
  511. package/src/editor/utils/data.ts +30 -0
  512. package/src/editor/utils/element.ts +13 -0
  513. package/src/editor/utils/event.ts +92 -0
  514. package/src/editor/utils/extension.ts +45 -0
  515. package/src/editor/utils/hotkey.ts +139 -0
  516. package/src/editor/utils/index.ts +7 -0
  517. package/src/exporter/svg.ts +15 -5
  518. package/src/index.ts +14 -3
  519. package/src/jsx/components/Polygon.ts +2 -1
  520. package/src/jsx/components/Text.ts +70 -71
  521. package/src/options/types.ts +17 -2
  522. package/src/renderer/composites/background.ts +2 -2
  523. package/src/renderer/composites/button.ts +4 -3
  524. package/src/renderer/composites/icon.ts +2 -0
  525. package/src/renderer/composites/text.ts +2 -1
  526. package/src/renderer/renderer.ts +4 -2
  527. package/src/renderer/stylize/rough.ts +3 -7
  528. package/src/renderer/types/index.ts +0 -1
  529. package/src/resource/utils/index.ts +0 -1
  530. package/src/resource/utils/ref.ts +1 -1
  531. package/src/runtime/Infographic.tsx +45 -4
  532. package/src/runtime/options.ts +37 -0
  533. package/src/runtime/utils.ts +23 -0
  534. package/src/templates/built-in.ts +112 -1
  535. package/src/templates/hierarchy-mindmap.ts +129 -0
  536. package/src/types/attrs.ts +17 -13
  537. package/src/types/data.ts +2 -0
  538. package/src/types/element.ts +1 -1
  539. package/src/types/event.ts +6 -0
  540. package/src/types/index.ts +2 -0
  541. package/src/{renderer/types → types}/text.ts +0 -1
  542. package/src/utils/attrs.ts +9 -0
  543. package/src/utils/element.ts +26 -0
  544. package/src/utils/icon.ts +45 -25
  545. package/src/utils/index.ts +5 -0
  546. package/src/utils/measure-text.ts +38 -0
  547. package/src/utils/recognizer.ts +75 -1
  548. package/src/utils/style.ts +8 -0
  549. package/src/utils/text.ts +135 -40
  550. /package/esm/{renderer/types/text.js → constants/shape.js} +0 -0
  551. /package/esm/{resource/utils → utils}/hash.d.ts +0 -0
  552. /package/esm/{resource/utils → utils}/hash.js +0 -0
  553. /package/lib/{renderer/types/text.js → constants/shape.js} +0 -0
  554. /package/lib/{resource/utils → utils}/hash.d.ts +0 -0
  555. /package/lib/{resource/utils → utils}/hash.js +0 -0
  556. /package/src/{resource/utils → utils}/hash.ts +0 -0
@@ -0,0 +1,204 @@
1
+ import { createElement, setAttributes } from '../../utils';
2
+ import type { IInteraction, InteractionInitOptions, Selection } from '../types';
3
+ import {
4
+ clientToViewport,
5
+ getElementViewportBounds,
6
+ getEventTarget,
7
+ getSelectableTarget,
8
+ } from '../utils';
9
+ import { Interaction } from './base';
10
+
11
+ type Rect = { x: number; y: number; width: number; height: number };
12
+
13
+ export class BrushSelect extends Interaction implements IInteraction {
14
+ name = 'brush-select';
15
+
16
+ private brush?: SVGRectElement;
17
+ private startPoint?: DOMPoint;
18
+ private pointerId?: number;
19
+ private shiftKey = false;
20
+ private completeInteraction?: () => void;
21
+ private dragging = false;
22
+ private dragThreshold = 4;
23
+
24
+ init(options: InteractionInitOptions) {
25
+ super.init(options);
26
+ this.editor.getDocument().addEventListener('pointerdown', this.handleStart);
27
+ }
28
+
29
+ destroy() {
30
+ this.clearBrush();
31
+ this.editor
32
+ .getDocument()
33
+ .removeEventListener('pointerdown', this.handleStart);
34
+ window.removeEventListener('pointermove', this.handleMove);
35
+ window.removeEventListener('pointerup', this.handleEnd);
36
+ }
37
+
38
+ private handleStart = (event: PointerEvent) => {
39
+ if (!this.interaction.isActive()) return;
40
+ if (event.button !== 0) return;
41
+ if (this.isTextSelectionTarget(event.target)) return;
42
+ if (this.hasElementAtStart(event.target)) return;
43
+
44
+ this.interaction.executeExclusiveInteraction(
45
+ this,
46
+ async () =>
47
+ new Promise<void>((resolve) => {
48
+ this.completeInteraction = resolve;
49
+
50
+ const svg = this.editor.getDocument();
51
+ this.startPoint = clientToViewport(svg, event.clientX, event.clientY);
52
+ this.pointerId = event.pointerId;
53
+ this.shiftKey = event.shiftKey;
54
+ this.dragging = false;
55
+
56
+ window.addEventListener('pointermove', this.handleMove);
57
+ window.addEventListener('pointerup', this.handleEnd);
58
+ }),
59
+ );
60
+ };
61
+
62
+ private handleMove = (event: PointerEvent) => {
63
+ if (event.pointerId !== this.pointerId || !this.startPoint) return;
64
+
65
+ const svg = this.editor.getDocument();
66
+ const current = clientToViewport(svg, event.clientX, event.clientY);
67
+ const dx = current.x - this.startPoint.x;
68
+ const dy = current.y - this.startPoint.y;
69
+
70
+ if (!this.dragging) {
71
+ if (Math.hypot(dx, dy) < this.dragThreshold) return;
72
+ this.dragging = true;
73
+ this.ensureBrush();
74
+ this.updateBrush(this.startPoint, current);
75
+ }
76
+
77
+ event.preventDefault();
78
+ event.stopPropagation();
79
+ this.updateBrush(this.startPoint, current);
80
+ };
81
+
82
+ private handleEnd = (event: PointerEvent) => {
83
+ if (event.pointerId !== this.pointerId || !this.startPoint) return;
84
+
85
+ window.removeEventListener('pointermove', this.handleMove);
86
+ window.removeEventListener('pointerup', this.handleEnd);
87
+
88
+ let rect: Rect | null = null;
89
+ if (this.dragging) {
90
+ event.preventDefault();
91
+ event.stopPropagation();
92
+ const svg = this.editor.getDocument();
93
+ const endPoint = clientToViewport(svg, event.clientX, event.clientY);
94
+ rect = this.updateBrush(this.startPoint, endPoint);
95
+ this.clearBrush();
96
+ }
97
+
98
+ this.pointerId = undefined;
99
+ this.startPoint = undefined;
100
+ this.dragging = false;
101
+ const withShift = this.shiftKey;
102
+ this.shiftKey = false;
103
+
104
+ if (!rect) {
105
+ this.completeInteraction?.();
106
+ this.completeInteraction = undefined;
107
+ return;
108
+ }
109
+
110
+ const selection = this.collectSelection(rect);
111
+ this.completeInteraction?.();
112
+ this.completeInteraction = undefined;
113
+
114
+ if (selection.length === 0) {
115
+ if (!withShift) this.interaction.clearSelection();
116
+ return;
117
+ }
118
+
119
+ const mode: 'replace' | 'add' = withShift ? 'add' : 'replace';
120
+ this.interaction.select(selection, mode);
121
+ };
122
+
123
+ private ensureBrush() {
124
+ if (this.brush) return this.brush;
125
+ this.brush = this.interaction.appendTransientElement(
126
+ createElement<SVGRectElement>('rect', {
127
+ fill: 'rgba(51, 132, 245, 0.08)',
128
+ stroke: '#3384F5',
129
+ 'stroke-dasharray': '4 2',
130
+ 'stroke-width': 1,
131
+ 'pointer-events': 'none',
132
+ }),
133
+ );
134
+ return this.brush;
135
+ }
136
+
137
+ private updateBrush(start: DOMPoint, current: DOMPoint) {
138
+ if (!this.brush) return null;
139
+
140
+ const x = Math.min(start.x, current.x);
141
+ const y = Math.min(start.y, current.y);
142
+ const width = Math.abs(start.x - current.x);
143
+ const height = Math.abs(start.y - current.y);
144
+
145
+ setAttributes(this.brush, { x, y, width, height });
146
+ return { x, y, width, height } as Rect;
147
+ }
148
+
149
+ private clearBrush() {
150
+ this.brush?.remove();
151
+ this.brush = undefined;
152
+ }
153
+
154
+ private collectSelection(rect: Rect): Selection {
155
+ const svg = this.editor.getDocument();
156
+
157
+ const candidates = Array.from(
158
+ svg.querySelectorAll<SVGElement>('[data-element-type]'),
159
+ );
160
+
161
+ const intersects = (a: Rect, b: Rect) => {
162
+ const ax2 = a.x + a.width;
163
+ const ay2 = a.y + a.height;
164
+ const bx2 = b.x + b.width;
165
+ const by2 = b.y + b.height;
166
+ return !(ax2 < b.x || bx2 < a.x || ay2 < b.y || by2 < a.y);
167
+ };
168
+
169
+ const collected = new Set<Selection[number]>();
170
+ return candidates.reduce<Selection>((selection, node) => {
171
+ const element = getSelectableTarget(node as unknown as SVGElement) as
172
+ | Selection[number]
173
+ | null;
174
+ if (!element || collected.has(element)) return selection;
175
+
176
+ const bounds = getElementViewportBounds(svg, element);
177
+ const targetRect: Rect = {
178
+ x: bounds.x,
179
+ y: bounds.y,
180
+ width: bounds.width,
181
+ height: bounds.height,
182
+ };
183
+
184
+ if (intersects(rect, targetRect)) {
185
+ selection.push(element);
186
+ collected.add(element);
187
+ }
188
+ return selection;
189
+ }, []);
190
+ }
191
+
192
+ private hasElementAtStart(target: EventTarget | null) {
193
+ if (!(target instanceof Element)) return false;
194
+ if (getEventTarget(target as unknown as SVGElement)) return true;
195
+ return Boolean(target.closest?.('[data-element-type]'));
196
+ }
197
+
198
+ private isTextSelectionTarget(target: EventTarget | null) {
199
+ if (!(target instanceof HTMLElement)) return false;
200
+ if (target.isContentEditable) return true;
201
+ const tag = target.tagName.toLowerCase();
202
+ return tag === 'input' || tag === 'textarea';
203
+ }
204
+ }
@@ -0,0 +1,72 @@
1
+ import { isEditingText } from '../../utils';
2
+ import type { IInteraction, InteractionInitOptions } from '../types';
3
+ import { ClickHandler, getEventTarget } from '../utils';
4
+ import { Interaction } from './base';
5
+ export class ClickSelect extends Interaction implements IInteraction {
6
+ name = 'click-select';
7
+
8
+ private clickHandler?: ClickHandler;
9
+
10
+ init(options: InteractionInitOptions) {
11
+ super.init(options);
12
+ const { editor, interaction } = this;
13
+
14
+ this.clickHandler = new ClickHandler(editor.getDocument(), { delay: 0 });
15
+
16
+ const handleSelect = (event: MouseEvent) => {
17
+ if (!interaction.isActive()) return;
18
+ interaction.executeExclusiveInteraction(this, async () => {
19
+ const target = getEventTarget(event.target as SVGElement);
20
+ if (isEditingText(target)) return;
21
+
22
+ if (this.shiftKey) {
23
+ // 多选
24
+ if (target) {
25
+ if (interaction.isSelected(target)) {
26
+ interaction.select([target], 'remove');
27
+ } else {
28
+ interaction.select([target], 'add');
29
+ }
30
+ }
31
+ } else {
32
+ // 单选
33
+ if (target) interaction.select([target], 'replace');
34
+ else interaction.clearSelection();
35
+ }
36
+ });
37
+ };
38
+
39
+ this.clickHandler.onClick(handleSelect);
40
+
41
+ document.addEventListener('keydown', this.onShiftKeyDown);
42
+ document.addEventListener('keyup', this.onShiftKeyUp);
43
+ document.addEventListener('keydown', this.onEscKeyDown);
44
+ }
45
+
46
+ destroy() {
47
+ this.clickHandler?.destroy();
48
+ document.removeEventListener('keydown', this.onShiftKeyDown);
49
+ document.removeEventListener('keyup', this.onShiftKeyUp);
50
+ document.removeEventListener('keydown', this.onEscKeyDown);
51
+ }
52
+
53
+ private shiftKey = false;
54
+
55
+ private onShiftKeyDown = (event: KeyboardEvent) => {
56
+ if (event.key === 'Shift') {
57
+ this.shiftKey = true;
58
+ }
59
+ };
60
+
61
+ private onShiftKeyUp = (event: KeyboardEvent) => {
62
+ if (event.key === 'Shift') {
63
+ this.shiftKey = false;
64
+ }
65
+ };
66
+
67
+ private onEscKeyDown = (event: KeyboardEvent) => {
68
+ if (event.key === 'Escape') {
69
+ this.interaction.clearSelection();
70
+ }
71
+ };
72
+ }
@@ -0,0 +1,260 @@
1
+ import type { TextElement } from '../../types';
2
+ import {
3
+ getTextContent,
4
+ getTextEntity,
5
+ injectStyleOnce,
6
+ isEditableText,
7
+ } from '../../utils';
8
+ import { UpdateTextCommand } from '../commands';
9
+ import type {
10
+ IInteraction,
11
+ InteractionInitOptions,
12
+ Selection,
13
+ SelectionChangePayload,
14
+ } from '../types';
15
+ import { ClickHandler, getEventTarget } from '../utils';
16
+ import { Interaction } from './base';
17
+
18
+ export class DblClickEditText extends Interaction implements IInteraction {
19
+ name = 'dblclick-edit-text';
20
+
21
+ private clickHandler?: ClickHandler;
22
+ private detachSelectionListener?: () => void;
23
+
24
+ init(options: InteractionInitOptions) {
25
+ super.init(options);
26
+ const { editor, commander, interaction } = options;
27
+ this.clickHandler = new ClickHandler(editor.getDocument()).onDoubleClick(
28
+ (event) => {
29
+ if (!interaction.isActive()) return;
30
+
31
+ interaction.executeExclusiveInteraction(this, async () => {
32
+ const target = getEventTarget(event.target as SVGElement);
33
+ if (!target) return;
34
+ if (isEditableText(target)) {
35
+ interaction.select([target], 'replace');
36
+ const originalText = getTextContent(target);
37
+ const text = await new Promise<string>((resolve) => {
38
+ const stopListen = this.listenSelectionChange(target);
39
+ editText(target, {
40
+ cursorPosition: {
41
+ clientX: event.clientX,
42
+ clientY: event.clientY,
43
+ },
44
+ onBlur: resolve,
45
+ onCancel: resolve,
46
+ });
47
+ this.detachSelectionListener = stopListen;
48
+ });
49
+
50
+ commander.execute(
51
+ new UpdateTextCommand(target, text, originalText),
52
+ );
53
+ }
54
+ });
55
+ },
56
+ );
57
+ }
58
+
59
+ destroy() {
60
+ this.clickHandler?.destroy();
61
+ this.detachSelectionListener?.();
62
+ }
63
+
64
+ private listenSelectionChange(target: Selection[number]) {
65
+ const handler = ({ next }: SelectionChangePayload) => {
66
+ if (!next.includes(target)) {
67
+ this.detachSelectionListener?.();
68
+ this.detachSelectionListener = undefined;
69
+ const entity = getTextEntity(target as TextElement);
70
+ if (entity) entity.blur();
71
+ }
72
+ };
73
+ this.emitter.on('selection:change', handler);
74
+ return () => this.emitter.off('selection:change', handler);
75
+ }
76
+ }
77
+
78
+ type EditTextOptions = {
79
+ cursorPosition?: {
80
+ clientX: number;
81
+ clientY: number;
82
+ };
83
+ onInput?: (text: string) => void;
84
+ onBlur?: (text: string) => void;
85
+ onCancel?: (text: string) => void;
86
+ };
87
+
88
+ const EDITOR_STYLE_ID = 'infographic-inline-text-editor-style';
89
+ const EDITOR_BASE_CLASS = 'infographic-inline-text-editor';
90
+
91
+ function editText(text: TextElement, options?: EditTextOptions) {
92
+ const entity = getTextEntity(text);
93
+ if (!entity) return;
94
+
95
+ ensureEditorStyles();
96
+ new InlineTextEditor(entity, options).start();
97
+ }
98
+
99
+ class InlineTextEditor {
100
+ constructor(
101
+ private entity: HTMLSpanElement,
102
+ private options?: EditTextOptions,
103
+ ) {}
104
+
105
+ start() {
106
+ this.entity.setAttribute('contenteditable', 'true');
107
+ this.entity.classList.add(EDITOR_BASE_CLASS);
108
+ this.entity.focus();
109
+ this.placeCaretAtClickPosition();
110
+ this.attachListeners();
111
+ }
112
+
113
+ private attachListeners() {
114
+ this.entity.addEventListener('paste', this.handlePaste);
115
+ this.entity.addEventListener('keydown', this.handleKeydown);
116
+ this.entity.addEventListener('input', this.handleInput);
117
+ this.entity.addEventListener('blur', this.handleBlur, { once: true });
118
+ }
119
+
120
+ private detachListeners() {
121
+ this.entity.removeEventListener('paste', this.handlePaste);
122
+ this.entity.removeEventListener('keydown', this.handleKeydown);
123
+ this.entity.removeEventListener('input', this.handleInput);
124
+ }
125
+
126
+ private handlePaste = (event: ClipboardEvent) => {
127
+ if (!event.clipboardData) return;
128
+ event.preventDefault();
129
+ this.insertPlainText(event.clipboardData.getData('text/plain'));
130
+ };
131
+
132
+ private handleKeydown = (event: KeyboardEvent) => {
133
+ if (event.key === 'Enter' && !event.shiftKey) {
134
+ event.preventDefault();
135
+ this.insertPlainText('\n');
136
+ }
137
+ };
138
+
139
+ private handleInput = () => {
140
+ this.normalizeSpanContent();
141
+ this.options?.onInput?.(this.getText());
142
+ };
143
+
144
+ private handleBlur = () => {
145
+ this.entity.removeAttribute('contenteditable');
146
+ this.entity.classList.remove(EDITOR_BASE_CLASS);
147
+ this.normalizeSpanContent();
148
+ this.options?.onBlur?.(this.getText());
149
+ this.detachListeners();
150
+ };
151
+
152
+ private insertPlainText(text: string) {
153
+ const selection = window.getSelection();
154
+ if (!selection) return;
155
+
156
+ if (!selection.rangeCount) {
157
+ this.placeCaretAtEnd();
158
+ }
159
+
160
+ const range = selection.rangeCount
161
+ ? selection.getRangeAt(0)
162
+ : document.createRange();
163
+ range.deleteContents();
164
+
165
+ const textNode = document.createTextNode(text);
166
+ range.insertNode(textNode);
167
+
168
+ range.setStartAfter(textNode);
169
+ range.setEndAfter(textNode);
170
+ selection.removeAllRanges();
171
+ selection.addRange(range);
172
+
173
+ // Mirror native input behavior so consumers stay in sync.
174
+ this.normalizeSpanContent();
175
+ this.options?.onInput?.(this.getText());
176
+ }
177
+
178
+ private normalizeSpanContent() {
179
+ if (
180
+ this.entity.childNodes.length === 1 &&
181
+ this.entity.firstChild?.nodeType === Node.TEXT_NODE
182
+ ) {
183
+ return;
184
+ }
185
+
186
+ const plainText = this.getText();
187
+ this.entity.textContent = plainText;
188
+ }
189
+
190
+ private placeCaretAtClickPosition() {
191
+ const selection = window.getSelection();
192
+ if (!selection) return;
193
+
194
+ const rangeFromPoint = this.getRangeFromPoint();
195
+ if (rangeFromPoint) {
196
+ selection.removeAllRanges();
197
+ selection.addRange(rangeFromPoint);
198
+ return;
199
+ }
200
+
201
+ this.placeCaretAtEnd();
202
+ }
203
+
204
+ private getRangeFromPoint(): Range | null {
205
+ const { cursorPosition } = this.options || {};
206
+ if (!cursorPosition) return null;
207
+
208
+ const { clientX, clientY } = cursorPosition;
209
+ const doc = document as any;
210
+ const rangeFromPoint: Range | null =
211
+ doc.caretRangeFromPoint?.(clientX, clientY) ??
212
+ (() => {
213
+ const caretPosition = doc.caretPositionFromPoint?.(clientX, clientY);
214
+ if (!caretPosition) return null;
215
+ const caretRange = document.createRange();
216
+ caretRange.setStart(caretPosition.offsetNode, caretPosition.offset);
217
+ caretRange.collapse(true);
218
+ return caretRange;
219
+ })();
220
+
221
+ if (!rangeFromPoint) return null;
222
+ if (!this.entity.contains(rangeFromPoint.startContainer)) return null;
223
+ return rangeFromPoint;
224
+ }
225
+
226
+ private placeCaretAtEnd() {
227
+ const selection = window.getSelection();
228
+ if (!selection) return;
229
+
230
+ const range = document.createRange();
231
+ range.selectNodeContents(this.entity);
232
+ range.collapse(false);
233
+
234
+ selection.removeAllRanges();
235
+ selection.addRange(range);
236
+ }
237
+
238
+ private getText() {
239
+ return this.entity.textContent || '';
240
+ }
241
+ }
242
+
243
+ function ensureEditorStyles() {
244
+ injectStyleOnce(
245
+ EDITOR_STYLE_ID,
246
+ `
247
+ .${EDITOR_BASE_CLASS} {
248
+ margin: 0;
249
+ padding: 0;
250
+ box-sizing: border-box;
251
+ background-color: transparent;
252
+ outline: none;
253
+ cursor: text;
254
+ }
255
+ .${EDITOR_BASE_CLASS}::selection {
256
+ background-color: #b3d4fc;
257
+ }
258
+ `,
259
+ );
260
+ }