Skip to content

Commit

Permalink
ynput#326: clean: reformat client.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilhemz authored Mar 15, 2024
1 parent 797ffbe commit 13dd76d
Showing 1 changed file with 60 additions and 60 deletions.
120 changes: 60 additions & 60 deletions openpype/hosts/photoshop/api/extension/client/client.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
// client facing part of extension, creates WSRPC client (jsx cannot
// client facing part of extension, creates WSRPC client (jsx cannot
// do that)
// consumes RPC calls from server (OpenPype) calls ./host/index.jsx and
// returns values back (in json format)

var logReturn = function(result){ log.warn('Result: ' + result);};

var csInterface = new CSInterface();

log.warn("script start");

WSRPC.DEBUG = false;
WSRPC.TRACE = false;

function myCallBack(){
log.warn("Triggered index.jsx");
}
// importing through manifest.xml isn't working because relative paths
// possibly TODO
jsx.evalFile('./host/index.jsx', myCallBack);

function runEvalScript(script) {
// because of asynchronous nature of functions in jsx
// this waits for response
return new Promise(function(resolve, reject){
csInterface.evalScript(script, resolve);
});
}

/** main entry point **/
startUp("WEBSOCKET_URL");

// get websocket server url from environment value
async function startUp(url){
log.warn("url", url);
log.warn("url", url);
promis = runEvalScript("getEnv('" + url + "')");

var res = await promis;
var res = await promis;
// run rest only after resolved promise
main(res);
}
Expand All @@ -45,15 +45,15 @@
log.debug("get_extension_version")
var path = csInterface.getSystemPath(SystemPath.EXTENSION);
log.debug("extension path " + path);

var result = window.cep.fs.readFile(path + "/CSXS/manifest.xml");
var version = undefined;
if(result.err === 0){
if (window.DOMParser) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(result.data.toString(), 'text/xml');
const children = xmlDoc.children;

for (let i = 0; i <= children.length; i++) {
if (children[i] && children[i].getAttribute('ExtensionBundleVersion')) {
version = children[i].getAttribute('ExtensionBundleVersion');
Expand All @@ -63,22 +63,22 @@
}
return version
}

function main(websocket_url){
// creates connection to 'websocket_url', registers routes
log.warn("websocket_url", websocket_url);
// creates connection to 'websocket_url', registers routes
log.warn("websocket_url", websocket_url);
var default_url = 'ws://localhost:8099/ws/';

if (websocket_url == ''){
websocket_url = default_url;
}
log.warn("connecting to:", websocket_url);
log.warn("connecting to:", websocket_url);
RPC = new WSRPC(websocket_url, 5000); // spin connection

RPC.connect();

log.warn("connected");

log.warn("connected");
function EscapeStringForJSX(str){
// Replaces:
// \ with \\
Expand All @@ -87,7 +87,7 @@
// See: https://stackoverflow.com/a/3967927/5285364
return str.replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/"/g, '\\"');
}

RPC.addRoute('Photoshop.open', function (data) {
log.warn('Server called client route "open":', data);
var escapedPath = EscapeStringForJSX(data.path);
Expand All @@ -97,7 +97,7 @@
return result;
});
});

RPC.addRoute('Photoshop.read', function (data) {
log.warn('Server called client route "read":', data);
return runEvalScript("getHeadline()")
Expand All @@ -106,7 +106,7 @@
return result;
});
});

RPC.addRoute('Photoshop.get_layers', function (data) {
log.warn('Server called client route "get_layers":', data);
return runEvalScript("getLayers()")
Expand All @@ -115,7 +115,7 @@
return result;
});
});

RPC.addRoute('Photoshop.set_visible', function (data) {
log.warn('Server called client route "set_visible":', data);
return runEvalScript("setVisible(" + data.layer_id + ", " +
Expand All @@ -125,17 +125,17 @@
return result;
});
});

RPC.addRoute('Photoshop.get_active_document_name', function (data) {
log.warn('Server called client route "get_active_document_name":',
log.warn('Server called client route "get_active_document_name":',
data);
return runEvalScript("getActiveDocumentName()")
.then(function(result){
log.warn("save: " + result);
return result;
});
});

RPC.addRoute('Photoshop.get_active_document_full_name', function (data) {
log.warn('Server called client route ' +
'"get_active_document_full_name":', data);
Expand All @@ -145,49 +145,49 @@
return result;
});
});

