zdog icon indicating copy to clipboard operation
zdog copied to clipboard

name option for items, so they can be accessed after copyGraph

Open desandro opened this issue 5 years ago • 3 comments

Add a 👍 reaction to this issue if you would like to see this feature added. Do not add +1 comments — They will be deleted.


Right now there's no easy way to access a child of a parent copied with copyGraph.

// right arm
let upperArm = new Zdog.Shape({
  //...
});
let foreArm = new Zdog.Shape({
  addTo: upperArm,
  // ...
});
// hand
new Zdog.Shape({
  addTo: foreArm,
  color: '#C25',
  // ...
});

// left arm
upperArm.copyGraph();

// How do set the left hand to a different color?

With the name option, you could access children via an Object property.

// right arm
let upperArm = new Zdog.Shape({
  //...
});
new Zdog.Shape({
  addTo: upperArm,
  name: 'foreArm', // accessible as parent.foreArm
  // ...
});
// hand
new Zdog.Shape({
  addTo: upperArm.foreArm,
  name: 'hand', // accessible as parent.hand
  color: '#C25',
  // ...
});

// left arm
let leftArm = upperArm.copyGraph();

// set left hand to different color
leftArm.foreArm.hand.color = '#EA0';

desandro avatar May 26 '19 18:05 desandro

Hi @desandro !

I could implement this if you like.

We have two things to solve:

1. Attach the name property to the instance.

It could be done in the create function of the Anchor class, so every shape inherits it.

2. Give direct access to a child with a parent.childName syntax

You could attach a property that points to the child in the parent instance for sure, but then you have to handle name collisions (naming an item "rotate" for example). This may force to output warnings to users telling certain names are restricted when they use it (which I never really liked as a developer experience).

If we don't want to worry about name collisions, I would personally opt for a getChild("my-child-name") property that loops throughout the children property looking for an item which has the correct name.

Maybe I'm not aware of an existing way to handle dynamic properties accessors without name collisions ?

Let me know what you think about it ! 😉

enzoferey avatar May 30 '19 13:05 enzoferey

@enzoferey Thanks for the the offer. I think I can handle this feature. But, I'd like to gauge outside interest before adding it in.

desandro avatar May 30 '19 14:05 desandro

Hi @desandro ! I discovered Zdog recently, and I like it a lot, it's a cool project. You made a very good job, thank's a lot. I would appreciate to be able to set a ChildName on the graphical objects. And, if you could add a "find" function on the main object to retrieve a child by its name, it would be really great. Thank's a lot.

gregja avatar Jun 07 '19 19:06 gregja