forked from AppFlowy-IO/appflowy-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* main: chore: bump version 2.3.4 (AppFlowy-IO#770) fix: count any non-whitespace character (AppFlowy-IO#771) feat: support customizing mobile page style (AppFlowy-IO#769) feat: add table style (AppFlowy-IO#766) feat: support auto complete for text block (AppFlowy-IO#764)
- Loading branch information
Showing
19 changed files
with
502 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
|
||
final _options = { | ||
'hello ': 'world', | ||
'support@g': 'mail.com', | ||
'appflowy ': 'editor', | ||
}; | ||
|
||
class AutoCompleteEditor extends StatelessWidget { | ||
const AutoCompleteEditor({ | ||
super.key, | ||
}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
body: SafeArea( | ||
child: Column( | ||
children: [ | ||
const SizedBox(height: 100), | ||
Container( | ||
height: 500, | ||
decoration: BoxDecoration( | ||
border: Border.all(color: Colors.blue), | ||
), | ||
child: AppFlowyEditor( | ||
editorState: EditorState( | ||
document: Document.blank(withInitialText: true), | ||
), | ||
commandShortcutEvents: [ | ||
tabToAutoCompleteCommand, | ||
...standardCommandShortcutEvents, | ||
], | ||
enableAutoComplete: true, | ||
autoCompleteTextProvider: (context, node, textSpan) { | ||
final editorState = context.read<EditorState>(); | ||
final selection = editorState.selection; | ||
final delta = node.delta; | ||
if (selection == null || | ||
delta == null || | ||
!selection.isCollapsed || | ||
selection.endIndex != delta.length || | ||
!node.path.equals(selection.start.path)) { | ||
return null; | ||
} | ||
final text = delta.toPlainText().toLowerCase(); | ||
for (final option in _options.keys) { | ||
if (text.endsWith(option)) { | ||
return _options[option]; | ||
} | ||
} | ||
return null; | ||
}, | ||
), | ||
), | ||
const SizedBox(height: 16), | ||
Text( | ||
'AutoComplete Options: $_options', | ||
style: Theme.of(context).textTheme.titleLarge, | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
lib/src/editor/block_component/base_component/auto_complete_command.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
// Add your custom block keys if it supports auto complete | ||
final autoCompletableBlockTypes = { | ||
ParagraphBlockKeys.type, | ||
NumberedListBlockKeys.type, | ||
TodoListBlockKeys.type, | ||
BulletedListBlockKeys.type, | ||
QuoteBlockKeys.type, | ||
HeadingBlockKeys.type, | ||
}; | ||
|
||
/// Auto complete the current block | ||
/// | ||
/// - support | ||
/// - desktop | ||
/// - web | ||
/// | ||
final CommandShortcutEvent tabToAutoCompleteCommand = CommandShortcutEvent( | ||
key: 'tab to auto complete', | ||
getDescription: () => 'Tab to auto complete', | ||
command: 'tab', | ||
handler: _tabToAutoCompleteCommandHandler, | ||
); | ||
|
||
CommandShortcutEventHandler _tabToAutoCompleteCommandHandler = (editorState) { | ||
final selection = editorState.selection; | ||
if (selection == null || !selection.isCollapsed) { | ||
return KeyEventResult.ignored; | ||
} | ||
|
||
final context = editorState.document.root.context; | ||
final node = editorState.getNodeAtPath(selection.end.path); | ||
final delta = node?.delta; | ||
|
||
// Now, this command only support auto complete the text if the cursor is at the end of the block | ||
if (context == null || | ||
node == null || | ||
!autoCompletableBlockTypes.contains(node.type) || | ||
delta == null || | ||
selection.endIndex != delta.length) { | ||
return KeyEventResult.ignored; | ||
} | ||
|
||
// Support async auto complete text provider in the future | ||
final autoCompleteText = editorState.autoCompleteTextProvider?.call( | ||
context, | ||
node, | ||
null, | ||
); | ||
if (autoCompleteText == null || autoCompleteText.isEmpty) { | ||
return KeyEventResult.ignored; | ||
} | ||
|
||
final transaction = editorState.transaction | ||
..insertText( | ||
node, | ||
selection.endIndex, | ||
autoCompleteText, | ||
); | ||
editorState.apply(transaction); | ||
|
||
return KeyEventResult.handled; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.