Browse Source

feat: implemnt export editor state

Lucas.Xu 2 years ago
parent
commit
778b55d44e

+ 25 - 0
frontend/app_flowy/packages/appflowy_editor/example/lib/main.dart

@@ -1,4 +1,5 @@
 import 'dart:convert';
 import 'dart:convert';
+import 'dart:io';
 
 
 import 'package:flutter/material.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 import 'package:flutter/services.dart';
@@ -8,6 +9,7 @@ import 'expandable_floating_action_button.dart';
 import 'plugin/youtube_link_node_widget.dart';
 import 'plugin/youtube_link_node_widget.dart';
 
 
 import 'package:appflowy_editor/appflowy_editor.dart';
 import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:path_provider/path_provider.dart';
 
 
 void main() {
 void main() {
   runApp(const MyApp());
   runApp(const MyApp());
@@ -58,6 +60,7 @@ class MyHomePage extends StatefulWidget {
 class _MyHomePageState extends State<MyHomePage> {
 class _MyHomePageState extends State<MyHomePage> {
   final editorKey = GlobalKey();
   final editorKey = GlobalKey();
   int page = 0;
   int page = 0;
+  EditorState? _editorState;
 
 
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
@@ -103,6 +106,7 @@ class _MyHomePageState extends State<MyHomePage> {
             ..handler = (message) {
             ..handler = (message) {
               debugPrint(message);
               debugPrint(message);
             };
             };
+          _editorState = editorState;
           return _buildAppFlowyEditor(editorState);
           return _buildAppFlowyEditor(editorState);
         } else {
         } else {
           return const Center(
           return const Center(
@@ -146,6 +150,21 @@ class _MyHomePageState extends State<MyHomePage> {
     );
     );
   }
   }
 
 
+  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() {
   Widget _buildExpandableFab() {
     return ExpandableFab(
     return ExpandableFab(
       distance: 112.0,
       distance: 112.0,
@@ -177,6 +196,12 @@ class _MyHomePageState extends State<MyHomePage> {
           },
           },
           icon: const Icon(Icons.text_fields),
           icon: const Icon(Icons.text_fields),
         ),
         ),
+        ActionButton(
+          onPressed: () {
+            _exportDocument(_editorState!);
+          },
+          icon: const Icon(Icons.print),
+        ),
       ],
       ],
     );
     );
   }
   }

+ 4 - 0
frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugin_registrant.cc

@@ -6,10 +6,14 @@
 
 
 #include "generated_plugin_registrant.h"
 #include "generated_plugin_registrant.h"
 
 
