Skip to content

Commit

Permalink
feat: support for indentation based on the padding of the tag
Browse files Browse the repository at this point in the history
  • Loading branch information
CatHood0 committed Jul 11, 2024
1 parent a322196 commit 4083c8c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 11 deletions.
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,10 @@ This is a **Dart** package that converts **HTML** input into Quill **Delta** for
<p dir="rtl">: Paragraph direction

<!--Text attributes-->
<p style="line-height: 1.0px;font-size: 12px;font-family: Times New Roman;color:#ffffff">: Inline attributes
<p style="padding: 10px;line-height: 1.0px;font-size: 12px;font-family: Times New Roman;color:#ffffff">: Inline attributes

<!--Custom Blocks-->
<pullquote data-author="john">: Custom html

```

## Not supported tags

```html
<!--Text indent-->
<p style="padding: 10px">
```

## Getting Started
Expand Down
12 changes: 10 additions & 2 deletions lib/parser/html_to_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ class DefaultHtmlToOperations extends HtmlOperations {
final alignAttribute = parseStyleAttribute(styles2 ?? '');
final dirAttribute = parseStyleAttribute(styles3 ?? '');
styleAttributes.addAll({...alignAttribute, ...dirAttribute});
if (styleAttributes.containsKey('align') || styleAttributes.containsKey('direction')) {
if (styleAttributes.containsKey('align') ||
styleAttributes.containsKey('direction') ||
styleAttributes.containsKey('indent')) {
blockAttributes['align'] = styleAttributes['align'];
blockAttributes['direction'] = styleAttributes['direction'];
blockAttributes['indent'] = styleAttributes['indent'];
styleAttributes.remove('align');
styleAttributes.remove('direction');
styleAttributes.remove('indent');
}
inlineAttributes.addAll(styleAttributes);
}
Expand Down Expand Up @@ -206,11 +210,15 @@ class DefaultHtmlToOperations extends HtmlOperations {
final alignAttribute = parseStyleAttribute(styles2 ?? '');
final dirAttribute = parseStyleAttribute(styles3 ?? '');
styleAttributes.addAll({...alignAttribute, ...dirAttribute});
if (styleAttributes.containsKey('align') || styleAttributes.containsKey('direction')) {
if (styleAttributes.containsKey('align') ||
styleAttributes.containsKey('direction') ||
styleAttributes.containsKey('indent')) {
blockAttributes['align'] = styleAttributes['align'];
blockAttributes['direction'] = styleAttributes['direction'];
blockAttributes['indent'] = styleAttributes['indent'];
styleAttributes.remove('align');
styleAttributes.remove('direction');
styleAttributes.remove('indent');
}
attributes.addAll(styleAttributes);
}
Expand Down
10 changes: 10 additions & 0 deletions lib/parser/html_utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:dart_quill_delta/dart_quill_delta.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_quill_delta_from_html/parser/extensions/node_ext.dart';
import 'package:flutter_quill_delta_from_html/parser/indent_parser.dart';
import 'package:html/dom.dart' as dom;
import 'colors.dart';
import 'custom_html_part.dart';
Expand Down Expand Up @@ -61,6 +62,12 @@ Map<String, dynamic> parseStyleAttribute(String style) {
final color = validateAndGetColor(value);
attributes['background'] = color;
break;
case 'padding-left' || 'padding-right':
final indentation = parseToIndent(value);
if (indentation != 0) {
attributes['indent'] = indentation;
}
break;
case 'font-size':
String? sizeToPass;

Expand Down Expand Up @@ -182,6 +189,7 @@ void processNode(
if (addSpanAttrs) {
newAttributes.remove('align');
newAttributes.remove('direction');
newAttributes.remove('indent');
newAttributes.addAll(spanAttributes);
}
}
Expand All @@ -190,6 +198,7 @@ void processNode(
if (node.isLink) {
final String? src = node.attributes['href'];
if (src != null) {
newAttributes.remove('indent');
newAttributes['link'] = src;
}
}
Expand All @@ -198,6 +207,7 @@ void processNode(
if (node.isBreakLine) {
newAttributes.remove('align');
newAttributes.remove('direction');
newAttributes.remove('indent');
delta.insert('\n', newAttributes);
}
}
Expand Down
52 changes: 52 additions & 0 deletions lib/parser/indent_parser.dart
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;
}

0 comments on commit 4083c8c

Please sign in to comment.