x4js 2.0.13 → 2.0.15

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 (260) hide show
  1. package/.vscode/launch.json +14 -0
  2. package/README.md +5 -0
  3. package/{lib/src/demo → demo}/main.scss +3 -1
  4. package/{lib/src/demo/main.tsx → demo/main.ts} +37 -36
  5. package/demo/package.json +26 -0
  6. package/demo/scss.d.ts +4 -0
  7. package/demo/svg.d.ts +1 -0
  8. package/demo/tsconfig.json +14 -0
  9. package/lib/README.txt +5 -0
  10. package/lib/cjs/x4.css +1 -1
  11. package/lib/cjs/x4.js +2 -1
  12. package/lib/esm/x4.css +1 -1
  13. package/lib/esm/x4.mjs +2 -1
  14. package/lib/src/components/boxes/boxes.module.scss +17 -0
  15. package/lib/src/components/boxes/boxes.ts +258 -17
  16. package/lib/src/components/breadcrumb/breadcrumb.scss +56 -28
  17. package/lib/src/components/breadcrumb/breadcrumb.ts +93 -84
  18. package/lib/src/components/btngroup/btngroup.module.scss +12 -0
  19. package/lib/src/components/btngroup/btngroup.ts +41 -8
  20. package/lib/src/components/button/button.module.scss +23 -5
  21. package/lib/src/components/button/button.ts +72 -4
  22. package/lib/src/components/canvas/canvas.module.scss +25 -0
  23. package/lib/src/components/canvas/canvas.ts +189 -0
  24. package/lib/src/components/canvas/canvas_ex.ts +276 -0
  25. package/lib/src/components/checkbox/checkbox.ts +18 -4
  26. package/lib/src/components/combobox/combobox.module.scss +24 -15
  27. package/lib/src/components/combobox/combobox.ts +107 -24
  28. package/lib/src/components/components.ts +7 -0
  29. package/lib/src/components/dialog/dialog.module.scss +40 -7
  30. package/lib/src/components/dialog/dialog.ts +166 -31
  31. package/lib/src/components/filedrop/cloud-arrow-up.svg +1 -0
  32. package/lib/src/components/filedrop/filedrop.module.scss +70 -0
  33. package/lib/src/components/filedrop/filedrop.ts +131 -0
  34. package/lib/src/components/form/form.module.scss +4 -0
  35. package/lib/src/components/form/form.ts +137 -6
  36. package/lib/src/components/gridview/arrow-down-light.svg +1 -0
  37. package/lib/src/components/gridview/arrow-up-light.svg +1 -0
  38. package/lib/src/components/gridview/gridview.module.scss +324 -0
  39. package/lib/src/components/gridview/gridview.ts +1175 -0
  40. package/lib/src/components/icon/icon.module.scss +2 -1
  41. package/lib/src/components/icon/icon.ts +4 -3
  42. package/lib/src/components/image/image.module.scss +8 -1
  43. package/lib/src/components/image/image.ts +105 -6
  44. package/lib/src/components/input/input.module.scss +8 -3
  45. package/lib/src/components/input/input.ts +178 -31
  46. package/lib/src/components/keyboard/arrow-up.svg +1 -0
  47. package/lib/src/components/keyboard/delete-left.svg +1 -0
  48. package/lib/src/components/keyboard/eye-slash.svg +1 -0
  49. package/lib/src/components/keyboard/keyboard.module.scss +134 -0
  50. package/lib/src/components/keyboard/keyboard.ts +526 -0
  51. package/lib/src/components/label/label.module.scss +22 -4
  52. package/lib/src/components/label/label.ts +33 -0
  53. package/lib/src/components/link/link.ts +81 -78
  54. package/lib/src/components/listbox/listbox.module.scss +61 -3
  55. package/lib/src/components/listbox/listbox.ts +164 -56
  56. package/lib/src/components/menu/menu.module.scss +10 -1
  57. package/lib/src/components/menu/menu.ts +6 -3
  58. package/lib/src/components/messages/messages.module.scss +44 -0
  59. package/lib/src/components/messages/messages.ts +164 -18
  60. package/lib/src/components/messages/pen-field.svg +1 -0
  61. package/lib/src/components/normalize.scss +5 -0
  62. package/lib/src/components/notification/notification.module.scss +4 -2
  63. package/lib/src/components/notification/notification.ts +2 -4
  64. package/lib/src/components/panel/panel.module.scss +12 -0
  65. package/lib/src/components/popup/popup.module.scss +10 -2
  66. package/lib/src/components/popup/popup.ts +141 -95
  67. package/lib/src/components/propgrid/folder-closed.svg +1 -0
  68. package/lib/src/components/propgrid/folder-open.svg +1 -0
  69. package/lib/src/components/propgrid/progrid.module.scss +112 -0
  70. package/lib/src/components/propgrid/propgrid.ts +288 -0
  71. package/lib/src/components/propgrid/updown.svg +4 -0
  72. package/lib/src/components/radio/radio.module.scss +147 -0
  73. package/lib/src/components/radio/radio.svg +4 -0
  74. package/lib/src/components/radio/radio.ts +142 -0
  75. package/lib/src/components/select/select.module.scss +9 -0
  76. package/lib/src/components/select/select.ts +134 -0
  77. package/lib/src/components/shared.scss +47 -0
  78. package/lib/src/components/sizers/sizer.ts +10 -2
  79. package/lib/src/components/slider/slider.module.scss +77 -30
  80. package/lib/src/components/slider/slider.ts +72 -22
  81. package/lib/src/components/tabs/tabs.module.scss +1 -2
  82. package/lib/src/components/tabs/tabs.ts +49 -12
  83. package/lib/src/components/textarea/textarea.module.scss +6 -2
  84. package/lib/src/components/textarea/textarea.ts +73 -8
  85. package/lib/src/components/textedit/textedit.module.scss +3 -1
  86. package/lib/src/components/textedit/textedit.ts +47 -15
  87. package/lib/src/components/themes.scss +7 -0
  88. package/lib/src/components/tickline/tickline.module.scss +26 -0
  89. package/lib/src/components/tickline/tickline.ts +82 -0
  90. package/lib/src/components/tooltips/comments-question.svg +1 -0
  91. package/lib/src/components/tooltips/tooltips.scss +30 -9
  92. package/lib/src/components/tooltips/tooltips.ts +10 -5
  93. package/lib/src/components/treeview/treeview.module.scss +129 -60
  94. package/lib/src/components/treeview/treeview.ts +47 -12
  95. package/lib/src/components/viewport/viewport.module.scss +7 -0
  96. package/lib/src/core/component.ts +113 -40
  97. package/lib/src/core/core_application.ts +223 -2
  98. package/lib/src/core/core_colors.ts +2 -2
  99. package/lib/src/{components/grid/datastore.ts → core/core_data.ts} +264 -252
  100. package/lib/src/core/core_dragdrop.ts +3 -3
  101. package/lib/src/core/core_element.ts +18 -1
  102. package/lib/src/core/core_events.ts +28 -0
  103. package/lib/src/core/core_i18n.ts +19 -3
  104. package/lib/src/core/core_react.ts +79 -0
  105. package/lib/src/core/core_router.ts +25 -9
  106. package/lib/src/core/core_state.ts +62 -0
  107. package/lib/src/core/core_styles.ts +5 -5
  108. package/lib/src/core/core_svg.ts +174 -12
  109. package/lib/src/core/core_tools.ts +305 -87
  110. package/lib/src/x4tsx.d.ts +25 -0
  111. package/lib/styles/x4.css +1 -1
  112. package/lib/types/x4js.d.ts +828 -119
  113. package/package.json +4 -4
  114. package/scripts/build.mjs +378 -0
  115. package/scripts/prepack.mjs +346 -0
  116. package/src/components/base.scss +25 -0
  117. package/src/components/boxes/boxes.module.scss +54 -0
  118. package/src/components/boxes/boxes.ts +370 -0
  119. package/src/components/breadcrumb/breadcrumb.scss +56 -0
  120. package/src/components/breadcrumb/breadcrumb.ts +93 -0
  121. package/src/components/breadcrumb/chevron-right.svg +1 -0
  122. package/src/components/btngroup/btngroup.module.scss +41 -0
  123. package/src/components/btngroup/btngroup.ts +153 -0
  124. package/src/components/button/button.module.scss +173 -0
  125. package/src/components/button/button.ts +185 -0
  126. package/src/components/calendar/calendar-check-sharp-light.svg +1 -0
  127. package/src/components/calendar/calendar.module.scss +163 -0
  128. package/src/components/calendar/calendar.ts +327 -0
  129. package/src/components/calendar/chevron-left-sharp-light.svg +1 -0
  130. package/src/components/calendar/chevron-right-sharp-light.svg +1 -0
  131. package/src/components/canvas/canvas.module.scss +25 -0
  132. package/src/components/canvas/canvas.ts +189 -0
  133. package/src/components/canvas/canvas_ex.ts +276 -0
  134. package/src/components/checkbox/check.svg +4 -0
  135. package/src/components/checkbox/checkbox.module.scss +142 -0
  136. package/src/components/checkbox/checkbox.ts +140 -0
  137. package/src/components/colorinput/colorinput.module.scss +65 -0
  138. package/src/components/colorinput/colorinput.ts +91 -0
  139. package/src/components/colorinput/crosshairs-simple-sharp-light.svg +1 -0
  140. package/src/components/colorpicker/colorpicker.module.scss +133 -0
  141. package/src/components/colorpicker/colorpicker.ts +482 -0
  142. package/src/components/combobox/combobox.module.scss +133 -0
  143. package/src/components/combobox/combobox.ts +275 -0
  144. package/src/components/combobox/updown.svg +4 -0
  145. package/src/components/components.ts +41 -0
  146. package/src/components/dialog/dialog.module.scss +104 -0
  147. package/src/components/dialog/dialog.ts +229 -0
  148. package/src/components/dialog/xmark-sharp-light.svg +1 -0
  149. package/src/components/filedrop/cloud-arrow-up.svg +1 -0
  150. package/src/components/filedrop/filedrop.module.scss +70 -0
  151. package/src/components/filedrop/filedrop.ts +131 -0
  152. package/src/components/form/form.module.scss +38 -0
  153. package/src/components/form/form.ts +172 -0
  154. package/src/components/gridview/arrow-down-light.svg +1 -0
  155. package/src/components/gridview/arrow-up-light.svg +1 -0
  156. package/src/components/gridview/gridview.module.scss +324 -0
  157. package/src/components/gridview/gridview.ts +1175 -0
  158. package/src/components/header/header.module.scss +40 -0
  159. package/src/components/header/header.ts +130 -0
  160. package/src/components/icon/icon.module.scss +31 -0
  161. package/src/components/icon/icon.ts +137 -0
  162. package/src/components/image/image.module.scss +28 -0
  163. package/src/components/image/image.ts +168 -0
  164. package/src/components/input/input.module.scss +74 -0
  165. package/src/components/input/input.ts +422 -0
  166. package/src/components/keyboard/arrow-up.svg +1 -0
  167. package/src/components/keyboard/delete-left.svg +1 -0
  168. package/src/components/keyboard/eye-slash.svg +1 -0
  169. package/src/components/keyboard/keyboard.module.scss +134 -0
  170. package/src/components/keyboard/keyboard.ts +526 -0
  171. package/src/components/label/label.module.scss +76 -0
  172. package/src/components/label/label.ts +97 -0
  173. package/src/components/link/link.ts +81 -0
  174. package/src/components/listbox/listbox.module.scss +161 -0
  175. package/src/components/listbox/listbox.ts +539 -0
  176. package/src/components/menu/caret-right-solid.svg +1 -0
  177. package/src/components/menu/menu.module.scss +117 -0
  178. package/src/components/menu/menu.ts +174 -0
  179. package/src/components/messages/circle-exclamation.svg +1 -0
  180. package/src/components/messages/messages.module.scss +92 -0
  181. package/src/components/messages/messages.ts +215 -0
  182. package/src/components/messages/pen-field.svg +1 -0
  183. package/src/components/normalize.scss +391 -0
  184. package/src/components/notification/circle-check-solid.svg +1 -0
  185. package/src/components/notification/circle-exclamation-solid.svg +1 -0
  186. package/src/components/notification/circle-notch-light.svg +1 -0
  187. package/src/components/notification/notification.module.scss +84 -0
  188. package/src/components/notification/notification.ts +107 -0
  189. package/src/components/notification/xmark-sharp-light.svg +1 -0
  190. package/src/components/panel/panel.module.scss +60 -0
  191. package/src/components/panel/panel.ts +58 -0
  192. package/src/components/popup/popup.module.scss +51 -0
  193. package/src/components/popup/popup.ts +442 -0
  194. package/src/components/progress/progress.module.scss +57 -0
  195. package/src/components/progress/progress.ts +44 -0
  196. package/src/components/propgrid/folder-closed.svg +1 -0
  197. package/src/components/propgrid/folder-open.svg +1 -0
  198. package/src/components/propgrid/progrid.module.scss +112 -0
  199. package/src/components/propgrid/propgrid.ts +288 -0
  200. package/src/components/propgrid/updown.svg +4 -0
  201. package/src/components/radio/radio.module.scss +147 -0
  202. package/src/components/radio/radio.svg +4 -0
  203. package/src/components/radio/radio.ts +142 -0
  204. package/src/components/rating/rating.module.scss +23 -0
  205. package/src/components/rating/rating.ts +131 -0
  206. package/src/components/rating/star-sharp-light.svg +1 -0
  207. package/src/components/rating/star-sharp-solid.svg +1 -0
  208. package/src/components/select/select.module.scss +9 -0
  209. package/src/components/select/select.ts +134 -0
  210. package/src/components/shared.scss +137 -0
  211. package/src/components/sizers/sizer.module.scss +90 -0
  212. package/src/components/sizers/sizer.ts +132 -0
  213. package/src/components/slider/slider.module.scss +118 -0
  214. package/src/components/slider/slider.ts +198 -0
  215. package/src/components/switch/switch.module.scss +127 -0
  216. package/src/components/switch/switch.ts +62 -0
  217. package/src/components/tabs/tabs.module.scss +45 -0
  218. package/src/components/tabs/tabs.ts +205 -0
  219. package/src/components/textarea/textarea.module.scss +63 -0
  220. package/src/components/textarea/textarea.ts +125 -0
  221. package/src/components/textedit/textedit.module.scss +116 -0
  222. package/src/components/textedit/textedit.ts +115 -0
  223. package/src/components/themes.scss +88 -0
  224. package/src/components/tickline/tickline.module.scss +26 -0
  225. package/src/components/tickline/tickline.ts +82 -0
  226. package/src/components/tooltips/circle-info-sharp-light.svg +1 -0
  227. package/src/components/tooltips/comments-question.svg +1 -0
  228. package/src/components/tooltips/tooltips.scss +72 -0
  229. package/src/components/tooltips/tooltips.ts +109 -0
  230. package/src/components/treeview/chevron-down-light.svg +1 -0
  231. package/src/components/treeview/treeview.module.scss +185 -0
  232. package/src/components/treeview/treeview.ts +445 -0
  233. package/src/components/viewport/viewport.module.scss +32 -0
  234. package/src/components/viewport/viewport.ts +41 -0
  235. package/src/core/component.ts +1075 -0
  236. package/src/core/core_application.ts +265 -0
  237. package/src/core/core_colors.ts +250 -0
  238. package/src/core/core_data.ts +1310 -0
  239. package/src/core/core_dom.ts +471 -0
  240. package/src/core/core_dragdrop.ts +201 -0
  241. package/src/core/core_element.ts +115 -0
  242. package/src/core/core_events.ts +177 -0
  243. package/src/core/core_i18n.ts +393 -0
  244. package/src/core/core_react.ts +79 -0
  245. package/src/core/core_router.ts +237 -0
  246. package/src/core/core_state.ts +62 -0
  247. package/src/core/core_styles.ts +214 -0
  248. package/src/core/core_svg.ts +712 -0
  249. package/src/core/core_tools.ts +906 -0
  250. package/src/types/scss.d.ts +4 -0
  251. package/src/types/svg.d.ts +1 -0
  252. package/src/types/x4react.d.ts +9 -0
  253. package/src/x4.scss +19 -0
  254. package/src/x4tsx.d.ts +25 -0
  255. package/tsconfig.json +14 -0
  256. package/lib/src/components/grid/gridview.ts +0 -1108
  257. package/lib/src/components/grid/memdb.ts +0 -325
  258. /package/{lib/src/demo → demo}/assets/house-light.svg +0 -0
  259. /package/{lib/src/demo → demo}/assets/radio.svg +0 -0
  260. /package/{lib/src/demo → demo}/index.html +0 -0
