react-native-live-widgets 1.2.0 → 1.2.2
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 +34 -0
- package/README.md +129 -50
- package/android/src/main/AndroidManifest.xml +12 -0
- package/android/src/main/assets/fonts/MaterialIcons-Regular.ttf +0 -0
- package/android/src/main/java/com/reactnativeliveactivity/bridge/LiveActivityModule.kt +61 -1
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityNotifier.kt +153 -17
- package/android/src/main/java/com/reactnativeliveactivity/liveactivity/LiveActivityReceiver.kt +1 -1
- package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +529 -59
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetImageCache.kt +53 -0
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +684 -83
- package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +129 -57
- package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicQuickSettingsTileService.kt +120 -0
- package/android/src/main/res/layout/rn_live_widget_hit_zone.xml +16 -0
- package/android/src/main/res/layout/rn_live_widget_host.xml +16 -87
- package/app.plugin.js +36 -0
- package/assets/MaterialIcons.json +2236 -0
- package/cli/codegen.js +789 -0
- package/cli/setup.js +323 -63
- package/docs/EXPO-GUIDE-EN.md +21 -3
- package/docs/EXPO-GUIDE-VI.md +21 -3
- package/docs/GUIDE-EN.md +444 -21
- package/docs/GUIDE-VI.md +473 -22
- package/docs/index.html +2263 -265
- package/docs/llms-full.txt +41 -0
- package/docs/llms.txt +24 -7
- package/ios/Bridge/LiveActivityModule.swift +333 -44
- package/ios/Intents/DynamicWidgetActionIntent.swift +57 -2
- package/ios/Models/DynamicLayoutModels.swift +120 -5
- package/ios/Renderer/DynamicNodeView.swift +184 -20
- package/ios/Storage/DynamicWidgetStateStore.swift +649 -38
- package/ios/Widgets/DynamicControlWidget.swift +63 -0
- package/ios/Widgets/DynamicHomeScreenWidget.swift +104 -26
- package/ios/Widgets/DynamicLiveActivityWidget.swift +25 -4
- package/lib/builder.d.ts +50 -7
- package/lib/builder.d.ts.map +1 -1
- package/lib/builder.js +391 -29
- package/lib/builder.js.map +1 -1
- package/lib/dispatcher.js +16 -10
- package/lib/dispatcher.js.map +1 -1
- package/lib/hooks.d.ts +13 -9
- package/lib/hooks.d.ts.map +1 -1
- package/lib/hooks.js +105 -42
- package/lib/hooks.js.map +1 -1
- package/lib/index.d.ts +96 -8
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +415 -97
- package/lib/index.js.map +1 -1
- package/lib/ir.d.ts +15 -0
- package/lib/ir.d.ts.map +1 -0
- package/lib/ir.js +386 -0
- package/lib/ir.js.map +1 -0
- package/lib/jsx.d.ts +19 -7
- package/lib/jsx.d.ts.map +1 -1
- package/lib/jsx.js +179 -95
- package/lib/jsx.js.map +1 -1
- package/lib/material-icons.generated.d.ts +8 -0
- package/lib/material-icons.generated.d.ts.map +1 -0
- package/lib/material-icons.generated.js +9 -0
- package/lib/material-icons.generated.js.map +1 -0
- package/lib/preview.d.ts +11 -27
- package/lib/preview.d.ts.map +1 -1
- package/lib/preview.js +456 -119
- package/lib/preview.js.map +1 -1
- package/lib/sf-symbols.generated.d.ts +8 -0
- package/lib/sf-symbols.generated.d.ts.map +1 -0
- package/lib/sf-symbols.generated.js +9 -0
- package/lib/sf-symbols.generated.js.map +1 -0
- package/lib/types.d.ts +506 -30
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +2 -1
- package/lib/types.js.map +1 -1
- package/package.json +7 -2
- package/scripts/build.js +98 -0
- package/scripts/generate-material-icons.js +93 -0
- package/scripts/generate-sf-symbols.js +95 -0
- package/scripts/release.js +115 -0
- package/scripts/test-ir-semantic.js +314 -0
- package/src/builder.ts +439 -26
- package/src/hooks.ts +88 -12
- package/src/index.ts +385 -57
- package/src/ir.ts +436 -0
- package/src/jsx.tsx +99 -21
- package/src/material-icons.generated.ts +3844 -0
- package/src/preview.tsx +533 -55
- package/src/react-native-env.d.ts +1 -0
- package/src/sf-symbols.generated.ts +9533 -0
- package/src/types.ts +601 -31
package/src/ir.ts
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LayoutNode,
|
|
3
|
+
WidgetSinglePageConfig,
|
|
4
|
+
WidgetIR,
|
|
5
|
+
SemanticNode,
|
|
6
|
+
NodeRenderPlan,
|
|
7
|
+
RenderPlanTarget,
|
|
8
|
+
StateBinding,
|
|
9
|
+
AccessibilityRole,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
let nodeCounter = 0;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 🌲 Trình biên dịch UI Intermediate Representation (IR).
|
|
16
|
+
* Chuyển hóa cây DSL thành cấu trúc dữ liệu trung gian chuẩn gồm:
|
|
17
|
+
* 1. Root LayoutNode (đã chuẩn hóa thuộc tính & định danh).
|
|
18
|
+
* 2. Semantic Tree độc lập (Tách biệt hoàn toàn khỏi việc vẽ hình của Canvas).
|
|
19
|
+
* 3. Render Plan (Renderer Planner phân loại Native RemoteViews vs Canvas Bitmap).
|
|
20
|
+
* 4. State Bindings (Danh sách các liên kết tương tác & đồng bộ trạng thái).
|
|
21
|
+
*/
|
|
22
|
+
export function compileToIR(input: LayoutNode | WidgetSinglePageConfig): WidgetIR {
|
|
23
|
+
nodeCounter = 0;
|
|
24
|
+
const rootNode = resolveRootLayoutNode(input);
|
|
25
|
+
|
|
26
|
+
// 1. Gán ID duy nhất và lập bản đồ cây
|
|
27
|
+
const indexedRoot = assignNodeIds(rootNode, '0');
|
|
28
|
+
|
|
29
|
+
// 2. Xây dựng Semantic Tree độc lập
|
|
30
|
+
const semanticTree: SemanticNode[] = [];
|
|
31
|
+
buildSemanticNodes(indexedRoot, '0', semanticTree);
|
|
32
|
+
const semanticSummary = synthesizeSemanticSummary(semanticTree);
|
|
33
|
+
|
|
34
|
+
// 3. Renderer Planner: Phân loại node theo khả năng native
|
|
35
|
+
const planNodes: Record<string, NodeRenderPlan> = {};
|
|
36
|
+
let nativeCount = 0;
|
|
37
|
+
let canvasCount = 0;
|
|
38
|
+
classifyRenderTargets(indexedRoot, planNodes, () => {
|
|
39
|
+
nativeCount++;
|
|
40
|
+
}, () => {
|
|
41
|
+
canvasCount++;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// 4. Trích xuất State Bindings
|
|
45
|
+
const stateBindings: StateBinding[] = [];
|
|
46
|
+
extractBindings(indexedRoot, stateBindings);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
version: '2.0',
|
|
50
|
+
timestamp: Date.now(),
|
|
51
|
+
root: indexedRoot,
|
|
52
|
+
semanticTree,
|
|
53
|
+
semanticSummary,
|
|
54
|
+
renderPlan: {
|
|
55
|
+
overallStrategy: nativeCount >= canvasCount ? 'native_first_hybrid' : 'canvas_fallback',
|
|
56
|
+
nativeNodeCount: nativeCount,
|
|
57
|
+
canvasNodeCount: canvasCount,
|
|
58
|
+
nodes: planNodes,
|
|
59
|
+
},
|
|
60
|
+
stateBindings,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function resolveRootLayoutNode(input: LayoutNode | WidgetSinglePageConfig): LayoutNode {
|
|
65
|
+
if ('type' in input) {
|
|
66
|
+
return input;
|
|
67
|
+
}
|
|
68
|
+
return input.medium || input.large || input.small || { type: 'VStack', children: [] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function assignNodeIds(node: any, path: string): LayoutNode {
|
|
72
|
+
if (!node || typeof node !== 'object') return node;
|
|
73
|
+
|
|
74
|
+
const cloned = { ...node };
|
|
75
|
+
if (!cloned.id) {
|
|
76
|
+
cloned.id = `node_${path}_${++nodeCounter}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (Array.isArray(cloned.children)) {
|
|
80
|
+
cloned.children = cloned.children.map((child: any, idx: number) =>
|
|
81
|
+
assignNodeIds(child, `${path}.${idx}`)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return cloned as LayoutNode;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function buildSemanticNodes(node: any, path: string, result: SemanticNode[]): void {
|
|
89
|
+
if (!node || typeof node !== 'object') return;
|
|
90
|
+
|
|
91
|
+
const props = node.props || {};
|
|
92
|
+
const accessible = props.accessible !== false;
|
|
93
|
+
const type = node.type;
|
|
94
|
+
|
|
95
|
+
let role: AccessibilityRole = props.accessibilityRole || 'none';
|
|
96
|
+
let label = props.accessibilityLabel || '';
|
|
97
|
+
let hint = props.accessibilityHint;
|
|
98
|
+
let value = props.accessibilityValue;
|
|
99
|
+
let interactive = false;
|
|
100
|
+
let actionId = props.actionId;
|
|
101
|
+
let deepLink = props.deepLink;
|
|
102
|
+
|
|
103
|
+
switch (type) {
|
|
104
|
+
case 'Text': {
|
|
105
|
+
if (role === 'none') {
|
|
106
|
+
const typo = props.typography || '';
|
|
107
|
+
role = typo.startsWith('title') || typo === 'largeTitle' || typo === 'headline'
|
|
108
|
+
? 'header'
|
|
109
|
+
: 'text';
|
|
110
|
+
}
|
|
111
|
+
if (!label && props.text !== undefined) {
|
|
112
|
+
label = String(props.text);
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
case 'Button': {
|
|
118
|
+
role = 'button';
|
|
119
|
+
interactive = true;
|
|
120
|
+
if (!label && props.title !== undefined) {
|
|
121
|
+
label = String(props.title);
|
|
122
|
+
}
|
|
123
|
+
if (!hint) {
|
|
124
|
+
hint = actionId ? `Kích hoạt hành động ${actionId}` : 'Chạm để thực hiện';
|
|
125
|
+
}
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
case 'Progress': {
|
|
130
|
+
role = 'progressbar';
|
|
131
|
+
if (!label) label = 'Tiến độ';
|
|
132
|
+
if (value === undefined && props.value !== undefined) {
|
|
133
|
+
const num = Number(props.value);
|
|
134
|
+
value = Number.isNaN(num) ? String(props.value) : `${Math.round(num * 100)}%`;
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
case 'Gauge': {
|
|
140
|
+
role = 'progressbar';
|
|
141
|
+
label = props.label || label || 'Chỉ số';
|
|
142
|
+
if (value === undefined) {
|
|
143
|
+
if (props.valueText) {
|
|
144
|
+
value = props.valueText;
|
|
145
|
+
} else if (props.value !== undefined) {
|
|
146
|
+
const num = Number(props.value);
|
|
147
|
+
value = Number.isNaN(num) ? String(props.value) : `${Math.round(num * 100)}%`;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
case 'Timer': {
|
|
154
|
+
role = 'timer';
|
|
155
|
+
if (!label) label = 'Đồng hồ đếm';
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
case 'Toggle': {
|
|
160
|
+
role = 'switch';
|
|
161
|
+
interactive = true;
|
|
162
|
+
if (!label && props.label) label = String(props.label);
|
|
163
|
+
value = props.isChecked ? 'Đã chọn' : 'Chưa chọn';
|
|
164
|
+
hint = 'Chạm để thay đổi trạng thái';
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
case 'Badge': {
|
|
169
|
+
role = 'summary';
|
|
170
|
+
if (!label && props.text) label = String(props.text);
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
case 'Image': {
|
|
175
|
+
role = props.systemName || props.url ? 'image' : 'none';
|
|
176
|
+
if (!label && props.systemName) {
|
|
177
|
+
label = `Biểu tượng ${props.systemName}`;
|
|
178
|
+
}
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
default:
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (accessible && (label || value || interactive)) {
|
|
187
|
+
result.push({
|
|
188
|
+
id: node.id || `semantic_${path}`,
|
|
189
|
+
path,
|
|
190
|
+
type,
|
|
191
|
+
role,
|
|
192
|
+
label: label.trim(),
|
|
193
|
+
hint,
|
|
194
|
+
value,
|
|
195
|
+
interactive,
|
|
196
|
+
actionId,
|
|
197
|
+
deepLink,
|
|
198
|
+
isDecorative: !accessible,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (Array.isArray(node.children)) {
|
|
203
|
+
node.children.forEach((child: any, idx: number) => {
|
|
204
|
+
buildSemanticNodes(child, `${path}.${idx}`, result);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function synthesizeSemanticSummary(tree: SemanticNode[]): string {
|
|
210
|
+
const parts: string[] = [];
|
|
211
|
+
for (const node of tree) {
|
|
212
|
+
if (node.isDecorative) continue;
|
|
213
|
+
|
|
214
|
+
let phrase = '';
|
|
215
|
+
if (node.role === 'header' || node.role === 'text') {
|
|
216
|
+
phrase = node.label;
|
|
217
|
+
} else if (node.role === 'button') {
|
|
218
|
+
phrase = `Nút ${node.label}`;
|
|
219
|
+
if (node.hint) phrase += ` (${node.hint})`;
|
|
220
|
+
} else if (node.role === 'progressbar' || node.role === 'timer') {
|
|
221
|
+
phrase = `${node.label} ${node.value ?? ''}`.trim();
|
|
222
|
+
} else if (node.role === 'switch') {
|
|
223
|
+
phrase = `${node.label}: ${node.value ?? ''}`.trim();
|
|
224
|
+
} else if (node.label) {
|
|
225
|
+
phrase = node.label;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (phrase && !parts.includes(phrase)) {
|
|
229
|
+
parts.push(phrase);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return parts.join('. ');
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 🎯 Phân loại Render Target (Native RemoteViews vs Canvas Bitmap).
|
|
238
|
+
* Áp dụng nguyên tắc "Native-first, Canvas-when-needed".
|
|
239
|
+
*/
|
|
240
|
+
function classifyRenderTargets(
|
|
241
|
+
node: any,
|
|
242
|
+
registry: Record<string, NodeRenderPlan>,
|
|
243
|
+
onNative: () => void,
|
|
244
|
+
onCanvas: () => void
|
|
245
|
+
): void {
|
|
246
|
+
if (!node || typeof node !== 'object') return;
|
|
247
|
+
|
|
248
|
+
const type = node.type;
|
|
249
|
+
const props = node.props || {};
|
|
250
|
+
const id = node.id || 'anonymous';
|
|
251
|
+
|
|
252
|
+
let target: RenderPlanTarget = 'native_remote_view';
|
|
253
|
+
let reason = '';
|
|
254
|
+
let hasNative = true;
|
|
255
|
+
let requiresCanvas = false;
|
|
256
|
+
|
|
257
|
+
switch (type) {
|
|
258
|
+
case 'Text':
|
|
259
|
+
target = 'native_remote_view';
|
|
260
|
+
reason = 'Ánh xạ trực tiếp sang native TextView với đầy đủ font scaling';
|
|
261
|
+
hasNative = true;
|
|
262
|
+
requiresCanvas = false;
|
|
263
|
+
onNative();
|
|
264
|
+
break;
|
|
265
|
+
|
|
266
|
+
case 'Button':
|
|
267
|
+
target = 'native_remote_view';
|
|
268
|
+
reason = 'Ánh xạ trực tiếp sang native Button với PendingIntent';
|
|
269
|
+
hasNative = true;
|
|
270
|
+
requiresCanvas = false;
|
|
271
|
+
onNative();
|
|
272
|
+
break;
|
|
273
|
+
|
|
274
|
+
case 'Progress':
|
|
275
|
+
if (props.style === 'circular') {
|
|
276
|
+
target = 'canvas_bitmap';
|
|
277
|
+
reason = 'Vòng tròn tiến độ 360° đòi hỏi vẽ đường cong Canvas 2D';
|
|
278
|
+
hasNative = false;
|
|
279
|
+
requiresCanvas = true;
|
|
280
|
+
onCanvas();
|
|
281
|
+
} else {
|
|
282
|
+
target = 'native_remote_view';
|
|
283
|
+
reason = 'Thanh tiến độ ngang ánh xạ trực tiếp sang native ProgressBar';
|
|
284
|
+
hasNative = true;
|
|
285
|
+
requiresCanvas = false;
|
|
286
|
+
onNative();
|
|
287
|
+
}
|
|
288
|
+
break;
|
|
289
|
+
|
|
290
|
+
case 'Gauge':
|
|
291
|
+
target = 'canvas_bitmap';
|
|
292
|
+
reason = 'Đồng hồ cung tròn 240° đòi hỏi tính toán góc quét và bóng kim bằng Canvas';
|
|
293
|
+
hasNative = false;
|
|
294
|
+
requiresCanvas = true;
|
|
295
|
+
onCanvas();
|
|
296
|
+
break;
|
|
297
|
+
|
|
298
|
+
case 'Equalizer':
|
|
299
|
+
target = 'canvas_bitmap';
|
|
300
|
+
reason = 'Dải sóng âm thanh nhiều cột đòi hỏi hoạt họa pha sóng Canvas';
|
|
301
|
+
hasNative = false;
|
|
302
|
+
requiresCanvas = true;
|
|
303
|
+
onCanvas();
|
|
304
|
+
break;
|
|
305
|
+
|
|
306
|
+
case 'Divider':
|
|
307
|
+
target = 'native_remote_view';
|
|
308
|
+
reason = 'Đường phân cách ánh xạ trực tiếp sang native View với background/height';
|
|
309
|
+
hasNative = true;
|
|
310
|
+
requiresCanvas = false;
|
|
311
|
+
onNative();
|
|
312
|
+
break;
|
|
313
|
+
|
|
314
|
+
case 'Image':
|
|
315
|
+
target = 'native_remote_view';
|
|
316
|
+
reason = 'Ảnh ánh xạ trực tiếp sang native ImageView';
|
|
317
|
+
hasNative = true;
|
|
318
|
+
requiresCanvas = false;
|
|
319
|
+
onNative();
|
|
320
|
+
break;
|
|
321
|
+
|
|
322
|
+
case 'VStack':
|
|
323
|
+
case 'HStack':
|
|
324
|
+
case 'ZStack':
|
|
325
|
+
case 'Grid': {
|
|
326
|
+
const hasDashed = props.borderStyle === 'dashed' || props.borderStyle === 'dotted';
|
|
327
|
+
const hasComplexGradient = props.gradient && props.gradient.colors?.length > 2;
|
|
328
|
+
|
|
329
|
+
if (hasDashed || hasComplexGradient) {
|
|
330
|
+
target = 'composite';
|
|
331
|
+
reason = 'Vùng chứa có viền đứt nét hoặc gradient đa điểm cần nền Canvas kèm con Native';
|
|
332
|
+
hasNative = false;
|
|
333
|
+
requiresCanvas = true;
|
|
334
|
+
onCanvas();
|
|
335
|
+
} else {
|
|
336
|
+
target = 'native_remote_view';
|
|
337
|
+
reason = 'Khung bố cục chuẩn ánh xạ sang LinearLayout hoặc FrameLayout';
|
|
338
|
+
hasNative = true;
|
|
339
|
+
requiresCanvas = false;
|
|
340
|
+
onNative();
|
|
341
|
+
}
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
default:
|
|
346
|
+
target = 'native_remote_view';
|
|
347
|
+
reason = 'Thành phần tiêu chuẩn';
|
|
348
|
+
hasNative = true;
|
|
349
|
+
onNative();
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const childrenTargets: RenderPlanTarget[] = [];
|
|
354
|
+
if (Array.isArray(node.children)) {
|
|
355
|
+
node.children.forEach((child: any) => {
|
|
356
|
+
classifyRenderTargets(child, registry, onNative, onCanvas);
|
|
357
|
+
const childPlan = registry[child.id];
|
|
358
|
+
if (childPlan) {
|
|
359
|
+
childrenTargets.push(childPlan.target);
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
registry[id] = {
|
|
365
|
+
id,
|
|
366
|
+
type,
|
|
367
|
+
target,
|
|
368
|
+
reason,
|
|
369
|
+
hasNativeEquivalent: hasNative,
|
|
370
|
+
requiresCanvas,
|
|
371
|
+
childrenTargets: childrenTargets.length > 0 ? childrenTargets : undefined,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function extractBindings(node: any, bindings: StateBinding[]): void {
|
|
376
|
+
if (!node || typeof node !== 'object') return;
|
|
377
|
+
|
|
378
|
+
const props = node.props || {};
|
|
379
|
+
const actionId = props.actionId;
|
|
380
|
+
|
|
381
|
+
if (actionId) {
|
|
382
|
+
let bindingType: StateBinding['bindingType'] = 'custom';
|
|
383
|
+
let targetKey: string | undefined = undefined;
|
|
384
|
+
|
|
385
|
+
if (actionId.startsWith('toggle:')) {
|
|
386
|
+
bindingType = 'toggle';
|
|
387
|
+
targetKey = actionId.replace('toggle:', '');
|
|
388
|
+
} else if (actionId.startsWith('page:')) {
|
|
389
|
+
bindingType = 'page';
|
|
390
|
+
targetKey = actionId.replace('page:', '');
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
bindings.push({
|
|
394
|
+
id: node.id || `bind_${bindings.length}`,
|
|
395
|
+
actionId,
|
|
396
|
+
bindingType,
|
|
397
|
+
targetKey,
|
|
398
|
+
initialValue: props.isChecked,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (Array.isArray(node.children)) {
|
|
403
|
+
node.children.forEach((child: any) => extractBindings(child, bindings));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* 🔍 Tiện ích gỡ lỗi: In ra cây kiểm tra trực quan của Intermediate Representation.
|
|
409
|
+
*/
|
|
410
|
+
export function inspectIR(input: LayoutNode | WidgetSinglePageConfig): string {
|
|
411
|
+
const ir = compileToIR(input);
|
|
412
|
+
const lines: string[] = [];
|
|
413
|
+
|
|
414
|
+
lines.push(`🌲 Widget IR v${ir.version} [${new Date(ir.timestamp).toISOString()}]`);
|
|
415
|
+
lines.push(`📊 Chiến lược kết xuất: ${ir.renderPlan.overallStrategy.toUpperCase()}`);
|
|
416
|
+
lines.push(` - Native RemoteViews nodes: ${ir.renderPlan.nativeNodeCount}`);
|
|
417
|
+
lines.push(` - Canvas Bitmap nodes: ${ir.renderPlan.canvasNodeCount}`);
|
|
418
|
+
lines.push(`\n♿ Tóm tắt âm thanh Trợ năng (Synthesized A11y Summary):`);
|
|
419
|
+
lines.push(` "${ir.semanticSummary}"`);
|
|
420
|
+
|
|
421
|
+
lines.push(`\n🎯 Kế hoạch phân bổ (Render Plan Tree):`);
|
|
422
|
+
for (const node of ir.semanticTree) {
|
|
423
|
+
const plan = ir.renderPlan.nodes[node.id];
|
|
424
|
+
const targetTag = plan?.target === 'native_remote_view' ? '[NATIVE]' : (plan?.target === 'canvas_bitmap' ? '[CANVAS]' : '[COMPOSITE]');
|
|
425
|
+
lines.push(` ${targetTag} ${node.path} <${node.type}> role=${node.role} label="${node.label}" ${node.interactive ? `(Action: ${node.actionId})` : ''}`);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (ir.stateBindings.length > 0) {
|
|
429
|
+
lines.push(`\n⚡ Liên kết trạng thái (State Bindings):`);
|
|
430
|
+
for (const b of ir.stateBindings) {
|
|
431
|
+
lines.push(` - [${b.bindingType.toUpperCase()}] action=${b.actionId} target=${b.targetKey ?? 'N/A'}`);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return lines.join('\n');
|
|
436
|
+
}
|
package/src/jsx.tsx
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
LinkNode,
|
|
18
18
|
GaugeNode,
|
|
19
19
|
ChartNode,
|
|
20
|
+
ToggleNode,
|
|
20
21
|
ContainerProps,
|
|
21
22
|
TextStyle,
|
|
22
23
|
ZStackProps,
|
|
@@ -24,8 +25,44 @@ import {
|
|
|
24
25
|
LinkProps,
|
|
25
26
|
EqualizerProps,
|
|
26
27
|
StyleProp,
|
|
28
|
+
DataBindingField,
|
|
27
29
|
} from './types';
|
|
28
|
-
import { UI, flattenStyle } from './builder';
|
|
30
|
+
import { UI, flattenStyle, isDataBinding, resolveBindingValue } from './builder';
|
|
31
|
+
|
|
32
|
+
type WidgetTextChild =
|
|
33
|
+
| React.ReactNode
|
|
34
|
+
| DataBindingField<any>
|
|
35
|
+
| Array<React.ReactNode | DataBindingField<any>>;
|
|
36
|
+
|
|
37
|
+
function flattenTextChildren(children: any): string {
|
|
38
|
+
if (children === undefined || children === null || typeof children === 'boolean') {
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
if (isDataBinding(children)) {
|
|
42
|
+
return children.toToken();
|
|
43
|
+
}
|
|
44
|
+
if (typeof children === 'string' || typeof children === 'number') {
|
|
45
|
+
return String(children);
|
|
46
|
+
}
|
|
47
|
+
if (Array.isArray(children)) {
|
|
48
|
+
return children.map(flattenTextChildren).join('');
|
|
49
|
+
}
|
|
50
|
+
return '';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function collectChildElements(children: any, out: any[] = []): any[] {
|
|
54
|
+
if (children === undefined || children === null || typeof children === 'boolean') {
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(children)) {
|
|
58
|
+
for (const item of children) {
|
|
59
|
+
collectChildElements(item, out);
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
out.push(children);
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
29
66
|
|
|
30
67
|
// ─── 1. JSX COMPONENT DEFINITIONS ────────────────────────────────────────────
|
|
31
68
|
|
|
@@ -66,22 +103,24 @@ export const Grid: React.FC<GridJSXProps> = () => null;
|
|
|
66
103
|
Grid.displayName = 'Grid';
|
|
67
104
|
|
|
68
105
|
export interface TextJSXProps extends TextStyle {
|
|
69
|
-
children?:
|
|
70
|
-
text?: string
|
|
106
|
+
children?: WidgetTextChild;
|
|
107
|
+
text?: string | DataBindingField<any>;
|
|
71
108
|
style?: StyleProp<TextStyle>;
|
|
72
109
|
}
|
|
73
110
|
export const Text: React.FC<TextJSXProps> = () => null;
|
|
74
111
|
Text.displayName = 'Text';
|
|
75
112
|
|
|
76
|
-
export type ButtonJSXProps = ButtonNode['props'] & {
|
|
77
|
-
|
|
113
|
+
export type ButtonJSXProps = Omit<ButtonNode['props'], 'title'> & {
|
|
114
|
+
title?: string | DataBindingField<any>;
|
|
115
|
+
children?: WidgetTextChild;
|
|
78
116
|
style?: StyleProp<ButtonNode['props']>;
|
|
79
117
|
};
|
|
80
118
|
export const Button: React.FC<ButtonJSXProps> = () => null;
|
|
81
119
|
Button.displayName = 'Button';
|
|
82
120
|
|
|
83
|
-
export type BadgeJSXProps = BadgeNode['props'] & {
|
|
84
|
-
|
|
121
|
+
export type BadgeJSXProps = Omit<BadgeNode['props'], 'text'> & {
|
|
122
|
+
text?: string | DataBindingField<any>;
|
|
123
|
+
children?: WidgetTextChild;
|
|
85
124
|
style?: StyleProp<BadgeNode['props']>;
|
|
86
125
|
};
|
|
87
126
|
export const Badge: React.FC<BadgeJSXProps> = () => null;
|
|
@@ -127,6 +166,21 @@ Gauge.displayName = 'Gauge';
|
|
|
127
166
|
export const Chart: React.FC<ChartNode['props']> = () => null;
|
|
128
167
|
Chart.displayName = 'Chart';
|
|
129
168
|
|
|
169
|
+
export type ToggleJSXProps = Partial<ToggleNode['props']> & {
|
|
170
|
+
children?: React.ReactNode;
|
|
171
|
+
actionId?: string;
|
|
172
|
+
key?: string;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export const Toggle: React.FC<ToggleJSXProps> = () => null;
|
|
176
|
+
Toggle.displayName = 'Toggle';
|
|
177
|
+
|
|
178
|
+
export const Switch: React.FC<ToggleJSXProps> = () => null;
|
|
179
|
+
Switch.displayName = 'Switch';
|
|
180
|
+
|
|
181
|
+
export const Checkbox: React.FC<ToggleJSXProps> = () => null;
|
|
182
|
+
Checkbox.displayName = 'Checkbox';
|
|
183
|
+
|
|
130
184
|
// ─── 2. RECURSIVE JSX -> LAYOUTNODE CONVERTER ────────────────────────────────
|
|
131
185
|
|
|
132
186
|
/**
|
|
@@ -148,6 +202,9 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
148
202
|
if (element == null || typeof element === 'boolean') {
|
|
149
203
|
return null;
|
|
150
204
|
}
|
|
205
|
+
if (isDataBinding(element)) {
|
|
206
|
+
return UI.Text(element.toToken());
|
|
207
|
+
}
|
|
151
208
|
if (typeof element === 'string' || typeof element === 'number') {
|
|
152
209
|
return UI.Text(String(element));
|
|
153
210
|
}
|
|
@@ -171,12 +228,12 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
171
228
|
|
|
172
229
|
const parsedChildren: LayoutNode[] = [];
|
|
173
230
|
if (children !== undefined && children !== null) {
|
|
174
|
-
|
|
231
|
+
for (const child of collectChildElements(children)) {
|
|
175
232
|
const parsed = renderToWidget(child);
|
|
176
233
|
if (parsed) {
|
|
177
234
|
parsedChildren.push(parsed);
|
|
178
235
|
}
|
|
179
|
-
}
|
|
236
|
+
}
|
|
180
237
|
}
|
|
181
238
|
|
|
182
239
|
// Trường hợp Component là Function
|
|
@@ -197,20 +254,18 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
197
254
|
return UI.Grid(restProps, ...parsedChildren);
|
|
198
255
|
}
|
|
199
256
|
if (isComp(Text, 'Text')) {
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
? String(children)
|
|
203
|
-
: restProps.text || '';
|
|
257
|
+
const childText = flattenTextChildren(children);
|
|
258
|
+
const textContent = childText || String(resolveBindingValue(restProps.text) ?? '');
|
|
204
259
|
return UI.Text(textContent, restProps);
|
|
205
260
|
}
|
|
206
261
|
if (isComp(Button, 'Button')) {
|
|
207
262
|
const title =
|
|
208
|
-
restProps.title ||
|
|
263
|
+
String(resolveBindingValue(restProps.title) || '') || flattenTextChildren(children);
|
|
209
264
|
return UI.Button({ ...restProps, title });
|
|
210
265
|
}
|
|
211
266
|
if (isComp(Badge, 'Badge')) {
|
|
212
267
|
const text =
|
|
213
|
-
restProps.text ||
|
|
268
|
+
String(resolveBindingValue(restProps.text) || '') || flattenTextChildren(children);
|
|
214
269
|
return UI.Badge({ ...restProps, text });
|
|
215
270
|
}
|
|
216
271
|
if (isComp(Progress, 'Progress')) {
|
|
@@ -221,7 +276,16 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
221
276
|
}
|
|
222
277
|
if (isComp(Image, 'Image')) {
|
|
223
278
|
return UI.Image(
|
|
224
|
-
restProps.url ||
|
|
279
|
+
restProps.url ||
|
|
280
|
+
restProps.base64 ||
|
|
281
|
+
restProps.iosSystemName ||
|
|
282
|
+
restProps.systemNameIos ||
|
|
283
|
+
restProps.androidSystemName ||
|
|
284
|
+
restProps.systemNameAndroid ||
|
|
285
|
+
restProps.materialName ||
|
|
286
|
+
restProps.systemName ||
|
|
287
|
+
restProps.source ||
|
|
288
|
+
'',
|
|
225
289
|
restProps
|
|
226
290
|
);
|
|
227
291
|
}
|
|
@@ -243,6 +307,12 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
243
307
|
if (isComp(Chart, 'Chart')) {
|
|
244
308
|
return UI.Chart(restProps.values ?? [], restProps);
|
|
245
309
|
}
|
|
310
|
+
if (isComp(Toggle, 'Toggle') || isComp(Switch, 'Switch') || isComp(Checkbox, 'Checkbox')) {
|
|
311
|
+
const label = restProps.label || (typeof children === 'string' ? children : '');
|
|
312
|
+
const actionId = restProps.actionId || (restProps.key ? `toggle:${restProps.key}` : 'toggle:item');
|
|
313
|
+
const style = isComp(Switch, 'Switch') ? 'switch' : (isComp(Checkbox, 'Checkbox') ? 'checkbox' : restProps.style);
|
|
314
|
+
return UI.Toggle(label, { ...restProps, actionId, style });
|
|
315
|
+
}
|
|
246
316
|
|
|
247
317
|
// Component do người dùng tự viết: gọi hàm render để đệ quy phân giải
|
|
248
318
|
try {
|
|
@@ -264,20 +334,20 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
264
334
|
return UI.ZStack(restProps, ...parsedChildren);
|
|
265
335
|
case 'Text': {
|
|
266
336
|
const textContent =
|
|
267
|
-
|
|
268
|
-
? String(children)
|
|
269
|
-
: restProps.text || '';
|
|
337
|
+
flattenTextChildren(children) || String(resolveBindingValue(restProps.text) ?? '');
|
|
270
338
|
return UI.Text(textContent, restProps);
|
|
271
339
|
}
|
|
272
340
|
case 'Button':
|
|
273
341
|
return UI.Button({
|
|
274
342
|
...restProps,
|
|
275
|
-
title:
|
|
343
|
+
title:
|
|
344
|
+
String(resolveBindingValue(restProps.title) || '') || flattenTextChildren(children),
|
|
276
345
|
});
|
|
277
346
|
case 'Badge':
|
|
278
347
|
return UI.Badge({
|
|
279
348
|
...restProps,
|
|
280
|
-
text:
|
|
349
|
+
text:
|
|
350
|
+
String(resolveBindingValue(restProps.text) || '') || flattenTextChildren(children),
|
|
281
351
|
});
|
|
282
352
|
case 'Progress':
|
|
283
353
|
return UI.Progress(restProps.value ?? 0, restProps);
|
|
@@ -287,6 +357,14 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
287
357
|
return UI.Spacer(restProps.minLength);
|
|
288
358
|
case 'Divider':
|
|
289
359
|
return UI.Divider(restProps);
|
|
360
|
+
case 'Toggle':
|
|
361
|
+
case 'Switch':
|
|
362
|
+
case 'Checkbox': {
|
|
363
|
+
const label = restProps.label || (typeof children === 'string' ? children : '');
|
|
364
|
+
const actionId = restProps.actionId || 'toggle:item';
|
|
365
|
+
const style = type === 'Switch' ? 'switch' : (type === 'Checkbox' ? 'checkbox' : restProps.style);
|
|
366
|
+
return UI.Toggle(label, { ...restProps, actionId, style });
|
|
367
|
+
}
|
|
290
368
|
default:
|
|
291
369
|
return null;
|
|
292
370
|
}
|