This repository has been archived by the owner on Mar 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcep-extension.js
342 lines (303 loc) · 7.88 KB
/
cep-extension.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
(function(global, $, undefined){
'use strict';
/**
* Responsible for handling the dynamic theme changing
* @class CEPExtension
* @constructor
* @param {String} stylesheet The selector for the style sheet
* @param {String} namespace The fullyqualified name of the app e.g., "com.cloudkid.App"
*/
var CEPExtension = function(stylesheet, namespace)
{
/**
* The stylesheet element
* @property {DOM} stylesheet
* @private
*/
this.stylesheet = $(stylesheet)[0];
/**
* The Adobe app interface
* @property {CSInterface} csInterface
*/
this.csInterface = new CSInterface();
/**
* If the extension is supported in the context
* @property {Boolean} supported
* @readOnly
*/
this.supported = (global.__adobe_cep__ !== undefined);
// Check that we can run
if (!this.supported)
{
throw "Extension must run as a CEP Extension inside Adobe applications";
return;
}
/**
* The system path to the extension without trailing slash
* @property {String} extensionPath
* @readOnly
*/
this.extensionPath = this.csInterface.getSystemPath(SystemPath.EXTENSION);
/**
* The current settings path
* @property {String} settingsPath
*/
this.settingsPath = this.csInterface.getSystemPath(SystemPath.USER_DATA) + "/" + namespace;
// Make the directory if we don't have it
global.cep.fs.makedir(this.settingsPath);
/**
* The full path to the settings file
* @property {String} settingsFile
*/
this.settingsFile = this.settingsPath + "/settings.json";
/**
* The current settings
* @property {Object|String|Number} settings
*/
this.settings = unserialize(global.cep.fs.readFile(this.settingsFile).data) || {};
/**
* The name of the app we're running within (e.g., "PHSP" = Photoshop)
* @property {String} appName
*/
this.appName = this.csInterface.hostEnvironment.appName;
/**
* Are we running in Flash? Flash has a different JS API, using JSFL instead of JSX
* @property {Boolean} isFlash
*/
this.isFlash = this.appName == "FLPR";
var self = this;
// Update the color of the panel when the theme color of the product changed.
this.csInterface.addEventListener(CSInterface.THEME_COLOR_CHANGED_EVENT, function(){
// Should get a latest HostEnvironment object from application.
var skinInfo = JSON.parse(global.__adobe_cep__.getHostEnvironment()).appSkinInfo;
// Gets the style information such as color info from the skinInfo,
// and redraw all UI controls of your extension according to the style info.
self.update(skinInfo);
});
// Update the theme right now
this.update(this.csInterface.hostEnvironment.appSkinInfo);
// Initialize
this.init();
};
// Reference to the prototype
var p = CEPExtension.prototype = {};
/**
* The name of the plugin
* @property {String} name
*/
p.name = null;
/**
* Initialize the extension, this is implementation specific
* @method init
*/
p.init = function()
{
// Extend something here
};
/**
* Toggle the theme to something else
* @method update
* @private
* @param {Object} info The App Skin Info from the application interface
*/
p.update = function(info)
{
// Get the background color
var color = info.panelBackgroundColor.color;
this.addRule(
"body",
"color: " + reverseColor(color) + ";"
+ "font-size:" + info.baseFontSize + "px;"
+ "background-color:"+ colorToHex(color)
);
};
/**
* Save a property that is accessible between settings
* @method setProp
* @param {String} name The name of the property to set
* @param {mixed} value The value of the property
*/
p.setProp = function(name, value)
{
this.settings[name] = value;
global.cep.fs.writeFile(this.settingsFile, JSON.stringify(this.settings));
};
/**
* Get a saved setting
* @method loadSettings
* @return {mixed} The settings object, string or whatever was saved
*/
p.getProp = function(name)
{
return this.settings[name] || null;
};
/**
* Add a rule to the style sheet
* @method addRule
* @param {String} selector The CSS selector
* @param {String} rule The CSS rules
*/
p.addRule = function(selector, rule)
{
var sheet = this.stylesheet.sheet;
if (sheet.addRule)
{
sheet.addRule(selector, rule);
}
else if (sheet.insertRule)
{
sheet.insertRule(selector + ' { ' + rule + ' }', sheet.cssRules.length);
}
};
/**
* Reverse a color
* @method reverseColor
* @private
* @param {int} color The color to reverse
* @param {Number} delta The amount to reverse by
* @return {String} The hexidecimal color (e.g. "#ffffff")
*/
var reverseColor = function(color, delta)
{
return colorToHex(
{
"red" : Math.abs(255 - color.red),
"green" : Math.abs(255 - color.green),
"blue" : Math.abs(255 - color.blue)
},
delta
);
};
/**
* Convert the Color object to string in hexadecimal format;
* @method colorToHex
* @private
* @param {Object} color The color to select
* @param {int} color.red The red color value
* @param {int} color.blue The blue color value
* @param {int} color.green The green color value
* @param {Number} delta The color shift
* @return {String} The hexidecimal number (e.g. "#ffffff")
*/
var colorToHex = function(color, delta)
{
return "#" + valueToHex(color.red, delta)
+ valueToHex(color.green, delta)
+ valueToHex(color.blue, delta);
};
/**
* Compute the value of a color to a hext value
* @method colorToHex
* @private
* @param {int} value A color value from 0 to 255
* @param {int} delta The amoutn to shift by
* @param {String} The single hex value
*/
var valueToHex = function(value, delta)
{
var computedValue = !isNaN(delta) ? value + delta : value;
if (computedValue < 0) {
computedValue = 0;
}
else if (computedValue > 255)
{
computedValue = 255;
}
computedValue = Math.round(computedValue).toString(16);
return computedValue.length == 1 ? "0" + computedValue : computedValue;
};
/**
* Execute a JSFL command
* @method execute
* @param {String} script The JSFL command to run or the path to the JSFL file
* @param {Object|Array|Function} [args] The optional arguments to pass to the script or the callback function
* @param {Function} [callback] Callback function if args is set to an object or array
*/
p.execute = function(script, args, callback)
{
// second argument can be callback for arguments
if (typeof args == "function")
{
callback = args;
args = undefined;
}
if (this.supported)
{
// Check for script paths
if (/^([\/a-zA-Z0-9\-|_\.\%\?]+\.js(fl|x)?)$/.test(script))
{
var self = this;
$.get(script, function(data)
{
self.execute(data, args, callback);
}
);
}
else
{
// Add the arguments to the global window
if (args !== undefined)
{
script = "var args="+JSON.stringify(args)+";\n"+script;
}
var self = this;
this.csInterface.evalScript(
script,
function(response)
{
// No callback, so we'll ignore
if (callback !== undefined)
{
// Bind the callback to this extension
callback.call(self, unserialize(response));
}
}
);
}
}
else
{
Debug.error("Unable to execute command");
}
};
/**
* Unserialize a string from an external file
* @method unserialize
* @private
* @param {String} str The input string
* @return {String} The unserialized string
*/
var unserialize = function(str)
{
var result;
// Check for undefined undefined
if (str == "undefined")
{
result = undefined;
}
else
{
// Unserialize the response
try
{
result = JSON.parse(str);
}
catch(e)
{
// Handle syntax error
result = str;
}
}
return result;
};
/**
* For debugging purposes
* @method toString
*/
p.toString = function()
{
return "[object CEPExtension(name='"+this.name+"')]";
};
// Assign to the parent window
global.CEPExtension = CEPExtension;
}(window, jQuery));