Skip to content

Commit

Permalink
fix(layer): Combine p tags with the same classes
Browse files Browse the repository at this point in the history
  • Loading branch information
morrislaptop committed May 18, 2018
1 parent cfdc28c commit 368e0f2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,26 @@ function shapeLayerToCode(tailwind, context, layer) {
return classesToCode(tailwind.screens, 'div', classes.filter(n => n))
}

function textLayerToCode(tailwind, context, layer) {
let tags = layer.textStyles.map(style => textStyleToCode(tailwind, context, layer, style))
function combineTextLayersWithTheSameClasses(tailwind, context, textStyles)
{
return Object.values(textStyles.reduce((obj, style) => {
let classes = fontAndTextClasses(tailwind, context, style.textStyle).join(' ')

if (obj[classes]) {
obj[classes].range.end = style.range.end
}
else {
obj[classes] = style
}

return obj
}, {}))
}

function textLayerToCode(tailwind, context, layer) {
let textStyles = combineTextLayersWithTheSameClasses(tailwind, context, layer.textStyles)

let tags = textStyles.map(style => textStyleToCode(tailwind, context, layer, style))

return tags.join("\n")
}
Expand Down
6 changes: 6 additions & 0 deletions test/layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ let tests = {
<p class="sample-text-style-with-color uppercase">RED</p>`)
},

TextLayerWithMultipleStylesThatAreTheSame(context, layer) {
let css = ext.layer(context, layer)

expect(css.code).toBe(`<p class="text-xl font-medium">Type something RED</p>`)
},

TextLayer(context, layer) {
let css = ext.layer(context, layer)

Expand Down
88 changes: 88 additions & 0 deletions test/sample-data/layers.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,94 @@
],
"assets": []
},
{
"type": "text",
"name": "Text layer with multiple styles that are the same",
"exportable": false,
"rotation": 0,
"opacity": 1,
"blendMode": "normal",
"borderRadius": 0,
"rect": {
"y": 44,
"x": 0,
"width": 220,
"height": 24
},
"content": "Type something RED",
"borders": [],
"fills": [],
"shadows": [],
"textStyles": [
{
"range": {
"location": 0,
"length": 4
},
"style": {
"fontFace": "SFProText-Medium",
"fontSize": 20,
"fontWeight": 500,
"fontStyle": "normal",
"fontFamily": "SFProText",
"fontStretch": "normal",
"textAlign": "left",
"weightText": "medium",
"color": {
"r": 0,
"g": 0,
"b": 0,
"a": 1
}
}
},
{
"range": {
"location": 5,
"length": 9
},
"style": {
"fontFace": "SFProText-Medium",
"fontSize": 20,
"fontWeight": 500,
"fontStyle": "normal",
"fontFamily": "SFProText",
"fontStretch": "normal",
"textAlign": "left",
"weightText": "medium",
"color": {
"r": 0,
"g": 0,
"b": 0,
"a": 1
}
}
},
{
"range": {
"location": 15,
"length": 3
},
"style": {
"fontFace": "SFProText-Medium",
"fontSize": 20,
"fontWeight": 500,
"fontStyle": "normal",
"fontFamily": "SFProText",
"fontStretch": "normal",
"textAlign": "left",
"weightText": "medium",
"color": {
"r": 0,
"g": 0,
"b": 0,
"a": 1
}
}
}
],
"assets": []
},
{
"type": "text",
"name": "Text layer",
Expand Down

0 comments on commit 368e0f2

Please # to comment.