Skip to content

Commit 7907565

Browse files
committed
refactor xd: create common and artboard filtering
1 parent 061859e commit 7907565

File tree

11 files changed

+298
-267
lines changed

11 files changed

+298
-267
lines changed

packages/octopus-psd/examples/node/convert-debug.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ async function convertDesign({
6060
})
6161

6262
const reader = await PSDFileReader.withRenderer({ path: filePath })
63+
6364
const sourceDesign = await reader.getSourceDesign()
6465
if (sourceDesign === null) {
6566
console.error('Creating SourceDesign Failed')

packages/octopus-psd/src/services/readers/psd-file-reader-common.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@ import type { SourceImage } from '../../entities/source/source-design.js'
1212
import type { Renderer } from '@opendesign/image-icc-profile-converter'
1313
import type { NodeChild, Layer, Group } from '@webtoon/psd-ts'
1414

15-
type NamedArtboard = {
16-
id: number
15+
type NamedPagedId = {
16+
id: string
1717
name: string
1818
}
1919

20-
type FileMeta = {
20+
export type DesignMeta = {
2121
designName: string
2222
content: {
23-
topLevelArtboards: NamedArtboard[]
23+
topLevelArtboards: NamedPagedId[]
24+
localComponents: NamedPagedId[]
25+
remoteComponents: {
26+
name: string
27+
id: string
28+
}[]
2429
}
2530
}
26-
2731
export type ConvertImageOptions = {
2832
buff: Uint8ClampedArray | Uint8Array
2933
width: number
@@ -296,9 +300,9 @@ export abstract class PSDFileReaderCommon {
296300
}
297301

298302
/**
299-
* Returns `FileMeta` with list of Artboards and designName
303+
* Returns `DesignMeta` with list of Artboards and designName
300304
*/
301-
async readFileMeta(): Promise<FileMeta | null> {
305+
async getDesignMeta(): Promise<DesignMeta | null> {
302306
const psd = await this._getPsd()
303307
if (!psd) {
304308
return null
@@ -307,14 +311,16 @@ export abstract class PSDFileReaderCommon {
307311
return {
308312
designName: psd.name,
309313
content: {
310-
topLevelArtboards: psd.children.reduce<NamedArtboard[]>((namedArtboards, child) => {
314+
topLevelArtboards: psd.children.reduce<NamedPagedId[]>((namedArtboards, child) => {
311315
if (isArtboard(child)) {
312-
const namedArtboard = { name: child.name, id: child.additionalProperties?.lyid?.value as number }
316+
const namedArtboard = { name: child.name, id: String(child.additionalProperties?.lyid?.value) }
313317
namedArtboards.push(namedArtboard)
314318
}
315319

316320
return namedArtboards
317321
}, []),
322+
localComponents: [],
323+
remoteComponents: [],
318324
},
319325
}
320326
}

packages/octopus-xd/examples/node/convert-debug.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs/promises'
21
import path from 'path'
32

43
import { displayPerf } from '@opendesign/octopus-common/dist/utils/console.js'
@@ -74,9 +73,10 @@ ${images.map((image) => ` file://${image}`).join('\n')}`)
7473
file://${manifest}`)
7574
})
7675

77-
const file = await fs.readFile(options.filename)
78-
const reader = new XDFileReader({ file, storeAssetsOnFs: true })
79-
const sourceDesign = await reader.sourceDesign
76+
const reader = new XDFileReader({ path: options.filename, storeAssetsOnFs: true })
77+
const sourceDesign = await reader.getSourceDesign({
78+
ids: ['43f6318e-2c94-4764-8849-f59b8e6b77a4', 'd7aff3d8-8d6d-462a-92dc-73edc53cbf20'],
79+
})
8080
const converter = createConverter({ sourceDesign })
8181
converter.convertDesign({ exporter })
8282
await exporter.completed()

packages/octopus-xd/examples/node/convert-local.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import fs from 'fs/promises'
21
import os from 'os'
32
import path from 'path'
43

@@ -9,9 +8,8 @@ import { createConverter, LocalExporter, XDFileReader } from '../../src/index-no
98
async function convert() {
109
const [filename] = process.argv.slice(2)
1110
const testDir = path.join(os.tmpdir(), uuidv4())
12-
const file = await fs.readFile(filename)
13-
const reader = new XDFileReader({ file, storeAssetsOnFs: true })
14-
const sourceDesign = await reader.sourceDesign
11+
const reader = new XDFileReader({ path: filename, storeAssetsOnFs: true })
12+
const sourceDesign = await reader.getSourceDesign()
1513
const converter = createConverter({ sourceDesign })
1614
const exporter = new LocalExporter({ path: testDir })
1715
await converter.convertDesign({ exporter })

packages/octopus-xd/src/entities/source/source-design.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,8 @@ import { SourceResources } from './source-resources.js'
77
import { Expander } from '../../services/conversion/expander/index.js'
88
import { PasteboardNormalizer } from '../../services/conversion/pasteboard-normalizer/index.js'
99

10-
import type { RawSourceInteractions } from './source-interactions.js'
11-
import type { RawSourceManifest } from './source-manifest.js'
12-
import type { RawArtboard, RawArtboardLike, RawPasteboard } from '../../typings/source/index.js'
13-
import type { RawResources } from '../../typings/source/resources.js'
14-
15-
type SourceDesignOptions = {
16-
manifest: {
17-
path: string
18-
rawValue: RawSourceManifest
19-
}
20-
resources: {
21-
path: string
22-
rawValue: RawResources
23-
}
24-
interactions: {
25-
path: string
26-
rawValue: RawSourceInteractions
27-
}
28-
images: {
29-
path: string
30-
getImageData: GetImageData
31-
}[]
32-
artboards: {
33-
path: string
34-
rawValue: RawArtboardLike
35-
}[]
36-
}
37-
38-
export type GetImageData = () => Promise<Uint8Array>
10+
import type { SourceEntry, GetImageData } from './source-entry.js'
11+
import type { RawArtboard, RawPasteboard } from '../../typings/source/index.js'
3912

4013
export class SourceDesign {
4114
private _manifest: SourceManifest
@@ -44,11 +17,9 @@ export class SourceDesign {
4417
private _artboards: SourceArtboard[]
4518
private _images: { path: string; getImageData: GetImageData }[]
4619

47-
constructor(options: SourceDesignOptions) {
20+
constructor(options: SourceEntry) {
4821
this._manifest = new SourceManifest({
49-
path: options.manifest.path,
5022
rawValue: options.manifest.rawValue,
51-
design: this,
5223
})
5324
this._resources = new SourceResources({
5425
path: options.resources.path,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { RawSourceInteractions } from './source-interactions.js'
2+
import type { RawSourceManifest } from './source-manifest.js'
3+
import type { RawArtboardLike } from '../../typings/source/artboard.js'
4+
import type { RawResources } from '../../typings/source/resources.js'
5+
6+
export type SourceEntry = {
7+
manifest: {
8+
path: string
9+
rawValue: RawSourceManifest
10+
}
11+
resources: {
12+
path: string
13+
rawValue: RawResources
14+
}
15+
interactions: {
16+
path: string
17+
rawValue: RawSourceInteractions
18+
}
19+
images: {
20+
path: string
21+
getImageData: GetImageData
22+
}[]
23+
artboards: {
24+
path: string
25+
rawValue: RawArtboardLike
26+
}[]
27+
}
28+
29+
export type GetImageData = () => Promise<Uint8Array>

packages/octopus-xd/src/entities/source/source-manifest.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { SourceDesign } from './source-design.js'
2-
31
export type RawGeneralEntry = {
42
id: string
53
name: string
@@ -23,19 +21,13 @@ export type RawSourceManifest = {
2321

2422
type SourceManifestOptions = {
2523
rawValue: RawSourceManifest
26-
path: string
27-
design: SourceDesign
2824
}
2925

3026
export class SourceManifest {
3127
protected _rawValue: RawSourceManifest
32-
private _path: string
33-
private _design: SourceDesign
3428

3529
constructor(options: SourceManifestOptions) {
36-
this._design = options.design
3730
this._rawValue = options.rawValue
38-
this._path = options.path
3931
}
4032

4133
get raw(): RawSourceManifest {

packages/octopus-xd/src/octopus-xd-converter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { pathBasename } from './utils/fs-path.js'
1010
import { readPackageMeta } from './utils/read-pkg-meta.js'
1111

1212
import type { SourceArtboard } from './entities/source/source-artboard.js'
13-
import type { GetImageData, SourceDesign } from './entities/source/source-design.js'
13+
import type { SourceDesign } from './entities/source/source-design.js'
14+
import type { GetImageData } from './entities/source/source-entry.js'
1415
import type { Exporter } from './services/conversion/exporter/index.js'
1516
import type { NodeFactories, WebFactories } from './services/general/platforms/index.js'
1617
import type { Logger } from './typings/index.js'

0 commit comments

Comments
 (0)