From 61be737fe29b69c509a238e6153dd75c730dcb9a Mon Sep 17 00:00:00 2001 From: Mathias Mogensen <42929161+Xazin@users.noreply.github.com> Date: Thu, 17 Aug 2023 09:45:28 +0200 Subject: [PATCH] fix: placeholder on paragraph when selected (#390) * fix: placeholder on paragraph when selected Closes: #380 * fix: review comment --- .../standard_block_components.dart | 4 ++- .../text_block_component.dart | 34 +++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/src/editor/block_component/standard_block_components.dart b/lib/src/editor/block_component/standard_block_components.dart index 59f3ca1fe..af0ddb7b6 100644 --- a/lib/src/editor/block_component/standard_block_components.dart +++ b/lib/src/editor/block_component/standard_block_components.dart @@ -6,7 +6,9 @@ const standardBlockComponentConfiguration = BlockComponentConfiguration(); final Map standardBlockComponentBuilderMap = { PageBlockKeys.type: PageBlockComponentBuilder(), ParagraphBlockKeys.type: TextBlockComponentBuilder( - configuration: standardBlockComponentConfiguration, + configuration: standardBlockComponentConfiguration.copyWith( + placeholderText: (_) => 'Enter a / to insert a block, or start typing', + ), ), TodoListBlockKeys.type: TodoListBlockComponentBuilder( configuration: standardBlockComponentConfiguration.copyWith( diff --git a/lib/src/editor/block_component/text_block_component/text_block_component.dart b/lib/src/editor/block_component/text_block_component/text_block_component.dart index 2eac7dc03..5ac1bb2bf 100644 --- a/lib/src/editor/block_component/text_block_component/text_block_component.dart +++ b/lib/src/editor/block_component/text_block_component/text_block_component.dart @@ -101,6 +101,29 @@ class _TextBlockComponentWidgetState extends State @override Node get node => widget.node; + bool _showPlaceholder = false; + + @override + void initState() { + super.initState(); + editorState.selectionNotifier.addListener(_onSelectionChange); + _onSelectionChange(); + } + + @override + void dispose() { + editorState.selectionNotifier.removeListener(_onSelectionChange); + super.dispose(); + } + + void _onSelectionChange() { + setState(() { + final selection = editorState.selection; + _showPlaceholder = selection != null && + (selection.isSingle && selection.start.path.equals(node.path)); + }); + } + @override Widget buildComponent(BuildContext context) { final textDirection = calculateTextDirection( @@ -120,14 +143,11 @@ class _TextBlockComponentWidgetState extends State key: forwardKey, node: widget.node, editorState: editorState, - placeholderText: placeholderText, - textSpanDecorator: (textSpan) => textSpan.updateTextStyle( - textStyle, - ), + placeholderText: _showPlaceholder ? placeholderText : ' ', + textSpanDecorator: (textSpan) => + textSpan.updateTextStyle(textStyle), placeholderTextSpanDecorator: (textSpan) => - textSpan.updateTextStyle( - placeholderTextStyle, - ), + textSpan.updateTextStyle(placeholderTextStyle), textDirection: textDirection, ), ],