@chocbite/ts-lib-ui 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +48 -2
  2. package/base/index.d.mts +3 -0
  3. package/base/index.mjs +521 -0
  4. package/base/index.mjs.map +1 -0
  5. package/canvas/index.d.mts +74 -0
  6. package/canvas/index.mjs +619 -0
  7. package/canvas/index.mjs.map +1 -0
  8. package/context-menu/index.d.mts +3 -0
  9. package/context-menu/index.mjs +2 -0
  10. package/context-menu-3OazryD0.mjs +527 -0
  11. package/context-menu-3OazryD0.mjs.map +1 -0
  12. package/document/index.d.mts +27 -0
  13. package/document/index.mjs +50 -0
  14. package/document/index.mjs.map +1 -0
  15. package/dom/index.d.mts +3 -0
  16. package/dom/index.mjs +8 -0
  17. package/dom/index.mjs.map +1 -0
  18. package/form/index.d.mts +747 -0
  19. package/form/index.mjs +2694 -0
  20. package/form/index.mjs.map +1 -0
  21. package/icons/index.d.mts +4109 -0
  22. package/icons/index.mjs +2 -0
  23. package/icons/mixins.scss +12 -0
  24. package/icons-CY4BLQ2F.mjs +4123 -0
  25. package/index-BND79jZp.d.mts +212 -0
  26. package/index-BcidwqIy.d.mts +167 -0
  27. package/index-CB3-qP6l.d.mts +120 -0
  28. package/index-CugtUzv_.d.mts +155 -0
  29. package/keyboard/index.d.mts +85 -0
  30. package/keyboard/index.mjs +1499 -0
  31. package/keyboard/index.mjs.map +1 -0
  32. package/list/index.d.mts +3 -0
  33. package/list/index.mjs +2 -0
  34. package/list-BcNSxtNm.mjs +617 -0
  35. package/list-BcNSxtNm.mjs.map +1 -0
  36. package/package.json +95 -4
  37. package/selection/index.d.mts +16 -0
  38. package/selection/index.mjs +96 -0
  39. package/selection/index.mjs.map +1 -0
  40. package/settings/index.d.mts +43 -0
  41. package/settings/index.mjs +110 -0
  42. package/settings/index.mjs.map +1 -0
  43. package/spinner/index.d.mts +10 -0
  44. package/spinner/index.mjs +39 -0
  45. package/spinner/index.mjs.map +1 -0
  46. package/style.css +2994 -0
  47. package/svg/index.d.mts +3 -0
  48. package/svg/index.mjs +2 -0
  49. package/svg-golJPaR4.mjs +358 -0
  50. package/svg-golJPaR4.mjs.map +1 -0
  51. package/theme/index.d.mts +179 -0
  52. package/theme/index.mjs +2 -0
  53. package/theme/mixins.scss +39 -0
  54. package/theme-DhIVwHQw.mjs +338 -0
  55. package/theme-DhIVwHQw.mjs.map +1 -0
  56. package/viewport/index.d.mts +1 -0
  57. package/viewport/index.mjs +2 -0
