I'm trying to create a small plugin that parses a paragraph from sketch, and converts it into segments that I can then use in a calling application. I need to be able to distinguish bold, italics etc, and also underlines, but I can't find any of these on the text layer's style, or elsewhere on the object.
For example, given the string: 'The quick brown fox jumps over the lazy dog', it would need to return out of that an array of javascript objects, in this case three (I can't make the third segment underlined in this editor, but in my example it would be in sketch:
[{text: "The ", font:regular, colour:white },
{text: "quick brown fox", font:bold, colour:white },
{text: " jumps over the lazy dog", font:regular, colour:white, decoration: underline}
]
So far this is what I have:
var sketch = require('sketch')
var document = sketch.getSelectedDocument()
var selectedLayers = document.selectedLayers
var selectedCount = selectedLayers.length
if (selectedCount === 0) {
console.log("There are no segments selected");
} else {
console.log('Selected layers:');
selectedLayers.forEach(function (layer, i) {
var conversionObject = {
"ix": i,
"name": layer.name,
"text": layer.text,
"colour" : layer.style.textColor
}
//TODO: Push object into array
//console.log(layer.text.type)
console.log(layer.style.textColor);
})
}
Am I going about this the wrong way, is there a better way to get each individual word from the layer rather than viewing it as a whole?