Skip to content

Commit

Permalink
[Feat] Google Fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
thize committed Jun 12, 2020
1 parent 5978b6d commit a7a183c
Show file tree
Hide file tree
Showing 8 changed files with 951 additions and 24 deletions.
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function generateArtboards(artboards) {
}

function generateSelection(items) {
const dartCode = formatDart(itemsToDart(items, true) + ";");
const dartCode = itemsToDart(items, true);
clipboard.copyText(dartCode);
}

14 changes: 8 additions & 6 deletions src/items_to_dart.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ const { Children } = require("./widgets/children");
const { ArtboardWidget } = require("./widgets/artboard");
const { wrapWithInkWell, wrapWithRotation } = require("./widgets/util/widgets_util");
const { ComponentWidget } = require("./widgets/component");
const { formatDart } = require("./widgets/util/format_dart");

function itemsToDart(items, withSimpleCode = false) {
function itemsToDart(items, isFirst = false) {
const ungroupedItems = removeItemsFromGroup(items);
const widgets = generateWidgetsFromItems(ungroupedItems);
const tree = new Tree(widgets, withSimpleCode);
const tree = new Tree(widgets, isFirst);
return tree.toDart();
}

Expand All @@ -37,19 +38,20 @@ class Tree {
/**
* @param {[any]} widgets list of all selection widgets
*/
constructor(widgets, withSimpleCode) {
constructor(widgets, isFirst) {
this.node = new Node(widgets[0]);
for (let i = 1; i < widgets.length; i++) {
const widget = widgets[i];
this.node = this.insertNodeIn(new Node(widget), this.node);
}
this.withSimpleCode = withSimpleCode;
this.isFirst = isFirst;
// console.log(this.node.debug(0));
}

toDart() {
const widget = this.node.toDart();
if (this.withSimpleCode) {
let widget = this.node.toDart();
if (this.isFirst) {
widget = formatDart(widget + ';');
return putSimpleCode(widget);
}
return widget;
Expand Down
20 changes: 10 additions & 10 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ function isSvgFolder(item) {
let onlySvg = true;
item.children.forEach(child => {
if (onlySvg) {
if (child instanceof Group) {
onlySvg = isSvgFolder(child);
} else {
const isPath = child instanceof Path;
const isPolygon = child instanceof Polygon;
const isBooleanGroup = child instanceof BooleanGroup;
const isSvgLine = _isSvgLine(child);
onlySvg = (isPath || isPolygon || isBooleanGroup || isSvgLine);
}
// if (child instanceof Group) {
// onlySvg = isSvgFolder(child);
// } else {
const isPath = child instanceof Path;
const isPolygon = child instanceof Polygon;
const isBooleanGroup = child instanceof BooleanGroup;
const isSvgLine = _isSvgLine(child);
onlySvg = (isPath || isPolygon || isBooleanGroup || isSvgLine);
// }
}
});
return onlySvg;
Expand Down Expand Up @@ -179,7 +179,7 @@ exports.putSimpleCode = putSimpleCode;

function _applySCRegexWithTag(str, regex, tag, method) {
if (method)
return str.replace(new RegExp(method + '(.*)', 'g'), (value) => {
return str.replace(new RegExp(method + '(.*)', 'mig'), (value) => {
value = value.replace(new RegExp(regex, 'g'), (number) => {
if (number == 0) return number;
return `wsz(` + number + ')';
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ComponentWidget {
const master = findMasterForSymbolId(this.xdNode.symbolId);
const componentName = !master ? this.xdNode.name : master.name;
let dartComponent = itemsToDart(this.xdNode.children);
dartComponent = wrapWithInkWell(this.xdNode, dartComponent);
dartComponent = wrapWithInkWell(this.xdNode, dartComponent) + ';';
return new StatelessWidget(componentName, dartComponent).toDart();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/stateless.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StatelessWidget {
@override
Widget build(BuildContext context) {
return ${this.child};
return ${this.child}
}
}`;
}
Expand Down
26 changes: 21 additions & 5 deletions src/widgets/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Bounds } = require("../bounds");
const { Parameter, ParameterRef } = require("./util/parameter");
const { getColor } = require("./util/color");
const { titleCase } = require("./util/widgets_util");
const { googleFonts } = require("./util/google_fonts");

class TextWidget {
constructor(xdNode) {
Expand Down Expand Up @@ -45,7 +46,7 @@ function checkForUnsupportedFeatures(o) {
if (o.paragraphSpacing) {
console.log('Paragraph spacing is not currently supported.');
}
if (o.strokeEnabled && node.stroke) {
if (o.strokeEnabled && o.stroke) {
console.log('Text border is not currently supported.');
}
}
Expand All @@ -65,7 +66,7 @@ function _getText(xdNode, params) {
textParam = _textTransformation(textParam, xdNode);
return 'Text('
+ `${textParam},` +
_getStyleParam(_getTextStyleParamList(xdNode, null, params)) +
_getStyleParam(xdNode, _getTextStyleParamList(xdNode, null, params)) +
_getTextAlignParam(xdNode) +
')';
}
Expand Down Expand Up @@ -94,7 +95,7 @@ function _getTextRich(xdNode, params) {
// Export a rich text object with an empty root span setting a default style.
// Child spans set their style as a delta of the default.
return 'Text.rich(TextSpan(' +
' ' + _getStyleParam(defaultStyleParams) +
' ' + _getStyleParam(xdNode, defaultStyleParams) +
` children: [${str}],` +
`), ${_getTextAlignParam(xdNode)})`;

Expand All @@ -105,7 +106,7 @@ function _getTextSpan(params, text, xdNode) {
text = _textTransformation(text, xdNode);
return 'TextSpan(' +
` text: '${text}',` +
_getStyleParam(params) +
_getStyleParam(xdNode, params) +
')';
}

Expand Down Expand Up @@ -133,17 +134,32 @@ function _getTextStyleParamList(xdNode, styleRange, params, isDefault = false) {
];
}

function _getStyleParam(params) {
function _getStyleParam(xdNode, params) {
if (!params) { return ''; }
let str = getParamList(params);
const family = _getFontFamilyName(xdNode);
if (googleFonts.includes(family)) {
return (!str ? '' : `style: GoogleFonts.${family}(${str}),`);
}
return (!str ? '' : `style: TextStyle(${str}), `);
}

function _getFontFamilyName(node) {
let family = node.fontFamily.replace(/\s+/g, '');
family = family[0].toLowerCase() + family.substring(1, family.length);
if (googleFonts.includes(family)) {
return family;
}
return node.fontFamily;
}

function _getFontFamily(o) {
return o.fontFamily;
}

function _getFontFamilyParam(o) {
const family = _getFontFamilyName(o);
if (googleFonts.includes(family)) return '';
return `fontFamily: '${_getFontFamily(o)}', `;
}

Expand Down
Loading

0 comments on commit a7a183c

Please sign in to comment.