diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aa391a0..3686143d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. +## 9.3.0 +* Breaking change: `Document.fromHtml(html)` is now returns `Document` instead of `Delta`, use `DeltaX.fromHtml` to return `Delta` + ## 9.2.14 * feat: move cursor after inserting video/image * Apple pencil diff --git a/example/lib/main.dart b/example/lib/main.dart index fbca48eb..989bdb1a 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart' show kIsWeb; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_localizations/flutter_localizations.dart' show diff --git a/example/lib/screens/quill/quill_screen.dart b/example/lib/screens/quill/quill_screen.dart index ea8b23bc..29aec158 100644 --- a/example/lib/screens/quill/quill_screen.dart +++ b/example/lib/screens/quill/quill_screen.dart @@ -82,8 +82,7 @@ class _QuillScreenState extends State { MenuItemButton( onPressed: () { final html = _controller.document.toDelta().toHtml(); - _controller.document = - Document.fromDelta(Document.fromHtml(html)); + _controller.document = Document.fromHtml(html); }, child: const Text('Load with HTML'), ), diff --git a/lib/src/models/documents/delta_x.dart b/lib/src/models/documents/delta_x.dart new file mode 100644 index 00000000..76c6489d --- /dev/null +++ b/lib/src/models/documents/delta_x.dart @@ -0,0 +1,48 @@ +import 'package:html2md/html2md.dart' as html2md; +import 'package:markdown/markdown.dart' as md; +import 'package:meta/meta.dart'; + +import '../../../markdown_quill.dart'; +import '../../../quill_delta.dart'; + +@immutable +class DeltaX { + /// Convert the HTML Raw string to [Delta] + /// + /// It will run using the following steps: + /// + /// 1. Convert the html to markdown string using `html2md` package + /// 2. Convert the markdown string to quill delta json string + /// 3. Decode the delta json string to [Delta] + /// + /// for more [info](https://github.com/singerdmx/flutter-quill/issues/1100) + /// + /// Please notice that this api is designed to be used internally and shouldn't + /// used for real world applications + /// + @experimental + static Delta fromHtml(String html) { + final markdown = html2md + .convert( + html, + ) + .replaceAll('unsafe:', ''); + + final mdDocument = md.Document(encodeHtml: false); + + final mdToDelta = MarkdownToDelta(markdownDocument: mdDocument); + + return mdToDelta.convert(markdown); + + // final deltaJsonString = markdownToDelta(markdown); + // final deltaJson = jsonDecode(deltaJsonString); + // if (deltaJson is! List) { + // throw ArgumentError( + // 'The delta json string should be of type list when jsonDecode() it', + // ); + // } + // return Delta.fromJson( + // deltaJson, + // ); + } +} diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index ce628007..82c6ebd5 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.dart @@ -1,11 +1,7 @@ import 'dart:async' show StreamController; -import 'package:html2md/html2md.dart' as html2md; -import 'package:markdown/markdown.dart' as md; import 'package:meta/meta.dart'; -import '../../../markdown_quill.dart'; - import '../../../quill_delta.dart'; import '../../widgets/quill/embeds.dart'; import '../rules/rule.dart'; @@ -14,6 +10,7 @@ import '../structs/history_changed.dart'; import '../structs/offset_value.dart'; import '../structs/segment_leaf_node.dart'; import 'attribute.dart'; +import 'delta_x.dart'; import 'history.dart'; import 'nodes/block.dart'; import 'nodes/container.dart'; @@ -469,43 +466,10 @@ class Document { delta.first.key == 'insert'; } - /// Convert the HTML Raw string to [Delta] - /// - /// It will run using the following steps: - /// - /// 1. Convert the html to markdown string using `html2md` package - /// 2. Convert the markdown string to quill delta json string - /// 3. Decode the delta json string to [Delta] - /// - /// for more [info](https://github.com/singerdmx/flutter-quill/issues/1100) - /// - /// Please notice that this api is designed to be used internally and shouldn't - /// used for real world applications - /// + /// Convert the HTML Raw string to [Document] @experimental - static Delta fromHtml(String html) { - final markdown = html2md - .convert( - html, - ) - .replaceAll('unsafe:', ''); - - final mdDocument = md.Document(encodeHtml: false); - - final mdToDelta = MarkdownToDelta(markdownDocument: mdDocument); - - return mdToDelta.convert(markdown); - - // final deltaJsonString = markdownToDelta(markdown); - // final deltaJson = jsonDecode(deltaJsonString); - // if (deltaJson is! List) { - // throw ArgumentError( - // 'The delta json string should be of type list when jsonDecode() it', - // ); - // } - // return Delta.fromJson( - // deltaJson, - // ); + static Document fromHtml(String html) { + return Document.fromDelta(DeltaX.fromHtml(html)); } } diff --git a/lib/src/widgets/raw_editor/raw_editor_state.dart b/lib/src/widgets/raw_editor/raw_editor_state.dart index c943fa6a..c9f88df5 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state.dart @@ -23,6 +23,7 @@ import 'package:html/parser.dart' as html_parser; import 'package:super_clipboard/super_clipboard.dart'; import '../../models/documents/attribute.dart'; +import '../../models/documents/delta_x.dart'; import '../../models/documents/document.dart'; import '../../models/documents/nodes/block.dart'; import '../../models/documents/nodes/embeddable.dart'; @@ -219,13 +220,14 @@ class QuillRawEditorState extends EditorState return; } final htmlBody = html_parser.parse(html).body?.outerHtml; - final deltaFromClipboard = Document.fromHtml(htmlBody ?? html); + final deltaFromClipboard = DeltaX.fromHtml(htmlBody ?? html); controller.replaceText( - textEditingValue.selection.start, - textEditingValue.selection.end - textEditingValue.selection.start, - deltaFromClipboard, - TextSelection.collapsed(offset: textEditingValue.selection.end)); + textEditingValue.selection.start, + textEditingValue.selection.end - textEditingValue.selection.start, + deltaFromClipboard, + TextSelection.collapsed(offset: textEditingValue.selection.end), + ); bringIntoView(textEditingValue.selection.extent); diff --git a/version.dart b/version.dart index ab976e8e..716ba77c 100644 --- a/version.dart +++ b/version.dart @@ -1 +1 @@ -const version = '9.2.14'; +const version = '9.3.0';