Skip to content

Commit 87b391f

Browse files
committed
Export Assets issue
1 parent 6c1755c commit 87b391f

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/Element.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ export class Element {
196196
}
197197
this.addAsset(this.node, 'png')
198198
}
199+
const css = await this.getCss()
200+
if (this.node.width === 1 && !('width' in css))
201+
this.additionalProps.w = '1px'
199202
break
200203
}
201204
case 'FRAME':
@@ -286,8 +289,14 @@ export class Element {
286289
return
287290
}
288291
if (this.parent) this.parent.addAsset(node, type)
289-
else
290-
this.assets[node.name + '.' + type] = async () => {
292+
else {
293+
let key = node.name
294+
let idx = 0
295+
while (key + '.' + type in this.assets) {
296+
key = node.name + '_' + idx
297+
idx++
298+
}
299+
this.assets[key + '.' + type] = async () => {
291300
const isSvg = type === 'svg'
292301
const options: ExportSettings = {
293302
format: isSvg ? 'SVG' : 'PNG',
@@ -303,6 +312,7 @@ export class Element {
303312
const data = await node.exportAsync(options)
304313
return data
305314
}
315+
}
306316
}
307317

308318
async getComponents(): Promise<Record<string, () => Promise<string>>> {

src/__tests__/Element.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function createNode(
1818
componentProperties,
1919
getMainComponentAsync,
2020
visible = true,
21+
css,
2122
...props
2223
}: {
2324
[_: string]: any
@@ -34,7 +35,7 @@ function createNode(
3435
): SceneNode {
3536
const ret = {
3637
type,
37-
getCSSAsync: async () => props,
38+
getCSSAsync: async () => css ?? props,
3839
exportAsync: async () => '<svg>\n<path/>\n</svg>',
3940
getStyledTextSegments: () => styledTextSegments,
4041
layoutSizingHorizontal,
@@ -1450,6 +1451,15 @@ describe('Element', () => {
14501451
expect(await element.getComponentType()).toEqual('Box')
14511452
expect(await element.render()).toEqual('<Box bg="red" />')
14521453
}
1454+
{
1455+
const element = createElement('RECTANGLE', {
1456+
width: '1px',
1457+
height: '10px',
1458+
fills: [],
1459+
css: { height: '10px' },
1460+
})
1461+
expect(await element.render()).toEqual('<Box h="10px" w="1px" />')
1462+
}
14531463
})
14541464
})
14551465

@@ -1696,6 +1706,7 @@ export function Component(props: ComponentProps) {
16961706
await element.render()
16971707
expect(await element.getAssets()).toEqual({
16981708
'image.svg': expect.any(Function),
1709+
'image_0.svg': expect.any(Function),
16991710
})
17001711
}
17011712
})

src/commands/__tests__/exportAssets.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,13 @@ describe('exportAssets', () => {
163163
],
164164
})
165165
;(globalThis as any).figma.currentPage.selection = [node]
166+
vi.spyOn(console, 'error').mockImplementation(() => {})
166167
;(node.exportAsync as any) = vi.fn().mockRejectedValue('test')
167168
await exportAssets()
168169
expect(notifyMock).toHaveBeenCalledWith('Error exporting assets', {
169170
timeout: 3000,
170171
error: true,
171172
})
173+
vi.spyOn(console, 'error').mockRestore()
172174
})
173175
})

src/commands/__tests__/exportComponents.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@ describe('exportComponents', () => {
164164
],
165165
})
166166
;(globalThis as any).figma.currentPage.selection = [node]
167+
vi.spyOn(console, 'error').mockImplementation(() => {})
167168
await exportComponents()
168169
expect(notifyMock).toHaveBeenCalledWith('Error exporting components', {
169170
timeout: 3000,
170171
error: true,
171172
})
173+
vi.spyOn(console, 'error').mockRestore()
172174
})
173175
})

0 commit comments

Comments
 (0)