From fd41459a30547ab6c7a970458f06c7a349a85b85 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Thu, 2 Mar 2023 19:34:08 +0800 Subject: [PATCH] fix: smart edit bugs (#1911) --- .../lib/plugins/document/document_page.dart | 4 +- .../widgets/smart_edit_toolbar_item.dart | 75 ++++++++++--------- .../lib/src/commands/command_extension.dart | 12 ++- 3 files changed, 53 insertions(+), 38 deletions(-) diff --git a/frontend/appflowy_flutter/lib/plugins/document/document_page.dart b/frontend/appflowy_flutter/lib/plugins/document/document_page.dart index f23ace9995..e6a00c842e 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/document_page.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/document_page.dart @@ -177,7 +177,9 @@ class _AppFlowyEditorPageState extends State<_AppFlowyEditorPage> { ] ], toolbarItems: [ - smartEditItem, + if (openAIKey != null && openAIKey!.isNotEmpty) ...[ + smartEditItem, + ] ], themeData: theme.copyWith(extensions: [ ...theme.extensions.values, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/openai/widgets/smart_edit_toolbar_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/openai/widgets/smart_edit_toolbar_item.dart index 8ec609ef8f..0247413544 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/openai/widgets/smart_edit_toolbar_item.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/plugins/openai/widgets/smart_edit_toolbar_item.dart @@ -42,11 +42,13 @@ class _SmartEditWidgetState extends State<_SmartEditWidget> { .toList(), buildChild: (controller) { return FlowyIconButton( + hoverColor: Colors.transparent, tooltipText: 'Smart Edit', preferBelow: false, icon: const Icon( - Icons.edit, - size: 14, + Icons.lightbulb_outline, + size: 13, + color: Colors.white, ), onPressed: () { controller.show(); @@ -55,39 +57,44 @@ class _SmartEditWidgetState extends State<_SmartEditWidget> { }, onSelected: (action, controller) { controller.close(); - final selection = - widget.editorState.service.selectionService.currentSelection.value; - if (selection == null) { - return; - } - final textNodes = widget - .editorState.service.selectionService.currentSelectedNodes - .whereType() - .toList(growable: false); - final input = widget.editorState.getTextInSelection( - textNodes.normalized, - selection.normalized, - ); - final transaction = widget.editorState.transaction; - transaction.insertNode( - selection.normalized.end.path.next, - Node( - type: kSmartEditType, - attributes: { - kSmartEditInstructionType: action.inner.toInstruction, - kSmartEditInputType: input, - }, - ), - ); - widget.editorState.apply( - transaction, - options: const ApplyOptions( - recordUndo: false, - recordRedo: false, - ), - withUpdateCursor: false, - ); + _insertSmartEditNode(action); }, ); } + + Future _insertSmartEditNode( + SmartEditActionWrapper actionWrapper) async { + final selection = + widget.editorState.service.selectionService.currentSelection.value; + if (selection == null) { + return; + } + final textNodes = widget + .editorState.service.selectionService.currentSelectedNodes + .whereType() + .toList(growable: false); + final input = widget.editorState.getTextInSelection( + textNodes.normalized, + selection.normalized, + ); + final transaction = widget.editorState.transaction; + transaction.insertNode( + selection.normalized.end.path.next, + Node( + type: kSmartEditType, + attributes: { + kSmartEditInstructionType: actionWrapper.inner.toInstruction, + kSmartEditInputType: input, + }, + ), + ); + return widget.editorState.apply( + transaction, + options: const ApplyOptions( + recordUndo: false, + recordRedo: false, + ), + withUpdateCursor: false, + ); + } } diff --git a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/commands/command_extension.dart b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/commands/command_extension.dart index e383cb1d1d..5a9ea4f4bd 100644 --- a/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/commands/command_extension.dart +++ b/frontend/appflowy_flutter/packages/appflowy_editor/lib/src/commands/command_extension.dart @@ -59,12 +59,18 @@ extension CommandExtension on EditorState { List res = []; if (!selection.isCollapsed) { for (var i = 0; i < textNodes.length; i++) { + final plainText = textNodes[i].toPlainText(); 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) { - res.add(textNodes[i].toPlainText().substring(0, selection.endIndex)); + res.add(plainText.substring(0, selection.endIndex)); } else { - res.add(textNodes[i].toPlainText()); + res.add(plainText); } } }