RPC.addRoute('Photoshop.save', function (data) {
log.warn('Server called client route "save":', data);

return runEvalScript("save()")
.then(function(result){
log.warn("save: " + result);
return result;
});
});

RPC.addRoute('Photoshop.get_selected_layers', function (data) {
log.warn('Server called client route "get_selected_layers":', data);

return runEvalScript("getSelectedLayers()")
.then(function(result){
log.warn("get_selected_layers: " + result);
return result;
});
});

RPC.addRoute('Photoshop.create_group', function (data) {
log.warn('Server called client route "create_group":', data);

return runEvalScript("createGroup('" + data.name + "')")
.then(function(result){
log.warn("createGroup: " + result);
return result;
});
});

RPC.addRoute('Photoshop.group_selected_layers', function (data) {
log.warn('Server called client route "group_selected_layers":',
log.warn('Server called client route "group_selected_layers":',
data);

return runEvalScript("groupSelectedLayers(null, "+
"'" + data.name +"')")
.then(function(result){
log.warn("group_selected_layers: " + result);
return result;
});
});

RPC.addRoute('Photoshop.import_smart_object', function (data) {
log.warn('Server called client "import_smart_object":', data);
var escapedPath = EscapeStringForJSX(data.path);
Expand All @@ -199,7 +199,7 @@
return result;
});
});

RPC.addRoute('Photoshop.replace_smart_object', function (data) {
log.warn('Server called route "replace_smart_object":', data);
var escapedPath = EscapeStringForJSX(data.path);
Expand All @@ -211,7 +211,7 @@
return result;
});
});

RPC.addRoute('Photoshop.delete_layer', function (data) {
log.warn('Server called route "delete_layer":', data);
return runEvalScript("deleteLayer("+data.layer_id+")")
Expand All @@ -220,6 +220,15 @@
return result;
});
});

RPC.addRoute('Photoshop.rename_layers', function (data) {
log.warn('Server called route "rename_layers":', data);
return runEvalScript("renameLayers('"+data.layers+"')")
.then(function(result){
log.warn("rename_layers: " + result);
return result;
});
});

RPC.addRoute('Photoshop.rename_layer', function (data) {
log.warn('Server called route "rename_layer":', data);
Expand All @@ -230,48 +239,39 @@
return result;
});
});

RPC.addRoute('Photoshop.rename_layers', function (data) {
log.warn('Server called route "rename_layers":', data);
return runEvalScript("renameLayers('"+data.layers+"')")
.then(function(result){
log.warn("rename_layers: " + result);
return result;
});
});


RPC.addRoute('Photoshop.select_layers', function (data) {
log.warn('Server called client route "select_layers":', data);

return runEvalScript("selectLayers('" + data.layers +"')")
.then(function(result){
log.warn("select_layers: " + result);
return result;
});
});

RPC.addRoute('Photoshop.is_saved', function (data) {
log.warn('Server called client route "is_saved":', data);

return runEvalScript("isSaved()")
.then(function(result){
log.warn("is_saved: " + result);
return result;
});
});

RPC.addRoute('Photoshop.saveAs', function (data) {
log.warn('Server called client route "saveAsJPEG":', data);
var escapedPath = EscapeStringForJSX(data.image_path);
return runEvalScript("saveAs('" + escapedPath + "', " +
"'" + data.ext + "', " +
"'" + data.ext + "', " +
data.as_copy + ")")
.then(function(result){
log.warn("save: " + result);
return result;
});
});

RPC.addRoute('Photoshop.imprint', function (data) {
log.warn('Server called client route "imprint":', data);
var escaped = data.payload.replace(/\n/g, "\\n");
Expand All @@ -291,19 +291,19 @@
log.warn('Server called client route "close":', data);
return runEvalScript("close()");
});

RPC.call('Photoshop.ping').then(function (data) {
log.warn('Result for calling server route "ping": ', data);
return runEvalScript("ping()")
.then(function(result){
log.warn("ping: " + result);
return result;
});

}, function (error) {
log.warn(error);
});

}

log.warn("end script");

0 comments on commit 13dd76d

Please sign in to comment.