Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/models/LayerAIContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SmallDetailContext>; // 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<LayerAIContextHistory>; // the hisory of the context

constructor(
Expand All @@ -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;
Expand All @@ -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<boolean> {
return await hasMask(this.currentLayer);
}
Expand Down
27 changes: 27 additions & 0 deletions src/models/LayerDTO.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 15 additions & 2 deletions src/services/layer_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/ps_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}