getImagesPathsFromDocument method

Iterable<String> getImagesPathsFromDocument({
  1. required bool onlyLocalImages,
})

Retrieves paths to images embedded in a Quill document.

it's not supported on web for now. This function parses the Document and returns a list of image paths.

document: The Quill document from which image paths will be retrieved. onlyLocalImages: If true, only local (non-web-url) image paths will be included.

Returns an iterable of image paths.

Example usage:

final quillDocument = _controller.document;
final imagePaths
 = getImagesPathsFromDocument(quillDocument, onlyLocalImages: true);
print('Image paths: $imagePaths');

Note: This function assumes that images are embedded as block embeds in the Quill document.

Implementation

Iterable<String> getImagesPathsFromDocument({
  required bool onlyLocalImages,
}) {
  _webIsNotSupported('getImagesPathsFromDocument');
  // final images = document.root.children
  //     .whereType<quill.Line>()
  //     .where((node) {
  //       if (node.isEmpty) {
  //         return false;
  //       }
  //       final firstNode = node.children.first;
  //       if (firstNode is! quill.Embed) {
  //         return false;
  //       }

  //       if (firstNode.value.type != quill.BlockEmbed.imageType) {
  //         return false;
  //       }
  //       final imageSource = firstNode.value.data;
  //       if (imageSource is! String) {
  //         return false;
  //       }
  //       if (onlyLocalImages && isHttpBasedUrl(imageSource)) {
  //         return false;
  //       }
  //       return imageSource.trim().isNotEmpty;
  //     })
  //     .toList()
  //     .map((e) => (e.children.first as quill.Embed).value.data as String);

  final images = <String>[];
  for (final item in document.toDelta().toJson()) {
    if (!item.containsKey(Operation.insertKey)) {
      return [];
    }
    final insertValue = item[Operation.insertKey];

    // Check if the insert value is a map with the "image" key
    if (insertValue is Map &&
        insertValue.containsKey(quill.BlockEmbed.imageType)) {
      final String imageUrl = insertValue[quill.BlockEmbed.imageType];
      images.add(imageUrl);
    }
  }
  return images;
}