Update Document.fromJson, add DeltaX class

pull/1755/head
Ellet 1 year ago
parent c2c4671f28
commit 431ae4b132
  1. 3
      CHANGELOG.md
  2. 1
      example/lib/main.dart
  3. 3
      example/lib/screens/quill/quill_screen.dart
  4. 48
      lib/src/models/documents/delta_x.dart
  5. 44
      lib/src/models/documents/document.dart
  6. 12
      lib/src/widgets/raw_editor/raw_editor_state.dart
  7. 2
      version.dart

@ -2,6 +2,9 @@
All notable changes to this project will be documented in this file. 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 ## 9.2.14
* feat: move cursor after inserting video/image * feat: move cursor after inserting video/image
* Apple pencil * Apple pencil

@ -1,5 +1,6 @@
import 'package:flutter/foundation.dart' show kIsWeb; import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart' import 'package:flutter_localizations/flutter_localizations.dart'
show show

@ -82,8 +82,7 @@ class _QuillScreenState extends State<QuillScreen> {
MenuItemButton( MenuItemButton(
onPressed: () { onPressed: () {
final html = _controller.document.toDelta().toHtml(); final html = _controller.document.toDelta().toHtml();
_controller.document = _controller.document = Document.fromHtml(html);
Document.fromDelta(Document.fromHtml(html));
}, },
child: const Text('Load with HTML'), child: const Text('Load with HTML'),
), ),

@ -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,
// );
}
}

@ -1,11 +1,7 @@
import 'dart:async' show StreamController; 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 'package:meta/meta.dart';
import '../../../markdown_quill.dart';
import '../../../quill_delta.dart'; import '../../../quill_delta.dart';
import '../../widgets/quill/embeds.dart'; import '../../widgets/quill/embeds.dart';
import '../rules/rule.dart'; import '../rules/rule.dart';
@ -14,6 +10,7 @@ import '../structs/history_changed.dart';
import '../structs/offset_value.dart'; import '../structs/offset_value.dart';
import '../structs/segment_leaf_node.dart'; import '../structs/segment_leaf_node.dart';
import 'attribute.dart'; import 'attribute.dart';
import 'delta_x.dart';
import 'history.dart'; import 'history.dart';
import 'nodes/block.dart'; import 'nodes/block.dart';
import 'nodes/container.dart'; import 'nodes/container.dart';
@ -469,43 +466,10 @@ class Document {
delta.first.key == 'insert'; delta.first.key == 'insert';
} }
/// Convert the HTML Raw string to [Delta] /// Convert the HTML Raw string to [Document]
///
/// 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 @experimental
static Delta fromHtml(String html) { static Document fromHtml(String html) {
final markdown = html2md return Document.fromDelta(DeltaX.fromHtml(html));
.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,
// );
} }
} }

@ -23,6 +23,7 @@ import 'package:html/parser.dart' as html_parser;
import 'package:super_clipboard/super_clipboard.dart'; import 'package:super_clipboard/super_clipboard.dart';
import '../../models/documents/attribute.dart'; import '../../models/documents/attribute.dart';
import '../../models/documents/delta_x.dart';
import '../../models/documents/document.dart'; import '../../models/documents/document.dart';
import '../../models/documents/nodes/block.dart'; import '../../models/documents/nodes/block.dart';
import '../../models/documents/nodes/embeddable.dart'; import '../../models/documents/nodes/embeddable.dart';
@ -219,13 +220,14 @@ class QuillRawEditorState extends EditorState
return; return;
} }
final htmlBody = html_parser.parse(html).body?.outerHtml; final htmlBody = html_parser.parse(html).body?.outerHtml;
final deltaFromClipboard = Document.fromHtml(htmlBody ?? html); final deltaFromClipboard = DeltaX.fromHtml(htmlBody ?? html);
controller.replaceText( controller.replaceText(
textEditingValue.selection.start, textEditingValue.selection.start,
textEditingValue.selection.end - textEditingValue.selection.start, textEditingValue.selection.end - textEditingValue.selection.start,
deltaFromClipboard, deltaFromClipboard,
TextSelection.collapsed(offset: textEditingValue.selection.end)); TextSelection.collapsed(offset: textEditingValue.selection.end),
);
bringIntoView(textEditingValue.selection.extent); bringIntoView(textEditingValue.selection.extent);

@ -1 +1 @@
const version = '9.2.14'; const version = '9.3.0';

Loading…
Cancel
Save