Coder Social home page Coder Social logo

mani3274 / node-sketch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oscarotero/node-sketch

0.0 0.0 0.0 2.95 MB

๐Ÿ’Ž Javascript library to manipulate sketch files

Home Page: https://oscarotero.github.io/node-sketch/

License: MIT License

JavaScript 100.00%

node-sketch's Introduction

๐Ÿ’Ž node-sketch

Javascript library to manipulate sketch files

Install

npm install node-sketch

Example:

const ns = require('node-sketch');

ns.read('design.sketch').then(sketch => {

    //Search the symbol named 'button'
    const buttonSymbol = sketch.symbolsPage.get('symbolMaster', 'button');

    //Search all instances of a symbol named 'old-button' and replace it with 'button'
    sketch
        .getAll('symbolInstance', instance => instance.symbolMaster.name === 'old-button')
        .forEach(instance => instance.symbolMaster = buttonSymbol);

    //Save the result
    sketch.save('modified-design.sketch');
});

View more examples in the /examples folder

API

Two classes are used to manage sketch files:

Sketch

Represents the sketch file and contains all data (pages, symbols, styles, shapes, etc). Contains the method .save() to create a sketch file with the result.

const ns = require('node-sketch');

ns.read('input.sketch').then(sketch => {
    sketch.document         // document data
    sketch.meta             // meta data
    sketch.user             // user data
    sketch.pages            // array with all pages
    sketch.symbolsPage      // the Symbols page
    sketch.sharedStyles     // array with the shared styles
    sketch.textStyles       // array with the text styles
    sketch.colors           // array containing the colors stored in the color palette
    sketch.gradients        // array containing the gradients stored in the gradient palette
    sketch.localSymbols     // array with all symbols stored in the document
    sketch.foreignSymbols   // array with the symbols loaded from external libraries

    sketch.save('output.sketch');
});

Node

It's the base class used by all other elements. Any page, symbol, color, etc is an instance of this class.

const symbolsPage = sketch.symbolsPage;

console.log(symbolsPage instanceof Node); //true 

//It include useful methods to search an inner node by class:
const button = symbolsPage.get('symbolMaster');

//by class and name
const button = symbolsPage.get('symbolMaster', 'button');

//by class and callback
const button = symbolsPage.get('symbolMaster', symbol => symbol.name === 'button');

//Just a callback
const button = symbolsPage.get(node => node._class === 'symbolMaster' && node.name === 'button');

//And the same than above but returning all inner nodes instead just the first:
const allSymbols = symbolsPage.getAll('symbolMaster');

There are other classes extending Node to provide special funcionalities in some nodes, like Style or SymbolInstance.

Plugins

The plugins namespace contains a set of plugins with common functions. For example:

const sketch = require('node-sketch');

sketch
    .read(__dirname + '/example.sketch')
    .then(file => {
        file
            .use(new sketch.plugins.RemoveDuplicatedSymbols())
            .use(new sketch.plugins.RemoveDuplicatedStyles())
            .use(new sketch.plugins.ExportImages(__dirname))
            .save(__dirname + '/result.sketch');
    })
    .catch(err => {
        console.error('Error reading the sketch file');
        console.error(err);
    });

You can see the list of available plugins here


See the API detailed

Or build it locally with npm run docs

node-sketch's People

Contributors

oscarotero avatar dependabot-support avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.