Skip to content

Commit 50a985a

Browse files
committed
chore: Add recap to web embeds. #1
1 parent 5eca861 commit 50a985a

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

README.md

+85
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,91 @@ are conditionally imported
893893
and compilation always work,
894894
regardless of the target platform!
895895

896+
To recap, here's how `lib/web_embeds/web_embeds.dart` should look like.
897+
898+
```dart
899+
import 'package:app/main.dart';
900+
import 'package:flutter/cupertino.dart';
901+
import 'package:flutter_quill/flutter_quill.dart';
902+
import 'package:flutter_quill_extensions/flutter_quill_extensions.dart';
903+
import 'package:responsive_framework/responsive_framework.dart';
904+
import 'package:universal_html/html.dart' as html;
905+
906+
// Conditionally importing the PlatformViewRegistry class according to the platform
907+
import 'mobile_platform_registry.dart' if (dart.library.html) 'web_platform_registry.dart' as ui_instance;
908+
909+
/// Class used to conditionally register the view factory.
910+
/// For more information, check https://github.com/flutter/flutter/issues/41563#issuecomment-547923478.
911+
class PlatformViewRegistryFix {
912+
void registerViewFactory(PlatformService platformService, imageURL, dynamic cbFnc) {
913+
if (platformService.isWebPlatform()) {
914+
ui_instance.PlatformViewRegistry.registerViewFactory(
915+
imageURL,
916+
cbFnc,
917+
);
918+
}
919+
}
920+
}
921+
922+
/// Class that conditionally registers the `platformViewRegistry`.
923+
class ImageUniversalUI {
924+
PlatformViewRegistryFix platformViewRegistry = PlatformViewRegistryFix();
925+
}
926+
927+
/// Custom embed for images to work on the web.
928+
class ImageEmbedBuilderWeb extends EmbedBuilder {
929+
@override
930+
String get key => BlockEmbed.imageType;
931+
932+
@override
933+
Widget build(
934+
BuildContext context,
935+
QuillController controller,
936+
Embed node,
937+
bool readOnly,
938+
bool inline,
939+
TextStyle textStyle,
940+
) {
941+
final imageUrl = node.value.data;
942+
if (isImageBase64(imageUrl)) {
943+
// TODO: handle imageUrl of base64
944+
return const SizedBox();
945+
}
946+
final size = MediaQuery.of(context).size;
947+
948+
// This is needed for images to be correctly embedded on the web.
949+
ImageUniversalUI().platformViewRegistry.registerViewFactory(PlatformService(), imageUrl, (viewId) {
950+
return html.ImageElement()
951+
..src = imageUrl
952+
..style.height = 'auto'
953+
..style.width = 'auto';
954+
});
955+
956+
// Rendering responsive image
957+
return Padding(
958+
padding: EdgeInsets.only(
959+
right: ResponsiveBreakpoints.of(context).smallerThan(TABLET)
960+
? size.width * 0.5
961+
: (ResponsiveBreakpoints.of(context).equals('4K'))
962+
? size.width * 0.75
963+
: size.width * 0.2,
964+
),
965+
child: SizedBox(
966+
height: MediaQuery.of(context).size.height * 0.45,
967+
child: HtmlElementView(
968+
viewType: imageUrl,
969+
),
970+
),
971+
);
972+
}
973+
}
974+
975+
/// List of default web embed builders.
976+
List<EmbedBuilder> get defaultEmbedBuildersWeb => [
977+
ImageEmbedBuilderWeb(),
978+
];
979+
```
980+
896981

897982
### 4.4 Creating the toolbar
898983

0 commit comments

Comments
 (0)