diff --git a/packages/core/src/input-binding/point-2d/plugin-test.ts b/packages/core/src/input-binding/point-2d/plugin-test.ts index e319f7f10..f9ec705c0 100644 --- a/packages/core/src/input-binding/point-2d/plugin-test.ts +++ b/packages/core/src/input-binding/point-2d/plugin-test.ts @@ -143,66 +143,42 @@ describe(Point2dInputPlugin.id, () => { { params: { params: { - x: {min: -1, max: 1, step: 0.1}, - y: {min: -2, max: 2, step: 0.2}, - }, - }, - expected: { - x: {min: -1, max: 1, step: 0.1}, - y: {min: -2, max: 2, step: 0.2}, - }, - }, - { - params: { - params: { - min: -1, - max: 1, - step: 0.1, + ...{min: -1, max: 1, step: 0.1}, x: {min: -2, max: 2, step: 0.2}, }, }, - expected: { - x: {min: -2, max: 2, step: 0.2}, - y: {min: -1, max: 1, step: 0.1}, - }, + expected: [ + {min: -2, max: 2, step: 0.2}, + {min: -1, max: 1, step: 0.1}, + ], }, { params: { params: { - min: -1, - max: 1, - step: 0.1, + ...{min: -1, max: 1, step: 0.1}, y: {min: -2, max: 2, step: 0.2}, }, }, - expected: { - x: {min: -1, max: 1, step: 0.1}, - y: {min: -2, max: 2, step: 0.2}, - }, + expected: [ + {min: -1, max: 1, step: 0.1}, + {min: -2, max: 2, step: 0.2}, + ], }, ].forEach(({params, expected}) => { describe(`when params=${JSON.stringify(params)}`, () => { it('should propagate dimension params', () => { const doc = createTestWindow().document; - const p = {x: 12, y: 34, hello: 'world'}; - const obj = {p: p}; const c = createInputBindingController(Point2dInputPlugin, { document: doc, params: params.params, - target: new BindingTarget(obj, 'p'), + target: new BindingTarget({p: {x: 12, y: 34}}, 'p'), }) as InputBindingController; - const constraint = getPoint2dConstraint(c.value); - - const xp = getDimensionProps( - constraint.components[0] as Constraint, - ); - assert.deepStrictEqual(xp, expected.x); - - const yp = getDimensionProps( - constraint.components[1] as Constraint, - ); - assert.deepStrictEqual(yp, expected.y); + const comps = getPoint2dConstraint(c.value).components; + [0, 1].forEach((i) => { + const p = getDimensionProps(comps[i] as Constraint); + assert.deepStrictEqual(p, expected[i]); + }); }); }); }); diff --git a/packages/core/src/input-binding/point-2d/plugin.ts b/packages/core/src/input-binding/point-2d/plugin.ts index cc0a13d65..ebc543a3b 100644 --- a/packages/core/src/input-binding/point-2d/plugin.ts +++ b/packages/core/src/input-binding/point-2d/plugin.ts @@ -47,20 +47,8 @@ function createConstraint( return new PointNdConstraint({ assembly: Point2dAssembly, components: [ - createDimensionConstraint( - { - ...params, - ...params.x, - }, - initialValue.x, - ), - createDimensionConstraint( - { - ...params, - ...params.y, - }, - initialValue.y, - ), + createDimensionConstraint({...params, ...params.x}, initialValue.x), + createDimensionConstraint({...params, ...params.y}, initialValue.y), ], }); } diff --git a/packages/core/src/input-binding/point-3d/plugin-test.ts b/packages/core/src/input-binding/point-3d/plugin-test.ts index 987ee43b1..f6eb4117f 100644 --- a/packages/core/src/input-binding/point-3d/plugin-test.ts +++ b/packages/core/src/input-binding/point-3d/plugin-test.ts @@ -1,9 +1,11 @@ import * as assert from 'assert'; import {describe, it} from 'mocha'; +import {InputBindingController} from '../../blade/binding/controller/input-binding'; import {BindingTarget} from '../../common/binding/target'; import {InputBindingValue} from '../../common/binding/value/input-binding'; import {findConstraint} from '../../common/constraint/composite'; +import {Constraint} from '../../common/constraint/constraint'; import {StepConstraint} from '../../common/constraint/step'; import {ComplexValue} from '../../common/model/complex-value'; import {getBoundValue} from '../../common/model/test-util'; @@ -15,6 +17,23 @@ import {createInputBindingController} from '../plugin'; import {Point3d} from './model/point-3d'; import {Point3dInputPlugin} from './plugin'; +function getPoint3dConstraint( + v: InputBindingValue, +): PointNdConstraint { + return (getBoundValue(v) as ComplexValue) + .constraint as PointNdConstraint; +} + +function getDimensionProps(c: Constraint) { + const [min, max] = findNumberRange(c); + const sc = findConstraint(c, StepConstraint); + return { + max: max, + min: min, + step: sc?.step, + }; +} + describe(Point3dInputPlugin.id, () => { it('should have right number of text views', () => { const doc = createTestWindow().document; @@ -34,13 +53,9 @@ describe(Point3dInputPlugin.id, () => { document: doc, params: {z: {step: 1}}, target: new BindingTarget({foo: {x: 12, y: 34, z: 56}}, 'foo'), - }); + }) as InputBindingController; - const cs = ( - getBoundValue( - c?.value as InputBindingValue, - ) as ComplexValue - ).constraint as PointNdConstraint; + const cs = getPoint3dConstraint(c.value); const zc = cs.components[2]; if (!zc) { assert.fail('Unexpected constraint'); @@ -60,19 +75,69 @@ describe(Point3dInputPlugin.id, () => { }, }, target: new BindingTarget({foo: {x: 12, y: 34, z: 56}}, 'foo'), - }); + }) as InputBindingController; - const cs = ( - getBoundValue( - c?.value as InputBindingValue, - ) as ComplexValue - ).constraint as PointNdConstraint; - const zc = cs.components[2]; - if (!zc) { - assert.fail('Unexpected constraint'); - } - const [min, max] = findNumberRange(zc); - assert.strictEqual(min, -123); - assert.strictEqual(max, 456); + const cs = getPoint3dConstraint(c.value); + const zp = getDimensionProps(cs.components[2] as Constraint); + assert.deepStrictEqual([zp.min, zp.max], [-123, 456]); + }); + + [ + { + params: { + params: { + ...{min: -1, max: 1, step: 0.1}, + x: {min: -2, max: 2, step: 0.2}, + }, + }, + expected: [ + {min: -2, max: 2, step: 0.2}, + {min: -1, max: 1, step: 0.1}, + {min: -1, max: 1, step: 0.1}, + ], + }, + { + params: { + params: { + ...{min: -1, max: 1, step: 0.1}, + y: {min: -2, max: 2, step: 0.2}, + }, + }, + expected: [ + {min: -1, max: 1, step: 0.1}, + {min: -2, max: 2, step: 0.2}, + {min: -1, max: 1, step: 0.1}, + ], + }, + { + params: { + params: { + ...{min: -1, max: 1, step: 0.1}, + z: {min: -2, max: 2, step: 0.2}, + }, + }, + expected: [ + {min: -1, max: 1, step: 0.1}, + {min: -1, max: 1, step: 0.1}, + {min: -2, max: 2, step: 0.2}, + ], + }, + ].forEach(({params, expected}) => { + describe(`when params=${JSON.stringify(params)}`, () => { + it('should propagate dimension params', () => { + const doc = createTestWindow().document; + const c = createInputBindingController(Point3dInputPlugin, { + document: doc, + params: params.params, + target: new BindingTarget({p: {x: 12, y: 34, z: 56}}, 'p'), + }) as InputBindingController; + + const comps = getPoint3dConstraint(c.value).components; + [0, 1, 2].forEach((i) => { + const p = getDimensionProps(comps[i] as Constraint); + assert.deepStrictEqual(p, expected[i]); + }); + }); + }); }); }); diff --git a/packages/core/src/input-binding/point-3d/plugin.ts b/packages/core/src/input-binding/point-3d/plugin.ts index 9987e3b52..e63f6a1e7 100644 --- a/packages/core/src/input-binding/point-3d/plugin.ts +++ b/packages/core/src/input-binding/point-3d/plugin.ts @@ -36,9 +36,9 @@ function createConstraint( return new PointNdConstraint({ assembly: Point3dAssembly, components: [ - createDimensionConstraint(params.x, initialValue.x), - createDimensionConstraint(params.y, initialValue.y), - createDimensionConstraint(params.z, initialValue.z), + createDimensionConstraint({...params, ...params.x}, initialValue.x), + createDimensionConstraint({...params, ...params.y}, initialValue.y), + createDimensionConstraint({...params, ...params.z}, initialValue.z), ], }); } @@ -75,7 +75,10 @@ export const Point3dInputPlugin: InputBindingPlugin< return null; } const result = parseRecord(params, (p) => ({ + max: p.optional.number, + min: p.optional.number, readonly: p.optional.constant(false), + step: p.optional.number, x: p.optional.custom(parsePointDimensionParams), y: p.optional.custom(parsePointDimensionParams), z: p.optional.custom(parsePointDimensionParams),