Support single-quote strings in style editor

This commit is contained in:
Brian Vaughn
2019-08-16 13:35:30 -07:00
parent 74f4a3f972
commit 024bac4263
@@ -178,7 +178,7 @@ function Row({
const validateAndSetLocalValue = value => {
let isValid = false;
try {
JSON.parse(value);
JSON.parse(sanitizeForParse(value));
isValid = true;
} catch (error) {}
@@ -198,7 +198,7 @@ function Row({
const submitValueChange = () => {
if (isValueValid) {
const parsedLocalValue = JSON.parse(localValue);
const parsedLocalValue = JSON.parse(sanitizeForParse(localValue));
if (value !== parsedLocalValue) {
changeValue(attribute, parsedLocalValue);
}
@@ -282,3 +282,16 @@ function Field({
/>
);
}
// We use JSON.parse to parse string values
// e.g. 'foo' is not valid JSON but it is a valid string
// so this method replaces e.g. 'foo' with "foo"
function sanitizeForParse(value: any) {
if (typeof value === 'string') {
if (value.charAt(0) === "'" && value.charAt(value.length - 1) === "'") {
return '"' + value.substr(1, value.length - 2) + '"';
}
}
return value;
}