├── .gitignore ├── Contentful.sketchplugin └── Contents │ ├── Resources │ └── icon@2x.png │ └── Sketch │ ├── lib │ ├── fetch.js │ ├── settings.js │ └── utils.js │ └── manifest.json ├── LICENSE ├── README.md ├── package.json └── sketch-contentful.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /Contentful.sketchplugin/Contents/Resources/icon@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stefanjudis/sketch-contentful/96443402e0db7c54b70fdfc9efcc5ea9b7c5109d/Contentful.sketchplugin/Contents/Resources/icon@2x.png -------------------------------------------------------------------------------- /Contentful.sketchplugin/Contents/Sketch/lib/fetch.js: -------------------------------------------------------------------------------- 1 | @import 'utils.js'; 2 | 3 | 4 | /** 5 | * Populate all layers in a document 6 | */ 7 | function populateData( context ) { 8 | initContext( context ); 9 | 10 | layersSuccessfulFilled = 0; 11 | errors = []; 12 | 13 | var spaceId = command.valueForKey_onDocument_forPluginIdentifier( 14 | 'spaceId', 15 | docData, 16 | 'contentful' 17 | ); 18 | 19 | var cdaToken = command.valueForKey_onDocument_forPluginIdentifier( 20 | 'cdaToken', 21 | docData, 22 | 'contentful' 23 | ); 24 | 25 | if ( spaceId.trim() == '' || cdaToken.trim() == '' ) { 26 | return context.api().message( 27 | 'Please define Space Id and Content Delivery API token in the settings' 28 | ); 29 | } 30 | 31 | var fieldLayers = page.children().forEach( function( kid ) { 32 | var connectedEntry = kid.name().match( /{entry:(.+?)}/ ); 33 | 34 | if ( connectedEntry ) { 35 | var data = fetchJSON( 36 | 'https://cdn.contentful.com/spaces/' + spaceId + '/entries?access_token=' + cdaToken + '&sys.id=' + connectedEntry[ 1 ] 37 | ).items[ 0 ]; 38 | 39 | kid.children().forEach( function( layer ) { 40 | var connectedField = layer.name().match( /{fields:(.+?)}/ ); 41 | 42 | if ( connectedField ) { 43 | if ( /^ **Contentful** -> **Open Settings**. 14 | 15 | ## Connect TextLayers to Contentful 16 | 17 | To connect your layers with Contentful you have to: 18 | 19 | - group all layers that belong to one entry 20 | - include entry identifier in name of this group `Your layer {entry:ENTRY_ID}` -> `Your layer {entry:fhdjasklfh7e2q90rfjweq9f8jeq9}` 21 | - all TextLayers inside of this group can now be connected to fields 22 | - include field itendifier in name of the Text layer `My title {fields:FIELD_NAME}` -> `My title {fields:title}` 23 | 24 | A the layers of a document could be as follows to make it work. 25 | 26 | ``` 27 | Author ${entry:fjsdaklfsjf8239qr} // a Group 28 | |__ Title ${fields:title} // a TextLayer 29 | |__ Description ${fields:description} // a TextLayer 30 | ``` 31 | 32 | Now you're able to fetch the recent data from Contentful and populate the data. To do so go to **Plugins** -> **Contentful** -> **Fetch data**. 33 | 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sketch-contentful", 3 | "version": "1.0.0", 4 | "description": "A Sketch App plugin to populate your documents with data stored in Contentful.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "build": "webpack" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/stefanjudis/sketch-contentful.git" 13 | }, 14 | "keywords": [ 15 | "sketch", 16 | "plugin", 17 | "contentful" 18 | ], 19 | "author": "stefan judis ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/stefanjudis/sketch-contentful/issues" 23 | }, 24 | "homepage": "https://github.com/stefanjudis/sketch-contentful#readme", 25 | "devDependencies": {} 26 | } 27 | -------------------------------------------------------------------------------- /sketch-contentful.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stefanjudis/sketch-contentful/96443402e0db7c54b70fdfc9efcc5ea9b7c5109d/sketch-contentful.gif --------------------------------------------------------------------------------