From bc40fb875a1a93e501c8fc8f1579ad0af7e19990 Mon Sep 17 00:00:00 2001 From: Ellet Date: Wed, 13 Dec 2023 11:25:47 +0300 Subject: [PATCH] Move internal method from Html to document instead of the controller --- lib/src/models/documents/document.dart | 39 +++++++++++++++++++ lib/src/widgets/quill/quill_controller.dart | 37 ------------------ .../widgets/raw_editor/raw_editor_state.dart | 2 +- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/lib/src/models/documents/document.dart b/lib/src/models/documents/document.dart index aa0495a2..2945e755 100644 --- a/lib/src/models/documents/document.dart +++ b/lib/src/models/documents/document.dart @@ -1,5 +1,10 @@ import 'dart:async' show StreamController; +import 'package:html2md/html2md.dart' as html2md; +import 'package:markdown/markdown.dart' as md; + +import '../../../markdown_quill.dart'; + import '../../../quill_delta.dart'; import '../../widgets/quill/embeds.dart'; import '../rules/rule.dart'; @@ -442,6 +447,40 @@ class Document { delta.first.data == '\n' && 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) + 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, + // ); + } } /// Source of a [Change]. diff --git a/lib/src/widgets/quill/quill_controller.dart b/lib/src/widgets/quill/quill_controller.dart index 41300cb2..31908d7d 100644 --- a/lib/src/widgets/quill/quill_controller.dart +++ b/lib/src/widgets/quill/quill_controller.dart @@ -2,11 +2,8 @@ import 'dart:math' as math; import 'package:flutter/services.dart' show ClipboardData, Clipboard; import 'package:flutter/widgets.dart'; -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 '../../models/documents/attribute.dart'; import '../../models/documents/document.dart'; @@ -508,38 +505,4 @@ class QuillController extends ChangeNotifier { // Notify toolbar buttons directly with attributes Map toolbarButtonToggler = const {}; - - /// 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) - 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/widgets/raw_editor/raw_editor_state.dart b/lib/src/widgets/raw_editor/raw_editor_state.dart index b8adf76e..eb594a5f 100644 --- a/lib/src/widgets/raw_editor/raw_editor_state.dart +++ b/lib/src/widgets/raw_editor/raw_editor_state.dart @@ -213,7 +213,7 @@ class QuillRawEditorState extends EditorState if (html == null) { return; } - final deltaFromCliboard = QuillController.fromHtml(html); + final deltaFromCliboard = Document.fromHtml(html); final delta = deltaFromCliboard.compose(controller.document.toDelta()); controller