Skip to content

Commit

Permalink
fix(toolbar): Select added digits if the document size input changes …
Browse files Browse the repository at this point in the history
…from zero while editing

Previously, trying to change the value of a dimension input that ends in
0s (e.g. from 100 to 200) by deleting and replacing the first digit
caused the input's value to be reset to 100 (as 0 is an invalid
dimension).

This commit works around the issue by selecting the changed digit(s) at
the beginning of the input.

This should fix #58 (in many cases).
  • Loading branch information
personalizedrefrigerator committed Dec 16, 2023
1 parent 7a1d4ea commit 2ab8246
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/js-draw/src/toolbar/widgets/DocumentPropertiesWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,32 @@ export default class DocumentPropertiesWidget extends BaseWidget {

return {
setValue: (value: number) => {
input.value = value.toString();
// Slightly improve the case where the user tries to change the
// first digit of a dimension like 600.
//
// As changing the value also gives the image zero size (which is unsupported,
// .setValue is called immediately). We work around this by trying to select
// the added/changed digits.
//
// See https://github.com/personalizedrefrigerator/js-draw/issues/58.
if (document.activeElement === input && input.value.match(/^0*$/)) {
// We need to switch to type="text" and back to type="number" because
// number inputs don't support selection.
//
// See https://stackoverflow.com/q/22381837
const originalValue = input.value;

input.type = 'text';
input.value = value.toString();

// Select the added digits
const lengthToSelect = Math.max(1, input.value.length - originalValue.length);
input.setSelectionRange(0, lengthToSelect);

input.type = 'number';
} else {
input.value = value.toString();
}
},
setIsAutomaticSize: (automatic: boolean) => {
input.disabled = automatic;
Expand Down

0 comments on commit 2ab8246

Please sign in to comment.