Skip to content

Commit 68db7e3

Browse files
committed
Merge branch 'dev'
2 parents 36a4314 + b30d51a commit 68db7e3

3 files changed

Lines changed: 258 additions & 16 deletions

File tree

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 CSS Hat
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

main.coffee

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Deps
2+
3+
autoprefixer = require('autoprefixer-core')
4+
{css, utils} = require 'octopus-helpers'
5+
{_} = utils
6+
7+
8+
# Private fns
9+
10+
_declaration = ($, vendorPrefixes, prefixer, property, value, modifier) ->
11+
return if not value? or value == ''
12+
value = modifier(value) if modifier
13+
return prefixer(property, value) if vendorPrefixes
14+
$ "#{property}: #{value};"
15+
16+
17+
_comment = ($, showComments, text) ->
18+
return unless showComments
19+
$ "/* #{text} */"
20+
21+
22+
defineVariable = (name, value, options) ->
23+
# TODO: add :root selector when selectorOptions is enabled
24+
"--#{name}: #{value};"
25+
26+
27+
renderVariable = (name) ->
28+
"var(--#{name})"
29+
30+
renderColor = (color, colorVariable, colorType) ->
31+
if color.a < 1
32+
css.colorFormat(color, colorType)
33+
else
34+
renderVariable(colorVariable)
35+
36+
_convertColor = _.partial(css.convertColor, renderColor)
37+
38+
_startSelector = ($, selector, selectorOptions, text) ->
39+
return unless selector
40+
$ '%s%s', utils.prettySelectors(text, selectorOptions), ' {'
41+
42+
43+
_endSelector = ($, selector) ->
44+
return unless selector
45+
$ '}'
46+
47+
48+
prefixer = null
49+
setAutoprefixer = (prefixOptions = '> 1%, last 2 versions, Firefox ESR, Opera 12.1') ->
50+
options = prefixOptions.split(',').map (val) -> val.trim()
51+
52+
try
53+
prefixer = autoprefixer({browsers: options})
54+
catch e
55+
'Parse error – try to check the syntax'
56+
57+
setNumberValue = (number) ->
58+
converted = parseInt(number, 10)
59+
if not number.match(/^\d+(\.\d+)?$/)
60+
return 'Please enter numeric value'
61+
else
62+
return converted
63+
64+
65+
_prefixed = ($, property, value) ->
66+
setAutoprefixer() unless prefixer
67+
68+
output = "#{property}: #{value}"
69+
prefixed = prefixer.process(output)
70+
71+
children = prefixed.root.childs
72+
$ "#{child.prop}: #{child.value};" for child in children
73+
74+
75+
class CSS
76+
77+
render: ($) ->
78+
$$ = $.indents
79+
prefixed = _.partial(_prefixed, $$)
80+
declaration = _.partial(_declaration, $$, @options.vendorPrefixes, prefixed)
81+
comment = _.partial(_comment, $, @options.showComments)
82+
83+
rootValue = switch @options.unit
84+
when 'px' then 0
85+
when 'em' then @options.emValue
86+
when 'rem' then @options.remValue
87+
unit = _.partial(css.unit, @options.unit, rootValue)
88+
89+
convertColor = _.partial(_convertColor, @options)
90+
fontStyles = _.partial(css.fontStyles, declaration, convertColor, unit, @options.quoteType)
91+
92+
selectorOptions =
93+
separator: @options.selectorTextStyle
94+
selector: @options.selectorType
95+
maxWords: 3
96+
fallbackSelectorPrefix: 'layer'
97+
startSelector = _.partial(_startSelector, $, @options.selector, selectorOptions)
98+
endSelector = _.partial(_endSelector, $, @options.selector)
99+
100+
if @type == 'textLayer'
101+
for textStyle in css.prepareTextStyles(@options.inheritFontStyles, @baseTextStyle, @textStyles)
102+
103+
comment(css.textSnippet(@text, textStyle))
104+
105+
if @options.selector
106+
if textStyle.ranges
107+
selectorText = utils.textFromRange(@text, textStyle.ranges[0])
108+
else
109+
selectorText = @name
110+
111+
startSelector(selectorText)
112+
113+
if not @options.inheritFontStyles or textStyle.base
114+
if @options.showAbsolutePositions
115+
declaration('position', 'absolute')
116+
declaration('left', @bounds.left, unit)
117+
declaration('top', @bounds.top, unit)
118+
119+
if @bounds
120+
declaration('width', unit(@bounds.width))
121+
declaration('height', unit(@bounds.height))
122+
123+
declaration('opacity', @opacity)
124+
125+
if @shadows
126+
declaration('text-shadow', css.convertTextShadows(convertColor, unit, @shadows))
127+
128+
fontStyles(textStyle)
129+
130+
endSelector()
131+
$.newline()
132+
else
133+
comment("Style for \"#{utils.trim(@name)}\"")
134+
startSelector(@name)
135+
136+
if @options.showAbsolutePositions
137+
declaration('position', 'absolute')
138+
declaration('left', @bounds.left, unit)
139+
declaration('top', @bounds.top, unit)
140+
141+
if @bounds
142+
declaration('width', unit(@bounds.width))
143+
declaration('height', unit(@bounds.height))
144+
145+
declaration('opacity', @opacity)
146+
147+
if @background
148+
declaration('background-color', @background.color, convertColor)
149+
150+
if @background.gradient
151+
declaration('background-image', css.convertGradients(convertColor, {gradient: @background.gradient, @bounds}))
152+
153+
if @borders
154+
border = @borders[0]
155+
declaration('border', "#{unit(border.width)} #{border.style} #{convertColor(border.color)}")
156+
157+
declaration('border-radius', @radius, css.radius)
158+
159+
if @shadows
160+
declaration('box-shadow', css.convertShadows(convertColor, unit, @shadows))
161+
162+
endSelector()
163+
164+
165+
module.exports = {defineVariable, renderVariable, setAutoprefixer, setNumberValue, renderClass: CSS}

