-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathdirectus.ts
More file actions
136 lines (121 loc) · 4.08 KB
/
directus.ts
File metadata and controls
136 lines (121 loc) · 4.08 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
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
import { joinURL } from 'ufo'
import { createOperationsGenerator } from '../utils/index'
import { defineProvider } from '../utils/provider'
type SharpOperationMap = {
// Image Operations https://sharp.pixelplumbing.com/api-operation/
rotate: [angle?: number, options?: { background: Color }]
flip: []
flop: []
sharpen: [sigma?: number] | [options?: { sigma?: number, m1?: number, m2?: number, x1?: number, y2?: number, y3?: number }]
median: [size?: number]
blur: [sigma?: number] | [options?: { sigma?: number, precision?: 'integer' | 'float' | 'approximate', minAmplitude?: number }]
flatten: [options?: { background: Color }]
unflatten: []
gamma: [gamma?: number, gammaOut?: number]
negate: [options?: { alpha?: boolean }]
normalize: [lower?: number, upper?: number]
normalise: [lower?: number, upper?: number] // Alias
clahe: [options?: { width: number, height: number, maxSlope?: number }]
convolve: [kernel: { width: number, height: number, kernel: number[], offset?: number }]
threshold: [value?: number, options?: { grayscale?: boolean }]
linear: [a?: number | number[], b?: number | number[]]
recomb: [matrix: number[][]]
modulate: [options?: {
brightness?: number
saturation?: number
hue?: number
lightness?: number
}]
// Color Manipulation https://sharp.pixelplumbing.com/api-colour/
tint: [color: Color]
grayscale: []
greyscale: [] // Alias
pipelineColorspace: [colorspace: SharpColorspace]
pipelineColourspace: [colourspace: SharpColorspace] // Alias
toColorspace: [colorspace: SharpColorspace]
toColourspace: [colourspace: SharpColorspace] // Alias
// Channel Manipulation https://sharp.pixelplumbing.com/api-channel/
removeAlpha: []
ensureAlpha: [alpha?: number]
extractChannel: [channel: 'red' | 'green' | 'blue' | 'alpha'] // Restricting to string channel extraction to avoid complexity.
}
type SharpColorspace
= | 'srgb'
| 'rgb'
| 'scrgb'
| 'rgb16'
| 'cmyk'
| 'lab'
| 'b-w'
| string // retain advanced options for advanced users
type Color
= | string
| { r: number, g: number, b: number, alpha?: number }
| { h: number, s: number, l: number, alpha?: number }
| { h: number, s: number, v: number, alpha?: number }
| { c: number, m: number, y: number, k: number, alpha?: number }
| { h: number, w: number, b: number }
| { l: number, c: number, h: number }
| { l: number, a: number, b: number }
| { h: number, c: number, g: number }
type KnownSharpOperation
= {
[K in keyof SharpOperationMap]:
SharpOperationMap[K] extends []
? [K]
: [K, ...SharpOperationMap[K]]
}[keyof SharpOperationMap]
type CustomSharpOperation<K extends string = string>
= K extends keyof SharpOperationMap
? never
: [key: K, ...args: any[]]
type SharpOperation
= | KnownSharpOperation
| CustomSharpOperation
type DirectusModifiers
= | { key: string }
| {
key?: never
withoutEnlargement?: boolean
transforms?: SharpOperation[]
}
interface DirectusOptions {
baseURL: string
width?: number
height?: number
quality?: number
format?: 'auto' | 'jpg' | 'png' | 'webp' | 'tiff' | 'avif'
fit?: 'cover' | 'contain' | 'inside' | 'outside' | 'fill'
modifiers?: DirectusModifiers
}
// HACK: See Discussion #2206
const operationsGenerator = createOperationsGenerator({
valueMap: {
transforms(value: SharpOperation[]) {
return value.length > 0
? JSON.stringify(
Array.from(new Set(value.map(v => JSON.stringify(v))))
.map(v => JSON.parse(v)),
)
: undefined
},
},
})
function isKeyModifier(
mod: DirectusModifiers | undefined,
): mod is { key: string } {
return !!mod && 'key' in mod && typeof mod.key === 'string'
}
export default defineProvider<DirectusOptions>({
getImage: (src, { modifiers, baseURL }) => {
if (isKeyModifier(modifiers)) {
return {
url: joinURL(baseURL, src + `?key=${modifiers.key}`),
}
}
const operations = operationsGenerator(modifiers)
return {
url: joinURL(baseURL, src + (operations ? `?${operations}` : '')),
}
},
})