@hypen-space/web 0.4.31 → 0.4.35
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/dist/canvas/accessibility.js +19 -9
- package/dist/canvas/accessibility.js.map +2 -2
- package/dist/canvas/events.d.ts +7 -0
- package/dist/canvas/events.js +53 -17
- package/dist/canvas/events.js.map +3 -3
- package/dist/canvas/index.js +19 -2134
- package/dist/canvas/index.js.map +3 -11
- package/dist/canvas/input.js +19 -9
- package/dist/canvas/input.js.map +2 -2
- package/dist/canvas/layout.js +19 -9
- package/dist/canvas/layout.js.map +2 -2
- package/dist/canvas/paint.d.ts +4 -0
- package/dist/canvas/paint.js +100 -11
- package/dist/canvas/paint.js.map +3 -3
- package/dist/canvas/renderer.js +821 -707
- package/dist/canvas/renderer.js.map +7 -7
- package/dist/canvas/text.js +19 -9
- package/dist/canvas/text.js.map +2 -2
- package/dist/canvas/utils.js +19 -9
- package/dist/canvas/utils.js.map +2 -2
- package/dist/client.js +19 -3863
- package/dist/client.js.map +3 -57
- package/dist/dom/applicators/advanced-layout.js +19 -9
- package/dist/dom/applicators/advanced-layout.js.map +2 -2
- package/dist/dom/applicators/background.js +19 -9
- package/dist/dom/applicators/background.js.map +2 -2
- package/dist/dom/applicators/border.js +19 -9
- package/dist/dom/applicators/border.js.map +2 -2
- package/dist/dom/applicators/color.js +19 -9
- package/dist/dom/applicators/color.js.map +2 -2
- package/dist/dom/applicators/display.js +19 -9
- package/dist/dom/applicators/display.js.map +2 -2
- package/dist/dom/applicators/effects.js +19 -9
- package/dist/dom/applicators/effects.js.map +2 -2
- package/dist/dom/applicators/events.js +26 -10
- package/dist/dom/applicators/events.js.map +3 -3
- package/dist/dom/applicators/font.js +19 -9
- package/dist/dom/applicators/font.js.map +2 -2
- package/dist/dom/applicators/index.js +26 -10
- package/dist/dom/applicators/index.js.map +3 -3
- package/dist/dom/applicators/layout.js +19 -9
- package/dist/dom/applicators/layout.js.map +2 -2
- package/dist/dom/applicators/margin.js +19 -9
- package/dist/dom/applicators/margin.js.map +2 -2
- package/dist/dom/applicators/padding.js +19 -9
- package/dist/dom/applicators/padding.js.map +2 -2
- package/dist/dom/applicators/size.js +19 -9
- package/dist/dom/applicators/size.js.map +2 -2
- package/dist/dom/applicators/transform.js +19 -9
- package/dist/dom/applicators/transform.js.map +2 -2
- package/dist/dom/applicators/transition.js +19 -9
- package/dist/dom/applicators/transition.js.map +2 -2
- package/dist/dom/applicators/typography.js +19 -9
- package/dist/dom/applicators/typography.js.map +2 -2
- package/dist/dom/canvas/index.js +19 -9
- package/dist/dom/canvas/index.js.map +2 -2
- package/dist/dom/components/app.js +19 -9
- package/dist/dom/components/app.js.map +2 -2
- package/dist/dom/components/audio.js +19 -9
- package/dist/dom/components/audio.js.map +2 -2
- package/dist/dom/components/avatar.js +19 -9
- package/dist/dom/components/avatar.js.map +2 -2
- package/dist/dom/components/badge.js +19 -9
- package/dist/dom/components/badge.js.map +2 -2
- package/dist/dom/components/button.js +19 -9
- package/dist/dom/components/button.js.map +2 -2
- package/dist/dom/components/card.js +19 -9
- package/dist/dom/components/card.js.map +2 -2
- package/dist/dom/components/center.js +19 -9
- package/dist/dom/components/center.js.map +2 -2
- package/dist/dom/components/checkbox.js +19 -9
- package/dist/dom/components/checkbox.js.map +2 -2
- package/dist/dom/components/column.js +19 -9
- package/dist/dom/components/column.js.map +2 -2
- package/dist/dom/components/container.js +19 -9
- package/dist/dom/components/container.js.map +2 -2
- package/dist/dom/components/divider.js +19 -9
- package/dist/dom/components/divider.js.map +2 -2
- package/dist/dom/components/grid.js +19 -9
- package/dist/dom/components/grid.js.map +2 -2
- package/dist/dom/components/heading.js +19 -9
- package/dist/dom/components/heading.js.map +2 -2
- package/dist/dom/components/hypenapp.js +19 -9
- package/dist/dom/components/hypenapp.js.map +2 -2
- package/dist/dom/components/image.js +19 -9
- package/dist/dom/components/image.js.map +2 -2
- package/dist/dom/components/index.js +20 -10
- package/dist/dom/components/index.js.map +3 -3
- package/dist/dom/components/input.js +19 -9
- package/dist/dom/components/input.js.map +2 -2
- package/dist/dom/components/link.js +19 -9
- package/dist/dom/components/link.js.map +2 -2
- package/dist/dom/components/list.js +19 -9
- package/dist/dom/components/list.js.map +2 -2
- package/dist/dom/components/paragraph.js +19 -9
- package/dist/dom/components/paragraph.js.map +2 -2
- package/dist/dom/components/progressbar.js +19 -9
- package/dist/dom/components/progressbar.js.map +2 -2
- package/dist/dom/components/route.js +19 -9
- package/dist/dom/components/route.js.map +2 -2
- package/dist/dom/components/router.js +19 -9
- package/dist/dom/components/router.js.map +2 -2
- package/dist/dom/components/row.js +19 -9
- package/dist/dom/components/row.js.map +2 -2
- package/dist/dom/components/select.js +19 -9
- package/dist/dom/components/select.js.map +2 -2
- package/dist/dom/components/slider.js +19 -9
- package/dist/dom/components/slider.js.map +2 -2
- package/dist/dom/components/spacer.js +19 -9
- package/dist/dom/components/spacer.js.map +2 -2
- package/dist/dom/components/spinner.js +19 -9
- package/dist/dom/components/spinner.js.map +2 -2
- package/dist/dom/components/stack.js +19 -9
- package/dist/dom/components/stack.js.map +2 -2
- package/dist/dom/components/switch.js +19 -9
- package/dist/dom/components/switch.js.map +2 -2
- package/dist/dom/components/text.js +19 -9
- package/dist/dom/components/text.js.map +2 -2
- package/dist/dom/components/textarea.js +20 -10
- package/dist/dom/components/textarea.js.map +3 -3
- package/dist/dom/components/video.js +19 -9
- package/dist/dom/components/video.js.map +2 -2
- package/dist/dom/debug.js +19 -9
- package/dist/dom/debug.js.map +2 -2
- package/dist/dom/element-data.js +19 -9
- package/dist/dom/element-data.js.map +2 -2
- package/dist/dom/events.js +28 -18
- package/dist/dom/events.js.map +3 -3
- package/dist/dom/index.js +19 -3682
- package/dist/dom/index.js.map +3 -57
- package/dist/dom/renderer.js +31 -11
- package/dist/dom/renderer.js.map +5 -5
- package/dist/hypen.js +31 -11
- package/dist/hypen.js.map +5 -5
- package/dist/index.js +19 -5810
- package/dist/index.js.map +3 -66
- package/package.json +2 -2
- package/src/canvas/events.ts +35 -8
- package/src/canvas/paint.ts +104 -4
- package/src/dom/applicators/events.ts +11 -2
- package/src/dom/components/textarea.ts +3 -3
- package/src/dom/events.ts +14 -12
- package/src/dom/renderer.ts +8 -1
package/dist/canvas/index.js
CHANGED
|
@@ -2,2155 +2,40 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
6
8
|
var __toCommonJS = (from) => {
|
|
7
|
-
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
8
10
|
if (entry)
|
|
9
11
|
return entry;
|
|
10
12
|
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
16
21
|
__moduleCache.set(from, entry);
|
|
17
22
|
return entry;
|
|
18
23
|
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
19
29
|
var __export = (target, all) => {
|
|
20
30
|
for (var name in all)
|
|
21
31
|
__defProp(target, name, {
|
|
22
32
|
get: all[name],
|
|
23
33
|
enumerable: true,
|
|
24
34
|
configurable: true,
|
|
25
|
-
set: (
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
26
36
|
});
|
|
27
37
|
};
|
|
28
38
|
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
29
|
-
|
|
30
|
-
// src/canvas/utils.ts
|
|
31
|
-
function parseSpacing(value) {
|
|
32
|
-
if (typeof value === "number") {
|
|
33
|
-
return { top: value, right: value, bottom: value, left: value };
|
|
34
|
-
}
|
|
35
|
-
if (typeof value === "string") {
|
|
36
|
-
const parts = value.split(/\s+/).map((v) => parseFloat(v) || 0);
|
|
37
|
-
if (parts.length === 1) {
|
|
38
|
-
return { top: parts[0], right: parts[0], bottom: parts[0], left: parts[0] };
|
|
39
|
-
}
|
|
40
|
-
if (parts.length === 2) {
|
|
41
|
-
return { top: parts[0], right: parts[1], bottom: parts[0], left: parts[1] };
|
|
42
|
-
}
|
|
43
|
-
if (parts.length === 4) {
|
|
44
|
-
return { top: parts[0], right: parts[1], bottom: parts[2], left: parts[3] };
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (typeof value === "object" && value !== null) {
|
|
48
|
-
return {
|
|
49
|
-
top: parseFloat(value.top) || 0,
|
|
50
|
-
right: parseFloat(value.right) || 0,
|
|
51
|
-
bottom: parseFloat(value.bottom) || 0,
|
|
52
|
-
left: parseFloat(value.left) || 0
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
return { top: 0, right: 0, bottom: 0, left: 0 };
|
|
56
|
-
}
|
|
57
|
-
function parseSize(value) {
|
|
58
|
-
if (typeof value === "number")
|
|
59
|
-
return value;
|
|
60
|
-
if (typeof value === "string") {
|
|
61
|
-
if (value === "auto")
|
|
62
|
-
return null;
|
|
63
|
-
const num = parseFloat(value);
|
|
64
|
-
return isNaN(num) ? null : num;
|
|
65
|
-
}
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
function isPointInRect(point, rect) {
|
|
69
|
-
return point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height;
|
|
70
|
-
}
|
|
71
|
-
function isPointInRoundedRect(point, rect, radius) {
|
|
72
|
-
const { x, y, width, height } = rect;
|
|
73
|
-
if (!isPointInRect(point, rect))
|
|
74
|
-
return false;
|
|
75
|
-
if (radius <= 0)
|
|
76
|
-
return true;
|
|
77
|
-
const px = point.x;
|
|
78
|
-
const py = point.y;
|
|
79
|
-
if (px < x + radius && py < y + radius) {
|
|
80
|
-
return Math.pow(px - (x + radius), 2) + Math.pow(py - (y + radius), 2) <= Math.pow(radius, 2);
|
|
81
|
-
}
|
|
82
|
-
if (px > x + width - radius && py < y + radius) {
|
|
83
|
-
return Math.pow(px - (x + width - radius), 2) + Math.pow(py - (y + radius), 2) <= Math.pow(radius, 2);
|
|
84
|
-
}
|
|
85
|
-
if (px < x + radius && py > y + height - radius) {
|
|
86
|
-
return Math.pow(px - (x + radius), 2) + Math.pow(py - (y + height - radius), 2) <= Math.pow(radius, 2);
|
|
87
|
-
}
|
|
88
|
-
if (px > x + width - radius && py > y + height - radius) {
|
|
89
|
-
return Math.pow(px - (x + width - radius), 2) + Math.pow(py - (y + height - radius), 2) <= Math.pow(radius, 2);
|
|
90
|
-
}
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
|
-
function mergeRects(rects) {
|
|
94
|
-
if (rects.length === 0)
|
|
95
|
-
return null;
|
|
96
|
-
if (rects.length === 1)
|
|
97
|
-
return rects[0];
|
|
98
|
-
let minX = Infinity;
|
|
99
|
-
let minY = Infinity;
|
|
100
|
-
let maxX = -Infinity;
|
|
101
|
-
let maxY = -Infinity;
|
|
102
|
-
for (const rect of rects) {
|
|
103
|
-
minX = Math.min(minX, rect.x);
|
|
104
|
-
minY = Math.min(minY, rect.y);
|
|
105
|
-
maxX = Math.max(maxX, rect.x + rect.width);
|
|
106
|
-
maxY = Math.max(maxY, rect.y + rect.height);
|
|
107
|
-
}
|
|
108
|
-
return {
|
|
109
|
-
x: minX,
|
|
110
|
-
y: minY,
|
|
111
|
-
width: maxX - minX,
|
|
112
|
-
height: maxY - minY
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
function createFontString(fontSize, fontWeight, fontFamily) {
|
|
116
|
-
return `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
117
|
-
}
|
|
118
|
-
function walkTree(node, callback) {
|
|
119
|
-
callback(node);
|
|
120
|
-
for (const child of node.children) {
|
|
121
|
-
walkTree(child, callback);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
function findNodeById(root, id) {
|
|
125
|
-
if (root.id === id)
|
|
126
|
-
return root;
|
|
127
|
-
for (const child of root.children) {
|
|
128
|
-
const found = findNodeById(child, id);
|
|
129
|
-
if (found)
|
|
130
|
-
return found;
|
|
131
|
-
}
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
function getAbsoluteBounds(node) {
|
|
135
|
-
if (!node.layout)
|
|
136
|
-
return null;
|
|
137
|
-
let x = node.layout.x;
|
|
138
|
-
let y = node.layout.y;
|
|
139
|
-
let current = node.parent;
|
|
140
|
-
while (current && current.layout) {
|
|
141
|
-
x += current.layout.contentX;
|
|
142
|
-
y += current.layout.contentY;
|
|
143
|
-
current = current.parent;
|
|
144
|
-
}
|
|
145
|
-
return {
|
|
146
|
-
x,
|
|
147
|
-
y,
|
|
148
|
-
width: node.layout.width,
|
|
149
|
-
height: node.layout.height
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// src/canvas/text.ts
|
|
154
|
-
import { frameworkLoggers } from "@hypen-space/core/logger";
|
|
155
|
-
var log = frameworkLoggers.canvas;
|
|
156
|
-
var textMetricsCache = new Map;
|
|
157
|
-
function getCacheKey(text, fontStyle, maxWidth) {
|
|
158
|
-
return `${text}|${fontStyle.fontSize}|${fontStyle.fontWeight}|${fontStyle.fontFamily}|${maxWidth || "auto"}`;
|
|
159
|
-
}
|
|
160
|
-
function measureText(ctx, text, fontStyle, maxWidth) {
|
|
161
|
-
const cacheKey = getCacheKey(text, fontStyle, maxWidth);
|
|
162
|
-
const cached = textMetricsCache.get(cacheKey);
|
|
163
|
-
if (cached)
|
|
164
|
-
return cached;
|
|
165
|
-
const font = createFontString(fontStyle.fontSize, fontStyle.fontWeight, fontStyle.fontFamily);
|
|
166
|
-
ctx.save();
|
|
167
|
-
ctx.font = font;
|
|
168
|
-
const lineHeight = fontStyle.lineHeight || fontStyle.fontSize * 1.2;
|
|
169
|
-
if (!maxWidth) {
|
|
170
|
-
const metrics = ctx.measureText(text);
|
|
171
|
-
const result2 = {
|
|
172
|
-
width: metrics.width,
|
|
173
|
-
height: lineHeight,
|
|
174
|
-
lines: [text],
|
|
175
|
-
lineHeight
|
|
176
|
-
};
|
|
177
|
-
ctx.restore();
|
|
178
|
-
textMetricsCache.set(cacheKey, result2);
|
|
179
|
-
return result2;
|
|
180
|
-
}
|
|
181
|
-
const lines = wrapText(ctx, text, maxWidth);
|
|
182
|
-
const width = Math.max(...lines.map((line) => ctx.measureText(line).width));
|
|
183
|
-
const height = lines.length * lineHeight;
|
|
184
|
-
const result = {
|
|
185
|
-
width,
|
|
186
|
-
height,
|
|
187
|
-
lines,
|
|
188
|
-
lineHeight
|
|
189
|
-
};
|
|
190
|
-
ctx.restore();
|
|
191
|
-
textMetricsCache.set(cacheKey, result);
|
|
192
|
-
return result;
|
|
193
|
-
}
|
|
194
|
-
function wrapText(ctx, text, maxWidth) {
|
|
195
|
-
const lines = [];
|
|
196
|
-
const paragraphs = text.split(`
|
|
197
|
-
`);
|
|
198
|
-
for (const paragraph of paragraphs) {
|
|
199
|
-
const words = paragraph.split(" ");
|
|
200
|
-
let currentLine = "";
|
|
201
|
-
for (const word of words) {
|
|
202
|
-
const testLine = currentLine ? `${currentLine} ${word}` : word;
|
|
203
|
-
const metrics = ctx.measureText(testLine);
|
|
204
|
-
if (metrics.width > maxWidth && currentLine) {
|
|
205
|
-
lines.push(currentLine);
|
|
206
|
-
currentLine = word;
|
|
207
|
-
} else {
|
|
208
|
-
currentLine = testLine;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
if (currentLine) {
|
|
212
|
-
lines.push(currentLine);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
return lines.length > 0 ? lines : [""];
|
|
216
|
-
}
|
|
217
|
-
function renderText(ctx, text, x, y, width, height, style) {
|
|
218
|
-
const font = createFontString(style.fontSize, style.fontWeight, style.fontFamily);
|
|
219
|
-
ctx.save();
|
|
220
|
-
ctx.font = font;
|
|
221
|
-
ctx.fillStyle = style.color;
|
|
222
|
-
ctx.textBaseline = "top";
|
|
223
|
-
const metrics = measureText(ctx, text, style, width);
|
|
224
|
-
let startY = y;
|
|
225
|
-
if (style.verticalAlign === "middle") {
|
|
226
|
-
startY = y + (height - metrics.height) / 2;
|
|
227
|
-
} else if (style.verticalAlign === "bottom") {
|
|
228
|
-
startY = y + height - metrics.height;
|
|
229
|
-
}
|
|
230
|
-
for (let i = 0;i < metrics.lines.length; i++) {
|
|
231
|
-
const line = metrics.lines[i];
|
|
232
|
-
const lineY = startY + i * metrics.lineHeight;
|
|
233
|
-
let lineX = x;
|
|
234
|
-
if (style.textAlign === "center") {
|
|
235
|
-
const lineWidth = ctx.measureText(line).width;
|
|
236
|
-
lineX = x + (width - lineWidth) / 2;
|
|
237
|
-
} else if (style.textAlign === "right") {
|
|
238
|
-
const lineWidth = ctx.measureText(line).width;
|
|
239
|
-
lineX = x + width - lineWidth;
|
|
240
|
-
}
|
|
241
|
-
ctx.fillText(line, lineX, lineY);
|
|
242
|
-
}
|
|
243
|
-
ctx.restore();
|
|
244
|
-
}
|
|
245
|
-
function clearTextCache() {
|
|
246
|
-
textMetricsCache.clear();
|
|
247
|
-
}
|
|
248
|
-
async function loadFont(fontFamily, fontWeight = "normal") {
|
|
249
|
-
if (!("fonts" in document))
|
|
250
|
-
return;
|
|
251
|
-
const font = `${fontWeight} 16px ${fontFamily}`;
|
|
252
|
-
try {
|
|
253
|
-
await document.fonts.load(font);
|
|
254
|
-
} catch (error) {
|
|
255
|
-
log.warn(`Failed to load font: ${font}`, error);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// src/canvas/layout.ts
|
|
260
|
-
function computeLayout(ctx, node, availableWidth, availableHeight, x = 0, y = 0) {
|
|
261
|
-
const props = node.props;
|
|
262
|
-
let margin = parseSpacing(props.margin || 0);
|
|
263
|
-
if (props.marginTop !== undefined)
|
|
264
|
-
margin.top = parseFloat(props.marginTop) || 0;
|
|
265
|
-
if (props.marginRight !== undefined)
|
|
266
|
-
margin.right = parseFloat(props.marginRight) || 0;
|
|
267
|
-
if (props.marginBottom !== undefined)
|
|
268
|
-
margin.bottom = parseFloat(props.marginBottom) || 0;
|
|
269
|
-
if (props.marginLeft !== undefined)
|
|
270
|
-
margin.left = parseFloat(props.marginLeft) || 0;
|
|
271
|
-
let padding = parseSpacing(props.padding || 0);
|
|
272
|
-
if (props.paddingTop !== undefined)
|
|
273
|
-
padding.top = parseFloat(props.paddingTop) || 0;
|
|
274
|
-
if (props.paddingRight !== undefined)
|
|
275
|
-
padding.right = parseFloat(props.paddingRight) || 0;
|
|
276
|
-
if (props.paddingBottom !== undefined)
|
|
277
|
-
padding.bottom = parseFloat(props.paddingBottom) || 0;
|
|
278
|
-
if (props.paddingLeft !== undefined)
|
|
279
|
-
padding.left = parseFloat(props.paddingLeft) || 0;
|
|
280
|
-
const borderWidth = parseFloat(props.borderWidth) || 0;
|
|
281
|
-
const borderColor = props.borderColor || "transparent";
|
|
282
|
-
const borderRadius = parseFloat(props.borderRadius) || 0;
|
|
283
|
-
const availableAfterMargin = {
|
|
284
|
-
width: availableWidth - margin.left - margin.right,
|
|
285
|
-
height: availableHeight - margin.top - margin.bottom
|
|
286
|
-
};
|
|
287
|
-
let width = parseSize(props.width);
|
|
288
|
-
let height = parseSize(props.height);
|
|
289
|
-
const type = node.type.toLowerCase();
|
|
290
|
-
if (type === "app") {
|
|
291
|
-
if (width === null)
|
|
292
|
-
width = availableAfterMargin.width;
|
|
293
|
-
if (height === null)
|
|
294
|
-
height = availableAfterMargin.height;
|
|
295
|
-
} else if (type === "spacer") {
|
|
296
|
-
if (width === null)
|
|
297
|
-
width = availableAfterMargin.width;
|
|
298
|
-
if (height === null)
|
|
299
|
-
height = availableAfterMargin.height;
|
|
300
|
-
} else if (type === "divider" || type === "separator") {
|
|
301
|
-
const orientation = props.orientation || "horizontal";
|
|
302
|
-
const thickness = parseFloat(props.thickness) || 1;
|
|
303
|
-
if (orientation === "vertical") {
|
|
304
|
-
if (width === null)
|
|
305
|
-
width = thickness;
|
|
306
|
-
if (height === null)
|
|
307
|
-
height = availableAfterMargin.height;
|
|
308
|
-
} else {
|
|
309
|
-
if (width === null)
|
|
310
|
-
width = availableAfterMargin.width;
|
|
311
|
-
if (height === null)
|
|
312
|
-
height = thickness;
|
|
313
|
-
}
|
|
314
|
-
} else if (type === "checkbox" || type === "radio") {
|
|
315
|
-
const size = parseFloat(props.size) || 20;
|
|
316
|
-
if (width === null)
|
|
317
|
-
width = size;
|
|
318
|
-
if (height === null)
|
|
319
|
-
height = size;
|
|
320
|
-
} else if (type === "switch" || type === "toggle") {
|
|
321
|
-
if (width === null)
|
|
322
|
-
width = 44;
|
|
323
|
-
if (height === null)
|
|
324
|
-
height = 24;
|
|
325
|
-
} else if (type === "slider") {
|
|
326
|
-
if (width === null)
|
|
327
|
-
width = 200;
|
|
328
|
-
if (height === null)
|
|
329
|
-
height = 20;
|
|
330
|
-
} else if (type === "progress" || type === "progressbar") {
|
|
331
|
-
if (width === null)
|
|
332
|
-
width = 200;
|
|
333
|
-
if (height === null)
|
|
334
|
-
height = 8;
|
|
335
|
-
} else if (type === "spinner" || type === "loading") {
|
|
336
|
-
const size = parseFloat(props.size) || 24;
|
|
337
|
-
if (width === null)
|
|
338
|
-
width = size;
|
|
339
|
-
if (height === null)
|
|
340
|
-
height = size;
|
|
341
|
-
} else if (type === "badge") {
|
|
342
|
-
if (width === null)
|
|
343
|
-
width = 20;
|
|
344
|
-
if (height === null)
|
|
345
|
-
height = 20;
|
|
346
|
-
} else if (type === "avatar") {
|
|
347
|
-
const size = parseFloat(props.size) || 40;
|
|
348
|
-
if (width === null)
|
|
349
|
-
width = size;
|
|
350
|
-
if (height === null)
|
|
351
|
-
height = size;
|
|
352
|
-
} else if (type === "icon") {
|
|
353
|
-
const size = parseFloat(props.size) || 24;
|
|
354
|
-
if (width === null)
|
|
355
|
-
width = size;
|
|
356
|
-
if (height === null)
|
|
357
|
-
height = size;
|
|
358
|
-
}
|
|
359
|
-
if (node.type === "text" && node.props[0]) {
|
|
360
|
-
const text = String(node.props[0] || "");
|
|
361
|
-
const fontSize = parseFloat(props.fontSize) || 16;
|
|
362
|
-
const fontWeight = props.fontWeight || "normal";
|
|
363
|
-
const fontFamily = props.fontFamily || "system-ui, sans-serif";
|
|
364
|
-
const lineHeight = parseFloat(props.lineHeight) || fontSize * 1.2;
|
|
365
|
-
const maxWidth2 = width || availableAfterMargin.width - padding.left - padding.right;
|
|
366
|
-
const metrics = measureText(ctx, text, { fontSize, fontWeight, fontFamily, lineHeight }, maxWidth2);
|
|
367
|
-
if (!width)
|
|
368
|
-
width = metrics.width + padding.left + padding.right;
|
|
369
|
-
if (!height)
|
|
370
|
-
height = metrics.height + padding.top + padding.bottom;
|
|
371
|
-
}
|
|
372
|
-
if (width === null)
|
|
373
|
-
width = availableAfterMargin.width;
|
|
374
|
-
if (height === null)
|
|
375
|
-
height = availableAfterMargin.height;
|
|
376
|
-
const minWidth = parseSize(props.minWidth);
|
|
377
|
-
const maxWidth = parseSize(props.maxWidth);
|
|
378
|
-
const minHeight = parseSize(props.minHeight);
|
|
379
|
-
const maxHeight = parseSize(props.maxHeight);
|
|
380
|
-
if (minWidth !== null)
|
|
381
|
-
width = Math.max(width, minWidth);
|
|
382
|
-
if (maxWidth !== null)
|
|
383
|
-
width = Math.min(width, maxWidth);
|
|
384
|
-
if (minHeight !== null)
|
|
385
|
-
height = Math.max(height, minHeight);
|
|
386
|
-
if (maxHeight !== null)
|
|
387
|
-
height = Math.min(height, maxHeight);
|
|
388
|
-
const layout = {
|
|
389
|
-
x: x + margin.left,
|
|
390
|
-
y: y + margin.top,
|
|
391
|
-
width,
|
|
392
|
-
height,
|
|
393
|
-
margin,
|
|
394
|
-
padding,
|
|
395
|
-
border: {
|
|
396
|
-
width: borderWidth,
|
|
397
|
-
color: borderColor,
|
|
398
|
-
radius: borderRadius
|
|
399
|
-
},
|
|
400
|
-
contentX: padding.left + borderWidth,
|
|
401
|
-
contentY: padding.top + borderWidth,
|
|
402
|
-
contentWidth: width - padding.left - padding.right - borderWidth * 2,
|
|
403
|
-
contentHeight: height - padding.top - padding.bottom - borderWidth * 2
|
|
404
|
-
};
|
|
405
|
-
node.layout = layout;
|
|
406
|
-
if (node.children.length > 0) {
|
|
407
|
-
layoutChildren(ctx, node);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
function layoutChildren(ctx, parent) {
|
|
411
|
-
const layout = parent.layout;
|
|
412
|
-
const props = parent.props;
|
|
413
|
-
if (parent.type.toLowerCase() === "stack") {
|
|
414
|
-
layoutStackChildren(ctx, parent);
|
|
415
|
-
return;
|
|
416
|
-
}
|
|
417
|
-
const flexDirection = props.flexDirection || (parent.type === "column" ? "column" : "row");
|
|
418
|
-
const isColumn = flexDirection === "column";
|
|
419
|
-
const justifyContent = isColumn ? props.verticalAlignment || "flex-start" : props.horizontalAlignment || "flex-start";
|
|
420
|
-
const alignItems = isColumn ? props.horizontalAlignment || "flex-start" : props.verticalAlignment || "flex-start";
|
|
421
|
-
const gap = parseFloat(props.gap) || 0;
|
|
422
|
-
const availableWidth = layout.contentWidth;
|
|
423
|
-
const availableHeight = layout.contentHeight;
|
|
424
|
-
const childInfo = [];
|
|
425
|
-
let totalMainSize = 0;
|
|
426
|
-
let totalFlexGrow = 0;
|
|
427
|
-
let totalFlexShrink = 0;
|
|
428
|
-
for (const child of parent.children) {
|
|
429
|
-
const flexGrow = parseFloat(child.props.flexGrow) || parseFloat(child.props.flex) || 0;
|
|
430
|
-
const flexShrink = parseFloat(child.props.flexShrink) || 1;
|
|
431
|
-
const flexBasis = parseSize(child.props.flexBasis);
|
|
432
|
-
computeLayout(ctx, child, availableWidth, availableHeight, 0, 0);
|
|
433
|
-
const childLayout = child.layout;
|
|
434
|
-
let mainSize = isColumn ? childLayout.height : childLayout.width;
|
|
435
|
-
if (flexBasis !== null) {
|
|
436
|
-
mainSize = flexBasis;
|
|
437
|
-
if (isColumn) {
|
|
438
|
-
childLayout.height = flexBasis;
|
|
439
|
-
} else {
|
|
440
|
-
childLayout.width = flexBasis;
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
childInfo.push({
|
|
444
|
-
width: childLayout.width,
|
|
445
|
-
height: childLayout.height,
|
|
446
|
-
flexGrow,
|
|
447
|
-
flexShrink,
|
|
448
|
-
flexBasis
|
|
449
|
-
});
|
|
450
|
-
totalMainSize += mainSize;
|
|
451
|
-
totalFlexGrow += flexGrow;
|
|
452
|
-
totalFlexShrink += flexShrink;
|
|
453
|
-
}
|
|
454
|
-
const totalGap = gap * (parent.children.length - 1);
|
|
455
|
-
totalMainSize += totalGap;
|
|
456
|
-
const availableMain = isColumn ? availableHeight : availableWidth;
|
|
457
|
-
let remainingSpace = availableMain - totalMainSize;
|
|
458
|
-
if (remainingSpace > 0 && totalFlexGrow > 0) {
|
|
459
|
-
const spacePerFlex = remainingSpace / totalFlexGrow;
|
|
460
|
-
for (let i = 0;i < parent.children.length; i++) {
|
|
461
|
-
const info = childInfo[i];
|
|
462
|
-
if (info.flexGrow > 0) {
|
|
463
|
-
const extraSpace = spacePerFlex * info.flexGrow;
|
|
464
|
-
if (isColumn) {
|
|
465
|
-
info.height += extraSpace;
|
|
466
|
-
} else {
|
|
467
|
-
info.width += extraSpace;
|
|
468
|
-
}
|
|
469
|
-
totalMainSize += extraSpace;
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
remainingSpace = 0;
|
|
473
|
-
}
|
|
474
|
-
if (remainingSpace < 0 && totalFlexShrink > 0) {
|
|
475
|
-
const shrinkPerFlex = Math.abs(remainingSpace) / totalFlexShrink;
|
|
476
|
-
for (let i = 0;i < parent.children.length; i++) {
|
|
477
|
-
const info = childInfo[i];
|
|
478
|
-
if (info.flexShrink > 0) {
|
|
479
|
-
const shrinkSpace = Math.min(shrinkPerFlex * info.flexShrink, isColumn ? info.height : info.width);
|
|
480
|
-
if (isColumn) {
|
|
481
|
-
info.height = Math.max(0, info.height - shrinkSpace);
|
|
482
|
-
} else {
|
|
483
|
-
info.width = Math.max(0, info.width - shrinkSpace);
|
|
484
|
-
}
|
|
485
|
-
totalMainSize -= shrinkSpace;
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
remainingSpace = availableMain - totalMainSize;
|
|
489
|
-
}
|
|
490
|
-
let mainStart = 0;
|
|
491
|
-
let spacing = 0;
|
|
492
|
-
if (justifyContent === "center") {
|
|
493
|
-
mainStart = Math.max(0, remainingSpace / 2);
|
|
494
|
-
} else if (justifyContent === "flex-end") {
|
|
495
|
-
mainStart = Math.max(0, remainingSpace);
|
|
496
|
-
} else if (justifyContent === "space-between") {
|
|
497
|
-
spacing = remainingSpace / Math.max(1, parent.children.length - 1);
|
|
498
|
-
} else if (justifyContent === "space-around") {
|
|
499
|
-
spacing = remainingSpace / parent.children.length;
|
|
500
|
-
mainStart = spacing / 2;
|
|
501
|
-
}
|
|
502
|
-
let currentMain = mainStart;
|
|
503
|
-
for (let i = 0;i < parent.children.length; i++) {
|
|
504
|
-
const child = parent.children[i];
|
|
505
|
-
const childLayout = child.layout;
|
|
506
|
-
const info = childInfo[i];
|
|
507
|
-
childLayout.width = info.width;
|
|
508
|
-
childLayout.height = info.height;
|
|
509
|
-
let crossStart = 0;
|
|
510
|
-
const availableCross = isColumn ? availableWidth : availableHeight;
|
|
511
|
-
const childCross = isColumn ? info.width : info.height;
|
|
512
|
-
if (alignItems === "center") {
|
|
513
|
-
crossStart = (availableCross - childCross) / 2;
|
|
514
|
-
} else if (alignItems === "flex-end") {
|
|
515
|
-
crossStart = availableCross - childCross;
|
|
516
|
-
}
|
|
517
|
-
if (isColumn) {
|
|
518
|
-
childLayout.x = layout.x + layout.contentX + crossStart;
|
|
519
|
-
childLayout.y = layout.y + layout.contentY + currentMain;
|
|
520
|
-
currentMain += info.height + gap;
|
|
521
|
-
} else {
|
|
522
|
-
childLayout.x = layout.x + layout.contentX + currentMain;
|
|
523
|
-
childLayout.y = layout.y + layout.contentY + crossStart;
|
|
524
|
-
currentMain += info.width + gap;
|
|
525
|
-
}
|
|
526
|
-
if (justifyContent === "space-between" || justifyContent === "space-around") {
|
|
527
|
-
currentMain += spacing;
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
function layoutStackChildren(ctx, parent) {
|
|
532
|
-
const layout = parent.layout;
|
|
533
|
-
const props = parent.props;
|
|
534
|
-
const horizontalAlignment = props.horizontalAlignment || "flex-start";
|
|
535
|
-
const verticalAlignment = props.verticalAlignment || "flex-start";
|
|
536
|
-
const availableWidth = layout.contentWidth;
|
|
537
|
-
const availableHeight = layout.contentHeight;
|
|
538
|
-
for (const child of parent.children) {
|
|
539
|
-
computeLayout(ctx, child, availableWidth, availableHeight, 0, 0);
|
|
540
|
-
const childLayout = child.layout;
|
|
541
|
-
let x = 0;
|
|
542
|
-
let y = 0;
|
|
543
|
-
if (horizontalAlignment === "center") {
|
|
544
|
-
x = (availableWidth - childLayout.width) / 2;
|
|
545
|
-
} else if (horizontalAlignment === "flex-end") {
|
|
546
|
-
x = availableWidth - childLayout.width;
|
|
547
|
-
}
|
|
548
|
-
if (verticalAlignment === "center") {
|
|
549
|
-
y = (availableHeight - childLayout.height) / 2;
|
|
550
|
-
} else if (verticalAlignment === "flex-end") {
|
|
551
|
-
y = availableHeight - childLayout.height;
|
|
552
|
-
}
|
|
553
|
-
childLayout.x = layout.x + layout.contentX + x;
|
|
554
|
-
childLayout.y = layout.y + layout.contentY + y;
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
// src/canvas/paint.ts
|
|
559
|
-
var customPainters = new Map;
|
|
560
|
-
function registerPainter(type, painter) {
|
|
561
|
-
customPainters.set(type.toLowerCase(), painter);
|
|
562
|
-
}
|
|
563
|
-
function paintNode(ctx, node) {
|
|
564
|
-
if (!node.visible || !node.layout)
|
|
565
|
-
return;
|
|
566
|
-
ctx.save();
|
|
567
|
-
applyTransforms(ctx, node);
|
|
568
|
-
if (node.opacity < 1) {
|
|
569
|
-
ctx.globalAlpha = node.opacity;
|
|
570
|
-
}
|
|
571
|
-
const customPainter = customPainters.get(node.type.toLowerCase());
|
|
572
|
-
if (customPainter) {
|
|
573
|
-
customPainter(ctx, node);
|
|
574
|
-
ctx.restore();
|
|
575
|
-
return;
|
|
576
|
-
}
|
|
577
|
-
switch (node.type.toLowerCase()) {
|
|
578
|
-
case "column":
|
|
579
|
-
case "row":
|
|
580
|
-
case "stack":
|
|
581
|
-
paintContainer(ctx, node);
|
|
582
|
-
break;
|
|
583
|
-
case "text":
|
|
584
|
-
paintText(ctx, node);
|
|
585
|
-
break;
|
|
586
|
-
case "button":
|
|
587
|
-
paintButton(ctx, node);
|
|
588
|
-
break;
|
|
589
|
-
case "input":
|
|
590
|
-
paintInput(ctx, node);
|
|
591
|
-
break;
|
|
592
|
-
case "image":
|
|
593
|
-
paintImage(ctx, node);
|
|
594
|
-
break;
|
|
595
|
-
case "spacer":
|
|
596
|
-
break;
|
|
597
|
-
case "divider":
|
|
598
|
-
case "separator":
|
|
599
|
-
paintDivider(ctx, node);
|
|
600
|
-
break;
|
|
601
|
-
case "checkbox":
|
|
602
|
-
paintCheckbox(ctx, node);
|
|
603
|
-
break;
|
|
604
|
-
case "radio":
|
|
605
|
-
paintRadio(ctx, node);
|
|
606
|
-
break;
|
|
607
|
-
case "switch":
|
|
608
|
-
case "toggle":
|
|
609
|
-
paintSwitch(ctx, node);
|
|
610
|
-
break;
|
|
611
|
-
case "slider":
|
|
612
|
-
paintSlider(ctx, node);
|
|
613
|
-
break;
|
|
614
|
-
case "progress":
|
|
615
|
-
case "progressbar":
|
|
616
|
-
paintProgress(ctx, node);
|
|
617
|
-
break;
|
|
618
|
-
case "spinner":
|
|
619
|
-
case "loading":
|
|
620
|
-
paintSpinner(ctx, node);
|
|
621
|
-
break;
|
|
622
|
-
case "card":
|
|
623
|
-
paintCard(ctx, node);
|
|
624
|
-
break;
|
|
625
|
-
case "badge":
|
|
626
|
-
paintBadge(ctx, node);
|
|
627
|
-
break;
|
|
628
|
-
case "avatar":
|
|
629
|
-
paintAvatar(ctx, node);
|
|
630
|
-
break;
|
|
631
|
-
case "icon":
|
|
632
|
-
paintIcon(ctx, node);
|
|
633
|
-
break;
|
|
634
|
-
case "link":
|
|
635
|
-
paintLink(ctx, node);
|
|
636
|
-
break;
|
|
637
|
-
case "app":
|
|
638
|
-
case "container":
|
|
639
|
-
case "box":
|
|
640
|
-
paintContainer(ctx, node);
|
|
641
|
-
break;
|
|
642
|
-
default:
|
|
643
|
-
paintContainer(ctx, node);
|
|
644
|
-
}
|
|
645
|
-
ctx.restore();
|
|
646
|
-
for (const child of node.children) {
|
|
647
|
-
paintNode(ctx, child);
|
|
648
|
-
}
|
|
649
|
-
if (node._needsRestore) {
|
|
650
|
-
ctx.restore();
|
|
651
|
-
delete node._needsRestore;
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
function paintContainer(ctx, node) {
|
|
655
|
-
const layout = node.layout;
|
|
656
|
-
const props = node.props;
|
|
657
|
-
const x = layout.x;
|
|
658
|
-
const y = layout.y;
|
|
659
|
-
const width = layout.width;
|
|
660
|
-
const height = layout.height;
|
|
661
|
-
const radius = layout.border.radius;
|
|
662
|
-
const shadow = props.shadow || props.boxShadow;
|
|
663
|
-
if (shadow) {
|
|
664
|
-
applyShadow(ctx, shadow);
|
|
665
|
-
}
|
|
666
|
-
const backgroundColor = props.backgroundColor || props.background;
|
|
667
|
-
if (backgroundColor) {
|
|
668
|
-
if (typeof backgroundColor === "string" && backgroundColor.includes("gradient")) {
|
|
669
|
-
ctx.fillStyle = parseGradient(ctx, backgroundColor, x, y, width, height);
|
|
670
|
-
} else {
|
|
671
|
-
ctx.fillStyle = backgroundColor;
|
|
672
|
-
}
|
|
673
|
-
if (radius > 0) {
|
|
674
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
675
|
-
ctx.fill();
|
|
676
|
-
} else {
|
|
677
|
-
ctx.fillRect(x, y, width, height);
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
if (shadow) {
|
|
681
|
-
ctx.shadowColor = "transparent";
|
|
682
|
-
ctx.shadowBlur = 0;
|
|
683
|
-
ctx.shadowOffsetX = 0;
|
|
684
|
-
ctx.shadowOffsetY = 0;
|
|
685
|
-
}
|
|
686
|
-
if (layout.border.width > 0 && layout.border.color !== "transparent") {
|
|
687
|
-
ctx.strokeStyle = layout.border.color;
|
|
688
|
-
ctx.lineWidth = layout.border.width;
|
|
689
|
-
if (radius > 0) {
|
|
690
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
691
|
-
ctx.stroke();
|
|
692
|
-
} else {
|
|
693
|
-
ctx.strokeRect(x, y, width, height);
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
const overflow = props.overflow || "visible";
|
|
697
|
-
if (overflow === "hidden" || overflow === "scroll" || overflow === "auto") {
|
|
698
|
-
ctx.save();
|
|
699
|
-
ctx.beginPath();
|
|
700
|
-
if (radius > 0) {
|
|
701
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
702
|
-
} else {
|
|
703
|
-
ctx.rect(x, y, width, height);
|
|
704
|
-
}
|
|
705
|
-
ctx.clip();
|
|
706
|
-
node._needsRestore = true;
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
function paintText(ctx, node) {
|
|
710
|
-
const layout = node.layout;
|
|
711
|
-
const props = node.props;
|
|
712
|
-
let text = String(props[0] || props.text || "");
|
|
713
|
-
const color = props.color || "#000000";
|
|
714
|
-
const fontSize = parseFloat(props.fontSize) || 16;
|
|
715
|
-
const fontWeight = props.fontWeight || "normal";
|
|
716
|
-
const fontFamily = props.fontFamily || "system-ui, sans-serif";
|
|
717
|
-
const textAlign = props.textAlign || "left";
|
|
718
|
-
const lineHeight = parseFloat(props.lineHeight) || fontSize * 1.2;
|
|
719
|
-
const textDecoration = props.textDecoration || "none";
|
|
720
|
-
const textTransform = props.textTransform || "none";
|
|
721
|
-
const letterSpacing = parseFloat(props.letterSpacing) || 0;
|
|
722
|
-
if (textTransform === "uppercase") {
|
|
723
|
-
text = text.toUpperCase();
|
|
724
|
-
} else if (textTransform === "lowercase") {
|
|
725
|
-
text = text.toLowerCase();
|
|
726
|
-
} else if (textTransform === "capitalize") {
|
|
727
|
-
text = text.replace(/\b\w/g, (char) => char.toUpperCase());
|
|
728
|
-
}
|
|
729
|
-
const textShadow = props.textShadow || props.shadow;
|
|
730
|
-
if (textShadow) {
|
|
731
|
-
applyShadow(ctx, textShadow);
|
|
732
|
-
}
|
|
733
|
-
const x = layout.x + layout.contentX;
|
|
734
|
-
const y = layout.y + layout.contentY;
|
|
735
|
-
if (letterSpacing !== 0) {
|
|
736
|
-
ctx.fillStyle = color;
|
|
737
|
-
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
738
|
-
ctx.textAlign = "left";
|
|
739
|
-
ctx.textBaseline = "top";
|
|
740
|
-
let currentX = x;
|
|
741
|
-
for (let i = 0;i < text.length; i++) {
|
|
742
|
-
ctx.fillText(text[i], currentX, y);
|
|
743
|
-
currentX += ctx.measureText(text[i]).width;
|
|
744
|
-
if (i < text.length - 1) {
|
|
745
|
-
currentX += letterSpacing;
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
if (textDecoration !== "none") {
|
|
749
|
-
applyTextDecoration(ctx, textDecoration, color, x, y, currentX - x, fontSize);
|
|
750
|
-
}
|
|
751
|
-
} else {
|
|
752
|
-
renderText(ctx, text, x, y, layout.contentWidth, layout.contentHeight, {
|
|
753
|
-
color,
|
|
754
|
-
fontSize,
|
|
755
|
-
fontWeight,
|
|
756
|
-
fontFamily,
|
|
757
|
-
textAlign,
|
|
758
|
-
verticalAlign: "top",
|
|
759
|
-
lineHeight
|
|
760
|
-
});
|
|
761
|
-
if (textDecoration !== "none") {
|
|
762
|
-
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
763
|
-
const textWidth = ctx.measureText(text).width;
|
|
764
|
-
applyTextDecoration(ctx, textDecoration, color, x, y, textWidth, fontSize);
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
if (textShadow) {
|
|
768
|
-
ctx.shadowColor = "transparent";
|
|
769
|
-
ctx.shadowBlur = 0;
|
|
770
|
-
ctx.shadowOffsetX = 0;
|
|
771
|
-
ctx.shadowOffsetY = 0;
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
|
-
function paintButton(ctx, node) {
|
|
775
|
-
const layout = node.layout;
|
|
776
|
-
const props = node.props;
|
|
777
|
-
const x = layout.x;
|
|
778
|
-
const y = layout.y;
|
|
779
|
-
const width = layout.width;
|
|
780
|
-
const height = layout.height;
|
|
781
|
-
const radius = layout.border.radius || 4;
|
|
782
|
-
const shadow = props.shadow || props.boxShadow;
|
|
783
|
-
if (shadow) {
|
|
784
|
-
applyShadow(ctx, shadow);
|
|
785
|
-
}
|
|
786
|
-
let backgroundColor = props.backgroundColor || "#007bff";
|
|
787
|
-
if (node.hovered) {
|
|
788
|
-
backgroundColor = props.hoverColor || "#0056b3";
|
|
789
|
-
}
|
|
790
|
-
if (node.focused) {
|
|
791
|
-
backgroundColor = props.focusColor || "#004085";
|
|
792
|
-
}
|
|
793
|
-
if (typeof backgroundColor === "string" && backgroundColor.includes("gradient")) {
|
|
794
|
-
ctx.fillStyle = parseGradient(ctx, backgroundColor, x, y, width, height);
|
|
795
|
-
} else {
|
|
796
|
-
ctx.fillStyle = backgroundColor;
|
|
797
|
-
}
|
|
798
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
799
|
-
ctx.fill();
|
|
800
|
-
if (shadow) {
|
|
801
|
-
ctx.shadowColor = "transparent";
|
|
802
|
-
ctx.shadowBlur = 0;
|
|
803
|
-
ctx.shadowOffsetX = 0;
|
|
804
|
-
ctx.shadowOffsetY = 0;
|
|
805
|
-
}
|
|
806
|
-
if (layout.border.width > 0) {
|
|
807
|
-
ctx.strokeStyle = layout.border.color;
|
|
808
|
-
ctx.lineWidth = layout.border.width;
|
|
809
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
810
|
-
ctx.stroke();
|
|
811
|
-
}
|
|
812
|
-
for (const child of node.children) {
|
|
813
|
-
paintNode(ctx, child);
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
function paintInput(ctx, node) {
|
|
817
|
-
const layout = node.layout;
|
|
818
|
-
const props = node.props;
|
|
819
|
-
const x = layout.x;
|
|
820
|
-
const y = layout.y;
|
|
821
|
-
const width = layout.width;
|
|
822
|
-
const height = layout.height;
|
|
823
|
-
const radius = layout.border.radius || 4;
|
|
824
|
-
ctx.fillStyle = props.backgroundColor || "#ffffff";
|
|
825
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
826
|
-
ctx.fill();
|
|
827
|
-
const borderColor = node.focused ? "#007bff" : layout.border.color || "#cccccc";
|
|
828
|
-
const borderWidth = node.focused ? 2 : layout.border.width || 1;
|
|
829
|
-
ctx.strokeStyle = borderColor;
|
|
830
|
-
ctx.lineWidth = borderWidth;
|
|
831
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
832
|
-
ctx.stroke();
|
|
833
|
-
const value = props.value || "";
|
|
834
|
-
const placeholder = props.placeholder || "";
|
|
835
|
-
const text = value || placeholder;
|
|
836
|
-
const textColor = value ? props.color || "#000000" : "#999999";
|
|
837
|
-
if (text) {
|
|
838
|
-
const fontSize = parseFloat(props.fontSize) || 16;
|
|
839
|
-
const fontWeight = props.fontWeight || "normal";
|
|
840
|
-
const fontFamily = props.fontFamily || "system-ui, sans-serif";
|
|
841
|
-
const lineHeight = parseFloat(props.lineHeight) || fontSize * 1.2;
|
|
842
|
-
renderText(ctx, text, layout.x + layout.contentX, layout.y + layout.contentY, layout.contentWidth, layout.contentHeight, {
|
|
843
|
-
color: textColor,
|
|
844
|
-
fontSize,
|
|
845
|
-
fontWeight,
|
|
846
|
-
fontFamily,
|
|
847
|
-
textAlign: "left",
|
|
848
|
-
verticalAlign: "middle",
|
|
849
|
-
lineHeight
|
|
850
|
-
});
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
function paintImage(ctx, node) {
|
|
854
|
-
const layout = node.layout;
|
|
855
|
-
const props = node.props;
|
|
856
|
-
const src = props.src || props[0];
|
|
857
|
-
if (!src)
|
|
858
|
-
return;
|
|
859
|
-
const x = layout.x;
|
|
860
|
-
const y = layout.y;
|
|
861
|
-
const width = layout.width;
|
|
862
|
-
const height = layout.height;
|
|
863
|
-
ctx.fillStyle = "#e0e0e0";
|
|
864
|
-
ctx.fillRect(x, y, width, height);
|
|
865
|
-
ctx.strokeStyle = "#999999";
|
|
866
|
-
ctx.lineWidth = 1;
|
|
867
|
-
ctx.strokeRect(x, y, width, height);
|
|
868
|
-
ctx.fillStyle = "#666666";
|
|
869
|
-
ctx.font = "14px sans-serif";
|
|
870
|
-
ctx.textAlign = "center";
|
|
871
|
-
ctx.textBaseline = "middle";
|
|
872
|
-
ctx.fillText("IMG", x + width / 2, y + height / 2);
|
|
873
|
-
}
|
|
874
|
-
function drawRoundedRect(ctx, x, y, width, height, radius) {
|
|
875
|
-
if (radius <= 0) {
|
|
876
|
-
ctx.rect(x, y, width, height);
|
|
877
|
-
return;
|
|
878
|
-
}
|
|
879
|
-
ctx.beginPath();
|
|
880
|
-
ctx.moveTo(x + radius, y);
|
|
881
|
-
ctx.lineTo(x + width - radius, y);
|
|
882
|
-
ctx.arcTo(x + width, y, x + width, y + radius, radius);
|
|
883
|
-
ctx.lineTo(x + width, y + height - radius);
|
|
884
|
-
ctx.arcTo(x + width, y + height, x + width - radius, y + height, radius);
|
|
885
|
-
ctx.lineTo(x + radius, y + height);
|
|
886
|
-
ctx.arcTo(x, y + height, x, y + height - radius, radius);
|
|
887
|
-
ctx.lineTo(x, y + radius);
|
|
888
|
-
ctx.arcTo(x, y, x + radius, y, radius);
|
|
889
|
-
ctx.closePath();
|
|
890
|
-
}
|
|
891
|
-
function applyShadow(ctx, shadow) {
|
|
892
|
-
if (typeof shadow === "string") {
|
|
893
|
-
const parts = shadow.trim().split(/\s+/);
|
|
894
|
-
if (parts.length >= 3) {
|
|
895
|
-
const offsetX = parseFloat(parts[0]);
|
|
896
|
-
const offsetY = parseFloat(parts[1]);
|
|
897
|
-
const blur = parseFloat(parts[2]);
|
|
898
|
-
const color = parts.slice(3).join(" ") || "rgba(0,0,0,0.3)";
|
|
899
|
-
ctx.shadowOffsetX = offsetX;
|
|
900
|
-
ctx.shadowOffsetY = offsetY;
|
|
901
|
-
ctx.shadowBlur = blur;
|
|
902
|
-
ctx.shadowColor = color;
|
|
903
|
-
}
|
|
904
|
-
} else if (typeof shadow === "object") {
|
|
905
|
-
ctx.shadowOffsetX = shadow.offsetX || 0;
|
|
906
|
-
ctx.shadowOffsetY = shadow.offsetY || 0;
|
|
907
|
-
ctx.shadowBlur = shadow.blur || 0;
|
|
908
|
-
ctx.shadowColor = shadow.color || "rgba(0,0,0,0.3)";
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
function parseGradient(ctx, gradientStr, x, y, width, height) {
|
|
912
|
-
if (gradientStr.startsWith("linear-gradient")) {
|
|
913
|
-
const match = gradientStr.match(/linear-gradient\((.*)\)/);
|
|
914
|
-
if (!match)
|
|
915
|
-
return gradientStr;
|
|
916
|
-
const parts = match[1].split(",").map((s) => s.trim());
|
|
917
|
-
let x0 = x, y0 = y, x1 = x, y1 = y + height;
|
|
918
|
-
let colorStart = 0;
|
|
919
|
-
if (parts[0].includes("deg") || parts[0].includes("to ")) {
|
|
920
|
-
colorStart = 1;
|
|
921
|
-
const direction = parts[0];
|
|
922
|
-
if (direction.includes("to right") || direction === "90deg") {
|
|
923
|
-
x1 = x + width;
|
|
924
|
-
y1 = y;
|
|
925
|
-
} else if (direction.includes("to left") || direction === "270deg") {
|
|
926
|
-
x0 = x + width;
|
|
927
|
-
x1 = x;
|
|
928
|
-
y0 = y;
|
|
929
|
-
y1 = y;
|
|
930
|
-
} else if (direction.includes("to top") || direction === "0deg") {
|
|
931
|
-
y0 = y + height;
|
|
932
|
-
y1 = y;
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
const gradient = ctx.createLinearGradient(x0, y0, x1, y1);
|
|
936
|
-
const colors = parts.slice(colorStart);
|
|
937
|
-
colors.forEach((color, i) => {
|
|
938
|
-
const stop = i / (colors.length - 1);
|
|
939
|
-
gradient.addColorStop(stop, color.trim());
|
|
940
|
-
});
|
|
941
|
-
return gradient;
|
|
942
|
-
} else if (gradientStr.startsWith("radial-gradient")) {
|
|
943
|
-
const match = gradientStr.match(/radial-gradient\((.*)\)/);
|
|
944
|
-
if (!match)
|
|
945
|
-
return gradientStr;
|
|
946
|
-
const parts = match[1].split(",").map((s) => s.trim());
|
|
947
|
-
const centerX = x + width / 2;
|
|
948
|
-
const centerY = y + height / 2;
|
|
949
|
-
const radius = Math.max(width, height) / 2;
|
|
950
|
-
const gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, radius);
|
|
951
|
-
parts.forEach((color, i) => {
|
|
952
|
-
const stop = i / (parts.length - 1);
|
|
953
|
-
gradient.addColorStop(stop, color.trim());
|
|
954
|
-
});
|
|
955
|
-
return gradient;
|
|
956
|
-
}
|
|
957
|
-
return gradientStr;
|
|
958
|
-
}
|
|
959
|
-
function applyTransforms(ctx, node) {
|
|
960
|
-
const props = node.props;
|
|
961
|
-
const layout = node.layout;
|
|
962
|
-
const originX = parseFloat(props.transformOriginX) || 0.5;
|
|
963
|
-
const originY = parseFloat(props.transformOriginY) || 0.5;
|
|
964
|
-
const centerX = layout.x + layout.width * originX;
|
|
965
|
-
const centerY = layout.y + layout.height * originY;
|
|
966
|
-
const translateX = parseFloat(props.translateX) || 0;
|
|
967
|
-
const translateY = parseFloat(props.translateY) || 0;
|
|
968
|
-
const rotate = parseFloat(props.rotate) || 0;
|
|
969
|
-
const scaleX = parseFloat(props.scaleX) || parseFloat(props.scale) || 1;
|
|
970
|
-
const scaleY = parseFloat(props.scaleY) || parseFloat(props.scale) || 1;
|
|
971
|
-
const skewX = parseFloat(props.skewX) || parseFloat(props.skew) || 0;
|
|
972
|
-
const skewY = parseFloat(props.skewY) || 0;
|
|
973
|
-
if (translateX !== 0 || translateY !== 0 || rotate !== 0 || scaleX !== 1 || scaleY !== 1 || skewX !== 0 || skewY !== 0) {
|
|
974
|
-
ctx.translate(centerX, centerY);
|
|
975
|
-
if (scaleX !== 1 || scaleY !== 1) {
|
|
976
|
-
ctx.scale(scaleX, scaleY);
|
|
977
|
-
}
|
|
978
|
-
if (rotate !== 0) {
|
|
979
|
-
ctx.rotate(rotate * Math.PI / 180);
|
|
980
|
-
}
|
|
981
|
-
if (skewX !== 0 || skewY !== 0) {
|
|
982
|
-
const skewXRad = skewX * Math.PI / 180;
|
|
983
|
-
const skewYRad = skewY * Math.PI / 180;
|
|
984
|
-
ctx.transform(1, Math.tan(skewYRad), Math.tan(skewXRad), 1, 0, 0);
|
|
985
|
-
}
|
|
986
|
-
ctx.translate(-centerX + translateX, -centerY + translateY);
|
|
987
|
-
}
|
|
988
|
-
if (props.transform && typeof props.transform === "string") {
|
|
989
|
-
parseTransformString(ctx, props.transform, centerX, centerY);
|
|
990
|
-
}
|
|
991
|
-
}
|
|
992
|
-
function parseTransformString(ctx, transformStr, originX, originY) {
|
|
993
|
-
const transforms = transformStr.match(/(\w+)\(([^)]+)\)/g);
|
|
994
|
-
if (!transforms)
|
|
995
|
-
return;
|
|
996
|
-
ctx.translate(originX, originY);
|
|
997
|
-
for (const transform of transforms) {
|
|
998
|
-
const match = transform.match(/(\w+)\(([^)]+)\)/);
|
|
999
|
-
if (!match)
|
|
1000
|
-
continue;
|
|
1001
|
-
const [, func, args] = match;
|
|
1002
|
-
const values = args.split(",").map((v) => parseFloat(v.trim()));
|
|
1003
|
-
switch (func.toLowerCase()) {
|
|
1004
|
-
case "translate":
|
|
1005
|
-
ctx.translate(values[0] || 0, values[1] || 0);
|
|
1006
|
-
break;
|
|
1007
|
-
case "rotate":
|
|
1008
|
-
ctx.rotate(values[0] * Math.PI / 180);
|
|
1009
|
-
break;
|
|
1010
|
-
case "scale":
|
|
1011
|
-
ctx.scale(values[0] || 1, values[1] || values[0] || 1);
|
|
1012
|
-
break;
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
ctx.translate(-originX, -originY);
|
|
1016
|
-
}
|
|
1017
|
-
function paintDivider(ctx, node) {
|
|
1018
|
-
const layout = node.layout;
|
|
1019
|
-
const props = node.props;
|
|
1020
|
-
const orientation = props.orientation || "horizontal";
|
|
1021
|
-
const color = props.color || props.backgroundColor || "#e0e0e0";
|
|
1022
|
-
const thickness = parseFloat(props.thickness) || 1;
|
|
1023
|
-
ctx.strokeStyle = color;
|
|
1024
|
-
ctx.lineWidth = thickness;
|
|
1025
|
-
ctx.beginPath();
|
|
1026
|
-
if (orientation === "vertical") {
|
|
1027
|
-
const x = layout.x + layout.width / 2;
|
|
1028
|
-
ctx.moveTo(x, layout.y);
|
|
1029
|
-
ctx.lineTo(x, layout.y + layout.height);
|
|
1030
|
-
} else {
|
|
1031
|
-
const y = layout.y + layout.height / 2;
|
|
1032
|
-
ctx.moveTo(layout.x, y);
|
|
1033
|
-
ctx.lineTo(layout.x + layout.width, y);
|
|
1034
|
-
}
|
|
1035
|
-
ctx.stroke();
|
|
1036
|
-
}
|
|
1037
|
-
function paintCheckbox(ctx, node) {
|
|
1038
|
-
const layout = node.layout;
|
|
1039
|
-
const props = node.props;
|
|
1040
|
-
const size = Math.min(layout.width, layout.height);
|
|
1041
|
-
const x = layout.x;
|
|
1042
|
-
const y = layout.y;
|
|
1043
|
-
const checkedValue = props.checked !== undefined ? props.checked : props.value;
|
|
1044
|
-
const checked = checkedValue === true || checkedValue === "true" || checkedValue !== false && checkedValue !== "false" && !!checkedValue;
|
|
1045
|
-
const radius = parseFloat(props.borderRadius) || 2;
|
|
1046
|
-
const bgColor = checked ? props.checkedColor || "#007bff" : props.backgroundColor || "#ffffff";
|
|
1047
|
-
ctx.fillStyle = bgColor;
|
|
1048
|
-
drawRoundedRect(ctx, x, y, size, size, radius);
|
|
1049
|
-
ctx.fill();
|
|
1050
|
-
const borderColor = checked ? props.checkedColor || "#007bff" : props.borderColor || "#cccccc";
|
|
1051
|
-
ctx.strokeStyle = borderColor;
|
|
1052
|
-
ctx.lineWidth = node.focused ? 2 : 1;
|
|
1053
|
-
drawRoundedRect(ctx, x, y, size, size, radius);
|
|
1054
|
-
ctx.stroke();
|
|
1055
|
-
if (checked) {
|
|
1056
|
-
ctx.strokeStyle = props.checkColor || "#ffffff";
|
|
1057
|
-
ctx.lineWidth = 2;
|
|
1058
|
-
ctx.lineCap = "round";
|
|
1059
|
-
ctx.lineJoin = "round";
|
|
1060
|
-
const padding = size * 0.25;
|
|
1061
|
-
ctx.beginPath();
|
|
1062
|
-
ctx.moveTo(x + padding, y + size / 2);
|
|
1063
|
-
ctx.lineTo(x + size * 0.4, y + size - padding);
|
|
1064
|
-
ctx.lineTo(x + size - padding, y + padding);
|
|
1065
|
-
ctx.stroke();
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
function paintRadio(ctx, node) {
|
|
1069
|
-
const layout = node.layout;
|
|
1070
|
-
const props = node.props;
|
|
1071
|
-
const size = Math.min(layout.width, layout.height);
|
|
1072
|
-
const centerX = layout.x + size / 2;
|
|
1073
|
-
const centerY = layout.y + size / 2;
|
|
1074
|
-
const radius = size / 2;
|
|
1075
|
-
const checkedValue = props.checked !== undefined ? props.checked : props.value;
|
|
1076
|
-
const checked = checkedValue === true || checkedValue === "true" || checkedValue !== false && checkedValue !== "false" && !!checkedValue;
|
|
1077
|
-
const bgColor = props.backgroundColor || "#ffffff";
|
|
1078
|
-
ctx.fillStyle = bgColor;
|
|
1079
|
-
ctx.beginPath();
|
|
1080
|
-
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
1081
|
-
ctx.fill();
|
|
1082
|
-
const borderColor = checked ? props.checkedColor || "#007bff" : props.borderColor || "#cccccc";
|
|
1083
|
-
ctx.strokeStyle = borderColor;
|
|
1084
|
-
ctx.lineWidth = node.focused ? 2 : 1;
|
|
1085
|
-
ctx.beginPath();
|
|
1086
|
-
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
1087
|
-
ctx.stroke();
|
|
1088
|
-
if (checked) {
|
|
1089
|
-
ctx.fillStyle = props.checkedColor || "#007bff";
|
|
1090
|
-
ctx.beginPath();
|
|
1091
|
-
ctx.arc(centerX, centerY, radius * 0.5, 0, Math.PI * 2);
|
|
1092
|
-
ctx.fill();
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
function paintSwitch(ctx, node) {
|
|
1096
|
-
const layout = node.layout;
|
|
1097
|
-
const props = node.props;
|
|
1098
|
-
const width = layout.width;
|
|
1099
|
-
const height = layout.height;
|
|
1100
|
-
const x = layout.x;
|
|
1101
|
-
const y = layout.y;
|
|
1102
|
-
const checkedValue = props.checked !== undefined ? props.checked : props.value;
|
|
1103
|
-
const checked = checkedValue === true || checkedValue === "true" || checkedValue !== false && checkedValue !== "false" && !!checkedValue;
|
|
1104
|
-
const radius = height / 2;
|
|
1105
|
-
const trackColor = checked ? props.checkedColor || "#4caf50" : props.backgroundColor || "#cccccc";
|
|
1106
|
-
ctx.fillStyle = trackColor;
|
|
1107
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
1108
|
-
ctx.fill();
|
|
1109
|
-
const thumbRadius = radius * 0.8;
|
|
1110
|
-
const thumbX = checked ? x + width - radius : x + radius;
|
|
1111
|
-
const thumbY = y + radius;
|
|
1112
|
-
ctx.fillStyle = props.thumbColor || "#ffffff";
|
|
1113
|
-
ctx.beginPath();
|
|
1114
|
-
ctx.arc(thumbX, thumbY, thumbRadius, 0, Math.PI * 2);
|
|
1115
|
-
ctx.fill();
|
|
1116
|
-
if (props.shadow !== false) {
|
|
1117
|
-
ctx.shadowColor = "rgba(0,0,0,0.2)";
|
|
1118
|
-
ctx.shadowBlur = 2;
|
|
1119
|
-
ctx.shadowOffsetY = 1;
|
|
1120
|
-
ctx.beginPath();
|
|
1121
|
-
ctx.arc(thumbX, thumbY, thumbRadius, 0, Math.PI * 2);
|
|
1122
|
-
ctx.fill();
|
|
1123
|
-
ctx.shadowColor = "transparent";
|
|
1124
|
-
ctx.shadowBlur = 0;
|
|
1125
|
-
ctx.shadowOffsetY = 0;
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
function paintSlider(ctx, node) {
|
|
1129
|
-
const layout = node.layout;
|
|
1130
|
-
const props = node.props;
|
|
1131
|
-
const width = layout.width;
|
|
1132
|
-
const height = layout.height;
|
|
1133
|
-
const x = layout.x;
|
|
1134
|
-
const y = layout.y;
|
|
1135
|
-
const min = parseFloat(props.min) || 0;
|
|
1136
|
-
const max = parseFloat(props.max) || 100;
|
|
1137
|
-
const value = parseFloat(props.value) || min;
|
|
1138
|
-
const percentage = (value - min) / (max - min);
|
|
1139
|
-
const trackHeight = parseFloat(props.trackHeight) || 4;
|
|
1140
|
-
const thumbSize = parseFloat(props.thumbSize) || 16;
|
|
1141
|
-
const trackY = y + (height - trackHeight) / 2;
|
|
1142
|
-
ctx.fillStyle = props.trackColor || "#e0e0e0";
|
|
1143
|
-
drawRoundedRect(ctx, x, trackY, width, trackHeight, trackHeight / 2);
|
|
1144
|
-
ctx.fill();
|
|
1145
|
-
const fillWidth = width * percentage;
|
|
1146
|
-
const fillColor = props.fillColor || props.color || "#007bff";
|
|
1147
|
-
if (typeof fillColor === "string" && fillColor.includes("gradient")) {
|
|
1148
|
-
ctx.fillStyle = parseGradient(ctx, fillColor, x, trackY, fillWidth, trackHeight);
|
|
1149
|
-
} else {
|
|
1150
|
-
ctx.fillStyle = fillColor;
|
|
1151
|
-
}
|
|
1152
|
-
drawRoundedRect(ctx, x, trackY, fillWidth, trackHeight, trackHeight / 2);
|
|
1153
|
-
ctx.fill();
|
|
1154
|
-
const thumbX = x + fillWidth;
|
|
1155
|
-
const thumbY = y + height / 2;
|
|
1156
|
-
ctx.fillStyle = props.thumbColor || "#007bff";
|
|
1157
|
-
ctx.beginPath();
|
|
1158
|
-
ctx.arc(thumbX, thumbY, thumbSize / 2, 0, Math.PI * 2);
|
|
1159
|
-
ctx.fill();
|
|
1160
|
-
ctx.strokeStyle = "#ffffff";
|
|
1161
|
-
ctx.lineWidth = 2;
|
|
1162
|
-
ctx.beginPath();
|
|
1163
|
-
ctx.arc(thumbX, thumbY, thumbSize / 2, 0, Math.PI * 2);
|
|
1164
|
-
ctx.stroke();
|
|
1165
|
-
}
|
|
1166
|
-
function paintProgress(ctx, node) {
|
|
1167
|
-
const layout = node.layout;
|
|
1168
|
-
const props = node.props;
|
|
1169
|
-
const width = layout.width;
|
|
1170
|
-
const height = layout.height;
|
|
1171
|
-
const x = layout.x;
|
|
1172
|
-
const y = layout.y;
|
|
1173
|
-
const min = parseFloat(props.min) || 0;
|
|
1174
|
-
const max = parseFloat(props.max) || 100;
|
|
1175
|
-
const value = parseFloat(props.value) || 0;
|
|
1176
|
-
const percentage = Math.min(Math.max((value - min) / (max - min), 0), 1);
|
|
1177
|
-
const radius = layout.border.radius || height / 2;
|
|
1178
|
-
ctx.fillStyle = props.backgroundColor || "#e0e0e0";
|
|
1179
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
1180
|
-
ctx.fill();
|
|
1181
|
-
if (percentage > 0) {
|
|
1182
|
-
const fillWidth = width * percentage;
|
|
1183
|
-
const fillColor = props.fillColor || props.color || "#007bff";
|
|
1184
|
-
if (typeof fillColor === "string" && fillColor.includes("gradient")) {
|
|
1185
|
-
ctx.fillStyle = parseGradient(ctx, fillColor, x, y, fillWidth, height);
|
|
1186
|
-
} else {
|
|
1187
|
-
ctx.fillStyle = fillColor;
|
|
1188
|
-
}
|
|
1189
|
-
drawRoundedRect(ctx, x, y, fillWidth, height, radius);
|
|
1190
|
-
ctx.fill();
|
|
1191
|
-
}
|
|
1192
|
-
if (props.showLabel) {
|
|
1193
|
-
const label = props.label || `${Math.round(percentage * 100)}%`;
|
|
1194
|
-
ctx.fillStyle = props.labelColor || "#ffffff";
|
|
1195
|
-
ctx.font = `${props.fontSize || 12}px ${props.fontFamily || "sans-serif"}`;
|
|
1196
|
-
ctx.textAlign = "center";
|
|
1197
|
-
ctx.textBaseline = "middle";
|
|
1198
|
-
ctx.fillText(label, x + width / 2, y + height / 2);
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
function paintSpinner(ctx, node) {
|
|
1202
|
-
const layout = node.layout;
|
|
1203
|
-
const props = node.props;
|
|
1204
|
-
const size = Math.min(layout.width, layout.height);
|
|
1205
|
-
const centerX = layout.x + size / 2;
|
|
1206
|
-
const centerY = layout.y + size / 2;
|
|
1207
|
-
const radius = size / 2 - 4;
|
|
1208
|
-
const thickness = parseFloat(props.thickness) || 4;
|
|
1209
|
-
const color = props.color || "#007bff";
|
|
1210
|
-
const rotation = Date.now() / 1000 * Math.PI;
|
|
1211
|
-
ctx.strokeStyle = color;
|
|
1212
|
-
ctx.lineWidth = thickness;
|
|
1213
|
-
ctx.lineCap = "round";
|
|
1214
|
-
ctx.beginPath();
|
|
1215
|
-
ctx.arc(centerX, centerY, radius, rotation, rotation + Math.PI * 1.5);
|
|
1216
|
-
ctx.stroke();
|
|
1217
|
-
ctx.globalAlpha = 0.3;
|
|
1218
|
-
ctx.beginPath();
|
|
1219
|
-
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
1220
|
-
ctx.stroke();
|
|
1221
|
-
ctx.globalAlpha = 1;
|
|
1222
|
-
}
|
|
1223
|
-
function paintCard(ctx, node) {
|
|
1224
|
-
const layout = node.layout;
|
|
1225
|
-
const props = node.props;
|
|
1226
|
-
const x = layout.x;
|
|
1227
|
-
const y = layout.y;
|
|
1228
|
-
const width = layout.width;
|
|
1229
|
-
const height = layout.height;
|
|
1230
|
-
const radius = layout.border.radius || 8;
|
|
1231
|
-
const shadow = props.shadow || props.boxShadow || "0 2 8 rgba(0,0,0,0.1)";
|
|
1232
|
-
applyShadow(ctx, shadow);
|
|
1233
|
-
const backgroundColor = props.backgroundColor || "#ffffff";
|
|
1234
|
-
if (typeof backgroundColor === "string" && backgroundColor.includes("gradient")) {
|
|
1235
|
-
ctx.fillStyle = parseGradient(ctx, backgroundColor, x, y, width, height);
|
|
1236
|
-
} else {
|
|
1237
|
-
ctx.fillStyle = backgroundColor;
|
|
1238
|
-
}
|
|
1239
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
1240
|
-
ctx.fill();
|
|
1241
|
-
ctx.shadowColor = "transparent";
|
|
1242
|
-
ctx.shadowBlur = 0;
|
|
1243
|
-
ctx.shadowOffsetX = 0;
|
|
1244
|
-
ctx.shadowOffsetY = 0;
|
|
1245
|
-
if (layout.border.width > 0) {
|
|
1246
|
-
ctx.strokeStyle = layout.border.color;
|
|
1247
|
-
ctx.lineWidth = layout.border.width;
|
|
1248
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
1249
|
-
ctx.stroke();
|
|
1250
|
-
}
|
|
1251
|
-
for (const child of node.children) {
|
|
1252
|
-
paintNode(ctx, child);
|
|
1253
|
-
}
|
|
1254
|
-
}
|
|
1255
|
-
function paintBadge(ctx, node) {
|
|
1256
|
-
const layout = node.layout;
|
|
1257
|
-
const props = node.props;
|
|
1258
|
-
const x = layout.x;
|
|
1259
|
-
const y = layout.y;
|
|
1260
|
-
const width = layout.width;
|
|
1261
|
-
const height = layout.height;
|
|
1262
|
-
const radius = layout.border.radius || height / 2;
|
|
1263
|
-
const backgroundColor = props.backgroundColor || "#dc3545";
|
|
1264
|
-
ctx.fillStyle = backgroundColor;
|
|
1265
|
-
drawRoundedRect(ctx, x, y, width, height, radius);
|
|
1266
|
-
ctx.fill();
|
|
1267
|
-
const text = String(props[0] || props.text || "");
|
|
1268
|
-
if (text) {
|
|
1269
|
-
ctx.fillStyle = props.color || "#ffffff";
|
|
1270
|
-
ctx.font = `${props.fontWeight || "bold"} ${props.fontSize || 10}px ${props.fontFamily || "sans-serif"}`;
|
|
1271
|
-
ctx.textAlign = "center";
|
|
1272
|
-
ctx.textBaseline = "middle";
|
|
1273
|
-
ctx.fillText(text, x + width / 2, y + height / 2);
|
|
1274
|
-
}
|
|
1275
|
-
}
|
|
1276
|
-
function paintAvatar(ctx, node) {
|
|
1277
|
-
const layout = node.layout;
|
|
1278
|
-
const props = node.props;
|
|
1279
|
-
const size = Math.min(layout.width, layout.height);
|
|
1280
|
-
const centerX = layout.x + size / 2;
|
|
1281
|
-
const centerY = layout.y + size / 2;
|
|
1282
|
-
const radius = size / 2;
|
|
1283
|
-
ctx.save();
|
|
1284
|
-
ctx.beginPath();
|
|
1285
|
-
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
1286
|
-
ctx.clip();
|
|
1287
|
-
const backgroundColor = props.backgroundColor || "#cccccc";
|
|
1288
|
-
ctx.fillStyle = backgroundColor;
|
|
1289
|
-
ctx.beginPath();
|
|
1290
|
-
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
1291
|
-
ctx.fill();
|
|
1292
|
-
const text = String(props[0] || props.text || props.initials || "");
|
|
1293
|
-
if (text) {
|
|
1294
|
-
ctx.fillStyle = props.color || "#ffffff";
|
|
1295
|
-
ctx.font = `${props.fontWeight || "bold"} ${props.fontSize || size / 2.5}px ${props.fontFamily || "sans-serif"}`;
|
|
1296
|
-
ctx.textAlign = "center";
|
|
1297
|
-
ctx.textBaseline = "middle";
|
|
1298
|
-
ctx.fillText(text, centerX, centerY);
|
|
1299
|
-
}
|
|
1300
|
-
ctx.restore();
|
|
1301
|
-
if (layout.border.width > 0) {
|
|
1302
|
-
ctx.strokeStyle = layout.border.color;
|
|
1303
|
-
ctx.lineWidth = layout.border.width;
|
|
1304
|
-
ctx.beginPath();
|
|
1305
|
-
ctx.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
1306
|
-
ctx.stroke();
|
|
1307
|
-
}
|
|
1308
|
-
}
|
|
1309
|
-
function paintIcon(ctx, node) {
|
|
1310
|
-
const layout = node.layout;
|
|
1311
|
-
const props = node.props;
|
|
1312
|
-
const size = Math.min(layout.width, layout.height);
|
|
1313
|
-
const x = layout.x;
|
|
1314
|
-
const y = layout.y;
|
|
1315
|
-
const color = props.color || "#000000";
|
|
1316
|
-
const iconName = props.icon || props.name || "circle";
|
|
1317
|
-
ctx.fillStyle = color;
|
|
1318
|
-
ctx.strokeStyle = color;
|
|
1319
|
-
ctx.lineWidth = 2;
|
|
1320
|
-
switch (iconName.toLowerCase()) {
|
|
1321
|
-
case "circle":
|
|
1322
|
-
ctx.beginPath();
|
|
1323
|
-
ctx.arc(x + size / 2, y + size / 2, size / 3, 0, Math.PI * 2);
|
|
1324
|
-
ctx.fill();
|
|
1325
|
-
break;
|
|
1326
|
-
case "square":
|
|
1327
|
-
const padding = size * 0.2;
|
|
1328
|
-
ctx.fillRect(x + padding, y + padding, size - padding * 2, size - padding * 2);
|
|
1329
|
-
break;
|
|
1330
|
-
case "star":
|
|
1331
|
-
drawStar(ctx, x + size / 2, y + size / 2, 5, size / 3, size / 6);
|
|
1332
|
-
ctx.fill();
|
|
1333
|
-
break;
|
|
1334
|
-
case "check":
|
|
1335
|
-
case "checkmark":
|
|
1336
|
-
const p = size * 0.2;
|
|
1337
|
-
ctx.beginPath();
|
|
1338
|
-
ctx.moveTo(x + p, y + size / 2);
|
|
1339
|
-
ctx.lineTo(x + size * 0.4, y + size - p);
|
|
1340
|
-
ctx.lineTo(x + size - p, y + p);
|
|
1341
|
-
ctx.stroke();
|
|
1342
|
-
break;
|
|
1343
|
-
case "x":
|
|
1344
|
-
case "close":
|
|
1345
|
-
const pd = size * 0.2;
|
|
1346
|
-
ctx.beginPath();
|
|
1347
|
-
ctx.moveTo(x + pd, y + pd);
|
|
1348
|
-
ctx.lineTo(x + size - pd, y + size - pd);
|
|
1349
|
-
ctx.moveTo(x + size - pd, y + pd);
|
|
1350
|
-
ctx.lineTo(x + pd, y + size - pd);
|
|
1351
|
-
ctx.stroke();
|
|
1352
|
-
break;
|
|
1353
|
-
default:
|
|
1354
|
-
ctx.beginPath();
|
|
1355
|
-
ctx.arc(x + size / 2, y + size / 2, size / 3, 0, Math.PI * 2);
|
|
1356
|
-
ctx.fill();
|
|
1357
|
-
}
|
|
1358
|
-
}
|
|
1359
|
-
function paintLink(ctx, node) {
|
|
1360
|
-
const layout = node.layout;
|
|
1361
|
-
const props = node.props;
|
|
1362
|
-
const text = String(props[0] || props.text || "");
|
|
1363
|
-
const color = node.hovered ? props.hoverColor || "#0056b3" : props.color || "#007bff";
|
|
1364
|
-
const fontSize = parseFloat(props.fontSize) || 16;
|
|
1365
|
-
const fontWeight = props.fontWeight || "normal";
|
|
1366
|
-
const fontFamily = props.fontFamily || "system-ui, sans-serif";
|
|
1367
|
-
const textDecoration = props.textDecoration !== undefined ? props.textDecoration : "underline";
|
|
1368
|
-
ctx.fillStyle = color;
|
|
1369
|
-
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`;
|
|
1370
|
-
ctx.textAlign = "left";
|
|
1371
|
-
ctx.textBaseline = "top";
|
|
1372
|
-
const x = layout.x + layout.contentX;
|
|
1373
|
-
const y = layout.y + layout.contentY;
|
|
1374
|
-
ctx.fillText(text, x, y);
|
|
1375
|
-
if (textDecoration === "underline") {
|
|
1376
|
-
const textWidth = ctx.measureText(text).width;
|
|
1377
|
-
ctx.strokeStyle = color;
|
|
1378
|
-
ctx.lineWidth = 1;
|
|
1379
|
-
ctx.beginPath();
|
|
1380
|
-
ctx.moveTo(x, y + fontSize + 2);
|
|
1381
|
-
ctx.lineTo(x + textWidth, y + fontSize + 2);
|
|
1382
|
-
ctx.stroke();
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
function applyTextDecoration(ctx, decoration, color, x, y, width, fontSize) {
|
|
1386
|
-
ctx.strokeStyle = color;
|
|
1387
|
-
ctx.lineWidth = Math.max(1, fontSize / 16);
|
|
1388
|
-
ctx.beginPath();
|
|
1389
|
-
if (decoration === "underline") {
|
|
1390
|
-
const underlineY = y + fontSize + 2;
|
|
1391
|
-
ctx.moveTo(x, underlineY);
|
|
1392
|
-
ctx.lineTo(x + width, underlineY);
|
|
1393
|
-
} else if (decoration === "line-through" || decoration === "strikethrough") {
|
|
1394
|
-
const lineThroughY = y + fontSize / 2;
|
|
1395
|
-
ctx.moveTo(x, lineThroughY);
|
|
1396
|
-
ctx.lineTo(x + width, lineThroughY);
|
|
1397
|
-
} else if (decoration === "overline") {
|
|
1398
|
-
ctx.moveTo(x, y);
|
|
1399
|
-
ctx.lineTo(x + width, y);
|
|
1400
|
-
}
|
|
1401
|
-
ctx.stroke();
|
|
1402
|
-
}
|
|
1403
|
-
function drawStar(ctx, cx, cy, spikes, outerRadius, innerRadius) {
|
|
1404
|
-
let rot = Math.PI / 2 * 3;
|
|
1405
|
-
let x = cx;
|
|
1406
|
-
let y = cy;
|
|
1407
|
-
const step = Math.PI / spikes;
|
|
1408
|
-
ctx.beginPath();
|
|
1409
|
-
ctx.moveTo(cx, cy - outerRadius);
|
|
1410
|
-
for (let i = 0;i < spikes; i++) {
|
|
1411
|
-
x = cx + Math.cos(rot) * outerRadius;
|
|
1412
|
-
y = cy + Math.sin(rot) * outerRadius;
|
|
1413
|
-
ctx.lineTo(x, y);
|
|
1414
|
-
rot += step;
|
|
1415
|
-
x = cx + Math.cos(rot) * innerRadius;
|
|
1416
|
-
y = cy + Math.sin(rot) * innerRadius;
|
|
1417
|
-
ctx.lineTo(x, y);
|
|
1418
|
-
rot += step;
|
|
1419
|
-
}
|
|
1420
|
-
ctx.lineTo(cx, cy - outerRadius);
|
|
1421
|
-
ctx.closePath();
|
|
1422
|
-
}
|
|
1423
|
-
|
|
1424
|
-
// src/canvas/events.ts
|
|
1425
|
-
class CanvasEventManager {
|
|
1426
|
-
canvas;
|
|
1427
|
-
engine;
|
|
1428
|
-
rootNode = null;
|
|
1429
|
-
hoveredNode = null;
|
|
1430
|
-
focusedNode = null;
|
|
1431
|
-
mouseDownNode = null;
|
|
1432
|
-
constructor(canvas, engine) {
|
|
1433
|
-
this.canvas = canvas;
|
|
1434
|
-
this.engine = engine;
|
|
1435
|
-
this.setupEventListeners();
|
|
1436
|
-
}
|
|
1437
|
-
setRootNode(node) {
|
|
1438
|
-
this.rootNode = node;
|
|
1439
|
-
}
|
|
1440
|
-
setupEventListeners() {
|
|
1441
|
-
this.canvas.addEventListener("mousemove", this.onMouseMove.bind(this));
|
|
1442
|
-
this.canvas.addEventListener("mousedown", this.onMouseDown.bind(this));
|
|
1443
|
-
this.canvas.addEventListener("mouseup", this.onMouseUp.bind(this));
|
|
1444
|
-
this.canvas.addEventListener("click", this.onClick.bind(this));
|
|
1445
|
-
this.canvas.addEventListener("dblclick", this.onDoubleClick.bind(this));
|
|
1446
|
-
this.canvas.addEventListener("keydown", this.onKeyDown.bind(this));
|
|
1447
|
-
this.canvas.addEventListener("keyup", this.onKeyUp.bind(this));
|
|
1448
|
-
}
|
|
1449
|
-
getCanvasCoordinates(e) {
|
|
1450
|
-
const rect = this.canvas.getBoundingClientRect();
|
|
1451
|
-
const scaleX = this.canvas.width / rect.width;
|
|
1452
|
-
const scaleY = this.canvas.height / rect.height;
|
|
1453
|
-
return {
|
|
1454
|
-
x: (e.clientX - rect.left) * scaleX,
|
|
1455
|
-
y: (e.clientY - rect.top) * scaleY
|
|
1456
|
-
};
|
|
1457
|
-
}
|
|
1458
|
-
hitTest(point) {
|
|
1459
|
-
if (!this.rootNode)
|
|
1460
|
-
return null;
|
|
1461
|
-
return this.hitTestNode(this.rootNode, point);
|
|
1462
|
-
}
|
|
1463
|
-
hitTestNode(node, point) {
|
|
1464
|
-
if (!node.visible || !node.layout)
|
|
1465
|
-
return null;
|
|
1466
|
-
const bounds = getAbsoluteBounds(node);
|
|
1467
|
-
if (!bounds)
|
|
1468
|
-
return null;
|
|
1469
|
-
for (let i = node.children.length - 1;i >= 0; i--) {
|
|
1470
|
-
const child = node.children[i];
|
|
1471
|
-
const hit = this.hitTestNode(child, point);
|
|
1472
|
-
if (hit)
|
|
1473
|
-
return hit;
|
|
1474
|
-
}
|
|
1475
|
-
const radius = node.layout.border.radius;
|
|
1476
|
-
if (isPointInRoundedRect(point, bounds, radius)) {
|
|
1477
|
-
return node;
|
|
1478
|
-
}
|
|
1479
|
-
return null;
|
|
1480
|
-
}
|
|
1481
|
-
onMouseMove(e) {
|
|
1482
|
-
const point = this.getCanvasCoordinates(e);
|
|
1483
|
-
const node = this.hitTest(point);
|
|
1484
|
-
if (node !== this.hoveredNode) {
|
|
1485
|
-
if (this.hoveredNode) {
|
|
1486
|
-
this.hoveredNode.hovered = false;
|
|
1487
|
-
this.dispatchNodeEvent(this.hoveredNode, "mouseleave", {});
|
|
1488
|
-
}
|
|
1489
|
-
this.hoveredNode = node;
|
|
1490
|
-
if (node) {
|
|
1491
|
-
node.hovered = true;
|
|
1492
|
-
this.dispatchNodeEvent(node, "mouseenter", {});
|
|
1493
|
-
}
|
|
1494
|
-
this.updateCursor(node);
|
|
1495
|
-
this.requestRedraw();
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
onMouseDown(e) {
|
|
1499
|
-
const point = this.getCanvasCoordinates(e);
|
|
1500
|
-
const node = this.hitTest(point);
|
|
1501
|
-
this.mouseDownNode = node;
|
|
1502
|
-
if (node && node.clickable) {
|
|
1503
|
-
this.dispatchNodeEvent(node, "mousedown", {
|
|
1504
|
-
button: e.button,
|
|
1505
|
-
clientX: e.clientX,
|
|
1506
|
-
clientY: e.clientY
|
|
1507
|
-
});
|
|
1508
|
-
}
|
|
1509
|
-
if (node && node.focusable) {
|
|
1510
|
-
this.setFocus(node);
|
|
1511
|
-
} else {
|
|
1512
|
-
this.setFocus(null);
|
|
1513
|
-
}
|
|
1514
|
-
}
|
|
1515
|
-
onMouseUp(e) {
|
|
1516
|
-
const point = this.getCanvasCoordinates(e);
|
|
1517
|
-
const node = this.hitTest(point);
|
|
1518
|
-
if (node && node.clickable) {
|
|
1519
|
-
this.dispatchNodeEvent(node, "mouseup", {
|
|
1520
|
-
button: e.button,
|
|
1521
|
-
clientX: e.clientX,
|
|
1522
|
-
clientY: e.clientY
|
|
1523
|
-
});
|
|
1524
|
-
}
|
|
1525
|
-
this.mouseDownNode = null;
|
|
1526
|
-
}
|
|
1527
|
-
onClick(e) {
|
|
1528
|
-
const point = this.getCanvasCoordinates(e);
|
|
1529
|
-
const node = this.hitTest(point);
|
|
1530
|
-
if (node && node.clickable && node === this.mouseDownNode) {
|
|
1531
|
-
this.dispatchNodeEvent(node, "click", {
|
|
1532
|
-
button: e.button,
|
|
1533
|
-
clientX: e.clientX,
|
|
1534
|
-
clientY: e.clientY
|
|
1535
|
-
});
|
|
1536
|
-
}
|
|
1537
|
-
}
|
|
1538
|
-
onDoubleClick(e) {
|
|
1539
|
-
const point = this.getCanvasCoordinates(e);
|
|
1540
|
-
const node = this.hitTest(point);
|
|
1541
|
-
if (node && node.clickable) {
|
|
1542
|
-
this.dispatchNodeEvent(node, "dblclick", {
|
|
1543
|
-
button: e.button,
|
|
1544
|
-
clientX: e.clientX,
|
|
1545
|
-
clientY: e.clientY
|
|
1546
|
-
});
|
|
1547
|
-
}
|
|
1548
|
-
}
|
|
1549
|
-
onKeyDown(e) {
|
|
1550
|
-
if (this.focusedNode) {
|
|
1551
|
-
this.dispatchNodeEvent(this.focusedNode, "keydown", {
|
|
1552
|
-
key: e.key,
|
|
1553
|
-
code: e.code,
|
|
1554
|
-
ctrlKey: e.ctrlKey,
|
|
1555
|
-
shiftKey: e.shiftKey,
|
|
1556
|
-
altKey: e.altKey
|
|
1557
|
-
});
|
|
1558
|
-
}
|
|
1559
|
-
}
|
|
1560
|
-
onKeyUp(e) {
|
|
1561
|
-
if (this.focusedNode) {
|
|
1562
|
-
this.dispatchNodeEvent(this.focusedNode, "keyup", {
|
|
1563
|
-
key: e.key,
|
|
1564
|
-
code: e.code,
|
|
1565
|
-
ctrlKey: e.ctrlKey,
|
|
1566
|
-
shiftKey: e.shiftKey,
|
|
1567
|
-
altKey: e.altKey
|
|
1568
|
-
});
|
|
1569
|
-
}
|
|
1570
|
-
}
|
|
1571
|
-
setFocus(node) {
|
|
1572
|
-
if (node === this.focusedNode)
|
|
1573
|
-
return;
|
|
1574
|
-
if (this.focusedNode) {
|
|
1575
|
-
this.focusedNode.focused = false;
|
|
1576
|
-
this.dispatchNodeEvent(this.focusedNode, "blur", {});
|
|
1577
|
-
}
|
|
1578
|
-
this.focusedNode = node;
|
|
1579
|
-
if (node) {
|
|
1580
|
-
node.focused = true;
|
|
1581
|
-
this.dispatchNodeEvent(node, "focus", {});
|
|
1582
|
-
}
|
|
1583
|
-
this.requestRedraw();
|
|
1584
|
-
}
|
|
1585
|
-
updateCursor(node) {
|
|
1586
|
-
if (!node) {
|
|
1587
|
-
this.canvas.style.cursor = "default";
|
|
1588
|
-
return;
|
|
1589
|
-
}
|
|
1590
|
-
const cursor = node.props.cursor || (node.clickable ? "pointer" : "default");
|
|
1591
|
-
this.canvas.style.cursor = cursor;
|
|
1592
|
-
}
|
|
1593
|
-
dispatchNodeEvent(node, eventType, data) {
|
|
1594
|
-
const actionName = node.props[`on${eventType}`] || node.props[eventType];
|
|
1595
|
-
if (actionName && typeof actionName === "string") {
|
|
1596
|
-
this.engine.dispatchAction(actionName, {
|
|
1597
|
-
type: eventType,
|
|
1598
|
-
nodeId: node.id,
|
|
1599
|
-
timestamp: Date.now(),
|
|
1600
|
-
...data
|
|
1601
|
-
});
|
|
1602
|
-
}
|
|
1603
|
-
}
|
|
1604
|
-
requestRedraw() {
|
|
1605
|
-
this.canvas.dispatchEvent(new CustomEvent("hypen:redraw"));
|
|
1606
|
-
}
|
|
1607
|
-
destroy() {}
|
|
1608
|
-
}
|
|
1609
|
-
|
|
1610
|
-
// src/canvas/input.ts
|
|
1611
|
-
class InputOverlay {
|
|
1612
|
-
container;
|
|
1613
|
-
overlay = null;
|
|
1614
|
-
focusedNode = null;
|
|
1615
|
-
onChangeCallback = null;
|
|
1616
|
-
bindPath = null;
|
|
1617
|
-
engine = null;
|
|
1618
|
-
constructor(container) {
|
|
1619
|
-
this.container = container || {};
|
|
1620
|
-
}
|
|
1621
|
-
showInput(node, canvasBounds, onChange, engine) {
|
|
1622
|
-
if (typeof document === "undefined")
|
|
1623
|
-
return;
|
|
1624
|
-
this.hideInput();
|
|
1625
|
-
this.bindPath = node.props.bind || null;
|
|
1626
|
-
this.engine = engine || null;
|
|
1627
|
-
const bounds = getAbsoluteBounds(node);
|
|
1628
|
-
if (!bounds)
|
|
1629
|
-
return;
|
|
1630
|
-
const isMultiline = node.type === "textarea";
|
|
1631
|
-
this.overlay = isMultiline ? document.createElement("textarea") : document.createElement("input");
|
|
1632
|
-
this.styleOverlay(node, bounds, canvasBounds);
|
|
1633
|
-
const value = node.props.value || "";
|
|
1634
|
-
this.overlay.value = value;
|
|
1635
|
-
this.onChangeCallback = onChange;
|
|
1636
|
-
this.overlay.addEventListener("input", this.onInput.bind(this));
|
|
1637
|
-
this.overlay.addEventListener("blur", this.onBlur.bind(this));
|
|
1638
|
-
this.overlay.addEventListener("keydown", this.onKeyDown.bind(this));
|
|
1639
|
-
this.container.appendChild(this.overlay);
|
|
1640
|
-
this.overlay.focus();
|
|
1641
|
-
this.focusedNode = node;
|
|
1642
|
-
}
|
|
1643
|
-
hideInput() {
|
|
1644
|
-
if (this.overlay) {
|
|
1645
|
-
this.overlay.remove();
|
|
1646
|
-
this.overlay = null;
|
|
1647
|
-
}
|
|
1648
|
-
this.focusedNode = null;
|
|
1649
|
-
this.onChangeCallback = null;
|
|
1650
|
-
this.bindPath = null;
|
|
1651
|
-
this.engine = null;
|
|
1652
|
-
}
|
|
1653
|
-
updatePosition(node, canvasBounds) {
|
|
1654
|
-
if (!this.overlay || node !== this.focusedNode)
|
|
1655
|
-
return;
|
|
1656
|
-
const bounds = getAbsoluteBounds(node);
|
|
1657
|
-
if (!bounds)
|
|
1658
|
-
return;
|
|
1659
|
-
this.positionOverlay(bounds, canvasBounds);
|
|
1660
|
-
}
|
|
1661
|
-
styleOverlay(node, bounds, canvasBounds) {
|
|
1662
|
-
if (!this.overlay)
|
|
1663
|
-
return;
|
|
1664
|
-
const props = node.props;
|
|
1665
|
-
this.positionOverlay(bounds, canvasBounds);
|
|
1666
|
-
const fontSize = parseFloat(props.fontSize) || 16;
|
|
1667
|
-
const fontWeight = props.fontWeight || "normal";
|
|
1668
|
-
const fontFamily = props.fontFamily || "system-ui, sans-serif";
|
|
1669
|
-
const color = props.color || "#000000";
|
|
1670
|
-
Object.assign(this.overlay.style, {
|
|
1671
|
-
fontSize: `${fontSize}px`,
|
|
1672
|
-
fontWeight,
|
|
1673
|
-
fontFamily,
|
|
1674
|
-
color,
|
|
1675
|
-
border: "none",
|
|
1676
|
-
outline: "2px solid #007bff",
|
|
1677
|
-
backgroundColor: props.backgroundColor || "#ffffff",
|
|
1678
|
-
padding: `${props.padding || 8}px`,
|
|
1679
|
-
borderRadius: `${props.borderRadius || 4}px`,
|
|
1680
|
-
boxSizing: "border-box",
|
|
1681
|
-
resize: "none"
|
|
1682
|
-
});
|
|
1683
|
-
if (props.placeholder) {
|
|
1684
|
-
this.overlay.placeholder = props.placeholder;
|
|
1685
|
-
}
|
|
1686
|
-
if (this.overlay instanceof HTMLInputElement && props.type) {
|
|
1687
|
-
this.overlay.type = props.type;
|
|
1688
|
-
}
|
|
1689
|
-
}
|
|
1690
|
-
positionOverlay(bounds, canvasBounds) {
|
|
1691
|
-
if (!this.overlay)
|
|
1692
|
-
return;
|
|
1693
|
-
const canvas = this.container.querySelector("canvas");
|
|
1694
|
-
const scaleX = canvasBounds.width / canvas.width;
|
|
1695
|
-
const scaleY = canvasBounds.height / canvas.height;
|
|
1696
|
-
const left = bounds.x * scaleX;
|
|
1697
|
-
const top = bounds.y * scaleY;
|
|
1698
|
-
const width = bounds.width * scaleX;
|
|
1699
|
-
const height = bounds.height * scaleY;
|
|
1700
|
-
Object.assign(this.overlay.style, {
|
|
1701
|
-
position: "absolute",
|
|
1702
|
-
left: `${left}px`,
|
|
1703
|
-
top: `${top}px`,
|
|
1704
|
-
width: `${width}px`,
|
|
1705
|
-
height: `${height}px`
|
|
1706
|
-
});
|
|
1707
|
-
}
|
|
1708
|
-
onInput(e) {
|
|
1709
|
-
if (!this.overlay || !this.onChangeCallback)
|
|
1710
|
-
return;
|
|
1711
|
-
const value = this.overlay.value;
|
|
1712
|
-
this.onChangeCallback(value);
|
|
1713
|
-
if (this.bindPath && this.engine) {
|
|
1714
|
-
this.engine.dispatchAction("__hypen_bind", {
|
|
1715
|
-
path: this.bindPath,
|
|
1716
|
-
value
|
|
1717
|
-
});
|
|
1718
|
-
}
|
|
1719
|
-
}
|
|
1720
|
-
onBlur() {
|
|
1721
|
-
this.hideInput();
|
|
1722
|
-
}
|
|
1723
|
-
onKeyDown(e) {
|
|
1724
|
-
if (!this.overlay)
|
|
1725
|
-
return;
|
|
1726
|
-
if (e.key === "Enter" && this.overlay instanceof HTMLInputElement) {
|
|
1727
|
-
e.preventDefault();
|
|
1728
|
-
this.overlay.blur();
|
|
1729
|
-
}
|
|
1730
|
-
if (e.key === "Escape") {
|
|
1731
|
-
e.preventDefault();
|
|
1732
|
-
this.overlay.blur();
|
|
1733
|
-
}
|
|
1734
|
-
}
|
|
1735
|
-
isShown() {
|
|
1736
|
-
return this.overlay !== null;
|
|
1737
|
-
}
|
|
1738
|
-
getFocusedNode() {
|
|
1739
|
-
return this.focusedNode;
|
|
1740
|
-
}
|
|
1741
|
-
}
|
|
1742
|
-
|
|
1743
|
-
// src/canvas/accessibility.ts
|
|
1744
|
-
class AccessibilityLayer {
|
|
1745
|
-
shadowRoot;
|
|
1746
|
-
nodeMap = new Map;
|
|
1747
|
-
enabled;
|
|
1748
|
-
constructor(container, enabled = true) {
|
|
1749
|
-
this.enabled = enabled && typeof document !== "undefined";
|
|
1750
|
-
if (this.enabled && typeof document !== "undefined") {
|
|
1751
|
-
this.shadowRoot = document.createElement("div");
|
|
1752
|
-
this.shadowRoot.setAttribute("role", "application");
|
|
1753
|
-
this.shadowRoot.setAttribute("aria-label", "Hypen Canvas Application");
|
|
1754
|
-
this.shadowRoot.style.position = "absolute";
|
|
1755
|
-
this.shadowRoot.style.left = "-9999px";
|
|
1756
|
-
this.shadowRoot.style.width = "1px";
|
|
1757
|
-
this.shadowRoot.style.height = "1px";
|
|
1758
|
-
this.shadowRoot.style.overflow = "hidden";
|
|
1759
|
-
if (container) {
|
|
1760
|
-
container.appendChild(this.shadowRoot);
|
|
1761
|
-
}
|
|
1762
|
-
} else {
|
|
1763
|
-
this.shadowRoot = {};
|
|
1764
|
-
}
|
|
1765
|
-
}
|
|
1766
|
-
syncTree(root) {
|
|
1767
|
-
if (!this.enabled)
|
|
1768
|
-
return;
|
|
1769
|
-
this.shadowRoot.innerHTML = "";
|
|
1770
|
-
this.nodeMap.clear();
|
|
1771
|
-
const shadowNode = this.createShadowNode(root);
|
|
1772
|
-
if (shadowNode) {
|
|
1773
|
-
this.shadowRoot.appendChild(shadowNode);
|
|
1774
|
-
}
|
|
1775
|
-
}
|
|
1776
|
-
createShadowNode(node) {
|
|
1777
|
-
if (!node.visible)
|
|
1778
|
-
return null;
|
|
1779
|
-
let element;
|
|
1780
|
-
switch (node.type.toLowerCase()) {
|
|
1781
|
-
case "button":
|
|
1782
|
-
element = document.createElement("button");
|
|
1783
|
-
element.textContent = this.getNodeText(node);
|
|
1784
|
-
if (node.props.onclick) {
|
|
1785
|
-
element.setAttribute("aria-label", "Clickable button");
|
|
1786
|
-
}
|
|
1787
|
-
break;
|
|
1788
|
-
case "input":
|
|
1789
|
-
element = document.createElement("input");
|
|
1790
|
-
element.type = node.props.type || "text";
|
|
1791
|
-
element.value = node.props.value || "";
|
|
1792
|
-
if (node.props.placeholder) {
|
|
1793
|
-
element.placeholder = node.props.placeholder;
|
|
1794
|
-
}
|
|
1795
|
-
break;
|
|
1796
|
-
case "textarea":
|
|
1797
|
-
element = document.createElement("textarea");
|
|
1798
|
-
element.value = node.props.value || "";
|
|
1799
|
-
if (node.props.placeholder) {
|
|
1800
|
-
element.placeholder = node.props.placeholder;
|
|
1801
|
-
}
|
|
1802
|
-
break;
|
|
1803
|
-
case "image":
|
|
1804
|
-
element = document.createElement("img");
|
|
1805
|
-
element.src = node.props.src || "";
|
|
1806
|
-
element.alt = node.props.alt || "Image";
|
|
1807
|
-
break;
|
|
1808
|
-
case "text":
|
|
1809
|
-
element = document.createElement("span");
|
|
1810
|
-
element.textContent = String(node.props[0] || node.props.text || "");
|
|
1811
|
-
break;
|
|
1812
|
-
case "column":
|
|
1813
|
-
case "row":
|
|
1814
|
-
case "container":
|
|
1815
|
-
case "box":
|
|
1816
|
-
element = document.createElement("div");
|
|
1817
|
-
element.setAttribute("role", node.type === "column" ? "group" : "group");
|
|
1818
|
-
break;
|
|
1819
|
-
default:
|
|
1820
|
-
element = document.createElement("div");
|
|
1821
|
-
}
|
|
1822
|
-
element.setAttribute("data-hypen-id", node.id);
|
|
1823
|
-
if (node.props["aria-label"]) {
|
|
1824
|
-
element.setAttribute("aria-label", node.props["aria-label"]);
|
|
1825
|
-
}
|
|
1826
|
-
if (node.focusable) {
|
|
1827
|
-
element.tabIndex = 0;
|
|
1828
|
-
}
|
|
1829
|
-
this.nodeMap.set(node.id, element);
|
|
1830
|
-
for (const child of node.children) {
|
|
1831
|
-
const childElement = this.createShadowNode(child);
|
|
1832
|
-
if (childElement) {
|
|
1833
|
-
element.appendChild(childElement);
|
|
1834
|
-
}
|
|
1835
|
-
}
|
|
1836
|
-
return element;
|
|
1837
|
-
}
|
|
1838
|
-
getNodeText(node) {
|
|
1839
|
-
if (node.type === "text") {
|
|
1840
|
-
return String(node.props[0] || node.props.text || "");
|
|
1841
|
-
}
|
|
1842
|
-
let text = "";
|
|
1843
|
-
for (const child of node.children) {
|
|
1844
|
-
text += this.getNodeText(child);
|
|
1845
|
-
}
|
|
1846
|
-
return text;
|
|
1847
|
-
}
|
|
1848
|
-
focusNode(nodeId) {
|
|
1849
|
-
if (!this.enabled)
|
|
1850
|
-
return;
|
|
1851
|
-
const element = this.nodeMap.get(nodeId);
|
|
1852
|
-
if (element) {
|
|
1853
|
-
element.focus();
|
|
1854
|
-
}
|
|
1855
|
-
}
|
|
1856
|
-
updateNode(node) {
|
|
1857
|
-
if (!this.enabled)
|
|
1858
|
-
return;
|
|
1859
|
-
const element = this.nodeMap.get(node.id);
|
|
1860
|
-
if (!element)
|
|
1861
|
-
return;
|
|
1862
|
-
if (node.type === "text") {
|
|
1863
|
-
element.textContent = String(node.props[0] || node.props.text || "");
|
|
1864
|
-
}
|
|
1865
|
-
if (node.type === "input" || node.type === "textarea") {
|
|
1866
|
-
element.value = node.props.value || "";
|
|
1867
|
-
}
|
|
1868
|
-
element.style.display = node.visible ? "" : "none";
|
|
1869
|
-
}
|
|
1870
|
-
getElement(nodeId) {
|
|
1871
|
-
return this.nodeMap.get(nodeId);
|
|
1872
|
-
}
|
|
1873
|
-
setEnabled(enabled) {
|
|
1874
|
-
this.enabled = enabled;
|
|
1875
|
-
if (!enabled && this.shadowRoot.parentElement) {
|
|
1876
|
-
this.shadowRoot.remove();
|
|
1877
|
-
}
|
|
1878
|
-
}
|
|
1879
|
-
destroy() {
|
|
1880
|
-
if (this.shadowRoot.parentElement) {
|
|
1881
|
-
this.shadowRoot.remove();
|
|
1882
|
-
}
|
|
1883
|
-
this.nodeMap.clear();
|
|
1884
|
-
}
|
|
1885
|
-
}
|
|
1886
|
-
|
|
1887
|
-
// src/canvas/renderer.ts
|
|
1888
|
-
import { frameworkLoggers as frameworkLoggers2 } from "@hypen-space/core/logger";
|
|
1889
|
-
var log2 = frameworkLoggers2.canvas;
|
|
1890
|
-
var DEFAULT_OPTIONS = {
|
|
1891
|
-
devicePixelRatio: typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1,
|
|
1892
|
-
backgroundColor: "#ffffff",
|
|
1893
|
-
enableAccessibility: true,
|
|
1894
|
-
enableHitTesting: true,
|
|
1895
|
-
enableInputOverlay: true,
|
|
1896
|
-
enableDirtyRects: false,
|
|
1897
|
-
enableLayerCaching: false,
|
|
1898
|
-
maxLayerCacheSize: 10,
|
|
1899
|
-
showLayoutBounds: false,
|
|
1900
|
-
showDirtyRects: false,
|
|
1901
|
-
logPerformance: false
|
|
1902
|
-
};
|
|
1903
|
-
|
|
1904
|
-
class CanvasRenderer {
|
|
1905
|
-
canvas;
|
|
1906
|
-
ctx;
|
|
1907
|
-
engine;
|
|
1908
|
-
options;
|
|
1909
|
-
rootNode = null;
|
|
1910
|
-
nodes = new Map;
|
|
1911
|
-
eventManager;
|
|
1912
|
-
inputOverlay;
|
|
1913
|
-
accessibilityLayer;
|
|
1914
|
-
rafId = null;
|
|
1915
|
-
needsRedraw = false;
|
|
1916
|
-
frameCount = 0;
|
|
1917
|
-
lastFrameTime = 0;
|
|
1918
|
-
constructor(canvas, engine, options) {
|
|
1919
|
-
this.canvas = canvas;
|
|
1920
|
-
this.engine = engine;
|
|
1921
|
-
this.options = { ...DEFAULT_OPTIONS, ...options };
|
|
1922
|
-
const ctx = canvas.getContext("2d");
|
|
1923
|
-
if (!ctx) {
|
|
1924
|
-
throw new Error("Failed to get 2D context from canvas");
|
|
1925
|
-
}
|
|
1926
|
-
this.ctx = ctx;
|
|
1927
|
-
this.setupHiDPI();
|
|
1928
|
-
this.eventManager = new CanvasEventManager(canvas, engine);
|
|
1929
|
-
this.inputOverlay = new InputOverlay(canvas.parentElement || (typeof document !== "undefined" ? document.body : null));
|
|
1930
|
-
this.accessibilityLayer = new AccessibilityLayer(canvas.parentElement || (typeof document !== "undefined" ? document.body : null), this.options.enableAccessibility || false);
|
|
1931
|
-
this.canvas.addEventListener("hypen:redraw", () => this.scheduleRedraw());
|
|
1932
|
-
}
|
|
1933
|
-
setupHiDPI() {
|
|
1934
|
-
const dpr = this.options.devicePixelRatio || 1;
|
|
1935
|
-
const rect = this.canvas.getBoundingClientRect();
|
|
1936
|
-
this.canvas.width = rect.width * dpr;
|
|
1937
|
-
this.canvas.height = rect.height * dpr;
|
|
1938
|
-
this.ctx.scale(dpr, dpr);
|
|
1939
|
-
this.canvas.style.width = `${rect.width}px`;
|
|
1940
|
-
this.canvas.style.height = `${rect.height}px`;
|
|
1941
|
-
}
|
|
1942
|
-
applyPatches(patches) {
|
|
1943
|
-
for (const patch of patches) {
|
|
1944
|
-
this.applyPatch(patch);
|
|
1945
|
-
}
|
|
1946
|
-
if (this.rootNode) {
|
|
1947
|
-
this.accessibilityLayer.syncTree(this.rootNode);
|
|
1948
|
-
}
|
|
1949
|
-
this.scheduleRedraw();
|
|
1950
|
-
}
|
|
1951
|
-
applyPatch(patch) {
|
|
1952
|
-
switch (patch.type) {
|
|
1953
|
-
case "create":
|
|
1954
|
-
this.onCreate(patch.id, patch.elementType, patch.props || {});
|
|
1955
|
-
break;
|
|
1956
|
-
case "setProp":
|
|
1957
|
-
this.onSetProp(patch.id, patch.name, patch.value);
|
|
1958
|
-
break;
|
|
1959
|
-
case "removeProp":
|
|
1960
|
-
this.onRemoveProp(patch.id, patch.name);
|
|
1961
|
-
break;
|
|
1962
|
-
case "setText":
|
|
1963
|
-
this.onSetText(patch.id, patch.text);
|
|
1964
|
-
break;
|
|
1965
|
-
case "insert":
|
|
1966
|
-
this.onInsert(patch.parentId, patch.id, patch.beforeId);
|
|
1967
|
-
break;
|
|
1968
|
-
case "move":
|
|
1969
|
-
this.onMove(patch.parentId, patch.id, patch.beforeId);
|
|
1970
|
-
break;
|
|
1971
|
-
case "remove":
|
|
1972
|
-
this.onRemove(patch.id);
|
|
1973
|
-
break;
|
|
1974
|
-
}
|
|
1975
|
-
}
|
|
1976
|
-
onCreate(id, elementType, props) {
|
|
1977
|
-
const node = {
|
|
1978
|
-
id,
|
|
1979
|
-
type: elementType,
|
|
1980
|
-
props: props instanceof Map ? Object.fromEntries(props) : props,
|
|
1981
|
-
children: [],
|
|
1982
|
-
parent: null,
|
|
1983
|
-
visible: true,
|
|
1984
|
-
opacity: parseFloat(props.opacity) || 1,
|
|
1985
|
-
clickable: elementType === "button" || !!props.onclick,
|
|
1986
|
-
hoverable: true,
|
|
1987
|
-
focusable: elementType === "input" || elementType === "textarea" || elementType === "button",
|
|
1988
|
-
focused: false,
|
|
1989
|
-
hovered: false
|
|
1990
|
-
};
|
|
1991
|
-
this.nodes.set(id, node);
|
|
1992
|
-
}
|
|
1993
|
-
onSetProp(id, name, value) {
|
|
1994
|
-
const node = this.nodes.get(id);
|
|
1995
|
-
if (!node)
|
|
1996
|
-
return;
|
|
1997
|
-
node.props[name] = value;
|
|
1998
|
-
if (name === "visible") {
|
|
1999
|
-
node.visible = !!value;
|
|
2000
|
-
}
|
|
2001
|
-
if (name === "opacity") {
|
|
2002
|
-
node.opacity = parseFloat(value) || 1;
|
|
2003
|
-
}
|
|
2004
|
-
this.accessibilityLayer.updateNode(node);
|
|
2005
|
-
}
|
|
2006
|
-
onRemoveProp(id, name) {
|
|
2007
|
-
const node = this.nodes.get(id);
|
|
2008
|
-
if (!node)
|
|
2009
|
-
return;
|
|
2010
|
-
delete node.props[name];
|
|
2011
|
-
if (name === "visible") {
|
|
2012
|
-
node.visible = true;
|
|
2013
|
-
}
|
|
2014
|
-
if (name === "opacity") {
|
|
2015
|
-
node.opacity = 1;
|
|
2016
|
-
}
|
|
2017
|
-
this.accessibilityLayer.updateNode(node);
|
|
2018
|
-
}
|
|
2019
|
-
onSetText(id, text) {
|
|
2020
|
-
const node = this.nodes.get(id);
|
|
2021
|
-
if (!node)
|
|
2022
|
-
return;
|
|
2023
|
-
node.props[0] = text;
|
|
2024
|
-
this.accessibilityLayer.updateNode(node);
|
|
2025
|
-
}
|
|
2026
|
-
onInsert(parentId, id, beforeId) {
|
|
2027
|
-
const child = this.nodes.get(id);
|
|
2028
|
-
if (!child)
|
|
2029
|
-
return;
|
|
2030
|
-
if (parentId === "root" && id === "root") {
|
|
2031
|
-
this.rootNode = child;
|
|
2032
|
-
this.eventManager.setRootNode(child);
|
|
2033
|
-
return;
|
|
2034
|
-
}
|
|
2035
|
-
const parent = this.nodes.get(parentId);
|
|
2036
|
-
if (!parent) {
|
|
2037
|
-
if (parentId === "root") {
|
|
2038
|
-
this.rootNode = child;
|
|
2039
|
-
this.eventManager.setRootNode(child);
|
|
2040
|
-
}
|
|
2041
|
-
return;
|
|
2042
|
-
}
|
|
2043
|
-
child.parent = parent;
|
|
2044
|
-
if (beforeId) {
|
|
2045
|
-
const beforeIndex = parent.children.findIndex((c) => c.id === beforeId);
|
|
2046
|
-
if (beforeIndex >= 0) {
|
|
2047
|
-
parent.children.splice(beforeIndex, 0, child);
|
|
2048
|
-
} else {
|
|
2049
|
-
parent.children.push(child);
|
|
2050
|
-
}
|
|
2051
|
-
} else {
|
|
2052
|
-
parent.children.push(child);
|
|
2053
|
-
}
|
|
2054
|
-
}
|
|
2055
|
-
onMove(parentId, id, beforeId) {
|
|
2056
|
-
const node = this.nodes.get(id);
|
|
2057
|
-
if (!node || !node.parent)
|
|
2058
|
-
return;
|
|
2059
|
-
const oldParent = node.parent;
|
|
2060
|
-
const oldIndex = oldParent.children.indexOf(node);
|
|
2061
|
-
if (oldIndex >= 0) {
|
|
2062
|
-
oldParent.children.splice(oldIndex, 1);
|
|
2063
|
-
}
|
|
2064
|
-
this.onInsert(parentId, id, beforeId);
|
|
2065
|
-
}
|
|
2066
|
-
onRemove(id) {
|
|
2067
|
-
const node = this.nodes.get(id);
|
|
2068
|
-
if (!node)
|
|
2069
|
-
return;
|
|
2070
|
-
if (node.parent) {
|
|
2071
|
-
const index = node.parent.children.indexOf(node);
|
|
2072
|
-
if (index >= 0) {
|
|
2073
|
-
node.parent.children.splice(index, 1);
|
|
2074
|
-
}
|
|
2075
|
-
}
|
|
2076
|
-
if (this.rootNode === node) {
|
|
2077
|
-
this.rootNode = null;
|
|
2078
|
-
this.eventManager.setRootNode(null);
|
|
2079
|
-
}
|
|
2080
|
-
this.nodes.delete(id);
|
|
2081
|
-
}
|
|
2082
|
-
scheduleRedraw() {
|
|
2083
|
-
if (this.rafId !== null)
|
|
2084
|
-
return;
|
|
2085
|
-
if (typeof requestAnimationFrame !== "undefined") {
|
|
2086
|
-
this.rafId = requestAnimationFrame(() => {
|
|
2087
|
-
this.render();
|
|
2088
|
-
this.rafId = null;
|
|
2089
|
-
});
|
|
2090
|
-
} else {
|
|
2091
|
-
this.render();
|
|
2092
|
-
}
|
|
2093
|
-
}
|
|
2094
|
-
render() {
|
|
2095
|
-
const startTime = performance.now();
|
|
2096
|
-
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
2097
|
-
if (this.options.backgroundColor) {
|
|
2098
|
-
this.ctx.fillStyle = this.options.backgroundColor;
|
|
2099
|
-
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
2100
|
-
}
|
|
2101
|
-
if (this.rootNode) {
|
|
2102
|
-
const dpr = this.options.devicePixelRatio || 1;
|
|
2103
|
-
computeLayout(this.ctx, this.rootNode, this.canvas.width / dpr, this.canvas.height / dpr);
|
|
2104
|
-
paintNode(this.ctx, this.rootNode);
|
|
2105
|
-
if (this.options.showLayoutBounds) {
|
|
2106
|
-
this.drawLayoutBounds(this.rootNode);
|
|
2107
|
-
}
|
|
2108
|
-
}
|
|
2109
|
-
if (this.options.logPerformance) {
|
|
2110
|
-
const elapsed = performance.now() - startTime;
|
|
2111
|
-
this.frameCount++;
|
|
2112
|
-
if (performance.now() - this.lastFrameTime > 1000) {
|
|
2113
|
-
log2.debug(`Canvas FPS: ${this.frameCount}, Last frame: ${elapsed.toFixed(2)}ms`);
|
|
2114
|
-
this.frameCount = 0;
|
|
2115
|
-
this.lastFrameTime = performance.now();
|
|
2116
|
-
}
|
|
2117
|
-
}
|
|
2118
|
-
}
|
|
2119
|
-
drawLayoutBounds(node) {
|
|
2120
|
-
if (!node.layout)
|
|
2121
|
-
return;
|
|
2122
|
-
const layout = node.layout;
|
|
2123
|
-
this.ctx.strokeStyle = "#ff0000";
|
|
2124
|
-
this.ctx.lineWidth = 1;
|
|
2125
|
-
this.ctx.strokeRect(layout.x, layout.y, layout.width, layout.height);
|
|
2126
|
-
for (const child of node.children) {
|
|
2127
|
-
this.drawLayoutBounds(child);
|
|
2128
|
-
}
|
|
2129
|
-
}
|
|
2130
|
-
getNode(id) {
|
|
2131
|
-
return this.nodes.get(id);
|
|
2132
|
-
}
|
|
2133
|
-
clear() {
|
|
2134
|
-
this.rootNode = null;
|
|
2135
|
-
this.nodes.clear();
|
|
2136
|
-
this.eventManager.setRootNode(null);
|
|
2137
|
-
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
2138
|
-
}
|
|
2139
|
-
registerPainter(type, painter) {
|
|
2140
|
-
registerPainter(type, painter);
|
|
2141
|
-
}
|
|
2142
|
-
setOptions(options) {
|
|
2143
|
-
this.options = { ...this.options, ...options };
|
|
2144
|
-
this.accessibilityLayer.setEnabled(this.options.enableAccessibility || false);
|
|
2145
|
-
}
|
|
2146
|
-
destroy() {
|
|
2147
|
-
if (this.rafId !== null) {
|
|
2148
|
-
cancelAnimationFrame(this.rafId);
|
|
2149
|
-
}
|
|
2150
|
-
this.eventManager.destroy();
|
|
2151
|
-
this.accessibilityLayer.destroy();
|
|
2152
|
-
}
|
|
2153
|
-
}
|
|
2154
39
|
export {
|
|
2155
40
|
registerPainter,
|
|
2156
41
|
InputOverlay,
|
|
@@ -2159,4 +44,4 @@ export {
|
|
|
2159
44
|
AccessibilityLayer
|
|
2160
45
|
};
|
|
2161
46
|
|
|
2162
|
-
//# debugId=
|
|
47
|
+
//# debugId=BA9D7DE7BA1C35A364756E2164756E21
|