Fix color format

commit_hash:7ef2dffc1b244549183211c5b0471677481b3c47
This commit is contained in:
4eb0da
2025-10-01 16:34:43 +03:00
parent 4ada2eca47
commit f5f58c79a5
10 changed files with 79 additions and 19 deletions
+17 -3
View File
@@ -1,6 +1,7 @@
import type { ActionArrayInsertValue, ActionArrayRemoveValue, ActionArraySetValue, WrappedError } from '../../typings/common';
import type { ArrayVariable, Variable } from '../../typings/variables';
import type { MaybeMissing } from '../expressions/json';
import { convertTypedValue } from '../expressions/utils';
import type { ComponentContext } from '../types/componentContext';
import { wrapError } from '../utils/wrapError';
@@ -31,12 +32,19 @@ export function arrayInsert(
length: list.length
}
}));
} else if (!value.type) {
logError(wrapError(new Error('Incorrect value type'), {
additional: {
name
}
}));
} else {
const newList = list.slice();
const val = convertTypedValue(value);
if (typeof index === 'number') {
newList.splice(index, 0, value.value);
newList.splice(index, 0, val);
} else {
newList.push(value.value);
newList.push(val);
}
variableInstance.setValue(newList);
}
@@ -105,9 +113,15 @@ export function arraySet(
length: list.length
}
}));
} else if (!value.type) {
logError(wrapError(new Error('Incorrect value type'), {
additional: {
name
}
}));
} else {
const newList = list.slice();
newList[index] = value.value;
newList[index] = convertTypedValue(value);
variableInstance.setValue(newList);
}
});
+10 -1
View File
@@ -1,6 +1,7 @@
import type { ActionDictSetValue, WrappedError } from '../../typings/common';
import type { Variable } from '../../typings/variables';
import type { MaybeMissing } from '../expressions/json';
import { convertTypedValue } from '../expressions/utils';
import type { ComponentContext } from '../types/componentContext';
import { wrapError } from '../utils/wrapError';
@@ -30,6 +31,14 @@ export function dictSetValue(
return;
}
if (value && !value.type) {
logError(wrapError(new Error('Incorrect value type'), {
additional: {
name
}
}));
}
const variableInstance = componentContext?.getVariable(name) || variables.get(name);
if (!variableInstance) {
@@ -46,7 +55,7 @@ export function dictSetValue(
const dict = variableInstance.getValue() as Record<string, unknown>;
const newDict = { ...dict };
if (value) {
newDict[key] = value.value;
newDict[key] = convertTypedValue(value);
} else {
delete newDict[key];
}
@@ -1,6 +1,7 @@
import type { ActionUpdateStructure, WrappedError } from '../../typings/common';
import type { Variable } from '../../typings/variables';
import type { MaybeMissing } from '../expressions/json';
import { convertTypedValue } from '../expressions/utils';
import type { ComponentContext } from '../types/componentContext';
import { wrapError } from '../utils/wrapError';
@@ -120,7 +121,7 @@ export function updateStructure(
}
}
temp[parts[parts.length - 1]] = value.value;
temp[parts[parts.length - 1]] = convertTypedValue(value);
variableInstance.setValue(newObj);
} else {
logError(wrapError(new Error('Action requires array or dictionary variable'), {
+26 -2
View File
@@ -6,6 +6,8 @@ import { parseColor, type ParsedColor } from '../utils/correctColor';
import { padLeft } from '../utils/padLeft';
import { MAX_INT, MIN_INT, toBigInt } from './bigint';
import { BOOLEAN, NUMBER } from './const';
import type { TypedValue } from '../../typings/common';
import type { MaybeMissing } from './json';
export function valToInternal(val: EvalValue): EvalValue {
if (val.type === 'url' || val.type === 'color') {
@@ -179,7 +181,11 @@ const EVAL_TYPE_TO_JS_TYPE = {
array: 'array',
dict: 'object'
};
export function convertJsValueToDivKit(ctx: EvalContext, val: unknown, evalType: EvalTypesWithoutDatetime): EvalValue {
export function convertJsValueToDivKit(
ctx: EvalContext | undefined,
val: unknown,
evalType: EvalTypesWithoutDatetime
): EvalValue {
const jsType = EVAL_TYPE_TO_JS_TYPE[evalType];
let type: string = typeof val;
@@ -200,7 +206,9 @@ export function convertJsValueToDivKit(ctx: EvalContext, val: unknown, evalType:
throw new Error(`Incorrect value type: expected ${typeToString(evalType)}, got ${typeToString(type)}.`);
}
if (jsType === 'number' && evalType === 'integer') {
checkIntegerOverflow(ctx, val as number);
if (ctx) {
checkIntegerOverflow(ctx, val as number);
}
try {
val = toBigInt(val as number);
} catch (_err) {
@@ -222,3 +230,19 @@ export function convertJsValueToDivKit(ctx: EvalContext, val: unknown, evalType:
value: val
} as EvalValue;
}
export function convertDivKitValueToJson(value: EvalValue) {
if (value.type === 'number' || value.type === 'integer') {
return Number(value.value);
} else if (value.type === 'boolean') {
return Boolean(value.value);
}
return value.value;
}
export function convertTypedValue(value: MaybeMissing<TypedValue>) {
return convertDivKitValueToJson(
convertJsValueToDivKit(undefined, value.value, value.type as EvalTypesWithoutDatetime)
);
}
@@ -3,7 +3,7 @@ import { writable } from 'svelte/store';
import type { EvalValue } from './eval';
import { parseColor } from '../utils/correctColor';
import { bigIntZero, toBigInt } from './bigint';
import { checkUrl } from './utils';
import { checkUrl, transformColorValue } from './utils';
import { BOOLEAN } from './const';
export type VariableType = 'string' | 'number' | 'integer' | 'boolean' | 'color' | 'url' | 'dict' | 'array';
@@ -161,8 +161,7 @@ export class ColorVariable extends Variable<string, 'color'> {
throw new Error('Incorrect variable value');
}
// save input value, some expression tests rely on that
return value;
return transformColorValue(value);
}
protected fromString(val: string) {
@@ -6,7 +6,7 @@ import {
} from 'vitest';
import { evalExpression, type EvalResult } from '../../src/expressions/eval';
import { valToString } from '../../src/expressions/utils';
import { transformColorValue, valToString } from '../../src/expressions/utils';
import { parse } from '../../src/expressions/expressions';
import { createVariable } from '../../src/expressions/variable';
@@ -34,6 +34,11 @@ function convertVals(val: EvalResult) {
type: 'datetime',
value: valToString(val, false)
};
} else if (val.type === 'color') {
return {
type: 'color',
value: transformColorValue(val.value)
};
}
return val;
@@ -40,7 +40,8 @@
],
"platforms": [
"android",
"ios"
"ios",
"web"
]
},
{
@@ -92,7 +93,8 @@
],
"platforms": [
"android",
"ios"
"ios",
"web"
]
}
]
@@ -317,7 +317,8 @@
}
],
"platforms": [
"android"
"android",
"web"
]
},
{
@@ -662,7 +663,8 @@
}
],
"platforms": [
"android"
"android",
"web"
]
},
{
@@ -317,7 +317,8 @@
}
],
"platforms": [
"android"
"android",
"web"
]
},
{
@@ -662,7 +663,8 @@
}
],
"platforms": [
"android"
"android",
"web"
]
},
{
@@ -1740,7 +1740,8 @@
}
],
"platforms": [
"android"
"android",
"web"
]
},
{
@@ -2017,7 +2018,8 @@
}
],
"platforms": [
"android"
"android",
"web"
]
},
{