Parcourir la source

feat: add default impl for image node

Vincent Chan il y a 2 ans
Parent
commit
91c62cb0ae

+ 4 - 14
frontend/app_flowy/packages/flowy_editor/example/lib/plugin/image_node_widget.dart

@@ -55,26 +55,22 @@ class _ImageNodeWidgetState extends State<ImageNodeWidget> with Selectable {
 
   @override
   Position end() {
-    // TODO: implement end
-    throw UnimplementedError();
+    return Position(path: node.path, offset: 0);
   }
 
   @override
   Position start() {
-    // TODO: implement start
-    throw UnimplementedError();
+    return Position(path: node.path, offset: 0);
   }
 
   @override
   List<Rect> getRectsInSelection(Selection selection) {
-    // TODO: implement getRectsInSelection
-    throw UnimplementedError();
+    return [];
   }
 
   @override
   Selection getSelectionInRange(Offset start, Offset end) {
-    // TODO: implement getSelectionInRange
-    throw UnimplementedError();
+    return Selection.collapsed(Position(path: node.path, offset: 0));
   }
 
   @override
@@ -82,12 +78,6 @@ class _ImageNodeWidgetState extends State<ImageNodeWidget> with Selectable {
     throw UnimplementedError();
   }
 
-  @override
-  Rect getCursorRectInPosition(Position position) {
-    // TODO: implement getCursorRectInPosition
-    throw UnimplementedError();
-  }
-
   @override
   Position getPositionInOffset(Offset start) {
     return Position(path: node.path, offset: 0);

+ 6 - 11
frontend/app_flowy/packages/flowy_editor/example/lib/plugin/youtube_link_node_widget.dart

@@ -32,7 +32,8 @@ class LinkNodeWidget extends StatefulWidget {
   State<LinkNodeWidget> createState() => _YouTubeLinkNodeWidgetState();
 }
 
-class _YouTubeLinkNodeWidgetState extends State<LinkNodeWidget> with Selectable {
+class _YouTubeLinkNodeWidgetState extends State<LinkNodeWidget>
+    with Selectable {
   Node get node => widget.node;
   EditorState get editorState => widget.editorState;
   String get src => widget.node.attributes['youtube_link'] as String;
@@ -66,12 +67,6 @@ class _YouTubeLinkNodeWidgetState extends State<LinkNodeWidget> with Selectable
     throw UnimplementedError();
   }
 
-  @override
-  Rect getCursorRectInPosition(Position position) {
-    // TODO: implement getCursorRectInPosition
-    throw UnimplementedError();
-  }
-
   @override
   Position getPositionInOffset(Offset start) {
     // TODO: implement getPositionInOffset
@@ -85,16 +80,16 @@ class _YouTubeLinkNodeWidgetState extends State<LinkNodeWidget> with Selectable
 
   late final PodPlayerController controller;
 
-@override
+  @override
   void initState() {
     controller = PodPlayerController(
       playVideoFrom: PlayVideoFrom.network(
-       src,
+        src,
       ),
     )..initialise();
-    super.initState();   
+    super.initState();
   }
-  
+
   Widget _build(BuildContext context) {
     return Column(
       children: [

+ 2 - 2
frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/default_selectable.dart

@@ -11,8 +11,8 @@ mixin DefaultSelectable {
   Position getPositionInOffset(Offset start) =>
       forward.getPositionInOffset(start);
 
-  Rect getCursorRectInPosition(Position position) =>
-      forward.getCursorRectInPosition(position).shift(baseOffset);
+  Rect? getCursorRectInPosition(Position position) =>
+      forward.getCursorRectInPosition(position)?.shift(baseOffset);
 
   List<Rect> getRectsInSelection(Selection selection) => forward
       .getRectsInSelection(selection)

+ 1 - 1
frontend/app_flowy/packages/flowy_editor/lib/render/rich_text/flowy_rich_text.dart

@@ -76,7 +76,7 @@ class _FlowyRichTextState extends State<FlowyRichText> with Selectable {
       path: widget.textNode.path, offset: widget.textNode.toRawString().length);
 
   @override
-  Rect getCursorRectInPosition(Position position) {
+  Rect? getCursorRectInPosition(Position position) {
     final textPosition = TextPosition(offset: position.offset);
     final cursorOffset =
         _renderParagraph.getOffsetForCaret(textPosition, Rect.zero);

+ 3 - 1
frontend/app_flowy/packages/flowy_editor/lib/render/selection/selectable.dart

@@ -25,7 +25,9 @@ mixin Selectable<T extends StatefulWidget> on State<T> {
     return null;
   }
 
-  Rect getCursorRectInPosition(Position position);
+  Rect? getCursorRectInPosition(Position position) {
+    return null;
+  }
 
   Offset localToGlobal(Offset offset);
 

+ 5 - 3
frontend/app_flowy/packages/flowy_editor/lib/service/input_service.dart

@@ -246,9 +246,11 @@ class _FlowyInputState extends State<FlowyInput>
       final size = renderBox.size;
       final transform = renderBox.getTransformTo(null);
       final rect = selectable.getCursorRectInPosition(selection.end);
-      _textInputConnection
-        ?..setEditableSizeAndTransform(size, transform)
-        ..setCaretRect(rect);
+      if (rect != null) {
+        _textInputConnection
+          ?..setEditableSizeAndTransform(size, transform)
+          ..setCaretRect(rect);
+      }
     }
   }
 }

+ 3 - 0
frontend/app_flowy/packages/flowy_editor/lib/service/internal_key_event_handlers/slash_handler.dart

@@ -71,6 +71,9 @@ FlowyKeyEventHandler slashShortcutHandler = (editorState, event) {
   }
 
   final rect = selectable.getCursorRectInPosition(selection.start);
+  if (rect == null) {
+    return KeyEventResult.ignored;
+  }
   final offset = selectable.localToGlobal(rect.topLeft);
   if (!selection.isCollapsed) {
     TransactionBuilder(editorState)