-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.coffee
More file actions
208 lines (144 loc) · 6.04 KB
/
main.coffee
File metadata and controls
208 lines (144 loc) · 6.04 KB
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
# Deps
{css, utils} = require 'octopus-helpers'
{_} = utils
# Private fns
_declaration = ($$, lessMixinSyntax, interpolatedSyntax, property, value, modifier) ->
return if not value? or value == ''
value = modifier(value) if modifier
value = "~\"#{value}\"" if interpolatedSyntax and lessMixinSyntax
if lessMixinSyntax
$$ ".#{property}(#{value});"
else
$$ "#{property}: #{value};"
renderColor = (color, colorVariable) ->
if color.a < 1
"fade(#{colorVariable}, #{100 - color.a * 100})"
else
renderVariable(colorVariable)
_comment = ($, showComments, text) ->
return unless showComments
$ "// #{text}"
defineVariable = (name, value, options) ->
"@#{name}: #{value};"
renderVariable = (name) -> "@#{name}"
_convertColor = _.partial(css.convertColor, renderColor)
_startSelector = ($, selector, selectorOptions, text) ->
return unless selector
$ '%s%s', utils.prettySelectors(text, selectorOptions), ' {'
_endSelector = ($, selector) ->
return unless selector
$ '}'
setNumberValue = (number) ->
converted = parseInt(number, 10)
if not number.match(/^\d+(\.\d+)?$/)
return 'Please enter numeric value'
else
return converted
declareAbsolutePosition = (declaration, bounds, unit) ->
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 = (enableLessHat, mixin, declaration, bounds, unit) ->
if enableLessHat
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 Less
render: ($) ->
$$ = $.indents
declaration = _.partial(_declaration, $$, false, false)
mixin = _.partial(_declaration, $$, @options.enableLessHat, @options.useInterpolationSyntax)
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, selectorOptions)
endSelector = _.partial(_endSelector, $, @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, @bounds, unit)
if @bounds
declareSizeMixinOrDimensions(@options.enableLessHat, 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, @bounds, unit)
if @bounds
declareSizeMixinOrDimensions(@options.enableLessHat, mixin, declaration, @bounds, unit)
endSelector()
$.newline()
else
if @options.showComments
comment("Style for \"#{utils.trim(@name)}\"")
startSelector(@name)
if @options.showAbsolutePositions
declaration('position', 'absolute')
declaration('left', @bounds.left, unit)
declaration('top', @bounds.top, unit)
if @bounds
width = boxModelDimension(@bounds.width)
height = boxModelDimension(@bounds.height)
if @options.enableLessHat
if width is height
mixin('size', width, unit)
else
mixin('size', "#{unit(width)}, #{unit(height)}")
else
declaration('width', width, unit)
declaration('height', height, unit)
mixin('opacity', @opacity)
if @background
declaration('background-color', @background.color, convertColor)
if @background.gradient
mixin('background-image', css.convertGradients(convertColor, {gradient: @background.gradient, @bounds}))
if @borders
border = @borders[0]
declaration('border', "#{unit(border.width)} #{border.style} #{convertColor(border.color)}")
mixin('border-radius', @radius, _.partial(css.radius, unit))
if @shadows
mixin('box-shadow', css.convertShadows(convertColor, unit, @shadows))
endSelector()
metadata = require './package.json'
module.exports = {defineVariable, renderVariable, setNumberValue, renderClass: Less, metadata}