react-native-live-widgets 1.1.0 → 1.2.0
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/CHANGELOG.md +20 -0
- package/README.md +76 -10
- package/android/build.gradle +14 -20
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +5 -1
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityHeadlessTaskService.kt +8 -0
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +4 -1
- package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +29 -3
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +26 -1
- package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +51 -15
- package/android/src/main/res/layout/rn_live_widget_host.xml +64 -74
- package/docs/GUIDE-EN.md +109 -7
- package/docs/GUIDE-VI.md +109 -7
- package/docs/index.html +440 -0
- package/docs/llms-full.txt +132 -0
- package/docs/llms.txt +88 -0
- package/ios/Models/DynamicLayoutModels.swift +5 -0
- package/ios/Widgets/DynamicHomeScreenWidget.swift +2 -3
- package/lib/builder.d.ts.map +1 -1
- package/lib/builder.js +29 -19
- package/lib/builder.js.map +1 -1
- package/lib/dispatcher.d.ts +7 -0
- package/lib/dispatcher.d.ts.map +1 -1
- package/lib/dispatcher.js +11 -0
- package/lib/dispatcher.js.map +1 -1
- package/lib/hooks.d.ts +66 -0
- package/lib/hooks.d.ts.map +1 -0
- package/lib/hooks.js +166 -0
- package/lib/hooks.js.map +1 -0
- package/lib/index.d.ts +32 -7
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +115 -8
- package/lib/index.js.map +1 -1
- package/lib/jsx.d.ts +79 -0
- package/lib/jsx.d.ts.map +1 -0
- package/lib/jsx.js +196 -0
- package/lib/jsx.js.map +1 -0
- package/lib/preview.d.ts +54 -0
- package/lib/preview.d.ts.map +1 -0
- package/lib/preview.js +466 -0
- package/lib/preview.js.map +1 -0
- package/lib/types.d.ts +8 -0
- package/lib/types.d.ts.map +1 -1
- package/package.json +3 -1
- package/src/builder.ts +29 -19
- package/src/dispatcher.ts +17 -0
- package/src/hooks.ts +223 -0
- package/src/index.ts +137 -13
- package/src/jsx.tsx +296 -0
- package/src/preview.tsx +719 -0
- package/src/react-ambient.d.ts +10 -0
- package/src/react-native-env.d.ts +43 -0
- package/src/types.ts +8 -0
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import React from 'react';
|
|
1
2
|
import { AppRegistry, NativeEventEmitter, NativeModules, Platform } from 'react-native';
|
|
2
3
|
import {
|
|
3
4
|
ActiveActivityInfo,
|
|
@@ -31,9 +32,21 @@ const TOKEN_CHANGED_EVENT = 'LiveActivityTokenChanged';
|
|
|
31
32
|
const UPDATE_TOKEN_CHANGED_EVENT = 'LiveActivityUpdateTokenChanged';
|
|
32
33
|
const ACTION_CLICKED_EVENT = 'LiveActivityActionClicked';
|
|
33
34
|
|
|
34
|
-
import {
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
import {
|
|
36
|
+
ActionHandler,
|
|
37
|
+
GlobalActionHandler,
|
|
38
|
+
actionHandlers,
|
|
39
|
+
globalActionHandlers,
|
|
40
|
+
registerAction,
|
|
41
|
+
registerActionHandler,
|
|
42
|
+
unregisterAction,
|
|
43
|
+
} from './dispatcher';
|
|
44
|
+
export type { ActionHandler, GlobalActionHandler };
|
|
45
|
+
export { registerAction, registerActionHandler, unregisterAction };
|
|
46
|
+
export * from './hooks';
|
|
47
|
+
export * from './jsx';
|
|
48
|
+
export * from './preview';
|
|
49
|
+
import { renderToWidget } from './jsx';
|
|
37
50
|
|
|
38
51
|
// Tự động lắng nghe action event từ Native để dispatch tới các hàm TypeScript đã đăng ký
|
|
39
52
|
if (liveActivityEmitter) {
|
|
@@ -48,6 +61,13 @@ if (liveActivityEmitter) {
|
|
|
48
61
|
console.warn(`[react-native-live-widgets] Error executing action '${actionId}':`, e);
|
|
49
62
|
}
|
|
50
63
|
}
|
|
64
|
+
for (const globalHandler of globalActionHandlers) {
|
|
65
|
+
try {
|
|
66
|
+
await globalHandler(actionId, { actionId, activityId });
|
|
67
|
+
} catch (e) {
|
|
68
|
+
console.warn(`[react-native-live-widgets] Error in global action handler for '${actionId}':`, e);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
51
71
|
});
|
|
52
72
|
}
|
|
53
73
|
|
|
@@ -60,7 +80,11 @@ export const LiveActivity = {
|
|
|
60
80
|
start: async (options: StartActivityOptions): Promise<boolean> => {
|
|
61
81
|
if (!LiveActivityModule?.start) return false;
|
|
62
82
|
try {
|
|
63
|
-
const
|
|
83
|
+
const opts = { ...options } as any;
|
|
84
|
+
if (React.isValidElement(opts.lockScreen)) {
|
|
85
|
+
opts.lockScreen = renderToWidget(opts.lockScreen);
|
|
86
|
+
}
|
|
87
|
+
const payload = JSON.stringify(opts);
|
|
64
88
|
return await LiveActivityModule.start(options.activityId, payload);
|
|
65
89
|
} catch (error) {
|
|
66
90
|
console.warn('[react-native-live-widgets] start error:', error);
|
|
@@ -74,7 +98,11 @@ export const LiveActivity = {
|
|
|
74
98
|
update: async (options: UpdateActivityOptions): Promise<boolean> => {
|
|
75
99
|
if (!LiveActivityModule?.update) return false;
|
|
76
100
|
try {
|
|
77
|
-
const
|
|
101
|
+
const opts = { ...options } as any;
|
|
102
|
+
if (React.isValidElement(opts.lockScreen)) {
|
|
103
|
+
opts.lockScreen = renderToWidget(opts.lockScreen);
|
|
104
|
+
}
|
|
105
|
+
const payload = JSON.stringify(opts);
|
|
78
106
|
return await LiveActivityModule.update(options.activityId, payload);
|
|
79
107
|
} catch (error) {
|
|
80
108
|
console.warn('[react-native-live-widgets] update error:', error);
|
|
@@ -249,6 +277,14 @@ export const LiveActivity = {
|
|
|
249
277
|
};
|
|
250
278
|
},
|
|
251
279
|
|
|
280
|
+
/**
|
|
281
|
+
* Đăng ký hàm lắng nghe toàn cục tất cả các sự kiện bấm nút từ Live Activity hoặc Widget.
|
|
282
|
+
* Nhận `(actionId, payload)`. Trả về hàm hủy đăng ký (unsubscribe).
|
|
283
|
+
*/
|
|
284
|
+
registerActionHandler: (handler: GlobalActionHandler): (() => void) => {
|
|
285
|
+
return registerActionHandler(handler);
|
|
286
|
+
},
|
|
287
|
+
|
|
252
288
|
/**
|
|
253
289
|
* Hủy đăng ký action handler theo `actionId`.
|
|
254
290
|
*/
|
|
@@ -346,6 +382,13 @@ export const LiveActivity = {
|
|
|
346
382
|
if (registered) {
|
|
347
383
|
await registered({ actionId, activityId });
|
|
348
384
|
}
|
|
385
|
+
for (const globalHandler of globalActionHandlers) {
|
|
386
|
+
try {
|
|
387
|
+
await globalHandler(actionId, { actionId, activityId });
|
|
388
|
+
} catch (e) {
|
|
389
|
+
console.warn('[react-native-live-widgets] background globalHandler error:', e);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
349
392
|
}
|
|
350
393
|
);
|
|
351
394
|
} catch (e) {
|
|
@@ -386,15 +429,53 @@ export const Widget = {
|
|
|
386
429
|
|
|
387
430
|
/**
|
|
388
431
|
* Thiết lập layout động cho Home Screen Widget (hỗ trợ small, medium, large).
|
|
389
|
-
*
|
|
432
|
+
* Hỗ trợ gọi 2 hoặc 3 tham số:
|
|
433
|
+
* - `Widget.setLayout('main', layoutConfig, appGroup?)`
|
|
434
|
+
* - `Widget.setLayout(layoutConfig, appGroup?)` (tự động gán widgetKey = 'main')
|
|
435
|
+
* Tự động lưu vào Shared Storage và yêu cầu OS vẽ lại.
|
|
390
436
|
*/
|
|
391
437
|
setLayout: async (
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
438
|
+
widgetKeyOrConfig: string | WidgetLayoutConfig,
|
|
439
|
+
layoutConfigOrAppGroup?: WidgetLayoutConfig | string,
|
|
440
|
+
maybeAppGroup?: string
|
|
395
441
|
): Promise<boolean> => {
|
|
396
442
|
try {
|
|
397
|
-
|
|
443
|
+
let widgetKey = 'main';
|
|
444
|
+
let layoutConfig: WidgetLayoutConfig;
|
|
445
|
+
let appGroup: string | undefined;
|
|
446
|
+
|
|
447
|
+
if (typeof widgetKeyOrConfig === 'string') {
|
|
448
|
+
widgetKey = widgetKeyOrConfig;
|
|
449
|
+
layoutConfig = layoutConfigOrAppGroup as WidgetLayoutConfig;
|
|
450
|
+
appGroup = maybeAppGroup;
|
|
451
|
+
} else {
|
|
452
|
+
layoutConfig = widgetKeyOrConfig;
|
|
453
|
+
appGroup = typeof layoutConfigOrAppGroup === 'string' ? layoutConfigOrAppGroup : undefined;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
let finalLayout: any = layoutConfig;
|
|
457
|
+
const convertIfJsx = (node: any) => {
|
|
458
|
+
if (!node) return node;
|
|
459
|
+
if (React.isValidElement(node) || (node as any)?.$$typeof || typeof (node as any)?.type === 'function') {
|
|
460
|
+
return renderToWidget(node);
|
|
461
|
+
}
|
|
462
|
+
return node;
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
if (React.isValidElement(finalLayout) || (finalLayout as any)?.$$typeof || typeof (finalLayout as any)?.type === 'function') {
|
|
466
|
+
finalLayout = { medium: renderToWidget(finalLayout) };
|
|
467
|
+
} else if (finalLayout && typeof finalLayout === 'object') {
|
|
468
|
+
const c = { ...finalLayout };
|
|
469
|
+
c.small = convertIfJsx(c.small);
|
|
470
|
+
c.medium = convertIfJsx(c.medium);
|
|
471
|
+
c.large = convertIfJsx(c.large);
|
|
472
|
+
c.accessoryRectangular = convertIfJsx(c.accessoryRectangular);
|
|
473
|
+
c.accessoryCircular = convertIfJsx(c.accessoryCircular);
|
|
474
|
+
c.accessoryInline = convertIfJsx(c.accessoryInline);
|
|
475
|
+
finalLayout = c;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const saved = await SharedStorage.set(`widget_layout_${widgetKey}`, finalLayout, appGroup);
|
|
398
479
|
if (!saved) return false;
|
|
399
480
|
return await Widget.reloadAll();
|
|
400
481
|
} catch (error) {
|
|
@@ -403,6 +484,17 @@ export const Widget = {
|
|
|
403
484
|
}
|
|
404
485
|
},
|
|
405
486
|
|
|
487
|
+
/**
|
|
488
|
+
* Cập nhật layout động cho Widget (Alias tiện ích cho `Widget.setLayout`).
|
|
489
|
+
*/
|
|
490
|
+
update: async (
|
|
491
|
+
widgetKeyOrConfig: string | WidgetLayoutConfig,
|
|
492
|
+
layoutConfigOrAppGroup?: WidgetLayoutConfig | string,
|
|
493
|
+
maybeAppGroup?: string
|
|
494
|
+
): Promise<boolean> => {
|
|
495
|
+
return (Widget.setLayout as any)(widgetKeyOrConfig, layoutConfigOrAppGroup, maybeAppGroup);
|
|
496
|
+
},
|
|
497
|
+
|
|
406
498
|
/**
|
|
407
499
|
* Thiết lập dòng thời gian tự động (Timeline Entries) cho Widget.
|
|
408
500
|
* Hệ điều hành iOS/Android sẽ tự động chuyển đổi giữa các layout theo từng mốc thời gian
|
|
@@ -490,13 +582,36 @@ export const Widget = {
|
|
|
490
582
|
* Cấu hình Endpoint API để bản thân Widget Extension tự chạy ngầm lấy dữ liệu mới
|
|
491
583
|
* theo chu kỳ (`getTimeline`) hoặc khi bấm nút `actionId: 'refresh'` trên mặt Widget,
|
|
492
584
|
* kể cả khi người dùng đã vuốt tắt (Kill) hoàn toàn ứng dụng chính!
|
|
585
|
+
*
|
|
586
|
+
* Hỗ trợ gọi linh hoạt:
|
|
587
|
+
* - `Widget.setRemoteConfig('main', { url, refreshIntervalMinutes }, appGroup?)`
|
|
588
|
+
* - `Widget.setRemoteConfig({ url, intervalMinutes }, appGroup?)` (tự động gán widgetKey = 'main')
|
|
493
589
|
*/
|
|
494
590
|
setRemoteConfig: async (
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
591
|
+
widgetKeyOrConfig: string | WidgetRemoteConfig,
|
|
592
|
+
remoteConfigOrAppGroup?: WidgetRemoteConfig | string,
|
|
593
|
+
maybeAppGroup?: string
|
|
498
594
|
): Promise<boolean> => {
|
|
499
595
|
try {
|
|
596
|
+
let widgetKey = 'main';
|
|
597
|
+
let remoteConfig: WidgetRemoteConfig;
|
|
598
|
+
let appGroup: string | undefined;
|
|
599
|
+
|
|
600
|
+
if (typeof widgetKeyOrConfig === 'string') {
|
|
601
|
+
widgetKey = widgetKeyOrConfig;
|
|
602
|
+
remoteConfig = remoteConfigOrAppGroup as WidgetRemoteConfig;
|
|
603
|
+
appGroup = maybeAppGroup;
|
|
604
|
+
} else {
|
|
605
|
+
remoteConfig = widgetKeyOrConfig;
|
|
606
|
+
appGroup = typeof remoteConfigOrAppGroup === 'string' ? remoteConfigOrAppGroup : undefined;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
if (remoteConfig.intervalMinutes !== undefined && remoteConfig.refreshIntervalMinutes === undefined) {
|
|
610
|
+
remoteConfig.refreshIntervalMinutes = remoteConfig.intervalMinutes;
|
|
611
|
+
} else if (remoteConfig.refreshIntervalMinutes !== undefined && remoteConfig.intervalMinutes === undefined) {
|
|
612
|
+
remoteConfig.intervalMinutes = remoteConfig.refreshIntervalMinutes;
|
|
613
|
+
}
|
|
614
|
+
|
|
500
615
|
const saved = await SharedStorage.set(
|
|
501
616
|
`widget_remote_config_${widgetKey}`,
|
|
502
617
|
remoteConfig,
|
|
@@ -510,6 +625,15 @@ export const Widget = {
|
|
|
510
625
|
}
|
|
511
626
|
},
|
|
512
627
|
|
|
628
|
+
/** Đăng ký lắng nghe tương tác nút bấm từ Widget theo `actionId` */
|
|
629
|
+
registerAction: registerAction,
|
|
630
|
+
|
|
631
|
+
/** Đăng ký lắng nghe toàn cục tất cả tương tác nút bấm từ Widget (alias) */
|
|
632
|
+
onWidgetAction: registerActionHandler,
|
|
633
|
+
|
|
634
|
+
/** Đăng ký lắng nghe toàn cục tất cả tương tác nút bấm từ Widget */
|
|
635
|
+
registerActionHandler: registerActionHandler,
|
|
636
|
+
|
|
513
637
|
/**
|
|
514
638
|
* (Android 8.0+) Mở hộp thoại hệ thống cho phép người dùng ghim trực tiếp Tiện ích ra màn hình chính ngay từ trong ứng dụng.
|
|
515
639
|
* Trên iOS, hàm trả về false vì Apple chỉ cho phép người dùng tự thêm tiện ích từ thư viện Widget của hệ điều hành.
|
package/src/jsx.tsx
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import {
|
|
3
|
+
LayoutNode,
|
|
4
|
+
VStackNode,
|
|
5
|
+
HStackNode,
|
|
6
|
+
ZStackNode,
|
|
7
|
+
GridNode,
|
|
8
|
+
TextNode,
|
|
9
|
+
ButtonNode,
|
|
10
|
+
BadgeNode,
|
|
11
|
+
ProgressNode,
|
|
12
|
+
TimerNode,
|
|
13
|
+
EqualizerNode,
|
|
14
|
+
ImageNode,
|
|
15
|
+
SpacerNode,
|
|
16
|
+
DividerNode,
|
|
17
|
+
LinkNode,
|
|
18
|
+
GaugeNode,
|
|
19
|
+
ChartNode,
|
|
20
|
+
ContainerProps,
|
|
21
|
+
TextStyle,
|
|
22
|
+
ZStackProps,
|
|
23
|
+
GridProps,
|
|
24
|
+
LinkProps,
|
|
25
|
+
EqualizerProps,
|
|
26
|
+
StyleProp,
|
|
27
|
+
} from './types';
|
|
28
|
+
import { UI, flattenStyle } from './builder';
|
|
29
|
+
|
|
30
|
+
// ─── 1. JSX COMPONENT DEFINITIONS ────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
export function extractPropsAndStyle<T extends object>(props: any): any {
|
|
33
|
+
if (!props) return {};
|
|
34
|
+
const { style, children, ...rest } = props;
|
|
35
|
+
if (!style) return rest;
|
|
36
|
+
const flattened = flattenStyle(style);
|
|
37
|
+
return { ...flattened, ...rest };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface VStackProps extends ContainerProps {
|
|
41
|
+
children?: React.ReactNode;
|
|
42
|
+
style?: StyleProp<ContainerProps>;
|
|
43
|
+
}
|
|
44
|
+
export const VStack: React.FC<VStackProps> = () => null;
|
|
45
|
+
VStack.displayName = 'VStack';
|
|
46
|
+
|
|
47
|
+
export interface HStackProps extends ContainerProps {
|
|
48
|
+
children?: React.ReactNode;
|
|
49
|
+
style?: StyleProp<ContainerProps>;
|
|
50
|
+
}
|
|
51
|
+
export const HStack: React.FC<HStackProps> = () => null;
|
|
52
|
+
HStack.displayName = 'HStack';
|
|
53
|
+
|
|
54
|
+
export interface ZStackJSXProps extends ZStackProps {
|
|
55
|
+
children?: React.ReactNode;
|
|
56
|
+
style?: StyleProp<ZStackProps>;
|
|
57
|
+
}
|
|
58
|
+
export const ZStack: React.FC<ZStackJSXProps> = () => null;
|
|
59
|
+
ZStack.displayName = 'ZStack';
|
|
60
|
+
|
|
61
|
+
export interface GridJSXProps extends GridProps {
|
|
62
|
+
children?: React.ReactNode;
|
|
63
|
+
style?: StyleProp<GridProps>;
|
|
64
|
+
}
|
|
65
|
+
export const Grid: React.FC<GridJSXProps> = () => null;
|
|
66
|
+
Grid.displayName = 'Grid';
|
|
67
|
+
|
|
68
|
+
export interface TextJSXProps extends TextStyle {
|
|
69
|
+
children?: React.ReactNode;
|
|
70
|
+
text?: string;
|
|
71
|
+
style?: StyleProp<TextStyle>;
|
|
72
|
+
}
|
|
73
|
+
export const Text: React.FC<TextJSXProps> = () => null;
|
|
74
|
+
Text.displayName = 'Text';
|
|
75
|
+
|
|
76
|
+
export type ButtonJSXProps = ButtonNode['props'] & {
|
|
77
|
+
children?: React.ReactNode;
|
|
78
|
+
style?: StyleProp<ButtonNode['props']>;
|
|
79
|
+
};
|
|
80
|
+
export const Button: React.FC<ButtonJSXProps> = () => null;
|
|
81
|
+
Button.displayName = 'Button';
|
|
82
|
+
|
|
83
|
+
export type BadgeJSXProps = BadgeNode['props'] & {
|
|
84
|
+
children?: React.ReactNode;
|
|
85
|
+
style?: StyleProp<BadgeNode['props']>;
|
|
86
|
+
};
|
|
87
|
+
export const Badge: React.FC<BadgeJSXProps> = () => null;
|
|
88
|
+
Badge.displayName = 'Badge';
|
|
89
|
+
|
|
90
|
+
export type ProgressJSXProps = ProgressNode['props'] & {
|
|
91
|
+
style?: StyleProp<ProgressNode['props']>;
|
|
92
|
+
};
|
|
93
|
+
export const Progress: React.FC<ProgressJSXProps> = () => null;
|
|
94
|
+
Progress.displayName = 'Progress';
|
|
95
|
+
|
|
96
|
+
export type TimerJSXProps = TimerNode['props'] & {
|
|
97
|
+
style?: StyleProp<TimerNode['props']>;
|
|
98
|
+
};
|
|
99
|
+
export const Timer: React.FC<TimerJSXProps> = () => null;
|
|
100
|
+
Timer.displayName = 'Timer';
|
|
101
|
+
|
|
102
|
+
export type ImageJSXProps = ImageNode['props'] & {
|
|
103
|
+
style?: StyleProp<ImageNode['props']>;
|
|
104
|
+
};
|
|
105
|
+
export const Image: React.FC<ImageJSXProps> = () => null;
|
|
106
|
+
Image.displayName = 'Image';
|
|
107
|
+
|
|
108
|
+
export const Spacer: React.FC<{ minLength?: number }> = () => null;
|
|
109
|
+
Spacer.displayName = 'Spacer';
|
|
110
|
+
|
|
111
|
+
export const Divider: React.FC<DividerNode['props']> = () => null;
|
|
112
|
+
Divider.displayName = 'Divider';
|
|
113
|
+
|
|
114
|
+
export interface LinkJSXProps extends LinkProps {
|
|
115
|
+
children?: React.ReactNode;
|
|
116
|
+
style?: StyleProp<LinkProps>;
|
|
117
|
+
}
|
|
118
|
+
export const Link: React.FC<LinkJSXProps> = () => null;
|
|
119
|
+
Link.displayName = 'Link';
|
|
120
|
+
|
|
121
|
+
export const Equalizer: React.FC<EqualizerProps> = () => null;
|
|
122
|
+
Equalizer.displayName = 'Equalizer';
|
|
123
|
+
|
|
124
|
+
export const Gauge: React.FC<GaugeNode['props']> = () => null;
|
|
125
|
+
Gauge.displayName = 'Gauge';
|
|
126
|
+
|
|
127
|
+
export const Chart: React.FC<ChartNode['props']> = () => null;
|
|
128
|
+
Chart.displayName = 'Chart';
|
|
129
|
+
|
|
130
|
+
// ─── 2. RECURSIVE JSX -> LAYOUTNODE CONVERTER ────────────────────────────────
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Chuyển đổi một cây JSX/TSX thành cấu trúc cây LayoutNode chuẩn của thư viện.
|
|
134
|
+
*
|
|
135
|
+
* ```tsx
|
|
136
|
+
* const layout = renderToWidget(
|
|
137
|
+
* <VStack padding={16} backgroundColor="#0F172A" borderRadius={16}>
|
|
138
|
+
* <HStack justifyContent="spaceBetween">
|
|
139
|
+
* <Text fontSize={14} fontWeight="bold" color="#FFFFFF">Vào ca sáng</Text>
|
|
140
|
+
* <Badge text="07:54 (Hợp lệ)" backgroundColor="#064E3B" textColor="#6EE7B7" />
|
|
141
|
+
* </HStack>
|
|
142
|
+
* <Button title="Làm mới" actionId="refresh" />
|
|
143
|
+
* </VStack>
|
|
144
|
+
* );
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
export function renderToWidget(element: any): LayoutNode | null {
|
|
148
|
+
if (element == null || typeof element === 'boolean') {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
if (typeof element === 'string' || typeof element === 'number') {
|
|
152
|
+
return UI.Text(String(element));
|
|
153
|
+
}
|
|
154
|
+
if (Array.isArray(element)) {
|
|
155
|
+
const children = element.map(renderToWidget).filter(Boolean) as LayoutNode[];
|
|
156
|
+
return UI.VStack({}, ...children);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Nếu bản thân đối tượng đã là một LayoutNode của builder (đã có type string)
|
|
160
|
+
if (element && typeof element.type === 'string' && (element as any).props !== undefined && !React.isValidElement(element)) {
|
|
161
|
+
return element as LayoutNode;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!React.isValidElement(element)) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const { type, props } = element as React.ReactElement<any>;
|
|
169
|
+
const restProps = extractPropsAndStyle(props);
|
|
170
|
+
const children = props?.children;
|
|
171
|
+
|
|
172
|
+
const parsedChildren: LayoutNode[] = [];
|
|
173
|
+
if (children !== undefined && children !== null) {
|
|
174
|
+
React.Children.forEach(children, (child: any) => {
|
|
175
|
+
const parsed = renderToWidget(child);
|
|
176
|
+
if (parsed) {
|
|
177
|
+
parsedChildren.push(parsed);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Trường hợp Component là Function
|
|
183
|
+
if (typeof type === 'function') {
|
|
184
|
+
const compName = (type as any).displayName || type.name;
|
|
185
|
+
const isComp = (target: any, name: string) => type === target || compName === name;
|
|
186
|
+
|
|
187
|
+
if (isComp(VStack, 'VStack')) {
|
|
188
|
+
return UI.VStack(restProps, ...parsedChildren);
|
|
189
|
+
}
|
|
190
|
+
if (isComp(HStack, 'HStack')) {
|
|
191
|
+
return UI.HStack(restProps, ...parsedChildren);
|
|
192
|
+
}
|
|
193
|
+
if (isComp(ZStack, 'ZStack')) {
|
|
194
|
+
return UI.ZStack(restProps, ...parsedChildren);
|
|
195
|
+
}
|
|
196
|
+
if (isComp(Grid, 'Grid')) {
|
|
197
|
+
return UI.Grid(restProps, ...parsedChildren);
|
|
198
|
+
}
|
|
199
|
+
if (isComp(Text, 'Text')) {
|
|
200
|
+
const textContent =
|
|
201
|
+
typeof children === 'string' || typeof children === 'number'
|
|
202
|
+
? String(children)
|
|
203
|
+
: restProps.text || '';
|
|
204
|
+
return UI.Text(textContent, restProps);
|
|
205
|
+
}
|
|
206
|
+
if (isComp(Button, 'Button')) {
|
|
207
|
+
const title =
|
|
208
|
+
restProps.title || (typeof children === 'string' ? children : '');
|
|
209
|
+
return UI.Button({ ...restProps, title });
|
|
210
|
+
}
|
|
211
|
+
if (isComp(Badge, 'Badge')) {
|
|
212
|
+
const text =
|
|
213
|
+
restProps.text || (typeof children === 'string' ? children : '');
|
|
214
|
+
return UI.Badge({ ...restProps, text });
|
|
215
|
+
}
|
|
216
|
+
if (isComp(Progress, 'Progress')) {
|
|
217
|
+
return UI.Progress(restProps.value ?? restProps.totalDuration ?? 0, restProps);
|
|
218
|
+
}
|
|
219
|
+
if (isComp(Timer, 'Timer')) {
|
|
220
|
+
return UI.Timer(restProps.targetTimestamp ?? Date.now(), restProps);
|
|
221
|
+
}
|
|
222
|
+
if (isComp(Image, 'Image')) {
|
|
223
|
+
return UI.Image(
|
|
224
|
+
restProps.url || restProps.base64 || restProps.systemName || restProps.source || '',
|
|
225
|
+
restProps
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
if (isComp(Spacer, 'Spacer')) {
|
|
229
|
+
return UI.Spacer(restProps.minLength);
|
|
230
|
+
}
|
|
231
|
+
if (isComp(Divider, 'Divider')) {
|
|
232
|
+
return UI.Divider(restProps);
|
|
233
|
+
}
|
|
234
|
+
if (isComp(Equalizer, 'Equalizer')) {
|
|
235
|
+
return UI.Equalizer(restProps);
|
|
236
|
+
}
|
|
237
|
+
if (isComp(Link, 'Link')) {
|
|
238
|
+
return UI.Link(restProps, ...parsedChildren);
|
|
239
|
+
}
|
|
240
|
+
if (isComp(Gauge, 'Gauge')) {
|
|
241
|
+
return UI.Gauge(restProps.value ?? 0, restProps);
|
|
242
|
+
}
|
|
243
|
+
if (isComp(Chart, 'Chart')) {
|
|
244
|
+
return UI.Chart(restProps.values ?? [], restProps);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Component do người dùng tự viết: gọi hàm render để đệ quy phân giải
|
|
248
|
+
try {
|
|
249
|
+
const rendered = (type as any)(props);
|
|
250
|
+
return renderToWidget(rendered);
|
|
251
|
+
} catch {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Trường hợp Component được truyền dạng string type
|
|
257
|
+
if (typeof type === 'string') {
|
|
258
|
+
switch (type) {
|
|
259
|
+
case 'VStack':
|
|
260
|
+
return UI.VStack(restProps, ...parsedChildren);
|
|
261
|
+
case 'HStack':
|
|
262
|
+
return UI.HStack(restProps, ...parsedChildren);
|
|
263
|
+
case 'ZStack':
|
|
264
|
+
return UI.ZStack(restProps, ...parsedChildren);
|
|
265
|
+
case 'Text': {
|
|
266
|
+
const textContent =
|
|
267
|
+
typeof children === 'string' || typeof children === 'number'
|
|
268
|
+
? String(children)
|
|
269
|
+
: restProps.text || '';
|
|
270
|
+
return UI.Text(textContent, restProps);
|
|
271
|
+
}
|
|
272
|
+
case 'Button':
|
|
273
|
+
return UI.Button({
|
|
274
|
+
...restProps,
|
|
275
|
+
title: restProps.title || (typeof children === 'string' ? children : ''),
|
|
276
|
+
});
|
|
277
|
+
case 'Badge':
|
|
278
|
+
return UI.Badge({
|
|
279
|
+
...restProps,
|
|
280
|
+
text: restProps.text || (typeof children === 'string' ? children : ''),
|
|
281
|
+
});
|
|
282
|
+
case 'Progress':
|
|
283
|
+
return UI.Progress(restProps.value ?? 0, restProps);
|
|
284
|
+
case 'Image':
|
|
285
|
+
return UI.Image(restProps.url || restProps.base64 || restProps.systemName || '', restProps);
|
|
286
|
+
case 'Spacer':
|
|
287
|
+
return UI.Spacer(restProps.minLength);
|
|
288
|
+
case 'Divider':
|
|
289
|
+
return UI.Divider(restProps);
|
|
290
|
+
default:
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return null;
|
|
296
|
+
}
|