-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.coffee
236 lines (166 loc) · 6.94 KB
/
main.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Deps
{css, utils} = require 'octopus-helpers'
{_} = utils
# Private fns
_declaration = ($$, scssSyntax, property, value, modifier) ->
return if not value? or value == ''
if scssSyntax
semicolon = ';'
else
semicolon = ''
if modifier
value = modifier(value)
$$ "#{property}: #{value}#{semicolon}"
_mixin = ($$, scssSyntax, name, value, modifier) ->
return unless value?
if scssSyntax
include = '@include '
semicolon = ';'
else
include = '+'
semicolon = ''
if modifier
value = modifier(value)
$$ "#{include}#{name}(#{value})#{semicolon}"
renderColor = (color, colorVariable) ->
colorVariable = renderVariable(colorVariable)
if color.a < 1
"rgba(#{colorVariable}, #{color.a})"
else
colorVariable
_comment = ($, showComments, text) ->
return unless showComments
$ "// #{text}"
defineVariable = (name, value, options = {}) ->
semicolon = if options.scssSyntax then ';' else ''
"$#{name}: #{value}#{semicolon}"
renderVariable = (name) -> "$#{name}"
_convertColor = _.partial(css.convertColor, renderColor)
_startSelector = ($, selector, scssSyntax, selectorOptions, text) ->
return unless selector
curlyBracket = if scssSyntax then ' {' else ''
$ '%s%s', utils.prettySelectors(text, selectorOptions), curlyBracket
_endSelector = ($, scssSyntax, selector) ->
$ '}' if selector and scssSyntax
setNumberValue = (number) ->
converted = parseInt(number, 10)
if not number.match(/^\d+(\.\d+)?$/)
return 'Please enter numeric value'
else
return converted
declareAbsolutePosition = (declaration, mixin, mixinLibrary, bounds, unit) ->
if mixinLibrary is 'Bourbon'
mixin('position', "absolute, #{unit(bounds.top)} 0 0 #{unit(bounds.left)}")
else
declaration('position', 'absolute')
declaration('left', bounds.left, unit)
declaration('top', bounds.top, unit)
declareDimensions = (declaration, bounds, unit) ->
declaration('width', unit(bounds.width))
declaration('height', unit(bounds.height))
declareSizeMixinOrDimensions = (mixinLibrary, mixin, declaration, bounds, unit) ->
if mixinLibrary is 'Bourbon'
if bounds.width == bounds.height
mixin('size', bounds.width, unit)
else
mixin('size', "#{unit(bounds.width)}, #{unit(bounds.height)}")
else
declareDimensions(declaration, bounds, unit)
class Sass
render: ($) ->
$$ = $.indents
declaration = _.partial(_declaration, $$, @options.scssSyntax)
mixin = _.partial(_mixin, $$, @options.scssSyntax)
comment = _.partial(_comment, $, @options)
boxModelDimension = _.partial(css.boxModelDimension, @options.boxSizing, if @borders then @borders[0].width else null)
rootValue = switch @options.unit
when 'px' then 0
when 'em' then @options.emValue
when 'rem' then @options.remValue
unit = _.partial(css.unit, @options.unit, rootValue)
lhRoot = switch @options.lineHeightUnit
when 'px' then 0
when 'em' then @options.emValue
when 'rem' then @options.remValue
lineHeightUnit = _.partial(css.lineHeightUnit, @options.lineHeightUnit, unit, lhRoot)
isUnitlessLh = @options.lineHeightUnit.toLowerCase().indexOf('unitless') isnt -1
convertColor = _.partial(_convertColor, @options)
fontStyles = _.partial(css.fontStyles, declaration, convertColor, unit, lineHeightUnit, isUnitlessLh, @options.quoteType)
selectorOptions =
separator: @options.selectorTextStyle
selector: @options.selectorType
maxWords: 3
fallbackSelectorPrefix: 'layer'
startSelector = _.partial(_startSelector, $, @options.selector, @options.scssSyntax, selectorOptions)
endSelector = _.partial(_endSelector, $, @options.scssSyntax, @options.selector)
if @type == 'textLayer'
if @baseTextStyle and @textStyles
fontName = @baseTextStyle.font?.name
if fontName
@baseTextStyle.font.name = fontName.replace(/([A-Z])/g, ' $1').trim()
for textStyle in css.prepareTextStyles(@options.inheritFontStyles, @baseTextStyle, @textStyles)
if @options.showComments
comment(css.textSnippet(@text, textStyle))
if @options.selector
if textStyle.ranges?[0]
selectorText = utils.textFromRange(@text, textStyle.ranges[0])
else
selectorText = @name
startSelector(selectorText)
if not @options.inheritFontStyles or textStyle.base
if @options.showAbsolutePositions
declareAbsolutePosition(declaration, mixin, @options.mixinLibrary, @bounds, unit)
if @bounds
declareSizeMixinOrDimensions(@options.mixinLibrary, mixin, declaration, @bounds, unit)
mixin('opacity', @opacity)
if @shadows
declaration('text-shadow', css.convertTextShadows(convertColor, unit, @shadows))
fontStyles(textStyle)
endSelector()
else
startSelector(@name)
comment('Text dimensions')
if @options.showAbsolutePositions
declareAbsolutePosition(declaration, mixin, @options.mixinLibrary, @bounds, unit)
if @bounds
declareSizeMixinOrDimensions(@options.mixinLibrary, mixin, declaration, @bounds, unit)
endSelector()
$.newline()
else
if @options.showComments
comment("Style for \"#{utils.trim(@name)}\"")
startSelector(@name)
if @options.showAbsolutePositions
declareAbsolutePosition(declaration, mixin, @options.mixinLibrary, @bounds, unit)
if @bounds
width = boxModelDimension(@bounds.width)
height = boxModelDimension(@bounds.height)
declareSizeMixinOrDimensions(@options.mixinLibrary, mixin, declaration, { width, height }, unit)
if @options.mixinLibrary is 'Compass'
mixin('opacity', @opacity)
else
declaration('opacity', @opacity)
if @background
declaration('background-color', @background.color, convertColor)
if @background.gradient
gradientStr = css.convertGradients(convertColor, {gradient: @background.gradient, @bounds})
if gradientStr
if @options.mixinLibrary is 'none'
declaration('background-image', gradientStr)
else
mixin('background-image', gradientStr)
if @borders
border = @borders[0]
declaration('border', "#{unit(border.width)} #{border.style} #{convertColor(border.color)}")
if @options.mixinLibrary is 'Compass'
mixin('border-radius', @radius, _.partial(css.radius, unit))
else
declaration('border-radius', @radius, _.partial(css.radius, unit))
if @shadows
if @options.mixinLibrary is 'Compass'
mixin('box-shadow', css.convertShadows(convertColor, unit, @shadows))
else
declaration('box-shadow', css.convertShadows(convertColor, unit, @shadows))
endSelector()
metadata = require './package.json'
module.exports = {defineVariable, renderVariable, setNumberValue, renderClass: Sass, metadata}