From 4083c8c56891f28c3b5437ed9d9e2bbeeede42c3 Mon Sep 17 00:00:00 2001 From: CatHood0 Date: Wed, 10 Jul 2024 20:36:44 -0400 Subject: [PATCH] feat: support for indentation based on the padding of the tag --- README.md | 10 +----- lib/parser/html_to_operation.dart | 12 +++++-- lib/parser/html_utils.dart | 10 ++++++ lib/parser/indent_parser.dart | 52 +++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 lib/parser/indent_parser.dart diff --git a/README.md b/README.md index 31e77fd..91bb519 100644 --- a/README.md +++ b/README.md @@ -49,18 +49,10 @@ This is a **Dart** package that converts **HTML** input into Quill **Delta** for

: Paragraph direction -

: Inline attributes +

: Inline attributes : Custom html - -``` - -## Not supported tags - -```html - -

``` ## Getting Started diff --git a/lib/parser/html_to_operation.dart b/lib/parser/html_to_operation.dart index 3f36b65..d99416a 100644 --- a/lib/parser/html_to_operation.dart +++ b/lib/parser/html_to_operation.dart @@ -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); } @@ -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); } diff --git a/lib/parser/html_utils.dart b/lib/parser/html_utils.dart index 58db728..c4a876d 100644 --- a/lib/parser/html_utils.dart +++ b/lib/parser/html_utils.dart @@ -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'; @@ -61,6 +62,12 @@ Map 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; @@ -182,6 +189,7 @@ void processNode( if (addSpanAttrs) { newAttributes.remove('align'); newAttributes.remove('direction'); + newAttributes.remove('indent'); newAttributes.addAll(spanAttributes); } } @@ -190,6 +198,7 @@ void processNode( if (node.isLink) { final String? src = node.attributes['href']; if (src != null) { + newAttributes.remove('indent'); newAttributes['link'] = src; } } @@ -198,6 +207,7 @@ void processNode( if (node.isBreakLine) { newAttributes.remove('align'); newAttributes.remove('direction'); + newAttributes.remove('indent'); delta.insert('\n', newAttributes); } } diff --git a/lib/parser/indent_parser.dart b/lib/parser/indent_parser.dart new file mode 100644 index 0000000..6324130 --- /dev/null +++ b/lib/parser/indent_parser.dart @@ -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; +}