forked from hi104/psd-grd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
205 lines (163 loc) · 5.4 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
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
var gradients;
var cssUtil;
var gradientCssCreator;
var gradientSvgCreator;
function checkSupportFileApi(){
if (window.File && window.FileReader && window.FileList && window.Blob) {
return true;
} else {
alert('The File APIs are not fully supported in this browser.');
return false;
}
}
function showFileInfo(files){
var output = [];
for (var i = 0, f; f = files[i]; i++) {
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
f.size, '</li>');
}
document.getElementById('file-list').innerHTML = '<ul>' + output.join('') + '</ul>';
}
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.target.files; //
showFileInfo(files);
readFile(files[0]);
}
function handleFileDrop(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files;
showFileInfo(files);
readFile(files[0]);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
function readFile(file) {
var reader = new FileReader();
reader.onload = function(e) {
var uint8_array = new Uint8Array(e.target.result);
start_parse(new PSD.StreamReader(uint8_array));
};
reader.readAsArrayBuffer(file);
}
var CssUtil = (function() {
function CssUtil() {}
CssUtil.prototype.createCSSRule = function(id, style_text) {
var head = document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
style.id = id;
style.className = "gradient-style";
var css = style_text;
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
};
return CssUtil;
})();
var timeoutFn;
function start_parse(stream_reader){
clearTimeout(timeoutFn); // cancel if parsing
$("#gradient-panel").empty();
$(".gradient-style").remove();
gradients = {};
var cssCreator = new PSDGradient.CSSCreator();
var parser = new PSDGradient.IncrementParser(stream_reader);
parser.setup();
var parse = function(){
if(parser.hasNext()){
var index = parser.index;
var grad_id = "grad-" + index.toString();
try{
var gradient = parser.parse();
var css = cssCreator.create(gradient, {selector: "." + grad_id});
cssUtil.createCSSRule(grad_id, css);
var elm = $("<div></div>").addClass("gradient grid "+ grad_id);
elm.data("grad", grad_id);
elm.appendTo($("#gradient-panel"));
gradients[grad_id] = gradient;
if($(".active").length ==0){
selectGradient($("." + grad_id));
}
}catch(ex){
console.log("create gradient error:" + ex.message);
}
timeoutFn = setTimeout(function () {
parse();
// downloadAllCSS();
}, 0);
}
};
parse();
}
function downloadAllSVG(){
var svg_text ="";
for (var id in gradients){
var svg = gradientSvgCreator.create(gradients[id], {id:id});
svg_text += svg + "\n";
}
$("#svg-info").val(svg_text);
var url_prefix ="data:text/txt, ";
var url = url_prefix + encodeURIComponent(svg_text);
window.open(url, "gradient.svg.txt", "");
}
function downloadAllCSS(){
var text ="";
for (var id in gradients){
var css = gradientCssCreator.create(gradients[id], {selector: "." + id});
text += css + "\n";
}
$("#svg-info").val(text);
var url_prefix ="data:text/txt, ";
var url = url_prefix + encodeURIComponent(text);
window.open(url, "gradient.css.txt", "");
}
function selectGradient(elem){
var css_id = $(elem).data("grad");
$("#gradient-panel .active").removeClass("active");
elem.addClass("active");
$("#sample-box").attr("class", css_id);
var style = gradientCssCreator.createGradientStyle(gradients[css_id]);
var svg = gradientSvgCreator.create(gradients[css_id]);
$("#grad-info").val(style);
$("#svg-info").val(svg);
}
$(function(){
gradientCssCreator = new PSDGradient.CSSCreator();
gradientSvgCreator = new PSDGradient.SVGCreator();
cssUtil = new CssUtil();
if(checkSupportFileApi()){
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileDrop, false);
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
$("#sample-box-wrap").resizable();
$(document).on("click", ".gradient", function(e){
selectGradient($(this));
});
$('#panel-tabs').on("click", "a", function(e){
e.preventDefault();
$(this).tab('show');
});
$("#download-all-svg").on("click", function(e){
e.preventDefault();
if (Object.keys(gradients).length > 0){
downloadAllSVG();
}
});
$("#download-all-css").on("click", function(e){
e.preventDefault();
if (Object.keys(gradients).length > 0){
downloadAllCSS();
}
});
});