Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5200 – the users choice is not saved in the settings but is recalculated in px #5298

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,26 @@ import Input from '../Input/Input';
import Select from '../Select';
import styles from './measure-input.module.less';
import { getSelectOptionsFromSchema } from '../../../utils';
import { MeasurementUnits } from 'src/script/ui/data/schema/options-schema';

const selectOptions = getSelectOptionsFromSchema({
enum: ['cm', 'px', 'pt', 'inch'],
enum: Object.values(MeasurementUnits),
});

const MeasureInput = ({
schema,
extraSchema,
value,
extraValue,
onChange,
onExtraChange,
name,
className,
...rest
}) => {
const [measure, setMeasure] = useState('px');
const [measure, setMeasure] = useState(
extraValue || extraSchema?.default || 'px',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use MeasurementUnits enum here instead of string literal?

);
const [cust, setCust] = useState(value || schema.default);

useEffect(() => {
Expand All @@ -44,27 +50,27 @@ const MeasureInput = ({
} // Hack: Set init value (RESET)
}, []);

useEffect(() => {
setMeasure(extraValue);
}, [extraValue]);

const handleChange = (value) => {
const convValue = convertValue(value, measure, 'px');
setCust(value);
onChange(convValue);
};

const handleMeasChange = (m) => {
const handleMeasChange = (unit) => {
setCust((prev) => {
convertValue(prev, measure, m);
convertValue(prev, measure, unit);
});
setMeasure(m);
};

const calcValue = () => {
const newValue = convertValue(value, 'px', measure);
setCust(newValue);
setMeasure(unit);
onExtraChange(unit);
};

useEffect(() => {
calcValue();
}, [value, measure, calcValue]);
setCust(convertValue(value, 'px', measure));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use MeasurementUnits enum here instead of 'px'?

}, [value, measure]);

const desc = schema || schema.properties[name];

Expand Down
31 changes: 23 additions & 8 deletions packages/ketcher-react/src/script/ui/component/form/form/form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,23 @@ class Form extends Component {
onUpdate(instance, valid, errs);
}

field(name, onChange) {
field(name, onChange, extraName) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename 'extra' to something like 'second input name' or something that will describe it more clear?

const { result, errors } = this.props;
const value = result[name];
const extraValue = extraName ? result[extraName] : null;

const handleOnChange = (name, value) => {
const newState = Object.assign({}, this.props.result, { [name]: value });
this.updateState(newState);
if (onChange) onChange(value);
};

return {
dataError: errors && errors[name],
value,
onChange: (val) => {
const newState = Object.assign({}, this.props.result, { [name]: val });
this.updateState(newState);
if (onChange) onChange(val);
},
extraValue,
onChange: (val) => handleOnChange(name, val),
onExtraChange: (val) => handleOnChange(extraName, val),
};
}

Expand Down Expand Up @@ -126,7 +131,8 @@ function Label({ labelPos, title, children, ...props }) {
}

function Field(props) {
const { name, onChange, component, labelPos, className, ...rest } = props;
const { name, extraName, onChange, component, labelPos, className, ...rest } =
props;
const [anchorEl, setAnchorEl] = useState(null);
const handlePopoverOpen = useCallback((event) => {
setAnchorEl(event.currentTarget);
Expand All @@ -136,13 +142,22 @@ function Field(props) {
}, []);
const { schema, stateStore } = useFormContext();
const desc = rest.schema || schema.properties[name];
const { dataError, ...fieldOpts } = stateStore.field(name, onChange);
const { dataError, onExtraChange, extraValue, ...fieldOpts } =
stateStore.field(name, onChange, extraName);

const getExtraSchema = () => {
return rest.extraSchema || schema.properties[extraName];
};

const Component = component;
const formField = component ? (
<Component
name={name}
schema={desc}
className={className}
onExtraChange={onExtraChange}
extraValue={extraValue}
{...(extraName && { extraSchema: getExtraSchema() })}
{...fieldOpts}
{...rest}
/>
Expand Down
42 changes: 42 additions & 0 deletions packages/ketcher-react/src/script/ui/data/schema/options-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ type ExtendedSchema = SchemaObject & {
default?: any;
};

export enum MeasurementUnits {
Px = 'px',
Cm = 'cm',
Pt = 'pt',
Inch = 'inch',
}

const editor: {
resetToSelect: ExtendedSchema;
rotationStep: ExtendedSchema;
Expand Down Expand Up @@ -64,15 +71,20 @@ const render: {
orFlagLabel: ExtendedSchema;
font: ExtendedSchema;
fontsz: ExtendedSchema;
fontszUnit: ExtendedSchema;
fontszsub: ExtendedSchema;
fontszsubUnit: ExtendedSchema;
carbonExplicitly: ExtendedSchema;
showCharge: ExtendedSchema;
showValence: ExtendedSchema;
showHydrogenLabels: ExtendedSchema;
aromaticCircle: ExtendedSchema;
doubleBondWidth: ExtendedSchema;
doubleBondWidthUnit: ExtendedSchema;
bondThickness: ExtendedSchema;
bondThicknessUnit: ExtendedSchema;
stereoBondWidth: ExtendedSchema;
stereoBondWidthUnit: ExtendedSchema;
} = {
showValenceWarnings: {
title: 'Show valence warnings',
Expand Down Expand Up @@ -173,13 +185,25 @@ const render: {
minimum: 1,
maximum: 96,
},
fontszUnit: {
title: 'Font size unit',
enum: Object.values(MeasurementUnits),
enumNames: Object.values(MeasurementUnits),
default: MeasurementUnits.Px,
},
fontszsub: {
title: 'Sub font size',
type: 'integer',
default: 13,
minimum: 1,
maximum: 96,
},
fontszsubUnit: {
title: 'Sub font size unit',
enum: Object.values(MeasurementUnits),
enumNames: Object.values(MeasurementUnits),
default: MeasurementUnits.Px,
},
// Atom
carbonExplicitly: {
title: 'Display carbon explicitly',
Expand Down Expand Up @@ -219,20 +243,38 @@ const render: {
minimum: 1,
maximum: 96,
},
doubleBondWidthUnit: {
title: 'Double bond width unit',
enum: Object.values(MeasurementUnits),
enumNames: Object.values(MeasurementUnits),
default: MeasurementUnits.Px,
},
bondThickness: {
title: 'Bond thickness',
type: 'integer',
default: defaultBondThickness,
minimum: 1,
maximum: 96,
},
bondThicknessUnit: {
title: 'Bond thickness unit',
enum: Object.values(MeasurementUnits),
enumNames: Object.values(MeasurementUnits),
default: MeasurementUnits.Px,
},
stereoBondWidth: {
title: 'Stereo (Wedge) bond width',
type: 'integer',
default: 6,
minimum: 1,
maximum: 96,
},
stereoBondWidthUnit: {
title: 'Stereo (Wedge) bond width unit',
enum: Object.values(MeasurementUnits),
enumNames: Object.values(MeasurementUnits),
default: MeasurementUnits.Px,
},
};

const server: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,18 @@ const SettingsDialog = (props: Props) => {
<Field name="showValenceWarnings" />
<Field name="atomColoring" />
<Field name="font" component={SystemFonts} data-testid="font" />
<Field name="fontsz" component={MeasureInput} labelPos={false} />
<Field name="fontszsub" component={MeasureInput} labelPos={false} />
<Field
name="fontsz"
component={MeasureInput}
labelPos={false}
extraName="fontszUnit"
/>
<Field
name="fontszsub"
component={MeasureInput}
labelPos={false}
extraName="fontszsubUnit"
/>
</fieldset>
),
};
Expand Down Expand Up @@ -222,12 +232,19 @@ const SettingsDialog = (props: Props) => {
name="doubleBondWidth"
component={MeasureInput}
labelPos={false}
extraName="doubleBondWidthUnit"
/>
<Field
name="bondThickness"
component={MeasureInput}
labelPos={false}
extraName="bondThicknessUnit"
/>
<Field name="bondThickness" component={MeasureInput} labelPos={false} />
<Field
name="stereoBondWidth"
component={MeasureInput}
labelPos={false}
extraName="stereoBondWidthUnit"
/>
</fieldset>
),
Expand Down
Loading