diff --git a/src/models/LayerAIContext.ts b/src/models/LayerAIContext.ts index cc860f7..afac897 100644 --- a/src/models/LayerAIContext.ts +++ b/src/models/LayerAIContext.ts @@ -14,12 +14,14 @@ import _ from 'lodash'; import photoshop from 'photoshop'; import { applyMask, duplicateLayer, hasMask } from 'services/layer_service'; import ContextObject from './ContextObject'; +import { LayerDTO } from './LayerDTO'; +import { getPSLayerByID } from 'utils/ps_utils'; export default class LayerAIContext extends ContextObject { name: string; smallDetails: Array; // The details from the above object generationModelName: string; - currentLayer: Layer; // the layer that the context is assigned to + _currentLayer: Layer | LayerDTO; // the layer that the context is assigned to history: Array; // the hisory of the context constructor( @@ -39,7 +41,7 @@ export default class LayerAIContext extends ContextObject { ) { super(); this.name = options.name; - this.currentLayer = options.currentLayer ?? this.currentLayer; + this._currentLayer = options.currentLayer ?? this.currentLayer; this.generationModelName = options.generationModelName ?? this.generationModelName; this.currentPrompt = options.currentPrompt ?? this.currentPrompt; @@ -52,6 +54,12 @@ export default class LayerAIContext extends ContextObject { this.tags = options.tags ?? this.tags; } + get currentLayer(): Layer { + return getPSLayerByID( + this._currentLayer?.id ?? (this._currentLayer as LayerDTO)._id + ); + } + public async hasLayerMask(): Promise { return await hasMask(this.currentLayer); } diff --git a/src/models/LayerDTO.ts b/src/models/LayerDTO.ts new file mode 100644 index 0000000..05011c4 --- /dev/null +++ b/src/models/LayerDTO.ts @@ -0,0 +1,27 @@ +export class LayerDTO { + name: string; + _docid?: number; + _id?: number; + + constructor(name: string, id: number, _docid?: number, _id?: number) { + this.name = name; + this._docid = _docid; + this._id = _id; + } + + get id(): number { + return this._id; + } + + set id(id: number) { + this._docid = id; + } + + get docid(): number { + return this._id; + } + + set docid(id: number) { + this._docid = id; + } +} diff --git a/src/services/layer_service.ts b/src/services/layer_service.ts index 44f322e..fe08164 100644 --- a/src/services/layer_service.ts +++ b/src/services/layer_service.ts @@ -13,6 +13,8 @@ import { AngleValue, PercentValue, PixelValue } from 'photoshop/util/unit'; import { Document } from 'photoshop/dom/Document'; import { storage } from 'uxp'; import { getHeightScale, getWidthScale } from 'utils/layer_utils'; +import { getPSLayerByID } from 'utils/ps_utils'; +import { LayerDTO } from 'models/LayerDTO'; const lfs = storage.localFileSystem; const bp = photoshop.action.batchPlay; @@ -233,13 +235,24 @@ export async function selectLayerMask(layer: Layer) { * This function will duplicate the given layer and return a reference to it. */ export async function duplicateLayer( - layer: Layer, + layerObj: Layer | LayerDTO, relativeObject?: Document | Layer, insertionLocation?: ElementPlacement, name?: string ) { + let layerFromPhotoshop = layerObj; + + if (layerObj.id === undefined) { + layerFromPhotoshop = getPSLayerByID((layerObj as LayerDTO)._id); + } return await executeInPhotoshop(duplicateLayer, async () => { - return await layer.duplicate(relativeObject, insertionLocation, name); + // get layer from photoshop document layers using the layer id + + return ((await layerFromPhotoshop) as Layer).duplicate( + relativeObject, + insertionLocation, + name + ); }); } diff --git a/src/utils/ps_utils.ts b/src/utils/ps_utils.ts index 2b992b0..e5889c4 100644 --- a/src/utils/ps_utils.ts +++ b/src/utils/ps_utils.ts @@ -5,3 +5,9 @@ export function getPhotoshopLayerFromName(layerName: string) { (layer) => layerName == layer.name )[0]; } + +export function getPSLayerByID(layerID: number) { + return photoshop.app.activeDocument?.layers?.filter( + (layer) => layerID == layer.id + )[0]; +}