mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
Merge pull request #780 from LucasXu0/feat/fix_focus_error
customize flowy_rich_text style
This commit is contained in:
commit
4a00f3c2ca
@ -18,12 +18,14 @@ class FlowySvg extends StatelessWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
return SvgPicture.asset(
|
return SizedBox.fromSize(
|
||||||
|
size: size,
|
||||||
|
child: SvgPicture.asset(
|
||||||
'assets/images/$name.svg',
|
'assets/images/$name.svg',
|
||||||
color: color,
|
color: color,
|
||||||
package: 'flowy_editor',
|
package: 'flowy_editor',
|
||||||
width: size.width,
|
fit: BoxFit.fill,
|
||||||
height: size.width,
|
),
|
||||||
);
|
);
|
||||||
} else if (number != null) {
|
} else if (number != null) {
|
||||||
final numberText =
|
final numberText =
|
||||||
|
@ -62,11 +62,14 @@ class _BulletedListTextNodeWidgetState extends State<BulletedListTextNodeWidget>
|
|||||||
size: Size.square(leftPadding),
|
size: Size.square(leftPadding),
|
||||||
name: 'point',
|
name: 'point',
|
||||||
),
|
),
|
||||||
FlowyRichText(
|
Expanded(
|
||||||
|
child: FlowyRichText(
|
||||||
key: _richTextKey,
|
key: _richTextKey,
|
||||||
|
placeholderText: 'List',
|
||||||
textNode: widget.textNode,
|
textNode: widget.textNode,
|
||||||
editorState: widget.editorState,
|
editorState: widget.editorState,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -82,11 +82,15 @@ class _CheckboxNodeWidgetState extends State<CheckboxNodeWidget>
|
|||||||
..commit();
|
..commit();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
FlowyRichText(
|
Expanded(
|
||||||
|
child: FlowyRichText(
|
||||||
key: _richTextKey,
|
key: _richTextKey,
|
||||||
|
placeholderText: 'To-do',
|
||||||
textNode: widget.textNode,
|
textNode: widget.textNode,
|
||||||
|
textSpanDecorator: _textSpanDecorator,
|
||||||
editorState: widget.editorState,
|
editorState: widget.editorState,
|
||||||
)
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -120,4 +124,24 @@ class _CheckboxNodeWidgetState extends State<CheckboxNodeWidget>
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextSpan _textSpanDecorator(TextSpan textSpan) {
|
||||||
|
return TextSpan(
|
||||||
|
children: textSpan.children
|
||||||
|
?.whereType<TextSpan>()
|
||||||
|
.map(
|
||||||
|
(span) => TextSpan(
|
||||||
|
text: span.text,
|
||||||
|
style: widget.textNode.attributes.check
|
||||||
|
? span.style?.copyWith(
|
||||||
|
color: Colors.grey,
|
||||||
|
decoration: TextDecoration.lineThrough,
|
||||||
|
)
|
||||||
|
: span.style,
|
||||||
|
recognizer: span.recognizer,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,15 +35,19 @@ class FlowyRichText extends StatefulWidget {
|
|||||||
this.cursorHeight,
|
this.cursorHeight,
|
||||||
this.cursorWidth = 2.0,
|
this.cursorWidth = 2.0,
|
||||||
this.textSpanDecorator,
|
this.textSpanDecorator,
|
||||||
|
this.placeholderText = ' ',
|
||||||
|
this.placeholderTextSpanDecorator,
|
||||||
required this.textNode,
|
required this.textNode,
|
||||||
required this.editorState,
|
required this.editorState,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
final double? cursorHeight;
|
|
||||||
final double cursorWidth;
|
|
||||||
final TextNode textNode;
|
final TextNode textNode;
|
||||||
final EditorState editorState;
|
final EditorState editorState;
|
||||||
|
final double? cursorHeight;
|
||||||
|
final double cursorWidth;
|
||||||
final FlowyTextSpanDecorator? textSpanDecorator;
|
final FlowyTextSpanDecorator? textSpanDecorator;
|
||||||
|
final String placeholderText;
|
||||||
|
final FlowyTextSpanDecorator? placeholderTextSpanDecorator;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<FlowyRichText> createState() => _FlowyRichTextState();
|
State<FlowyRichText> createState() => _FlowyRichTextState();
|
||||||
@ -51,10 +55,14 @@ class FlowyRichText extends StatefulWidget {
|
|||||||
|
|
||||||
class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
|
class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
|
||||||
final _textKey = GlobalKey();
|
final _textKey = GlobalKey();
|
||||||
|
final _placeholderTextKey = GlobalKey();
|
||||||
|
|
||||||
RenderParagraph get _renderParagraph =>
|
RenderParagraph get _renderParagraph =>
|
||||||
_textKey.currentContext?.findRenderObject() as RenderParagraph;
|
_textKey.currentContext?.findRenderObject() as RenderParagraph;
|
||||||
|
|
||||||
|
RenderParagraph get _placeholderRenderParagraph =>
|
||||||
|
_placeholderTextKey.currentContext?.findRenderObject() as RenderParagraph;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return _buildRichText(context);
|
return _buildRichText(context);
|
||||||
@ -74,6 +82,7 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
|
|||||||
_renderParagraph.getOffsetForCaret(textPosition, Rect.zero);
|
_renderParagraph.getOffsetForCaret(textPosition, Rect.zero);
|
||||||
final cursorHeight = widget.cursorHeight ??
|
final cursorHeight = widget.cursorHeight ??
|
||||||
_renderParagraph.getFullHeightForCaret(textPosition) ??
|
_renderParagraph.getFullHeightForCaret(textPosition) ??
|
||||||
|
_placeholderRenderParagraph.getFullHeightForCaret(textPosition) ??
|
||||||
18.0; // default height
|
18.0; // default height
|
||||||
return Rect.fromLTWH(
|
return Rect.fromLTWH(
|
||||||
cursorOffset.dx - (widget.cursorWidth / 2),
|
cursorOffset.dx - (widget.cursorWidth / 2),
|
||||||
@ -129,9 +138,33 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildRichText(BuildContext context) {
|
Widget _buildRichText(BuildContext context) {
|
||||||
return Align(
|
return Stack(
|
||||||
alignment: Alignment.centerLeft,
|
children: [
|
||||||
child: _buildSingleRichText(context),
|
_buildPlaceholderText(context),
|
||||||
|
_buildSingleRichText(context),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget _buildPlaceholderText(BuildContext context) {
|
||||||
|
final textSpan = TextSpan(
|
||||||
|
children: [
|
||||||
|
TextSpan(
|
||||||
|
text: widget.placeholderText,
|
||||||
|
style: TextStyle(
|
||||||
|
color: widget.textNode.toRawString().isNotEmpty
|
||||||
|
? Colors.transparent
|
||||||
|
: Colors.grey,
|
||||||
|
fontSize: baseFontSize,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
return RichText(
|
||||||
|
key: _placeholderTextKey,
|
||||||
|
text: widget.placeholderTextSpanDecorator != null
|
||||||
|
? widget.placeholderTextSpanDecorator!(textSpan)
|
||||||
|
: textSpan,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,12 +63,16 @@ class _HeadingTextNodeWidgetState extends State<HeadingTextNodeWidget>
|
|||||||
top: topPadding,
|
top: topPadding,
|
||||||
bottom: bottomPadding,
|
bottom: bottomPadding,
|
||||||
),
|
),
|
||||||
|
child: Expanded(
|
||||||
child: FlowyRichText(
|
child: FlowyRichText(
|
||||||
key: _richTextKey,
|
key: _richTextKey,
|
||||||
|
placeholderText: 'Heading',
|
||||||
|
placeholderTextSpanDecorator: _placeholderTextSpanDecorator,
|
||||||
textSpanDecorator: _textSpanDecorator,
|
textSpanDecorator: _textSpanDecorator,
|
||||||
textNode: widget.textNode,
|
textNode: widget.textNode,
|
||||||
editorState: widget.editorState,
|
editorState: widget.editorState,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
@ -90,4 +94,21 @@ class _HeadingTextNodeWidgetState extends State<HeadingTextNodeWidget>
|
|||||||
.toList(),
|
.toList(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TextSpan _placeholderTextSpanDecorator(TextSpan textSpan) {
|
||||||
|
return TextSpan(
|
||||||
|
children: textSpan.children
|
||||||
|
?.whereType<TextSpan>()
|
||||||
|
.map(
|
||||||
|
(span) => TextSpan(
|
||||||
|
text: span.text,
|
||||||
|
style: span.style?.copyWith(
|
||||||
|
fontSize: widget.textNode.attributes.fontSize,
|
||||||
|
),
|
||||||
|
recognizer: span.recognizer,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,11 +63,14 @@ class _NumberListTextNodeWidgetState extends State<NumberListTextNodeWidget>
|
|||||||
size: Size.square(leftPadding),
|
size: Size.square(leftPadding),
|
||||||
number: widget.textNode.attributes.number,
|
number: widget.textNode.attributes.number,
|
||||||
),
|
),
|
||||||
FlowyRichText(
|
Expanded(
|
||||||
|
child: FlowyRichText(
|
||||||
key: _richTextKey,
|
key: _richTextKey,
|
||||||
|
placeholderText: 'List',
|
||||||
textNode: widget.textNode,
|
textNode: widget.textNode,
|
||||||
editorState: widget.editorState,
|
editorState: widget.editorState,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -59,15 +59,27 @@ class _QuotedTextNodeWidgetState extends State<QuotedTextNodeWidget>
|
|||||||
return Row(
|
return Row(
|
||||||
children: [
|
children: [
|
||||||
FlowySvg(
|
FlowySvg(
|
||||||
size: Size.square(leftPadding),
|
size: Size(
|
||||||
|
leftPadding,
|
||||||
|
_quoteHeight,
|
||||||
|
),
|
||||||
name: 'quote',
|
name: 'quote',
|
||||||
),
|
),
|
||||||
FlowyRichText(
|
Expanded(
|
||||||
|
child: FlowyRichText(
|
||||||
key: _richTextKey,
|
key: _richTextKey,
|
||||||
|
placeholderText: 'Quote',
|
||||||
textNode: widget.textNode,
|
textNode: widget.textNode,
|
||||||
editorState: widget.editorState,
|
editorState: widget.editorState,
|
||||||
),
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double get _quoteHeight {
|
||||||
|
final lines =
|
||||||
|
widget.textNode.toRawString().characters.where((c) => c == '\n').length;
|
||||||
|
return (lines + 1) * leftPadding;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,9 +208,16 @@ class _PopupListWidgetState extends State<PopupListWidget> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.logicalKey == LogicalKeyboardKey.enter) {
|
if (event.logicalKey == LogicalKeyboardKey.enter) {
|
||||||
|
if (0 <= selectedIndex && selectedIndex < widget.items.length) {
|
||||||
widget.items[selectedIndex].handler(widget.editorState);
|
widget.items[selectedIndex].handler(widget.editorState);
|
||||||
return KeyEventResult.handled;
|
return KeyEventResult.handled;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.logicalKey == LogicalKeyboardKey.escape) {
|
||||||
|
clearPopupListOverlay();
|
||||||
|
return KeyEventResult.handled;
|
||||||
|
}
|
||||||
|
|
||||||
var newSelectedIndex = selectedIndex;
|
var newSelectedIndex = selectedIndex;
|
||||||
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
|
if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
|
||||||
|
@ -32,23 +32,23 @@ class FlowyKeyboard extends StatefulWidget {
|
|||||||
|
|
||||||
class _FlowyKeyboardState extends State<FlowyKeyboard>
|
class _FlowyKeyboardState extends State<FlowyKeyboard>
|
||||||
with FlowyKeyboardService {
|
with FlowyKeyboardService {
|
||||||
final FocusNode focusNode = FocusNode(debugLabel: 'flowy_keyboard_service');
|
final FocusNode _focusNode = FocusNode(debugLabel: 'flowy_keyboard_service');
|
||||||
|
|
||||||
bool isFocus = true;
|
bool isFocus = true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Focus(
|
return Focus(
|
||||||
focusNode: focusNode,
|
focusNode: _focusNode,
|
||||||
autofocus: true,
|
|
||||||
onKey: _onKey,
|
onKey: _onKey,
|
||||||
|
onFocusChange: _onFocusChange,
|
||||||
child: widget.child,
|
child: widget.child,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
focusNode.dispose();
|
_focusNode.dispose();
|
||||||
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
@ -56,13 +56,17 @@ class _FlowyKeyboardState extends State<FlowyKeyboard>
|
|||||||
@override
|
@override
|
||||||
void enable() {
|
void enable() {
|
||||||
isFocus = true;
|
isFocus = true;
|
||||||
focusNode.requestFocus();
|
_focusNode.requestFocus();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void disable() {
|
void disable() {
|
||||||
isFocus = false;
|
isFocus = false;
|
||||||
focusNode.unfocus();
|
_focusNode.unfocus();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onFocusChange(bool value) {
|
||||||
|
debugPrint('[KeyBoard Service] focus change $value');
|
||||||
}
|
}
|
||||||
|
|
||||||
KeyEventResult _onKey(FocusNode node, RawKeyEvent event) {
|
KeyEventResult _onKey(FocusNode node, RawKeyEvent event) {
|
||||||
|
@ -265,6 +265,8 @@ class _FlowySelectionState extends State<FlowySelection>
|
|||||||
}
|
}
|
||||||
final selection = Selection.collapsed(position);
|
final selection = Selection.collapsed(position);
|
||||||
editorState.updateCursorSelection(selection);
|
editorState.updateCursorSelection(selection);
|
||||||
|
|
||||||
|
editorState.service.keyboardService?.enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
Loading…
Reference in New Issue
Block a user