react-native-divkit 0.1.0-alpha.1 → 0.1.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +200 -201
- package/dist/DivKit.d.ts.map +1 -1
- package/dist/DivKit.js +24 -7
- package/dist/DivKit.js.map +1 -1
- package/dist/actions/array.d.ts.map +1 -1
- package/dist/actions/array.js +1 -1
- package/dist/actions/array.js.map +1 -1
- package/dist/actions/copyToClipboard.d.ts.map +1 -1
- package/dist/actions/copyToClipboard.js +2 -1
- package/dist/actions/copyToClipboard.js.map +1 -1
- package/dist/actions/dict.d.ts.map +1 -1
- package/dist/actions/dict.js.map +1 -1
- package/dist/actions/updateStructure.d.ts.map +1 -1
- package/dist/actions/updateStructure.js.map +1 -1
- package/dist/components/container/DivContainer.d.ts.map +1 -1
- package/dist/components/container/DivContainer.js +3 -5
- package/dist/components/container/DivContainer.js.map +1 -1
- package/dist/components/image/DivImage.d.ts.map +1 -1
- package/dist/components/image/DivImage.js +1 -6
- package/dist/components/image/DivImage.js.map +1 -1
- package/dist/components/state/DivState.d.ts.map +1 -1
- package/dist/components/state/DivState.js +3 -5
- package/dist/components/state/DivState.js.map +1 -1
- package/dist/components/text/DivText.d.ts.map +1 -1
- package/dist/components/text/DivText.js +4 -4
- package/dist/components/text/DivText.js.map +1 -1
- package/dist/components/utilities/Background.d.ts +11 -0
- package/dist/components/utilities/Background.d.ts.map +1 -0
- package/dist/components/utilities/Background.js +73 -0
- package/dist/components/utilities/Background.js.map +1 -0
- package/dist/components/utilities/Outer.d.ts.map +1 -1
- package/dist/components/utilities/Outer.js +24 -10
- package/dist/components/utilities/Outer.js.map +1 -1
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js.map +1 -1
- package/dist/expressions/eval.d.ts.map +1 -1
- package/dist/expressions/eval.js +19 -11
- package/dist/expressions/eval.js.map +1 -1
- package/dist/expressions/funcs/array.d.ts.map +1 -1
- package/dist/expressions/funcs/array.js +72 -168
- package/dist/expressions/funcs/array.js.map +1 -1
- package/dist/expressions/funcs/colors.d.ts.map +1 -1
- package/dist/expressions/funcs/colors.js.map +1 -1
- package/dist/expressions/funcs/customFuncs.d.ts.map +1 -1
- package/dist/expressions/funcs/customFuncs.js +6 -4
- package/dist/expressions/funcs/customFuncs.js.map +1 -1
- package/dist/expressions/funcs/datetime.d.ts.map +1 -1
- package/dist/expressions/funcs/datetime.js +1 -1
- package/dist/expressions/funcs/datetime.js.map +1 -1
- package/dist/expressions/funcs/dict.d.ts.map +1 -1
- package/dist/expressions/funcs/dict.js.map +1 -1
- package/dist/expressions/funcs/funcs.d.ts.map +1 -1
- package/dist/expressions/funcs/funcs.js +21 -13
- package/dist/expressions/funcs/funcs.js.map +1 -1
- package/dist/expressions/funcs/math.d.ts.map +1 -1
- package/dist/expressions/funcs/math.js +40 -20
- package/dist/expressions/funcs/math.js.map +1 -1
- package/dist/expressions/funcs/std.d.ts.map +1 -1
- package/dist/expressions/funcs/std.js +4 -4
- package/dist/expressions/funcs/std.js.map +1 -1
- package/dist/expressions/funcs/strings.d.ts.map +1 -1
- package/dist/expressions/funcs/strings.js +1 -2
- package/dist/expressions/funcs/strings.js.map +1 -1
- package/dist/expressions/funcs/trigonometry.js +2 -2
- package/dist/expressions/funcs/trigonometry.js.map +1 -1
- package/dist/expressions/json.d.ts +2 -2
- package/dist/expressions/json.d.ts.map +1 -1
- package/dist/expressions/json.js +6 -4
- package/dist/expressions/json.js.map +1 -1
- package/dist/expressions/utils.d.ts.map +1 -1
- package/dist/expressions/utils.js +9 -10
- package/dist/expressions/utils.js.map +1 -1
- package/dist/expressions/variable.d.ts.map +1 -1
- package/dist/expressions/variable.js +3 -7
- package/dist/expressions/variable.js.map +1 -1
- package/dist/expressions/walk.d.ts.map +1 -1
- package/dist/expressions/walk.js.map +1 -1
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/useAction.d.ts.map +1 -1
- package/dist/hooks/useAction.js.map +1 -1
- package/dist/hooks/useDerivedFromVars.d.ts.map +1 -1
- package/dist/hooks/useDerivedFromVars.js.map +1 -1
- package/dist/hooks/useVariable.d.ts.map +1 -1
- package/dist/hooks/useVariable.js +4 -4
- package/dist/hooks/useVariable.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/stores/createObservable.d.ts.map +1 -1
- package/dist/stores/createObservable.js.map +1 -1
- package/dist/utils/applyTemplate.d.ts +2 -2
- package/dist/utils/applyTemplate.d.ts.map +1 -1
- package/dist/utils/applyTemplate.js +13 -13
- package/dist/utils/applyTemplate.js.map +1 -1
- package/dist/utils/correctColor.d.ts.map +1 -1
- package/dist/utils/correctColor.js +8 -6
- package/dist/utils/correctColor.js.map +1 -1
- package/dist/utils/formatDate.d.ts.map +1 -1
- package/dist/utils/formatDate.js +7 -10
- package/dist/utils/formatDate.js.map +1 -1
- package/dist/utils/wrapError.d.ts.map +1 -1
- package/dist/utils/wrapError.js.map +1 -1
- package/package.json +8 -7
- package/src/DivKit.tsx +258 -220
- package/src/actions/array.ts +91 -64
- package/src/actions/copyToClipboard.ts +28 -19
- package/src/actions/dict.ts +36 -26
- package/src/actions/updateStructure.ts +86 -61
- package/src/components/README.md +38 -18
- package/src/components/container/DivContainer.tsx +4 -14
- package/src/components/image/DivImage.tsx +1 -11
- package/src/components/state/DivState.tsx +3 -9
- package/src/components/text/DivText.tsx +8 -20
- package/src/components/utilities/Background.tsx +120 -0
- package/src/components/utilities/Outer.tsx +24 -23
- package/src/components/utilities/README.md +37 -32
- package/src/context/index.ts +2 -11
- package/src/expressions/ast.d.ts +16 -9
- package/src/expressions/eval.ts +82 -37
- package/src/expressions/funcs/array.ts +129 -209
- package/src/expressions/funcs/colors.ts +1 -3
- package/src/expressions/funcs/customFuncs.ts +6 -4
- package/src/expressions/funcs/datetime.ts +10 -3
- package/src/expressions/funcs/dict.ts +16 -2
- package/src/expressions/funcs/funcs.ts +75 -89
- package/src/expressions/funcs/math.ts +103 -43
- package/src/expressions/funcs/std.ts +4 -7
- package/src/expressions/funcs/strings.ts +9 -25
- package/src/expressions/funcs/trigonometry.ts +2 -2
- package/src/expressions/json.ts +60 -53
- package/src/expressions/utils.ts +24 -22
- package/src/expressions/variable.ts +5 -21
- package/src/expressions/walk.ts +6 -3
- package/src/hooks/README.md +61 -53
- package/src/hooks/index.ts +3 -18
- package/src/hooks/useAction.ts +1 -3
- package/src/hooks/useDerivedFromVars.ts +3 -13
- package/src/hooks/useVariable.ts +7 -17
- package/src/index.ts +10 -48
- package/src/stores/createObservable.ts +35 -35
- package/src/types/alignment.d.ts +15 -6
- package/src/types/background.d.ts +6 -2
- package/src/types/base.d.ts +41 -9
- package/src/types/componentContext.d.ts +27 -22
- package/src/types/container.d.ts +1 -4
- package/src/types/text.d.ts +1 -1
- package/src/utils/applyTemplate.ts +103 -109
- package/src/utils/correctColor.ts +9 -8
- package/src/utils/formatDate.ts +175 -86
- package/src/utils/wrapError.ts +7 -4
|
@@ -7,46 +7,46 @@ export type Subscriber<T> = (value: T) => void;
|
|
|
7
7
|
export type Unsubscriber = () => void;
|
|
8
8
|
|
|
9
9
|
export class Observable<T> {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
private value: T;
|
|
11
|
+
private subscribers = new Set<Subscriber<T>>();
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
constructor(initialValue: T) {
|
|
14
|
+
this.value = initialValue;
|
|
15
|
+
}
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Subscribe to value changes
|
|
19
|
+
* @param callback Function called immediately with current value and on each change
|
|
20
|
+
* @returns Unsubscribe function
|
|
21
|
+
*/
|
|
22
|
+
subscribe(callback: Subscriber<T>): Unsubscriber {
|
|
23
|
+
this.subscribers.add(callback);
|
|
24
|
+
// Immediately call with current value (Svelte store behavior)
|
|
25
|
+
callback(this.value);
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
return () => {
|
|
28
|
+
this.subscribers.delete(callback);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Update the value and notify all subscribers
|
|
34
|
+
* @param newValue New value to set
|
|
35
|
+
*/
|
|
36
|
+
set(newValue: T): void {
|
|
37
|
+
if (this.value !== newValue) {
|
|
38
|
+
this.value = newValue;
|
|
39
|
+
this.subscribers.forEach(cb => cb(newValue));
|
|
40
|
+
}
|
|
40
41
|
}
|
|
41
|
-
}
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Get current value without subscribing
|
|
45
|
+
* @returns Current value
|
|
46
|
+
*/
|
|
47
|
+
get(): T {
|
|
48
|
+
return this.value;
|
|
49
|
+
}
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
|
@@ -55,7 +55,7 @@ export class Observable<T> {
|
|
|
55
55
|
* @returns Observable instance
|
|
56
56
|
*/
|
|
57
57
|
export function writable<T>(initialValue: T): Observable<T> {
|
|
58
|
-
|
|
58
|
+
return new Observable(initialValue);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
package/src/types/alignment.d.ts
CHANGED
|
@@ -3,11 +3,20 @@ export type AlignmentHorizontal = 'left' | 'center' | 'right' | 'start' | 'end';
|
|
|
3
3
|
export type AlignmentVertical = 'top' | 'center' | 'bottom' | 'baseline';
|
|
4
4
|
|
|
5
5
|
export type ContentAlignmentHorizontal =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
| 'left'
|
|
7
|
+
| 'center'
|
|
8
|
+
| 'right'
|
|
9
|
+
| 'start'
|
|
10
|
+
| 'end'
|
|
11
|
+
| 'space-between'
|
|
12
|
+
| 'space-around'
|
|
13
|
+
| 'space-evenly';
|
|
9
14
|
|
|
10
15
|
export type ContentAlignmentVertical =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
'
|
|
16
|
+
| 'top'
|
|
17
|
+
| 'center'
|
|
18
|
+
| 'bottom'
|
|
19
|
+
| 'space-between'
|
|
20
|
+
| 'space-around'
|
|
21
|
+
| 'space-evenly'
|
|
22
|
+
| 'baseline';
|
|
@@ -67,5 +67,9 @@ export interface RadialBackground {
|
|
|
67
67
|
center_y?: RadialGradientCenter;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
export type Background =
|
|
71
|
-
|
|
70
|
+
export type Background =
|
|
71
|
+
| GradientBackground
|
|
72
|
+
| ImageBackground
|
|
73
|
+
| SolidBackground
|
|
74
|
+
| RadialBackground
|
|
75
|
+
| NinePatchImageBackground;
|
package/src/types/base.d.ts
CHANGED
|
@@ -4,12 +4,34 @@ import type { Border } from './border';
|
|
|
4
4
|
import type { Background } from './background';
|
|
5
5
|
import type { EdgeInsets } from './edgeInserts';
|
|
6
6
|
import type { Dimension } from './sizes';
|
|
7
|
-
import type {
|
|
7
|
+
import type {
|
|
8
|
+
Action,
|
|
9
|
+
AnimatorDirection,
|
|
10
|
+
AnimatorRepeatCount,
|
|
11
|
+
BooleanInt,
|
|
12
|
+
DisappearAction,
|
|
13
|
+
DivFunction,
|
|
14
|
+
DivVariable,
|
|
15
|
+
Interpolation,
|
|
16
|
+
VariableTrigger,
|
|
17
|
+
VisibilityAction
|
|
18
|
+
} from '../../typings/common';
|
|
8
19
|
import type { Focus } from './focus';
|
|
9
20
|
import type { Animation } from './animation';
|
|
10
21
|
|
|
11
|
-
export type AccessibilityType =
|
|
12
|
-
|
|
22
|
+
export type AccessibilityType =
|
|
23
|
+
| 'none'
|
|
24
|
+
| 'button'
|
|
25
|
+
| 'image'
|
|
26
|
+
| 'text'
|
|
27
|
+
| 'edit_text'
|
|
28
|
+
| 'header'
|
|
29
|
+
| 'tab_bar'
|
|
30
|
+
| 'list'
|
|
31
|
+
| 'select'
|
|
32
|
+
| 'checkbox'
|
|
33
|
+
| 'radio'
|
|
34
|
+
| 'auto';
|
|
13
35
|
|
|
14
36
|
export type AccessibilityMode = 'default' | 'merge' | 'exclude';
|
|
15
37
|
|
|
@@ -64,10 +86,12 @@ export interface ChangeBoundsTransition extends TransitionBase {
|
|
|
64
86
|
type: 'change_bounds';
|
|
65
87
|
}
|
|
66
88
|
|
|
67
|
-
export type TransitionChange =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
89
|
+
export type TransitionChange =
|
|
90
|
+
| ChangeBoundsTransition
|
|
91
|
+
| {
|
|
92
|
+
type: 'set';
|
|
93
|
+
items: TransitionChange[];
|
|
94
|
+
};
|
|
71
95
|
|
|
72
96
|
export interface PivotFixedValue {
|
|
73
97
|
type: 'pivot-fixed';
|
|
@@ -133,8 +157,16 @@ export type TooltipMode = TooltipModeModal | TooltipModeNonModal;
|
|
|
133
157
|
export interface Tooltip {
|
|
134
158
|
id: string;
|
|
135
159
|
div: DivBaseData;
|
|
136
|
-
position:
|
|
137
|
-
|
|
160
|
+
position:
|
|
161
|
+
| 'left'
|
|
162
|
+
| 'top-left'
|
|
163
|
+
| 'top'
|
|
164
|
+
| 'top-right'
|
|
165
|
+
| 'right'
|
|
166
|
+
| 'bottom-right'
|
|
167
|
+
| 'bottom'
|
|
168
|
+
| 'bottom-left'
|
|
169
|
+
| 'center';
|
|
138
170
|
duration?: number;
|
|
139
171
|
offset?: {
|
|
140
172
|
x: {
|
|
@@ -28,10 +28,12 @@ export interface PagerRegisterData {
|
|
|
28
28
|
destroy: () => void;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export type ComponentKey =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
export type ComponentKey =
|
|
32
|
+
| string
|
|
33
|
+
| {
|
|
34
|
+
index: number;
|
|
35
|
+
data: object;
|
|
36
|
+
};
|
|
35
37
|
|
|
36
38
|
export interface ComponentContext<T extends DivBaseData = DivBaseData> {
|
|
37
39
|
path: string[];
|
|
@@ -70,24 +72,27 @@ export interface ComponentContext<T extends DivBaseData = DivBaseData> {
|
|
|
70
72
|
keepComplex?: boolean,
|
|
71
73
|
maxDepth?: number
|
|
72
74
|
): Readable<MaybeMissing<T>>;
|
|
73
|
-
getJsonWithVars<T>(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
produceChildContext(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
75
|
+
getJsonWithVars<T>(jsonProp: T, additionalVars?: Map<string, Variable>, keepComplex?: boolean): MaybeMissing<T>;
|
|
76
|
+
evalExpression(
|
|
77
|
+
store: Store | undefined,
|
|
78
|
+
expr: Node,
|
|
79
|
+
opts?: {
|
|
80
|
+
weekStartDay?: number;
|
|
81
|
+
storeUsedVars?: Set<string>;
|
|
82
|
+
}
|
|
83
|
+
): ReturnType<typeof evalExpression>;
|
|
84
|
+
produceChildContext(
|
|
85
|
+
div: MaybeMissing<DivBaseData>,
|
|
86
|
+
opts?: {
|
|
87
|
+
path?: string | number | undefined;
|
|
88
|
+
isRootState?: boolean;
|
|
89
|
+
isTooltipRoot?: boolean;
|
|
90
|
+
fake?: number;
|
|
91
|
+
variables?: Map<string, Variable>;
|
|
92
|
+
id?: string;
|
|
93
|
+
key?: ComponentKey;
|
|
94
|
+
}
|
|
95
|
+
): ComponentContext;
|
|
91
96
|
dup(fakeReason: number): ComponentContext;
|
|
92
97
|
getVariable(varName: string, type?: VariableType): Variable | undefined;
|
|
93
98
|
getAnimator(name: string): MaybeMissing<Animator> | undefined;
|
package/src/types/container.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import type { DivBaseData } from './base';
|
|
2
2
|
import type { DivActionableData } from './actionable';
|
|
3
|
-
import type {
|
|
4
|
-
ContentAlignmentHorizontal,
|
|
5
|
-
ContentAlignmentVertical
|
|
6
|
-
} from './alignment';
|
|
3
|
+
import type { ContentAlignmentHorizontal, ContentAlignmentVertical } from './alignment';
|
|
7
4
|
import type { BooleanInt } from '../../typings/common';
|
|
8
5
|
import type { Drawable } from './drawable';
|
|
9
6
|
import type { DivAspect } from './image';
|
package/src/types/text.d.ts
CHANGED
|
@@ -1,145 +1,139 @@
|
|
|
1
|
-
import type { WrappedError } from
|
|
2
|
-
import type { DivBase, TemplateContext } from
|
|
3
|
-
import type { DivBaseData } from
|
|
1
|
+
import type { WrappedError } from './wrapError';
|
|
2
|
+
import type { DivBase, TemplateContext } from '../../typings/common';
|
|
3
|
+
import type { DivBaseData } from '../types/base';
|
|
4
4
|
|
|
5
5
|
export function applyTemplate<T extends DivBase>(
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
json: T,
|
|
7
|
+
templateContext: TemplateContext,
|
|
8
|
+
templates: Record<string, unknown>,
|
|
9
|
+
_logError: (error: WrappedError) => void
|
|
10
10
|
): {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
json: T;
|
|
12
|
+
templateContext: TemplateContext;
|
|
13
13
|
};
|
|
14
14
|
export function applyTemplate(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
json: DivBaseData,
|
|
16
|
+
templateContext: TemplateContext,
|
|
17
|
+
templates: Record<string, unknown>,
|
|
18
|
+
_logError: (error: WrappedError) => void
|
|
19
19
|
): {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
json: DivBaseData;
|
|
21
|
+
templateContext: TemplateContext;
|
|
22
22
|
} {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
const template = templates[json.type];
|
|
24
|
+
|
|
25
|
+
if (!template) {
|
|
26
|
+
// If template is missing, treat it as a standard component
|
|
27
|
+
return {
|
|
28
|
+
json,
|
|
29
|
+
templateContext
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
let i;
|
|
34
|
+
const newContext: TemplateContext = {};
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
for (i in templateContext) {
|
|
37
|
+
if (Object.prototype.hasOwnProperty.call(templateContext, i)) {
|
|
38
|
+
newContext[i] = templateContext[i];
|
|
39
|
+
}
|
|
39
40
|
}
|
|
40
|
-
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
for (i in json) {
|
|
43
|
+
if (i === 'type' || i === '__proto__') {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
if (Object.prototype.hasOwnProperty.call(json, i)) {
|
|
48
|
+
newContext[i] = json[i as keyof typeof json];
|
|
49
|
+
}
|
|
49
50
|
}
|
|
50
|
-
}
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
52
|
+
function copyTemplated(base: any, extender: any) {
|
|
53
|
+
const keys = Object.keys(extender).filter(key => key !== '__proto__');
|
|
54
|
+
const simpleKeys = keys.filter(key => key.charAt(0) !== '$');
|
|
55
|
+
const templateKeys = keys.filter(key => key.charAt(0) === '$');
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
57
|
+
simpleKeys.forEach(key => {
|
|
58
|
+
const item = extender[key];
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
if (typeof item === 'object' && item !== null) {
|
|
61
|
+
base[key] = Array.isArray(item) ? [] : {};
|
|
62
|
+
copyTemplated(base[key], item);
|
|
63
|
+
} else {
|
|
64
|
+
base[key] = item;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
67
|
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
templateKeys.forEach(key => {
|
|
69
|
+
const item = extender[key];
|
|
70
70
|
|
|
71
|
-
|
|
71
|
+
const val = newContext[item];
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
73
|
+
if (val !== undefined) {
|
|
74
|
+
const prop = key.substring(1);
|
|
75
|
+
base[prop] = val;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
return base;
|
|
80
|
+
}
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
const newJson = copyTemplated({}, template);
|
|
83
83
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
for (i in json) {
|
|
85
|
+
if (i === 'type' || i === '__proto__') {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
88
|
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
if (Object.prototype.hasOwnProperty.call(json, i)) {
|
|
90
|
+
newJson[i] = json[i as keyof typeof json];
|
|
91
|
+
}
|
|
91
92
|
}
|
|
92
|
-
}
|
|
93
93
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
94
|
+
return {
|
|
95
|
+
json: newJson,
|
|
96
|
+
templateContext: newContext
|
|
97
|
+
};
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
export function applyTemplatesRecursively(
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
json: any,
|
|
102
|
+
templates: Record<string, unknown>,
|
|
103
|
+
logError: (error: WrappedError) => void
|
|
104
104
|
): any {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
applyTemplatesRecursively(item, templates, logError),
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
let resolvedJson = json;
|
|
116
|
-
|
|
117
|
-
// Attempt expansion if type matches a template
|
|
118
|
-
if (json.type && templates[json.type]) {
|
|
119
|
-
try {
|
|
120
|
-
const res = applyTemplate(json, {}, templates, logError);
|
|
121
|
-
resolvedJson = res.json;
|
|
122
|
-
} catch (_e) {
|
|
123
|
-
// Expansion failed
|
|
105
|
+
if (!json || typeof json !== 'object') {
|
|
106
|
+
return json;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (Array.isArray(json)) {
|
|
110
|
+
return json.map(item => applyTemplatesRecursively(item, templates, logError));
|
|
124
111
|
}
|
|
125
|
-
}
|
|
126
112
|
|
|
127
|
-
|
|
128
|
-
const result: any = {};
|
|
129
|
-
for (const key in resolvedJson) {
|
|
130
|
-
if (!Object.prototype.hasOwnProperty.call(resolvedJson, key)) continue;
|
|
113
|
+
let resolvedJson = json;
|
|
131
114
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
115
|
+
// Attempt expansion if type matches a template
|
|
116
|
+
if (json.type && templates[json.type]) {
|
|
117
|
+
try {
|
|
118
|
+
const res = applyTemplate(json, {}, templates, logError);
|
|
119
|
+
resolvedJson = res.json;
|
|
120
|
+
} catch (_e) {
|
|
121
|
+
// Expansion failed
|
|
122
|
+
}
|
|
135
123
|
}
|
|
136
124
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
125
|
+
// Recursively process children
|
|
126
|
+
const result: any = {};
|
|
127
|
+
for (const key in resolvedJson) {
|
|
128
|
+
if (!Object.prototype.hasOwnProperty.call(resolvedJson, key)) continue;
|
|
129
|
+
|
|
130
|
+
if (key === 'type') {
|
|
131
|
+
result[key] = resolvedJson[key];
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
result[key] = applyTemplatesRecursively(resolvedJson[key], templates, logError);
|
|
136
|
+
}
|
|
143
137
|
|
|
144
|
-
|
|
138
|
+
return result;
|
|
145
139
|
}
|
|
@@ -8,7 +8,7 @@ import { padLeft } from './padLeft';
|
|
|
8
8
|
* @returns Color with opacity if it has correct format, or defaultColor elsewhere
|
|
9
9
|
*/
|
|
10
10
|
export function correctColor(color: string | undefined, alpha = 1, defaultColor = 'transparent'): string {
|
|
11
|
-
color = (typeof color === 'string' && color || '').toLowerCase();
|
|
11
|
+
color = ((typeof color === 'string' && color) || '').toLowerCase();
|
|
12
12
|
|
|
13
13
|
if (color.charAt(0) !== '#') {
|
|
14
14
|
return defaultColor;
|
|
@@ -24,7 +24,7 @@ export function correctColor(color: string | undefined, alpha = 1, defaultColor
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export function correctColorWithAlpha(color: string | undefined, alpha: number, defaultColor = 'transparent'): string {
|
|
27
|
-
color = (typeof color === 'string' && color || '').toLowerCase();
|
|
27
|
+
color = ((typeof color === 'string' && color) || '').toLowerCase();
|
|
28
28
|
|
|
29
29
|
if (color.charAt(0) !== '#') {
|
|
30
30
|
return defaultColor;
|
|
@@ -48,16 +48,18 @@ export interface ParsedColor {
|
|
|
48
48
|
|
|
49
49
|
export function stringifyColorToCss(color: ParsedColor): string {
|
|
50
50
|
if (color.a === 255) {
|
|
51
|
-
return `#${[color.r, color.g, color.b]
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
return `#${[color.r, color.g, color.b]
|
|
52
|
+
.map(it => {
|
|
53
|
+
return padLeft(Math.round(it).toString(16), 2);
|
|
54
|
+
})
|
|
55
|
+
.join('')}`;
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
return `rgba(${color.r},${color.g},${color.b},${(color.a / 255).toFixed(2)})`;
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
export function parseColor(color: string): ParsedColor | null {
|
|
60
|
-
const colorMatch =
|
|
62
|
+
const colorMatch =
|
|
61
63
|
// #AARRGGBB
|
|
62
64
|
color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i) ||
|
|
63
65
|
// #ARGB
|
|
@@ -65,8 +67,7 @@ export function parseColor(color: string): ParsedColor | null {
|
|
|
65
67
|
// #RRGGBB
|
|
66
68
|
color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i) ||
|
|
67
69
|
// #RGB
|
|
68
|
-
color.match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i)
|
|
69
|
-
);
|
|
70
|
+
color.match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i);
|
|
70
71
|
|
|
71
72
|
if (colorMatch) {
|
|
72
73
|
// with alpha part in color
|