Skip to content

Commit 3e26b00

Browse files
authored
fix: Correct the alignment of narrow text in input fields. (#9327)
* fix: Correct the alignment of narrow text in input fields. * chore: Clarify purpose of first argument to positionTextElement_().
1 parent 5cc95e4 commit 3e26b00

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

core/field.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,8 +849,7 @@ export abstract class Field<T = any>
849849
totalHeight = Math.max(totalHeight, constants!.FIELD_BORDER_RECT_HEIGHT);
850850
}
851851

852-
this.size_.height = totalHeight;
853-
this.size_.width = totalWidth;
852+
this.size_ = new Size(totalWidth, totalHeight);
854853

855854
this.positionTextElement_(xOffset, contentWidth);
856855
this.positionBorderRect_();

core/field_dropdown.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import * as aria from './utils/aria.js';
2929
import {Coordinate} from './utils/coordinate.js';
3030
import * as dom from './utils/dom.js';
3131
import * as parsing from './utils/parsing.js';
32+
import {Size} from './utils/size.js';
3233
import * as utilsString from './utils/string.js';
3334
import {Svg} from './utils/svg.js';
3435

@@ -553,8 +554,7 @@ export class FieldDropdown extends Field<string> {
553554
} else {
554555
arrowWidth = dom.getTextWidth(this.arrow as SVGTSpanElement);
555556
}
556-
this.size_.width = imageWidth + arrowWidth + xPadding * 2;
557-
this.size_.height = height;
557+
this.size_ = new Size(imageWidth + arrowWidth + xPadding * 2, height);
558558

559559
let arrowX = 0;
560560
if (block.RTL) {
@@ -595,8 +595,7 @@ export class FieldDropdown extends Field<string> {
595595
height / 2 - this.getConstants()!.FIELD_DROPDOWN_SVG_ARROW_SIZE / 2,
596596
);
597597
}
598-
this.size_.width = textWidth + arrowWidth + xPadding * 2;
599-
this.size_.height = height;
598+
this.size_ = new Size(textWidth + arrowWidth + xPadding * 2, height);
600599

601600
this.positionTextElement_(xPadding, textWidth);
602601
}

core/field_input.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ import type {WorkspaceSvg} from './workspace_svg.js';
4545
*/
4646
type InputTypes = string | number;
4747

48+
/**
49+
* The minimum width of an input field.
50+
*/
51+
const MINIMUM_WIDTH = 14;
52+
4853
/**
4954
* Abstract class for an editable input field.
5055
*
@@ -113,8 +118,8 @@ export abstract class FieldInput<T extends InputTypes> extends Field<
113118
*/
114119
protected override get size_() {
115120
const s = super.size_;
116-
if (s.width < 14) {
117-
s.width = 14;
121+
if (s.width < MINIMUM_WIDTH) {
122+
s.width = MINIMUM_WIDTH;
118123
}
119124

120125
return s;
@@ -730,6 +735,23 @@ export abstract class FieldInput<T extends InputTypes> extends Field<
730735
return true;
731736
}
732737

738+
/**
739+
* Position a field's text element after a size change. This handles both LTR
740+
* and RTL positioning.
741+
*
742+
* @param xMargin x offset to use when positioning the text element.
743+
* @param contentWidth The content width.
744+
*/
745+
protected override positionTextElement_(
746+
xMargin: number,
747+
contentWidth: number,
748+
) {
749+
const effectiveWidth = xMargin * 2 + contentWidth;
750+
const delta =
751+
effectiveWidth < MINIMUM_WIDTH ? (MINIMUM_WIDTH - effectiveWidth) / 2 : 0;
752+
super.positionTextElement_(xMargin + delta, contentWidth);
753+
}
754+
733755
/**
734756
* Use the `getText_` developer hook to override the field's text
735757
* representation. When we're currently editing, return the current HTML value

0 commit comments

Comments
 (0)