-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.ts
169 lines (164 loc) · 5.93 KB
/
assets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import sketch, { Document, AnyLayer, Slice, Artboard } from 'sketch/dom'
import { write, readPluginAsset } from '../util/fs'
import { iterateDocument, isIgnored, isSlice9 } from '../util/dom'
import { slugifyName, childName, stringSort } from '../util/string'
import { template, replace } from '../util/template'
import { disclaimer } from './disclaimer'
export function assetPath (name: string, size: string, fileFormat: string): string {
let fileName = slugifyName(name).join('/')
if (size !== '1' && size !== '1x') {
fileName = `${fileName}@${size}`
}
return `assets/${fileName}.${fileFormat}`
}
export function assetPathForLayer (item: AnyLayer, artboard: Artboard | null, suffix: string = ''): string {
if (isIgnored(item)) {
throw new Error(`Layer ${item.name} is ignored and should not be exported.`)
}
if (item.exportFormats.length === 0) {
throw new Error(`Layer ${item.name} is not exported and cant be turned into an asset path.`)
}
return assetPath((artboard === null || artboard === undefined ? item : artboard).name + suffix, '1', item.exportFormats[0].fileFormat)
}
export interface ISlice9 {
path: (row: number, column: number) => string
width: number
height: number
slice: {
x: number
y: number
width: number
height: number
}
}
function slice9Export (item: Slice, target: (path: string) => string, slice9s: { [assetName: string]: ISlice9 }, idNameMap: { [id: string]: string }): void {
const artboard = item.getParentArtboard()
if (artboard === null || artboard === undefined) {
return
}
const workSlice = item.duplicate()
for (let row = 0; row < 3; row++) {
const y = row === 0 ? 0 : row === 1 ? item.frame.y : item.frame.y + item.frame.height
const height = row === 0 ? item.frame.y : row === 1 ? item.frame.height : artboard.frame.height - item.frame.height - item.frame.y
for (let column = 0; column < 3; column++) {
const x = column === 0 ? 0 : column === 1 ? item.frame.x : item.frame.x + item.frame.width
const width = column === 0 ? item.frame.x : column === 1 ? item.frame.width : artboard.frame.width - item.frame.width - item.frame.x
workSlice.name = `slice-9-${row}-${column}`
workSlice.frame.x = x
workSlice.frame.y = y
workSlice.frame.width = width
workSlice.frame.height = height
workSlice.exportFormats.forEach(format => {
const buffer = sketch.export(workSlice, {
output: false,
scales: format.size
})
write(target(assetPath(`${artboard.name}-${row}-${column}`, format.size, format.fileFormat)), buffer)
})
}
}
const slice9Name = childName(artboard.name)
slice9s[slice9Name] = {
width: artboard.frame.width,
height: artboard.frame.height,
path: (row: number, column: number) => assetPathForLayer(item, artboard, `-${row}-${column}`),
slice: {
x: item.frame.x,
y: item.frame.y,
width: item.frame.width,
height: item.frame.height
}
}
idNameMap[artboard.id] = slice9Name
workSlice.remove()
}
export function writeAssets (document: Document, target: (path: string) => string): { [id: string]: string } {
const idNameMap: { [id: string]: string } = {}
const assets: { [key: string]: string } = {}
const slice9s: { [key: string]: ISlice9 } = {}
let assetFound = false
iterateDocument(document, item => {
if (/^slice-9-[012]-[012]$/.test(item.name)) {
// remove slice-9 garbage
item.remove()
return
}
if (isSlice9(item)) {
slice9Export(item, target, slice9s, idNameMap)
return
}
if (item.exportFormats.length > 0) {
item.exportFormats.forEach(format => {
assetFound = true
const buffer = sketch.export(item, {
output: false,
scales: format.size
})
write(target(assetPath(item.name, format.size, format.fileFormat)), buffer)
})
if (assetFound) {
const assetName = childName(item.name)
assets[assetName] = assetPathForLayer(item, null)
idNameMap[item.id] = assetName
}
}
})
if (assetFound) {
write(target('src/Asset.tsx'), `${disclaimer}
${template(readPluginAsset('Asset.tsx').toString(), {
lines: {
skip: () => '',
images: input => Object.keys(assets).length > 0 ? input : '',
slice9: input => Object.keys(slice9s).length > 0 ? input : ''
},
blocks: {
properties: parts => {
let result = []
template(parts, {
blocks: {
image: template => {
result = result.concat(
Object.keys(assets).sort(stringSort).map(name => {
const asset = assets[name]
return replace(template, {
name, asset
})
})
)
return ''
},
slice9: template => {
result = result.concat(
Object.keys(slice9s).sort(stringSort).map(name => {
const slice9 = slice9s[name]
return replace(template, {
slice9: name,
width: slice9.width,
height: slice9.height,
sliceX: slice9.slice.x,
sliceY: slice9.slice.y,
sliceW: slice9.slice.width,
sliceH: slice9.slice.height,
path0: slice9.path(0, 0),
path1: slice9.path(0, 1),
path2: slice9.path(0, 2),
path3: slice9.path(1, 0),
path4: slice9.path(1, 1),
path5: slice9.path(1, 2),
path6: slice9.path(2, 0),
path7: slice9.path(2, 1),
path8: slice9.path(2, 2)
})
})
)
return ''
}
}
})
return result.map(entry => entry.replace(/\n$/ig, '')).join(',\n') + '\n'
}
}
})}`)
}
return idNameMap
}