Skip to content

Commit

Permalink
[api change] Make additionalProperties a Record
Browse files Browse the repository at this point in the history
Sorry I didn't make this change when introducing this feature, but I
didn't know enough TypeScript to make it work. Anyhow, Record type is
better here, since it makes checking keys much more efficient.

Also, a couple of additional quality-of-life-changes:
- expose `AliKey` as part of public API,
- expose `additionalProperties` on Group (things like `ArtboardData` are
on Group, not Layer).
  • Loading branch information
Lucas Czaplinski authored and pastelmind committed Nov 14, 2022
1 parent 5e1fd35 commit 33fd2f9
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
8 changes: 7 additions & 1 deletion packages/psd/src/classes/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright 2021-present NAVER WEBTOON
// MIT License

import {GroupFrame} from "../sections";
import {GroupFrame, LayerProperties} from "../sections";
import {NodeChild, NodeParent} from "./Node";
import {NodeBase} from "./NodeBase";

Expand Down Expand Up @@ -30,6 +30,12 @@ export class Group implements NodeBase<NodeParent, NodeChild> {
return this.parent.composedOpacity * (this.opacity / 255);
}

get additionalProperties():
| LayerProperties["additionalLayerProperties"]
| undefined {
return this.layerFrame?.layerProperties.additionalLayerProperties;
}

addChild(node: NodeChild): void {
this.children.push(node);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/psd/src/classes/Psd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Psd extends Synthesizable implements NodeBase<never, NodeChild> {
public readonly globalLightAngle?: number = undefined;
public readonly globalLightAltitude?: number = undefined;
public readonly resolutionInfo?: ResolutionInfo = undefined;
public readonly additionalLayerProperties: AdditionalLayerProperties = [];
public readonly additionalLayerProperties: AdditionalLayerProperties = {};

static parse(buffer: ArrayBuffer): Psd {
const parsingResult = parse(buffer);
Expand Down
2 changes: 2 additions & 0 deletions packages/psd/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export {
SliceOrigin,
DimensionUnit,
ResolutionUnit,
AliKey,
PathRecordType,
} from "./interfaces";
export type {Guide} from "./interfaces";

Expand Down
10 changes: 8 additions & 2 deletions packages/psd/src/sections/LayerAndMaskInformation/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export interface Frame {
layerRecord?: LayerRecord;
}

export type AdditionalLayerProperties = AdditionalLayerInfo[];
export type AdditionalLayerProperties = {
[K in AdditionalLayerInfo as K["key"]]?: K;
};

export interface LayerProperties {
name: string;
Expand Down Expand Up @@ -88,6 +90,10 @@ export const createLayerProperties = (
additionalLayerInfos,
} = layerRecord;

const additionalLayerProperties = Object.fromEntries(
additionalLayerInfos.map((ali) => [ali.key, ali])
) as AdditionalLayerProperties;

return {
name,
top,
Expand All @@ -103,7 +109,7 @@ export const createLayerProperties = (
text: layerText,
textProperties: engineData,
maskData,
additionalLayerProperties: additionalLayerInfos,
additionalLayerProperties,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ export function readGlobalAdditionalLayerInformation(
);
}

return additionalLayerInfos;
return Object.fromEntries(
additionalLayerInfos.map((ali) => [ali.key, ali])
) as AdditionalLayerProperties;
}

function readLayerRectangle(cursor: Cursor): [number, number, number, number] {
Expand Down
7 changes: 2 additions & 5 deletions packages/psd/tests/integration/fillAdjustments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import * as path from "path";
import {beforeAll, describe, expect, it} from "vitest";

import type Psd from "../../src/index";
import PSD from "../../src/index";
import {AliKey} from "../../src/interfaces";
import PSD, {AliKey} from "../../src/index";

const FIXTURE_DIR = path.join(__dirname, "fixtures");

Expand All @@ -22,9 +21,7 @@ describe("adjustements parsing", () => {
});

it("describe HSL changes", () => {
const hue = psd.layers[10].additionalProperties.find(
({key}) => key === AliKey.HueSaturation
);
const hue = psd.layers[10].additionalProperties[AliKey.HueSaturation];

expect(hue).toStrictEqual({
adjustment: [
Expand Down
22 changes: 7 additions & 15 deletions packages/psd/tests/integration/placedLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import {beforeAll, describe, expect, it} from "vitest";

import type Psd from "../../src/index";
import PSD from "../../src/index";
import {
AliKey,
LinkedLayerAliBlock,
SmartObjectPlacedLayerDataAliBlock,
StringDescriptorValue,
} from "../../src/interfaces";
import {AliKey, StringDescriptorValue} from "../../src/interfaces";

const FIXTURE_DIR = path.join(__dirname, "fixtures");

Expand All @@ -26,29 +21,26 @@ describe("placed layer data parsing", () => {
});

it("should contain links to placed files inside layers", () => {
const placedObject = psd.layers[0].additionalProperties.find(
({key}) => key === AliKey.PlacedLayerData
) as SmartObjectPlacedLayerDataAliBlock;
const placedObject =
psd.layers[0].additionalProperties[AliKey.PlacedLayerData];

const id = placedObject.data.descriptor.items.get(
const id = placedObject?.data.descriptor.items.get(
"Idnt"
) as StringDescriptorValue;
expect(id.value).toStrictEqual("5a96c404-ab9c-1177-97ef-96ca454b82b7");
});

it("should contain embedded files as extra resources", () => {
const linkedLayer = psd.additionalLayerProperties.find(
({key}) => key === AliKey.LinkedLayer2
) as LinkedLayerAliBlock;
expect(linkedLayer.layers[0]).toContain({
const linkedLayer = psd.additionalLayerProperties[AliKey.LinkedLayer2];
expect(linkedLayer?.layers[0]).toContain({
uniqueId: "5a96c404-ab9c-1177-97ef-96ca454b82b7",
filename: "linked-layer.png",
filetype: "png ",
});

const hash = crypto
.createHash("sha256")
.update(linkedLayer.layers[0].contents)
.update(linkedLayer?.layers[0].contents ?? "")
.digest("hex");

// NOTE: when changing the hash, please make sure the result is coherent :)
Expand Down
7 changes: 2 additions & 5 deletions packages/psd/tests/integration/vectorMask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import * as path from "path";
import {beforeAll, describe, expect, it} from "vitest";

import type Psd from "../../src/index";
import PSD from "../../src/index";
import {AliKey, PathRecordType} from "../../src/interfaces";
import PSD, {AliKey, PathRecordType} from "../../src/index";

const FIXTURE_DIR = path.join(__dirname, "fixtures");

Expand All @@ -20,9 +19,7 @@ describe("vector mask parsing", () => {
});

it("should return pathRecords to build vector mask", () => {
const vmsk = psd.layers[0].additionalProperties.find(
({key}) => key === AliKey.VectorMaskSetting1
);
const vmsk = psd.layers[0].additionalProperties[AliKey.VectorMaskSetting1];

expect(vmsk).toStrictEqual({
key: "vmsk",
Expand Down

0 comments on commit 33fd2f9

Please sign in to comment.