-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathtwicpics.ts
More file actions
84 lines (76 loc) · 2.38 KB
/
Copy pathtwicpics.ts
File metadata and controls
84 lines (76 loc) · 2.38 KB
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
import { encodeQueryItem, joinURL } from 'ufo'
import { defineProvider } from '../utils/provider'
import { createMapper, createOperationsGenerator } from '../utils/index'
import type { InferModifiers } from '../utils/index'
const fits = createMapper({
fill: 'resize',
inside: 'contain',
outside: 'contain',
cover: 'cover',
contain: 'inside',
missingValue: 'cover',
} as const)
const operationsGenerator = createOperationsGenerator({
keyMap: {
format: 'output',
quality: 'quality',
background: 'background',
focus: 'focus',
zoom: 'zoom',
},
valueMap: {
format(value: string) {
if (value === 'jpg') {
return 'jpeg'
}
return value
},
background(value: string) {
if (value.startsWith('#')) {
return value.replace('#', '')
}
return value
},
focus: {
auto: 'auto',
faces: 'faces',
north: '50px0p',
northEast: '100px0p',
northWest: '0px0p',
west: '0px50p',
southWest: '100px100p',
south: '50px100p',
southEast: '0px100p',
east: '100px50p',
center: '50px50p',
},
},
joinWith: '/',
formatter: (key: 'output' | 'format' | 'fit' | 'quality' | 'background' | 'focus' | 'zoom', value) => encodeQueryItem(key, value),
} as const)
interface TwicpicsOptions {
baseURL?: string
modifiers?: InferModifiers<typeof operationsGenerator>
& { fit?: 'fill' | 'inside' | 'outside' | 'cover' | 'contain' }
& Partial<Record<'resize' | 'fill' | 'contain' | 'inside' | 'outside' | 'cover' | 'missingValue', string>>
& Partial<Record<typeof fits extends (fit: string) => infer Fit ? NonNullable<Fit> : string, string>>
}
export default defineProvider<TwicpicsOptions>({
getImage: (src, { modifiers, baseURL = '/' }) => {
const { width, height, fit, ...providerModifiers } = modifiers
let w = width
let h = height
if (width || height) {
if (fit && fit === 'outside') {
// fit = outside is equivalent to twicPics contain ( max( width, height ) x max( width, height ) )
w = Math.max(width || 0, height || 0)
h = Math.max(width || 0, height || 0)
}
providerModifiers[fits(fit as keyof typeof fits)] = `${w || '-'}x${h || '-'}`
}
const operations = operationsGenerator(providerModifiers)
return {
url: joinURL(baseURL, src + (operations ? ('?twic=v1/' + operations) : '')),
}
},
})