### Interact with a Bloc Instance and Manage its Lifecycle in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md This example illustrates the basic usage of a `CounterBloc` instance. It shows how to instantiate the bloc, access its current state, add events to trigger state changes, and asynchronously wait for event processing. Finally, it demonstrates the importance of calling `close()` to release resources when the bloc is no longer needed. ```Dart Future main() async { /// Create a `CounterBloc` instance. final bloc = CounterBloc(); /// Access the state of the `bloc` via `state`. print(bloc.state); // 0 /// Interact with the `bloc` to trigger `state` changes. bloc.add(Increment()); /// Wait for next iteration of the event-loop /// to ensure event has been processed. await Future.delayed(Duration.zero); /// Access the new `state`. print(bloc.state); // 1 /// Close the `bloc` when it is no longer needed. await bloc.close(); } ``` -------------------------------- ### Convert Markdown to Quill Delta with Customizations Source: https://github.com/tarekkma/markdown_quill/blob/master/README.md This example illustrates how to customize the conversion process in `markdown_quill`. It shows configuring the `markdown` parser with GitHub Flavored Markdown extensions and custom block syntaxes (like `EmbeddableTableSyntax`). It also demonstrates adding custom element-to-block attribute mappings and custom embed handlers for specific content types like tables. ```Dart import 'package:flutter_quill/flutter_quill.dart'; import 'package:markdown/markdown.dart' as md; import 'package:markdown_quill/markdown_quill.dart'; // Configure the markdown parser final mdDocument = md.Document( encodeHtml: false, extensionSet: md.ExtensionSet.gitHubFlavored, // you can add custom syntax. blockSyntaxes: [const EmbeddableTableSyntax()], ); final mdToDelta = MarkdownToDelta( markdownDocument: mdDocument, // you can add custom attributes based on tags customElementToBlockAttribute: { 'h4': (element) => [HeaderAttribute(level: 4)], }, // custom embed customElementToEmbeddable: { EmbeddableTable.tableType: EmbeddableTable.fromMdSyntax, }, ); final deltaToMd = DeltaToMarkdown( customEmbedHandlers: { EmbeddableTable.tableType: EmbeddableTable.toMdSyntax, }, ); const markdown = ''' Hi, this is a test of markdown_quill. | Syntax | Description | Test Text | | :--- | :----: | ---: | | Header | Title | Here's this | | Paragraph | Text | And more | # H1 ok # H2 # H3 done # H4 '''; final delta = mdToDelta.convert(markdown); final markdownAgain = deltaToMd.convert(delta); ``` -------------------------------- ### Create a CounterCubit in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md Demonstrates how to define a `Cubit` class, extend `Cubit`, set an initial state in the constructor, and update the state using the `emit` method within a function like `increment()`. ```Dart /// A `CounterCubit` which manages an `int` as its state. class CounterCubit extends Cubit { /// The initial state of the `CounterCubit` is 0. CounterCubit() : super(0); /// When increment is called, the current state /// of the cubit is accessed via `state` and /// a new `state` is emitted via `emit`. void increment() => emit(state + 1); } ``` -------------------------------- ### Use a Cubit instance in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md Shows how to instantiate a `Cubit`, access its current state using the `state` getter, trigger state changes by calling its methods, and properly close the `Cubit` when it's no longer needed to free up resources. ```Dart void main() { /// Create a `CounterCubit` instance. final cubit = CounterCubit(); /// Access the state of the `cubit` via `state`. print(cubit.state); // 0 /// Interact with the `cubit` to trigger `state` changes. cubit.increment(); /// Access the new `state`. print(cubit.state); // 1 /// Close the `cubit` when it is no longer needed. cubit.close(); } ``` -------------------------------- ### Register a Global BlocObserver using BlocOverrides.runZoned in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md This snippet shows how to apply the `MyBlocObserver` globally to an application using `BlocOverrides.runZoned`. By wrapping the application's bloc usage within this zone, all bloc events, changes, and errors will be routed through the registered observer, enabling comprehensive application-wide monitoring and debugging. ```Dart void main() { BlocOverrides.runZoned( () { // Use blocs... }, blocObserver: MyBlocObserver(), ); } ``` -------------------------------- ### Implement a Global BlocObserver for Centralized Logging in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md This code defines `MyBlocObserver`, a custom class extending `BlocObserver`. It overrides various methods like `onCreate`, `onEvent`, `onChange`, `onTransition`, `onError`, and `onClose` to provide a centralized mechanism for observing the lifecycle and behavior of all `BlocBase` and `Bloc` instances across the entire application. ```Dart class MyBlocObserver extends BlocObserver { @override void onCreate(BlocBase bloc) { super.onCreate(bloc); print('onCreate -- ${bloc.runtimeType}'); } @override void onEvent(Bloc bloc, Object? event) { super.onEvent(bloc, event); print('onEvent -- ${bloc.runtimeType}, $event'); } @override void onChange(BlocBase bloc, Change change) { super.onChange(bloc, change); print('onChange -- ${bloc.runtimeType}, $change'); } @override void onTransition(Bloc bloc, Transition transition) { super.onTransition(bloc, transition); print('onTransition -- ${bloc.runtimeType}, $transition'); } @override void onError(BlocBase bloc, Object error, StackTrace stackTrace) { print('onError -- ${bloc.runtimeType}, $error'); super.onError(error, stackTrace); } @override void onClose(BlocBase bloc) { super.onClose(bloc); print('onClose -- ${bloc.runtimeType}'); } } ``` -------------------------------- ### Implement a custom BlocObserver in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md Explains how to create a custom `BlocObserver` by extending the `BlocObserver` class. This allows global observation of lifecycle events (creation, changes, errors, closing) across all `BlocBase` instances (including `Cubits` and `Blocs`) in an application. ```Dart class MyBlocObserver extends BlocObserver { @override void onCreate(BlocBase bloc) { super.onCreate(bloc); print('onCreate -- ${bloc.runtimeType}'); } @override void onChange(BlocBase bloc, Change change) { super.onChange(bloc, change); print('onChange -- ${bloc.runtimeType}, $change'); } @override void onError(BlocBase bloc, Object error, StackTrace stackTrace) { print('onError -- ${bloc.runtimeType}, $error'); super.onError(bloc, error, stackTrace); } @override void onClose(BlocBase bloc) { super.onClose(bloc); print('onClose -- ${bloc.runtimeType}'); } } ``` -------------------------------- ### Convert Markdown to Quill Delta and Back (Simple) Source: https://github.com/tarekkma/markdown_quill/blob/master/README.md This snippet demonstrates the basic usage of `markdown_quill` to convert a Markdown string to Quill Delta format and then back to Markdown. It shows how to initialize the `MarkdownToDelta` and `DeltaToMarkdown` converters with a basic `markdown` parser configuration. ```Dart import 'package:markdown/markdown.dart' as md; import 'package:markdown_quill/markdown_quill.dart'; // Configure the markdown parser final mdDocument = md.Document(encodeHtml: false); final mdToDelta = MarkdownToDelta(markdownDocument: mdDocument); final deltaToMd = DeltaToMarkdown(); const markdown = ''' # Test Hello > Testing This is an `inline code` and this is `` code block `` '''; final delta = mdToDelta.convert(markdown); final markdownAgain = deltaToMd.convert(delta); ``` -------------------------------- ### Apply a custom BlocObserver globally in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md Demonstrates how to register a custom `BlocObserver` globally using `BlocOverrides.runZoned`. This ensures that all `BlocBase` instances created within the `runZoned` callback will use the specified observer for logging and debugging purposes. ```Dart void main() { BlocOverrides.runZoned( () { // Use cubits... }, blocObserver: MyBlocObserver(), ); } ``` -------------------------------- ### Define a Custom Bloc Class with Event Handlers in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md This snippet demonstrates how to create a `CounterBloc` by extending `Bloc`. It defines `CounterEvent` as an abstract class and `Increment` as a concrete event. The `CounterBloc` constructor initializes the bloc's state and registers an `on` handler to process `Increment` events, updating the state accordingly. ```Dart /// The events which `CounterBloc` will react to. abstract class CounterEvent {} /// Notifies bloc to increment state. class Increment extends CounterEvent {} /// A `CounterBloc` which handles converting `CounterEvent`s into `int`s. class CounterBloc extends Bloc { /// The initial state of the `CounterBloc` is 0. CounterBloc() : super(0) { /// When an `Increment` event is added, /// the current `state` of the bloc is accessed via the `state` property /// and a new state is emitted via `emit`. on((event, emit) => emit(state + 1)); } } ``` -------------------------------- ### Override Bloc Lifecycle Methods for Internal Debugging in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md This snippet extends the `CounterBloc` to demonstrate how to override key lifecycle methods: `onEvent`, `onChange`, `onTransition`, and `onError`. These overrides provide hooks to log or react to events, state changes, transitions, and errors directly within the bloc's implementation, aiding in debugging and understanding its behavior. ```Dart abstract class CounterEvent {} class Increment extends CounterEvent {} class CounterBloc extends Bloc { CounterBloc() : super(0) { on((event, emit) => emit(state + 1)); } @override void onEvent(CounterEvent event) { super.onEvent(event); print(event); } @override void onChange(Change change) { super.onChange(change); print(change); } @override void onTransition(Transition transition) { super.onTransition(transition); print(transition); } @override void onError(Object error, StackTrace stackTrace) { print('$error, $stackTrace'); super.onError(error, stackTrace); } } ``` -------------------------------- ### Observe Cubit state changes and errors in Dart Source: https://github.com/tarekkma/markdown_quill/blob/master/test/md_files/bloc.md Illustrates how to override `onChange` and `onError` methods within a `Cubit` to log or react to state changes and errors specific to that `Cubit` instance. `onChange` is called before a state change, and `onError` is called when an error occurs. ```Dart class CounterCubit extends Cubit { CounterCubit() : super(0); void increment() => emit(state + 1); @override void onChange(Change change) { super.onChange(change); print(change); } @override void onError(Object error, StackTrace stackTrace) { print('$error, $stackTrace'); super.onError(error, stackTrace); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.