appflowy-editor/example/lib/pages/editor.dart

163 lines
4.8 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
2023-07-07 16:07:47 +08:00
class Editor extends StatelessWidget {
const Editor({
super.key,
required this.jsonString,
required this.onEditorStateChange,
this.editorStyle,
});
final Future<String> jsonString;
final EditorStyle? editorStyle;
final void Function(EditorState editorState) onEditorStateChange;
@override
Widget build(BuildContext context) {
return FutureBuilder<String>(
future: jsonString,
builder: (context, snapshot) {
if (snapshot.hasData &&
snapshot.connectionState == ConnectionState.done) {
final editorState = EditorState(
document: Document.fromJson(
Map<String, Object>.from(
json.decode(snapshot.data!),
),
),
);
editorState.logConfiguration
..handler = debugPrint
..level = LogLevel.off;
editorState.transactionStream.listen((event) {
if (event.$1 == TransactionTime.after) {
onEditorStateChange(editorState);
}
});
2023-09-11 15:03:14 +08:00
final editorScrollController = EditorScrollController(
editorState: editorState,
shrinkWrap: false,
);
2023-04-25 19:58:48 +08:00
if (PlatformExtension.isDesktopOrWeb) {
return FloatingToolbar(
2023-04-28 16:25:51 +08:00
items: [
paragraphItem,
2023-04-29 16:39:32 +08:00
...headingItems,
2023-05-02 18:44:52 +08:00
...markdownFormatItems,
quoteItem,
bulletedListItem,
numberedListItem,
linkItem,
2023-07-03 21:38:42 -05:00
buildTextColorItem(),
buildHighlightColorItem(),
...textDirectionItems,
...alignmentItems,
2023-04-28 16:25:51 +08:00
],
editorState: editorState,
2023-09-11 15:03:14 +08:00
editorScrollController: editorScrollController,
child: _buildDesktopEditor(
context,
editorState,
2023-09-11 15:03:14 +08:00
editorScrollController,
),
);
feat: mobile tool bar basic features (#161) * refactor: refactor mobile toolbar * feat: add text decoration icon in AFMobileIcons * chore: update mobile parameter * feat: add text decoration basic feature * chore: replace text by AppFlowyEditorLocalizations * feat: add code toolbar item * chore: refactor code - add hasMenu in MToolbarItem - add const size * feat: add line input menu * chore: create toolbar items draft file * refactor: textDecorationMToolbarItem refactor * fix: add key to ensures MToolbarWidget is rebuilt whenever the selection changes * feat: add link toolbar item and the way to close item menu * chore: replace m by mobile * chore:replace BuiltInAttributeKey by FlowyRichTextKeys * chore: replace m by mobile * refactor: refactor MobileToolbarWidget * refactor: add MobileToolbarStyle to control all cosmetic parameters * refactor: refactor MobileToolbarItemMenuBtn to stateless widget * feat: add heading menu * fix: fix open the wrong menu * feat: add heading icon * feat: add todo list and list toolbar item * feat: add quote toolbar item * fix: fix selection service error * test: add mobile toolbar basic elements test * test: add item menu button test and helper widgets * test: add text decoration mobile toolbar item test * test: add code mobile toolbar item test * test: add heading mobile toolbar item test * test: add list mobile toolbar item test * chore: fix typo * test: add quote mobile toolbar item test * test: add todo list mobile toolbar item test * test: add link mobile toolbar item test * chore: update name and add Material widget * fix: flutter analyze * fix: flutter analyze in test mobile toolbar * chore: dart format test code * refactor: improve code * fix: fix refactor error * chore: move all the tests in mobile to test/mobile * chore: move toolbar/items to toolbar/desktop/items * chore: remove the mobileToolbarItemMenuStateKey * chore: add some comments * test: update testing --------- Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>
2023-06-13 01:01:08 -05:00
} else if (PlatformExtension.isMobile) {
2023-04-25 19:58:48 +08:00
return Column(
children: [
Expanded(
2023-05-20 05:43:38 -05:00
child: _buildMobileEditor(
context,
editorState,
2023-09-11 15:03:14 +08:00
editorScrollController,
2023-05-20 05:43:38 -05:00
),
),
feat: mobile tool bar basic features (#161) * refactor: refactor mobile toolbar * feat: add text decoration icon in AFMobileIcons * chore: update mobile parameter * feat: add text decoration basic feature * chore: replace text by AppFlowyEditorLocalizations * feat: add code toolbar item * chore: refactor code - add hasMenu in MToolbarItem - add const size * feat: add line input menu * chore: create toolbar items draft file * refactor: textDecorationMToolbarItem refactor * fix: add key to ensures MToolbarWidget is rebuilt whenever the selection changes * feat: add link toolbar item and the way to close item menu * chore: replace m by mobile * chore:replace BuiltInAttributeKey by FlowyRichTextKeys * chore: replace m by mobile * refactor: refactor MobileToolbarWidget * refactor: add MobileToolbarStyle to control all cosmetic parameters * refactor: refactor MobileToolbarItemMenuBtn to stateless widget * feat: add heading menu * fix: fix open the wrong menu * feat: add heading icon * feat: add todo list and list toolbar item * feat: add quote toolbar item * fix: fix selection service error * test: add mobile toolbar basic elements test * test: add item menu button test and helper widgets * test: add text decoration mobile toolbar item test * test: add code mobile toolbar item test * test: add heading mobile toolbar item test * test: add list mobile toolbar item test * chore: fix typo * test: add quote mobile toolbar item test * test: add todo list mobile toolbar item test * test: add link mobile toolbar item test * chore: update name and add Material widget * fix: flutter analyze * fix: flutter analyze in test mobile toolbar * chore: dart format test code * refactor: improve code * fix: fix refactor error * chore: move all the tests in mobile to test/mobile * chore: move toolbar/items to toolbar/desktop/items * chore: remove the mobileToolbarItemMenuStateKey * chore: add some comments * test: update testing --------- Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>
2023-06-13 01:01:08 -05:00
MobileToolbar(
editorState: editorState,
toolbarItems: [
textDecorationMobileToolbarItem,
2023-07-03 21:38:42 -05:00
buildTextAndBackgroundColorMobileToolbarItem(),
feat: mobile tool bar basic features (#161) * refactor: refactor mobile toolbar * feat: add text decoration icon in AFMobileIcons * chore: update mobile parameter * feat: add text decoration basic feature * chore: replace text by AppFlowyEditorLocalizations * feat: add code toolbar item * chore: refactor code - add hasMenu in MToolbarItem - add const size * feat: add line input menu * chore: create toolbar items draft file * refactor: textDecorationMToolbarItem refactor * fix: add key to ensures MToolbarWidget is rebuilt whenever the selection changes * feat: add link toolbar item and the way to close item menu * chore: replace m by mobile * chore:replace BuiltInAttributeKey by FlowyRichTextKeys * chore: replace m by mobile * refactor: refactor MobileToolbarWidget * refactor: add MobileToolbarStyle to control all cosmetic parameters * refactor: refactor MobileToolbarItemMenuBtn to stateless widget * feat: add heading menu * fix: fix open the wrong menu * feat: add heading icon * feat: add todo list and list toolbar item * feat: add quote toolbar item * fix: fix selection service error * test: add mobile toolbar basic elements test * test: add item menu button test and helper widgets * test: add text decoration mobile toolbar item test * test: add code mobile toolbar item test * test: add heading mobile toolbar item test * test: add list mobile toolbar item test * chore: fix typo * test: add quote mobile toolbar item test * test: add todo list mobile toolbar item test * test: add link mobile toolbar item test * chore: update name and add Material widget * fix: flutter analyze * fix: flutter analyze in test mobile toolbar * chore: dart format test code * refactor: improve code * fix: fix refactor error * chore: move all the tests in mobile to test/mobile * chore: move toolbar/items to toolbar/desktop/items * chore: remove the mobileToolbarItemMenuStateKey * chore: add some comments * test: update testing --------- Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>
2023-06-13 01:01:08 -05:00
headingMobileToolbarItem,
todoListMobileToolbarItem,
listMobileToolbarItem,
linkMobileToolbarItem,
quoteMobileToolbarItem,
dividerMobileToolbarItem,
feat: mobile tool bar basic features (#161) * refactor: refactor mobile toolbar * feat: add text decoration icon in AFMobileIcons * chore: update mobile parameter * feat: add text decoration basic feature * chore: replace text by AppFlowyEditorLocalizations * feat: add code toolbar item * chore: refactor code - add hasMenu in MToolbarItem - add const size * feat: add line input menu * chore: create toolbar items draft file * refactor: textDecorationMToolbarItem refactor * fix: add key to ensures MToolbarWidget is rebuilt whenever the selection changes * feat: add link toolbar item and the way to close item menu * chore: replace m by mobile * chore:replace BuiltInAttributeKey by FlowyRichTextKeys * chore: replace m by mobile * refactor: refactor MobileToolbarWidget * refactor: add MobileToolbarStyle to control all cosmetic parameters * refactor: refactor MobileToolbarItemMenuBtn to stateless widget * feat: add heading menu * fix: fix open the wrong menu * feat: add heading icon * feat: add todo list and list toolbar item * feat: add quote toolbar item * fix: fix selection service error * test: add mobile toolbar basic elements test * test: add item menu button test and helper widgets * test: add text decoration mobile toolbar item test * test: add code mobile toolbar item test * test: add heading mobile toolbar item test * test: add list mobile toolbar item test * chore: fix typo * test: add quote mobile toolbar item test * test: add todo list mobile toolbar item test * test: add link mobile toolbar item test * chore: update name and add Material widget * fix: flutter analyze * fix: flutter analyze in test mobile toolbar * chore: dart format test code * refactor: improve code * fix: fix refactor error * chore: move all the tests in mobile to test/mobile * chore: move toolbar/items to toolbar/desktop/items * chore: remove the mobileToolbarItemMenuStateKey * chore: add some comments * test: update testing --------- Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>
2023-06-13 01:01:08 -05:00
codeMobileToolbarItem,
],
),
2023-04-25 19:58:48 +08:00
],
);
}
}
feat: mobile tool bar basic features (#161) * refactor: refactor mobile toolbar * feat: add text decoration icon in AFMobileIcons * chore: update mobile parameter * feat: add text decoration basic feature * chore: replace text by AppFlowyEditorLocalizations * feat: add code toolbar item * chore: refactor code - add hasMenu in MToolbarItem - add const size * feat: add line input menu * chore: create toolbar items draft file * refactor: textDecorationMToolbarItem refactor * fix: add key to ensures MToolbarWidget is rebuilt whenever the selection changes * feat: add link toolbar item and the way to close item menu * chore: replace m by mobile * chore:replace BuiltInAttributeKey by FlowyRichTextKeys * chore: replace m by mobile * refactor: refactor MobileToolbarWidget * refactor: add MobileToolbarStyle to control all cosmetic parameters * refactor: refactor MobileToolbarItemMenuBtn to stateless widget * feat: add heading menu * fix: fix open the wrong menu * feat: add heading icon * feat: add todo list and list toolbar item * feat: add quote toolbar item * fix: fix selection service error * test: add mobile toolbar basic elements test * test: add item menu button test and helper widgets * test: add text decoration mobile toolbar item test * test: add code mobile toolbar item test * test: add heading mobile toolbar item test * test: add list mobile toolbar item test * chore: fix typo * test: add quote mobile toolbar item test * test: add todo list mobile toolbar item test * test: add link mobile toolbar item test * chore: update name and add Material widget * fix: flutter analyze * fix: flutter analyze in test mobile toolbar * chore: dart format test code * refactor: improve code * fix: fix refactor error * chore: move all the tests in mobile to test/mobile * chore: move toolbar/items to toolbar/desktop/items * chore: remove the mobileToolbarItemMenuStateKey * chore: add some comments * test: update testing --------- Co-authored-by: Lucas.Xu <lucas.xu@appflowy.io>
2023-06-13 01:01:08 -05:00
return const Center(
child: CircularProgressIndicator(),
);
},
);
}
2023-04-20 13:45:46 +08:00
2023-05-20 05:43:38 -05:00
Widget _buildMobileEditor(
BuildContext context,
EditorState editorState,
2023-09-11 15:03:14 +08:00
EditorScrollController? editorScrollController,
2023-05-20 05:43:38 -05:00
) {
2023-07-07 16:07:47 +08:00
return AppFlowyEditor(
2023-05-20 05:43:38 -05:00
editorStyle: const EditorStyle.mobile(),
editorState: editorState,
2023-09-11 15:03:14 +08:00
editorScrollController: editorScrollController,
2023-05-20 05:43:38 -05:00
);
}
Widget _buildDesktopEditor(
BuildContext context,
EditorState editorState,
2023-09-11 15:03:14 +08:00
EditorScrollController? editorScrollController,
) {
final customBlockComponentBuilders = {
...standardBlockComponentBuilderMap,
ImageBlockKeys.type: ImageBlockComponentBuilder(
showMenu: true,
menuBuilder: (node, _) {
return const Positioned(
right: 10,
child: Text('Sample Menu'),
);
},
)
};
2023-07-07 16:07:47 +08:00
return AppFlowyEditor(
2023-04-20 13:45:46 +08:00
editorState: editorState,
2023-09-11 15:03:14 +08:00
shrinkWrap: true,
editorScrollController: editorScrollController,
blockComponentBuilders: customBlockComponentBuilders,
feat: find dialog 1760 (#106) * feat: make find menu widget * feat: service for find menu * feat: add find menu shortcut event * feat: create a search service * docs: explain search service * fix: unhighlight method takes searched word * fix: unhighlight before each search * feat: navigate between matches * feat: forget highlighting from undo stack * feat: replace logic and ui * feat: replace shortcut handler and widget * test: find functionality * test: replace menu tests * refactor: separate class for search algo * refactor: suggested changes * refactor: remove unhighlight method * feat: add find highlight color * refactor: name of the search algo class * test: unit tests for search algorithm * chore: simplify syntax * test: renamed test group * refactor: move to editor * refactor: add shortcut for find * refactor: use new api * refactor: add abstrac class search algo * fix: avoid multiple instances of find dialog * refactor: xazin's suggestions * chore: separately build input decor * test: search algorithm * chore: unhighlight properly * refactor: replace handler * refactor: move tests into new * test: find menu widget test * test: replace menu * fix: localizations + resolve scroll bug partially * fix: without update selection on highlight * fix: do not select words when highlighting * test: update expected selection * fix: matches and styling * feat: separate attribute for highlighting * feat: unique color for selected match * test: unique color for selected match * refactor: cleaning the code * fix: navigate to first match * fix: tests and replace logic --------- Co-authored-by: Mathias Mogensen <mathiasrieckm@gmail.com> Co-authored-by: Mathias Mogensen <42929161+Xazin@users.noreply.github.com>
2023-08-10 16:38:45 +05:30
commandShortcutEvents: [
customToggleHighlightCommand(
style: ToggleColorsStyle(
highlightColor: Theme.of(context).highlightColor,
),
),
...[
...standardCommandShortcutEvents
..removeWhere(
(el) => el == toggleHighlightCommand,
),
],
feat: find dialog 1760 (#106) * feat: make find menu widget * feat: service for find menu * feat: add find menu shortcut event * feat: create a search service * docs: explain search service * fix: unhighlight method takes searched word * fix: unhighlight before each search * feat: navigate between matches * feat: forget highlighting from undo stack * feat: replace logic and ui * feat: replace shortcut handler and widget * test: find functionality * test: replace menu tests * refactor: separate class for search algo * refactor: suggested changes * refactor: remove unhighlight method * feat: add find highlight color * refactor: name of the search algo class * test: unit tests for search algorithm * chore: simplify syntax * test: renamed test group * refactor: move to editor * refactor: add shortcut for find * refactor: use new api * refactor: add abstrac class search algo * fix: avoid multiple instances of find dialog * refactor: xazin's suggestions * chore: separately build input decor * test: search algorithm * chore: unhighlight properly * refactor: replace handler * refactor: move tests into new * test: find menu widget test * test: replace menu * fix: localizations + resolve scroll bug partially * fix: without update selection on highlight * fix: do not select words when highlighting * test: update expected selection * fix: matches and styling * feat: separate attribute for highlighting * feat: unique color for selected match * test: unique color for selected match * refactor: cleaning the code * fix: navigate to first match * fix: tests and replace logic --------- Co-authored-by: Mathias Mogensen <mathiasrieckm@gmail.com> Co-authored-by: Mathias Mogensen <42929161+Xazin@users.noreply.github.com>
2023-08-10 16:38:45 +05:30
...findAndReplaceCommands(
context: context,
localizations: FindReplaceLocalizations(
find: 'Find',
previousMatch: 'Previous match',
nextMatch: 'Next match',
close: 'Close',
replace: 'Replace',
replaceAll: 'Replace all',
),
),
],
characterShortcutEvents: standardCharacterShortcutEvents,
2023-04-20 13:45:46 +08:00
);
}
}