A laugh and a reminder

I was staring at the below code last night for some minutes, wondering why it was failing in the most obvious way, and finally laughed when I figured it out. Thought this might help someone else.

So, annotationNodes is a perfectly fine array of nodes (all console.log'ed and confirmed), but as soon as I assigned it to selection.items, the latter would be entirely empty, and of course the commands.group() would fail.

Seemed plain as the nose on your face that something was completely crazy.

Those moments of disbelief were quite memorable. I started to doubt whether I had the “real” selection or the real commands or something pretty basic. Then I started to doubt my sanity.

Then I Read The Fine Documentation for .group() one last time in desperation, and noticed a small note that grouping wouldn’t accept locked nodes (same as the UI). So then the light went on, and, sure enough, all the nodes in annotationNodes were locked (I had created them that way for a reason). Easy fix.

  // Group all the annotations and associate it with the board.
  if (annotationNodes.length > 0) {
    selection.items = annotationNodes
    let group = selection.items[0]
    group.locked = true
    group.name = `{{d2d annotation group}}`
    boardSetTextDecorationGuid(board, group.guid)

I wonder if we should make the selection.items setter print a console warning if a plugin passes it any locked items. It sounds like you would have been able to notice a console warning in this situation? (And if so that would have saved you a bunch of debugging time).

My instinct is first to say, YES, great idea, thanks!

Then I think of adding stray messages to the console, when you could probably find another 100 cases which might benefit from debugging hints like that, and then I say NO. :wink:

1 Like