@media-quest/engine 0.0.30 → 0.0.32

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.
@@ -1,165 +1,368 @@
1
- import { DCss } from "./css";
2
- import { DUtil } from "../utils/DUtil";
3
-
4
- type DStyle2 = CSSStyleDeclaration;
5
- export type PStyle = Partial<DStyle>;
6
- export interface DStyle {
7
- opacity: number;
8
- backgroundColor: string;
9
- visibility: "visible" | "hidden";
10
- cursor:
11
- | "pointer"
12
- | "help"
13
- | "copy"
14
- | "wait"
15
- | "not-allowed"
16
- | "context-menu"
17
- | "move"
18
- | "grabbing"
19
- | "grab"
20
- | "zoom-in"
21
- | "zoom-out"
22
- | "none"
23
- | "auto"
24
- | "default";
25
-
26
- // POSITION
27
- h: number;
28
- w: number;
29
- x: number;
30
- y: number;
31
-
32
- // BORDERS
33
- borderStyle: "solid" | "none" | "dotted" | "dashed";
34
- borderRadius: DCss.Px | DCss.Percent;
35
- borderWidth: DCss.Px;
36
- borderColor: string;
37
-
38
- margin: DCss.Px | DCss.Percent;
39
- padding: DCss.Px | DCss.Percent;
40
-
41
- // Translate
42
- transform: string;
43
- translate: string;
44
-
45
- // TEXT
46
- fontSize: DCss.Px;
47
- textColor: string;
48
- fontWeight: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
49
- textAlign: "right" | "left" | "center";
50
- letterSpacing: DCss.Px;
51
- }
52
-
53
- export namespace DStyle {
54
- export const normalize = <T extends HTMLElement>(el: T): T => {
55
- el.style.padding = "0";
56
- el.style.margin = "0";
57
- el.style.position = "absolute";
58
- el.style.boxSizing = "border-box";
59
- return el;
60
- };
61
-
62
- export const applyStyles = <T extends HTMLElement>(el: T, style: Partial<DStyle>, scale: number): T => {
63
- // const scalePx = DCss.toStringCre(this.scale);
64
- const {
65
- x,
66
- y,
67
- backgroundColor,
68
- borderColor,
69
- borderWidth,
70
- borderRadius,
71
- borderStyle,
72
- w,
73
- opacity,
74
- cursor,
75
- fontSize,
76
- fontWeight,
77
- textColor,
78
- textAlign,
79
- translate,
80
- margin,
81
- padding,
82
- letterSpacing,
83
- h,
84
- transform,
85
- visibility,
86
- } = style;
87
-
88
- // this.el.style.fontWeight = '900';
89
- if (backgroundColor) {
90
- el.style.backgroundColor = backgroundColor;
91
- }
92
-
93
- if (cursor) {
94
- el.style.cursor = cursor;
95
- }
96
-
97
- if (transform) {
98
- el.style.transform = transform;
99
- }
100
-
101
- if (textColor) {
102
- el.style.color = textColor;
103
- }
104
- if (textAlign) {
105
- el.style.textAlign = textAlign;
106
- }
107
-
108
- if (borderColor) {
109
- el.style.borderColor = borderColor;
110
- }
111
-
112
- if (borderWidth) {
113
- el.style.borderWidth = DCss.toString(borderWidth, scale);
114
- }
115
- if (fontWeight) {
116
- el.style.fontWeight = fontWeight + "";
117
- }
118
- if (borderStyle) {
119
- el.style.borderStyle = borderStyle;
120
- }
121
- if (fontSize) {
122
- el.style.fontSize = DCss.toString(fontSize, scale);
123
- }
124
-
125
- if (DUtil.isNumber(x)) {
126
- el.style.left = x + "%";
127
- }
128
- if (DUtil.isNumber(y)) {
129
- el.style.bottom = y + "%";
130
- }
131
-
132
- if (DUtil.isNumber(h)) {
133
- el.style.height = h + "%";
134
- }
135
- if (DUtil.isNumber(w)) {
136
- el.style.width = w + "%";
137
- }
138
-
139
- if (DCss.isLengthUnit(borderRadius)) {
140
- el.style.borderRadius = DCss.toString(borderRadius, scale);
141
- }
142
-
143
- if (letterSpacing) {
144
- el.style.letterSpacing = DCss.toString(letterSpacing, scale);
145
- }
146
- if (margin) {
147
- el.style.margin = DCss.toString(margin, scale);
148
- }
149
- if (padding) {
150
- el.style.padding = DCss.toString(padding, scale);
151
- }
152
-
153
- if (DUtil.isNumber(opacity)) {
154
- el.style.opacity = opacity + "";
155
- }
156
-
157
- if (visibility) {
158
- el.style.visibility = visibility;
159
- }
160
-
161
- return el;
162
- };
163
- // const propNames = new Set(...ElementKeyNames);
164
- // export const validKey = (keyName: string) => propNames.has(keyName);
165
- }
1
+ import { DCss } from "./css";
2
+ import { DUtil } from "../utils/DUtil";
3
+
4
+ export type PStyle = Partial<DStyle>;
5
+ export interface DStyle {
6
+ opacity: number;
7
+ backgroundColor: string;
8
+ visibility: "visible" | "hidden";
9
+ cursor:
10
+ | "pointer"
11
+ | "help"
12
+ | "copy"
13
+ | "wait"
14
+ | "not-allowed"
15
+ | "context-menu"
16
+ | "move"
17
+ | "grabbing"
18
+ | "grab"
19
+ | "zoom-in"
20
+ | "zoom-out"
21
+ | "none"
22
+ | "auto"
23
+ | "default";
24
+
25
+ zIndex: number;
26
+ // POSITION
27
+ h: number;
28
+ w: number;
29
+ x: number;
30
+ y: number;
31
+ height: DCss.LengthUnit;
32
+ maxHeight: DCss.LengthUnit;
33
+ minHeight: DCss.LengthUnit;
34
+ width: DCss.LengthUnit;
35
+ maxWidth: DCss.LengthUnit;
36
+ minWidth: DCss.LengthUnit;
37
+ bottom: DCss.LengthUnit;
38
+ top: DCss.LengthUnit;
39
+ left: DCss.LengthUnit;
40
+ right: DCss.LengthUnit;
41
+ boxShadow: string;
42
+
43
+ // BORDERS
44
+ borderStyle: "solid" | "none" | "dotted" | "dashed";
45
+ borderRadius: DCss.Px | DCss.Percent;
46
+ borderWidth: DCss.Px;
47
+ borderColor: string;
48
+
49
+ margin: DCss.Px | DCss.Percent;
50
+ padding: DCss.Px | DCss.Percent;
51
+ paddingLeft: DCss.Px | DCss.Percent;
52
+ paddingRight: DCss.Px | DCss.Percent;
53
+ paddingTop: DCss.Px | DCss.Percent;
54
+ paddingBottom: DCss.Px | DCss.Percent;
55
+
56
+ // Translate
57
+ transform: string;
58
+ translate: string;
59
+
60
+ // TEXT
61
+ fontSize: DCss.Px;
62
+ textColor: string;
63
+ fontWeight: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
64
+ textAlign: "right" | "left" | "center";
65
+ letterSpacing: DCss.Px;
66
+ lineHeight: number;
67
+
68
+ // LAYOUT & POSITIONING OVERRIDE
69
+ position: "absolute" | "relative";
70
+ display: "flex" | "block";
71
+ flexDirection: "row" | "colum";
72
+ flexWrap: "nowrap" | "wrap";
73
+ justifyContent:
74
+ | "flex-start"
75
+ | "flex-end"
76
+ | "center"
77
+ | "space-around"
78
+ | "space-evenly"
79
+ | "space-between";
80
+ alignItems: "stretch" | "baseline" | "center" | "flex-start" | "flex-end";
81
+ gap: DCss.Px;
82
+ alignContent:
83
+ | "stretch"
84
+ | "center"
85
+ | "flex-start"
86
+ | "flex-end"
87
+ | "space-around"
88
+ | "space-evenly"
89
+ | "space-between";
90
+ }
91
+
92
+ export namespace DStyle {
93
+ import isNumber = DUtil.isNumber;
94
+ import isLengthUnit = DCss.isLengthUnit;
95
+ export const normalize = <T extends HTMLElement>(el: T): T => {
96
+ const s = el.style;
97
+ // el.style.padding = "";
98
+ // el.style.margin = "";
99
+ // el.style.position = "absolute";
100
+ // el.style.boxSizing = "border-box";
101
+ s.position = "absolute";
102
+ s.boxSizing = "border-box";
103
+ s.bottom = "";
104
+ s.top = "";
105
+ s.left = "";
106
+ s.right = "";
107
+ s.boxShadow = "";
108
+ s.padding = "0px";
109
+ s.margin = "0px";
110
+ s.fontSize = "12px";
111
+ s.lineHeight = "1";
112
+ s.textAlign = "center";
113
+ s.textDecoration = "none";
114
+ s.boxShadow = "";
115
+ s.boxSizing = "border-box";
116
+ s.fontWeight = "500";
117
+ s.borderStyle = "";
118
+ s.borderRadius = "";
119
+ s.borderWidth = "";
120
+ s.borderWidth = "10px";
121
+ s.borderColor = "black";
122
+ s.backgroundColor = "";
123
+ s.border = "";
124
+ s.borderStyle = "none";
125
+ s.outline = "";
126
+ s.wordSpacing = "";
127
+ s.userSelect = "none";
128
+ s.textShadow = "";
129
+ return el;
130
+ };
131
+
132
+ export const applyStyles = <T extends HTMLElement>(
133
+ el: T,
134
+ style: Partial<DStyle>,
135
+ scale: number,
136
+ ): T => {
137
+ // const scalePx = DCss.toStringCre(this.scale);
138
+ const {
139
+ x,
140
+ y,
141
+ left,
142
+ right,
143
+ top,
144
+ bottom,
145
+ backgroundColor,
146
+ borderColor,
147
+ borderWidth,
148
+ borderRadius,
149
+ borderStyle,
150
+ w,
151
+ opacity,
152
+ cursor,
153
+ fontSize,
154
+ fontWeight,
155
+ lineHeight,
156
+ textColor,
157
+ textAlign,
158
+ translate,
159
+ margin,
160
+ padding,
161
+ letterSpacing,
162
+ h,
163
+ height,
164
+ width,
165
+ transform,
166
+ visibility,
167
+ justifyContent,
168
+ alignContent,
169
+ flexWrap,
170
+ display,
171
+ flexDirection,
172
+ alignItems,
173
+ position,
174
+ paddingLeft,
175
+ paddingTop,
176
+ paddingRight,
177
+ paddingBottom,
178
+ gap,
179
+
180
+ zIndex,
181
+ //
182
+ boxShadow,
183
+ minWidth,
184
+ maxWidth,
185
+ minHeight,
186
+ maxHeight,
187
+ } = style;
188
+
189
+ if (typeof zIndex === "number") {
190
+ el.style.zIndex = "" + zIndex;
191
+ }
192
+
193
+ if (DCss.isLengthUnit(minWidth)) {
194
+ el.style.minWidth = DCss.toString(minWidth, scale);
195
+ }
196
+
197
+ if (DCss.isLengthUnit(maxWidth)) {
198
+ el.style.maxWidth = DCss.toString(maxWidth, scale);
199
+ }
200
+
201
+ if (typeof lineHeight === "number") {
202
+ el.style.lineHeight = "" + lineHeight;
203
+ }
204
+
205
+ if (DCss.isLengthUnit(minHeight)) {
206
+ el.style.minHeight = DCss.toString(minHeight, scale);
207
+ }
208
+
209
+ if (DCss.isLengthUnit(maxHeight)) {
210
+ el.style.maxHeight = DCss.toString(maxHeight, scale);
211
+ }
212
+
213
+ if (boxShadow) {
214
+ el.style.boxShadow = boxShadow;
215
+ }
216
+
217
+ if (gap) {
218
+ el.style.gap = DCss.toString(gap, scale);
219
+ }
220
+
221
+ if (paddingLeft) {
222
+ el.style.paddingLeft = DCss.toString(paddingLeft, scale);
223
+ }
224
+
225
+ if (paddingRight) {
226
+ el.style.paddingRight = DCss.toString(paddingRight, scale);
227
+ }
228
+
229
+ if (paddingTop) {
230
+ el.style.paddingTop = DCss.toString(paddingTop, scale);
231
+ }
232
+
233
+ if (paddingBottom) {
234
+ el.style.paddingBottom = DCss.toString(paddingBottom, scale);
235
+ }
236
+
237
+ if (position) {
238
+ el.style.position = position;
239
+ }
240
+ if (justifyContent) {
241
+ el.style.justifyContent = justifyContent;
242
+ }
243
+
244
+ if (alignContent) {
245
+ el.style.alignContent = alignContent;
246
+ }
247
+ if (flexWrap) {
248
+ el.style.flexWrap = flexWrap;
249
+ }
250
+ if (display) {
251
+ el.style.display = display;
252
+ }
253
+ if (flexDirection) {
254
+ el.style.flexDirection = flexDirection;
255
+ }
256
+ if (alignItems) {
257
+ el.style.alignItems = alignItems;
258
+ }
259
+
260
+ // this.el.style.fontWeight = '900';
261
+ if (backgroundColor) {
262
+ el.style.backgroundColor = backgroundColor;
263
+ }
264
+
265
+ if (cursor) {
266
+ el.style.cursor = cursor;
267
+ }
268
+
269
+ if (transform) {
270
+ el.style.transform = transform;
271
+ }
272
+
273
+ if (textColor) {
274
+ el.style.color = textColor;
275
+ }
276
+ if (textAlign) {
277
+ el.style.textAlign = textAlign;
278
+ }
279
+
280
+ if (borderColor) {
281
+ el.style.borderColor = borderColor;
282
+ }
283
+
284
+ if (borderWidth) {
285
+ el.style.borderWidth = DCss.toString(borderWidth, scale);
286
+ }
287
+
288
+ if (fontWeight) {
289
+ el.style.fontWeight = fontWeight + "";
290
+ }
291
+
292
+ if (borderStyle) {
293
+ el.style.borderStyle = borderStyle;
294
+ }
295
+
296
+ if (fontSize) {
297
+ el.style.fontSize = DCss.toString(fontSize, scale);
298
+ }
299
+
300
+ if (DUtil.isNumber(x)) {
301
+ el.style.left = x + "%";
302
+ }
303
+
304
+ if (DUtil.isNumber(y)) {
305
+ el.style.bottom = y + "%";
306
+ }
307
+
308
+ if (DCss.isLengthUnit(height)) {
309
+ el.style.height = DCss.toString(height, scale);
310
+ }
311
+
312
+ if (DCss.isLengthUnit(width)) {
313
+ el.style.width = DCss.toString(width, scale);
314
+ }
315
+
316
+ if (DCss.isLengthUnit(left)) {
317
+ el.style.left = DCss.toString(left, scale);
318
+ }
319
+
320
+ if (DCss.isLengthUnit(right)) {
321
+ el.style.right = DCss.toString(right, scale);
322
+ }
323
+
324
+ if (DCss.isLengthUnit(bottom)) {
325
+ el.style.bottom = DCss.toString(bottom, scale);
326
+ }
327
+
328
+ if (DCss.isLengthUnit(top)) {
329
+ el.style.top = DCss.toString(top, scale);
330
+ }
331
+
332
+ if (DUtil.isNumber(h)) {
333
+ el.style.height = DCss.toString(h, scale);
334
+ }
335
+
336
+ if (isNumber(w)) {
337
+ el.style.width = DCss.toString(w, scale);
338
+ }
339
+
340
+ if (DCss.isLengthUnit(borderRadius)) {
341
+ el.style.borderRadius = DCss.toString(borderRadius, scale);
342
+ }
343
+
344
+ if (letterSpacing) {
345
+ el.style.letterSpacing = DCss.toString(letterSpacing, scale);
346
+ }
347
+
348
+ if (margin) {
349
+ el.style.margin = DCss.toString(margin, scale);
350
+ }
351
+
352
+ if (padding) {
353
+ el.style.padding = DCss.toString(padding, scale);
354
+ }
355
+
356
+ if (isNumber(opacity)) {
357
+ el.style.opacity = opacity + "";
358
+ }
359
+
360
+ if (visibility) {
361
+ el.style.visibility = visibility;
362
+ }
363
+ return el;
364
+ };
365
+
366
+ // const propNames = new Set(...ElementKeyNames);
367
+ // export const validKey = (keyName: string) => propNames.has(keyName);
368
+ }
@@ -1,13 +1,23 @@
1
- import { DElement, DElementBaseDto } from "./DElement";
2
- import { ScaleService } from "../engine/scale";
3
-
4
- export interface DTextDto extends DElementBaseDto {
5
- readonly _tag: "p";
6
- readonly innerText: string;
7
- }
8
-
9
- export class DText extends DElement<HTMLParagraphElement> {
10
- constructor(dto: DTextDto, scale: ScaleService) {
11
- super(document.createElement("p"), dto, scale);
12
- }
13
- }
1
+ import { DElement, DElementBaseDto } from "./DElement";
2
+ import { ScaleService } from "../engine/scale";
3
+ import { ButtonClickAction } from "./button-click-action";
4
+
5
+ export interface DTextDto extends DElementBaseDto {
6
+ readonly _tag: "p";
7
+ readonly innerText: string;
8
+ }
9
+
10
+ export class DText extends DElement<HTMLParagraphElement> {
11
+ constructor(dto: DTextDto, scale: ScaleService) {
12
+ super(document.createElement("p"), dto, scale);
13
+ }
14
+
15
+ registerClickHandler(clickHandler: (action: ButtonClickAction) => void) {
16
+ this.el.onclick = () => {
17
+ const onClick = this.dto.onClick;
18
+ if (onClick) {
19
+ clickHandler(onClick);
20
+ }
21
+ };
22
+ }
23
+ }
@@ -1,25 +1,44 @@
1
- import { DElement, DElementBaseDto } from "./DElement";
2
- import { DText, DTextDto } from "./DText";
3
- import { DImg, DImgDto } from "./DImg";
4
- import { ScaleService } from "../engine/scale";
5
-
6
- export interface DDivDto extends DElementBaseDto {
7
- readonly _tag: "div";
8
- readonly children: Array<DTextDto | DImgDto>;
9
- }
10
-
11
- export class DDiv extends DElement<HTMLDivElement> {
12
- private readonly TAG = "[ DDiv ]: ";
13
- protected readonly defaultStyle = { x: 22, y: 4 };
14
- private children: Array<DText | DImg> = [];
15
-
16
- constructor(dto: DDivDto, scale: ScaleService, children: Array<DText | DImg>) {
17
- const d = document.createElement("div");
18
- super(d, dto, scale);
19
- this.children = children;
20
-
21
- this.children.forEach((child) => {
22
- child.appendYourself(this.el);
23
- });
24
- }
25
- }
1
+ import { DElement, DElementBaseDto } from "./DElement";
2
+ import { DText, DTextDto } from "./DText";
3
+ import { DImg, DImgDto } from "./DImg";
4
+ import { ScaleService } from "../engine/scale";
5
+ import { DButton, DButtonDto } from "./DButton";
6
+ import { ButtonClickAction } from "./button-click-action";
7
+
8
+ export interface DDivDto extends DElementBaseDto {
9
+ readonly _tag: "div";
10
+ readonly children: Array<DTextDto | DImgDto | DButtonDto>;
11
+ }
12
+
13
+ export class DDiv extends DElement<HTMLDivElement> {
14
+ private readonly TAG = "[ DDiv ]: ";
15
+ protected readonly defaultStyle = { x: 22, y: 4 };
16
+ private children: Array<DText | DImg | DButton> = [];
17
+
18
+ registerClickHandler(clickHandler: (action: ButtonClickAction) => void) {
19
+ this.el.onclick = () => {
20
+ const action = this.dto.onClick;
21
+ if (action) {
22
+ clickHandler(action);
23
+ }
24
+ };
25
+ this.children.forEach((child) => {
26
+ child.registerClickHandler(clickHandler);
27
+ });
28
+ }
29
+
30
+ constructor(dto: DDivDto, scale: ScaleService, children: Array<DText | DImg | DButton>) {
31
+ const d = document.createElement("div");
32
+ super(d, dto, scale);
33
+ this.children = children;
34
+
35
+ this.children.forEach((child) => {
36
+ child.appendYourself(this.el);
37
+ });
38
+ }
39
+ protected whenConstructed() {
40
+ this.children.forEach((child) => {
41
+ console.log(child);
42
+ });
43
+ }
44
+ }