class SvgNode { constructor(tag, attrs, items) { this.tag = tag this.attrs = attrs || {} this.items = items || [] } append(...items){ this.items = [...this.items, ...items.filter(x => x)] return this } set_attr(attr, value) { this.attrs[attr] = value return this } set_attrs(attrs) { this.attrs = { ...this.attrs, ...attrs } return this } add_style(style) { this.style = { ...this.style || {}, ...style } return this } clear_style() { this.style = null return this } move(x, y) { if (this.transform) this.transform = { x: this.transform.x + x, y: this.transform.y + y } else this.transform = { x, y } return this } get(attr) { } _render_node(node) { // console.log(typeof node) // if (typeof node == 'undefined') return '' if (typeof node == 'string') return node else if (typeof node == 'object' && node.render) return node.render() else if (Array.isArray(node)) return node.map(this._render_node).join('') else return '' } render() { let attrs = Object.keys(this.attrs).map(x => ` ${x}="${this.attrs[x]}"`).join('') // console.log(this.style ? Object.keys(this.style).map(x => `${x}:${this.style[x]}`) : '') let style = this.style ? ' style="' + Object.keys(this.style).map(x => `${x}:${this.style[x]}`).join('; ') + '"' : '' let transfrom = this.transform ? ` transform="translate(${this.transform.x},${this.transform.y})"` : '' let items = this.items ? Array.isArray(this.items) ? this.items.map(x => this._render_node(x)).join('') : this._render_node(this.items) : '' if (this.tag) return `<${this.tag}${attrs}${style}${transfrom}>${items}` else return items } } module.exports = SvgNode module.exports.default = module.exports;