Skip to content

Commit dad740d

Browse files
committed
fix: Correct the alignment of narrow text in input fields.
1 parent ef235cf commit dad740d

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
@@ -852,8 +852,7 @@ export abstract class Field<T = any>
852852
totalHeight = Math.max(totalHeight, constants!.FIELD_BORDER_RECT_HEIGHT);
853853
}
854854

855-
this.size_.height = totalHeight;
856-
this.size_.width = totalWidth;
855+
this.size_ = new Size(totalWidth, totalHeight);
857856

858857
this.positionTextElement_(xOffset, contentWidth);
859858
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
*
@@ -115,8 +120,8 @@ export abstract class FieldInput<T extends InputTypes> extends Field<
115120
*/
116121
protected override get size_() {
117122
const s = super.size_;
118-
if (s.width < 14) {
119-
s.width = 14;
123+
if (s.width < MINIMUM_WIDTH) {
124+
s.width = MINIMUM_WIDTH;
120125
}
121126

122127
return s;
@@ -732,6 +737,23 @@ export abstract class FieldInput<T extends InputTypes> extends Field<
732737
return true;
733738
}
734739

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

0 commit comments

Comments
 (0)