UNPKG

@selenite/graph-editor

Version:

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

128 lines (127 loc) 5.07 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var _a, _b; import { Node, path, registerNode } from '../Node.svelte'; import { getVarsFromFormatString } from '../../../utils/string'; import { upperFirst } from 'lodash-es'; let FormatNode = class FormatNode extends Node { varsType; constructor(params = {}) { // super('Format', { factory, height: 124.181818 + 43.818182 }); super({ label: 'Format', ...params }); this.varsType = params.varsType ?? 'any'; this.pythonComponent.setDataCodeGetter('result', () => { const vars = this.getFormatVariablesKeys(); const var_bindings = Object.entries(vars) .map(([key, varname]) => `${varname}=$(${key})`) .join(', '); return `$(format).format(${var_bindings})`; }); this.addOutData('result', { type: params.outType ?? 'string' }); this.addInData('format', { type: 'string', initial: '{name}', control: { type: 'textarea', onChange: (value) => { this.params.format = value; this.updateDataInputs(); } } }); this.updateDataInputs(); } getFormatVariablesKeys() { const res = {}; for (const key in this.inputs) { if (key.startsWith('data-')) res[key] = key.slice('data-'.length); } return res; } getValues(inputs) { const values = {}; Object.keys(this.inputs).forEach((key) => { if (key.startsWith('data-')) values[key.slice('data-'.length)] = this.getData(key, inputs); }); return values; } data(inputs) { let res = this.getData('format', inputs); // console.debug(res) const values = this.getValues(inputs); for (const [k, v] of Object.entries(values)) { res = res.replace(`{${k}}`, v); } return { result: res }; } updateDataInputs(inputs) { const formatString = this.getData('format', inputs); let anyChange = false; // console.debug(formatString); if (formatString) { try { const vars = getVarsFromFormatString(formatString); // console.debug('vars', vars); vars.forEach((varName) => { const key = 'data-' + varName; if (!(key in this.inputs)) { // const varData = this.getData(varName, inputs); anyChange = true; this.addInData(key, { label: upperFirst(varName), alwaysShowLabel: true, type: this.varsType }); // console.debug('added', key); } }); for (const [i, var_] of vars.entries()) { const key = 'data-' + var_; this.inputs[key].index = i; } for (const key in this.inputs) { if (key.startsWith('data-') && !vars.includes(key.substr(5))) { this.removeInput(key); anyChange = true; } } } catch (error) { return; } } if (anyChange) this.updateElement(); } }; FormatNode = __decorate([ registerNode('string.Format'), __metadata("design:paramtypes", [typeof (_a = typeof FormatNodeParams !== "undefined" && FormatNodeParams) === "function" ? _a : Object]) ], FormatNode); export { FormatNode }; let FormatGroupNameNode = class FormatGroupNameNode extends FormatNode { constructor(params = {}) { super({ label: 'Format Group Name', varsType: 'groupNameRef', outType: 'groupNameRef', ...params }); } }; FormatGroupNameNode = __decorate([ registerNode('xml.FormatGroupName'), __metadata("design:paramtypes", [typeof (_b = typeof FormatNodeParams !== "undefined" && FormatNodeParams) === "function" ? _b : Object]) ], FormatGroupNameNode); export { FormatGroupNameNode };