Skip to content

Commit

Permalink
[Feat] SimpleCode
Browse files Browse the repository at this point in the history
  • Loading branch information
thize committed Jun 11, 2020
1 parent e820846 commit 5978b6d
Show file tree
Hide file tree
Showing 18 changed files with 478 additions and 78 deletions.
7 changes: 5 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const { formatDart } = require("./src/widgets/util/format_dart");
const { ComponentWidget } = require("./src/widgets/component");
const { listToString } = require("./src/util");

//TODO:
// * console.log change to own console

function onTapGenerate() {
const items = scenegraph.selection.items;
const hasSelection = items.length > 0;
Expand Down Expand Up @@ -44,14 +47,14 @@ function generateComponents(components) {
function generateArtboards(artboards) {
const artboardsWidget = [];
artboards.forEach(artboard => {
const dartCode = new StatelessWidget(artboard.name, itemsToDart([artboard])).toDart();
const dartCode = new StatelessWidget(artboard.name, itemsToDart([artboard], true)).toDart();
artboardsWidget.push(dartCode);
});
clipboard.copyText(formatDart(listToString(artboardsWidget)));
}

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

1 change: 0 additions & 1 deletion src/icon/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { Rectangle, Color, Group } = require("scenegraph");
//TODO:
/*
* getFolder() return correct
* console.log change to own console
* exportAndroidAdaptiveIcons
*/

Expand Down
26 changes: 20 additions & 6 deletions src/items_to_dart.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
const { xdItemToWidget, widgetCanHaveChild, removeItemsFromGroup, fixAllNumbers } = require("./util");
/*
Copyright 2020 Adobe
All Rights Reserved.
NOTICE: Adobe permits you to use, modify, and distribute this file in
accordance with the terms of the Adobe license agreement accompanying
it. If you have received this file from a source other than Adobe,
then your use, modification, or distribution of it requires the prior
written permission of Adobe.
*/

const { xdItemToWidget, widgetCanHaveChild, removeItemsFromGroup, fixAllNumbers, putSimpleCode } = require("./util");
const { Bounds } = require("./bounds");
const { Children } = require("./widgets/children");
const { ArtboardWidget } = require("./widgets/artboard");
const { wrapWithInkWell, wrapWithRotation } = require("./widgets/util/widgets_util");
const { ComponentWidget } = require("./widgets/component");

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

Expand All @@ -27,17 +37,21 @@ class Tree {
/**
* @param {[any]} widgets list of all selection widgets
*/
constructor(widgets) {
constructor(widgets, withSimpleCode) {
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;
// console.log(this.node.debug(0));
}

toDart() {
const widget = this.node.toDart();
if (this.withSimpleCode) {
return putSimpleCode(widget);
}
return widget;
}

Expand Down Expand Up @@ -246,7 +260,7 @@ class Node {
constructor(widget, father, type) {
this.widget = widget;
this.father = father;
this.type = type == null ? widget.XdNode.name : type;
this.type = type == null ? widget.xdNode.name : type;
this.children = [];
this.bounds;
this.updateBounds();
Expand Down Expand Up @@ -287,7 +301,7 @@ class Node {
if (this.widget instanceof ComponentWidget) {
return dartWidget;
}
return fixAllNumbers(wrapWithRotation(this, wrapWithInkWell(this.widget.XdNode, dartWidget)));
return fixAllNumbers(wrapWithRotation(this, wrapWithInkWell(this.widget.xdNode, dartWidget)));
}

debug(depth) {
Expand Down
49 changes: 46 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { SvgWidget } = require("./widgets/svg");
const { TextWidget } = require("./widgets/text");
const { MaskWidget } = require("./widgets/mask");

function fix(num, digits = 1) {
function fix(num, digits = 2) {
let p = Math.pow(10, digits);
num = Math.round(num * p) / p;
return num + (num === (num | 0) ? '.0' : '');
Expand Down Expand Up @@ -139,13 +139,56 @@ function listToString(list) {

exports.listToString = listToString;

function getOpacity(xdNode) {
let o = xdNode, opacity = 1.0;
while (o) {
if (o.opacity != null) { opacity *= o.opacity; }
o = o.parent;
}
return opacity;
}

exports.getOpacity = getOpacity;

function fixAllNumbers(str) {
const onlyFloatNumbers = `\\-?\\d+\\.\\d+`;
return str;
str = str.replace(new RegExp(onlyFloatNumbers, 'g'), (value) => {
return fix(value);
});
return str;
}

exports.fixAllNumbers = fixAllNumbers;
exports.fixAllNumbers = fixAllNumbers;

function putSimpleCode(str) {
const getNumberRegex = '[0-9]+([\\.][0-9]+)?';
str = _applySCRegexWithTag(str, getNumberRegex, 'width');
str = _applySCRegexWithTag(str, getNumberRegex, 'height');
str = _applySCRegexWithTag(str, getNumberRegex, 'fontSize');
str = _applySCRegexWithTag(str, getNumberRegex, 'blurRadius');
str = _applySCRegexWithTag(str, getNumberRegex, 'right');
str = _applySCRegexWithTag(str, getNumberRegex, 'left');
str = _applySCRegexWithTag(str, getNumberRegex, 'top');
str = _applySCRegexWithTag(str, getNumberRegex, 'bottom');
str = _applySCRegexWithTag(str, getNumberRegex, null, 'Offset');
str = _applySCRegexWithTag(str, getNumberRegex, null, 'circular');
return str;
}

exports.putSimpleCode = putSimpleCode;

function _applySCRegexWithTag(str, regex, tag, method) {
if (method)
return str.replace(new RegExp(method + '(.*)', 'g'), (value) => {
value = value.replace(new RegExp(regex, 'g'), (number) => {
if (number == 0) return number;
return `wsz(` + number + ')';
});
return value;
});
return str.replace(new RegExp(tag + ': ' + regex, 'g'), (value) => {
var matches_array = value.match(regex);
if (matches_array[0] == 0) return value;
return tag + ': wsz(' + matches_array[0] + ')';
});
}
6 changes: 3 additions & 3 deletions src/widgets/artboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const { Bounds } = require("../bounds");
const { getAlignmentByFather } = require("./util/alignment_by_father");

class ArtboardWidget {
constructor(XdNode) {
this.XdNode = XdNode;
this.bounds = new Bounds(XdNode);
constructor(xdNode) {
this.xdNode = xdNode;
this.bounds = new Bounds(xdNode);
}

toDart(child) {
Expand Down
18 changes: 9 additions & 9 deletions src/widgets/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ const { Bounds } = require("../bounds");
const { StatelessWidget } = require("./stateless");

class ComponentWidget {
constructor(XdNode) {
this.XdNode = XdNode;
this.bounds = new Bounds(XdNode);
constructor(xdNode) {
this.xdNode = xdNode;
this.bounds = new Bounds(xdNode);
}

toDart() {
const { findMasterForSymbolId } = require("../util");
const { cleanVarName } = require("./util/widgets_util");
const master = findMasterForSymbolId(this.XdNode.symbolId);
const componentName = !master ? this.XdNode.name : master.name;
const master = findMasterForSymbolId(this.xdNode.symbolId);
const componentName = !master ? this.xdNode.name : master.name;
return `const ${cleanVarName(componentName, true)}()`;
}

toDartClass() {
const { itemsToDart } = require("../items_to_dart");
const { findMasterForSymbolId } = require("../util");
const { wrapWithInkWell } = require("./util/widgets_util");
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);
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);
return new StatelessWidget(componentName, dartComponent).toDart();
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/widgets/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ const { Bounds } = require("../bounds");
const { getAlignmentByFather } = require("./util/alignment_by_father");

class ContainerWidget {
constructor(XdNode) {
this.XdNode = XdNode;
this.bounds = new Bounds(XdNode);
constructor(xdNode) {
this.xdNode = xdNode;
this.bounds = new Bounds(xdNode);
}

toDart(child) {
const alignment = child != null ? getAlignmentByFather(child, this, true) : '';
let childWidget = child != null ? `child:${child.toDart()},` : ``;
return `Container(
${alignment}
width: ${this.XdNode.localBounds.width},
height: ${this.XdNode.localBounds.height},
width: ${this.xdNode.localBounds.width},
height: ${this.xdNode.localBounds.height},
color: ${randomColor()},
${childWidget}
)`;
Expand Down
12 changes: 6 additions & 6 deletions src/widgets/grid.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const { Bounds } = require("../bounds");

class GridWidget {
constructor(XdNode) {
this.XdNode = XdNode;
this.bounds = new Bounds(XdNode);
constructor(xdNode) {
this.xdNode = xdNode;
this.bounds = new Bounds(xdNode);
}

toDart() {
return `
Container(
// [${this.XdNode.name}] Repeat grid aren't supported.
width: ${this.XdNode.localBounds.width},
height: ${this.XdNode.localBounds.height},
// [${this.xdNode.name}] Repeat grid aren't supported.
width: ${this.xdNode.localBounds.width},
height: ${this.xdNode.localBounds.height},
color: Colors.red,
)`;
}
Expand Down
14 changes: 7 additions & 7 deletions src/widgets/group.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const { Bounds } = require("../bounds");

class GroupWidget {
constructor(XdNode) {
this.XdNode = XdNode;
this.bounds = new Bounds(XdNode);
constructor(xdNode) {
this.xdNode = xdNode;
this.bounds = new Bounds(xdNode);
}

toDart() {
const { itemsToDart } = require("../items_to_dart");
const { removeItemsFromGroup } = require("../util");
const ungroupedItems = removeItemsFromGroup(this.XdNode.children);
const ungroupedItems = removeItemsFromGroup(this.xdNode.children);
const itemsDart = itemsToDart(ungroupedItems);
return `
Container(
// Group: ${this.XdNode.name}
// Group: ${this.xdNode.name}
alignment: Alignment.center,
width: ${this.XdNode.localBounds.width},
height: ${this.XdNode.localBounds.height},
width: ${this.xdNode.localBounds.width},
height: ${this.xdNode.localBounds.height},
child: ${itemsDart},
)`;
}
Expand Down
10 changes: 5 additions & 5 deletions src/widgets/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ const { Bounds } = require("../bounds");
const { getAlignmentByFather } = require("./util/alignment_by_father");

class ImageWidget {
constructor(XdNode) {
this.XdNode = XdNode;
this.bounds = new Bounds(XdNode);
constructor(xdNode) {
this.xdNode = xdNode;
this.bounds = new Bounds(xdNode);
}

toDart(child) {
const alignment = child != null ? getAlignmentByFather(child, this, true) : '';
let childWidget = child != null ? `child:${child.toDart()},` : ``;
return `Container(
${alignment}
width: ${this.XdNode.localBounds.width},
height: ${this.XdNode.localBounds.height},
width: ${this.xdNode.localBounds.width},
height: ${this.xdNode.localBounds.height},
color: ${randomColor()},
${childWidget}
)`;
Expand Down
12 changes: 6 additions & 6 deletions src/widgets/inkwell.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const { Bounds } = require("../bounds");

class InkWellWidget {
constructor(XdNode) {
this.XdNode = XdNode;
this.bounds = new Bounds(XdNode);
constructor(xdNode) {
this.xdNode = xdNode;
this.bounds = new Bounds(xdNode);
}

toDart(childWidget) {
const { itemsToDart } = require("../items_to_dart");
const child = !childWidget ? itemsToDart(this.XdNode.children) : childWidget;
const child = !childWidget ? itemsToDart(this.xdNode.children) : childWidget;
return `
InkWell(
onTap: (){
//TODO: onTap ${this.XdNode.name}
print('onTap ${this.XdNode.name}');
//TODO: onTap ${this.xdNode.name}
print('onTap ${this.xdNode.name}');
},
child: ${child},
)`;
Expand Down
14 changes: 7 additions & 7 deletions src/widgets/mask.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const { Bounds } = require("../bounds");

class MaskWidget {
constructor(XdNode) {
this.XdNode = XdNode;
this.bounds = new Bounds(XdNode);
constructor(xdNode) {
this.xdNode = xdNode;
this.bounds = new Bounds(xdNode);
}

toDart() {
// const { itemsToDart } = require("../items_to_dart");
// const { removeItemsFromGroup } = require("../util");
// const ungroupedItems = removeItemsFromGroup(this.XdNode.children);
// const ungroupedItems = removeItemsFromGroup(this.xdNode.children);
// const itemsDart = itemsToDart(ungroupedItems);
return `
Container(
// [${this.XdNode.name}] Group masks aren't supported.
width: ${this.XdNode.localBounds.width},
height: ${this.XdNode.localBounds.height},
// [${this.xdNode.name}] Group masks aren't supported.
width: ${this.xdNode.localBounds.width},
height: ${this.xdNode.localBounds.height},
color: Colors.red,
)`;
}
Expand Down
Loading

0 comments on commit 5978b6d

Please sign in to comment.