Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,58 @@ $id: '${id}'`
}
};

const convertFormat = (
text: string,
fromFormat: "json" | "yaml",
toFormat: "json" | "yaml"
): { text: string; error: boolean } => {
if (!text || !text.trim()) return { text, error: false };

try {
const parsed = parse(text, fromFormat);

if (toFormat === "yaml") {
return {
text: YAML.dump(parsed, { indent: $settings.indentSize }),
error: false
};
} else {
return {
text: JSON.stringify(parsed, null, $settings.indentSize),
error: false
};
}
} catch (e) {
return { text, error: true };
}
};

const setFormat = (newFormat: "json" | "yaml") => () => {
if (format === newFormat) return;
let hasError = false;
schemas = schemas.map((schema) => {
const result = convertFormat(schema.text ?? "", format, newFormat);
if (result.error) hasError = true;

return {
...schema,
text: result.text
};
});

instances = instances.map((instance) => {
const result = convertFormat(instance.text ?? "", format, newFormat);
if (result.error) hasError = true;

return {
...instance,
text: result.text
};
});

if (hasError) return;

format = newFormat;
schemas = [newSchema("Schema", schemaUrl, true)];
instances = [newInstance()];
selectedInstance = 0;
};
</script>

Expand Down