From beb310b0f0770dae8228702912338d21a6270dd2 Mon Sep 17 00:00:00 2001 From: Amir Panahandeh Date: Wed, 26 Jul 2023 17:52:54 +0330 Subject: [PATCH] Setup main.dart --- packages/fleather/example/lib/main.dart | 147 +------------------ packages/fleather/example/macos/Podfile.lock | 4 +- 2 files changed, 6 insertions(+), 145 deletions(-) diff --git a/packages/fleather/example/lib/main.dart b/packages/fleather/example/lib/main.dart index a3e815db..7e9dd8bf 100644 --- a/packages/fleather/example/lib/main.dart +++ b/packages/fleather/example/lib/main.dart @@ -1,10 +1,5 @@ -import 'dart:convert'; - -import 'package:fleather/fleather.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:quill_delta/quill_delta.dart'; -import 'package:url_launcher/url_launcher.dart'; void main() { runApp(const FleatherApp()); @@ -31,153 +26,19 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { - final FocusNode _focusNode = FocusNode(); - FleatherController? _controller; - @override void initState() { super.initState(); - _initController(); - } - - Future _initController() async { - try { - final result = await rootBundle.loadString('assets/welcome.json'); - final heuristics = ParchmentHeuristics( - formatRules: [], - insertRules: [ - ForceNewlineForInsertsAroundInlineImageRule(), - ], - deleteRules: [], - ).merge(ParchmentHeuristics.fallback); - final doc = ParchmentDocument.fromJson( - jsonDecode(result), - heuristics: heuristics, - ); - _controller = FleatherController(doc); - } catch (err, st) { - print('Cannot read welcome.json: $err\n$st'); - _controller = FleatherController(); - } - setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(elevation: 0, title: Text('Fleather Demo')), - body: _controller == null - ? Center(child: const CircularProgressIndicator()) - : Column( - children: [ - FleatherToolbar.basic(controller: _controller!), - Divider(height: 1, thickness: 1, color: Colors.grey.shade200), - Expanded( - child: FleatherEditor( - controller: _controller!, - focusNode: _focusNode, - padding: EdgeInsets.only( - left: 16, - right: 16, - bottom: MediaQuery.of(context).padding.bottom, - ), - onLaunchUrl: _launchUrl, - maxContentWidth: 800, - embedBuilder: _embedBuilder, - ), - ), - ], - ), + body: Column( + children: [ + ], + ), ); } - - Widget _embedBuilder(BuildContext context, EmbedNode node) { - if (node.value.type == 'hr') { - final theme = FleatherTheme.of(context)!; - return Divider( - height: theme.paragraph.style.fontSize! * theme.paragraph.style.height!, - thickness: 2, - color: Colors.grey.shade200, - ); - } - - if (node.value.type == 'icon') { - final data = node.value.data; - // Icons.rocket_launch_outlined - return Icon( - IconData(int.parse(data['codePoint']), fontFamily: data['fontFamily']), - color: Color(int.parse(data['color'])), - size: 18, - ); - } - - if (node.value.type == 'image' && - node.value.data['source_type'] == 'assets') { - return Padding( - // Caret takes 2 pixels, hence not symmetric padding values. - padding: const EdgeInsets.only(left: 4, right: 2, top: 2, bottom: 2), - child: Container( - width: 300, - height: 300, - decoration: BoxDecoration( - image: DecorationImage( - image: AssetImage(node.value.data['source']), - fit: BoxFit.cover, - ), - ), - ), - ); - } - - throw UnimplementedError(); - } - - void _launchUrl(String? url) async { - if (url == null) return; - final uri = Uri.parse(url); - final _canLaunch = await canLaunchUrl(uri); - if (_canLaunch) { - await launchUrl(uri); - } - } -} - -/// This is an example insert rule that will insert a new line before and -/// after inline image embed. -class ForceNewlineForInsertsAroundInlineImageRule extends InsertRule { - @override - Delta? apply(Delta document, int index, Object data) { - if (data is! String) return null; - - final iter = DeltaIterator(document); - final previous = iter.skip(index); - final target = iter.next(); - final cursorBeforeInlineEmbed = _isInlineImage(target.data); - final cursorAfterInlineEmbed = - previous != null && _isInlineImage(previous.data); - - if (cursorBeforeInlineEmbed || cursorAfterInlineEmbed) { - final delta = Delta()..retain(index); - if (cursorAfterInlineEmbed && !data.startsWith('\n')) { - delta.insert('\n'); - } - delta.insert(data); - if (cursorBeforeInlineEmbed && !data.endsWith('\n')) { - delta.insert('\n'); - } - return delta; - } - return null; - } - - bool _isInlineImage(Object data) { - if (data is EmbeddableObject) { - return data.type == 'image' && data.inline; - } - if (data is Map) { - return data[EmbeddableObject.kTypeKey] == 'image' && - data[EmbeddableObject.kInlineKey]; - } - return false; - } } diff --git a/packages/fleather/example/macos/Podfile.lock b/packages/fleather/example/macos/Podfile.lock index f0c4f661..50cbb889 100644 --- a/packages/fleather/example/macos/Podfile.lock +++ b/packages/fleather/example/macos/Podfile.lock @@ -15,8 +15,8 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24 - url_launcher_macos: c04e4fa86382d4f94f6b38f14625708be3ae52e2 + url_launcher_macos: 5335912b679c073563f29d89d33d10d459f95451 PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7 -COCOAPODS: 1.12.1 +COCOAPODS: 1.11.3