Skip to content

Commit

Permalink
Add support for custom pointer scale, #471
Browse files Browse the repository at this point in the history
  • Loading branch information
cocopon committed Mar 21, 2023
1 parent 6beb962 commit 3a15716
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
15 changes: 14 additions & 1 deletion packages/core/src/common/number/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {Constraint} from '../constraint/constraint';
import {DefiniteRangeConstraint} from '../constraint/definite-range';
import {RangeConstraint} from '../constraint/range';
import {StepConstraint} from '../constraint/step';
import {Formatter} from '../converter/formatter';
import {createNumberFormatter} from '../converter/number';
import {MicroParser, MicroParsers} from '../micro-parsers';
import {NumberTextPropsObject} from './view/number-text';

export function mapRange(
Expand Down Expand Up @@ -107,6 +109,17 @@ export function createNumberTextPropsObject(
params.format ??
createNumberFormatter(getSuitableDecimalDigits(params, initialValue)),
keyScale: getSuitableKeyScale(params),
pointerScale: getSuitablePointerScale(params, initialValue),
pointerScale:
params.pointerScale ?? getSuitablePointerScale(params, initialValue),
};
}

export function createNumberTextInputParamsParser(p: typeof MicroParsers) {
return {
format: p.optional.function as MicroParser<Formatter<number>>,
max: p.optional.number,
min: p.optional.number,
pointerScale: p.optional.number,
step: p.optional.number,
};
}
16 changes: 7 additions & 9 deletions packages/core/src/common/point-nd/util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import {isRecord} from '../../misc/type-util';
import {CompositeConstraint} from '../constraint/composite';
import {Constraint} from '../constraint/constraint';
import {Formatter} from '../converter/formatter';
import {MicroParser, MicroParsers, parseRecord} from '../micro-parsers';
import {createRangeConstraint, createStepConstraint} from '../number/util';
import {MicroParsers, parseRecord} from '../micro-parsers';
import {
createNumberTextInputParamsParser,
createRangeConstraint,
createStepConstraint,
} from '../number/util';
import {PointDimensionParams} from '../params';

export function createPointDimensionParser(p: typeof MicroParsers) {
return {
format: p.optional.function as MicroParser<Formatter<number>>,
max: p.optional.number,
min: p.optional.number,
step: p.optional.number,
};
return createNumberTextInputParamsParser(p);
}

export function parsePointDimensionParams(
Expand Down
20 changes: 15 additions & 5 deletions packages/core/src/input-binding/number/plugin-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe(NumberInputPlugin.id, () => {
assert.strictEqual(constraint?.origin, 1);
});

it('should use specified formatter', () => {
it('should apply format', () => {
const doc = createTestWindow().document;
const c = createInputBindingController(NumberInputPlugin, {
document: doc,
Expand All @@ -47,10 +47,20 @@ describe(NumberInputPlugin.id, () => {
});

const vc = c?.valueController as NumberTextController;
if (!(vc instanceof NumberTextController)) {
assert.fail('unexpected controller');
}

assert.strictEqual(vc.view.inputElement.value, 'foo 123 bar');
});

it('should apply pointerScale', () => {
const doc = createTestWindow().document;
const c = createInputBindingController(NumberInputPlugin, {
document: doc,
params: {
pointerScale: 123,
},
target: new BindingTarget({foo: 123}, 'foo'),
});

const vc = c?.valueController as NumberTextController;
assert.strictEqual(vc.props.get('pointerScale'), 123);
});
});
9 changes: 4 additions & 5 deletions packages/core/src/input-binding/number/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {ListController} from '../../common/controller/list';
import {Formatter} from '../../common/converter/formatter';
import {numberFromUnknown, parseNumber} from '../../common/converter/number';
import {createListConstraint, parseListOptions} from '../../common/list-util';
import {MicroParser, parseRecord} from '../../common/micro-parsers';
import {parseRecord} from '../../common/micro-parsers';
import {ValueMap} from '../../common/model/value-map';
import {createValue} from '../../common/model/values';
import {NumberTextController} from '../../common/number/controller/number-text';
Expand All @@ -20,6 +20,7 @@ import {
SliderTextController,
} from '../../common/number/controller/slider-text';
import {
createNumberTextInputParamsParser,
createNumberTextPropsObject,
createRangeConstraint,
createStepConstraint,
Expand All @@ -34,6 +35,7 @@ export interface NumberTextInputParams {
format?: Formatter<number>;
max?: number;
min?: number;
pointerScale?: number;
step?: number;
}

Expand Down Expand Up @@ -81,12 +83,9 @@ export const NumberInputPlugin: InputBindingPlugin<
return null;
}
const result = parseRecord<NumberInputParams>(params, (p) => ({
format: p.optional.function as MicroParser<Formatter<number>>,
max: p.optional.number,
min: p.optional.number,
...createNumberTextInputParamsParser(p),
options: p.optional.custom<ListParamsOptions<number>>(parseListOptions),
readonly: p.optional.constant(false),
step: p.optional.number,
}));
return result
? {
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/input-binding/point-2d/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import {
} from '../../common/params';
import {parsePickerLayout} from '../../common/picker-util';
import {createPointAxis, PointAxis} from '../../common/point-nd/point-axis';
import {
createPointDimensionParser,
parsePointDimensionParams,
} from '../../common/point-nd/util';
import {createPointDimensionParser} from '../../common/point-nd/util';
import {parsePointDimensionParams} from '../../common/point-nd/util';
import {createDimensionConstraint} from '../../common/point-nd/util';
import {deepMerge, isEmpty, Tuple2} from '../../misc/type-util';
import {VERSION} from '../../version';
Expand Down

0 comments on commit 3a15716

Please sign in to comment.