UNPKG

@selenite/graph-editor

Version:

A graph editor for visual programming, based on rete and svelte.

54 lines (53 loc) 1.54 kB
import { ErrorWNotif } from '../../../global/todo.svelte'; import { formatXMLArray } from '@selenite/commons'; function formatToXML(obj) { if (obj instanceof Array) { return formatXMLArray(obj); } if (typeof obj === 'boolean') { return obj ? '1' : '0'; } return obj.toString(); } export class XMLData { tag; name; properties; children; node; constructor({ tag, name, properties, children, node }) { this.tag = tag; this.name = name; this.properties = properties; this.children = children; this.node = node; } toXml() { let xml = `<${this.tag}`; if (this.name) xml += ` name="${this.name}"`; Object.entries(this.properties).forEach(([name, value]) => { // console.log(`property: ${name} value: ${JSON.stringify(value)}`) const val = formatToXML(value); xml += ` ${name}="${val}"`; }); if (this.children.length === 0) xml += '/>'; else { xml += '>'; try { this.children.forEach((child) => { if (!child) return; xml += child.toXml(); }); } catch (e) { console.error(e); throw new ErrorWNotif('Error generating XML, are you sure you connected everything ?'); } xml += `</${this.tag}>`; } return xml; } }