소스 검색

Fixes broken layer link in embeds

Anatoly Pulyaevskiy 6 년 전
부모
커밋
a834f34385

+ 16
- 36
packages/zefyr/lib/src/widgets/common.dart 파일 보기

@@ -44,7 +44,6 @@ class _RawZefyrLineState extends State<RawZefyrLine> {
44 44
   @override
45 45
   Widget build(BuildContext context) {
46 46
     final scope = ZefyrScope.of(context);
47
-
48 47
     if (scope.isEditable) {
49 48
       ensureVisible(context, scope);
50 49
     }
@@ -55,25 +54,23 @@ class _RawZefyrLineState extends State<RawZefyrLine> {
55 54
       content = buildEmbed(context, scope);
56 55
     } else {
57 56
       assert(widget.style != null);
58
-
59
-      final text = ZefyrRichText(
57
+      content = ZefyrRichText(
60 58
         node: widget.node,
61 59
         text: buildText(context),
62 60
       );
63
-      if (scope.isEditable) {
64
-        content = EditableBox(
65
-          child: text,
66
-          node: widget.node,
67
-          layerLink: _link,
68
-          renderContext: scope.renderContext,
69
-          showCursor: scope.showCursor,
70
-          selection: scope.selection,
71
-          selectionColor: theme.selectionColor,
72
-        );
73
-        content = CompositedTransformTarget(link: _link, child: content);
74
-      } else {
75
-        content = text;
76
-      }
61
+    }
62
+
63
+    if (scope.isEditable) {
64
+      content = EditableBox(
65
+        child: content,
66
+        node: widget.node,
67
+        layerLink: _link,
68
+        renderContext: scope.renderContext,
69
+        showCursor: scope.showCursor,
70
+        selection: scope.selection,
71
+        selectionColor: theme.selectionColor,
72
+      );
73
+      content = CompositedTransformTarget(link: _link, child: content);
77 74
     }
78 75
 
79 76
     if (widget.padding != null) {
@@ -143,32 +140,15 @@ class _RawZefyrLineState extends State<RawZefyrLine> {
143 140
   }
144 141
 
145 142
   Widget buildEmbed(BuildContext context, ZefyrScope scope) {
146
-    final theme = ZefyrTheme.of(context);
147
-
148 143
     EmbedNode node = widget.node.children.single;
149 144
     EmbedAttribute embed = node.style.get(NotusAttribute.embed);
150 145
 
151
-    Widget result;
152 146
     if (embed.type == EmbedType.horizontalRule) {
153
-      result = ZefyrHorizontalRule(node: node);
147
+      return ZefyrHorizontalRule(node: node);
154 148
     } else if (embed.type == EmbedType.image) {
155
-      result = ZefyrImage(node: node, delegate: scope.imageDelegate);
149
+      return ZefyrImage(node: node, delegate: scope.imageDelegate);
156 150
     } else {
157 151
       throw new UnimplementedError('Unimplemented embed type ${embed.type}');
158 152
     }
159
-
160
-    if (!scope.isEditable) {
161
-      return result;
162
-    }
163
-
164
-    return new EditableBox(
165
-      child: result,
166
-      node: widget.node,
167
-      layerLink: _link,
168
-      renderContext: scope.renderContext,
169
-      showCursor: scope.showCursor,
170
-      selection: scope.selection,
171
-      selectionColor: theme.selectionColor,
172
-    );
173 153
   }
174 154
 }

+ 1
- 0
packages/zefyr/lib/src/widgets/editable_box.dart 파일 보기

@@ -102,6 +102,7 @@ class RenderEditableProxyBox extends RenderBox
102 102
   set showCursor(ValueNotifier<bool> value) {
103 103
     assert(value != null);
104 104
     if (_showCursor == value) return;
105
+    // TODO: only mark to repaint if cursor is collapsed and inside this box.
105 106
     if (attached) _showCursor.removeListener(markNeedsPaint);
106 107
     _showCursor = value;
107 108
     if (attached) _showCursor.addListener(markNeedsPaint);

+ 6
- 0
packages/zefyr/lib/src/widgets/scope.dart 파일 보기

@@ -1,3 +1,4 @@
1
+import 'package:flutter/foundation.dart';
1 2
 import 'package:flutter/material.dart';
2 3
 import 'package:notus/notus.dart';
3 4
 
@@ -192,6 +193,11 @@ class ZefyrScope extends ChangeNotifier {
192 193
     }
193 194
     notifyListeners();
194 195
   }
196
+
197
+  @override
198
+  String toString() {
199
+    return '$ZefyrScope#${shortHash(this)}';
200
+  }
195 201
 }
196 202
 
197 203
 class ZefyrScopeAccess extends InheritedWidget {

+ 11
- 13
packages/zefyr/lib/src/widgets/selection.dart 파일 보기

@@ -10,6 +10,7 @@ import 'package:zefyr/util.dart';
10 10
 
11 11
 import 'controller.dart';
12 12
 import 'editable_box.dart';
13
+import 'render_context.dart';
13 14
 import 'scope.dart';
14 15
 
15 16
 RenderEditableBox _getEditableBox(HitTestResult result) {
@@ -232,8 +233,7 @@ class _ZefyrSelectionOverlayState extends State<ZefyrSelectionOverlay>
232 233
 
233 234
     RenderEditableProxyBox box = _getEditableBox(result);
234 235
     if (box == null) {
235
-      final scope = ZefyrScope.of(context);
236
-      box = scope.renderContext.closestBoxForGlobalPoint(globalPoint);
236
+      box = _editor.renderContext.closestBoxForGlobalPoint(globalPoint);
237 237
     }
238 238
     if (box == null) return null;
239 239
 
@@ -349,14 +349,13 @@ class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
349 349
 
350 350
   @override
351 351
   Widget build(BuildContext context) {
352
-    final scope = ZefyrScope.of(context);
353 352
     if (selection == null ||
354 353
         selection.isCollapsed ||
355 354
         widget.controls == null ||
356
-        scope.focusOwner != FocusOwner.editor) {
355
+        _scope.focusOwner != FocusOwner.editor) {
357 356
       return new Container();
358 357
     }
359
-    final block = scope.renderContext.boxForTextOffset(documentOffset);
358
+    final block = _scope.renderContext.boxForTextOffset(documentOffset);
360 359
     final position = getPosition(block);
361 360
     Widget handle;
362 361
     if (position == null) {
@@ -400,9 +399,11 @@ class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
400 399
   Offset _dragPosition;
401 400
 
402 401
   void _handleScopeChange() {
403
-    setState(() {
404
-      _selection = _scope.selection;
405
-    });
402
+    if (_selection != _scope.selection) {
403
+      setState(() {
404
+        _selection = _scope.selection;
405
+      });
406
+    }
406 407
   }
407 408
 
408 409
   void _handleDragStart(DragStartDetails details) {
@@ -412,8 +413,7 @@ class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
412 413
   void _handleDragUpdate(DragUpdateDetails details) {
413 414
     _dragPosition += details.delta;
414 415
     final globalPoint = _dragPosition;
415
-    final scope = ZefyrScope.of(context);
416
-    final paragraph = scope.renderContext.boxForGlobalPoint(globalPoint);
416
+    final paragraph = _scope.renderContext.boxForGlobalPoint(globalPoint);
417 417
     if (paragraph == null) {
418 418
       return;
419 419
     }
@@ -430,7 +430,7 @@ class _SelectionHandleDriverState extends State<SelectionHandleDriver> {
430 430
     }
431 431
 
432 432
     if (newSelection != _selection) {
433
-      scope.updateSelection(newSelection, source: ChangeSource.local);
433
+      _scope.updateSelection(newSelection, source: ChangeSource.local);
434 434
     }
435 435
   }
436 436
 }
@@ -468,7 +468,6 @@ class _SelectionToolbarState extends State<_SelectionToolbar> {
468 468
       return Container();
469 469
     }
470 470
     final boxes = block.getEndpointsForSelection(selection);
471
-
472 471
     // Find the horizontal midpoint, just above the selected text.
473 472
     final Offset midpoint = new Offset(
474 473
       (boxes.length == 1)
@@ -481,7 +480,6 @@ class _SelectionToolbarState extends State<_SelectionToolbar> {
481 480
       block.localToGlobal(Offset.zero),
482 481
       block.localToGlobal(block.size.bottomRight(Offset.zero)),
483 482
     );
484
-
485 483
     final toolbar = widget.controls
486 484
         .buildToolbar(context, editingRegion, midpoint, widget.delegate);
487 485
     return new CompositedTransformFollower(