diff --git a/helper.js b/helper.js index e5290be..81dab2e 100644 --- a/helper.js +++ b/helper.js @@ -1,3 +1,5 @@ +const { unselectActiveLayers } = require('./psapi') + const app = window.require('photoshop').app function getActiveLayer () { @@ -13,88 +15,79 @@ function getActiveLayer () { return activeLayers[0] } -// async function scaleDownLayer () { -// let layer = getActiveLayer() -// console.log(layer.name) -// async function scaleLayer (executionContext) { -// console.log('scaleLayer got called') -// await layer.scale(50, 50) -// } -// await require('photoshop').core.executeAsModal(scaleLayer) -// } function getSize () { let doc = app.activeDocument return { height: doc.height, width: doc.width } } -function getLayerSize (layer) { - console.log('layer.bounds:') - console.dir(layer.bounds) - const bounds = layer.bounds - const height = bounds.bottom - bounds.top - const width = bounds.right - bounds.left - return { - height: height, - width: width, - left: bounds.left, - right: bounds.right, - top: bounds.top, - bottom: bounds.bottom - } -} -async function getSelectionInfo () { - console.log('getSelectionInfo was called') +// function getLayerSize (layer) { +// console.log('layer.bounds:') +// console.dir(layer.bounds) +// const bounds = layer.bounds +// const height = bounds.bottom - bounds.top +// const width = bounds.right - bounds.left +// return { +// height: height, +// width: width, +// left: bounds.left, +// right: bounds.right, +// top: bounds.top, +// bottom: bounds.bottom +// } +// } +// async function getSelectionInfo () { +// console.log('getSelectionInfo was called') - const { batchPlay } = require('photoshop').action - const { executeAsModal } = require('photoshop').core +// const { batchPlay } = require('photoshop').action +// const { executeAsModal } = require('photoshop').core - async function batchPlayWrapper () { - const result = await batchPlay( - [ - { - _obj: 'get', - _target: [ - { - _property: 'selection' - }, - { - _ref: 'document', - _id: app.activeDocument._id - } - ], - _options: { - dialogOptions: 'dontDisplay' - } - } - ], - { - synchronousExecution: true, - modalBehavior: 'execute' - } - ) +// async function batchPlayWrapper () { +// const result = await batchPlay( +// [ +// { +// _obj: 'get', +// _target: [ +// { +// _property: 'selection' +// }, +// { +// _ref: 'document', +// _id: app.activeDocument._id +// } +// ], +// _options: { +// dialogOptions: 'dontDisplay' +// } +// } +// ], +// { +// synchronousExecution: true, +// modalBehavior: 'execute' +// } +// ) - return result - } +// return result +// } - try { - const selection = (await executeAsModal(batchPlayWrapper))[0].selection +// try { +// const selection = (await executeAsModal(batchPlayWrapper))[0].selection - let selection_info = { - left: selection.left._value, - right: selection.right._value, - bottom: selection.bottom._value, - top: selection.top._value, - height: selection.bottom._value - selection.top._value, - width: selection.right._value - selection.left._value - } - // console.dir({selection_info}) - return selection_info - } catch (e) { - console.warn('selection info error', e) - } -} +// let selection_info = { +// left: selection.left._value, +// right: selection.right._value, +// bottom: selection.bottom._value, +// top: selection.top._value, +// height: selection.bottom._value - selection.top._value, +// width: selection.right._value - selection.left._value +// } +// // console.dir({selection_info}) +// return selection_info +// } catch (e) { +// console.warn('selection info error', e) +// } +// } const { batchPlay } = require('photoshop').action const { executeAsModal } = require('photoshop').core @@ -181,96 +174,106 @@ async function unSelect () { return result } -async function layerToSelectionHelper () { - // console.log("executeAsModal layer.translate") +// async function layerToSelectionHelper () { +// // console.log("executeAsModal layer.translate") - //get selection info - let activeLayer = getActiveLayer() - let selectionInfoPromise = await getSelectionInfo() - selectionInfoPromise.then(async value => { - console.dir(value) +// //get selection info +// let activeLayer = getActiveLayer() +// let selectionInfoPromise = await getSelectionInfo() +// selectionInfoPromise.then(async value => { +// console.dir(value) - let selection = value[0].selection +// let selection = value[0].selection - // let selectionInfo = value[0].selection +// // let selectionInfo = value[0].selection - //unselect everything so you can move the layer - // top_new = layer_info.top - top_dist - executeAsModal(unSelect).then(() => { - console.log('done unSelect Exe') - //scale layer - async function scaleLayer (executionContext) { - console.log('scaleLayer got called') - let layer_info = getLayerSize(activeLayer) - scale_x_ratio = (selection_info.width / layer_info.width) * 100 - scale_y_ratio = (selection_info.height / layer_info.height) * 100 - console.log('scale_x_y_ratio:', scale_x_ratio, scale_y_ratio) - activeLayer.scale(scale_x_ratio, scale_y_ratio) - } +// //unselect everything so you can move the layer +// // top_new = layer_info.top - top_dist +// executeAsModal(unSelect).then(() => { +// console.log('done unSelect Exe') +// //scale layer +// async function scaleLayer (executionContext) { +// console.log('scaleLayer got called') +// let layer_info = getLayerSize(activeLayer) +// scale_x_ratio = (selection_info.width / layer_info.width) * 100 +// scale_y_ratio = (selection_info.height / layer_info.height) * 100 +// console.log('scale_x_y_ratio:', scale_x_ratio, scale_y_ratio) +// activeLayer.scale(scale_x_ratio, scale_y_ratio) +// } - executeAsModal(scaleLayer).then(async () => { - console.log('done scaling Exe') +// executeAsModal(scaleLayer).then(async () => { +// console.log('done scaling Exe') - await require('photoshop').core.executeAsModal(moveLayerExe) - }) - }) - }) -} +// await require('photoshop').core.executeAsModal(moveLayerExe) +// }) +// }) +// }) +// } -async function layerToSelection () { - //store active layer for later +// async function layerToSelection (selection_info) { +// //store active layer for later - const { executeAsModal } = require('photoshop').core +// const { executeAsModal } = require('photoshop').core - try { - //Store selection info - //unSelect - //move layer - //scale layer - //Select from selection info - let selection_info = await getSelectionInfo() - console.dir({ selection_info }) +// try { +// //Store selection info +// //unSelect +// //move layer +// //scale layer +// //Select from selection info +// // let selection_info = await getSelectionInfo() + - console.log('selection_info:') - console.dir({ selection_info }) +// console.log('selection_info:',selection_info) + - console.log('unSelect') +// console.log('unSelect') - await executeAsModal(unSelect, {'commandName': 'unSelect'}) +// await executeAsModal(unSelect, {'commandName': 'unSelect'}) - //scale layer - async function scaleLayer (executionContext) { - console.log('scaleLayer got called') - const activeLayer = getActiveLayer() - let layer_info = getLayerSize(activeLayer) - scale_x_ratio = (selection_info.width / layer_info.width) * 100 - scale_y_ratio = (selection_info.height / layer_info.height) * 100 - console.log('scale_x_y_ratio:', scale_x_ratio, scale_y_ratio) - activeLayer.scale(scale_x_ratio, scale_y_ratio) - } - await executeAsModal(scaleLayer, {'commandName': 'scaleLayer'}) +// //scale layer +// async function scaleLayer (executionContext) { +// console.log('scaleLayer got called') +// // const activeLayer = getActiveLayer() +// const activeLayer = await app.activeDocument.activeLayers[0] + +// let layer_info = getLayerSize(activeLayer) +// scale_x_ratio = (selection_info.width / layer_info.width) * 100 +// scale_y_ratio = (selection_info.height / layer_info.height) * 100 +// console.log('scale_x_y_ratio:', scale_x_ratio, scale_y_ratio) +// activeLayer.scale(scale_x_ratio, scale_y_ratio) +// } +// await executeAsModal(scaleLayer, {'commandName': 'scaleLayer'}) - async function moveLayerExe (layerToMove, selection_info) { - // const activeLayer = getActiveLayer() - let layer_info = getLayerSize(layerToMove) - top_dist = layer_info.top - selection_info.top - left_dist = layer_info.left - selection_info.left - await layerToMove.translate(-left_dist, -top_dist) - } - const activeLayer = await getActiveLayer() - await executeAsModal(async () => { - await moveLayerExe(activeLayer, selection_info) - }, {'commandName': 'moveLayerExe'}) +// async function moveLayerExe (layerToMove, selection_info) { + +// let layer_info = getLayerSize(layerToMove) +// top_dist = layer_info.top - selection_info.top +// left_dist = layer_info.left - selection_info.left +// await layerToMove.translate(-left_dist, -top_dist) +// } +// // const activeLayer = await getActiveLayer() +// //store all active layers +// const activeLayers = await app.activeDocument.activeLayers - reselect(selection_info) - } catch (e) { - console.warn(e) - } + +// await executeAsModal(async () => { + +// for (let layer of activeLayers){ +// await psapi.selectLayers([layer]) +// await moveLayerExe(layer, selection_info) +// } +// }, {'commandName': 'moveLayerExe'}) + +// await reselect(selection_info) +// } catch (e) { +// console.warn(e) +// } -} +// } module.exports = { - layerToSelection + // layerToSelection } diff --git a/index.js b/index.js index 2b040f8..ce64c03 100644 --- a/index.js +++ b/index.js @@ -1682,12 +1682,27 @@ document.querySelector('#mModelsMenu').addEventListener('change', evt => { sdapi.requestSwapModel(g_model_title) }) -// document.getElementById('btnGetActiveLayer').addEventListener('click', getActiveLayer) -// document.getElementById('btnScaleDown').addEventListener('click', scaleDownLayer) -// document.getElementById('btnSelectionInfo').addEventListener('click', getSelectionInfo) + document .getElementById('btnLayerToSelection') - .addEventListener('click', helper.layerToSelection) + .addEventListener('click',async()=>{ + try{ + + const isSelectionAreaValid = await psapi.checkIfSelectionAreaIsActive() + if(isSelectionAreaValid){ + const validSelection = isSelectionAreaValid + psapi.layerToSelection(validSelection) + + }else{ + psapi.promptForMarqueeTool() + } + + }catch(e) + { + console.warn(e) + } + } + ) // document.getElementById('bGetInitImage').addEventListener('click', () => { // sdapi.getInitImage(g_init_image_name) @@ -1840,25 +1855,6 @@ async function imageToSmartObject () { // document.getElementById('btnNewLayer').addEventListener('click', imageToSmartObject ) -async function fillLayer () { - // User picks an image file - const storage = require('uxp').storage - const fs = storage.localFileSystem - let imageFile = await fs.getFileForOpening({ - types: storage.fileTypes.images - }) - - // Create ImageFill for this image - const ImageFill = require('scenegraph').ImageFill - let fill = new ImageFill(imageFile) - - let layer = getActiveLayer() - layer.fillImage() -} -//fillLayer() - -// Set fill of first selected item -// selection.items[0].fill = fill; async function placeEmbedded () { console.log('placeEmbedded():') @@ -1970,7 +1966,7 @@ async function ImagesToLayersExe (images_paths) { await openImageExe() //local image to new document await convertToSmartObjectExe() //convert the current image to smart object await stackLayers() // move the smart object to the original/old document - await helper.layerToSelection() //transform the new smart object layer to fit selection area + await psapi.layerToSelection(g_selection) //transform the new smart object layer to fit selection area layer = await app.activeDocument.activeLayers[0] image_path_to_layer[image_path] = layer // await reselect(selectionInfo) diff --git a/psapi.js b/psapi.js index 1ef1269..05eda77 100644 --- a/psapi.js +++ b/psapi.js @@ -2,7 +2,7 @@ const app = window.require('photoshop').app const batchPlay = require('photoshop').action.batchPlay const { executeAsModal } = require('photoshop').core const export_png = require('./export_png') -const { layerToSelection } = require('./helper') +// const { layerToSelection } = require('./helper') const storage = require('uxp').storage const fs = storage.localFileSystem @@ -1065,7 +1065,8 @@ async function newExportPng (layer,image_name,width,height) { await selectLayers([dupLayer]) // await selectLayerChannelCommand() await selectCanvasExe() - await layerToSelection() + const canvas_selection_info = await getSelectionInfoExe() + await layerToSelection(canvas_selection_info) // const selection_info = await getSelectionInfoExe() // await exportDoc.crop(selection_info) // export_image_name = `${layer.name}.png` @@ -1102,6 +1103,91 @@ async function mergeVisibleExe () { }) } +async function layerToSelection (selection_info) { + //store active layer for later + + + try { + //Store selection info + //unSelect + //move layer + //scale layer + //Select from selection info + // let selection_info = await getSelectionInfo() + + + console.log('selection_info:',selection_info) + + + console.log('unSelect') + + + function getLayerSize (layer) { + console.log('layer.bounds:') + console.dir(layer.bounds) + const bounds = layer.bounds + const height = bounds.bottom - bounds.top + const width = bounds.right - bounds.left + return { + height: height, + width: width, + left: bounds.left, + right: bounds.right, + top: bounds.top, + bottom: bounds.bottom + } + } + //scale layer + async function scaleLayer (layer,selection_info) { + console.log('scaleLayer got called') + // const activeLayer = getActiveLayer() + // const activeLayer = await app.activeDocument.activeLayers[0] + + let layer_info = getLayerSize(layer) + scale_x_ratio = (selection_info.width / layer_info.width) * 100 + scale_y_ratio = (selection_info.height / layer_info.height) * 100 + console.log('scale_x_y_ratio:', scale_x_ratio, scale_y_ratio) + await layer.scale(scale_x_ratio, scale_y_ratio) + } + + + + async function moveLayerExe (layerToMove, selection_info) { + + let layer_info = getLayerSize(layerToMove) + top_dist = layer_info.top - selection_info.top + left_dist = layer_info.left - selection_info.left + await layerToMove.translate(-left_dist, -top_dist) + } + // const activeLayer = await getActiveLayer() + + //store all active layers + const activeLayers = await app.activeDocument.activeLayers + await unSelectMarqueeExe() + // await executeAsModal(unSelect, {'commandName': 'unSelect'}) + // await executeAsModal(scaleLayer, {'commandName': 'scaleLayer'}) + + + + + + await executeAsModal(async () => { + + for (let layer of activeLayers){ + await selectLayers([layer])// make sure only one layer is selected + await scaleLayer(layer,selection_info)//scale to selection size + await moveLayerExe(layer, selection_info)//move to selection + } + }, {'commandName': 'moveLayerExe'}) + + // await reselect(selection_info) + } catch (e) { + console.warn(e) + } + + +} + module.exports = { createSolidLayer, createEmptyGroup, @@ -1139,5 +1225,7 @@ module.exports = { saveUniqueDocumentIdExe, readUniqueDocumentIdExe, newExportPng, - mergeVisibleExe + mergeVisibleExe, + selectCanvasExe, + layerToSelection }