Skip to content

Commit f2f82bc

Browse files
committed
Split Icon
1 parent 0bffb1e commit f2f82bc

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

src/Element.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ export class Element {
132132
else {
133133
this.additionalProps = {}
134134
}
135-
136135
switch (this.node.type) {
137136
case 'ELLIPSE':
138137
this.additionalProps = {
@@ -172,6 +171,13 @@ export class Element {
172171
if (this.node.children.length > 1 && (await this.hasSpaceProps())) break
173172
// has instance type children, skip
174173
if (this.node.children.some((child) => child.type === 'INSTANCE')) break
174+
// if child is square, It is an icon asset
175+
if (
176+
this.node.width !== this.node.height &&
177+
this.node.children.length === 1 &&
178+
this.node.children.some((child) => child.width === child.height)
179+
)
180+
break
175181
const res = await checkSvgImageChildrenType(this.node)
176182
if (res) {
177183
if (res.type === 'SVG' && res.fill) {

src/__tests__/Element.test.ts

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,25 +146,71 @@ describe('Element', () => {
146146
)
147147
})
148148

149+
it('should divide wrapper and icon asset when children is a square', async () => {
150+
const element = createElement('FRAME', {
151+
width: '100px',
152+
height: '120px',
153+
children: [createNode('VECTOR', { width: '60px', height: '60px' })],
154+
})
155+
expect(await element.getComponentType()).toEqual('Box')
156+
expect(await element.render()).toEqual(
157+
'<Box w="100px" h="120px">\n <Image boxSize="60px" />\n</Box>',
158+
)
159+
})
160+
149161
describe('Overlap', async () => {
150162
it('should render Image with overlap', async () => {
151-
const element = createElement('INSTANCE', {
152-
name: 'image',
153-
width: '60px',
154-
height: '60px',
155-
children: [
156-
createNode('VECTOR', {
157-
fills: [],
158-
}),
159-
createNode('STAR', {
160-
fills: [],
161-
}),
162-
],
163-
})
164-
expect(await element.getComponentType()).toEqual('Image')
165-
expect(await element.render()).toEqual(
166-
'<Image boxSize="60px" src="image" />',
167-
)
163+
{
164+
const element = createElement('INSTANCE', {
165+
name: 'image',
166+
width: '60px',
167+
height: '60px',
168+
children: [
169+
createNode('VECTOR', {
170+
fills: [],
171+
}),
172+
createNode('STAR', {
173+
fills: [],
174+
}),
175+
],
176+
})
177+
expect(await element.getComponentType()).toEqual('Image')
178+
expect(await element.render()).toEqual(
179+
'<Image boxSize="60px" src="image" />',
180+
)
181+
}
182+
{
183+
const element = createElement('FRAME', {
184+
width: '100px',
185+
height: '120px',
186+
children: [
187+
createNode('RECTANGLE', {
188+
width: '100px',
189+
fills: [],
190+
height: '120px',
191+
children: [
192+
createNode('FRAME', {
193+
name: 'image',
194+
width: '60px',
195+
height: '60px',
196+
children: [
197+
createNode('VECTOR', {
198+
fills: [],
199+
}),
200+
createNode('STAR', {
201+
fills: [],
202+
}),
203+
],
204+
}),
205+
],
206+
}),
207+
],
208+
})
209+
expect(await element.getComponentType()).toEqual('Box')
210+
expect(await element.render()).toEqual(
211+
'<Box w="100px" h="120px">\n <Box w="100%" h="120px">\n <Image boxSize="60px" src="image" />\n </Box>\n</Box>',
212+
)
213+
}
168214
})
169215

170216
it('should render Image with overlap', async () => {
@@ -450,7 +496,7 @@ describe('Element', () => {
450496
const element = createElement('RECTANGLE', {
451497
name: 'image',
452498
width: '60px',
453-
height: '60px',
499+
height: '68px',
454500
fills: [
455501
{
456502
type: 'IMAGE',
@@ -459,7 +505,7 @@ describe('Element', () => {
459505
})
460506
expect(await element.getComponentType()).toEqual('Image')
461507
expect(await element.render()).toEqual(
462-
'<Image boxSize="60px" src="image" />',
508+
'<Image w="60px" h="68px" src="image" />',
463509
)
464510
}
465511
{

0 commit comments

Comments
 (0)