From fe34c0425e03c9e4488b6f39ff851c58a0da2e17 Mon Sep 17 00:00:00 2001 From: vaishnavi-2307 Date: Tue, 8 Oct 2024 16:14:21 +0530 Subject: [PATCH] Fix image size in PDF post export --- .../repositories/export_notes_repository.dart | 96 ++++++++++++------- pubspec.lock | 8 -- pubspec.yaml | 1 - 3 files changed, 64 insertions(+), 41 deletions(-) diff --git a/lib/features/notes/data/repositories/export_notes_repository.dart b/lib/features/notes/data/repositories/export_notes_repository.dart index 55d208ef..fbb6960b 100644 --- a/lib/features/notes/data/repositories/export_notes_repository.dart +++ b/lib/features/notes/data/repositories/export_notes_repository.dart @@ -4,11 +4,9 @@ import 'dart:io'; import 'package:dairy_app/core/logger/logger.dart'; import 'package:dairy_app/features/notes/domain/repositories/export_notes_repository.dart'; import 'package:dairy_app/features/notes/domain/repositories/notes_repository.dart'; -import 'package:delta_markdown/delta_markdown.dart'; import 'package:flutter/services.dart'; import 'package:flutter_html_to_pdf/flutter_html_to_pdf.dart'; import 'package:intl/intl.dart'; -import 'package:markdown/markdown.dart'; import 'package:path_provider/path_provider.dart'; final log = printer("ExportNotesRepository"); @@ -103,9 +101,37 @@ class ExportNotesRepository implements IExportNotesRepository { // utils String quillDeltaToHtml(String delta) { - final markdown = deltaToMarkdown(delta); - final html = markdownToHtml(markdown); - return html; + var deltaMap = jsonDecode(delta); + var html = StringBuffer(); + + for (var op in deltaMap) { + if (op["insert"] is String) { + var text = op["insert"] as String; + var attributes = op["attributes"] as Map?; + + if (attributes != null) { + if (attributes.containsKey("bold")) { + text = "$text"; + } + if (attributes.containsKey("italic")) { + text = "$text"; + } + // Add more attribute handling as needed + } + + html.write(text); + } else if (op["insert"] is Map) { + var insert = op["insert"] as Map; + if (insert.containsKey("image")) { + var image = insert["image"] as Map; + html.write( + ''); + } + // Handle other insert types as needed + } + } + + return html.toString(); } String formatDate(DateTime date) { @@ -129,38 +155,44 @@ class ExportNotesRepository implements IExportNotesRepository { var deltaMap = jsonDecode(delta); for (Map deltaElement in deltaMap) { - // Local images needs to be prefixed with file:/// - + // Handle image elements if (deltaElement.containsKey("insert") && - deltaElement["insert"].runtimeType != String && - deltaElement["insert"].containsKey("image") && - !(deltaElement["insert"]["image"] as String).startsWith("http")) { - deltaElement["insert"]["image"] = - "file://" + deltaElement["insert"]["image"]; + deltaElement["insert"] is Map && + deltaElement["insert"].containsKey("image")) { + var imageUrl = deltaElement["insert"]["image"] as String; + + // Prefix local images with file:/// + if (!imageUrl.startsWith("http")) { + imageUrl = "file://" + imageUrl; + } + + // Add width and height attributes to limit image size + deltaElement["insert"]["image"] = { + "src": imageUrl, + "width": "500", // You can adjust this value as needed + "height": "auto" // This maintains the aspect ratio + }; } // Remove unsupported attributes on text - if (deltaElement.containsKey("insert") && - deltaElement["insert"].runtimeType == String) { - if (deltaElement.containsKey("attributes")) { - // 1. Remove underline - deltaElement["attributes"].remove("underline"); - - // 2. Remove Strikethrough - deltaElement["attributes"].remove("strike"); - - // 3. Remove color - deltaElement["attributes"].remove("color"); - - // 4. Remove background color - deltaElement["attributes"].remove("background"); - - // 5. Remove checklist - deltaElement["attributes"].remove("list"); - - // 6. Remove subscript and superscript - deltaElement["attributes"].remove("script"); + deltaElement["insert"] is String && + deltaElement.containsKey("attributes")) { + var attributes = deltaElement["attributes"] as Map; + + // Remove specific unsupported attributes + attributes.removeWhere((key, value) => [ + "underline", + "strike", + "color", + "background", + "list", + "script" + ].contains(key)); + + // If all attributes are removed, remove the attributes key entirely + if (attributes.isEmpty) { + deltaElement.remove("attributes"); } } } diff --git a/pubspec.lock b/pubspec.lock index 529585d6..48c490e8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1144,14 +1144,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.0" - markdown: - dependency: "direct main" - description: - name: markdown - sha256: acf35edccc0463a9d7384e437c015a3535772e09714cf60e07eeef3a15870dcd - url: "https://pub.dev" - source: hosted - version: "7.1.1" matcher: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 7ee7d1be..ec74296b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -66,7 +66,6 @@ dependencies: intl: ^0.18.0 local_auth: ^2.1.6 logger: ^1.1.0 - markdown: ^7.1.1 package_info_plus: ^4.1.0 path_provider: ^2.0.9 permission_handler: ^11.0.1