Skip to content

Commit

Permalink
fix: Don't add padding around zero-width fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
gonfunko committed Jan 16, 2025
1 parent c68d645 commit 5347833
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
20 changes: 0 additions & 20 deletions core/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ export abstract class Field<T = any>
*/
DEFAULT_VALUE: T | null = null;

/** Non-breaking space. */
static readonly NBSP = '\u00A0';

/**
* A value used to signal when a field's constructor should *not* set the
* field's value or run configure_, and should allow a subclass to do that
Expand Down Expand Up @@ -905,17 +902,6 @@ export abstract class Field<T = any>
if (this.isDirty_) {
this.render_();
this.isDirty_ = false;
} else if (this.visible_ && this.size_.width === 0) {
// If the field is not visible the width will be 0 as well, one of the
// problems with the old system.
this.render_();
// Don't issue a warning if the field is actually zero width.
if (this.size_.width !== 0) {
console.warn(
'Deprecated use of setting size_.width to 0 to rerender a' +
' field. Set field.isDirty_ to true instead.',
);
}
}
return this.size_;
}
Expand Down Expand Up @@ -979,16 +965,10 @@ export abstract class Field<T = any>
*/
protected getDisplayText_(): string {
let text = this.getText();
if (!text) {
// Prevent the field from disappearing if empty.
return Field.NBSP;
}
if (text.length > this.maxDisplayLength) {
// Truncate displayed string and add an ellipsis ('...').
text = text.substring(0, this.maxDisplayLength - 2) + '…';
}
// Replace whitespace with non-breaking spaces so the text doesn't collapse.
text = text.replace(/\s/g, Field.NBSP);
if (this.sourceBlock_ && this.sourceBlock_.RTL) {
// The SVG is LTR, force text to be RTL by adding an RLM.
text += '\u200F';
Expand Down
5 changes: 5 additions & 0 deletions core/renderers/common/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ export class RenderInfo {
}
}

// Don't add padding after zero-width fields.
if (prev && Types.isField(prev) && prev.width === 0) {
return this.constants_.NO_PADDING;
}

return this.constants_.MEDIUM_PADDING;
}

Expand Down
6 changes: 6 additions & 0 deletions core/renderers/geras/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ export class RenderInfo extends BaseRenderInfo {
if (!Types.isInput(prev) && (!next || Types.isStatementInput(next))) {
// Between an editable field and the end of the row.
if (Types.isField(prev) && prev.isEditable) {
if (prev.width === 0) {
return this.constants_.NO_PADDING;
}
return this.constants_.MEDIUM_PADDING;
}
// Padding at the end of an icon-only row to make the block shape clearer.
Expand Down Expand Up @@ -276,6 +279,9 @@ export class RenderInfo extends BaseRenderInfo {
Types.isField(next) &&
prev.isEditable === next.isEditable
) {
if (prev.width === 0) {
return this.constants_.NO_PADDING;
}
return this.constants_.LARGE_PADDING;
}

Expand Down
6 changes: 6 additions & 0 deletions core/renderers/thrasos/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export class RenderInfo extends BaseRenderInfo {
if (!Types.isInput(prev) && !next) {
// Between an editable field and the end of the row.
if (Types.isField(prev) && prev.isEditable) {
if (prev.width === 0) {
return this.constants_.NO_PADDING;
}
return this.constants_.MEDIUM_PADDING;
}
// Padding at the end of an icon-only row to make the block shape clearer.
Expand Down Expand Up @@ -204,6 +207,9 @@ export class RenderInfo extends BaseRenderInfo {
Types.isField(next) &&
prev.isEditable === next.isEditable
) {
if (prev.width === 0) {
return this.constants_.NO_PADDING;
}
return this.constants_.LARGE_PADDING;
}

Expand Down
6 changes: 6 additions & 0 deletions core/renderers/zelos/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ export class RenderInfo extends BaseRenderInfo {
if (prev && Types.isLeftSquareCorner(prev) && next && Types.isHat(next)) {
return this.constants_.NO_PADDING;
}

// No space after zero-width fields.
if (prev && Types.isField(prev) && prev.width === 0) {
return this.constants_.NO_PADDING;
}

return this.constants_.MEDIUM_PADDING;
}

Expand Down

0 comments on commit 5347833

Please sign in to comment.