Skip to content

Commit

Permalink
Feat: added support for another way to make a check list
Browse files Browse the repository at this point in the history
  • Loading branch information
CatHood0 committed Jul 8, 2024
1 parent 16e1783 commit 4e1201a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.2.2

* Feat: added support for data-checked attribute for <li> tags
* Fix: removed duplicated getting inline attributes at resolveCurrentElement

## 1.2.1

* Feat: added support for align attribute (a different way of align the text, like: text-align)
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Flutter Quill Delta from HTML

This is a Dart package that converts HTML input into Quill Delta format, which is used in the flutter_quill package. This allows developers to easily convert HTML content to a format that can be displayed and edited using the Quill rich text editor in Flutter applications.
This is a Dart package that converts HTML input into Quill Delta format, which is used in the `flutter_quill` package. This allows developers to easily convert `HTML` content to a format that can be displayed and edited using the Quill rich text editor in Flutter applications.

**This package** supports the conversion of a wide range of HTML tags and attributes into their corresponding Delta operations, ensuring that your HTML content is accurately represented in the Quill editor.

Expand All @@ -22,7 +22,8 @@ This is a Dart package that converts HTML input into Quill Delta format, which i
<ul>: Unordered lists
<ol>: Ordered lists
<li>: List items
<input type="checkbox">: Check lists
<li data-checked="true">: Check lists
<input type="checkbox">: Another alternative to make a check lists

Links
<a>: Hyperlinks with support for the href attribute
Expand Down Expand Up @@ -65,7 +66,7 @@ Add the dependency to your pubspec.yaml:

```yaml
dependencies:
flutter_quill_delta_from_html: ^1.2.1
flutter_quill_delta_from_html: ^1.2.2
```
Then, import the package and use it in your Flutter application:
Expand Down
8 changes: 8 additions & 0 deletions lib/parser/html_to_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ class DefaultHtmlToOperations extends HtmlOperations {
}
}
for (final item in items) {
if (checkbox == null) {
final dataChecked = item.attributes['data-checked'] ?? '';
final blockAttrs = parseStyleAttribute(dataChecked);
var isCheckList = item.localName == 'li' && blockAttrs.isNotEmpty && blockAttrs.containsKey('list');
if (isCheckList) {
attributes['list'] = blockAttrs['list'];
}
}
for (final node in item.nodes) {
if (node.nodeType == dom.Node.TEXT_NODE) {
delta.insert(node.text);
Expand Down
8 changes: 8 additions & 0 deletions lib/parser/html_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ Map<String, dynamic> parseStyleAttribute(String style) {
attributes['align'] = style;
case 'rtl':
attributes['direction'] = 'rtl';
case 'true' || 'false':
//then is check list
if (style == 'true') {
attributes['list'] = 'checked';
} else {
attributes['list'] = 'unchecked';
}
break;
default:
break;
}
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_quill_delta_from_html
description: "Convert easily HTML inputs content to Quill Delta format"
version: 1.2.1
version: 1.2.2
homepage:
repository: https://github.com/CatHood0/flutter_quill_delta_from_html/
issue_tracker: https://github.com/CatHood0/flutter_quill_delta_from_html/issues
Expand All @@ -12,7 +12,7 @@ environment:
dependencies:
flutter:
sdk: flutter
dart_quill_delta: ^9.5.9
dart_quill_delta: ^9.5.12
html: ^0.15.4
meta: ^1.12.0

Expand Down
16 changes: 16 additions & 0 deletions test/flutter_quill_delta_from_html_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ void main() {
expect(delta, expectedDelta);
});

test('Checklist', () {
const html =
'<ul><li data-checked="true">First item</li><li data-checked="false">Second item</li></ul>';
final converter = HtmlToDelta();
final delta = converter.convert(html);

final expectedDelta = Delta()
..insert('First item')
..insert('\n', {'list': 'checked'})
..insert('Second item')
..insert('\n', {'list': 'unchecked'})
..insert('\n');

expect(delta, expectedDelta);
});

test('Image', () {
const html = '<p>This is an image:</p><img src="https://example.com/image.png" />';
final converter = HtmlToDelta();
Expand Down

0 comments on commit 4e1201a

Please sign in to comment.