Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new Layer() with existing definition results in sublayer id's being identical #35

Open
jonmadison opened this issue Jul 1, 2019 · 5 comments

Comments

@jonmadison
Copy link

jonmadison commented Jul 1, 2019

when i create a new artboard from an existing artboard:

const artboard = new Artboard(source_artboard);

only the outer layer ID is created anew; all sublayers are identical, resulting in interesting behavior within Sketch: I select a sublayer, and both that sublayer and the duplicated object's sublayer are selected.

My current workaround is creating a newObject function that will create new object Ids for each sublayer:

let newObject = source_obj  => {
  Object.keys(source_obj).forEach(key => {
    if (key === "do_objectID") {
      source_obj[key] = uuidv1().toUpperCase();
      return source_obj;
    }
    if (typeof source_obj[key] === "object") {
      newObject(source_obj[key]);
    }
  });
  return source_obj;
};

and instead call it

const artboard = newObject(source_artboard);

It'd be nice if new Artboard(source_json) would do the same. Any reason not to?

@dbanksdesign
Copy link
Member

What is source_artboard? If it raw sketch JSON, you can pass it into the constructor as a 2nd argument and it should hydrate the class (and all child layer classes) properly by just passing through the JSON. So it won't change any IDs. The first argument of all the model constructors are for if you are creating them by hand.

const customArtboard = new Artboard({ /* opts */ });
const artboard = new Artboard(null, source_artboard);

Does that work, or do we need the ability to throw out IDs from raw JSON in some cases?

@bise1157
Copy link

bise1157 commented Sep 4, 2019

I ran into the same thing. I wanted to use an artboard as a template and duplicate it multiple times. if you use const artboard = new Artboard(null, source_artboard); every artboard and all layers will have the same id. And this results in the "interesting behavior within Sketch".

@dbanksdesign
Copy link
Member

Ahhhhh ok I see the issue now. Should each new instance of any model always generate a new id? I'm trying to think of any downsides to this..

@bise1157
Copy link

bise1157 commented Sep 5, 2019

This would be the idea. I used @jonmadison's approach and it changes all ids.
But I found two more problems for my file:

  1. layerToClass does not support all layer types yet. I added rectangle and oval to strToClass and it worked right away.
  2. some objects in the instances are still referencing the original model (this is what I think) - like "fills" or "attributedString"

My workaround is to change all the do_objectIDs in my json and deepClone the object before passing it to the artboard constructor. This worked for me at least.

@dbanksdesign
Copy link
Member

If you want to open a PR to add layer types to layerToClass that would be wonderful, if not I can do it tomorrow. We should definitely fix the object IDs, it is not great to have to do work arounds like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants