@onekeyfe/react-native-native-list 6.7.0-2 → 6.7.0-21
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/android/src/main/java/com/onekey/nativelist/HybridNativeList.kt +14 -1
- package/android/src/main/java/com/onekey/nativelist/NativeListAccessoryStack.kt +245 -9
- package/android/src/main/java/com/onekey/nativelist/NativeListActivityDetailsView.kt +35 -7
- package/android/src/main/java/com/onekey/nativelist/NativeListActivityRenderer.kt +25 -3
- package/android/src/main/java/com/onekey/nativelist/NativeListAdapter.kt +17 -1
- package/android/src/main/java/com/onekey/nativelist/NativeListButtonGroupRenderer.kt +102 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListContainerSlotAdapter.kt +12 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListDataRowRenderer.kt +102 -7
- package/android/src/main/java/com/onekey/nativelist/NativeListIdentityRenderer.kt +225 -11
- package/android/src/main/java/com/onekey/nativelist/NativeListLeadingVisual.kt +147 -33
- package/android/src/main/java/com/onekey/nativelist/NativeListMarketRenderer.kt +92 -28
- package/android/src/main/java/com/onekey/nativelist/NativeListMessageRenderer.kt +25 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListModels.kt +149 -3
- package/android/src/main/java/com/onekey/nativelist/NativeListPrimitives.kt +51 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListRendererRegistry.kt +2 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListRendererRowView.kt +2 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListResolvedText.kt +3 -2
- package/android/src/main/java/com/onekey/nativelist/NativeListSectionHeaderRenderer.kt +69 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListTextView.kt +55 -1
- package/android/src/main/java/com/onekey/nativelist/NativeListView.kt +1953 -117
- package/android/src/test/java/com/margelo/nitro/nativelist/NativeListActivityHostClipPolicyTest.kt +35 -0
- package/android/src/test/java/com/margelo/nitro/nativelist/NativeListIdentityTrailingAxisTest.kt +36 -0
- package/android/src/test/java/com/margelo/nitro/nativelist/NativeListLeadingSlotFrameTest.kt +68 -0
- package/android/src/test/java/com/margelo/nitro/nativelist/NativeListLeadingVisualClipPolicyTest.kt +44 -0
- package/android/src/test/java/com/margelo/nitro/nativelist/NativeListPressPolicyTest.kt +10 -0
- package/docs/ROW_TEMPLATES.md +70 -11
- package/docs/SPEC.md +1235 -20
- package/docs/STYLE_SPEC.md +6 -0
- package/ios/HybridNativeList.swift +21 -2
- package/ios/NativeListAccessoryStack.swift +235 -39
- package/ios/NativeListActivityDetailsView.swift +30 -11
- package/ios/NativeListActivityRenderer.swift +8 -2
- package/ios/NativeListButtonGroupRenderer.swift +89 -0
- package/ios/NativeListDataRowRenderer.swift +155 -27
- package/ios/NativeListDesignAssets.swift +9 -0
- package/ios/NativeListIdentityRenderer.swift +239 -31
- package/ios/NativeListLeadingVisual.swift +119 -22
- package/ios/NativeListMarketRenderer.swift +113 -18
- package/ios/NativeListMessageRenderer.swift +34 -1
- package/ios/NativeListModels.swift +42 -2
- package/ios/NativeListRendererRegistry.swift +1 -0
- package/ios/NativeListResolvedText.swift +2 -1
- package/ios/NativeListRowHost.swift +14 -0
- package/ios/NativeListSectionHeaderRenderer.swift +88 -0
- package/ios/RNCNativeListView.swift +1052 -58
- package/ios/Resources/NativeListIcons.xcassets/onekey_crypto_coin_outline.imageset/Contents.json +15 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_crypto_coin_outline.imageset/icon.svg +4 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_document2.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_document2.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_dot_ver_solid.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_dot_ver_solid.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_gas_solid.imageset/Contents.json +15 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_gas_solid.imageset/icon.svg +3 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_plus_large.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_plus_large.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_refresh_ccw_outline.imageset/Contents.json +15 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_refresh_ccw_outline.imageset/icon.svg +3 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_ticket.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_ticket.imageset/icon.svg +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_wallet.imageset/Contents.json +1 -0
- package/ios/Resources/NativeListIcons.xcassets/onekey_wallet.imageset/icon.svg +1 -0
- package/ios/tests/NativeListAccessoryStackTests.swift +89 -0
- package/ios/tests/NativeListActivityPresentationTests.swift +153 -0
- package/ios/tests/NativeListIdentityTitleIconLayoutTests.swift +81 -0
- package/ios/tests/NativeListLeadingVisualPairBackdropTests.swift +50 -0
- package/ios/tests/NativeListLifecycleTests.swift +2789 -3
- package/ios/tests/NativeListMarketMetricsTests.swift +117 -0
- package/lib/module/NativeList.js +223 -36
- package/lib/module/NativeList.web.js +260 -33
- package/lib/module/index.js +1 -1
- package/lib/module/models.js +3 -0
- package/lib/module/selection.js +1 -1
- package/lib/module/validation.js +1386 -81
- package/lib/module/web/NativeListWebEngine.js +914 -80
- package/lib/module/web/templates/ActivityRowRenderer.js +487 -7
- package/lib/module/web/templates/ButtonGroupRowRenderer.js +47 -0
- package/lib/module/web/templates/DataRowRenderer.js +40 -5
- package/lib/module/web/templates/IdentityRowRenderer.js +145 -5
- package/lib/module/web/templates/MarketRowRenderer.js +8 -3
- package/lib/module/web/templates/MarketTableRowRenderer.js +577 -0
- package/lib/module/web/templates/MediaTileRowRenderer.js +260 -5
- package/lib/module/web/templates/MessageRowRenderer.js +18 -2
- package/lib/module/web/templates/RowContainerStyle.js +1 -0
- package/lib/module/web/templates/RowRendererRegistry.js +8 -3
- package/lib/module/web/templates/SectionHeaderRowRenderer.js +70 -3
- package/lib/nitrogen/generated/android/c++/JHybridNativeListSpec.cpp +26 -0
- package/lib/nitrogen/generated/android/c++/JHybridNativeListSpec.hpp +4 -0
- package/lib/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.cpp +10 -0
- package/lib/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/HybridNativeListSpec.kt +20 -0
- package/lib/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.hpp +14 -0
- package/lib/nitrogen/generated/ios/c++/views/HybridNativeListComponent.mm +12 -0
- package/lib/nitrogen/generated/ios/swift/HybridNativeListSpec.swift +2 -0
- package/lib/nitrogen/generated/ios/swift/HybridNativeListSpec_cxx.swift +43 -0
- package/lib/nitrogen/generated/shared/c++/HybridNativeListSpec.cpp +4 -0
- package/lib/nitrogen/generated/shared/c++/HybridNativeListSpec.hpp +4 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridNativeListComponent.cpp +4 -0
- package/lib/nitrogen/generated/shared/c++/views/HybridNativeListComponent.hpp +6 -0
- package/lib/nitrogen/generated/shared/json/NativeListConfig.json +2 -0
- package/lib/typescript/src/NativeList.d.ts +3 -3
- package/lib/typescript/src/NativeList.nitro.d.ts +2 -0
- package/lib/typescript/src/NativeList.types.d.ts +29 -1
- package/lib/typescript/src/NativeList.web.d.ts +2 -2
- package/lib/typescript/src/index.d.ts +2 -2
- package/lib/typescript/src/models.d.ts +196 -17
- package/lib/typescript/src/validation.d.ts +20 -0
- package/lib/typescript/src/web/NativeListWebEngine.d.ts +58 -1
- package/lib/typescript/src/web/templates/ActivityRowRenderer.d.ts +3 -3
- package/lib/typescript/src/web/templates/ButtonGroupRowRenderer.d.ts +14 -0
- package/lib/typescript/src/web/templates/IdentityRowRenderer.d.ts +1 -1
- package/lib/typescript/src/web/templates/MarketTableRowRenderer.d.ts +17 -0
- package/lib/typescript/src/web/templates/MediaTileRowRenderer.d.ts +3 -3
- package/lib/typescript/src/web/templates/MessageRowRenderer.d.ts +10 -4
- package/lib/typescript/src/web/templates/RowRendererRegistry.d.ts +3 -1
- package/nitrogen/generated/android/c++/JHybridNativeListSpec.cpp +26 -0
- package/nitrogen/generated/android/c++/JHybridNativeListSpec.hpp +4 -0
- package/nitrogen/generated/android/c++/views/JHybridNativeListStateUpdater.cpp +10 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nativelist/HybridNativeListSpec.kt +20 -0
- package/nitrogen/generated/ios/c++/HybridNativeListSpecSwift.hpp +14 -0
- package/nitrogen/generated/ios/c++/views/HybridNativeListComponent.mm +12 -0
- package/nitrogen/generated/ios/swift/HybridNativeListSpec.swift +2 -0
- package/nitrogen/generated/ios/swift/HybridNativeListSpec_cxx.swift +43 -0
- package/nitrogen/generated/shared/c++/HybridNativeListSpec.cpp +4 -0
- package/nitrogen/generated/shared/c++/HybridNativeListSpec.hpp +4 -0
- package/nitrogen/generated/shared/c++/views/HybridNativeListComponent.cpp +4 -0
- package/nitrogen/generated/shared/c++/views/HybridNativeListComponent.hpp +6 -0
- package/nitrogen/generated/shared/json/NativeListConfig.json +2 -0
- package/package.json +3 -3
- package/react-native-native-list.podspec +1 -0
- package/src/NativeList.nitro.ts +2 -0
- package/src/NativeList.tsx +418 -89
- package/src/NativeList.types.ts +34 -1
- package/src/NativeList.web.tsx +417 -66
- package/src/index.ts +2 -0
- package/src/models.ts +355 -143
- package/src/selection.ts +1 -0
- package/src/validation.ts +3315 -166
- package/src/web/NativeListWebEngine.ts +1473 -113
- package/src/web/templates/ActivityRowRenderer.ts +732 -8
- package/src/web/templates/ButtonGroupRowRenderer.ts +58 -0
- package/src/web/templates/DataRowRenderer.ts +93 -20
- package/src/web/templates/IdentityRowRenderer.ts +217 -6
- package/src/web/templates/MarketRowRenderer.ts +19 -3
- package/src/web/templates/MarketTableRowRenderer.ts +755 -0
- package/src/web/templates/MediaTileRowRenderer.ts +313 -5
- package/src/web/templates/MessageRowRenderer.ts +23 -7
- package/src/web/templates/RowContainerStyle.ts +2 -0
- package/src/web/templates/RowRendererRegistry.ts +21 -6
- package/src/web/templates/SectionHeaderRowRenderer.ts +89 -5
package/lib/module/validation.js
CHANGED
|
@@ -12,15 +12,48 @@ const MAX_HEADER_LENGTH = 4096;
|
|
|
12
12
|
const MAX_SPACER_HEIGHT = 512;
|
|
13
13
|
const MAX_SECTION_INDEX_TITLE_LENGTH = 8;
|
|
14
14
|
const MAX_MARKET_BADGES = 3;
|
|
15
|
+
const MARKET_METRIC_FIELDS = ['key', 'text', 'textSegments', 'secondaryText', 'secondaryTextSegments', 'weight', 'alignment', 'tone', 'secondaryTone'];
|
|
16
|
+
function isDevelopment() {
|
|
17
|
+
return typeof __DEV__ !== 'undefined' && __DEV__;
|
|
18
|
+
}
|
|
19
|
+
class NativeListInputError extends Error {
|
|
20
|
+
constructor(path, reason) {
|
|
21
|
+
super(`NativeList ${path}: ${reason}`);
|
|
22
|
+
this.path = path;
|
|
23
|
+
this.reason = reason;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
15
26
|
function fail(path, message) {
|
|
16
|
-
throw new
|
|
27
|
+
throw new NativeListInputError(path, message);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Component boundaries report locations, never caller-controlled payload values. */
|
|
31
|
+
export function containNativeListInput(operation) {
|
|
32
|
+
try {
|
|
33
|
+
return operation();
|
|
34
|
+
} catch (error) {
|
|
35
|
+
const path = error instanceof NativeListInputError ? error.path : 'input';
|
|
36
|
+
const reason = isDevelopment() && error instanceof NativeListInputError ? `: ${error.reason.split('"')[0].trim()}` : '';
|
|
37
|
+
console.warn(`NativeList rejected invalid input at ${path}${reason}`);
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
17
40
|
}
|
|
18
|
-
function
|
|
41
|
+
export function emptyNativeListSnapshot() {
|
|
42
|
+
return {
|
|
43
|
+
schemaVersion: 1,
|
|
44
|
+
generation: 0,
|
|
45
|
+
layout: {
|
|
46
|
+
kind: 'linear'
|
|
47
|
+
},
|
|
48
|
+
rows: []
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function assertPlainSerializable(value, path, seen, requireFinite = true) {
|
|
19
52
|
if (value === null || value === undefined) return;
|
|
20
53
|
const valueType = typeof value;
|
|
21
54
|
if (valueType === 'string' || valueType === 'boolean') return;
|
|
22
55
|
if (valueType === 'number') {
|
|
23
|
-
if (!Number.isFinite(value)) fail(path, 'numbers must be finite');
|
|
56
|
+
if (requireFinite && !Number.isFinite(value)) fail(path, 'numbers must be finite');
|
|
24
57
|
return;
|
|
25
58
|
}
|
|
26
59
|
if (valueType !== 'object') fail(path, 'must contain only plain serializable data');
|
|
@@ -28,21 +61,23 @@ function assertPlainSerializable(value, path, seen) {
|
|
|
28
61
|
if (seen.has(objectValue)) fail(path, 'must not contain circular references');
|
|
29
62
|
seen.add(objectValue);
|
|
30
63
|
if (Array.isArray(value)) {
|
|
31
|
-
value.forEach((item, index) => assertPlainSerializable(item, `${path}[${index}]`, seen));
|
|
64
|
+
value.forEach((item, index) => assertPlainSerializable(item, `${path}[${index}]`, seen, requireFinite));
|
|
32
65
|
} else {
|
|
33
66
|
const prototype = Object.getPrototypeOf(value);
|
|
34
67
|
if (prototype !== Object.prototype && prototype !== null) {
|
|
35
68
|
fail(path, 'must contain plain objects only');
|
|
36
69
|
}
|
|
37
|
-
Object.entries(value).forEach(([key, item]) =>
|
|
70
|
+
Object.entries(value).forEach(([key, item]) => {
|
|
71
|
+
if (key === '__proto__' || key === 'constructor' && typeof item === 'object') fail(path, 'unsafe object key');
|
|
72
|
+
assertPlainSerializable(item, `${path}.${key}`, seen, requireFinite);
|
|
73
|
+
});
|
|
38
74
|
}
|
|
39
75
|
seen.delete(objectValue);
|
|
40
76
|
}
|
|
41
77
|
function assertKey(key, path) {
|
|
42
|
-
if (typeof key !== 'string' || key.length === 0 || key.length > MAX_KEY_LENGTH) {
|
|
43
|
-
fail(path, `must be a non-
|
|
78
|
+
if (typeof key !== 'string' || key.trim().length === 0 || key.length > MAX_KEY_LENGTH) {
|
|
79
|
+
fail(path, `must be a non-blank string no longer than ${MAX_KEY_LENGTH} characters`);
|
|
44
80
|
}
|
|
45
|
-
if (key.trim() !== key) fail(path, 'must not have leading or trailing whitespace');
|
|
46
81
|
}
|
|
47
82
|
function assertText(text, path) {
|
|
48
83
|
if (text === undefined) return;
|
|
@@ -136,10 +171,13 @@ const TEXT_STYLE_KEYS_BY_ROW_TYPE = {
|
|
|
136
171
|
metricCard: ['title', 'value', 'subtitle', 'trend'],
|
|
137
172
|
sectionHeader: ['title', 'subtitle', 'value'],
|
|
138
173
|
action: ['title', 'value'],
|
|
174
|
+
buttonGroup: [],
|
|
139
175
|
system: ['title', 'message', 'actionText']
|
|
140
176
|
};
|
|
141
177
|
const EXTRA_STYLE_KEYS_BY_ROW_TYPE = {
|
|
142
|
-
|
|
178
|
+
identity: ['leadingContentMaxWidthRatio'],
|
|
179
|
+
market: ['titleBadgeLayout', 'contentTrailingGap', 'subtitleTrailingPadding', 'changeWidth', 'changeHeight', 'changeCornerRadius'],
|
|
180
|
+
sectionHeader: ['valuePlacement', 'titleDecoration']
|
|
143
181
|
};
|
|
144
182
|
function assertTextStyle(style, path, additionalKeys = []) {
|
|
145
183
|
if (style === undefined) return;
|
|
@@ -252,6 +290,15 @@ function assertRowStyle(row, path) {
|
|
|
252
290
|
// Market keeps its own richer assertions, driven from assertMarketRow.
|
|
253
291
|
if (row.type === 'market') return;
|
|
254
292
|
assertBoxStyle(style, `${path}.style`);
|
|
293
|
+
if (row.type === 'identity') {
|
|
294
|
+
const ratio = style?.leadingContentMaxWidthRatio;
|
|
295
|
+
if (ratio !== undefined && (!Number.isFinite(ratio) || ratio <= 0 || ratio > 1)) fail(`${path}.style.leadingContentMaxWidthRatio`, 'must be within (0, 1]');
|
|
296
|
+
}
|
|
297
|
+
if (row.type === 'sectionHeader') {
|
|
298
|
+
const sectionStyle = style;
|
|
299
|
+
if (sectionStyle?.valuePlacement !== undefined && sectionStyle.valuePlacement !== 'afterTitle') fail(`${path}.style.valuePlacement`, 'must be afterTitle');
|
|
300
|
+
if (sectionStyle?.titleDecoration !== undefined && !['none', 'dashed'].includes(sectionStyle.titleDecoration)) fail(`${path}.style.titleDecoration`, 'must be none or dashed');
|
|
301
|
+
}
|
|
255
302
|
const slots = style;
|
|
256
303
|
textKeys.forEach(key => assertTextStyle(slots[key], `${path}.style.${key}`));
|
|
257
304
|
}
|
|
@@ -347,7 +394,187 @@ function normalizeRowStyles(row) {
|
|
|
347
394
|
}
|
|
348
395
|
return next;
|
|
349
396
|
}
|
|
397
|
+
function assertMarketBadges(badges, path) {
|
|
398
|
+
if ((badges?.length ?? 0) > MAX_MARKET_BADGES) {
|
|
399
|
+
fail(`${path}.badges`, `supports at most ${MAX_MARKET_BADGES} badges`);
|
|
400
|
+
}
|
|
401
|
+
const badgeKeys = new Set();
|
|
402
|
+
badges?.forEach((badge, index) => {
|
|
403
|
+
const badgePath = `${path}.badges[${index}]`;
|
|
404
|
+
assertKey(badge.key, `${badgePath}.key`);
|
|
405
|
+
if (badgeKeys.has(badge.key)) {
|
|
406
|
+
fail(`${path}.badges`, `duplicate badge key "${badge.key}"`);
|
|
407
|
+
}
|
|
408
|
+
badgeKeys.add(badge.key);
|
|
409
|
+
assertText(badge.text, `${badgePath}.text`);
|
|
410
|
+
// OneKey patch: share typography bounds with Market text styles.
|
|
411
|
+
assertTextStyle(badge.style, `${badgePath}.style`, ['height', 'horizontalPadding']);
|
|
412
|
+
if (badge.style) assertUnknownKeys(badge.style, ['fontSize', 'fontWeight', 'lineHeight', 'height', 'horizontalPadding'], `${badgePath}.style`, 'Market badge style key');
|
|
413
|
+
assertBoundedStyleNumber(badge.style?.height, `${badgePath}.style.height`, 1, 64);
|
|
414
|
+
assertBoundedStyleNumber(badge.style?.horizontalPadding, `${badgePath}.style.horizontalPadding`, 0, 32);
|
|
415
|
+
if (badge.iconName !== undefined && badge.iconName !== 'verified') {
|
|
416
|
+
fail(`${badgePath}.iconName`, 'must be verified when provided');
|
|
417
|
+
}
|
|
418
|
+
if (badge.iconName !== undefined && badge.icon !== undefined) {
|
|
419
|
+
fail(`${badgePath}.icon`, 'cannot be combined with iconName');
|
|
420
|
+
}
|
|
421
|
+
assertImage(badge.icon, `${badgePath}.icon`);
|
|
422
|
+
if (badge.tone !== undefined && !['neutral', 'info', 'success', 'warning', 'danger'].includes(badge.tone)) {
|
|
423
|
+
fail(`${badgePath}.tone`, 'must be neutral, info, success, warning, or danger');
|
|
424
|
+
}
|
|
425
|
+
if (!badge.text && !badge.icon && !badge.iconName) {
|
|
426
|
+
fail(badgePath, 'requires text, icon, or iconName');
|
|
427
|
+
}
|
|
428
|
+
if (badge.actionKey !== undefined) {
|
|
429
|
+
assertKey(badge.actionKey, `${badgePath}.actionKey`);
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
function assertWebMarketTable(table, path) {
|
|
434
|
+
if (!table) return;
|
|
435
|
+
assertUnknownKeys(table, ['columns', 'cells', 'testID', 'draggable', 'contextMenuActionKey'], path, 'Web table key');
|
|
436
|
+
if (table.draggable !== undefined && typeof table.draggable !== 'boolean') fail(`${path}.draggable`, 'must be boolean');
|
|
437
|
+
if (table.contextMenuActionKey !== undefined) assertKey(table.contextMenuActionKey, `${path}.contextMenuActionKey`);
|
|
438
|
+
assertText(table.testID, `${path}.testID`);
|
|
439
|
+
if (!Array.isArray(table.columns) || table.columns.length < 1 || table.columns.length > 12) fail(`${path}.columns`, 'must contain 1...12 columns');
|
|
440
|
+
if (!Array.isArray(table.cells) || table.cells.length !== table.columns.length) fail(`${path}.cells`, 'must match the column count');
|
|
441
|
+
const keys = new Set();
|
|
442
|
+
table.columns.forEach((column, index) => {
|
|
443
|
+
const field = `${path}.columns[${index}]`;
|
|
444
|
+
assertUnknownKeys(column, ['key', 'width', 'minWidth', 'flex', 'alignment', 'paddingStart', 'paddingEnd'], field, 'Web column key');
|
|
445
|
+
assertKey(column.key, `${field}.key`);
|
|
446
|
+
if (keys.has(column.key)) fail(field, 'column keys must be unique');
|
|
447
|
+
keys.add(column.key);
|
|
448
|
+
assertBoundedStyleNumber(column.width, `${field}.width`, 0, 4096);
|
|
449
|
+
assertBoundedStyleNumber(column.minWidth, `${field}.minWidth`, 0, 4096);
|
|
450
|
+
assertBoundedStyleNumber(column.flex, `${field}.flex`, 0, 16);
|
|
451
|
+
assertBoundedStyleNumber(column.paddingStart, `${field}.paddingStart`, 0, 64);
|
|
452
|
+
assertBoundedStyleNumber(column.paddingEnd, `${field}.paddingEnd`, 0, 64);
|
|
453
|
+
if (column.alignment !== undefined && !['start', 'center', 'end'].includes(column.alignment)) fail(`${field}.alignment`, 'must be start, center, or end');
|
|
454
|
+
});
|
|
455
|
+
const text = (value, field) => {
|
|
456
|
+
assertUnknownKeys(value, ['text', 'segments', 'tone', 'style'], field, 'Web text key');
|
|
457
|
+
assertText(value.text, `${field}.text`);
|
|
458
|
+
assertTextTone(value.tone, `${field}.tone`);
|
|
459
|
+
assertTextStyle(value.style, `${field}.style`);
|
|
460
|
+
if (value.style) assertUnknownKeys(value.style, ['fontSize', 'fontWeight', 'lineHeight', 'color'], `${field}.style`, 'Web table text style');
|
|
461
|
+
if ((value.segments?.length ?? 0) > 64) fail(`${field}.segments`, 'supports at most 64 segments');
|
|
462
|
+
value.segments?.forEach((segment, index) => {
|
|
463
|
+
assertUnknownKeys(segment, ['text', 'style'], `${field}.segments[${index}]`, 'Web text segment');
|
|
464
|
+
assertText(segment.text, `${field}.segments[${index}].text`);
|
|
465
|
+
if (segment.style !== undefined && segment.style !== 'subscript') fail(field, 'segment style must be subscript');
|
|
466
|
+
});
|
|
467
|
+
};
|
|
468
|
+
const line = (parts, field) => {
|
|
469
|
+
if (!parts) return;
|
|
470
|
+
if (!Array.isArray(parts) || parts.length > 8) fail(field, 'supports at most 8 text parts');
|
|
471
|
+
parts.forEach((value, index) => text(value, `${field}[${index}]`));
|
|
472
|
+
};
|
|
473
|
+
table.cells.forEach((cell, index) => {
|
|
474
|
+
const field = `${path}.cells[${index}]`;
|
|
475
|
+
assertKey(cell.key, `${field}.key`);
|
|
476
|
+
if (cell.key !== table.columns[index].key) fail(field, 'cell key/order must match its column');
|
|
477
|
+
switch (cell.kind) {
|
|
478
|
+
case 'favorite':
|
|
479
|
+
assertUnknownKeys(cell, ['key', 'kind', 'active', 'disabled', 'testID', 'hoverActionKey', 'actionKey', 'accessibilityLabel'], field, 'Web favorite key');
|
|
480
|
+
if (typeof cell.active !== 'boolean') fail(field, 'active must be boolean');
|
|
481
|
+
if (cell.disabled !== undefined && typeof cell.disabled !== 'boolean') fail(field, 'disabled must be boolean');
|
|
482
|
+
if (cell.hoverActionKey !== undefined) assertKey(cell.hoverActionKey, `${field}.hoverActionKey`);
|
|
483
|
+
assertText(cell.testID, `${field}.testID`);
|
|
484
|
+
assertKey(cell.actionKey, `${field}.actionKey`);
|
|
485
|
+
assertText(cell.accessibilityLabel, `${field}.accessibilityLabel`);
|
|
486
|
+
break;
|
|
487
|
+
case 'asset':
|
|
488
|
+
assertUnknownKeys(cell, ['key', 'kind', 'leading', 'title', 'secondary', 'hoverSecondary', 'hoverActionKey', 'hoverActionLabel', 'badges', 'lineGap', 'secondaryBadges', 'hoverImages'], field, 'Web asset key');
|
|
489
|
+
assertBoundedStyleNumber(cell.lineGap, `${field}.lineGap`, 0, 8);
|
|
490
|
+
assertMarketBadges(cell.secondaryBadges, `${field}.secondary`);
|
|
491
|
+
cell.secondaryBadges?.forEach((badge, i) => {
|
|
492
|
+
assertUnknownKeys(badge, ['key', 'text', 'style'], `${field}.secondaryBadges[${i}]`, 'Web secondary badge key');
|
|
493
|
+
if (!badge.text) fail(field, 'secondary badges require text');
|
|
494
|
+
});
|
|
495
|
+
if (cell.hoverImages !== undefined && (!Array.isArray(cell.hoverImages) || cell.hoverImages.length > 4)) fail(field, 'supports at most 4 hover images');
|
|
496
|
+
cell.hoverImages?.forEach((image, i) => {
|
|
497
|
+
if (image !== null) assertImage(image, `${field}.hoverImages[${i}]`);
|
|
498
|
+
});
|
|
499
|
+
assertLeadingVisual(cell.leading, `${field}.leading`);
|
|
500
|
+
text(cell.title, `${field}.title`);
|
|
501
|
+
line(cell.secondary, `${field}.secondary`);
|
|
502
|
+
line(cell.hoverSecondary, `${field}.hoverSecondary`);
|
|
503
|
+
assertText(cell.hoverActionLabel, `${field}.hoverActionLabel`);
|
|
504
|
+
if (cell.hoverActionKey !== undefined) assertKey(cell.hoverActionKey, `${field}.hoverActionKey`);
|
|
505
|
+
assertMarketBadges(cell.badges, field);
|
|
506
|
+
cell.badges?.forEach((badge, i) => assertUnknownKeys(badge, ['key', 'text', 'iconName', 'icon', 'textColor', 'backgroundColor', 'actionKey', 'accessibilityLabel', 'style'], `${field}.badges[${i}]`, 'Web title badge key'));
|
|
507
|
+
break;
|
|
508
|
+
case 'text':
|
|
509
|
+
assertUnknownKeys(cell, ['key', 'kind', 'primary', 'secondary', 'partGap'], field, 'Web text cell key');
|
|
510
|
+
assertBoundedStyleNumber(cell.partGap, `${field}.partGap`, 0, 8);
|
|
511
|
+
if (!Array.isArray(cell.primary) || !cell.primary.length) fail(field, 'requires primary text');
|
|
512
|
+
line(cell.primary, `${field}.primary`);
|
|
513
|
+
line(cell.secondary, `${field}.secondary`);
|
|
514
|
+
break;
|
|
515
|
+
case 'sparkline':
|
|
516
|
+
assertUnknownKeys(cell, ['key', 'kind', 'points', 'width', 'height', 'strokeColor', 'fillColor'], field, 'Web sparkline key');
|
|
517
|
+
if (!Array.isArray(cell.points) || cell.points.length > 256 || cell.points.some(value => !Number.isFinite(value))) fail(field, 'supports at most 256 finite samples');
|
|
518
|
+
if (typeof cell.width !== 'number' || typeof cell.height !== 'number') fail(field, 'sparkline dimensions are required');
|
|
519
|
+
assertBoundedStyleNumber(cell.width, `${field}.width`, 1, 512);
|
|
520
|
+
assertBoundedStyleNumber(cell.height, `${field}.height`, 1, 512);
|
|
521
|
+
for (const color of [cell.strokeColor, cell.fillColor]) if (typeof color !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(color)) fail(field, 'colors must be #RRGGBB or #RRGGBBAA');
|
|
522
|
+
break;
|
|
523
|
+
default:
|
|
524
|
+
fail(field, 'unsupported Web table cell kind');
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
function assertMarketMetrics(metrics, path) {
|
|
529
|
+
if (metrics === undefined) return;
|
|
530
|
+
if (!Array.isArray(metrics) || ![2, 3].includes(metrics.length)) fail(path, 'must contain 2 or 3 columns');
|
|
531
|
+
const keys = new Set();
|
|
532
|
+
metrics.forEach((column, index) => {
|
|
533
|
+
const columnPath = `${path}[${index}]`;
|
|
534
|
+
const value = protocolObject(column, columnPath);
|
|
535
|
+
protocolKey(value.key, `${columnPath}.key`);
|
|
536
|
+
if (keys.has(column.key)) fail(`${columnPath}.key`, 'must be unique');
|
|
537
|
+
keys.add(column.key);
|
|
538
|
+
if (typeof value.text !== 'string') fail(`${columnPath}.text`, 'must be a string');
|
|
539
|
+
for (const field of ['textSegments', 'secondaryTextSegments']) {
|
|
540
|
+
const segments = column[field];
|
|
541
|
+
if (segments !== undefined && !Array.isArray(segments)) fail(`${columnPath}.${field}`, 'must be an array');
|
|
542
|
+
segments?.forEach((segment, segmentIndex) => {
|
|
543
|
+
const segmentPath = `${columnPath}.${field}[${segmentIndex}]`;
|
|
544
|
+
protocolObject(segment, segmentPath);
|
|
545
|
+
if (typeof segment.text !== 'string') fail(`${segmentPath}.text`, 'must be a string');
|
|
546
|
+
if (segment.style !== undefined && segment.style !== 'subscript') fail(`${segmentPath}.style`, 'must be subscript');
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
if (value.weight !== undefined && ![1, 2, 3].includes(value.weight)) fail(`${columnPath}.weight`, 'must be 1, 2, or 3');
|
|
550
|
+
if (value.alignment !== undefined && !['start', 'center', 'end'].includes(value.alignment)) fail(`${columnPath}.alignment`, 'must be start, center, or end');
|
|
551
|
+
if (value.secondaryText !== undefined && typeof value.secondaryText !== 'string') fail(`${columnPath}.secondaryText`, 'must be a string');
|
|
552
|
+
assertTextTone(value.tone, `${columnPath}.tone`);
|
|
553
|
+
assertTextTone(value.secondaryTone, `${columnPath}.secondaryTone`);
|
|
554
|
+
});
|
|
555
|
+
}
|
|
350
556
|
function assertMarketRow(row, path) {
|
|
557
|
+
assertWebMarketTable(row.webTable, `${path}.webTable`);
|
|
558
|
+
assertMarketMetrics(row.metrics, `${path}.metrics`);
|
|
559
|
+
if (row.metrics !== undefined) {
|
|
560
|
+
row.metrics.forEach((column, index) => {
|
|
561
|
+
const columnPath = `${path}.metrics[${index}]`;
|
|
562
|
+
const value = protocolObject(column, columnPath);
|
|
563
|
+
assertUnknownKeys(value, MARKET_METRIC_FIELDS, columnPath, 'Market metric key');
|
|
564
|
+
assertKey(column.key, `${columnPath}.key`);
|
|
565
|
+
assertText(column.text, `${columnPath}.text`);
|
|
566
|
+
assertText(column.secondaryText, `${columnPath}.secondaryText`);
|
|
567
|
+
assertTextTone(column.tone, `${columnPath}.tone`);
|
|
568
|
+
assertTextTone(column.secondaryTone, `${columnPath}.secondaryTone`);
|
|
569
|
+
for (const field of ['textSegments', 'secondaryTextSegments']) {
|
|
570
|
+
column[field]?.forEach((segment, segmentIndex) => {
|
|
571
|
+
const segmentPath = `${columnPath}.${field}[${segmentIndex}]`;
|
|
572
|
+
assertText(segment.text, `${segmentPath}.text`);
|
|
573
|
+
if (segment.style !== undefined && segment.style !== 'subscript') fail(`${segmentPath}.style`, 'must be subscript');
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
}
|
|
351
578
|
if (!['token', 'stock', 'perp'].includes(row.variant)) {
|
|
352
579
|
fail(`${path}.variant`, 'must be token, stock, or perp');
|
|
353
580
|
}
|
|
@@ -356,6 +583,7 @@ function assertMarketRow(row, path) {
|
|
|
356
583
|
// OneKey patch: validate the independently laid out Market name.
|
|
357
584
|
if (row.subtitlePrefix) {
|
|
358
585
|
assertText(row.subtitlePrefix.text, `${path}.subtitlePrefix.text`);
|
|
586
|
+
assertText(row.subtitlePrefix.hoverActionKey, `${path}.subtitlePrefix.hoverActionKey`);
|
|
359
587
|
assertBoundedStyleNumber(row.subtitlePrefix.gap, `${path}.subtitlePrefix.gap`, 0, 64);
|
|
360
588
|
assertBoundedStyleNumber(row.subtitlePrefix.maxWidth, `${path}.subtitlePrefix.maxWidth`, 1, 320);
|
|
361
589
|
assertTextStyle(row.subtitlePrefix.style, `${path}.subtitlePrefix.style`);
|
|
@@ -384,40 +612,7 @@ function assertMarketRow(row, path) {
|
|
|
384
612
|
if (!['positive', 'negative', 'neutral'].includes(row.change.tone)) {
|
|
385
613
|
fail(`${path}.change.tone`, 'must be positive, negative, or neutral');
|
|
386
614
|
}
|
|
387
|
-
|
|
388
|
-
fail(`${path}.badges`, `supports at most ${MAX_MARKET_BADGES} badges`);
|
|
389
|
-
}
|
|
390
|
-
const badgeKeys = new Set();
|
|
391
|
-
row.badges?.forEach((badge, index) => {
|
|
392
|
-
const badgePath = `${path}.badges[${index}]`;
|
|
393
|
-
assertKey(badge.key, `${badgePath}.key`);
|
|
394
|
-
if (badgeKeys.has(badge.key)) {
|
|
395
|
-
fail(`${path}.badges`, `duplicate badge key "${badge.key}"`);
|
|
396
|
-
}
|
|
397
|
-
badgeKeys.add(badge.key);
|
|
398
|
-
assertText(badge.text, `${badgePath}.text`);
|
|
399
|
-
// OneKey patch: share typography bounds with Market text styles.
|
|
400
|
-
assertTextStyle(badge.style, `${badgePath}.style`, ['height', 'horizontalPadding']);
|
|
401
|
-
if (badge.style) assertUnknownKeys(badge.style, ['fontSize', 'fontWeight', 'lineHeight', 'height', 'horizontalPadding'], `${badgePath}.style`, 'Market badge style key');
|
|
402
|
-
assertBoundedStyleNumber(badge.style?.height, `${badgePath}.style.height`, 1, 64);
|
|
403
|
-
assertBoundedStyleNumber(badge.style?.horizontalPadding, `${badgePath}.style.horizontalPadding`, 0, 32);
|
|
404
|
-
if (badge.iconName !== undefined && badge.iconName !== 'verified') {
|
|
405
|
-
fail(`${badgePath}.iconName`, 'must be verified when provided');
|
|
406
|
-
}
|
|
407
|
-
if (badge.iconName !== undefined && badge.icon !== undefined) {
|
|
408
|
-
fail(`${badgePath}.icon`, 'cannot be combined with iconName');
|
|
409
|
-
}
|
|
410
|
-
assertImage(badge.icon, `${badgePath}.icon`);
|
|
411
|
-
if (badge.tone !== undefined && !['neutral', 'info', 'success', 'warning', 'danger'].includes(badge.tone)) {
|
|
412
|
-
fail(`${badgePath}.tone`, 'must be neutral, info, success, warning, or danger');
|
|
413
|
-
}
|
|
414
|
-
if (!badge.text && !badge.icon && !badge.iconName) {
|
|
415
|
-
fail(badgePath, 'requires text, icon, or iconName');
|
|
416
|
-
}
|
|
417
|
-
if (badge.actionKey !== undefined) {
|
|
418
|
-
assertKey(badge.actionKey, `${badgePath}.actionKey`);
|
|
419
|
-
}
|
|
420
|
-
});
|
|
615
|
+
assertMarketBadges(row.badges, path);
|
|
421
616
|
for (const [key, value] of [['pressActionKey', row.pressActionKey], ['pressInActionKey', row.pressInActionKey], ['longPressActionKey', row.longPressActionKey], ['diagnostics.imageBindActionKey', row.diagnostics?.imageBindActionKey]]) {
|
|
422
617
|
if (value !== undefined) assertKey(value, `${path}.${key}`);
|
|
423
618
|
}
|
|
@@ -441,18 +636,114 @@ function assertVisualShape(shape, path) {
|
|
|
441
636
|
function assertTrailingAccessories(accessories, path) {
|
|
442
637
|
accessories?.forEach((accessory, index) => {
|
|
443
638
|
const accessoryPath = `${path}[${index}]`;
|
|
639
|
+
if (accessory.kind === 'button') {
|
|
640
|
+
assertUnknownKeys(accessory, ['kind', 'label', 'actionKey', 'disabled', 'testID', 'accessibilityLabel', 'textColor', 'backgroundColor', 'pressedBackgroundColor', 'hoverBackgroundColor', 'focusRingColor'], accessoryPath, 'button key');
|
|
641
|
+
if (typeof accessory.label !== 'string' || !accessory.label.trim()) fail(`${accessoryPath}.label`, 'must be non-blank');
|
|
642
|
+
assertKey(accessory.actionKey, `${accessoryPath}.actionKey`);
|
|
643
|
+
if (accessory.disabled !== undefined && typeof accessory.disabled !== 'boolean') fail(`${accessoryPath}.disabled`, 'must be boolean');
|
|
644
|
+
for (const field of ['textColor', 'backgroundColor', 'pressedBackgroundColor', 'hoverBackgroundColor', 'focusRingColor']) {
|
|
645
|
+
const value = accessory[field];
|
|
646
|
+
if (value !== undefined && (typeof value !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(value))) fail(`${accessoryPath}.${field}`, 'must be #RRGGBB or #RRGGBBAA');
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
if (accessory.kind === 'icon' && accessory.buttonStyle !== undefined && accessory.buttonStyle !== 'tertiary') fail(`${accessoryPath}.buttonStyle`, 'must be tertiary');
|
|
444
650
|
if (accessory.kind === 'progress' && (accessory.value < 0 || accessory.value > 1)) {
|
|
445
651
|
fail(`${accessoryPath}.value`, 'must be within 0...1');
|
|
446
652
|
}
|
|
447
653
|
if (accessory.kind === 'valuePair') {
|
|
654
|
+
if (accessory.primaryFontWeight !== undefined && accessory.primaryFontWeight !== 'medium') fail(`${accessoryPath}.primaryFontWeight`, 'must be medium when provided');
|
|
655
|
+
assertTextHelp(accessory.primaryHelp, `${accessoryPath}.primaryHelp`);
|
|
448
656
|
assertTextTone(accessory.primaryTone, `${accessoryPath}.primaryTone`);
|
|
449
657
|
assertTextTone(accessory.secondaryTone, `${accessoryPath}.secondaryTone`);
|
|
658
|
+
for (const field of ['primaryTextSegments', 'secondaryTextSegments']) {
|
|
659
|
+
const segments = accessory[field];
|
|
660
|
+
if (segments === undefined) continue;
|
|
661
|
+
const segmentPath = `${accessoryPath}.${field}`;
|
|
662
|
+
if (!Array.isArray(segments) || segments.length > 64) fail(segmentPath, 'must be an array of at most 64 segments');
|
|
663
|
+
let textLength = 0;
|
|
664
|
+
segments.forEach((segment, segmentIndex) => {
|
|
665
|
+
const path = `${segmentPath}[${segmentIndex}]`;
|
|
666
|
+
const entry = protocolObject(segment, path);
|
|
667
|
+
assertUnknownKeys(entry, ['text', 'style'], path, 'segment key');
|
|
668
|
+
if (typeof entry.text !== 'string' || entry.text.length > MAX_TEXT_LENGTH) fail(`${path}.text`, `must be a string no longer than ${MAX_TEXT_LENGTH} characters`);
|
|
669
|
+
if (entry.style !== undefined && entry.style !== 'subscript') fail(`${path}.style`, 'must be subscript when provided');
|
|
670
|
+
textLength += entry.text.length;
|
|
671
|
+
});
|
|
672
|
+
if (textLength > MAX_TEXT_LENGTH) fail(segmentPath, `total text must be at most ${MAX_TEXT_LENGTH} characters`);
|
|
673
|
+
}
|
|
450
674
|
}
|
|
451
675
|
});
|
|
452
676
|
}
|
|
677
|
+
function assertTextHelp(help, path) {
|
|
678
|
+
if (help === undefined) return;
|
|
679
|
+
const object = protocolObject(help, path);
|
|
680
|
+
assertUnknownKeys(object, ['actionKey', 'presentation', 'underline'], path, 'help key');
|
|
681
|
+
assertKey(help.actionKey, `${path}.actionKey`);
|
|
682
|
+
if (!['tooltip', 'popover'].includes(help.presentation)) fail(`${path}.presentation`, 'must be tooltip or popover');
|
|
683
|
+
if (help.underline !== undefined && help.underline !== 'dashed') fail(`${path}.underline`, 'must be dashed');
|
|
684
|
+
}
|
|
685
|
+
function assertButtonGroup(row, path) {
|
|
686
|
+
if (!Array.isArray(row.buttons) || row.buttons.length === 0) fail(`${path}.buttons`, 'must contain at least one button');
|
|
687
|
+
const keys = new Set();
|
|
688
|
+
row.buttons.forEach((button, index) => {
|
|
689
|
+
const itemPath = `${path}.buttons[${index}]`;
|
|
690
|
+
const object = protocolObject(button, itemPath);
|
|
691
|
+
assertUnknownKeys(object, ['key', 'label', 'actionKey', 'disabled', 'loading', 'testID', 'accessibilityLabel'], itemPath, 'button key');
|
|
692
|
+
assertKey(button.key, `${itemPath}.key`);
|
|
693
|
+
if (keys.has(button.key)) fail(`${itemPath}.key`, 'duplicate button key');
|
|
694
|
+
keys.add(button.key);
|
|
695
|
+
if (typeof button.label !== 'string' || !button.label.trim() || button.label.length > MAX_TEXT_LENGTH) fail(`${itemPath}.label`, 'must be non-blank bounded text');
|
|
696
|
+
assertKey(button.actionKey, `${itemPath}.actionKey`);
|
|
697
|
+
for (const field of ['disabled', 'loading']) {
|
|
698
|
+
if (button[field] !== undefined && typeof button[field] !== 'boolean') fail(`${itemPath}.${field}`, 'must be boolean');
|
|
699
|
+
}
|
|
700
|
+
for (const field of ['testID', 'accessibilityLabel']) {
|
|
701
|
+
if (button[field] !== undefined && typeof button[field] !== 'string') fail(`${itemPath}.${field}`, 'must be a string');
|
|
702
|
+
}
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
function assertIdentityExtras(row, path) {
|
|
706
|
+
for (const field of ['titleIcon', 'secondaryTitleIcon']) {
|
|
707
|
+
const titleIcon = row[field];
|
|
708
|
+
if (titleIcon === undefined) continue;
|
|
709
|
+
const iconPath = `${path}.${field}`;
|
|
710
|
+
const icon = protocolObject(titleIcon, iconPath);
|
|
711
|
+
assertUnknownKeys(icon, ['name', 'tintColor', 'accessibilityLabel', 'hoverActionKey'], iconPath, 'title icon key');
|
|
712
|
+
if (typeof titleIcon.name !== 'string' || !titleIcon.name.trim()) fail(`${iconPath}.name`, 'must be non-blank');
|
|
713
|
+
if (titleIcon.tintColor !== undefined && (typeof titleIcon.tintColor !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(titleIcon.tintColor))) fail(`${iconPath}.tintColor`, 'must be #RRGGBB or #RRGGBBAA');
|
|
714
|
+
if (titleIcon.accessibilityLabel !== undefined && typeof titleIcon.accessibilityLabel !== 'string') fail(`${iconPath}.accessibilityLabel`, 'must be a string');
|
|
715
|
+
if (titleIcon.hoverActionKey !== undefined) assertKey(titleIcon.hoverActionKey, `${iconPath}.hoverActionKey`);
|
|
716
|
+
}
|
|
717
|
+
if (row.titleImage !== undefined) {
|
|
718
|
+
const imagePath = `${path}.titleImage`;
|
|
719
|
+
const descriptor = protocolObject(row.titleImage, imagePath);
|
|
720
|
+
assertUnknownKeys(descriptor, ['image', 'accessibilityLabel', 'hoverActionKey'], imagePath, 'title image key');
|
|
721
|
+
protocolObject(row.titleImage.image, `${imagePath}.image`);
|
|
722
|
+
if (typeof row.titleImage.image.uri !== 'string') fail(`${imagePath}.image.uri`, 'must be a string');
|
|
723
|
+
assertImage(row.titleImage.image, `${imagePath}.image`);
|
|
724
|
+
const {
|
|
725
|
+
width,
|
|
726
|
+
height
|
|
727
|
+
} = row.titleImage.image;
|
|
728
|
+
if (width !== height || width < 1 || width > 40) fail(`${imagePath}.image`, 'must be square within 1...40');
|
|
729
|
+
if (row.titleImage.accessibilityLabel !== undefined && typeof row.titleImage.accessibilityLabel !== 'string') fail(`${imagePath}.accessibilityLabel`, 'must be a string');
|
|
730
|
+
if (row.titleImage.hoverActionKey !== undefined) assertKey(row.titleImage.hoverActionKey, `${imagePath}.hoverActionKey`);
|
|
731
|
+
}
|
|
732
|
+
const ratio = row.style?.leadingContentMaxWidthRatio;
|
|
733
|
+
if (ratio !== undefined && (typeof ratio !== 'number' || !Number.isFinite(ratio) || ratio <= 0 || ratio > 1)) fail(`${path}.style.leadingContentMaxWidthRatio`, 'must be within (0, 1]');
|
|
734
|
+
assertTrailingAccessories(row.trailing, `${path}.trailing`);
|
|
735
|
+
}
|
|
453
736
|
function assertImage(image, path) {
|
|
454
737
|
if (!image) return;
|
|
738
|
+
if (!isAllowedImageUri(image.uri)) fail(`${path}.uri`, 'unsupported image URI');
|
|
455
739
|
if (!image.uri.trim()) fail(`${path}.uri`, 'must not be empty');
|
|
740
|
+
if (image.fallbackUris !== undefined) {
|
|
741
|
+
if (!Array.isArray(image.fallbackUris)) fail(`${path}.fallbackUris`, 'must be an array');
|
|
742
|
+
image.fallbackUris.forEach((uri, index) => {
|
|
743
|
+
if (typeof uri !== 'string') fail(`${path}.fallbackUris[${index}]`, 'must be a string');
|
|
744
|
+
assertText(uri, `${path}.fallbackUris[${index}]`);
|
|
745
|
+
});
|
|
746
|
+
}
|
|
456
747
|
if (!Number.isFinite(image.width) || !Number.isFinite(image.height) || image.width <= 0 || image.height <= 0 || image.width > MAX_IMAGE_EDGE || image.height > MAX_IMAGE_EDGE) {
|
|
457
748
|
fail(path, `width and height must be within 1...${MAX_IMAGE_EDGE}`);
|
|
458
749
|
}
|
|
@@ -487,6 +778,9 @@ function assertLeadingVisual(visual, path) {
|
|
|
487
778
|
assertText(visual.cornerIcon?.name, `${path}.cornerIcon.name`);
|
|
488
779
|
// OneKey patch: validate optional selector overlays at the JSON boundary.
|
|
489
780
|
assertText(visual.fallbackIcon?.name, `${path}.fallbackIcon.name`);
|
|
781
|
+
if (visual.fallbackTextColor !== undefined && (typeof visual.fallbackTextColor !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(visual.fallbackTextColor))) fail(`${path}.fallbackTextColor`, 'must be a hex color');
|
|
782
|
+
if (visual.fallbackIcon?.size !== undefined && (!Number.isFinite(visual.fallbackIcon.size) || visual.fallbackIcon.size <= 0 || visual.fallbackIcon.size > 40)) fail(`${path}.fallbackIcon.size`, 'must be within (0, 40]');
|
|
783
|
+
if (visual.fallbackIcon?.backgroundColor !== undefined && !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(visual.fallbackIcon.backgroundColor)) fail(`${path}.fallbackIcon.backgroundColor`, 'must be a hex color');
|
|
490
784
|
if ((visual.overlays?.length ?? 0) > 2) fail(`${path}.overlays`, 'supports at most two overlays');
|
|
491
785
|
visual.overlays?.forEach((overlay, index) => {
|
|
492
786
|
if (!['topLeft', 'bottomRight'].includes(overlay.position)) fail(`${path}.overlays[${index}].position`, 'must be topLeft or bottomRight');
|
|
@@ -503,7 +797,11 @@ function assertLeadingVisual(visual, path) {
|
|
|
503
797
|
}
|
|
504
798
|
if (overlay.padding !== undefined && overlay.padding * 2 >= Math.min(overlay.width ?? overlay.size ?? 20, overlay.height ?? overlay.size ?? 20)) fail(`${path}.overlays[${index}].padding`, 'must fit inside the overlay');
|
|
505
799
|
assertImage(overlay.image, `${path}.overlays[${index}].image`);
|
|
800
|
+
if (overlay.fallbackIcon !== undefined && !overlay.image) fail(`${path}.overlays[${index}].fallbackIcon`, 'requires image');
|
|
801
|
+
if (overlay.fallbackIcon !== undefined && (typeof overlay.fallbackIcon.name !== 'string' || !overlay.fallbackIcon.name.trim())) fail(`${path}.overlays[${index}].fallbackIcon.name`, 'must be non-blank');
|
|
802
|
+
if (overlay.fallbackIcon?.tintColor !== undefined && !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(overlay.fallbackIcon.tintColor)) fail(`${path}.overlays[${index}].fallbackIcon.tintColor`, 'must be a hex color');
|
|
506
803
|
assertText(overlay.text, `${path}.overlays[${index}].text`);
|
|
804
|
+
if (overlay.fontWeight !== undefined && !['regular', 'medium', 'semibold', 'bold'].includes(overlay.fontWeight)) fail(`${path}.overlays[${index}].fontWeight`, 'must be regular, medium, semibold, or bold');
|
|
507
805
|
});
|
|
508
806
|
if (visual.kind === 'token') {
|
|
509
807
|
assertImage(visual.networkImage, `${path}.networkImage`);
|
|
@@ -517,6 +815,7 @@ function assertVisual(row, path) {
|
|
|
517
815
|
});
|
|
518
816
|
}
|
|
519
817
|
function assertActivity(row, path) {
|
|
818
|
+
if (row.hideLeading !== undefined && typeof row.hideLeading !== 'boolean') fail(`${path}.hideLeading`, 'must be boolean');
|
|
520
819
|
assertText(row.title, `${path}.title`);
|
|
521
820
|
assertText(row.description, `${path}.description`);
|
|
522
821
|
if (row.presentation !== undefined && !['stacked', 'table'].includes(row.presentation)) fail(`${path}.presentation`, 'must be stacked or table');
|
|
@@ -595,6 +894,7 @@ function assertRowType(type, path) {
|
|
|
595
894
|
}
|
|
596
895
|
function assertRow(row, index, path = `rows[${index}]`) {
|
|
597
896
|
assertRowType(row.type, path);
|
|
897
|
+
assertUnknownKeys(row, ROW_FIELDS[row.type], path, 'row key');
|
|
598
898
|
assertKey(row.key, `${path}.key`);
|
|
599
899
|
// OneKey patch: explicit dimensions and opacity cannot corrupt list layout.
|
|
600
900
|
if (row.height !== undefined && (row.height < 0 || row.height > 4096)) fail(`${path}.height`, 'must be within 0...4096');
|
|
@@ -613,10 +913,26 @@ function assertRow(row, index, path = `rows[${index}]`) {
|
|
|
613
913
|
assertWalletGroup(row, path);
|
|
614
914
|
break;
|
|
615
915
|
case 'identity':
|
|
916
|
+
if (row.contentHeight !== undefined && row.contentHeight !== 'auto') {
|
|
917
|
+
fail(`${path}.contentHeight`, 'must be auto');
|
|
918
|
+
}
|
|
919
|
+
if (row.subtitleLines !== undefined && ![0, 1, 2].includes(row.subtitleLines)) {
|
|
920
|
+
fail(`${path}.subtitleLines`, 'must be 0, 1, or 2');
|
|
921
|
+
}
|
|
922
|
+
if (row.longPressActionKey !== undefined) {
|
|
923
|
+
assertKey(row.longPressActionKey, `${path}.longPressActionKey`);
|
|
924
|
+
}
|
|
616
925
|
if (row.presentation !== undefined && row.presentation !== 'walletSidebar' && row.presentation !== 'accountSelector' && row.presentation !== 'networkSelector') {
|
|
617
926
|
fail(`${path}.presentation`, 'must be walletSidebar, accountSelector, or networkSelector when provided');
|
|
618
927
|
}
|
|
619
928
|
assertText(row.title, `${path}.title`);
|
|
929
|
+
if ((row.titleSuffixes?.length ?? 0) > 3) fail(`${path}.titleSuffixes`, 'supports at most 3 suffixes');
|
|
930
|
+
row.titleSuffixes?.forEach((suffix, index) => {
|
|
931
|
+
assertText(suffix.text, `${path}.titleSuffixes[${index}].text`);
|
|
932
|
+
if (!['secondary', 'disabled'].includes(suffix.tone)) fail(`${path}.titleSuffixes[${index}].tone`, 'must be secondary or disabled');
|
|
933
|
+
if (suffix.maxWidth !== undefined && (!Number.isFinite(suffix.maxWidth) || suffix.maxWidth < 0 || suffix.maxWidth > 4096)) fail(`${path}.titleSuffixes[${index}].maxWidth`, 'must be finite within 0...4096');
|
|
934
|
+
});
|
|
935
|
+
assertIdentityExtras(row, path);
|
|
620
936
|
assertText(row.subtitle, `${path}.subtitle`);
|
|
621
937
|
// OneKey patch: selector text segments truncate independently.
|
|
622
938
|
row.subtitleSegments?.forEach((segment, segmentIndex) => {
|
|
@@ -648,10 +964,13 @@ function assertRow(row, index, path = `rows[${index}]`) {
|
|
|
648
964
|
assertActivity(row, path);
|
|
649
965
|
break;
|
|
650
966
|
case 'message':
|
|
967
|
+
if (row.presentation !== undefined && row.presentation !== 'inlineWarning') {
|
|
968
|
+
fail(`${path}.presentation`, 'must be inlineWarning');
|
|
969
|
+
}
|
|
651
970
|
assertText(row.title, `${path}.title`);
|
|
652
971
|
assertText(row.body, `${path}.body`);
|
|
653
|
-
if (![1, 2, 3].includes(row.bodyLines ?? 3)) {
|
|
654
|
-
fail(`${path}.bodyLines`, 'must be 1, 2, or 3');
|
|
972
|
+
if (![0, 1, 2, 3].includes(row.bodyLines ?? 3)) {
|
|
973
|
+
fail(`${path}.bodyLines`, 'must be 0, 1, 2, or 3');
|
|
655
974
|
}
|
|
656
975
|
assertImage(row.thumbnail, `${path}.thumbnail`);
|
|
657
976
|
break;
|
|
@@ -662,6 +981,13 @@ function assertRow(row, index, path = `rows[${index}]`) {
|
|
|
662
981
|
row.columns.forEach((column, columnIndex) => {
|
|
663
982
|
assertKey(column.key, `${path}.columns[${columnIndex}].key`);
|
|
664
983
|
assertText(column.text, `${path}.columns[${columnIndex}].text`);
|
|
984
|
+
assertIdentityExtras(column, `${path}.columns[${columnIndex}]`);
|
|
985
|
+
for (const field of ['textSegments', 'secondaryTextSegments']) {
|
|
986
|
+
column[field]?.forEach((segment, segmentIndex) => {
|
|
987
|
+
assertText(segment.text, `${path}.columns[${columnIndex}].${field}[${segmentIndex}].text`);
|
|
988
|
+
if (segment.style !== undefined && segment.style !== 'subscript') fail(`${path}.columns[${columnIndex}].${field}[${segmentIndex}].style`, 'must be subscript');
|
|
989
|
+
});
|
|
990
|
+
}
|
|
665
991
|
assertText(column.secondaryLeadingText, `${path}.columns[${columnIndex}].secondaryLeadingText`);
|
|
666
992
|
assertText(column.secondaryText, `${path}.columns[${columnIndex}].secondaryText`);
|
|
667
993
|
assertTextTone(column.secondaryTone, `${path}.columns[${columnIndex}].secondaryTone`);
|
|
@@ -669,6 +995,10 @@ function assertRow(row, index, path = `rows[${index}]`) {
|
|
|
669
995
|
if ((row.badges?.length ?? 0) > MAX_BADGES) {
|
|
670
996
|
fail(`${path}.badges`, `supports at most ${MAX_BADGES} badges`);
|
|
671
997
|
}
|
|
998
|
+
if ((row.trailing?.length ?? 0) > MAX_TRAILING_ACCESSORIES) {
|
|
999
|
+
fail(`${path}.trailing`, `supports at most ${MAX_TRAILING_ACCESSORIES} accessories`);
|
|
1000
|
+
}
|
|
1001
|
+
assertTrailingAccessories(row.trailing, `${path}.trailing`);
|
|
672
1002
|
break;
|
|
673
1003
|
case 'market':
|
|
674
1004
|
assertMarketRow(row, path);
|
|
@@ -719,6 +1049,11 @@ function assertRow(row, index, path = `rows[${index}]`) {
|
|
|
719
1049
|
assertText(row.title, `${path}.title`);
|
|
720
1050
|
assertText(row.subtitle, `${path}.subtitle`);
|
|
721
1051
|
assertText(row.value, `${path}.value`);
|
|
1052
|
+
if (row.leadingBadge) {
|
|
1053
|
+
assertKey(row.leadingBadge.key, `${path}.leadingBadge.key`);
|
|
1054
|
+
assertText(row.leadingBadge.text, `${path}.leadingBadge.text`);
|
|
1055
|
+
}
|
|
1056
|
+
assertTextHelp(row.valueHelp, `${path}.valueHelp`);
|
|
722
1057
|
if (row.valueActionKey !== undefined) {
|
|
723
1058
|
assertKey(row.valueActionKey, `${path}.valueActionKey`);
|
|
724
1059
|
}
|
|
@@ -739,6 +1074,9 @@ function assertRow(row, index, path = `rows[${index}]`) {
|
|
|
739
1074
|
}
|
|
740
1075
|
assertTrailingAccessories(row.trailing, `${path}.trailing`);
|
|
741
1076
|
break;
|
|
1077
|
+
case 'buttonGroup':
|
|
1078
|
+
assertButtonGroup(row, path);
|
|
1079
|
+
break;
|
|
742
1080
|
case 'system':
|
|
743
1081
|
const presentation = 'presentation' in row ? row.presentation : undefined;
|
|
744
1082
|
if (presentation !== undefined && presentation !== 'market') {
|
|
@@ -799,9 +1137,23 @@ function assertMediaPreview(value, path) {
|
|
|
799
1137
|
fail(`${path}.probeOrder`, 'must contain one or two distinct image/video candidates');
|
|
800
1138
|
}
|
|
801
1139
|
}
|
|
802
|
-
|
|
1140
|
+
function assertSnapshotContract(snapshot) {
|
|
1141
|
+
if (snapshot.disclosure !== undefined) {
|
|
1142
|
+
if (typeof snapshot.disclosure.expanded !== 'boolean') fail('snapshot.disclosure.expanded', 'must be a boolean');
|
|
1143
|
+
const inset = snapshot.disclosure.expandedTrailingInset;
|
|
1144
|
+
if (inset !== undefined && (!Number.isFinite(inset) || inset < 0)) fail('snapshot.disclosure.expandedTrailingInset', 'must be finite and nonnegative');
|
|
1145
|
+
}
|
|
1146
|
+
if (snapshot.structuralTransition) {
|
|
1147
|
+
assertKey(snapshot.structuralTransition.id, 'snapshot.structuralTransition.id');
|
|
1148
|
+
if (snapshot.structuralTransition.anchorKey !== undefined) assertKey(snapshot.structuralTransition.anchorKey, 'snapshot.structuralTransition.anchorKey');
|
|
1149
|
+
const duration = snapshot.structuralTransition.durationMs;
|
|
1150
|
+
if (duration !== undefined && (!Number.isFinite(duration) || duration < 0 || duration > 2000)) fail('snapshot.structuralTransition.durationMs', 'must be finite within 0...2000');
|
|
1151
|
+
}
|
|
803
1152
|
if (snapshot.capabilities?.refreshTriggerDistance !== undefined && (!Number.isFinite(snapshot.capabilities.refreshTriggerDistance) || snapshot.capabilities.refreshTriggerDistance <= 0)) fail('capabilities.refreshTriggerDistance', 'must be a positive finite number');
|
|
804
1153
|
assertPlainSerializable(snapshot, 'snapshot', new Set());
|
|
1154
|
+
assertText(snapshot.theme?.imageFallbackBackground, 'theme.imageFallbackBackground');
|
|
1155
|
+
if (snapshot.theme?.successBackground !== undefined && (typeof snapshot.theme.successBackground !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(snapshot.theme.successBackground))) fail('theme.successBackground', 'must be a hex color');
|
|
1156
|
+
if (snapshot.theme?.rowHoverBackground !== undefined && (typeof snapshot.theme.rowHoverBackground !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(snapshot.theme.rowHoverBackground))) fail('theme.rowHoverBackground', 'must be a hex color');
|
|
805
1157
|
if (snapshot.schemaVersion !== 1) fail('snapshot.schemaVersion', 'must be 1');
|
|
806
1158
|
if (!Number.isSafeInteger(snapshot.generation) || snapshot.generation < 0) {
|
|
807
1159
|
fail('snapshot.generation', 'must be a non-negative safe integer');
|
|
@@ -870,7 +1222,619 @@ export function validateSnapshot(snapshot) {
|
|
|
870
1222
|
if (snapshot.selection?.mode === 'single' && selectedKeys.size > 1) {
|
|
871
1223
|
fail('snapshot.selection.selectedKeys', 'supports at most one key in single mode');
|
|
872
1224
|
}
|
|
873
|
-
return
|
|
1225
|
+
return snapshot;
|
|
1226
|
+
}
|
|
1227
|
+
const COMMON_ROW_FIELDS = ['height', 'heightRounding', 'testID', 'opacity', 'backgroundColor', 'backgroundFullWidth', 'pressDisabled', 'key', 'revision', 'sectionKey', 'groupId', 'groupPosition', 'size', 'density', 'disabled', 'selected', 'accessibilityLabel', 'separator', 'type', 'style'];
|
|
1228
|
+
const ROW_FIELDS = {
|
|
1229
|
+
identity: [...COMMON_ROW_FIELDS, 'contentHeight', 'longPressActionKey', 'titleMatch', 'titleActionKey', 'titleActionOnHover', 'subtitleSegments', 'presentation', 'leading', 'leadingAction', 'title', 'titleSuffixes', 'titleIcon', 'secondaryTitleIcon', 'titleImage', 'subtitle', 'tertiary', 'tertiaryTone', 'titleLines', 'subtitleLines', 'badges', 'trailing', 'draggable'],
|
|
1230
|
+
walletGroup: [...COMMON_ROW_FIELDS, 'parent', 'children', 'draggable'],
|
|
1231
|
+
rail: [...COMMON_ROW_FIELDS, 'visual', 'title', 'status', 'badge', 'draggable'],
|
|
1232
|
+
activity: [...COMMON_ROW_FIELDS, 'leading', 'hideLeading', 'secondaryLeading', 'title', 'description', 'status', 'primaryAmount', 'secondaryAmount', 'amounts', 'badges', 'presentation', 'fee', 'descriptionActionKey', 'footerActions'],
|
|
1233
|
+
message: [...COMMON_ROW_FIELDS, 'presentation', 'unread', 'leading', 'title', 'body', 'bodyLines', 'time', 'thumbnail'],
|
|
1234
|
+
dataRow: [...COMMON_ROW_FIELDS, 'leading', 'columns', 'trailing', 'checkbox', 'index', 'badge', 'badges', 'favorite', 'favoriteActive'],
|
|
1235
|
+
market: [...COMMON_ROW_FIELDS, 'variant', 'webTable', 'metrics', 'leadingAction', 'leading', 'title', 'subtitle', 'subtitlePrefix', 'subtitleSegments', 'price', 'priceSegments', 'change', 'badges', 'pressActionKey', 'pressInActionKey', 'longPressActionKey', 'diagnostics'],
|
|
1236
|
+
mediaTile: [...COMMON_ROW_FIELDS, 'variant', 'media', 'image', 'imageState', 'networkImage', 'title', 'subtitle', 'badge', 'closeActionKey'],
|
|
1237
|
+
metricCard: [...COMMON_ROW_FIELDS, 'variant', 'title', 'value', 'subtitle', 'trend', 'trendTone', 'visual', 'badge', 'metrics', 'progress'],
|
|
1238
|
+
sectionHeader: [...COMMON_ROW_FIELDS, 'sticky', 'valueActionTestID', 'valueSegments', 'titleActionKey', 'titleActionOnHover', 'presentation', 'indexTitle', 'variant', 'title', 'leadingBadge', 'titleLoading', 'subtitle', 'value', 'valueHelp', 'valueActionKey', 'titleIcon', 'valueIcon', 'checkbox'],
|
|
1239
|
+
action: [...COMMON_ROW_FIELDS, 'presentation', 'title', 'actionKey', 'tone', 'icon', 'checkbox', 'trailing'],
|
|
1240
|
+
buttonGroup: [...COMMON_ROW_FIELDS, 'buttons'],
|
|
1241
|
+
system: [...COMMON_ROW_FIELDS, 'variant', 'presentation', 'loadingStyle', 'message', 'actionKey', 'actionText', 'title', 'borderColor']
|
|
1242
|
+
};
|
|
1243
|
+
const COMMON_PATCH_FIELDS = ['height', 'heightRounding', 'opacity', 'pressDisabled', 'revision', 'disabled', 'selected', 'accessibilityLabel', 'separator', 'style'];
|
|
1244
|
+
const PATCH_FIELDS = {
|
|
1245
|
+
identity: [...COMMON_PATCH_FIELDS, 'contentHeight', 'longPressActionKey', 'titleMatch', 'titleActionKey', 'titleActionOnHover', 'subtitleSegments', 'leading', 'leadingAction', 'title', 'titleSuffixes', 'titleIcon', 'secondaryTitleIcon', 'titleImage', 'subtitle', 'tertiary', 'tertiaryTone', 'subtitleLines', 'badges', 'trailing'],
|
|
1246
|
+
rail: [...COMMON_PATCH_FIELDS, 'title', 'visual', 'status', 'badge'],
|
|
1247
|
+
activity: [...COMMON_PATCH_FIELDS, 'presentation', 'leading', 'title', 'badges', 'status', 'hideLeading', 'secondaryLeading', 'description', 'primaryAmount', 'secondaryAmount', 'amounts', 'fee', 'descriptionActionKey', 'footerActions'],
|
|
1248
|
+
message: [...COMMON_PATCH_FIELDS, 'presentation', 'title', 'unread', 'body', 'bodyLines', 'time', 'thumbnail'],
|
|
1249
|
+
dataRow: [...COMMON_PATCH_FIELDS, 'leading', 'badges', 'trailing', 'badge', 'columns', 'checkbox', 'index', 'favorite', 'favoriteActive'],
|
|
1250
|
+
market: [...COMMON_PATCH_FIELDS, 'longPressActionKey', 'subtitleSegments', 'leading', 'leadingAction', 'title', 'subtitle', 'badges', 'webTable', 'metrics', 'price', 'priceSegments', 'change', 'pressActionKey', 'pressInActionKey', 'diagnostics'],
|
|
1251
|
+
mediaTile: [...COMMON_PATCH_FIELDS, 'title', 'subtitle', 'badge', 'media', 'image', 'imageState', 'networkImage', 'closeActionKey'],
|
|
1252
|
+
metricCard: [...COMMON_PATCH_FIELDS, 'title', 'subtitle', 'visual', 'badge', 'variant', 'value', 'trend', 'trendTone', 'metrics', 'progress'],
|
|
1253
|
+
sectionHeader: [...COMMON_PATCH_FIELDS, 'titleActionKey', 'titleActionOnHover', 'title', 'titleIcon', 'subtitle', 'checkbox', 'variant', 'value', 'leadingBadge', 'titleLoading', 'valueHelp', 'valueActionKey', 'valueIcon'],
|
|
1254
|
+
action: [...COMMON_PATCH_FIELDS, 'title', 'trailing', 'checkbox', 'tone', 'icon'],
|
|
1255
|
+
buttonGroup: [...COMMON_PATCH_FIELDS, 'buttons'],
|
|
1256
|
+
system: ['revision', 'disabled', 'message']
|
|
1257
|
+
};
|
|
1258
|
+
function protocolObject(value, path) {
|
|
1259
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) fail(path, 'must be an object');
|
|
1260
|
+
return value;
|
|
1261
|
+
}
|
|
1262
|
+
function protocolKey(value, path) {
|
|
1263
|
+
if (typeof value !== 'string' || value.trim().length === 0) fail(path, 'must be a non-blank string');
|
|
1264
|
+
}
|
|
1265
|
+
const PROTOCOL_ARRAY_FIELDS = new Set(['columns', 'titleSuffixes', 'cells', 'points', 'images', 'overlays', 'badges', 'trailing', 'footerActions', 'buttons', 'amounts', 'metrics', 'children', 'subtitleSegments', 'titleMatch', 'selectedKeys', 'segments', 'hoverSecondary', 'hoverImages', 'secondaryBadges', 'fallbackUris', 'probeOrder', 'priceSegments', 'textSegments', 'secondaryTextSegments', 'primaryTextSegments']);
|
|
1266
|
+
const PROTOCOL_NUMBER_FIELDS = new Set(['leadingContentMaxWidthRatio', 'height', 'width', 'fontSize', 'lineHeight', 'opacity', 'offsetY', 'offsetX', 'cornerRadius', 'borderWidth', 'horizontalPadding', 'verticalPadding', 'leadingGap', 'lineGap', 'titleBadgeGap', 'trailingGap', 'start', 'end', 'padding', 'offset', 'overscan', 'minWidth', 'flex', 'paddingStart', 'paddingEnd', 'changeWidth', 'changeHeight', 'changeCornerRadius', 'contentTrailingGap', 'subtitleTrailingPadding', 'partGap', 'contentPadding', 'contentPaddingHorizontal', 'contentPaddingTop', 'contentPaddingBottom', 'itemSpacing', 'gridColumns']);
|
|
1267
|
+
|
|
1268
|
+
/** Validate only shapes that renderers dereference, not display budgets. */
|
|
1269
|
+
function assertProtocolFields(value, path, parentField = '') {
|
|
1270
|
+
if (value === undefined || value === null || typeof value !== 'object') return;
|
|
1271
|
+
if (Array.isArray(value)) {
|
|
1272
|
+
for (const [index, item] of value.entries()) {
|
|
1273
|
+
const itemPath = `${path}[${index}]`;
|
|
1274
|
+
if (['selectedKeys', 'fallbackUris', 'probeOrder'].includes(parentField)) {
|
|
1275
|
+
if (typeof item !== 'string') fail(itemPath, 'must be a string');
|
|
1276
|
+
} else if (parentField === 'points') {
|
|
1277
|
+
if (typeof item !== 'number') fail(itemPath, 'must be a finite number');
|
|
1278
|
+
} else if (parentField === 'hoverImages' && item === null) continue;else protocolObject(item, itemPath);
|
|
1279
|
+
assertProtocolFields(item, itemPath, parentField);
|
|
1280
|
+
}
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
const object = value;
|
|
1284
|
+
if (parentField === 'fallbackIcon') {
|
|
1285
|
+
protocolKey(object.name, `${path}.name`);
|
|
1286
|
+
if (object.size !== undefined && (typeof object.size !== 'number' || !Number.isFinite(object.size) || object.size <= 0 || object.size > 40)) fail(`${path}.size`, 'must be within (0, 40]');
|
|
1287
|
+
for (const field of ['tintColor', 'backgroundColor']) {
|
|
1288
|
+
const color = object[field];
|
|
1289
|
+
if (color !== undefined && (typeof color !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(color))) fail(`${path}.${field}`, 'must be a hex color');
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
if (parentField === 'overlays' && object.fallbackIcon !== undefined && !object.image) fail(`${path}.fallbackIcon`, 'requires image');
|
|
1293
|
+
if (!path.includes('.style.') && ['image', 'networkImage', 'thumbnail', 'images', 'hoverImages'].includes(parentField) && typeof object.uri !== 'string') fail(`${path}.uri`, 'must be a string');
|
|
1294
|
+
Object.entries(value).forEach(([field, item]) => {
|
|
1295
|
+
if (item === undefined) return;
|
|
1296
|
+
if (field === 'headers') {
|
|
1297
|
+
const headers = protocolObject(item, `${path}.headers`);
|
|
1298
|
+
for (const [name, headerValue] of Object.entries(headers)) {
|
|
1299
|
+
if (!name.trim() || typeof headerValue !== 'string') fail(`${path}.headers`, 'must contain string headers');
|
|
1300
|
+
}
|
|
1301
|
+
return;
|
|
1302
|
+
}
|
|
1303
|
+
const fieldPath = `${path}.${field}`;
|
|
1304
|
+
if (!path.endsWith('.style') && PROTOCOL_ARRAY_FIELDS.has(field) && !Array.isArray(item)) fail(fieldPath, 'must be an array');
|
|
1305
|
+
if (PROTOCOL_NUMBER_FIELDS.has(field) && typeof item !== 'number') fail(fieldPath, 'must be a finite number');
|
|
1306
|
+
if (PROTOCOL_NUMBER_FIELDS.has(field) && typeof item === 'number' && item < 0 && !['offsetX', 'offsetY'].includes(field)) fail(fieldPath, 'must be non-negative');
|
|
1307
|
+
if (parentField === 'overlays' && field === 'fontWeight' && !['regular', 'medium', 'semibold', 'bold'].includes(item)) fail(fieldPath, 'must be regular, medium, semibold, or bold');
|
|
1308
|
+
if (field === 'buttonStyle' && item !== 'tertiary') fail(fieldPath, 'must be tertiary');
|
|
1309
|
+
if (field === 'hideLeading' && typeof item !== 'boolean') fail(fieldPath, 'must be boolean');
|
|
1310
|
+
if (field === 'key' || field === 'sectionKey' || field === 'groupId' || field === 'actionKey' || field.endsWith('ActionKey')) protocolKey(item, fieldPath);
|
|
1311
|
+
if ((field === 'uri' || field === 'fallbackUri') && typeof item !== 'string') fail(fieldPath, 'must be a string');
|
|
1312
|
+
if (field === 'style' && !(item === 'subscript' && /[Ss]egments\[/.test(path)) || field === 'container') protocolObject(item, fieldPath);
|
|
1313
|
+
if (path.endsWith('.style') && ['title', 'subtitle', 'tertiary', 'body', 'time', 'price', 'change', 'columns', 'columnSecondary', 'value', 'valueSecondary', 'badge', 'description', 'status', 'primaryAmount', 'secondaryAmount', 'trend', 'message', 'actionText'].includes(field)) protocolObject(item, fieldPath);
|
|
1314
|
+
assertProtocolFields(item, fieldPath, field);
|
|
1315
|
+
});
|
|
1316
|
+
}
|
|
1317
|
+
function assertRowProtocol(row, path) {
|
|
1318
|
+
protocolObject(row, path);
|
|
1319
|
+
assertRowType(row.type, path);
|
|
1320
|
+
protocolKey(row.key, `${path}.key`);
|
|
1321
|
+
assertCoreRow(row, path);
|
|
1322
|
+
assertProtocolFields(row, path);
|
|
1323
|
+
for (const field of ['leading', 'visual', 'secondaryLeading']) {
|
|
1324
|
+
const visual = row[field];
|
|
1325
|
+
if (visual !== undefined || field === 'leading' && (row.type === 'identity' || row.type === 'market')) {
|
|
1326
|
+
const object = protocolObject(visual, `${path}.${field}`);
|
|
1327
|
+
if (object.kind === 'stackedImages' && !Array.isArray(object.images)) fail(`${path}.${field}.images`, 'must be an array');
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
for (const field of ['title', 'subtitle', 'tertiary', 'body', 'time', 'price', 'indexTitle']) {
|
|
1331
|
+
const value = row[field];
|
|
1332
|
+
if (value !== undefined && typeof value !== 'string') fail(`${path}.${field}`, 'must be a string');
|
|
1333
|
+
}
|
|
1334
|
+
if (row.height !== undefined && row.height < 0) fail(`${path}.height`, 'must be non-negative');
|
|
1335
|
+
if (row.type === 'identity') {
|
|
1336
|
+
if (isDevelopment()) assertIdentityExtras(row, path);
|
|
1337
|
+
if (row.titleSuffixes !== undefined && !Array.isArray(row.titleSuffixes)) fail(`${path}.titleSuffixes`, 'must be an array');
|
|
1338
|
+
row.titleSuffixes?.forEach((suffix, index) => {
|
|
1339
|
+
protocolObject(suffix, `${path}.titleSuffixes[${index}]`);
|
|
1340
|
+
if (typeof suffix.text !== 'string' || !['secondary', 'disabled'].includes(suffix.tone)) fail(`${path}.titleSuffixes[${index}]`, 'invalid title suffix');
|
|
1341
|
+
if (suffix.maxWidth !== undefined && (!Number.isFinite(suffix.maxWidth) || suffix.maxWidth < 0 || suffix.maxWidth > 4096)) fail(`${path}.titleSuffixes[${index}].maxWidth`, 'must be finite within 0...4096');
|
|
1342
|
+
});
|
|
1343
|
+
let end = 0;
|
|
1344
|
+
row.titleMatch?.forEach(match => {
|
|
1345
|
+
if (!Number.isInteger(match.start) || !Number.isInteger(match.end) || match.start < end || match.end <= match.start || match.end > row.title.length) fail(`${path}.titleMatch`, 'invalid text range');
|
|
1346
|
+
end = match.end;
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
if (row.type === 'buttonGroup' && isDevelopment()) assertButtonGroup(row, path);
|
|
1350
|
+
if (row.type === 'sectionHeader' && isDevelopment()) assertTextHelp(row.valueHelp, `${path}.valueHelp`);
|
|
1351
|
+
if (row.type === 'mediaTile' && row.media) {
|
|
1352
|
+
protocolObject(row.media.source, `${path}.media.source`);
|
|
1353
|
+
if (!Array.isArray(row.media.probeOrder)) fail(`${path}.media.probeOrder`, 'must be an array');
|
|
1354
|
+
}
|
|
1355
|
+
if (row.type === 'walletGroup') {
|
|
1356
|
+
protocolObject(row.parent, `${path}.parent`);
|
|
1357
|
+
if (!Array.isArray(row.children)) fail(`${path}.children`, 'must be an array');
|
|
1358
|
+
const members = [row.parent, ...row.children];
|
|
1359
|
+
const keys = new Set();
|
|
1360
|
+
if (row.parent.key !== row.key) fail(`${path}.parent.key`, 'must match its group');
|
|
1361
|
+
members.forEach((member, index) => {
|
|
1362
|
+
assertRowProtocol(member, `${path}.members[${index}]`);
|
|
1363
|
+
if (member.type !== 'identity' || keys.has(member.key)) fail(path, 'invalid wallet member identity');
|
|
1364
|
+
keys.add(member.key);
|
|
1365
|
+
});
|
|
1366
|
+
}
|
|
1367
|
+
if (row.type === 'dataRow') {
|
|
1368
|
+
if (!Array.isArray(row.columns)) fail(`${path}.columns`, 'must be an array');
|
|
1369
|
+
row.columns.forEach((column, index) => {
|
|
1370
|
+
const columnPath = `${path}.columns[${index}]`;
|
|
1371
|
+
protocolObject(column, columnPath);
|
|
1372
|
+
if (isDevelopment()) assertIdentityExtras(column, columnPath);
|
|
1373
|
+
});
|
|
1374
|
+
if (isDevelopment()) assertTrailingAccessories(row.trailing, `${path}.trailing`);
|
|
1375
|
+
}
|
|
1376
|
+
if (row.type === 'market') {
|
|
1377
|
+
protocolObject(row.change, `${path}.change`);
|
|
1378
|
+
assertMarketMetrics(row.metrics, `${path}.metrics`);
|
|
1379
|
+
if (row.webTable) {
|
|
1380
|
+
const {
|
|
1381
|
+
columns,
|
|
1382
|
+
cells
|
|
1383
|
+
} = row.webTable;
|
|
1384
|
+
if (!Array.isArray(columns) || !Array.isArray(cells) || columns.length !== cells.length) fail(`${path}.webTable`, 'columns and cells must match');
|
|
1385
|
+
const keys = new Set();
|
|
1386
|
+
columns.forEach((column, index) => {
|
|
1387
|
+
protocolKey(column.key, `${path}.webTable.columns[${index}].key`);
|
|
1388
|
+
if (keys.has(column.key) || cells[index]?.key !== column.key) fail(`${path}.webTable`, 'invalid column identity');
|
|
1389
|
+
keys.add(column.key);
|
|
1390
|
+
const cell = cells[index];
|
|
1391
|
+
if (!['favorite', 'asset', 'text', 'sparkline'].includes(cell.kind)) fail(`${path}.webTable.cells[${index}]`, 'unsupported cell kind');
|
|
1392
|
+
if (cell.kind === 'asset') protocolObject(cell.title, `${path}.webTable.cells[${index}].title`);
|
|
1393
|
+
if ((cell.kind === 'asset' || cell.kind === 'text') && cell.secondary !== undefined && !Array.isArray(cell.secondary)) fail(`${path}.webTable.cells[${index}].secondary`, 'must be an array');
|
|
1394
|
+
if (cell.kind === 'text' && !Array.isArray(cell.primary)) fail(`${path}.webTable.cells[${index}].primary`, 'must be an array');
|
|
1395
|
+
// The Web renderer spreads samples into Math.min/Math.max.
|
|
1396
|
+
if (cell.kind === 'sparkline' && (!Array.isArray(cell.points) || cell.points.length > 256)) fail(`${path}.webTable.cells[${index}].points`, 'must be an array of at most 256 samples');
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
function assertSnapshotProtocol(snapshot) {
|
|
1402
|
+
protocolObject(snapshot, 'snapshot');
|
|
1403
|
+
if (snapshot.disclosure !== undefined) {
|
|
1404
|
+
protocolObject(snapshot.disclosure, 'snapshot.disclosure');
|
|
1405
|
+
if (typeof snapshot.disclosure.expanded !== 'boolean') fail('snapshot.disclosure.expanded', 'must be a boolean');
|
|
1406
|
+
const inset = snapshot.disclosure.expandedTrailingInset;
|
|
1407
|
+
if (inset !== undefined && (!Number.isFinite(inset) || inset < 0)) fail('snapshot.disclosure.expandedTrailingInset', 'must be finite and nonnegative');
|
|
1408
|
+
}
|
|
1409
|
+
if (snapshot.structuralTransition) {
|
|
1410
|
+
protocolObject(snapshot.structuralTransition, 'snapshot.structuralTransition');
|
|
1411
|
+
protocolKey(snapshot.structuralTransition.id, 'snapshot.structuralTransition.id');
|
|
1412
|
+
if (snapshot.structuralTransition.anchorKey !== undefined) protocolKey(snapshot.structuralTransition.anchorKey, 'snapshot.structuralTransition.anchorKey');
|
|
1413
|
+
const duration = snapshot.structuralTransition.durationMs;
|
|
1414
|
+
if (duration !== undefined && (!Number.isFinite(duration) || duration < 0 || duration > 2000)) fail('snapshot.structuralTransition.durationMs', 'must be finite within 0...2000');
|
|
1415
|
+
}
|
|
1416
|
+
if (snapshot.theme?.successBackground !== undefined && (typeof snapshot.theme.successBackground !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(snapshot.theme.successBackground))) fail('theme.successBackground', 'must be a hex color');
|
|
1417
|
+
if (snapshot.theme?.rowHoverBackground !== undefined && (typeof snapshot.theme.rowHoverBackground !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(snapshot.theme.rowHoverBackground))) fail('theme.rowHoverBackground', 'must be a hex color');
|
|
1418
|
+
if (snapshot.schemaVersion !== 1) fail('snapshot.schemaVersion', 'must be 1');
|
|
1419
|
+
if (!Number.isSafeInteger(snapshot.generation) || snapshot.generation < 0) fail('snapshot.generation', 'must be a non-negative safe integer');
|
|
1420
|
+
protocolObject(snapshot.layout, 'snapshot.layout');
|
|
1421
|
+
assertProtocolFields(snapshot.layout, 'snapshot.layout');
|
|
1422
|
+
if (snapshot.layout.orientation !== undefined && !['vertical', 'horizontal'].includes(snapshot.layout.orientation)) fail('snapshot.layout.orientation', 'is not supported');
|
|
1423
|
+
if (!['linear', 'sectioned', 'grid', 'table'].includes(snapshot.layout.kind)) fail('snapshot.layout.kind', 'is not supported');
|
|
1424
|
+
if (!Array.isArray(snapshot.rows)) fail('snapshot.rows', 'must be an array');
|
|
1425
|
+
if (snapshot.layout.kind === 'grid' && (!Number.isInteger(snapshot.layout.gridColumns) || (snapshot.layout.gridColumns ?? 0) < 1 || (snapshot.layout.gridColumns ?? 0) > 7)) fail('snapshot.layout.gridColumns', 'must be an integer from 1 to 7');
|
|
1426
|
+
const keys = new Set();
|
|
1427
|
+
for (const [index, row] of snapshot.rows.entries()) {
|
|
1428
|
+
assertRowProtocol(row, `rows[${index}]`);
|
|
1429
|
+
if (keys.has(row.key)) fail(`rows[${index}].key`, 'duplicate row identity');
|
|
1430
|
+
keys.add(row.key);
|
|
1431
|
+
}
|
|
1432
|
+
if (snapshot.emptyState) assertRowProtocol(snapshot.emptyState, 'emptyState');
|
|
1433
|
+
if (snapshot.fixedFooter) assertRowProtocol(snapshot.fixedFooter, 'fixedFooter');
|
|
1434
|
+
if (snapshot.selection) {
|
|
1435
|
+
if (!Array.isArray(snapshot.selection.selectedKeys)) fail('snapshot.selection.selectedKeys', 'must be an array');
|
|
1436
|
+
snapshot.selection.selectedKeys.forEach(key => {
|
|
1437
|
+
if (!keys.has(key)) fail('snapshot.selection.selectedKeys', 'unknown row identity');
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
function isRecord(value) {
|
|
1442
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
1443
|
+
}
|
|
1444
|
+
function isAllowedImageUri(value) {
|
|
1445
|
+
return typeof value === 'string' && value.trim().length > 0 && (!/^[a-z][a-z\d+.-]*:/i.test(value) || /^(https?|file|content|ph|assets-library|asset|res|blob|onekey-avatar):/i.test(value) || /^data:image\//i.test(value));
|
|
1446
|
+
}
|
|
1447
|
+
const IMAGE_FIELDS = ['uri', 'width', 'height', 'headers', 'contentFit', 'cachePolicy', 'autoplay', 'optimizeTos', 'overscan', 'loadingStrategy', 'fallbackUri', 'fallbackUris', 'retryTimes'];
|
|
1448
|
+
const ICON_FIELDS = ['name', 'tintColor', 'backgroundColor', 'size', 'accessibilityLabel', 'hoverActionKey'];
|
|
1449
|
+
const VISUAL_FIELDS = ['kind', 'name', 'image', 'images', 'networkImage', 'shape', 'tintColor', 'backgroundColor', 'fallbackIcon', 'fallbackText', 'fallbackTextColor', 'overlays', 'borderStyle', 'borderColor', 'cornerIcon'];
|
|
1450
|
+
const TEXT_STYLE_FIELDS = ['token', 'fontSize', 'fontWeight', 'color', 'lineHeight', 'lines', 'truncate', 'alignment', 'verticalAlignment', 'offsetY'];
|
|
1451
|
+
const ACCESSORY_FIELDS = {
|
|
1452
|
+
value: ['kind', 'text', 'secondary', 'textSegments'],
|
|
1453
|
+
valuePair: ['kind', 'primary', 'secondary', 'primaryFontWeight', 'primaryHelp', 'primaryTextSegments', 'secondaryTextSegments', 'primaryTone', 'secondaryTone'],
|
|
1454
|
+
button: ['kind', 'label', 'actionKey', 'disabled', 'testID', 'accessibilityLabel', 'textColor', 'backgroundColor', 'pressedBackgroundColor', 'hoverBackgroundColor', 'focusRingColor'],
|
|
1455
|
+
icon: ['kind', 'name', 'tintColor', 'buttonStyle', 'disabled', 'actionKey', 'testID', 'hoverActionKey', 'accessibilityLabel'],
|
|
1456
|
+
checkbox: ['kind', 'state', 'disabled', 'loading', 'target', 'actionKey'],
|
|
1457
|
+
radio: ['kind', 'checked', 'disabled', 'actionKey'],
|
|
1458
|
+
switch: ['kind', 'value', 'disabled', 'actionKey'],
|
|
1459
|
+
chevron: ['kind', 'actionKey'],
|
|
1460
|
+
menu: ['kind', 'actionKey'],
|
|
1461
|
+
drag: ['kind', 'actionKey'],
|
|
1462
|
+
spinner: ['kind'],
|
|
1463
|
+
progress: ['kind', 'value']
|
|
1464
|
+
};
|
|
1465
|
+
const REQUIRED_ROW_FIELDS = {
|
|
1466
|
+
identity: ['title', 'leading'],
|
|
1467
|
+
walletGroup: ['parent', 'children'],
|
|
1468
|
+
rail: ['title', 'visual'],
|
|
1469
|
+
activity: ['title', 'leading'],
|
|
1470
|
+
message: ['title', 'body', 'time'],
|
|
1471
|
+
dataRow: ['columns'],
|
|
1472
|
+
market: ['title', 'leading', 'variant', 'price', 'change'],
|
|
1473
|
+
mediaTile: ['title', 'variant'],
|
|
1474
|
+
metricCard: ['title', 'value'],
|
|
1475
|
+
sectionHeader: ['title', 'sectionKey'],
|
|
1476
|
+
action: ['title', 'actionKey'],
|
|
1477
|
+
buttonGroup: ['buttons'],
|
|
1478
|
+
system: ['variant']
|
|
1479
|
+
};
|
|
1480
|
+
function assertCoreRow(row, path) {
|
|
1481
|
+
const object = row;
|
|
1482
|
+
for (const key of REQUIRED_ROW_FIELDS[row.type]) {
|
|
1483
|
+
const value = object[key];
|
|
1484
|
+
if (value === undefined || value === null) fail(`${path}.${key}`, 'required field');
|
|
1485
|
+
if (['title', 'body', 'time', 'price', 'value', 'variant'].includes(key) && typeof value !== 'string') fail(`${path}.${key}`, 'must be a string');
|
|
1486
|
+
}
|
|
1487
|
+
if (row.type === 'market') {
|
|
1488
|
+
if (!row.price.trim()) fail(`${path}.price`, 'must be nonblank');
|
|
1489
|
+
if (typeof row.change?.text !== 'string') fail(`${path}.change.text`, 'must be a string');
|
|
1490
|
+
}
|
|
1491
|
+
if (row.type === 'activity') {
|
|
1492
|
+
row.amounts?.forEach((amount, index) => {
|
|
1493
|
+
protocolKey(amount.key, `${path}.amounts[${index}].key`);
|
|
1494
|
+
if (typeof amount.text !== 'string') fail(`${path}.amounts[${index}].text`, 'required financial text');
|
|
1495
|
+
});
|
|
1496
|
+
if (row.fee && typeof row.fee.primary !== 'string') fail(`${path}.fee.primary`, 'required financial text');
|
|
1497
|
+
row.footerActions?.forEach((action, index) => {
|
|
1498
|
+
protocolKey(action.key, `${path}.footerActions[${index}].key`);
|
|
1499
|
+
protocolKey(action.label, `${path}.footerActions[${index}].label`);
|
|
1500
|
+
});
|
|
1501
|
+
}
|
|
1502
|
+
if (row.type === 'action') protocolKey(row.actionKey, `${path}.actionKey`);
|
|
1503
|
+
if (row.type === 'sectionHeader') protocolKey(row.sectionKey, `${path}.sectionKey`);
|
|
1504
|
+
if (row.type === 'system' && ['retry', 'warning', 'noMatch'].includes(row.variant)) {
|
|
1505
|
+
if (typeof object.message !== 'string') fail(`${path}.message`, 'must be a string');
|
|
1506
|
+
if (row.variant === 'retry') protocolKey(row.actionKey, `${path}.actionKey`);
|
|
1507
|
+
}
|
|
1508
|
+
if (row.type === 'dataRow') {
|
|
1509
|
+
if (!Array.isArray(row.columns) || row.columns.length === 0) fail(`${path}.columns`, 'required columns');
|
|
1510
|
+
row.columns.forEach((column, index) => {
|
|
1511
|
+
protocolObject(column, `${path}.columns[${index}]`);
|
|
1512
|
+
protocolKey(column.key, `${path}.columns[${index}].key`);
|
|
1513
|
+
if (typeof column.text !== 'string') fail(`${path}.columns[${index}].text`, 'must be a string');
|
|
1514
|
+
});
|
|
1515
|
+
}
|
|
1516
|
+
if (row.type === 'buttonGroup') {
|
|
1517
|
+
if (!Array.isArray(row.buttons) || row.buttons.length === 0) fail(`${path}.buttons`, 'required buttons');
|
|
1518
|
+
const keys = new Set();
|
|
1519
|
+
row.buttons.forEach((button, index) => {
|
|
1520
|
+
const field = `${path}.buttons[${index}]`;
|
|
1521
|
+
protocolObject(button, field);
|
|
1522
|
+
protocolKey(button.key, `${field}.key`);
|
|
1523
|
+
protocolKey(button.actionKey, `${field}.actionKey`);
|
|
1524
|
+
protocolKey(button.label, `${field}.label`);
|
|
1525
|
+
if (keys.has(button.key)) fail(field, 'duplicate button identity');
|
|
1526
|
+
keys.add(button.key);
|
|
1527
|
+
});
|
|
1528
|
+
}
|
|
1529
|
+
for (const field of ['trailing', 'leadingAction', 'titleIcon', 'valueIcon']) {
|
|
1530
|
+
const value = object[field];
|
|
1531
|
+
if (value === undefined) continue;
|
|
1532
|
+
// Identity title icons are decorative descriptors, not trailing controls.
|
|
1533
|
+
if (field === 'titleIcon' && row.type !== 'sectionHeader') continue;
|
|
1534
|
+
const accessories = field === 'trailing' ? value : [value];
|
|
1535
|
+
if (!Array.isArray(accessories)) fail(`${path}.${field}`, 'must be an array');
|
|
1536
|
+
accessories.forEach((accessory, index) => {
|
|
1537
|
+
const entry = protocolObject(accessory, `${path}.${field}[${index}]`);
|
|
1538
|
+
for (const key of entry.kind === 'value' ? ['text'] : entry.kind === 'valuePair' ? ['primary', 'secondary'] : []) {
|
|
1539
|
+
if (typeof entry[key] !== 'string') fail(`${path}.${field}[${index}].${key}`, 'required financial text');
|
|
1540
|
+
}
|
|
1541
|
+
if (['button', 'menu', 'switch'].includes(entry.kind) || 'actionKey' in entry) {
|
|
1542
|
+
protocolKey(entry.actionKey, `${path}.${field}[${index}].actionKey`);
|
|
1543
|
+
if (entry.kind === 'button') protocolKey(entry.label, `${path}.${field}[${index}].label`);
|
|
1544
|
+
}
|
|
1545
|
+
if (entry.kind === 'icon') protocolKey(entry.name, `${path}.${field}[${index}].name`);
|
|
1546
|
+
});
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
function pickFields(object, fields) {
|
|
1550
|
+
return Object.fromEntries(Object.entries(object).filter(([key]) => fields.includes(key)));
|
|
1551
|
+
}
|
|
1552
|
+
function sanitizeImage(value) {
|
|
1553
|
+
if (!isRecord(value) || !isAllowedImageUri(value.uri) || typeof value.width !== 'number' || !Number.isFinite(value.width) || value.width <= 0 || typeof value.height !== 'number' || !Number.isFinite(value.height) || value.height <= 0) return undefined;
|
|
1554
|
+
const next = pickFields(value, IMAGE_FIELDS);
|
|
1555
|
+
next.width = Math.min(MAX_IMAGE_EDGE, value.width);
|
|
1556
|
+
next.height = Math.min(MAX_IMAGE_EDGE, value.height);
|
|
1557
|
+
if (next.headers !== undefined) {
|
|
1558
|
+
if (!isRecord(next.headers)) delete next.headers;else next.headers = Object.fromEntries(Object.entries(next.headers).filter(([key, item]) => key.trim() && key.length <= MAX_HEADER_LENGTH && typeof item === 'string' && item.length <= MAX_HEADER_LENGTH).slice(0, MAX_IMAGE_HEADERS));
|
|
1559
|
+
}
|
|
1560
|
+
if (next.fallbackUris !== undefined) {
|
|
1561
|
+
if (Array.isArray(next.fallbackUris)) next.fallbackUris = next.fallbackUris.filter(isAllowedImageUri);else delete next.fallbackUris;
|
|
1562
|
+
}
|
|
1563
|
+
if (next.fallbackUri !== undefined && !isAllowedImageUri(next.fallbackUri)) delete next.fallbackUri;
|
|
1564
|
+
if (next.overscan !== undefined && (typeof next.overscan !== 'number' || !Number.isFinite(next.overscan) || next.overscan < 1 || next.overscan > 4)) delete next.overscan;
|
|
1565
|
+
if (next.retryTimes !== undefined && (typeof next.retryTimes !== 'number' || !Number.isFinite(next.retryTimes) || next.retryTimes < 0)) delete next.retryTimes;
|
|
1566
|
+
const options = {
|
|
1567
|
+
contentFit: ['cover', 'contain', 'fill', 'center'],
|
|
1568
|
+
cachePolicy: ['memory-disk', 'memory', 'disk', 'none'],
|
|
1569
|
+
loadingStrategy: ['static', 'skeleton', 'none']
|
|
1570
|
+
};
|
|
1571
|
+
for (const [key, allowed] of Object.entries(options)) if (next[key] !== undefined && !allowed.includes(next[key])) delete next[key];
|
|
1572
|
+
for (const key of ['autoplay', 'optimizeTos']) if (next[key] !== undefined && typeof next[key] !== 'boolean') delete next[key];
|
|
1573
|
+
return next;
|
|
1574
|
+
}
|
|
1575
|
+
const NESTED_DECORATION_FIELDS = {
|
|
1576
|
+
webTable: ['columns', 'cells', 'testID', 'draggable', 'contextMenuActionKey'],
|
|
1577
|
+
columns: ['key', 'text', 'textSegments', 'titleIcon', 'secondaryTitleIcon', 'secondaryLeadingText', 'secondaryText', 'secondaryTextSegments', 'weight', 'alignment', 'tone', 'secondaryTone', 'width', 'minWidth', 'flex', 'paddingStart', 'paddingEnd'],
|
|
1578
|
+
cells: ['key', 'kind', 'active', 'disabled', 'testID', 'hoverActionKey', 'actionKey', 'accessibilityLabel', 'leading', 'title', 'secondary', 'lineGap', 'secondaryBadges', 'hoverImages', 'hoverSecondary', 'hoverActionLabel', 'badges', 'primary', 'partGap', 'points', 'width', 'height', 'strokeColor', 'fillColor'],
|
|
1579
|
+
badges: ['key', 'text', 'tone', 'iconName', 'icon', 'textColor', 'backgroundColor', 'actionKey', 'accessibilityLabel', 'style'],
|
|
1580
|
+
overlays: ['position', 'size', 'width', 'height', 'padding', 'offset', 'offsetX', 'offsetY', 'image', 'fallbackIcon', 'name', 'text', 'fontWeight', 'tintColor', 'backgroundColor'],
|
|
1581
|
+
primaryHelp: ['actionKey', 'presentation', 'underline'],
|
|
1582
|
+
valueHelp: ['actionKey', 'presentation', 'underline']
|
|
1583
|
+
};
|
|
1584
|
+
function sanitizeDecoration(value, field) {
|
|
1585
|
+
if (value === undefined || value === null) return undefined;
|
|
1586
|
+
if (!Array.isArray(value) && ['image', 'networkImage', 'thumbnail', 'source', 'images', 'hoverImages'].includes(field)) return sanitizeImage(value);
|
|
1587
|
+
if (['titleImage', 'titleIcon', 'secondaryTitleIcon', 'fallbackIcon', 'cornerIcon', 'media'].includes(field) && !isRecord(value)) return undefined;
|
|
1588
|
+
if (field === 'style' && typeof value !== 'string' && !isRecord(value)) return undefined;
|
|
1589
|
+
if (['badges', 'overlays'].includes(field) && !Array.isArray(value) && !isRecord(value)) return undefined;
|
|
1590
|
+
if (field === 'style' && typeof value === 'string') return value === 'subscript' ? value : undefined;
|
|
1591
|
+
if (Array.isArray(value)) {
|
|
1592
|
+
const items = value.map(item => sanitizeDecoration(item, field));
|
|
1593
|
+
return field === 'hoverImages' ? items.map(item => item ?? null) : items.filter(item => item !== undefined);
|
|
1594
|
+
}
|
|
1595
|
+
if (!isRecord(value)) return value;
|
|
1596
|
+
let object = value;
|
|
1597
|
+
if (field === 'titleImage') {
|
|
1598
|
+
const image = sanitizeImage(value.image);
|
|
1599
|
+
if (!image) return undefined;
|
|
1600
|
+
const edge = Math.min(40, Math.max(1, Math.round(Math.min(image.width, image.height))));
|
|
1601
|
+
object = {
|
|
1602
|
+
...pickFields(value, ['image', 'accessibilityLabel', 'hoverActionKey']),
|
|
1603
|
+
image: {
|
|
1604
|
+
...image,
|
|
1605
|
+
width: edge,
|
|
1606
|
+
height: edge
|
|
1607
|
+
}
|
|
1608
|
+
};
|
|
1609
|
+
} else if (['titleIcon', 'secondaryTitleIcon', 'fallbackIcon', 'cornerIcon'].includes(field) && value.kind === undefined) {
|
|
1610
|
+
if (typeof value.name !== 'string' || !value.name.trim()) return undefined;
|
|
1611
|
+
object = pickFields(value, ICON_FIELDS);
|
|
1612
|
+
} else if (['leading', 'visual', 'secondaryLeading'].includes(field)) {
|
|
1613
|
+
object = pickFields(value, VISUAL_FIELDS);
|
|
1614
|
+
} else if (field === 'media') {
|
|
1615
|
+
object = pickFields(value, ['source', 'probeOrder']);
|
|
1616
|
+
}
|
|
1617
|
+
if (field === 'trailing' || ['leadingAction', 'titleIcon', 'valueIcon'].includes(field) && object.kind !== undefined) {
|
|
1618
|
+
const fields = ACCESSORY_FIELDS[object.kind];
|
|
1619
|
+
if (fields) object = pickFields(object, fields);
|
|
1620
|
+
} else if (field === 'buttons') object = pickFields(object, ['key', 'label', 'actionKey', 'disabled', 'loading', 'testID', 'accessibilityLabel']);else if (field === 'container') object = pickFields(object, ['height', 'backgroundColor', 'opacity', 'cornerRadius', 'borderWidth', 'borderColor', 'contentVerticalAlignment']);else if (field === 'imageStyle') object = pickFields(object, ['width', 'height', 'shape', 'cornerRadius', 'contentFit']);else if (field === 'style') object = pickFields(object, [...TEXT_STYLE_FIELDS, 'container', ...new Set(Object.values(ROW_BOX_STYLE_KEYS_BY_TYPE).flat()), ...new Set(Object.values(TEXT_STYLE_KEYS_BY_ROW_TYPE).flat()), ...new Set(Object.values(EXTRA_STYLE_KEYS_BY_ROW_TYPE).flat()), 'height']);else if (['title', 'subtitle', 'tertiary', 'body', 'time', 'price', 'change', 'value', 'valueSecondary', 'columnSecondary', 'primaryAmount', 'secondaryAmount', 'description', 'status', 'trend', 'message', 'actionText'].includes(field) && !('text' in object)) {
|
|
1621
|
+
object = pickFields(object, TEXT_STYLE_FIELDS);
|
|
1622
|
+
}
|
|
1623
|
+
if (NESTED_DECORATION_FIELDS[field]) object = pickFields(object, NESTED_DECORATION_FIELDS[field]);
|
|
1624
|
+
if (['textSegments', 'primaryTextSegments', 'secondaryTextSegments', 'priceSegments', 'segments'].includes(field)) object = pickFields(object, ['text', 'style']);
|
|
1625
|
+
if (field === 'subtitleSegments') object = pickFields(object, ['text', 'style', 'textSegments', 'tone', 'separatorBefore']);
|
|
1626
|
+
const styleObject = ['style', 'imageStyle', 'container'].includes(field) || TEXT_STYLE_FIELDS.some(key => key in object);
|
|
1627
|
+
const next = {};
|
|
1628
|
+
for (const [key, item] of Object.entries(object)) {
|
|
1629
|
+
if (item === undefined || item === null) continue;
|
|
1630
|
+
if ((/color$/i.test(key) || /background$/i.test(key)) && (typeof item !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(item))) continue;
|
|
1631
|
+
if (/TextSegments$/.test(key) || ['textSegments', 'priceSegments', 'subtitleSegments', 'segments'].includes(key)) {
|
|
1632
|
+
if (!Array.isArray(item) || item.some(segment => !isRecord(segment) || typeof segment.text !== 'string')) continue;
|
|
1633
|
+
}
|
|
1634
|
+
if (styleObject && PROTOCOL_NUMBER_FIELDS.has(key) && typeof item !== 'number') continue;
|
|
1635
|
+
if (key === 'token' && (typeof item !== 'string' || !Object.hasOwn(TYPOGRAPHY_TOKENS, item))) continue;
|
|
1636
|
+
if (key === 'fontWeight' && !['regular', 'medium', 'semibold', 'bold'].includes(item)) continue;
|
|
1637
|
+
if (['disabled', 'loading', 'checked', 'selected', 'separator', 'hideLeading', 'pressDisabled', 'draggable', 'active', 'autoplay'].includes(key) && typeof item !== 'boolean') continue;
|
|
1638
|
+
if (key === 'primaryFontWeight' && item !== 'medium') continue;
|
|
1639
|
+
if (key === 'buttonStyle' && item !== 'tertiary') continue;
|
|
1640
|
+
if (key === 'hoverActionKey' && (typeof item !== 'string' || !item.trim())) continue;
|
|
1641
|
+
if (key === 'accessibilityLabel' && typeof item !== 'string') continue;
|
|
1642
|
+
if ((field === 'style' || PROTOCOL_NUMBER_FIELDS.has(key)) && typeof item !== 'object' && typeof item === 'number' && (!Number.isFinite(item) || item < 0 && !['offsetX', 'offsetY'].includes(key))) continue;
|
|
1643
|
+
if (field === 'style' && PROTOCOL_NUMBER_FIELDS.has(key) && typeof item !== 'number') continue;
|
|
1644
|
+
if (key === 'leadingContentMaxWidthRatio' && (typeof item !== 'number' || item <= 0 || item > 1)) continue;
|
|
1645
|
+
const normalized = sanitizeDecoration(item, field === 'style' && key === 'image' ? 'imageStyle' : key);
|
|
1646
|
+
if (normalized !== undefined) next[key] = normalized;
|
|
1647
|
+
}
|
|
1648
|
+
if (field === 'titleImage') {
|
|
1649
|
+
// The nested image was already normalized with title-specific geometry.
|
|
1650
|
+
if (!next.image) return undefined;
|
|
1651
|
+
}
|
|
1652
|
+
if (field === 'media' && (!next.source || !Array.isArray(next.probeOrder))) return undefined;
|
|
1653
|
+
if (['leading', 'visual', 'secondaryLeading'].includes(field)) {
|
|
1654
|
+
if (next.kind === 'image' && !next.image) return {
|
|
1655
|
+
kind: 'token',
|
|
1656
|
+
...pickFields(next, ['fallbackIcon', 'fallbackText', 'fallbackTextColor'])
|
|
1657
|
+
};
|
|
1658
|
+
if (next.kind === 'stackedImages' && (!Array.isArray(next.images) || next.images.length < 2)) return {
|
|
1659
|
+
kind: 'token'
|
|
1660
|
+
};
|
|
1661
|
+
if (next.kind === 'icon' && (typeof next.name !== 'string' || !next.name.trim())) return {
|
|
1662
|
+
kind: 'token'
|
|
1663
|
+
};
|
|
1664
|
+
if (!['token', 'account', 'wallet', 'network', 'image', 'icon', 'stackedImages'].includes(next.kind)) return {
|
|
1665
|
+
kind: 'token'
|
|
1666
|
+
};
|
|
1667
|
+
}
|
|
1668
|
+
if (field === 'overlays' && (!['topLeft', 'bottomRight'].includes(next.position) || !next.image && !next.name && !next.text)) return undefined;
|
|
1669
|
+
if (field === 'trailing' && next.kind === 'icon' && !('actionKey' in value) && (typeof next.name !== 'string' || !next.name.trim())) return undefined;
|
|
1670
|
+
if (field === 'trailing' && !['value', 'valuePair', 'button', 'checkbox', 'radio', 'switch', 'chevron', 'menu', 'drag', 'icon', 'spinner', 'progress'].includes(next.kind) && !('actionKey' in value)) return undefined;
|
|
1671
|
+
if (field === 'badges' && (typeof next.key !== 'string' || !next.text && !next.iconName && !next.icon)) return undefined;
|
|
1672
|
+
if (next.kind === 'sparkline') {
|
|
1673
|
+
const points = Array.isArray(value.points) ? value.points.filter(point => typeof point === 'number' && Number.isFinite(point)).slice(0, 256) : [];
|
|
1674
|
+
// Preserve the table's column identity even when its optional drawing fails.
|
|
1675
|
+
return points.length < 2 ? {
|
|
1676
|
+
key: next.key,
|
|
1677
|
+
kind: 'text',
|
|
1678
|
+
primary: []
|
|
1679
|
+
} : {
|
|
1680
|
+
...next,
|
|
1681
|
+
points
|
|
1682
|
+
};
|
|
1683
|
+
}
|
|
1684
|
+
return next;
|
|
1685
|
+
}
|
|
1686
|
+
function sanitizeMarketMetrics(value) {
|
|
1687
|
+
if (!Array.isArray(value)) return value;
|
|
1688
|
+
return value.map(metric => {
|
|
1689
|
+
const next = pickFields(metric, MARKET_METRIC_FIELDS);
|
|
1690
|
+
for (const field of ['textSegments', 'secondaryTextSegments']) {
|
|
1691
|
+
if (next[field] !== undefined) next[field] = sanitizeDecoration(next[field], field);
|
|
1692
|
+
}
|
|
1693
|
+
return next;
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
function sanitizeRowForProduction(row) {
|
|
1697
|
+
const object = protocolObject(row, 'row');
|
|
1698
|
+
const fields = ROW_FIELDS[row.type];
|
|
1699
|
+
if (!fields) return row;
|
|
1700
|
+
const next = pickFields(object, fields);
|
|
1701
|
+
for (const [field, value] of Object.entries(next)) {
|
|
1702
|
+
if ((/color$/i.test(field) || /background$/i.test(field)) && (typeof value !== 'string' || !/^#[0-9a-f]{6}([0-9a-f]{2})?$/i.test(value))) {
|
|
1703
|
+
delete next[field];
|
|
1704
|
+
continue;
|
|
1705
|
+
}
|
|
1706
|
+
if (field === 'parent' && isRecord(value)) next[field] = sanitizeRowForProduction(value);else if (field === 'children' && Array.isArray(value)) next[field] = value.map(child => sanitizeRowForProduction(child));else {
|
|
1707
|
+
const normalized = row.type === 'market' && field === 'metrics' ? sanitizeMarketMetrics(value) : sanitizeDecoration(value, field);
|
|
1708
|
+
if (normalized === undefined) delete next[field];else next[field] = normalized;
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
if (isRecord(next.style)) next.style = pickFields(next.style, ['container', ...ROW_BOX_STYLE_KEYS_BY_TYPE[row.type], ...(TEXT_STYLE_KEYS_BY_ROW_TYPE[row.type] ?? []), ...(EXTRA_STYLE_KEYS_BY_ROW_TYPE[row.type] ?? [])]);
|
|
1712
|
+
return sameJsonValue(row, next) ? row : next;
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
/** Empty images and optional nulls share one meaning before strict assertions. */
|
|
1716
|
+
function normalizeEmptyImages(value, field = '') {
|
|
1717
|
+
if (value === null || value === undefined || typeof value !== 'object') return value;
|
|
1718
|
+
if (Array.isArray(value)) {
|
|
1719
|
+
const next = value.map(item => normalizeEmptyImages(item, field));
|
|
1720
|
+
return next.every((item, index) => item === value[index]) ? value : next.filter(item => item !== undefined);
|
|
1721
|
+
}
|
|
1722
|
+
const object = value;
|
|
1723
|
+
if (typeof object.uri === 'string' && !object.uri.trim() && 'width' in object && 'height' in object) return undefined;
|
|
1724
|
+
let changed = false;
|
|
1725
|
+
const next = {};
|
|
1726
|
+
for (const [key, item] of Object.entries(object)) {
|
|
1727
|
+
const optional = object.type && !['key', 'type', ...(REQUIRED_ROW_FIELDS[object.type] ?? [])].includes(key);
|
|
1728
|
+
if (item === undefined || item === null && optional && !(object.type === 'market' && key === 'metrics') || ['priceSegments', 'subtitleSegments'].includes(key) && Array.isArray(item) && item.length === 0) {
|
|
1729
|
+
changed = true;
|
|
1730
|
+
continue;
|
|
1731
|
+
}
|
|
1732
|
+
const normalized = field === 'headers' ? item : normalizeEmptyImages(item, key);
|
|
1733
|
+
if (normalized !== undefined) next[key] = normalized;
|
|
1734
|
+
if (normalized !== item) changed = true;
|
|
1735
|
+
}
|
|
1736
|
+
if (field === 'media' && object.source !== undefined && next.source === undefined || field === 'titleImage' && object.image !== undefined && next.image === undefined) return undefined;
|
|
1737
|
+
if (['leading', 'visual', 'secondaryLeading'].includes(field) && next.kind === 'image' && !next.image) return {
|
|
1738
|
+
kind: 'token'
|
|
1739
|
+
};
|
|
1740
|
+
if (field === 'overlays' && object.image !== undefined && !next.image) return undefined;
|
|
1741
|
+
return changed ? next : value;
|
|
1742
|
+
}
|
|
1743
|
+
function normalizeSnapshotForProduction(snapshot) {
|
|
1744
|
+
if (!Array.isArray(snapshot.rows)) return snapshot;
|
|
1745
|
+
let rows = snapshot.rows.map(sanitizeRowForProduction);
|
|
1746
|
+
rows = rows.map((row, index) => {
|
|
1747
|
+
if (!row.groupId) return row;
|
|
1748
|
+
const previous = rows[index - 1]?.groupId === row.groupId;
|
|
1749
|
+
const following = rows[index + 1]?.groupId === row.groupId;
|
|
1750
|
+
return {
|
|
1751
|
+
...row,
|
|
1752
|
+
groupPosition: previous ? following ? 'middle' : 'last' : following ? 'first' : 'single'
|
|
1753
|
+
};
|
|
1754
|
+
});
|
|
1755
|
+
const next = {
|
|
1756
|
+
...snapshot,
|
|
1757
|
+
rows,
|
|
1758
|
+
...(snapshot.emptyState ? {
|
|
1759
|
+
emptyState: sanitizeRowForProduction(snapshot.emptyState)
|
|
1760
|
+
} : {}),
|
|
1761
|
+
...(snapshot.fixedFooter ? {
|
|
1762
|
+
fixedFooter: sanitizeRowForProduction(snapshot.fixedFooter)
|
|
1763
|
+
} : {})
|
|
1764
|
+
};
|
|
1765
|
+
if (snapshot.theme) next.theme = sanitizeDecoration(snapshot.theme, 'theme');
|
|
1766
|
+
if (snapshot.listStyle) next.listStyle = sanitizeDecoration(snapshot.listStyle, 'style');
|
|
1767
|
+
if (snapshot.selection && Array.isArray(snapshot.selection.selectedKeys)) {
|
|
1768
|
+
const selectable = new Set(rows.filter(row => !row.disabled && !['sectionHeader', 'action', 'system', 'buttonGroup', 'market', 'walletGroup'].includes(row.type)).map(row => row.key));
|
|
1769
|
+
const selectedKeys = [...new Set(snapshot.selection.selectedKeys.filter(key => selectable.has(key)))];
|
|
1770
|
+
next.selection = {
|
|
1771
|
+
...snapshot.selection,
|
|
1772
|
+
selectedKeys: snapshot.selection.mode === 'none' ? [] : snapshot.selection.mode === 'single' ? selectedKeys.slice(0, 1) : selectedKeys
|
|
1773
|
+
};
|
|
1774
|
+
}
|
|
1775
|
+
return sameJsonValue(snapshot, next) ? snapshot : next;
|
|
1776
|
+
}
|
|
1777
|
+
export function validateSnapshot(snapshot) {
|
|
1778
|
+
assertPlainSerializable(snapshot, 'snapshot', new Set(), false);
|
|
1779
|
+
protocolObject(snapshot, 'snapshot');
|
|
1780
|
+
const normalized = normalizeEmptyImages(snapshot);
|
|
1781
|
+
if (Array.isArray(normalized.rows)) {
|
|
1782
|
+
normalized.rows.forEach((row, index) => {
|
|
1783
|
+
if (row.type === 'market') assertMarketMetrics(row.metrics, `snapshot.rows[${index}].metrics`);
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
const canonical = isDevelopment() ? normalized : normalizeSnapshotForProduction(normalized);
|
|
1787
|
+
assertPlainSerializable(canonical, 'snapshot', new Set());
|
|
1788
|
+
if (isDevelopment()) assertSnapshotContract(canonical);
|
|
1789
|
+
assertSnapshotProtocol(canonical);
|
|
1790
|
+
return normalizeSnapshotStyles(canonical);
|
|
1791
|
+
}
|
|
1792
|
+
const ownedNativeSnapshots = new WeakMap();
|
|
1793
|
+
function capturePlainData(value, seen = new Map()) {
|
|
1794
|
+
if (value === null || typeof value !== 'object') return value;
|
|
1795
|
+
const previous = seen.get(value);
|
|
1796
|
+
if (previous) return previous;
|
|
1797
|
+
const copy = Array.isArray(value) ? new Array(value.length) : Object.create(Object.getPrototypeOf(value));
|
|
1798
|
+
seen.set(value, copy);
|
|
1799
|
+
const captureKey = key => {
|
|
1800
|
+
Object.defineProperty(copy, key, {
|
|
1801
|
+
value: capturePlainData(value[key], seen),
|
|
1802
|
+
enumerable: true,
|
|
1803
|
+
writable: true,
|
|
1804
|
+
configurable: true
|
|
1805
|
+
});
|
|
1806
|
+
};
|
|
1807
|
+
if (Array.isArray(value)) {
|
|
1808
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
1809
|
+
if (index in value) captureKey(String(index));
|
|
1810
|
+
}
|
|
1811
|
+
Object.keys(value).forEach(key => {
|
|
1812
|
+
const index = Number(key);
|
|
1813
|
+
if (Number.isInteger(index) && index >= 0 && index < value.length && String(index) === key) return;
|
|
1814
|
+
captureKey(key);
|
|
1815
|
+
});
|
|
1816
|
+
} else {
|
|
1817
|
+
Object.keys(value).forEach(captureKey);
|
|
1818
|
+
}
|
|
1819
|
+
return copy;
|
|
1820
|
+
}
|
|
1821
|
+
function deepFreeze(value, seen = new WeakSet()) {
|
|
1822
|
+
if (value === null || typeof value !== 'object' || seen.has(value)) return value;
|
|
1823
|
+
seen.add(value);
|
|
1824
|
+
Object.values(value).forEach(item => deepFreeze(item, seen));
|
|
1825
|
+
return Object.freeze(value);
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
/** Capture caller data once before validation, including accessor values. */
|
|
1829
|
+
export function captureNativeListInput(value) {
|
|
1830
|
+
return capturePlainData(value);
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
/** Private native-wrapper ownership; exported validators keep their public identity. */
|
|
1834
|
+
export function ownValidatedNativeSnapshot(snapshot) {
|
|
1835
|
+
const owned = deepFreeze(snapshot);
|
|
1836
|
+
ownedNativeSnapshots.set(owned, new Map(owned.rows.map((row, index) => [row.key, index])));
|
|
1837
|
+
return owned;
|
|
874
1838
|
}
|
|
875
1839
|
|
|
876
1840
|
/**
|
|
@@ -892,8 +1856,12 @@ function normalizeSnapshotStyles(snapshot) {
|
|
|
892
1856
|
return {
|
|
893
1857
|
...snapshot,
|
|
894
1858
|
rows,
|
|
895
|
-
emptyState
|
|
896
|
-
|
|
1859
|
+
...(emptyState !== undefined ? {
|
|
1860
|
+
emptyState
|
|
1861
|
+
} : {}),
|
|
1862
|
+
...(fixedFooter !== undefined ? {
|
|
1863
|
+
fixedFooter
|
|
1864
|
+
} : {})
|
|
897
1865
|
};
|
|
898
1866
|
}
|
|
899
1867
|
function assertPatchChanges(patch, index) {
|
|
@@ -916,7 +1884,23 @@ function assertPatchChanges(patch, index) {
|
|
|
916
1884
|
}
|
|
917
1885
|
switch (patch.type) {
|
|
918
1886
|
case 'identity':
|
|
1887
|
+
if (patch.changes.contentHeight !== undefined && patch.changes.contentHeight !== 'auto') {
|
|
1888
|
+
fail(`${path}.contentHeight`, 'must be auto');
|
|
1889
|
+
}
|
|
1890
|
+
if (patch.changes.subtitleLines !== undefined && ![0, 1, 2].includes(patch.changes.subtitleLines)) {
|
|
1891
|
+
fail(`${path}.subtitleLines`, 'must be 0, 1, or 2');
|
|
1892
|
+
}
|
|
1893
|
+
if (patch.changes.longPressActionKey !== undefined) {
|
|
1894
|
+
assertKey(patch.changes.longPressActionKey, `${path}.longPressActionKey`);
|
|
1895
|
+
}
|
|
919
1896
|
assertText(patch.changes.title, `${path}.title`);
|
|
1897
|
+
if ((patch.changes.titleSuffixes?.length ?? 0) > 3) fail(`${path}.titleSuffixes`, 'supports at most 3 suffixes');
|
|
1898
|
+
patch.changes.titleSuffixes?.forEach((suffix, index) => {
|
|
1899
|
+
assertText(suffix.text, `${path}.titleSuffixes[${index}].text`);
|
|
1900
|
+
if (!['secondary', 'disabled'].includes(suffix.tone)) fail(`${path}.titleSuffixes[${index}].tone`, 'must be secondary or disabled');
|
|
1901
|
+
if (suffix.maxWidth !== undefined && (!Number.isFinite(suffix.maxWidth) || suffix.maxWidth < 0 || suffix.maxWidth > 4096)) fail(`${path}.titleSuffixes[${index}].maxWidth`, 'must be finite within 0...4096');
|
|
1902
|
+
});
|
|
1903
|
+
assertIdentityExtras(patch.changes, path);
|
|
920
1904
|
assertText(patch.changes.subtitle, `${path}.subtitle`);
|
|
921
1905
|
assertText(patch.changes.tertiary, `${path}.tertiary`);
|
|
922
1906
|
if (patch.changes.tertiaryTone !== undefined && !['secondary', 'info'].includes(patch.changes.tertiaryTone)) {
|
|
@@ -950,11 +1934,14 @@ function assertPatchChanges(patch, index) {
|
|
|
950
1934
|
}
|
|
951
1935
|
break;
|
|
952
1936
|
case 'message':
|
|
1937
|
+
if (patch.changes.presentation !== undefined && patch.changes.presentation !== 'inlineWarning') {
|
|
1938
|
+
fail(`${path}.presentation`, 'must be inlineWarning');
|
|
1939
|
+
}
|
|
953
1940
|
assertText(patch.changes.title, `${path}.title`);
|
|
954
1941
|
assertText(patch.changes.body, `${path}.body`);
|
|
955
1942
|
assertText(patch.changes.time, `${path}.time`);
|
|
956
|
-
if (patch.changes.bodyLines !== undefined && ![1, 2, 3].includes(patch.changes.bodyLines)) {
|
|
957
|
-
fail(`${path}.bodyLines`, 'must be 1, 2, or 3');
|
|
1943
|
+
if (patch.changes.bodyLines !== undefined && ![0, 1, 2, 3].includes(patch.changes.bodyLines)) {
|
|
1944
|
+
fail(`${path}.bodyLines`, 'must be 0, 1, 2, or 3');
|
|
958
1945
|
}
|
|
959
1946
|
assertImage(patch.changes.thumbnail, `${path}.thumbnail`);
|
|
960
1947
|
break;
|
|
@@ -965,12 +1952,23 @@ function assertPatchChanges(patch, index) {
|
|
|
965
1952
|
patch.changes.columns?.forEach((column, columnIndex) => {
|
|
966
1953
|
assertKey(column.key, `${path}.columns[${columnIndex}].key`);
|
|
967
1954
|
assertText(column.text, `${path}.columns[${columnIndex}].text`);
|
|
1955
|
+
assertIdentityExtras(column, `${path}.columns[${columnIndex}]`);
|
|
1956
|
+
for (const field of ['textSegments', 'secondaryTextSegments']) {
|
|
1957
|
+
column[field]?.forEach((segment, segmentIndex) => {
|
|
1958
|
+
assertText(segment.text, `${path}.columns[${columnIndex}].${field}[${segmentIndex}].text`);
|
|
1959
|
+
if (segment.style !== undefined && segment.style !== 'subscript') fail(`${path}.columns[${columnIndex}].${field}[${segmentIndex}].style`, 'must be subscript');
|
|
1960
|
+
});
|
|
1961
|
+
}
|
|
968
1962
|
assertText(column.secondaryText, `${path}.columns[${columnIndex}].secondaryText`);
|
|
969
1963
|
assertTextTone(column.secondaryTone, `${path}.columns[${columnIndex}].secondaryTone`);
|
|
970
1964
|
});
|
|
971
1965
|
if ((patch.changes.badges?.length ?? 0) > MAX_BADGES) {
|
|
972
1966
|
fail(`${path}.badges`, `supports at most ${MAX_BADGES} badges`);
|
|
973
1967
|
}
|
|
1968
|
+
if ((patch.changes.trailing?.length ?? 0) > MAX_TRAILING_ACCESSORIES) {
|
|
1969
|
+
fail(`${path}.trailing`, `supports at most ${MAX_TRAILING_ACCESSORIES} accessories`);
|
|
1970
|
+
}
|
|
1971
|
+
assertTrailingAccessories(patch.changes.trailing, `${path}.trailing`);
|
|
974
1972
|
break;
|
|
975
1973
|
case 'market':
|
|
976
1974
|
assertMarketRow({
|
|
@@ -1029,6 +2027,11 @@ function assertPatchChanges(patch, index) {
|
|
|
1029
2027
|
assertText(patch.changes.title, `${path}.title`);
|
|
1030
2028
|
assertText(patch.changes.subtitle, `${path}.subtitle`);
|
|
1031
2029
|
assertText(patch.changes.value, `${path}.value`);
|
|
2030
|
+
if (patch.changes.leadingBadge) {
|
|
2031
|
+
assertKey(patch.changes.leadingBadge.key, `${path}.leadingBadge.key`);
|
|
2032
|
+
assertText(patch.changes.leadingBadge.text, `${path}.leadingBadge.text`);
|
|
2033
|
+
}
|
|
2034
|
+
assertTextHelp(patch.changes.valueHelp, `${path}.valueHelp`);
|
|
1032
2035
|
if (patch.changes.valueActionKey !== undefined) {
|
|
1033
2036
|
assertKey(patch.changes.valueActionKey, `${path}.valueActionKey`);
|
|
1034
2037
|
}
|
|
@@ -1042,6 +2045,11 @@ function assertPatchChanges(patch, index) {
|
|
|
1042
2045
|
}
|
|
1043
2046
|
assertTrailingAccessories(patch.changes.trailing, `${path}.trailing`);
|
|
1044
2047
|
break;
|
|
2048
|
+
case 'buttonGroup':
|
|
2049
|
+
if (patch.changes.buttons !== undefined) assertButtonGroup({
|
|
2050
|
+
buttons: patch.changes.buttons
|
|
2051
|
+
}, path);
|
|
2052
|
+
break;
|
|
1045
2053
|
case 'system':
|
|
1046
2054
|
assertText(patch.changes.message, `${path}.message`);
|
|
1047
2055
|
break;
|
|
@@ -1049,19 +2057,119 @@ function assertPatchChanges(patch, index) {
|
|
|
1049
2057
|
fail(`patches[${index}].type`, 'is not supported');
|
|
1050
2058
|
}
|
|
1051
2059
|
}
|
|
2060
|
+
function normalizePatch(patch, index) {
|
|
2061
|
+
const path = `patches[${index}]`;
|
|
2062
|
+
protocolObject(patch, path);
|
|
2063
|
+
protocolKey(patch.key, `${path}.key`);
|
|
2064
|
+
if (isDevelopment()) assertKey(patch.key, `${path}.key`);
|
|
2065
|
+
assertRowType(patch.type, path);
|
|
2066
|
+
const raw = protocolObject(patch.changes, `${path}.changes`);
|
|
2067
|
+
assertPlainSerializable(raw, `${path}.changes`, new Set(), false);
|
|
2068
|
+
const fields = PATCH_FIELDS[patch.type];
|
|
2069
|
+
const changes = {};
|
|
2070
|
+
for (const [field, value] of Object.entries(raw)) {
|
|
2071
|
+
if (value === undefined) continue;
|
|
2072
|
+
if (!fields?.includes(field)) {
|
|
2073
|
+
if (isDevelopment()) fail(`${path}.changes.${field}`, 'unsupported patch field');
|
|
2074
|
+
continue;
|
|
2075
|
+
}
|
|
2076
|
+
if (value === null) {
|
|
2077
|
+
if (patch.type === 'market' && field === 'metrics') fail(`${path}.changes.metrics`, 'use a full snapshot to remove metrics');
|
|
2078
|
+
if (REQUIRED_ROW_FIELDS[patch.type].includes(field) || patch.type === 'system' && field === 'message') {
|
|
2079
|
+
// The concrete system variant is checked after merging with its baseline.
|
|
2080
|
+
if (patch.type !== 'system') fail(`${path}.changes.${field}`, 'required field cannot be cleared');
|
|
2081
|
+
}
|
|
2082
|
+
changes[field] = null;
|
|
2083
|
+
continue;
|
|
2084
|
+
}
|
|
2085
|
+
if (patch.type === 'market' && field === 'metrics') assertMarketMetrics(value, `${path}.changes.metrics`);
|
|
2086
|
+
if (['priceSegments', 'subtitleSegments'].includes(field) && Array.isArray(value) && value.length === 0) {
|
|
2087
|
+
changes[field] = null;
|
|
2088
|
+
continue;
|
|
2089
|
+
}
|
|
2090
|
+
const normalized = normalizeEmptyImages(value, field);
|
|
2091
|
+
if (normalized === undefined) {
|
|
2092
|
+
if (!REQUIRED_ROW_FIELDS[patch.type].includes(field)) changes[field] = null;
|
|
2093
|
+
continue;
|
|
2094
|
+
}
|
|
2095
|
+
const canonical = isDevelopment() ? normalized : patch.type === 'market' && field === 'metrics' ? sanitizeMarketMetrics(normalized) : sanitizeDecoration(normalized, field);
|
|
2096
|
+
if (canonical !== undefined) changes[field] = canonical;else if (field === 'titleImage') changes[field] = null;
|
|
2097
|
+
}
|
|
2098
|
+
if (patch.type === 'market' && Array.isArray(changes.priceSegments) && changes.priceSegments.length > 0 && (typeof changes.price !== 'string' || !changes.price.trim())) fail(`${path}.changes.price`, 'is required when priceSegments is updated');
|
|
2099
|
+
if (!isDevelopment()) {
|
|
2100
|
+
const sanitized = sanitizeRowForProduction({
|
|
2101
|
+
type: patch.type,
|
|
2102
|
+
...changes
|
|
2103
|
+
});
|
|
2104
|
+
for (const [field, value] of Object.entries(changes)) {
|
|
2105
|
+
if (value === null) continue;
|
|
2106
|
+
if (sanitized[field] === undefined) delete changes[field];else changes[field] = sanitized[field];
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
const values = Object.fromEntries(Object.entries(changes).filter(([, value]) => value !== null));
|
|
2110
|
+
if (patch.type === 'market' && 'metrics' in changes) assertMarketMetrics(changes.metrics, `${path}.changes.metrics`);
|
|
2111
|
+
assertPlainSerializable(values, `${path}.changes`, new Set());
|
|
2112
|
+
assertProtocolFields(values, `${path}.changes`);
|
|
2113
|
+
if (isDevelopment()) assertPatchChanges({
|
|
2114
|
+
...patch,
|
|
2115
|
+
changes: values
|
|
2116
|
+
}, index);
|
|
2117
|
+
const normalizedPatch = {
|
|
2118
|
+
type: patch.type,
|
|
2119
|
+
key: patch.key,
|
|
2120
|
+
changes
|
|
2121
|
+
};
|
|
2122
|
+
return Object.keys(changes).length > 0 ? normalizePatchStyles([normalizedPatch])[0] : undefined;
|
|
2123
|
+
}
|
|
2124
|
+
function reportRejectedPatch(index) {
|
|
2125
|
+
console.warn(`NativeList rejected invalid patch unit at patches[${index}]`);
|
|
2126
|
+
}
|
|
1052
2127
|
export function validatePatches(patches) {
|
|
1053
|
-
|
|
1054
|
-
const
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
if (
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
2128
|
+
if (!Array.isArray(patches)) fail('patches', 'must be an array');
|
|
2129
|
+
const units = new Map();
|
|
2130
|
+
const rejectedKeys = new Set();
|
|
2131
|
+
const reject = (key, index, error) => {
|
|
2132
|
+
if (isDevelopment()) throw error;
|
|
2133
|
+
rejectedKeys.add(key);
|
|
2134
|
+
reportRejectedPatch(index);
|
|
2135
|
+
};
|
|
2136
|
+
patches.forEach((input, index) => {
|
|
2137
|
+
const key = isRecord(input) && typeof input.key === 'string' ? input.key : '';
|
|
2138
|
+
try {
|
|
2139
|
+
protocolObject(input, `patches[${index}]`);
|
|
2140
|
+
protocolKey(input.key, `patches[${index}].key`);
|
|
2141
|
+
assertRowType(input.type, `patches[${index}]`);
|
|
2142
|
+
const changes = capturePlainData(protocolObject(input.changes, `patches[${index}].changes`));
|
|
2143
|
+
assertPlainSerializable(changes, `patches[${index}].changes`, new Set(), false);
|
|
2144
|
+
const previous = units.get(key);
|
|
2145
|
+
if (previous && previous.patch.type !== input.type) fail(`patches[${index}].type`, 'conflicting row types');
|
|
2146
|
+
const defined = Object.fromEntries(Object.entries(changes).filter(([, value]) => value !== undefined));
|
|
2147
|
+
units.set(key, {
|
|
2148
|
+
index,
|
|
2149
|
+
patch: {
|
|
2150
|
+
key,
|
|
2151
|
+
type: input.type,
|
|
2152
|
+
changes: {
|
|
2153
|
+
...previous?.patch.changes,
|
|
2154
|
+
...defined
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
});
|
|
2158
|
+
} catch (error) {
|
|
2159
|
+
reject(key, index, error);
|
|
1061
2160
|
}
|
|
1062
|
-
assertPatchChanges(patch, index);
|
|
1063
2161
|
});
|
|
1064
|
-
|
|
2162
|
+
const accepted = [];
|
|
2163
|
+
for (const [key, unit] of units) {
|
|
2164
|
+
if (rejectedKeys.has(key)) continue;
|
|
2165
|
+
try {
|
|
2166
|
+
const patch = normalizePatch(unit.patch, unit.index);
|
|
2167
|
+
if (patch) accepted.push(patch);
|
|
2168
|
+
} catch (error) {
|
|
2169
|
+
reject(key, unit.index, error);
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2172
|
+
return accepted;
|
|
1065
2173
|
}
|
|
1066
2174
|
|
|
1067
2175
|
/** Patches reach the native side without passing through a snapshot. */
|
|
@@ -1083,30 +2191,227 @@ function normalizePatchStyles(patches) {
|
|
|
1083
2191
|
});
|
|
1084
2192
|
return changed ? next : patches;
|
|
1085
2193
|
}
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
2194
|
+
const EMPTY_ROW_PATCHES = Object.freeze([]);
|
|
2195
|
+
function sameJsonValue(left, right) {
|
|
2196
|
+
if (left === right) return true;
|
|
2197
|
+
if (left === null || right === null) return false;
|
|
2198
|
+
if (typeof left !== 'object' || typeof right !== 'object') return false;
|
|
2199
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
2200
|
+
return Array.isArray(left) && Array.isArray(right) && left.length === right.length && left.every((value, index) => sameJsonValue(value, right[index]));
|
|
2201
|
+
}
|
|
2202
|
+
const leftObject = left;
|
|
2203
|
+
const rightObject = right;
|
|
2204
|
+
const leftKeys = Object.keys(leftObject).filter(key => leftObject[key] !== undefined);
|
|
2205
|
+
const rightKeys = Object.keys(rightObject).filter(key => rightObject[key] !== undefined);
|
|
2206
|
+
return leftKeys.length === rightKeys.length && leftKeys.every(key => sameJsonValue(leftObject[key], rightObject[key]));
|
|
2207
|
+
}
|
|
2208
|
+
function sameOptionalPresence(left, right) {
|
|
2209
|
+
return left === undefined === (right === undefined);
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
/** Plan value-only patches; callers own valid immutable snapshots and commit state. */
|
|
2213
|
+
export function planNativeListPatches(previous, next) {
|
|
2214
|
+
if (previous === next) return EMPTY_ROW_PATCHES;
|
|
2215
|
+
const {
|
|
2216
|
+
rows: previousRows,
|
|
2217
|
+
...previousMetadata
|
|
2218
|
+
} = previous;
|
|
2219
|
+
const {
|
|
2220
|
+
rows: nextRows,
|
|
2221
|
+
...nextMetadata
|
|
2222
|
+
} = next;
|
|
2223
|
+
if (previousRows.length !== nextRows.length || !sameJsonValue(previousMetadata, nextMetadata)) return undefined;
|
|
2224
|
+
if (previousRows === nextRows) return EMPTY_ROW_PATCHES;
|
|
2225
|
+
const patches = [];
|
|
2226
|
+
for (let index = 0; index < nextRows.length; index += 1) {
|
|
2227
|
+
const oldRow = previousRows[index];
|
|
2228
|
+
const row = nextRows[index];
|
|
2229
|
+
if (oldRow.key !== row.key || oldRow.type !== row.type) return undefined;
|
|
2230
|
+
if (oldRow === row) continue;
|
|
2231
|
+
if (oldRow.type === 'identity' && row.type === 'identity') {
|
|
2232
|
+
const {
|
|
2233
|
+
subtitle: oldSubtitle,
|
|
2234
|
+
subtitleSegments: oldSubtitleSegments,
|
|
2235
|
+
trailing: oldTrailing,
|
|
2236
|
+
...oldMetadata
|
|
2237
|
+
} = oldRow;
|
|
2238
|
+
const {
|
|
2239
|
+
subtitle,
|
|
2240
|
+
subtitleSegments,
|
|
2241
|
+
trailing,
|
|
2242
|
+
...metadata
|
|
2243
|
+
} = row;
|
|
2244
|
+
if (!sameOptionalPresence(oldSubtitle, subtitle) || !sameOptionalPresence(oldSubtitleSegments, subtitleSegments) || !sameOptionalPresence(oldTrailing, trailing) || !sameJsonValue(oldMetadata, metadata)) return undefined;
|
|
2245
|
+
if (!sameJsonValue(oldSubtitle, subtitle) || !sameJsonValue(oldSubtitleSegments, subtitleSegments) || !sameJsonValue(oldTrailing, trailing)) {
|
|
2246
|
+
patches.push({
|
|
2247
|
+
type: 'identity',
|
|
2248
|
+
key: row.key,
|
|
2249
|
+
changes: {
|
|
2250
|
+
subtitle,
|
|
2251
|
+
subtitleSegments,
|
|
2252
|
+
trailing
|
|
2253
|
+
}
|
|
2254
|
+
});
|
|
2255
|
+
}
|
|
2256
|
+
} else if (oldRow.type === 'dataRow' && row.type === 'dataRow') {
|
|
2257
|
+
const {
|
|
2258
|
+
columns: oldColumns,
|
|
2259
|
+
trailing: oldTrailing,
|
|
2260
|
+
...oldMetadata
|
|
2261
|
+
} = oldRow;
|
|
2262
|
+
const {
|
|
2263
|
+
columns,
|
|
2264
|
+
trailing,
|
|
2265
|
+
...metadata
|
|
2266
|
+
} = row;
|
|
2267
|
+
if (!sameOptionalPresence(oldTrailing, trailing) || !sameJsonValue(oldMetadata, metadata) || !sameJsonValue(oldColumns[0], columns[0])) return undefined;
|
|
2268
|
+
if (!sameJsonValue(oldColumns.slice(1), columns.slice(1)) || !sameJsonValue(oldTrailing, trailing)) {
|
|
2269
|
+
patches.push({
|
|
2270
|
+
type: 'dataRow',
|
|
2271
|
+
key: row.key,
|
|
2272
|
+
changes: {
|
|
2273
|
+
columns,
|
|
2274
|
+
trailing
|
|
2275
|
+
}
|
|
2276
|
+
});
|
|
2277
|
+
}
|
|
2278
|
+
} else if (!sameJsonValue(oldRow, row)) {
|
|
2279
|
+
return undefined;
|
|
2280
|
+
}
|
|
2281
|
+
}
|
|
2282
|
+
return patches.length === 0 ? EMPTY_ROW_PATCHES : patches;
|
|
2283
|
+
}
|
|
2284
|
+
function mergeAndValidateSingleRow(previous, patch, index) {
|
|
2285
|
+
if (previous.key !== patch.key) fail(`patches[${index}].key`, 'does not match row identity');
|
|
2286
|
+
if (previous.type !== patch.type) fail(`patches[${index}].type`, 'does not match row type');
|
|
2287
|
+
const changes = {
|
|
2288
|
+
...patch.changes
|
|
2289
|
+
};
|
|
2290
|
+
if (previous.type === 'market') {
|
|
2291
|
+
if (typeof changes.price === 'string' && !('priceSegments' in changes) && previous.priceSegments !== undefined) changes.priceSegments = null;
|
|
2292
|
+
if ((previous.webTable !== undefined || 'webTable' in changes) && ['price', 'priceSegments', 'change'].some(field => field in changes) && changes.webTable === undefined) fail(`patches[${index}].changes.webTable`, 'table quote changes require visible table cells');
|
|
2293
|
+
}
|
|
2294
|
+
if (previous.type === 'identity' || previous.type === 'market') {
|
|
2295
|
+
if (typeof changes.subtitle === 'string' && !('subtitleSegments' in changes) && previous.subtitleSegments !== undefined) changes.subtitleSegments = null;
|
|
2296
|
+
if (Array.isArray(changes.subtitleSegments) && changes.subtitleSegments.length > 0 && !('subtitle' in changes) && previous.subtitle !== undefined) changes.subtitle = null;
|
|
2297
|
+
}
|
|
2298
|
+
const merged = {
|
|
2299
|
+
...previous
|
|
2300
|
+
};
|
|
2301
|
+
for (const [field, value] of Object.entries(changes)) {
|
|
2302
|
+
if (value === null) delete merged[field];else if (value !== undefined) merged[field] = value;
|
|
2303
|
+
}
|
|
2304
|
+
const next = normalizeRowStyles(merged);
|
|
2305
|
+
assertPlainSerializable(next, `rows[${index}]`, new Set());
|
|
2306
|
+
if (isDevelopment()) assertRow(next, index);
|
|
2307
|
+
assertRowProtocol(next, `rows[${index}]`);
|
|
2308
|
+
return {
|
|
2309
|
+
row: next,
|
|
2310
|
+
patch: {
|
|
2311
|
+
type: patch.type,
|
|
2312
|
+
key: patch.key,
|
|
2313
|
+
changes
|
|
2314
|
+
}
|
|
2315
|
+
};
|
|
2316
|
+
}
|
|
2317
|
+
|
|
2318
|
+
/** One canonical batch owns both accepted rows and the native wire payload. */
|
|
2319
|
+
export function applyValidatedRowPatches(snapshot, patches) {
|
|
2320
|
+
if (!Array.isArray(patches)) fail('patches', 'must be an array');
|
|
2321
|
+
const owned = ownedNativeSnapshots.has(snapshot);
|
|
2322
|
+
const rowIndexes = ownedNativeSnapshots.get(snapshot) ?? validatedRowIndexes.get(snapshot) ?? new Map(snapshot.rows.map((row, index) => [row.key, index]));
|
|
2323
|
+
validatedRowIndexes.set(snapshot, rowIndexes);
|
|
2324
|
+
const units = new Map();
|
|
2325
|
+
const rejected = new Set();
|
|
2326
|
+
const rejectedGroups = new Set();
|
|
2327
|
+
const reject = (key, index, error) => {
|
|
2328
|
+
if (isDevelopment()) throw error;
|
|
2329
|
+
rejected.add(key);
|
|
2330
|
+
const rowIndex = rowIndexes.get(key);
|
|
2331
|
+
const group = rowIndex === undefined ? undefined : snapshot.rows[rowIndex].groupId;
|
|
2332
|
+
if (group) rejectedGroups.add(group);
|
|
2333
|
+
reportRejectedPatch(index);
|
|
2334
|
+
};
|
|
2335
|
+
patches.forEach((input, index) => {
|
|
2336
|
+
const key = isRecord(input) && typeof input.key === 'string' ? input.key : '';
|
|
2337
|
+
try {
|
|
2338
|
+
protocolObject(input, `patches[${index}]`);
|
|
2339
|
+
protocolKey(input.key, `patches[${index}].key`);
|
|
2340
|
+
const rowIndex = rowIndexes.get(input.key);
|
|
2341
|
+
if (rowIndex === undefined) fail(`patches[${index}].key`, 'unknown row identity');
|
|
2342
|
+
if (snapshot.rows[rowIndex].type !== input.type) fail(`patches[${index}].type`, 'does not match row type');
|
|
2343
|
+
const changes = protocolObject(input.changes, `patches[${index}].changes`);
|
|
2344
|
+
const captured = capturePlainData(changes);
|
|
2345
|
+
assertPlainSerializable(captured, `patches[${index}].changes`, new Set(), false);
|
|
2346
|
+
const previous = units.get(input.key);
|
|
2347
|
+
// Undefined is omitted at every merge, so it cannot overwrite an earlier change.
|
|
2348
|
+
const defined = Object.fromEntries(Object.entries(captured).filter(([, value]) => value !== undefined));
|
|
2349
|
+
units.set(input.key, {
|
|
2350
|
+
index,
|
|
2351
|
+
type: input.type,
|
|
2352
|
+
changes: {
|
|
2353
|
+
...previous?.changes,
|
|
2354
|
+
...defined
|
|
2355
|
+
}
|
|
2356
|
+
});
|
|
2357
|
+
} catch (error) {
|
|
2358
|
+
reject(key, index, error);
|
|
1098
2359
|
}
|
|
1099
|
-
return {
|
|
1100
|
-
...row,
|
|
1101
|
-
...patch.changes,
|
|
1102
|
-
key: row.key,
|
|
1103
|
-
type: row.type
|
|
1104
|
-
};
|
|
1105
2360
|
});
|
|
1106
|
-
|
|
2361
|
+
const pending = new Map();
|
|
2362
|
+
for (const [key, unit] of units) {
|
|
2363
|
+
if (rejected.has(key)) continue;
|
|
2364
|
+
try {
|
|
2365
|
+
const patch = normalizePatch({
|
|
2366
|
+
key,
|
|
2367
|
+
type: unit.type,
|
|
2368
|
+
changes: unit.changes
|
|
2369
|
+
}, unit.index);
|
|
2370
|
+
if (!patch) continue;
|
|
2371
|
+
const merged = mergeAndValidateSingleRow(snapshot.rows[rowIndexes.get(key)], patch, rowIndexes.get(key));
|
|
2372
|
+
if (merged.row.disabled && snapshot.selection?.selectedKeys.includes(key)) fail(`patches[${unit.index}]`, 'cannot disable a selected row');
|
|
2373
|
+
pending.set(key, merged);
|
|
2374
|
+
} catch (error) {
|
|
2375
|
+
reject(key, unit.index, error);
|
|
2376
|
+
}
|
|
2377
|
+
}
|
|
2378
|
+
const accepted = [];
|
|
2379
|
+
let rows;
|
|
2380
|
+
for (const [key, value] of pending) {
|
|
2381
|
+
const index = rowIndexes.get(key);
|
|
2382
|
+
const group = snapshot.rows[index].groupId;
|
|
2383
|
+
if (rejected.has(key) || group && rejectedGroups.has(group)) continue;
|
|
2384
|
+
if (!rows) rows = snapshot.rows.slice();
|
|
2385
|
+
rows[index] = owned ? deepFreeze(value.row) : value.row;
|
|
2386
|
+
accepted.push(owned ? deepFreeze(value.patch) : value.patch);
|
|
2387
|
+
}
|
|
2388
|
+
if (!rows) return {
|
|
2389
|
+
patches: EMPTY_ROW_PATCHES,
|
|
2390
|
+
snapshot,
|
|
2391
|
+
json: '[]'
|
|
2392
|
+
};
|
|
2393
|
+
const next = owned ? Object.freeze({
|
|
2394
|
+
...snapshot,
|
|
2395
|
+
rows: Object.freeze(rows)
|
|
2396
|
+
}) : {
|
|
1107
2397
|
...snapshot,
|
|
1108
2398
|
rows
|
|
1109
|
-
}
|
|
2399
|
+
};
|
|
2400
|
+
if (owned) ownedNativeSnapshots.set(next, rowIndexes);else validatedRowIndexes.set(next, rowIndexes);
|
|
2401
|
+
return {
|
|
2402
|
+
patches: accepted,
|
|
2403
|
+
snapshot: next,
|
|
2404
|
+
json: JSON.stringify(accepted)
|
|
2405
|
+
};
|
|
2406
|
+
}
|
|
2407
|
+
const validatedRowIndexes = new WeakMap();
|
|
2408
|
+
export function applyOwnedNativePatches(snapshot, patches) {
|
|
2409
|
+
const owned = ownedNativeSnapshots.has(snapshot) ? snapshot : ownValidatedNativeSnapshot(validateSnapshot(capturePlainData(snapshot)));
|
|
2410
|
+
return applyValidatedRowPatches(owned, patches);
|
|
2411
|
+
}
|
|
2412
|
+
export function applyRowPatches(snapshot, patches) {
|
|
2413
|
+
const baseline = ownedNativeSnapshots.has(snapshot) ? snapshot : validateSnapshot(snapshot);
|
|
2414
|
+
return applyValidatedRowPatches(baseline, patches).snapshot;
|
|
1110
2415
|
}
|
|
1111
2416
|
export function serializeSnapshot(snapshot) {
|
|
1112
2417
|
return JSON.stringify(validateSnapshot(snapshot));
|