package.json

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "csshat-language-css",
33
"title": "CSS",
4-
"version": "1.2.4",
4+
"version": "2.1.1",
55
"description": "CSS language",
6-
"main": "dust.js",
6+
"main": "main",
77
"scope": "source.css",
8+
"codeLanguage": true,
89
"engines": {
910
"csshat": "~2.0.0",
1011
"avocode": "*"
@@ -20,33 +21,75 @@
2021
"author": "Tomas Hanacek <tomashanacek@abdoc.net> (https://github.com/tomashanacek)",
2122
"contributors": [
2223
"Marek Hrabe <marekhrabe@abdoc.net> (https://github.com/marekhrabe)",
23-
"Petr Brzek <petrbrzek@abdoc.net> (https://github.com/petrbrzek)"
24+
"Petr Brzek <petrbrzek@abdoc.net> (https://github.com/petrbrzek)",
25+
"Patrik Holcak <patrikholcak@abdoc.net> (https://github.com/patrikholcak)"
2426
],
2527
"license": "MIT",
2628
"dependencies": {
27-
"octopus-helpers": "^0.2.9"
29+
"autoprefixer-core": "^4.0.2",
30+
"octopus-helpers": "^1.5.0"
2831
},
2932
"settings": {
30-
"vendorPrefixes": {
33+
"inheritFontStyles": {
34+
"description": "Group common styles for selected layers.",
3135
"type": "boolean",
3236
"default": true
3337
},
34-
"inheritFontStyles": {
38+
"showComments": {
39+
"description": "Show a brief description of selected layers.",
3540
"type": "boolean",
3641
"default": true
3742
},
38-
"selector": {
43+
"showAbsolutePositions": {
44+
"description": "Show absolute X and Y coordinates for all layers.",
3945
"type": "boolean",
4046
"default": false
4147
},
42-
"selectorTextStyle": {
43-
"type": "hidden",
48+
"useColorName": {
49+
"description": "Show color name values of common colors rather than hex codes.",
50+
"type": "boolean",
51+
"default": true
52+
},
53+
"vendorPrefixes": {
54+
"description": "Generate vendor prefixes like `display: -webkit-flex;`",
55+
"type": "boolean",
56+
"default": true
57+
},
58+
"unit": {
59+
"type": "select",
4460
"options": [
45-
"dash-case",
46-
"camelCase",
47-
"snake_case"
61+
"px",
62+
"em",
63+
"rem"
4864
],
49-
"default": "dash-case"
65+
"default": "px"
66+
},
67+
"emValue": {
68+
"description": "1 em = x px — Ammount of pixels that correspond to one em. Em value is **not** relative to parent element.",
69+
"type": "text",
70+
"default": 16,
71+
"validate": "setNumberValue",
72+
"showFor": "unit",
73+
"showWhen": "em"
74+
},
75+
"remValue": {
76+
"description": "1 rem = x px — Ammount of pixels that correspond to one rem",
77+
"type": "text",
78+
"default": 16,
79+
"validate": "setNumberValue",
80+
"showFor": "unit",
81+
"showWhen": "rem"
82+
},
83+
"autoprefixer": {
84+
"description": "Defines which vendor prefixes will be generated. Read more on [Github](https://github.com/postcss/autoprefixer).",
85+
"type": "text",
86+
"validate": "setAutoprefixer",
87+
"default": "> 1%, last 2 versions, Firefox ESR, Opera 12.1"
88+
},
89+
"selector": {
90+
"description": "Wrap code in a css selector.",
91+
"type": "boolean",
92+
"default": false
5093
},
5194
"selectorType": {
5295
"type": "select",
@@ -57,6 +100,15 @@
57100
],
58101
"default": "class"
59102
},
103+
"selectorTextStyle": {
104+
"type": "select",
105+
"options": [
106+
"dash-case",
107+
"snake_case",
108+
"camelCase"
109+
],
110+
"default": "dash-case"
111+
},
60112
"colorType": {
61113
"type": "select",
62114
"options": [
@@ -66,9 +118,13 @@
66118
],
67119
"default": "hex"
68120
},
69-
"showTextSnippet": {
70-
"type": "boolean",
71-
"default": true
121+
"quoteType": {
122+
"type": "select",
123+
"options": [
124+
"'",
125+
"\""
126+
],
127+
"default": "\""
72128
}
73129
},
74130
"bundledDependencies": [

0 commit comments

Comments
 (0)