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

Add min/max row/columns to resize card editor #21244

Merged
merged 13 commits into from
Jul 2, 2024
Merged

Conversation

piitaya
Copy link
Member

@piitaya piitaya commented Jul 1, 2024

Proposed change

Allows card to provide a min and max rows and columns for resize card editor in a grid.
I implemented it on 11 cards (future PRs will be open for others cards):

  • tile card
  • entity card
  • thermostat card
  • humidifier card
  • button card
  • media card
  • sensor card
  • map card
  • iframe card
  • statistic card
  • weather forecast card

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

Summary by CodeRabbit

  • New Features

    • Introduced dynamic grid layout calculations for various Lovelace cards and components, allowing for more flexible and responsive user interfaces.
  • Improvements

    • Enhanced the HuiEntityCard, HuiHumidifierCard, HuiThermostatCard, and HuiTileCard with new methods to determine layout options based on card configurations.
    • Improved the HaGridLayoutSlider with refined styles and behaviors for better user interaction.
    • Updated the HuiCardLayoutEditor to better handle default and computed grid size options.
  • Refactor

    • Replaced direct grid size assignments with a new computeSizeOnGrid function to ensure values are within specified bounds.

@piitaya piitaya added this to the 2024.7 milestone Jul 1, 2024
Copy link
Contributor

coderabbitai bot commented Jul 1, 2024

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The updates enhance Lovelace panels and components by incorporating dynamic grid layout calculations. Key additions include the introduction of getLayoutOptions methods across multiple card classes, implementation of the computeSizeOnGrid function for grid calculations, and modifications to the look and behavior of the HaGridLayoutSlider. These changes collectively improve the flexibility and responsiveness of the Lovelace interface, offering customizable grid configurations based on card features and conditions.

Changes

File(s) Summary
src/components/ha-grid-size-picker.ts Introduced disabledColumns and disabledRows to conditionally disable sliders in HaGridSizeEditor component based on min/max values.
src/panels/lovelace/cards/hui-entity-card.ts, hui-humidifier-card.ts, hui-thermostat-card.ts, hui-tile-card.ts Added/updated getLayoutOptions() methods in card classes to calculate layout options based on configuration.
src/panels/lovelace/editor/card-editor/sections/.../hui-grid-section.ts Replaced DEFAULT_GRID_OPTIONS with computeSizeOnGrid function for precise grid size calculations and added GridSizeValue type for rows/columns representation.
src/panels/lovelace/editor/card-editor/.../hui-card-layout-editor.ts Updated functionality to merge default and provided layout options, directly compute grid sizes, and modify render method for layout configuration based on merged options.
src/panels/lovelace/editor/card-editor/ha-grid-layout-slider.ts Altered HaGridLayoutSlider class by adjusting the structure and styles of slider elements, including background and active element opacities, and disabled state styling.
src/panels/lovelace/types.ts Expanded LovelaceLayoutOptions interface to include grid_max_columns, grid_min_columns, grid_min_rows, and grid_max_rows properties for enhanced layout configuration across Lovelace panels.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant UI as Lovelace UI
    participant Card as LovelaceCard
    participant Editor as HaGridSizeEditor
    User->>UI: Load Lovelace Panel
    UI->>Card: Instantiates Card
    Card->>Card: Call getLayoutOptions()
    Card->>UI: Return layout options
    UI->>Editor: Open Layout Editor
    Editor->>Editor: Calculate disabledColumns and disabledRows
    Editor->>UI: Render with updated grid layout and sliders
Loading
sequenceDiagram
    participant User
    participant UI as Lovelace UI
    participant Slider as HaGridLayoutSlider
    User->>UI: Load Grid Layout Editor
    UI->>Slider: Instantiate Slider
    Slider->>Slider: Adjust track structure, background and active elements
    Slider->>UI: Render with new styles and behaviors
Loading

