-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.coffee
More file actions
executable file
·177 lines (122 loc) · 5.11 KB
/
main.coffee
File metadata and controls
executable file
·177 lines (122 loc) · 5.11 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
# Deps
{css, utils} = require 'octopus-helpers'
{_} = utils
# Private fns
_declaration = ($, property, value, modifier) ->
return if not value? or value == ''
value = modifier(value) if modifier
$ "#{property}: #{value};"
_comment = ($, showComments, text) ->
return unless showComments
$ "/* #{text} */"
defineVariable = (name, value, options) ->
# TODO: add :root selector when selectorOptions is enabled
"--#{name}: #{value};"
renderVariable = (name) ->
"var(--#{name})"
renderColor = (color, colorVariable, colorType) ->
if color.a < 1
css.colorFormat(color, colorType)
else
renderVariable(colorVariable)
_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))
class CSS
render: ($) ->
$$ = $.indents
declaration = _.partial(_declaration, $$)
comment = _.partial(_comment, $, @options.showComments)
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)
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
declareDimensions(declaration, @bounds, unit)
declaration('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
declareDimensions(declaration, @bounds, unit)
endSelector()
$.newline()
else
comment("Style for \"#{utils.trim(@name)}\"")
startSelector(@name)
if @options.showAbsolutePositions
declareAbsolutePosition(declaration, @bounds, unit)
if @bounds
width = boxModelDimension(@bounds.width)
height = boxModelDimension(@bounds.height)
declareDimensions(declaration, { width, height }, unit)
declaration('opacity', @opacity)
if @background
declaration('background-color', @background.color, convertColor)
if @background.gradient
declaration('background-image', css.convertGradients(convertColor, {gradient: @background.gradient, @bounds}))
if @borders
border = @borders[0]
declaration('border', "#{unit(border.width)} #{border.style} #{convertColor(border.color)}")
declaration('border-radius', @radius, _.partial(css.radius, unit))
if @shadows
declaration('box-shadow', css.convertShadows(convertColor, unit, @shadows))
endSelector()
metadata = require './package.json'
module.exports = {defineVariable, renderVariable, setNumberValue, renderClass: CSS, metadata}