-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.js
executable file
·60 lines (53 loc) · 1.89 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const scenegraph = require("scenegraph");
const clipboard = require("clipboard");
const { Artboard, SymbolInstance } = require("scenegraph");
const { itemsToDart } = require("./src/items_to_dart");
const { StatelessWidget } = require("./src/widgets/stateless");
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;
if (hasSelection) {
const firstItem = items[0];
const isArtboard = firstItem instanceof Artboard;
const isOnlyOneComponent = items.length == 1 && firstItem instanceof SymbolInstance;
if (isOnlyOneComponent) {
generateComponents(items);
} else if (isArtboard) {
generateArtboards(items);
} else {
generateSelection(items);
}
} else {
console.log(`Nothing selected`);
}
}
module.exports = {
commands: {
onTapGenerate: onTapGenerate
}
};
function generateComponents(components) {
const componentsWidget = [];
components.forEach(component => {
const dartCode = new ComponentWidget(component).toDartClass();
componentsWidget.push(dartCode);
});
clipboard.copyText(formatDart(listToString(componentsWidget)));
}
function generateArtboards(artboards) {
const artboardsWidget = [];
artboards.forEach(artboard => {
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, true) + ";");
clipboard.copyText(dartCode);
}