-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPSD.table.jsx
274 lines (243 loc) · 5.5 KB
/
PSD.table.jsx
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Created with JetBrains WebStorm.
* User: sapphire.shand
* Date: 13-9-11
* Time: 下午2:56
* To change this template use File | Settings | File Templates.
*/
#include "PSD.jsx"
#include "PSD.selection.jsx"
PSD.Tabel = {
/**
* 根据文档属性获取表格大小等信息
* @param doc app.activeDocument this.activeDoc
*/
getTableBounds: function(doc) {
var bounds = {
left:0,
top:0,
bottom:doc.height.value,
right:doc.width.value,
width:doc.width.value,
height:doc.height.value
}
return bounds;
},
/**
* 根据图层列表获取表格的横向或者纵向的坐标的列表 [0,20,107,120,241,253,351,382,460,600]
* @param clist Array functionArtLayerInfos + getTableBouns
*/
getFieldList: function(clist, field, sortBy) {
var list = [], i, f = {}, by, offset = {left:'right', top:'bottom'}, off = offset[field];
for (i = 0; i < clist.length; i ++) {
if (!f[clist[i][field]]) {
list.push(clist[i][field]);
f[clist[i][field]] = true;
}
if (!f[clist[i][off]]) {
list.push(clist[i][off]);
f[clist[i][off]] = true;
}
}
if (sortBy) {
by = function(a,b){return b - a;}
} else {
by = function(a,b){return a - b;}
}
return list.sort(by);
},
/**
* 根据横向坐标获取表格thead头信息 主要是每个td的宽度
* @param cols Array
* @return data = {height: 0; td:[{width:20},{width:87},{width:13}]}
*/
getTheadData: function (cols) {
var data = {height:0}, ths = [], i, width, leng = cols.length-1;
for (i = 0; i < leng; i++) {
width = cols[i + 1] - cols[i];
ths[i] = {width:width};
}
data.ths = ths;
return data;
},
/**
* 根据文档和内容图层获取表格body数据
* @param doc
* @param flist 内容图层
* TODo 边界值检测 重构
*/
getTableData: function(doc, flist) {
var clist = flist.concat([this.getTableBounds(doc)]);
var rows = this.getFieldList(clist, 'top');
var cols = this.getFieldList(clist, 'left');
var head = this.getTheadData(cols);
var slist = this.sort(flist, ['top','left']);
var table = {}
, trs = []
, i = 0
, j = 0
, height
, rleng = rows.length
, cleng = cols.length
, sleng = slist.length
, colspan = 0
, tmpcols
, left
, top
, info
;
for(n = 0; n < sleng; n++) {
top = slist[n].top;
left = slist[n].left;
for (; i < rleng; i++) {
if (!trs[i]) {
height = rows[i + 1] - rows[i];
trs[i] = {height:height};
}
if (!trs[i].tds){
trs[i].tds = [];
}
// tds = [];
if (top == rows[i]) {
colspan = 0;
for (; j < cleng; j ++) {
if (left == cols[j]) {
if (colspan != 0 ) {
info = {
colspan:colspan,
name:slist[n].name,
layer:slist[n],
x:cols[j-colspan],
y:rows[i],
width:cols[j] - cols[j-colspan],
height:rows[i+1] - rows[i]
};
trs[i].tds.push(info);
}
tmpcols = cols.indexOf(slist[n].right)-j;
info = {
colspan:tmpcols,
name:slist[n].name,
layer:slist[n],
x:cols[j],
y:rows[i],
width:cols[j+tmpcols] - cols[j],
height:rows[i+1] - rows[i]
};
//插入layer数据
trs[i].tds.push(info);
j += tmpcols;
if (j >= cleng-1) {
j = 0;
}
colspan = 0;
break;
} else {
colspan++;
}
}
if (colspan > 0) {
info = {
colspan:colspan,
x:cols[j-colspan],
y:rows[i],
width:cols[j] - cols[j-colspan],
height:rows[i+1] - rows[i]
};
trs[i].tds.push(info);
} else{
if (j == 0) {
i++;
}
break;
}
} else {
info = {
colspan:cleng-1-j,
x:cols[j],
y:rows[i],
width:cols[cleng-1] - cols[j],
height:rows[i+1] - rows[i]
};
trs[i].tds.push(info);
j = 0;
}
}
}
if (i < rleng-1) {//没有内容图层的尾部处理
if (j < cleng-1 && j > 0) {
info = {
colspan:cleng-1-j,
x:cols[j],
y:rows[i],
width:cols[cleng-1] - cols[j],
height:rows[i+1] - rows[i]
};
trs[i].tds.push(info);
j = 0;
i++;
}
if (i < rleng -1) {
if (!trs[i]) {
height = rows[i + 1] - rows[i];
trs[i] = {height:height};
}
if (!trs[i].tds){
trs[i].tds = [];
}
info = {
colspan:cleng-1-j,
x:cols[j],
y:rows[i],
width:cols[cleng-1] - cols[j],
height:rows[i+1] - rows[i]
};
trs[i].tds.push(info);
}
}
table.body = trs;
table.head = [head];
return table;
},
/**
* 对图层按属性排序
* @param cList 图层列表
* @param field [2] 根据此属性排序
* @param sortBy 升序还是降序
* @return {*} 排序后的数据 原数据不变
*/
sort: function(clist, field, sortBy) {
var list=[], i, by;
for(i = 0; i < clist.length; i++){
list.push(clist[i]);
}
if (sortBy) {
by = function(a,b){
if (b[field[0]] !== a[field[0]]) {
return b[field[0]] - a[field[0]];
} else {
return b[field[1]]-a[field[1]];
}
}
} else {
by = function(a,b){
if (b[field[0]] !== a[field[0]]) {
return a[field[0]] - b[field[0]];
} else {
return a[field[1]]-b[field[1]];
}
}
}
return list.sort(by);
}
};
if (!Array.prototype.indexOf){
Array.prototype.indexOf = function(item, i){
i || (i = 0);
var length = this.length;
if (i < 0) {i = length + i;}
for (; i < length; i++)
if (this[i] === item) return i;
return -1;
};
}