These diagrams illustrate the interaction flow for loading Lovelace cards with dynamic layout options and updating the grid layout editor with redesigned sliders.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Outside diff range comments (4)
src/panels/lovelace/types.ts (1)

Line range hint 118-118: Avoid using any type.

Replace any with a more specific type to avoid disabling type checking rules.

- export interface LovelaceGenericElementEditor<C = any> extends HTMLElement {
+ export interface LovelaceGenericElementEditor<C = unknown> extends HTMLElement {
- setConfig(config: any): void;
+ setConfig(config: Record<string, unknown>): void;

Also applies to: 122-122

src/panels/lovelace/editor/card-editor/hui-card-layout-editor.ts (1)

Line range hint 187-187: Avoid using delete operator and non-null assertion.

Replace delete with undefined assignment and non-null assertion with optional chaining.

- delete newConfig.layout_options;
+ newConfig.layout_options = undefined;

- if (newConfig.layout_options!.grid_columns === undefined) {
+ if (newConfig.layout_options?.grid_columns === undefined) {
- if (newConfig.layout_options!.grid_rows === undefined) {
+ if (newConfig.layout_options?.grid_rows === undefined) {
- if (Object.keys(newConfig.layout_options!).length === 0) {
+ if (Object.keys(newConfig.layout_options ?? {}).length === 0) {
- delete newConfig.layout_options;
+ newConfig.layout_options = undefined;

Also applies to: 205-205, 206-206, 208-208, 209-209, 211-211, 212-212

src/panels/lovelace/sections/hui-grid-section.ts (1)

Line range hint 91-91: Avoid using non-null assertion.

Replace non-null assertion with optional chaining.

- return this._cardConfigKeys.get(cardConfig)!;
+ return this._cardConfigKeys.get(cardConfig) ?? '';

- const { rows, columns } = computeSizeOnGrid(layoutOptions!);
+ const { rows, columns } = computeSizeOnGrid(layoutOptions ?? {});

- this.lovelace!.saveConfig(newConfig);
+ this.lovelace?.saveConfig(newConfig);

- this.lovelace!.config,
+ this.lovelace?.config,

Also applies to: 133-133, 189-189, 193-193

src/panels/lovelace/editor/card-editor/ha-grid-layout-slider.ts (1)

Line range hint 141-141: Add explicit type annotation for savedValue.

The variable savedValue implicitly has the any type. Declare a type or initialize the variable with some value.

-      let savedValue;
+      let savedValue: number | undefined;

Comment on lines 125 to 139
const grid_columns = 2;
let grid_rows = 1;
if (this._config?.features?.length) {
options.grid_rows += Math.ceil((this._config.features.length * 2) / 3);
grid_rows += Math.ceil((this._config.features.length * 2) / 3);
}
if (this._config?.vertical) {
options.grid_rows++;
grid_rows!++;
}
return options;
return {
grid_columns,
grid_rows,
grid_min_rows: grid_rows,
grid_max_rows: grid_rows,
grid_min_columns: grid_columns,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace non-null assertions with optional chaining.

Non-null assertions can lead to runtime errors. Use optional chaining instead.

    const grid_columns = 2;
    let grid_rows = 1;
    if (this._config?.features?.length) {
      grid_rows += Math.ceil((this._config.features.length * 2) / 3);
    }
    if (this._config?.vertical) {
      grid_rows++;
    }
    return {
      grid_columns,
      grid_rows,
      grid_min_rows: grid_rows,
      grid_max_rows: grid_rows,
      grid_min_columns: grid_columns,
    };
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const grid_columns = 2;
let grid_rows = 1;
if (this._config?.features?.length) {
options.grid_rows += Math.ceil((this._config.features.length * 2) / 3);
grid_rows += Math.ceil((this._config.features.length * 2) / 3);
}
if (this._config?.vertical) {
options.grid_rows++;
grid_rows!++;
}
return options;
return {
grid_columns,
grid_rows,
grid_min_rows: grid_rows,
grid_max_rows: grid_rows,
grid_min_columns: grid_columns,
};
const grid_columns = 2;
let grid_rows = 1;
if (this._config?.features?.length) {
grid_rows += Math.ceil((this._config.features.length * 2) / 3);
}
if (this._config?.vertical) {
grid_rows++;
}
return {
grid_columns,
grid_rows,
grid_min_rows: grid_rows,
grid_max_rows: grid_rows,
grid_min_columns: grid_columns,
};
Tools
Biome

[error] 131-131: Forbidden non-null assertion.

(lint/style/noNonNullAssertion)

Comment on lines 45 to 52
const disabledColumns =
this.columnMin !== undefined &&
this.columnMin !== undefined &&
this.columnMin === this.columnMax;
const disabledRows =
this.rowMin !== undefined &&
this.rowMin !== undefined &&
this.rowMin === this.rowMax;
Copy link
Contributor

Choose a reason for hiding this comment

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

Correct the redundant condition in disabledColumns and disabledRows.

The conditions this.columnMin !== undefined and this.rowMin !== undefined are repeated.

- const disabledColumns =
-   this.columnMin !== undefined &&
-   this.columnMin !== undefined &&
-   this.columnMin === this.columnMax;
+ const disabledColumns =
+   this.columnMin !== undefined &&
+   this.columnMin === this.columnMax;

- const disabledRows =
-   this.rowMin !== undefined &&
-   this.rowMin !== undefined &&
-   this.rowMin === this.rowMax;
+ const disabledRows =
+   this.rowMin !== undefined &&
+   this.rowMin === this.rowMax;
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const disabledColumns =
this.columnMin !== undefined &&
this.columnMin !== undefined &&
this.columnMin === this.columnMax;
const disabledRows =
this.rowMin !== undefined &&
this.rowMin !== undefined &&
this.rowMin === this.rowMax;
const disabledColumns =
this.columnMin !== undefined &&
this.columnMin === this.columnMax;
const disabledRows =
this.rowMin !== undefined &&
this.rowMin === this.rowMax;

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Outside diff range comments (2)
src/panels/lovelace/cards/hui-thermostat-card.ts (1)

Line range hint 84-84: Replace non-null assertions with optional chaining.

Non-null assertions can lead to runtime errors. Use optional chaining instead.

- entityId: this._config!.entity,
+ entityId: this._config?.entity,

- .label=${this.hass!.localize(
+ .label=${this.hass?.localize(

- .features=${this._config.features}
+ .features=${this._config?.features}

Also applies to: 128-128, 153-153

src/panels/lovelace/cards/hui-humidifier-card.ts (1)

Line range hint 92-92: Replace non-null assertions with optional chaining.

Non-null assertions can lead to runtime errors. Use optional chaining instead.

- entityId: this._config!.entity,
+ entityId: this._config?.entity,

- .label=${this.hass!.localize(
+ .label=${this.hass?.localize(

- .features=${this._config.features}
+ .features=${this._config?.features}

Also applies to: 136-136, 161-161

src/panels/lovelace/cards/hui-tile-card.ts Outdated Show resolved Hide resolved
src/panels/lovelace/cards/hui-tile-card.ts Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Comment on lines 248 to 255
public getLayoutOptions(): LovelaceLayoutOptions {
if (this._config?.footer) {
return {
grid_columns: 2,
};
}
return {
grid_columns: 2,
grid_rows: 2,
grid_min_rows: 2,
};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor for readability and maintainability.

The getLayoutOptions method can be refactored to improve readability and maintainability. Consider extracting the logic into smaller helper functions.

public getLayoutOptions(): LovelaceLayoutOptions {
  const grid_columns = 2;
  const grid_min_columns = 2;
  const { grid_rows, grid_min_rows, grid_max_rows } = this.calculateGridRows();

  return {
    grid_columns,
    grid_rows,
    grid_min_rows,
    grid_max_rows,
    grid_min_columns,
  };
}

private calculateGridRows() {
  let grid_rows = 2;
  let grid_min_rows = 2;
  let grid_max_rows = 2;

  if (this._config?.footer) {
    grid_rows = 1;
    grid_min_rows = 1;
    grid_max_rows = 1;
  }

  return { grid_rows, grid_min_rows, grid_max_rows };
}

Comment on lines +125 to +139
const grid_columns = 2;
let grid_rows = 1;
if (this._config?.features?.length) {
options.grid_rows += Math.ceil((this._config.features.length * 2) / 3);
const featureHeight = Math.ceil((this._config.features.length * 2) / 3);
grid_rows += featureHeight;
}
if (this._config?.vertical) {
options.grid_rows++;
grid_rows!++;
}
return options;
return {
grid_columns,
grid_rows,
grid_min_rows: grid_rows,
grid_min_columns: grid_columns,
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor for readability and maintainability.

The getLayoutOptions method can be refactored to improve readability and maintainability. Consider extracting the logic into smaller helper functions.

public getLayoutOptions(): LovelaceLayoutOptions {
  const grid_columns = 2;
  const grid_min_columns = 2;
  const { grid_rows, grid_min_rows, grid_max_rows } = this.calculateGridRows();

  return {
    grid_columns,
    grid_rows,
    grid_min_rows,
    grid_max_rows,
    grid_min_columns,
  };
}

private calculateGridRows() {
  let grid_rows = 1;
  let grid_min_rows = 1;
  let grid_max_rows = 1;

  if (this._config?.features?.length) {
    const featureHeight = Math.ceil((this._config.features.length * 2) / 3);
    grid_rows += featureHeight;
    grid_min_rows += featureHeight;
    grid_max_rows += featureHeight;
  }

  if (this._config?.vertical) {
    grid_rows++;
    grid_min_rows++;
    grid_max_rows++;
  }

  return { grid_rows, grid_min_rows, grid_max_rows };
}
Tools
Biome

[error] 132-132: Forbidden non-null assertion.

(lint/style/noNonNullAssertion)


Replace non-null assertions with optional chaining.

Non-null assertions can lead to runtime errors. Use optional chaining instead.

    const grid_columns = 2;
    let grid_rows = 1;
    if (this._config?.features?.length) {
      const featureHeight = Math.ceil((this._config.features.length * 2) / 3);
      grid_rows += featureHeight;
    }
    if (this._config?.vertical) {
      grid_rows++;
    }
    return {
      grid_columns,
      grid_rows,
      grid_min_rows: grid_rows,
      grid_min_columns: grid_columns,
    };
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const grid_columns = 2;
let grid_rows = 1;
if (this._config?.features?.length) {
options.grid_rows += Math.ceil((this._config.features.length * 2) / 3);
const featureHeight = Math.ceil((this._config.features.length * 2) / 3);
grid_rows += featureHeight;
}
if (this._config?.vertical) {
options.grid_rows++;
grid_rows!++;
}
return options;
return {
grid_columns,
grid_rows,
grid_min_rows: grid_rows,
grid_min_columns: grid_columns,
};
const grid_columns = 2;
let grid_rows = 1;
if (this._config?.features?.length) {
const featureHeight = Math.ceil((this._config.features.length * 2) / 3);
grid_rows += featureHeight;
}
if (this._config?.vertical) {
grid_rows++;
}
return {
grid_columns,
grid_rows,
grid_min_rows: grid_rows,
grid_min_columns: grid_columns,
};
Tools
Biome

[error] 132-132: Forbidden non-null assertion.

(lint/style/noNonNullAssertion)

@piitaya
Copy link
Member Author

piitaya commented Jul 2, 2024

@coderabbitai pause

bramkragten
bramkragten previously approved these changes Jul 2, 2024
@piitaya piitaya requested a review from bramkragten July 2, 2024 18:35
@bramkragten bramkragten merged commit 094203f into dev Jul 2, 2024
13 checks passed
@bramkragten bramkragten deleted the resize_card_min_max branch July 2, 2024 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants