retejs / rete

JavaScript framework for visual programming
https://retejs.org
MIT License
10.17k stars 653 forks source link

How to set default values during Node creation #364

Closed sttng closed 5 years ago

sttng commented 5 years ago

I am working (in my free time) on a RenderMan node editor using rete.js (https://github.com/sttng/slim-shady). I want to prepopulate some values (some of them are to be user editable later, some aren't). How would I prepopulate control values during constructor calling ?

Ni55aN commented 5 years ago

Do you mean a default values in Control?

You can call setValue or define a default value in node.data[key], where key is a second parameter in the contructor

sttng commented 5 years ago

setValue doesn't work yet, because Vue render isn't ready yet. So basically "...vueContext.value" doesn't exists yet. Key is not the Value of the Control, but rather the "name" of the Control. I checked the CodePen https://codepen.io/Ni55aN/pen/xzgQYq and I cant set a default value for lets say a number Control ("NumControl) of Number Component either. For ex. maybe I would like the default to be "100" for every Number Component added - while of course the users can still set it to whatever he / she likes.

Ni55aN commented 5 years ago
mounted() {
  this.value = this.getData(this.ikey);
}

here you can set an initial value, for example:

let val = this.getData(this.ikey);
this.value =  val === undefined ? 100 : val;
sttng commented 5 years ago

OK I choose a different approach, which lets me call the Control Constructur and define the default value dynamically

1) Add default (defaultVal) value to Constructor and to props property:

class NumControl extends Rete.Control {

  constructor(emitter, key, readonly, defaultVal) {
    super(key);
    this.component = VueNumControl;
    this.props = { emitter, ikey: key, readonly, defaultVal};
  }
  1. Add defaultVal to the props of VueNumControl
var VueNumControl = {
  props: ['readonly', 'defaultVal', 'emitter', 'ikey', 'getData', 'putData'],
  1. Enhance mounted
  mounted() {
    this.value = this.getData(this.ikey);
    let val = this.getData(this.ikey);
    this.value =  val === undefined ? this.defaultVal : val;
    this.update();
  }