-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
plugin.ts
90 lines (85 loc) · 2.09 KB
/
plugin.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
import {
BaseBladeParams,
BladePlugin,
createSliderTextProps,
createValue,
DefiniteRangeConstraint,
Formatter,
getSuitablePointerScale,
LabeledValueBladeController,
LabelPropsObject,
MicroParser,
numberToString,
parseNumber,
parseRecord,
SliderTextController,
ValueMap,
VERSION,
} from '@tweakpane/core';
import {SliderBladeApi} from './api/slider.js';
export interface SliderBladeParams extends BaseBladeParams {
max: number;
min: number;
view: 'slider';
format?: Formatter<number>;
label?: string;
value?: number;
}
export const SliderBladePlugin: BladePlugin<SliderBladeParams> = {
id: 'slider',
type: 'blade',
core: VERSION,
accept(params) {
const result = parseRecord<SliderBladeParams>(params, (p) => ({
max: p.required.number,
min: p.required.number,
view: p.required.constant('slider'),
format: p.optional.function as MicroParser<Formatter<number>>,
label: p.optional.string,
value: p.optional.number,
}));
return result ? {params: result} : null;
},
controller(args) {
const initialValue = args.params.value ?? 0;
const drc = new DefiniteRangeConstraint({
max: args.params.max,
min: args.params.min,
});
const v = createValue(initialValue, {
constraint: drc,
});
const vc = new SliderTextController(args.document, {
...createSliderTextProps({
formatter: args.params.format ?? numberToString,
keyScale: createValue(1),
max: drc.values.value('max'),
min: drc.values.value('min'),
pointerScale: getSuitablePointerScale(args.params, initialValue),
}),
parser: parseNumber,
value: v,
viewProps: args.viewProps,
});
return new LabeledValueBladeController<number, SliderTextController>(
args.document,
{
blade: args.blade,
props: ValueMap.fromObject<LabelPropsObject>({
label: args.params.label,
}),
value: v,
valueController: vc,
},
);
},
api(args) {
if (!(args.controller instanceof LabeledValueBladeController)) {
return null;
}
if (!(args.controller.valueController instanceof SliderTextController)) {
return null;
}
return new SliderBladeApi(args.controller);
},
};