@chocbite/ts-lib-ui 1.0.3 → 1.0.5
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.
- package/AGENTS.md +609 -0
- package/README.md +0 -1
- package/SOURCE.md +17135 -0
- package/base/index.mjs +1 -1
- package/base/index.mjs.map +1 -1
- package/canvas/index.d.mts +114 -59
- package/canvas/index.mjs +661 -575
- package/canvas/index.mjs.map +1 -1
- package/context-menu/index.mjs +1 -1
- package/{context-menu-_9dMpC-H.mjs → context-menu-CA4o4fXx.mjs} +2 -2
- package/{context-menu-_9dMpC-H.mjs.map → context-menu-CA4o4fXx.mjs.map} +1 -1
- package/form/index.d.mts +1 -1
- package/form/index.mjs +4 -4
- package/{index-CzpbJoAj.d.mts → index-DCn7acFY.d.mts} +13 -4
- package/keyboard/index.mjs +1 -1
- package/list/index.d.mts +1 -1
- package/list/index.mjs +1 -1
- package/{list-Bkm9Ailw.mjs → list-CNYJ3sm-.mjs} +131 -21
- package/list-CNYJ3sm-.mjs.map +1 -0
- package/package.json +7 -7
- package/spinner/index.mjs +1 -1
- package/state_ui/index.mjs +2 -0
- package/{state-OCzEmq3j.mjs → state_ui-Bm3TeKNL.mjs} +3 -3
- package/state_ui-Bm3TeKNL.mjs.map +1 -0
- package/style.css +409 -135
- package/test/index.d.mts +98 -0
- package/test/index.mjs +193 -0
- package/test/index.mjs.map +1 -0
- package/theme/index.mjs +1 -1
- package/{theme-DlD5P5tY.mjs → theme-ChmpRObz.mjs} +2 -2
- package/{theme-DlD5P5tY.mjs.map → theme-ChmpRObz.mjs.map} +1 -1
- package/list-Bkm9Ailw.mjs.map +0 -1
- package/state/index.mjs +0 -2
- package/state-OCzEmq3j.mjs.map +0 -1
- package/viewport/index.d.mts +0 -1
- package/viewport/index.mjs +0 -2
- /package/{state → state_ui}/index.d.mts +0 -0
package/canvas/index.mjs
CHANGED
|
@@ -1,617 +1,703 @@
|
|
|
1
1
|
import "../style.css";
|
|
2
2
|
import { Base, define_element } from "../base/index.mjs";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
sw.appendChild(svg.line(-8, 8, 8, -8).elem);
|
|
29
|
-
sw.appendChild(svg.isosceles_triangle(-7, 7, 8, 6).rotate(-135, -7, 7).elem);
|
|
30
|
-
sw.appendChild(svg.isosceles_triangle(7, -7, 8, 6).rotate(45, 7, -7).elem);
|
|
31
|
-
const move = svg.svg(32, 32, "0 0 32 32").cl("move").elem;
|
|
32
|
-
move.appendChild(svg.create("circle").elem);
|
|
33
|
-
move.appendChild(svg.line(0, 8, 0, -8).elem);
|
|
34
|
-
move.appendChild(svg.isosceles_triangle(0, 10, 8, 6).rotate(180, 0, 10).elem);
|
|
35
|
-
move.appendChild(svg.isosceles_triangle(0, -10, 8, 6).rotate(0, 0, -10).elem);
|
|
36
|
-
move.appendChild(svg.line(-10, 0, 10, 0).elem);
|
|
37
|
-
move.appendChild(svg.isosceles_triangle(-10, 0, 8, 6).rotate(270, -10, 0).elem);
|
|
38
|
-
move.appendChild(svg.isosceles_triangle(10, 0, 8, 6).rotate(90, 10, 0).elem);
|
|
39
|
-
const rotate_sym = svg.svg(32, 32, "0 0 32 32").a("y", "-30").cl("rotate").elem;
|
|
40
|
-
rotate_sym.appendChild(svg.create("circle").elem);
|
|
41
|
-
rotate_sym.appendChild(svg.attr(material_autorenew_rounded()).a("x", "-12").a("y", "-12").elem);
|
|
42
|
-
const rotation_center_indicator = svg.svg(16, 16, "0 0 16 16").cl("rotation-center").elem;
|
|
43
|
-
rotation_center_indicator.appendChild(svg.create("circle").elem);
|
|
44
|
-
rotation_center_indicator.appendChild(svg.line(-6, 0, 6, 0).elem);
|
|
45
|
-
rotation_center_indicator.appendChild(svg.line(0, -6, 0, 6).elem);
|
|
46
|
-
const DEG_TO_RAD = Math.PI / 180;
|
|
47
|
-
/**Rounds the value to the grid, a grid of 0 does not snap*/
|
|
48
|
-
const snap = (value, grid) => grid ? Math.round(value / grid) * grid : value;
|
|
49
|
-
/**Stops a handle following the pointer, for pointerup and pointercancel*/
|
|
50
|
-
const handle_release = (ev) => {
|
|
51
|
-
const handle = ev.currentTarget;
|
|
52
|
-
if (handle.hasPointerCapture(ev.pointerId)) handle.releasePointerCapture(ev.pointerId);
|
|
53
|
-
handle.onpointermove = null;
|
|
54
|
-
};
|
|
55
|
-
var ViewportMover = class {
|
|
56
|
-
#canvas = svg.create("svg").cl("viewport-mover").elem;
|
|
57
|
-
#outline = this.#canvas.appendChild(svg.rectangle_from_corner(-1, -1, 1, 1, 0).elem);
|
|
58
|
-
#nw_corner = this.#canvas.appendChild(node_clone(nw));
|
|
59
|
-
#ne_corner = this.#canvas.appendChild(node_clone(ne));
|
|
60
|
-
#sw_corner = this.#canvas.appendChild(node_clone(sw));
|
|
61
|
-
#se_corner = this.#canvas.appendChild(node_clone(se));
|
|
62
|
-
#move = this.#canvas.appendChild(node_clone(move));
|
|
63
|
-
#rotate = this.#canvas.appendChild(node_clone(rotate_sym));
|
|
64
|
-
#rc_indicator = this.#canvas.appendChild(node_clone(rotation_center_indicator));
|
|
65
|
-
#scale_buffer = 1;
|
|
66
|
-
#grid_x_buffer = 1;
|
|
67
|
-
#grid_y_buffer = 1;
|
|
68
|
-
#grid_rotate_buffer = 0;
|
|
69
|
-
constructor(scale, grid_x, grid_y, grid_rotate) {
|
|
70
|
-
scale.sub((val) => {
|
|
71
|
-
this.#scale_buffer = val.value;
|
|
72
|
-
const s = `scale(${1 / val.value})`;
|
|
73
|
-
this.#nw_corner.setAttribute("transform", s);
|
|
74
|
-
this.#ne_corner.setAttribute("transform", s);
|
|
75
|
-
this.#sw_corner.setAttribute("transform", s);
|
|
76
|
-
this.#se_corner.setAttribute("transform", s);
|
|
77
|
-
this.#move.setAttribute("transform", s);
|
|
78
|
-
this.#rotate.setAttribute("transform", s);
|
|
79
|
-
this.#rc_indicator.setAttribute("transform", s);
|
|
80
|
-
}, true);
|
|
81
|
-
grid_x.sub((val) => {
|
|
82
|
-
this.#grid_x_buffer = val.value;
|
|
83
|
-
}, true);
|
|
84
|
-
grid_y.sub((val) => {
|
|
85
|
-
this.#grid_y_buffer = val.value;
|
|
86
|
-
}, true);
|
|
87
|
-
grid_rotate.sub((val) => {
|
|
88
|
-
this.#grid_rotate_buffer = val.value;
|
|
89
|
-
}, true);
|
|
90
|
-
this.#setup_position();
|
|
91
|
-
this.#setup_corner(this.#nw_corner, -1, -1);
|
|
92
|
-
this.#setup_corner(this.#ne_corner, 1, -1);
|
|
93
|
-
this.#setup_corner(this.#sw_corner, -1, 1);
|
|
94
|
-
this.#setup_corner(this.#se_corner, 1, 1);
|
|
95
|
-
this.#setup_rotation();
|
|
96
|
-
}
|
|
97
|
-
#update_mover_transform() {
|
|
98
|
-
if (this.#rotation === 0) this.#canvas.removeAttribute("transform");
|
|
99
|
-
else {
|
|
100
|
-
const cx = this.#position_x + this.#rotation_center_x;
|
|
101
|
-
const cy = this.#position_y + this.#rotation_center_y;
|
|
102
|
-
this.#canvas.setAttribute("transform", `rotate(${this.#rotation} ${cx} ${cy})`);
|
|
103
|
-
}
|
|
3
|
+
import { i as theme_init_variable_root } from "../theme-ChmpRObz.mjs";
|
|
4
|
+
import { $q as material_open_with_rounded, ha as material_arrow_range_rounded, r2 as material_rotate_right_rounded } from "../icons-e2J_Ojic.mjs";
|
|
5
|
+
import { BLUE, GREY } from "@chocbite/ts-lib/colors";
|
|
6
|
+
// src/canvas/shared.ts
|
|
7
|
+
const theme_root = theme_init_variable_root("canvas", "Canvas", "Theme variables for canvas viewports and pages");
|
|
8
|
+
const colors = theme_root.make_sub_group("colors", "Colors", "Colors used in viewports");
|
|
9
|
+
colors.make_variable("background", "Background Color", "Color of the viewport around the page", GREY["300"], GREY["900"], "Color", void 0);
|
|
10
|
+
colors.make_variable("pageBackground", "Page Background Color", "Color of the page behind the widgets", GREY["50"], GREY["800"], "Color", void 0);
|
|
11
|
+
const edit = theme_root.make_sub_group("edit", "Edit", "Settings for viewports in edit mode");
|
|
12
|
+
edit.make_variable("checker1", "Checkerboard First Color", "First color of the checkerboard shown behind the page in edit mode", GREY["300"], GREY["600"], "Color", void 0);
|
|
13
|
+
edit.make_variable("checker2", "Checkerboard Second Color", "Second color of the checkerboard shown behind the page in edit mode", GREY["200"], GREY["700"], "Color", void 0);
|
|
14
|
+
edit.make_variable("checkerSize", "Checkerboard Square Size", "Size of one square of the checkerboard shown behind the page in edit mode", "0.625rem", "0.625rem", "Length", {
|
|
15
|
+
min: .1,
|
|
16
|
+
max: 10
|
|
17
|
+
});
|
|
18
|
+
const transformer = theme_root.make_sub_group("transformer", "Transformer", "Settings for the transformer used to move, resize and rotate widgets in edit mode");
|
|
19
|
+
transformer.make_variable("color", "Transformer Color", "Color of the transformer outline and handle borders", BLUE["600"], BLUE["300"], "Color", void 0);
|
|
20
|
+
transformer.make_variable("handleBackground", "Handle Background Color", "Background color of the transformer handles", GREY["50"], GREY["900"], "Color", void 0);
|
|
21
|
+
// src/canvas/page.ts
|
|
22
|
+
/**Page of widgets with a fixed size
|
|
23
|
+
* Width and height are the size of the page in page units (px before any viewport scaling)*/
|
|
24
|
+
var Page = class extends Base {
|
|
25
|
+
/**Returns the name used to define the element */
|
|
26
|
+
static element_name() {
|
|
27
|
+
return "page";
|
|
104
28
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return this.#element;
|
|
109
|
-
}
|
|
110
|
-
attach_to_element(element, canvas) {
|
|
111
|
-
this.#element = void 0;
|
|
112
|
-
this.#canvas.setAttribute("x", element.position_x.toString());
|
|
113
|
-
this.#canvas.setAttribute("y", element.position_y.toString());
|
|
114
|
-
this.width = element.width;
|
|
115
|
-
this.height = element.height;
|
|
116
|
-
this.position_x = element.position_x;
|
|
117
|
-
this.position_y = element.position_y;
|
|
118
|
-
this.#rotation = element.rotation;
|
|
119
|
-
this.#rotation_center_x = element.rotation_center_x;
|
|
120
|
-
this.#rotation_center_y = element.rotation_center_y;
|
|
121
|
-
this.#update_rc_indicator();
|
|
122
|
-
this.#element = element;
|
|
123
|
-
this.#update_mover_transform();
|
|
124
|
-
canvas.appendChild(this.#canvas);
|
|
125
|
-
}
|
|
126
|
-
detach_from_element() {
|
|
127
|
-
this.#element = void 0;
|
|
128
|
-
this.#canvas.remove();
|
|
129
|
-
}
|
|
130
|
-
#setup_position() {
|
|
131
|
-
this.#move.onpointerdown = (e) => {
|
|
132
|
-
e.preventDefault();
|
|
133
|
-
e.stopPropagation();
|
|
134
|
-
if (!this.#element) return;
|
|
135
|
-
this.#move.setPointerCapture(e.pointerId);
|
|
136
|
-
const x = this.#position_x;
|
|
137
|
-
const y = this.#position_y;
|
|
138
|
-
const offset_x = e.offsetX;
|
|
139
|
-
const offset_y = e.offsetY;
|
|
140
|
-
this.#move.onpointermove = (ev) => {
|
|
141
|
-
if (this.#element) {
|
|
142
|
-
const pos_x = (ev.offsetX - offset_x) / this.#scale_buffer + x;
|
|
143
|
-
this.position_x = ev.shiftKey ? pos_x : snap(pos_x, this.#grid_x_buffer);
|
|
144
|
-
const pos_y = (ev.offsetY - offset_y) / this.#scale_buffer + y;
|
|
145
|
-
this.position_y = ev.shiftKey ? pos_y : snap(pos_y, this.#grid_y_buffer);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
};
|
|
149
|
-
this.#move.onpointerup = handle_release;
|
|
150
|
-
this.#move.onpointercancel = handle_release;
|
|
151
|
-
}
|
|
152
|
-
#position_x = 0;
|
|
153
|
-
set position_x(value) {
|
|
154
|
-
this.#canvas.setAttribute("x", String(value));
|
|
155
|
-
this.#position_x = value;
|
|
156
|
-
if (this.#element) this.#element.position_x = value;
|
|
157
|
-
this.#update_mover_transform();
|
|
158
|
-
}
|
|
159
|
-
#position_y = 0;
|
|
160
|
-
set position_y(value) {
|
|
161
|
-
this.#canvas.setAttribute("y", String(value));
|
|
162
|
-
this.#position_y = value;
|
|
163
|
-
if (this.#element) this.#element.position_y = value;
|
|
164
|
-
this.#update_mover_transform();
|
|
165
|
-
}
|
|
166
|
-
#setup_rotation() {
|
|
167
|
-
this.#rotate.onpointerdown = (e) => {
|
|
168
|
-
e.preventDefault();
|
|
169
|
-
e.stopPropagation();
|
|
170
|
-
if (!this.#element) return;
|
|
171
|
-
this.#rotate.setPointerCapture(e.pointerId);
|
|
172
|
-
const ctm = this.#canvas.getScreenCTM();
|
|
173
|
-
if (!ctm) return;
|
|
174
|
-
const center = new DOMPoint(this.#rotation_center_x, this.#rotation_center_y).matrixTransform(ctm);
|
|
175
|
-
const initial_angle = Math.atan2(e.clientY - center.y, e.clientX - center.x);
|
|
176
|
-
const initial_rotation = this.#rotation;
|
|
177
|
-
this.#rotate.onpointermove = (ev) => {
|
|
178
|
-
if (!this.#element) return;
|
|
179
|
-
const current_angle = Math.atan2(ev.clientY - center.y, ev.clientX - center.x);
|
|
180
|
-
const raw_rotation = initial_rotation + (current_angle - initial_angle) / DEG_TO_RAD;
|
|
181
|
-
this.rotation = ev.shiftKey ? raw_rotation : snap(raw_rotation, this.#grid_rotate_buffer);
|
|
182
|
-
};
|
|
183
|
-
};
|
|
184
|
-
this.#rotate.onpointerup = handle_release;
|
|
185
|
-
this.#rotate.onpointercancel = handle_release;
|
|
29
|
+
/**Returns the namespace override for the element*/
|
|
30
|
+
static element_name_space() {
|
|
31
|
+
return "canvas";
|
|
186
32
|
}
|
|
187
|
-
#
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
33
|
+
#width = 1920;
|
|
34
|
+
#height = 1080;
|
|
35
|
+
#widgets = [];
|
|
36
|
+
constructor() {
|
|
37
|
+
super();
|
|
38
|
+
this.style.width = this.#width + "px";
|
|
39
|
+
this.style.height = this.#height + "px";
|
|
191
40
|
}
|
|
192
|
-
|
|
193
|
-
set
|
|
194
|
-
this.#
|
|
195
|
-
|
|
196
|
-
this.#update_mover_transform();
|
|
197
|
-
}
|
|
198
|
-
#rotation_center_x = 0;
|
|
199
|
-
#rotation_center_y = 0;
|
|
200
|
-
#setup_corner(handle, sx, sy) {
|
|
201
|
-
handle.onpointerdown = (e) => {
|
|
202
|
-
e.preventDefault();
|
|
203
|
-
e.stopPropagation();
|
|
204
|
-
if (!this.#element) return;
|
|
205
|
-
handle.setPointerCapture(e.pointerId);
|
|
206
|
-
const initial_x = e.clientX;
|
|
207
|
-
const initial_y = e.clientY;
|
|
208
|
-
const initial_w = this.#width;
|
|
209
|
-
const initial_h = this.#height;
|
|
210
|
-
const initial_px = this.#position_x;
|
|
211
|
-
const initial_py = this.#position_y;
|
|
212
|
-
const initial_rcx = this.#rotation_center_x;
|
|
213
|
-
const initial_rcy = this.#rotation_center_y;
|
|
214
|
-
const rad = this.#rotation * DEG_TO_RAD;
|
|
215
|
-
const cos_r = Math.cos(rad);
|
|
216
|
-
const sin_r = Math.sin(rad);
|
|
217
|
-
handle.onpointermove = (ev) => {
|
|
218
|
-
if (!this.#element) return;
|
|
219
|
-
const canvas_dx = (ev.clientX - initial_x) / this.#scale_buffer;
|
|
220
|
-
const canvas_dy = (ev.clientY - initial_y) / this.#scale_buffer;
|
|
221
|
-
const local_dx = cos_r * canvas_dx + sin_r * canvas_dy;
|
|
222
|
-
const local_dy = -sin_r * canvas_dx + cos_r * canvas_dy;
|
|
223
|
-
let new_w = Math.max(1, initial_w + sx * local_dx);
|
|
224
|
-
let new_h = Math.max(1, initial_h + sy * local_dy);
|
|
225
|
-
if (!ev.shiftKey) {
|
|
226
|
-
new_w = Math.round(new_w);
|
|
227
|
-
new_h = Math.round(new_h);
|
|
228
|
-
}
|
|
229
|
-
const new_rcx = initial_w > 0 ? initial_rcx * (new_w / initial_w) : initial_rcx;
|
|
230
|
-
const new_rcy = initial_h > 0 ? initial_rcy * (new_h / initial_h) : initial_rcy;
|
|
231
|
-
const drc_x = initial_rcx - new_rcx;
|
|
232
|
-
const drc_y = initial_rcy - new_rcy;
|
|
233
|
-
const delta_w = new_w - initial_w;
|
|
234
|
-
const delta_h = new_h - initial_h;
|
|
235
|
-
let new_px = initial_px;
|
|
236
|
-
let new_py = initial_py;
|
|
237
|
-
if (sx < 0) {
|
|
238
|
-
new_px -= delta_w * cos_r;
|
|
239
|
-
new_py -= delta_w * sin_r;
|
|
240
|
-
}
|
|
241
|
-
if (sy < 0) {
|
|
242
|
-
new_px += delta_h * sin_r;
|
|
243
|
-
new_py -= delta_h * cos_r;
|
|
244
|
-
}
|
|
245
|
-
new_px += drc_x * (1 - cos_r) + drc_y * sin_r;
|
|
246
|
-
new_py += -drc_x * sin_r + drc_y * (1 - cos_r);
|
|
247
|
-
this.#rotation_center_x = new_rcx;
|
|
248
|
-
this.#rotation_center_y = new_rcy;
|
|
249
|
-
if (this.#element) {
|
|
250
|
-
this.#element.rotation_center_x = new_rcx;
|
|
251
|
-
this.#element.rotation_center_y = new_rcy;
|
|
252
|
-
}
|
|
253
|
-
this.#update_rc_indicator();
|
|
254
|
-
this.width = new_w;
|
|
255
|
-
this.height = new_h;
|
|
256
|
-
this.position_x = new_px;
|
|
257
|
-
this.position_y = new_py;
|
|
258
|
-
};
|
|
259
|
-
};
|
|
260
|
-
handle.onpointerup = handle_release;
|
|
261
|
-
handle.onpointercancel = handle_release;
|
|
41
|
+
/**Sets the width of the page */
|
|
42
|
+
set width(width) {
|
|
43
|
+
this.#width = width;
|
|
44
|
+
this.style.width = width + "px";
|
|
262
45
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
this.#
|
|
266
|
-
this.#ne_corner.setAttribute("x", String(value));
|
|
267
|
-
this.#ne_corner.setAttribute("transform-origin", `${value} 0`);
|
|
268
|
-
this.#se_corner.setAttribute("x", String(value));
|
|
269
|
-
this.#se_corner.setAttribute("transform-origin", `${value} ${this.#height}`);
|
|
270
|
-
this.#move.setAttribute("x", String(value / 2));
|
|
271
|
-
this.#move.setAttribute("transform-origin", `${value / 2} ${this.#height / 2}`);
|
|
272
|
-
this.#rotate.setAttribute("x", String(value / 2));
|
|
273
|
-
this.#rotate.setAttribute("transform-origin", `${value / 2} 0`);
|
|
274
|
-
this.#width = value;
|
|
275
|
-
if (this.#element) this.#element.width = value;
|
|
46
|
+
/**Returns the width of the page */
|
|
47
|
+
get width() {
|
|
48
|
+
return this.#width;
|
|
276
49
|
}
|
|
277
|
-
|
|
278
|
-
set height(
|
|
279
|
-
this.#
|
|
280
|
-
this
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
this.#
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
50
|
+
/**Sets the height of the page */
|
|
51
|
+
set height(height) {
|
|
52
|
+
this.#height = height;
|
|
53
|
+
this.style.height = height + "px";
|
|
54
|
+
}
|
|
55
|
+
/**Returns the height of the page */
|
|
56
|
+
get height() {
|
|
57
|
+
return this.#height;
|
|
58
|
+
}
|
|
59
|
+
/**Sets the widgets on the page, the array order is the drawing order with the last widget on top
|
|
60
|
+
* Widgets already on the page are kept in place, so they are only reconnected if their order changes */
|
|
61
|
+
set widgets(widgets) {
|
|
62
|
+
const keep = new Set(widgets);
|
|
63
|
+
for (const widget of this.#widgets) if (!keep.has(widget) && widget.parentElement === this) widget.remove();
|
|
64
|
+
for (let i = 0; i < widgets.length; i++) {
|
|
65
|
+
const current = this.children[i];
|
|
66
|
+
if (current !== widgets[i]) this.insertBefore(widgets[i], current ?? null);
|
|
67
|
+
}
|
|
68
|
+
this.#widgets = widgets;
|
|
69
|
+
}
|
|
70
|
+
/**Returns the widgets on the page */
|
|
71
|
+
get widgets() {
|
|
72
|
+
return this.#widgets;
|
|
288
73
|
}
|
|
289
74
|
};
|
|
290
|
-
|
|
291
|
-
|
|
75
|
+
define_element(Page);
|
|
76
|
+
/**Creates a page of widgets with a fixed size */
|
|
77
|
+
const canvas_page = (options) => options ? new Page().opts(options) : new Page();
|
|
78
|
+
// src/canvas/widget.ts
|
|
79
|
+
/**Event dispatched on a widget when its position, size or rotation changes */
|
|
80
|
+
const WIDGET_TRANSFORM_EVENT = "widget_transform";
|
|
81
|
+
/**Shared class for widgets placed at a fixed position in a viewport
|
|
82
|
+
* Position and size are in viewport units (px before any viewport scaling)
|
|
83
|
+
* Rotation is in degrees clockwise around the center of the widget
|
|
84
|
+
*
|
|
85
|
+
* Width and height must always be set, they default to 0
|
|
86
|
+
*
|
|
87
|
+
* Widgets declare their original size, which is the size their content is laid out at
|
|
88
|
+
* The content is then scaled from the original size to the width and height of the widget
|
|
89
|
+
* Widgets that should not stretch, like text, return width and height as their original size
|
|
90
|
+
*
|
|
91
|
+
* WIDGET_TRANSFORM_EVENT is dispatched whenever position, size or rotation changes*/
|
|
92
|
+
var WidgetBase = class extends Base {
|
|
93
|
+
/**Returns the name used to define the element, Abstract classes should return @abstract@ */
|
|
292
94
|
static element_name() {
|
|
293
|
-
return "
|
|
95
|
+
return "@abstract@";
|
|
294
96
|
}
|
|
97
|
+
/**Returns the namespace override for the element*/
|
|
295
98
|
static element_name_space() {
|
|
296
|
-
return "
|
|
297
|
-
}
|
|
298
|
-
#
|
|
299
|
-
#
|
|
300
|
-
#
|
|
301
|
-
#
|
|
302
|
-
#
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
this
|
|
306
|
-
this
|
|
307
|
-
this
|
|
308
|
-
|
|
309
|
-
});
|
|
310
|
-
#root = this.appendChild(svg.create("svg").elem);
|
|
311
|
-
#panner;
|
|
312
|
-
#zoomer;
|
|
313
|
-
/**Detaches the mover when clicking outside the viewport*/
|
|
314
|
-
#outside_pointerdown = (e) => {
|
|
315
|
-
if (!this.contains(e.target)) this.#detach_mover();
|
|
316
|
-
};
|
|
317
|
-
/**Detaches the mover when the browser window loses focus*/
|
|
318
|
-
#window_blur = () => this.#detach_mover();
|
|
319
|
-
/**Document the listeners are attached to, while connected*/
|
|
320
|
-
#listening_document;
|
|
99
|
+
return "canvas";
|
|
100
|
+
}
|
|
101
|
+
#x = 0;
|
|
102
|
+
#y = 0;
|
|
103
|
+
#width = 0;
|
|
104
|
+
#height = 0;
|
|
105
|
+
#rotation = 0;
|
|
106
|
+
constructor() {
|
|
107
|
+
super();
|
|
108
|
+
this.style.position = "absolute";
|
|
109
|
+
this.style.boxSizing = "border-box";
|
|
110
|
+
this.style.transformOrigin = "0 0";
|
|
111
|
+
}
|
|
321
112
|
connectedCallback() {
|
|
322
113
|
super.connectedCallback();
|
|
323
|
-
this.#
|
|
324
|
-
this.#listening_document.addEventListener("pointerdown", this.#outside_pointerdown);
|
|
325
|
-
this.#listening_document.defaultView?.addEventListener("blur", this.#window_blur);
|
|
114
|
+
this.#update();
|
|
326
115
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
this.#
|
|
330
|
-
this.#
|
|
331
|
-
this.#listening_document = void 0;
|
|
116
|
+
/**Sets the x position of the left edge of the widget */
|
|
117
|
+
set x(x) {
|
|
118
|
+
this.#x = x;
|
|
119
|
+
this.#update();
|
|
332
120
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
this.#
|
|
336
|
-
this.#canvas_width = canvas_width;
|
|
337
|
-
this.#canvas_height = canvas_height;
|
|
338
|
-
this.#panner = this.#root.appendChild(svg.create("svg").a("width", canvas_width.toString()).a("height", canvas_height.toString()).elem);
|
|
339
|
-
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);
|
|
340
|
-
(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);
|
|
341
|
-
this.#canvas = this.#zoomer.appendChild(svg.svg(canvas_width, canvas_height, `0 0 ${canvas_width} ${canvas_height}`).elem);
|
|
342
|
-
this.#canvas_elements = this.#canvas.appendChild(svg.create("g").elem);
|
|
343
|
-
let count = 0;
|
|
344
|
-
let mover_x = 0;
|
|
345
|
-
let mover_y = 0;
|
|
346
|
-
let initial_x = 0;
|
|
347
|
-
let initial_y = 0;
|
|
348
|
-
let initial_id = 0;
|
|
349
|
-
let second_initial_id = 0;
|
|
350
|
-
let double_click = 0;
|
|
351
|
-
this.addEventListener("pointerdown", (e) => {
|
|
352
|
-
if (e.pointerType === "mouse" || e.pointerType === "touch") {
|
|
353
|
-
e.preventDefault();
|
|
354
|
-
e.stopPropagation();
|
|
355
|
-
const target = e.target;
|
|
356
|
-
if (target.closest(".viewport-mover")) return;
|
|
357
|
-
const elementCanvas = target.closest(".viewport-element");
|
|
358
|
-
if (elementCanvas) {
|
|
359
|
-
const element = this.#elements.find((el) => el.canvas === elementCanvas);
|
|
360
|
-
if (element) {
|
|
361
|
-
this.attach_mover(element);
|
|
362
|
-
return;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
this.#detach_mover();
|
|
366
|
-
const now = performance.now();
|
|
367
|
-
if (now - double_click < 300) {
|
|
368
|
-
this.#pan_coordinates(0, 0);
|
|
369
|
-
this.#zoom_coordinates(Math.min(this.#viewport_height / this.#canvas_height, this.#viewport_width / this.#canvas_width), 0, 0);
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
372
|
-
double_click = now;
|
|
373
|
-
this.setPointerCapture(e.pointerId);
|
|
374
|
-
if (count === 0) {
|
|
375
|
-
mover_x = this.#pan_x.ok();
|
|
376
|
-
mover_y = this.#pan_y.ok();
|
|
377
|
-
initial_x = e.offsetX;
|
|
378
|
-
initial_y = e.offsetY;
|
|
379
|
-
initial_id = e.pointerId;
|
|
380
|
-
} else if (count === 1) second_initial_id = e.pointerId;
|
|
381
|
-
count++;
|
|
382
|
-
}
|
|
383
|
-
});
|
|
384
|
-
this.onpointermove = (ev) => {
|
|
385
|
-
if (count === 0) return;
|
|
386
|
-
if (ev.pointerId === initial_id) this.#pan_coordinates(mover_x + (ev.offsetX - initial_x) / count, mover_y + (ev.offsetY - initial_y) / count);
|
|
387
|
-
else if (ev.pointerId === second_initial_id) {
|
|
388
|
-
if (count < 2) return;
|
|
389
|
-
this.#pan_coordinates(mover_x + (ev.offsetX - initial_x) / count, mover_y + (ev.offsetY - initial_y) / count);
|
|
390
|
-
}
|
|
391
|
-
};
|
|
392
|
-
this.onpointerup = (ev) => {
|
|
393
|
-
if (this.hasPointerCapture(ev.pointerId)) {
|
|
394
|
-
this.releasePointerCapture(ev.pointerId);
|
|
395
|
-
count--;
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
this.onpointercancel = this.onpointerup;
|
|
399
|
-
this.addEventListener("wheel", (e) => {
|
|
400
|
-
e.preventDefault();
|
|
401
|
-
e.stopPropagation();
|
|
402
|
-
const move_scale = e.shiftKey ? e.altKey ? .01 : .05 : e.altKey ? 2 : .5;
|
|
403
|
-
if (e.ctrlKey) this.#pan_coordinates(void 0, this.#pan_y.ok() - e.deltaY * move_scale);
|
|
404
|
-
else {
|
|
405
|
-
const scale_scale = e.shiftKey ? e.altKey ? 5e-5 : 1e-4 : e.altKey ? .005 : .001;
|
|
406
|
-
const scale = this.#zoom.ok() * (1 - e.deltaY * scale_scale);
|
|
407
|
-
this.#zoom_coordinates(scale, e.offsetX - this.#viewport_width / 2, e.offsetY - this.#viewport_height / 2);
|
|
408
|
-
}
|
|
409
|
-
this.#pan_coordinates(this.#pan_x.ok() - e.deltaX * move_scale, void 0);
|
|
410
|
-
}, { capture: true });
|
|
121
|
+
/**Returns the x position of the left edge of the widget */
|
|
122
|
+
get x() {
|
|
123
|
+
return this.#x;
|
|
411
124
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
}
|
|
417
|
-
if (y !== void 0) {
|
|
418
|
-
this.#panner.setAttribute("y", (this.#viewport_height_half + y).toFixed(0));
|
|
419
|
-
this.#pan_y.set_ok(y);
|
|
420
|
-
}
|
|
125
|
+
/**Sets the y position of the top edge of the widget */
|
|
126
|
+
set y(y) {
|
|
127
|
+
this.#y = y;
|
|
128
|
+
this.#update();
|
|
421
129
|
}
|
|
422
|
-
/**
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
scale = Math.max(.001, Math.min(1e4, scale));
|
|
426
|
-
const zoom_factor = scale / this.#zoom.ok();
|
|
427
|
-
this.#pan_coordinates(this.#pan_x.ok() * zoom_factor + x * (1 - zoom_factor), this.#pan_y.ok() * zoom_factor + y * (1 - zoom_factor));
|
|
428
|
-
this.#zoomer.style.scale = scale.toString();
|
|
429
|
-
this.#zoom.set_ok(scale);
|
|
430
|
-
}
|
|
431
|
-
#canvas;
|
|
432
|
-
#canvas_width;
|
|
433
|
-
#canvas_height;
|
|
434
|
-
#canvas_elements;
|
|
435
|
-
set canvas_width(value) {
|
|
436
|
-
this.#canvas.setAttribute("width", value.toString());
|
|
437
|
-
this.#canvas.setAttribute("viewBox", `0 0 ${value} ${this.#canvas_height}`);
|
|
438
|
-
this.#zoomer.setAttribute("width", value.toString());
|
|
439
|
-
this.#canvas_width = value;
|
|
440
|
-
}
|
|
441
|
-
get canvas_width() {
|
|
442
|
-
return this.#canvas_width;
|
|
443
|
-
}
|
|
444
|
-
set canvas_height(value) {
|
|
445
|
-
this.#canvas.setAttribute("height", value.toString());
|
|
446
|
-
this.#canvas.setAttribute("viewBox", `0 0 ${this.#canvas_width} ${value}`);
|
|
447
|
-
this.#zoomer.setAttribute("height", value.toString());
|
|
448
|
-
this.#canvas_height = value;
|
|
449
|
-
}
|
|
450
|
-
get canvas_height() {
|
|
451
|
-
return this.#canvas_height;
|
|
452
|
-
}
|
|
453
|
-
#pan_x = state$1.rosw(ok(0), (value) => {
|
|
454
|
-
return sync_resolve(ok(this.#pan_coordinates(value, void 0)));
|
|
455
|
-
});
|
|
456
|
-
pan_x = this.#pan_x.read_write;
|
|
457
|
-
#pan_y = state$1.rosw(ok(0), (value) => {
|
|
458
|
-
return sync_resolve(ok(this.#pan_coordinates(void 0, value)));
|
|
459
|
-
});
|
|
460
|
-
pan_y = this.#pan_y.read_write;
|
|
461
|
-
#zoom = state$1.rosw(ok(1), (value) => sync_resolve(ok(this.#zoom_coordinates(value, 0, 0))));
|
|
462
|
-
zoom = this.#zoom.read_write;
|
|
463
|
-
#grid_x = state$1.rosw(ok(10), (value) => {
|
|
464
|
-
this.#grid_x.set_ok(value);
|
|
465
|
-
return sync_resolve(ok(void 0));
|
|
466
|
-
});
|
|
467
|
-
grid_x = this.#grid_x.read_write;
|
|
468
|
-
#grid_y = state$1.rosw(ok(10), (value) => {
|
|
469
|
-
this.#grid_y.set_ok(value);
|
|
470
|
-
return sync_resolve(ok(void 0));
|
|
471
|
-
});
|
|
472
|
-
grid_y = this.#grid_y.read_write;
|
|
473
|
-
#grid_rotate = state$1.rosw(ok(15), (value) => {
|
|
474
|
-
this.#grid_rotate.set_ok(value);
|
|
475
|
-
return sync_resolve(ok(void 0));
|
|
476
|
-
});
|
|
477
|
-
grid_rotate = this.#grid_rotate.read_write;
|
|
478
|
-
#elements = [];
|
|
479
|
-
#state_sub;
|
|
480
|
-
set elements(elements) {
|
|
481
|
-
if (this.#state_sub) {
|
|
482
|
-
this.detach_state(this.#state_sub);
|
|
483
|
-
this.#state_sub = void 0;
|
|
484
|
-
}
|
|
485
|
-
if (state$1.is.state(elements)) this.#state_sub = this.attach_state(elements, (r) => this.#update_rows(r.ok ? r.value : []));
|
|
486
|
-
else this.#update_rows(elements);
|
|
130
|
+
/**Returns the y position of the top edge of the widget */
|
|
131
|
+
get y() {
|
|
132
|
+
return this.#y;
|
|
487
133
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (moved && !rows.includes(moved)) this.#detach_mover();
|
|
134
|
+
/**Sets the width of the widget */
|
|
135
|
+
set width(width) {
|
|
136
|
+
this.#width = width;
|
|
137
|
+
this.#update();
|
|
493
138
|
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
139
|
+
/**Returns the width of the widget */
|
|
140
|
+
get width() {
|
|
141
|
+
return this.#width;
|
|
142
|
+
}
|
|
143
|
+
/**Sets the height of the widget */
|
|
144
|
+
set height(height) {
|
|
145
|
+
this.#height = height;
|
|
146
|
+
this.#update();
|
|
497
147
|
}
|
|
498
|
-
|
|
499
|
-
|
|
148
|
+
/**Returns the height of the widget */
|
|
149
|
+
get height() {
|
|
150
|
+
return this.#height;
|
|
151
|
+
}
|
|
152
|
+
/**Sets the rotation of the widget in degrees, rotating clockwise around the center of the widget */
|
|
153
|
+
set rotation(rotation) {
|
|
154
|
+
this.#rotation = rotation;
|
|
155
|
+
this.#update();
|
|
156
|
+
}
|
|
157
|
+
/**Returns the rotation of the widget in degrees */
|
|
158
|
+
get rotation() {
|
|
159
|
+
return this.#rotation;
|
|
160
|
+
}
|
|
161
|
+
#update() {
|
|
162
|
+
const width = this.#width;
|
|
163
|
+
const height = this.#height;
|
|
164
|
+
const layout_width = this.original_width;
|
|
165
|
+
const layout_height = this.original_height;
|
|
166
|
+
const scale_x = layout_width > 0 ? width / layout_width : 1;
|
|
167
|
+
const scale_y = layout_height > 0 ? height / layout_height : 1;
|
|
168
|
+
this.style.left = this.#x + "px";
|
|
169
|
+
this.style.top = this.#y + "px";
|
|
170
|
+
this.style.width = layout_width + "px";
|
|
171
|
+
this.style.height = layout_height + "px";
|
|
172
|
+
this.style.transform = `translate(${width / 2}px, ${height / 2}px) rotate(${this.#rotation}deg) translate(${-width / 2}px, ${-height / 2}px) scale(${scale_x}, ${scale_y})`;
|
|
173
|
+
this.dispatchEvent(new Event(WIDGET_TRANSFORM_EVENT));
|
|
500
174
|
}
|
|
501
175
|
};
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
176
|
+
// src/canvas/transformer.ts
|
|
177
|
+
/**Smallest width and height a widget can be resized to */
|
|
178
|
+
const MIN_SIZE = 1;
|
|
179
|
+
/**Grid positions and sizes snap to, and the finer grid used while alt is held */
|
|
180
|
+
const SNAP_GRID = 5;
|
|
181
|
+
const SNAP_GRID_FINE = 1;
|
|
182
|
+
/**Angle in degrees rotation snaps to, and the finer angle used while alt is held */
|
|
183
|
+
const SNAP_ANGLE = 15;
|
|
184
|
+
const SNAP_ANGLE_FINE = 5;
|
|
185
|
+
/**Rounds the value to the nearest multiple of step, a step of 0 returns the value unchanged */
|
|
186
|
+
function snap(value, step) {
|
|
187
|
+
if (step <= 0) return value;
|
|
188
|
+
return Math.round(value / step) * step;
|
|
505
189
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
190
|
+
/**Resize handles, x and y is the edge they move, -1 is the left or top edge, 1 the right or bottom edge, 0 none */
|
|
191
|
+
const RESIZE_HANDLES = [
|
|
192
|
+
{
|
|
193
|
+
name: "nw",
|
|
194
|
+
x: -1,
|
|
195
|
+
y: -1
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
name: "n",
|
|
199
|
+
x: 0,
|
|
200
|
+
y: -1
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
name: "ne",
|
|
204
|
+
x: 1,
|
|
205
|
+
y: -1
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "e",
|
|
209
|
+
x: 1,
|
|
210
|
+
y: 0
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
name: "se",
|
|
214
|
+
x: 1,
|
|
215
|
+
y: 1
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
name: "s",
|
|
219
|
+
x: 0,
|
|
220
|
+
y: 1
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
name: "sw",
|
|
224
|
+
x: -1,
|
|
225
|
+
y: 1
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
name: "w",
|
|
229
|
+
x: -1,
|
|
230
|
+
y: 0
|
|
231
|
+
}
|
|
232
|
+
];
|
|
233
|
+
/**Dashed outline with handles, attached to a widget to move, resize and rotate it
|
|
234
|
+
* Corner handles resize both axes and side handles one axis, keeping the opposite corner or side in place
|
|
235
|
+
* The handle above the outline rotates the widget around its center
|
|
236
|
+
* The center handle and the outline itself move the widget, the inside of the outline is left to the widgets below
|
|
237
|
+
*
|
|
238
|
+
* Position and size snap to a 5px grid and rotation to 15 degrees
|
|
239
|
+
* Holding alt snaps to a 1px grid and 5 degrees instead, holding shift disables snapping
|
|
240
|
+
*
|
|
241
|
+
* Resize and rotate cursors follow the rotation of the widget in 45 degree steps
|
|
242
|
+
*
|
|
243
|
+
* The transformer is drawn in viewport pixels, so it keeps its size when the page is zoomed*/
|
|
244
|
+
var Transformer = class extends Base {
|
|
245
|
+
/**Returns the name used to define the element */
|
|
246
|
+
static element_name() {
|
|
247
|
+
return "transformer";
|
|
248
|
+
}
|
|
249
|
+
/**Returns the namespace override for the element*/
|
|
250
|
+
static element_name_space() {
|
|
251
|
+
return "canvas";
|
|
252
|
+
}
|
|
253
|
+
#widget;
|
|
254
|
+
#offset_x = 0;
|
|
255
|
+
#offset_y = 0;
|
|
256
|
+
#scale = 1;
|
|
257
|
+
#actions = /* @__PURE__ */ new Map();
|
|
258
|
+
#drag;
|
|
521
259
|
constructor() {
|
|
522
|
-
|
|
260
|
+
super();
|
|
261
|
+
this.hidden = true;
|
|
262
|
+
for (const side of [
|
|
263
|
+
"top",
|
|
264
|
+
"right",
|
|
265
|
+
"bottom",
|
|
266
|
+
"left"
|
|
267
|
+
]) this.#add("side " + side, { type: "move" });
|
|
268
|
+
this.#add("line", void 0);
|
|
269
|
+
for (const handle of RESIZE_HANDLES) this.#add("handle " + handle.name, {
|
|
270
|
+
type: "resize",
|
|
271
|
+
x: handle.x,
|
|
272
|
+
y: handle.y
|
|
273
|
+
}, material_arrow_range_rounded);
|
|
274
|
+
this.#add("handle rotate", { type: "rotate" }, material_rotate_right_rounded);
|
|
275
|
+
this.#add("handle move", { type: "move" }, material_open_with_rounded);
|
|
276
|
+
this.addEventListener("pointerdown", this.#on_pointer_down);
|
|
277
|
+
}
|
|
278
|
+
disconnectedCallback() {
|
|
279
|
+
super.disconnectedCallback();
|
|
280
|
+
this.#stop_drag();
|
|
281
|
+
}
|
|
282
|
+
/**Sets the widget the transformer is attached to, undefined hides the transformer */
|
|
283
|
+
set widget(widget) {
|
|
284
|
+
if (this.#widget === widget) return;
|
|
285
|
+
this.#stop_drag();
|
|
286
|
+
this.#widget?.removeEventListener(WIDGET_TRANSFORM_EVENT, this.#on_widget_transform);
|
|
287
|
+
this.#widget = widget;
|
|
288
|
+
widget?.addEventListener(WIDGET_TRANSFORM_EVENT, this.#on_widget_transform);
|
|
289
|
+
this.#update();
|
|
290
|
+
}
|
|
291
|
+
/**Returns the widget the transformer is attached to */
|
|
292
|
+
get widget() {
|
|
293
|
+
return this.#widget;
|
|
294
|
+
}
|
|
295
|
+
/**Returns whether a handle is being dragged */
|
|
296
|
+
get dragging() {
|
|
297
|
+
return this.#drag !== void 0;
|
|
298
|
+
}
|
|
299
|
+
/**Sets how page units map to viewport pixels, viewport = offset + page * scale
|
|
300
|
+
* During a drag the widget is moved so the grabbed point stays under the pointer */
|
|
301
|
+
view(offset_x, offset_y, scale) {
|
|
302
|
+
this.#offset_x = offset_x;
|
|
303
|
+
this.#offset_y = offset_y;
|
|
304
|
+
this.#scale = scale;
|
|
305
|
+
this.#update();
|
|
306
|
+
if (this.#drag?.last) this.#apply_drag(this.#drag, this.#drag.last);
|
|
307
|
+
}
|
|
308
|
+
#add(class_name, action, icon) {
|
|
309
|
+
const element = this.appendChild(document.createElement("div"));
|
|
310
|
+
element.className = class_name;
|
|
311
|
+
if (icon) element.appendChild(icon());
|
|
312
|
+
if (action) this.#actions.set(element, action);
|
|
313
|
+
}
|
|
314
|
+
#update() {
|
|
315
|
+
const widget = this.#widget;
|
|
316
|
+
this.hidden = !widget;
|
|
317
|
+
if (!widget) return;
|
|
318
|
+
const scale = this.#scale;
|
|
319
|
+
this.style.left = this.#offset_x + widget.x * scale + "px";
|
|
320
|
+
this.style.top = this.#offset_y + widget.y * scale + "px";
|
|
321
|
+
this.style.width = widget.width * scale + "px";
|
|
322
|
+
this.style.height = widget.height * scale + "px";
|
|
323
|
+
this.style.transform = `rotate(${widget.rotation}deg)`;
|
|
324
|
+
const half_turn = (widget.rotation % 180 + 180) % 180;
|
|
325
|
+
this.setAttribute("cursor-step", String(Math.round(half_turn / 45) % 4));
|
|
326
|
+
}
|
|
327
|
+
#start(action, e) {
|
|
328
|
+
const widget = this.#widget;
|
|
329
|
+
if (!widget || this.#drag) return;
|
|
330
|
+
e.preventDefault();
|
|
331
|
+
e.stopPropagation();
|
|
332
|
+
const rect = this.getBoundingClientRect();
|
|
333
|
+
const center_x = rect.left + rect.width / 2;
|
|
334
|
+
const center_y = rect.top + rect.height / 2;
|
|
335
|
+
const scale = this.#scale;
|
|
336
|
+
const origin_x = center_x - this.#offset_x - (widget.x + widget.width / 2) * scale;
|
|
337
|
+
const origin_y = center_y - this.#offset_y - (widget.y + widget.height / 2) * scale;
|
|
338
|
+
this.#drag = {
|
|
339
|
+
action,
|
|
340
|
+
pointer_id: e.pointerId,
|
|
341
|
+
origin_x,
|
|
342
|
+
origin_y,
|
|
343
|
+
page_x: (e.clientX - origin_x - this.#offset_x) / scale,
|
|
344
|
+
page_y: (e.clientY - origin_y - this.#offset_y) / scale,
|
|
345
|
+
angle: Math.atan2(e.clientY - center_y, e.clientX - center_x),
|
|
346
|
+
last: void 0,
|
|
347
|
+
x: widget.x,
|
|
348
|
+
y: widget.y,
|
|
349
|
+
width: widget.width,
|
|
350
|
+
height: widget.height,
|
|
351
|
+
rotation: widget.rotation
|
|
352
|
+
};
|
|
353
|
+
window.addEventListener("pointermove", this.#on_pointer_move);
|
|
354
|
+
window.addEventListener("pointerup", this.#on_pointer_up);
|
|
355
|
+
window.addEventListener("pointercancel", this.#on_pointer_up);
|
|
356
|
+
}
|
|
357
|
+
#stop_drag() {
|
|
358
|
+
if (!this.#drag) return;
|
|
359
|
+
this.#drag = void 0;
|
|
360
|
+
window.removeEventListener("pointermove", this.#on_pointer_move);
|
|
361
|
+
window.removeEventListener("pointerup", this.#on_pointer_up);
|
|
362
|
+
window.removeEventListener("pointercancel", this.#on_pointer_up);
|
|
363
|
+
}
|
|
364
|
+
#on_widget_transform = () => {
|
|
365
|
+
this.#update();
|
|
366
|
+
};
|
|
367
|
+
#on_pointer_down = (e) => {
|
|
368
|
+
const action = e.target instanceof Element ? this.#actions.get(e.target) : void 0;
|
|
369
|
+
if (!action || e.button !== 0) return;
|
|
370
|
+
this.#start(action, e);
|
|
371
|
+
};
|
|
372
|
+
#on_pointer_move = (e) => {
|
|
373
|
+
const drag = this.#drag;
|
|
374
|
+
if (!drag || e.pointerId !== drag.pointer_id) return;
|
|
375
|
+
drag.last = e;
|
|
376
|
+
this.#apply_drag(drag, e);
|
|
377
|
+
};
|
|
378
|
+
#on_pointer_up = (e) => {
|
|
379
|
+
if (e.pointerId === this.#drag?.pointer_id) this.#stop_drag();
|
|
380
|
+
};
|
|
381
|
+
/**Updates the widget from the pointer event and the current view */
|
|
382
|
+
#apply_drag(drag, e) {
|
|
383
|
+
const widget = this.#widget;
|
|
384
|
+
const scale = this.#scale;
|
|
385
|
+
if (!widget || scale <= 0) return;
|
|
386
|
+
const view_x = drag.origin_x + this.#offset_x;
|
|
387
|
+
const view_y = drag.origin_y + this.#offset_y;
|
|
388
|
+
const delta_x = (e.clientX - view_x) / scale - drag.page_x;
|
|
389
|
+
const delta_y = (e.clientY - view_y) / scale - drag.page_y;
|
|
390
|
+
const grid = e.shiftKey ? 0 : e.altKey ? SNAP_GRID_FINE : SNAP_GRID;
|
|
391
|
+
const action = drag.action;
|
|
392
|
+
switch (action.type) {
|
|
393
|
+
case "move":
|
|
394
|
+
widget.x = snap(drag.x + delta_x, grid);
|
|
395
|
+
widget.y = snap(drag.y + delta_y, grid);
|
|
396
|
+
break;
|
|
397
|
+
case "resize": {
|
|
398
|
+
const radians = drag.rotation * Math.PI / 180;
|
|
399
|
+
const cos = Math.cos(radians);
|
|
400
|
+
const sin = Math.sin(radians);
|
|
401
|
+
const local_x = delta_x * cos + delta_y * sin;
|
|
402
|
+
const local_y = delta_y * cos - delta_x * sin;
|
|
403
|
+
const width = action.x ? Math.max(MIN_SIZE, snap(drag.width + action.x * local_x, grid)) : drag.width;
|
|
404
|
+
const height = action.y ? Math.max(MIN_SIZE, snap(drag.height + action.y * local_y, grid)) : drag.height;
|
|
405
|
+
const shift_x = action.x * (width - drag.width) / 2;
|
|
406
|
+
const shift_y = action.y * (height - drag.height) / 2;
|
|
407
|
+
const center_x = drag.x + drag.width / 2 + shift_x * cos - shift_y * sin;
|
|
408
|
+
const center_y = drag.y + drag.height / 2 + shift_x * sin + shift_y * cos;
|
|
409
|
+
widget.x = center_x - width / 2;
|
|
410
|
+
widget.y = center_y - height / 2;
|
|
411
|
+
widget.width = width;
|
|
412
|
+
widget.height = height;
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
case "rotate": {
|
|
416
|
+
const now = Math.atan2(e.clientY - (view_y + (drag.y + drag.height / 2) * scale), e.clientX - (view_x + (drag.x + drag.width / 2) * scale));
|
|
417
|
+
widget.rotation = (snap(drag.rotation + (now - drag.angle) * 180 / Math.PI, e.shiftKey ? 0 : e.altKey ? SNAP_ANGLE_FINE : SNAP_ANGLE) % 360 + 360) % 360;
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
523
421
|
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
422
|
+
};
|
|
423
|
+
define_element(Transformer);
|
|
424
|
+
// src/canvas/viewport.ts
|
|
425
|
+
const ZOOM_MIN = .1;
|
|
426
|
+
const ZOOM_MAX = 20;
|
|
427
|
+
/**Zoom factor per wheel pixel */
|
|
428
|
+
const WHEEL_ZOOM_SPEED = .002;
|
|
429
|
+
/**Zoom factor per wheel pixel for touchpad pinch, which reports much smaller deltas than a mouse wheel */
|
|
430
|
+
const PINCH_ZOOM_SPEED = .01;
|
|
431
|
+
/**Screen pixels panned per wheel pixel */
|
|
432
|
+
const WHEEL_PAN_SPEED = .5;
|
|
433
|
+
/**Pixels per line, for wheel events reported in lines */
|
|
434
|
+
const WHEEL_LINE_HEIGHT = 16;
|
|
435
|
+
/**Speed multiplier for wheel zoom and pan while alt is held */
|
|
436
|
+
const WHEEL_SLOW = .1;
|
|
437
|
+
/**Viewport showing a page of widgets
|
|
438
|
+
* The page is scaled to fit inside the viewport element while keeping its aspect ratio, and centered
|
|
439
|
+
*
|
|
440
|
+
* When edit is true the page can be zoomed and panned, and a checkerboard is shown behind it
|
|
441
|
+
* Wheel and touchpad two finger swipe pans, ctrl + wheel or swipe zooms toward the cursor
|
|
442
|
+
* Shift + vertical wheel pans horizontally
|
|
443
|
+
* Touchpad pinch zooms toward the cursor
|
|
444
|
+
* Holding alt makes wheel zoom and pan 10 times slower
|
|
445
|
+
* Two finger touch pinch zooms and pans, middle or right mouse drag pans
|
|
446
|
+
*
|
|
447
|
+
* Pressing a widget in edit mode selects it and attaches a transformer to move, resize and rotate it
|
|
448
|
+
* Pressing outside any widget clears the selection*/
|
|
449
|
+
var Viewport = class extends Base {
|
|
450
|
+
/**Returns the name used to define the element */
|
|
451
|
+
static element_name() {
|
|
452
|
+
return "viewport";
|
|
530
453
|
}
|
|
531
|
-
|
|
532
|
-
|
|
454
|
+
/**Returns the namespace override for the element*/
|
|
455
|
+
static element_name_space() {
|
|
456
|
+
return "canvas";
|
|
457
|
+
}
|
|
458
|
+
#background = this.appendChild(document.createElement("div"));
|
|
459
|
+
#container = this.appendChild(document.createElement("div"));
|
|
460
|
+
#transformer = this.appendChild(new Transformer());
|
|
461
|
+
#page;
|
|
462
|
+
#page_observer = new ResizeObserver(() => {
|
|
463
|
+
this.#update_view();
|
|
464
|
+
});
|
|
465
|
+
#scale = 1;
|
|
466
|
+
#fit_x = 0;
|
|
467
|
+
#fit_y = 0;
|
|
468
|
+
#edit = false;
|
|
469
|
+
#zoom = 1;
|
|
470
|
+
#pan_x = 0;
|
|
471
|
+
#pan_y = 0;
|
|
472
|
+
#touches = /* @__PURE__ */ new Map();
|
|
473
|
+
#pan_pointer_id;
|
|
474
|
+
#pan_pointer_last = {
|
|
475
|
+
x: 0,
|
|
476
|
+
y: 0
|
|
477
|
+
};
|
|
478
|
+
/**Whether the ctrl key is physically held, to tell touchpad pinch from ctrl + wheel */
|
|
479
|
+
#ctrl_held = false;
|
|
480
|
+
constructor() {
|
|
481
|
+
super();
|
|
482
|
+
this.#background.className = "checkerboard";
|
|
483
|
+
this.#container.className = "container";
|
|
484
|
+
this.attach_to_resize_observer();
|
|
485
|
+
}
|
|
486
|
+
/**Sets the page shown in the viewport */
|
|
487
|
+
set page(page) {
|
|
488
|
+
if (this.#page === page) return;
|
|
489
|
+
this.selected = void 0;
|
|
490
|
+
if (this.#page) {
|
|
491
|
+
this.#page_observer.unobserve(this.#page);
|
|
492
|
+
if (this.#page.parentElement === this.#container) this.#page.remove();
|
|
493
|
+
}
|
|
494
|
+
this.#page = page;
|
|
495
|
+
if (page) {
|
|
496
|
+
this.#container.appendChild(page);
|
|
497
|
+
this.#page_observer.observe(page);
|
|
498
|
+
}
|
|
499
|
+
this.#update_view();
|
|
500
|
+
}
|
|
501
|
+
/**Returns the page shown in the viewport */
|
|
502
|
+
get page() {
|
|
503
|
+
return this.#page;
|
|
504
|
+
}
|
|
505
|
+
/**Sets edit mode, which enables zooming and panning and shows a checkerboard background
|
|
506
|
+
* Leaving edit mode resets the view to fit the page */
|
|
507
|
+
set edit(edit) {
|
|
508
|
+
if (this.#edit === edit) return;
|
|
509
|
+
this.#edit = edit;
|
|
510
|
+
this.toggleAttribute("edit", edit);
|
|
511
|
+
this.#listen_keys(edit && this.isConnected);
|
|
512
|
+
if (edit) {
|
|
513
|
+
this.addEventListener("wheel", this.#on_wheel, { passive: false });
|
|
514
|
+
this.addEventListener("pointerdown", this.#on_pointer_down);
|
|
515
|
+
this.addEventListener("pointermove", this.#on_pointer_move);
|
|
516
|
+
this.addEventListener("pointerup", this.#on_pointer_up);
|
|
517
|
+
this.addEventListener("pointercancel", this.#on_pointer_up);
|
|
518
|
+
this.addEventListener("mousedown", this.#on_mouse_down);
|
|
519
|
+
this.addEventListener("contextmenu", this.#on_context_menu);
|
|
520
|
+
} else {
|
|
521
|
+
this.removeEventListener("wheel", this.#on_wheel);
|
|
522
|
+
this.removeEventListener("pointerdown", this.#on_pointer_down);
|
|
523
|
+
this.removeEventListener("pointermove", this.#on_pointer_move);
|
|
524
|
+
this.removeEventListener("pointerup", this.#on_pointer_up);
|
|
525
|
+
this.removeEventListener("pointercancel", this.#on_pointer_up);
|
|
526
|
+
this.removeEventListener("mousedown", this.#on_mouse_down);
|
|
527
|
+
this.removeEventListener("contextmenu", this.#on_context_menu);
|
|
528
|
+
this.#touches.clear();
|
|
529
|
+
this.#pan_pointer_id = void 0;
|
|
530
|
+
this.selected = void 0;
|
|
531
|
+
this.#zoom = 1;
|
|
532
|
+
this.#pan_x = 0;
|
|
533
|
+
this.#pan_y = 0;
|
|
534
|
+
this.#update_view();
|
|
535
|
+
}
|
|
533
536
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
this
|
|
537
|
-
this.#update_transform();
|
|
537
|
+
/**Returns whether edit mode is enabled */
|
|
538
|
+
get edit() {
|
|
539
|
+
return this.#edit;
|
|
538
540
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
+
/**Sets the selected widget, which the transformer is attached to, selection is cleared when leaving edit mode */
|
|
542
|
+
set selected(widget) {
|
|
543
|
+
this.#transformer.widget = widget;
|
|
541
544
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
set rotation(value) {
|
|
546
|
-
this.#rotation = value;
|
|
547
|
-
this.#update_transform();
|
|
545
|
+
/**Returns the selected widget */
|
|
546
|
+
get selected() {
|
|
547
|
+
return this.#transformer.widget;
|
|
548
548
|
}
|
|
549
|
-
|
|
550
|
-
|
|
549
|
+
/**Returns the current scale from page units to screen pixels */
|
|
550
|
+
get scale() {
|
|
551
|
+
return this.#scale;
|
|
551
552
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
set rotation_center_x(value) {
|
|
555
|
-
this.#rotation_center_x = value;
|
|
556
|
-
this.#update_transform();
|
|
557
|
-
}
|
|
558
|
-
get rotation_center_x() {
|
|
559
|
-
return this.#rotation_center_x;
|
|
560
|
-
}
|
|
561
|
-
/**Rotation center y coordinate, relative to the element's display area.
|
|
562
|
-
* Defaults to half of default_height (center of element). */
|
|
563
|
-
set rotation_center_y(value) {
|
|
564
|
-
this.#rotation_center_y = value;
|
|
565
|
-
this.#update_transform();
|
|
566
|
-
}
|
|
567
|
-
get rotation_center_y() {
|
|
568
|
-
return this.#rotation_center_y;
|
|
569
|
-
}
|
|
570
|
-
#update_transform() {
|
|
571
|
-
if (this.#rotation === 0) this.canvas.removeAttribute("transform");
|
|
572
|
-
else {
|
|
573
|
-
const cx = this.#position_x + this.#rotation_center_x;
|
|
574
|
-
const cy = this.#position_y + this.#rotation_center_y;
|
|
575
|
-
this.canvas.setAttribute("transform", `rotate(${this.#rotation} ${cx} ${cy})`);
|
|
576
|
-
}
|
|
553
|
+
on_resize() {
|
|
554
|
+
this.#update_view();
|
|
577
555
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
* - If the element has an optimal_ratio, the viewBox stays at default
|
|
582
|
-
* dimensions and the SVG content is stretched/scaled to fill the new size.
|
|
583
|
-
* - If the element has no optimal_ratio, the viewBox is updated to match
|
|
584
|
-
* the new dimensions and on_resize is called. */
|
|
585
|
-
set width(value) {
|
|
586
|
-
this.#width = value;
|
|
587
|
-
this.#apply_size();
|
|
556
|
+
connectedCallback() {
|
|
557
|
+
super.connectedCallback();
|
|
558
|
+
if (this.#edit) this.#listen_keys(true);
|
|
588
559
|
}
|
|
589
|
-
|
|
590
|
-
|
|
560
|
+
disconnectedCallback() {
|
|
561
|
+
super.disconnectedCallback();
|
|
562
|
+
this.#listen_keys(false);
|
|
563
|
+
}
|
|
564
|
+
/**Adds or removes the window listeners tracking the ctrl key */
|
|
565
|
+
#listen_keys(listen) {
|
|
566
|
+
if (listen) {
|
|
567
|
+
window.addEventListener("keydown", this.#on_key);
|
|
568
|
+
window.addEventListener("keyup", this.#on_key);
|
|
569
|
+
window.addEventListener("blur", this.#on_blur);
|
|
570
|
+
} else {
|
|
571
|
+
window.removeEventListener("keydown", this.#on_key);
|
|
572
|
+
window.removeEventListener("keyup", this.#on_key);
|
|
573
|
+
window.removeEventListener("blur", this.#on_blur);
|
|
574
|
+
this.#ctrl_held = false;
|
|
575
|
+
}
|
|
591
576
|
}
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
this.#
|
|
577
|
+
#update_view() {
|
|
578
|
+
if (!this.#page) return;
|
|
579
|
+
const width = this.#page.width;
|
|
580
|
+
const height = this.#page.height;
|
|
581
|
+
if (width <= 0 || height <= 0) return;
|
|
582
|
+
const rect = this.actual_size;
|
|
583
|
+
const fit = Math.min(rect.width / width, rect.height / height);
|
|
584
|
+
this.#fit_x = (rect.width - width * fit) / 2;
|
|
585
|
+
this.#fit_y = (rect.height - height * fit) / 2;
|
|
586
|
+
this.#scale = fit * this.#zoom;
|
|
587
|
+
this.#container.style.transform = `translate(${this.#fit_x + this.#pan_x}px, ${this.#fit_y + this.#pan_y}px) scale(${this.#scale})`;
|
|
588
|
+
this.#transformer.view(this.#fit_x + this.#pan_x, this.#fit_y + this.#pan_y, this.#scale);
|
|
589
|
+
}
|
|
590
|
+
/**Zooms by the factor, keeping the given point in viewport coordinates in place */
|
|
591
|
+
#zoom_at(x, y, factor) {
|
|
592
|
+
const zoom = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, this.#zoom * factor));
|
|
593
|
+
const ratio = zoom / this.#zoom;
|
|
594
|
+
this.#pan_x += (x - this.#fit_x - this.#pan_x) * (1 - ratio);
|
|
595
|
+
this.#pan_y += (y - this.#fit_y - this.#pan_y) * (1 - ratio);
|
|
596
|
+
this.#zoom = zoom;
|
|
597
|
+
}
|
|
598
|
+
/**Returns the position of the event in viewport coordinates */
|
|
599
|
+
#local(e) {
|
|
600
|
+
const rect = this.getBoundingClientRect();
|
|
601
|
+
return {
|
|
602
|
+
x: e.clientX - rect.left,
|
|
603
|
+
y: e.clientY - rect.top
|
|
604
|
+
};
|
|
600
605
|
}
|
|
601
|
-
|
|
602
|
-
|
|
606
|
+
/**Selects the widget under the pointer, or clears the selection if there is none */
|
|
607
|
+
#select_at(e) {
|
|
608
|
+
const page = this.#page;
|
|
609
|
+
let element = e.target instanceof Element ? e.target : null;
|
|
610
|
+
while (element && element.parentElement !== page) element = element.parentElement;
|
|
611
|
+
this.selected = page?.widgets.find((widget) => widget === element);
|
|
612
|
+
}
|
|
613
|
+
/**Returns center and distance of the first two touches */
|
|
614
|
+
#pinch() {
|
|
615
|
+
if (this.#touches.size < 2) return void 0;
|
|
616
|
+
const [a, b] = this.#touches.values();
|
|
617
|
+
return {
|
|
618
|
+
x: (a.x + b.x) / 2,
|
|
619
|
+
y: (a.y + b.y) / 2,
|
|
620
|
+
distance: Math.hypot(a.x - b.x, a.y - b.y)
|
|
621
|
+
};
|
|
603
622
|
}
|
|
604
|
-
#
|
|
605
|
-
|
|
606
|
-
this.
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
623
|
+
#on_wheel = (e) => {
|
|
624
|
+
e.preventDefault();
|
|
625
|
+
const unit = e.deltaMode === WheelEvent.DOM_DELTA_LINE ? WHEEL_LINE_HEIGHT : e.deltaMode === WheelEvent.DOM_DELTA_PAGE ? this.actual_size.height : 1;
|
|
626
|
+
const delta_x = e.deltaX * unit;
|
|
627
|
+
const delta_y = e.deltaY * unit;
|
|
628
|
+
const speed = e.altKey ? WHEEL_SLOW : 1;
|
|
629
|
+
if (e.ctrlKey) {
|
|
630
|
+
const zoom_speed = this.#ctrl_held ? WHEEL_ZOOM_SPEED : PINCH_ZOOM_SPEED;
|
|
631
|
+
const point = this.#local(e);
|
|
632
|
+
this.#zoom_at(point.x, point.y, Math.exp(-delta_y * zoom_speed * speed));
|
|
633
|
+
} else {
|
|
634
|
+
const swap = e.shiftKey && delta_x === 0;
|
|
635
|
+
this.#pan_x -= (swap ? delta_y : delta_x) * speed * WHEEL_PAN_SPEED;
|
|
636
|
+
this.#pan_y -= (swap ? 0 : delta_y) * speed * WHEEL_PAN_SPEED;
|
|
610
637
|
}
|
|
611
|
-
|
|
638
|
+
this.#update_view();
|
|
639
|
+
};
|
|
640
|
+
#on_pointer_down = (e) => {
|
|
641
|
+
if (e.pointerType === "touch") {
|
|
642
|
+
if (this.#touches.size === 0 && !this.#transformer.dragging) this.#select_at(e);
|
|
643
|
+
this.#touches.set(e.pointerId, this.#local(e));
|
|
644
|
+
this.setPointerCapture(e.pointerId);
|
|
645
|
+
} else if (e.button === 0) this.#select_at(e);
|
|
646
|
+
else if (e.button === 1 || e.button === 2) {
|
|
647
|
+
e.preventDefault();
|
|
648
|
+
this.#pan_pointer_id = e.pointerId;
|
|
649
|
+
this.#pan_pointer_last = this.#local(e);
|
|
650
|
+
this.setPointerCapture(e.pointerId);
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
#on_pointer_move = (e) => {
|
|
654
|
+
this.#ctrl_held = e.ctrlKey;
|
|
655
|
+
if (this.#touches.has(e.pointerId)) {
|
|
656
|
+
const before = this.#pinch();
|
|
657
|
+
this.#touches.set(e.pointerId, this.#local(e));
|
|
658
|
+
const after = this.#pinch();
|
|
659
|
+
if (!before || !after || before.distance <= 0) return;
|
|
660
|
+
this.#pan_x += after.x - before.x;
|
|
661
|
+
this.#pan_y += after.y - before.y;
|
|
662
|
+
this.#zoom_at(after.x, after.y, after.distance / before.distance);
|
|
663
|
+
this.#update_view();
|
|
664
|
+
} else if (e.pointerId === this.#pan_pointer_id) {
|
|
665
|
+
const point = this.#local(e);
|
|
666
|
+
this.#pan_x += point.x - this.#pan_pointer_last.x;
|
|
667
|
+
this.#pan_y += point.y - this.#pan_pointer_last.y;
|
|
668
|
+
this.#pan_pointer_last = point;
|
|
669
|
+
this.#update_view();
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
#on_pointer_up = (e) => {
|
|
673
|
+
this.#touches.delete(e.pointerId);
|
|
674
|
+
if (e.pointerId === this.#pan_pointer_id) this.#pan_pointer_id = void 0;
|
|
675
|
+
};
|
|
676
|
+
/**Prevents the browser autoscroll on middle mouse button */
|
|
677
|
+
#on_mouse_down = (e) => {
|
|
678
|
+
if (e.button === 1) e.preventDefault();
|
|
679
|
+
};
|
|
680
|
+
/**Prevents the context menu, as the right mouse button pans in edit mode */
|
|
681
|
+
#on_context_menu = (e) => {
|
|
682
|
+
e.preventDefault();
|
|
683
|
+
};
|
|
684
|
+
#on_key = (e) => {
|
|
685
|
+
this.#ctrl_held = e.ctrlKey;
|
|
686
|
+
};
|
|
687
|
+
#on_blur = () => {
|
|
688
|
+
this.#ctrl_held = false;
|
|
689
|
+
};
|
|
612
690
|
};
|
|
691
|
+
define_element(Viewport);
|
|
692
|
+
/**Creates a viewport showing a page of widgets */
|
|
693
|
+
const canvas_viewport = (options) => options ? new Viewport().opts(options) : new Viewport();
|
|
613
694
|
// src/canvas/index.ts
|
|
614
|
-
|
|
615
|
-
|
|
695
|
+
/**Displaying and editing graphics with a fixed position and size */
|
|
696
|
+
const canvas = {
|
|
697
|
+
page: canvas_page,
|
|
698
|
+
viewport: canvas_viewport,
|
|
699
|
+
WIDGET_TRANSFORM_EVENT
|
|
700
|
+
};
|
|
701
|
+
export { WIDGET_TRANSFORM_EVENT, WidgetBase, canvas, canvas as default };
|
|
616
702
|
|
|
617
703
|
//# sourceMappingURL=index.mjs.map
|