A rich text editor with flutter_quill, saved, with undo
The flutter_quill editor and toolbar you know, on a rich text that merges with other peers. Undo takes back your own edits and leaves the others' alone.
A rich text editor with flutter_quill, saved, with undoSource ↗
A flutter_quill editor and toolbar bound to a collaborative rich text. Hive keeps the page on the device, and undo takes back only your own edits, formats included.
- main.dart
- editor_page.dart
- Install
import 'package:crdt_lf_hive/crdt_lf_hive.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import 'editor_page.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// On the web Hive uses IndexedDB and needs no path.
if (!kIsWeb) {
Hive.init((await getApplicationDocumentsDirectory()).path);
}
CRDTHive.initialize();
final backend = await CRDTHive.open();
runApp(EditorApp(backend: backend));
}
/// The app: one rich text page stored in [backend].
class EditorApp extends StatelessWidget {
/// Creates the app of the page stored in [backend].
const EditorApp({required this.backend, super.key});
/// Where the page is stored.
final CRDTStorageBackend backend;
Widget build(BuildContext context) {
return MaterialApp(
title: 'Editor',
// flutter_quill reads its texts from here, and throws without them.
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
FlutterQuillLocalizations.delegate,
],
home: EditorPage(backend: backend),
);
}
}
import 'dart:async';
// flutter_quill has a ChangeSource of its own.
import 'package:crdt_lf/crdt_lf.dart' hide ChangeSource;
import 'package:crdt_lf_flutter/crdt_lf_flutter.dart';
import 'package:crdt_lf_hive/crdt_lf_hive.dart';
import 'package:crdt_lf_quill/crdt_lf_quill.dart';
import 'package:flutter/material.dart';
import 'package:flutter_quill/flutter_quill.dart';
/// A page that edits the rich text stored in [backend], with undo.
class EditorPage extends StatefulWidget {
/// Creates the page of the rich text stored in [backend].
const EditorPage({required this.backend, super.key});
/// Where the page is stored.
final CRDTStorageBackend backend;
State<EditorPage> createState() => _EditorPageState();
}
class _EditorPageState extends State<EditorPage> {
// The State owns these: the binder never disposes them, and a new
// FocusNode on each build would drop the keyboard.
final _controller = QuillController.basic();
final _focusNode = FocusNode();
final _scrollController = ScrollController();
PersistentDocument? _page;
CRDTUndoManager? _undo;
void initState() {
super.initState();
unawaited(_open());
}
Future<void> _open() async {
final page = await widget.backend.openDocument('page');
final text = CRDTFugueRichTextHandler(page.document, 'page');
// Records only the edits made in this editor.
final undo = CRDTUndoManager(
page.document,
trackedOrigins: {crdtQuillOrigin},
)..track(text);
if (!mounted) {
undo.dispose();
return _close(page);
}
setState(() {
_page = page;
_undo = undo;
});
}
void dispose() {
_controller.dispose();
_focusNode.dispose();
_scrollController.dispose();
_undo?.dispose();
final page = _page;
if (page != null) {
unawaited(_close(page));
}
super.dispose();
}
static Future<void> _close(PersistentDocument page) async {
await page.persistence.dispose(); // writes what is still waiting
page.document.dispose();
await page.persistence.storage.close();
}
Widget build(BuildContext context) {
final page = _page;
if (page == null) {
return const Scaffold(body: Center(child: CircularProgressIndicator()));
}
return Scaffold(
appBar: AppBar(title: const Text('Editor')),
body: CrdtProvider.value(
value: page.document,
child: CrdtQuillBinder(
id: 'page',
controller: _controller,
// The editor, its shortcuts and the toolbar buttons undo through it.
undoManager: _undo,
builder:
(context) => Column(
children: [
// The binder gives the controller a new document: key the
// toolbar on it, so its undo and redo buttons follow.
ListenableBuilder(
listenable: _controller,
builder:
(context, _) => QuillSimpleToolbar(
key: ObjectKey(_controller.document),
controller: _controller,
config: const QuillSimpleToolbarConfig(
multiRowsDisplay: false,
),
),
),
const Divider(height: 1),
Expanded(
child: QuillEditor.basic(
controller: _controller,
focusNode: _focusNode,
scrollController: _scrollController,
config: QuillEditorConfig(
padding: const EdgeInsets.all(16),
// Shows an embed of another peer that has no builder.
unknownEmbedBuilder: crdtQuillUnknownEmbedBuilder(),
),
),
),
],
),
),
),
);
}
}
flutter pub add crdt_lf crdt_lf_flutter crdt_lf_hive crdt_lf_quill \
flutter_quill hive path_provider "flutter_localizations@{sdk: flutter}"
Key points
- Set up the editor package as its own documentation explains (here, the localizations of flutter_quill).
- Hide the
ChangeSourceofcrdt_lfwhen an editor package has a type with the same name. - The page owns the editor controller and disposes it: the binder never does.
- Pass a
CRDTUndoManagerthat trackscrdtQuillOriginasundoManager: undo then takes back only the edits of this editor.