Update flutter_quill_extensions

pull/1511/head
Ellet 1 year ago
parent cf93061446
commit d1950f0053
No known key found for this signature in database
GPG Key ID: C488CC70BBCEF0D1
  1. 4
      flutter_quill_extensions/CHANGELOG.md
  2. 7
      flutter_quill_extensions/lib/logic/extensions/controller.dart
  3. 49
      flutter_quill_extensions/lib/logic/utils/quill_image_utils.dart

@ -1,3 +1,7 @@
## 0.6.4
- Update `QuillImageUtilities`
- Add new extension on `QuillController` to access `QuillImageUtilities` instance easier
## 0.6.3 ## 0.6.3
- Update `README.md` - Update `README.md`

@ -1,6 +1,7 @@
import 'package:flutter_quill/flutter_quill.dart'; import 'package:flutter_quill/flutter_quill.dart';
import '../../presentation/embeds/editor/webview.dart'; import '../../presentation/embeds/editor/webview.dart';
import '../utils/quill_image_utils.dart';
/// Extension functions on [QuillController] /// Extension functions on [QuillController]
/// that make it easier to insert the embed blocks /// that make it easier to insert the embed blocks
@ -64,4 +65,10 @@ extension QuillControllerExt on QuillController {
..skipRequestKeyboard = true ..skipRequestKeyboard = true
..replaceText(index, length, BlockEmbed.video(videoUrl), null); ..replaceText(index, length, BlockEmbed.video(videoUrl), null);
} }
QuillImageUtilities get imageUtilities {
return QuillImageUtilities(
controller: this,
);
}
} }

