Skip to content

Commit 431ae4b

Browse files
author
Ellet
committed
Update Document.fromJson, add DeltaX class
1 parent c2c4671 commit 431ae4b

File tree

7 files changed

+65
-48
lines changed

7 files changed

+65
-48
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 9.3.0
6+
* Breaking change: `Document.fromHtml(html)` is now returns `Document` instead of `Delta`, use `DeltaX.fromHtml` to return `Delta`
7+
58
## 9.2.14
69
* feat: move cursor after inserting video/image
710
* Apple pencil

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/foundation.dart' show kIsWeb;
22
import 'package:flutter/material.dart';
3+
import 'package:flutter/services.dart';
34
import 'package:flutter_bloc/flutter_bloc.dart';
45
import 'package:flutter_localizations/flutter_localizations.dart'
56
show

example/lib/screens/quill/quill_screen.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ class _QuillScreenState extends State<QuillScreen> {
8282
MenuItemButton(
8383
onPressed: () {
8484
final html = _controller.document.toDelta().toHtml();
85-
_controller.document =
86-
Document.fromDelta(Document.fromHtml(html));
85+
_controller.document = Document.fromHtml(html);
8786
},
8887
child: const Text('Load with HTML'),
8988
),

lib/src/models/documents/delta_x.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import 'package:html2md/html2md.dart' as html2md;
2+
import 'package:markdown/markdown.dart' as md;
3+
import 'package:meta/meta.dart';
4+
5+
import '../../../markdown_quill.dart';
6+
import '../../../quill_delta.dart';
7+
8+
@immutable
9+
class DeltaX {
10+
/// Convert the HTML Raw string to [Delta]
11+
///
12+
/// It will run using the following steps:
13+
///
14+
/// 1. Convert the html to markdown string using `html2md` package
15+
/// 2. Convert the markdown string to quill delta json string
16+
/// 3. Decode the delta json string to [Delta]
17+
///
18+
/// for more [info](https://github.com/singerdmx/flutter-quill/issues/1100)
19+
///
20+
/// Please notice that this api is designed to be used internally and shouldn't
21+
/// used for real world applications
22+
///
23+
@experimental
24+
static Delta fromHtml(String html) {
25+
final markdown = html2md
26+
.convert(
27+
html,
28+
)
29+
.replaceAll('unsafe:', '');
30+
31+
final mdDocument = md.Document(encodeHtml: false);
32+
33+
final mdToDelta = MarkdownToDelta(markdownDocument: mdDocument);
34+
35+
return mdToDelta.convert(markdown);
36+
37+
// final deltaJsonString = markdownToDelta(markdown);
38+
// final deltaJson = jsonDecode(deltaJsonString);
39+
// if (deltaJson is! List) {
40+
// throw ArgumentError(
41+
// 'The delta json string should be of type list when jsonDecode() it',
42+
// );
43+
// }
44+
// return Delta.fromJson(
45+
// deltaJson,
46+
// );
47+
}
48+
}

lib/src/models/documents/document.dart

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import 'dart:async' show StreamController;
22

3-
import 'package:html2md/html2md.dart' as html2md;
4-
import 'package:markdown/markdown.dart' as md;
53
import 'package:meta/meta.dart';
64

7-
import '../../../markdown_quill.dart';
8-
95
import '../../../quill_delta.dart';
106
import '../../widgets/quill/embeds.dart';
117
import '../rules/rule.dart';
@@ -14,6 +10,7 @@ import '../structs/history_changed.dart';
1410
import '../structs/offset_value.dart';
1511
import '../structs/segment_leaf_node.dart';
1612
import 'attribute.dart';
13+
import 'delta_x.dart';
1714
import 'history.dart';
1815
import 'nodes/block.dart';
1916
import 'nodes/container.dart';
@@ -469,43 +466,10 @@ class Document {
469466
delta.first.key == 'insert';
470467
}
471468

472-
/// Convert the HTML Raw string to [Delta]
473-
///
474-
/// It will run using the following steps:
475-
///
476-
/// 1. Convert the html to markdown string using `html2md` package
477-
/// 2. Convert the markdown string to quill delta json string
478-
/// 3. Decode the delta json string to [Delta]
479-
///
480-
/// for more [info](https://github.com/singerdmx/flutter-quill/issues/1100)
481-
///
482-
/// Please notice that this api is designed to be used internally and shouldn't
483-
/// used for real world applications
484-
///
469+
/// Convert the HTML Raw string to [Document]
485470
@experimental
486-
static Delta fromHtml(String html) {
487-
final markdown = html2md
488-
.convert(
489-
html,
490-
)
491-
.replaceAll('unsafe:', '');
492-
493-
final mdDocument = md.Document(encodeHtml: false);
494-
495-
final mdToDelta = MarkdownToDelta(markdownDocument: mdDocument);
496-
497-
return mdToDelta.convert(markdown);
498-
499-
// final deltaJsonString = markdownToDelta(markdown);
500-
// final deltaJson = jsonDecode(deltaJsonString);
501-
// if (deltaJson is! List) {
502-
// throw ArgumentError(
503-
// 'The delta json string should be of type list when jsonDecode() it',
504-
// );
505-
// }
506-
// return Delta.fromJson(
507-
// deltaJson,
508-
// );
471+
static Document fromHtml(String html) {
472+
return Document.fromDelta(DeltaX.fromHtml(html));
509473
}
510474
}
511475

lib/src/widgets/raw_editor/raw_editor_state.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'package:html/parser.dart' as html_parser;
2323
import 'package:super_clipboard/super_clipboard.dart';
2424

2525
import '../../models/documents/attribute.dart';
26+
import '../../models/documents/delta_x.dart';
2627
import '../../models/documents/document.dart';
2728
import '../../models/documents/nodes/block.dart';
2829
import '../../models/documents/nodes/embeddable.dart';
@@ -219,13 +220,14 @@ class QuillRawEditorState extends EditorState
219220
return;
220221
}
221222
final htmlBody = html_parser.parse(html).body?.outerHtml;
222-
final deltaFromClipboard = Document.fromHtml(htmlBody ?? html);
223+
final deltaFromClipboard = DeltaX.fromHtml(htmlBody ?? html);
223224

224225
controller.replaceText(
225-
textEditingValue.selection.start,
226-
textEditingValue.selection.end - textEditingValue.selection.start,
227-
deltaFromClipboard,
228-
TextSelection.collapsed(offset: textEditingValue.selection.end));
226+
textEditingValue.selection.start,
227+
textEditingValue.selection.end - textEditingValue.selection.start,
228+
deltaFromClipboard,
229+
TextSelection.collapsed(offset: textEditingValue.selection.end),
230+
);
229231

230232
bringIntoView(textEditingValue.selection.extent);
231233

version.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const version = '9.2.14';
1+
const version = '9.3.0';

0 commit comments

Comments
 (0)