diff --git a/example/lib/pages/home_page.dart b/example/lib/pages/home_page.dart index 0836a98e..15623a81 100644 --- a/example/lib/pages/home_page.dart +++ b/example/lib/pages/home_page.dart @@ -17,6 +17,12 @@ import 'package:tuple/tuple.dart'; import '../universal_ui/universal_ui.dart'; import 'read_only_page.dart'; +enum _SelectionType { + none, + word, + // line, +} + class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); @@ -25,6 +31,14 @@ class HomePage extends StatefulWidget { class _HomePageState extends State { QuillController? _controller; final FocusNode _focusNode = FocusNode(); + Timer? _selectAllTimer; + _SelectionType _selectionType = _SelectionType.none; + + @override + void dispose() { + _selectAllTimer?.cancel(); + super.dispose(); + } @override void initState() { @@ -99,39 +113,109 @@ class _HomePageState extends State { ); } + bool _onTripleClickSelection() { + final controller = _controller!; + + _selectAllTimer?.cancel(); + _selectAllTimer = null; + + // If you want to select all text after paragraph, uncomment this line + // if (_selectionType == _SelectionType.line) { + // final selection = TextSelection( + // baseOffset: 0, + // extentOffset: controller.document.length, + // ); + + // controller.updateSelection(selection, ChangeSource.REMOTE); + + // _selectionType = _SelectionType.none; + + // return true; + // } + + if (controller.selection.isCollapsed) { + _selectionType = _SelectionType.none; + } + + if (_selectionType == _SelectionType.none) { + _selectionType = _SelectionType.word; + _startTripleClickTimer(); + return false; + } + + if (_selectionType == _SelectionType.word) { + final child = controller.document.queryChild( + controller.selection.baseOffset, + ); + final offset = child.node?.documentOffset ?? 0; + final length = child.node?.length ?? 0; + + final selection = TextSelection( + baseOffset: offset, + extentOffset: offset + length, + ); + + controller.updateSelection(selection, ChangeSource.REMOTE); + + // _selectionType = _SelectionType.line; + + _selectionType = _SelectionType.none; + + _startTripleClickTimer(); + + return true; + } + + return false; + } + + void _startTripleClickTimer() { + _selectAllTimer = Timer(const Duration(milliseconds: 900), () { + _selectionType = _SelectionType.none; + }); + } + Widget _buildWelcomeEditor(BuildContext context) { - var quillEditor = QuillEditor( - controller: _controller!, - scrollController: ScrollController(), - scrollable: true, - focusNode: _focusNode, - autoFocus: false, - readOnly: false, - placeholder: 'Add content', - enableSelectionToolbar: isMobile(), - expands: false, - padding: EdgeInsets.zero, - onImagePaste: _onImagePaste, - customStyles: DefaultStyles( - h1: DefaultTextBlockStyle( - const TextStyle( - fontSize: 32, - color: Colors.black, - height: 1.15, - fontWeight: FontWeight.w300, - ), - const Tuple2(16, 0), - const Tuple2(0, 0), - null), - sizeSmall: const TextStyle(fontSize: 9), + Widget quillEditor = MouseRegion( + cursor: SystemMouseCursors.text, + child: QuillEditor( + controller: _controller!, + scrollController: ScrollController(), + scrollable: true, + focusNode: _focusNode, + autoFocus: false, + readOnly: false, + placeholder: 'Add content', + enableSelectionToolbar: isMobile(), + expands: false, + padding: EdgeInsets.zero, + onImagePaste: _onImagePaste, + onTapUp: (details, p1) { + return _onTripleClickSelection(); + }, + customStyles: DefaultStyles( + h1: DefaultTextBlockStyle( + const TextStyle( + fontSize: 32, + color: Colors.black, + height: 1.15, + fontWeight: FontWeight.w300, + ), + const Tuple2(16, 0), + const Tuple2(0, 0), + null), + sizeSmall: const TextStyle(fontSize: 9), + ), + embedBuilders: [ + ...FlutterQuillEmbeds.builders(), + NotesEmbedBuilder(addEditNote: _addEditNote) + ], ), - embedBuilders: [ - ...FlutterQuillEmbeds.builders(), - NotesEmbedBuilder(addEditNote: _addEditNote) - ], ); if (kIsWeb) { - quillEditor = QuillEditor( + quillEditor = MouseRegion( + cursor: SystemMouseCursors.text, + child: QuillEditor( controller: _controller!, scrollController: ScrollController(), scrollable: true, @@ -141,6 +225,9 @@ class _HomePageState extends State { placeholder: 'Add content', expands: false, padding: EdgeInsets.zero, + onTapUp: (details, p1) { + return _onTripleClickSelection(); + }, customStyles: DefaultStyles( h1: DefaultTextBlockStyle( const TextStyle( @@ -154,7 +241,9 @@ class _HomePageState extends State { null), sizeSmall: const TextStyle(fontSize: 9), ), - embedBuilders: defaultEmbedBuildersWeb); + embedBuilders: defaultEmbedBuildersWeb, + ), + ); } var toolbar = QuillToolbar.basic( controller: _controller!,