diff --git a/frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart b/frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart index 539bb74425..e5aae9432d 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart +++ b/frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -8,6 +9,7 @@ import 'expandable_floating_action_button.dart'; import 'plugin/youtube_link_node_widget.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:path_provider/path_provider.dart'; void main() { runApp(const MyApp()); @@ -58,6 +60,7 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { final editorKey = GlobalKey(); int page = 0; + EditorState? _editorState; @override Widget build(BuildContext context) { @@ -103,6 +106,7 @@ class _MyHomePageState extends State { ..handler = (message) { debugPrint(message); }; + _editorState = editorState; return _buildAppFlowyEditor(editorState); } else { return const Center( @@ -146,6 +150,21 @@ class _MyHomePageState extends State { ); } + void _exportDocument(EditorState editorState) async { + // await FileSaver.instance.saveAs(String name, Uint8List bytes, String ext, MimeType); + final document = editorState.document.toJson(); + debugPrint(document.toString()); + final json = jsonEncode(document); + debugPrint(json); + + final directory = await getTemporaryDirectory(); + final path = directory.path; + debugPrint(path); + + final file = File('$path/temp.json'); + await file.writeAsString(json); + } + Widget _buildExpandableFab() { return ExpandableFab( distance: 112.0, @@ -177,6 +196,12 @@ class _MyHomePageState extends State { }, icon: const Icon(Icons.text_fields), ), + ActionButton( + onPressed: () { + _exportDocument(_editorState!); + }, + icon: const Icon(Icons.print), + ), ], ); } diff --git a/frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugin_registrant.cc b/frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugin_registrant.cc index 00fd3bc03f..45a19e066d 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugin_registrant.cc +++ b/frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugin_registrant.cc @@ -6,10 +6,14 @@ #include "generated_plugin_registrant.h" +#include #include #include void fl_register_plugins(FlPluginRegistry* registry) { + g_autoptr(FlPluginRegistrar) file_saver_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "FileSaverPlugin"); + file_saver_plugin_register_with_registrar(file_saver_registrar); g_autoptr(FlPluginRegistrar) rich_clipboard_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "RichClipboardPlugin"); rich_clipboard_plugin_register_with_registrar(rich_clipboard_linux_registrar); diff --git a/frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugins.cmake b/frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugins.cmake index 0342e3868a..10140a75b4 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugins.cmake +++ b/frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + file_saver rich_clipboard_linux url_launcher_linux ) diff --git a/frontend/app_flowy/packages/appflowy_editor/example/macos/Flutter/GeneratedPluginRegistrant.swift b/frontend/app_flowy/packages/appflowy_editor/example/macos/Flutter/GeneratedPluginRegistrant.swift index cc167443dc..ac60ac6ec9 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/frontend/app_flowy/packages/appflowy_editor/example/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,11 +5,15 @@ import FlutterMacOS import Foundation +import file_saver +import path_provider_macos import rich_clipboard_macos import url_launcher_macos import wakelock_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + FileSaverPlugin.register(with: registry.registrar(forPlugin: "FileSaverPlugin")) + PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) RichClipboardPlugin.register(with: registry.registrar(forPlugin: "RichClipboardPlugin")) UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin")) WakelockMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockMacosPlugin")) diff --git a/frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock b/frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock index d3a1dd3611..c56e297a68 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock +++ b/frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock @@ -1,5 +1,9 @@ PODS: + - file_saver (0.0.1): + - FlutterMacOS - FlutterMacOS (1.0.0) + - path_provider_macos (0.0.1): + - FlutterMacOS - rich_clipboard_macos (0.0.1): - FlutterMacOS - url_launcher_macos (0.0.1): @@ -8,14 +12,20 @@ PODS: - FlutterMacOS DEPENDENCIES: + - file_saver (from `Flutter/ephemeral/.symlinks/plugins/file_saver/macos`) - FlutterMacOS (from `Flutter/ephemeral`) + - path_provider_macos (from `Flutter/ephemeral/.symlinks/plugins/path_provider_macos/macos`) - rich_clipboard_macos (from `Flutter/ephemeral/.symlinks/plugins/rich_clipboard_macos/macos`) - url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`) - wakelock_macos (from `Flutter/ephemeral/.symlinks/plugins/wakelock_macos/macos`) EXTERNAL SOURCES: + file_saver: + :path: Flutter/ephemeral/.symlinks/plugins/file_saver/macos FlutterMacOS: :path: Flutter/ephemeral + path_provider_macos: + :path: Flutter/ephemeral/.symlinks/plugins/path_provider_macos/macos rich_clipboard_macos: :path: Flutter/ephemeral/.symlinks/plugins/rich_clipboard_macos/macos url_launcher_macos: @@ -24,7 +34,9 @@ EXTERNAL SOURCES: :path: Flutter/ephemeral/.symlinks/plugins/wakelock_macos/macos SPEC CHECKSUMS: + file_saver: 44e6fbf666677faf097302460e214e977fdd977b FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424 + path_provider_macos: 3c0c3b4b0d4a76d2bf989a913c2de869c5641a19 rich_clipboard_macos: 43364b66b9dc69d203eb8dd6d758e2d12e02723c url_launcher_macos: 597e05b8e514239626bcf4a850fcf9ef5c856ec3 wakelock_macos: bc3f2a9bd8d2e6c89fee1e1822e7ddac3bd004a9 diff --git a/frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml b/frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml index 4e2e2d68ab..593d9ff45f 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml +++ b/frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml @@ -40,6 +40,7 @@ dependencies: video_player: ^2.4.5 pod_player: 0.0.8 flutter_inappwebview: ^5.4.3+7 + file_saver: ^0.1.1 dev_dependencies: flutter_test: diff --git a/frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugin_registrant.cc b/frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugin_registrant.cc index 4f7884874d..391ef56268 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugin_registrant.cc +++ b/frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugin_registrant.cc @@ -6,9 +6,12 @@ #include "generated_plugin_registrant.h" +#include #include void RegisterPlugins(flutter::PluginRegistry* registry) { + FileSaverPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("FileSaverPlugin")); UrlLauncherWindowsRegisterWithRegistrar( registry->GetRegistrarForPlugin("UrlLauncherWindows")); } diff --git a/frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugins.cmake b/frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugins.cmake index 88b22e5c77..047111654d 100644 --- a/frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugins.cmake +++ b/frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + file_saver url_launcher_windows ) diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/node.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/document/node.dart index a0c4e33a70..909e7dd494 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/node.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/document/node.dart @@ -163,7 +163,8 @@ class Node extends ChangeNotifier with LinkedListEntry { 'type': type, }; if (children.isNotEmpty) { - map['children'] = children.map((node) => node.toJson()); + map['children'] = + (children.map((node) => node.toJson())).toList(growable: false); } if (_attributes.isNotEmpty) { map['attributes'] = _attributes; diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart index 5bf49c0048..a17b2fbf98 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart @@ -33,6 +33,12 @@ class StateTree { return StateTree(root: root); } + Map toJson() { + return { + 'document': root.toJson(), + }; + } + Node? nodeAtPath(Path path) { return root.childAtPath(path); }