-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathmain.js
140 lines (129 loc) · 4.57 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const { Artboard, Text, Color, Rectangle } = require("scenegraph"); // XD拡張APIのクラスをインポート
let clipboard = require("clipboard");
let { openAlert } = require('./modules/alert-helper');
let colorBgcCodeDic = {};
let colorFcCodeDic = {};
let fSizeDic = {};
let fFamilyDic = {};
const CTYPE = {
ARTBOARD: 'Artboard',
WRAPPERLIST: 'WrapperList',
RECTANGLE: 'Rectangle',
Text: 'Text',
ELLIPSE: 'Ellipse'
};
const CLASS_FORMAT = {
BACKGROUND_COLOR: '$o-bgColor--{0} : #{0};',
FONT_COLOR: '$o-fColor--{0} : #{0};',
FONT_SIZE: '$o-fSize--{0} : {0}px;',
FONT_FAMILY: '$o-fFamily : {0}'
}
let cTypeFormatChange = {}
cTypeFormatChange[CTYPE.RECTANGLE] = function(child) {
let hexCode = subStringColorCode(convertToHex(child.fill.value));
colorBgcCodeDic[hexCode] = CLASS_FORMAT.BACKGROUND_COLOR.format(hexCode);
}
cTypeFormatChange[CTYPE.Text] = function(child) {
let hexCode = subStringColorCode(convertToHex(child.fill.value));
colorFcCodeDic[hexCode] = CLASS_FORMAT.FONT_COLOR.format(hexCode);
fSizeDic[child.styleRanges[0].fontSize] = CLASS_FORMAT.FONT_SIZE.format(child.styleRanges[0].fontSize);
fFamilyDic[child.styleRanges[0].fontFamily] = child.styleRanges[0].fontFamily;
}
cTypeFormatChange[CTYPE.ELLIPSE] = function(child) {
let hexCode = subStringColorCode(convertToHex(child.fill.value));
colorBgcCodeDic[hexCode] = CLASS_FORMAT.BACKGROUND_COLOR.format(hexCode);
}
function myCommand(selection, documentRoot) {
if (documentRoot && documentRoot.children && documentRoot.children.length > 0) {
documentRoot.children.forEach(element => {
search(element);
});
let outputString = '';
Object.keys(colorBgcCodeDic).forEach(function(key) {
outputString += colorBgcCodeDic[key] + '\n';
});
Object.keys(colorFcCodeDic).forEach(function(key) {
outputString += colorFcCodeDic[key] + '\n';
});
Object.keys(fSizeDic).forEach(function(key) {
outputString += fSizeDic[key] + '\n';
});
let fFamilyString = '';
Object.keys(fFamilyDic).forEach(function(key) {
if(fFamilyString.length > 0) {
fFamilyString += ',"' + fFamilyDic[key] + '"';
}
else{
fFamilyString += '"' + fFamilyDic[key] + '"';
}
});
if(fFamilyString.length > 0) {
outputString += CLASS_FORMAT.FONT_FAMILY.format(fFamilyString) + '\n';
}
if (outputString.length > 0) {
clipboard.copyText(outputString);
const oAlert = openAlert('successTitle', 'successMessage');
oAlert.showModal();
return false;
} else {
const oAlert = openAlert('objectExistTitle', 'objectExistMessage');
oAlert.showModal();
return false;
}
} else {
const oAlert = openAlert('artboardExistTitle', 'artboardExistMessage');
oAlert.showModal();
return false;
}
}
function search(element) {
if (element.constructor.name === CTYPE.ARTBOARD) {
search(element.children);
} else if (element.constructor.name === CTYPE.WRAPPERLIST) {
element.forEach(child => {
if (cTypeFormatChange[child.constructor.name]) {
cTypeFormatChange[child.constructor.name](child);
}
});
} else if (element.constructor.name === CTYPE.Text ||
element.constructor.name === CTYPE.ELLIPSE ||
element.constructor.name === CTYPE.RECTANGLE) {
if (cTypeFormatChange[element.constructor.name]) {
cTypeFormatChange[element.constructor.name](element);
}
}
return;
}
function convertToHex(target) {
if (typeof(target) === 'number') {
return target.toString(16);
}
return '';
}
function subStringColorCode(target) {
if (target && target.length > 0) {
return target.substring(2, 8);
}
return '';
}
// 存在チェック
if (String.prototype.format == undefined) {
/**
* フォーマット関数
*/
String.prototype.format = function(arg) {
// 置換ファンク
var rep_fn = undefined;
// オブジェクトの場合
if (typeof arg == "object") {
rep_fn = function(m, k) { return arg[k]; }
}
// 複数引数だった場合
else {
var args = arguments;
rep_fn = function(m, k) { return args[parseInt(k)]; }
}
return this.replace(/\{(\w+)\}/g, rep_fn);
}
}
module.exports.commands = { myCommandId: myCommand };