+#include <file_saver/file_saver_plugin.h>
 #include <rich_clipboard_linux/rich_clipboard_plugin.h>
 #include <rich_clipboard_linux/rich_clipboard_plugin.h>
 #include <url_launcher_linux/url_launcher_plugin.h>
 #include <url_launcher_linux/url_launcher_plugin.h>
 
 
 void fl_register_plugins(FlPluginRegistry* registry) {
 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 =
   g_autoptr(FlPluginRegistrar) rich_clipboard_linux_registrar =
       fl_plugin_registry_get_registrar_for_plugin(registry, "RichClipboardPlugin");
       fl_plugin_registry_get_registrar_for_plugin(registry, "RichClipboardPlugin");
   rich_clipboard_plugin_register_with_registrar(rich_clipboard_linux_registrar);
   rich_clipboard_plugin_register_with_registrar(rich_clipboard_linux_registrar);

+ 1 - 0
frontend/app_flowy/packages/appflowy_editor/example/linux/flutter/generated_plugins.cmake

@@ -3,6 +3,7 @@
 #
 #
 
 
 list(APPEND FLUTTER_PLUGIN_LIST
 list(APPEND FLUTTER_PLUGIN_LIST
+  file_saver
   rich_clipboard_linux
   rich_clipboard_linux
   url_launcher_linux
   url_launcher_linux
 )
 )

+ 4 - 0
frontend/app_flowy/packages/appflowy_editor/example/macos/Flutter/GeneratedPluginRegistrant.swift

@@ -5,11 +5,15 @@
 import FlutterMacOS
 import FlutterMacOS
 import Foundation
 import Foundation
 
 
+import file_saver
+import path_provider_macos
 import rich_clipboard_macos
 import rich_clipboard_macos
 import url_launcher_macos
 import url_launcher_macos
 import wakelock_macos
 import wakelock_macos
 
 
 func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
 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"))
   RichClipboardPlugin.register(with: registry.registrar(forPlugin: "RichClipboardPlugin"))
   UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
   UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
   WakelockMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockMacosPlugin"))
   WakelockMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockMacosPlugin"))

+ 12 - 0
frontend/app_flowy/packages/appflowy_editor/example/macos/Podfile.lock

@@ -1,5 +1,9 @@
 PODS:
 PODS:
+  - file_saver (0.0.1):
+    - FlutterMacOS
   - FlutterMacOS (1.0.0)
   - FlutterMacOS (1.0.0)
+  - path_provider_macos (0.0.1):
+    - FlutterMacOS
   - rich_clipboard_macos (0.0.1):
   - rich_clipboard_macos (0.0.1):
     - FlutterMacOS
     - FlutterMacOS
   - url_launcher_macos (0.0.1):
   - url_launcher_macos (0.0.1):
@@ -8,14 +12,20 @@ PODS:
     - FlutterMacOS
     - FlutterMacOS
 
 
 DEPENDENCIES:
 DEPENDENCIES:
+  - file_saver (from `Flutter/ephemeral/.symlinks/plugins/file_saver/macos`)
   - FlutterMacOS (from `Flutter/ephemeral`)
   - 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`)
   - rich_clipboard_macos (from `Flutter/ephemeral/.symlinks/plugins/rich_clipboard_macos/macos`)
   - url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)
   - url_launcher_macos (from `Flutter/ephemeral/.symlinks/plugins/url_launcher_macos/macos`)
   - wakelock_macos (from `Flutter/ephemeral/.symlinks/plugins/wakelock_macos/macos`)
   - wakelock_macos (from `Flutter/ephemeral/.symlinks/plugins/wakelock_macos/macos`)
 
 
 EXTERNAL SOURCES:
 EXTERNAL SOURCES:
+  file_saver:
+    :path: Flutter/ephemeral/.symlinks/plugins/file_saver/macos
   FlutterMacOS:
   FlutterMacOS:
     :path: Flutter/ephemeral
     :path: Flutter/ephemeral
+  path_provider_macos:
+    :path: Flutter/ephemeral/.symlinks/plugins/path_provider_macos/macos
   rich_clipboard_macos:
   rich_clipboard_macos:
     :path: Flutter/ephemeral/.symlinks/plugins/rich_clipboard_macos/macos
     :path: Flutter/ephemeral/.symlinks/plugins/rich_clipboard_macos/macos
   url_launcher_macos:
   url_launcher_macos:
@@ -24,7 +34,9 @@ EXTERNAL SOURCES:
     :path: Flutter/ephemeral/.symlinks/plugins/wakelock_macos/macos
     :path: Flutter/ephemeral/.symlinks/plugins/wakelock_macos/macos
 
 
 SPEC CHECKSUMS:
 SPEC CHECKSUMS:
+  file_saver: 44e6fbf666677faf097302460e214e977fdd977b
   FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
   FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
+  path_provider_macos: 3c0c3b4b0d4a76d2bf989a913c2de869c5641a19
   rich_clipboard_macos: 43364b66b9dc69d203eb8dd6d758e2d12e02723c
   rich_clipboard_macos: 43364b66b9dc69d203eb8dd6d758e2d12e02723c
   url_launcher_macos: 597e05b8e514239626bcf4a850fcf9ef5c856ec3
   url_launcher_macos: 597e05b8e514239626bcf4a850fcf9ef5c856ec3
   wakelock_macos: bc3f2a9bd8d2e6c89fee1e1822e7ddac3bd004a9
   wakelock_macos: bc3f2a9bd8d2e6c89fee1e1822e7ddac3bd004a9

+ 1 - 0
frontend/app_flowy/packages/appflowy_editor/example/pubspec.yaml

@@ -40,6 +40,7 @@ dependencies:
   video_player: ^2.4.5
   video_player: ^2.4.5
   pod_player: 0.0.8
   pod_player: 0.0.8
   flutter_inappwebview: ^5.4.3+7
   flutter_inappwebview: ^5.4.3+7
+  file_saver: ^0.1.1
 
 
 dev_dependencies:
 dev_dependencies:
   flutter_test:
   flutter_test:

+ 3 - 0
frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugin_registrant.cc

@@ -6,9 +6,12 @@
 
 
 #include "generated_plugin_registrant.h"
 #include "generated_plugin_registrant.h"
 
 
+#include <file_saver/file_saver_plugin.h>
 #include <url_launcher_windows/url_launcher_windows.h>
 #include <url_launcher_windows/url_launcher_windows.h>
 
 
 void RegisterPlugins(flutter::PluginRegistry* registry) {
 void RegisterPlugins(flutter::PluginRegistry* registry) {
+  FileSaverPluginRegisterWithRegistrar(
+      registry->GetRegistrarForPlugin("FileSaverPlugin"));
   UrlLauncherWindowsRegisterWithRegistrar(
   UrlLauncherWindowsRegisterWithRegistrar(
       registry->GetRegistrarForPlugin("UrlLauncherWindows"));
       registry->GetRegistrarForPlugin("UrlLauncherWindows"));
 }
 }

+ 1 - 0
frontend/app_flowy/packages/appflowy_editor/example/windows/flutter/generated_plugins.cmake

@@ -3,6 +3,7 @@
 #
 #
 
 
 list(APPEND FLUTTER_PLUGIN_LIST
 list(APPEND FLUTTER_PLUGIN_LIST
+  file_saver
   url_launcher_windows
   url_launcher_windows
 )
 )
 
 

+ 2 - 1
frontend/app_flowy/packages/appflowy_editor/lib/src/document/node.dart

@@ -163,7 +163,8 @@ class Node extends ChangeNotifier with LinkedListEntry<Node> {
       'type': type,
       'type': type,
     };
     };
     if (children.isNotEmpty) {
     if (children.isNotEmpty) {
-      map['children'] = children.map((node) => node.toJson());
+      map['children'] =
+          (children.map((node) => node.toJson())).toList(growable: false);
     }
     }
     if (_attributes.isNotEmpty) {
     if (_attributes.isNotEmpty) {
       map['attributes'] = _attributes;
       map['attributes'] = _attributes;

+ 6 - 0
frontend/app_flowy/packages/appflowy_editor/lib/src/document/state_tree.dart

@@ -33,6 +33,12 @@ class StateTree {
     return StateTree(root: root);
     return StateTree(root: root);
   }
   }
 
 
+  Map<String, Object> toJson() {
+    return {
+      'document': root.toJson(),
+    };
+  }
+
   Node? nodeAtPath(Path path) {
   Node? nodeAtPath(Path path) {
     return root.childAtPath(path);
     return root.childAtPath(path);
   }
   }