|
| 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} |
0 commit comments