Skip to content

Commit 6975d96

Browse files
committed
update exporter in ai
1 parent 3a38f43 commit 6975d96

File tree

14 files changed

+74
-66
lines changed

14 files changed

+74
-66
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { uniqueIdFactory } from '@opendesign/octopus-common/dist/utils/common.js
33
import { SourceArtboard } from './source-artboard.js'
44

55
import type { Metadata } from '../../services/readers/ai-file-reader-common.js'
6-
import type { SourceImage, SourceTree } from '../../typings/index.js'
6+
import type { SourceTree } from '../../typings/index.js'
77
import type { AdditionalTextData } from '../../typings/raw/index.js'
8+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
89
import type { Nullish } from '@opendesign/octopus-common/dist/utility-types.js'
910

1011
export class SourceDesign {

packages/octopus-ai/src/services/conversion/design-converter/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { TextLayerGroupingservice } from '../text-layer-grouping-service/index.j
1111
import type { SourceArtboard } from '../../../entities/source/source-artboard.js'
1212
import type { SourceDesign } from '../../../entities/source/source-design.js'
1313
import type { OctopusAIConverter } from '../../../octopus-ai-converter.js'
14-
import type { SourceImage } from '../../../typings/index.js'
1514
import type { Manifest } from '../../../typings/manifest/index.js'
1615
import type { Octopus } from '../../../typings/octopus/index.js'
1716
import type { AdditionalTextData } from '../../../typings/raw/index.js'
1817
import type { Exporter } from '../exporters/index.js'
18+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
1919
import type { SafeResult } from '@opendesign/octopus-common/dist/utils/queue.js'
2020

2121
type DesignConverterGeneralOptions = {
@@ -133,8 +133,7 @@ export class DesignConverter {
133133
const images = await Promise.all(
134134
artboardImages.map(async (image) => {
135135
const imageId = image.id
136-
const rawData = await image.getImageData()
137-
const imagePath = (await rejectTo(exporter?.exportImage?.(image.id, rawData) ?? Promise.reject(''))) as string
136+
const imagePath = (await rejectTo(exporter?.exportImage?.(image) ?? Promise.reject(''))) as string
138137
this.manifest.setExportedImage(imageId, imagePath)
139138

140139
return image

packages/octopus-ai/src/services/conversion/design-converter/test/index.test.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ describe('DesignConverter', () => {
101101

102102
const sourceDesign = {
103103
images: [
104-
{ id: 'image-1.jpg', path: 'path/image-1.jpg', getImageData: async () => 'base64;image-1' },
105-
{ id: 'image-2.jpg', path: 'path/image-1.jpg', getImageData: async () => 'base64;image-2' },
106-
{ id: 'image-3.jpg', path: 'path/image-3.jpg', getImageData: async () => 'base64;image-3' },
104+
{ id: 'image-1.jpg', getImageData: async () => 'base64;image-1' },
105+
{ id: 'image-2.jpg', getImageData: async () => 'base64;image-2' },
106+
{ id: 'image-3.jpg', getImageData: async () => 'base64;image-3' },
107107
],
108108
}
109109

@@ -113,7 +113,7 @@ describe('DesignConverter', () => {
113113
.mockReturnValueOnce({ id: 1, value: { id: '1' }, error: null, time: 5 })
114114

115115
const exporter: any = {
116-
exportImage: vi.fn().mockImplementation(async (imagePath) => 'root/' + imagePath),
116+
exportImage: vi.fn().mockImplementation(async (image) => 'root/' + image.id),
117117
exportArtboard: vi.fn().mockImplementation(async (artboard) => 'root/' + artboard.value.id),
118118
}
119119

@@ -132,8 +132,15 @@ describe('DesignConverter', () => {
132132
images: [sourceDesign.images[0], sourceDesign.images[2]],
133133
})
134134

135-
expect(exporter.exportImage).toHaveBeenCalledWith('image-1.jpg', 'base64;image-1')
136-
expect(exporter.exportImage).toHaveBeenCalledWith('image-3.jpg', 'base64;image-3')
135+
expect(exporter.exportImage).toHaveBeenCalledWith({
136+
getImageData: expect.any(Function),
137+
id: 'image-1.jpg',
138+
})
139+
140+
expect(exporter.exportImage).toHaveBeenCalledWith({
141+
getImageData: expect.any(Function),
142+
id: 'image-3.jpg',
143+
})
137144

138145
expect(manifestInstanceMock.setExportedImage).toHaveBeenCalledWith('image-1.jpg', 'root/image-1.jpg')
139146
expect(manifestInstanceMock.setExportedImage).toHaveBeenCalledWith('image-3.jpg', 'root/image-3.jpg')
Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,39 @@
11
import type { SourceArtboard } from '../../../entities/source/source-artboard.js'
22
import type { SourceDesign } from '../../../entities/source/source-design.js'
33
import type { ArtboardConversionResult, DesignConversionResult } from '../design-converter/index.js'
4+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
45

56
export type AuxiliaryData = { metadata: string; additionalTextData: string | null }
67

7-
export interface Exporter {
8-
exportAuxiliaryData?(_design: SourceDesign): Promise<AuxiliaryData>
9-
exportImage?(_path: string, _data: Uint8Array): Promise<unknown>
10-
exportArtboard?(_source: SourceArtboard, _artboard: ArtboardConversionResult): Promise<unknown>
11-
exportManifest?(_manifest: DesignConversionResult): Promise<unknown>
12-
getBasePath(): Promise<string>
13-
finalizeExport(): void
8+
export abstract class Exporter {
9+
exportImage(image: SourceImage): Promise<unknown> {
10+
const { id } = image
11+
console.log('calling default export method `exportImage()`')
12+
return Promise.resolve(id)
13+
}
14+
15+
finalizeExport(): void {
16+
console.log('calling default export method `finalizeExport()`')
17+
Promise.resolve()
18+
}
19+
20+
exportArtboard(_source: SourceArtboard, _artboard: ArtboardConversionResult): Promise<unknown> {
21+
console.log('calling default export method `exportArtboard()`')
22+
return Promise.resolve()
23+
}
24+
25+
exportManifest(_manifest: DesignConversionResult): Promise<unknown> {
26+
console.log('calling default export method `exportManifest()`')
27+
return Promise.resolve()
28+
}
29+
30+
exportAuxiliaryData(_design: SourceDesign): Promise<AuxiliaryData> {
31+
console.log('calling default export method `exportAuxiliaryData()`')
32+
return Promise.resolve({ metadata: '', additionalTextData: null })
33+
}
34+
35+
getBasePath(): Promise<string> {
36+
console.log('calling default export method `getBasePath()`')
37+
return Promise.resolve('')
38+
}
1439
}

packages/octopus-ai/src/services/conversion/exporters/local-exporter.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Exporter, AuxiliaryData } from './index.js'
1111
import type { SourceArtboard } from '../../../entities/source/source-artboard.js'
1212
import type { SourceDesign } from '../../../entities/source/source-design.js'
1313
import type { ArtboardConversionResult, DesignConversionResult } from '../design-converter/index.js'
14+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
1415
import type { DetachedPromiseControls } from '@opendesign/octopus-common/dist/utils/async.js'
1516

1617
type LocalExporterOptions = {
@@ -103,12 +104,12 @@ export class LocalExporter implements Exporter {
103104

104105
/**
105106
* Exports given Image into folder specified in `LocalExporter.IMAGES_DIR_NAME`
106-
* @param {string} name Name of the exported Image
107-
* @param {Buffer} data Data representation of given image
107+
* @param {SourceImage} with signature {id:string, getImageData: () => Promise<Uint8Array>}
108108
* @returns {Promise<string>} which designates path to the exported Image
109109
*/
110-
exportImage(name: string, data: Buffer): Promise<string> {
111-
return this._save(path.join(LocalExporter.IMAGES_DIR_NAME, path.basename(name)), data)
110+
async exportImage(image: SourceImage): Promise<string> {
111+
const data = await image.getImageData()
112+
return this._save(path.join(LocalExporter.IMAGES_DIR_NAME, path.basename(image.id)), Buffer.from(data))
112113
}
113114

114115
finalizeExport(): void {

packages/octopus-ai/src/services/conversion/exporters/temp-exporter.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Exporter, AuxiliaryData } from './index.js'
1111
import type { SourceArtboard } from '../../../entities/source/source-artboard.js'
1212
import type { SourceDesign } from '../../../entities/source/source-design.js'
1313
import type { ArtboardConversionResult, DesignConversionResult } from '../design-converter/index.js'
14+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
1415
import type { DetachedPromiseControls } from '@opendesign/octopus-common/dist/utils/async.js'
1516

1617
type TempExporterOptions = {
@@ -26,6 +27,7 @@ type TempExporterOptions = {
2627
/**
2728
* Exporter created to be used in manual runs.
2829
*/
30+
2931
export class TempExporter extends EventEmitter implements Exporter {
3032
private _outputDir: Promise<string>
3133
private _tempDir: string
@@ -132,8 +134,9 @@ export class TempExporter extends EventEmitter implements Exporter {
132134
* @param {Buffer} data Data representation of given image
133135
* @returns {Promise<string>} which designates path to the exported Image
134136
*/
135-
async exportImage(name: string, data: Buffer): Promise<string> {
136-
return this._save(path.join(TempExporter.IMAGES_DIR_NAME, path.basename(name)), data)
137+
async exportImage(image: SourceImage): Promise<string> {
138+
const data = await image.getImageData()
139+
return this._save(path.join(TempExporter.IMAGES_DIR_NAME, path.basename(image.id)), Buffer.from(data))
137140
}
138141

139142
finalizeExport(): void {
Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,6 @@
1-
import type { AuxiliaryData } from './index.js'
2-
import type { SourceArtboard } from '../../../entities/source/source-artboard.js'
3-
import type { SourceDesign } from '../../../octopus-ai-converter.js'
4-
import type { ArtboardConversionResult, DesignConversionResult } from '../design-converter/index.js'
1+
import { Exporter } from './index.js'
52

63
/**
74
* Minimalistic exporter used for web build
85
*/
9-
export class WebExporter {
10-
exportImage(name: string, _data: Uint8Array): Promise<unknown> {
11-
return Promise.resolve(name)
12-
}
13-
14-
getBasePath(): Promise<string> {
15-
return Promise.resolve('')
16-
}
17-
18-
finalizeExport(): void {
19-
Promise.resolve()
20-
}
21-
exportArtboard(_source: SourceArtboard, _artboard: ArtboardConversionResult): Promise<unknown> {
22-
console.log('Exporting artboard')
23-
return Promise.resolve()
24-
}
25-
26-
exportManifest(_manifest: DesignConversionResult): Promise<unknown> {
27-
console.log('Exporting manifest')
28-
return Promise.resolve()
29-
}
30-
31-
exportAuxiliaryData(_design: SourceDesign): Promise<AuxiliaryData> {
32-
console.log('Exporting auxiliary data')
33-
return Promise.resolve({ metadata: '', additionalTextData: null })
34-
}
35-
}
6+
export class WebExporter extends Exporter {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { SourceDesign } from '../../entities/source/source-design.js'
22

3-
import type { SourceImage, SourceTree } from '../../typings/index.js'
3+
import type { SourceTree } from '../../typings/index.js'
44
import type { AdditionalTextData, RawArtboardEntry } from '../../typings/raw/index.js'
5-
import type { DesignMeta } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
5+
import type { DesignMeta, SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
66

77
export type Metadata = {
88
version: string

packages/octopus-ai/src/services/readers/node/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { v4 as uuidv4 } from 'uuid'
88

99
import { AIFileReaderCommon } from '../ai-file-reader-common.js'
1010

11-
import type { SourceImage } from '../../../typings/index.js'
1211
import type { AdditionalTextData, RawArtboardEntry } from '../../../typings/raw/index.js'
1312
import type { FsContext } from '@opendesign/illustrator-parser-pdfcpu/fs_context'
13+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
1414

1515
type AIFileReaderOptions = {
1616
/** Path to the .ai file. */

packages/octopus-ai/src/services/readers/node/test/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ describe('AIFileReader', () => {
2424
const privateDataMock = Promise.resolve({ Textlayers: [], LayerNames: [] })
2525
vi.mocked(FSContext).mockResolvedValueOnce(ctxMock)
2626
vi.mocked(ArtBoardRefs).mockReturnValueOnce([{ idx: 1 }, { idx: 2 }] as any)
27-
vi.mocked(ArtBoard).mockImplementation(async (_, ref) => ({ id: `${ref.idx}` } as any))
27+
vi.mocked(ArtBoard).mockImplementation(async (_, ref) => ({ Id: ref.idx } as any))
2828
vi.mocked(PrivateData).mockResolvedValueOnce(await privateDataMock)
2929

3030
const result = await AIFileReader.prototype['_getSourceData']()
3131

3232
expect(mkdir).toHaveBeenCalledWith('root/path', { recursive: true })
3333
expect(result).toEqual({
3434
additionalTextData: { LayerNames: [], Textlayers: [] },
35-
artboards: [{ id: '1' }, { id: '2' }],
35+
artboards: [{ Id: 1 }, { Id: 2 }],
3636
metadata: { version: '1.0.1' },
3737
})
3838
expect(AIFileReader.prototype['_images']).toEqual(ctxMock.Bitmaps)

packages/octopus-ai/src/services/readers/web/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { WASMContext } from '@opendesign/illustrator-parser-pdfcpu/wasm_context'
33

44
import { AIFileReaderCommon } from '../ai-file-reader-common.js'
55

6-
import type { SourceImage } from '../../../typings/index.js'
76
import type { AdditionalTextData, RawArtboardEntry } from '../../../typings/raw/index.js'
87
import type { BitmapReader, WasmContext } from '@opendesign/illustrator-parser-pdfcpu/wasm_context'
8+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
99

1010
type AIFileReaderOptions = {
1111
/** Uint8Array representation of converted .ai file */

packages/octopus-ai/src/typings/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import type { AdditionalTextData, RawArtboardEntry } from './raw/index.js'
44
import type { Metadata } from '../services/readers/ai-file-reader-common.js'
5+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
56

67
export interface Logger {
78
fatal: Function
@@ -25,8 +26,6 @@ export type GradientStop = {
2526

2627
export type Color = { r: number; g: number; b: number; a: number }
2728

28-
export type SourceImage = { id: string; getImageData: () => Promise<Uint8Array> }
29-
3029
export type SourceTree = {
3130
metadata: Metadata
3231
images: SourceImage[]

packages/octopus-common/src/typings/octopus-common/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { Octopus as OctopusRaw } from '@opendesign/octopus-ts'
22

33
export type Octopus = OctopusRaw['schemas']
4-
54
export type ComponentMeta = { id: string; name: string; role: 'ARTBOARD' | 'COMPONENT' }
65
export type PageMeta = { id: string; name: string; children: ComponentMeta[] }
76
export type Origin = { name: 'FIGMA' | 'ILLUSTRATOR' | 'XD' | 'PHOTOSHOP'; version: string }
8-
7+
export type SourceImage = { id: string; getImageData: () => Promise<Uint8Array> }
98
export type DesignMeta = {
109
pages: PageMeta[]
1110
components: ComponentMeta[]

packages/octopus-psd/src/services/exporters/local-exporter.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { stringify } from '../../utils/stringify.js'
1010

1111
import type { AbstractExporter } from './abstract-exporter.js'
1212
import type { ComponentConversionResult, DesignConversionResult } from '../conversion/design-converter.js'
13+
import type { SourceImage } from '@opendesign/octopus-common/dist/typings/octopus-common/index.js'
1314
import type { DetachedPromiseControls } from '@opendesign/octopus-common/dist/utils/async.js'
1415

1516
export type LocalExporterOptions = {
@@ -85,8 +86,10 @@ export class LocalExporter implements AbstractExporter {
8586
* @param {Uint8Array} data image data
8687
* @returns {Promise<string>} returns path to the exported Image
8788
*/
88-
async exportImage(name: string, data: Uint8Array): Promise<string> {
89-
return this._save(path.join(LocalExporter.IMAGES_DIR_NAME, path.basename(name)), Buffer.from(data))
89+
async exportImage(image: SourceImage): Promise<string> {
90+
const { id } = image
91+
const data = await image.getImageData()
92+
return this._save(path.join(LocalExporter.IMAGES_DIR_NAME, path.basename(id)), Buffer.from(data))
9093
}
9194

9295
/**

0 commit comments

Comments
 (0)