feat: add step as slider param

This commit is contained in:
Felix Roos 2023-10-02 20:44:51 +02:00
parent 1c0da7cd49
commit 28966739f6
2 changed files with 6 additions and 4 deletions

View file

@ -6,7 +6,7 @@ export let sliderValues = {};
const getSliderID = (from) => `slider_${from}`; const getSliderID = (from) => `slider_${from}`;
export class SliderWidget extends WidgetType { export class SliderWidget extends WidgetType {
constructor(value, min, max, from, to, view) { constructor(value, min, max, from, to, step, view) {
super(); super();
this.value = value; this.value = value;
this.min = min; this.min = min;
@ -14,6 +14,7 @@ export class SliderWidget extends WidgetType {
this.from = from; this.from = from;
this.originalFrom = from; this.originalFrom = from;
this.to = to; this.to = to;
this.step = step;
this.view = view; this.view = view;
} }
@ -29,7 +30,7 @@ export class SliderWidget extends WidgetType {
slider.type = 'range'; slider.type = 'range';
slider.min = this.min; slider.min = this.min;
slider.max = this.max; slider.max = this.max;
slider.step = (this.max - this.min) / 1000; slider.step = this.step ?? (this.max - this.min) / 1000;
slider.originalValue = this.value; slider.originalValue = this.value;
// to make sure the code stays in sync, let's save the original value // to make sure the code stays in sync, let's save the original value
// becuase .value automatically clamps values so it'll desync with the code // becuase .value automatically clamps values so it'll desync with the code
@ -66,9 +67,9 @@ export const updateWidgets = (view, widgets) => {
}; };
function getWidgets(widgetConfigs, view) { function getWidgets(widgetConfigs, view) {
return widgetConfigs.map(({ from, to, value, min, max }) => { return widgetConfigs.map(({ from, to, value, min, max, step }) => {
return Decoration.widget({ return Decoration.widget({
widget: new SliderWidget(value, min, max, from, to, view), widget: new SliderWidget(value, min, max, from, to, step, view),
side: 0, side: 0,
}).range(from /* , to */); }).range(from /* , to */);
}); });

View file

@ -43,6 +43,7 @@ export function transpiler(input, options = {}) {
value: node.arguments[0].raw, // don't use value! value: node.arguments[0].raw, // don't use value!
min: node.arguments[1]?.value ?? 0, min: node.arguments[1]?.value ?? 0,
max: node.arguments[2]?.value ?? 1, max: node.arguments[2]?.value ?? 1,
step: node.arguments[3]?.value,
}); });
return this.replace(widgetWithLocation(node)); return this.replace(widgetWithLocation(node));
} }