fix: smart edit bugs (#1911)

This commit is contained in:
Lucas.Xu
2023-03-02 19:34:08 +08:00
committed by GitHub
parent a1a5675875
commit fd41459a30
3 changed files with 53 additions and 38 deletions

View File

@ -177,7 +177,9 @@ class _AppFlowyEditorPageState extends State<_AppFlowyEditorPage> {
] ]
], ],
toolbarItems: [ toolbarItems: [
if (openAIKey != null && openAIKey!.isNotEmpty) ...[
smartEditItem, smartEditItem,
]
], ],
themeData: theme.copyWith(extensions: [ themeData: theme.copyWith(extensions: [
...theme.extensions.values, ...theme.extensions.values,

View File

@ -42,11 +42,13 @@ class _SmartEditWidgetState extends State<_SmartEditWidget> {
.toList(), .toList(),
buildChild: (controller) { buildChild: (controller) {
return FlowyIconButton( return FlowyIconButton(
hoverColor: Colors.transparent,
tooltipText: 'Smart Edit', tooltipText: 'Smart Edit',
preferBelow: false, preferBelow: false,
icon: const Icon( icon: const Icon(
Icons.edit, Icons.lightbulb_outline,
size: 14, size: 13,
color: Colors.white,
), ),
onPressed: () { onPressed: () {
controller.show(); controller.show();
@ -55,6 +57,13 @@ class _SmartEditWidgetState extends State<_SmartEditWidget> {
}, },
onSelected: (action, controller) { onSelected: (action, controller) {
controller.close(); controller.close();
_insertSmartEditNode(action);
},
);
}
Future<void> _insertSmartEditNode(
SmartEditActionWrapper actionWrapper) async {
final selection = final selection =
widget.editorState.service.selectionService.currentSelection.value; widget.editorState.service.selectionService.currentSelection.value;
if (selection == null) { if (selection == null) {
@ -74,12 +83,12 @@ class _SmartEditWidgetState extends State<_SmartEditWidget> {
Node( Node(
type: kSmartEditType, type: kSmartEditType,
attributes: { attributes: {
kSmartEditInstructionType: action.inner.toInstruction, kSmartEditInstructionType: actionWrapper.inner.toInstruction,
kSmartEditInputType: input, kSmartEditInputType: input,
}, },
), ),
); );
widget.editorState.apply( return widget.editorState.apply(
transaction, transaction,
options: const ApplyOptions( options: const ApplyOptions(
recordUndo: false, recordUndo: false,
@ -87,7 +96,5 @@ class _SmartEditWidgetState extends State<_SmartEditWidget> {
), ),
withUpdateCursor: false, withUpdateCursor: false,
); );
},
);
} }
} }

View File

@ -59,12 +59,18 @@ extension CommandExtension on EditorState {
List<String> res = []; List<String> res = [];
if (!selection.isCollapsed) { if (!selection.isCollapsed) {
for (var i = 0; i < textNodes.length; i++) { for (var i = 0; i < textNodes.length; i++) {
final plainText = textNodes[i].toPlainText();
if (i == 0) { if (i == 0) {
res.add(textNodes[i].toPlainText().substring(selection.startIndex)); res.add(
plainText.substring(
selection.startIndex,
plainText.length,
),
);
} else if (i == textNodes.length - 1) { } else if (i == textNodes.length - 1) {
res.add(textNodes[i].toPlainText().substring(0, selection.endIndex)); res.add(plainText.substring(0, selection.endIndex));
} else { } else {
res.add(textNodes[i].toPlainText()); res.add(plainText);
} }
} }
} }