mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-11-07 04:23:40 +09:00
merge main
This commit is contained in:
commit
43a5889481
2 files changed
+26
-20
No files matched your search
@@ -22,31 +22,36 @@ export const isValid = (property: string, value: string): boolean => {
|
||||
return true;
|
||||
};
|
||||
|
||||
// wtf?
|
||||
// eslint-disable-next-line no-useless-escape
|
||||
const mathRegex = /[\+\-\*\/]/;
|
||||
// If expression is a math expression, evaluates it.
|
||||
// Otherwise returns undefined.
|
||||
const evaluateMath = (expression: string) => {
|
||||
if (/^[\d\s.+*/-]+$/.test(expression) === false) {
|
||||
return undefined;
|
||||
}
|
||||
try {
|
||||
// Eval is safe here because of the regex above
|
||||
const result = eval(`(${expression})`);
|
||||
if (typeof result === "number") {
|
||||
return result;
|
||||
}
|
||||
} catch (err) {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
// - 2+2px
|
||||
// - 2*2
|
||||
const evaluate = (input: string, parsedUnit: [Unit] | null) => {
|
||||
const parsed = parseFloat(input);
|
||||
// If its not a number, it can't be a math expression.
|
||||
if (isNaN(parsed)) return parsed;
|
||||
const result = evaluateMath(
|
||||
parsedUnit === null ? input : input.replace(parsedUnit[0], "")
|
||||
);
|
||||
|
||||
// It's a math expression
|
||||
if (mathRegex.test(input)) {
|
||||
// Get rid of the unit
|
||||
if (parsedUnit !== null) {
|
||||
input = input.replace(parsedUnit[0], "");
|
||||
}
|
||||
try {
|
||||
return eval(`(${input})`);
|
||||
} catch (err) {
|
||||
return parsed;
|
||||
}
|
||||
if (result !== undefined) {
|
||||
return result;
|
||||
}
|
||||
|
||||
return parsed;
|
||||
const number = parseFloat(input);
|
||||
return isNaN(number) ? undefined : number;
|
||||
};
|
||||
|
||||
// Helper to let user input:
|
||||
@@ -73,7 +78,7 @@ export const parseCssValue = (
|
||||
|
||||
// If we get a unit but there is no number - we assume its an accidental
|
||||
// unit match and its a keyword value.
|
||||
if (isNaN(number) === true) {
|
||||
if (number === undefined) {
|
||||
if (isValid(property, input)) {
|
||||
return {
|
||||
type: "keyword",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@webstudio-is/react-sdk",
|
||||
"version": "0.7.1",
|
||||
"version": "0.7.2",
|
||||
"description": "Webstudio JavaScript / TypeScript API",
|
||||
"author": "Webstudio <github@webstudio.is>",
|
||||
"homepage": "https://webstudio.is",
|
||||
@@ -56,6 +56,7 @@
|
||||
"zod": "^3.19.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"detect-font": "^0.1.5",
|
||||
"@webstudio-is/asset-uploader": "^*",
|
||||
"@webstudio-is/fonts": "*",
|
||||
"@webstudio-is/icons": "*",
|
||||
|
||||
Reference in new issue
Block a user