Skip to content

Commit

Permalink
Fix style not an object when import from a svg
Browse files Browse the repository at this point in the history
  • Loading branch information
langonginc committed Jun 2, 2024
1 parent 1ef23b2 commit b94b764
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/components/header/import-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,41 @@ export const ImportFromSvg = (props: { isOpen: boolean; onClose: () => void }) =
},
];

function convertStyleStringToObject(style: any): Record<string, string> {
if (typeof style === 'object') {
return style;
} else if (typeof style === 'string') {
const styleObj: Record<string, string> = {};
const styleArray = style.split(';');
styleArray.forEach(item => {
const [property, value] = item.split(':');
if (property && value) {
const trimmedProperty = property.trim();
const trimmedValue = value.trim();
const camelCaseProperty = trimmedProperty.replace(/-([a-z])/g, (match, letter) =>
letter.toUpperCase()
);
styleObj[camelCaseProperty] = trimmedValue;
}
});
return styleObj;
}
return {};
}

const handleImport = () => {
const parser = new DOMParser();
const doc = parser.parseFromString(svgString, 'image/svg+xml');

const dfs = (element: Element): SvgsElem => {
const attributes: Record<string, string> = {};
Array.from(element.attributes).forEach(attr => {
attributes[attr.name] = `"${attr.value}"`;
if (attr.name === 'style') {
console.log(convertStyleStringToObject(attr.value));
attributes[attr.name] = JSON.stringify(convertStyleStringToObject(attr.value));
} else {
attributes[attr.name] = `"${attr.value}"`;
}
});

const children: SvgsElem[] = [];
Expand Down

0 comments on commit b94b764

Please sign in to comment.