@@ -0,0 +1,619 @@
1
+ import "../style.css";
2
+ import { Base, define_element } from "../base/index.mjs";
3
+ import { t as svg } from "../svg-golJPaR4.mjs";
4
+ import { node_clone } from "../dom/index.mjs";
5
+ import { Yo as material_autorenew_rounded } from "../icons-CY4BLQ2F.mjs";
6
+ import { state as state$1 } from "@chocbite/ts-lib/state";
7
+ import { ok, some } from "@chocbite/ts-lib/result";
8
+ import { sync_resolve } from "@chocbite/ts-lib/common";
9
+ // src/canvas/viewport_mover.ts
10
+ const nw = svg.svg(32, 32, "0 0 32 32").cl("nw").elem;
11
+ nw.appendChild(svg.create("circle").elem);
12
+ nw.appendChild(svg.line(-8, -8, 8, 8).elem);
13
+ nw.appendChild(svg.isosceles_triangle(-7, -7, 8, 6).rotate(-45, -7, -7).elem);
14
+ nw.appendChild(svg.isosceles_triangle(7, 7, 8, 6).rotate(135, 7, 7).elem);
15
+ const ne = svg.svg(32, 32, "0 0 32 32").cl("ne").elem;
16
+ ne.appendChild(svg.create("circle").elem);
17
+ ne.appendChild(svg.line(-8, 8, 8, -8).elem);
18
+ ne.appendChild(svg.isosceles_triangle(-7, 7, 8, 6).rotate(-135, -7, 7).elem);
19
+ ne.appendChild(svg.isosceles_triangle(7, -7, 8, 6).rotate(45, 7, -7).elem);
20
+ const se = svg.svg(32, 32, "0 0 32 32").cl("se").elem;
21
+ se.appendChild(svg.create("circle").elem);
22
+ se.appendChild(svg.line(-8, -8, 8, 8).elem);
23
+ se.appendChild(svg.isosceles_triangle(-7, -7, 8, 6).rotate(-45, -7, -7).elem);
24
+ se.appendChild(svg.isosceles_triangle(7, 7, 8, 6).rotate(135, 7, 7).elem);
25
+ const sw = svg.svg(32, 32, "0 0 32 32").cl("sw").elem;
26
+ sw.appendChild(svg.create("circle").elem);
27
+ sw.appendChild(svg.line(-8, 8, 8, -8).elem);
28
+ sw.appendChild(svg.isosceles_triangle(-7, 7, 8, 6).rotate(-135, -7, 7).elem);
29
+ sw.appendChild(svg.isosceles_triangle(7, -7, 8, 6).rotate(45, 7, -7).elem);
30
+ const move = svg.svg(32, 32, "0 0 32 32").cl("move").elem;
31
+ move.appendChild(svg.create("circle").elem);
32
+ move.appendChild(svg.line(0, 8, 0, -8).elem);
33
+ move.appendChild(svg.isosceles_triangle(0, 10, 8, 6).rotate(180, 0, 10).elem);
34
+ move.appendChild(svg.isosceles_triangle(0, -10, 8, 6).rotate(0, 0, -10).elem);
35
+ move.appendChild(svg.line(-10, 0, 10, 0).elem);
36
+ move.appendChild(svg.isosceles_triangle(-10, 0, 8, 6).rotate(270, -10, 0).elem);
37
+ move.appendChild(svg.isosceles_triangle(10, 0, 8, 6).rotate(90, 10, 0).elem);
38
+ const rotate_sym = svg.svg(32, 32, "0 0 32 32").a("y", "-30").cl("rotate").elem;
39
+ rotate_sym.appendChild(svg.create("circle").elem);
40
+ rotate_sym.appendChild(svg.attr(material_autorenew_rounded()).a("x", "-12").a("y", "-12").elem);
41
+ const rotation_center_indicator = svg.svg(16, 16, "0 0 16 16").cl("rotation-center").elem;
42
+ rotation_center_indicator.appendChild(svg.create("circle").elem);
43
+ rotation_center_indicator.appendChild(svg.line(-6, 0, 6, 0).elem);
44
+ rotation_center_indicator.appendChild(svg.line(0, -6, 0, 6).elem);
45
+ const DEG_TO_RAD = Math.PI / 180;
46
+ var ViewportMover = class {
47
+ #canvas = svg.create("svg").cl("viewport-mover").elem;
48
+ #outline = this.#canvas.appendChild(svg.rectangle_from_corner(-1, -1, 1, 1, 0).elem);
49
+ #nw_corner = this.#canvas.appendChild(node_clone(nw));
50
+ #ne_corner = this.#canvas.appendChild(node_clone(ne));
51
+ #sw_corner = this.#canvas.appendChild(node_clone(sw));
52
+ #se_corner = this.#canvas.appendChild(node_clone(se));
53
+ #move = this.#canvas.appendChild(node_clone(move));
54
+ #rotate = this.#canvas.appendChild(node_clone(rotate_sym));
55
+ #rc_indicator = this.#canvas.appendChild(node_clone(rotation_center_indicator));
56
+ #scale_buffer = 1;
57
+ #grid_x_buffer = 1;
58
+ #grid_y_buffer = 1;
59
+ #grid_rotate_buffer = 0;
60
+ constructor(scale, grid_x, grid_y, grid_rotate) {
61
+ scale.sub((val) => {
62
+ this.#scale_buffer = val.value;
63
+ const s = `scale(${1 / val.value})`;
64
+ this.#nw_corner.setAttribute("transform", s);
65
+ this.#ne_corner.setAttribute("transform", s);
66
+ this.#sw_corner.setAttribute("transform", s);
67
+ this.#se_corner.setAttribute("transform", s);
68
+ this.#move.setAttribute("transform", s);
69
+ this.#rotate.setAttribute("transform", s);
70
+ this.#rc_indicator.setAttribute("transform", s);
71
+ }, true);
72
+ grid_x.sub((val) => {
73
+ this.#grid_x_buffer = val.value;
74
+ }, true);
75
+ grid_y.sub((val) => {
76
+ this.#grid_y_buffer = val.value;
77
+ }, true);
78
+ grid_rotate.sub((val) => {
79
+ this.#grid_rotate_buffer = val.value;
80
+ }, true);
81
+ this.#setup_position();
82
+ this.#setup_corner(this.#nw_corner, -1, -1);
83
+ this.#setup_corner(this.#ne_corner, 1, -1);
84
+ this.#setup_corner(this.#sw_corner, -1, 1);
85
+ this.#setup_corner(this.#se_corner, 1, 1);
86
+ this.#setup_rotation();
87
+ }
88
+ #update_mover_transform() {
89
+ if (this.#rotation === 0) this.#canvas.removeAttribute("transform");
90
+ else {
91
+ const cx = this.#position_x + this.#rotation_center_x;
92
+ const cy = this.#position_y + this.#rotation_center_y;
93
+ this.#canvas.setAttribute("transform", `rotate(${this.#rotation} ${cx} ${cy})`);
94
+ }
95
+ }
96
+ #element;
97
+ attach_to_element(element, canvas) {
98
+ this.#element = void 0;
99
+ this.#canvas.setAttribute("x", element.position_x.toString());
100
+ this.#canvas.setAttribute("y", element.position_y.toString());
101
+ this.width = element.width;
102
+ this.height = element.height;
103
+ this.position_x = element.position_x;
104
+ this.position_y = element.position_y;
105
+ this.#rotation = element.rotation;
106
+ this.#rotation_center_x = element.rotation_center_x;
107
+ this.#rotation_center_y = element.rotation_center_y;
108
+ this.#update_rc_indicator();
109
+ this.#element = element;
110
+ this.#update_mover_transform();
111
+ canvas.appendChild(this.#canvas);
112
+ }
113
+ detach_from_element() {
114
+ this.#element = void 0;
115
+ this.#canvas.remove();
116
+ }
117
+ #setup_position() {
118
+ this.#move.onpointerdown = (e) => {
119
+ e.preventDefault();
120
+ e.stopPropagation();
121
+ if (!this.#element) return;
122
+ this.#move.setPointerCapture(e.pointerId);
123
+ const x = this.#position_x;
124
+ const y = this.#position_y;
125
+ const offset_x = e.offsetX;
126
+ const offset_y = e.offsetY;
127
+ this.#move.onpointermove = (ev) => {
128
+ if (this.#element) {
129
+ const pos_x = (ev.offsetX - offset_x) / this.#scale_buffer + x;
130
+ this.position_x = ev.shiftKey ? pos_x : Math.round(pos_x / this.#grid_x_buffer) * this.#grid_x_buffer;
131
+ const pos_y = (ev.offsetY - offset_y) / this.#scale_buffer + y;
132
+ this.position_y = ev.shiftKey ? pos_y : Math.round(pos_y / this.#grid_y_buffer) * this.#grid_y_buffer;
133
+ }
134
+ };
135
+ this.#move.onpointerup = (ev) => {
136
+ this.#move.releasePointerCapture(ev.pointerId);
137
+ this.#move.onpointermove = null;
138
+ };
139
+ };
140
+ }
141
+ #position_x = 0;
142
+ set position_x(value) {
143
+ this.#canvas.setAttribute("x", String(value));
144
+ this.#position_x = value;
145
+ if (this.#element) this.#element.position_x = value;
146
+ this.#update_mover_transform();
147
+ }
148
+ #position_y = 0;
149
+ set position_y(value) {
150
+ this.#canvas.setAttribute("y", String(value));
151
+ this.#position_y = value;
152
+ if (this.#element) this.#element.position_y = value;
153
+ this.#update_mover_transform();
154
+ }
155
+ #setup_rotation() {
156
+ this.#rotate.onpointerdown = (e) => {
157
+ e.preventDefault();
158
+ e.stopPropagation();
159
+ if (!this.#element) return;
160
+ this.#rotate.setPointerCapture(e.pointerId);
161
+ const ctm = this.#canvas.getScreenCTM();
162
+ if (!ctm) return;
163
+ const center = new DOMPoint(this.#rotation_center_x, this.#rotation_center_y).matrixTransform(ctm);
164
+ const initial_angle = Math.atan2(e.clientY - center.y, e.clientX - center.x);
165
+ const initial_rotation = this.#rotation;
166
+ this.#rotate.onpointermove = (ev) => {
167
+ if (!this.#element) return;
168
+ const current_angle = Math.atan2(ev.clientY - center.y, ev.clientX - center.x);
169
+ const raw_rotation = initial_rotation + (current_angle - initial_angle) / DEG_TO_RAD;
170
+ this.rotation = ev.shiftKey ? raw_rotation : Math.round(raw_rotation / this.#grid_rotate_buffer) * this.#grid_rotate_buffer;
171
+ };
172
+ this.#rotate.onpointerup = (ev) => {
173
+ this.#rotate.releasePointerCapture(ev.pointerId);
174
+ this.#rotate.onpointermove = null;
175
+ };
176
+ };
177
+ }
178
+ #update_rc_indicator() {
179
+ this.#rc_indicator.setAttribute("x", String(this.#rotation_center_x));
180
+ this.#rc_indicator.setAttribute("y", String(this.#rotation_center_y));
181
+ this.#rc_indicator.setAttribute("transform-origin", `${this.#rotation_center_x} ${this.#rotation_center_y}`);
182
+ }
183
+ #rotation = 0;
184
+ set rotation(value) {
185
+ this.#rotation = value;
186
+ if (this.#element) this.#element.rotation = value;
187
+ this.#update_mover_transform();
188
+ }
189
+ #rotation_center_x = 0;
190
+ #rotation_center_y = 0;
191
+ #setup_corner(handle, sx, sy) {
192
+ handle.onpointerdown = (e) => {
193
+ e.preventDefault();
194
+ e.stopPropagation();
195
+ if (!this.#element) return;
196
+ handle.setPointerCapture(e.pointerId);
197
+ const initial_x = e.clientX;
198
+ const initial_y = e.clientY;
199
+ const initial_w = this.#width;
200
+ const initial_h = this.#height;
201
+ const initial_px = this.#position_x;
202
+ const initial_py = this.#position_y;
203
+ const initial_rcx = this.#rotation_center_x;
204
+ const initial_rcy = this.#rotation_center_y;
205
+ const rad = this.#rotation * DEG_TO_RAD;
206
+ const cos_r = Math.cos(rad);
207
+ const sin_r = Math.sin(rad);
208
+ handle.onpointermove = (ev) => {
209
+ if (!this.#element) return;
210
+ const canvas_dx = (ev.clientX - initial_x) / this.#scale_buffer;
211
+ const canvas_dy = (ev.clientY - initial_y) / this.#scale_buffer;
212
+ const local_dx = cos_r * canvas_dx + sin_r * canvas_dy;
213
+ const local_dy = -sin_r * canvas_dx + cos_r * canvas_dy;
214
+ let new_w = Math.max(1, initial_w + sx * local_dx);
215
+ let new_h = Math.max(1, initial_h + sy * local_dy);
216
+ if (!ev.shiftKey) {
217
+ new_w = Math.round(new_w);
218
+ new_h = Math.round(new_h);
219
+ }
220
+ const new_rcx = initial_w > 0 ? initial_rcx * (new_w / initial_w) : initial_rcx;
221
+ const new_rcy = initial_h > 0 ? initial_rcy * (new_h / initial_h) : initial_rcy;
222
+ const drc_x = initial_rcx - new_rcx;
223
+ const drc_y = initial_rcy - new_rcy;
224
+ const delta_w = new_w - initial_w;
225
+ const delta_h = new_h - initial_h;
226
+ let new_px = initial_px;
227
+ let new_py = initial_py;
228
+ if (sx < 0) {
229
+ new_px -= delta_w * cos_r;
230
+ new_py -= delta_w * sin_r;
231
+ }
232
+ if (sy < 0) {
233
+ new_px += delta_h * sin_r;
234
+ new_py -= delta_h * cos_r;
235
+ }
236
+ new_px += drc_x * (1 - cos_r) + drc_y * sin_r;
237
+ new_py += -drc_x * sin_r + drc_y * (1 - cos_r);
238
+ this.#rotation_center_x = new_rcx;
239
+ this.#rotation_center_y = new_rcy;
240
+ if (this.#element) {
241
+ this.#element.rotation_center_x = new_rcx;
242
+ this.#element.rotation_center_y = new_rcy;
243
+ }
244
+ this.#update_rc_indicator();
245
+ this.width = new_w;
246
+ this.height = new_h;
247
+ this.position_x = new_px;
248
+ this.position_y = new_py;
249
+ };
250
+ handle.onpointerup = (ev) => {
251
+ handle.releasePointerCapture(ev.pointerId);
252
+ handle.onpointermove = null;
253
+ };
254
+ };
255
+ }
256
+ #width = 0;
257
+ set width(value) {
258
+ this.#outline.setAttribute("width", (value + 2).toString());
259
+ this.#ne_corner.setAttribute("x", String(value));
260
+ this.#ne_corner.setAttribute("transform-origin", `${value} 0`);
261
+ this.#se_corner.setAttribute("x", String(value));
262
+ this.#se_corner.setAttribute("transform-origin", `${value} ${this.#height}`);
263
+ this.#move.setAttribute("x", String(value / 2));
264
+ this.#move.setAttribute("transform-origin", `${value / 2} ${this.#height / 2}`);
265
+ this.#rotate.setAttribute("x", String(value / 2));
266
+ this.#rotate.setAttribute("transform-origin", `${value / 2} 0`);
267
+ this.#width = value;
268
+ if (this.#element) this.#element.width = value;
269
+ }
270
+ #height = 0;
271
+ set height(value) {
272
+ this.#outline.setAttribute("height", (value + 2).toString());
273
+ this.#sw_corner.setAttribute("y", String(value));
274
+ this.#sw_corner.setAttribute("transform-origin", `0 ${value}`);
275
+ this.#se_corner.setAttribute("y", String(value));
276
+ this.#se_corner.setAttribute("transform-origin", `${this.#width} ${value}`);
277
+ this.#move.setAttribute("y", String(value / 2));
278
+ this.#move.setAttribute("transform-origin", `${this.#width / 2} ${value / 2}`);
279
+ this.#height = value;
280
+ if (this.#element) this.#element.height = value;
281
+ }
282
+ };
283
+ // src/canvas/viewport.ts
284
+ var Viewport = class extends Base {
285
+ static element_name() {
286
+ return "viewport";
287
+ }
288
+ static element_name_space() {
289
+ return "editor";
290
+ }
291
+ #viewport_width = 0;
292
+ #viewport_width_half = 0;
293
+ #viewport_height = 0;
294
+ #viewport_height_half = 0;
295
+ #resize_observer = new ResizeObserver((a) => {
296
+ this.#viewport_width = a[0].contentRect.width;
297
+ this.#viewport_width_half = this.#viewport_width / 2;
298
+ this.#viewport_height = a[0].contentRect.height;
299
+ this.#viewport_height_half = this.#viewport_height / 2;
300
+ this.#pan_coordinates(this.#pan_x.ok() + (a[0].contentRect.width - this.#viewport_width) / 2, this.#pan_y.ok() + (a[0].contentRect.height - this.#viewport_height) / 2);
301
+ this.#root.setAttribute("viewBox", `0 0 ${this.#viewport_width} ${this.#viewport_height}`);
302
+ });
303
+ #root = this.appendChild(svg.create("svg").elem);
304
+ #panner;
305
+ #zoomer;
306
+ constructor(canvas_width, canvas_height, infinite_canvas = false) {
307
+ super();
308
+ this.#resize_observer.observe(this);
309
+ this.#canvas_width = canvas_width;
310
+ this.#canvas_height = canvas_height;
311
+ this.#panner = this.#root.appendChild(svg.create("svg").a("width", canvas_width.toString()).a("height", canvas_height.toString()).elem);
312
+ this.#zoomer = this.#panner.appendChild(svg.create("svg").a("x", "-50%").a("y", "-50%").a("width", canvas_width.toString()).a("height", canvas_height.toString()).elem);
313
+ (infinite_canvas ? this.#root : this.#zoomer).prepend(svg.create("rect").attribute("x", "0").attribute("y", "0").attribute("width", "100%").attribute("height", "100%").fill("white").elem);
314
+ this.#canvas = this.#zoomer.appendChild(svg.svg(canvas_width, canvas_height, `0 0 ${canvas_width} ${canvas_height}`).elem);
315
+ this.#canvas_elements = this.#canvas.appendChild(svg.create("g").elem);
316
+ let count = 0;
317
+ let mover_x = 0;
318
+ let mover_y = 0;
319
+ let initial_x = 0;
320
+ let initial_y = 0;
321
+ let initial_id = 0;
322
+ let second_initial_id = 0;
323
+ let double_click = 0;
324
+ this.addEventListener("pointerdown", (e) => {
325
+ if (e.pointerType === "mouse" || e.pointerType === "touch") {
326
+ e.preventDefault();
327
+ e.stopPropagation();
328
+ const target = e.target;
329
+ if (target.closest(".viewport-mover")) return;
330
+ const elementCanvas = target.closest(".viewport-element");
331
+ if (elementCanvas) {
332
+ const element = this.#elements.find((el) => el.canvas === elementCanvas);
333
+ if (element) {
334
+ this.attach_mover(element);
335
+ return;
336
+ }
337
+ }
338
+ this.#detach_mover();
339
+ const now = performance.now();
340
+ if (now - double_click < 300) {
341
+ this.#pan_coordinates(0, 0);
342
+ this.#zoom_coordinates(Math.min(this.#viewport_height / this.#canvas_height, this.#viewport_width / this.#canvas_width), 0, 0);
343
+ return;
344
+ }
345
+ double_click = now;
346
+ this.setPointerCapture(e.pointerId);
347
+ if (count === 0) {
348
+ mover_x = this.#pan_x.ok();
349
+ mover_y = this.#pan_y.ok();
350
+ initial_x = e.offsetX;
351
+ initial_y = e.offsetY;
352
+ initial_id = e.pointerId;
353
+ } else if (count === 1) second_initial_id = e.pointerId;
354
+ count++;
355
+ }
356
+ });
357
+ this.onpointermove = (ev) => {
358
+ if (count === 0) return;
359
+ if (ev.pointerId === initial_id) this.#pan_coordinates(mover_x + (ev.offsetX - initial_x) / count, mover_y + (ev.offsetY - initial_y) / count);
360
+ else if (ev.pointerId === second_initial_id) {
361
+ if (count < 2) return;
362
+ this.#pan_coordinates(mover_x + (ev.offsetX - initial_x) / count, mover_y + (ev.offsetY - initial_y) / count);
363
+ }
364
+ };
365
+ this.onpointerup = (ev) => {
366
+ if (this.hasPointerCapture(ev.pointerId)) {
367
+ this.releasePointerCapture(ev.pointerId);
368
+ count--;
369
+ }
370
+ };
371
+ this.addEventListener("wheel", (e) => {
372
+ e.preventDefault();
373
+ e.stopPropagation();
374
+ const move_scale = e.shiftKey ? e.altKey ? .01 : .05 : e.altKey ? 2 : .5;
375
+ if (e.ctrlKey) this.#pan_coordinates(void 0, this.#pan_y.ok() - e.deltaY * move_scale);
376
+ else {
377
+ const scale_scale = e.shiftKey ? e.altKey ? 5e-5 : 1e-4 : e.altKey ? .005 : .001;
378
+ const scale = this.#zoom.ok() * (1 - e.deltaY * scale_scale);
379
+ this.#zoom_coordinates(scale, e.offsetX - this.#viewport_width / 2, e.offsetY - this.#viewport_height / 2);
380
+ }
381
+ this.#pan_coordinates(this.#pan_x.ok() - e.deltaX * move_scale, void 0);
382
+ }, { capture: true });
383
+ document.addEventListener("pointerdown", (e) => {
384
+ if (!this.contains(e.target)) this.#detach_mover();
385
+ });
386
+ window.addEventListener("blur", () => {
387
+ this.#detach_mover();
388
+ });
389
+ }
390
+ #pan_coordinates(x, y) {
391
+ if (x !== void 0) {
392
+ this.#panner.setAttribute("x", (this.#viewport_width_half + x).toFixed(0));
393
+ this.#pan_x.set_ok(x);
394
+ }
395
+ if (y !== void 0) {
396
+ this.#panner.setAttribute("y", (this.#viewport_height_half + y).toFixed(0));
397
+ this.#pan_y.set_ok(y);
398
+ }
399
+ }
400
+ /**Zooms coordinate aware to offset canvas position so hover position stays
401
+ * coordinates are relative to the center */
402
+ #zoom_coordinates(scale, x, y) {
403
+ scale = Math.max(.001, Math.min(1e4, scale));
404
+ const zoom_factor = scale / this.#zoom.ok();
405
+ this.#pan_coordinates(this.#pan_x.ok() * zoom_factor + x * (1 - zoom_factor), this.#pan_y.ok() * zoom_factor + y * (1 - zoom_factor));
406
+ this.#zoomer.style.scale = scale.toString();
407
+ this.#zoom.set_ok(scale);
408
+ }
409
+ #canvas;
410
+ #canvas_width;
411
+ #canvas_height;
412
+ #canvas_elements;
413
+ set canvas_width(value) {
414
+ this.#canvas.setAttribute("width", value.toString());
415
+ this.#canvas.setAttribute("viewBox", `0 0 ${value} ${this.#canvas_height}`);
416
+ this.#zoomer.setAttribute("width", value.toString());
417
+ this.#canvas_width = value;
418
+ }
419
+ get canvas_width() {
420
+ return this.#canvas_width;
421
+ }
422
+ set canvas_height(value) {
423
+ this.#canvas.setAttribute("height", value.toString());
424
+ this.#canvas.setAttribute("viewBox", `0 0 ${this.#canvas_width} ${value}`);
425
+ this.#zoomer.setAttribute("height", value.toString());
426
+ this.#canvas_height = value;
427
+ }
428
+ get canvas_height() {
429
+ return this.#canvas_height;
430
+ }
431
+ #pan_x = state$1.rosw(ok(1), (value) => {
432
+ return sync_resolve(ok(this.#pan_coordinates(value, void 0)));
433
+ });
434
+ pan_x = this.#pan_x.read_write;
435
+ #pan_y = state$1.rosw(ok(1), (value) => {
436
+ return sync_resolve(ok(this.#pan_coordinates(void 0, value)));
437
+ });
438
+ pan_y = this.#pan_y.read_write;
439
+ #zoom = state$1.rosw(ok(1), (value) => sync_resolve(ok(this.#zoom_coordinates(value, 0, 0))));
440
+ zoom = this.#zoom.read_write;
441
+ #grid_x = state$1.rosw(ok(10), (value) => {
442
+ this.#grid_x.set_ok(value);
443
+ return sync_resolve(ok(void 0));
444
+ });
445
+ grid_x = this.#grid_x.read_write;
446
+ #grid_y = state$1.rosw(ok(10), (value) => {
447
+ this.#grid_y.set_ok(value);
448
+ return sync_resolve(ok(void 0));
449
+ });
450
+ grid_y = this.#grid_y.read_write;
451
+ #grid_rotate = state$1.rosw(ok(15), (value) => {
452
+ this.#grid_rotate.set_ok(value);
453
+ return sync_resolve(ok(void 0));
454
+ });
455
+ grid_rotate = this.#grid_rotate.read_write;
456
+ #elements = [];
457
+ #state_sub;
458
+ set elements(elements) {
459
+ if (this.#state_sub) {
460
+ this.detach_state(this.#state_sub);
461
+ this.#state_sub = void 0;
462
+ }
463
+ if (state$1.is.state(elements)) this.#state_sub = this.attach_state(elements, (r) => this.#update_rows(r.ok ? r.value : []));
464
+ else this.#update_rows(elements);
465
+ }
466
+ #update_rows(rows) {
467
+ this.#elements = rows;
468
+ if (rows.length === 0) {
469
+ this.#canvas_elements.replaceChildren();
470
+ return;
471
+ }
472
+ const read = state$1.a.read(rows);
473
+ for (let i = 0; i < read.length; i++) {
474
+ const row = read[i];
475
+ if (row.type === "fresh") this.#canvas_elements.replaceChildren(...row.items.map((i) => i.canvas));
476
+ else if (row.type === "added") {
477
+ const child = this.#canvas_elements.children[row.index];
478
+ const rows = row.items.map((i) => i.canvas);
479
+ if (child) child.before(...rows);
480
+ else this.#canvas_elements.append(...rows);
481
+ } else if (row.type === "removed") for (let i = 0; i < row.items.length; i++) this.#canvas_elements.children[row.index].remove();
482
+ else if (row.type === "changed") for (let i = 0; i < row.items.length; i++) this.#canvas_elements.replaceChild(row.items[i].canvas, this.#canvas_elements.children[row.index + i]);
483
+ else if (row.type === "moved") {
484
+ const extracted = [];
485
+ for (let i = 0; i < row.items.length; i++) {
486
+ const child = this.#canvas_elements.children[row.from_index + i];
487
+ if (child) extracted.push(child);
488
+ child.remove();
489
+ }
490
+ const child = this.#canvas_elements.children[row.to_index];
491
+ for (let i = 0; i < extracted.length; i++) if (child) child.before(extracted[i]);
492
+ else this.#canvas_elements.append(extracted[i]);
493
+ }
494
+ }
495
+ }
496
+ #mover;
497
+ attach_mover(move) {
498
+ (this.#mover ??= new ViewportMover(this.#zoom, this.#grid_x, this.#grid_y, this.#grid_rotate)).attach_to_element(move, this.#canvas);
499
+ }
500
+ #detach_mover() {
501
+ this.#mover?.detach_from_element();
502
+ }
503
+ };
504
+ define_element(Viewport);
505
+ function create_viewport(canvas_width, canvas_height, infinite_canvas = false) {
506
+ return new Viewport(canvas_width, canvas_height, infinite_canvas);
507
+ }
508
+ // src/canvas/viewport_element.ts
509
+ var ViewportElement = class {
510
+ /**Element optimal ratio, returns the ratio where the element has no distortion.
511
+ * If the element can dynamically change based on ratio, return none. */
512
+ optimal_ratio() {
513
+ const w = this.default_width();
514
+ const h = this.default_height();
515
+ if (w === 0 || h === 0) return some(0);
516
+ return some(w / h);
517
+ }
518
+ /**Called when the element has no optimal ratio and its dimensions change.
519
+ * Descendants can override this to re-layout their content in the new
520
+ * coordinate space defined by the updated viewBox. */
521
+ on_resize(_width, _height) {}
522
+ canvas = svg.svg(this.default_width(), this.default_height(), `0 0 ${this.default_width()} ${this.default_height()}`).cl("viewport-element", `${this.element_name_space()}-${this.element_name()}`).elem;
523
+ constructor() {
524
+ if (this.optimal_ratio().some) this.canvas.setAttribute("preserveAspectRatio", "none");
525
+ }
526
+ #position_x = 0;
527
+ #position_y = 0;
528
+ set position_x(value) {
529
+ this.#position_x = value;
530
+ this.canvas.setAttribute("x", value.toString());
531
+ this.#update_transform();
532
+ }
533
+ get position_x() {
534
+ return this.#position_x;
535
+ }
536
+ set position_y(value) {
537
+ this.#position_y = value;
538
+ this.canvas.setAttribute("y", value.toString());
539
+ this.#update_transform();
540
+ }
541
+ get position_y() {
542
+ return this.#position_y;
543
+ }
544
+ #rotation = 0;
545
+ #rotation_center_x = this.default_width() / 2;
546
+ #rotation_center_y = this.default_height() / 2;
547
+ set rotation(value) {
548
+ this.#rotation = value;
549
+ this.#update_transform();
550
+ }
551
+ get rotation() {
552
+ return this.#rotation;
553
+ }
554
+ /**Rotation center x coordinate, relative to the element's display area.
555
+ * Defaults to half of default_width (center of element). */
556
+ set rotation_center_x(value) {
557
+ this.#rotation_center_x = value;
558
+ this.#update_transform();
559
+ }
560
+ get rotation_center_x() {
561
+ return this.#rotation_center_x;
562
+ }
563
+ /**Rotation center y coordinate, relative to the element's display area.
564
+ * Defaults to half of default_height (center of element). */
565
+ set rotation_center_y(value) {
566
+ this.#rotation_center_y = value;
567
+ this.#update_transform();
568
+ }
569
+ get rotation_center_y() {
570
+ return this.#rotation_center_y;
571
+ }
572
+ #update_transform() {
573
+ if (this.#rotation === 0) this.canvas.removeAttribute("transform");
574
+ else {
575
+ const cx = this.#position_x + this.#rotation_center_x;
576
+ const cy = this.#position_y + this.#rotation_center_y;
577
+ this.canvas.setAttribute("transform", `rotate(${this.#rotation} ${cx} ${cy})`);
578
+ }
579
+ }
580
+ #width = this.default_width();
581
+ #height = this.default_height();
582
+ /**Display width of the element. Changing this scales the element.
583
+ * - If the element has an optimal_ratio, the viewBox stays at default
584
+ * dimensions and the SVG content is stretched/scaled to fill the new size.
585
+ * - If the element has no optimal_ratio, the viewBox is updated to match
586
+ * the new dimensions and on_resize is called. */
587
+ set width(value) {
588
+ this.#width = value;
589
+ this.#apply_size();
590
+ }
591
+ get width() {
592
+ return this.#width;
593
+ }
594
+ /**Display height of the element. Changing this scales the element.
595
+ * - If the element has an optimal_ratio, the viewBox stays at default
596
+ * dimensions and the SVG content is stretched/scaled to fill the new size.
597
+ * - If the element has no optimal_ratio, the viewBox is updated to match
598
+ * the new dimensions and on_resize is called. */
599
+ set height(value) {
600
+ this.#height = value;
601
+ this.#apply_size();
602
+ }
603
+ get height() {
604
+ return this.#height;
605
+ }
606
+ #apply_size() {
607
+ this.canvas.setAttribute("width", this.#width.toString());
608
+ this.canvas.setAttribute("height", this.#height.toString());
609
+ if (this.optimal_ratio().none) {
610
+ this.canvas.setAttribute("viewBox", `0 0 ${this.#width} ${this.#height}`);
611
+ this.on_resize(this.#width, this.#height);
612
+ }
613
+ }
614
+ };
615
+ // src/canvas/index.ts
616
+ const viewport = { create_viewport };
617
+ export { ViewportElement, viewport };
618
+
619
+ //# sourceMappingURL=index.mjs.map