-
Notifications
You must be signed in to change notification settings - Fork 1
VB-277 :fix(VB-277/truncate-handlling):function added to remove and store styles #504
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
Merged
csAdityaPachauri
merged 5 commits into
1st-oct-2025
from
VB-277/truncate-handlling-develop_v4
Sep 24, 2025
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ed0bac2
fix(VB-277/truncate-handlling):function added to remove and store styles
csAdityaPachauri 4a8303a
fix:utility added for styles handlling
csAdityaPachauri 48b4fcc
fix(function-description): added
csAdityaPachauri e991dd1
fix: imports
csAdityaPachauri b225a71
feat(VB-277):unit test cases addd
csAdityaPachauri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| /** | ||
| * Utility module for handling -webkit-line-clamp, overflow, and white-space properties | ||
| * when elements are focused/unfocused in the visual builder | ||
| */ | ||
|
|
||
| interface LineClampStyles { | ||
| webkitLineClamp?: string; | ||
| overflow?: string; | ||
| display?: string; | ||
| webkitBoxOrient?: string; | ||
| whiteSpace?: string; | ||
| } | ||
|
|
||
| // WeakMap to store original styles for each element | ||
| const originalStyles = new WeakMap<Element, LineClampStyles>(); | ||
|
|
||
| /** | ||
| * Removes truncate related styles from an element and stores the original values | ||
| * @param element - The element to remove line-clamp and white-space styles from | ||
| */ | ||
| export function removeTruncateStyles(element: Element): void { | ||
| if (!element || !(element instanceof HTMLElement)) return; | ||
|
|
||
| const computedStyles = window.getComputedStyle(element); | ||
| const styles: LineClampStyles = {}; | ||
|
|
||
| // Store original styles if they exist | ||
| if ( | ||
| computedStyles.webkitLineClamp && | ||
| computedStyles.webkitLineClamp !== "none" | ||
| ) { | ||
| styles.webkitLineClamp = computedStyles.webkitLineClamp; | ||
| } | ||
|
|
||
| if (computedStyles.overflow && computedStyles.overflow !== "visible") { | ||
| styles.overflow = computedStyles.overflow; | ||
| } | ||
|
|
||
| if (computedStyles.display && computedStyles.display.includes("box")) { | ||
| styles.display = computedStyles.display; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should avoid this. This will turn inline span into block elements. |
||
|
|
||
| if ( | ||
| computedStyles.webkitBoxOrient && | ||
| computedStyles.webkitBoxOrient !== "horizontal" | ||
| ) { | ||
| styles.webkitBoxOrient = computedStyles.webkitBoxOrient; | ||
| } | ||
|
|
||
| if (computedStyles.whiteSpace && computedStyles.whiteSpace === "nowrap") { | ||
| styles.whiteSpace = computedStyles.whiteSpace; | ||
| } | ||
|
|
||
| // Only store if we found line-clamp related styles | ||
| if (Object.keys(styles).length > 0) { | ||
| originalStyles.set(element, styles); | ||
|
|
||
| // Remove the styles | ||
| element.style.webkitLineClamp = "none"; | ||
| element.style.overflow = "visible"; | ||
| // Reset display if it was -webkit-box | ||
| if (styles.display?.includes("box")) { | ||
| element.style.display = "block"; | ||
| } | ||
| if (styles.webkitBoxOrient) { | ||
| element.style.webkitBoxOrient = "horizontal"; | ||
| } | ||
| if (styles.whiteSpace) { | ||
| element.style.whiteSpace = "normal"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Restores the original truncate related styles to an element | ||
| * @param element - The element to restore line-clamp and white-space styles to | ||
| */ | ||
| export function restoreTruncateStyles(element: Element): void { | ||
| if (!element || !(element instanceof HTMLElement)) return; | ||
|
|
||
| const storedStyles = originalStyles.get(element); | ||
| if (!storedStyles) return; | ||
|
|
||
| // Restore original styles | ||
| if (storedStyles.webkitLineClamp) { | ||
| element.style.webkitLineClamp = storedStyles.webkitLineClamp; | ||
| } else { | ||
| element.style.removeProperty("-webkit-line-clamp"); | ||
| } | ||
|
|
||
| if (storedStyles.overflow) { | ||
| element.style.overflow = storedStyles.overflow; | ||
| } else { | ||
| element.style.removeProperty("overflow"); | ||
| } | ||
|
|
||
| if (storedStyles.display) { | ||
| element.style.display = storedStyles.display; | ||
| } else { | ||
| element.style.removeProperty("display"); | ||
| } | ||
|
|
||
| if (storedStyles.webkitBoxOrient) { | ||
| element.style.webkitBoxOrient = storedStyles.webkitBoxOrient; | ||
| } else { | ||
| element.style.removeProperty("-webkit-box-orient"); | ||
| } | ||
|
|
||
| if (storedStyles.whiteSpace) { | ||
| element.style.whiteSpace = storedStyles.whiteSpace; | ||
| } else { | ||
| element.style.removeProperty("white-space"); | ||
| } | ||
|
|
||
| // Clean up stored styles | ||
| originalStyles.delete(element); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if an element has truncate or white-space styles applied | ||
| * @param element - The element to check | ||
| * @returns boolean indicating if the element has line-clamp or white-space styles | ||
| */ | ||
| export function hasTruncateStyles(element: Element): boolean { | ||
| if (!element || !(element instanceof HTMLElement)) return false; | ||
|
|
||
| const computedStyles = window.getComputedStyle(element); | ||
| return ( | ||
| computedStyles.webkitLineClamp !== "none" || | ||
| (computedStyles.overflow === "hidden" && | ||
| computedStyles.display?.includes("box") && | ||
| computedStyles.webkitBoxOrient === "vertical") || | ||
| computedStyles.whiteSpace === "nowrap" | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if an element currently has stored truncate styles | ||
| * @param element - The element to check | ||
| * @returns boolean indicating if the element has stored styles | ||
| */ | ||
| export function hasStoredLineClampStyles(element: Element): boolean { | ||
| return originalStyles.has(element); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make a utility for hiding and showing the original element.