-
Notifications
You must be signed in to change notification settings - Fork 15
/
helpers.ts
43 lines (38 loc) · 1.36 KB
/
helpers.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
import { Actions, AttributeContext } from '..'
const fit = (_: AttributeContext, v: number, oldMin: number, oldMax: number, newMin: number, newMax: number) => {
return ((v - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin
}
const fitInt = (ctx: AttributeContext, v: number, oldMin: number, oldMax: number, newMin: number, newMax: number) => {
return Math.round(fit(ctx, v, oldMin, oldMax, newMin, newMax))
}
const clampFit = (_: AttributeContext, v: number, oldMin: number, oldMax: number, newMin: number, newMax: number) => {
return Math.max(newMin, Math.min(newMax, fit(_, v, oldMin, oldMax, newMin, newMax)))
}
const clampFitInt = (
_: AttributeContext,
v: number,
oldMin: number,
oldMax: number,
newMin: number,
newMax: number,
) => {
return Math.round(clampFit(_, v, oldMin, oldMax, newMin, newMax))
}
export const HelperActions: Actions = {
setAll: (ctx, regexp, newValue) => {
const re = new RegExp(regexp)
ctx.walkSignals((name, signal) => re.test(name) && (signal.value = newValue))
},
toggleAll: (ctx, regexp) => {
const re = new RegExp(regexp)
ctx.walkSignals((name, signal) => re.test(name) && (signal.value = !signal.value))
},
clipboard: (_, text) => {
if (!navigator.clipboard) throw new Error('Clipboard API not available')
navigator.clipboard.writeText(text)
},
fit,
fitInt,
clampFit,
clampFitInt,
}