From 4522e67c816b0743db8cc77a54930904b615a3eb Mon Sep 17 00:00:00 2001
From: Xin Yao <singerdmx@gmail.com>
Date: Wed, 4 Aug 2021 03:41:20 -0700
Subject: [PATCH] Read custom image styles 'mobileWidth', 'mobileHeight',
 'mobileMargin'

---
 example/assets/sample_data.json |  2 +-
 lib/src/widgets/editor.dart     | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/example/assets/sample_data.json b/example/assets/sample_data.json
index ee1857d0..6fba6e1d 100644
--- a/example/assets/sample_data.json
+++ b/example/assets/sample_data.json
@@ -14,7 +14,7 @@
       },
       "attributes":{
          "width":"230",
-         "style":"display: block; margin: auto;"
+         "style":"display: block; margin: auto; mobileWidth: 5; mobileHeight: 5; mobileMargin: 1;"
       }
    },
    {
diff --git a/lib/src/widgets/editor.dart b/lib/src/widgets/editor.dart
index 5cf89f1d..fa39aa14 100644
--- a/lib/src/widgets/editor.dart
+++ b/lib/src/widgets/editor.dart
@@ -97,12 +97,34 @@ String _standardizeImageUrl(String url) {
   return url;
 }
 
+bool _isMobile() => io.Platform.isAndroid || io.Platform.isIOS;
+
 Widget _defaultEmbedBuilder(
     BuildContext context, leaf.Embed node, bool readOnly) {
   assert(!kIsWeb, 'Please provide EmbedBuilder for Web');
   switch (node.value.type) {
     case 'image':
       final imageUrl = _standardizeImageUrl(node.value.data);
+
+      final style = node.style.attributes['style'];
+      if (_isMobile() && style != null) {
+        final imageStyles = style.value.toString().split(';');
+        final _keys = {'mobileWidth', 'mobileHeight', 'mobileMargin'};
+        final _attrs = <String, String>{};
+        for (final imageStyle in imageStyles) {
+          final _index = imageStyle.indexOf(':');
+          if (_index < 0) {
+            continue;
+          }
+          final _key = imageStyle.substring(0, _index).trim();
+          if (_keys.contains(_key)) {
+            _attrs[_key] = imageStyle.substring(_index + 1).trim();
+          }
+        }
+        if (_attrs.isNotEmpty) {
+          // TODO: return image with custom width, height and margin
+        }
+      }
       return imageUrl.startsWith('http')
           ? Image.network(imageUrl)
           : isBase64(imageUrl)