Skip to content

Commit 37e9774

Browse files
committed
fix border & fill's fillType, support color & gradient
1 parent 45cf9c4 commit 37e9774

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed

src/transform.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,10 @@ function getStyleInfo (style) {
9494
}
9595
}
9696

97-
const FILLTYPE_MAP = {
98-
'0': 'color'
99-
}
10097

98+
const FILL_TYPES = ['color', 'gradient']
10199
const BORDER_POSITIONS = ['center', 'inside', 'outside']
100+
const GRADIENT_TYPES = ['linear', 'radial', 'angular']
102101
/**
103102
* Transform layer.style.borders
104103
* @param {Array} borders border style list
@@ -108,12 +107,18 @@ function transformBorders (borders) {
108107
if (!borders || !borders.length) return []
109108
return borders.filter(v => v.isEnabled)
110109
.map(v => {
111-
return {
112-
fillType: FILLTYPE_MAP[v.fillType],
110+
const fillType = FILL_TYPES[v.fillType]
111+
const borderData = {
112+
fillType,
113113
position: BORDER_POSITIONS[v.position],
114-
thickness: v.thickness,
115-
color: transformColor(v.color)
114+
thickness: v.thickness
115+
}
116+
if (fillType === 'color') {
117+
borderData.color = transformColor(v.color)
118+
} else if (fillType === 'gradient') {
119+
borderData.gradient = transformGradient(v.gradient)
116120
}
121+
return borderData
117122
})
118123
}
119124

@@ -126,11 +131,16 @@ function transformFills (fills) {
126131
if (!fills || !fills.length) return []
127132
return fills.filter(v => v.isEnabled)
128133
.map(v => {
129-
return {
130-
fillType: FILLTYPE_MAP[v.fillType],
131-
thickness: v.thickness,
132-
color: transformColor(v.color)
134+
const fillType = FILL_TYPES[v.fillType]
135+
const fillData = {
136+
fillType
133137
}
138+
if (fillType === 'color') {
139+
fillData.color = transformColor(v.color)
140+
} else if (fillType === 'gradient') {
141+
fillData.gradient = transformGradient(v.gradient)
142+
}
143+
return fillData
134144
})
135145
}
136146

@@ -176,6 +186,30 @@ function transformColor (color) {
176186
}
177187
}
178188

189+
function transformGradient (gradient) {
190+
const stops = gradient.stops.map(stop => {
191+
return {
192+
color: transformColor(stop.color),
193+
position: stop.position
194+
}
195+
})
196+
const data = {
197+
type: GRADIENT_TYPES[gradient.gradientType],
198+
colorStops: stops,
199+
from: transformPosition(gradient.from),
200+
to: transformPosition(gradient.to),
201+
}
202+
return data
203+
}
204+
205+
function transformPosition (position) {
206+
const parts = position.slice(1, -1).split(/,\s*/)
207+
return {
208+
x: +parts[0],
209+
y: +parts[1]
210+
}
211+
}
212+
179213
/**
180214
* transform artboard.
181215
* @param {Object} artboard artboard data

0 commit comments

Comments
 (0)