npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

xd-scenegraph-helper

v0.0.8

Published

xd-scenegraph-helper is a set of lightweight helper functions with no external dependencies that let you easily traverse the scenegraph of a XD document. While traversing, these functions will always respect the order of XD document flow. XD's default flo

Downloads

3

Readme

xd-scenegraph-helper.

xd-scenegraph-helper is a set of lightweight helper functions with no external dependencies that let's you easily traverse the scenegraph of a XD document. While traversing, these functions will always respect the order of XD document flow. XD's default flow is: Nodes are included in back-to-front order

Before Installation.

Make sure you have scenengraph correctly exposed to your plugin's code as a dependency. If you're using webpack you can do it by adding this to webpack.config.js:

externals: {
scenegraph:  "scenegraph",
},

Installation.

You can install this package via npm.

npm i xd-scenegraph-helper
// or
yarn add xd-scenegraph-helper

Usage

You can use it in your plugin setup like so:

const {findAll, findOne, findChildren, findChild} = require("xd-scenegraph-helper");

API

findAll

Searches this entire subtree (this node's children, its children's children, etc). Returns all nodes for which callback returns true.

Signature

findAll (targetNode: SceneNode.isContainer ,callback?: (node: SceneNode) => boolean): ReadonlyArray<[SceneNode]>

Parameters

callback

A function that evaluates whether to return the provided node. If this argument is omitted, findAll returns all nodes in the subtree.

Remarks

Nodes are included in back-to-front order. Parents always appear before their children, and children appear in same relative order before their children, and children appear in same relative order as in the children array.

This traversal method is known as "pre-order traversal". Example: find all Rectangle nodes and all nodes across the tree.

const  scenegraph  =  require("scenegraph");
//selected node is an Artboard here.
const  selectedNode =  scenegraph.selection.items[0];
const {findAll} = require("xd-scenegraph-helper");
//for getting all nodes across the tree.
const allNodesInTree = findAll(selectedNode);
//for getting specific nodes across the tree.
const rectangles = findAll(selectedNode, (node)=> node instanceof scenegraph.Rectangle)

findOne

Searches this entire subtree (this node's children, its children's children, etc). Returns the first node for which callback returns true.

Signature

findOne (targetNode: SceneNode.isContainer ,callback?: (node: SceneNode) => boolean): Scenenode | null

Parameters

callback

A function that evaluates whether to return the provided node.

Remarks

This function returns null if no matching node is found. The traversal order is the same as in findAll. Example:

const  scenegraph  =  require("scenegraph");
//selected node is an Artboard here.
const  selectedNode =  scenegraph.selection.items[0];
const {findOne} = require("xd-scenegraph-helper");
//for getting specific a node across the tree.
const rectangle = findOne(selectedNode, (node)=> node instanceof scenegraph.Rectangle)

findChild

Searches the immediate children of this node (i.e. not including the children's children). Returns the first node for which callback returns true.

Signature

findChild (targetNode: SceneNode.isContainer, callback?: (node: SceneNode) => boolean): Scenenode | null

Parameters

callback

A function that evaluates whether to return the provided node.

Remarks

This function returns null if no matching node is found. Example: find Group node whose is a immediate child of the targetNode.

const  scenegraph  =  require("scenegraph");
//selected node is an Artboard here.
const  selectedNode =  scenegraph.selection.items[0];
const {findChild} = require("xd-scenegraph-helper");
//for getting specific immediate child.
const rectangle = findChild(selectedNode,(node)=> node instanceof scenegraph.Group)

findChildren

Searches the immediate children of this node (i.e. not including the children's children). Returns all nodes for which callback returns true.

Signature

findChildren (targetNode: SceneNode.isContainer, callback?: (node: SceneNode) => boolean): ReadonlyArray<[SceneNode]>

Parameters

callback

A function that evaluates whether to return the provided node. If this argument is omitted, findChildren returns node.children.

Remarks

Example: find all Group nodes who are immediate child of the targetNode.

const  scenegraph  =  require("scenegraph");
//selected node is an Artboard here.
const  selectedNode =  scenegraph.selection.items[0];
const {findChildren} = require("xd-scenegraph-helper");
//for getting specific immediate children.
const groups = findChild(selectedNode,(node)=> node instanceof scenegraph.Group)

Authors

License

MIT

Acknowledgments

xd-scenegraph-herlper is heavily inspired by figma's API implementation.

Issues

Feel free to open an issue or submit a PR (please include isolated reproducible code) and i'll take a look at it as soon as humanly possible.