@@ -0,0 +1,712 @@
1
+ /**
2
+ * ___ ___ __
3
+ * \ \/ / / _
4
+ * \ / /_| |_
5
+ * / \____ _|
6
+ * /__/\__\ |_|
7
+ *
8
+ * @file core_svg.ts
9
+ * @author Etienne Cochard
10
+ *
11
+ * @copyright (c) 2024 R-libre ingenierie
12
+ *
13
+ * Use of this source code is governed by an MIT-style license
14
+ * that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.
15
+ **/
16
+
17
+ import { Component, ComponentProps } from './component';
18
+ import { isUnitLess } from "./core_styles";
19
+ import { DOMEventHandler, GlobalDOMEvents, addEvent } from './core_dom';
20
+ import { isNumber, isString } from './core_tools';
21
+
22
+ const SVG_NS = "http://www.w3.org/2000/svg";
23
+
24
+ // degrees to radian
25
+ function d2r( d: number ): number {
26
+ return d * Math.PI / 180.0;
27
+ }
28
+
29
+ // polar to cartesian
30
+ function p2c( x: number, y: number, r: number, deg: number ): {x: number,y: number} {
31
+ const rad = d2r( deg );
32
+ return {
33
+ x: x + r * Math.cos( rad ),
34
+ y: y + r * Math.sin( rad )
35
+ };
36
+ }
37
+
38
+ // fix prec for numbers
39
+ function num( x: number ): number {
40
+ return Math.round( x * 1000 ) / 1000;
41
+ }
42
+
43
+ // clean values
44
+ function clean( a: any, ...b: any ) {
45
+ // just round number values to 3 digits
46
+ b = b.map( ( v: any ) => {
47
+ if( typeof v === 'number' && isFinite(v) ) {
48
+ return num(v);
49
+ }
50
+
51
+ return v;
52
+ });
53
+
54
+ return String.raw( a, ...b );
55
+ }
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+ class SvgItem {
66
+ protected _dom : SVGElement;
67
+
68
+ constructor( tag: string ) {
69
+ this._dom = document.createElementNS("http://www.w3.org/2000/svg", tag );
70
+ }
71
+
72
+ /**
73
+ * @returns the svh element dom
74
+ */
75
+
76
+ getDom( ) {
77
+ return this._dom;
78
+ }
79
+
80
+ /**
81
+ *
82
+ */
83
+
84
+ reset( ) {
85
+ const attrs = this._dom.attributes;
86
+ for (let i = attrs.length - 1; i >= 0; i--) {
87
+ this._dom.removeAttribute(attrs[i].name);
88
+ }
89
+
90
+ return this;
91
+ }
92
+
93
+
94
+ /**
95
+ * change the stroke color
96
+ * @param color
97
+ */
98
+
99
+ stroke( color: string, width?: number ): this {
100
+ this.setAttr( 'stroke', color );
101
+ if( width!==undefined ) {
102
+ this.setAttr( 'stroke-width', width+'px' );
103
+ }
104
+ return this;
105
+ }
106
+
107
+ /**
108
+ * change the stroke width
109
+ * @param width
110
+ */
111
+ strokeWidth( width: number ): this {
112
+ this.setAttr( 'stroke-width', width+'px' );
113
+ return this;
114
+ }
115
+
116
+ strokeCap( cap: "butt" | "round" | "sqaure" ) {
117
+ return this.setAttr( "stroke-linecap", cap );
118
+ }
119
+
120
+ strokeOpacity( opacity: number ) {
121
+ return this.setAttr( "stroke-opacity", opacity+"" );
122
+ }
123
+
124
+ /**
125
+ *
126
+ */
127
+
128
+ antiAlias( set: boolean ) {
129
+ return this.setAttr( "shape-rendering", set ? "auto" : "crispEdges" );
130
+ }
131
+
132
+ /**
133
+ * change the fill color
134
+ * @param color
135
+ */
136
+
137
+ fill( color: string ): this {
138
+ this.setAttr( 'fill', color );
139
+ return this;
140
+ }
141
+
142
+ no_fill( ): this {
143
+ this.setAttr( 'fill', "transparent" );
144
+ return this;
145
+ }
146
+
147
+ /**
148
+ * return the given attribute if any
149
+ */
150
+
151
+ getAttr( name: string ) : string {
152
+ return this._dom.getAttribute( name );
153
+ }
154
+
155
+ getNumAttr( name: string ) {
156
+ return parseInt( this._dom.getAttribute( name ) );
157
+ }
158
+
159
+ /**
160
+ * define a new attribute
161
+ * @param name attibute name
162
+ * @param value attribute value
163
+ * @returns this
164
+ */
165
+
166
+ setAttr( name: string, value: string ) : this {
167
+ this._dom.setAttribute( name, value );
168
+ return this;
169
+ }
170
+
171
+ /**
172
+ *
173
+ */
174
+
175
+ setStyle<K extends keyof CSSStyleDeclaration>( name: K, value: string | number ) : this {
176
+ const _style = this._dom.style;
177
+
178
+ if( isNumber(value) ) {
179
+ let v = value+"";
180
+ if( !isUnitLess(name as string) ) {
181
+ v += "px";
182
+ }
183
+
184
+ (_style as any)[name] = v;
185
+ }
186
+ else {
187
+ (_style as any)[name] = value;
188
+ }
189
+
190
+ return this;
191
+ }
192
+
193
+ /**
194
+ * add a class
195
+ * @param name class name to add
196
+ */
197
+
198
+ addClass( cls: string ): this {
199
+ if( !cls ) return;
200
+
201
+ cls = cls.trim();
202
+ if( cls.indexOf(' ')>=0 ) {
203
+ const ccs = cls.split( " " );
204
+ this._dom.classList.add(...ccs);
205
+ }
206
+ else {
207
+ this._dom.classList.add(cls);
208
+ }
209
+
210
+ return this;
211
+ }
212
+
213
+ /**
214
+ * remove a class
215
+ * @param name class name to remove
216
+ */
217
+
218
+ removeClass( cls: string ): this {
219
+ if( !cls ) return;
220
+
221
+ if( cls.indexOf(' ')>=0 ) {
222
+ const ccs = cls.split( " " );
223
+ this._dom.classList.remove(...ccs);
224
+ }
225
+ else {
226
+ this._dom.classList.remove(cls);
227
+ }
228
+
229
+ return this;
230
+ }
231
+
232
+ /**
233
+ *
234
+ */
235
+
236
+ clip( id: string ): this {
237
+ this.setAttr( "clip-path", `url(#${id})` );
238
+ return this;
239
+ }
240
+
241
+ /**
242
+ *
243
+ */
244
+
245
+ transform( tr: string ): this {
246
+ const t = this.getAttr( "transform" ) ?? "";
247
+ this.setAttr( "transform", t+' '+tr );
248
+ return this;
249
+ }
250
+
251
+ clear_transform( ) {
252
+ this.setAttr( "transform", null );
253
+ return this;
254
+ }
255
+
256
+ /**
257
+ *
258
+ */
259
+
260
+ rotate( deg: number, cx: number, cy: number ): this {
261
+ this.transform( `rotate( ${deg} ${cx} ${cy} )` );
262
+ return this;
263
+ }
264
+
265
+ translate( dx: number, dy: number ): this {
266
+ this.transform( `translate( ${dx} ${dy} )` );
267
+ return this;
268
+ }
269
+
270
+ scale( x: number ): this {
271
+ this.transform( `scale( ${x} )` );
272
+ return this;
273
+ }
274
+
275
+ /**
276
+ *
277
+ */
278
+
279
+ addDOMEvent<K extends keyof GlobalDOMEvents>( name: K, listener: GlobalDOMEvents[K], prepend = false ) {
280
+ addEvent( this._dom, name, listener as DOMEventHandler, prepend );
281
+ return this;
282
+ }
283
+ }
284
+
285
+
286
+
287
+ /**
288
+ *
289
+ */
290
+
291
+ export class SvgPath extends SvgItem {
292
+ private _path: string;
293
+
294
+ constructor( ) {
295
+ super( 'path' );
296
+ this._path = '';
297
+ }
298
+
299
+ private _update( ): this {
300
+ this.setAttr( 'd', this._path );
301
+ return this;
302
+ }
303
+
304
+ reset( ) {
305
+ this._path = "";
306
+ super.reset( );
307
+ return this;
308
+ }
309
+
310
+ /**
311
+ * move the current pos
312
+ * @param x new pos x
313
+ * @param y new pos y
314
+ * @returns this
315
+ */
316
+
317
+ moveTo( x: number, y: number ) : this {
318
+ this._path += clean`M${x},${y}`;
319
+ return this._update( );
320
+ }
321
+
322
+ /**
323
+ * draw aline to the given point
324
+ * @param x end x
325
+ * @param y end y
326
+ * @returns this
327
+ */
328
+
329
+ lineTo( x: number, y: number ): this {
330
+ this._path += clean`L${x},${y}`;
331
+ return this._update( );
332
+ }
333
+
334
+ /**
335
+ * draw a curve
336
+ */
337
+
338
+ curveTo( x1: number, y1: number, x2: number, y2: number, x3: number, y3: number ) {
339
+ this._path += clean`C${x1},${y1} ${x2},${y2} ${x3},${y3}`;
340
+ return this._update( );
341
+ }
342
+
343
+
344
+ /**
345
+ * close the currentPath
346
+ */
347
+
348
+ closePath( ): this {
349
+ this._path += 'Z';
350
+ return this._update( );
351
+ }
352
+
353
+ /**
354
+ * draw an arc
355
+ * @param x center x
356
+ * @param y center y
357
+ * @param r radius
358
+ * @param start angle start in degrees
359
+ * @param end angle end in degrees
360
+ * @returns this
361
+ */
362
+
363
+ arc( x: number, y: number, r: number, start: number, end: number, clockwise= true ): this {
364
+
365
+ const st = p2c( x, y, r, start-90 );
366
+ const en = p2c( x, y, r, end-90 );
367
+
368
+ const flag = ((end-start) <= 180 ? "0" : "1");
369
+ this._path += clean`M${st.x},${st.y}A${r},${r} 0 ${flag} ${(clockwise ? '1' : '0')} ${en.x},${en.y}`;
370
+
371
+ return this._update( );
372
+ }
373
+ }
374
+
375
+ /**
376
+ *
377
+ */
378
+
379
+ export class SvgText extends SvgItem {
380
+
381
+ constructor( x: number, y: number, txt: string ) {
382
+ super( 'text' );
383
+
384
+ this.setAttr( 'x', num(x)+'' );
385
+ this.setAttr( 'y', num(y)+'' );
386
+
387
+ this._dom.innerHTML = txt;
388
+ }
389
+
390
+ font( font: string ): this {
391
+ return this.setAttr( 'font-family', font );
392
+ }
393
+
394
+ fontSize( size: number | string ): this {
395
+ return this.setAttr( 'font-size', size+'' );
396
+ }
397
+
398
+ fontWeight( weight: 'light' | 'normal' | 'bold' ): this {
399
+ return this.setAttr( 'font-weight', weight );
400
+ }
401
+
402
+ textAlign( align: 'left' | 'center' | 'right' ): this {
403
+
404
+ let al;
405
+ switch( align ) {
406
+ case 'left': al = 'start'; break;
407
+ case 'center': al = 'middle'; break;
408
+ case 'right': al = 'end'; break;
409
+ default: return this;
410
+ }
411
+
412
+ return this.setAttr( 'text-anchor', al );
413
+ }
414
+
415
+ verticalAlign( align: 'top' | 'center' | 'bottom' | 'baseline' ): this {
416
+
417
+ let al;
418
+ switch( align ) {
419
+ case 'top': al = 'hanging'; break;
420
+ case 'center': al = 'middle'; break;
421
+ case 'bottom': al = 'baseline'; break;
422
+ case 'baseline': al = 'mathematical'; break;
423
+ default: return;
424
+ }
425
+
426
+ return this.setAttr( 'alignment-baseline', al );
427
+ }
428
+ }
429
+
430
+ /**
431
+ *
432
+ */
433
+
434
+ export class SvgIcon extends SvgItem {
435
+ constructor( svg: string ) {
436
+ super( "svg" );
437
+
438
+ if( svg.startsWith("data:image/svg+xml,") ) {
439
+ svg = svg.substring( 19 );
440
+ }
441
+
442
+ const parser = new DOMParser();
443
+ const doc = parser.parseFromString( decodeURIComponent(svg), "image/svg+xml");
444
+
445
+ const parserErrorElement = doc.querySelector("parsererror");
446
+ if( parserErrorElement ) {
447
+ console.error( "error while parsing svg:\n"+ parserErrorElement.textContent );
448
+ }
449
+
450
+ const svgRoot = doc.documentElement; // The <svg> element from the string
451
+ for( let i=0; i<svgRoot.attributes.length; i++) {
452
+ this._dom.setAttribute( svgRoot.attributes[i].name, svgRoot.attributes[i].value );
453
+ }
454
+
455
+ for( let i=0; i<svgRoot.childNodes.length; i++) {
456
+ const child = svgRoot.childNodes[i];
457
+ if (child.nodeType === 1) {
458
+ this._dom.appendChild(child);
459
+ }
460
+ }
461
+ }
462
+ }
463
+
464
+
465
+
466
+ /**
467
+ *
468
+ */
469
+
470
+ export class SvgShape extends SvgItem {
471
+ constructor( tag: string ) {
472
+ super( tag );
473
+ }
474
+ }
475
+
476
+ /**
477
+ *
478
+ */
479
+
480
+ type number_or_perc = number | `${string}%`
481
+
482
+ export class SvgGradient extends SvgItem {
483
+
484
+ private static g_id = 1;
485
+
486
+ private _id: string;
487
+ private _stops: { offset: number_or_perc, color: string } [];
488
+
489
+ constructor( x1: number_or_perc, y1: number_or_perc, x2: number_or_perc, y2: number_or_perc ) {
490
+ super( 'linearGradient')
491
+
492
+ this._id = 'gx-'+SvgGradient.g_id;
493
+ SvgGradient.g_id++;
494
+
495
+ this.setAttr( 'id', this._id );
496
+ this.setAttr( 'x1', isString(x1) ? x1 : num(x1)+'' );
497
+ this.setAttr( 'x2', isString(x2) ? x2 : num(x2)+'' );
498
+ this.setAttr( 'y1', isString(y1) ? y1 : num(y1)+'' );
499
+ this.setAttr( 'y2', isString(y2) ? y2 : num(y2)+'' );
500
+
501
+ this._stops = [];
502
+ }
503
+
504
+ get id( ) {
505
+ return 'url(#'+this._id+')';
506
+ }
507
+
508
+ addStop( offset: number_or_perc, color: string ): this {
509
+ this._dom.insertAdjacentHTML( "beforeend", `<stop offset="${offset}%" stop-color="${color}"></stop>`);
510
+ return this;
511
+ }
512
+ }
513
+
514
+ /**
515
+ *
516
+ */
517
+
518
+ export class SvgGroup extends SvgItem {
519
+
520
+ constructor( tag = "g" ) {
521
+ super( tag )
522
+ }
523
+
524
+ /**
525
+ *
526
+ */
527
+
528
+ append<K extends SvgItem>( item: K ): K {
529
+ this._dom.appendChild( item.getDom() );
530
+ return item;
531
+ }
532
+
533
+ appendItems<K extends SvgItem>( items: K[] ) {
534
+ items.forEach( item => {
535
+ this._dom.appendChild( item.getDom() );
536
+ } );
537
+ }
538
+
539
+ /**
540
+ *
541
+ */
542
+
543
+ path( ): SvgPath {
544
+ const path = new SvgPath( );
545
+ return this.append( path );
546
+ }
547
+
548
+ text( x: number, y: number, txt: string ) {
549
+ const text = new SvgText( x, y, txt );
550
+ return this.append( text );
551
+ }
552
+
553
+ ellipse( x: number, y: number, r1: number, r2: number ): SvgShape {
554
+ const shape = new SvgShape( 'ellipse' );
555
+ shape.setAttr( 'cx', num(x)+'' );
556
+ shape.setAttr( 'cy', num(y)+'' );
557
+ shape.setAttr( 'rx', num(r1)+'' );
558
+ shape.setAttr( 'ry', num(r2)+'' );
559
+ return this.append( shape );
560
+ }
561
+
562
+ circle( x: number, y: number, r1: number ): SvgShape {
563
+ const shape = new SvgShape( 'ellipse' );
564
+ shape.setAttr( 'cx', num(x)+'' );
565
+ shape.setAttr( 'cy', num(y)+'' );
566
+ shape.setAttr( 'rx', num(r1)+'' );
567
+ shape.setAttr( 'ry', num(r1)+'' );
568
+ return this.append( shape );
569
+ }
570
+
571
+ icon( svg: string, x: number, y: number, w: number, h: number ): SvgIcon {
572
+ const icon = new SvgIcon( svg );
573
+ icon.setAttr( 'x', num(x)+'' );
574
+ icon.setAttr( 'y', num(y)+'' );
575
+ icon.setAttr( 'width', num(w)+'' );
576
+ icon.setAttr( 'height', num(h)+'' );
577
+ return this.append( icon );
578
+ }
579
+
580
+ rect( x: number, y: number, w: number, h: number ): SvgShape {
581
+
582
+ if( h<0 ) {
583
+ y = y+h;
584
+ h = -h;
585
+ }
586
+
587
+ const shape = new SvgShape( 'rect' );
588
+ shape.setAttr( 'x', num(x)+'' );
589
+ shape.setAttr( 'y', num(y)+'' );
590
+ shape.setAttr( 'width', num(w)+'' );
591
+ shape.setAttr( 'height', num(h)+'' );
592
+ return this.append( shape );
593
+ }
594
+
595
+ group( id?: string ) {
596
+ const group = new SvgGroup( );
597
+ if( id ) {
598
+ group.setAttr( 'id', id );
599
+ }
600
+
601
+ return this.append( group );
602
+ }
603
+
604
+ /**
605
+ *
606
+ * example
607
+ * ```ts
608
+ * const g = c.linear_gradient( '0%', '0%', '0%', '100%' )
609
+ * .addStop( 0, 'red' )
610
+ * .addStop( 100, 'green' );
611
+ *
612
+ * p.rect( 0, 0, 100, 100 )
613
+ * .stroke( g.id );
614
+ *
615
+ * ```
616
+ */
617
+
618
+ linear_gradient( x1: number_or_perc, y1: number_or_perc, x2: number_or_perc, y2: number_or_perc ) {
619
+ const grad = new SvgGradient( x1, y1, x2, y2 );
620
+ return this.append( grad );
621
+ }
622
+
623
+ /**
624
+ * clear
625
+ */
626
+
627
+ clear( ) {
628
+ const dom = this._dom;
629
+ while( dom.firstChild ) {
630
+ dom.removeChild( dom.firstChild );
631
+ }
632
+ }
633
+ }
634
+
635
+
636
+
637
+
638
+
639
+ export class SvgBuilder extends SvgGroup {
640
+ private static g_clip_id = 1;
641
+ private static g_pat_id = 1;
642
+
643
+ constructor( ) {
644
+ super( );
645
+ }
646
+
647
+ addClip( x: number, y: number, w: number, h: number ) {
648
+ const id = 'clip-'+SvgBuilder.g_clip_id++;
649
+ const clip = new SvgGroup( 'clipPath' );
650
+ clip.setAttr('id', id );
651
+ clip.rect( x, y, w, h );
652
+
653
+ this.append(clip);
654
+ return {id,clip};
655
+ }
656
+
657
+ addPattern( x: number, y: number, w: number, h: number ) {
658
+ const id = 'pat-'+SvgBuilder.g_pat_id++;
659
+
660
+ const pat = new SvgGroup( 'pattern' );
661
+ pat.setAttr( 'id', id );
662
+ pat.setAttr( 'x', num(x)+'' );
663
+ pat.setAttr( 'y', num(y)+'' );
664
+ pat.setAttr( 'width', num(w)+'' );
665
+ pat.setAttr( 'height', num(h)+'' );
666
+ pat.setAttr( 'patternUnits', "userSpaceOnUse" );
667
+
668
+ this.append(pat);
669
+ return {id,pat};
670
+ }
671
+ }
672
+
673
+
674
+
675
+ /**
676
+ *
677
+ */
678
+
679
+ interface SvgProps extends ComponentProps {
680
+ viewbox?: string;
681
+ svg: SvgBuilder;
682
+ }
683
+
684
+ /**
685
+ *
686
+ */
687
+
688
+ export class SvgComponent<P extends SvgProps = SvgProps> extends Component<P> {
689
+
690
+ constructor( props: P ) {
691
+ super( { ...props, tag: "svg", ns: SVG_NS } );
692
+
693
+ this.setAttribute( 'xmlns', SVG_NS );
694
+
695
+ if( props.viewbox ) {
696
+ this.setAttribute( "viewBox", props.viewbox );
697
+ }
698
+
699
+ if( props.svg ) {
700
+ this.dom.appendChild( props.svg.getDom() );
701
+ }
702
+ }
703
+
704
+ setSvg( bld: SvgBuilder ) {
705
+ this.clearContent( );
706
+ this.dom.appendChild( bld.getDom() );
707
+ }
708
+
709
+ addItems( ...items: SvgItem[] ) {
710
+ items.forEach( item => this.dom.appendChild( item.getDom() ) );
711
+ }
712
+ }