releasebird-javascript-sdk 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,518 @@
1
+ import RbirdScreenshotManager from "./RbirdScreenshotManager";
2
+
3
+ const svgns = "http://www.w3.org/2000/svg";
4
+
5
+ class Menu {
6
+ shapes;
7
+ colors;
8
+ fills;
9
+ sizes;
10
+ resets;
11
+
12
+ reset;
13
+
14
+ settings;
15
+
16
+ constructor(settings, reset) {
17
+ this.reset = reset;
18
+ this.settings = settings;
19
+ }
20
+
21
+ setShapes(shapes) {
22
+
23
+ const temp = [];
24
+
25
+ const pathButton = document.createElement("button");
26
+ pathButton.addEventListener("click", (event) => this.onClick("shape", "path"));
27
+ pathButton.style.background = "none";
28
+ pathButton.style.boxShadow = "none";
29
+ pathButton.style.border = "none";
30
+ pathButton.style.cursor = "pointer";
31
+ pathButton.innerHTML = "<svg fill=\"none\" height=\"20pt\" viewBox=\"0 0 120 120\" width=\"20px\" xmlns=\"http://www.w3.org/2000/svg\">\n" +
32
+ " <path clip-rule=\"evenodd\"\n" +
33
+ " d=\"m76.25 82.5c-.6904 0-1.25.5596-1.25 1.25s.5596 1.25 1.25 1.25h21.25c6.904 0 12.5 5.5964 12.5 12.5 0 6.904-5.596 12.5-12.5 12.5h-71.5c-2.2091 0-4-1.791-4-4s1.7909-4 4-4h71.5c2.4853 0 4.5-2.0147 4.5-4.5s-2.0147-4.5-4.5-4.5h-21.25c-5.1086 0-9.25-4.1414-9.25-9.25s4.1414-9.25 9.25-9.25h24.75c2.209 0 4 1.7909 4 4s-1.791 4-4 4z\"\n" +
34
+ " fill=\"#ffd482\" fill-rule=\"evenodd\"/>\n" +
35
+ " <path d=\"m37.626 72.6258c-1.0469-1.0469-2.4429-1.6793-3.9205-1.5873-3.232.2012-9.1224 1.1271-12.6358 5.0559-7.0697 7.9054-5.1282 17.0827-8.9811 19.7647-5.51917 3.8419-.6888 14.1409 13.9114 14.1409 11.321 0 20.5606-5.883 22.7243-23.246.2166-1.7381-.4371-3.4669-1.6756-4.7055z\"\n" +
36
+ " fill=\"#ffba33\"/>\n" +
37
+ " <path d=\"m34.0099 80.1329c-2.3432-2.3431-2.3432-6.1421 0-8.4853l58.1289-58.1289c3.9053-3.90529 10.2372-3.90528 14.1422 0l.7.7005c3.906 3.9052 3.906 10.2368 0 14.1421l-58.1285 58.1289c-2.3431 2.3432-6.1421 2.3432-8.4853 0z\"\n" +
38
+ " fill=\"#944700\"/>\n" +
39
+ "</svg>";
40
+ pathButton.value = "path";
41
+ temp.push(pathButton);
42
+
43
+ const rectButton = document.createElement("button");
44
+ rectButton.addEventListener("click", (event) => this.onClick("shape", "rect"));
45
+ rectButton.style.background = "none";
46
+ rectButton.style.cursor = "pointer";
47
+ rectButton.style.boxShadow = "none";
48
+ rectButton.style.border = "none";
49
+ rectButton.innerHTML = "<svg id=\"Layer_1\" enable-background=\"new 0 0 48 48\" height=\"20pt\" viewBox=\"0 0 48 48\" width=\"20pt\"\n" +
50
+ " xmlns=\"http://www.w3.org/2000/svg\">\n" +
51
+ " <path d=\"m44 12.5h-40c-.83 0-1.5.67-1.5 1.5v20c0 .83.67 1.5 1.5 1.5h40c.83 0 1.5-.67 1.5-1.5v-20c0-.83-.67-1.5-1.5-1.5zm-1.5 20h-37v-17h37z\"/>\n" +
52
+ "</svg>";
53
+ rectButton.value = "rect";
54
+ temp.push(rectButton);
55
+
56
+ const circleButton = document.createElement("button");
57
+ circleButton.addEventListener("click", (event) => this.onClick("shape", "circle"));
58
+ circleButton.style.background = "none";
59
+ circleButton.style.cursor = "pointer";
60
+ circleButton.style.boxShadow = "none";
61
+ circleButton.style.border = "none";
62
+ circleButton.innerHTML = "<svg height=\"20pt\" viewBox=\"0 0 512 512\" width=\"20pt\" xmlns=\"http://www.w3.org/2000/svg\">\n" +
63
+ " <path d=\"m256 512c-68.378906 0-132.667969-26.628906-181.019531-74.980469-48.351563-48.351562-74.980469-112.640625-74.980469-181.019531s26.628906-132.667969 74.980469-181.019531c48.351562-48.351563 112.640625-74.980469 181.019531-74.980469s132.667969 26.628906 181.019531 74.980469c48.351563 48.351562 74.980469 112.640625 74.980469 181.019531s-26.628906 132.667969-74.980469 181.019531c-48.351562 48.351563-112.640625 74.980469-181.019531 74.980469zm0-482c-124.617188 0-226 101.382812-226 226s101.382812 226 226 226 226-101.382812 226-226-101.382812-226-226-226zm0 0\"/>\n" +
64
+ " <path d=\"m256 512c-68.378906 0-132.667969-26.628906-181.019531-74.980469-48.351563-48.351562-74.980469-112.640625-74.980469-181.019531s26.628906-132.667969 74.980469-181.019531c48.351562-48.351563 112.640625-74.980469 181.019531-74.980469s132.667969 26.628906 181.019531 74.980469c48.351563 48.351562 74.980469 112.640625 74.980469 181.019531s-26.628906 132.667969-74.980469 181.019531c-48.351562 48.351563-112.640625 74.980469-181.019531 74.980469zm0-482c-124.617188 0-226 101.382812-226 226s101.382812 226 226 226 226-101.382812 226-226-101.382812-226-226-226zm0 0\"/>\n" +
65
+ "</svg>";
66
+ circleButton.value = "circle";
67
+ temp.push(circleButton);
68
+
69
+ const ellipseButton = document.createElement("button");
70
+ ellipseButton.style.background = "none";
71
+ ellipseButton.style.boxShadow = "none";
72
+ ellipseButton.style.cursor = "pointer";
73
+ ellipseButton.style.border = "none";
74
+ ellipseButton.addEventListener("click", (event) => this.onClick("shape", "ellipse"));
75
+ ellipseButton.innerHTML = "<svg clip-rule=\"evenodd\" fill-rule=\"evenodd\" height=\"20pt\" stroke-linejoin=\"round\" stroke-miterlimit=\"2\"\n" +
76
+ " viewBox=\"0 0 24 24\" width=\"20pt\" xmlns=\"http://www.w3.org/2000/svg\">\n" +
77
+ " <g id=\"Icon\">\n" +
78
+ " <path d=\"m12 5.25c-3.509 0-6.615 1.048-8.544 2.627-1.407 1.151-2.206 2.588-2.206 4.123s.799 2.972 2.206 4.123c1.929 1.579 5.035 2.627 8.544 2.627s6.615-1.048 8.544-2.627c1.407-1.151 2.206-2.588 2.206-4.123s-.799-2.972-2.206-4.123c-1.929-1.579-5.035-2.627-8.544-2.627zm0 1.5c3.114 0 5.883.887 7.595 2.288 1.02.835 1.655 1.848 1.655 2.962s-.635 2.127-1.655 2.962c-1.712 1.401-4.481 2.288-7.595 2.288s-5.883-.887-7.595-2.288c-1.02-.835-1.655-1.848-1.655-2.962s.635-2.127 1.655-2.962c1.712-1.401 4.481-2.288 7.595-2.288z\"/>\n" +
79
+ " </g>\n" +
80
+ "</svg>";
81
+ ellipseButton.value = "ellipse";
82
+ temp.push(ellipseButton);
83
+
84
+
85
+ this.shapes = temp;
86
+
87
+ }
88
+
89
+ setResets() {
90
+ const button = document.createElement("button");
91
+ button.addEventListener("click", (event) => this.reset());
92
+ button.style.setProperty("border-color", 'black');
93
+ button.style.border = "none";
94
+ button.style.background = "none";
95
+ button.style.cursor = "pointer";
96
+ button.style.marginTop = '3px';
97
+ button.innerHTML = "<svg id=\"Capa_1\" enable-background=\"new 0 0 511.697 511.697\" height=\"15pt\" viewBox=\"0 0 511.697 511.697\" width=\"15pt\"\n" +
98
+ " xmlns=\"http://www.w3.org/2000/svg\">\n" +
99
+ " <g>\n" +
100
+ " <path clip-rule=\"evenodd\"\n" +
101
+ " d=\"m450.09 150.319c82.253 82.41 82.126 215.896-.284 298.149-39.451 39.376-92.891 61.526-148.63 61.606h-301.176v-60.235h301.176c83.167-.214 150.414-67.809 150.199-150.976-.214-82.864-67.335-149.985-150.199-150.199h-98.676v86.806l-202.5-116.922 202.5-116.926v86.807h98.678c55.893.006 109.48 22.278 148.912 61.89z\"\n" +
102
+ " fill=\"#0291f7\" fill-rule=\"evenodd\"/>\n" +
103
+ " </g>\n" +
104
+ "</svg>";
105
+ this.resets = [button];
106
+ }
107
+
108
+ setColors(colors) {
109
+ const temp = [];
110
+ const color = document.createElement("div");
111
+ color.id = 'colorCircle';
112
+ color.addEventListener("click", (event) => this.openColorPicker());
113
+ color.style.cursor = 'pointer';
114
+ color.style.marginTop = '3px';
115
+ color.innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"18pt\" width=\"18pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
116
+ " y=\"0px\"\n" +
117
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
118
+ "<path style=\"fill:" + this.settings['color'] + "\"\n" +
119
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
120
+ "/>\n" +
121
+ "</svg>\n"
122
+ temp.push(color);
123
+ this.colors = temp;
124
+ }
125
+
126
+ closeColorPicker = () => {
127
+ const picker = document.getElementById("rbirdColorPicker");
128
+ if (picker) {
129
+ document.body.removeChild(picker);
130
+ }
131
+ }
132
+
133
+ openColorPicker = () => {
134
+ const colorPicker = document.createElement("div");
135
+ colorPicker.id = "rbirdColorPicker";
136
+ colorPicker.style.width = "200px";
137
+ colorPicker.style.height = "60px";
138
+ colorPicker.style.cursor = "pointer";
139
+ colorPicker.style.backgroundColor = "white";
140
+ colorPicker.style.position = "absolute";
141
+ colorPicker.style.top = "80px";
142
+ colorPicker.style.zIndex = "900000000000";
143
+ colorPicker.style.left = "50%";
144
+ colorPicker.style.border = "1px solid lightgrey";
145
+ colorPicker.style.padding = "10px";
146
+ colorPicker.style.paddingTop = "15px";
147
+ document.body.appendChild(colorPicker);
148
+
149
+ const blackIcon = document.createElement("div");
150
+ blackIcon.style.float = "left";
151
+ blackIcon.addEventListener("click", (event) => {
152
+ this.onClick("color", "black");
153
+ if (this.colors[0]) {
154
+ this.colors[0].innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
155
+ " y=\"0px\"\n" +
156
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
157
+ "<path style=\"fill:black;\"\n" +
158
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
159
+ "/>\n" +
160
+ "</svg>\n";
161
+ }
162
+ });
163
+ blackIcon.innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
164
+ " y=\"0px\"\n" +
165
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
166
+ "<path style=\"fill:#000000;\"\n" +
167
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
168
+ "/>\n" +
169
+ "</svg>\n";
170
+ colorPicker.appendChild(blackIcon);
171
+
172
+ const redIcon = document.createElement("div");
173
+ redIcon.style.float = "left";
174
+ redIcon.style.marginLeft = "10px";
175
+ redIcon.addEventListener("click", (event) => {
176
+ this.onClick("color", "red");
177
+ if (this.colors[0]) {
178
+ this.colors[0].innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
179
+ " y=\"0px\"\n" +
180
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
181
+ "<path style=\"fill:red;\"\n" +
182
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
183
+ "/>\n" +
184
+ "</svg>\n";
185
+ }
186
+ });
187
+ redIcon.innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
188
+ " y=\"0px\"\n" +
189
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
190
+ "<path style=\"fill:red;\"\n" +
191
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
192
+ "/>\n" +
193
+ "</svg>\n";
194
+ colorPicker.appendChild(redIcon);
195
+
196
+ const blueIcon = document.createElement("div");
197
+ blueIcon.style.float = "left";
198
+ blueIcon.style.marginLeft = "10px";
199
+ blueIcon.addEventListener("click", (event) => {
200
+ this.onClick("color", "blue");
201
+ if (this.colors[0]) {
202
+ this.colors[0].innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
203
+ " y=\"0px\"\n" +
204
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
205
+ "<path style=\"fill:blue;\"\n" +
206
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
207
+ "/>\n" +
208
+ "</svg>\n";
209
+ }
210
+
211
+ });
212
+ blueIcon.innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
213
+ " y=\"0px\"\n" +
214
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
215
+ "<path style=\"fill:blue;\"\n" +
216
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
217
+ "/>\n" +
218
+ "</svg>\n";
219
+ colorPicker.appendChild(blueIcon);
220
+
221
+ const yellowIcon = document.createElement("div");
222
+ yellowIcon.style.float = "left";
223
+ yellowIcon.style.marginLeft = "10px";
224
+ yellowIcon.addEventListener("click", (event) => {
225
+ this.onClick("color", "yellow");
226
+ if (this.colors[0]) {
227
+ this.colors[0].innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
228
+ " y=\"0px\"\n" +
229
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
230
+ "<path style=\"fill:yellow;\"\n" +
231
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
232
+ "/>\n" +
233
+ "</svg>\n";
234
+ }
235
+ });
236
+ yellowIcon.innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
237
+ " y=\"0px\"\n" +
238
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
239
+ "<path style=\"fill:yellow;\"\n" +
240
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
241
+ "/>\n" +
242
+ "</svg>\n";
243
+ colorPicker.appendChild(yellowIcon);
244
+
245
+ const greenIcon = document.createElement("div");
246
+ greenIcon.style.float = "left";
247
+ greenIcon.style.marginLeft = "10px";
248
+ greenIcon.addEventListener("click", (event) => {
249
+ this.onClick("color", "green");
250
+ if (this.colors[0]) {
251
+ this.colors[0].innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
252
+ " y=\"0px\"\n" +
253
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
254
+ "<path style=\"fill:green;\"\n" +
255
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
256
+ "/>\n" +
257
+ "</svg>\n";
258
+ }
259
+ });
260
+ greenIcon.innerHTML = "<svg version=\"1.1\" id=\"Capa_1\" xmlns=\"http://www.w3.org/2000/svg\" height=\"20pt\" width=\"20pt\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\"\n" +
261
+ " y=\"0px\"\n" +
262
+ " viewBox=\"0 0 512 512\" style=\"enable-background:new 0 0 512 512;\" xml:space=\"preserve\">\n" +
263
+ "<path style=\"fill:green;\"\n" +
264
+ " d=\"M256,0C114.608,0,0,114.608,0,256s114.608,256,256,256s256-114.608,256-256S397.392,0,256,0L256,0z\"\n" +
265
+ "/>\n" +
266
+ "</svg>\n";
267
+ colorPicker.appendChild(greenIcon);
268
+
269
+ colorPicker.addEventListener("click", (event) => this.closeColorPicker());
270
+ }
271
+
272
+ setFills(fills) {
273
+ this.fills = [];
274
+ }
275
+
276
+ setSizes(sizes) {
277
+ this.sizes = [];
278
+ }
279
+
280
+ getElement() {
281
+ this.updateButtons();
282
+
283
+ const menu = document.createElement("div");
284
+ menu.classList.add("menu")
285
+
286
+ Object.keys(this.settings).forEach((setting) => {
287
+ const section = document.createElement("div");
288
+ section.style.float = "left";
289
+ this[setting + "s"].forEach((button) => section.appendChild(button));
290
+ menu.appendChild(section);
291
+ });
292
+
293
+ const nextButton = document.createElement('div')
294
+ nextButton.style.backgroundColor = 'green';
295
+ nextButton.style.color = 'white';
296
+ nextButton.style.fontWeight = '500';
297
+ nextButton.innerHTML = "Take Screenshot";
298
+ nextButton.style.float = "left";
299
+ nextButton.style.padding = "13px";
300
+ nextButton.style.paddingTop = "8px";
301
+ nextButton.style.paddingBottom = "8px";
302
+ nextButton.style.fontFamily = "Arial";
303
+ nextButton.style.fontSize = "16px";
304
+ nextButton.style.borderRadius = "8px";
305
+ nextButton.style.marginLeft = "10px";
306
+ nextButton.style.cursor = "pointer";
307
+
308
+ nextButton.addEventListener("click", (event) => {
309
+ RbirdScreenshotManager.getInstance().screenshot();
310
+ });
311
+
312
+ menu.appendChild(nextButton);
313
+
314
+ return menu;
315
+ }
316
+
317
+ onClick(setting, value) {
318
+ this.settings[setting] = value;
319
+ this.updateButtons();
320
+ }
321
+
322
+ updateButtons() {
323
+ Object.keys(this.settings).forEach((setting) => {
324
+ this[setting + "s"].forEach((button) => {
325
+ if (button.value == this.settings[setting])
326
+ button.classList.add("selected");
327
+ else
328
+ button.classList.remove("selected");
329
+ });
330
+ });
331
+ }
332
+ }
333
+
334
+ export default class SVGEditor {
335
+ svg;
336
+ objects= [];
337
+
338
+ codeSection;
339
+
340
+ originalMousePosition= null;
341
+ lastMousePosition = null;
342
+ currentMousePosition = null;
343
+ createdObject= null;
344
+ focusedObject = null;
345
+ menuSettings;
346
+
347
+ constructor(editorConfig) {
348
+ this.svg = SVGEditor.createSVG(editorConfig.dimension);
349
+ this.attachEvents(this.svg);
350
+
351
+ const menu = this.createMenu();
352
+ this.codeSection = document.createElement("pre");
353
+
354
+ const editor = document.createElement("div");
355
+ editor.classList.add("svg-editor");
356
+ editor.style.height = '100%';
357
+ editor.appendChild(menu);
358
+ editor.appendChild(this.svg);
359
+ editorConfig?.element?.appendChild(editor);
360
+ // editor.appendChild(this.codeSection);
361
+ if (editorConfig?.element?.getElementsByClassName('svg-editor').length === 0) {
362
+ editorConfig?.element?.appendChild(editor);
363
+ }
364
+
365
+ }
366
+
367
+ reset() {
368
+ const element = this.objects.pop();
369
+ if (element) {
370
+ this.svg.removeChild(element);
371
+ }
372
+ }
373
+
374
+ static createSVG(dimension) {
375
+ const svg = document.createElementNS(svgns, "svg");
376
+ svg.setAttribute("width", dimension.width);
377
+ svg.setAttribute("height", dimension.height);
378
+
379
+ return svg;
380
+ }
381
+
382
+ attachEvents(svg) {
383
+ svg.addEventListener("mousedown", this.onMouseDown.bind(this));
384
+ svg.addEventListener("mousemove", this.onMouseMove.bind(this));
385
+ svg.addEventListener("mouseup", this.onMouseUp.bind(this));
386
+ }
387
+
388
+ createMenu() {
389
+ this.menuSettings = {
390
+ shape: "path",
391
+ color: "black",
392
+ fill: "none",
393
+ size: 5,
394
+ reset: "reset"
395
+ };
396
+
397
+ const menu = new Menu(this.menuSettings, () => this.reset());
398
+ menu.setColors(["black", "red", "blue", "yellow"]);
399
+ menu.setFills(["none", "black", "red", "blue", "yellow"]);
400
+ menu.setSizes([1, 2, 3, 4, 5]);
401
+ menu.setShapes(["rect", "line", "circle", "ellipse", "path"]);
402
+ menu.setResets();
403
+ const element = menu.getElement();
404
+ element.style.position = 'absolute';
405
+ element.style.top = '10px';
406
+ element.style.left = '50%';
407
+ element.style.backgroundColor = 'white';
408
+ element.style.border = '1px solid lightgrey';
409
+ element.style.padding = '10px';
410
+ element.style.borderRadius = '8px';
411
+ element.style.transform = 'translateX(-50%)';
412
+ return element;
413
+ }
414
+
415
+ updateCode() {
416
+ this.codeSection.innerText = this.svg.innerHTML.replace(new RegExp("><", "g"), ">\n<");
417
+ }
418
+
419
+ onMouseDown(event) {
420
+ this.createdObject = document.createElementNS(svgns, this.menuSettings.shape);
421
+ this.createdObject.setAttributeNS(null, "fill", this.menuSettings.fill);
422
+ this.createdObject.setAttributeNS(null, "stroke", this.menuSettings.color);
423
+ this.createdObject.setAttributeNS(null, "stroke-width", String(this.menuSettings.size));
424
+ this.svg.appendChild(this.createdObject);
425
+
426
+ this.originalMousePosition = this.lastMousePosition = this.currentMousePosition = {
427
+ x: event.offsetX,
428
+ y: event.offsetY
429
+ };
430
+
431
+ switch (this.menuSettings.shape) {
432
+ case "path":
433
+ const start = "M " + this.currentMousePosition.x + " " + this.currentMousePosition.y;
434
+ this.createdObject.setAttributeNS(null, "d", start);
435
+ }
436
+ }
437
+
438
+ onMouseMove(event) {
439
+ // Ignore if not in mouse event
440
+ if (this.originalMousePosition === null)
441
+ return;
442
+
443
+ this.lastMousePosition = this.currentMousePosition;
444
+ this.currentMousePosition = {x: event.offsetX, y: event.offsetY};
445
+
446
+ this.updateCurrentObject();
447
+ }
448
+
449
+ onMouseUp() {
450
+ // If shape is nothing
451
+ if (this.originalMousePosition?.x != this.currentMousePosition?.x ||
452
+ this.originalMousePosition?.y != this.currentMousePosition?.y) {
453
+ this.objects.push(this.createdObject);
454
+ this.focus(this.createdObject);
455
+ } else if (this.menuSettings.shape !== "path") {
456
+ this.svg.removeChild(this.createdObject);
457
+ }
458
+
459
+ // TODO: If path, compress
460
+
461
+ // @ts-ignore
462
+ this.originalMousePosition = this.lastMousePosition = this.currentMousePosition = null;
463
+ // @ts-ignore
464
+ this.createdObject = null;
465
+ }
466
+
467
+ focus(object) {
468
+
469
+ }
470
+
471
+ updateCurrentObject() {
472
+ const topLeft = {
473
+ x: Math.min(this.currentMousePosition?.x, this.originalMousePosition?.x),
474
+ y: Math.min(this.currentMousePosition?.y, this.originalMousePosition?.y)
475
+ };
476
+
477
+ const bottomRight = {
478
+ x: Math.max(this.currentMousePosition?.x, this.originalMousePosition?.x),
479
+ y: Math.max(this.currentMousePosition?.y, this.originalMousePosition?.y)
480
+ };
481
+
482
+ switch (this.menuSettings.shape) {
483
+ case "line":
484
+ this.createdObject.setAttributeNS(null, "x1", String(this.originalMousePosition.x));
485
+ this.createdObject.setAttributeNS(null, "y1", String(this.originalMousePosition.y));
486
+ this.createdObject.setAttributeNS(null, "x2", String(this.currentMousePosition.x));
487
+ this.createdObject.setAttributeNS(null, "y2", String(this.currentMousePosition.y));
488
+ break;
489
+ case "rect":
490
+ this.createdObject.setAttributeNS(null, "x", String(topLeft.x));
491
+ this.createdObject.setAttributeNS(null, "y", String(topLeft.y));
492
+ this.createdObject.setAttributeNS(null, "width", String(bottomRight.x - topLeft.x));
493
+ this.createdObject.setAttributeNS(null, "height", String(bottomRight.y - topLeft.y));
494
+ break;
495
+ case "circle":
496
+ this.createdObject.setAttributeNS(null, "cx", String((topLeft.x + bottomRight.x) / 2));
497
+ this.createdObject.setAttributeNS(null, "cy", String((topLeft.y + bottomRight.y) / 2));
498
+ const radius = Math.max(bottomRight.x - topLeft.x, bottomRight.y - topLeft.y) / 2;
499
+ this.createdObject.setAttributeNS(null, "r", String(radius));
500
+ break;
501
+ case "ellipse":
502
+ this.createdObject.setAttributeNS(null, "cx", String((topLeft.x + bottomRight.x) / 2));
503
+ this.createdObject.setAttributeNS(null, "cy", String((topLeft.y + bottomRight.y) / 2));
504
+ this.createdObject.setAttributeNS(null, "rx", String((bottomRight.x - topLeft.x) / 2));
505
+ this.createdObject.setAttributeNS(null, "ry", String((bottomRight.y - topLeft.y) / 2));
506
+ break;
507
+ case "path":
508
+ let d = this.createdObject.getAttribute("d") + " l ";
509
+ d += this.currentMousePosition.x - this.lastMousePosition.x;
510
+ d += " ";
511
+ d += this.currentMousePosition.y - this.lastMousePosition.y;
512
+ this.createdObject.setAttributeNS(null, "d", d);
513
+
514
+ }
515
+
516
+ this.updateCode();
517
+ }
518
+ }