-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for indentation based on the padding of the tag
- Loading branch information
CatHood0
committed
Jul 11, 2024
1 parent
a322196
commit 4083c8c
Showing
4 changed files
with
73 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,52 @@ | ||
/// Converts a CSS `padding-left` || 'padding-right' value to a standardized indentation level. | ||
/// | ||
/// The method supports various units such as `px`, `pt`, `pc`, `em`, `rem`, and `%`. | ||
/// The conversion is based on the following assumptions: | ||
/// - `16px` equals 1 indentation level | ||
/// - `12pt` equals 1 indentation level | ||
/// - `1pc` equals 1 indentation level | ||
/// - `1em` or `1rem` equals 1 indentation level | ||
/// - `100%` equals 1 indentation level | ||
/// | ||
/// If the unit is not recognized, the indentation level defaults to 0. | ||
/// | ||
/// [value] The CSS `padding-left` value as a string (e.g., "32px", "2em"). | ||
/// | ||
/// Returns the corresponding indentation level as an integer. | ||
int parseToIndent(String value) { | ||
// Extract numeric part from the value and parse it to double | ||
double indentValue = double.tryParse(value.replaceAll(RegExp(r'[^\d.]'), '')) ?? 0; | ||
|
||
// Extract the unit part from the value | ||
String unit = value.replaceAll(RegExp(r'[\d.]'), '').trim(); | ||
|
||
// Convert the value to an indentation level based on the unit | ||
switch (unit) { | ||
case 'px': | ||
indentValue /= 16; // Assume 16px = 1 indent level | ||
break; | ||
case 'pt': | ||
indentValue /= 12; // Assume 12pt = 1 indent level | ||
break; | ||
case 'pc': | ||
indentValue *= 1; // 1pc = 1 indent level | ||
break; | ||
case 'em': | ||
case 'rem': | ||
indentValue *= 1; // 1em or 1rem = 1 indent level | ||
break; | ||
case '%': | ||
indentValue /= 100; // Assume 100% = 1 indent level | ||
break; | ||
default: | ||
indentValue = 0; // If unit is not recognized, set indent level to 0 | ||
break; | ||
} | ||
// Round to the nearest whole number for the indent level | ||
int indentLevel = indentValue.round(); | ||
return indentLevel < 1 | ||
? 0 | ||
: indentLevel > 5 | ||
? 5 | ||
: indentLevel; | ||
} |