@chocbite/ts-lib-ui 1.0.2 → 1.0.4
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/README.md +16 -1
- package/base/index.d.mts +2 -2
- package/base/index.mjs +69 -77
- package/base/index.mjs.map +1 -1
- package/canvas/index.d.mts +117 -60
- package/canvas/index.mjs +652 -584
- package/canvas/index.mjs.map +1 -1
- package/context-menu/index.d.mts +1 -1
- package/context-menu/index.mjs +1 -1
- package/{context-menu-CiNcuP-a.mjs → context-menu-Cz9rqUaM.mjs} +71 -37
- package/context-menu-Cz9rqUaM.mjs.map +1 -0
- package/document/index.mjs +3 -1
- package/document/index.mjs.map +1 -1
- package/form/index.d.mts +16 -5
- package/form/index.mjs +412 -153
- package/form/index.mjs.map +1 -1
- package/icons/index.d.mts +1 -1
- package/icons/index.mjs +1 -1
- package/icons-e2J_Ojic.mjs +4124 -0
- package/{index-BcidwqIy.d.mts → index-BDWg5GX2.d.mts} +7 -18
- package/{index-BND79jZp.d.mts → index-CvzH3M_5.d.mts} +16 -9
- package/{index-CugtUzv_.d.mts → index-DCn7acFY.d.mts} +30 -12
- package/{index-CB3-qP6l.d.mts → index-DhRb2iA8.d.mts} +7 -5
- package/keyboard/index.d.mts +1 -1
- package/keyboard/index.mjs +121 -50
- package/keyboard/index.mjs.map +1 -1
- package/list/index.d.mts +1 -1
- package/list/index.mjs +1 -1
- package/{list-CQDnxsLV.mjs → list-DXkEJKjx.mjs} +218 -117
- package/list-DXkEJKjx.mjs.map +1 -0
- package/package.json +7 -7
- package/selection/index.mjs +9 -11
- package/selection/index.mjs.map +1 -1
- package/settings/index.d.mts +23 -5
- package/settings/index.mjs +17 -10
- package/settings/index.mjs.map +1 -1
- package/spinner/index.d.mts +1 -1
- package/spinner/index.mjs +2 -2
- package/state/index.d.mts +36 -0
- package/state/index.mjs +2 -0
- package/state-OCzEmq3j.mjs +87 -0
- package/state-OCzEmq3j.mjs.map +1 -0
- package/style.css +423 -142
- package/svg/index.d.mts +1 -1
- package/svg/index.mjs +1 -1
- package/{svg-golJPaR4.mjs → svg-1bp-yIPC.mjs} +20 -17
- package/svg-1bp-yIPC.mjs.map +1 -0
- package/theme/index.d.mts +21 -2
- package/theme/index.mjs +1 -1
- package/{theme-9ib89_3b.mjs → theme-CFeGnUuF.mjs} +125 -44
- package/theme-CFeGnUuF.mjs.map +1 -0
- package/context-menu-CiNcuP-a.mjs.map +0 -1
- package/icons-CY4BLQ2F.mjs +0 -4123
- package/list-CQDnxsLV.mjs.map +0 -1
- package/svg-golJPaR4.mjs.map +0 -1
- package/theme-9ib89_3b.mjs.map +0 -1
- package/viewport/index.d.mts +0 -1
- package/viewport/index.mjs +0 -2
package/canvas/index.mjs
CHANGED
|
@@ -1,619 +1,687 @@
|
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
this
|
|
66
|
-
this
|
|
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})`);
|
|
3
|
+
import { i as theme_init_variable_root } from "../theme-CFeGnUuF.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";
|
|
28
|
+
}
|
|
29
|
+
/**Returns the namespace override for the element*/
|
|
30
|
+
static element_name_space() {
|
|
31
|
+
return "canvas";
|
|
32
|
+
}
|
|
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";
|
|
40
|
+
}
|
|
41
|
+
/**Sets the width of the page */
|
|
42
|
+
set width(width) {
|
|
43
|
+
this.#width = width;
|
|
44
|
+
this.style.width = width + "px";
|
|
45
|
+
}
|
|
46
|
+
/**Returns the width of the page */
|
|
47
|
+
get width() {
|
|
48
|
+
return this.#width;
|
|
49
|
+
}
|
|
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);
|
|
94
67
|
}
|
|
68
|
+
this.#widgets = widgets;
|
|
95
69
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.#
|
|
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
|
-
};
|
|
70
|
+
/**Returns the widgets on the page */
|
|
71
|
+
get widgets() {
|
|
72
|
+
return this.#widgets;
|
|
140
73
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
};
|
|
74
|
+
};
|
|
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@ */
|
|
94
|
+
static element_name() {
|
|
95
|
+
return "@abstract@";
|
|
177
96
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
this.#rc_indicator.setAttribute("transform-origin", `${this.#rotation_center_x} ${this.#rotation_center_y}`);
|
|
97
|
+
/**Returns the namespace override for the element*/
|
|
98
|
+
static element_name_space() {
|
|
99
|
+
return "canvas";
|
|
182
100
|
}
|
|
101
|
+
#x = 0;
|
|
102
|
+
#y = 0;
|
|
103
|
+
#width = 0;
|
|
104
|
+
#height = 0;
|
|
183
105
|
#rotation = 0;
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
this
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
};
|
|
106
|
+
constructor() {
|
|
107
|
+
super();
|
|
108
|
+
this.style.position = "absolute";
|
|
109
|
+
this.style.boxSizing = "border-box";
|
|
110
|
+
this.style.transformOrigin = "0 0";
|
|
111
|
+
}
|
|
112
|
+
connectedCallback() {
|
|
113
|
+
super.connectedCallback();
|
|
114
|
+
this.#update();
|
|
115
|
+
}
|
|
116
|
+
/**Sets the x position of the left edge of the widget */
|
|
117
|
+
set x(x) {
|
|
118
|
+
this.#x = x;
|
|
119
|
+
this.#update();
|
|
120
|
+
}
|
|
121
|
+
/**Returns the x position of the left edge of the widget */
|
|
122
|
+
get x() {
|
|
123
|
+
return this.#x;
|
|
124
|
+
}
|
|
125
|
+
/**Sets the y position of the top edge of the widget */
|
|
126
|
+
set y(y) {
|
|
127
|
+
this.#y = y;
|
|
128
|
+
this.#update();
|
|
129
|
+
}
|
|
130
|
+
/**Returns the y position of the top edge of the widget */
|
|
131
|
+
get y() {
|
|
132
|
+
return this.#y;
|
|
133
|
+
}
|
|
134
|
+
/**Sets the width of the widget */
|
|
135
|
+
set width(width) {
|
|
136
|
+
this.#width = width;
|
|
137
|
+
this.#update();
|
|
138
|
+
}
|
|
139
|
+
/**Returns the width of the widget */
|
|
140
|
+
get width() {
|
|
141
|
+
return this.#width;
|
|
255
142
|
}
|
|
256
|
-
|
|
257
|
-
set
|
|
258
|
-
this.#
|
|
259
|
-
this.#
|
|
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;
|
|
143
|
+
/**Sets the height of the widget */
|
|
144
|
+
set height(height) {
|
|
145
|
+
this.#height = height;
|
|
146
|
+
this.#update();
|
|
269
147
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
this.#
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
this.#
|
|
277
|
-
this.#
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
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));
|
|
281
174
|
}
|
|
282
175
|
};
|
|
283
|
-
// src/canvas/
|
|
284
|
-
|
|
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
|
+
* Steps below 1 divide by the inverse, so steps like 0.1 do not give floating point errors like 0.30000000000000004 */
|
|
187
|
+
function snap(value, step) {
|
|
188
|
+
if (step <= 0) return value;
|
|
189
|
+
return step < 1 ? Math.round(value / step) / Math.round(1 / step) : Math.round(value / step) * step;
|
|
190
|
+
}
|
|
191
|
+
/**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 */
|
|
192
|
+
const RESIZE_HANDLES = [
|
|
193
|
+
{
|
|
194
|
+
name: "nw",
|
|
195
|
+
x: -1,
|
|
196
|
+
y: -1
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
name: "n",
|
|
200
|
+
x: 0,
|
|
201
|
+
y: -1
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
name: "ne",
|
|
205
|
+
x: 1,
|
|
206
|
+
y: -1
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
name: "e",
|
|
210
|
+
x: 1,
|
|
211
|
+
y: 0
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
name: "se",
|
|
215
|
+
x: 1,
|
|
216
|
+
y: 1
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
name: "s",
|
|
220
|
+
x: 0,
|
|
221
|
+
y: 1
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
name: "sw",
|
|
225
|
+
x: -1,
|
|
226
|
+
y: 1
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
name: "w",
|
|
230
|
+
x: -1,
|
|
231
|
+
y: 0
|
|
232
|
+
}
|
|
233
|
+
];
|
|
234
|
+
/**Dashed outline with handles, attached to a widget to move, resize and rotate it
|
|
235
|
+
* Corner handles resize both axes and side handles one axis, keeping the opposite corner or side in place
|
|
236
|
+
* The handle above the outline rotates the widget around its center
|
|
237
|
+
* The center handle and the outline itself move the widget, the inside of the outline is left to the widgets below
|
|
238
|
+
*
|
|
239
|
+
* Position and size snap to a 5px grid and rotation to 15 degrees
|
|
240
|
+
* Holding alt snaps to a 1px grid and 5 degrees instead, holding shift disables snapping
|
|
241
|
+
*
|
|
242
|
+
* Resize and rotate cursors follow the rotation of the widget in 45 degree steps
|
|
243
|
+
*
|
|
244
|
+
* The transformer is drawn in viewport pixels, so it keeps its size when the page is zoomed*/
|
|
245
|
+
var Transformer = class extends Base {
|
|
246
|
+
/**Returns the name used to define the element */
|
|
285
247
|
static element_name() {
|
|
286
|
-
return "
|
|
248
|
+
return "transformer";
|
|
287
249
|
}
|
|
250
|
+
/**Returns the namespace override for the element*/
|
|
288
251
|
static element_name_space() {
|
|
289
|
-
return "
|
|
290
|
-
}
|
|
291
|
-
#
|
|
292
|
-
#
|
|
293
|
-
#
|
|
294
|
-
#
|
|
295
|
-
#
|
|
296
|
-
|
|
297
|
-
|
|
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) {
|
|
252
|
+
return "canvas";
|
|
253
|
+
}
|
|
254
|
+
#widget;
|
|
255
|
+
#offset_x = 0;
|
|
256
|
+
#offset_y = 0;
|
|
257
|
+
#scale = 1;
|
|
258
|
+
#actions = /* @__PURE__ */ new Map();
|
|
259
|
+
#drag;
|
|
260
|
+
constructor() {
|
|
307
261
|
super();
|
|
308
|
-
this
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
this.#
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
262
|
+
this.hidden = true;
|
|
263
|
+
for (const side of [
|
|
264
|
+
"top",
|
|
265
|
+
"right",
|
|
266
|
+
"bottom",
|
|
267
|
+
"left"
|
|
268
|
+
]) this.#add("side " + side, { type: "move" });
|
|
269
|
+
this.#add("line", void 0);
|
|
270
|
+
for (const handle of RESIZE_HANDLES) this.#add("handle " + handle.name, {
|
|
271
|
+
type: "resize",
|
|
272
|
+
x: handle.x,
|
|
273
|
+
y: handle.y
|
|
274
|
+
}, material_arrow_range_rounded);
|
|
275
|
+
this.#add("handle rotate", { type: "rotate" }, material_rotate_right_rounded);
|
|
276
|
+
this.#add("handle move", { type: "move" }, material_open_with_rounded);
|
|
277
|
+
this.addEventListener("pointerdown", this.#on_pointer_down);
|
|
278
|
+
}
|
|
279
|
+
disconnectedCallback() {
|
|
280
|
+
super.disconnectedCallback();
|
|
281
|
+
this.#stop_drag();
|
|
282
|
+
}
|
|
283
|
+
/**Sets the widget the transformer is attached to, undefined hides the transformer */
|
|
284
|
+
set widget(widget) {
|
|
285
|
+
if (this.#widget === widget) return;
|
|
286
|
+
this.#stop_drag();
|
|
287
|
+
this.#widget?.removeEventListener(WIDGET_TRANSFORM_EVENT, this.#on_widget_transform);
|
|
288
|
+
this.#widget = widget;
|
|
289
|
+
widget?.addEventListener(WIDGET_TRANSFORM_EVENT, this.#on_widget_transform);
|
|
290
|
+
this.#update();
|
|
291
|
+
}
|
|
292
|
+
/**Returns the widget the transformer is attached to */
|
|
293
|
+
get widget() {
|
|
294
|
+
return this.#widget;
|
|
295
|
+
}
|
|
296
|
+
/**Returns whether a handle is being dragged */
|
|
297
|
+
get dragging() {
|
|
298
|
+
return this.#drag !== void 0;
|
|
299
|
+
}
|
|
300
|
+
/**Sets how page units map to viewport pixels, viewport = offset + page * scale */
|
|
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
|
+
}
|
|
307
|
+
#add(class_name, action, icon) {
|
|
308
|
+
const element = this.appendChild(document.createElement("div"));
|
|
309
|
+
element.className = class_name;
|
|
310
|
+
if (icon) element.appendChild(icon());
|
|
311
|
+
if (action) this.#actions.set(element, action);
|
|
312
|
+
}
|
|
313
|
+
#update() {
|
|
314
|
+
const widget = this.#widget;
|
|
315
|
+
this.hidden = !widget;
|
|
316
|
+
if (!widget) return;
|
|
317
|
+
const scale = this.#scale;
|
|
318
|
+
this.style.left = this.#offset_x + widget.x * scale + "px";
|
|
319
|
+
this.style.top = this.#offset_y + widget.y * scale + "px";
|
|
320
|
+
this.style.width = widget.width * scale + "px";
|
|
321
|
+
this.style.height = widget.height * scale + "px";
|
|
322
|
+
this.style.transform = `rotate(${widget.rotation}deg)`;
|
|
323
|
+
const half_turn = (widget.rotation % 180 + 180) % 180;
|
|
324
|
+
this.setAttribute("cursor-step", String(Math.round(half_turn / 45) % 4));
|
|
325
|
+
}
|
|
326
|
+
#start(action, e) {
|
|
327
|
+
const widget = this.#widget;
|
|
328
|
+
if (!widget || this.#drag) return;
|
|
329
|
+
e.preventDefault();
|
|
330
|
+
e.stopPropagation();
|
|
331
|
+
const rect = this.getBoundingClientRect();
|
|
332
|
+
this.#drag = {
|
|
333
|
+
action,
|
|
334
|
+
pointer_id: e.pointerId,
|
|
335
|
+
client_x: e.clientX,
|
|
336
|
+
client_y: e.clientY,
|
|
337
|
+
center_x: rect.left + rect.width / 2,
|
|
338
|
+
center_y: rect.top + rect.height / 2,
|
|
339
|
+
x: widget.x,
|
|
340
|
+
y: widget.y,
|
|
341
|
+
width: widget.width,
|
|
342
|
+
height: widget.height,
|
|
343
|
+
rotation: widget.rotation
|
|
364
344
|
};
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
345
|
+
window.addEventListener("pointermove", this.#on_pointer_move);
|
|
346
|
+
window.addEventListener("pointerup", this.#on_pointer_up);
|
|
347
|
+
window.addEventListener("pointercancel", this.#on_pointer_up);
|
|
348
|
+
}
|
|
349
|
+
#stop_drag() {
|
|
350
|
+
if (!this.#drag) return;
|
|
351
|
+
this.#drag = void 0;
|
|
352
|
+
window.removeEventListener("pointermove", this.#on_pointer_move);
|
|
353
|
+
window.removeEventListener("pointerup", this.#on_pointer_up);
|
|
354
|
+
window.removeEventListener("pointercancel", this.#on_pointer_up);
|
|
355
|
+
}
|
|
356
|
+
#on_widget_transform = () => {
|
|
357
|
+
this.#update();
|
|
358
|
+
};
|
|
359
|
+
#on_pointer_down = (e) => {
|
|
360
|
+
const action = e.target instanceof Element ? this.#actions.get(e.target) : void 0;
|
|
361
|
+
if (!action || e.button !== 0) return;
|
|
362
|
+
this.#start(action, e);
|
|
363
|
+
};
|
|
364
|
+
#on_pointer_move = (e) => {
|
|
365
|
+
const drag = this.#drag;
|
|
366
|
+
const widget = this.#widget;
|
|
367
|
+
if (!drag || !widget || e.pointerId !== drag.pointer_id) return;
|
|
368
|
+
const delta_x = (e.clientX - drag.client_x) / this.#scale;
|
|
369
|
+
const delta_y = (e.clientY - drag.client_y) / this.#scale;
|
|
370
|
+
const grid = e.shiftKey ? 0 : e.altKey ? SNAP_GRID_FINE : SNAP_GRID;
|
|
371
|
+
const action = drag.action;
|
|
372
|
+
switch (action.type) {
|
|
373
|
+
case "move":
|
|
374
|
+
widget.x = snap(drag.x + delta_x, grid);
|
|
375
|
+
widget.y = snap(drag.y + delta_y, grid);
|
|
376
|
+
break;
|
|
377
|
+
case "resize": {
|
|
378
|
+
const radians = drag.rotation * Math.PI / 180;
|
|
379
|
+
const cos = Math.cos(radians);
|
|
380
|
+
const sin = Math.sin(radians);
|
|
381
|
+
const local_x = delta_x * cos + delta_y * sin;
|
|
382
|
+
const local_y = delta_y * cos - delta_x * sin;
|
|
383
|
+
const width = action.x ? Math.max(MIN_SIZE, snap(drag.width + action.x * local_x, grid)) : drag.width;
|
|
384
|
+
const height = action.y ? Math.max(MIN_SIZE, snap(drag.height + action.y * local_y, grid)) : drag.height;
|
|
385
|
+
const shift_x = action.x * (width - drag.width) / 2;
|
|
386
|
+
const shift_y = action.y * (height - drag.height) / 2;
|
|
387
|
+
const center_x = drag.x + drag.width / 2 + shift_x * cos - shift_y * sin;
|
|
388
|
+
const center_y = drag.y + drag.height / 2 + shift_x * sin + shift_y * cos;
|
|
389
|
+
widget.x = center_x - width / 2;
|
|
390
|
+
widget.y = center_y - height / 2;
|
|
391
|
+
widget.width = width;
|
|
392
|
+
widget.height = height;
|
|
393
|
+
break;
|
|
369
394
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
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);
|
|
395
|
+
case "rotate": {
|
|
396
|
+
const start = Math.atan2(drag.client_y - drag.center_y, drag.client_x - drag.center_x);
|
|
397
|
+
const now = Math.atan2(e.clientY - drag.center_y, e.clientX - drag.center_x);
|
|
398
|
+
widget.rotation = (snap(drag.rotation + (now - start) * 180 / Math.PI, e.shiftKey ? 0 : e.altKey ? SNAP_ANGLE_FINE : SNAP_ANGLE) % 360 + 360) % 360;
|
|
399
|
+
break;
|
|
380
400
|
}
|
|
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
401
|
}
|
|
402
|
+
};
|
|
403
|
+
#on_pointer_up = (e) => {
|
|
404
|
+
if (e.pointerId === this.#drag?.pointer_id) this.#stop_drag();
|
|
405
|
+
};
|
|
406
|
+
};
|
|
407
|
+
define_element(Transformer);
|
|
408
|
+
// src/canvas/viewport.ts
|
|
409
|
+
const ZOOM_MIN = .1;
|
|
410
|
+
const ZOOM_MAX = 20;
|
|
411
|
+
/**Zoom factor per wheel pixel */
|
|
412
|
+
const WHEEL_ZOOM_SPEED = .002;
|
|
413
|
+
/**Zoom factor per wheel pixel for touchpad pinch, which reports much smaller deltas than a mouse wheel */
|
|
414
|
+
const PINCH_ZOOM_SPEED = .01;
|
|
415
|
+
/**Screen pixels panned per wheel pixel */
|
|
416
|
+
const WHEEL_PAN_SPEED = .5;
|
|
417
|
+
/**Pixels per line, for wheel events reported in lines */
|
|
418
|
+
const WHEEL_LINE_HEIGHT = 16;
|
|
419
|
+
/**Speed multiplier for wheel zoom and pan while alt is held */
|
|
420
|
+
const WHEEL_SLOW = .1;
|
|
421
|
+
/**Viewport showing a page of widgets
|
|
422
|
+
* The page is scaled to fit inside the viewport element while keeping its aspect ratio, and centered
|
|
423
|
+
*
|
|
424
|
+
* When edit is true the page can be zoomed and panned, and a checkerboard is shown behind it
|
|
425
|
+
* Wheel and touchpad two finger swipe pans, ctrl + wheel or swipe zooms toward the cursor
|
|
426
|
+
* Shift + vertical wheel pans horizontally
|
|
427
|
+
* Touchpad pinch zooms toward the cursor
|
|
428
|
+
* Holding alt makes wheel zoom and pan 10 times slower
|
|
429
|
+
* Two finger touch pinch zooms and pans, middle or right mouse drag pans
|
|
430
|
+
*
|
|
431
|
+
* Pressing a widget in edit mode selects it and attaches a transformer to move, resize and rotate it
|
|
432
|
+
* Pressing outside any widget clears the selection*/
|
|
433
|
+
var Viewport = class extends Base {
|
|
434
|
+
/**Returns the name used to define the element */
|
|
435
|
+
static element_name() {
|
|
436
|
+
return "viewport";
|
|
399
437
|
}
|
|
400
|
-
/**
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
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));
|
|
438
|
+
/**Returns the namespace override for the element*/
|
|
439
|
+
static element_name_space() {
|
|
440
|
+
return "canvas";
|
|
441
|
+
}
|
|
442
|
+
#background = this.appendChild(document.createElement("div"));
|
|
443
|
+
#container = this.appendChild(document.createElement("div"));
|
|
444
|
+
#transformer = this.appendChild(new Transformer());
|
|
445
|
+
#page;
|
|
446
|
+
#page_observer = new ResizeObserver(() => {
|
|
447
|
+
this.#update_view();
|
|
454
448
|
});
|
|
455
|
-
|
|
456
|
-
#
|
|
457
|
-
#
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
449
|
+
#scale = 1;
|
|
450
|
+
#fit_x = 0;
|
|
451
|
+
#fit_y = 0;
|
|
452
|
+
#edit = false;
|
|
453
|
+
#zoom = 1;
|
|
454
|
+
#pan_x = 0;
|
|
455
|
+
#pan_y = 0;
|
|
456
|
+
#touches = /* @__PURE__ */ new Map();
|
|
457
|
+
#pan_pointer_id;
|
|
458
|
+
#pan_pointer_last = {
|
|
459
|
+
x: 0,
|
|
460
|
+
y: 0
|
|
461
|
+
};
|
|
462
|
+
/**Whether the ctrl key is physically held, to tell touchpad pinch from ctrl + wheel */
|
|
463
|
+
#ctrl_held = false;
|
|
464
|
+
constructor() {
|
|
465
|
+
super();
|
|
466
|
+
this.#background.className = "checkerboard";
|
|
467
|
+
this.#container.className = "container";
|
|
468
|
+
this.attach_to_resize_observer();
|
|
469
|
+
}
|
|
470
|
+
/**Sets the page shown in the viewport */
|
|
471
|
+
set page(page) {
|
|
472
|
+
if (this.#page === page) return;
|
|
473
|
+
this.selected = void 0;
|
|
474
|
+
if (this.#page) {
|
|
475
|
+
this.#page_observer.unobserve(this.#page);
|
|
476
|
+
if (this.#page.parentElement === this.#container) this.#page.remove();
|
|
462
477
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
this.#elements = rows;
|
|
468
|
-
if (rows.length === 0) {
|
|
469
|
-
this.#canvas_elements.replaceChildren();
|
|
470
|
-
return;
|
|
478
|
+
this.#page = page;
|
|
479
|
+
if (page) {
|
|
480
|
+
this.#container.appendChild(page);
|
|
481
|
+
this.#page_observer.observe(page);
|
|
471
482
|
}
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
483
|
+
this.#update_view();
|
|
484
|
+
}
|
|
485
|
+
/**Returns the page shown in the viewport */
|
|
486
|
+
get page() {
|
|
487
|
+
return this.#page;
|
|
488
|
+
}
|
|
489
|
+
/**Sets edit mode, which enables zooming and panning and shows a checkerboard background
|
|
490
|
+
* Leaving edit mode resets the view to fit the page */
|
|
491
|
+
set edit(edit) {
|
|
492
|
+
if (this.#edit === edit) return;
|
|
493
|
+
this.#edit = edit;
|
|
494
|
+
this.toggleAttribute("edit", edit);
|
|
495
|
+
this.#listen_keys(edit && this.isConnected);
|
|
496
|
+
if (edit) {
|
|
497
|
+
this.addEventListener("wheel", this.#on_wheel, { passive: false });
|
|
498
|
+
this.addEventListener("pointerdown", this.#on_pointer_down);
|
|
499
|
+
this.addEventListener("pointermove", this.#on_pointer_move);
|
|
500
|
+
this.addEventListener("pointerup", this.#on_pointer_up);
|
|
501
|
+
this.addEventListener("pointercancel", this.#on_pointer_up);
|
|
502
|
+
this.addEventListener("mousedown", this.#on_mouse_down);
|
|
503
|
+
this.addEventListener("contextmenu", this.#on_context_menu);
|
|
504
|
+
} else {
|
|
505
|
+
this.removeEventListener("wheel", this.#on_wheel);
|
|
506
|
+
this.removeEventListener("pointerdown", this.#on_pointer_down);
|
|
507
|
+
this.removeEventListener("pointermove", this.#on_pointer_move);
|
|
508
|
+
this.removeEventListener("pointerup", this.#on_pointer_up);
|
|
509
|
+
this.removeEventListener("pointercancel", this.#on_pointer_up);
|
|
510
|
+
this.removeEventListener("mousedown", this.#on_mouse_down);
|
|
511
|
+
this.removeEventListener("contextmenu", this.#on_context_menu);
|
|
512
|
+
this.#touches.clear();
|
|
513
|
+
this.#pan_pointer_id = void 0;
|
|
514
|
+
this.selected = void 0;
|
|
515
|
+
this.#zoom = 1;
|
|
516
|
+
this.#pan_x = 0;
|
|
517
|
+
this.#pan_y = 0;
|
|
518
|
+
this.#update_view();
|
|
494
519
|
}
|
|
495
520
|
}
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
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})`);
|
|
521
|
+
/**Returns whether edit mode is enabled */
|
|
522
|
+
get edit() {
|
|
523
|
+
return this.#edit;
|
|
524
|
+
}
|
|
525
|
+
/**Sets the selected widget, which the transformer is attached to, selection is cleared when leaving edit mode */
|
|
526
|
+
set selected(widget) {
|
|
527
|
+
this.#transformer.widget = widget;
|
|
528
|
+
}
|
|
529
|
+
/**Returns the selected widget */
|
|
530
|
+
get selected() {
|
|
531
|
+
return this.#transformer.widget;
|
|
532
|
+
}
|
|
533
|
+
/**Returns the current scale from page units to screen pixels */
|
|
534
|
+
get scale() {
|
|
535
|
+
return this.#scale;
|
|
536
|
+
}
|
|
537
|
+
on_resize() {
|
|
538
|
+
this.#update_view();
|
|
539
|
+
}
|
|
540
|
+
connectedCallback() {
|
|
541
|
+
super.connectedCallback();
|
|
542
|
+
if (this.#edit) this.#listen_keys(true);
|
|
543
|
+
}
|
|
544
|
+
disconnectedCallback() {
|
|
545
|
+
super.disconnectedCallback();
|
|
546
|
+
this.#listen_keys(false);
|
|
547
|
+
}
|
|
548
|
+
/**Adds or removes the window listeners tracking the ctrl key */
|
|
549
|
+
#listen_keys(listen) {
|
|
550
|
+
if (listen) {
|
|
551
|
+
window.addEventListener("keydown", this.#on_key);
|
|
552
|
+
window.addEventListener("keyup", this.#on_key);
|
|
553
|
+
window.addEventListener("blur", this.#on_blur);
|
|
554
|
+
} else {
|
|
555
|
+
window.removeEventListener("keydown", this.#on_key);
|
|
556
|
+
window.removeEventListener("keyup", this.#on_key);
|
|
557
|
+
window.removeEventListener("blur", this.#on_blur);
|
|
558
|
+
this.#ctrl_held = false;
|
|
578
559
|
}
|
|
579
560
|
}
|
|
580
|
-
#
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
this.#
|
|
589
|
-
this.#
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
561
|
+
#update_view() {
|
|
562
|
+
if (!this.#page) return;
|
|
563
|
+
const width = this.#page.width;
|
|
564
|
+
const height = this.#page.height;
|
|
565
|
+
if (width <= 0 || height <= 0) return;
|
|
566
|
+
const rect = this.actual_size;
|
|
567
|
+
const fit = Math.min(rect.width / width, rect.height / height);
|
|
568
|
+
this.#fit_x = (rect.width - width * fit) / 2;
|
|
569
|
+
this.#fit_y = (rect.height - height * fit) / 2;
|
|
570
|
+
this.#scale = fit * this.#zoom;
|
|
571
|
+
this.#container.style.transform = `translate(${this.#fit_x + this.#pan_x}px, ${this.#fit_y + this.#pan_y}px) scale(${this.#scale})`;
|
|
572
|
+
this.#transformer.view(this.#fit_x + this.#pan_x, this.#fit_y + this.#pan_y, this.#scale);
|
|
573
|
+
}
|
|
574
|
+
/**Zooms by the factor, keeping the given point in viewport coordinates in place */
|
|
575
|
+
#zoom_at(x, y, factor) {
|
|
576
|
+
const zoom = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, this.#zoom * factor));
|
|
577
|
+
const ratio = zoom / this.#zoom;
|
|
578
|
+
this.#pan_x += (x - this.#fit_x - this.#pan_x) * (1 - ratio);
|
|
579
|
+
this.#pan_y += (y - this.#fit_y - this.#pan_y) * (1 - ratio);
|
|
580
|
+
this.#zoom = zoom;
|
|
581
|
+
}
|
|
582
|
+
/**Returns the position of the event in viewport coordinates */
|
|
583
|
+
#local(e) {
|
|
584
|
+
const rect = this.getBoundingClientRect();
|
|
585
|
+
return {
|
|
586
|
+
x: e.clientX - rect.left,
|
|
587
|
+
y: e.clientY - rect.top
|
|
588
|
+
};
|
|
602
589
|
}
|
|
603
|
-
|
|
604
|
-
|
|
590
|
+
/**Selects the widget under the pointer, or clears the selection if there is none */
|
|
591
|
+
#select_at(e) {
|
|
592
|
+
const page = this.#page;
|
|
593
|
+
let element = e.target instanceof Element ? e.target : null;
|
|
594
|
+
while (element && element.parentElement !== page) element = element.parentElement;
|
|
595
|
+
this.selected = page?.widgets.find((widget) => widget === element);
|
|
596
|
+
}
|
|
597
|
+
/**Returns center and distance of the first two touches */
|
|
598
|
+
#pinch() {
|
|
599
|
+
if (this.#touches.size < 2) return void 0;
|
|
600
|
+
const [a, b] = this.#touches.values();
|
|
601
|
+
return {
|
|
602
|
+
x: (a.x + b.x) / 2,
|
|
603
|
+
y: (a.y + b.y) / 2,
|
|
604
|
+
distance: Math.hypot(a.x - b.x, a.y - b.y)
|
|
605
|
+
};
|
|
605
606
|
}
|
|
606
|
-
#
|
|
607
|
-
|
|
608
|
-
this.
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
607
|
+
#on_wheel = (e) => {
|
|
608
|
+
e.preventDefault();
|
|
609
|
+
const unit = e.deltaMode === WheelEvent.DOM_DELTA_LINE ? WHEEL_LINE_HEIGHT : e.deltaMode === WheelEvent.DOM_DELTA_PAGE ? this.actual_size.height : 1;
|
|
610
|
+
const delta_x = e.deltaX * unit;
|
|
611
|
+
const delta_y = e.deltaY * unit;
|
|
612
|
+
const speed = e.altKey ? WHEEL_SLOW : 1;
|
|
613
|
+
if (e.ctrlKey) {
|
|
614
|
+
const zoom_speed = this.#ctrl_held ? WHEEL_ZOOM_SPEED : PINCH_ZOOM_SPEED;
|
|
615
|
+
const point = this.#local(e);
|
|
616
|
+
this.#zoom_at(point.x, point.y, Math.exp(-delta_y * zoom_speed * speed));
|
|
617
|
+
} else {
|
|
618
|
+
const swap = e.shiftKey && delta_x === 0;
|
|
619
|
+
this.#pan_x -= (swap ? delta_y : delta_x) * speed * WHEEL_PAN_SPEED;
|
|
620
|
+
this.#pan_y -= (swap ? 0 : delta_y) * speed * WHEEL_PAN_SPEED;
|
|
612
621
|
}
|
|
613
|
-
|
|
622
|
+
this.#update_view();
|
|
623
|
+
};
|
|
624
|
+
#on_pointer_down = (e) => {
|
|
625
|
+
if (e.pointerType === "touch") {
|
|
626
|
+
if (this.#touches.size === 0 && !this.#transformer.dragging) this.#select_at(e);
|
|
627
|
+
this.#touches.set(e.pointerId, this.#local(e));
|
|
628
|
+
this.setPointerCapture(e.pointerId);
|
|
629
|
+
} else if (e.button === 0) this.#select_at(e);
|
|
630
|
+
else if (e.button === 1 || e.button === 2) {
|
|
631
|
+
e.preventDefault();
|
|
632
|
+
this.#pan_pointer_id = e.pointerId;
|
|
633
|
+
this.#pan_pointer_last = this.#local(e);
|
|
634
|
+
this.setPointerCapture(e.pointerId);
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
#on_pointer_move = (e) => {
|
|
638
|
+
this.#ctrl_held = e.ctrlKey;
|
|
639
|
+
if (this.#touches.has(e.pointerId)) {
|
|
640
|
+
const before = this.#pinch();
|
|
641
|
+
this.#touches.set(e.pointerId, this.#local(e));
|
|
642
|
+
const after = this.#pinch();
|
|
643
|
+
if (!before || !after || before.distance <= 0) return;
|
|
644
|
+
this.#pan_x += after.x - before.x;
|
|
645
|
+
this.#pan_y += after.y - before.y;
|
|
646
|
+
this.#zoom_at(after.x, after.y, after.distance / before.distance);
|
|
647
|
+
this.#update_view();
|
|
648
|
+
} else if (e.pointerId === this.#pan_pointer_id) {
|
|
649
|
+
const point = this.#local(e);
|
|
650
|
+
this.#pan_x += point.x - this.#pan_pointer_last.x;
|
|
651
|
+
this.#pan_y += point.y - this.#pan_pointer_last.y;
|
|
652
|
+
this.#pan_pointer_last = point;
|
|
653
|
+
this.#update_view();
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
#on_pointer_up = (e) => {
|
|
657
|
+
this.#touches.delete(e.pointerId);
|
|
658
|
+
if (e.pointerId === this.#pan_pointer_id) this.#pan_pointer_id = void 0;
|
|
659
|
+
};
|
|
660
|
+
/**Prevents the browser autoscroll on middle mouse button */
|
|
661
|
+
#on_mouse_down = (e) => {
|
|
662
|
+
if (e.button === 1) e.preventDefault();
|
|
663
|
+
};
|
|
664
|
+
/**Prevents the context menu, as the right mouse button pans in edit mode */
|
|
665
|
+
#on_context_menu = (e) => {
|
|
666
|
+
e.preventDefault();
|
|
667
|
+
};
|
|
668
|
+
#on_key = (e) => {
|
|
669
|
+
this.#ctrl_held = e.ctrlKey;
|
|
670
|
+
};
|
|
671
|
+
#on_blur = () => {
|
|
672
|
+
this.#ctrl_held = false;
|
|
673
|
+
};
|
|
614
674
|
};
|
|
675
|
+
define_element(Viewport);
|
|
676
|
+
/**Creates a viewport showing a page of widgets */
|
|
677
|
+
const canvas_viewport = (options) => options ? new Viewport().opts(options) : new Viewport();
|
|
615
678
|
// src/canvas/index.ts
|
|
616
|
-
|
|
617
|
-
|
|
679
|
+
/**Displaying and editing graphics with a fixed position and size */
|
|
680
|
+
const canvas = {
|
|
681
|
+
page: canvas_page,
|
|
682
|
+
viewport: canvas_viewport,
|
|
683
|
+
WIDGET_TRANSFORM_EVENT
|
|
684
|
+
};
|
|
685
|
+
export { WIDGET_TRANSFORM_EVENT, WidgetBase, canvas, canvas as default };
|
|
618
686
|
|
|
619
687
|
//# sourceMappingURL=index.mjs.map
|