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

add support for document color #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Prism.sketchplugin/Contents/Sketch/Prism.cocoascript
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@import "build/Formats.js";
@import "build/Helpers.js";
@import "build/ColorFormatter.js";
@import "build/ColorAssetRepository.js";
@import "build/Base.js";
@import "build/Cell.js";
@import "build/Palette.js";
Expand Down
11 changes: 10 additions & 1 deletion Prism.sketchplugin/Contents/Sketch/build/Main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 21 additions & 16 deletions Prism.sketchplugin/Contents/Sketch/build/Palette.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Prism.sketchplugin/Contents/Sketch/src/ColorAssetRepository.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
###
ColorAssetRepository:

###
class ColorAssetRepository

nameBasedMap: {}
colorBasedMap: {}

constructor: (document) ->
if document
documentColorAssets = document.documentData().assets().colorAssets().objectEnumerator()
@nameBasedMap = NSMutableDictionary.alloc().init()
@colorBasedMap = NSMutableDictionary.alloc().init()

while colorAsset = documentColorAssets.nextObject()
color = colorAsset.color().immutableModelObject()
name = colorAsset.name()
unless @nameBasedMap.objectForKey(name)
@nameBasedMap.setObject_forKey(colorAsset, name)

unless @colorBasedMap.objectForKey(color)
@colorBasedMap.setObject_forKey(colorAsset, color)

colorAssetByName: (name) ->
@nameBasedMap.objectForKey(name)

colorAssetByColor: (color) ->
@colorBasedMap.objectForKey(color)
14 changes: 14 additions & 0 deletions Prism.sketchplugin/Contents/Sketch/src/Main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,37 @@ generatePalette = (context) ->
colorNameChanged = (context) ->
log "Color Name Changed..."
textLayer = context.actionContext.layer
parentLayer = textLayer.parentGroup()
artboard = textLayer.parentArtboard()
newText = context.actionContext.new # The new value for the changed text layer
oldText = context.actionContext.old # The old value for the changed text layer
pluginID = context.plugin.identifier() # Plugin ID for saving data

#Be sure that text layer has a colorValue and its parent artboard is in fact the Prism Palette.
colorValue = context.command.valueForKey_onLayer_forPluginIdentifier( Cell::TEXT_LAYER_TAG , textLayer, pluginID )
inPalette = context.command.valueForKey_onLayer_forPluginIdentifier( Palette::ARTBOARD_TAG , artboard, pluginID )
return unless colorValue && inPalette

document = require('sketch/dom').getSelectedDocument().sketchObject

#If the new text is empty, remove the alias for that color value, otherwise save the alias
if "#{newText}".trim() != ""
#Save alias
context.command.setValue_forKey_onLayer_forPluginIdentifier(newText, colorValue, artboard, pluginID )

#Rename Document Color
colorAssetRepo = new ColorAssetRepository(document)
colorDict = context.command.valueForKey_onLayer_forPluginIdentifier( Cell::CELL_LAYER_TAG , parentLayer, pluginID )
color = ColorFormatter.dictionaryToColor(colorDict).immutableModelObject()
colorAsset = colorAssetRepo.colorAssetByName("#{oldText}".trim())
colorAsset.name = newText
else
#Remove Alias
context.command.setValue_forKey_onLayer_forPluginIdentifier(null, colorValue, artboard, pluginID )

# Add document to context to propely generate palette
context["document"] = document

#Load Palette and regenerate
palette = new Palette(context,textLayer)
palette.regenerate()
Expand Down
32 changes: 19 additions & 13 deletions Prism.sketchplugin/Contents/Sketch/src/Palette.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ class Palette extends Base

if @context.document
documentColorAssets = @context.document.documentData().assets().colorAssets().objectEnumerator()
colorsArray = NSMutableArray.alloc().init()
while color = documentColorAssets.nextObject()
colorsArray.addObject(color.color())
@colors = colorsArray
colorAssets = NSMutableArray.alloc().init()
while colorAsset = documentColorAssets.nextObject()
colorAssets.addObject(colorAsset)
@colorAssets = colorAssets

regenerate: ->
array = @getColorsDictionaries().map (colorDictionary) ->
ColorFormatter.dictionaryToColor(colorDictionary)
@colors = NSArray.alloc().initWithArray(array)
@generate()

generate: ->
NSApplication.sharedApplication().displayDialog_withTitle("There are no colors on your Document Colors.", "Feed me colors!") if @colors.count() == 0
NSApplication.sharedApplication().displayDialog_withTitle("There are no colors on your Document Colors.", "Feed me colors!") if @colorAssets.count() == 0

#If it wasn't found, then create it.
unless @artboard
Expand All @@ -44,17 +41,21 @@ class Palette extends Base

@artboard.removeAllLayers()

# Generate Document Color Name mapping dictionary
colorAssetRepo = new ColorAssetRepository(@context.document)

# Palette Generation
row = 0
column = 0
for i in [0...@colors.count()]
for i in [0...@colorAssets.count()]
#If in the last column, reset column and increment row...
(column = 0; row++) if column >= @COLORS_PER_ROW
#Cell setup
color = @colors[i]
colorAsset = @colorAssets[i]
color = colorAsset.color()
cell = new Cell(@context)
#If there's an alias defined then use it, otherwise use the color classifier.
name = @aliasForColor(color) ? @colorClassifier.classify(color.immutableModelObject().hexValue())
#If there's named document color then use it, otherwise an alias defined then use it, finally use the color classifier.
name = @documentColorName(colorAsset) ? @aliasForColor(color) ? @colorClassifier.classify(color.immutableModelObject().hexValue())
cell.setColor_withName( color, name )
cell.setX((cell.width + @CELL_SPACING) * column + @CELL_SPACING)
cell.setY((cell.height + @CELL_SPACING) * row + @CELL_SPACING)
Expand All @@ -63,7 +64,7 @@ class Palette extends Base
column++

@artboard.frame().setHeight((cell.height + @CELL_SPACING) * (row+1) + @CELL_SPACING)
@artboard.frame().setWidth((cell.width + @CELL_SPACING) * Math.min(@colors.count(),@COLORS_PER_ROW) + @CELL_SPACING)
@artboard.frame().setWidth((cell.width + @CELL_SPACING) * Math.min(@colorAssets.count(),@COLORS_PER_ROW) + @CELL_SPACING)

#Position Artboard
@artboard.removeFromParent()
Expand Down Expand Up @@ -91,3 +92,8 @@ class Palette extends Base

aliasForColor: (color) ->
@valueForKey_onLayer(color.immutableModelObject().hexValue(),@artboard)

documentColorName: (colorAsset) ->
name = colorAsset.displayName()
if name.length() > 0
return name