@ -6,9 +6,19 @@ import 'package:path/path.dart' as path;
import '../../presentation/embeds/utils.dart'; import '../../presentation/embeds/utils.dart';
typedef OnGenerateNewFileNameCallback = String Function(
String currentFileName,
String fileExt,
);
class QuillImageUtilities { class QuillImageUtilities {
const QuillImageUtilities._(); const QuillImageUtilities({
required this.controller,
});
final quill.QuillController controller;
/// Private function that is throw an error if the platform is web
static void _webIsNotSupported(String functionName) { static void _webIsNotSupported(String functionName) {
if (kIsWeb) { if (kIsWeb) {
throw UnsupportedError( throw UnsupportedError(
@ -60,7 +70,7 @@ class QuillImageUtilities {
required Iterable<String> images, required Iterable<String> images,
required deleteThePreviousImages, required deleteThePreviousImages,
required Directory saveDirectory, required Directory saveDirectory,
String startOfEachFile = 'quill-image-', OnGenerateNewFileNameCallback? onGenerateNewFileName,
}) async { }) async {
_webIsNotSupported('saveImagesToDirectory'); _webIsNotSupported('saveImagesToDirectory');
final newImagesFutures = images.map((cachedImagePath) async { final newImagesFutures = images.map((cachedImagePath) async {
@ -71,11 +81,14 @@ class QuillImageUtilities {
return ''; return '';
} }
final newImageFileExtensionWithDot = path.extension(cachedImagePath); final newImageFileExtension = path.extension(cachedImagePath); // with dot
final dateTimeAsString = DateTime.now().toIso8601String(); final dateTimeString = DateTime.now().toIso8601String();
final newImageFileName = final newImageFileName = onGenerateNewFileName?.call(
'$startOfEachFile$dateTimeAsString$newImageFileExtensionWithDot'; cachedImagePath,
newImageFileExtension,
) ??
'quill-image-$dateTimeString$newImageFileExtension';
final newImagePath = path.join(saveDirectory.path, newImageFileName); final newImagePath = path.join(saveDirectory.path, newImageFileName);
final newImageFile = await previousImageFile.copy(newImagePath); final newImageFile = await previousImageFile.copy(newImagePath);
if (deleteThePreviousImages) { if (deleteThePreviousImages) {
@ -91,6 +104,13 @@ class QuillImageUtilities {
/// Deletes all local images referenced in a Quill document. /// Deletes all local images referenced in a Quill document.
/// it's not supported on web for now /// it's not supported on web for now
/// ///
/// Be **careful**, on desktop you should never delete user images. only if you
/// are sure the image is saved in applicaton documents directory
///
/// on mobile the app is sandboxed so you can't delete user images
/// because it will be a copy of the image for the app
/// so you should be safe
///
/// This function removes local images from the /// This function removes local images from the
/// file system that are referenced in the provided [document]. /// file system that are referenced in the provided [document].
/// ///
@ -106,12 +126,9 @@ class QuillImageUtilities {
/// print('Error deleting local images: $e'); /// print('Error deleting local images: $e');
/// } /// }
/// ``` /// ```
static Future<void> deleteAllLocalImagesOfDocument( Future<void> deleteAllLocalImagesOfDocument() async {
quill.Document document,
) async {
_webIsNotSupported('deleteAllLocalImagesOfDocument'); _webIsNotSupported('deleteAllLocalImagesOfDocument');
final imagesPaths = getImagesPathsFromDocument( final imagesPaths = getImagesPathsFromDocument(
document,
onlyLocalImages: true, onlyLocalImages: true,
); );
for (final image in imagesPaths) { for (final image in imagesPaths) {
@ -133,7 +150,7 @@ class QuillImageUtilities {
/// Retrieves paths to images embedded in a Quill document. /// Retrieves paths to images embedded in a Quill document.
/// ///
/// it's not supported on web for now. /// it's not supported on web for now.
/// This function parses the [document] and returns a list of image paths. /// This function parses the Document and returns a list of image paths.
/// ///
/// [document]: The Quill document from which image paths will be retrieved. /// [document]: The Quill document from which image paths will be retrieved.
/// [onlyLocalImages]: If `true`, /// [onlyLocalImages]: If `true`,
@ -151,12 +168,11 @@ class QuillImageUtilities {
/// ///
/// Note: This function assumes that images are /// Note: This function assumes that images are
/// embedded as block embeds in the Quill document. /// embedded as block embeds in the Quill document.
static Iterable<String> getImagesPathsFromDocument( Iterable<String> getImagesPathsFromDocument({
quill.Document document, {
required bool onlyLocalImages, required bool onlyLocalImages,
}) { }) {
_webIsNotSupported('getImagesPathsFromDocument'); _webIsNotSupported('getImagesPathsFromDocument');
final images = document.root.children final images = controller.document.root.children
.whereType<quill.Line>() .whereType<quill.Line>()
.where((node) { .where((node) {
if (node.isEmpty) { if (node.isEmpty) {
@ -226,7 +242,6 @@ class QuillImageUtilities {
/// It is specifically designed for mobile /// It is specifically designed for mobile
/// operating systems (Android and iOS). /// operating systems (Android and iOS).
/// ///
/// [document] is the Quill document from which to extract image paths.
/// ///
/// [replaceUnexistentImagesWith] is an optional parameter. /// [replaceUnexistentImagesWith] is an optional parameter.
/// If provided, it replaces non-existent image paths /// If provided, it replaces non-existent image paths
@ -235,13 +250,11 @@ class QuillImageUtilities {
/// ///
/// Returns a list of cached image paths found in the document. /// Returns a list of cached image paths found in the document.
/// On non-mobile platforms, this function returns an empty list. /// On non-mobile platforms, this function returns an empty list.
static Future<Iterable<String>> getCachedImagePathsFromDocument( Future<Iterable<String>> getCachedImagePathsFromDocument({
quill.Document document, {
String? replaceUnexistentImagesWith, String? replaceUnexistentImagesWith,
}) async { }) async {
_webIsNotSupported('getCachedImagePathsFromDocument'); _webIsNotSupported('getCachedImagePathsFromDocument');
final imagePaths = getImagesPathsFromDocument( final imagePaths = getImagesPathsFromDocument(
document,
onlyLocalImages: true, onlyLocalImages: true,
); );

Loading…
Cancel
Save